10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22*212Scf46844
23*212Scf46844 /*
24*212Scf46844 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25*212Scf46844 * Use is subject to license terms.
26*212Scf46844 */
27*212Scf46844
280Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
290Sstevel@tonic-gate /* All Rights Reserved */
300Sstevel@tonic-gate /* */
310Sstevel@tonic-gate
320Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
330Sstevel@tonic-gate
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate * make directory.
360Sstevel@tonic-gate * If -m is used with a valid mode, directories will be
370Sstevel@tonic-gate * created in that mode. Otherwise, the default mode will
380Sstevel@tonic-gate * be 777 possibly altered by the process's file mode creation
390Sstevel@tonic-gate * mask.
400Sstevel@tonic-gate * If -p is used, make the directory as well as
410Sstevel@tonic-gate * its non-existing parent directories.
420Sstevel@tonic-gate */
430Sstevel@tonic-gate
440Sstevel@tonic-gate #include <signal.h>
450Sstevel@tonic-gate #include <stdio.h>
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate #include <sys/stat.h>
480Sstevel@tonic-gate #include <errno.h>
490Sstevel@tonic-gate #include <string.h>
500Sstevel@tonic-gate #include <locale.h>
510Sstevel@tonic-gate #include <stdlib.h>
520Sstevel@tonic-gate #include <unistd.h>
530Sstevel@tonic-gate #include <libgen.h>
540Sstevel@tonic-gate #include <stdarg.h>
550Sstevel@tonic-gate #include <wchar.h>
560Sstevel@tonic-gate
570Sstevel@tonic-gate #define MSGEXISTS "\"%s\": Exists but is not a directory\n"
580Sstevel@tonic-gate #define MSGUSAGE "usage: mkdir [-m mode] [-p] dirname ...\n"
590Sstevel@tonic-gate #define MSGFMT1 "\"%s\": %s\n"
600Sstevel@tonic-gate #define MSGFAILED "Failed to make directory \"%s\"; %s\n"
610Sstevel@tonic-gate
620Sstevel@tonic-gate extern int optind, errno;
630Sstevel@tonic-gate extern char *optarg;
640Sstevel@tonic-gate
650Sstevel@tonic-gate static char
660Sstevel@tonic-gate *simplify(char *path);
670Sstevel@tonic-gate
680Sstevel@tonic-gate void
690Sstevel@tonic-gate errmsg(int severity, int code, char *format, ...);
700Sstevel@tonic-gate
710Sstevel@tonic-gate extern mode_t
720Sstevel@tonic-gate newmode(char *ms, mode_t new_mode, mode_t umsk, char *file, char *path);
730Sstevel@tonic-gate
740Sstevel@tonic-gate #define ALLRWX (S_IRWXU | S_IRWXG | S_IRWXO)
750Sstevel@tonic-gate
760Sstevel@tonic-gate
77*212Scf46844 int
main(int argc,char * argv[])780Sstevel@tonic-gate main(int argc, char *argv[])
790Sstevel@tonic-gate {
800Sstevel@tonic-gate int pflag, errflg, mflag;
810Sstevel@tonic-gate int c, local_errno, tmp_errno;
820Sstevel@tonic-gate mode_t cur_umask;
830Sstevel@tonic-gate mode_t mode;
840Sstevel@tonic-gate mode_t modediff;
850Sstevel@tonic-gate char *d;
860Sstevel@tonic-gate struct stat buf;
870Sstevel@tonic-gate
880Sstevel@tonic-gate pflag = mflag = errflg = 0;
890Sstevel@tonic-gate local_errno = 0;
900Sstevel@tonic-gate
910Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
920Sstevel@tonic-gate
930Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
940Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
950Sstevel@tonic-gate #endif
960Sstevel@tonic-gate
970Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
980Sstevel@tonic-gate
990Sstevel@tonic-gate cur_umask = umask(0);
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate mode = ALLRWX;
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate while ((c = getopt(argc, argv, "m:p")) != EOF) {
1040Sstevel@tonic-gate switch (c) {
1050Sstevel@tonic-gate case 'm':
1060Sstevel@tonic-gate mflag++;
1070Sstevel@tonic-gate mode = newmode(optarg, ALLRWX, cur_umask, "", "");
1080Sstevel@tonic-gate break;
1090Sstevel@tonic-gate case 'p':
1100Sstevel@tonic-gate pflag++;
1110Sstevel@tonic-gate break;
1120Sstevel@tonic-gate case '?':
1130Sstevel@tonic-gate errflg++;
1140Sstevel@tonic-gate break;
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate * When using default ACLs, mkdir() should be called with
1210Sstevel@tonic-gate * 0777 always; and umask or default ACL should do the work.
1220Sstevel@tonic-gate * Because of the POSIX.2 requirement that the
1230Sstevel@tonic-gate * intermediate mode be at least -wx------,
1240Sstevel@tonic-gate * we do some trickery here.
1250Sstevel@tonic-gate *
1260Sstevel@tonic-gate * If pflag is not set, we can just leave the umask as
1270Sstevel@tonic-gate * it the user specified it, unless it masks any of bits 0300.
1280Sstevel@tonic-gate */
1290Sstevel@tonic-gate if (pflag) {
1300Sstevel@tonic-gate modediff = cur_umask & (S_IXUSR | S_IWUSR);
1310Sstevel@tonic-gate if (modediff)
1320Sstevel@tonic-gate cur_umask &= ~modediff;
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate (void) umask(cur_umask);
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate argc -= optind;
1370Sstevel@tonic-gate if (argc < 1 || errflg) {
1380Sstevel@tonic-gate errmsg(0, 2, gettext(MSGUSAGE));
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate argv = &argv[optind];
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate errno = 0;
1430Sstevel@tonic-gate while (argc--) {
1440Sstevel@tonic-gate if ((d = simplify(*argv++)) == NULL) {
1450Sstevel@tonic-gate exit(2);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate * When -p is set, invokes mkdirp library routine.
1500Sstevel@tonic-gate * Although successfully invoked, mkdirp sets errno to ENOENT
1510Sstevel@tonic-gate * if one of the directory in the pathname does not exist,
1520Sstevel@tonic-gate * thus creates a confusion on success/failure status
1530Sstevel@tonic-gate * possibly checked by the calling routine or shell.
1540Sstevel@tonic-gate * Therefore, errno is reset only when
1550Sstevel@tonic-gate * mkdirp has executed successfully, otherwise save
1560Sstevel@tonic-gate * in local_errno.
1570Sstevel@tonic-gate */
1580Sstevel@tonic-gate if (pflag) {
1590Sstevel@tonic-gate /*
1600Sstevel@tonic-gate * POSIX.2 says that it is not an error if
1610Sstevel@tonic-gate * the argument names an existing directory.
1620Sstevel@tonic-gate * We will, however, complain if the argument
1630Sstevel@tonic-gate * exists but is not a directory.
1640Sstevel@tonic-gate */
1650Sstevel@tonic-gate if (lstat(d, &buf) != -1) {
1660Sstevel@tonic-gate if (S_ISDIR(buf.st_mode)) {
1670Sstevel@tonic-gate continue;
1680Sstevel@tonic-gate } else {
1690Sstevel@tonic-gate local_errno = EEXIST;
1700Sstevel@tonic-gate errmsg(0, 0, gettext(MSGEXISTS), d);
1710Sstevel@tonic-gate continue;
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate errno = 0;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate if (mkdirp(d, ALLRWX) < 0) {
1770Sstevel@tonic-gate tmp_errno = errno;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate if (tmp_errno == EEXIST) {
1800Sstevel@tonic-gate if (lstat(d, &buf) != -1) {
1810Sstevel@tonic-gate if (! S_ISDIR(buf.st_mode)) {
1820Sstevel@tonic-gate local_errno =
1830Sstevel@tonic-gate tmp_errno;
1840Sstevel@tonic-gate errmsg(0, 0, gettext(
1850Sstevel@tonic-gate MSGEXISTS), d);
1860Sstevel@tonic-gate continue;
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate /* S_ISDIR: do nothing */
1890Sstevel@tonic-gate } else {
1900Sstevel@tonic-gate local_errno = tmp_errno;
1910Sstevel@tonic-gate perror("mkdir");
1920Sstevel@tonic-gate errmsg(0, 0,
1930Sstevel@tonic-gate gettext(MSGFAILED), d,
1940Sstevel@tonic-gate strerror(local_errno));
1950Sstevel@tonic-gate continue;
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate } else {
1980Sstevel@tonic-gate local_errno = tmp_errno;
1990Sstevel@tonic-gate errmsg(0, 0, gettext(MSGFMT1), d,
2000Sstevel@tonic-gate strerror(tmp_errno));
2010Sstevel@tonic-gate continue;
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate errno = 0;
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate * get the file mode for the newly
2090Sstevel@tonic-gate * created directory and test for
2100Sstevel@tonic-gate * set gid bit being inherited from the parent
2110Sstevel@tonic-gate * directory to include it with the file
2120Sstevel@tonic-gate * mode creation for the last directory
2130Sstevel@tonic-gate * on the dir path.
2140Sstevel@tonic-gate *
2150Sstevel@tonic-gate * This is only needed if mflag was specified
2160Sstevel@tonic-gate * or if the umask was adjusted with -wx-----
2170Sstevel@tonic-gate *
2180Sstevel@tonic-gate * If mflag is specified, we chmod to the specified
2190Sstevel@tonic-gate * mode, oring in the 02000 bit.
2200Sstevel@tonic-gate *
2210Sstevel@tonic-gate * If modediff is set, those bits need to be
2220Sstevel@tonic-gate * removed from the last directory component,
2230Sstevel@tonic-gate * all other bits are kept regardless of umask
2240Sstevel@tonic-gate * in case a default ACL is present.
2250Sstevel@tonic-gate */
2260Sstevel@tonic-gate if (mflag || modediff) {
2270Sstevel@tonic-gate mode_t tmpmode;
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate (void) lstat(d, &buf);
2300Sstevel@tonic-gate if (modediff && !mflag)
2310Sstevel@tonic-gate tmpmode = (buf.st_mode & 07777)
2320Sstevel@tonic-gate & ~modediff;
2330Sstevel@tonic-gate else
2340Sstevel@tonic-gate tmpmode = mode | (buf.st_mode & 02000);
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate if (chmod(d, tmpmode) < 0) {
2370Sstevel@tonic-gate tmp_errno = errno;
2380Sstevel@tonic-gate local_errno = errno;
2390Sstevel@tonic-gate errmsg(0, 0, gettext(MSGFMT1), d,
2400Sstevel@tonic-gate strerror(tmp_errno));
2410Sstevel@tonic-gate continue;
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate errno = 0;
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate continue;
2470Sstevel@tonic-gate } else {
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate * No -p. Make only one directory
2500Sstevel@tonic-gate */
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate errno = 0;
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate if (mkdir(d, mode) < 0) {
2550Sstevel@tonic-gate local_errno = tmp_errno = errno;
2560Sstevel@tonic-gate errmsg(0, 0, gettext(MSGFAILED), d,
2570Sstevel@tonic-gate strerror(tmp_errno));
2580Sstevel@tonic-gate continue;
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate if (mflag) {
2610Sstevel@tonic-gate mode_t tmpmode;
2620Sstevel@tonic-gate (void) lstat(d, &buf);
2630Sstevel@tonic-gate tmpmode = mode | (buf.st_mode & 02000);
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate if (chmod(d, tmpmode) < 0) {
2660Sstevel@tonic-gate tmp_errno = errno;
2670Sstevel@tonic-gate local_errno = errno;
2680Sstevel@tonic-gate errmsg(0, 0, gettext(MSGFMT1), d,
2690Sstevel@tonic-gate strerror(tmp_errno));
2700Sstevel@tonic-gate continue;
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate errno = 0;
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate } /* end while */
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate /* When pflag is set, the errno is saved in local_errno */
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate if (local_errno)
2800Sstevel@tonic-gate errno = local_errno;
281*212Scf46844 return (errno ? 2: 0);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate * errmsg - This is an interface required by the code common to mkdir and
2860Sstevel@tonic-gate * chmod. The severity parameter is ignored here, but is meaningful
2870Sstevel@tonic-gate * to chmod.
2880Sstevel@tonic-gate */
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate /* ARGSUSED */
2910Sstevel@tonic-gate /* PRINTFLIKE3 */
2920Sstevel@tonic-gate void
errmsg(int severity,int code,char * format,...)2930Sstevel@tonic-gate errmsg(int severity, int code, char *format, ...)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate va_list ap;
2960Sstevel@tonic-gate va_start(ap, format);
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate (void) fprintf(stderr, "mkdir: ");
2990Sstevel@tonic-gate (void) vfprintf(stderr, format, ap);
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate va_end(ap);
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate if (code > 0) {
3040Sstevel@tonic-gate exit(code);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate /*
3090Sstevel@tonic-gate * simplify - given a pathname in a writable buffer, simplify that
3100Sstevel@tonic-gate * path by removing meaningless occurances of path
3110Sstevel@tonic-gate * syntax.
3120Sstevel@tonic-gate *
3130Sstevel@tonic-gate * The change happens in place in the argument. The
3140Sstevel@tonic-gate * result is neceassarily no longer than the original.
3150Sstevel@tonic-gate *
3160Sstevel@tonic-gate * Return the pointer supplied by the caller on success, or
3170Sstevel@tonic-gate * NULL on error.
3180Sstevel@tonic-gate *
3190Sstevel@tonic-gate * The caller should handle error reporting based upon the
3200Sstevel@tonic-gate * returned vlaue.
3210Sstevel@tonic-gate */
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate static char *
simplify(char * mbPath)3240Sstevel@tonic-gate simplify(char *mbPath)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate int i;
3270Sstevel@tonic-gate size_t mbPathlen; /* length of multi-byte path */
3280Sstevel@tonic-gate size_t wcPathlen; /* length of wide-character path */
3290Sstevel@tonic-gate wchar_t *wptr; /* scratch pointer */
3300Sstevel@tonic-gate wchar_t *wcPath; /* wide-character version of the path */
3310Sstevel@tonic-gate
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate * bail out if there is nothing there.
3340Sstevel@tonic-gate */
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate if (!mbPath)
3370Sstevel@tonic-gate return (mbPath);
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate /*
3400Sstevel@tonic-gate * convert the multi-byte version of the path to a
3410Sstevel@tonic-gate * wide-character rendering, for doing our figuring.
3420Sstevel@tonic-gate */
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate mbPathlen = strlen(mbPath);
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate if ((wcPath = calloc(sizeof (wchar_t), mbPathlen+1)) == NULL) {
3470Sstevel@tonic-gate perror("mkdir");
3480Sstevel@tonic-gate exit(2);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate if ((wcPathlen = mbstowcs(wcPath, mbPath, mbPathlen)) == (size_t)-1) {
3520Sstevel@tonic-gate free(wcPath);
3530Sstevel@tonic-gate return (NULL);
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate /*
3570Sstevel@tonic-gate * remove duplicate slashes first ("//../" -> "/")
3580Sstevel@tonic-gate */
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate for (wptr = wcPath, i = 0; i < wcPathlen; i++) {
3610Sstevel@tonic-gate *wptr++ = wcPath[i];
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate if (wcPath[i] == '/') {
3640Sstevel@tonic-gate i++;
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate while (wcPath[i] == '/') {
3670Sstevel@tonic-gate i++;
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate i--;
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate *wptr = '\0';
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate /*
3770Sstevel@tonic-gate * next skip initial occurances of "./"
3780Sstevel@tonic-gate */
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate for (wcPathlen = wcslen(wcPath), wptr = wcPath, i = 0;
3810Sstevel@tonic-gate i < wcPathlen-2 && wcPath[i] == '.' && wcPath[i+1] == '/';
3820Sstevel@tonic-gate i += 2) {
3830Sstevel@tonic-gate /* empty body */
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate /*
3870Sstevel@tonic-gate * now make reductions of various forms.
3880Sstevel@tonic-gate */
3890Sstevel@tonic-gate
3900Sstevel@tonic-gate while (i < wcPathlen) {
3910Sstevel@tonic-gate if (i < wcPathlen-2 && wcPath[i] == '/' &&
3920Sstevel@tonic-gate wcPath[i+1] == '.' && wcPath[i+2] == '/') {
3930Sstevel@tonic-gate /* "/./" -> "/" */
3940Sstevel@tonic-gate i += 2;
3950Sstevel@tonic-gate } else {
3960Sstevel@tonic-gate /* Normal case: copy the character */
3970Sstevel@tonic-gate *wptr++ = wcPath[i++];
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate *wptr = '\0';
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate /*
4040Sstevel@tonic-gate * now convert back to the multi-byte format.
4050Sstevel@tonic-gate */
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate if (wcstombs(mbPath, wcPath, mbPathlen) == (size_t)-1) {
4080Sstevel@tonic-gate free(wcPath);
4090Sstevel@tonic-gate return (NULL);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate free(wcPath);
4130Sstevel@tonic-gate return (mbPath);
4140Sstevel@tonic-gate }
415