Default STEP export to board only
[geda-pcb/pcjc2/v2.git] / src / lrealpath.c
blob8c4607b0a949d865c30a486aa8ca0e6bf81d6f5d
1 /*!
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.
39 @end deftypefn
43 #include "config.h"
44 #include "lrealpath.h"
46 #ifdef HAVE_LIMITS_H
47 #include <limits.h>
48 #endif
49 #ifdef HAVE_STDLIB_H
50 #include <stdlib.h>
51 #endif
52 #ifdef HAVE_UNISTD_H
53 #include <unistd.h>
54 #endif
55 #ifdef HAVE_STRING_H
56 #include <string.h>
57 #endif
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 *);
63 #endif
65 #if defined(HAVE_REALPATH)
66 # if defined (PATH_MAX)
67 # define REALPATH_LIMIT PATH_MAX
68 # else
69 # if defined (MAXPATHLEN)
70 # define REALPATH_LIMIT MAXPATHLEN
71 # endif
72 # endif
73 #else
74 /* cygwin has realpath, so it won't get here. */
75 # if defined (_WIN32)
76 # define WIN32_LEAN_AND_MEAN
77 # include <windows.h> /* for GetFullPathName */
78 # endif
79 #endif
81 /*!
82 * \brief A well-defined realpath () that is always compiled in.
84 char *
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);
95 if (rp == NULL)
96 rp = filename;
97 return strdup (rp);
99 /* REALPATH_LIMIT */
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);
107 if (rp == NULL)
108 return strdup (filename);
109 else
110 return rp;
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
120 skip this. */
121 #elif defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H)
123 /* Find out the max path size. */
124 long path_max = pathconf ("/", _PC_PATH_MAX);
125 if (path_max > 0)
127 /* PATH_MAX is bounded. */
128 char *buf, *rp, *ret;
129 buf = (char *) malloc (path_max);
130 if (buf == NULL)
131 return NULL;
132 rp = realpath (filename, buf);
133 ret = strdup (rp ? rp : filename);
134 free (buf);
135 return ret;
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)
148 char buf[MAX_PATH];
149 char* basename;
150 DWORD len = GetFullPathName (filename, MAX_PATH, buf, &basename);
151 if (len == 0 || len > MAX_PATH - 1)
152 return strdup (filename);
153 else
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);
159 return strdup (buf);
162 #else
164 /* This system is a lost cause, just duplicate the filename. */
165 return strdup (filename);
166 #endif