mkdir.c revision 135718
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 * 4. Neither the name of the University nor the names of its contributors
141556Srgrimes *    may be used to endorse or promote products derived from this software
151556Srgrimes *    without specific prior written permission.
161556Srgrimes *
171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271556Srgrimes * SUCH DAMAGE.
281556Srgrimes */
291556Srgrimes
30114433Sobrien#if 0
311556Srgrimes#ifndef lint
3220418Sstevestatic char const copyright[] =
331556Srgrimes"@(#) Copyright (c) 1983, 1992, 1993\n\
341556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
351556Srgrimes#endif /* not lint */
361556Srgrimes
371556Srgrimes#ifndef lint
3836048Scharnierstatic char sccsid[] = "@(#)mkdir.c	8.2 (Berkeley) 1/25/94";
39114433Sobrien#endif /* not lint */
4036048Scharnier#endif
4199109Sobrien#include <sys/cdefs.h>
4299109Sobrien__FBSDID("$FreeBSD: head/bin/mkdir/mkdir.c 135718 2004-09-24 06:57:26Z das $");
431556Srgrimes
441556Srgrimes#include <sys/types.h>
451556Srgrimes#include <sys/stat.h>
461556Srgrimes
471556Srgrimes#include <err.h>
481556Srgrimes#include <errno.h>
4971029Salfred#include <libgen.h>
501556Srgrimes#include <stdio.h>
511556Srgrimes#include <stdlib.h>
521556Srgrimes#include <string.h>
5350528Smharo#include <sysexits.h>
541556Srgrimes#include <unistd.h>
551556Srgrimes
5691084Smarkmstatic int	build(char *, mode_t);
5791084Smarkmstatic void	usage(void);
581556Srgrimes
5950528Smharoint vflag;
6050528Smharo
611556Srgrimesint
6290110Simpmain(int argc, char *argv[])
631556Srgrimes{
6491084Smarkm	int ch, exitval, success, pflag;
6591084Smarkm	mode_t omode, *set = (mode_t *)NULL;
6624524Smpp	char *mode;
671556Srgrimes
682923Sphk	omode = pflag = 0;
691556Srgrimes	mode = NULL;
7050528Smharo	while ((ch = getopt(argc, argv, "m:pv")) != -1)
711556Srgrimes		switch(ch) {
7250870Smharo		case 'm':
7350870Smharo			mode = optarg;
7450870Smharo			break;
751556Srgrimes		case 'p':
761556Srgrimes			pflag = 1;
771556Srgrimes			break;
7850528Smharo		case 'v':
7950528Smharo			vflag = 1;
8050528Smharo			break;
811556Srgrimes		case '?':
821556Srgrimes		default:
831556Srgrimes			usage();
841556Srgrimes		}
851556Srgrimes
861556Srgrimes	argc -= optind;
871556Srgrimes	argv += optind;
881556Srgrimes	if (argv[0] == NULL)
891556Srgrimes		usage();
901556Srgrimes
911556Srgrimes	if (mode == NULL) {
921556Srgrimes		omode = S_IRWXU | S_IRWXG | S_IRWXO;
931556Srgrimes	} else {
941556Srgrimes		if ((set = setmode(mode)) == NULL)
951556Srgrimes			errx(1, "invalid file mode: %s", mode);
9624524Smpp		omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
9741842Simp		free(set);
981556Srgrimes	}
991556Srgrimes
1001556Srgrimes	for (exitval = 0; *argv != NULL; ++argv) {
10140606Smsmith		success = 1;
10224524Smpp		if (pflag) {
10324524Smpp			if (build(*argv, omode))
10440606Smsmith				success = 0;
10540602Smsmith		} else if (mkdir(*argv, omode) < 0) {
10671029Salfred			if (errno == ENOTDIR || errno == ENOENT)
10771029Salfred				warn("%s", dirname(*argv));
10871029Salfred			else
10971029Salfred				warn("%s", *argv);
11040606Smsmith			success = 0;
11150528Smharo		} else if (vflag)
11250528Smharo			(void)printf("%s\n", *argv);
11350528Smharo
11440606Smsmith		if (!success)
11524524Smpp			exitval = 1;
11640535Smsmith		/*
11740535Smsmith		 * The mkdir() and umask() calls both honor only the low
11840535Smsmith		 * nine bits, so if you try to set a mode including the
11940602Smsmith		 * sticky, setuid, setgid bits you lose them.  Don't do
12040602Smsmith		 * this unless the user has specifically requested a mode,
12140602Smsmith		 * as chmod will (obviously) ignore the umask.
12240535Smsmith		 */
12340606Smsmith		if (success && mode != NULL && chmod(*argv, omode) == -1) {
12440606Smsmith			warn("%s", *argv);
12540535Smsmith			exitval = 1;
12640535Smsmith		}
1271556Srgrimes	}
1281556Srgrimes	exit(exitval);
1291556Srgrimes}
1301556Srgrimes
1311556Srgrimesint
13290110Simpbuild(char *path, mode_t omode)
1331556Srgrimes{
1341556Srgrimes	struct stat sb;
1351556Srgrimes	mode_t numask, oumask;
13624524Smpp	int first, last, retval;
1371556Srgrimes	char *p;
1381556Srgrimes
1391556Srgrimes	p = path;
1402959Sbde	oumask = 0;
14124524Smpp	retval = 0;
1421556Srgrimes	if (p[0] == '/')		/* Skip leading '/'. */
1431556Srgrimes		++p;
14424524Smpp	for (first = 1, last = 0; !last ; ++p) {
14524524Smpp		if (p[0] == '\0')
14624524Smpp			last = 1;
14724524Smpp		else if (p[0] != '/')
1481556Srgrimes			continue;
1491556Srgrimes		*p = '\0';
150135718Sdas		if (!last && p[1] == '\0')
15124524Smpp			last = 1;
1521556Srgrimes		if (first) {
1531556Srgrimes			/*
1541556Srgrimes			 * POSIX 1003.2:
1551556Srgrimes			 * For each dir operand that does not name an existing
1561556Srgrimes			 * directory, effects equivalent to those cased by the
1571556Srgrimes			 * following command shall occcur:
1581556Srgrimes			 *
1591556Srgrimes			 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
1601556Srgrimes			 *    mkdir [-m mode] dir
1611556Srgrimes			 *
1621556Srgrimes			 * We change the user's umask and then restore it,
1631556Srgrimes			 * instead of doing chmod's.
1641556Srgrimes			 */
1651556Srgrimes			oumask = umask(0);
1661556Srgrimes			numask = oumask & ~(S_IWUSR | S_IXUSR);
1671556Srgrimes			(void)umask(numask);
1681556Srgrimes			first = 0;
1691556Srgrimes		}
17024524Smpp		if (last)
17124524Smpp			(void)umask(oumask);
17290266Szarzycki		if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
17391235Ssobomax			if (errno == EEXIST || errno == EISDIR) {
17490266Szarzycki				if (stat(path, &sb) < 0) {
17590266Szarzycki					warn("%s", path);
17690266Szarzycki					retval = 1;
17790266Szarzycki					break;
17890266Szarzycki				} else if (!S_ISDIR(sb.st_mode)) {
17990266Szarzycki					if (last)
18090266Szarzycki						errno = EEXIST;
18190266Szarzycki					else
18290266Szarzycki						errno = ENOTDIR;
18390266Szarzycki					warn("%s", path);
18490266Szarzycki					retval = 1;
18590266Szarzycki					break;
18690266Szarzycki				}
18790266Szarzycki			} else {
1881556Srgrimes				warn("%s", path);
18924524Smpp				retval = 1;
19024524Smpp				break;
19190266Szarzycki			}
19290266Szarzycki		} else if (vflag)
19390266Szarzycki			printf("%s\n", path);
19440602Smsmith		if (!last)
19540602Smsmith		    *p = '/';
1961556Srgrimes	}
19724524Smpp	if (!first && !last)
1981556Srgrimes		(void)umask(oumask);
19924524Smpp	return (retval);
2001556Srgrimes}
2011556Srgrimes
2021556Srgrimesvoid
20390110Simpusage(void)
2041556Srgrimes{
20550870Smharo
20650870Smharo	(void)fprintf(stderr, "usage: mkdir [-pv] [-m mode] directory ...\n");
20750528Smharo	exit (EX_USAGE);
2081556Srgrimes}
209