aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/include/unistd.h10
-rw-r--r--src/lib/libkogata/Makefile2
-rw-r--r--src/lib/libkogata/unistd.c67
3 files changed, 78 insertions, 1 deletions
diff --git a/src/lib/include/unistd.h b/src/lib/include/unistd.h
new file mode 100644
index 0000000..5080d6c
--- /dev/null
+++ b/src/lib/include/unistd.h
@@ -0,0 +1,10 @@
+#pragma once
+#include <stddef.h>
+
+int chdir(const char* path);
+
+char* getcwd(char* buf, size_t buf_len);
+
+char* pathncat(char* buf, const char* add, size_t buf_len); // path simplification
+
+/* vim: set ts=4 sw=4 tw=0 noet :*/
diff --git a/src/lib/libkogata/Makefile b/src/lib/libkogata/Makefile
index 20c6dee..95de086 100644
--- a/src/lib/libkogata/Makefile
+++ b/src/lib/libkogata/Makefile
@@ -1,6 +1,6 @@
OBJ = start.o malloc.o debug.o syscall.o user_region.o \
mainloop.o gip.o draw.o keyboard.o \
- stdio.o
+ stdio.o unistd.o
LIB = ../../common/libkogata/libkogata.lib ../../common/libalgo/libalgo.lib ../../common/libc/libc.lib
diff --git a/src/lib/libkogata/unistd.c b/src/lib/libkogata/unistd.c
new file mode 100644
index 0000000..80a32e4
--- /dev/null
+++ b/src/lib/libkogata/unistd.c
@@ -0,0 +1,67 @@
+#include <string.h>
+
+#include <syscall.h>
+
+#include <unistd.h>
+
+
+char cwd_buf[256];
+
+char* getcwd(char* buf, size_t buf_len) {
+ if (buf_len > strlen(cwd_buf)) {
+ strcpy(buf, cwd_buf);
+ return buf;
+ } else {
+ return 0;
+ }
+}
+
+int chdir(const char* path) {
+ char cwd_buf2[256];
+ strcpy(cwd_buf2, cwd_buf);
+
+ if (!pathncat(cwd_buf2, path, 256)) return -1;
+
+ stat_t st;
+ if (!stat(cwd_buf2, &st)) return -1;
+ if (!st.type & FT_DIR) return -1;
+
+ strcpy(cwd_buf, cwd_buf2);
+ return 0;
+}
+
+char* pathncat(char* buf, const char* add, size_t buf_len) {
+ if (strchr(add, ':')) {
+ if (strlen(add) < buf_len) {
+ strcpy(buf, add);
+ return buf;
+ } else {
+ return 0;
+ }
+ } else {
+ char* sep_init = strchr(buf, ':');
+ if (add[0] == '/') {
+ if (strlen(add) + (sep_init + 1 - buf) < buf_len) {
+ strcpy(sep_init + 1, add);
+ return buf;
+ } else {
+ return 0;
+ }
+ } else {
+ //TODO: simplify '..'
+ char* end = buf + strlen(buf) - 1;
+ if (*end != '/') end++;
+ if (end + 1 - buf + strlen(add) < buf_len) {
+ *end = '/';
+ strcpy(end + 1, add);
+ return buf;
+ } else {
+ return 0;
+ }
+ }
+ return 0;
+ }
+}
+
+
+/* vim: set ts=4 sw=4 tw=0 noet :*/