/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1983, 1992, 19934* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#include <sys/types.h>32#include <sys/stat.h>3334#include <err.h>35#include <errno.h>36#include <libgen.h>37#include <stdio.h>38#include <stdlib.h>39#include <string.h>40#include <sysexits.h>41#include <unistd.h>4243static int build(char *, mode_t);44static void usage(void);4546static int vflag;4748int49main(int argc, char *argv[])50{51int ch, exitval, success, pflag;52mode_t omode;53void *set = NULL;54char *mode;5556omode = pflag = 0;57mode = NULL;58while ((ch = getopt(argc, argv, "m:pv")) != -1)59switch(ch) {60case 'm':61mode = optarg;62break;63case 'p':64pflag = 1;65break;66case 'v':67vflag = 1;68break;69case '?':70default:71usage();72}7374argc -= optind;75argv += optind;76if (argv[0] == NULL)77usage();7879if (mode == NULL) {80omode = S_IRWXU | S_IRWXG | S_IRWXO;81} else {82if ((set = setmode(mode)) == NULL)83errx(1, "invalid file mode: %s", mode);84omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);85free(set);86}8788for (exitval = 0; *argv != NULL; ++argv) {89if (pflag) {90success = build(*argv, omode);91} else if (mkdir(*argv, omode) < 0) {92if (errno == ENOTDIR || errno == ENOENT)93warn("%s", dirname(*argv));94else95warn("%s", *argv);96success = 0;97} else {98success = 1;99if (vflag)100(void)printf("%s\n", *argv);101}102if (!success)103exitval = 1;104/*105* The mkdir() and umask() calls both honor only the low106* nine bits, so if you try to set a mode including the107* sticky, setuid, setgid bits you lose them. Don't do108* this unless the user has specifically requested a mode,109* as chmod will (obviously) ignore the umask. Do this110* on newly created directories only.111*/112if (success == 1 && mode != NULL && chmod(*argv, omode) == -1) {113warn("%s", *argv);114exitval = 1;115}116}117exit(exitval);118}119120121/*122* Returns 1 if a directory has been created,123* 2 if it already existed, and 0 on failure.124*/125static int126build(char *path, mode_t omode)127{128struct stat sb;129mode_t numask, oumask;130int first, last, retval;131char *p;132133p = path;134oumask = 0;135retval = 1;136if (p[0] == '/') /* Skip leading '/'. */137++p;138for (first = 1, last = 0; !last ; ++p) {139if (p[0] == '\0')140last = 1;141else if (p[0] != '/')142continue;143*p = '\0';144if (!last && p[1] == '\0')145last = 1;146if (first) {147/*148* POSIX 1003.2:149* For each dir operand that does not name an existing150* directory, effects equivalent to those caused by the151* following command shall occur:152*153* mkdir -p -m $(umask -S),u+wx $(dirname dir) &&154* mkdir [-m mode] dir155*156* We change the user's umask and then restore it,157* instead of doing chmod's.158*/159oumask = umask(0);160numask = oumask & ~(S_IWUSR | S_IXUSR);161(void)umask(numask);162first = 0;163}164if (last)165(void)umask(oumask);166if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) {167if (errno == EEXIST || errno == EISDIR) {168if (stat(path, &sb) < 0) {169warn("%s", path);170retval = 0;171break;172} else if (!S_ISDIR(sb.st_mode)) {173if (last)174errno = EEXIST;175else176errno = ENOTDIR;177warn("%s", path);178retval = 0;179break;180}181if (last)182retval = 2;183} else {184warn("%s", path);185retval = 0;186break;187}188} else if (vflag)189printf("%s\n", path);190if (!last)191*p = '/';192}193if (!first && !last)194(void)umask(oumask);195return (retval);196}197198static void199usage(void)200{201202(void)fprintf(stderr,203"usage: mkdir [-pv] [-m mode] directory_name ...\n");204exit (EX_USAGE);205}206207208