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