From bfb74dd57aa0943934647bf56ea5ce7eb927136c Mon Sep 17 00:00:00 2001 From: Freywar Ulvnaudgari Date: Fri, 17 Jul 2026 20:30:35 +0300 Subject: [PATCH] Separate log and output streams --- src/kernel/fat16.c | 3 +- src/kernel/kernel.c | 10 ++++--- src/kernel/process.c | 9 +++--- src/lib/util.h | 4 +++ src/user/app/cat/cat.c | 9 ++---- src/user/app/cp/cp.c | 14 ++++----- src/user/app/echo/echo.c | 8 +++--- src/user/app/ls/ls.c | 22 +++++++------- src/user/app/mkdir/mkdir.c | 15 ++++------ src/user/app/rm/rm.c | 15 ++++------ src/user/app/shell/shell.c | 49 ++++++++++++++++++-------------- src/user/app/terminal/terminal.c | 33 ++++++++++----------- src/user/app/wc/wc.c | 9 ++---- src/user/syscall.h | 7 +++++ 14 files changed, 101 insertions(+), 106 deletions(-) diff --git a/src/kernel/fat16.c b/src/kernel/fat16.c index 1b14c54..c703cb7 100644 --- a/src/kernel/fat16.c +++ b/src/kernel/fat16.c @@ -713,9 +713,8 @@ static uint64_t directory_stream_read(stream_t *self, uint64_t max, char *to) { return 0; } uint64_t length = string_length(node->name); - uint64_t to_read = length + 1 > max ? max : length + 1; + uint64_t to_read = length > max ? max : length; memory_copy(node->name, to_read, to); - to[to_read - 1] = '\0'; return to_read; } diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index fcd48fb..ac90532 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -58,8 +58,10 @@ void kernel_main() { kernel->kernel_stack = kernel->kernel_rsp = (uint8_t *)memory_allocate(PAGE_SIZE) + PAGE_SIZE; kernel->user_stack = kernel->user_rsp = (uint8_t *)memory_allocate(PAGE_SIZE) + PAGE_SIZE; kernel->cwd = memory_allocate(sizeof(path_t)); - kernel->fds[kernel->free_fd++] = keyboard_init(); - kernel->fds[kernel->free_fd++] = vga_init(); + kernel->fds[STDIN] = keyboard_init(); + kernel->fds[STDOUT] = vga_init(); + kernel->fds[STDERR] = kernel->fds[STDOUT]; + kernel->free_fd = STDERR + 1; kernel->code = EXIT_CODE_OK; current_process = kernel; @@ -79,10 +81,10 @@ void kernel_main() { fs_read(shell_node, 0, shell_node->size, shell_code); fs_close(shell_node); - process_t *terminal = process_create(kernel, terminal_code, terminal_node->size, 0, 1); + process_t *terminal = process_create(kernel, terminal_code, terminal_node->size, STDIN, STDOUT); memory_free(terminal_code); - process_t *shell = process_create(kernel, shell_code, shell_node->size, 0, 1); + process_t *shell = process_create(kernel, shell_code, shell_node->size, STDIN, STDOUT); memory_free(shell_code); uint64_t *terminal_stack = (uint64_t *)terminal->user_stack; diff --git a/src/kernel/process.c b/src/kernel/process.c index f5cec1c..b37d2b0 100644 --- a/src/kernel/process.c +++ b/src/kernel/process.c @@ -75,9 +75,10 @@ process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t proc->pid = free_pid++; proc->state = PROCESS_RUNNING; proc->cwd = path_open_again(parent->cwd); - proc->fds[0] = parent->fds[stdin]; - proc->fds[1] = parent->fds[stdout]; - proc->free_fd = 2; + proc->fds[STDIN] = parent->fds[stdin]; + proc->fds[STDOUT] = parent->fds[stdout]; + proc->fds[STDERR] = parent->fds[STDERR]; + proc->free_fd = STDERR + 1; proc->code = EXIT_CODE_OK; return proc; @@ -129,7 +130,7 @@ void process_next() { } void process_destroy(process_t *proc) { - for (uint64_t i = 2; i < MAX_FDS; i++) { + for (uint64_t i = STDERR + 1; i < MAX_FDS; i++) { if (proc->fds[i]) { proc->fds[i]->close(proc->fds[i]); proc->fds[i] = NUL; diff --git a/src/lib/util.h b/src/lib/util.h index 9cffaf5..cfa4afb 100644 --- a/src/lib/util.h +++ b/src/lib/util.h @@ -4,6 +4,10 @@ #define NUL 0 // TODO Fix VSCode thinking `NULL` conflicts with some other definition. +#define STDIN 0 +#define STDOUT 1 +#define STDERR 2 + typedef uint64_t exit_code_t; #define EXIT_CODE_OK 0 diff --git a/src/user/app/cat/cat.c b/src/user/app/cat/cat.c index 407d848..b7820be 100644 --- a/src/user/app/cat/cat.c +++ b/src/user/app/cat/cat.c @@ -4,18 +4,15 @@ #define BLOCK_SIZE 65536 -#define PRINT_S(s) write(1, s, sizeof(s) - 1); -#define PRINT_D(s) write(1, s, string_length(s)); - exit_code_t pass(uint64_t fd) { static char buffer[BLOCK_SIZE]; uint64_t bytes; while ((bytes = read(fd, BLOCK_SIZE, buffer))) { if (bytes == (uint64_t)-1) { - PRINT_S("cat: could not read file\n"); + ERR_S("cat: could not read file\n"); return EXIT_CODE_GENERAL_FAILURE; } - write(1, buffer, bytes); + write(STDOUT, buffer, bytes); } return EXIT_CODE_OK; @@ -25,7 +22,7 @@ exit_code_t cat(const char *path) { uint64_t fd = open(path, OPEN_FILE); if (fd == (uint64_t)-1) { - PRINT_S("cat: path does not exist\n"); + ERR_S("cat: path does not exist\n"); return EXIT_CODE_GENERAL_FAILURE; } diff --git a/src/user/app/cp/cp.c b/src/user/app/cp/cp.c index 1258553..005ba61 100644 --- a/src/user/app/cp/cp.c +++ b/src/user/app/cp/cp.c @@ -4,29 +4,27 @@ #define BLOCK_SIZE 65536 -#define PRINT_S(s) write(1, s, sizeof(s) - 1); -#define PRINT_D(s) write(1, s, string_length(s)); - exit_code_t main(uint64_t argc, const char **argv) { if (argc != 3) { - PRINT_S("cp: requires source and target"); + ERR_S("cp: requires source and target\n"); return EXIT_CODE_GENERAL_FAILURE; } uint64_t source = open(argv[1], OPEN_FILE); if (source == (uint64_t)-1) { - PRINT_S("cp: source does not exist\n"); + ERR_S("cp: source does not exist\n"); return EXIT_CODE_GENERAL_FAILURE; } uint64_t target = open(argv[2], OPEN_FILE | OPEN_CREATE); if (target == (uint64_t)-1) { - PRINT_S("cp: target path does not exist\n"); + ERR_S("cp: target path does not exist\n"); close(source); return EXIT_CODE_GENERAL_FAILURE; } if (truncate(target, 0) == (uint64_t)-1) { + ERR_S("cp: could not write file\n"); close(source); close(target); return EXIT_CODE_GENERAL_FAILURE; @@ -36,13 +34,13 @@ exit_code_t main(uint64_t argc, const char **argv) { uint64_t bytes; while ((bytes = read(source, BLOCK_SIZE, buffer))) { if (bytes == (uint64_t)-1) { - PRINT_S("cp: could not read file\n"); + ERR_S("cp: could not read file\n"); close(source); close(target); return EXIT_CODE_GENERAL_FAILURE; } if (write(target, buffer, bytes) == (uint64_t)-1) { - PRINT_S("cp: could not write file\n"); + ERR_S("cp: could not write file\n"); close(source); close(target); return EXIT_CODE_GENERAL_FAILURE; diff --git a/src/user/app/echo/echo.c b/src/user/app/echo/echo.c index 8e06eb8..f5fadbb 100644 --- a/src/user/app/echo/echo.c +++ b/src/user/app/echo/echo.c @@ -1,14 +1,14 @@ -#include "src/lib/string.h" #include "src/lib/util.h" #include "src/user/syscall.h" exit_code_t main(uint64_t argc, const char **argv) { for (uint64_t i = 1; i < argc; i++) { if (i > 1) { - write(1, " ", 1); + OUT_S(" "); } - write(1, argv[i], string_length(argv[i])); + OUT_D(argv[i]); } - write(1, "\n", 1); + OUT_S("\n"); + return EXIT_CODE_OK; } diff --git a/src/user/app/ls/ls.c b/src/user/app/ls/ls.c index 5308a52..d434753 100644 --- a/src/user/app/ls/ls.c +++ b/src/user/app/ls/ls.c @@ -1,29 +1,27 @@ -#include "src/lib/string.h" #include "src/lib/syscall.h" #include "src/lib/util.h" #include "src/user/syscall.h" -#define PRINT_S(s) write(1, s, sizeof(s) - 1); -#define PRINT_D(s) write(1, s, string_length(s)); +#define BLOCK_SIZE 256 exit_code_t ls(const char *path) { uint64_t fd = open(path, OPEN_DIRECTORY); if (fd == (uint64_t)-1) { - PRINT_S("ls: path does not exist\n"); + ERR_S("ls: path does not exist\n"); return EXIT_CODE_GENERAL_FAILURE; } - char buffer[100]; + char buffer[BLOCK_SIZE]; uint64_t bytes; - while ((bytes = read(fd, 100, buffer))) { + while ((bytes = read(fd, BLOCK_SIZE, buffer))) { if (bytes == (uint64_t)-1) { - PRINT_S("ls: could not read directory\n"); + ERR_S("ls: could not read directory\n"); close(fd); return EXIT_CODE_GENERAL_FAILURE; } - write(1, buffer, bytes - 1); - PRINT_S("\n"); + write(STDOUT, buffer, bytes); + OUT_S("\n"); } close(fd); @@ -39,9 +37,9 @@ exit_code_t main(uint64_t argc, const char **argv) { exit_code_t code = EXIT_CODE_OK; for (uint64_t i = 1; i < argc; i++) { if (argc > 2) { - PRINT_S("\n"); - PRINT_D(argv[i]); - PRINT_S(":\n"); + OUT_S("\n"); + OUT_D(argv[i]); + OUT_S(":\n"); } exit_code_t c = ls(argv[i]); if (c != EXIT_CODE_OK) { diff --git a/src/user/app/mkdir/mkdir.c b/src/user/app/mkdir/mkdir.c index eb552d2..aa07a26 100644 --- a/src/user/app/mkdir/mkdir.c +++ b/src/user/app/mkdir/mkdir.c @@ -1,28 +1,23 @@ #include "src/lib/syscall.h" #include "src/lib/util.h" #include "src/user/syscall.h" -#include - -#define BLOCK_SIZE 65536 - -#define PRINT_S(s) write(1, s, sizeof(s) - 1); -#define PRINT_D(s) write(1, s, string_length(s)); exit_code_t main(uint64_t argc, const char **argv) { if (argc < 2) { - PRINT_S("mkdir: requires target(s)\n"); + ERR_S("mkdir: requires target(s)\n"); return EXIT_CODE_GENERAL_FAILURE; } + exit_code_t code = EXIT_CODE_OK; for (uint64_t i = 1; i < argc; i++) { uint64_t fd = open(argv[i], OPEN_DIRECTORY | OPEN_CREATE | OPEN_EXCLUSIVE); if (fd == (uint64_t)-1) { - PRINT_S("mkdir: could not create directory\n"); - return EXIT_CODE_GENERAL_FAILURE; + ERR_S("mkdir: could not create directory\n"); + code = EXIT_CODE_GENERAL_FAILURE; } else { close(fd); } } - return EXIT_CODE_OK; + return code; } diff --git a/src/user/app/rm/rm.c b/src/user/app/rm/rm.c index f954f33..b557e6f 100644 --- a/src/user/app/rm/rm.c +++ b/src/user/app/rm/rm.c @@ -2,23 +2,18 @@ #include "src/lib/util.h" #include "src/user/syscall.h" -#define BLOCK_SIZE 65536 - -#define PRINT_S(s) write(1, s, sizeof(s) - 1); -#define PRINT_D(s) write(1, s, string_length(s)); - exit_code_t main(uint64_t argc, const char **argv) { if (argc < 2) { - PRINT_S("rm: requires one or more arguments\n"); + ERR_S("rm: requires one or more arguments\n"); return EXIT_CODE_GENERAL_FAILURE; } uint64_t code = EXIT_CODE_OK; for (uint64_t i = 1; i < argc; i++) { - exit_code_t c = remove(argv[i]); - if (c != EXIT_CODE_OK) { - PRINT_S("rm: coult not remove file\n"); - code = c; + uint64_t removed = remove(argv[i]); + if (removed == (uint64_t)-1) { + ERR_S("rm: could not remove file\n"); + code = EXIT_CODE_GENERAL_FAILURE; } } diff --git a/src/user/app/shell/shell.c b/src/user/app/shell/shell.c index 693cbd4..9553799 100644 --- a/src/user/app/shell/shell.c +++ b/src/user/app/shell/shell.c @@ -3,15 +3,15 @@ #include "src/lib/util.h" #include "src/user/syscall.h" -static void help(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout); -static void cd(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout); +static exit_code_t help(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout); +static exit_code_t cd(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout); #define PATH "/bin/" -#define PRINT_S(o, s) write(o, s, sizeof(s) - 1); -#define PRINT_D(o, s) write(o, s, string_length(s)); +#define WRITE_S(o, s) write(o, s, sizeof(s) - 1); +#define WRITE_D(o, s) write(o, s, string_length(s)); -typedef void (*app_t)(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout); +typedef exit_code_t (*app_t)(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout); typedef struct { const char *name; @@ -23,28 +23,33 @@ static app_entry_t builtins[] = { {"cd", cd}, }; -static void help(uint8_t argc, __attribute__((unused)) char **argv, __attribute__((unused)) uint64_t stdin, uint64_t stdout) { +static exit_code_t help(uint8_t argc, __attribute__((unused)) char **argv, __attribute__((unused)) uint64_t stdin, uint64_t stdout) { if (argc > 1) { - PRINT_S(stdout, "help: expects no arguments\n"); - return; + ERR_S("help: expects no arguments\n"); + return EXIT_CODE_GENERAL_FAILURE; } - PRINT_S(stdout, "Available commands:\n"); + WRITE_S(stdout, "Available commands:\n"); for (uint64_t i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) { - PRINT_D(stdout, builtins[i].name); - PRINT_S(stdout, "\n"); + WRITE_D(stdout, builtins[i].name); + WRITE_S(stdout, "\n"); } + + return EXIT_CODE_OK; } -static void cd(uint8_t argc, char **argv, __attribute__((unused)) uint64_t stdin, uint64_t stdout) { +static exit_code_t cd(uint8_t argc, char **argv, __attribute__((unused)) uint64_t stdin, __attribute__((unused)) uint64_t stdout) { if (argc != 2) { - PRINT_S(stdout, "cd: requires a single path\n"); - return; + ERR_S("cd: requires a single path\n"); + return EXIT_CODE_GENERAL_FAILURE; } if (chdir(argv[1]) == (uint64_t)-1) { - PRINT_S(stdout, "cd: path does not exist\n"); + ERR_S("cd: path does not exist\n"); + return EXIT_CODE_GENERAL_FAILURE; } + + return EXIT_CODE_OK; } static void print_prompt() { @@ -53,9 +58,9 @@ static void print_prompt() { char *components[16]; uint8_t cl = string_split(path, '/', 16, components); - PRINT_S(1, "[") - PRINT_D(1, pl == 1 ? "/" : components[cl - 1]); - PRINT_S(1, "]$ "); + OUT_S("[") + OUT_D(pl == 1 ? "/" : components[cl - 1]); + OUT_S("]$ "); } static void free_fds(uint64_t *fds, uint8_t fds_count) { @@ -69,7 +74,7 @@ static void kill_pids(__attribute__((unused)) uint64_t *pids, __attribute__((unu } static void execute(char *command) { - PRINT_S(1, "\n"); + OUT_S("\n"); if (string_equal(command, "^C")) { print_prompt(); @@ -155,8 +160,8 @@ static void execute(char *command) { print_prompt(); } -uint64_t main() { - PRINT_D(1, "Welcome to FreywarOS v" VERSION "!\n\n"); +exit_code_t main() { + ERR_S("Welcome to FreywarOS v" VERSION "!\n\n"); print_prompt(); @@ -178,5 +183,5 @@ uint64_t main() { } } - return 0; + return EXIT_CODE_OK; } diff --git a/src/user/app/terminal/terminal.c b/src/user/app/terminal/terminal.c index b4d101b..6d841f7 100644 --- a/src/user/app/terminal/terminal.c +++ b/src/user/app/terminal/terminal.c @@ -2,9 +2,6 @@ #include "src/lib/util.h" #include "src/user/syscall.h" -#define PRINT_S(s) write(1, s, sizeof(s) - 1); -#define PRINT_D(s) write(1, s, string_length(s)); - #define PROMPT_LENGTH 255 static const char bs[PROMPT_LENGTH + 1]; @@ -19,7 +16,7 @@ static void on_home_pressed() { return; } - write(1, bs, prompt_offset); + write(STDOUT, bs, prompt_offset); prompt_offset = 0; } @@ -28,7 +25,7 @@ static void on_left_pressed() { return; } - write(1, bs, 1); + write(STDOUT, bs, 1); prompt_offset--; } @@ -37,7 +34,7 @@ static void on_right_pressed() { return; } - write(1, prompt + prompt_offset, 1); + write(STDOUT, prompt + prompt_offset, 1); prompt_offset++; } @@ -46,27 +43,27 @@ static void on_end_pressed() { return; } - write(1, prompt + prompt_offset, prompt_length - prompt_offset); + write(STDOUT, prompt + prompt_offset, prompt_length - prompt_offset); prompt_offset = prompt_length; } static void on_enter_pressed() { prompt[prompt_length] = '\n'; - write(2, prompt, prompt_length + 1); + write(3, prompt, prompt_length + 1); prompt_length = prompt_offset = 0; } static void on_cancel_pressed() { - PRINT_S("^C\n"); - write(2, "^C\n", 3); + OUT_S("^C\n"); + write(3, "^C\n", 3); prompt_length = prompt_offset = 0; } static void on_disconnect_pressed() { - PRINT_S("^D\n"); - write(2, "^D\n", 3); + OUT_S("^D\n"); + write(3, "^D\n", 3); exit(EXIT_CODE_OK); } @@ -80,9 +77,9 @@ static void on_ctrl_character_pressed(char c) { static void redraw_from_cursor() { uint64_t tail = prompt_length - prompt_offset; - write(1, prompt + prompt_offset, tail); - write(1, ws, 1); // erase the character past the end - write(1, bs, tail + 1); // move back to cursor position + write(STDOUT, prompt + prompt_offset, tail); + write(STDOUT, ws, 1); // erase the character past the end + write(STDOUT, bs, tail + 1); // move back to cursor position } static void on_character_pressed(char c) { @@ -96,7 +93,7 @@ static void on_character_pressed(char c) { prompt_length++; prompt_offset++; - write(1, &c, 1); // emit the character itself + write(STDOUT, &c, 1); // emit the character itself redraw_from_cursor(); } @@ -110,7 +107,7 @@ static void on_backspace_pressed() { prompt_offset--; prompt_length--; - write(1, bs, 1); + write(STDOUT, bs, 1); redraw_from_cursor(); } @@ -197,7 +194,7 @@ uint64_t main() { memory_set(' ', PROMPT_LENGTH, (char *)ws); char c; - while (read(0, 1, &c)) { + while (read(STDIN, 1, &c)) { on_char_received(c); } diff --git a/src/user/app/wc/wc.c b/src/user/app/wc/wc.c index f437f81..36f20af 100644 --- a/src/user/app/wc/wc.c +++ b/src/user/app/wc/wc.c @@ -5,16 +5,13 @@ #define BLOCK_SIZE 65536 -#define PRINT_S(s) write(1, s, sizeof(s) - 1); -#define PRINT_D(s) write(1, s, string_length(s)); - exit_code_t pass(uint64_t fd) { static char buffer[BLOCK_SIZE]; uint64_t bytes; uint64_t total = 0; while ((bytes = read(fd, BLOCK_SIZE, buffer))) { if (bytes == (uint64_t)-1) { - PRINT_S("wc: could not read file\n"); + ERR_S("wc: could not read file\n"); return EXIT_CODE_GENERAL_FAILURE; } else { total += bytes; @@ -22,7 +19,7 @@ exit_code_t pass(uint64_t fd) { } uint64_t l = string_format("%d\n", BLOCK_SIZE, buffer, total); - write(1, buffer, l); + write(STDOUT, buffer, l); return EXIT_CODE_OK; } @@ -31,7 +28,7 @@ exit_code_t wc(const char *path) { uint64_t fd = open(path, OPEN_FILE); if (fd == (uint64_t)-1) { - PRINT_S("wc: path does not exist\n"); + OUT_S("wc: path does not exist\n"); return EXIT_CODE_GENERAL_FAILURE; } diff --git a/src/user/syscall.h b/src/user/syscall.h index b96ea21..842ae18 100644 --- a/src/user/syscall.h +++ b/src/user/syscall.h @@ -1,6 +1,13 @@ #pragma once #include "src/lib/util.h" +#include "src/lib/string.h" + +#define OUT_S(s) write(STDOUT, s, sizeof(s) - 1); +#define OUT_D(s) write(STDOUT, s, string_length(s)); + +#define ERR_S(s) write(STDERR, s, sizeof(s) - 1); +#define ERR_D(s) write(STDERR, s, string_length(s)); uint64_t read(int64_t fd, uint64_t max, void *to);