realpath.c 4.1 KB

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