1 /*********************************************************************** 2 * * 3 * This software is part of the ast package * 4 * Copyright (c) 1992-2008 AT&T Intellectual Property * 5 * and is licensed under the * 6 * Common Public License, Version 1.0 * 7 * by AT&T Intellectual Property * 8 * * 9 * A copy of the License is available at * 10 * http://www.opensource.org/licenses/cpl1.0.txt * 11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 12 * * 13 * Information and Software Systems Research * 14 * AT&T Research * 15 * Florham Park NJ * 16 * * 17 * Glenn Fowler <gsf@research.att.com> * 18 * David Korn <dgk@research.att.com> * 19 * * 20 ***********************************************************************/ 21 #pragma prototyped 22 /* 23 * David Korn 24 * AT&T Bell Laboratories 25 * 26 * mkdir 27 */ 28 29 static const char usage[] = 30 "[-?\n@(#)$Id: mkdir (AT&T Research) 2007-04-25 $\n]" 31 USAGE_LICENSE 32 "[+NAME?mkdir - make directories]" 33 "[+DESCRIPTION?\bmkdir\b creates one or more directories. By " 34 "default, the mode of created directories is \ba=rwx\b minus the " 35 "bits set in the \bumask\b(1).]" 36 "[m:mode]:[mode?Set the mode of created directories to \amode\a. " 37 "\amode\a is symbolic or octal mode as in \bchmod\b(1). Relative " 38 "modes assume an initial mode of \ba=rwx\b.]" 39 "[p:parents?Create any missing intermediate pathname components. For " 40 "each dir operand that does not name an existing directory, effects " 41 "equivalent to those caused by the following command shall occur: " 42 "\vmkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode]] " 43 "dir\v where the \b-m\b mode option represents that option supplied to " 44 "the original invocation of \bmkdir\b, if any. Each dir operand that " 45 "names an existing directory shall be ignored without error.]" 46 "\n" 47 "\ndirectory ...\n" 48 "\n" 49 "[+EXIT STATUS?]{" 50 "[+0?All directories created successfully, or the \b-p\b option " 51 "was specified and all the specified directories now exist.]" 52 "[+>0?An error occurred.]" 53 "}" 54 "[+SEE ALSO?\bchmod\b(1), \brmdir\b(1), \bumask\b(1)]" 55 ; 56 57 #include <cmd.h> 58 #include <ls.h> 59 60 #define DIRMODE (S_IRWXU|S_IRWXG|S_IRWXO) 61 62 int 63 b_mkdir(int argc, char** argv, void* context) 64 { 65 register char* arg; 66 register int n; 67 register mode_t mode = DIRMODE; 68 register mode_t mask = 0; 69 register int mflag = 0; 70 register int pflag = 0; 71 char* name; 72 mode_t dmode; 73 struct stat st; 74 75 cmdinit(argc, argv, context, ERROR_CATALOG, 0); 76 while (n = optget(argv, usage)) switch (n) 77 { 78 case 'p': 79 pflag = 1; 80 break; 81 case 'm': 82 mflag = 1; 83 mode = strperm(arg = opt_info.arg, &opt_info.arg, mode); 84 if (*opt_info.arg) 85 error(ERROR_exit(0), "%s: invalid mode", arg); 86 break; 87 case ':': 88 error(2, "%s", opt_info.arg); 89 break; 90 case '?': 91 error(ERROR_usage(2), "%s", opt_info.arg); 92 break; 93 } 94 argv += opt_info.index; 95 if (error_info.errors || !*argv) 96 error(ERROR_usage(2), "%s", optusage(NiL)); 97 mask = umask(0); 98 if (mflag || pflag) 99 { 100 dmode = DIRMODE & ~mask; 101 if (!mflag) 102 mode = dmode; 103 dmode |= S_IWUSR | S_IXUSR; 104 } 105 else 106 { 107 mode &= ~mask; 108 umask(mask); 109 mask = 0; 110 } 111 while (arg = *argv++) 112 { 113 if (mkdir(arg, mode) < 0) 114 { 115 if (!pflag || !(errno == ENOENT || errno == EEXIST || errno == ENOTDIR)) 116 { 117 error(ERROR_system(0), "%s:", arg); 118 continue; 119 } 120 if (errno == EEXIST) 121 continue; 122 123 /* 124 * -p option, preserve intermediates 125 * first eliminate trailing /'s 126 */ 127 128 n = strlen(arg); 129 while (n > 0 && arg[--n] == '/'); 130 arg[n + 1] = 0; 131 for (name = arg, n = *arg; n;) 132 { 133 /* skip over slashes */ 134 while (*arg == '/') 135 arg++; 136 /* skip to next component */ 137 while ((n = *arg) && n != '/') 138 arg++; 139 *arg = 0; 140 if (mkdir(name, n ? dmode : mode) < 0 && errno != EEXIST && access(name, F_OK) < 0) 141 { 142 *arg = n; 143 error(ERROR_system(0), "%s:", name); 144 break; 145 } 146 if (!(*arg = n) && (mode & (S_ISVTX|S_ISUID|S_ISGID))) 147 { 148 if (stat(name, &st)) 149 { 150 error(ERROR_system(0), "%s: cannot stat", name); 151 break; 152 } 153 if ((st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)) != (mode & (S_ISVTX|S_ISUID|S_ISGID)) && chmod(name, mode)) 154 { 155 error(ERROR_system(0), "%s: cannot change mode from %s to %s", name, fmtperm(st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)), fmtperm(mode)); 156 break; 157 } 158 } 159 } 160 } 161 } 162 if (mask) 163 umask(mask); 164 return error_info.errors != 0; 165 } 166