getcwd.c 3.8 KB

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