realpath.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <sys/stat.h> /* for S_IFLNK */
  20. libc_hidden_proto(strcat)
  21. libc_hidden_proto(strcpy)
  22. 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 resolved_path[])
  39. #else
  40. char *realpath(path, resolved_path)
  41. const char *path;
  42. char resolved_path[];
  43. #endif
  44. {
  45. char copy_path[PATH_MAX];
  46. char link_path[PATH_MAX];
  47. char got_path[PATH_MAX];
  48. char *new_path = got_path;
  49. char *max_path;
  50. int readlinks = 0;
  51. int n;
  52. /* Make a copy of the source path since we may need to modify it. */
  53. if (strlen(path) >= PATH_MAX - 2) {
  54. __set_errno(ENAMETOOLONG);
  55. return NULL;
  56. }
  57. strcpy(copy_path, path);
  58. path = copy_path;
  59. max_path = copy_path + PATH_MAX - 2;
  60. /* If it's a relative pathname use getwd for starters. */
  61. if (*path != '/') {
  62. /* Ohoo... */
  63. #define HAVE_GETCWD
  64. #ifdef HAVE_GETCWD
  65. getcwd(new_path, PATH_MAX - 1);
  66. #else
  67. getwd(new_path);
  68. #endif
  69. new_path += strlen(new_path);
  70. if (new_path[-1] != '/')
  71. *new_path++ = '/';
  72. } else {
  73. *new_path++ = '/';
  74. path++;
  75. }
  76. /* Expand each slash-separated pathname component. */
  77. while (*path != '\0') {
  78. /* Ignore stray "/". */
  79. if (*path == '/') {
  80. path++;
  81. continue;
  82. }
  83. if (*path == '.') {
  84. /* Ignore ".". */
  85. if (path[1] == '\0' || path[1] == '/') {
  86. path++;
  87. continue;
  88. }
  89. if (path[1] == '.') {
  90. if (path[2] == '\0' || path[2] == '/') {
  91. path += 2;
  92. /* Ignore ".." at root. */
  93. if (new_path == got_path + 1)
  94. continue;
  95. /* Handle ".." by backing up. */
  96. while ((--new_path)[-1] != '/');
  97. continue;
  98. }
  99. }
  100. }
  101. /* Safely copy the next pathname component. */
  102. while (*path != '\0' && *path != '/') {
  103. if (path > max_path) {
  104. __set_errno(ENAMETOOLONG);
  105. return NULL;
  106. }
  107. *new_path++ = *path++;
  108. }
  109. #ifdef S_IFLNK
  110. /* Protect against infinite loops. */
  111. if (readlinks++ > MAX_READLINKS) {
  112. __set_errno(ELOOP);
  113. return NULL;
  114. }
  115. /* See if latest pathname component is a symlink. */
  116. *new_path = '\0';
  117. n = readlink(got_path, link_path, PATH_MAX - 1);
  118. if (n < 0) {
  119. /* EINVAL means the file exists but isn't a symlink. */
  120. if (errno != EINVAL) {
  121. /* Make sure it's null terminated. */
  122. *new_path = '\0';
  123. strcpy(resolved_path, got_path);
  124. return NULL;
  125. }
  126. } else {
  127. /* Note: readlink doesn't add the null byte. */
  128. link_path[n] = '\0';
  129. if (*link_path == '/')
  130. /* Start over for an absolute symlink. */
  131. new_path = got_path;
  132. else
  133. /* Otherwise back up over this component. */
  134. while (*(--new_path) != '/');
  135. /* Safe sex check. */
  136. if (strlen(path) + n >= PATH_MAX - 2) {
  137. __set_errno(ENAMETOOLONG);
  138. return NULL;
  139. }
  140. /* Insert symlink contents into path. */
  141. strcat(link_path, path);
  142. strcpy(copy_path, link_path);
  143. path = copy_path;
  144. }
  145. #endif /* S_IFLNK */
  146. *new_path++ = '/';
  147. }
  148. /* Delete trailing slash but don't whomp a lone slash. */
  149. if (new_path != got_path + 1 && new_path[-1] == '/')
  150. new_path--;
  151. /* Make sure it's null terminated. */
  152. *new_path = '\0';
  153. strcpy(resolved_path, got_path);
  154. return resolved_path;
  155. }