1 /* $OpenBSD: mkdir.c,v 1.25 2013/04/02 20:26:17 naddy Exp $ */
2 /* $NetBSD: mkdir.c,v 1.14 1995/06/25 21:59:21 mycroft Exp $ */
5 * Copyright (c) 1983, 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/types.h>
44 extern char *__progname
;
46 int mkpath(char *, mode_t
, mode_t
);
50 main(int argc
, char *argv
[])
52 int ch
, rv
, exitval
, pflag
;
54 mode_t mode
, dir_mode
;
56 setlocale(LC_ALL
, "");
59 * The default file mode is a=rwx (0777) with selected permissions
60 * removed in accordance with the file mode creation mask. For
61 * intermediate path name components, the mode is the default modified
62 * by u+wx so that the subdirectories can always be created.
64 mode
= 0777 & ~umask(0);
65 dir_mode
= mode
| S_IWUSR
| S_IXUSR
;
68 while ((ch
= getopt(argc
, argv
, "m:p")) != -1)
74 if ((set
= setmode(optarg
)) == NULL
)
75 errx(1, "invalid file mode: %s", optarg
);
76 mode
= getmode(set
, S_IRWXU
| S_IRWXG
| S_IRWXO
);
88 for (exitval
= 0; *argv
!= NULL
; ++argv
) {
91 /* Remove trailing slashes, per POSIX. */
92 slash
= strrchr(*argv
, '\0');
93 while (--slash
> *argv
&& *slash
== '/')
97 rv
= mkpath(*argv
, mode
, dir_mode
);
99 rv
= mkdir(*argv
, mode
);
101 * The mkdir() and umask() calls both honor only the
102 * low nine bits, so if you try to set a mode including
103 * the sticky, setuid, setgid bits you lose them. Don't
104 * do this unless the user has specifically requested
105 * a mode as chmod will (obviously) ignore the umask.
107 if (rv
== 0 && mode
> 0777)
108 rv
= chmod(*argv
, mode
);
119 * mkpath -- create directories.
121 * mode - file mode of terminal directory
122 * dir_mode - file mode of intermediate directories
125 mkpath(char *path
, mode_t mode
, mode_t dir_mode
)
134 slash
+= strspn(slash
, "/");
135 slash
+= strcspn(slash
, "/");
137 done
= (*slash
== '\0');
140 if (mkdir(path
, done
? mode
: dir_mode
) == 0) {
141 if (mode
> 0777 && chmod(path
, mode
) < 0)
144 int mkdir_errno
= errno
;
146 if (stat(path
, &sb
)) {
147 /* Not there; use mkdir()s errno */
151 if (!S_ISDIR(sb
.st_mode
)) {
152 /* Is there, but isn't a directory */
170 (void)fprintf(stderr
, "usage: %s [-p] [-m mode] directory ...\n",