dcde8b07c6108fbdaefc34142e5e6929e49fddcf
[platform/upstream/bash.git] / lib / sh / pathcanon.c
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
21 #include <config.h>
22
23 #include <bashtypes.h>
24 #ifndef _MINIX
25 #  include <sys/param.h>
26 #endif
27 #include <posixstat.h>
28
29 #if defined (HAVE_UNISTD_H)
30 #  include <unistd.h>
31 #endif
32
33 #include <filecntl.h>
34 #include <bashansi.h>
35 #include <stdio.h>
36 #include <chartypes.h>
37
38 #include "shell.h"
39
40 /* Return 1 if PATH corresponds to a directory.  A function for debugging. */
41 static int
42 _path_isdir (path)
43      char *path;
44 {
45   int l;
46   struct stat sb;
47
48   l = stat (path, &sb) == 0 && S_ISDIR (sb.st_mode);
49   return l;
50 }
51
52 /* Canonicalize PATH, and return a new path.  The new path differs from PATH
53    in that:
54         Multple `/'s are collapsed to a single `/'.
55         Leading `./'s and trailing `/.'s are removed.
56         Trailing `/'s are removed.
57         Non-leading `../'s and trailing `..'s are handled by removing
58         portions of the path. */
59
60 /* Look for ROOTEDPATH, PATHSEP, DIRSEP, and ISDIRSEP in ../../general.h */
61
62 #define DOUBLE_SLASH(p) ((p[0] == '/') && (p[1] == '/') && p[2] != '/')
63
64 char *
65 sh_canonpath (path, flags)
66      char *path;
67      int flags;
68 {
69   char stub_char;
70   char *result, *p, *q, *base, *dotdot;
71   int rooted, double_slash_path;
72
73   /* The result cannot be larger than the input PATH. */
74   result = (flags & PATH_NOALLOC) ? path : savestring (path);
75
76   /* POSIX.2 says to leave a leading `//' alone.  On cygwin, we skip over any
77      leading `x:' (dos drive name). */
78   if (rooted = ROOTEDPATH(path))
79     {
80       stub_char = DIRSEP;
81 #if defined (__CYGWIN__)
82       base = (ISALPHA((unsigned char)result[0]) && result[1] == ':') ? result + 3 : result + 1;
83 #else
84       base = result + 1;
85 #endif
86       double_slash_path = DOUBLE_SLASH (path);
87       base += double_slash_path;
88     }
89   else
90     {
91       stub_char = '.';
92 #if defined (__CYGWIN__)
93       base = (ISALPHA((unsigned char)result[0]) && result[1] == ':') ? result + 2 : result;
94 #else
95       base = result;
96 #endif
97       double_slash_path = 0;
98     }
99
100   /*
101    * invariants:
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).
107    */
108   p = q = dotdot = base;
109
110   while (*p)
111     {
112       if (ISDIRSEP(p[0])) /* null element */
113         p++;
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 ../ */
117         {
118           p += 2; /* skip `..' */
119           if (q > dotdot)       /* can backtrack */
120             {
121               if (flags & PATH_CHECKDOTDOT)
122                 {
123                   char c;
124
125                   /* Make sure what we have so far corresponds to a valid
126                      path before we chop some of it off. */
127                   c = *q;
128                   *q = '\0';
129                   if (_path_isdir (result) == 0)
130                     {
131                       if ((flags & PATH_NOALLOC) == 0)
132                         free (result);
133                       return ((char *)NULL);
134                     }
135                   *q = c;
136                 }
137
138               while (--q > dotdot && ISDIRSEP(*q) == 0)
139                 ;
140             }
141           else if (rooted == 0)
142             {
143               /* /.. is / but ./../ is .. */
144               if (q != base)
145                 *q++ = DIRSEP;
146               *q++ = '.';
147               *q++ = '.';
148               dotdot = q;
149             }
150         }
151       else      /* real path element */
152         {
153           /* add separator if not at start of work portion of result */
154           if (q != base)
155             *q++ = DIRSEP;
156           while (*p && (ISDIRSEP(*p) == 0))
157             *q++ = *p++;
158           /* Check here for a valid directory with _path_isdir. */
159           if (flags & PATH_CHECKEXISTS)
160             {
161               char c;
162
163               /* Make sure what we have so far corresponds to a valid
164                  path before we chop some of it off. */
165               c = *q;
166               *q = '\0';
167               if (_path_isdir (result) == 0)
168                 {
169                   if ((flags & PATH_NOALLOC) == 0)
170                     free (result);
171                   return ((char *)NULL);
172                 }
173               *q = c;
174             }
175         }
176     }
177
178   /* Empty string is really ``.'' or `/', depending on what we started with. */
179   if (q == result)
180     *q++ = stub_char;
181   *q = '\0';
182
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)
187     {
188       if (result[2] == '\0')    /* short-circuit for bare `//' */
189         result[1] = '\0';
190       else
191         strcpy (result, result + 1);
192     }
193
194   return (result);
195 }