/lib/libc/stdlib/realpath.c

https://bitbucket.org/kmv/aeriebsd-src · C · 193 lines · 124 code · 9 blank · 60 comment · 50 complexity · c2908cd498cfc1b638c0bf5656423672 MD5 · raw file

  1. /*
  2. * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. The names of the authors may not be used to endorse or promote
  13. * products derived from this software without specific prior written
  14. * permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #if defined(LIBC_SCCS) && !defined(lint)
  29. static const char rcsid[] = "$ABSD$";
  30. #endif
  31. #include <sys/param.h>
  32. #include <sys/stat.h>
  33. #include <errno.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. /*
  38. * char *realpath(const char *path, char resolved[PATH_MAX]);
  39. *
  40. * Find the real name of path, by removing all ".", ".." and symlink
  41. * components. Returns (resolved) on success, or (NULL) on failure,
  42. * in which case the path which caused trouble is left in (resolved).
  43. */
  44. char *
  45. realpath(const char *path, char resolved[PATH_MAX])
  46. {
  47. struct stat sb;
  48. char *p, *q, *s;
  49. size_t left_len, resolved_len;
  50. unsigned symlinks;
  51. int serrno, slen;
  52. char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
  53. serrno = errno;
  54. symlinks = 0;
  55. if (path[0] == '/') {
  56. resolved[0] = '/';
  57. resolved[1] = '\0';
  58. if (path[1] == '\0')
  59. return (resolved);
  60. resolved_len = 1;
  61. left_len = strlcpy(left, path + 1, sizeof(left));
  62. } else {
  63. if (getcwd(resolved, PATH_MAX) == NULL) {
  64. strlcpy(resolved, ".", PATH_MAX);
  65. return (NULL);
  66. }
  67. resolved_len = strlen(resolved);
  68. left_len = strlcpy(left, path, sizeof(left));
  69. }
  70. if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
  71. errno = ENAMETOOLONG;
  72. return (NULL);
  73. }
  74. /*
  75. * Iterate over path components in `left'.
  76. */
  77. while (left_len != 0) {
  78. /*
  79. * Extract the next path component and adjust `left'
  80. * and its length.
  81. */
  82. p = strchr(left, '/');
  83. s = p ? p : left + left_len;
  84. if (s - left >= sizeof(next_token)) {
  85. errno = ENAMETOOLONG;
  86. return (NULL);
  87. }
  88. memcpy(next_token, left, s - left);
  89. next_token[s - left] = '\0';
  90. left_len -= s - left;
  91. if (p != NULL)
  92. memmove(left, s + 1, left_len + 1);
  93. if (resolved[resolved_len - 1] != '/') {
  94. if (resolved_len + 1 >= PATH_MAX) {
  95. errno = ENAMETOOLONG;
  96. return (NULL);
  97. }
  98. resolved[resolved_len++] = '/';
  99. resolved[resolved_len] = '\0';
  100. }
  101. if (next_token[0] == '\0')
  102. continue;
  103. else if (strcmp(next_token, ".") == 0)
  104. continue;
  105. else if (strcmp(next_token, "..") == 0) {
  106. /*
  107. * Strip the last path component except when we have
  108. * single "/"
  109. */
  110. if (resolved_len > 1) {
  111. resolved[resolved_len - 1] = '\0';
  112. q = strrchr(resolved, '/') + 1;
  113. *q = '\0';
  114. resolved_len = q - resolved;
  115. }
  116. continue;
  117. }
  118. /*
  119. * Append the next path component and lstat() it. If
  120. * lstat() fails we still can return successfully if
  121. * there are no more path components left.
  122. */
  123. resolved_len = strlcat(resolved, next_token, PATH_MAX);
  124. if (resolved_len >= PATH_MAX) {
  125. errno = ENAMETOOLONG;
  126. return (NULL);
  127. }
  128. if (lstat(resolved, &sb) != 0) {
  129. if (errno == ENOENT && p == NULL) {
  130. errno = serrno;
  131. return (resolved);
  132. }
  133. return (NULL);
  134. }
  135. if (S_ISLNK(sb.st_mode)) {
  136. if (symlinks++ > MAXSYMLINKS) {
  137. errno = ELOOP;
  138. return (NULL);
  139. }
  140. slen = readlink(resolved, symlink, sizeof(symlink) - 1);
  141. if (slen < 0)
  142. return (NULL);
  143. symlink[slen] = '\0';
  144. if (symlink[0] == '/') {
  145. resolved[1] = 0;
  146. resolved_len = 1;
  147. } else if (resolved_len > 1) {
  148. /* Strip the last path component. */
  149. resolved[resolved_len - 1] = '\0';
  150. q = strrchr(resolved, '/') + 1;
  151. *q = '\0';
  152. resolved_len = q - resolved;
  153. }
  154. /*
  155. * If there are any path components left, then
  156. * append them to symlink. The result is placed
  157. * in `left'.
  158. */
  159. if (p != NULL) {
  160. if (symlink[slen - 1] != '/') {
  161. if (slen + 1 >= sizeof(symlink)) {
  162. errno = ENAMETOOLONG;
  163. return (NULL);
  164. }
  165. symlink[slen] = '/';
  166. symlink[slen + 1] = 0;
  167. }
  168. left_len = strlcat(symlink, left, sizeof(left));
  169. if (left_len >= sizeof(left)) {
  170. errno = ENAMETOOLONG;
  171. return (NULL);
  172. }
  173. }
  174. left_len = strlcpy(left, symlink, sizeof(left));
  175. }
  176. }
  177. /*
  178. * Remove trailing slash except when the resolved pathname
  179. * is a single "/".
  180. */
  181. if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
  182. resolved[resolved_len - 1] = '\0';
  183. return (resolved);
  184. }