diff options
author | dzwdz | 2022-05-01 18:35:18 +0200 |
---|---|---|
committer | dzwdz | 2022-05-01 18:35:18 +0200 |
commit | 935eccb23be9464091ea0f6be3873c6450ff040c (patch) | |
tree | 592be8e676b79254e891ad90a17cf83743ff51cf | |
parent | cad354b671cc7fa783a3fd0631a0f85c40b69a0c (diff) |
init/cat: read files until EOF
-rw-r--r-- | initrd/long.txt | 7 | ||||
-rw-r--r-- | src/init/shell.c | 19 |
2 files changed, 20 insertions, 6 deletions
diff --git a/initrd/long.txt b/initrd/long.txt new file mode 100644 index 0000000..ed0b66a --- /dev/null +++ b/initrd/long.txt @@ -0,0 +1,7 @@ +this file is longer than 512 characters. each line is exactly 80 characters long +01234567890123456789012345678901234567890123456789012345678901234567890123456789 +0 10 20 30 40 50 60 70 80 +for this file to be over 512 bytes, it needs 512/81 = 7 lines. that's a fun fact + ,_____O> [duck sound] | that's as good of a duck as i can make. it's very good +~|____| | /-- this X is the 513th byte in this file. very coolio + | | ,. . ., , ., | X. here it is. what you've waited for. idk what to say diff --git a/src/init/shell.c b/src/init/shell.c index 6314721..7714c38 100644 --- a/src/init/shell.c +++ b/src/init/shell.c @@ -48,6 +48,7 @@ static void cmd_cat_ls(const char *args, bool ls) { int fd; static char buf[512]; int len; // first used for strlen(args), then length of buffer + size_t pos = 0; if (!args) args = "/"; len = strlen(args); @@ -69,17 +70,23 @@ static void cmd_cat_ls(const char *args, bool ls) { return; } - len = _syscall_read(fd, buf, sizeof buf, 0); - if (ls) - for (int i = 0; i < len; i++) - if (buf[i] == '\0') buf[i] = '\n'; + while (true) { + len = _syscall_read(fd, buf, sizeof buf, pos); + if (len <= 0) break; + pos += len; + + if (ls) { + for (int i = 0; i < len; i++) + if (buf[i] == '\0') buf[i] = '\n'; + } + _syscall_write(__stdout, buf, len, 0); + } - _syscall_write(__stdout, buf, len, 0); _syscall_close(fd); } static void cmd_hexdump(const char *args) { - static uint8_t buf[513]; + static uint8_t buf[512]; int fd, len; fd = _syscall_open(args, strlen(args)); |