realpath.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifdef HAVE_CONFIG_H
  17. #include <config.h>
  18. #endif
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <strings.h>
  24. #include <limits.h> /* for PATH_MAX */
  25. #include <sys/param.h> /* for MAXPATHLEN */
  26. #include <errno.h>
  27. #include <sys/stat.h> /* for S_IFLNK */
  28. #ifndef PATH_MAX
  29. #ifdef _POSIX_VERSION
  30. #define PATH_MAX _POSIX_PATH_MAX
  31. #else
  32. #ifdef MAXPATHLEN
  33. #define PATH_MAX MAXPATHLEN
  34. #else
  35. #define PATH_MAX 1024
  36. #endif
  37. #endif
  38. #endif
  39. #define MAX_READLINKS 32
  40. #ifdef __STDC__
  41. char *realpath(const char *path, char resolved_path[])
  42. #else
  43. char *realpath(path, resolved_path)
  44. const char *path;
  45. char resolved_path[];
  46. #endif
  47. {
  48. char copy_path[PATH_MAX];
  49. char link_path[PATH_MAX];
  50. char got_path[PATH_MAX];
  51. char *new_path = got_path;
  52. char *max_path;
  53. int readlinks = 0;
  54. int n;
  55. /* Make a copy of the source path since we may need to modify it. */
  56. if (__strlen(path) >= PATH_MAX - 2) {
  57. __set_errno(ENAMETOOLONG);
  58. return NULL;
  59. }
  60. __strcpy(copy_path, path);
  61. path = copy_path;
  62. max_path = copy_path + PATH_MAX - 2;
  63. /* If it's a relative pathname use getwd for starters. */
  64. if (*path != '/') {
  65. /* Ohoo... */
  66. #define HAVE_GETCWD
  67. #ifdef HAVE_GETCWD
  68. getcwd(new_path, PATH_MAX - 1);
  69. #else
  70. getwd(new_path);
  71. #endif
  72. new_path += __strlen(new_path);
  73. if (new_path[-1] != '/')
  74. *new_path++ = '/';
  75. } else {
  76. *new_path++ = '/';
  77. path++;
  78. }
  79. /* Expand each slash-separated pathname component. */
  80. while (*path != '\0') {
  81. /* Ignore stray "/". */
  82. if (*path == '/') {
  83. path++;
  84. continue;
  85. }
  86. if (*path == '.') {
  87. /* Ignore ".". */
  88. if (path[1] == '\0' || path[1] == '/') {
  89. path++;
  90. continue;
  91. }
  92. if (path[1] == '.') {
  93. if (path[2] == '\0' || path[2] == '/') {
  94. path += 2;
  95. /* Ignore ".." at root. */
  96. if (new_path == got_path + 1)
  97. continue;
  98. /* Handle ".." by backing up. */
  99. while ((--new_path)[-1] != '/');
  100. continue;
  101. }
  102. }
  103. }
  104. /* Safely copy the next pathname component. */
  105. while (*path != '\0' && *path != '/') {
  106. if (path > max_path) {
  107. __set_errno(ENAMETOOLONG);
  108. return NULL;
  109. }
  110. *new_path++ = *path++;
  111. }
  112. #ifdef S_IFLNK
  113. /* Protect against infinite loops. */
  114. if (readlinks++ > MAX_READLINKS) {
  115. __set_errno(ELOOP);
  116. return NULL;
  117. }
  118. /* See if latest pathname component is a symlink. */
  119. *new_path = '\0';
  120. n = readlink(got_path, link_path, PATH_MAX - 1);
  121. if (n < 0) {
  122. /* EINVAL means the file exists but isn't a symlink. */
  123. if (errno != EINVAL) {
  124. /* Make sure it's null terminated. */
  125. *new_path = '\0';
  126. __strcpy(resolved_path, got_path);
  127. return NULL;
  128. }
  129. } else {
  130. /* Note: readlink doesn't add the null byte. */
  131. link_path[n] = '\0';
  132. if (*link_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. /* Safe sex check. */
  139. if (__strlen(path) + n >= PATH_MAX - 2) {
  140. __set_errno(ENAMETOOLONG);
  141. return NULL;
  142. }
  143. /* Insert symlink contents into path. */
  144. __strcat(link_path, path);
  145. __strcpy(copy_path, link_path);
  146. path = copy_path;
  147. }
  148. #endif /* S_IFLNK */
  149. *new_path++ = '/';
  150. }
  151. /* Delete trailing slash but don't whomp a lone slash. */
  152. if (new_path != got_path + 1 && new_path[-1] == '/')
  153. new_path--;
  154. /* Make sure it's null terminated. */
  155. *new_path = '\0';
  156. __strcpy(resolved_path, got_path);
  157. return resolved_path;
  158. }