morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

chmod.c (1204B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <sys/stat.h>
      5 #include <unistd.h>
      6 
      7 #include "util.h"
      8 
      9 static int    rflag   = 0;
     10 static char  *modestr = "";
     11 static mode_t mask    = 0;
     12 static int    ret     = 0;
     13 
     14 void
     15 chmodr(const char *path)
     16 {
     17 	struct stat st;
     18 	mode_t m;
     19 
     20 	if (stat(path, &st) < 0) {
     21 		weprintf("stat %s:", path);
     22 		ret = 1;
     23 		return;
     24 	}
     25 
     26 	m = parsemode(modestr, st.st_mode, mask);
     27 	if (chmod(path, m) < 0) {
     28 		weprintf("chmod %s:", path);
     29 		ret = 1;
     30 	}
     31 	if (rflag)
     32 		recurse(path, chmodr);
     33 }
     34 
     35 static void
     36 usage(void)
     37 {
     38 	eprintf("usage: %s [-R] mode [file ...]\n", argv0);
     39 }
     40 
     41 int
     42 main(int argc, char *argv[])
     43 {
     44 	size_t i;
     45 
     46 	argv0 = argv[0];
     47 	for (i = 1; i < argc && argv[i][0] == '-'; i++) {
     48 		switch (argv[i][1]) {
     49 		case 'R':
     50 			rflag = 1;
     51 			break;
     52 		case 'r': case 'w': case 'x': case 's': case 't':
     53 			/*
     54 			 * -[rwxst] are valid modes so do not interpret
     55 			 * them as options - in any case we are done if
     56 			 * we hit this case
     57 			 */
     58 			goto done;
     59 		default:
     60 			usage();
     61 		}
     62 	}
     63 done:
     64 	mask = getumask();
     65 	modestr = argv[i];
     66 
     67 	if (argc - i - 1 < 1)
     68 		usage();
     69 
     70 	for (++i; i < argc; i++)
     71 		chmodr(argv[i]);
     72 
     73 	return ret;
     74 }