Home | History | Annotate | Line # | Download | only in mkdir
mkdir.c revision 1.12
      1 /*	$NetBSD: mkdir.c,v 1.12 1995/03/21 07:28:54 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 char copyright[] =
     38 "@(#) Copyright (c) 1983, 1992, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n";
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)mkdir.c	8.2 (Berkeley) 1/25/94";
     45 #else
     46 static char rcsid[] = "$NetBSD: mkdir.c,v 1.12 1995/03/21 07:28:54 cgd Exp $";
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/types.h>
     51 #include <sys/stat.h>
     52 
     53 #include <err.h>
     54 #include <errno.h>
     55 #include <locale.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 
     61 int	mkpath __P((char *, mode_t, mode_t));
     62 void	usage __P((void));
     63 
     64 int
     65 main(argc, argv)
     66 	int argc;
     67 	char *argv[];
     68 {
     69 	int ch, exitval, pflag;
     70 	void *set;
     71 	mode_t mode, dir_mode;
     72 
     73 	setlocale(LC_ALL, "");
     74 
     75 	/* default file mode is a=rwx (777) with selected permissions
     76 	   removed in accordance with the file mode creation mask.
     77 	   For intermediate path name components, the mode is the default
     78 	   modified by u+wx so that the subdirectories can always be
     79 	   created. */
     80 	mode = 0777 & ~umask(0);
     81 	dir_mode = mode | S_IWUSR | S_IXUSR;
     82 
     83 	pflag = 0;
     84 	while ((ch = getopt(argc, argv, "pm:")) != -1)
     85 		switch(ch) {
     86 		case 'p':
     87 			pflag = 1;
     88 			break;
     89 		case 'm':
     90 			if ((set = setmode(optarg)) == NULL) {
     91 				errx(1, "invalid file mode.");
     92 				/* NOTREACHED */
     93 			}
     94 			mode = getmode (set, S_IRWXU | S_IRWXG | S_IRWXO);
     95 			break;
     96 		case '?':
     97 		default:
     98 			usage();
     99 		}
    100 
    101 	if (!*(argv += optind))
    102 		usage();
    103 
    104 	for (exitval = 0; *argv; ++argv) {
    105 		register char *slash;
    106 
    107 		/* delete trailing slashes */
    108 		slash = strrchr(*argv, '\0');
    109 		while (--slash > *argv && *slash == '/')
    110 			*slash = '\0';
    111 
    112 		if (pflag) {
    113 			exitval |= mkpath(*argv, mode, dir_mode);
    114 		} else if (mkdir(*argv, mode) < 0) {
    115 			warn("%s", *argv);
    116 			exitval = 1;
    117 		}
    118 	}
    119 	exit(exitval);
    120 }
    121 
    122 /*
    123  * mkpath -- create directories.
    124  *	path     - path
    125  *	mode     - file mode of terminal directory
    126  *	dir_mode - file mode of intermediate directories
    127  */
    128 int
    129 mkpath(path, mode, dir_mode)
    130 	char *path;
    131 	mode_t mode;
    132 	mode_t dir_mode;
    133 {
    134 	struct stat sb;
    135 	register char *slash;
    136 
    137 	/* skip leading slashes */
    138 	slash = path;
    139 	while (*slash == '/')
    140 		slash++;
    141 
    142 	while ((slash = strchr(slash, '/')) != NULL) {
    143 		*slash = '\0';
    144 
    145 		if (stat(path, &sb)) {
    146 			if (errno != ENOENT || mkdir(path, dir_mode)) {
    147 				warn("%s", path);
    148 				return 1;
    149 			}
    150 		} else if (!S_ISDIR(sb.st_mode)) {
    151 		        warnx("%s: %s", path, strerror(ENOTDIR));
    152 			return 1;
    153 		}
    154 
    155 		/* skip multiple slashes */
    156 		*slash++ = '/';
    157 		while (*slash == '/')
    158 			slash++;
    159 	}
    160 
    161 	if (mkdir (path, mode)) {
    162 		warn("%s", path);
    163 		return 1;
    164 	}
    165 
    166 	return(0);
    167 }
    168 
    169 void
    170 usage()
    171 {
    172 	(void)fprintf(stderr, "usage: mkdir [-p] [-m mode] dirname ...\n");
    173 	exit (1);
    174 }
    175