1/* $NetBSD: mkdir.c,v 1.37 2008/07/20 00:52:40 lukem Exp $ */ 2 3/* 4 * Copyright (c) 1983, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33#ifndef lint 34__COPYRIGHT("@(#) Copyright (c) 1983, 1992, 1993\ 35 The Regents of the University of California. All rights reserved."); 36#endif /* not lint */ 37 38#ifndef lint 39#if 0 40static char sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 1/25/94"; 41#else 42__RCSID("$NetBSD: mkdir.c,v 1.37 2008/07/20 00:52:40 lukem Exp $"); 43#endif 44#endif /* not lint */ 45 46#include <sys/param.h> 47#include <sys/stat.h> 48#include <sys/types.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 58static int mkpath(char *, mode_t, mode_t); 59__dead static void usage(void); 60 61int 62main(int argc, char *argv[]) 63{ 64 int ch, exitval, pflag; 65 void *set; 66 mode_t mode, dir_mode; 67 68 setprogname(argv[0]); 69 (void)setlocale(LC_ALL, ""); 70 71 /* 72 * The default file mode is a=rwx (0777) with selected permissions 73 * removed in accordance with the file mode creation mask. For 74 * intermediate path name components, the mode is the default modified 75 * by u+wx so that the subdirectories can always be created. 76 */ 77 mode = (S_IRWXU | S_IRWXG | S_IRWXO) & ~umask(0); 78 dir_mode = mode | S_IWUSR | S_IXUSR; 79 80 pflag = 0; 81 while ((ch = getopt(argc, argv, "m:p")) != -1) 82 switch (ch) { 83 case 'p': 84 pflag = 1; 85 break; 86 case 'm': 87 if ((set = setmode(optarg)) == NULL) { 88 err(EXIT_FAILURE, "Cannot set file mode `%s'", 89 optarg); 90 /* NOTREACHED */ 91 } 92 mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO); 93 free(set); 94 break; 95 case '?': 96 default: 97 usage(); 98 /* NOTREACHED */ 99 } 100 argc -= optind; 101 argv += optind; 102 103 if (*argv == NULL) { 104 usage(); 105 /* NOTREACHED */ 106 } 107 108 for (exitval = EXIT_SUCCESS; *argv != NULL; ++argv) { 109#ifdef notdef 110 char *slash; 111 112 /* Kernel takes care of this */ 113 /* Remove trailing slashes, per POSIX. */ 114 slash = strrchr(*argv, '\0'); 115 while (--slash > *argv && *slash == '/') 116 *slash = '\0'; 117#endif 118 119 if (pflag) { 120 if (mkpath(*argv, mode, dir_mode) < 0) 121 exitval = EXIT_FAILURE; 122 } else { 123 if (mkdir(*argv, mode) < 0) { 124 warn("%s", *argv); 125 exitval = EXIT_FAILURE; 126 } else { 127 /* 128 * The mkdir() and umask() calls both honor 129 * only the file permission bits, so if you try 130 * to set a mode including the sticky, setuid, 131 * setgid bits you lose them. So chmod(). 132 */ 133 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) != 0 && 134 chmod(*argv, mode) == -1) { 135 warn("%s", *argv); 136 exitval = EXIT_FAILURE; 137 } 138 } 139 } 140 } 141 exit(exitval); 142 /* NOTREACHED */ 143} 144 145/* 146 * mkpath -- create directories. 147 * path - path 148 * mode - file mode of terminal directory 149 * dir_mode - file mode of intermediate directories 150 */ 151static int 152mkpath(char *path, mode_t mode, mode_t dir_mode) 153{ 154 struct stat sb; 155 char *slash; 156 int done, rv; 157 158 done = 0; 159 slash = path; 160 161 for (;;) { 162 slash += strspn(slash, "/"); 163 slash += strcspn(slash, "/"); 164 165 done = (*slash == '\0'); 166 *slash = '\0'; 167 168 rv = mkdir(path, done ? mode : dir_mode); 169 if (rv < 0) { 170 /* 171 * Can't create; path exists or no perms. 172 * stat() path to determine what's there now. 173 */ 174 int sverrno; 175 176 sverrno = errno; 177 if (stat(path, &sb) < 0) { 178 /* Not there; use mkdir()s error */ 179 errno = sverrno; 180 warn("%s", path); 181 return -1; 182 } 183 if (!S_ISDIR(sb.st_mode)) { 184 /* Is there, but isn't a directory */ 185 errno = ENOTDIR; 186 warn("%s", path); 187 return -1; 188 } 189 } else if (done) { 190 /* 191 * Created ok, and this is the last element 192 */ 193 /* 194 * The mkdir() and umask() calls both honor only the 195 * file permission bits, so if you try to set a mode 196 * including the sticky, setuid, setgid bits you lose 197 * them. So chmod(). 198 */ 199 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) != 0 && 200 chmod(path, mode) == -1) { 201 warn("%s", path); 202 return -1; 203 } 204 } 205 206 if (done) { 207 break; 208 } 209 *slash = '/'; 210 } 211 212 return 0; 213} 214 215static void 216usage(void) 217{ 218 219 (void)fprintf(stderr, "usage: %s [-p] [-m mode] dirname ...\n", 220 getprogname()); 221 exit(EXIT_FAILURE); 222 /* NOTREACHED */ 223} 224