realpath.c 4.0 KB

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