libc-top-half/musl/src/unistd/getcwd.c
raw| 1 | #include <unistd.h> |
| 2 | #include <errno.h> |
| 3 | #include <limits.h> |
| 4 | #include <string.h> |
| 5 | #include "syscall.h" |
| 6 | |
| 7 | char *getcwd(char *buf, size_t size) |
| 8 | { |
| 9 | char tmp[buf ? 1 : PATH_MAX]; |
| 10 | if (!buf) { |
| 11 | buf = tmp; |
| 12 | size = sizeof tmp; |
| 13 | } else if (!size) { |
| 14 | errno = EINVAL; |
| 15 | return 0; |
| 16 | } |
| 17 | long ret = syscall(SYS_getcwd, buf, size); |
| 18 | if (ret < 0) |
| 19 | return 0; |
| 20 | if (ret == 0 || buf[0] != '/') { |
| 21 | errno = ENOENT; |
| 22 | return 0; |
| 23 | } |
| 24 | return buf == tmp ? strdup(buf) : buf; |
| 25 | } |