mkdir.c revision 71029
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[] =
4550471Speter  "$FreeBSD: head/bin/mkdir/mkdir.c 71029 2001-01-14 12:08:50Z alfred $";
461556Srgrimes#endif /* not lint */
471556Srgrimes
481556Srgrimes#include <sys/types.h>
491556Srgrimes#include <sys/stat.h>
501556Srgrimes
511556Srgrimes#include <err.h>
521556Srgrimes#include <errno.h>
5371029Salfred#include <libgen.h>
541556Srgrimes#include <stdio.h>
551556Srgrimes#include <stdlib.h>
561556Srgrimes#include <string.h>
5750528Smharo#include <sysexits.h>
581556Srgrimes#include <unistd.h>
591556Srgrimes
6024524Smppint	build __P((char *, mode_t));
611556Srgrimesvoid	usage __P((void));
621556Srgrimes
6350528Smharoint vflag;
6450528Smharo
651556Srgrimesint
661556Srgrimesmain(argc, argv)
671556Srgrimes	int argc;
681556Srgrimes	char *argv[];
691556Srgrimes{
7040606Smsmith	int ch, exitval, success, omode, pflag;
712923Sphk	mode_t *set = (mode_t *)NULL;
7224524Smpp	char *mode;
731556Srgrimes
742923Sphk	omode = pflag = 0;
751556Srgrimes	mode = NULL;
7650528Smharo	while ((ch = getopt(argc, argv, "m:pv")) != -1)
771556Srgrimes		switch(ch) {
7850870Smharo		case 'm':
7950870Smharo			mode = optarg;
8050870Smharo			break;
811556Srgrimes		case 'p':
821556Srgrimes			pflag = 1;
831556Srgrimes			break;
8450528Smharo		case 'v':
8550528Smharo			vflag = 1;
8650528Smharo			break;
871556Srgrimes		case '?':
881556Srgrimes		default:
891556Srgrimes			usage();
901556Srgrimes		}
911556Srgrimes
921556Srgrimes	argc -= optind;
931556Srgrimes	argv += optind;
941556Srgrimes	if (argv[0] == NULL)
951556Srgrimes		usage();
961556Srgrimes
971556Srgrimes	if (mode == NULL) {
981556Srgrimes		omode = S_IRWXU | S_IRWXG | S_IRWXO;
991556Srgrimes	} else {
1001556Srgrimes		if ((set = setmode(mode)) == NULL)
1011556Srgrimes			errx(1, "invalid file mode: %s", mode);
10224524Smpp		omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
10341842Simp		free(set);
1041556Srgrimes	}
1051556Srgrimes
1061556Srgrimes	for (exitval = 0; *argv != NULL; ++argv) {
10740606Smsmith		success = 1;
10824524Smpp		if (pflag) {
10924524Smpp			if (build(*argv, omode))
11040606Smsmith				success = 0;
11140602Smsmith		} else if (mkdir(*argv, omode) < 0) {
11271029Salfred			if (errno == ENOTDIR || errno == ENOENT)
11371029Salfred				warn("%s", dirname(*argv));
11471029Salfred			else
11571029Salfred				warn("%s", *argv);
11640606Smsmith			success = 0;
11750528Smharo		} else if (vflag)
11850528Smharo			(void)printf("%s\n", *argv);
11950528Smharo
12040606Smsmith		if (!success)
12124524Smpp			exitval = 1;
12240535Smsmith		/*
12340535Smsmith		 * The mkdir() and umask() calls both honor only the low
12440535Smsmith		 * nine bits, so if you try to set a mode including the
12540602Smsmith		 * sticky, setuid, setgid bits you lose them.  Don't do
12640602Smsmith		 * this unless the user has specifically requested a mode,
12740602Smsmith		 * as chmod will (obviously) ignore the umask.
12840535Smsmith		 */
12940606Smsmith		if (success && mode != NULL && chmod(*argv, omode) == -1) {
13040606Smsmith			warn("%s", *argv);
13140535Smsmith			exitval = 1;
13240535Smsmith		}
1331556Srgrimes	}
1341556Srgrimes	exit(exitval);
1351556Srgrimes}
1361556Srgrimes
1371556Srgrimesint
13824524Smppbuild(path, omode)
1391556Srgrimes	char *path;
14024524Smpp	mode_t omode;
1411556Srgrimes{
1421556Srgrimes	struct stat sb;
1431556Srgrimes	mode_t numask, oumask;
14424524Smpp	int first, last, retval;
1451556Srgrimes	char *p;
1461556Srgrimes
1471556Srgrimes	p = path;
1482959Sbde	oumask = 0;
14924524Smpp	retval = 0;
1501556Srgrimes	if (p[0] == '/')		/* Skip leading '/'. */
1511556Srgrimes		++p;
15224524Smpp	for (first = 1, last = 0; !last ; ++p) {
15324524Smpp		if (p[0] == '\0')
15424524Smpp			last = 1;
15524524Smpp		else if (p[0] != '/')
1561556Srgrimes			continue;
1571556Srgrimes		*p = '\0';
15824524Smpp		if (p[1] == '\0')
15924524Smpp			last = 1;
1601556Srgrimes		if (first) {
1611556Srgrimes			/*
1621556Srgrimes			 * POSIX 1003.2:
1631556Srgrimes			 * For each dir operand that does not name an existing
1641556Srgrimes			 * directory, effects equivalent to those cased by the
1651556Srgrimes			 * following command shall occcur:
1661556Srgrimes			 *
1671556Srgrimes			 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
1681556Srgrimes			 *    mkdir [-m mode] dir
1691556Srgrimes			 *
1701556Srgrimes			 * We change the user's umask and then restore it,
1711556Srgrimes			 * instead of doing chmod's.
1721556Srgrimes			 */
1731556Srgrimes			oumask = umask(0);
1741556Srgrimes			numask = oumask & ~(S_IWUSR | S_IXUSR);
1751556Srgrimes			(void)umask(numask);
1761556Srgrimes			first = 0;
1771556Srgrimes		}
17824524Smpp		if (last)
17924524Smpp			(void)umask(oumask);
1801556Srgrimes		if (stat(path, &sb)) {
1811556Srgrimes			if (errno != ENOENT ||
18224524Smpp			    mkdir(path, last ? omode :
18324524Smpp				  S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
1841556Srgrimes				warn("%s", path);
18524524Smpp				retval = 1;
18624524Smpp				break;
18750528Smharo			} else if (vflag)
18850528Smharo				printf("%s\n", path);
1891556Srgrimes		}
19024524Smpp		else if ((sb.st_mode & S_IFMT) != S_IFDIR) {
19124524Smpp			if (last)
19224524Smpp				errno = EEXIST;
19324524Smpp			else
19424524Smpp				errno = ENOTDIR;
19524524Smpp			warn("%s", path);
19624524Smpp			retval = 1;
19724524Smpp			break;
19824524Smpp		}
19940602Smsmith		if (!last)
20040602Smsmith		    *p = '/';
2011556Srgrimes	}
20224524Smpp	if (!first && !last)
2031556Srgrimes		(void)umask(oumask);
20424524Smpp	return (retval);
2051556Srgrimes}
2061556Srgrimes
2071556Srgrimesvoid
2081556Srgrimesusage()
2091556Srgrimes{
21050870Smharo
21150870Smharo	(void)fprintf(stderr, "usage: mkdir [-pv] [-m mode] directory ...\n");
21250528Smharo	exit (EX_USAGE);
2131556Srgrimes}
214