realpath.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. char *realpath(const char *path, char got_path[])
  33. {
  34. char copy_path[PATH_MAX];
  35. char *max_path, *new_path, *allocated_path;
  36. size_t path_len;
  37. int readlinks = 0;
  38. #ifdef S_IFLNK
  39. int link_len;
  40. #endif
  41. if (path == NULL) {
  42. __set_errno(EINVAL);
  43. return NULL;
  44. }
  45. if (*path == '\0') {
  46. __set_errno(ENOENT);
  47. return NULL;
  48. }
  49. /* Make a copy of the source path since we may need to modify it. */
  50. path_len = strlen(path);
  51. if (path_len >= PATH_MAX - 2) {
  52. __set_errno(ENAMETOOLONG);
  53. return NULL;
  54. }
  55. /* Copy so that path is at the end of copy_path[] */
  56. strcpy(copy_path + (PATH_MAX-1) - path_len, path);
  57. path = copy_path + (PATH_MAX-1) - path_len;
  58. allocated_path = got_path ? NULL : (got_path = malloc(PATH_MAX));
  59. max_path = got_path + PATH_MAX - 2; /* points to last non-NUL char */
  60. new_path = got_path;
  61. if (*path != '/') {
  62. /* If it's a relative pathname use getcwd for starters. */
  63. if (!getcwd(new_path, PATH_MAX - 1))
  64. goto err;
  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 (new_path > max_path) {
  100. __set_errno(ENAMETOOLONG);
  101. err:
  102. free(allocated_path);
  103. return NULL;
  104. }
  105. *new_path++ = *path++;
  106. }
  107. #ifdef S_IFLNK
  108. /* Protect against infinite loops. */
  109. if (readlinks++ > MAX_READLINKS) {
  110. __set_errno(ELOOP);
  111. goto err;
  112. }
  113. path_len = strlen(path);
  114. /* See if last (so far) pathname component is a symlink. */
  115. *new_path = '\0';
  116. {
  117. int sv_errno = errno;
  118. link_len = readlink(got_path, copy_path, PATH_MAX - 1);
  119. if (link_len < 0) {
  120. /* EINVAL means the file exists but isn't a symlink. */
  121. if (errno != EINVAL) {
  122. goto err;
  123. }
  124. } else {
  125. /* Safe sex check. */
  126. if (path_len + link_len >= PATH_MAX - 2) {
  127. __set_errno(ENAMETOOLONG);
  128. goto err;
  129. }
  130. /* Note: readlink doesn't add the null byte. */
  131. /* copy_path[link_len] = '\0'; - we don't need it too */
  132. if (*copy_path == '/')
  133. /* Start over for an absolute symlink. */
  134. new_path = got_path;
  135. else
  136. /* Otherwise back up over this component. */
  137. while (*(--new_path) != '/');
  138. /* Prepend symlink contents to path. */
  139. memmove(copy_path + (PATH_MAX-1) - link_len - path_len, copy_path, link_len);
  140. path = copy_path + (PATH_MAX-1) - link_len - path_len;
  141. }
  142. __set_errno(sv_errno);
  143. }
  144. #endif /* S_IFLNK */
  145. *new_path++ = '/';
  146. }
  147. /* Delete trailing slash but don't whomp a lone slash. */
  148. if (new_path != got_path + 1 && new_path[-1] == '/')
  149. new_path--;
  150. /* Make sure it's null terminated. */
  151. *new_path = '\0';
  152. return got_path;
  153. }