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) 1988 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 (c) 1999 by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  *	compath(pathname)
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  *	This compresses pathnames.  All strings of multiple slashes are
35*7c478bd9Sstevel@tonic-gate  *	changed to a single slash.  All occurrences of "./" are removed.
36*7c478bd9Sstevel@tonic-gate  *	Whenever possible, strings of "/.." are removed together with
37*7c478bd9Sstevel@tonic-gate  *	the directory names that they follow.
38*7c478bd9Sstevel@tonic-gate  *
39*7c478bd9Sstevel@tonic-gate  *	WARNING: since pathname is altered by this function, it should
40*7c478bd9Sstevel@tonic-gate  *		 be located in a temporary buffer. This avoids the problem
41*7c478bd9Sstevel@tonic-gate  *		 of accidently changing strings obtained from makefiles
42*7c478bd9Sstevel@tonic-gate  *		 and stored in global structures.
43*7c478bd9Sstevel@tonic-gate  */
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #include <string.h>
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate char *
compath(char * pathname)48*7c478bd9Sstevel@tonic-gate compath(char *pathname)
49*7c478bd9Sstevel@tonic-gate {
50*7c478bd9Sstevel@tonic-gate 	char	*nextchar;
51*7c478bd9Sstevel@tonic-gate 	char	*lastchar;
52*7c478bd9Sstevel@tonic-gate 	char	*sofar;
53*7c478bd9Sstevel@tonic-gate 	char	*pnend;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	int	pnlen;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 		/*
58*7c478bd9Sstevel@tonic-gate 		 *	do not change the path if it has no "/"
59*7c478bd9Sstevel@tonic-gate 		 */
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	if (strchr(pathname, '/') == 0)
62*7c478bd9Sstevel@tonic-gate 		return (pathname);
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 		/*
65*7c478bd9Sstevel@tonic-gate 		 *	find all strings consisting of more than one '/'
66*7c478bd9Sstevel@tonic-gate 		 */
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	for (lastchar = pathname + 1; *lastchar != '\0'; lastchar++)
69*7c478bd9Sstevel@tonic-gate 		if ((*lastchar == '/') && (*(lastchar - 1) == '/')) {
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 			/*
72*7c478bd9Sstevel@tonic-gate 			 *	find the character after the last slash
73*7c478bd9Sstevel@tonic-gate 			 */
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 			nextchar = lastchar;
76*7c478bd9Sstevel@tonic-gate 			while (*++lastchar == '/') {
77*7c478bd9Sstevel@tonic-gate 			}
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 			/*
80*7c478bd9Sstevel@tonic-gate 			 *	eliminate the extra slashes by copying
81*7c478bd9Sstevel@tonic-gate 			 *	everything after the slashes over the slashes
82*7c478bd9Sstevel@tonic-gate 			 */
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 			sofar = nextchar;
85*7c478bd9Sstevel@tonic-gate 			while ((*nextchar++ = *lastchar++) != '\0')
86*7c478bd9Sstevel@tonic-gate 				;
87*7c478bd9Sstevel@tonic-gate 			lastchar = sofar;
88*7c478bd9Sstevel@tonic-gate 		}
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 		/*
91*7c478bd9Sstevel@tonic-gate 		 *	find all strings of "./"
92*7c478bd9Sstevel@tonic-gate 		 */
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	for (lastchar = pathname + 1; *lastchar != '\0'; lastchar++)
95*7c478bd9Sstevel@tonic-gate 		if ((*lastchar == '/') && (*(lastchar - 1) == '.') &&
96*7c478bd9Sstevel@tonic-gate 		    ((lastchar - 1 == pathname) || (*(lastchar - 2) == '/'))) {
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 			/*
99*7c478bd9Sstevel@tonic-gate 			 *	copy everything after the "./" over the "./"
100*7c478bd9Sstevel@tonic-gate 			 */
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 			nextchar = lastchar - 1;
103*7c478bd9Sstevel@tonic-gate 			sofar = nextchar;
104*7c478bd9Sstevel@tonic-gate 			while ((*nextchar++ = *++lastchar) != '\0')
105*7c478bd9Sstevel@tonic-gate 				;
106*7c478bd9Sstevel@tonic-gate 			lastchar = sofar;
107*7c478bd9Sstevel@tonic-gate 		}
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 		/*
110*7c478bd9Sstevel@tonic-gate 		 *	find each occurrence of "/.."
111*7c478bd9Sstevel@tonic-gate 		 */
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	for (lastchar = pathname + 1; *lastchar != '\0'; lastchar++)
114*7c478bd9Sstevel@tonic-gate 		if ((lastchar != pathname) && (*lastchar == '/') &&
115*7c478bd9Sstevel@tonic-gate 		    (*(lastchar + 1) == '.') && (*(lastchar + 2) == '.') &&
116*7c478bd9Sstevel@tonic-gate 		    ((*(lastchar + 3) == '/') || (*(lastchar + 3) == '\0'))) {
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 			/*
119*7c478bd9Sstevel@tonic-gate 			 *	find the directory name preceding the "/.."
120*7c478bd9Sstevel@tonic-gate 			 */
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 			nextchar = lastchar - 1;
123*7c478bd9Sstevel@tonic-gate 			while ((nextchar != pathname) &&
124*7c478bd9Sstevel@tonic-gate 			    (*(nextchar - 1) != '/'))
125*7c478bd9Sstevel@tonic-gate 				--nextchar;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 			/*
128*7c478bd9Sstevel@tonic-gate 			 *	make sure the preceding directory's name
129*7c478bd9Sstevel@tonic-gate 			 *	is not "." or ".."
130*7c478bd9Sstevel@tonic-gate 			 */
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 			if ((*nextchar == '.') &&
133*7c478bd9Sstevel@tonic-gate 			    (*(nextchar + 1) == '/') ||
134*7c478bd9Sstevel@tonic-gate 			    ((*(nextchar + 1) == '.') &&
135*7c478bd9Sstevel@tonic-gate 			    (*(nextchar + 2) == '/'))) {
136*7c478bd9Sstevel@tonic-gate 				/* EMPTY */;
137*7c478bd9Sstevel@tonic-gate 			} else {
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 				/*
140*7c478bd9Sstevel@tonic-gate 				 * 	prepare to eliminate either
141*7c478bd9Sstevel@tonic-gate 				 *	"dir_name/../" or "dir_name/.."
142*7c478bd9Sstevel@tonic-gate 				 */
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 				if (*(lastchar + 3) == '/')
145*7c478bd9Sstevel@tonic-gate 					lastchar += 4;
146*7c478bd9Sstevel@tonic-gate 				else
147*7c478bd9Sstevel@tonic-gate 					lastchar += 3;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 				/*
150*7c478bd9Sstevel@tonic-gate 				 *	copy everything after the "/.." to
151*7c478bd9Sstevel@tonic-gate 				 *	before the preceding directory name
152*7c478bd9Sstevel@tonic-gate 				 */
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 				sofar = nextchar - 1;
155*7c478bd9Sstevel@tonic-gate 				while ((*nextchar++ = *lastchar++) != '\0');
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 				lastchar = sofar;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 				/*
160*7c478bd9Sstevel@tonic-gate 				 *	if the character before what was taken
161*7c478bd9Sstevel@tonic-gate 				 *	out is '/', set up to check if the
162*7c478bd9Sstevel@tonic-gate 				 *	slash is part of "/.."
163*7c478bd9Sstevel@tonic-gate 				 */
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 				if ((sofar + 1 != pathname) && (*sofar == '/'))
166*7c478bd9Sstevel@tonic-gate 					--lastchar;
167*7c478bd9Sstevel@tonic-gate 			}
168*7c478bd9Sstevel@tonic-gate 		}
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	/*
171*7c478bd9Sstevel@tonic-gate 	 *	if the string is more than a character long and ends
172*7c478bd9Sstevel@tonic-gate 	 *	in '/', eliminate the '/'.
173*7c478bd9Sstevel@tonic-gate 	 */
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	pnlen = strlen(pathname);
176*7c478bd9Sstevel@tonic-gate 	pnend = strchr(pathname, '\0') - 1;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	if ((pnlen > 1) && (*pnend == '/')) {
179*7c478bd9Sstevel@tonic-gate 		*pnend-- = '\0';
180*7c478bd9Sstevel@tonic-gate 		pnlen--;
181*7c478bd9Sstevel@tonic-gate 	}
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	/*
184*7c478bd9Sstevel@tonic-gate 	 *	if the string has more than two characters and ends in
185*7c478bd9Sstevel@tonic-gate 	 *	"/.", remove the "/.".
186*7c478bd9Sstevel@tonic-gate 	 */
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	if ((pnlen > 2) && (*(pnend - 1) == '/') && (*pnend == '.'))
189*7c478bd9Sstevel@tonic-gate 		*--pnend = '\0';
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	/*
192*7c478bd9Sstevel@tonic-gate 	 *	if all characters were deleted, return ".";
193*7c478bd9Sstevel@tonic-gate 	 *	otherwise return pathname
194*7c478bd9Sstevel@tonic-gate 	 */
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	if (*pathname == '\0')
197*7c478bd9Sstevel@tonic-gate 		(void) strcpy(pathname, ".");
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	return (pathname);
200*7c478bd9Sstevel@tonic-gate }
201