1 /***********************************************************************
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 *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
20 ***********************************************************************/
24 * AT&T Bell Laboratories
29 static const char usage
[] =
30 "[-?\n@(#)$Id: mkdir (AT&T Research) 2009-12-03 $\n]"
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 "
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.]"
56 "[+SEE ALSO?\bchmod\b(1), \brmdir\b(1), \bumask\b(1)]"
62 #define DIRMODE (S_IRWXU|S_IRWXG|S_IRWXO)
65 b_mkdir(int argc
, char** argv
, void* context
)
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;
78 cmdinit(argc
, argv
, context
, ERROR_CATALOG
, 0);
81 switch (optget(argv
, usage
))
87 mode
= strperm(arg
= opt_info
.arg
, &opt_info
.arg
, mode
);
89 error(ERROR_exit(0), "%s: invalid mode", arg
);
98 error(2, "%s", opt_info
.arg
);
101 error(ERROR_usage(2), "%s", opt_info
.arg
);
106 argv
+= opt_info
.index
;
107 if (error_info
.errors
|| !*argv
)
108 error(ERROR_usage(2), "%s", optusage(NiL
));
112 dmode
= DIRMODE
& ~mask
;
115 dmode
|= S_IWUSR
| S_IXUSR
;
123 while (arg
= *argv
++)
125 if (mkdir(arg
, mode
) < 0)
127 if (!pflag
|| !(errno
== ENOENT
|| errno
== EEXIST
|| errno
== ENOTDIR
))
129 error(ERROR_system(0), "%s:", arg
);
136 * -p option, preserve intermediates
137 * first eliminate trailing /'s
141 while (n
> 0 && arg
[--n
] == '/');
143 for (name
= arg
, n
= *arg
; n
;)
145 /* skip over slashes */
148 /* skip to next component */
149 while ((n
= *arg
) && n
!= '/')
152 if (mkdir(name
, n
? dmode
: mode
) < 0 && errno
!= EEXIST
&& access(name
, F_OK
) < 0)
155 error(ERROR_system(0), "%s:", name
);
159 error(0, "%s: directory created", name
);
160 if (!(*arg
= n
) && (mode
& (S_ISVTX
|S_ISUID
|S_ISGID
)))
164 error(ERROR_system(0), "%s: cannot stat", name
);
167 if ((st
.st_mode
& (S_ISVTX
|S_ISUID
|S_ISGID
)) != (mode
& (S_ISVTX
|S_ISUID
|S_ISGID
)) && chmod(name
, mode
))
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
));
176 error(0, "%s: directory created", arg
);
180 return error_info
.errors
!= 0;