realpath.c 3.8 KB

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