file_cmds-220.4.tar.gz
[apple/file_cmds.git] / mkdir / mkdir.c
1 /*
2 * Copyright (c) 1983, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char const copyright[] =
36 "@(#) Copyright (c) 1983, 1992, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 1/25/94";
43 #endif
44 #endif /* not lint */
45 #include <sys/cdefs.h>
46 __RCSID("$FreeBSD: src/bin/mkdir/mkdir.c,v 1.26 2002/06/30 05:13:54 obrien Exp $");
47
48 #include <sys/types.h>
49 #include <sys/stat.h>
50
51 #include <err.h>
52 #include <errno.h>
53 #include <libgen.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <sysexits.h>
58 #include <unistd.h>
59
60 static void usage(void);
61
62 int vflag;
63
64 int
65 main(int argc, char *argv[])
66 {
67 int ch, exitval, success, pflag;
68 mode_t omode, *set = (mode_t *)NULL;
69 char *mode;
70
71 pflag = 0;
72 mode = NULL;
73 while ((ch = getopt(argc, argv, "m:pv")) != -1)
74 switch(ch) {
75 case 'm':
76 mode = optarg;
77 break;
78 case 'p':
79 pflag = 1;
80 break;
81 case 'v':
82 vflag = 1;
83 break;
84 case '?':
85 default:
86 usage();
87 }
88
89 // argc -= optind;
90 argv += optind;
91 if (argv[0] == NULL)
92 usage();
93
94 if (mode == NULL) {
95 omode = S_IRWXU | S_IRWXG | S_IRWXO;
96 } else {
97 if ((set = setmode(mode)) == NULL)
98 errx(1, "invalid file mode: %s", mode);
99 omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
100 free(set);
101 }
102
103 for (exitval = 0; *argv != NULL; ++argv) {
104 success = 1;
105 if (pflag) {
106 int status = mkpath_np(*argv, omode);
107 if (status && status != EEXIST) {
108 warnc(status, "%s", *argv);
109 success = 0;
110 }
111 } else if (mkdir(*argv, omode) < 0) {
112 if (errno == ENOTDIR || errno == ENOENT)
113 warn("%s", dirname(*argv));
114 else
115 warn("%s", *argv);
116 success = 0;
117 } else if (vflag)
118 (void)printf("mkdir: created directory '%s'\n", *argv);
119
120 if (!success)
121 exitval = 1;
122 /*
123 * The mkdir() and umask() calls both honor only the low
124 * nine bits, so if you try to set a mode including the
125 * sticky, setuid, setgid bits you lose them. Don't do
126 * this unless the user has specifically requested a mode,
127 * as chmod will (obviously) ignore the umask.
128 */
129 if (success && mode != NULL && chmod(*argv, omode) == -1) {
130 warn("%s", *argv);
131 exitval = 1;
132 }
133 }
134 exit(exitval);
135 }
136
137 void
138 usage(void)
139 {
140
141 (void)fprintf(stderr, "usage: mkdir [-pv] [-m mode] directory ...\n");
142 exit (EX_USAGE);
143 }