realpath.c 4.0 KB

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