mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 01:20:25 +00:00
Userland: Use Core::ArgsParser for 'chgrp'
This commit is contained in:
parent
c8d690d840
commit
14db94f44b
Notes:
sideshowbarker
2024-07-19 04:11:20 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/14db94f44be Pull-request: https://github.com/SerenityOS/serenity/pull/3019
1 changed files with 13 additions and 11 deletions
|
@ -26,6 +26,7 @@
|
|||
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <grp.h>
|
||||
#include <pwd.h>
|
||||
#include <stdio.h>
|
||||
|
@ -40,33 +41,34 @@ int main(int argc, char** argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
if (argc < 2) {
|
||||
printf("usage: chgrp <gid> <path>\n");
|
||||
return 0;
|
||||
}
|
||||
const char* gid_arg = nullptr;
|
||||
const char* path = nullptr;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(gid_arg, "Group ID", "gid");
|
||||
args_parser.add_positional_argument(path, "Path to file", "path");
|
||||
args_parser.parse(argc, argv);
|
||||
|
||||
gid_t new_gid = -1;
|
||||
auto gid_arg = String(argv[1]);
|
||||
|
||||
if (gid_arg.is_empty()) {
|
||||
if (String(gid_arg).is_empty()) {
|
||||
fprintf(stderr, "Empty gid option\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto number = gid_arg.to_uint();
|
||||
|
||||
auto number = String(gid_arg).to_uint();
|
||||
if (number.has_value()) {
|
||||
new_gid = number.value();
|
||||
} else {
|
||||
auto* group = getgrnam(gid_arg.characters());
|
||||
auto* group = getgrnam(gid_arg);
|
||||
if (!group) {
|
||||
fprintf(stderr, "Unknown group '%s'\n", gid_arg.characters());
|
||||
fprintf(stderr, "Unknown group '%s'\n", gid_arg);
|
||||
return 1;
|
||||
}
|
||||
new_gid = group->gr_gid;
|
||||
}
|
||||
|
||||
int rc = chown(argv[2], -1, new_gid);
|
||||
int rc = chown(path, -1, new_gid);
|
||||
if (rc < 0) {
|
||||
perror("chgrp");
|
||||
return 1;
|
||||
|
|
Loading…
Reference in a new issue