Summary Files Commits Branches @commit @diff @stats
path: root / chwrite / chwrite.c
diff options
context:
space:
mode:
Commite52ee8a3f702e82df6a686c03cdc0b2366d0770a (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-10-31 11:05:53 +0200
Committer
 Syndamia <kamen@syndamia.com>
2026-10-31 11:10:51 +0200
Files54c61a6de1c32ce2e6780701a0cafd3afd077757 /chwrite/chwrite.c
cli-e52ee8a3f702e82df6a686c03cdc0b2366d0770a.tar
cli-e52ee8a3f702e82df6a686c03cdc0b2366d0770a.tar.gz
cli-e52ee8a3f702e82df6a686c03cdc0b2366d0770a.zip
Parentf522fb4f17114d48e2de5b60a9b34dea350e77cb (diff)
feat: Implement chwrite command
A narrowed down version of chmod. Recursion must be done in-script.
1 files changed, 55 insertions, 0 deletions
-rw-r--r--chwrite/chwrite.c55
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;
+}