]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/pathcanon.c
1 /* pathcanon.c -- Canonicalize and manipulate pathnames. */
3 /* Copyright (C) 2000 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
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
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
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. */
23 #include "bashtypes.h"
25 # include <sys/param.h>
27 #include "posixstat.h"
29 #if defined (HAVE_UNISTD_H)
41 /* Return 1 if PATH corresponds to a directory. A function for debugging. */
49 l
= stat (path
, &sb
) == 0 && S_ISDIR (sb
.st_mode
);
53 /* Canonicalize PATH, and return a new path. The new path differs from PATH
55 Multple `/'s are collapsed to a single `/'.
56 Leading `./'s and trailing `/.'s are removed.
57 Trailing `/'s are removed.
58 Non-leading `../'s and trailing `..'s are handled by removing
59 portions of the path. */
61 /* Look for ROOTEDPATH, PATHSEP, DIRSEP, and ISDIRSEP in ../../general.h */
63 #define DOUBLE_SLASH(p) ((p[0] == '/') && (p[1] == '/') && p[2] != '/')
66 sh_canonpath (path
, flags
)
71 char *result
, *p
, *q
, *base
, *dotdot
;
72 int rooted
, double_slash_path
;
74 /* The result cannot be larger than the input PATH. */
75 result
= (flags
& PATH_NOALLOC
) ? path
: savestring (path
);
77 /* POSIX.2 says to leave a leading `//' alone. On cygwin, we skip over any
78 leading `x:' (dos drive name). */
79 if (rooted
= ROOTEDPATH(path
))
82 #if defined (__CYGWIN__)
83 base
= (isalpha(result
[0]) && result
[1] == ':') ? result
+ 3 : result
+ 1;
87 double_slash_path
= DOUBLE_SLASH (path
);
88 base
+= double_slash_path
;
93 #if defined (__CYGWIN__)
94 base
= (isalpha(result
[0]) && result
[1] == ':') ? result
+ 2 : result
;
102 * base points to the portion of the path we want to modify
103 * p points at beginning of path element we're considering.
104 * q points just past the last path element we wrote (no slash).
105 * dotdot points just past the point where .. cannot backtrack
106 * any further (no slash).
108 p
= q
= dotdot
= base
;
112 if (ISDIRSEP(p
[0])) /* null element */
114 else if(p
[0] == '.' && PATHSEP(p
[1])) /* . and ./ */
115 p
+= 1; /* don't count the separator in case it is nul */
116 else if (p
[0] == '.' && p
[1] == '.' && PATHSEP(p
[2])) /* .. and ../ */
118 p
+= 2; /* skip `..' */
119 if (q
> dotdot
) /* can backtrack */
121 if (flags
& PATH_CHECKDOTDOT
)
125 /* Make sure what we have so far corresponds to a valid
126 path before we chop some of it off. */
129 if (_path_isdir (result
) == 0)
131 if ((flags
& PATH_NOALLOC
) == 0)
133 return ((char *)NULL
);
138 while (--q
> dotdot
&& ISDIRSEP(*q
) == 0)
141 else if (rooted
== 0)
143 /* /.. is / but ./../ is .. */
151 else /* real path element */
153 /* add separator if not at start of work portion of result */
156 while (*p
&& (ISDIRSEP(*p
) == 0))
158 /* Check here for a valid directory with _path_isdir. */
159 if (flags
& PATH_CHECKEXISTS
)
163 /* Make sure what we have so far corresponds to a valid
164 path before we chop some of it off. */
167 if (_path_isdir (result
) == 0)
169 if ((flags
& PATH_NOALLOC
) == 0)
171 return ((char *)NULL
);
178 /* Empty string is really ``.'' or `/', depending on what we started with. */
183 /* If the result starts with `//', but the original path does not, we
184 can turn the // into /. Because of how we set `base', this should never
185 be true, but it's a sanity check. */
186 if (DOUBLE_SLASH(result
) && double_slash_path
== 0)
188 if (result
[2] == '\0') /* short-circuit for bare `//' */
191 strcpy (result
, result
+ 1);