1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:47:45 +00:00

Kernel+Userland: Implement fchmod() syscall and use it to improve /bin/cp.

/bin/cp will now copy the permission bits from source to destination. :^)
This commit is contained in:
Andreas Kling 2019-03-01 10:39:19 +01:00
parent b5e5f26a82
commit 1b16a29044
10 changed files with 54 additions and 14 deletions

View file

@ -2,6 +2,7 @@
#include <fcntl.h>
#include <assert.h>
#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char** argv)
{
@ -9,6 +10,7 @@ int main(int argc, char** argv)
printf("usage: cp <source> <destination>\n");
return 0;
}
int src_fd = open(argv[1], O_RDONLY);
if (src_fd < 0) {
perror("open src");
@ -20,6 +22,13 @@ int main(int argc, char** argv)
return 1;
}
struct stat src_stat;
int rc = fstat(src_fd, &src_stat);
if (rc < 0) {
perror("stat src");
return 1;
}
for (;;) {
char buffer[BUFSIZ];
ssize_t nread = read(src_fd, buffer, sizeof(buffer));
@ -42,6 +51,13 @@ int main(int argc, char** argv)
bufptr += nwritten;
}
}
rc = fchmod(dst_fd, src_stat.st_mode);
if (rc < 0) {
perror("fchmod dst");
return 1;
}
close(src_fd);
close(dst_fd);
return 0;