diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2023-05-11 13:15:01 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-05-11 16:33:18 +0200 |
commit | 23dd16febeef232a9bb5fd6637a364987b822926 (patch) | |
tree | c285903b8b6c6aaa465b7c036b05d7c752bf86ac /Userland/Utilities/errno.cpp | |
parent | a22a6c371e03c4435cbd53cd95a528a27f648462 (diff) | |
download | serenity-23dd16febeef232a9bb5fd6637a364987b822926.zip |
errno: Look up errors by name as well as number
eg, `errno enotsup` now shows the description and number for ENOTSUP.
Diffstat (limited to 'Userland/Utilities/errno.cpp')
-rw-r--r-- | Userland/Utilities/errno.cpp | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/Userland/Utilities/errno.cpp b/Userland/Utilities/errno.cpp index dacf548b9d..b9116614c0 100644 --- a/Userland/Utilities/errno.cpp +++ b/Userland/Utilities/errno.cpp @@ -22,7 +22,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) StringView keyword; Core::ArgsParser args_parser; - args_parser.add_positional_argument(keyword, "Error number or string to search", "keyword", Core::ArgsParser::Required::No); + args_parser.add_positional_argument(keyword, "Error number or name to look up", "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'); args_parser.parse(arguments); @@ -54,19 +54,24 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) return 0; } - auto maybe_error_code = keyword.to_int(); - if (!maybe_error_code.has_value()) { - warnln("ERROR: Not understood: {}", keyword); - return 1; + if (auto maybe_error_code = keyword.to_int(); maybe_error_code.has_value()) { + auto error_code = maybe_error_code.value(); + auto error = strerror(error_code); + if (error == "Unknown error"sv) { + warnln("ERROR: Unknown errno: {}", keyword); + return 1; + } + output_errno_description(error_code, error); + return 0; } - auto error_code = maybe_error_code.value(); - auto error = strerror(error_code); - if (error == "Unknown error"sv) { - warnln("ERROR: Unknown errno: {}", keyword); - return 1; + for (int i = 0; i < sys_nerr; i++) { + if (keyword.equals_ignoring_ascii_case(s_errno_names[i])) { + output_errno_description(i, strerror(i)); + return 0; + } } - output_errno_description(error_code, error); - return 0; + warnln("ERROR: Not understood: {}", keyword); + return 1; } |