getcwd.c 3.4 KB

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