From 6c01d9a7e34e1fccc2775b0e2187ac5e50dd4392 Mon Sep 17 00:00:00 2001
From: dzwdz
Date: Mon, 11 Jul 2022 20:53:03 +0200
Subject: init: file_reopen, keep stdin/stdout on their standard fds

---
 src/init/main.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

(limited to 'src/init/main.c')

diff --git a/src/init/main.c b/src/init/main.c
index 1674f8c..88553e9 100644
--- a/src/init/main.c
+++ b/src/init/main.c
@@ -19,7 +19,7 @@ int main(void) {
 	// allocate bss
 	_syscall_memflag(&_bss_start, &_bss_end - &_bss_start, MEMFLAG_PRESENT);
 
-	stdout = file_open("/com1", 0);
+	file_reopen(stdout, "/com1", 0);
 	printf("preinit\n");
 
 	/* move everything provided by the kernel to /kdev */
@@ -55,12 +55,14 @@ int main(void) {
 	}
 
 	if (!_syscall_fork(0, NULL)) {
-		libc_file *new = file_open("/kdev/com1", 0);
-		if (!new) {
+		if (!file_reopen(stdout, "/kdev/com1", 0)) {
+			printf("couldn't open /kdev/com1\n"); // TODO borked
+			_syscall_exit(1);
+		}
+		if (!file_reopen(stdin, "/kdev/com1", 0)) {
 			printf("couldn't open /kdev/com1\n");
 			_syscall_exit(1);
 		}
-		stdout = stdin = new; // TODO file_clone, this is bad
 
 		shell_loop();
 		_syscall_exit(1);
@@ -68,17 +70,11 @@ int main(void) {
 
 
 	if (!_syscall_fork(0, NULL)) {
-		libc_file *new;
-		new = file_open("/vga_tty", 0);
-		if (!new) {
-			printf("couldn't open /vga_tty\n");
+		if (!file_reopen(stdout, "/vga_tty", 0)) {
+			printf("couldn't open /vga_tty\n"); // TODO borked
 			_syscall_exit(1);
 		}
-		file_close(stdout);
-		stdout = new;
-
-		stdin = file_open("/keyboard", 0);
-		if (!stdin) {
+		if (!file_reopen(stdin, "/keyboard", 0)) {
 			printf("couldn't open /keyboard\n");
 			_syscall_exit(1);
 		}
-- 
cgit v1.2.3