getcwd.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. /* These functions find the absolute path to the current working directory. */
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <sys/stat.h>
  10. #include <dirent.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <sys/syscall.h>
  14. libc_hidden_proto(getcwd)
  15. libc_hidden_proto(strcat)
  16. libc_hidden_proto(strcpy)
  17. libc_hidden_proto(strncpy)
  18. libc_hidden_proto(strlen)
  19. libc_hidden_proto(opendir)
  20. libc_hidden_proto(readdir)
  21. libc_hidden_proto(closedir)
  22. libc_hidden_proto(stat)
  23. #ifdef __NR_getcwd
  24. # define __NR___syscall_getcwd __NR_getcwd
  25. static inline
  26. _syscall2(int, __syscall_getcwd, char *, buf, unsigned long, size);
  27. #else
  28. /* If the syscall is not present, we have to walk up the
  29. * directory tree till we hit the root. Now we _could_
  30. * use /proc/self/cwd if /proc is mounted... That approach
  31. * is left an an exercise for the reader... */
  32. /* Seems a few broken filesystems (like coda) don't like this */
  33. /* #undef FAST_DIR_SEARCH_POSSIBLE on Linux */
  34. /* Routine to find the step back down */
  35. static char *search_dir(dev_t this_dev, ino_t this_ino, char *path_buf, int path_size)
  36. {
  37. DIR *dp;
  38. struct dirent *d;
  39. char *ptr;
  40. int slen;
  41. struct stat st;
  42. # ifdef FAST_DIR_SEARCH_POSSIBLE
  43. /* The test is for ELKS lib 0.0.9, this should be fixed in the real kernel */
  44. int slow_search = (sizeof(ino_t) != sizeof(d->d_ino));
  45. # endif
  46. if (stat(path_buf, &st) < 0) {
  47. goto oops;
  48. }
  49. # ifdef FAST_DIR_SEARCH_POSSIBLE
  50. if (this_dev != st.st_dev)
  51. slow_search = 1;
  52. # endif
  53. slen = strlen(path_buf);
  54. ptr = path_buf + slen - 1;
  55. if (*ptr != '/') {
  56. if (slen + 2 > path_size) {
  57. goto oops;
  58. }
  59. strcpy(++ptr, "/");
  60. slen++;
  61. }
  62. slen++;
  63. dp = opendir(path_buf);
  64. if (dp == 0) {
  65. goto oops;
  66. }
  67. while ((d = readdir(dp)) != 0) {
  68. # ifdef FAST_DIR_SEARCH_POSSIBLE
  69. if (slow_search || this_ino == d->d_ino) {
  70. # endif
  71. if (slen + strlen(d->d_name) > path_size) {
  72. goto oops;
  73. }
  74. strcpy(ptr + 1, d->d_name);
  75. if (stat(path_buf, &st) < 0)
  76. continue;
  77. if (st.st_ino == this_ino && st.st_dev == this_dev) {
  78. closedir(dp);
  79. return path_buf;
  80. }
  81. # ifdef FAST_DIR_SEARCH_POSSIBLE
  82. }
  83. # endif
  84. }
  85. closedir(dp);
  86. return 0;
  87. oops:
  88. __set_errno(ERANGE);
  89. return 0;
  90. }
  91. /* Routine to go up tree */
  92. static char *recurser(char *path_buf, int path_size, dev_t root_dev, ino_t root_ino)
  93. {
  94. struct stat st;
  95. dev_t this_dev;
  96. ino_t this_ino;
  97. if (stat(path_buf, &st) < 0) {
  98. if (errno != EFAULT)
  99. goto oops;
  100. return 0;
  101. }
  102. this_dev = st.st_dev;
  103. this_ino = st.st_ino;
  104. if (this_dev == root_dev && this_ino == root_ino) {
  105. if (path_size < 2) {
  106. goto oops;
  107. }
  108. strcpy(path_buf, "/");
  109. return path_buf;
  110. }
  111. if (strlen(path_buf) + 4 > path_size) {
  112. goto oops;
  113. }
  114. strcat(path_buf, "/..");
  115. if (recurser(path_buf, path_size, root_dev, root_ino) == 0)
  116. return 0;
  117. return search_dir(this_dev, this_ino, path_buf, path_size);
  118. oops:
  119. __set_errno(ERANGE);
  120. return 0;
  121. }
  122. static inline
  123. int __syscall_getcwd(char * buf, unsigned long size)
  124. {
  125. int len;
  126. char *cwd;
  127. struct stat st;
  128. int olderrno;
  129. olderrno = errno;
  130. len = -1;
  131. /* get stat for root to have a valid parameters for the terminating condition */
  132. if (stat("/", &st) < 0) {
  133. /* root dir not found! */
  134. return -1;
  135. }
  136. /* start with actual dir */
  137. if (buf) strncpy(buf, ".", size);
  138. cwd = recurser(buf, size, st.st_dev, st.st_ino);
  139. if (cwd) {
  140. len = strlen(buf);
  141. __set_errno(olderrno);
  142. }
  143. return len;
  144. }
  145. #endif /* __NR_getcwd */
  146. char *getcwd(char *buf, size_t size)
  147. {
  148. int ret;
  149. char *path;
  150. size_t alloc_size = size;
  151. if (size == 0) {
  152. if (buf != NULL) {
  153. __set_errno(EINVAL);
  154. return NULL;
  155. }
  156. alloc_size = PATH_MAX;
  157. }
  158. path=buf;
  159. if (buf == NULL) {
  160. path = malloc(alloc_size);
  161. if (path == NULL)
  162. return NULL;
  163. }
  164. ret = __syscall_getcwd(path, alloc_size);
  165. if (ret >= 0)
  166. {
  167. if (buf == NULL && size == 0)
  168. buf = realloc(path, ret);
  169. if (buf == NULL)
  170. buf = path;
  171. return buf;
  172. }
  173. if (buf == NULL)
  174. free (path);
  175. return NULL;
  176. }
  177. libc_hidden_def(getcwd)