kill: Parse CLI arguments using Arguments.strings

The previous implementation used argv and LibC to parse arguments,
it now uses the Arguments.strings interface to do so.
This commit is contained in:
itskarudo 2022-02-12 11:08:44 +01:00 committed by Tim Flynn
parent 500098c76d
commit 16aeb8b51d
Notes: sideshowbarker 2024-07-17 18:58:09 +09:00

View file

@ -12,7 +12,6 @@
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
static void print_usage_and_exit() static void print_usage_and_exit()
@ -27,9 +26,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(Core::System::pledge("stdio proc")); TRY(Core::System::pledge("stdio proc"));
int argc = arguments.argc; int argc = arguments.argc;
char** argv = arguments.argv; auto strings = arguments.strings;
if (argc == 2 && !strcmp(argv[1], "-l")) { if (argc == 2 && strings[1] == "-l") {
for (size_t i = 0; i < NSIG; ++i) { for (size_t i = 0; i < NSIG; ++i) {
if (i && !(i % 5)) if (i && !(i % 5))
outln(""); outln("");
@ -45,29 +44,29 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
int pid_argi = 1; int pid_argi = 1;
if (argc == 3) { if (argc == 3) {
pid_argi = 2; pid_argi = 2;
if (argv[1][0] != '-') if (strings[1][0] != '-')
print_usage_and_exit(); print_usage_and_exit();
Optional<unsigned> number; Optional<unsigned> number;
if (isalpha(argv[1][1])) { if (isalpha(strings[1][1])) {
int value = getsignalbyname(&argv[1][1]); int value = getsignalbyname(&strings[1][1]);
if (value >= 0 && value < NSIG) if (value >= 0 && value < NSIG)
number = value; number = value;
} }
if (!number.has_value()) if (!number.has_value())
number = StringView(&argv[1][1]).to_uint(); number = strings[1].substring_view(1, 1).to_uint();
if (!number.has_value()) { if (!number.has_value()) {
warnln("'{}' is not a valid signal name or number", &argv[1][1]); warnln("'{}' is not a valid signal name or number", &strings[1][1]);
return 2; return 2;
} }
signum = number.value(); signum = number.value();
} }
auto pid_opt = String(argv[pid_argi]).to_int(); auto pid_opt = strings[pid_argi].to_int();
if (!pid_opt.has_value()) { if (!pid_opt.has_value()) {
warnln("'{}' is not a valid PID", argv[pid_argi]); warnln("'{}' is not a valid PID", strings[pid_argi]);
return 3; return 3;
} }
pid_t pid = pid_opt.value(); pid_t pid = pid_opt.value();