]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/mkdir/mkdir.c
This commit was generated by cvs2svn to compensate for changes in r89010,
[FreeBSD/FreeBSD.git] / bin / 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 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
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 int     build __P((char *, mode_t));
61 int     main __P((int, char *[]));
62 void    usage __P((void));
63
64 int vflag;
65
66 int
67 main(argc, argv)
68         int argc;
69         char *argv[];
70 {
71         int ch, exitval, success, omode, pflag;
72         mode_t *set = (mode_t *)NULL;
73         char *mode;
74
75         omode = pflag = 0;
76         mode = NULL;
77         while ((ch = getopt(argc, argv, "m:pv")) != -1)
78                 switch(ch) {
79                 case 'm':
80                         mode = optarg;
81                         break;
82                 case 'p':
83                         pflag = 1;
84                         break;
85                 case 'v':
86                         vflag = 1;
87                         break;
88                 case '?':
89                 default:
90                         usage();
91                 }
92
93         argc -= optind;
94         argv += optind;
95         if (argv[0] == NULL)
96                 usage();
97
98         if (mode == NULL) {
99                 omode = S_IRWXU | S_IRWXG | S_IRWXO;
100         } else {
101                 if ((set = setmode(mode)) == NULL)
102                         errx(1, "invalid file mode: %s", mode);
103                 omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
104                 free(set);
105         }
106
107         for (exitval = 0; *argv != NULL; ++argv) {
108                 success = 1;
109                 if (pflag) {
110                         if (build(*argv, omode))
111                                 success = 0;
112                 } else if (mkdir(*argv, omode) < 0) {
113                         if (errno == ENOTDIR || errno == ENOENT)
114                                 warn("%s", dirname(*argv));
115                         else
116                                 warn("%s", *argv);
117                         success = 0;
118                 } else if (vflag)
119                         (void)printf("%s\n", *argv);
120                 
121                 if (!success)
122                         exitval = 1;
123                 /*
124                  * The mkdir() and umask() calls both honor only the low
125                  * nine bits, so if you try to set a mode including the
126                  * sticky, setuid, setgid bits you lose them.  Don't do
127                  * this unless the user has specifically requested a mode,
128                  * as chmod will (obviously) ignore the umask.
129                  */
130                 if (success && mode != NULL && chmod(*argv, omode) == -1) {
131                         warn("%s", *argv);
132                         exitval = 1;
133                 }
134         }
135         exit(exitval);
136 }
137
138 int
139 build(path, omode)
140         char *path;
141         mode_t omode;
142 {
143         struct stat sb;
144         mode_t numask, oumask;
145         int first, last, retval;
146         char *p;
147
148         p = path;
149         oumask = 0;
150         retval = 0;
151         if (p[0] == '/')                /* Skip leading '/'. */
152                 ++p;
153         for (first = 1, last = 0; !last ; ++p) {
154                 if (p[0] == '\0')
155                         last = 1;
156                 else if (p[0] != '/')
157                         continue;
158                 *p = '\0';
159                 if (p[1] == '\0')
160                         last = 1;
161                 if (first) {
162                         /*
163                          * POSIX 1003.2:
164                          * For each dir operand that does not name an existing
165                          * directory, effects equivalent to those cased by the
166                          * following command shall occcur:
167                          *
168                          * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
169                          *    mkdir [-m mode] dir
170                          *
171                          * We change the user's umask and then restore it,
172                          * instead of doing chmod's.
173                          */
174                         oumask = umask(0);
175                         numask = oumask & ~(S_IWUSR | S_IXUSR);
176                         (void)umask(numask);
177                         first = 0;
178                 }
179                 if (last)
180                         (void)umask(oumask);
181                 if (stat(path, &sb)) {
182                         if (errno != ENOENT ||
183                             mkdir(path, last ? omode : 
184                                   S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
185                                 warn("%s", path);
186                                 retval = 1;
187                                 break;
188                         } else if (vflag)
189                                 printf("%s\n", path);
190                 }
191                 else if ((sb.st_mode & S_IFMT) != S_IFDIR) {
192                         if (last)
193                                 errno = EEXIST;
194                         else
195                                 errno = ENOTDIR;
196                         warn("%s", path);
197                         retval = 1;
198                         break;
199                 }
200                 if (!last)
201                     *p = '/';
202         }
203         if (!first && !last)
204                 (void)umask(oumask);
205         return (retval);
206 }
207
208 void
209 usage()
210 {
211
212         (void)fprintf(stderr, "usage: mkdir [-pv] [-m mode] directory ...\n");
213         exit (EX_USAGE);
214 }