Skip to content
Snippets Groups Projects
Commit a60b4c57 authored by Joey Le's avatar Joey Le
Browse files

Cmd loop had a problem I think

parent 408575e6
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
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;
pids[i] = fork();
if (pids[i] == -1) {
perror("fork");
return ERR_MEMORY;
}
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");
if (i < num_commands - 1) {
prev_pipe_read = pipefd[0];
close(pipefd[1]);
}
} else {
fprintf(stderr, "%s\n", CMD_ERR_PIPE_LIMIT);
}
free(cmd._cmd_buffer);
continue;
}
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;
}
buffer[bytes_read] = '\0';
printf("%s", buffer);
}
close(prev_pipe_read);
}
free(cmd._cmd_buffer);
}
for (int i = 0; i < num_commands; i++) {
waitpid(pids[i], NULL, 0);
}
return rc;
return OK;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment