171671b9obrien/* Libiberty realpath.  Like realpath, but more consistent behavior.
271671b9obrien   Based on gdb_realpath from GDB.
371671b9obrien
471671b9obrien   Copyright 2003 Free Software Foundation, Inc.
571671b9obrien
671671b9obrien   This file is part of the libiberty library.
771671b9obrien
871671b9obrien   This program is free software; you can redistribute it and/or modify
971671b9obrien   it under the terms of the GNU General Public License as published by
1071671b9obrien   the Free Software Foundation; either version 2 of the License, or
1171671b9obrien   (at your option) any later version.
1271671b9obrien
1371671b9obrien   This program is distributed in the hope that it will be useful,
1471671b9obrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1571671b9obrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1671671b9obrien   GNU General Public License for more details.
1771671b9obrien
1871671b9obrien   You should have received a copy of the GNU General Public License
1971671b9obrien   along with this program; if not, write to the Free Software
200acbbeedim   Foundation, Inc., 51 Franklin Street - Fifth Floor,
210acbbeedim   Boston, MA 02110-1301, USA.  */
2271671b9obrien
2371671b9obrien/*
2471671b9obrien
2571671b9obrien@deftypefn Replacement {const char*} lrealpath (const char *@var{name})
2671671b9obrien
2771671b9obrienGiven a pointer to a string containing a pathname, returns a canonical
2871671b9obrienversion of the filename.  Symlinks will be resolved, and ``.'' and ``..''
2971671b9obriencomponents will be simplified.  The returned value will be allocated using
3071671b9obrien@code{malloc}, or @code{NULL} will be returned on a memory allocation error.
3171671b9obrien
3271671b9obrien@end deftypefn
3371671b9obrien
3471671b9obrien*/
3571671b9obrien
3671671b9obrien#include "config.h"
3771671b9obrien#include "ansidecl.h"
3871671b9obrien#include "libiberty.h"
3971671b9obrien
4071671b9obrien#ifdef HAVE_LIMITS_H
4171671b9obrien#include <limits.h>
4271671b9obrien#endif
4371671b9obrien#ifdef HAVE_STDLIB_H
4471671b9obrien#include <stdlib.h>
4571671b9obrien#endif
4671671b9obrien#ifdef HAVE_UNISTD_H
4771671b9obrien#include <unistd.h>
4871671b9obrien#endif
4971671b9obrien#ifdef HAVE_STRING_H
5071671b9obrien#include <string.h>
5171671b9obrien#endif
5271671b9obrien
5371671b9obrien/* On GNU libc systems the declaration is only visible with _GNU_SOURCE.  */
5471671b9obrien#if defined(HAVE_CANONICALIZE_FILE_NAME) \
5571671b9obrien    && defined(NEED_DECLARATION_CANONICALIZE_FILE_NAME)
5671671b9obrienextern char *canonicalize_file_name (const char *);
5771671b9obrien#endif
5871671b9obrien
5971671b9obrien#if defined(HAVE_REALPATH)
6071671b9obrien# if defined (PATH_MAX)
6171671b9obrien#  define REALPATH_LIMIT PATH_MAX
6271671b9obrien# else
6371671b9obrien#  if defined (MAXPATHLEN)
6471671b9obrien#   define REALPATH_LIMIT MAXPATHLEN
6571671b9obrien#  endif
6671671b9obrien# endif
670acbbeedim#else
680acbbeedim  /* cygwin has realpath, so it won't get here.  */
690acbbeedim# if defined (_WIN32)
700acbbeedim#  define WIN32_LEAN_AND_MEAN
710acbbeedim#  include <windows.h> /* for GetFullPathName */
720acbbeedim# endif
7371671b9obrien#endif
7471671b9obrien
7571671b9obrienchar *
760acbbeedimlrealpath (const char *filename)
7771671b9obrien{
7871671b9obrien  /* Method 1: The system has a compile time upper bound on a filename
7971671b9obrien     path.  Use that and realpath() to canonicalize the name.  This is
8071671b9obrien     the most common case.  Note that, if there isn't a compile time
8171671b9obrien     upper bound, you want to avoid realpath() at all costs.  */
8271671b9obrien#if defined(REALPATH_LIMIT)
8371671b9obrien  {
8471671b9obrien    char buf[REALPATH_LIMIT];
8571671b9obrien    const char *rp = realpath (filename, buf);
8671671b9obrien    if (rp == NULL)
8771671b9obrien      rp = filename;
8871671b9obrien    return strdup (rp);
8971671b9obrien  }
9071671b9obrien#endif /* REALPATH_LIMIT */
9171671b9obrien
9271671b9obrien  /* Method 2: The host system (i.e., GNU) has the function
9371671b9obrien     canonicalize_file_name() which malloc's a chunk of memory and
9471671b9obrien     returns that, use that.  */
9571671b9obrien#if defined(HAVE_CANONICALIZE_FILE_NAME)
9671671b9obrien  {
9771671b9obrien    char *rp = canonicalize_file_name (filename);
9871671b9obrien    if (rp == NULL)
9971671b9obrien      return strdup (filename);
10071671b9obrien    else
10171671b9obrien      return rp;
10271671b9obrien  }
10371671b9obrien#endif
10471671b9obrien
10571671b9obrien  /* Method 3: Now we're getting desperate!  The system doesn't have a
10671671b9obrien     compile time buffer size and no alternative function.  Query the
10771671b9obrien     OS, using pathconf(), for the buffer limit.  Care is needed
10871671b9obrien     though, some systems do not limit PATH_MAX (return -1 for
10971671b9obrien     pathconf()) making it impossible to pass a correctly sized buffer
11071671b9obrien     to realpath() (it could always overflow).  On those systems, we
11171671b9obrien     skip this.  */
11271671b9obrien#if defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H)
11371671b9obrien  {
11471671b9obrien    /* Find out the max path size.  */
11571671b9obrien    long path_max = pathconf ("/", _PC_PATH_MAX);
11671671b9obrien    if (path_max > 0)
11771671b9obrien      {
11871671b9obrien	/* PATH_MAX is bounded.  */
11971671b9obrien	char *buf, *rp, *ret;
1200acbbeedim	buf = (char *) malloc (path_max);
12171671b9obrien	if (buf == NULL)
12271671b9obrien	  return NULL;
12371671b9obrien	rp = realpath (filename, buf);
12471671b9obrien	ret = strdup (rp ? rp : filename);
12571671b9obrien	free (buf);
12671671b9obrien	return ret;
12771671b9obrien      }
12871671b9obrien  }
12971671b9obrien#endif
13071671b9obrien
1310acbbeedim  /* The MS Windows method.  If we don't have realpath, we assume we
1320acbbeedim     don't have symlinks and just canonicalize to a Windows absolute
1330acbbeedim     path.  GetFullPath converts ../ and ./ in relative paths to
1340acbbeedim     absolute paths, filling in current drive if one is not given
1350acbbeedim     or using the current directory of a specified drive (eg, "E:foo").
1360acbbeedim     It also converts all forward slashes to back slashes.  */
1370acbbeedim#if defined (_WIN32)
1380acbbeedim  {
1390acbbeedim    char buf[MAX_PATH];
1400acbbeedim    char* basename;
1410acbbeedim    DWORD len = GetFullPathName (filename, MAX_PATH, buf, &basename);
1420acbbeedim    if (len == 0 || len > MAX_PATH - 1)
1430acbbeedim      return strdup (filename);
1440acbbeedim    else
1450acbbeedim      {
1460acbbeedim	/* The file system is case-preserving but case-insensitive,
1470acbbeedim	   Canonicalize to lowercase, using the codepage associated
1480acbbeedim	   with the process locale.  */
1490acbbeedim        CharLowerBuff (buf, len);
1500acbbeedim        return strdup (buf);
1510acbbeedim      }
1520acbbeedim  }
1530acbbeedim#endif
1540acbbeedim
15571671b9obrien  /* This system is a lost cause, just duplicate the filename.  */
15671671b9obrien  return strdup (filename);
15771671b9obrien}
158