mkdir.c revision 141578
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: head/bin/mkdir/mkdir.c 141578 2005-02-09 17:37:39Z ru $"); 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) { 10240606Smsmith success = 1; 10324524Smpp if (pflag) { 10424524Smpp if (build(*argv, omode)) 10540606Smsmith success = 0; 10640602Smsmith } else if (mkdir(*argv, omode) < 0) { 10771029Salfred if (errno == ENOTDIR || errno == ENOENT) 10871029Salfred warn("%s", dirname(*argv)); 10971029Salfred else 11071029Salfred warn("%s", *argv); 11140606Smsmith success = 0; 11250528Smharo } else if (vflag) 11350528Smharo (void)printf("%s\n", *argv); 11450528Smharo 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, 12240602Smsmith * as chmod will (obviously) ignore the umask. 12340535Smsmith */ 12440606Smsmith if (success && mode != NULL && chmod(*argv, omode) == -1) { 12540606Smsmith warn("%s", *argv); 12640535Smsmith exitval = 1; 12740535Smsmith } 1281556Srgrimes } 1291556Srgrimes exit(exitval); 1301556Srgrimes} 1311556Srgrimes 1321556Srgrimesint 13390110Simpbuild(char *path, mode_t omode) 1341556Srgrimes{ 1351556Srgrimes struct stat sb; 1361556Srgrimes mode_t numask, oumask; 13724524Smpp int first, last, retval; 1381556Srgrimes char *p; 1391556Srgrimes 1401556Srgrimes p = path; 1412959Sbde oumask = 0; 14224524Smpp retval = 0; 1431556Srgrimes if (p[0] == '/') /* Skip leading '/'. */ 1441556Srgrimes ++p; 14524524Smpp for (first = 1, last = 0; !last ; ++p) { 14624524Smpp if (p[0] == '\0') 14724524Smpp last = 1; 14824524Smpp else if (p[0] != '/') 1491556Srgrimes continue; 1501556Srgrimes *p = '\0'; 151135718Sdas if (!last && p[1] == '\0') 15224524Smpp last = 1; 1531556Srgrimes if (first) { 1541556Srgrimes /* 1551556Srgrimes * POSIX 1003.2: 1561556Srgrimes * For each dir operand that does not name an existing 1571556Srgrimes * directory, effects equivalent to those cased by the 1581556Srgrimes * following command shall occcur: 1591556Srgrimes * 1601556Srgrimes * mkdir -p -m $(umask -S),u+wx $(dirname dir) && 1611556Srgrimes * mkdir [-m mode] dir 1621556Srgrimes * 1631556Srgrimes * We change the user's umask and then restore it, 1641556Srgrimes * instead of doing chmod's. 1651556Srgrimes */ 1661556Srgrimes oumask = umask(0); 1671556Srgrimes numask = oumask & ~(S_IWUSR | S_IXUSR); 1681556Srgrimes (void)umask(numask); 1691556Srgrimes first = 0; 1701556Srgrimes } 17124524Smpp if (last) 17224524Smpp (void)umask(oumask); 17390266Szarzycki if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 17491235Ssobomax if (errno == EEXIST || errno == EISDIR) { 17590266Szarzycki if (stat(path, &sb) < 0) { 17690266Szarzycki warn("%s", path); 17790266Szarzycki retval = 1; 17890266Szarzycki break; 17990266Szarzycki } else if (!S_ISDIR(sb.st_mode)) { 18090266Szarzycki if (last) 18190266Szarzycki errno = EEXIST; 18290266Szarzycki else 18390266Szarzycki errno = ENOTDIR; 18490266Szarzycki warn("%s", path); 18590266Szarzycki retval = 1; 18690266Szarzycki break; 18790266Szarzycki } 18890266Szarzycki } else { 1891556Srgrimes warn("%s", path); 19024524Smpp retval = 1; 19124524Smpp break; 19290266Szarzycki } 19390266Szarzycki } else if (vflag) 19490266Szarzycki printf("%s\n", path); 19540602Smsmith if (!last) 19640602Smsmith *p = '/'; 1971556Srgrimes } 19824524Smpp if (!first && !last) 1991556Srgrimes (void)umask(oumask); 20024524Smpp return (retval); 2011556Srgrimes} 2021556Srgrimes 2031556Srgrimesvoid 20490110Simpusage(void) 2051556Srgrimes{ 20650870Smharo 207141578Sru (void)fprintf(stderr, 208141578Sru "usage: mkdir [-pv] [-m mode] directory_name ...\n"); 20950528Smharo exit (EX_USAGE); 2101556Srgrimes} 211