getcwd.c 3.8 KB

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