realpath.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * realpath.c -- canonicalize pathname by removing symlinks
  3. * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Library Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Library Public License for more details.
  14. */
  15. #define readlink __readlink
  16. #define getcwd __getcwd
  17. #ifdef HAVE_CONFIG_H
  18. #include <config.h>
  19. #endif
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <strings.h>
  25. #include <limits.h> /* for PATH_MAX */
  26. #include <sys/param.h> /* for MAXPATHLEN */
  27. #include <errno.h>
  28. #include <sys/stat.h> /* for S_IFLNK */
  29. #ifndef PATH_MAX
  30. #ifdef _POSIX_VERSION
  31. #define PATH_MAX _POSIX_PATH_MAX
  32. #else
  33. #ifdef MAXPATHLEN
  34. #define PATH_MAX MAXPATHLEN
  35. #else
  36. #define PATH_MAX 1024
  37. #endif
  38. #endif
  39. #endif
  40. #define MAX_READLINKS 32
  41. #ifdef __STDC__
  42. char *realpath(const char *path, char resolved_path[])
  43. #else
  44. char *realpath(path, resolved_path)
  45. const char *path;
  46. char resolved_path[];
  47. #endif
  48. {
  49. char copy_path[PATH_MAX];
  50. char link_path[PATH_MAX];
  51. char got_path[PATH_MAX];
  52. char *new_path = got_path;
  53. char *max_path;
  54. int readlinks = 0;
  55. int n;
  56. /* Make a copy of the source path since we may need to modify it. */
  57. if (__strlen(path) >= PATH_MAX - 2) {
  58. __set_errno(ENAMETOOLONG);
  59. return NULL;
  60. }
  61. __strcpy(copy_path, path);
  62. path = copy_path;
  63. max_path = copy_path + PATH_MAX - 2;
  64. /* If it's a relative pathname use getwd for starters. */
  65. if (*path != '/') {
  66. /* Ohoo... */
  67. #define HAVE_GETCWD
  68. #ifdef HAVE_GETCWD
  69. getcwd(new_path, PATH_MAX - 1);
  70. #else
  71. getwd(new_path);
  72. #endif
  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. }