1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1992-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * David Korn
1N/A * AT&T Bell Laboratories
1N/A *
1N/A * mkdir
1N/A */
1N/A
1N/Astatic const char usage[] =
1N/A"[-?\n@(#)$Id: mkdir (AT&T Research) 2010-04-08 $\n]"
1N/AUSAGE_LICENSE
1N/A"[+NAME?mkdir - make directories]"
1N/A"[+DESCRIPTION?\bmkdir\b creates one or more directories. By "
1N/A "default, the mode of created directories is \ba=rwx\b minus the "
1N/A "bits set in the \bumask\b(1).]"
1N/A"[m:mode]:[mode?Set the mode of created directories to \amode\a. "
1N/A "\amode\a is symbolic or octal mode as in \bchmod\b(1). Relative "
1N/A "modes assume an initial mode of \ba=rwx\b.]"
1N/A"[p:parents?Create any missing intermediate pathname components. For "
1N/A "each dir operand that does not name an existing directory, effects "
1N/A "equivalent to those caused by the following command shall occur: "
1N/A "\vmkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode]] "
1N/A "dir\v where the \b-m\b mode option represents that option supplied to "
1N/A "the original invocation of \bmkdir\b, if any. Each dir operand that "
1N/A "names an existing directory shall be ignored without error.]"
1N/A"[v:verbose?Print a message on the standard error for each created "
1N/A "directory.]"
1N/A"\n"
1N/A"\ndirectory ...\n"
1N/A"\n"
1N/A"[+EXIT STATUS?]{"
1N/A "[+0?All directories created successfully, or the \b-p\b option "
1N/A "was specified and all the specified directories now exist.]"
1N/A "[+>0?An error occurred.]"
1N/A"}"
1N/A"[+SEE ALSO?\bchmod\b(1), \brmdir\b(1), \bumask\b(1)]"
1N/A;
1N/A
1N/A#include <cmd.h>
1N/A#include <ls.h>
1N/A
1N/A#define DIRMODE (S_IRWXU|S_IRWXG|S_IRWXO)
1N/A
1N/Aint
1N/Ab_mkdir(int argc, char** argv, void* context)
1N/A{
1N/A register char* path;
1N/A register int n;
1N/A register mode_t mode = DIRMODE;
1N/A register mode_t mask = 0;
1N/A register int mflag = 0;
1N/A register int pflag = 0;
1N/A register int vflag = 0;
1N/A int made;
1N/A char* part;
1N/A mode_t dmode;
1N/A struct stat st;
1N/A
1N/A cmdinit(argc, argv, context, ERROR_CATALOG, 0);
1N/A for (;;)
1N/A {
1N/A switch (optget(argv, usage))
1N/A {
1N/A case 'm':
1N/A mflag = 1;
1N/A mode = strperm(opt_info.arg, &part, mode);
1N/A if (*part)
1N/A error(ERROR_exit(0), "%s: invalid mode", opt_info.arg);
1N/A continue;
1N/A case 'p':
1N/A pflag = 1;
1N/A continue;
1N/A case 'v':
1N/A vflag = 1;
1N/A continue;
1N/A case ':':
1N/A error(2, "%s", opt_info.arg);
1N/A break;
1N/A case '?':
1N/A error(ERROR_usage(2), "%s", opt_info.arg);
1N/A break;
1N/A }
1N/A break;
1N/A }
1N/A argv += opt_info.index;
1N/A if (error_info.errors || !*argv)
1N/A error(ERROR_usage(2), "%s", optusage(NiL));
1N/A mask = umask(0);
1N/A if (mflag || pflag)
1N/A {
1N/A dmode = DIRMODE & ~mask;
1N/A if (!mflag)
1N/A mode = dmode;
1N/A dmode |= S_IWUSR | S_IXUSR;
1N/A }
1N/A else
1N/A {
1N/A mode &= ~mask;
1N/A umask(mask);
1N/A mask = 0;
1N/A }
1N/A while (path = *argv++)
1N/A {
1N/A if (!mkdir(path, mode))
1N/A {
1N/A if (vflag)
1N/A error(0, "%s: directory created", path);
1N/A made = 1;
1N/A }
1N/A else if (!pflag || !(errno == ENOENT || errno == EEXIST || errno == ENOTDIR))
1N/A {
1N/A error(ERROR_system(0), "%s:", path);
1N/A continue;
1N/A }
1N/A else if (errno == EEXIST)
1N/A continue;
1N/A else
1N/A {
1N/A /*
1N/A * -p option, preserve intermediates
1N/A * first eliminate trailing /'s
1N/A */
1N/A
1N/A made = 0;
1N/A n = strlen(path);
1N/A while (n > 0 && path[--n] == '/');
1N/A path[n + 1] = 0;
1N/A for (part = path, n = *part; n;)
1N/A {
1N/A /* skip over slashes */
1N/A while (*part == '/')
1N/A part++;
1N/A /* skip to next component */
1N/A while ((n = *part) && n != '/')
1N/A part++;
1N/A *part = 0;
1N/A if (mkdir(path, n ? dmode : mode) < 0 && errno != EEXIST && access(path, F_OK) < 0)
1N/A {
1N/A error(ERROR_system(0), "%s: cannot create intermediate directory", path);
1N/A *part = n;
1N/A break;
1N/A }
1N/A if (vflag)
1N/A error(0, "%s: directory created", path);
1N/A if (!(*part = n))
1N/A {
1N/A made = 1;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A if (made && (mode & (S_ISVTX|S_ISUID|S_ISGID)))
1N/A {
1N/A if (stat(path, &st))
1N/A {
1N/A error(ERROR_system(0), "%s: cannot stat", path);
1N/A break;
1N/A }
1N/A if ((st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)) != (mode & (S_ISVTX|S_ISUID|S_ISGID)) && chmod(path, mode))
1N/A {
1N/A error(ERROR_system(0), "%s: cannot change mode from %s to %s", path, fmtperm(st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)), fmtperm(mode));
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A if (mask)
1N/A umask(mask);
1N/A return error_info.errors != 0;
1N/A}