4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 /* Copyright (c) 1988 AT&T */
23 /* All Rights Reserved */
27 * Copyright (c) 1999 by Sun Microsystems, Inc.
28 * All rights reserved.
31 #pragma ident "%Z%%M% %I% %E% SMI"
36 * This compresses pathnames. All strings of multiple slashes are
37 * changed to a single slash. All occurrences of "./" are removed.
38 * Whenever possible, strings of "/.." are removed together with
39 * the directory names that they follow.
41 * WARNING: since pathname is altered by this function, it should
42 * be located in a temporary buffer. This avoids the problem
43 * of accidently changing strings obtained from makefiles
44 * and stored in global structures.
50 compath(char *pathname
)
60 * do not change the path if it has no "/"
63 if (strchr(pathname
, '/') == 0)
67 * find all strings consisting of more than one '/'
70 for (lastchar
= pathname
+ 1; *lastchar
!= '\0'; lastchar
++)
71 if ((*lastchar
== '/') && (*(lastchar
- 1) == '/')) {
74 * find the character after the last slash
78 while (*++lastchar
== '/') {
82 * eliminate the extra slashes by copying
83 * everything after the slashes over the slashes
87 while ((*nextchar
++ = *lastchar
++) != '\0')
93 * find all strings of "./"
96 for (lastchar
= pathname
+ 1; *lastchar
!= '\0'; lastchar
++)
97 if ((*lastchar
== '/') && (*(lastchar
- 1) == '.') &&
98 ((lastchar
- 1 == pathname
) || (*(lastchar
- 2) == '/'))) {
101 * copy everything after the "./" over the "./"
104 nextchar
= lastchar
- 1;
106 while ((*nextchar
++ = *++lastchar
) != '\0')
112 * find each occurrence of "/.."
115 for (lastchar
= pathname
+ 1; *lastchar
!= '\0'; lastchar
++)
116 if ((lastchar
!= pathname
) && (*lastchar
== '/') &&
117 (*(lastchar
+ 1) == '.') && (*(lastchar
+ 2) == '.') &&
118 ((*(lastchar
+ 3) == '/') || (*(lastchar
+ 3) == '\0'))) {
121 * find the directory name preceding the "/.."
124 nextchar
= lastchar
- 1;
125 while ((nextchar
!= pathname
) &&
126 (*(nextchar
- 1) != '/'))
130 * make sure the preceding directory's name
134 if ((*nextchar
== '.') &&
135 (*(nextchar
+ 1) == '/') ||
136 ((*(nextchar
+ 1) == '.') &&
137 (*(nextchar
+ 2) == '/'))) {
142 * prepare to eliminate either
143 * "dir_name/../" or "dir_name/.."
146 if (*(lastchar
+ 3) == '/')
152 * copy everything after the "/.." to
153 * before the preceding directory name
156 sofar
= nextchar
- 1;
157 while ((*nextchar
++ = *lastchar
++) != '\0');
162 * if the character before what was taken
163 * out is '/', set up to check if the
164 * slash is part of "/.."
167 if ((sofar
+ 1 != pathname
) && (*sofar
== '/'))
173 * if the string is more than a character long and ends
174 * in '/', eliminate the '/'.
177 pnlen
= strlen(pathname
);
178 pnend
= strchr(pathname
, '\0') - 1;
180 if ((pnlen
> 1) && (*pnend
== '/')) {
186 * if the string has more than two characters and ends in
187 * "/.", remove the "/.".
190 if ((pnlen
> 2) && (*(pnend
- 1) == '/') && (*pnend
== '.'))
194 * if all characters were deleted, return ".";
195 * otherwise return pathname
198 if (*pathname
== '\0')
199 (void) strcpy(pathname
, ".");