sync
[bitrig.git] / bin / mkdir / mkdir.c
blob1ce449f1697ecb2f50846e85891ec724e2b61b2f
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 $ */
4 /*
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
10 * are met:
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
30 * SUCH DAMAGE.
33 #include <sys/types.h>
34 #include <sys/stat.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <locale.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
44 extern char *__progname;
46 int mkpath(char *, mode_t, mode_t);
47 void usage(void);
49 int
50 main(int argc, char *argv[])
52 int ch, rv, exitval, pflag;
53 void *set;
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;
67 pflag = 0;
68 while ((ch = getopt(argc, argv, "m:p")) != -1)
69 switch(ch) {
70 case 'p':
71 pflag = 1;
72 break;
73 case 'm':
74 if ((set = setmode(optarg)) == NULL)
75 errx(1, "invalid file mode: %s", optarg);
76 mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
77 free(set);
78 break;
79 default:
80 usage();
82 argc -= optind;
83 argv += optind;
85 if (*argv == NULL)
86 usage();
88 for (exitval = 0; *argv != NULL; ++argv) {
89 char *slash;
91 /* Remove trailing slashes, per POSIX. */
92 slash = strrchr(*argv, '\0');
93 while (--slash > *argv && *slash == '/')
94 *slash = '\0';
96 if (pflag) {
97 rv = mkpath(*argv, mode, dir_mode);
98 } else {
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);
110 if (rv < 0) {
111 warn("%s", *argv);
112 exitval = 1;
115 exit(exitval);
119 * mkpath -- create directories.
120 * path - path
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)
127 struct stat sb;
128 char *slash;
129 int done;
131 slash = path;
133 for (;;) {
134 slash += strspn(slash, "/");
135 slash += strcspn(slash, "/");
137 done = (*slash == '\0');
138 *slash = '\0';
140 if (mkdir(path, done ? mode : dir_mode) == 0) {
141 if (mode > 0777 && chmod(path, mode) < 0)
142 return (-1);
143 } else {
144 int mkdir_errno = errno;
146 if (stat(path, &sb)) {
147 /* Not there; use mkdir()s errno */
148 errno = mkdir_errno;
149 return (-1);
151 if (!S_ISDIR(sb.st_mode)) {
152 /* Is there, but isn't a directory */
153 errno = ENOTDIR;
154 return (-1);
158 if (done)
159 break;
161 *slash = '/';
164 return (0);
167 void
168 usage(void)
170 (void)fprintf(stderr, "usage: %s [-p] [-m mode] directory ...\n",
171 __progname);
172 exit(1);