mkdir.c revision 76886
1115921Sache/* 2115921Sache * Copyright (c) 1983, 1992, 1993 3115921Sache * The Regents of the University of California. All rights reserved. 4115921Sache * 5115921Sache * Redistribution and use in source and binary forms, with or without 6115921Sache * modification, are permitted provided that the following conditions 7115921Sache * are met: 8115921Sache * 1. Redistributions of source code must retain the above copyright 9115921Sache * notice, this list of conditions and the following disclaimer. 10115921Sache * 2. Redistributions in binary form must reproduce the above copyright 11115921Sache * notice, this list of conditions and the following disclaimer in the 12115921Sache * documentation and/or other materials provided with the distribution. 13115921Sache * 3. All advertising materials mentioning features or use of this software 14115921Sache * must display the following acknowledgement: 15115921Sache * This product includes software developed by the University of 16115921Sache * California, Berkeley and its contributors. 17115921Sache * 4. Neither the name of the University nor the names of its contributors 18115921Sache * may be used to endorse or promote products derived from this software 19115921Sache * without specific prior written permission. 20115921Sache * 21115921Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22115921Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23115921Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24115921Sache * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25115921Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26115921Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27115921Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28115921Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29115921Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30115921Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31115921Sache * SUCH DAMAGE. 32115921Sache */ 33115921Sache 34115925Sache#ifndef lint 35115921Sachestatic char const copyright[] = 36115921Sache"@(#) Copyright (c) 1983, 1992, 1993\n\ 37115921Sache The Regents of the University of California. All rights reserved.\n"; 38115921Sache#endif /* not lint */ 39115925Sache 40115921Sache#ifndef lint 41115921Sache#if 0 42115921Sachestatic char sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 1/25/94"; 43115921Sache#endif 44static const char rcsid[] = 45 "$FreeBSD: head/bin/mkdir/mkdir.c 76886 2001-05-20 05:49:20Z kris $"; 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 60int build __P((char *, mode_t)); 61int main __P((int, char *[])); 62void usage __P((void)); 63 64int vflag; 65 66int 67main(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 138int 139build(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 208void 209usage() 210{ 211 212 (void)fprintf(stderr, "usage: mkdir [-pv] [-m mode] directory ...\n"); 213 exit (EX_USAGE); 214} 215