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