]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/sh/pathcanon.c
Imported from ../bash-2.05b.tar.gz.
[thirdparty/bash.git] / lib / sh / pathcanon.c
CommitLineData
28ef6c31
JA
1/* pathcanon.c -- Canonicalize and manipulate pathnames. */
2
3/* Copyright (C) 2000 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20
f73dda09 21#include <config.h>
28ef6c31 22
f73dda09 23#include <bashtypes.h>
28ef6c31
JA
24#ifndef _MINIX
25# include <sys/param.h>
26#endif
f73dda09 27#include <posixstat.h>
28ef6c31
JA
28
29#if defined (HAVE_UNISTD_H)
30# include <unistd.h>
31#endif
32
f73dda09
JA
33#include <filecntl.h>
34#include <bashansi.h>
28ef6c31 35#include <stdio.h>
f73dda09 36#include <chartypes.h>
28ef6c31
JA
37
38#include "shell.h"
39
7117c2d2
JA
40#if defined (__CYGWIN__)
41#include <sys/cygwin.h>
42
43static int
44_is_cygdrive (path)
45 char *path;
46{
47 static char user[MAXPATHLEN];
48 static char system[MAXPATHLEN];
49 static int first_time = 1;
50
51 /* If the path is the first part of a network path, treat it as
52 existing. */
53 if (path[0] == '/' && path[1] == '/' && !strchr (path + 2, '/'))
54 return 1;
55 /* Otherwise check for /cygdrive prefix. */
56 if (first_time)
57 {
58 char user_flags[MAXPATHLEN];
59 char system_flags[MAXPATHLEN];
60 /* Get the cygdrive info */
61 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system, user_flags, system_flags);
62 first_time = 0;
63 }
64 return !strcasecmp (path, user) || !strcasecmp (path, system);
65}
66#endif /* __CYGWIN__ */
67
28ef6c31
JA
68/* Return 1 if PATH corresponds to a directory. A function for debugging. */
69static int
70_path_isdir (path)
71 char *path;
72{
73 int l;
74 struct stat sb;
75
76 l = stat (path, &sb) == 0 && S_ISDIR (sb.st_mode);
7117c2d2
JA
77#if defined (__CYGWIN__)
78 if (l == 0)
79 l = _is_cygdrive (path);
80#endif
28ef6c31
JA
81 return l;
82}
83
84/* Canonicalize PATH, and return a new path. The new path differs from PATH
85 in that:
86 Multple `/'s are collapsed to a single `/'.
87 Leading `./'s and trailing `/.'s are removed.
88 Trailing `/'s are removed.
89 Non-leading `../'s and trailing `..'s are handled by removing
90 portions of the path. */
91
92/* Look for ROOTEDPATH, PATHSEP, DIRSEP, and ISDIRSEP in ../../general.h */
93
94#define DOUBLE_SLASH(p) ((p[0] == '/') && (p[1] == '/') && p[2] != '/')
95
96char *
97sh_canonpath (path, flags)
98 char *path;
99 int flags;
100{
101 char stub_char;
102 char *result, *p, *q, *base, *dotdot;
103 int rooted, double_slash_path;
104
105 /* The result cannot be larger than the input PATH. */
106 result = (flags & PATH_NOALLOC) ? path : savestring (path);
107
108 /* POSIX.2 says to leave a leading `//' alone. On cygwin, we skip over any
109 leading `x:' (dos drive name). */
110 if (rooted = ROOTEDPATH(path))
111 {
112 stub_char = DIRSEP;
113#if defined (__CYGWIN__)
f73dda09 114 base = (ISALPHA((unsigned char)result[0]) && result[1] == ':') ? result + 3 : result + 1;
28ef6c31
JA
115#else
116 base = result + 1;
117#endif
118 double_slash_path = DOUBLE_SLASH (path);
119 base += double_slash_path;
120 }
121 else
122 {
123 stub_char = '.';
124#if defined (__CYGWIN__)
f73dda09 125 base = (ISALPHA((unsigned char)result[0]) && result[1] == ':') ? result + 2 : result;
28ef6c31
JA
126#else
127 base = result;
128#endif
f73dda09 129 double_slash_path = 0;
28ef6c31
JA
130 }
131
132 /*
133 * invariants:
134 * base points to the portion of the path we want to modify
135 * p points at beginning of path element we're considering.
136 * q points just past the last path element we wrote (no slash).
137 * dotdot points just past the point where .. cannot backtrack
138 * any further (no slash).
139 */
140 p = q = dotdot = base;
141
142 while (*p)
143 {
144 if (ISDIRSEP(p[0])) /* null element */
145 p++;
146 else if(p[0] == '.' && PATHSEP(p[1])) /* . and ./ */
147 p += 1; /* don't count the separator in case it is nul */
148 else if (p[0] == '.' && p[1] == '.' && PATHSEP(p[2])) /* .. and ../ */
149 {
150 p += 2; /* skip `..' */
151 if (q > dotdot) /* can backtrack */
152 {
153 if (flags & PATH_CHECKDOTDOT)
154 {
155 char c;
156
157 /* Make sure what we have so far corresponds to a valid
158 path before we chop some of it off. */
159 c = *q;
160 *q = '\0';
161 if (_path_isdir (result) == 0)
162 {
163 if ((flags & PATH_NOALLOC) == 0)
164 free (result);
165 return ((char *)NULL);
166 }
167 *q = c;
168 }
169
170 while (--q > dotdot && ISDIRSEP(*q) == 0)
171 ;
172 }
173 else if (rooted == 0)
174 {
175 /* /.. is / but ./../ is .. */
176 if (q != base)
177 *q++ = DIRSEP;
178 *q++ = '.';
179 *q++ = '.';
180 dotdot = q;
181 }
182 }
183 else /* real path element */
184 {
185 /* add separator if not at start of work portion of result */
186 if (q != base)
187 *q++ = DIRSEP;
188 while (*p && (ISDIRSEP(*p) == 0))
189 *q++ = *p++;
190 /* Check here for a valid directory with _path_isdir. */
191 if (flags & PATH_CHECKEXISTS)
192 {
193 char c;
194
195 /* Make sure what we have so far corresponds to a valid
196 path before we chop some of it off. */
197 c = *q;
198 *q = '\0';
199 if (_path_isdir (result) == 0)
200 {
201 if ((flags & PATH_NOALLOC) == 0)
202 free (result);
203 return ((char *)NULL);
204 }
205 *q = c;
206 }
207 }
208 }
209
210 /* Empty string is really ``.'' or `/', depending on what we started with. */
211 if (q == result)
212 *q++ = stub_char;
213 *q = '\0';
214
215 /* If the result starts with `//', but the original path does not, we
216 can turn the // into /. Because of how we set `base', this should never
217 be true, but it's a sanity check. */
218 if (DOUBLE_SLASH(result) && double_slash_path == 0)
219 {
220 if (result[2] == '\0') /* short-circuit for bare `//' */
221 result[1] = '\0';
222 else
223 strcpy (result, result + 1);
224 }
225
226 return (result);
227}