💾 Archived View for thrig.me › blog › 2023 › 07 › 27 › mperm.c captured on 2024-07-09 at 01:19:09.
⬅️ Previous capture (2023-09-08)
-=-=-=-=-=-=-
// mperm - mirror ownership and permissions from one file to others #include <sys/stat.h> #include <err.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <sysexits.h> #include <unistd.h> void emit_help(void); int main(int argc, char *argv[]) { #ifdef __OpenBSD__ if (pledge("chown fattr rpath stdio wpath", NULL) == -1) err(1, "pledge failed"); #endif if (argc < 2) emit_help(); argv++; struct stat src; if (stat(*argv, &src) == -1) err(1, "cannot stat '%s'", *argv); argv++; src.st_mode &= 07777; // filter off the file type while (*argv) { int fd = open(*argv, O_RDONLY); if (fd == -1) err(1, "cannot open '%s'", *argv); if (fchown(fd, src.st_uid, src.st_gid) == -1) err(1, "chown failed '%s'", *argv); if (fchmod(fd, src.st_mode) == -1) err(1, "chmod failed '%s'", *argv); argv++; } } void emit_help(void) { fputs("Usage: mperm source target ..\n", stderr); exit(EX_USAGE); }