xref: /dflybsd-src/contrib/gcc-4.7/libiberty/lrealpath.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* Libiberty realpath.  Like realpath, but more consistent behavior.
2*e4b17023SJohn Marino    Based on gdb_realpath from GDB.
3*e4b17023SJohn Marino 
4*e4b17023SJohn Marino    Copyright 2003 Free Software Foundation, Inc.
5*e4b17023SJohn Marino 
6*e4b17023SJohn Marino    This file is part of the libiberty library.
7*e4b17023SJohn Marino 
8*e4b17023SJohn Marino    This program is free software; you can redistribute it and/or modify
9*e4b17023SJohn Marino    it under the terms of the GNU General Public License as published by
10*e4b17023SJohn Marino    the Free Software Foundation; either version 2 of the License, or
11*e4b17023SJohn Marino    (at your option) any later version.
12*e4b17023SJohn Marino 
13*e4b17023SJohn Marino    This program is distributed in the hope that it will be useful,
14*e4b17023SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*e4b17023SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*e4b17023SJohn Marino    GNU General Public License for more details.
17*e4b17023SJohn Marino 
18*e4b17023SJohn Marino    You should have received a copy of the GNU General Public License
19*e4b17023SJohn Marino    along with this program; if not, write to the Free Software
20*e4b17023SJohn Marino    Foundation, Inc., 51 Franklin Street - Fifth Floor,
21*e4b17023SJohn Marino    Boston, MA 02110-1301, USA.  */
22*e4b17023SJohn Marino 
23*e4b17023SJohn Marino /*
24*e4b17023SJohn Marino 
25*e4b17023SJohn Marino @deftypefn Replacement {const char*} lrealpath (const char *@var{name})
26*e4b17023SJohn Marino 
27*e4b17023SJohn Marino Given a pointer to a string containing a pathname, returns a canonical
28*e4b17023SJohn Marino version of the filename.  Symlinks will be resolved, and ``.'' and ``..''
29*e4b17023SJohn Marino components will be simplified.  The returned value will be allocated using
30*e4b17023SJohn Marino @code{malloc}, or @code{NULL} will be returned on a memory allocation error.
31*e4b17023SJohn Marino 
32*e4b17023SJohn Marino @end deftypefn
33*e4b17023SJohn Marino 
34*e4b17023SJohn Marino */
35*e4b17023SJohn Marino 
36*e4b17023SJohn Marino #include "config.h"
37*e4b17023SJohn Marino #include "ansidecl.h"
38*e4b17023SJohn Marino #include "libiberty.h"
39*e4b17023SJohn Marino 
40*e4b17023SJohn Marino #ifdef HAVE_LIMITS_H
41*e4b17023SJohn Marino #include <limits.h>
42*e4b17023SJohn Marino #endif
43*e4b17023SJohn Marino #ifdef HAVE_STDLIB_H
44*e4b17023SJohn Marino #include <stdlib.h>
45*e4b17023SJohn Marino #endif
46*e4b17023SJohn Marino #ifdef HAVE_UNISTD_H
47*e4b17023SJohn Marino #include <unistd.h>
48*e4b17023SJohn Marino #endif
49*e4b17023SJohn Marino #ifdef HAVE_STRING_H
50*e4b17023SJohn Marino #include <string.h>
51*e4b17023SJohn Marino #endif
52*e4b17023SJohn Marino 
53*e4b17023SJohn Marino /* On GNU libc systems the declaration is only visible with _GNU_SOURCE.  */
54*e4b17023SJohn Marino #if defined(HAVE_CANONICALIZE_FILE_NAME) \
55*e4b17023SJohn Marino     && defined(NEED_DECLARATION_CANONICALIZE_FILE_NAME)
56*e4b17023SJohn Marino extern char *canonicalize_file_name (const char *);
57*e4b17023SJohn Marino #endif
58*e4b17023SJohn Marino 
59*e4b17023SJohn Marino #if defined(HAVE_REALPATH)
60*e4b17023SJohn Marino # if defined (PATH_MAX)
61*e4b17023SJohn Marino #  define REALPATH_LIMIT PATH_MAX
62*e4b17023SJohn Marino # else
63*e4b17023SJohn Marino #  if defined (MAXPATHLEN)
64*e4b17023SJohn Marino #   define REALPATH_LIMIT MAXPATHLEN
65*e4b17023SJohn Marino #  endif
66*e4b17023SJohn Marino # endif
67*e4b17023SJohn Marino #else
68*e4b17023SJohn Marino   /* cygwin has realpath, so it won't get here.  */
69*e4b17023SJohn Marino # if defined (_WIN32)
70*e4b17023SJohn Marino #  define WIN32_LEAN_AND_MEAN
71*e4b17023SJohn Marino #  include <windows.h> /* for GetFullPathName */
72*e4b17023SJohn Marino # endif
73*e4b17023SJohn Marino #endif
74*e4b17023SJohn Marino 
75*e4b17023SJohn Marino char *
lrealpath(const char * filename)76*e4b17023SJohn Marino lrealpath (const char *filename)
77*e4b17023SJohn Marino {
78*e4b17023SJohn Marino   /* Method 1: The system has a compile time upper bound on a filename
79*e4b17023SJohn Marino      path.  Use that and realpath() to canonicalize the name.  This is
80*e4b17023SJohn Marino      the most common case.  Note that, if there isn't a compile time
81*e4b17023SJohn Marino      upper bound, you want to avoid realpath() at all costs.  */
82*e4b17023SJohn Marino #if defined(REALPATH_LIMIT)
83*e4b17023SJohn Marino   {
84*e4b17023SJohn Marino     char buf[REALPATH_LIMIT];
85*e4b17023SJohn Marino     const char *rp = realpath (filename, buf);
86*e4b17023SJohn Marino     if (rp == NULL)
87*e4b17023SJohn Marino       rp = filename;
88*e4b17023SJohn Marino     return strdup (rp);
89*e4b17023SJohn Marino   }
90*e4b17023SJohn Marino #endif /* REALPATH_LIMIT */
91*e4b17023SJohn Marino 
92*e4b17023SJohn Marino   /* Method 2: The host system (i.e., GNU) has the function
93*e4b17023SJohn Marino      canonicalize_file_name() which malloc's a chunk of memory and
94*e4b17023SJohn Marino      returns that, use that.  */
95*e4b17023SJohn Marino #if defined(HAVE_CANONICALIZE_FILE_NAME)
96*e4b17023SJohn Marino   {
97*e4b17023SJohn Marino     char *rp = canonicalize_file_name (filename);
98*e4b17023SJohn Marino     if (rp == NULL)
99*e4b17023SJohn Marino       return strdup (filename);
100*e4b17023SJohn Marino     else
101*e4b17023SJohn Marino       return rp;
102*e4b17023SJohn Marino   }
103*e4b17023SJohn Marino #endif
104*e4b17023SJohn Marino 
105*e4b17023SJohn Marino   /* Method 3: Now we're getting desperate!  The system doesn't have a
106*e4b17023SJohn Marino      compile time buffer size and no alternative function.  Query the
107*e4b17023SJohn Marino      OS, using pathconf(), for the buffer limit.  Care is needed
108*e4b17023SJohn Marino      though, some systems do not limit PATH_MAX (return -1 for
109*e4b17023SJohn Marino      pathconf()) making it impossible to pass a correctly sized buffer
110*e4b17023SJohn Marino      to realpath() (it could always overflow).  On those systems, we
111*e4b17023SJohn Marino      skip this.  */
112*e4b17023SJohn Marino #if defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H)
113*e4b17023SJohn Marino   {
114*e4b17023SJohn Marino     /* Find out the max path size.  */
115*e4b17023SJohn Marino     long path_max = pathconf ("/", _PC_PATH_MAX);
116*e4b17023SJohn Marino     if (path_max > 0)
117*e4b17023SJohn Marino       {
118*e4b17023SJohn Marino 	/* PATH_MAX is bounded.  */
119*e4b17023SJohn Marino 	char *buf, *rp, *ret;
120*e4b17023SJohn Marino 	buf = (char *) malloc (path_max);
121*e4b17023SJohn Marino 	if (buf == NULL)
122*e4b17023SJohn Marino 	  return NULL;
123*e4b17023SJohn Marino 	rp = realpath (filename, buf);
124*e4b17023SJohn Marino 	ret = strdup (rp ? rp : filename);
125*e4b17023SJohn Marino 	free (buf);
126*e4b17023SJohn Marino 	return ret;
127*e4b17023SJohn Marino       }
128*e4b17023SJohn Marino   }
129*e4b17023SJohn Marino #endif
130*e4b17023SJohn Marino 
131*e4b17023SJohn Marino   /* The MS Windows method.  If we don't have realpath, we assume we
132*e4b17023SJohn Marino      don't have symlinks and just canonicalize to a Windows absolute
133*e4b17023SJohn Marino      path.  GetFullPath converts ../ and ./ in relative paths to
134*e4b17023SJohn Marino      absolute paths, filling in current drive if one is not given
135*e4b17023SJohn Marino      or using the current directory of a specified drive (eg, "E:foo").
136*e4b17023SJohn Marino      It also converts all forward slashes to back slashes.  */
137*e4b17023SJohn Marino #if defined (_WIN32)
138*e4b17023SJohn Marino   {
139*e4b17023SJohn Marino     char buf[MAX_PATH];
140*e4b17023SJohn Marino     char* basename;
141*e4b17023SJohn Marino     DWORD len = GetFullPathName (filename, MAX_PATH, buf, &basename);
142*e4b17023SJohn Marino     if (len == 0 || len > MAX_PATH - 1)
143*e4b17023SJohn Marino       return strdup (filename);
144*e4b17023SJohn Marino     else
145*e4b17023SJohn Marino       {
146*e4b17023SJohn Marino 	/* The file system is case-preserving but case-insensitive,
147*e4b17023SJohn Marino 	   Canonicalize to lowercase, using the codepage associated
148*e4b17023SJohn Marino 	   with the process locale.  */
149*e4b17023SJohn Marino         CharLowerBuff (buf, len);
150*e4b17023SJohn Marino         return strdup (buf);
151*e4b17023SJohn Marino       }
152*e4b17023SJohn Marino   }
153*e4b17023SJohn Marino #endif
154*e4b17023SJohn Marino 
155*e4b17023SJohn Marino   /* This system is a lost cause, just duplicate the filename.  */
156*e4b17023SJohn Marino   return strdup (filename);
157*e4b17023SJohn Marino }
158