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