morpheus-base

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

mkdir.c (1074B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <errno.h>
      3 #include <fcntl.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 #include <sys/stat.h>
      7 #include <unistd.h>
      8 
      9 #include "util.h"
     10 
     11 static void mkdirp(char *);
     12 
     13 static void
     14 usage(void)
     15 {
     16 	eprintf("usage: %s [-pm] directory...\n", argv0);
     17 }
     18 
     19 int
     20 main(int argc, char *argv[])
     21 {
     22 	int pflag = 0;
     23 	int mflag = 0;
     24 	int mode;
     25 
     26 	ARGBEGIN {
     27 	case 'p':
     28 		pflag = 1;
     29 		break;
     30 	case 'm':
     31 		mflag = 1;
     32 		mode = estrtol(EARGF(usage()), 8);
     33 		break;
     34 	default:
     35 		usage();
     36 	} ARGEND;
     37 
     38 	if (argc < 1)
     39 		usage();
     40 
     41 	for (; argc > 0; argc--, argv++) {
     42 		if (pflag) {
     43 			mkdirp(argv[0]);
     44 		} else if (mkdir(argv[0], S_IRWXU|S_IRWXG|S_IRWXO) < 0) {
     45 			eprintf("mkdir %s:", argv[0]);
     46 		}
     47 		if (mflag)
     48 			if (chmod(argv[0], mode) < 0)
     49 				eprintf("chmod %s:", argv[0]);
     50 	}
     51 
     52 	return 0;
     53 }
     54 
     55 static void
     56 mkdirp(char *path)
     57 {
     58 	char *p = path;
     59 
     60 	do {
     61 		if (*p && (p = strchr(&p[1], '/')))
     62 			*p = '\0';
     63 		if (mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO) < 0 && errno != EEXIST)
     64 			eprintf("mkdir %s:", path);
     65 		if (p)
     66 			*p = '/';
     67 	} while (p);
     68 }