From 020ce659a5214e16ac6f5c8261650a6890938cd3 Mon Sep 17 00:00:00 2001
From: jl4589 <jl4589@drexel.edu>
Date: Tue, 4 Mar 2025 16:59:52 -0500
Subject: [PATCH] Hopefully execut pipeline works correctly now

---
 Assignment-05/starter/dshlib.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/Assignment-05/starter/dshlib.c b/Assignment-05/starter/dshlib.c
index d07fc55..619876d 100644
--- a/Assignment-05/starter/dshlib.c
+++ b/Assignment-05/starter/dshlib.c
@@ -73,7 +73,7 @@ int execute_pipeline(command_list_t *cmd_list) {
             return ERR_MEMORY;
         }
 
-        if (pids[i] == 0) { // Child process
+        if (pids[i] == 0) { 
             if (prev_pipe_read != -1) {
                 dup2(prev_pipe_read, STDIN_FILENO);
                 close(prev_pipe_read);
@@ -86,7 +86,7 @@ int execute_pipeline(command_list_t *cmd_list) {
             }
 
             cmd_buff_t *cmd = &cmd_list->commands[i];
-            execvp(cmd->argv[0], cmd->argv); 
+            execvp(cmd->argv[0], cmd->argv);
             perror("execvp");
             exit(EXIT_FAILURE);
         } else { 
@@ -101,6 +101,20 @@ int execute_pipeline(command_list_t *cmd_list) {
         }
     }
 
+    if (prev_pipe_read != -1) {
+        char buffer[1024];
+        ssize_t bytes_read;
+        while ((bytes_read = read(prev_pipe_read, buffer, sizeof(buffer) - 1)) {
+            if (bytes_read < 0) {
+                perror("read");
+                break;
+            }
+            buffer[bytes_read] = '\0';
+            printf("%s", buffer); 
+        }
+        close(prev_pipe_read);
+    }
+
     for (int i = 0; i < num_commands; i++) {
         waitpid(pids[i], NULL, 0);
     }
@@ -108,6 +122,7 @@ int execute_pipeline(command_list_t *cmd_list) {
     return OK;
 }
 
+
 int parse_pipeline(char *cmd_line, command_list_t *clist) {
     if (cmd_line == NULL || clist == NULL) {
         return ERR_CMD_OR_ARGS_TOO_BIG;
-- 
GitLab