1139969Simp/*- 21556Srgrimes * Copyright (c) 1983, 1992, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * Redistribution and use in source and binary forms, with or without 61556Srgrimes * modification, are permitted provided that the following conditions 71556Srgrimes * are met: 81556Srgrimes * 1. Redistributions of source code must retain the above copyright 91556Srgrimes * notice, this list of conditions and the following disclaimer. 101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111556Srgrimes * notice, this list of conditions and the following disclaimer in the 121556Srgrimes * documentation and/or other materials provided with the distribution. 131556Srgrimes * 4. Neither the name of the University nor the names of its contributors 141556Srgrimes * may be used to endorse or promote products derived from this software 151556Srgrimes * without specific prior written permission. 161556Srgrimes * 171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271556Srgrimes * SUCH DAMAGE. 281556Srgrimes */ 291556Srgrimes 30114433Sobrien#if 0 311556Srgrimes#ifndef lint 3220418Sstevestatic char const copyright[] = 331556Srgrimes"@(#) Copyright (c) 1983, 1992, 1993\n\ 341556Srgrimes The Regents of the University of California. All rights reserved.\n"; 351556Srgrimes#endif /* not lint */ 361556Srgrimes 371556Srgrimes#ifndef lint 3836048Scharnierstatic char sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 1/25/94"; 39114433Sobrien#endif /* not lint */ 4036048Scharnier#endif 4199109Sobrien#include <sys/cdefs.h> 4299109Sobrien__FBSDID("$FreeBSD$"); 431556Srgrimes 441556Srgrimes#include <sys/types.h> 451556Srgrimes#include <sys/stat.h> 461556Srgrimes 471556Srgrimes#include <err.h> 481556Srgrimes#include <errno.h> 4971029Salfred#include <libgen.h> 501556Srgrimes#include <stdio.h> 511556Srgrimes#include <stdlib.h> 521556Srgrimes#include <string.h> 5350528Smharo#include <sysexits.h> 541556Srgrimes#include <unistd.h> 551556Srgrimes 5691084Smarkmstatic int build(char *, mode_t); 5791084Smarkmstatic void usage(void); 581556Srgrimes 5950528Smharoint vflag; 6050528Smharo 611556Srgrimesint 6290110Simpmain(int argc, char *argv[]) 631556Srgrimes{ 6491084Smarkm int ch, exitval, success, pflag; 65140816Sssouhlal mode_t omode; 66140816Sssouhlal void *set = NULL; 6724524Smpp char *mode; 681556Srgrimes 692923Sphk omode = pflag = 0; 701556Srgrimes mode = NULL; 7150528Smharo while ((ch = getopt(argc, argv, "m:pv")) != -1) 721556Srgrimes switch(ch) { 7350870Smharo case 'm': 7450870Smharo mode = optarg; 7550870Smharo break; 761556Srgrimes case 'p': 771556Srgrimes pflag = 1; 781556Srgrimes break; 7950528Smharo case 'v': 8050528Smharo vflag = 1; 8150528Smharo break; 821556Srgrimes case '?': 831556Srgrimes default: 841556Srgrimes usage(); 851556Srgrimes } 861556Srgrimes 871556Srgrimes argc -= optind; 881556Srgrimes argv += optind; 891556Srgrimes if (argv[0] == NULL) 901556Srgrimes usage(); 911556Srgrimes 921556Srgrimes if (mode == NULL) { 931556Srgrimes omode = S_IRWXU | S_IRWXG | S_IRWXO; 941556Srgrimes } else { 951556Srgrimes if ((set = setmode(mode)) == NULL) 961556Srgrimes errx(1, "invalid file mode: %s", mode); 9724524Smpp omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO); 9841842Simp free(set); 991556Srgrimes } 1001556Srgrimes 1011556Srgrimes for (exitval = 0; *argv != NULL; ++argv) { 10224524Smpp if (pflag) { 103163213Sru success = build(*argv, omode); 10440602Smsmith } else if (mkdir(*argv, omode) < 0) { 10571029Salfred if (errno == ENOTDIR || errno == ENOENT) 10671029Salfred warn("%s", dirname(*argv)); 10771029Salfred else 10871029Salfred warn("%s", *argv); 10940606Smsmith success = 0; 110163213Sru } else { 111163213Sru success = 1; 112163213Sru if (vflag) 113163213Sru (void)printf("%s\n", *argv); 114163213Sru } 11540606Smsmith if (!success) 11624524Smpp exitval = 1; 11740535Smsmith /* 11840535Smsmith * The mkdir() and umask() calls both honor only the low 11940535Smsmith * nine bits, so if you try to set a mode including the 12040602Smsmith * sticky, setuid, setgid bits you lose them. Don't do 12140602Smsmith * this unless the user has specifically requested a mode, 122163213Sru * as chmod will (obviously) ignore the umask. Do this 123163213Sru * on newly created directories only. 12440535Smsmith */ 125163213Sru if (success == 1 && mode != NULL && chmod(*argv, omode) == -1) { 12640606Smsmith warn("%s", *argv); 12740535Smsmith exitval = 1; 12840535Smsmith } 1291556Srgrimes } 1301556Srgrimes exit(exitval); 1311556Srgrimes} 1321556Srgrimes 133163213Sru 134163213Sru/* 135163213Sru * Returns 1 if a directory has been created, 136163213Sru * 2 if it already existed, and 0 on failure. 137163213Sru */ 138250084Seadlerstatic int 13990110Simpbuild(char *path, mode_t omode) 1401556Srgrimes{ 1411556Srgrimes struct stat sb; 1421556Srgrimes mode_t numask, oumask; 14324524Smpp int first, last, retval; 1441556Srgrimes char *p; 1451556Srgrimes 1461556Srgrimes p = path; 1472959Sbde oumask = 0; 148163213Sru retval = 1; 1491556Srgrimes if (p[0] == '/') /* Skip leading '/'. */ 1501556Srgrimes ++p; 15124524Smpp for (first = 1, last = 0; !last ; ++p) { 15224524Smpp if (p[0] == '\0') 15324524Smpp last = 1; 15424524Smpp else if (p[0] != '/') 1551556Srgrimes continue; 1561556Srgrimes *p = '\0'; 157135718Sdas if (!last && p[1] == '\0') 15824524Smpp last = 1; 1591556Srgrimes if (first) { 1601556Srgrimes /* 1611556Srgrimes * POSIX 1003.2: 1621556Srgrimes * For each dir operand that does not name an existing 163163213Sru * directory, effects equivalent to those caused by the 1641556Srgrimes * following command shall occcur: 1651556Srgrimes * 1661556Srgrimes * mkdir -p -m $(umask -S),u+wx $(dirname dir) && 1671556Srgrimes * mkdir [-m mode] dir 1681556Srgrimes * 1691556Srgrimes * We change the user's umask and then restore it, 1701556Srgrimes * instead of doing chmod's. 1711556Srgrimes */ 1721556Srgrimes oumask = umask(0); 1731556Srgrimes numask = oumask & ~(S_IWUSR | S_IXUSR); 1741556Srgrimes (void)umask(numask); 1751556Srgrimes first = 0; 1761556Srgrimes } 17724524Smpp if (last) 17824524Smpp (void)umask(oumask); 17990266Szarzycki if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 18091235Ssobomax if (errno == EEXIST || errno == EISDIR) { 18190266Szarzycki if (stat(path, &sb) < 0) { 18290266Szarzycki warn("%s", path); 183163213Sru retval = 0; 18490266Szarzycki break; 18590266Szarzycki } else if (!S_ISDIR(sb.st_mode)) { 18690266Szarzycki if (last) 18790266Szarzycki errno = EEXIST; 18890266Szarzycki else 18990266Szarzycki errno = ENOTDIR; 19090266Szarzycki warn("%s", path); 191163213Sru retval = 0; 19290266Szarzycki break; 19390266Szarzycki } 194163213Sru if (last) 195163213Sru retval = 2; 19690266Szarzycki } else { 1971556Srgrimes warn("%s", path); 198163213Sru retval = 0; 19924524Smpp break; 20090266Szarzycki } 20190266Szarzycki } else if (vflag) 20290266Szarzycki printf("%s\n", path); 20340602Smsmith if (!last) 20440602Smsmith *p = '/'; 2051556Srgrimes } 20624524Smpp if (!first && !last) 2071556Srgrimes (void)umask(oumask); 20824524Smpp return (retval); 2091556Srgrimes} 2101556Srgrimes 211250084Seadlerstatic void 21290110Simpusage(void) 2131556Srgrimes{ 21450870Smharo 215141578Sru (void)fprintf(stderr, 216141578Sru "usage: mkdir [-pv] [-m mode] directory_name ...\n"); 21750528Smharo exit (EX_USAGE); 2181556Srgrimes} 219