diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-02-10 15:26:47 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-02-10 15:26:47 +0100 |
| commit | 1a8731e797f030a84bb982007ee4477b506f7bd6 (patch) | |
| tree | 3e31f3b6e791f58cc79454d35e6f940439c54725 | |
| parent | 6d42a1476ab5dd2893fd5ab8de4b473be120e796 (diff) | |
add 'new'
| -rw-r--r-- | core/new.c | 45 | ||||
| -rw-r--r-- | lib/sys/creat.h | 11 | ||||
| -rw-r--r-- | lib/sys/mkdir.h | 11 |
3 files changed, 67 insertions, 0 deletions
diff --git a/core/new.c b/core/new.c new file mode 100644 index 0000000..5c0e7b8 --- /dev/null +++ b/core/new.c @@ -0,0 +1,45 @@ +#include "../lib/io/io.h" +#include "../lib/sys/mkdir.h" +#include "../lib/sys/creat.h" +#include "../lib/cstr/cstr.h" + +enum { + FILE, + DIRECTORY, + NUMBER_OF_MODES, +}; + +void new_file(char *path); +void new_directory(char *path); + +int(*new_funcs[NUMBER_OF_MODES])(const char *, unsigned int) = { + &creat, + &mkdir +}; + +int main(int argc, char **argv) +{ + if (argc < 3) { + wff(STDERR_FD, "new [-f | -d] <name> ..."); + return -1; + } + + int type = FILE; + + if (cstr_length(argv[1]) != 2) { + wff(STDERR_FD, "error: unknown entry type.\n"); + return -1; + } + + switch(argv[1][1]) { + case 'f': type = FILE; break; + case 'd': type = DIRECTORY; break; + default: wff(STDERR_FD, "error: unknown entry type.\n"); return -1; + } + + for (int i = 2; i < argc; ++i) { + new_funcs[type](argv[i], MODE_USER_READ | MODE_USER_WRITE); + } + + return 0; +} diff --git a/lib/sys/creat.h b/lib/sys/creat.h new file mode 100644 index 0000000..9c6e492 --- /dev/null +++ b/lib/sys/creat.h @@ -0,0 +1,11 @@ +#ifndef CREAT_H +#define CREAT_H + +#include "syscalls.h" + +int creat(const char *pathname, unsigned int mode) +{ + return syscall(CREAT, pathname, mode); +} + +#endif diff --git a/lib/sys/mkdir.h b/lib/sys/mkdir.h new file mode 100644 index 0000000..2de3b01 --- /dev/null +++ b/lib/sys/mkdir.h @@ -0,0 +1,11 @@ +#ifndef MKDIR_H +#define MKDIR_H + +#include "syscalls.h" + +int mkdir(const char *pathname, unsigned int mode) +{ + return syscall(MKDIR, pathname, mode); +} + +#endif |