getcwd.c 3.6 KB

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