sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

mkdir.c (739B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <sys/stat.h>
      3 
      4 #include <errno.h>
      5 #include <stdlib.h>
      6 
      7 #include "util.h"
      8 
      9 static void
     10 usage(void)
     11 {
     12 	eprintf("usage: %s [-p] [-m mode] name ...\n", argv0);
     13 }
     14 
     15 int
     16 main(int argc, char *argv[])
     17 {
     18 	mode_t mode, mask;
     19 	int pflag = 0, ret = 0;
     20 
     21 	mask = umask(0);
     22 	mode = 0777 & ~mask;
     23 
     24 	ARGBEGIN {
     25 	case 'p':
     26 		pflag = 1;
     27 		break;
     28 	case 'm':
     29 		mode = parsemode(EARGF(usage()), 0777, mask);
     30 		break;
     31 	default:
     32 		usage();
     33 	} ARGEND
     34 
     35 	if (!argc)
     36 		usage();
     37 
     38 	for (; *argv; argc--, argv++) {
     39 		if (pflag) {
     40 			if (mkdirp(*argv, mode, 0777 & (~mask | 0300)) < 0)
     41 				ret = 1;
     42 		} else if (mkdir(*argv, mode) < 0) {
     43 			weprintf("mkdir %s:", *argv);
     44 			ret = 1;
     45 		}
     46 	}
     47 
     48 	return ret;
     49 }