From a60b4c57078e17d5ecabf1ac92faa92ca5aab1c3 Mon Sep 17 00:00:00 2001
From: jl4589 <jl4589@drexel.edu>
Date: Tue, 4 Mar 2025 17:06:00 -0500
Subject: [PATCH] Cmd loop had a problem I think

---
 Assignment-05/starter/dshlib.c | 122 ++++++++++++++-------------------
 1 file changed, 52 insertions(+), 70 deletions(-)

diff --git a/Assignment-05/starter/dshlib.c b/Assignment-05/starter/dshlib.c
index 30a4cbd..f02f23a 100644
--- a/Assignment-05/starter/dshlib.c
+++ b/Assignment-05/starter/dshlib.c
@@ -174,91 +174,73 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) {
     return OK;
 }
 
-int exec_local_cmd_loop() {
-    char cmd_buff[SH_CMD_MAX];
-    int rc = OK;
-
-    while (1) {
-        printf("%s", SH_PROMPT);
+int execute_pipeline(command_list_t *cmd_list) {
+    int num_commands = cmd_list->num;
+    int pipefd[2];
+    int prev_pipe_read = -1;
+    pid_t pids[num_commands];
 
-        if (fgets(cmd_buff, SH_CMD_MAX, stdin) == NULL) {
-            printf("\n");
-            break; 
+    for (int i = 0; i < num_commands; i++) {
+        if (i < num_commands - 1) {
+            if (pipe(pipefd) == -1) {
+                perror("pipe");
+                return ERR_MEMORY;
+            }
         }
 
-        cmd_buff[strcspn(cmd_buff, "\n")] = '\0';
-
-        if (strlen(cmd_buff) == 0) {
-            printf("%s\n", CMD_WARN_NO_CMD);
-            rc = WARN_NO_CMDS;
-            continue;
+        pids[i] = fork();
+        if (pids[i] == -1) {
+            perror("fork");
+            return ERR_MEMORY;
         }
 
-        if (strstr(cmd_buff, "|") != NULL) {
-            command_list_t cmd_list;
-            if (parse_pipeline(cmd_buff, &cmd_list) != OK) {
-                fprintf(stderr, "%s\n", CMD_ERR_PIPE_LIMIT);
-                rc = ERR_TOO_MANY_COMMANDS;
-                continue;
-            }
-
-            if (execute_pipeline(&cmd_list) != OK) {
-                fprintf(stderr, "%s\n", CMD_ERR_PIPE_LIMIT);
-                rc = ERR_MEMORY;
+        if (pids[i] == 0) { 
+            if (prev_pipe_read != -1) {
+                dup2(prev_pipe_read, STDIN_FILENO);
+                close(prev_pipe_read);
             }
 
-            for (int i = 0; i < cmd_list.num; i++) {
-                free(cmd_list.commands[i]._cmd_buffer);
-            }
-        } else {
-            cmd_buff_t cmd;
-            if (build_cmd_buff(cmd_buff, &cmd) != OK) {
-                fprintf(stderr, "%s\n", CMD_ERR_PIPE_LIMIT);
-                rc = ERR_MEMORY;
-                continue;
+            if (i < num_commands - 1) {
+                dup2(pipefd[1], STDOUT_FILENO);
+                close(pipefd[1]);
+                close(pipefd[0]);
             }
 
-            if (strcmp(cmd.argv[0], EXIT_CMD) == 0) {
-                free(cmd._cmd_buffer); 
-                break; 
+            cmd_buff_t *cmd = &cmd_list->commands[i];
+            execvp(cmd->argv[0], cmd->argv);
+            perror("execvp");
+            exit(EXIT_FAILURE);
+        } else { 
+            if (prev_pipe_read != -1) {
+                close(prev_pipe_read);
             }
 
-            if (strcmp(cmd.argv[0], "cd") == 0) {
-                if (cmd.argc == 1) {
-                    chdir(getenv("HOME"));
-                } else if (cmd.argc == 2) {
-                    if (chdir(cmd.argv[1]) != 0) {
-                        perror("cd");
-                    }
-                } else {
-                    fprintf(stderr, "%s\n", CMD_ERR_PIPE_LIMIT);
-                }
-                free(cmd._cmd_buffer);
-                continue;
+            if (i < num_commands - 1) {
+                prev_pipe_read = pipefd[0];
+                close(pipefd[1]);
             }
+        }
+    }
 
-            pid_t pid = fork();
-            if (pid < 0) {
-                perror("fork failed");
-                rc = ERR_MEMORY;
-            } else if (pid == 0) {
-                execvp(cmd.argv[0], cmd.argv);
-                perror("execvp failed");
-                exit(EXIT_FAILURE); 
-            } else {
-                int status;
-                wait(&status);
-
-                if (WIFEXITED(status)) {
-                    if (WEXITSTATUS(status) != 0) {
-                        fprintf(stderr, "Command failed with exit code %d\n", WEXITSTATUS(status));
-                    }
-                }
+    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;
             }
-
-            free(cmd._cmd_buffer);
+            buffer[bytes_read] = '\0';
+            printf("%s", buffer); 
         }
+        close(prev_pipe_read);
     }
 
-    return rc;
+    
+    for (int i = 0; i < num_commands; i++) {
+        waitpid(pids[i], NULL, 0);
+    }
+
+    return OK;
 }
+
-- 
GitLab