1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:34:59 +00:00

errno: Include error code names in output

And align the output nicely while we're at it. :^)
This commit is contained in:
Sam Atkins 2023-05-11 12:54:47 +01:00 committed by Andreas Kling
parent b98252728e
commit a22a6c371e

View file

@ -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;
}