1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 18:10:56 +09:00

Userland: Add the chgrp command

The chgrp command allows the user to easily modify a file's group while
leaving its owner unchanged.
This commit is contained in:
0xtechnobabble 2020-01-12 13:00:14 +02:00 committed by Andreas Kling
parent 954daaa916
commit f501014fae
Notes: sideshowbarker 2024-07-19 10:08:19 +09:00

48
Userland/chgrp.cpp Normal file
View file

@ -0,0 +1,48 @@
#include <AK/String.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char** argv)
{
if (pledge("stdio rpath chown", nullptr) < 0) {
perror("pledge");
return 1;
}
if (argc < 2) {
printf("usage: chgrp <gid> <path>\n");
return 0;
}
gid_t new_gid = -1;
auto gid_arg = String(argv[1]);
if (gid_arg.is_empty()) {
fprintf(stderr, "Empty gid option\n");
return 1;
}
bool ok;
new_gid = gid_arg.to_uint(ok);
if (!ok) {
new_gid = getgrnam(gid_arg.characters())->gr_gid;
if(!new_gid) {
fprintf(stderr, "Invalid gid: '%s'\n", gid_arg.characters());
return 1;
}
}
int rc = chown(argv[2], -1, new_gid);
if (rc < 0) {
perror("chgrp");
return 1;
}
return 0;
}