diff options
| Commit | e52ee8a3f702e82df6a686c03cdc0b2366d0770a (patch) | |
|---|---|---|
| Author | 2026-10-31 11:05:53 +0200 | |
| Committer | 2026-10-31 11:10:51 +0200 | |
| Files | 54c61a6de1c32ce2e6780701a0cafd3afd077757 /chwrite | |
| cli-e52ee8a3f702e82df6a686c03cdc0b2366d0770a.tar cli-e52ee8a3f702e82df6a686c03cdc0b2366d0770a.tar.gz cli-e52ee8a3f702e82df6a686c03cdc0b2366d0770a.zip | ||
| Parent | f522fb4f17114d48e2de5b60a9b34dea350e77cb (diff) | |
feat: Implement chwrite command
A narrowed down version of chmod. Recursion must be done in-script.
2 files changed, 63 insertions, 0 deletions
| -rw-r--r-- | chwrite/Makefile | 8 | ||||
| -rw-r--r-- | chwrite/chwrite.c | 55 |
diff --git a/chwrite/Makefile b/chwrite/Makefile new file mode 100644 index 0000000..c9e0b66 --- /dev/null +++ b/chwrite/Makefile @@ -0,0 +1,8 @@ +CFLAGS = -std=c89 -Wall -O2 +CFLAGS += -D_XOPEN_SOURCE=500 + +all: chwrite + +chwrite: chwrite.o + +.PHONY: all diff --git a/chwrite/chwrite.c b/chwrite/chwrite.c new file mode 100644 index 0000000..6be8ec8 --- /dev/null +++ b/chwrite/chwrite.c @@ -0,0 +1,55 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> + +static void +help_message(void) { + printf( + "chwrite [-h|--help]\n" + "chwrite +|+w|-|-w FILE...\n" + "\n" + "Change write permissions for file(s)\n" + ); +} + +int +main(int argc, char *argv[]) { + int i = 0, f = 0, result = 0; + struct stat fs = { 0, }; + char operation; + + if (argc <= 1) { + help_message(); + return 0; + } + + operation = argv[1][0]; + + if (operation != '+' && operation != '-') { + help_message(); + return 1; + } + + for (i = 2; i < argc; ++i) { + f = open(argv[i], 0); + if (f == -1) + return errno; + + result = fstat(f, &fs); + if (result == -1) + return errno; + + if (operation == '+') + fs.st_mode |= S_IWUSR; + else + fs.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH); + + result = fchmod(f, fs.st_mode); + if (result == -1) + return errno; + } + return result; +} |
