lrealpath.c 4.7 KB

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