diff --git a/setperm/GNUmakefile b/setperm/GNUmakefile new file mode 100644 index 0000000..f5f530a --- /dev/null +++ b/setperm/GNUmakefile @@ -0,0 +1,5 @@ +include GNUmakerules +setperm: +clean: + rm setperm +install: $(BINDIR)/setperm diff --git a/setperm/setperm.c b/setperm/setperm.c new file mode 100644 index 0000000..aac1878 --- /dev/null +++ b/setperm/setperm.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include + +char *cmnd; + +void usage(void) { + fprintf(stderr, "Usage: %s -F protofile file ...\n", cmnd); + exit(1); +} + +typedef struct { + uid_t user; + gid_t group; + mode_t mode; +} perm_t; + +int getperm(const char *filename, perm_t *permp) { + struct stat sb; + + if (stat(filename, &sb) == -1) return -1; + permp->user = sb.st_uid; + permp->group = sb.st_gid; + permp->mode = sb.st_mode; + return 0; +} + +int setperm(const char *filename, perm_t perm) { + int rc = 0; + + if (chown(filename, perm.user, perm.group) == -1) { + fprintf (stderr, "%s: cannot change owner of `%s' to %ld:%ld: %s\n", + cmnd, filename, (long)perm.user, (long)perm.group, strerror(errno)); + rc = 1; + } + if (chmod(filename, perm.mode) == -1) { + fprintf (stderr, "%s: cannot change mode of %s to %lo: %s\n", + cmnd, filename, (unsigned long)perm.mode, strerror(errno)); + rc = 1; + } + return rc; +} + +int main (int argc, char **argv) { + int c; + perm_t perm; + int i; + + cmnd = argv[0]; + + while ((c = getopt(argc, argv, "o:g:m:F:R")) != EOF) { + switch (c) { + case 'o': + case 'p': + case 'm': + case 'R': + fprintf (stderr, "%s: option `%c' not yet implemented. Sorry\n", cmnd, c); + exit(1); + case 'F': + if (getperm (optarg, &perm) == -1) { + fprintf (stderr, "%s: cannot get permissions from %s: %s\n", + cmnd, optarg, strerror(errno)); + exit(1); + } + break; + case '?': + usage (); + default: + assert(0); + } + } + for (i = optind; i < argc; i ++) { + setperm(argv[i], perm); + } + return 0; +}