| 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., 59 Temple Place - Suite 330,
|
|---|
| 21 | Boston, MA 02111-1307, 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 | #include "config.h"
|
|---|
| 37 | #include "ansidecl.h"
|
|---|
| 38 | #include "libiberty.h"
|
|---|
| 39 |
|
|---|
| 40 | #ifdef HAVE_LIMITS_H
|
|---|
| 41 | #include <limits.h>
|
|---|
| 42 | #endif
|
|---|
| 43 | #ifdef HAVE_STDLIB_H
|
|---|
| 44 | #include <stdlib.h>
|
|---|
| 45 | #endif
|
|---|
| 46 | #ifdef HAVE_UNISTD_H
|
|---|
| 47 | #include <unistd.h>
|
|---|
| 48 | #endif
|
|---|
| 49 | #ifdef HAVE_STRING_H
|
|---|
| 50 | #include <string.h>
|
|---|
| 51 | #endif
|
|---|
| 52 |
|
|---|
| 53 | /* On GNU libc systems the declaration is only visible with _GNU_SOURCE. */
|
|---|
| 54 | #if defined(HAVE_CANONICALIZE_FILE_NAME) \
|
|---|
| 55 | && defined(NEED_DECLARATION_CANONICALIZE_FILE_NAME)
|
|---|
| 56 | extern char *canonicalize_file_name (const char *);
|
|---|
| 57 | #endif
|
|---|
| 58 |
|
|---|
| 59 | #if defined(HAVE_REALPATH)
|
|---|
| 60 | # if defined (PATH_MAX)
|
|---|
| 61 | # define REALPATH_LIMIT PATH_MAX
|
|---|
| 62 | # else
|
|---|
| 63 | # if defined (MAXPATHLEN)
|
|---|
| 64 | # define REALPATH_LIMIT MAXPATHLEN
|
|---|
| 65 | # endif
|
|---|
| 66 | # endif
|
|---|
| 67 | #endif
|
|---|
| 68 |
|
|---|
| 69 | char *
|
|---|
| 70 | lrealpath (filename)
|
|---|
| 71 | const char *filename;
|
|---|
| 72 | {
|
|---|
| 73 | /* Method 1: The system has a compile time upper bound on a filename
|
|---|
| 74 | path. Use that and realpath() to canonicalize the name. This is
|
|---|
| 75 | the most common case. Note that, if there isn't a compile time
|
|---|
| 76 | upper bound, you want to avoid realpath() at all costs. */
|
|---|
| 77 | #if defined(REALPATH_LIMIT)
|
|---|
| 78 | {
|
|---|
| 79 | char buf[REALPATH_LIMIT];
|
|---|
| 80 | const char *rp = realpath (filename, buf);
|
|---|
| 81 | if (rp == NULL)
|
|---|
| 82 | rp = filename;
|
|---|
| 83 | return strdup (rp);
|
|---|
| 84 | }
|
|---|
| 85 | #endif /* REALPATH_LIMIT */
|
|---|
| 86 |
|
|---|
| 87 | /* Method 2: The host system (i.e., GNU) has the function
|
|---|
| 88 | canonicalize_file_name() which malloc's a chunk of memory and
|
|---|
| 89 | returns that, use that. */
|
|---|
| 90 | #if defined(HAVE_CANONICALIZE_FILE_NAME)
|
|---|
| 91 | {
|
|---|
| 92 | char *rp = canonicalize_file_name (filename);
|
|---|
| 93 | if (rp == NULL)
|
|---|
| 94 | return strdup (filename);
|
|---|
| 95 | else
|
|---|
| 96 | return rp;
|
|---|
| 97 | }
|
|---|
| 98 | #endif
|
|---|
| 99 |
|
|---|
| 100 | /* Method 3: Now we're getting desperate! The system doesn't have a
|
|---|
| 101 | compile time buffer size and no alternative function. Query the
|
|---|
| 102 | OS, using pathconf(), for the buffer limit. Care is needed
|
|---|
| 103 | though, some systems do not limit PATH_MAX (return -1 for
|
|---|
| 104 | pathconf()) making it impossible to pass a correctly sized buffer
|
|---|
| 105 | to realpath() (it could always overflow). On those systems, we
|
|---|
| 106 | skip this. */
|
|---|
| 107 | #if defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H)
|
|---|
| 108 | {
|
|---|
| 109 | /* Find out the max path size. */
|
|---|
| 110 | long path_max = pathconf ("/", _PC_PATH_MAX);
|
|---|
| 111 | if (path_max > 0)
|
|---|
| 112 | {
|
|---|
| 113 | /* PATH_MAX is bounded. */
|
|---|
| 114 | char *buf, *rp, *ret;
|
|---|
| 115 | buf = malloc (path_max);
|
|---|
| 116 | if (buf == NULL)
|
|---|
| 117 | return NULL;
|
|---|
| 118 | rp = realpath (filename, buf);
|
|---|
| 119 | ret = strdup (rp ? rp : filename);
|
|---|
| 120 | free (buf);
|
|---|
| 121 | return ret;
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | #endif
|
|---|
| 125 |
|
|---|
| 126 | /* This system is a lost cause, just duplicate the filename. */
|
|---|
| 127 | return strdup (filename);
|
|---|
| 128 | }
|
|---|