diff options
author | dzwdz | 2022-09-03 23:12:31 +0200 |
---|---|---|
committer | dzwdz | 2022-09-03 23:12:31 +0200 |
commit | ecc54f4be44fa1fd1ce79b0458a04eef2667cba8 (patch) | |
tree | 23a0ab00c8231f454915fc1f248a78a0044f3eb4 /src/user/lib/sysstat.c | |
parent | 1a276eef00057cb3171e9a63a674f07e840624b8 (diff) |
user: implement a basic mkdir
Diffstat (limited to 'src/user/lib/sysstat.c')
-rw-r--r-- | src/user/lib/sysstat.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/user/lib/sysstat.c b/src/user/lib/sysstat.c new file mode 100644 index 0000000..df192c5 --- /dev/null +++ b/src/user/lib/sysstat.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> + +int mkdir(const char *path, mode_t mode) { + // TODO error when directory already exits + // TODO fopen-like wrapper that calls open() with a processed path + (void)mode; + size_t plen = strlen(path); + char *tmp = NULL; + /* ensure trailing slash */ + if (plen >= 1 && path[plen - 1] != '/') { + tmp = malloc(plen + 2); + memcpy(tmp, path, plen); + tmp[plen] = '/'; + tmp[plen + 1] = '\0'; + path = tmp; + } + FILE *f = fopen(path, "a"); /* sets errno */ + if (f) fclose(f); + free(tmp); + return f == NULL ? -1 : 0; +} |