xref: /titanic_41/usr/src/lib/libcmd/common/mkdir.c (revision bbb1277b6ec1b0daad4e3ed1a2b891d3e2ece2eb)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1992-2009 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                                                                      *
20 ***********************************************************************/
21 #pragma prototyped
22 /*
23  * David Korn
24  * AT&T Bell Laboratories
25  *
26  * mkdir
27  */
28 
29 static const char usage[] =
30 "[-?\n@(#)$Id: mkdir (AT&T Research) 2007-04-25 $\n]"
31 USAGE_LICENSE
32 "[+NAME?mkdir - make directories]"
33 "[+DESCRIPTION?\bmkdir\b creates one or more directories.  By "
34 	"default, the mode of created directories is \ba=rwx\b minus the "
35 	"bits set in the \bumask\b(1).]"
36 "[m:mode]:[mode?Set the mode of created directories to \amode\a.  "
37 	"\amode\a is symbolic or octal mode as in \bchmod\b(1).  Relative "
38 	"modes assume an initial mode of \ba=rwx\b.]"
39 "[p:parents?Create any missing intermediate pathname components. For "
40     "each dir operand that does not name an existing directory, effects "
41     "equivalent to those caused by the following command shall occur: "
42     "\vmkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode]] "
43     "dir\v where the \b-m\b mode option represents that option supplied to "
44     "the original invocation of \bmkdir\b, if any. Each dir operand that "
45     "names an existing directory shall be ignored without error.]"
46 "\n"
47 "\ndirectory ...\n"
48 "\n"
49 "[+EXIT STATUS?]{"
50         "[+0?All directories created successfully, or the \b-p\b option "
51 	"was specified and all the specified directories now exist.]"
52         "[+>0?An error occurred.]"
53 "}"
54 "[+SEE ALSO?\bchmod\b(1), \brmdir\b(1), \bumask\b(1)]"
55 ;
56 
57 #include <cmd.h>
58 #include <ls.h>
59 
60 #define DIRMODE	(S_IRWXU|S_IRWXG|S_IRWXO)
61 
62 int
63 b_mkdir(int argc, char** argv, void* context)
64 {
65 	register char*	arg;
66 	register int	n;
67 	register mode_t	mode = DIRMODE;
68 	register mode_t	mask = 0;
69 	register int	mflag = 0;
70 	register int	pflag = 0;
71 	char*		name;
72 	mode_t		dmode;
73 	struct stat	st;
74 
75 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
76 	for (;;)
77 	{
78 		switch (optget(argv, usage))
79 		{
80 		case 0:
81 			break;
82 		case 'p':
83 			pflag = 1;
84 			continue;
85 		case 'm':
86 			mflag = 1;
87 			mode = strperm(arg = opt_info.arg, &opt_info.arg, mode);
88 			if (*opt_info.arg)
89 				error(ERROR_exit(0), "%s: invalid mode", arg);
90 			continue;
91 		case ':':
92 			error(2, "%s", opt_info.arg);
93 			continue;
94 		case '?':
95 			error(ERROR_usage(2), "%s", opt_info.arg);
96 			continue;
97 		}
98 		break;
99 	}
100 	argv += opt_info.index;
101 	if (error_info.errors || !*argv)
102 		error(ERROR_usage(2), "%s", optusage(NiL));
103 	mask = umask(0);
104 	if (mflag || pflag)
105 	{
106 		dmode = DIRMODE & ~mask;
107 		if (!mflag)
108 			mode = dmode;
109 		dmode |= S_IWUSR | S_IXUSR;
110 	}
111 	else
112 	{
113 		mode &= ~mask;
114 		umask(mask);
115 		mask = 0;
116 	}
117 	while (arg = *argv++)
118 	{
119 		if (mkdir(arg, mode) < 0)
120 		{
121 			if (!pflag || !(errno == ENOENT || errno == EEXIST || errno == ENOTDIR))
122 			{
123 				error(ERROR_system(0), "%s:", arg);
124 				continue;
125 			}
126 			if (errno == EEXIST)
127 				continue;
128 
129 			/*
130 			 * -p option, preserve intermediates
131 			 * first eliminate trailing /'s
132 			 */
133 
134 			n = strlen(arg);
135 			while (n > 0 && arg[--n] == '/');
136 			arg[n + 1] = 0;
137 			for (name = arg, n = *arg; n;)
138 			{
139 				/* skip over slashes */
140 				while (*arg == '/')
141 					arg++;
142 				/* skip to next component */
143 				while ((n = *arg) && n != '/')
144 					arg++;
145 				*arg = 0;
146 				if (mkdir(name, n ? dmode : mode) < 0 && errno != EEXIST && access(name, F_OK) < 0)
147 				{
148 					*arg = n;
149 					error(ERROR_system(0), "%s:", name);
150 					break;
151 				}
152 				if (!(*arg = n) && (mode & (S_ISVTX|S_ISUID|S_ISGID)))
153 				{
154 					if (stat(name, &st))
155 					{
156 						error(ERROR_system(0), "%s: cannot stat", name);
157 						break;
158 					}
159 					if ((st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)) != (mode & (S_ISVTX|S_ISUID|S_ISGID)) && chmod(name, mode))
160 					{
161 						error(ERROR_system(0), "%s: cannot change mode from %s to %s", name, fmtperm(st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)), fmtperm(mode));
162 						break;
163 					}
164 				}
165 			}
166 		}
167 	}
168 	if (mask)
169 		umask(mask);
170 	return error_info.errors != 0;
171 }
172