xref: /titanic_44/usr/src/cmd/mkdir/mkdir.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T		*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved					*/
24*7c478bd9Sstevel@tonic-gate /*								*/
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  * make directory.
35*7c478bd9Sstevel@tonic-gate  * If -m is used with a valid mode, directories will be
36*7c478bd9Sstevel@tonic-gate  * created in that mode.  Otherwise, the default mode will
37*7c478bd9Sstevel@tonic-gate  * be 777 possibly altered by the process's file mode creation
38*7c478bd9Sstevel@tonic-gate  * mask.
39*7c478bd9Sstevel@tonic-gate  * If -p is used, make the directory as well as
40*7c478bd9Sstevel@tonic-gate  * its non-existing parent directories.
41*7c478bd9Sstevel@tonic-gate  */
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #include	<signal.h>
44*7c478bd9Sstevel@tonic-gate #include	<stdio.h>
45*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
46*7c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
47*7c478bd9Sstevel@tonic-gate #include	<errno.h>
48*7c478bd9Sstevel@tonic-gate #include	<string.h>
49*7c478bd9Sstevel@tonic-gate #include	<locale.h>
50*7c478bd9Sstevel@tonic-gate #include	<stdlib.h>
51*7c478bd9Sstevel@tonic-gate #include	<unistd.h>
52*7c478bd9Sstevel@tonic-gate #include	<libgen.h>
53*7c478bd9Sstevel@tonic-gate #include	<stdarg.h>
54*7c478bd9Sstevel@tonic-gate #include	<wchar.h>
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #define	MSGEXISTS	"\"%s\": Exists but is not a directory\n"
57*7c478bd9Sstevel@tonic-gate #define	MSGUSAGE 	"usage: mkdir [-m mode] [-p] dirname ...\n"
58*7c478bd9Sstevel@tonic-gate #define	MSGFMT1  	"\"%s\": %s\n"
59*7c478bd9Sstevel@tonic-gate #define	MSGFAILED	"Failed to make directory \"%s\"; %s\n"
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate extern int optind,  errno;
62*7c478bd9Sstevel@tonic-gate extern char *optarg;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static char
65*7c478bd9Sstevel@tonic-gate *simplify(char *path);
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate void
68*7c478bd9Sstevel@tonic-gate errmsg(int severity, int code, char *format, ...);
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate extern mode_t
71*7c478bd9Sstevel@tonic-gate newmode(char *ms, mode_t new_mode, mode_t umsk, char *file, char *path);
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate #define	ALLRWX (S_IRWXU | S_IRWXG | S_IRWXO)
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate void
77*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate 	int 	pflag, errflg, mflag;
80*7c478bd9Sstevel@tonic-gate 	int 	c, local_errno, tmp_errno;
81*7c478bd9Sstevel@tonic-gate 	mode_t	cur_umask;
82*7c478bd9Sstevel@tonic-gate 	mode_t	mode;
83*7c478bd9Sstevel@tonic-gate 	mode_t	modediff;
84*7c478bd9Sstevel@tonic-gate 	char 	*d;
85*7c478bd9Sstevel@tonic-gate 	struct stat	buf;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	pflag = mflag = errflg = 0;
88*7c478bd9Sstevel@tonic-gate 	local_errno = 0;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
93*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
94*7c478bd9Sstevel@tonic-gate #endif
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	cur_umask = umask(0);
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	mode = ALLRWX;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "m:p")) != EOF) {
103*7c478bd9Sstevel@tonic-gate 		switch (c) {
104*7c478bd9Sstevel@tonic-gate 		case 'm':
105*7c478bd9Sstevel@tonic-gate 			mflag++;
106*7c478bd9Sstevel@tonic-gate 			mode = newmode(optarg, ALLRWX, cur_umask, "", "");
107*7c478bd9Sstevel@tonic-gate 			break;
108*7c478bd9Sstevel@tonic-gate 		case 'p':
109*7c478bd9Sstevel@tonic-gate 			pflag++;
110*7c478bd9Sstevel@tonic-gate 			break;
111*7c478bd9Sstevel@tonic-gate 		case '?':
112*7c478bd9Sstevel@tonic-gate 			errflg++;
113*7c478bd9Sstevel@tonic-gate 			break;
114*7c478bd9Sstevel@tonic-gate 		}
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	/*
119*7c478bd9Sstevel@tonic-gate 	 * When using default ACLs, mkdir() should be called with
120*7c478bd9Sstevel@tonic-gate 	 * 0777 always; and umask or default ACL should do the work.
121*7c478bd9Sstevel@tonic-gate 	 * Because of the POSIX.2 requirement that the
122*7c478bd9Sstevel@tonic-gate 	 * intermediate mode be at least -wx------,
123*7c478bd9Sstevel@tonic-gate 	 * we do some trickery here.
124*7c478bd9Sstevel@tonic-gate 	 *
125*7c478bd9Sstevel@tonic-gate 	 * If pflag is not set, we can just leave the umask as
126*7c478bd9Sstevel@tonic-gate 	 * it the user specified it, unless it masks any of bits 0300.
127*7c478bd9Sstevel@tonic-gate 	 */
128*7c478bd9Sstevel@tonic-gate 	if (pflag) {
129*7c478bd9Sstevel@tonic-gate 		modediff = cur_umask & (S_IXUSR | S_IWUSR);
130*7c478bd9Sstevel@tonic-gate 		if (modediff)
131*7c478bd9Sstevel@tonic-gate 			cur_umask &= ~modediff;
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 	(void) umask(cur_umask);
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	argc -= optind;
136*7c478bd9Sstevel@tonic-gate 	if (argc < 1 || errflg) {
137*7c478bd9Sstevel@tonic-gate 		errmsg(0, 2, gettext(MSGUSAGE));
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	errno = 0;
142*7c478bd9Sstevel@tonic-gate 	while (argc--) {
143*7c478bd9Sstevel@tonic-gate 		if ((d = simplify(*argv++)) == NULL) {
144*7c478bd9Sstevel@tonic-gate 			exit(2);
145*7c478bd9Sstevel@tonic-gate 		}
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 		/*
148*7c478bd9Sstevel@tonic-gate 		 * When -p is set, invokes mkdirp library routine.
149*7c478bd9Sstevel@tonic-gate 		 * Although successfully invoked, mkdirp sets errno to ENOENT
150*7c478bd9Sstevel@tonic-gate 		 * if one of the directory in the pathname does not exist,
151*7c478bd9Sstevel@tonic-gate 		 * thus creates a confusion on success/failure status
152*7c478bd9Sstevel@tonic-gate 		 * possibly checked by the calling routine or shell.
153*7c478bd9Sstevel@tonic-gate 		 * Therefore, errno is reset only when
154*7c478bd9Sstevel@tonic-gate 		 * mkdirp has executed successfully, otherwise save
155*7c478bd9Sstevel@tonic-gate 		 * in local_errno.
156*7c478bd9Sstevel@tonic-gate 		 */
157*7c478bd9Sstevel@tonic-gate 		if (pflag) {
158*7c478bd9Sstevel@tonic-gate 			/*
159*7c478bd9Sstevel@tonic-gate 			 * POSIX.2 says that it is not an error if
160*7c478bd9Sstevel@tonic-gate 			 * the argument names an existing directory.
161*7c478bd9Sstevel@tonic-gate 			 * We will, however, complain if the argument
162*7c478bd9Sstevel@tonic-gate 			 * exists but is not a directory.
163*7c478bd9Sstevel@tonic-gate 			 */
164*7c478bd9Sstevel@tonic-gate 			if (lstat(d, &buf) != -1) {
165*7c478bd9Sstevel@tonic-gate 				if (S_ISDIR(buf.st_mode)) {
166*7c478bd9Sstevel@tonic-gate 					continue;
167*7c478bd9Sstevel@tonic-gate 				} else {
168*7c478bd9Sstevel@tonic-gate 					local_errno = EEXIST;
169*7c478bd9Sstevel@tonic-gate 					errmsg(0, 0, gettext(MSGEXISTS), d);
170*7c478bd9Sstevel@tonic-gate 					continue;
171*7c478bd9Sstevel@tonic-gate 				}
172*7c478bd9Sstevel@tonic-gate 			}
173*7c478bd9Sstevel@tonic-gate 			errno = 0;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 			if (mkdirp(d, ALLRWX) < 0) {
176*7c478bd9Sstevel@tonic-gate 				tmp_errno = errno;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 				if (tmp_errno == EEXIST) {
179*7c478bd9Sstevel@tonic-gate 					if (lstat(d, &buf) != -1) {
180*7c478bd9Sstevel@tonic-gate 						if (! S_ISDIR(buf.st_mode)) {
181*7c478bd9Sstevel@tonic-gate 							local_errno =
182*7c478bd9Sstevel@tonic-gate 							    tmp_errno;
183*7c478bd9Sstevel@tonic-gate 							errmsg(0, 0, gettext(
184*7c478bd9Sstevel@tonic-gate 							    MSGEXISTS), d);
185*7c478bd9Sstevel@tonic-gate 							continue;
186*7c478bd9Sstevel@tonic-gate 						}
187*7c478bd9Sstevel@tonic-gate 						/* S_ISDIR: do nothing */
188*7c478bd9Sstevel@tonic-gate 					} else {
189*7c478bd9Sstevel@tonic-gate 						local_errno = tmp_errno;
190*7c478bd9Sstevel@tonic-gate 						perror("mkdir");
191*7c478bd9Sstevel@tonic-gate 						errmsg(0, 0,
192*7c478bd9Sstevel@tonic-gate 						    gettext(MSGFAILED), d,
193*7c478bd9Sstevel@tonic-gate 						    strerror(local_errno));
194*7c478bd9Sstevel@tonic-gate 						continue;
195*7c478bd9Sstevel@tonic-gate 					}
196*7c478bd9Sstevel@tonic-gate 				} else {
197*7c478bd9Sstevel@tonic-gate 					local_errno = tmp_errno;
198*7c478bd9Sstevel@tonic-gate 					errmsg(0, 0, gettext(MSGFMT1), d,
199*7c478bd9Sstevel@tonic-gate 					    strerror(tmp_errno));
200*7c478bd9Sstevel@tonic-gate 					continue;
201*7c478bd9Sstevel@tonic-gate 				}
202*7c478bd9Sstevel@tonic-gate 			}
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 			errno = 0;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 			/*
207*7c478bd9Sstevel@tonic-gate 			 * get the file mode for the newly
208*7c478bd9Sstevel@tonic-gate 			 * created directory and test for
209*7c478bd9Sstevel@tonic-gate 			 * set gid bit being inherited from the parent
210*7c478bd9Sstevel@tonic-gate 			 * directory to include it with the file
211*7c478bd9Sstevel@tonic-gate 			 * mode creation for the last directory
212*7c478bd9Sstevel@tonic-gate 			 * on the dir path.
213*7c478bd9Sstevel@tonic-gate 			 *
214*7c478bd9Sstevel@tonic-gate 			 * This is only needed if mflag was specified
215*7c478bd9Sstevel@tonic-gate 			 * or if the umask was adjusted with -wx-----
216*7c478bd9Sstevel@tonic-gate 			 *
217*7c478bd9Sstevel@tonic-gate 			 * If mflag is specified, we chmod to the specified
218*7c478bd9Sstevel@tonic-gate 			 * mode, oring in the 02000 bit.
219*7c478bd9Sstevel@tonic-gate 			 *
220*7c478bd9Sstevel@tonic-gate 			 * If modediff is set, those bits need to be
221*7c478bd9Sstevel@tonic-gate 			 * removed from the last directory component,
222*7c478bd9Sstevel@tonic-gate 			 * all other bits are kept regardless of umask
223*7c478bd9Sstevel@tonic-gate 			 * in case a default ACL is present.
224*7c478bd9Sstevel@tonic-gate 			 */
225*7c478bd9Sstevel@tonic-gate 			if (mflag || modediff) {
226*7c478bd9Sstevel@tonic-gate 				mode_t tmpmode;
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 				(void) lstat(d, &buf);
229*7c478bd9Sstevel@tonic-gate 				if (modediff && !mflag)
230*7c478bd9Sstevel@tonic-gate 					tmpmode = (buf.st_mode & 07777)
231*7c478bd9Sstevel@tonic-gate 								& ~modediff;
232*7c478bd9Sstevel@tonic-gate 				else
233*7c478bd9Sstevel@tonic-gate 					tmpmode = mode | (buf.st_mode & 02000);
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 				if (chmod(d, tmpmode) < 0) {
236*7c478bd9Sstevel@tonic-gate 					tmp_errno = errno;
237*7c478bd9Sstevel@tonic-gate 					local_errno = errno;
238*7c478bd9Sstevel@tonic-gate 					errmsg(0, 0, gettext(MSGFMT1), d,
239*7c478bd9Sstevel@tonic-gate 					    strerror(tmp_errno));
240*7c478bd9Sstevel@tonic-gate 					continue;
241*7c478bd9Sstevel@tonic-gate 				}
242*7c478bd9Sstevel@tonic-gate 				errno = 0;
243*7c478bd9Sstevel@tonic-gate 			}
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 			continue;
246*7c478bd9Sstevel@tonic-gate 		} else {
247*7c478bd9Sstevel@tonic-gate 			/*
248*7c478bd9Sstevel@tonic-gate 			 * No -p. Make only one directory
249*7c478bd9Sstevel@tonic-gate 			 */
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 			errno = 0;
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 			if (mkdir(d, mode) < 0) {
254*7c478bd9Sstevel@tonic-gate 				local_errno = tmp_errno = errno;
255*7c478bd9Sstevel@tonic-gate 				errmsg(0, 0, gettext(MSGFAILED), d,
256*7c478bd9Sstevel@tonic-gate 				    strerror(tmp_errno));
257*7c478bd9Sstevel@tonic-gate 				continue;
258*7c478bd9Sstevel@tonic-gate 			}
259*7c478bd9Sstevel@tonic-gate 			if (mflag) {
260*7c478bd9Sstevel@tonic-gate 				mode_t tmpmode;
261*7c478bd9Sstevel@tonic-gate 				(void) lstat(d, &buf);
262*7c478bd9Sstevel@tonic-gate 				tmpmode = mode | (buf.st_mode & 02000);
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 				if (chmod(d, tmpmode) < 0) {
265*7c478bd9Sstevel@tonic-gate 					tmp_errno = errno;
266*7c478bd9Sstevel@tonic-gate 					local_errno = errno;
267*7c478bd9Sstevel@tonic-gate 					errmsg(0, 0, gettext(MSGFMT1), d,
268*7c478bd9Sstevel@tonic-gate 					    strerror(tmp_errno));
269*7c478bd9Sstevel@tonic-gate 					continue;
270*7c478bd9Sstevel@tonic-gate 				}
271*7c478bd9Sstevel@tonic-gate 				errno = 0;
272*7c478bd9Sstevel@tonic-gate 			}
273*7c478bd9Sstevel@tonic-gate 		}
274*7c478bd9Sstevel@tonic-gate 	} /* end while */
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 	/* When pflag is set, the errno is saved in local_errno */
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 	if (local_errno)
279*7c478bd9Sstevel@tonic-gate 	    errno = local_errno;
280*7c478bd9Sstevel@tonic-gate 	exit(errno ? 2: 0);
281*7c478bd9Sstevel@tonic-gate }
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate /*
284*7c478bd9Sstevel@tonic-gate  *  errmsg - This is an interface required by the code common to mkdir and
285*7c478bd9Sstevel@tonic-gate  *		chmod. The severity parameter is ignored here, but is meaningful
286*7c478bd9Sstevel@tonic-gate  *		to chmod.
287*7c478bd9Sstevel@tonic-gate  */
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
290*7c478bd9Sstevel@tonic-gate /* PRINTFLIKE3 */
291*7c478bd9Sstevel@tonic-gate void
292*7c478bd9Sstevel@tonic-gate errmsg(int severity, int code, char *format, ...)
293*7c478bd9Sstevel@tonic-gate {
294*7c478bd9Sstevel@tonic-gate 	va_list ap;
295*7c478bd9Sstevel@tonic-gate 	va_start(ap, format);
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "mkdir: ");
298*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 	va_end(ap);
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 	if (code > 0) {
303*7c478bd9Sstevel@tonic-gate 		exit(code);
304*7c478bd9Sstevel@tonic-gate 	}
305*7c478bd9Sstevel@tonic-gate }
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate /*
308*7c478bd9Sstevel@tonic-gate  *	simplify - given a pathname in a writable buffer, simplify that
309*7c478bd9Sstevel@tonic-gate  *		   path by removing meaningless occurances of path
310*7c478bd9Sstevel@tonic-gate  *		   syntax.
311*7c478bd9Sstevel@tonic-gate  *
312*7c478bd9Sstevel@tonic-gate  *		   The change happens in place in the argument.  The
313*7c478bd9Sstevel@tonic-gate  *		   result is neceassarily no longer than the original.
314*7c478bd9Sstevel@tonic-gate  *
315*7c478bd9Sstevel@tonic-gate  *		   Return the pointer supplied by the caller on success, or
316*7c478bd9Sstevel@tonic-gate  *		   NULL on error.
317*7c478bd9Sstevel@tonic-gate  *
318*7c478bd9Sstevel@tonic-gate  *		   The caller should handle error reporting based upon the
319*7c478bd9Sstevel@tonic-gate  *		   returned vlaue.
320*7c478bd9Sstevel@tonic-gate  */
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate static char *
323*7c478bd9Sstevel@tonic-gate simplify(char *mbPath)
324*7c478bd9Sstevel@tonic-gate {
325*7c478bd9Sstevel@tonic-gate 	int i;
326*7c478bd9Sstevel@tonic-gate 	size_t mbPathlen;	/* length of multi-byte path */
327*7c478bd9Sstevel@tonic-gate 	size_t wcPathlen;	/* length of wide-character path */
328*7c478bd9Sstevel@tonic-gate 	wchar_t *wptr;		/* scratch pointer */
329*7c478bd9Sstevel@tonic-gate 	wchar_t *wcPath;	/* wide-character version of the path */
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	/*
332*7c478bd9Sstevel@tonic-gate 	 *  bail out if there is nothing there.
333*7c478bd9Sstevel@tonic-gate 	 */
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	if (!mbPath)
336*7c478bd9Sstevel@tonic-gate 	    return (mbPath);
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	/*
339*7c478bd9Sstevel@tonic-gate 	 *  convert the multi-byte version of the path to a
340*7c478bd9Sstevel@tonic-gate 	 *  wide-character rendering, for doing our figuring.
341*7c478bd9Sstevel@tonic-gate 	 */
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 	mbPathlen = strlen(mbPath);
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 	if ((wcPath = calloc(sizeof (wchar_t), mbPathlen+1)) == NULL) {
346*7c478bd9Sstevel@tonic-gate 		perror("mkdir");
347*7c478bd9Sstevel@tonic-gate 		exit(2);
348*7c478bd9Sstevel@tonic-gate 	}
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 	if ((wcPathlen = mbstowcs(wcPath, mbPath, mbPathlen)) == (size_t)-1) {
351*7c478bd9Sstevel@tonic-gate 		free(wcPath);
352*7c478bd9Sstevel@tonic-gate 		return (NULL);
353*7c478bd9Sstevel@tonic-gate 	}
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 	/*
356*7c478bd9Sstevel@tonic-gate 	 *  remove duplicate slashes first ("//../" -> "/")
357*7c478bd9Sstevel@tonic-gate 	 */
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	for (wptr = wcPath, i = 0; i < wcPathlen; i++) {
360*7c478bd9Sstevel@tonic-gate 		*wptr++ = wcPath[i];
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 		if (wcPath[i] == '/') {
363*7c478bd9Sstevel@tonic-gate 			i++;
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 			while (wcPath[i] == '/') {
366*7c478bd9Sstevel@tonic-gate 				i++;
367*7c478bd9Sstevel@tonic-gate 			}
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 			i--;
370*7c478bd9Sstevel@tonic-gate 		}
371*7c478bd9Sstevel@tonic-gate 	}
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 	*wptr = '\0';
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	/*
376*7c478bd9Sstevel@tonic-gate 	 *  next skip initial occurances of "./"
377*7c478bd9Sstevel@tonic-gate 	 */
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	for (wcPathlen = wcslen(wcPath), wptr = wcPath, i = 0;
380*7c478bd9Sstevel@tonic-gate 	    i < wcPathlen-2 && wcPath[i] == '.' && wcPath[i+1] == '/';
381*7c478bd9Sstevel@tonic-gate 	    i += 2) {
382*7c478bd9Sstevel@tonic-gate 		/* empty body */
383*7c478bd9Sstevel@tonic-gate 	}
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 	/*
386*7c478bd9Sstevel@tonic-gate 	 *  now make reductions of various forms.
387*7c478bd9Sstevel@tonic-gate 	 */
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	while (i < wcPathlen) {
390*7c478bd9Sstevel@tonic-gate 		if (i < wcPathlen-2 && wcPath[i] == '/' &&
391*7c478bd9Sstevel@tonic-gate 		    wcPath[i+1] == '.' && wcPath[i+2] == '/') {
392*7c478bd9Sstevel@tonic-gate 			/* "/./" -> "/" */
393*7c478bd9Sstevel@tonic-gate 			i += 2;
394*7c478bd9Sstevel@tonic-gate 		} else {
395*7c478bd9Sstevel@tonic-gate 			/* Normal case: copy the character */
396*7c478bd9Sstevel@tonic-gate 			*wptr++ = wcPath[i++];
397*7c478bd9Sstevel@tonic-gate 		}
398*7c478bd9Sstevel@tonic-gate 	}
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 	*wptr = '\0';
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate 	/*
403*7c478bd9Sstevel@tonic-gate 	 *  now convert back to the multi-byte format.
404*7c478bd9Sstevel@tonic-gate 	 */
405*7c478bd9Sstevel@tonic-gate 
406*7c478bd9Sstevel@tonic-gate 	if (wcstombs(mbPath, wcPath, mbPathlen) == (size_t)-1) {
407*7c478bd9Sstevel@tonic-gate 		free(wcPath);
408*7c478bd9Sstevel@tonic-gate 		return (NULL);
409*7c478bd9Sstevel@tonic-gate 	}
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 	free(wcPath);
412*7c478bd9Sstevel@tonic-gate 	return (mbPath);
413*7c478bd9Sstevel@tonic-gate }
414