1 /* $OpenBSD: mkdir.c,v 1.23 2008/09/30 23:25:31 millert Exp $ */ 2 /* $NetBSD: mkdir.c,v 1.14 1995/06/25 21:59:21 mycroft Exp $ */ 3 4 /* 5 * Copyright (c) 1983, 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 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. 19 * 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. 31 */ 32 33 #ifndef lint 34 static char copyright[] = 35 "@(#) Copyright (c) 1983, 1992, 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 1/25/94"; 42 #else 43 static char rcsid[] = "$OpenBSD: mkdir.c,v 1.23 2008/09/30 23:25:31 millert Exp $"; 44 #endif 45 #endif /* not lint */ 46 47 #include <sys/types.h> 48 #include <sys/stat.h> 49 50 #include <err.h> 51 #include <errno.h> 52 #include <locale.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <unistd.h> 57 58 extern char *__progname; 59 60 int mkpath(char *, mode_t, mode_t); 61 void usage(void); 62 63 int 64 main(int argc, char *argv[]) 65 { 66 int ch, rv, exitval, pflag; 67 void *set; 68 mode_t mode, dir_mode; 69 70 setlocale(LC_ALL, ""); 71 72 /* 73 * The default file mode is a=rwx (0777) with selected permissions 74 * removed in accordance with the file mode creation mask. For 75 * intermediate path name components, the mode is the default modified 76 * by u+wx so that the subdirectories can always be created. 77 */ 78 mode = 0777 & ~umask(0); 79 dir_mode = mode | S_IWUSR | S_IXUSR; 80 81 pflag = 0; 82 while ((ch = getopt(argc, argv, "m:p")) != -1) 83 switch(ch) { 84 case 'p': 85 pflag = 1; 86 break; 87 case 'm': 88 if ((set = setmode(optarg)) == NULL) 89 errx(1, "invalid file mode: %s", optarg); 90 mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO); 91 free(set); 92 break; 93 default: 94 usage(); 95 } 96 argc -= optind; 97 argv += optind; 98 99 if (*argv == NULL) 100 usage(); 101 102 for (exitval = 0; *argv != NULL; ++argv) { 103 char *slash; 104 105 /* Remove trailing slashes, per POSIX. */ 106 slash = strrchr(*argv, '\0'); 107 while (--slash > *argv && *slash == '/') 108 *slash = '\0'; 109 110 if (pflag) { 111 rv = mkpath(*argv, mode, dir_mode); 112 } else { 113 rv = mkdir(*argv, mode); 114 /* 115 * The mkdir() and umask() calls both honor only the 116 * low nine bits, so if you try to set a mode including 117 * the sticky, setuid, setgid bits you lose them. Don't 118 * do this unless the user has specifically requested 119 * a mode as chmod will (obviously) ignore the umask. 120 */ 121 if (rv == 0 && mode > 0777) 122 rv = chmod(*argv, mode); 123 } 124 if (rv < 0) { 125 warn("%s", *argv); 126 exitval = 1; 127 } 128 } 129 exit(exitval); 130 } 131 132 /* 133 * mkpath -- create directories. 134 * path - path 135 * mode - file mode of terminal directory 136 * dir_mode - file mode of intermediate directories 137 */ 138 int 139 mkpath(char *path, mode_t mode, mode_t dir_mode) 140 { 141 struct stat sb; 142 char *slash; 143 int done, exists; 144 145 slash = path; 146 147 for (;;) { 148 slash += strspn(slash, "/"); 149 slash += strcspn(slash, "/"); 150 151 done = (*slash == '\0'); 152 *slash = '\0'; 153 154 /* skip existing path components */ 155 exists = !stat(path, &sb); 156 if (!done && exists && S_ISDIR(sb.st_mode)) { 157 *slash = '/'; 158 continue; 159 } 160 161 if (mkdir(path, done ? mode : dir_mode) == 0) { 162 if (mode > 0777 && chmod(path, mode) < 0) 163 return (-1); 164 } else { 165 if (!exists) { 166 /* Not there */ 167 return (-1); 168 } 169 if (!S_ISDIR(sb.st_mode)) { 170 /* Is there, but isn't a directory */ 171 errno = ENOTDIR; 172 return (-1); 173 } 174 } 175 176 if (done) 177 break; 178 179 *slash = '/'; 180 } 181 182 return (0); 183 } 184 185 void 186 usage(void) 187 { 188 (void)fprintf(stderr, "usage: %s [-p] [-m mode] directory ...\n", 189 __progname); 190 exit(1); 191 } 192