mkdir.c revision 3e14f97f673e8a630f076077de35afdd43dc1587
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/***********************************************************************
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* This software is part of the ast package *
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner* Copyright (c) 1992-2010 AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* and is licensed under the *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Common Public License, Version 1.0 *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* by AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* A copy of the License is available at *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Information and Software Systems Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* AT&T Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Florham Park NJ *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Glenn Fowler <gsf@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* David Korn <dgk@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin***********************************************************************/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * David Korn
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * AT&T Bell Laboratories
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic const char usage[] =
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner"[-?\n@(#)$Id: mkdir (AT&T Research) 2009-12-03 $\n]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+NAME?mkdir - make directories]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+DESCRIPTION?\bmkdir\b creates one or more directories. By "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "default, the mode of created directories is \ba=rwx\b minus the "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "bits set in the \bumask\b(1).]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[m:mode]:[mode?Set the mode of created directories to \amode\a. "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "\amode\a is symbolic or octal mode as in \bchmod\b(1). Relative "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "modes assume an initial mode of \ba=rwx\b.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[p:parents?Create any missing intermediate pathname components. For "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "each dir operand that does not name an existing directory, effects "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "equivalent to those caused by the following command shall occur: "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "\vmkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode]] "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "dir\v where the \b-m\b mode option represents that option supplied to "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "the original invocation of \bmkdir\b, if any. Each dir operand that "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "names an existing directory shall be ignored without error.]"
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner"[v:verbose?Print a message on the standard error for each created "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"\ndirectory ...\n"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+EXIT STATUS?]{"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "[+0?All directories created successfully, or the \b-p\b option "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "was specified and all the specified directories now exist.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "[+>0?An error occurred.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+SEE ALSO?\bchmod\b(1), \brmdir\b(1), \bumask\b(1)]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register char* arg;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register int n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register int mflag = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register int pflag = 0;
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner register int vflag = 0;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz mode = strperm(arg = opt_info.arg, &opt_info.arg, mode);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (!pflag || !(errno == ENOENT || errno == EEXIST || errno == ENOTDIR))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * -p option, preserve intermediates
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * first eliminate trailing /'s
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* skip over slashes */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* skip to next component */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (mkdir(name, n ? dmode : mode) < 0 && errno != EEXIST && access(name, F_OK) < 0)
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin if (!(*arg = n) && (mode & (S_ISVTX|S_ISUID|S_ISGID)))
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin if ((st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)) != (mode & (S_ISVTX|S_ISUID|S_ISGID)) && chmod(name, mode))
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin error(ERROR_system(0), "%s: cannot change mode from %s to %s", name, fmtperm(st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)), fmtperm(mode));