1639582171271 executor in business analysis using tools PDF

Title 1639582171271 executor in business analysis using tools
Author Boaz Kiptoo
Course International Business Management
Institution University of Nairobi
Pages 7
File Size 46.9 KB
File Type PDF
Total Downloads 3
Total Views 159

Summary

technique in business management and data analysis using sata data tool analist...


Description

#include #include #include #include #include #include #include #include #include





"command.h" "executor.h"

static int recurse_tree(struct tree *t, int input_fd, int output_fd); int execute(struct tree *t) { /* case 1 = check that the tree isn't null */ if (t != NULL) { return recurse_tree(t, -1, -1); } return 0; } static struct subshell { Shell_t *shp; struct subshell *prev; struct subshell *pipe; Dt_t *var; if (subshell==NULL){ return 0 } /* the recurse_tree() function recursively executes commands in the tree */ static int recurse_tree(struct tree *t, int input_fd, int output_fd) { pid_t process_id; int status0, status1; int execute(struct tree *t) { if (t == NULL) { return 0; } /* conjuction not pipe */ if (t -> conjunction == NONE) { /* exit command, exit(0). */ if (strcmp(t -> argv[0], "exit") == 0) { exit(0); } else if (strcmp(t -> argv[0], "cd") == 0) { if (t -> argv[1] == NULL) { chdir(getenv("HOME")); return 0; } else { chdir(t -> argv[1]); return 0;

} } else { pid_t child_pid; int in, out; child_pid = fork(); /* Checking for a fork error. */ if (child_pid < 0) { err(EX_OSERR, "error: fork error."); } if (child_pid == 0) { /* redirection input. */ if (t -> input != NULL) { in = open(t -> input, O_RDONLY); if(in < 0) { err(EX_OSERR, "error: error opening file."); } if (dup2(in, STDIN_FILENO) < 0) { err(EX_OSERR, "error: dup2 error."); } if (close(in) < 0) { err(EX_OSERR, "error: error closing file."); } } /* redirection output. */ else if (t -> output != NULL) { out = open(t -> output, O_RDWR | O_CREAT | O_TRUNC, 0664); if (out < 0) { err(EX_OSERR, "error: error opening file."); } if (dup2(out, STDOUT_FILENO) < 0) { err(EX_OSERR, "error: dup 2 error."); } if (close(out) < 0) { err(EX_OSERR, "error: error closing file."); } } execvp(t -> argv[0], t -> argv); printf("failed to execute %s\n", t -> argv[0]); exit(1); } //*parent portion of code*// else {

int status; wait(&status); return status; } } } /* conjunction isn't NONE or PIPE. */ else if (t -> conjunction == AND) { if (t -> input == NULL && t -> output == NULL) { pid_t child_pid; child_pid = fork(); /* fork errors checking. */ if (child_pid < 0) { err(EX_OSERR, "error: fork error."); } /* ***** This is the child code. ***** */ if (child_pid == 0) { if (execute(t -> left) == 0) { if (execute(t -> right) == 0) { exit(0); } else { exit(-1); } } else { exit(-1); } } /* parent code*/ else { int status; wait(&status); return status; } } /* Input redirection. */ if (t -> input != NULL) { pid_t child_pid; child_pid = fork(); /* Checking for fork errors. */ if (child_pid < 0) { err(EX_OSERR, "error: fork error"); }

/* child's part of the code */ if (child_pid == 0) { int in; in = open(t -> input, O_RDONLY); if (in < 0) { err(EX_OSERR, "error: error opening file."); } if (dup2(in, STDIN_FILENO) < 0) { err(EX_OSERR, "error: dup2 error."); } if (close(in) < 0) { err(EX_OSERR, "error: error closing file."); } if (execute (t -> left) == 0) { if (execute(t -> right) == 0) { exit(0); } else { exit(-1); } } else { exit(-1); } } /* parent code */ else { int status; wait(&status); return status; } } /* Output redirection. */ else if(t -> output != NULL) { int out; out = open(t -> output, O_RDWR | O_CREAT | O_TRUNC, 0664); if (out < 0) { err(EX_OSERR, "error: error opening file."); } if (dup2(out, STDOUT_FILENO) < 0) { err(EX_OSERR, "error: dup2 error."); }

if (close(out) < 0) { err(EX_OSERR, "error: error closing file."); } pid_t child_pid; child_pid = fork(); /*Checking for fork errors. */ if (child_pid < 0) { err(EX_OSERR, "fork error"); } /* childs part of the code */ if (child_pid == 0) { if (execute(t -> left) == 0) { if (execute(t -> right) == 0) { exit(0); } else { exit(-1); } } else { exit(-1); } } /* parent part of the code */ else { int status; wait(&status); return status; } } return 0; } /* case where the conjunction is not NONE or NOT */ else if (t -> conjunction == PIPE) { int pipe_fd[1]; pid_t child_pid; /* Checking for fork errors. */ child_pid = fork(); /* child's part of the code */ if (child_pid == 0) { pid_t child_pipe; /* pipe error checking. */ if(pipe(pipe_fd) < 0) {

err(EX_OSERR, "error: pipe error."); } child_pipe = fork(); /* checking for errors */ if (child_pipe < 0) { err(EX_OSERR, "error: fork error."); } if (child_pipe == 0) { /* pipe closing read end. */ close(pipe_fd[0]); if (dup2(pipe_fd[1], STDOUT_FILENO) < 0) { err(EX_OSERR, "error: dup2 error."); } if (execute(t -> left) == 0) { exit(0); } else { exit(-1); } } /*checking status*/ else { int status; wait(&status); if(status != 0) { exit(EXIT_FAILURE); } /* Pipe closing. */ close(pipe_fd[1]); if (dup2(pipe_fd[0], STDIN_FILENO) < 0) { err(EX_OSERR, "error: dup2 error."); } if(execute(t -> right) == 0) { exit(0); } else { exit(-1); } } }

else { int second_status; wait(&second_status); return second_status; } } return 0; }...


Similar Free PDFs