Summary Files Commits Branches @commit @diff @stats
path: root / chwrite
diff options
context:
space:
mode:
2 files changed, 63 insertions, 0 deletions
-rw-r--r--chwrite/Makefile8
-rw-r--r--chwrite/chwrite.c55
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;
+}