2 * \file src/lrealpath.c
4 * \brief Libiberty realpath.
6 * Like realpath, but more consistent behavior.
8 * Based on gdb_realpath from GDB.
10 * Copyright 2003 Free Software Foundation, Inc.
12 * This file is part of the libiberty library.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street - Fifth Floor,
27 * Boston, MA 02110-1301, USA.
32 @deftypefn Replacement {const char*} lrealpath (const char *@var{name})
34 Given a pointer to a string containing a pathname, returns a canonical
35 version of the filename. Symlinks will be resolved, and ``.'' and ``..''
36 components will be simplified. The returned value will be allocated using
37 @code{malloc}, or @code{NULL} will be returned on a memory allocation error.
44 #include "lrealpath.h"
59 /* On GNU libc systems the declaration is only visible with _GNU_SOURCE. */
60 #if defined(HAVE_CANONICALIZE_FILE_NAME) \
61 && defined(NEED_DECLARATION_CANONICALIZE_FILE_NAME)
62 extern char *canonicalize_file_name (const char *);
65 #if defined(HAVE_REALPATH)
66 # if defined (PATH_MAX)
67 # define REALPATH_LIMIT PATH_MAX
69 # if defined (MAXPATHLEN)
70 # define REALPATH_LIMIT MAXPATHLEN
74 /* cygwin has realpath, so it won't get here. */
76 # define WIN32_LEAN_AND_MEAN
77 # include <windows.h> /* for GetFullPathName */
82 * \brief A well-defined realpath () that is always compiled in.
85 lrealpath (const char *filename
)
87 /* Method 1: The system has a compile time upper bound on a filename
88 path. Use that and realpath() to canonicalize the name. This is
89 the most common case. Note that, if there isn't a compile time
90 upper bound, you want to avoid realpath() at all costs. */
91 #if defined(REALPATH_LIMIT)
93 char buf
[REALPATH_LIMIT
];
94 const char *rp
= realpath (filename
, buf
);
101 /* Method 2: The host system (i.e., GNU) has the function
102 canonicalize_file_name() which malloc's a chunk of memory and
103 returns that, use that. */
104 #elif defined(HAVE_CANONICALIZE_FILE_NAME)
106 char *rp
= canonicalize_file_name (filename
);
108 return strdup (filename
);
112 /* HAVE_CANONICALIZE_FILE_NAME */
114 /* Method 3: Now we're getting desperate! The system doesn't have a
115 compile time buffer size and no alternative function. Query the
116 OS, using pathconf(), for the buffer limit. Care is needed
117 though, some systems do not limit PATH_MAX (return -1 for
118 pathconf()) making it impossible to pass a correctly sized buffer
119 to realpath() (it could always overflow). On those systems, we
121 #elif defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H)
123 /* Find out the max path size. */
124 long path_max
= pathconf ("/", _PC_PATH_MAX
);
127 /* PATH_MAX is bounded. */
128 char *buf
, *rp
, *ret
;
129 buf
= (char *) malloc (path_max
);
132 rp
= realpath (filename
, buf
);
133 ret
= strdup (rp
? rp
: filename
);
138 /* HAVE_REALPATH && HAVE_UNISTD_H */
140 /* The MS Windows method. If we don't have realpath, we assume we
141 don't have symlinks and just canonicalize to a Windows absolute
142 path. GetFullPath converts ../ and ./ in relative paths to
143 absolute paths, filling in current drive if one is not given
144 or using the current directory of a specified drive (eg, "E:foo").
145 It also converts all forward slashes to back slashes. */
146 #elif defined (_WIN32)
150 DWORD len
= GetFullPathName (filename
, MAX_PATH
, buf
, &basename
);
151 if (len
== 0 || len
> MAX_PATH
- 1)
152 return strdup (filename
);
155 /* The file system is case-preserving but case-insensitive,
156 Canonicalize to lowercase, using the codepage associated
157 with the process locale. */
158 CharLowerBuff (buf
, len
);
164 /* This system is a lost cause, just duplicate the filename. */
165 return strdup (filename
);