xref: /onnv-gate/usr/src/lib/libpkg/common/canonize.c (revision 9781:ccf49524d5dc)
1*9781SMoriah.Waterland@Sun.COM /*
2*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER START
3*9781SMoriah.Waterland@Sun.COM  *
4*9781SMoriah.Waterland@Sun.COM  * The contents of this file are subject to the terms of the
5*9781SMoriah.Waterland@Sun.COM  * Common Development and Distribution License (the "License").
6*9781SMoriah.Waterland@Sun.COM  * You may not use this file except in compliance with the License.
7*9781SMoriah.Waterland@Sun.COM  *
8*9781SMoriah.Waterland@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9781SMoriah.Waterland@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*9781SMoriah.Waterland@Sun.COM  * See the License for the specific language governing permissions
11*9781SMoriah.Waterland@Sun.COM  * and limitations under the License.
12*9781SMoriah.Waterland@Sun.COM  *
13*9781SMoriah.Waterland@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*9781SMoriah.Waterland@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9781SMoriah.Waterland@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*9781SMoriah.Waterland@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*9781SMoriah.Waterland@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*9781SMoriah.Waterland@Sun.COM  *
19*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER END
20*9781SMoriah.Waterland@Sun.COM  */
21*9781SMoriah.Waterland@Sun.COM 
22*9781SMoriah.Waterland@Sun.COM /*
23*9781SMoriah.Waterland@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*9781SMoriah.Waterland@Sun.COM  * Use is subject to license terms.
25*9781SMoriah.Waterland@Sun.COM  */
26*9781SMoriah.Waterland@Sun.COM 
27*9781SMoriah.Waterland@Sun.COM /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*9781SMoriah.Waterland@Sun.COM /* All Rights Reserved */
29*9781SMoriah.Waterland@Sun.COM 
30*9781SMoriah.Waterland@Sun.COM 
31*9781SMoriah.Waterland@Sun.COM #include <string.h>
32*9781SMoriah.Waterland@Sun.COM 
33*9781SMoriah.Waterland@Sun.COM #define	isdot(x)	((x[0] == '.') && (!x[1] || (x[1] == '/')))
34*9781SMoriah.Waterland@Sun.COM #define	isdotdot(x)	((x[0] == '.') && (x[1] == '.') && \
35*9781SMoriah.Waterland@Sun.COM 			    (!x[2] || (x[2] == '/')))
36*9781SMoriah.Waterland@Sun.COM 
37*9781SMoriah.Waterland@Sun.COM void
canonize(char * file)38*9781SMoriah.Waterland@Sun.COM canonize(char *file)
39*9781SMoriah.Waterland@Sun.COM {
40*9781SMoriah.Waterland@Sun.COM 	char *pt, *last;
41*9781SMoriah.Waterland@Sun.COM 	int level;
42*9781SMoriah.Waterland@Sun.COM 
43*9781SMoriah.Waterland@Sun.COM 	/* remove references such as "./" and "../" and "//" */
44*9781SMoriah.Waterland@Sun.COM 	for (pt = file; *pt; /* void */) {
45*9781SMoriah.Waterland@Sun.COM 		if (isdot(pt))
46*9781SMoriah.Waterland@Sun.COM 			(void) strcpy(pt, pt[1] ? pt+2 : pt+1);
47*9781SMoriah.Waterland@Sun.COM 		else if (isdotdot(pt)) {
48*9781SMoriah.Waterland@Sun.COM 			level = 0;
49*9781SMoriah.Waterland@Sun.COM 			last = pt;
50*9781SMoriah.Waterland@Sun.COM 			do {
51*9781SMoriah.Waterland@Sun.COM 				level++;
52*9781SMoriah.Waterland@Sun.COM 				last += 2;
53*9781SMoriah.Waterland@Sun.COM 				if (*last)
54*9781SMoriah.Waterland@Sun.COM 					last++;
55*9781SMoriah.Waterland@Sun.COM 			} while (isdotdot(last));
56*9781SMoriah.Waterland@Sun.COM 			--pt; /* point to previous '/' */
57*9781SMoriah.Waterland@Sun.COM 			while (level--) {
58*9781SMoriah.Waterland@Sun.COM 				if (pt <= file)
59*9781SMoriah.Waterland@Sun.COM 					return;
60*9781SMoriah.Waterland@Sun.COM 				while ((*--pt != '/') && (pt > file))
61*9781SMoriah.Waterland@Sun.COM 					;
62*9781SMoriah.Waterland@Sun.COM 			}
63*9781SMoriah.Waterland@Sun.COM 			if (*pt == '/')
64*9781SMoriah.Waterland@Sun.COM 				pt++;
65*9781SMoriah.Waterland@Sun.COM 			(void) strcpy(pt, last);
66*9781SMoriah.Waterland@Sun.COM 		} else {
67*9781SMoriah.Waterland@Sun.COM 			while (*pt && (*pt != '/'))
68*9781SMoriah.Waterland@Sun.COM 				pt++;
69*9781SMoriah.Waterland@Sun.COM 			if (*pt == '/') {
70*9781SMoriah.Waterland@Sun.COM 				while (pt[1] == '/')
71*9781SMoriah.Waterland@Sun.COM 					(void) strcpy(pt, pt+1);
72*9781SMoriah.Waterland@Sun.COM 				pt++;
73*9781SMoriah.Waterland@Sun.COM 			}
74*9781SMoriah.Waterland@Sun.COM 		}
75*9781SMoriah.Waterland@Sun.COM 	}
76*9781SMoriah.Waterland@Sun.COM 	if ((--pt > file) && (*pt == '/'))
77*9781SMoriah.Waterland@Sun.COM 		*pt = '\0';
78*9781SMoriah.Waterland@Sun.COM }
79*9781SMoriah.Waterland@Sun.COM 
80*9781SMoriah.Waterland@Sun.COM void
canonize_slashes(char * file)81*9781SMoriah.Waterland@Sun.COM canonize_slashes(char *file)
82*9781SMoriah.Waterland@Sun.COM {
83*9781SMoriah.Waterland@Sun.COM 	char *pt;
84*9781SMoriah.Waterland@Sun.COM 
85*9781SMoriah.Waterland@Sun.COM 	/* remove references such as "//" */
86*9781SMoriah.Waterland@Sun.COM 	for (pt = file; *pt; /* void */) {
87*9781SMoriah.Waterland@Sun.COM 		while (*pt && (*pt != '/'))
88*9781SMoriah.Waterland@Sun.COM 			pt++;
89*9781SMoriah.Waterland@Sun.COM 		if (*pt == '/') {
90*9781SMoriah.Waterland@Sun.COM 			while (pt[1] == '/')
91*9781SMoriah.Waterland@Sun.COM 				(void) strcpy(pt, pt+1);
92*9781SMoriah.Waterland@Sun.COM 			pt++;
93*9781SMoriah.Waterland@Sun.COM 		}
94*9781SMoriah.Waterland@Sun.COM 	}
95*9781SMoriah.Waterland@Sun.COM 	if ((--pt > file) && (*pt == '/'))
96*9781SMoriah.Waterland@Sun.COM 		*pt = '\0';
97*9781SMoriah.Waterland@Sun.COM }
98