use libc-internal malloc for pthread_atfork
[musl] / src / unistd / getcwd.c
index b64b560..f407ffe 100644 (file)
@@ -1,8 +1,25 @@
 #include <unistd.h>
 #include <errno.h>
+#include <limits.h>
+#include <string.h>
 #include "syscall.h"
 
 char *getcwd(char *buf, size_t size)
 {
-       return syscall(SYS_getcwd, buf, size) < 0 ? NULL : buf;
+       char tmp[buf ? 1 : PATH_MAX];
+       if (!buf) {
+               buf = tmp;
+               size = sizeof tmp;
+       } else if (!size) {
+               errno = EINVAL;
+               return 0;
+       }
+       long ret = syscall(SYS_getcwd, buf, size);
+       if (ret < 0)
+               return 0;
+       if (ret == 0 || buf[0] != '/') {
+               errno = ENOENT;
+               return 0;
+       }
+       return buf == tmp ? strdup(buf) : buf;
 }