mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 09:51:57 -05:00
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:
parent
500098c76d
commit
16aeb8b51d
1 changed files with 9 additions and 10 deletions
|
@ -12,7 +12,6 @@
|
|||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void print_usage_and_exit()
|
||||
|
@ -27,9 +26,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
TRY(Core::System::pledge("stdio proc"));
|
||||
|
||||
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) {
|
||||
if (i && !(i % 5))
|
||||
outln("");
|
||||
|
@ -45,29 +44,29 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
int pid_argi = 1;
|
||||
if (argc == 3) {
|
||||
pid_argi = 2;
|
||||
if (argv[1][0] != '-')
|
||||
if (strings[1][0] != '-')
|
||||
print_usage_and_exit();
|
||||
|
||||
Optional<unsigned> number;
|
||||
|
||||
if (isalpha(argv[1][1])) {
|
||||
int value = getsignalbyname(&argv[1][1]);
|
||||
if (isalpha(strings[1][1])) {
|
||||
int value = getsignalbyname(&strings[1][1]);
|
||||
if (value >= 0 && value < NSIG)
|
||||
number = 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()) {
|
||||
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;
|
||||
}
|
||||
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()) {
|
||||
warnln("'{}' is not a valid PID", argv[pid_argi]);
|
||||
warnln("'{}' is not a valid PID", strings[pid_argi]);
|
||||
return 3;
|
||||
}
|
||||
pid_t pid = pid_opt.value();
|
||||
|
|
Loading…
Add table
Reference in a new issue