aboutsummaryrefslogtreecommitdiff
path: root/core/new.c
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2023-02-10 15:26:47 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2023-02-10 15:26:47 +0100
commit1a8731e797f030a84bb982007ee4477b506f7bd6 (patch)
tree3e31f3b6e791f58cc79454d35e6f940439c54725 /core/new.c
parent6d42a1476ab5dd2893fd5ab8de4b473be120e796 (diff)
add 'new'
Diffstat (limited to 'core/new.c')
-rw-r--r--core/new.c45
1 files changed, 45 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;
+}