1 /* Libiberty realpath.  Like realpath, but more consistent behavior.
2    Based on gdb_realpath from GDB.
3 
4    Copyright 2003 Free Software Foundation, Inc.
5 
6    This file is part of the libiberty library.
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street - Fifth Floor,
21    Boston, MA 02110-1301, USA.  */
22 
23 /*
24 
25 @deftypefn Replacement {const char*} lrealpath (const char *@var{name})
26 
27 Given a pointer to a string containing a pathname, returns a canonical
28 version of the filename.  Symlinks will be resolved, and ``.'' and ``..''
29 components will be simplified.  The returned value will be allocated using
30 @code{malloc}, or @code{NULL} will be returned on a memory allocation error.
31 
32 @end deftypefn
33 
34 */
35 
36 #include "config.h"
37 #include "lrealpath.h"
38 
39 #ifdef HAVE_LIMITS_H
40 #include <limits.h>
41 #endif
42 #ifdef HAVE_STDLIB_H
43 #include <stdlib.h>
44 #endif
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 #ifdef HAVE_STRING_H
49 #include <string.h>
50 #endif
51 
52 /* On GNU libc systems the declaration is only visible with _GNU_SOURCE.  */
53 #if defined(HAVE_CANONICALIZE_FILE_NAME) \
54     && defined(NEED_DECLARATION_CANONICALIZE_FILE_NAME)
55 extern char *canonicalize_file_name (const char *);
56 #endif
57 
58 #if defined(HAVE_REALPATH)
59 # if defined (PATH_MAX)
60 #  define REALPATH_LIMIT PATH_MAX
61 # else
62 #  if defined (MAXPATHLEN)
63 #   define REALPATH_LIMIT MAXPATHLEN
64 #  endif
65 # endif
66 #else
67   /* cygwin has realpath, so it won't get here.  */
68 # if defined (_WIN32)
69 #  define WIN32_LEAN_AND_MEAN
70 #  include <windows.h> /* for GetFullPathName */
71 # endif
72 #endif
73 
74 char *
lrealpath(const char * filename)75 lrealpath (const char *filename)
76 {
77   /* Method 1: The system has a compile time upper bound on a filename
78      path.  Use that and realpath() to canonicalize the name.  This is
79      the most common case.  Note that, if there isn't a compile time
80      upper bound, you want to avoid realpath() at all costs.  */
81 #if defined(REALPATH_LIMIT)
82   {
83     char buf[REALPATH_LIMIT];
84     const char *rp = realpath (filename, buf);
85     if (rp == NULL)
86       rp = filename;
87     return strdup (rp);
88   }
89   /* REALPATH_LIMIT */
90 
91   /* Method 2: The host system (i.e., GNU) has the function
92      canonicalize_file_name() which malloc's a chunk of memory and
93      returns that, use that.  */
94 #elif defined(HAVE_CANONICALIZE_FILE_NAME)
95   {
96     char *rp = canonicalize_file_name (filename);
97     if (rp == NULL)
98       return strdup (filename);
99     else
100       return rp;
101   }
102   /* HAVE_CANONICALIZE_FILE_NAME */
103 
104   /* Method 3: Now we're getting desperate!  The system doesn't have a
105      compile time buffer size and no alternative function.  Query the
106      OS, using pathconf(), for the buffer limit.  Care is needed
107      though, some systems do not limit PATH_MAX (return -1 for
108      pathconf()) making it impossible to pass a correctly sized buffer
109      to realpath() (it could always overflow).  On those systems, we
110      skip this.  */
111 #elif defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H)
112   {
113     /* Find out the max path size.  */
114     long path_max = pathconf ("/", _PC_PATH_MAX);
115     if (path_max > 0)
116       {
117 	/* PATH_MAX is bounded.  */
118 	char *buf, *rp, *ret;
119 	buf = (char *) malloc (path_max);
120 	if (buf == NULL)
121 	  return NULL;
122 	rp = realpath (filename, buf);
123 	ret = strdup (rp ? rp : filename);
124 	free (buf);
125 	return ret;
126       }
127     else
128       {
129 	return NULL;
130       }
131   }
132   /* HAVE_REALPATH && HAVE_UNISTD_H */
133 
134   /* The MS Windows method.  If we don't have realpath, we assume we
135      don't have symlinks and just canonicalize to a Windows absolute
136      path.  GetFullPath converts ../ and ./ in relative paths to
137      absolute paths, filling in current drive if one is not given
138      or using the current directory of a specified drive (eg, "E:foo").
139      It also converts all forward slashes to back slashes.  */
140 #elif defined (_WIN32)
141   {
142     char buf[MAX_PATH];
143     char* basename;
144     DWORD len = GetFullPathName (filename, MAX_PATH, buf, &basename);
145     if (len == 0 || len > MAX_PATH - 1)
146       return strdup (filename);
147     else
148       {
149 	/* The file system is case-preserving but case-insensitive,
150 	   Canonicalize to lowercase, using the codepage associated
151 	   with the process locale.  */
152         CharLowerBuff (buf, len);
153         return strdup (buf);
154       }
155   }
156 #else
157 
158   /* This system is a lost cause, just duplicate the filename.  */
159   return strdup (filename);
160 #endif
161 }
162