mkdir.c revision 114433
11556Srgrimes/* 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 * 3. All advertising materials mentioning features or use of this software 141556Srgrimes * must display the following acknowledgement: 151556Srgrimes * This product includes software developed by the University of 161556Srgrimes * California, Berkeley and its contributors. 171556Srgrimes * 4. Neither the name of the University nor the names of its contributors 181556Srgrimes * may be used to endorse or promote products derived from this software 191556Srgrimes * without specific prior written permission. 201556Srgrimes * 211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311556Srgrimes * SUCH DAMAGE. 321556Srgrimes */ 331556Srgrimes 34114433Sobrien#if 0 351556Srgrimes#ifndef lint 3620418Sstevestatic char const copyright[] = 371556Srgrimes"@(#) Copyright (c) 1983, 1992, 1993\n\ 381556Srgrimes The Regents of the University of California. All rights reserved.\n"; 391556Srgrimes#endif /* not lint */ 401556Srgrimes 411556Srgrimes#ifndef lint 4236048Scharnierstatic char sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 1/25/94"; 43114433Sobrien#endif /* not lint */ 4436048Scharnier#endif 4599109Sobrien#include <sys/cdefs.h> 4699109Sobrien__FBSDID("$FreeBSD: head/bin/mkdir/mkdir.c 114433 2003-05-01 16:58:57Z obrien $"); 471556Srgrimes 481556Srgrimes#include <sys/types.h> 491556Srgrimes#include <sys/stat.h> 501556Srgrimes 511556Srgrimes#include <err.h> 521556Srgrimes#include <errno.h> 5371029Salfred#include <libgen.h> 541556Srgrimes#include <stdio.h> 551556Srgrimes#include <stdlib.h> 561556Srgrimes#include <string.h> 5750528Smharo#include <sysexits.h> 581556Srgrimes#include <unistd.h> 591556Srgrimes 6091084Smarkmstatic int build(char *, mode_t); 6191084Smarkmstatic void usage(void); 621556Srgrimes 6350528Smharoint vflag; 6450528Smharo 651556Srgrimesint 6690110Simpmain(int argc, char *argv[]) 671556Srgrimes{ 6891084Smarkm int ch, exitval, success, pflag; 6991084Smarkm mode_t omode, *set = (mode_t *)NULL; 7024524Smpp char *mode; 711556Srgrimes 722923Sphk omode = pflag = 0; 731556Srgrimes mode = NULL; 7450528Smharo while ((ch = getopt(argc, argv, "m:pv")) != -1) 751556Srgrimes switch(ch) { 7650870Smharo case 'm': 7750870Smharo mode = optarg; 7850870Smharo break; 791556Srgrimes case 'p': 801556Srgrimes pflag = 1; 811556Srgrimes break; 8250528Smharo case 'v': 8350528Smharo vflag = 1; 8450528Smharo break; 851556Srgrimes case '?': 861556Srgrimes default: 871556Srgrimes usage(); 881556Srgrimes } 891556Srgrimes 901556Srgrimes argc -= optind; 911556Srgrimes argv += optind; 921556Srgrimes if (argv[0] == NULL) 931556Srgrimes usage(); 941556Srgrimes 951556Srgrimes if (mode == NULL) { 961556Srgrimes omode = S_IRWXU | S_IRWXG | S_IRWXO; 971556Srgrimes } else { 981556Srgrimes if ((set = setmode(mode)) == NULL) 991556Srgrimes errx(1, "invalid file mode: %s", mode); 10024524Smpp omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO); 10141842Simp free(set); 1021556Srgrimes } 1031556Srgrimes 1041556Srgrimes for (exitval = 0; *argv != NULL; ++argv) { 10540606Smsmith success = 1; 10624524Smpp if (pflag) { 10724524Smpp if (build(*argv, omode)) 10840606Smsmith success = 0; 10940602Smsmith } else if (mkdir(*argv, omode) < 0) { 11071029Salfred if (errno == ENOTDIR || errno == ENOENT) 11171029Salfred warn("%s", dirname(*argv)); 11271029Salfred else 11371029Salfred warn("%s", *argv); 11440606Smsmith success = 0; 11550528Smharo } else if (vflag) 11650528Smharo (void)printf("%s\n", *argv); 11750528Smharo 11840606Smsmith if (!success) 11924524Smpp exitval = 1; 12040535Smsmith /* 12140535Smsmith * The mkdir() and umask() calls both honor only the low 12240535Smsmith * nine bits, so if you try to set a mode including the 12340602Smsmith * sticky, setuid, setgid bits you lose them. Don't do 12440602Smsmith * this unless the user has specifically requested a mode, 12540602Smsmith * as chmod will (obviously) ignore the umask. 12640535Smsmith */ 12740606Smsmith if (success && mode != NULL && chmod(*argv, omode) == -1) { 12840606Smsmith warn("%s", *argv); 12940535Smsmith exitval = 1; 13040535Smsmith } 1311556Srgrimes } 1321556Srgrimes exit(exitval); 1331556Srgrimes} 1341556Srgrimes 1351556Srgrimesint 13690110Simpbuild(char *path, mode_t omode) 1371556Srgrimes{ 1381556Srgrimes struct stat sb; 1391556Srgrimes mode_t numask, oumask; 14024524Smpp int first, last, retval; 1411556Srgrimes char *p; 1421556Srgrimes 1431556Srgrimes p = path; 1442959Sbde oumask = 0; 14524524Smpp retval = 0; 1461556Srgrimes if (p[0] == '/') /* Skip leading '/'. */ 1471556Srgrimes ++p; 14824524Smpp for (first = 1, last = 0; !last ; ++p) { 14924524Smpp if (p[0] == '\0') 15024524Smpp last = 1; 15124524Smpp else if (p[0] != '/') 1521556Srgrimes continue; 1531556Srgrimes *p = '\0'; 15424524Smpp if (p[1] == '\0') 15524524Smpp last = 1; 1561556Srgrimes if (first) { 1571556Srgrimes /* 1581556Srgrimes * POSIX 1003.2: 1591556Srgrimes * For each dir operand that does not name an existing 1601556Srgrimes * directory, effects equivalent to those cased by the 1611556Srgrimes * following command shall occcur: 1621556Srgrimes * 1631556Srgrimes * mkdir -p -m $(umask -S),u+wx $(dirname dir) && 1641556Srgrimes * mkdir [-m mode] dir 1651556Srgrimes * 1661556Srgrimes * We change the user's umask and then restore it, 1671556Srgrimes * instead of doing chmod's. 1681556Srgrimes */ 1691556Srgrimes oumask = umask(0); 1701556Srgrimes numask = oumask & ~(S_IWUSR | S_IXUSR); 1711556Srgrimes (void)umask(numask); 1721556Srgrimes first = 0; 1731556Srgrimes } 17424524Smpp if (last) 17524524Smpp (void)umask(oumask); 17690266Szarzycki if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 17791235Ssobomax if (errno == EEXIST || errno == EISDIR) { 17890266Szarzycki if (stat(path, &sb) < 0) { 17990266Szarzycki warn("%s", path); 18090266Szarzycki retval = 1; 18190266Szarzycki break; 18290266Szarzycki } else if (!S_ISDIR(sb.st_mode)) { 18390266Szarzycki if (last) 18490266Szarzycki errno = EEXIST; 18590266Szarzycki else 18690266Szarzycki errno = ENOTDIR; 18790266Szarzycki warn("%s", path); 18890266Szarzycki retval = 1; 18990266Szarzycki break; 19090266Szarzycki } 19190266Szarzycki } else { 1921556Srgrimes warn("%s", path); 19324524Smpp retval = 1; 19424524Smpp break; 19590266Szarzycki } 19690266Szarzycki } else if (vflag) 19790266Szarzycki printf("%s\n", path); 19840602Smsmith if (!last) 19940602Smsmith *p = '/'; 2001556Srgrimes } 20124524Smpp if (!first && !last) 2021556Srgrimes (void)umask(oumask); 20324524Smpp return (retval); 2041556Srgrimes} 2051556Srgrimes 2061556Srgrimesvoid 20790110Simpusage(void) 2081556Srgrimes{ 20950870Smharo 21050870Smharo (void)fprintf(stderr, "usage: mkdir [-pv] [-m mode] directory ...\n"); 21150528Smharo exit (EX_USAGE); 2121556Srgrimes} 213