realpath.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. 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. }
  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. ;
  101. continue;
  102. }
  103. }
  104. }
  105. /* Safely copy the next pathname component. */
  106. while (*path != '\0' && *path != '/') {
  107. if (path > max_path) {
  108. 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. 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. }
  131. else {
  132. /* Note: readlink doesn't add the null byte. */
  133. link_path[n] = '\0';
  134. if (*link_path == '/')
  135. /* Start over for an absolute symlink. */
  136. new_path = got_path;
  137. else
  138. /* Otherwise back up over this component. */
  139. while (*(--new_path) != '/')
  140. ;
  141. /* Safe sex check. */
  142. if (strlen(path) + n >= PATH_MAX-2) {
  143. errno = ENAMETOOLONG;
  144. return NULL;
  145. }
  146. /* Insert symlink contents into path. */
  147. strcat(link_path, path);
  148. strcpy(copy_path, link_path);
  149. path = copy_path;
  150. }
  151. #endif /* S_IFLNK */
  152. *new_path++ = '/';
  153. }
  154. /* Delete trailing slash but don't whomp a lone slash. */
  155. if (new_path != got_path + 1 && new_path[-1] == '/')
  156. new_path--;
  157. /* Make sure it's null terminated. */
  158. *new_path = '\0';
  159. strcpy (resolved_path, got_path);
  160. return resolved_path;
  161. }