mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:57:47 +00: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:
parent
954daaa916
commit
f501014fae
1 changed files with 48 additions and 0 deletions
48
Userland/chgrp.cpp
Normal file
48
Userland/chgrp.cpp
Normal 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;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue