getcwd.c 468 B

1234567891011121314151617181920212223242526272829303132
  1. #include "lib.h"
  2. #include <stddef.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include "sys9.h"
  9. #include "dir.h"
  10. char*
  11. getcwd(char *buf, size_t len)
  12. {
  13. int fd;
  14. fd = _OPEN(".", OREAD);
  15. if(fd < 0) {
  16. errno = EACCES;
  17. return 0;
  18. }
  19. if(_FD2PATH(fd, buf, len) < 0) {
  20. errno = EIO;
  21. _CLOSE(fd);
  22. return 0;
  23. }
  24. _CLOSE(fd);
  25. /* RSC: is this necessary? */
  26. if(buf[0] == '\0')
  27. strcpy(buf, "/");
  28. return buf;
  29. }