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