realpath.c 3.8 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 <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. if (path == NULL) {
  54. __set_errno(EINVAL);
  55. return NULL;
  56. }
  57. if (*path == '\0') {
  58. __set_errno(ENOENT);
  59. return NULL;
  60. }
  61. /* Make a copy of the source path since we may need to modify it. */
  62. if (strlen(path) >= PATH_MAX - 2) {
  63. __set_errno(ENAMETOOLONG);
  64. return NULL;
  65. }
  66. strcpy(copy_path, path);
  67. path = copy_path;
  68. max_path = copy_path + PATH_MAX - 2;
  69. /* If it's a relative pathname use getcwd for starters. */
  70. if (*path != '/') {
  71. /* Ohoo... */
  72. getcwd(new_path, PATH_MAX - 1);
  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 (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. /* See if latest pathname component is a symlink. */
  120. *new_path = '\0';
  121. n = readlink(got_path, link_path, PATH_MAX - 1);
  122. if (n < 0) {
  123. /* EINVAL means the file exists but isn't a symlink. */
  124. if (errno != EINVAL) {
  125. /* Make sure it's null terminated. */
  126. *new_path = '\0';
  127. strcpy(resolved_path, got_path);
  128. return NULL;
  129. }
  130. } else {
  131. /* Note: readlink doesn't add the null byte. */
  132. link_path[n] = '\0';
  133. if (*link_path == '/')
  134. /* Start over for an absolute symlink. */
  135. new_path = got_path;
  136. else
  137. /* Otherwise back up over this component. */
  138. while (*(--new_path) != '/');
  139. /* Safe sex check. */
  140. if (strlen(path) + n >= PATH_MAX - 2) {
  141. __set_errno(ENAMETOOLONG);
  142. return NULL;
  143. }
  144. /* Insert symlink contents into path. */
  145. strcat(link_path, path);
  146. strcpy(copy_path, link_path);
  147. path = copy_path;
  148. }
  149. #endif /* S_IFLNK */
  150. *new_path++ = '/';
  151. }
  152. /* Delete trailing slash but don't whomp a lone slash. */
  153. if (new_path != got_path + 1 && new_path[-1] == '/')
  154. new_path--;
  155. /* Make sure it's null terminated. */
  156. *new_path = '\0';
  157. strcpy(resolved_path, got_path);
  158. return resolved_path;
  159. }