summaryrefslogtreecommitdiffstats
path: root/libc-top-half/musl/src/unistd/getcwd.c
diff options
context:
space:
mode:
Diffstat (limited to 'libc-top-half/musl/src/unistd/getcwd.c')
-rw-r--r--libc-top-half/musl/src/unistd/getcwd.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/libc-top-half/musl/src/unistd/getcwd.c b/libc-top-half/musl/src/unistd/getcwd.c
new file mode 100644
index 0000000..f407ffe
--- /dev/null
+++ b/libc-top-half/musl/src/unistd/getcwd.c
@@ -0,0 +1,25 @@
+#include <unistd.h>
+#include <errno.h>
+#include <limits.h>
+#include <string.h>
+#include "syscall.h"
+
+char *getcwd(char *buf, size_t size)
+{
+ 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;
+}