mkdir.c revision 40535
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.
321556Srgrimes */
331556Srgrimes
341556Srgrimes#ifndef lint
3520418Sstevestatic char const copyright[] =
361556Srgrimes"@(#) Copyright (c) 1983, 1992, 1993\n\
371556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381556Srgrimes#endif /* not lint */
391556Srgrimes
401556Srgrimes#ifndef lint
4136048Scharnier#if 0
4236048Scharnierstatic char sccsid[] = "@(#)mkdir.c	8.2 (Berkeley) 1/25/94";
4336048Scharnier#endif
4436048Scharnierstatic const char rcsid[] =
4540535Smsmith	"$Id: mkdir.c,v 1.11 1998/05/15 06:23:45 charnier Exp $";
461556Srgrimes#endif /* not lint */
471556Srgrimes
481556Srgrimes#include <sys/types.h>
491556Srgrimes#include <sys/stat.h>
501556Srgrimes
511556Srgrimes#include <err.h>
521556Srgrimes#include <errno.h>
531556Srgrimes#include <stdio.h>
541556Srgrimes#include <stdlib.h>
551556Srgrimes#include <string.h>
561556Srgrimes#include <unistd.h>
571556Srgrimes
5824524Smppint	build __P((char *, mode_t));
591556Srgrimesvoid	usage __P((void));
601556Srgrimes
611556Srgrimesint
621556Srgrimesmain(argc, argv)
631556Srgrimes	int argc;
641556Srgrimes	char *argv[];
651556Srgrimes{
6624524Smpp	int ch, exitval, omode, pflag;
672923Sphk	mode_t *set = (mode_t *)NULL;
6824524Smpp	char *mode;
691556Srgrimes
702923Sphk	omode = pflag = 0;
711556Srgrimes	mode = NULL;
7224348Simp	while ((ch = getopt(argc, argv, "m:p")) != -1)
731556Srgrimes		switch(ch) {
741556Srgrimes		case 'p':
751556Srgrimes			pflag = 1;
761556Srgrimes			break;
771556Srgrimes		case 'm':
781556Srgrimes			mode = optarg;
791556Srgrimes			break;
801556Srgrimes		case '?':
811556Srgrimes		default:
821556Srgrimes			usage();
831556Srgrimes		}
841556Srgrimes
851556Srgrimes	argc -= optind;
861556Srgrimes	argv += optind;
871556Srgrimes	if (argv[0] == NULL)
881556Srgrimes		usage();
891556Srgrimes
901556Srgrimes	if (mode == NULL) {
911556Srgrimes		omode = S_IRWXU | S_IRWXG | S_IRWXO;
921556Srgrimes	} else {
931556Srgrimes		if ((set = setmode(mode)) == NULL)
941556Srgrimes			errx(1, "invalid file mode: %s", mode);
9524524Smpp		omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
961556Srgrimes	}
971556Srgrimes
981556Srgrimes	for (exitval = 0; *argv != NULL; ++argv) {
9924524Smpp		if (pflag) {
10024524Smpp			if (build(*argv, omode))
10124524Smpp				exitval = 1;
1021556Srgrimes			continue;
1031556Srgrimes		}
10424524Smpp		if (mkdir(*argv, omode) < 0) {
10524524Smpp			warn("%s", *argv);
10624524Smpp			exitval = 1;
1071556Srgrimes		}
10840535Smsmith		/*
10940535Smsmith		 * The mkdir() and umask() calls both honor only the low
11040535Smsmith		 * nine bits, so if you try to set a mode including the
11140535Smsmith		 * sticky, setuid, setgid bits you lose them.  So chmod().
11240535Smsmith		 */
11340535Smsmith		if (chmod(*argv, mode) == -1) {
11440535Smsmith			warn("%s", *argv);
11540535Smsmith			exitval = 1;
11640535Smsmith		}
1171556Srgrimes	}
1181556Srgrimes	exit(exitval);
1191556Srgrimes}
1201556Srgrimes
1211556Srgrimesint
12224524Smppbuild(path, omode)
1231556Srgrimes	char *path;
12424524Smpp	mode_t omode;
1251556Srgrimes{
1261556Srgrimes	struct stat sb;
1271556Srgrimes	mode_t numask, oumask;
12824524Smpp	int first, last, retval;
1291556Srgrimes	char *p;
1301556Srgrimes
1311556Srgrimes	p = path;
1322959Sbde	oumask = 0;
13324524Smpp	retval = 0;
1341556Srgrimes	if (p[0] == '/')		/* Skip leading '/'. */
1351556Srgrimes		++p;
13624524Smpp	for (first = 1, last = 0; !last ; ++p) {
13724524Smpp		if (p[0] == '\0')
13824524Smpp			last = 1;
13924524Smpp		else if (p[0] != '/')
1401556Srgrimes			continue;
1411556Srgrimes		*p = '\0';
14224524Smpp		if (p[1] == '\0')
14324524Smpp			last = 1;
1441556Srgrimes		if (first) {
1451556Srgrimes			/*
1461556Srgrimes			 * POSIX 1003.2:
1471556Srgrimes			 * For each dir operand that does not name an existing
1481556Srgrimes			 * directory, effects equivalent to those cased by the
1491556Srgrimes			 * following command shall occcur:
1501556Srgrimes			 *
1511556Srgrimes			 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
1521556Srgrimes			 *    mkdir [-m mode] dir
1531556Srgrimes			 *
1541556Srgrimes			 * We change the user's umask and then restore it,
1551556Srgrimes			 * instead of doing chmod's.
1561556Srgrimes			 */
1571556Srgrimes			oumask = umask(0);
1581556Srgrimes			numask = oumask & ~(S_IWUSR | S_IXUSR);
1591556Srgrimes			(void)umask(numask);
1601556Srgrimes			first = 0;
1611556Srgrimes		}
16224524Smpp		if (last)
16324524Smpp			(void)umask(oumask);
1641556Srgrimes		if (stat(path, &sb)) {
1651556Srgrimes			if (errno != ENOENT ||
16624524Smpp			    mkdir(path, last ? omode :
16724524Smpp				  S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
1681556Srgrimes				warn("%s", path);
16924524Smpp				retval = 1;
17024524Smpp				break;
1711556Srgrimes			}
17240535Smsmith                	/*
17340535Smsmith                         * The mkdir() and umask() calls both honor only the low
17440535Smsmith                	 * nine bits, so if you try to set a mode including the
17540535Smsmith                    	 * sticky, setuid, setgid bits you lose them. So chmod().
17640535Smsmith                         */
17740535Smsmith                    	if (chmod(*path, mode) == -1) {
17840535Smsmith				warn("%s", *path);
17940535Smsmith				exitval = 1;
18040535Smsmith                        }
1811556Srgrimes		}
18224524Smpp		else if ((sb.st_mode & S_IFMT) != S_IFDIR) {
18324524Smpp			if (last)
18424524Smpp				errno = EEXIST;
18524524Smpp			else
18624524Smpp				errno = ENOTDIR;
18724524Smpp			warn("%s", path);
18824524Smpp			retval = 1;
18924524Smpp			break;
19024524Smpp		}
1911556Srgrimes		*p = '/';
1921556Srgrimes	}
19324524Smpp	if (!first && !last)
1941556Srgrimes		(void)umask(oumask);
19524524Smpp	return (retval);
1961556Srgrimes}
1971556Srgrimes
1981556Srgrimesvoid
1991556Srgrimesusage()
2001556Srgrimes{
2011556Srgrimes	(void)fprintf(stderr, "usage: mkdir [-p] [-m mode] directory ...\n");
2021556Srgrimes	exit (1);
2031556Srgrimes}
204