88929540a3feddbb1e8794f5995c12b620dedbed
[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 #include <errno.h>
38
39 #include "shell.h"
40
41 #if !defined (errno)
42 extern int errno;
43 #endif
44
45 #if defined (__CYGWIN__)
46 #include <sys/cygwin.h>
47
48 static int
49 _is_cygdrive (path)
50      char *path;
51 {
52   static char user[MAXPATHLEN];
53   static char system[MAXPATHLEN];
54   static int first_time = 1;
55
56   /* If the path is the first part of a network path, treat it as
57      existing. */
58   if (path[0] == '/' && path[1] == '/' && !strchr (path + 2, '/'))
59     return 1; 
60   /* Otherwise check for /cygdrive prefix. */
61   if (first_time)
62     {
63       char user_flags[MAXPATHLEN];
64       char system_flags[MAXPATHLEN];
65       /* Get the cygdrive info */
66       cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system, user_flags, system_flags);
67       first_time = 0;
68     }
69   return !strcasecmp (path, user) || !strcasecmp (path, system);
70 }
71 #endif /* __CYGWIN__ */ 
72
73 /* Return 1 if PATH corresponds to a directory.  A function for debugging. */
74 static int
75 _path_isdir (path)
76      char *path;
77 {
78   int l, x;
79   struct stat sb;
80
81   /* This should leave errno set to the correct value. */
82   l = stat (path, &sb) == 0 && S_ISDIR (sb.st_mode);
83 #if defined (__CYGWIN__)
84   if (l == 0)
85     l = _is_cygdrive (path);
86 #endif
87   return l;
88 }
89
90 /* Canonicalize PATH, and return a new path.  The new path differs from PATH
91    in that:
92         Multple `/'s are collapsed to a single `/'.
93         Leading `./'s and trailing `/.'s are removed.
94         Trailing `/'s are removed.
95         Non-leading `../'s and trailing `..'s are handled by removing
96         portions of the path. */
97
98 /* Look for ROOTEDPATH, PATHSEP, DIRSEP, and ISDIRSEP in ../../general.h */
99
100 #define DOUBLE_SLASH(p) ((p[0] == '/') && (p[1] == '/') && p[2] != '/')
101
102 char *
103 sh_canonpath (path, flags)
104      char *path;
105      int flags;
106 {
107   char stub_char;
108   char *result, *p, *q, *base, *dotdot;
109   int rooted, double_slash_path;
110
111   /* The result cannot be larger than the input PATH. */
112   result = (flags & PATH_NOALLOC) ? path : savestring (path);
113
114   /* POSIX.2 says to leave a leading `//' alone.  On cygwin, we skip over any
115      leading `x:' (dos drive name). */
116   if (rooted = ROOTEDPATH(path))
117     {
118       stub_char = DIRSEP;
119 #if defined (__CYGWIN__)
120       base = (ISALPHA((unsigned char)result[0]) && result[1] == ':') ? result + 3 : result + 1;
121 #else
122       base = result + 1;
123 #endif
124       double_slash_path = DOUBLE_SLASH (path);
125       base += double_slash_path;
126     }
127   else
128     {
129       stub_char = '.';
130 #if defined (__CYGWIN__)
131       base = (ISALPHA((unsigned char)result[0]) && result[1] == ':') ? result + 2 : result;
132 #else
133       base = result;
134 #endif
135       double_slash_path = 0;
136     }
137
138   /*
139    * invariants:
140    *      base points to the portion of the path we want to modify
141    *      p points at beginning of path element we're considering.
142    *      q points just past the last path element we wrote (no slash).
143    *      dotdot points just past the point where .. cannot backtrack
144    *      any further (no slash).
145    */
146   p = q = dotdot = base;
147
148   while (*p)
149     {
150       if (ISDIRSEP(p[0])) /* null element */
151         p++;
152       else if(p[0] == '.' && PATHSEP(p[1]))     /* . and ./ */
153         p += 1;         /* don't count the separator in case it is nul */
154       else if (p[0] == '.' && p[1] == '.' && PATHSEP(p[2])) /* .. and ../ */
155         {
156           p += 2; /* skip `..' */
157           if (q > dotdot)       /* can backtrack */
158             {
159               if (flags & PATH_CHECKDOTDOT)
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               while (--q > dotdot && ISDIRSEP(*q) == 0)
177                 ;
178             }
179           else if (rooted == 0)
180             {
181               /* /.. is / but ./../ is .. */
182               if (q != base)
183                 *q++ = DIRSEP;
184               *q++ = '.';
185               *q++ = '.';
186               dotdot = q;
187             }
188         }
189       else      /* real path element */
190         {
191           /* add separator if not at start of work portion of result */
192           if (q != base)
193             *q++ = DIRSEP;
194           while (*p && (ISDIRSEP(*p) == 0))
195             *q++ = *p++;
196           /* Check here for a valid directory with _path_isdir. */
197           if (flags & PATH_CHECKEXISTS)
198             {
199               char c;
200
201               /* Make sure what we have so far corresponds to a valid
202                  path before we chop some of it off. */
203               c = *q;
204               *q = '\0';
205               if (_path_isdir (result) == 0)
206                 {
207                   if ((flags & PATH_NOALLOC) == 0)
208                     free (result);
209                   return ((char *)NULL);
210                 }
211               *q = c;
212             }
213         }
214     }
215
216   /* Empty string is really ``.'' or `/', depending on what we started with. */
217   if (q == result)
218     *q++ = stub_char;
219   *q = '\0';
220
221   /* If the result starts with `//', but the original path does not, we
222      can turn the // into /.  Because of how we set `base', this should never
223      be true, but it's a sanity check. */
224   if (DOUBLE_SLASH(result) && double_slash_path == 0)
225     {
226       if (result[2] == '\0')    /* short-circuit for bare `//' */
227         result[1] = '\0';
228       else
229         strcpy (result, result + 1);
230     }
231
232   return (result);
233 }