From f887d2f38b205237cbd03ce921aff7a79986e2ff Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sat, 11 Feb 2023 14:55:28 +0100 Subject: add mknod --- core/write.c | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) (limited to 'core/write.c') 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] \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] \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; } -- cgit v1.2.3-70-g09d2