diff options
author | Alex Auvolat <alex@adnab.me> | 2016-07-15 23:12:14 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2016-07-15 23:12:14 +0200 |
commit | 32407e728971006ed3d0885e01c22fb66c8adc57 (patch) | |
tree | 89483d39e8e2638383f815d4e73b647334fe2fe9 /src/lib/libc/unistd.c | |
parent | ba4e59a1d687173ac5cfa74d26d71d6059dc6bc6 (diff) | |
download | kogata-32407e728971006ed3d0885e01c22fb66c8adc57.tar.gz kogata-32407e728971006ed3d0885e01c22fb66c8adc57.zip |
Move stuff around, again
Diffstat (limited to 'src/lib/libc/unistd.c')
-rw-r--r-- | src/lib/libc/unistd.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/lib/libc/unistd.c b/src/lib/libc/unistd.c new file mode 100644 index 0000000..5ae1735 --- /dev/null +++ b/src/lib/libc/unistd.c @@ -0,0 +1,66 @@ +#include <string.h> +#include <unistd.h> + +#include <kogata/syscall.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 :*/ |