mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 09:47:35 +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:
parent
df7b81bdf5
commit
954daaa916
1 changed files with 16 additions and 4 deletions
|
@ -1,4 +1,6 @@
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
|
#include <grp.h>
|
||||||
|
#include <pwd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
@ -19,17 +21,27 @@ int main(int argc, char** argv)
|
||||||
fprintf(stderr, "Empty uid/gid spec\n");
|
fprintf(stderr, "Empty uid/gid spec\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ok;
|
bool ok;
|
||||||
new_uid = parts[0].to_uint(ok);
|
new_uid = parts[0].to_uint(ok);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
fprintf(stderr, "Invalid uid: '%s'\n", parts[0].characters());
|
new_uid = getpwnam(parts[0].characters())->pw_uid;
|
||||||
return 1;
|
|
||||||
|
if (!new_uid) {
|
||||||
|
fprintf(stderr, "Invalid uid: '%s'\n", parts[0].characters());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parts.size() == 2) {
|
if (parts.size() == 2) {
|
||||||
new_gid = parts[1].to_uint(ok);
|
new_gid = parts[1].to_uint(ok);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
fprintf(stderr, "Invalid gid: '%s'\n", parts[1].characters());
|
new_gid = getgrnam(parts[1].characters())->gr_gid;
|
||||||
return 1;
|
|
||||||
|
if(!new_gid) {
|
||||||
|
fprintf(stderr, "Invalid gid: '%s'\n", parts[1].characters());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue