aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2023-02-11 14:55:28 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2023-02-11 14:55:28 +0100
commitf887d2f38b205237cbd03ce921aff7a79986e2ff (patch)
tree56c55cfd55815a70814793d37c4873974e693703 /core
parenta121299c2ad417b238b7c20d30f4cf064e3700fd (diff)
add mknod
Diffstat (limited to 'core')
-rw-r--r--core/new.c30
-rw-r--r--core/write.c42
2 files changed, 57 insertions, 15 deletions
diff --git a/core/new.c b/core/new.c
index 5c0e7b8..9fab46b 100644
--- a/core/new.c
+++ b/core/new.c
@@ -2,25 +2,46 @@
#include "../lib/sys/mkdir.h"
#include "../lib/sys/creat.h"
#include "../lib/cstr/cstr.h"
+#include "../lib/sys/mknod.h"
+#include "../lib/sys/errno.h"
+#include "../lib/sys/stat.h"
enum {
FILE,
DIRECTORY,
+ BLOCK_OR_CHARACTER,
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 new_block_or_character(int argc, char **argv)
+{
+ if (argc != 4) {
+ wff(STDERR_FD, "new -n <name> <type> <major> <major>");
+ return -1;
+ }
+
+ u32 major = cstr_to_u64(argv[2]);
+ u32 minor = cstr_to_u64(argv[3]);
+ u32 type = argv[1][0] == 'c' ? S_IFCHR : S_IFBLK;
+ int err;
+
+ if ((err = mknod(argv[0], MODE_USER_READ | MODE_USER_WRITE | type, device(major, minor))) < 0) {
+ wff(STDERR_FD, "%s\n", errstr[-err]);
+ return -1;
+ }
+
+ return 0;
+}
+
int main(int argc, char **argv)
{
if (argc < 3) {
- wff(STDERR_FD, "new [-f | -d] <name> ...");
+ wff(STDERR_FD, "new [[-f | -d] <name> ... | -n <name> <type> <major> <minor>]\n");
return -1;
}
@@ -34,6 +55,7 @@ int main(int argc, char **argv)
switch(argv[1][1]) {
case 'f': type = FILE; break;
case 'd': type = DIRECTORY; break;
+ case 'n': return new_block_or_character(argc - 2, argv + 2);
default: wff(STDERR_FD, "error: unknown entry type.\n"); return -1;
}
diff --git a/core/write.c b/core/write.c
index a0be81c..005f76d 100644
--- a/core/write.c
+++ b/core/write.c
@@ -1,30 +1,50 @@
#include "../lib/io/io.h"
#include "../lib/container/container.h"
+#include "../lib/cstr/cstr.h"
+#include "../lib/tctl/tctl.h"
#define BUFFER_SIZE 128
char buf[BUFFER_SIZE];
int main(int argc, const char **argv)
{
- if (argc != 2) {
- wstd("write [filename]\n");
+ u64 size;
+
+ if (argc > 3) {
+ wf(STDERR_FD, "write [-nobuf] <filename>\n");
return -1;
}
- int fd = open(argv[1], OPEN_WRITE_ONLY | OPEN_CREATE, MODE_USER_READ | MODE_USER_WRITE | MODE_OTHER_READ | MODE_GROUP_READ);
+ if (argc == 3) {
+ if (cstr_compare(argv[1], "-nobuf") == 0) {
- container_t *string = new_container(sizeof(char));
- u64 size;
+ int fd = open(argv[2], OPEN_WRITE_ONLY | OPEN_CREATE, MODE_USER_READ | MODE_USER_WRITE | MODE_OTHER_READ | MODE_GROUP_READ);
- while ((size = read(STDIN_FD, buf, BUFFER_SIZE))) {
- container_append(string, buf, size);
- }
+ while ((size = read(STDIN_FD, buf, BUFFER_SIZE))) {
+ write(fd, buf, BUFFER_SIZE);
+ }
+
+ close(fd);
+ } else {
+ wstd("write [-nobuf] <filename>\n");
+ return -1;
+ }
+ } else {
+ int fd = open(argv[1], OPEN_WRITE_ONLY | OPEN_CREATE, MODE_USER_READ | MODE_USER_WRITE | MODE_OTHER_READ | MODE_GROUP_READ);
- container_push(string, 0);
+ container_t *string = new_container(sizeof(char));
- write(fd, string->data, string->size);
+ while ((size = read(STDIN_FD, buf, BUFFER_SIZE))) {
+ container_append(string, buf, size);
+ }
- close(fd);
+
+ container_push(string, 0);
+
+ write(fd, string->data, string->size);
+
+ close(fd);
+ }
return 0;
}