mkdir.c revision 24524
11556Srgrimes/*
21556Srgrimes * Copyright (c) 1983, 1992, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 3. All advertising materials mentioning features or use of this software
141556Srgrimes *    must display the following acknowledgement:
151556Srgrimes *	This product includes software developed by the University of
161556Srgrimes *	California, Berkeley and its contributors.
171556Srgrimes * 4. Neither the name of the University nor the names of its contributors
181556Srgrimes *    may be used to endorse or promote products derived from this software
191556Srgrimes *    without specific prior written permission.
201556Srgrimes *
211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311556Srgrimes * SUCH DAMAGE.
323044Sdg *
3324524Smpp *	$Id: mkdir.c,v 1.9 1997/03/28 15:24:25 imp Exp $
341556Srgrimes */
351556Srgrimes
361556Srgrimes#ifndef lint
3720418Sstevestatic char const copyright[] =
381556Srgrimes"@(#) Copyright (c) 1983, 1992, 1993\n\
391556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
401556Srgrimes#endif /* not lint */
411556Srgrimes
421556Srgrimes#ifndef lint
4320418Sstevestatic char const sccsid[] = "@(#)mkdir.c	8.2 (Berkeley) 1/25/94";
441556Srgrimes#endif /* not lint */
451556Srgrimes
461556Srgrimes#include <sys/types.h>
471556Srgrimes#include <sys/stat.h>
481556Srgrimes
491556Srgrimes#include <err.h>
501556Srgrimes#include <errno.h>
511556Srgrimes#include <stdio.h>
521556Srgrimes#include <stdlib.h>
531556Srgrimes#include <string.h>
541556Srgrimes#include <unistd.h>
551556Srgrimes
5624524Smppint	build __P((char *, mode_t));
571556Srgrimesvoid	usage __P((void));
581556Srgrimes
591556Srgrimesint
601556Srgrimesmain(argc, argv)
611556Srgrimes	int argc;
621556Srgrimes	char *argv[];
631556Srgrimes{
6424524Smpp	int ch, exitval, omode, pflag;
652923Sphk	mode_t *set = (mode_t *)NULL;
6624524Smpp	char *mode;
671556Srgrimes
682923Sphk	omode = pflag = 0;
691556Srgrimes	mode = NULL;
7024348Simp	while ((ch = getopt(argc, argv, "m:p")) != -1)
711556Srgrimes		switch(ch) {
721556Srgrimes		case 'p':
731556Srgrimes			pflag = 1;
741556Srgrimes			break;
751556Srgrimes		case 'm':
761556Srgrimes			mode = optarg;
771556Srgrimes			break;
781556Srgrimes		case '?':
791556Srgrimes		default:
801556Srgrimes			usage();
811556Srgrimes		}
821556Srgrimes
831556Srgrimes	argc -= optind;
841556Srgrimes	argv += optind;
851556Srgrimes	if (argv[0] == NULL)
861556Srgrimes		usage();
871556Srgrimes
881556Srgrimes	if (mode == NULL) {
891556Srgrimes		omode = S_IRWXU | S_IRWXG | S_IRWXO;
901556Srgrimes	} else {
911556Srgrimes		if ((set = setmode(mode)) == NULL)
921556Srgrimes			errx(1, "invalid file mode: %s", mode);
9324524Smpp		omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
941556Srgrimes	}
951556Srgrimes
961556Srgrimes	for (exitval = 0; *argv != NULL; ++argv) {
9724524Smpp		if (pflag) {
9824524Smpp			if (build(*argv, omode))
9924524Smpp				exitval = 1;
1001556Srgrimes			continue;
1011556Srgrimes		}
10224524Smpp		if (mkdir(*argv, omode) < 0) {
10324524Smpp			warn("%s", *argv);
10424524Smpp			exitval = 1;
1051556Srgrimes		}
1061556Srgrimes	}
1071556Srgrimes	exit(exitval);
1081556Srgrimes}
1091556Srgrimes
1101556Srgrimesint
11124524Smppbuild(path, omode)
1121556Srgrimes	char *path;
11324524Smpp	mode_t omode;
1141556Srgrimes{
1151556Srgrimes	struct stat sb;
1161556Srgrimes	mode_t numask, oumask;
11724524Smpp	int first, last, retval;
1181556Srgrimes	char *p;
1191556Srgrimes
1201556Srgrimes	p = path;
1212959Sbde	oumask = 0;
12224524Smpp	retval = 0;
1231556Srgrimes	if (p[0] == '/')		/* Skip leading '/'. */
1241556Srgrimes		++p;
12524524Smpp	for (first = 1, last = 0; !last ; ++p) {
12624524Smpp		if (p[0] == '\0')
12724524Smpp			last = 1;
12824524Smpp		else if (p[0] != '/')
1291556Srgrimes			continue;
1301556Srgrimes		*p = '\0';
13124524Smpp		if (p[1] == '\0')
13224524Smpp			last = 1;
1331556Srgrimes		if (first) {
1341556Srgrimes			/*
1351556Srgrimes			 * POSIX 1003.2:
1361556Srgrimes			 * For each dir operand that does not name an existing
1371556Srgrimes			 * directory, effects equivalent to those cased by the
1381556Srgrimes			 * following command shall occcur:
1391556Srgrimes			 *
1401556Srgrimes			 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
1411556Srgrimes			 *    mkdir [-m mode] dir
1421556Srgrimes			 *
1431556Srgrimes			 * We change the user's umask and then restore it,
1441556Srgrimes			 * instead of doing chmod's.
1451556Srgrimes			 */
1461556Srgrimes			oumask = umask(0);
1471556Srgrimes			numask = oumask & ~(S_IWUSR | S_IXUSR);
1481556Srgrimes			(void)umask(numask);
1491556Srgrimes			first = 0;
1501556Srgrimes		}
15124524Smpp		if (last)
15224524Smpp			(void)umask(oumask);
1531556Srgrimes		if (stat(path, &sb)) {
1541556Srgrimes			if (errno != ENOENT ||
15524524Smpp			    mkdir(path, last ? omode :
15624524Smpp				  S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
1571556Srgrimes				warn("%s", path);
15824524Smpp				retval = 1;
15924524Smpp				break;
1601556Srgrimes			}
1611556Srgrimes		}
16224524Smpp		else if ((sb.st_mode & S_IFMT) != S_IFDIR) {
16324524Smpp			if (last)
16424524Smpp				errno = EEXIST;
16524524Smpp			else
16624524Smpp				errno = ENOTDIR;
16724524Smpp			warn("%s", path);
16824524Smpp			retval = 1;
16924524Smpp			break;
17024524Smpp		}
1711556Srgrimes		*p = '/';
1721556Srgrimes	}
17324524Smpp	if (!first && !last)
1741556Srgrimes		(void)umask(oumask);
17524524Smpp	return (retval);
1761556Srgrimes}
1771556Srgrimes
1781556Srgrimesvoid
1791556Srgrimesusage()
1801556Srgrimes{
1811556Srgrimes	(void)fprintf(stderr, "usage: mkdir [-p] [-m mode] directory ...\n");
1821556Srgrimes	exit (1);
1831556Srgrimes}
184