8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / cscope-fast / compath.c
blob7301d7297d826be290d4eacb8b8fc53f31cabd93
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
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"
34 * compath(pathname)
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 #include <string.h>
49 char *
50 compath(char *pathname)
52 char *nextchar;
53 char *lastchar;
54 char *sofar;
55 char *pnend;
57 int pnlen;
60 * do not change the path if it has no "/"
63 if (strchr(pathname, '/') == 0)
64 return (pathname);
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
77 nextchar = lastchar;
78 while (*++lastchar == '/') {
82 * eliminate the extra slashes by copying
83 * everything after the slashes over the slashes
86 sofar = nextchar;
87 while ((*nextchar++ = *lastchar++) != '\0')
89 lastchar = sofar;
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;
105 sofar = nextchar;
106 while ((*nextchar++ = *++lastchar) != '\0')
108 lastchar = sofar;
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) != '/'))
127 --nextchar;
130 * make sure the preceding directory's name
131 * is not "." or ".."
134 if ((*nextchar == '.') &&
135 (*(nextchar + 1) == '/') ||
136 ((*(nextchar + 1) == '.') &&
137 (*(nextchar + 2) == '/'))) {
138 /* EMPTY */;
139 } else {
142 * prepare to eliminate either
143 * "dir_name/../" or "dir_name/.."
146 if (*(lastchar + 3) == '/')
147 lastchar += 4;
148 else
149 lastchar += 3;
152 * copy everything after the "/.." to
153 * before the preceding directory name
156 sofar = nextchar - 1;
157 while ((*nextchar++ = *lastchar++) != '\0');
159 lastchar = sofar;
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 == '/'))
168 --lastchar;
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 == '/')) {
181 *pnend-- = '\0';
182 pnlen--;
186 * if the string has more than two characters and ends in
187 * "/.", remove the "/.".
190 if ((pnlen > 2) && (*(pnend - 1) == '/') && (*pnend == '.'))
191 *--pnend = '\0';
194 * if all characters were deleted, return ".";
195 * otherwise return pathname
198 if (*pathname == '\0')
199 (void) strcpy(pathname, ".");
201 return (pathname);