1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 10:28:13 +00:00

Userland: Add named gid/uid args parsing

This patch makes it so that if a user provides a groupname/username
instead of an id, chown will automatically convert it to a gid/uid
using getgrnam() or getpwnam() respectively.
This commit is contained in:
0xtechnobabble 2020-01-12 12:13:54 +02:00 committed by Andreas Kling
parent df7b81bdf5
commit 954daaa916

View file

@ -1,4 +1,6 @@
#include <AK/String.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
@ -19,17 +21,27 @@ int main(int argc, char** argv)
fprintf(stderr, "Empty uid/gid spec\n");
return 1;
}
bool ok;
new_uid = parts[0].to_uint(ok);
if (!ok) {
fprintf(stderr, "Invalid uid: '%s'\n", parts[0].characters());
return 1;
new_uid = getpwnam(parts[0].characters())->pw_uid;
if (!new_uid) {
fprintf(stderr, "Invalid uid: '%s'\n", parts[0].characters());
return 1;
}
}
if (parts.size() == 2) {
new_gid = parts[1].to_uint(ok);
if (!ok) {
fprintf(stderr, "Invalid gid: '%s'\n", parts[1].characters());
return 1;
new_gid = getgrnam(parts[1].characters())->gr_gid;
if(!new_gid) {
fprintf(stderr, "Invalid gid: '%s'\n", parts[1].characters());
return 1;
}
}
}