From 58eed4a5791cc99859a2591a59f3e0de0a2dc1ab Mon Sep 17 00:00:00 2001
From: dzwdz
Date: Thu, 7 Sep 2023 01:31:09 +0200
Subject: user: fix stdio stream pos; remove the shell pipe redir workaround

---
 src/cmd/shell/shell.c | 26 ++------------------------
 src/libc/stdio/file.c |  4 ++--
 2 files changed, 4 insertions(+), 26 deletions(-)

diff --git a/src/cmd/shell/shell.c b/src/cmd/shell/shell.c
index d87527c..b5ad907 100644
--- a/src/cmd/shell/shell.c
+++ b/src/cmd/shell/shell.c
@@ -101,30 +101,8 @@ void run_args(int argc, char **argv, struct redir *redir) {
 			if (!f) {
 				err(1, "couldn't open %s for redirection", redir->stdout);
 			}
-
-			/* a workaround for file offsets not being preserved across exec()s.
-			 * TODO document that weird behaviour of exec() */
-			hid_t p[2];
-			if (_sys_pipe(p, 0) < 0) {
-				errx(1, "couldn't create redirection pipe");
-			}
-			if (!_sys_fork(FORK_NOREAP, NULL)) {
-				/* the child forwards data from the pipe to the file */
-				const size_t buflen = 512;
-				char *buf = malloc(buflen);
-				if (!buf) err(1, "when redirecting");
-				close(p[1]);
-				for (;;) {
-					long len = _sys_read(p[0], buf, buflen, 0);
-					if (len <= 0) exit(0);
-					fwrite(buf, 1, len, f);
-					if (ferror(f)) exit(0);
-				}
-			}
-
-			fclose(f);
-			close(p[0]);
-			if (_sys_dup(p[1], 1, 0) < 0) {
+			// TODO fileno error checking
+			if (_sys_dup(fileno(f), 1, 0) < 0) {
 				errx(1, "dup() failed when redirecting");
 			}
 		}
diff --git a/src/libc/stdio/file.c b/src/libc/stdio/file.c
index 30a578e..d8b7d5a 100644
--- a/src/libc/stdio/file.c
+++ b/src/libc/stdio/file.c
@@ -10,8 +10,8 @@
 #include <unistd.h>
 
 static FILE _stdin_null  = { .fd = STDIN_FILENO };
-static FILE _stdout_null = { .fd = STDOUT_FILENO };
-static FILE _stderr_null = { .fd = STDERR_FILENO };
+static FILE _stdout_null = { .fd = STDOUT_FILENO, .pos = -1 };
+static FILE _stderr_null = { .fd = STDERR_FILENO, .pos = -1 };
 FILE *const stdin = &_stdin_null;
 FILE *const stdout = &_stdout_null;
 FILE *const stderr = &_stderr_null;
-- 
cgit v1.2.3