Back to index

cell-binutils  2.17cvs20070401
lrealpath.c
Go to the documentation of this file.
00001 /* Libiberty realpath.  Like realpath, but more consistent behavior.
00002    Based on gdb_realpath from GDB.
00003 
00004    Copyright 2003 Free Software Foundation, Inc.
00005 
00006    This file is part of the libiberty library.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor,
00021    Boston, MA 02110-1301, USA.  */
00022 
00023 /*
00024 
00025 @deftypefn Replacement {const char*} lrealpath (const char *@var{name})
00026 
00027 Given a pointer to a string containing a pathname, returns a canonical
00028 version of the filename.  Symlinks will be resolved, and ``.'' and ``..''
00029 components will be simplified.  The returned value will be allocated using
00030 @code{malloc}, or @code{NULL} will be returned on a memory allocation error.
00031 
00032 @end deftypefn
00033 
00034 */
00035 
00036 #include "config.h"
00037 #include "ansidecl.h"
00038 #include "libiberty.h"
00039 
00040 #ifdef HAVE_LIMITS_H
00041 #include <limits.h>
00042 #endif
00043 #ifdef HAVE_STDLIB_H
00044 #include <stdlib.h>
00045 #endif
00046 #ifdef HAVE_UNISTD_H
00047 #include <unistd.h>
00048 #endif
00049 #ifdef HAVE_STRING_H
00050 #include <string.h>
00051 #endif
00052 
00053 /* On GNU libc systems the declaration is only visible with _GNU_SOURCE.  */
00054 #if defined(HAVE_CANONICALIZE_FILE_NAME) \
00055     && defined(NEED_DECLARATION_CANONICALIZE_FILE_NAME)
00056 extern char *canonicalize_file_name (const char *);
00057 #endif
00058 
00059 #if defined(HAVE_REALPATH)
00060 # if defined (PATH_MAX)
00061 #  define REALPATH_LIMIT PATH_MAX
00062 # else
00063 #  if defined (MAXPATHLEN)
00064 #   define REALPATH_LIMIT MAXPATHLEN
00065 #  endif
00066 # endif
00067 #else
00068   /* cygwin has realpath, so it won't get here.  */ 
00069 # if defined (_WIN32)
00070 #  define WIN32_LEAN_AND_MEAN
00071 #  include <windows.h> /* for GetFullPathName */
00072 # endif
00073 #endif
00074 
00075 char *
00076 lrealpath (const char *filename)
00077 {
00078   /* Method 1: The system has a compile time upper bound on a filename
00079      path.  Use that and realpath() to canonicalize the name.  This is
00080      the most common case.  Note that, if there isn't a compile time
00081      upper bound, you want to avoid realpath() at all costs.  */
00082 #if defined(REALPATH_LIMIT)
00083   {
00084     char buf[REALPATH_LIMIT];
00085     const char *rp = realpath (filename, buf);
00086     if (rp == NULL)
00087       rp = filename;
00088     return strdup (rp);
00089   }
00090 #endif /* REALPATH_LIMIT */
00091 
00092   /* Method 2: The host system (i.e., GNU) has the function
00093      canonicalize_file_name() which malloc's a chunk of memory and
00094      returns that, use that.  */
00095 #if defined(HAVE_CANONICALIZE_FILE_NAME)
00096   {
00097     char *rp = canonicalize_file_name (filename);
00098     if (rp == NULL)
00099       return strdup (filename);
00100     else
00101       return rp;
00102   }
00103 #endif
00104 
00105   /* Method 3: Now we're getting desperate!  The system doesn't have a
00106      compile time buffer size and no alternative function.  Query the
00107      OS, using pathconf(), for the buffer limit.  Care is needed
00108      though, some systems do not limit PATH_MAX (return -1 for
00109      pathconf()) making it impossible to pass a correctly sized buffer
00110      to realpath() (it could always overflow).  On those systems, we
00111      skip this.  */
00112 #if defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H)
00113   {
00114     /* Find out the max path size.  */
00115     long path_max = pathconf ("/", _PC_PATH_MAX);
00116     if (path_max > 0)
00117       {
00118        /* PATH_MAX is bounded.  */
00119        char *buf, *rp, *ret;
00120        buf = (char *) malloc (path_max);
00121        if (buf == NULL)
00122          return NULL;
00123        rp = realpath (filename, buf);
00124        ret = strdup (rp ? rp : filename);
00125        free (buf);
00126        return ret;
00127       }
00128   }
00129 #endif
00130 
00131   /* The MS Windows method.  If we don't have realpath, we assume we
00132      don't have symlinks and just canonicalize to a Windows absolute
00133      path.  GetFullPath converts ../ and ./ in relative paths to
00134      absolute paths, filling in current drive if one is not given
00135      or using the current directory of a specified drive (eg, "E:foo").
00136      It also converts all forward slashes to back slashes.  */
00137 #if defined (_WIN32)
00138   {
00139     char buf[MAX_PATH];
00140     char* basename;
00141     DWORD len = GetFullPathName (filename, MAX_PATH, buf, &basename);
00142     if (len == 0 || len > MAX_PATH - 1)
00143       return strdup (filename);
00144     else
00145       {
00146        /* The file system is case-preserving but case-insensitive,
00147           Canonicalize to lowercase, using the codepage associated
00148           with the process locale.  */
00149         CharLowerBuff (buf, len);
00150         return strdup (buf);
00151       }
00152   }
00153 #endif
00154 
00155   /* This system is a lost cause, just duplicate the filename.  */
00156   return strdup (filename);
00157 }