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