getcwd.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 == 0) {
  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. goto oops;
  65. }
  66. strcpy(ptr + 1, d->d_name);
  67. if (stat(path_buf, &st) < 0)
  68. continue;
  69. if (st.st_ino == this_ino && st.st_dev == this_dev) {
  70. closedir(dp);
  71. return path_buf;
  72. }
  73. # ifdef FAST_DIR_SEARCH_POSSIBLE
  74. }
  75. # endif
  76. }
  77. closedir(dp);
  78. return 0;
  79. oops:
  80. __set_errno(ERANGE);
  81. return 0;
  82. }
  83. /* Routine to go up tree */
  84. static char *recurser(char *path_buf, int path_size, dev_t root_dev, ino_t root_ino)
  85. {
  86. struct stat st;
  87. dev_t this_dev;
  88. ino_t this_ino;
  89. if (stat(path_buf, &st) < 0) {
  90. if (errno != EFAULT)
  91. goto oops;
  92. return 0;
  93. }
  94. this_dev = st.st_dev;
  95. this_ino = st.st_ino;
  96. if (this_dev == root_dev && this_ino == root_ino) {
  97. if (path_size < 2) {
  98. goto oops;
  99. }
  100. strcpy(path_buf, "/");
  101. return path_buf;
  102. }
  103. if (strlen(path_buf) + 4 > path_size) {
  104. goto oops;
  105. }
  106. strcat(path_buf, "/..");
  107. if (recurser(path_buf, path_size, root_dev, root_ino) == 0)
  108. return 0;
  109. return search_dir(this_dev, this_ino, path_buf, path_size);
  110. oops:
  111. __set_errno(ERANGE);
  112. return 0;
  113. }
  114. static __always_inline
  115. int __syscall_getcwd(char * buf, unsigned long size)
  116. {
  117. int len;
  118. char *cwd;
  119. struct stat st;
  120. int olderrno;
  121. olderrno = errno;
  122. len = -1;
  123. /* get stat for root to have a valid parameters for the terminating condition */
  124. if (stat("/", &st) < 0) {
  125. /* root dir not found! */
  126. return -1;
  127. }
  128. /* start with actual dir */
  129. if (buf) strncpy(buf, ".", size);
  130. cwd = recurser(buf, size, st.st_dev, st.st_ino);
  131. if (cwd) {
  132. len = strlen(buf) + 1;
  133. __set_errno(olderrno);
  134. }
  135. return len;
  136. }
  137. #endif /* __NR_getcwd */
  138. char *getcwd(char *buf, size_t size)
  139. {
  140. int ret;
  141. char *path;
  142. size_t alloc_size = size;
  143. if (size == 0) {
  144. if (buf != NULL) {
  145. __set_errno(EINVAL);
  146. return NULL;
  147. }
  148. alloc_size = MAX (PATH_MAX, getpagesize ());
  149. }
  150. path=buf;
  151. if (buf == NULL) {
  152. path = malloc(alloc_size);
  153. if (path == NULL)
  154. return NULL;
  155. }
  156. ret = __syscall_getcwd(path, alloc_size);
  157. if (ret >= 0)
  158. {
  159. if (buf == NULL && size == 0)
  160. buf = realloc(path, ret);
  161. if (buf == NULL)
  162. buf = path;
  163. return buf;
  164. }
  165. if (buf == NULL)
  166. free (path);
  167. return NULL;
  168. }
  169. libc_hidden_def(getcwd)