From f501014fae4eb3d0a05ccc21683ed18172e86ff7 Mon Sep 17 00:00:00 2001 From: 0xtechnobabble <0xtechnobabble@protonmail.com> Date: Sun, 12 Jan 2020 13:00:14 +0200 Subject: [PATCH] Userland: Add the chgrp command The chgrp command allows the user to easily modify a file's group while leaving its owner unchanged. --- Userland/chgrp.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Userland/chgrp.cpp diff --git a/Userland/chgrp.cpp b/Userland/chgrp.cpp new file mode 100644 index 0000000000..1afa03ee7f --- /dev/null +++ b/Userland/chgrp.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + if (pledge("stdio rpath chown", nullptr) < 0) { + perror("pledge"); + return 1; + } + + if (argc < 2) { + printf("usage: chgrp \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; +}