getcwd.c 353 B

12345678910111213141516171819
  1. #include <unistd.h>
  2. #include <errno.h>
  3. #include <limits.h>
  4. #include <string.h>
  5. #include "syscall.h"
  6. char *getcwd(char *buf, size_t size)
  7. {
  8. char tmp[PATH_MAX];
  9. if (!buf) {
  10. buf = tmp;
  11. size = PATH_MAX;
  12. } else if (!size) {
  13. errno = EINVAL;
  14. return 0;
  15. }
  16. if (syscall(SYS_getcwd, buf, size) < 0) return 0;
  17. return buf == tmp ? strdup(buf) : buf;
  18. }