1 /*===========================================================================
2 Copyright (c) 1998-2000, The Santa Cruz Operation
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 *Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 *Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
15 *Neither name of The Santa Cruz Operation nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
20 IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 =========================================================================*/
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.
47 static char const rcsid[] = "$Id: compath.c,v 1.3 2001/07/05 14:31:00 broeker Exp $";
56 compath(char *pathname) /*FDEF*/
66 * do not change the path if it has no "/"
69 if (strchr(pathname, '/') == NULL)
73 * find all strings consisting of more than one '/'
76 for (lastchar = pathname + 1; *lastchar != '\0'; lastchar++)
77 if ((*lastchar == '/') && (*(lastchar - 1) == '/'))
81 * find the character after the last slash
85 while (*++lastchar == '/')
90 * eliminate the extra slashes by copying
91 * everything after the slashes over the slashes
95 while ((*nextchar++ = *lastchar++) != '\0')
101 * find all strings of "./"
104 for (lastchar = pathname + 1; *lastchar != '\0'; lastchar++)
105 if ((*lastchar == '/') && (*(lastchar - 1) == '.') &&
106 ((lastchar - 1 == pathname) || (*(lastchar - 2) == '/')))
110 * copy everything after the "./" over the "./"
113 nextchar = lastchar - 1;
115 while ((*nextchar++ = *++lastchar) != '\0')
121 * find each occurrence of "/.."
124 for (lastchar = pathname + 1; *lastchar != '\0'; lastchar++)
125 if ((lastchar != pathname) && (*lastchar == '/') &&
126 (*(lastchar + 1) == '.') && (*(lastchar + 2) == '.') &&
127 ((*(lastchar + 3) == '/') || (*(lastchar + 3) == '\0')))
131 * find the directory name preceding the "/.."
134 nextchar = lastchar - 1;
135 while ((nextchar != pathname) &&
136 (*(nextchar - 1) != '/'))
140 * make sure the preceding directory's name
144 if ((*nextchar == '.') &&
145 ((*(nextchar + 1) == '/') ||
146 ((*(nextchar + 1) == '.') && (*(nextchar + 2) == '/'))))
152 * prepare to eliminate either
153 * "dir_name/../" or "dir_name/.."
156 if (*(lastchar + 3) == '/')
162 * copy everything after the "/.." to
163 * before the preceding directory name
166 sofar = nextchar - 1;
167 while ((*nextchar++ = *lastchar++) != '\0');
172 * if the character before what was taken
173 * out is '/', set up to check if the
174 * slash is part of "/.."
177 if ((sofar + 1 != pathname) && (*sofar == '/'))
183 * if the string is more than a character long and ends
184 * in '/', eliminate the '/'.
187 pnlen = strlen(pathname);
188 pnend = strchr(pathname, '\0') - 1;
190 if ((pnlen > 1) && (*pnend == '/'))
197 * if the string has more than two characters and ends in
198 * "/.", remove the "/.".
201 if ((pnlen > 2) && (*(pnend - 1) == '/') && (*pnend == '.'))
205 * if all characters were deleted, return ".";
206 * otherwise return pathname
209 if (*pathname == '\0')
210 (void) strcpy(pathname, ".");