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