xref: /netbsd-src/external/gpl3/gcc/dist/libiberty/lrealpath.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
14fee23f9Smrg /* Libiberty realpath.  Like realpath, but more consistent behavior.
24fee23f9Smrg    Based on gdb_realpath from GDB.
34fee23f9Smrg 
4*b1e83836Smrg    Copyright (C) 2003-2022 Free Software Foundation, Inc.
54fee23f9Smrg 
64fee23f9Smrg    This file is part of the libiberty library.
74fee23f9Smrg 
84fee23f9Smrg    This program is free software; you can redistribute it and/or modify
94fee23f9Smrg    it under the terms of the GNU General Public License as published by
104fee23f9Smrg    the Free Software Foundation; either version 2 of the License, or
114fee23f9Smrg    (at your option) any later version.
124fee23f9Smrg 
134fee23f9Smrg    This program is distributed in the hope that it will be useful,
144fee23f9Smrg    but WITHOUT ANY WARRANTY; without even the implied warranty of
154fee23f9Smrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
164fee23f9Smrg    GNU General Public License for more details.
174fee23f9Smrg 
184fee23f9Smrg    You should have received a copy of the GNU General Public License
194fee23f9Smrg    along with this program; if not, write to the Free Software
204fee23f9Smrg    Foundation, Inc., 51 Franklin Street - Fifth Floor,
214fee23f9Smrg    Boston, MA 02110-1301, USA.  */
224fee23f9Smrg 
234fee23f9Smrg /*
244fee23f9Smrg 
254fee23f9Smrg @deftypefn Replacement {const char*} lrealpath (const char *@var{name})
264fee23f9Smrg 
274fee23f9Smrg Given a pointer to a string containing a pathname, returns a canonical
284fee23f9Smrg version of the filename.  Symlinks will be resolved, and ``.'' and ``..''
294fee23f9Smrg components will be simplified.  The returned value will be allocated using
304fee23f9Smrg @code{malloc}, or @code{NULL} will be returned on a memory allocation error.
314fee23f9Smrg 
324fee23f9Smrg @end deftypefn
334fee23f9Smrg 
344fee23f9Smrg */
354fee23f9Smrg 
364fee23f9Smrg #include "config.h"
374fee23f9Smrg #include "ansidecl.h"
384fee23f9Smrg #include "libiberty.h"
394fee23f9Smrg 
404fee23f9Smrg #ifdef HAVE_LIMITS_H
414fee23f9Smrg #include <limits.h>
424fee23f9Smrg #endif
434fee23f9Smrg #ifdef HAVE_STDLIB_H
444fee23f9Smrg #include <stdlib.h>
454fee23f9Smrg #endif
464fee23f9Smrg #ifdef HAVE_UNISTD_H
474fee23f9Smrg #include <unistd.h>
484fee23f9Smrg #endif
494fee23f9Smrg #ifdef HAVE_STRING_H
504fee23f9Smrg #include <string.h>
514fee23f9Smrg #endif
524fee23f9Smrg 
534fee23f9Smrg /* On GNU libc systems the declaration is only visible with _GNU_SOURCE.  */
544fee23f9Smrg #if defined(HAVE_CANONICALIZE_FILE_NAME) \
554fee23f9Smrg     && defined(NEED_DECLARATION_CANONICALIZE_FILE_NAME)
564fee23f9Smrg extern char *canonicalize_file_name (const char *);
574fee23f9Smrg #endif
584fee23f9Smrg 
594fee23f9Smrg #if defined(HAVE_REALPATH)
604fee23f9Smrg # if defined (PATH_MAX)
614fee23f9Smrg #  define REALPATH_LIMIT PATH_MAX
624fee23f9Smrg # else
634fee23f9Smrg #  if defined (MAXPATHLEN)
644fee23f9Smrg #   define REALPATH_LIMIT MAXPATHLEN
654fee23f9Smrg #  endif
664fee23f9Smrg # endif
674fee23f9Smrg #else
684fee23f9Smrg   /* cygwin has realpath, so it won't get here.  */
694fee23f9Smrg # if defined (_WIN32)
704fee23f9Smrg #  define WIN32_LEAN_AND_MEAN
714fee23f9Smrg #  include <windows.h> /* for GetFullPathName */
724fee23f9Smrg # endif
734fee23f9Smrg #endif
744fee23f9Smrg 
754fee23f9Smrg char *
lrealpath(const char * filename)764fee23f9Smrg lrealpath (const char *filename)
774fee23f9Smrg {
784fee23f9Smrg   /* Method 1: The system has a compile time upper bound on a filename
794fee23f9Smrg      path.  Use that and realpath() to canonicalize the name.  This is
804fee23f9Smrg      the most common case.  Note that, if there isn't a compile time
814fee23f9Smrg      upper bound, you want to avoid realpath() at all costs.  */
824fee23f9Smrg #if defined(REALPATH_LIMIT)
834fee23f9Smrg   {
844fee23f9Smrg     char buf[REALPATH_LIMIT];
854fee23f9Smrg     const char *rp = realpath (filename, buf);
864fee23f9Smrg     if (rp == NULL)
874fee23f9Smrg       rp = filename;
884fee23f9Smrg     return strdup (rp);
894fee23f9Smrg   }
904fee23f9Smrg #endif /* REALPATH_LIMIT */
914fee23f9Smrg 
924fee23f9Smrg   /* Method 2: The host system (i.e., GNU) has the function
934fee23f9Smrg      canonicalize_file_name() which malloc's a chunk of memory and
944fee23f9Smrg      returns that, use that.  */
954fee23f9Smrg #if defined(HAVE_CANONICALIZE_FILE_NAME)
964fee23f9Smrg   {
974fee23f9Smrg     char *rp = canonicalize_file_name (filename);
984fee23f9Smrg     if (rp == NULL)
994fee23f9Smrg       return strdup (filename);
1004fee23f9Smrg     else
1014fee23f9Smrg       return rp;
1024fee23f9Smrg   }
1034fee23f9Smrg #endif
1044fee23f9Smrg 
1054fee23f9Smrg   /* Method 3: Now we're getting desperate!  The system doesn't have a
1064fee23f9Smrg      compile time buffer size and no alternative function.  Query the
1074fee23f9Smrg      OS, using pathconf(), for the buffer limit.  Care is needed
1084fee23f9Smrg      though, some systems do not limit PATH_MAX (return -1 for
1094fee23f9Smrg      pathconf()) making it impossible to pass a correctly sized buffer
1104fee23f9Smrg      to realpath() (it could always overflow).  On those systems, we
1114fee23f9Smrg      skip this.  */
1124fee23f9Smrg #if defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H)
1134fee23f9Smrg   {
1144fee23f9Smrg     /* Find out the max path size.  */
1154fee23f9Smrg     long path_max = pathconf ("/", _PC_PATH_MAX);
1164fee23f9Smrg     if (path_max > 0)
1174fee23f9Smrg       {
1184fee23f9Smrg 	/* PATH_MAX is bounded.  */
1194fee23f9Smrg 	char *buf, *rp, *ret;
1204fee23f9Smrg 	buf = (char *) malloc (path_max);
1214fee23f9Smrg 	if (buf == NULL)
1224fee23f9Smrg 	  return NULL;
1234fee23f9Smrg 	rp = realpath (filename, buf);
1244fee23f9Smrg 	ret = strdup (rp ? rp : filename);
1254fee23f9Smrg 	free (buf);
1264fee23f9Smrg 	return ret;
1274fee23f9Smrg       }
1284fee23f9Smrg   }
1294fee23f9Smrg #endif
1304fee23f9Smrg 
1314fee23f9Smrg   /* The MS Windows method.  If we don't have realpath, we assume we
1324fee23f9Smrg      don't have symlinks and just canonicalize to a Windows absolute
1334fee23f9Smrg      path.  GetFullPath converts ../ and ./ in relative paths to
1344fee23f9Smrg      absolute paths, filling in current drive if one is not given
1354fee23f9Smrg      or using the current directory of a specified drive (eg, "E:foo").
1364fee23f9Smrg      It also converts all forward slashes to back slashes.  */
1374fee23f9Smrg #if defined (_WIN32)
1384fee23f9Smrg   {
1394fee23f9Smrg     char buf[MAX_PATH];
1404fee23f9Smrg     char* basename;
1414fee23f9Smrg     DWORD len = GetFullPathName (filename, MAX_PATH, buf, &basename);
1424fee23f9Smrg     if (len == 0 || len > MAX_PATH - 1)
1434fee23f9Smrg       return strdup (filename);
1444fee23f9Smrg     else
1454fee23f9Smrg       {
1464fee23f9Smrg 	/* The file system is case-preserving but case-insensitive,
1474fee23f9Smrg 	   Canonicalize to lowercase, using the codepage associated
1484fee23f9Smrg 	   with the process locale.  */
1494fee23f9Smrg         CharLowerBuff (buf, len);
1504fee23f9Smrg         return strdup (buf);
1514fee23f9Smrg       }
1524fee23f9Smrg   }
1534fee23f9Smrg #endif
1544fee23f9Smrg 
1554fee23f9Smrg   /* This system is a lost cause, just duplicate the filename.  */
1564fee23f9Smrg   return strdup (filename);
1574fee23f9Smrg }
158