realpath.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * realpath.c -- canonicalize pathname by removing symlinks
  3. * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
  4. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  7. */
  8. #ifdef HAVE_CONFIG_H
  9. #include <config.h>
  10. #endif
  11. #include <sys/types.h>
  12. #include <unistd.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <limits.h> /* for PATH_MAX */
  16. #include <sys/param.h> /* for MAXPATHLEN */
  17. #include <errno.h>
  18. #include <stdlib.h>
  19. #include <sys/stat.h> /* for S_IFLNK */
  20. /* Experimentally off - libc_hidden_proto(strcat) */
  21. /* Experimentally off - libc_hidden_proto(strcpy) */
  22. /* Experimentally off - libc_hidden_proto(strlen) */
  23. libc_hidden_proto(readlink)
  24. libc_hidden_proto(getcwd)
  25. #ifndef PATH_MAX
  26. #ifdef _POSIX_VERSION
  27. #define PATH_MAX _POSIX_PATH_MAX
  28. #else
  29. #ifdef MAXPATHLEN
  30. #define PATH_MAX MAXPATHLEN
  31. #else
  32. #define PATH_MAX 1024
  33. #endif
  34. #endif
  35. #endif
  36. #define MAX_READLINKS 32
  37. #ifdef __STDC__
  38. char *realpath(const char *path, char got_path[])
  39. #else
  40. char *realpath(path, got_path)
  41. const char *path;
  42. char got_path[];
  43. #endif
  44. {
  45. char copy_path[PATH_MAX];
  46. /* use user supplied buffer directly - reduces stack usage */
  47. /* char got_path[PATH_MAX]; */
  48. char *max_path;
  49. char *new_path;
  50. size_t path_len;
  51. int readlinks = 0;
  52. #ifdef S_IFLNK
  53. int link_len;
  54. #endif
  55. if (path == NULL) {
  56. __set_errno(EINVAL);
  57. return NULL;
  58. }
  59. if (*path == '\0') {
  60. __set_errno(ENOENT);
  61. return NULL;
  62. }
  63. /* Make a copy of the source path since we may need to modify it. */
  64. path_len = strlen(path);
  65. if (path_len >= PATH_MAX - 2) {
  66. __set_errno(ENAMETOOLONG);
  67. return NULL;
  68. }
  69. /* Copy so that path is at the end of copy_path[] */
  70. strcpy(copy_path + (PATH_MAX-1) - path_len, path);
  71. path = copy_path + (PATH_MAX-1) - path_len;
  72. max_path = got_path + PATH_MAX - 2; /* points to last non-NUL char */
  73. new_path = got_path;
  74. if (*path != '/') {
  75. /* If it's a relative pathname use getcwd for starters. */
  76. if (!getcwd(new_path, PATH_MAX - 1))
  77. return NULL;
  78. new_path += strlen(new_path);
  79. if (new_path[-1] != '/')
  80. *new_path++ = '/';
  81. } else {
  82. *new_path++ = '/';
  83. path++;
  84. }
  85. /* Expand each slash-separated pathname component. */
  86. while (*path != '\0') {
  87. /* Ignore stray "/". */
  88. if (*path == '/') {
  89. path++;
  90. continue;
  91. }
  92. if (*path == '.') {
  93. /* Ignore ".". */
  94. if (path[1] == '\0' || path[1] == '/') {
  95. path++;
  96. continue;
  97. }
  98. if (path[1] == '.') {
  99. if (path[2] == '\0' || path[2] == '/') {
  100. path += 2;
  101. /* Ignore ".." at root. */
  102. if (new_path == got_path + 1)
  103. continue;
  104. /* Handle ".." by backing up. */
  105. while ((--new_path)[-1] != '/');
  106. continue;
  107. }
  108. }
  109. }
  110. /* Safely copy the next pathname component. */
  111. while (*path != '\0' && *path != '/') {
  112. if (new_path > max_path) {
  113. __set_errno(ENAMETOOLONG);
  114. return NULL;
  115. }
  116. *new_path++ = *path++;
  117. }
  118. #ifdef S_IFLNK
  119. /* Protect against infinite loops. */
  120. if (readlinks++ > MAX_READLINKS) {
  121. __set_errno(ELOOP);
  122. return NULL;
  123. }
  124. path_len = strlen(path);
  125. /* See if last (so far) pathname component is a symlink. */
  126. *new_path = '\0';
  127. {
  128. int sv_errno = errno;
  129. link_len = readlink(got_path, copy_path, PATH_MAX - 1);
  130. if (link_len < 0) {
  131. /* EINVAL means the file exists but isn't a symlink. */
  132. if (errno != EINVAL) {
  133. return NULL;
  134. }
  135. } else {
  136. /* Safe sex check. */
  137. if (path_len + link_len >= PATH_MAX - 2) {
  138. __set_errno(ENAMETOOLONG);
  139. return NULL;
  140. }
  141. /* Note: readlink doesn't add the null byte. */
  142. /* copy_path[link_len] = '\0'; - we don't need it too */
  143. if (*copy_path == '/')
  144. /* Start over for an absolute symlink. */
  145. new_path = got_path;
  146. else
  147. /* Otherwise back up over this component. */
  148. while (*(--new_path) != '/');
  149. /* Prepend symlink contents to path. */
  150. memmove(copy_path + (PATH_MAX-1) - link_len - path_len, copy_path, link_len);
  151. path = copy_path + (PATH_MAX-1) - link_len - path_len;
  152. }
  153. __set_errno(sv_errno);
  154. }
  155. #endif /* S_IFLNK */
  156. *new_path++ = '/';
  157. }
  158. /* Delete trailing slash but don't whomp a lone slash. */
  159. if (new_path != got_path + 1 && new_path[-1] == '/')
  160. new_path--;
  161. /* Make sure it's null terminated. */
  162. *new_path = '\0';
  163. return got_path;
  164. }