mkdir.c revision 20418
1169689Skan/* 2169689Skan * Copyright (c) 1983, 1992, 1993 3169689Skan * The Regents of the University of California. All rights reserved. 4169689Skan * 5169689Skan * Redistribution and use in source and binary forms, with or without 6169689Skan * modification, are permitted provided that the following conditions 7169689Skan * are met: 8169689Skan * 1. Redistributions of source code must retain the above copyright 9169689Skan * notice, this list of conditions and the following disclaimer. 10169689Skan * 2. Redistributions in binary form must reproduce the above copyright 11169689Skan * notice, this list of conditions and the following disclaimer in the 12169689Skan * documentation and/or other materials provided with the distribution. 13169689Skan * 3. All advertising materials mentioning features or use of this software 14169689Skan * must display the following acknowledgement: 15169689Skan * This product includes software developed by the University of 16169689Skan * California, Berkeley and its contributors. 17169689Skan * 4. Neither the name of the University nor the names of its contributors 18169689Skan * may be used to endorse or promote products derived from this software 19169689Skan * without specific prior written permission. 20169689Skan * 21169689Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24169689Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31169689Skan * SUCH DAMAGE. 32169689Skan * 33169689Skan * $Id: mkdir.c,v 1.5 1994/09/24 02:56:03 davidg Exp $ 34169689Skan */ 35169689Skan 36169689Skan#ifndef lint 37169689Skanstatic char const copyright[] = 38169689Skan"@(#) Copyright (c) 1983, 1992, 1993\n\ 39169689Skan The Regents of the University of California. All rights reserved.\n"; 40169689Skan#endif /* not lint */ 41169689Skan 42169689Skan#ifndef lint 43169689Skanstatic char const sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 1/25/94"; 44169689Skan#endif /* not lint */ 45169689Skan 46169689Skan#include <sys/types.h> 47169689Skan#include <sys/stat.h> 48169689Skan 49169689Skan#include <err.h> 50169689Skan#include <errno.h> 51169689Skan#include <stdio.h> 52169689Skan#include <stdlib.h> 53169689Skan#include <string.h> 54169689Skan#include <unistd.h> 55169689Skan 56169689Skanint build __P((char *)); 57169689Skanvoid usage __P((void)); 58169689Skan 59169689Skanint 60169689Skanmain(argc, argv) 61169689Skan int argc; 62169689Skan char *argv[]; 63169689Skan{ 64169689Skan int ch, exitval, oct, omode, pflag; 65169689Skan mode_t *set = (mode_t *)NULL; 66169689Skan char *ep, *mode; 67169689Skan 68169689Skan omode = pflag = 0; 69169689Skan mode = NULL; 70169689Skan while ((ch = getopt(argc, argv, "m:p")) != EOF) 71169689Skan switch(ch) { 72169689Skan case 'p': 73169689Skan pflag = 1; 74169689Skan break; 75169689Skan case 'm': 76169689Skan mode = optarg; 77169689Skan break; 78169689Skan case '?': 79169689Skan default: 80169689Skan usage(); 81169689Skan } 82169689Skan 83169689Skan argc -= optind; 84169689Skan argv += optind; 85169689Skan if (argv[0] == NULL) 86169689Skan usage(); 87169689Skan 88169689Skan if (mode == NULL) { 89169689Skan omode = S_IRWXU | S_IRWXG | S_IRWXO; 90169689Skan oct = 1; 91169689Skan } else if (*mode >= '0' && *mode <= '7') { 92169689Skan omode = (int)strtol(mode, &ep, 8); 93169689Skan if (omode < 0 || *ep) 94169689Skan errx(1, "invalid file mode: %s", mode); 95169689Skan oct = 1; 96169689Skan } else { 97169689Skan if ((set = setmode(mode)) == NULL) 98169689Skan errx(1, "invalid file mode: %s", mode); 99169689Skan oct = 0; 100169689Skan } 101169689Skan 102169689Skan for (exitval = 0; *argv != NULL; ++argv) { 103169689Skan if (pflag && build(*argv)) { 104169689Skan exitval = 1; 105169689Skan continue; 106169689Skan } 107169689Skan if (mkdir(*argv, oct ? 108169689Skan omode : getmode(set, S_IRWXU | S_IRWXG | S_IRWXO)) < 0) { 109169689Skan if (!pflag) { 110169689Skan warn("%s", *argv); 111169689Skan exitval = 1; 112169689Skan } 113169689Skan } 114169689Skan } 115169689Skan exit(exitval); 116169689Skan} 117169689Skan 118169689Skanint 119169689Skanbuild(path) 120169689Skan char *path; 121169689Skan{ 122169689Skan struct stat sb; 123169689Skan mode_t numask, oumask; 124169689Skan int first; 125169689Skan char *p; 126169689Skan 127169689Skan p = path; 128169689Skan oumask = 0; 129169689Skan if (p[0] == '/') /* Skip leading '/'. */ 130169689Skan ++p; 131169689Skan for (first = 1;; ++p) { 132169689Skan if (p[0] == '\0' || (p[0] == '/' && p[1] == '\0')) 133169689Skan break; 134169689Skan if (p[0] != '/') 135169689Skan continue; 136169689Skan *p = '\0'; 137169689Skan if (first) { 138169689Skan /* 139169689Skan * POSIX 1003.2: 140169689Skan * For each dir operand that does not name an existing 141169689Skan * directory, effects equivalent to those cased by the 142169689Skan * following command shall occcur: 143169689Skan * 144169689Skan * mkdir -p -m $(umask -S),u+wx $(dirname dir) && 145169689Skan * mkdir [-m mode] dir 146169689Skan * 147169689Skan * We change the user's umask and then restore it, 148169689Skan * instead of doing chmod's. 149169689Skan */ 150169689Skan oumask = umask(0); 151169689Skan numask = oumask & ~(S_IWUSR | S_IXUSR); 152169689Skan (void)umask(numask); 153169689Skan first = 0; 154169689Skan } 155169689Skan if (stat(path, &sb)) { 156169689Skan if (errno != ENOENT || 157169689Skan mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 158169689Skan warn("%s", path); 159169689Skan return (1); 160169689Skan } 161169689Skan } 162169689Skan *p = '/'; 163169689Skan } 164169689Skan if (!first) 165169689Skan (void)umask(oumask); 166169689Skan return (0); 167169689Skan} 168169689Skan 169169689Skanvoid 170169689Skanusage() 171169689Skan{ 172169689Skan (void)fprintf(stderr, "usage: mkdir [-p] [-m mode] directory ...\n"); 173169689Skan exit (1); 174169689Skan} 175169689Skan