getcwd.c 3.3 KB

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