realpath.c 3.7 KB

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