ladybird/Userland/Utilities/errno.cpp

58 lines
1.5 KiB
C++
Raw Normal View History

2021-05-16 01:00:09 +03:00
/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/ArgsParser.h>
2021-12-21 21:08:23 +03:00
#include <LibMain/Main.h>
#include <string.h>
2021-05-16 01:00:09 +03:00
2021-12-21 21:08:23 +03:00
ErrorOr<int> serenity_main(Main::Arguments arguments)
2021-05-16 01:00:09 +03:00
{
bool list = false;
bool search = false;
2021-12-21 21:08:23 +03:00
StringView keyword;
2021-05-16 01:00:09 +03:00
Core::ArgsParser args_parser;
args_parser.add_positional_argument(keyword, "Error number or string to search", "keyword", Core::ArgsParser::Required::No);
args_parser.add_option(list, "List all errno values", "list", 'l');
args_parser.add_option(search, "Search for error descriptions containing keyword", "search", 's');
2021-12-21 21:08:23 +03:00
args_parser.parse(arguments);
2021-05-16 01:00:09 +03:00
if (list) {
for (int i = 0; i < sys_nerr; i++) {
outln("{} {}", i, strerror(i));
}
return 0;
}
2021-12-21 21:08:23 +03:00
if (keyword.is_empty())
2021-05-16 01:00:09 +03:00
return 0;
if (search) {
for (int i = 0; i < sys_nerr; i++) {
auto error = DeprecatedString::formatted("{}", strerror(i));
2021-05-16 01:00:09 +03:00
if (error.contains(keyword, CaseSensitivity::CaseInsensitive)) {
outln("{} {}", i, error);
}
}
return 0;
}
2021-12-21 21:08:23 +03:00
auto maybe_errno = keyword.to_int();
2021-05-16 01:00:09 +03:00
if (!maybe_errno.has_value()) {
warnln("ERROR: Not understood: {}", keyword);
return 1;
}
auto error = DeprecatedString::formatted("{}", strerror(maybe_errno.value()));
2021-12-21 21:08:23 +03:00
if (error == "Unknown error"sv) {
2021-05-16 01:00:09 +03:00
warnln("ERROR: Unknown errno: {}", keyword);
return 1;
}
outln("{} {}", keyword, error);
return 0;
}