XEmacs 21.2-b1
[chise/xemacs-chise.git.1] / src / realpath.c
1 /*
2  * realpath.c -- canonicalize pathname by removing symlinks
3  * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
4  *
5
6 This file is part of XEmacs.
7
8 XEmacs is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with XEmacs; see the file COPYING.  If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 /* Synched up with: Not in FSF. */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <sys/types.h>
30 #if defined(HAVE_UNISTD_H) || defined(STDC_HEADERS)
31 #include <unistd.h>
32 #endif
33 #include <stdio.h>
34 #include <string.h>
35 #ifdef _POSIX_VERSION
36 #include <limits.h>                     /* for PATH_MAX */
37 #else
38 #include <sys/param.h>                  /* for MAXPATHLEN */
39 #endif
40 #include <errno.h>
41 #ifndef STDC_HEADERS
42 extern int errno;
43 #endif
44
45 #ifdef WINDOWSNT
46 #include <direct.h>
47 #endif
48
49 #include <sys/stat.h>                   /* for S_IFLNK */
50
51 #ifndef PATH_MAX
52 #ifdef _POSIX_VERSION
53 #define PATH_MAX _POSIX_PATH_MAX
54 #else
55 #ifdef MAXPATHLEN
56 #define PATH_MAX MAXPATHLEN
57 #else
58 #define PATH_MAX 1024
59 #endif
60 #endif
61 #endif
62
63 #define MAX_READLINKS 32
64
65 #ifdef __STDC__
66 char *xrealpath(const char *path, char resolved_path [])
67 #else
68 char *xrealpath(path, resolved_path)
69 const char *path;
70 char resolved_path [];
71 #endif
72 {
73   char copy_path[PATH_MAX];
74   char *new_path = resolved_path;
75   char *max_path;
76   int readlinks = 0;
77 #ifdef S_IFLNK
78   char link_path[PATH_MAX];
79   int n;
80 #endif
81
82   /* Make a copy of the source path since we may need to modify it. */
83   strcpy(copy_path, path);
84   path = copy_path;
85   max_path = copy_path + PATH_MAX - 2;
86 #ifdef WINDOWSNT
87   /*
88   ** In NT we have two different cases:  (1) the path name begins
89   ** with a drive letter, e.g., "C:"; and (2) the path name begins
90   ** with just a slash, which roots to the current drive. In the 
91   ** first case we are going to leave things alone, in the second
92   ** case we will prepend the drive letter to the given path.
93   ** Note: So far in testing, I'm only seeing case #1, even though
94   ** I've tried to get the other cases to happen. 
95   ** August Hill, 31 Aug 1997.
96   **
97   ** Check for a driver letter...C:/...
98   */
99   if (*(path + 1) == ':')
100     {
101       strncpy(new_path, path, 3);
102       new_path += 3;
103       path += 3;
104     }
105
106   /*
107   ** No drive letter, but a beginning slash? Prepend the drive
108   ** letter...
109   */
110   else if (*path == '/')
111     {
112       getcwd(new_path, PATH_MAX - 1);
113       new_path += 3;
114       path++;
115     }
116
117   /*
118   ** Just a path name, prepend the current directory
119   */
120   else
121     {
122       getcwd(new_path, PATH_MAX - 1);
123       new_path += strlen(new_path);
124       if (new_path[-1] != '/')
125         *new_path++ = '/';
126     }
127
128 #else
129   /* If it's a relative pathname use getwd for starters. */
130   if (*path != '/')
131     {
132 #ifdef HAVE_GETCWD
133       getcwd(new_path, PATH_MAX - 1);
134 #else
135       getwd(new_path);
136 #endif
137       new_path += strlen(new_path);
138       if (new_path[-1] != '/')
139         *new_path++ = '/';
140     }
141   else
142     {
143       *new_path++ = '/';
144       path++;
145     }
146 #endif
147   /* Expand each slash-separated pathname component. */
148   while (*path != '\0')
149     {
150       /* Ignore stray "/". */
151       if (*path == '/')
152         {
153           path++;
154           continue;
155         }
156
157       if (*path == '.')
158         {
159           /* Ignore ".". */
160           if (path[1] == '\0' || path[1] == '/')
161             {
162               path++;
163               continue;
164             }
165
166           if (path[1] == '.')
167             {
168               if (path[2] == '\0' || path[2] == '/')
169                 {
170                   path += 2;
171
172                   /* Ignore ".." at root. */
173                   if (new_path == resolved_path + 1)
174                     continue;
175
176                   /* Handle ".." by backing up. */
177                   while ((--new_path)[-1] != '/')
178                     ;
179                   continue;
180                 }
181             }
182         }
183
184       /* Safely copy the next pathname component. */
185       while (*path != '\0' && *path != '/')
186         {
187           if (path > max_path)
188             {
189               errno = ENAMETOOLONG;
190               return NULL;
191             }
192           *new_path++ = *path++;
193         }
194
195 #ifdef S_IFLNK
196       /* See if latest pathname component is a symlink. */
197       *new_path = '\0';
198       n = readlink(resolved_path, link_path, PATH_MAX - 1);
199
200       if (n < 0)
201         {
202           /* EINVAL means the file exists but isn't a symlink. */
203           if (errno != EINVAL)
204             return NULL;
205         }
206       else
207         {
208           /* Protect against infinite loops. */
209           if (readlinks++ > MAX_READLINKS)
210             {
211               errno = ELOOP;
212               return NULL;
213             }
214
215           /* Note: readlink doesn't add the null byte. */
216           link_path[n] = '\0';
217
218           if (*link_path == '/')
219             /* Start over for an absolute symlink. */
220             new_path = resolved_path;
221           else
222             /* Otherwise back up over this component. */
223             while (*(--new_path) != '/')
224               ;
225
226           /* Safe sex check. */
227           if (strlen(path) + n >= PATH_MAX)
228             {
229               errno = ENAMETOOLONG;
230               return NULL;
231             }
232
233           /* Insert symlink contents into path. */
234           strcat(link_path, path);
235           strcpy(copy_path, link_path);
236           path = copy_path;
237         }
238 #endif /* S_IFLNK */
239       *new_path++ = '/';
240     }
241
242   /* Delete trailing slash but don't whomp a lone slash. */
243   if (new_path != resolved_path + 1 && new_path[-1] == '/')
244     new_path--;
245
246   /* Make sure it's null terminated. */
247   *new_path = '\0';
248   return resolved_path;
249 }