getcwd.c 4.1 KB

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