xref: /netbsd-src/external/gpl3/binutils.old/dist/libiberty/lrealpath.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
116dce513Schristos /* Libiberty realpath.  Like realpath, but more consistent behavior.
216dce513Schristos    Based on gdb_realpath from GDB.
316dce513Schristos 
4*e992f068Schristos    Copyright (C) 2003-2022 Free Software Foundation, Inc.
516dce513Schristos 
616dce513Schristos    This file is part of the libiberty library.
716dce513Schristos 
816dce513Schristos    This program is free software; you can redistribute it and/or modify
916dce513Schristos    it under the terms of the GNU General Public License as published by
1016dce513Schristos    the Free Software Foundation; either version 2 of the License, or
1116dce513Schristos    (at your option) any later version.
1216dce513Schristos 
1316dce513Schristos    This program is distributed in the hope that it will be useful,
1416dce513Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1516dce513Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1616dce513Schristos    GNU General Public License for more details.
1716dce513Schristos 
1816dce513Schristos    You should have received a copy of the GNU General Public License
1916dce513Schristos    along with this program; if not, write to the Free Software
2016dce513Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor,
2116dce513Schristos    Boston, MA 02110-1301, USA.  */
2216dce513Schristos 
2316dce513Schristos /*
2416dce513Schristos 
2516dce513Schristos @deftypefn Replacement {const char*} lrealpath (const char *@var{name})
2616dce513Schristos 
2716dce513Schristos Given a pointer to a string containing a pathname, returns a canonical
2816dce513Schristos version of the filename.  Symlinks will be resolved, and ``.'' and ``..''
2916dce513Schristos components will be simplified.  The returned value will be allocated using
3016dce513Schristos @code{malloc}, or @code{NULL} will be returned on a memory allocation error.
3116dce513Schristos 
3216dce513Schristos @end deftypefn
3316dce513Schristos 
3416dce513Schristos */
3516dce513Schristos 
3616dce513Schristos #include "config.h"
3716dce513Schristos #include "ansidecl.h"
3816dce513Schristos #include "libiberty.h"
3916dce513Schristos 
4016dce513Schristos #ifdef HAVE_LIMITS_H
4116dce513Schristos #include <limits.h>
4216dce513Schristos #endif
4316dce513Schristos #ifdef HAVE_STDLIB_H
4416dce513Schristos #include <stdlib.h>
4516dce513Schristos #endif
4616dce513Schristos #ifdef HAVE_UNISTD_H
4716dce513Schristos #include <unistd.h>
4816dce513Schristos #endif
4916dce513Schristos #ifdef HAVE_STRING_H
5016dce513Schristos #include <string.h>
5116dce513Schristos #endif
5216dce513Schristos 
5316dce513Schristos /* On GNU libc systems the declaration is only visible with _GNU_SOURCE.  */
5416dce513Schristos #if defined(HAVE_CANONICALIZE_FILE_NAME) \
5516dce513Schristos     && defined(NEED_DECLARATION_CANONICALIZE_FILE_NAME)
5616dce513Schristos extern char *canonicalize_file_name (const char *);
5716dce513Schristos #endif
5816dce513Schristos 
5916dce513Schristos #if defined(HAVE_REALPATH)
6016dce513Schristos # if defined (PATH_MAX)
6116dce513Schristos #  define REALPATH_LIMIT PATH_MAX
6216dce513Schristos # else
6316dce513Schristos #  if defined (MAXPATHLEN)
6416dce513Schristos #   define REALPATH_LIMIT MAXPATHLEN
6516dce513Schristos #  endif
6616dce513Schristos # endif
6716dce513Schristos #else
6816dce513Schristos   /* cygwin has realpath, so it won't get here.  */
6916dce513Schristos # if defined (_WIN32)
7016dce513Schristos #  define WIN32_LEAN_AND_MEAN
7116dce513Schristos #  include <windows.h> /* for GetFullPathName */
7216dce513Schristos # endif
7316dce513Schristos #endif
7416dce513Schristos 
7516dce513Schristos char *
lrealpath(const char * filename)7616dce513Schristos lrealpath (const char *filename)
7716dce513Schristos {
7816dce513Schristos   /* Method 1: The system has a compile time upper bound on a filename
7916dce513Schristos      path.  Use that and realpath() to canonicalize the name.  This is
8016dce513Schristos      the most common case.  Note that, if there isn't a compile time
8116dce513Schristos      upper bound, you want to avoid realpath() at all costs.  */
8216dce513Schristos #if defined(REALPATH_LIMIT)
8316dce513Schristos   {
8416dce513Schristos     char buf[REALPATH_LIMIT];
8516dce513Schristos     const char *rp = realpath (filename, buf);
8616dce513Schristos     if (rp == NULL)
8716dce513Schristos       rp = filename;
8816dce513Schristos     return strdup (rp);
8916dce513Schristos   }
9016dce513Schristos #endif /* REALPATH_LIMIT */
9116dce513Schristos 
9216dce513Schristos   /* Method 2: The host system (i.e., GNU) has the function
9316dce513Schristos      canonicalize_file_name() which malloc's a chunk of memory and
9416dce513Schristos      returns that, use that.  */
9516dce513Schristos #if defined(HAVE_CANONICALIZE_FILE_NAME)
9616dce513Schristos   {
9716dce513Schristos     char *rp = canonicalize_file_name (filename);
9816dce513Schristos     if (rp == NULL)
9916dce513Schristos       return strdup (filename);
10016dce513Schristos     else
10116dce513Schristos       return rp;
10216dce513Schristos   }
10316dce513Schristos #endif
10416dce513Schristos 
10516dce513Schristos   /* Method 3: Now we're getting desperate!  The system doesn't have a
10616dce513Schristos      compile time buffer size and no alternative function.  Query the
10716dce513Schristos      OS, using pathconf(), for the buffer limit.  Care is needed
10816dce513Schristos      though, some systems do not limit PATH_MAX (return -1 for
10916dce513Schristos      pathconf()) making it impossible to pass a correctly sized buffer
11016dce513Schristos      to realpath() (it could always overflow).  On those systems, we
11116dce513Schristos      skip this.  */
11216dce513Schristos #if defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H)
11316dce513Schristos   {
11416dce513Schristos     /* Find out the max path size.  */
11516dce513Schristos     long path_max = pathconf ("/", _PC_PATH_MAX);
11616dce513Schristos     if (path_max > 0)
11716dce513Schristos       {
11816dce513Schristos 	/* PATH_MAX is bounded.  */
11916dce513Schristos 	char *buf, *rp, *ret;
12016dce513Schristos 	buf = (char *) malloc (path_max);
12116dce513Schristos 	if (buf == NULL)
12216dce513Schristos 	  return NULL;
12316dce513Schristos 	rp = realpath (filename, buf);
12416dce513Schristos 	ret = strdup (rp ? rp : filename);
12516dce513Schristos 	free (buf);
12616dce513Schristos 	return ret;
12716dce513Schristos       }
12816dce513Schristos   }
12916dce513Schristos #endif
13016dce513Schristos 
13116dce513Schristos   /* The MS Windows method.  If we don't have realpath, we assume we
13216dce513Schristos      don't have symlinks and just canonicalize to a Windows absolute
13316dce513Schristos      path.  GetFullPath converts ../ and ./ in relative paths to
13416dce513Schristos      absolute paths, filling in current drive if one is not given
13516dce513Schristos      or using the current directory of a specified drive (eg, "E:foo").
13616dce513Schristos      It also converts all forward slashes to back slashes.  */
13716dce513Schristos #if defined (_WIN32)
13816dce513Schristos   {
13916dce513Schristos     char buf[MAX_PATH];
14016dce513Schristos     char* basename;
14116dce513Schristos     DWORD len = GetFullPathName (filename, MAX_PATH, buf, &basename);
14216dce513Schristos     if (len == 0 || len > MAX_PATH - 1)
14316dce513Schristos       return strdup (filename);
14416dce513Schristos     else
14516dce513Schristos       {
14616dce513Schristos 	/* The file system is case-preserving but case-insensitive,
14716dce513Schristos 	   Canonicalize to lowercase, using the codepage associated
14816dce513Schristos 	   with the process locale.  */
14916dce513Schristos         CharLowerBuff (buf, len);
15016dce513Schristos         return strdup (buf);
15116dce513Schristos       }
15216dce513Schristos   }
15316dce513Schristos #endif
15416dce513Schristos 
15516dce513Schristos   /* This system is a lost cause, just duplicate the filename.  */
15616dce513Schristos   return strdup (filename);
15716dce513Schristos }
158