1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 08:47:44 +00:00

Add chown() syscall and a simple /bin/chown program.

This commit is contained in:
Andreas Kling 2019-02-27 12:32:53 +01:00
parent 711e2b2651
commit 1d2529b4a1
19 changed files with 130 additions and 5 deletions

1
Userland/.gitignore vendored
View file

@ -35,3 +35,4 @@ ln
df
su
env
chown

View file

@ -26,6 +26,7 @@ OBJS = \
rmdir.o \
dmesg.o \
chmod.o \
chown.o \
top.o \
df.o \
ln.o \
@ -62,6 +63,7 @@ APPS = \
rmdir \
dmesg \
chmod \
chown \
top \
ln \
df \
@ -173,6 +175,9 @@ rmdir: rmdir.o
chmod: chmod.o
$(LD) -o $@ $(LDFLAGS) $< -lc
chown: chown.o
$(LD) -o $@ $(LDFLAGS) $< -lc
top: top.o
$(LD) -o $@ $(LDFLAGS) $< -lc

43
Userland/chown.cpp Normal file
View file

@ -0,0 +1,43 @@
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <AK/AKString.h>
int main(int argc, char **argv)
{
if (argc < 2) {
printf("usage: chown <uid[:gid]> <path>\n");
return 0;
}
uid_t new_uid = -1;
gid_t new_gid = -1;
auto parts = String(argv[1]).split(':');
if (parts.is_empty()) {
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;
}
if (parts.size() == 2) {
new_gid = parts[1].to_uint(ok);
if (!ok) {
fprintf(stderr, "Invalid gid: '%s'\n", parts[1].characters());
return 1;
}
}
int rc = chown(argv[2], new_uid, new_gid);
if (rc < 0) {
perror("chown");
return 1;
}
return 0;
}