summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2023-05-11 12:54:47 +0100
committerAndreas Kling <kling@serenityos.org>2023-05-11 16:33:18 +0200
commita22a6c371e03c4435cbd53cd95a528a27f648462 (patch)
treeef42333d7d9780215f6f22a6dd7e3b93ac2868ae
parentb98252728efaece9cdf8fb939cc98aa4c7ea59cf (diff)
downloadserenity-a22a6c371e03c4435cbd53cd95a528a27f648462.zip
errno: Include error code names in output
And align the output nicely while we're at it. :^)
-rw-r--r--Userland/Utilities/errno.cpp35
1 files changed, 25 insertions, 10 deletions
diff --git a/Userland/Utilities/errno.cpp b/Userland/Utilities/errno.cpp
index ddf19e2db6..dacf548b9d 100644
--- a/Userland/Utilities/errno.cpp
+++ b/Userland/Utilities/errno.cpp
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, the SerenityOS developers.
+ * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -8,6 +9,12 @@
#include <LibMain/Main.h>
#include <string.h>
+Array<StringView, EMAXERRNO + 1> s_errno_names = {
+#define __ENUMERATE_ERRNO_CODE(c, s) #c##sv,
+ ENUMERATE_ERRNO_CODES(__ENUMERATE_ERRNO_CODE)
+#undef __ENUMERATE_ERRNO_CODE
+};
+
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
bool list = false;
@@ -20,10 +27,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.add_option(search, "Search for error descriptions containing keyword", "search", 's');
args_parser.parse(arguments);
+ auto max_name_length = 0;
+ for (int i = 0; i < sys_nerr; i++) {
+ max_name_length = max(max_name_length, s_errno_names[i].length());
+ }
+
+ auto output_errno_description = [max_name_length](int errno_code, auto error_string) {
+ outln("{:{}} {:>2} {}", s_errno_names[errno_code], max_name_length, errno_code, error_string);
+ };
+
if (list) {
- for (int i = 0; i < sys_nerr; i++) {
- outln("{} {}", i, strerror(i));
- }
+ for (int i = 0; i < sys_nerr; i++)
+ output_errno_description(i, strerror(i));
return 0;
}
@@ -33,25 +48,25 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (search) {
for (int i = 0; i < sys_nerr; i++) {
auto error = DeprecatedString::formatted("{}", strerror(i));
- if (error.contains(keyword, CaseSensitivity::CaseInsensitive)) {
- outln("{} {}", i, error);
- }
+ if (error.contains(keyword, CaseSensitivity::CaseInsensitive))
+ output_errno_description(i, error);
}
return 0;
}
- auto maybe_errno = keyword.to_int();
- if (!maybe_errno.has_value()) {
+ auto maybe_error_code = keyword.to_int();
+ if (!maybe_error_code.has_value()) {
warnln("ERROR: Not understood: {}", keyword);
return 1;
}
+ auto error_code = maybe_error_code.value();
- auto error = DeprecatedString::formatted("{}", strerror(maybe_errno.value()));
+ auto error = strerror(error_code);
if (error == "Unknown error"sv) {
warnln("ERROR: Unknown errno: {}", keyword);
return 1;
}
- outln("{} {}", keyword, error);
+ output_errno_description(error_code, error);
return 0;
}