getcwd.c 3.4 KB

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