xref: /titanic_44/usr/src/lib/libcmd/common/mkdir.c (revision 8339b41da2395f0525c46ceedfeb01961893ec44)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1992-2010 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) 2009-12-03 $\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 "[v:verbose?Print a message on the standard error for each created "
47     "directory.]"
48 "\n"
49 "\ndirectory ...\n"
50 "\n"
51 "[+EXIT STATUS?]{"
52         "[+0?All directories created successfully, or the \b-p\b option "
53 	"was specified and all the specified directories now exist.]"
54         "[+>0?An error occurred.]"
55 "}"
56 "[+SEE ALSO?\bchmod\b(1), \brmdir\b(1), \bumask\b(1)]"
57 ;
58 
59 #include <cmd.h>
60 #include <ls.h>
61 
62 #define DIRMODE	(S_IRWXU|S_IRWXG|S_IRWXO)
63 
64 int
65 b_mkdir(int argc, char** argv, void* context)
66 {
67 	register char*	arg;
68 	register int	n;
69 	register mode_t	mode = DIRMODE;
70 	register mode_t	mask = 0;
71 	register int	mflag = 0;
72 	register int	pflag = 0;
73 	register int	vflag = 0;
74 	char*		name;
75 	mode_t		dmode;
76 	struct stat	st;
77 
78 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
79 	for (;;)
80 	{
81 		switch (optget(argv, usage))
82 		{
83 		case 0:
84 			break;
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 'p':
92 			pflag = 1;
93 			continue;
94 		case 'v':
95 			vflag = 1;
96 			continue;
97 		case ':':
98 			error(2, "%s", opt_info.arg);
99 			continue;
100 		case '?':
101 			error(ERROR_usage(2), "%s", opt_info.arg);
102 			continue;
103 		}
104 		break;
105 	}
106 	argv += opt_info.index;
107 	if (error_info.errors || !*argv)
108 		error(ERROR_usage(2), "%s", optusage(NiL));
109 	mask = umask(0);
110 	if (mflag || pflag)
111 	{
112 		dmode = DIRMODE & ~mask;
113 		if (!mflag)
114 			mode = dmode;
115 		dmode |= S_IWUSR | S_IXUSR;
116 	}
117 	else
118 	{
119 		mode &= ~mask;
120 		umask(mask);
121 		mask = 0;
122 	}
123 	while (arg = *argv++)
124 	{
125 		if (mkdir(arg, mode) < 0)
126 		{
127 			if (!pflag || !(errno == ENOENT || errno == EEXIST || errno == ENOTDIR))
128 			{
129 				error(ERROR_system(0), "%s:", arg);
130 				continue;
131 			}
132 			if (errno == EEXIST)
133 				continue;
134 
135 			/*
136 			 * -p option, preserve intermediates
137 			 * first eliminate trailing /'s
138 			 */
139 
140 			n = strlen(arg);
141 			while (n > 0 && arg[--n] == '/');
142 			arg[n + 1] = 0;
143 			for (name = arg, n = *arg; n;)
144 			{
145 				/* skip over slashes */
146 				while (*arg == '/')
147 					arg++;
148 				/* skip to next component */
149 				while ((n = *arg) && n != '/')
150 					arg++;
151 				*arg = 0;
152 				if (mkdir(name, n ? dmode : mode) < 0 && errno != EEXIST && access(name, F_OK) < 0)
153 				{
154 					*arg = n;
155 					error(ERROR_system(0), "%s:", name);
156 					break;
157 				}
158 				if (vflag)
159 					error(0, "%s: directory created", name);
160 				if (!(*arg = n) && (mode & (S_ISVTX|S_ISUID|S_ISGID)))
161 				{
162 					if (stat(name, &st))
163 					{
164 						error(ERROR_system(0), "%s: cannot stat", name);
165 						break;
166 					}
167 					if ((st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)) != (mode & (S_ISVTX|S_ISUID|S_ISGID)) && chmod(name, mode))
168 					{
169 						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));
170 						break;
171 					}
172 				}
173 			}
174 		}
175 		else if (vflag)
176 			error(0, "%s: directory created", arg);
177 	}
178 	if (mask)
179 		umask(mask);
180 	return error_info.errors != 0;
181 }
182