186d7f5d3SJohn Marino /* Libiberty realpath. Like realpath, but more consistent behavior.
286d7f5d3SJohn Marino Based on gdb_realpath from GDB.
386d7f5d3SJohn Marino
486d7f5d3SJohn Marino Copyright 2003 Free Software Foundation, Inc.
586d7f5d3SJohn Marino
686d7f5d3SJohn Marino This file is part of the libiberty library.
786d7f5d3SJohn Marino
886d7f5d3SJohn Marino This program is free software; you can redistribute it and/or modify
986d7f5d3SJohn Marino it under the terms of the GNU General Public License as published by
1086d7f5d3SJohn Marino the Free Software Foundation; either version 2 of the License, or
1186d7f5d3SJohn Marino (at your option) any later version.
1286d7f5d3SJohn Marino
1386d7f5d3SJohn Marino This program is distributed in the hope that it will be useful,
1486d7f5d3SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
1586d7f5d3SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1686d7f5d3SJohn Marino GNU General Public License for more details.
1786d7f5d3SJohn Marino
1886d7f5d3SJohn Marino You should have received a copy of the GNU General Public License
1986d7f5d3SJohn Marino along with this program; if not, write to the Free Software
2086d7f5d3SJohn Marino Foundation, Inc., 51 Franklin Street - Fifth Floor,
2186d7f5d3SJohn Marino Boston, MA 02110-1301, USA. */
2286d7f5d3SJohn Marino
2386d7f5d3SJohn Marino /*
2486d7f5d3SJohn Marino
2586d7f5d3SJohn Marino @deftypefn Replacement {const char*} lrealpath (const char *@var{name})
2686d7f5d3SJohn Marino
2786d7f5d3SJohn Marino Given a pointer to a string containing a pathname, returns a canonical
2886d7f5d3SJohn Marino version of the filename. Symlinks will be resolved, and ``.'' and ``..''
2986d7f5d3SJohn Marino components will be simplified. The returned value will be allocated using
3086d7f5d3SJohn Marino @code{malloc}, or @code{NULL} will be returned on a memory allocation error.
3186d7f5d3SJohn Marino
3286d7f5d3SJohn Marino @end deftypefn
3386d7f5d3SJohn Marino
3486d7f5d3SJohn Marino */
3586d7f5d3SJohn Marino
3686d7f5d3SJohn Marino #include "config.h"
3786d7f5d3SJohn Marino #include "ansidecl.h"
3886d7f5d3SJohn Marino #include "libiberty.h"
3986d7f5d3SJohn Marino
4086d7f5d3SJohn Marino #ifdef HAVE_LIMITS_H
4186d7f5d3SJohn Marino #include <limits.h>
4286d7f5d3SJohn Marino #endif
4386d7f5d3SJohn Marino #ifdef HAVE_STDLIB_H
4486d7f5d3SJohn Marino #include <stdlib.h>
4586d7f5d3SJohn Marino #endif
4686d7f5d3SJohn Marino #ifdef HAVE_UNISTD_H
4786d7f5d3SJohn Marino #include <unistd.h>
4886d7f5d3SJohn Marino #endif
4986d7f5d3SJohn Marino #ifdef HAVE_STRING_H
5086d7f5d3SJohn Marino #include <string.h>
5186d7f5d3SJohn Marino #endif
5286d7f5d3SJohn Marino
5386d7f5d3SJohn Marino /* On GNU libc systems the declaration is only visible with _GNU_SOURCE. */
5486d7f5d3SJohn Marino #if defined(HAVE_CANONICALIZE_FILE_NAME) \
5586d7f5d3SJohn Marino && defined(NEED_DECLARATION_CANONICALIZE_FILE_NAME)
5686d7f5d3SJohn Marino extern char *canonicalize_file_name (const char *);
5786d7f5d3SJohn Marino #endif
5886d7f5d3SJohn Marino
5986d7f5d3SJohn Marino #if defined(HAVE_REALPATH)
6086d7f5d3SJohn Marino # if defined (PATH_MAX)
6186d7f5d3SJohn Marino # define REALPATH_LIMIT PATH_MAX
6286d7f5d3SJohn Marino # else
6386d7f5d3SJohn Marino # if defined (MAXPATHLEN)
6486d7f5d3SJohn Marino # define REALPATH_LIMIT MAXPATHLEN
6586d7f5d3SJohn Marino # endif
6686d7f5d3SJohn Marino # endif
6786d7f5d3SJohn Marino #else
6886d7f5d3SJohn Marino /* cygwin has realpath, so it won't get here. */
6986d7f5d3SJohn Marino # if defined (_WIN32)
7086d7f5d3SJohn Marino # define WIN32_LEAN_AND_MEAN
7186d7f5d3SJohn Marino # include <windows.h> /* for GetFullPathName */
7286d7f5d3SJohn Marino # endif
7386d7f5d3SJohn Marino #endif
7486d7f5d3SJohn Marino
7586d7f5d3SJohn Marino char *
lrealpath(const char * filename)7686d7f5d3SJohn Marino lrealpath (const char *filename)
7786d7f5d3SJohn Marino {
7886d7f5d3SJohn Marino /* Method 1: The system has a compile time upper bound on a filename
7986d7f5d3SJohn Marino path. Use that and realpath() to canonicalize the name. This is
8086d7f5d3SJohn Marino the most common case. Note that, if there isn't a compile time
8186d7f5d3SJohn Marino upper bound, you want to avoid realpath() at all costs. */
8286d7f5d3SJohn Marino #if defined(REALPATH_LIMIT)
8386d7f5d3SJohn Marino {
8486d7f5d3SJohn Marino char buf[REALPATH_LIMIT];
8586d7f5d3SJohn Marino const char *rp = realpath (filename, buf);
8686d7f5d3SJohn Marino if (rp == NULL)
8786d7f5d3SJohn Marino rp = filename;
8886d7f5d3SJohn Marino return strdup (rp);
8986d7f5d3SJohn Marino }
9086d7f5d3SJohn Marino #endif /* REALPATH_LIMIT */
9186d7f5d3SJohn Marino
9286d7f5d3SJohn Marino /* Method 2: The host system (i.e., GNU) has the function
9386d7f5d3SJohn Marino canonicalize_file_name() which malloc's a chunk of memory and
9486d7f5d3SJohn Marino returns that, use that. */
9586d7f5d3SJohn Marino #if defined(HAVE_CANONICALIZE_FILE_NAME)
9686d7f5d3SJohn Marino {
9786d7f5d3SJohn Marino char *rp = canonicalize_file_name (filename);
9886d7f5d3SJohn Marino if (rp == NULL)
9986d7f5d3SJohn Marino return strdup (filename);
10086d7f5d3SJohn Marino else
10186d7f5d3SJohn Marino return rp;
10286d7f5d3SJohn Marino }
10386d7f5d3SJohn Marino #endif
10486d7f5d3SJohn Marino
10586d7f5d3SJohn Marino /* Method 3: Now we're getting desperate! The system doesn't have a
10686d7f5d3SJohn Marino compile time buffer size and no alternative function. Query the
10786d7f5d3SJohn Marino OS, using pathconf(), for the buffer limit. Care is needed
10886d7f5d3SJohn Marino though, some systems do not limit PATH_MAX (return -1 for
10986d7f5d3SJohn Marino pathconf()) making it impossible to pass a correctly sized buffer
11086d7f5d3SJohn Marino to realpath() (it could always overflow). On those systems, we
11186d7f5d3SJohn Marino skip this. */
11286d7f5d3SJohn Marino #if defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H)
11386d7f5d3SJohn Marino {
11486d7f5d3SJohn Marino /* Find out the max path size. */
11586d7f5d3SJohn Marino long path_max = pathconf ("/", _PC_PATH_MAX);
11686d7f5d3SJohn Marino if (path_max > 0)
11786d7f5d3SJohn Marino {
11886d7f5d3SJohn Marino /* PATH_MAX is bounded. */
11986d7f5d3SJohn Marino char *buf, *rp, *ret;
12086d7f5d3SJohn Marino buf = (char *) malloc (path_max);
12186d7f5d3SJohn Marino if (buf == NULL)
12286d7f5d3SJohn Marino return NULL;
12386d7f5d3SJohn Marino rp = realpath (filename, buf);
12486d7f5d3SJohn Marino ret = strdup (rp ? rp : filename);
12586d7f5d3SJohn Marino free (buf);
12686d7f5d3SJohn Marino return ret;
12786d7f5d3SJohn Marino }
12886d7f5d3SJohn Marino }
12986d7f5d3SJohn Marino #endif
13086d7f5d3SJohn Marino
13186d7f5d3SJohn Marino /* The MS Windows method. If we don't have realpath, we assume we
13286d7f5d3SJohn Marino don't have symlinks and just canonicalize to a Windows absolute
13386d7f5d3SJohn Marino path. GetFullPath converts ../ and ./ in relative paths to
13486d7f5d3SJohn Marino absolute paths, filling in current drive if one is not given
13586d7f5d3SJohn Marino or using the current directory of a specified drive (eg, "E:foo").
13686d7f5d3SJohn Marino It also converts all forward slashes to back slashes. */
13786d7f5d3SJohn Marino #if defined (_WIN32)
13886d7f5d3SJohn Marino {
13986d7f5d3SJohn Marino char buf[MAX_PATH];
14086d7f5d3SJohn Marino char* basename;
14186d7f5d3SJohn Marino DWORD len = GetFullPathName (filename, MAX_PATH, buf, &basename);
14286d7f5d3SJohn Marino if (len == 0 || len > MAX_PATH - 1)
14386d7f5d3SJohn Marino return strdup (filename);
14486d7f5d3SJohn Marino else
14586d7f5d3SJohn Marino {
14686d7f5d3SJohn Marino /* The file system is case-preserving but case-insensitive,
14786d7f5d3SJohn Marino Canonicalize to lowercase, using the codepage associated
14886d7f5d3SJohn Marino with the process locale. */
14986d7f5d3SJohn Marino CharLowerBuff (buf, len);
15086d7f5d3SJohn Marino return strdup (buf);
15186d7f5d3SJohn Marino }
15286d7f5d3SJohn Marino }
15386d7f5d3SJohn Marino #endif
15486d7f5d3SJohn Marino
15586d7f5d3SJohn Marino /* This system is a lost cause, just duplicate the filename. */
15686d7f5d3SJohn Marino return strdup (filename);
15786d7f5d3SJohn Marino }
158