From e52ee8a3f702e82df6a686c03cdc0b2366d0770a Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 31 Oct 2026 11:05:53 +0200 Subject: feat: Implement chwrite command A narrowed down version of chmod. Recursion must be done in-script. --- chwrite/Makefile | 8 ++++++++ chwrite/chwrite.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 chwrite/Makefile create mode 100644 chwrite/chwrite.c (limited to 'chwrite') 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 +#include +#include +#include +#include +#include + +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; +} -- cgit v1.3.1-23-gd82d