xref: /titanic_41/usr/src/lib/libcmd/common/mkdir.c (revision 40db2e2b777b79f3dd0d6d9629593a07f86b9c0a)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1992-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
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) 2006-08-27 $\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 
74 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
75 	while (n = optget(argv, usage)) switch (n)
76 	{
77 	case 'p':
78 		pflag = 1;
79 		break;
80 	case 'm':
81 		mflag = 1;
82 		mode = strperm(arg = opt_info.arg, &opt_info.arg, mode);
83 		if (*opt_info.arg)
84 			error(ERROR_exit(0), "%s: invalid mode", arg);
85 		break;
86 	case ':':
87 		error(2, "%s", opt_info.arg);
88 		break;
89 	case '?':
90 		error(ERROR_usage(2), "%s", opt_info.arg);
91 		break;
92 	}
93 	argv += opt_info.index;
94 	if (error_info.errors || !*argv)
95 		error(ERROR_usage(2), "%s", optusage(NiL));
96 	mask = umask(0);
97 	if (mflag || pflag)
98 	{
99 		dmode = DIRMODE & ~mask;
100 		if (!mflag)
101 			mode = dmode;
102 		dmode |= S_IWUSR | S_IXUSR;
103 	}
104 	else
105 	{
106 		mode &= ~mask;
107 		umask(mask);
108 		mask = 0;
109 	}
110 	while (arg = *argv++)
111 	{
112 		if (mkdir(arg, mode) < 0)
113 		{
114 			if (!pflag || !(errno == ENOENT || errno == EEXIST || errno == ENOTDIR))
115 			{
116 				error(ERROR_system(0), "%s:", arg);
117 				continue;
118 			}
119 			if (errno == EEXIST)
120 				continue;
121 
122 			/*
123 			 * -p option, preserve intermediates
124 			 * first eliminate trailing /'s
125 			 */
126 
127 			n = strlen(arg);
128 			while (n > 0 && arg[--n] == '/');
129 			arg[n + 1] = 0;
130 			for (name = arg, n = *arg; n;)
131 			{
132 				/* skip over slashes */
133 				while (*arg == '/')
134 					arg++;
135 				/* skip to next component */
136 				while ((n = *arg) && n != '/')
137 					arg++;
138 				*arg = 0;
139 				if (mkdir(name, n ? dmode : mode) < 0 && errno != EEXIST && access(name, F_OK) < 0)
140 				{
141 					*arg = n;
142 					error(ERROR_system(0), "%s:", name);
143 					break;
144 				}
145 				*arg = n;
146 			}
147 		}
148 	}
149 	if (mask)
150 		umask(mask);
151 	return error_info.errors != 0;
152 }
153