From 9df3708a1afbc944281e8dd7909a46aa6b73872f Mon Sep 17 00:00:00 2001
From: Ron Williams <ron.williams.redox@gmail.com>
Date: Thu, 26 Dec 2024 17:13:47 -0800
Subject: [PATCH] fix popen, improve tests

---
 src/header/stdio/mod.rs                       | 10 ++++++++--
 tests/expected/bins_static/stdio/popen.stdout | 16 ++++++++--------
 tests/run_tests.sh                            |  2 +-
 tests/stdio/popen.c                           |  4 +++-
 tests/unistd/dup.c                            |  7 +++++--
 5 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs
index 22bd933b4..dd55d1f5b 100644
--- a/src/header/stdio/mod.rs
+++ b/src/header/stdio/mod.rs
@@ -917,9 +917,15 @@ pub unsafe extern "C" fn popen(command: *const c_char, mode: *const c_char) -> *
         //TODO: dup errors are ignored, should they be?
         {
             if write {
-                unistd::dup2(0, pipes[0]);
+                match unistd::dup2(pipes[0], 0) {
+                    0 => {}
+                    e => stdlib::exit(127),
+                }
             } else {
-                unistd::dup2(1, pipes[1]);
+                match unistd::dup2(pipes[1], 1) {
+                    1 => {}
+                    e => stdlib::exit(127),
+                }
             }
 
             unistd::close(pipes[0]);
diff --git a/tests/expected/bins_static/stdio/popen.stdout b/tests/expected/bins_static/stdio/popen.stdout
index 988b6f4a4..05f7216cb 100644
--- a/tests/expected/bins_static/stdio/popen.stdout
+++ b/tests/expected/bins_static/stdio/popen.stdout
@@ -1,8 +1,8 @@
-1-never-gonna-give-you-up
-2-never-gonna-let-you-down
-3-never-gonna-run-around
-4-and-desert-you
-5-never-gonna-make-you-cry
-6-never-gonna-say-goodbye
-7-never-gonna-tell-a-lie
-8-and-hurt-you
+Line 1: 1-never-gonna-give-you-up
+Line 2: 2-never-gonna-let-you-down
+Line 3: 3-never-gonna-run-around
+Line 4: 4-and-desert-you
+Line 5: 5-never-gonna-make-you-cry
+Line 6: 6-never-gonna-say-goodbye
+Line 7: 7-never-gonna-tell-a-lie
+Line 8: 8-and-hurt-you
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index eb93eb58b..c836ed566 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -1,4 +1,4 @@
-#!/usr/bin/env bash
+#!/usr/bin/bash
 
 # Binaries that should generate the same output every time
 EXPECT_NAMES=(\
diff --git a/tests/stdio/popen.c b/tests/stdio/popen.c
index 56a2ab104..0b9cc519b 100644
--- a/tests/stdio/popen.c
+++ b/tests/stdio/popen.c
@@ -7,9 +7,11 @@ int main(void) {
     FILE *fp = popen("ls -1 example_dir", "r");
     ERROR_IF(popen, fp, == NULL);
 
+    int lineno = 0;
     char path[256] = { 0 };
     while (fgets(path, 256, fp) != NULL) {
-        printf("%s", path);
+        lineno++;
+        printf("Line %d: %s", lineno, path);
     }
 
     int status = pclose(fp);
diff --git a/tests/unistd/dup.c b/tests/unistd/dup.c
index af1fb1f14..cb7449e57 100644
--- a/tests/unistd/dup.c
+++ b/tests/unistd/dup.c
@@ -27,7 +27,8 @@ int main(void) {
     ERROR_IF(close, c2, == -1);
     UNEXP_IF(close, c2, != 0);
 
-    int fd3 = open("dup.out", 0x0002, 0x1000);
+    int fd3 = open("dup.out", O_RDWR);
+
     ERROR_IF(open, fd3, == -1);
     UNEXP_IF(open, fd3, < 0);
 
@@ -35,7 +36,9 @@ int main(void) {
     ERROR_IF(dup2, fd4, == -1);
     UNEXP_IF(dup2, fd4, < 0);
 
-    printf("hello fd %d", fd3);
+    int p = printf("hello fd %d", fd3);
+    ERROR_IF(printf, p, == -1);
+    UNEXP_IF(printf, p, < 0);
 
     int c3 = close(fd3);
     ERROR_IF(close, c3, == -1);
-- 
GitLab