diff pipes.cpp @ 2:00b62a080354

fix numerous bugs
author Yasuri <yasuri@charliesmp.net>
date Sun, 13 Sep 2026 19:40:11 +0000
parents 21b543c7aa14
children
line wrap: on
line diff
--- a/pipes.cpp	Sun Sep 13 19:25:50 2026 +0000
+++ b/pipes.cpp	Sun Sep 13 19:40:11 2026 +0000
@@ -2,11 +2,12 @@
 #include <unistd.h>
 #include <spawn.h>
 #include <cstring>
+#include <cerrno>
 #include <string>
 #include <sys/wait.h>
 #include "process.h"
 extern char** environ;
-std::string run_piped(const char* path, char* const argv[], const std::string& input) 
+std::string run_piped(char* const argv[], const std::string& input) 
 {
     int status;
     pid_t pid;
@@ -24,17 +25,28 @@
     posix_spawn_file_actions_addclose(&actions, pipeB[0]); // makes sure child doesn't read its own output pipe
     posix_spawn_file_actions_addclose(&actions, pipeA[0]); // close read fd in child
     posix_spawn_file_actions_addclose(&actions, pipeB[1]); // close read fd in child, part 2
-    status = posix_spawn(&pid, path, &actions, NULL, const_cast<char* const*>(argv), environ);
+    status = posix_spawnp(&pid, argv[0], &actions, NULL, const_cast<char* const*>(argv), environ);
     close(pipeA[0]);
     close(pipeB[1]);
     if (status != 0) {
         posix_spawn_file_actions_destroy(&actions);
         close(pipeA[1]);
         close(pipeB[0]);
-        std::cerr << "posix_spawn failed: " << strerror(status) << std::endl;
+        std::cerr << "posix_spawnp failed for " << argv[0] << ": " << strerror(status) << std::endl;
         return "error";
     }
-    write(pipeA[1],input.c_str(), input.size());
+    size_t total_written = 0;
+    while (total_written < input.size()) {
+        ssize_t n = write(pipeA[1], input.c_str() + total_written, input.size() - total_written);
+        if (n < 0) {
+            if (errno == EINTR) {
+                continue; // interrupted
+            }
+            perror("write");
+            break; // error, give up
+        }
+        total_written += n;
+    }
     close(pipeA[1]);
     char buf[256];
 std::string result;