getcwd.c 3.9 KB

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