static int exec_tree_pipeline_exec(t_list **pipeline_base, \ char ***env) { t_list *pipeline; t_list *pipeline_prev; t_pipe_fd pipe_fd; int ret; pipeline = *pipeline_base; pipeline_prev = NULL; while (pipeline) { if ((ret = is_built_in(pipeline->data, env)) == 0 || ret < 0 || ret == 1) { rm_list(&pipeline, pipeline_base); if (ret < 0 || ret == 1) return (ret); } else if ((ret = exec_tree_pipeline_exec_cmd(&pipeline, &pipeline_prev, \ &pipe_fd, env)) < 0) return (ret); } return (0); }
void execute(char **cmd) { int status; int i = 0; pid_t pid; if(is_built_in(cmd[0])) { execute_built_in(cmd); } else if (is_external(cmd[0])) { pid = fork(); /* error */ if (pid == -1) exit(1); /* child */ else if (pid == 0) { execv(cmd[0], cmd); exit(0); } /* parent */ else waitpid(pid, &status, 0); } else printf("%s: Command not found.\n", cmd[0]); }
void execute_son(int fd_in, int fd_out, t_list_p *item, char **envp) { st_exec_test(fd_in, fd_out); if (item->in) process_redir_r(item, envp); else if (item->out) process_redir_w(item, envp); else if (item->app) process_redir_a(item, envp); if (!is_built_in(item->str)) process_command(item->str, envp); else if (ft_is_env_i(item->str) > 0) process_command((item->str + ft_is_env_i(item->str)), NULL); else check_built_in(item->str); exit(0); }
int my_execparent(t_pcs cmd[], int i) { if (cmd == NULL || !is_open(NULL) || cmd[i].pid <= 0) return (0); if (cmd[i].pipedright != NULL) close(cmd[i].pipedright[1]); if (cmd[i].pipedleft != NULL) close(cmd[i].pipedleft[1]); if (cmd[i + 1].is_piped == 0 || cmd[i + 1].pathn == NULL) { while (waitpid(cmd[i].pid, &(cmd[i].retv), WNOHANG) != cmd[i].pid); if (WIFEXITED(cmd[i].retv)) { cmd[i].ret = WEXITSTATUS(cmd[i].retv); if (!cmd[i].cdt || (cmd[i].cdt && returncdt_checked(cmd, i))) set_lastreturnvalue(NULL, cmd[i].ret); } if (WIFSIGNALED(cmd[i].retv)) cmd[i].sig = WTERMSIG(cmd[i].retv); } if (is_built_in(cmd[i].pathn) && is_shellin(&(cmd[i]), get_shellins())) exec_shellin(&(cmd[i])); return (cmd[i].retv); }
/* === shell_body function === >>> heart of the program. Initializes memory blocks, sets path, hostname, current directory, > and handles user input by calling appropriate functions to deal with the request. */ int shell_body(){ //Startup preperation// //ALL Pointer variables// char* cwd_path = malloc(MAX_PATH_SIZE*sizeof(char)); //path of current working directory. char* input = malloc(INITIAL_BLOCK*sizeof(char)); //user input into shell. char** arg_ptrs = NULL; //where makearv holds args parsed. char* hostname = malloc(64*sizeof(char)); //holds hostname of machine. void* ptrvector[] = {cwd_path, input, hostname, arg_ptrs}; //all ptrs that needs to be freed, go here. ///////////////////////////////////////////// //Declared Variables// pid_t shells_pid = getpid(); //process id. size_t n_getline = (INITIAL_BLOCK*sizeof(char)); //getline n arguement. int num_of_args; //return of makeargv with '\n', '\t', ' '. int builtin_result; //return of is_built_in. ///////////////////////////////////////////////// //error handling variables// int errorexit = 0; char* cwd_overflow; //used for error handling of getcwd(). /////////////////////////////////////////////////////////////// gethostname(hostname, 64); //gets hostname of machine //if your hostname > 64chars, you are part of the problem. //Let the shell begin... while(runstatus){ // signal(SIGCHLD, SIG_IGN); waitpid(WNOHANG); cwd_overflow = getcwd(cwd_path,MAX_PATH_SIZE); if(cwd_overflow == NULL) perror("getcwd"); //means cwd is overflowing if true. printf(BOLD_ON ANSI_COLOR_GREEN"%s@%s"ANSI_COLOR_BLUE" %s [PID:%li] $ " ANSI_COLOR_RESET BOLD_OFF,getenv("USER"),hostname,cwd_path,shells_pid); getline(&input, &n_getline, stdin); num_of_args = makeargv(input, " \n\t", &arg_ptrs); //split input. if(num_of_args != 0){ builtin_result = is_built_in(arg_ptrs[0]); if(builtin_result >= 0){ built_in_handler(arg_ptrs,builtin_result); } else{ if((*arg_ptrs[num_of_args-1] == '&') || (contain_rd(input) != 0)){ signal(SIGCHLD, SIG_DFL); pid_t newpid = fork(); if(newpid > 0){ //if parent. if(*arg_ptrs[num_of_args-1] == '&') printf("Putting process %li in background.\n",newpid+1 ); else { waitpid(newpid); //must have both waits in this order otherwise death. wait(NULL); } } else{ //if child if(*arg_ptrs[num_of_args-1] == '&') arg_ptrs[num_of_args-1] = '\0'; multiprocess_handler(arg_ptrs,num_of_args-1); return 1; //1 represents return of child process as exit code. exit(0); //you can never be too safe... } } else{ //no backgrounding or redirects. Deals with request in initial shell process. multiprocess_handler(arg_ptrs,num_of_args); } } } } cleanup(ptrvector); return errorexit; }
int main(int argc, char *argv[]) { struct CommandData* Cmd_Data = malloc(sizeof(*Cmd_Data)); pid_t pid1, pid2; //character array initialized to the size of PATH_MAX+1, PATH_MAX is the //maximum length a path can be. From limits.h char currentDir[PATH_MAX + 1]; //Character line that will be passed to the parser char* line; //Line to be parsed int tempBuiltIn; int tempFdIn = 1, tempFdOut = 0, fdIn = -1, fdOut = -1; int fd[2]; fd[READ] = -1; //output fd[WRITE] = -1; //input int pipeNeeded = 0; //No pipe needed at first int loopcntr = 1; //Not sure if this is correct setenv("DEBUG", "NO",1); //Loop forever while(loopcntr){ //character arrays to store both the environment variable name and value char* envName[256]; char* envVal[256]; //initializing them for(int i = 0; i <= 256; i++){ if(i == 256){ envName[i-1] = '\0'; envVal[i-1] = '\0'; } else{ envName[i] = "."; envVal[i] = "."; } } //Print shell prompt if (getcwd(currentDir, sizeof(currentDir)) != NULL){ fprintf(stdout, "%s>", currentDir); } //Read in the line of commands line = read_line(); //Parse the line memset(Cmd_Data , 0, sizeof(*Cmd_Data)); ParseCommandLine(line, Cmd_Data); tempBuiltIn = is_built_in(Cmd_Data); //Was the command entered a built in command? if(tempBuiltIn != -1){ //if the input string had an assignment operation, extract the name //and value from the input string and store them in envName, envVal. if(isEnvAssignment == 1){ int cntr = 0; int j = 0; while(j < sizeof(*line)){ if(line[j] == ' ') break; j++; } for(int i = j; i < sizeof(*line); i++){ if(line[i] == '\0') envVal[i] = '\0'; else if(line[i] == '='){ envName[i] = '\0'; cntr = i; } else if(i > cntr) envVal[i] = &line[i]; else envName[i] = &line[i]; } isEnvAssignment = 0; fprintf(stdout, "Command: %s\n", envVal[0]); setenv(*envName, *envVal, 0); } //execute the built in command. envName, and envVal are passed in to //allow export to execute. executeBuiltInCommand(Cmd_Data, tempBuiltIn, &loopcntr, *envName, *envVal); } else{ //Check if input file needs to be opened if(Cmd_Data->infile != NULL){ char* temp = Cmd_Data->infile; fdIn = open(temp, O_RDONLY, 0); //Get FD for the file tempFdIn = dup(STDIN_FILENO); } //Check if output file needs to be opened if(Cmd_Data->outfile != NULL){ char* temp = Cmd_Data->outfile; mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; fdOut = open(temp, O_WRONLY | O_CREAT | O_TRUNC, mode); tempFdOut = dup(STDOUT_FILENO); } //Is a pipe needed? if(Cmd_Data->numcommands > 1){ pipe(fd); pipeNeeded = 1; //Set as true } //Create a child process pid1 = fork(); //Was there an error during the fork? if(pid1 == -1){ fprintf(stderr,"ERROR WITH FORK\n"); } //Child process else if(pid1 == 0){ if(pipeNeeded){ //Fork again pid2 = fork(); //Was there an error during the fork? if(pid2 == -1){ fprintf(stderr,"ERROR WITH FORK\n"); } //Child process else if(pid2 == 0){ //Was an output file opened? if(fdOut != -1){ dup2(fdOut, STDOUT_FILENO); //Make STDOUT point at fdIn close(fdOut); //Not needed anymore } ; close(fd[WRITE]); dup2(fd[READ], STDIN_FILENO); close(fd[READ]); execCmd(Cmd_Data, 1); close(STDIN_FILENO); } //Parent process else{ //Was an input file opened? if(fdIn != -1){ dup2(fdIn, STDIN_FILENO); //Make STDIN point at fdIn close(fdIn); //Not needed anymore } close(fd[READ]); dup2(fd[WRITE], STDOUT_FILENO); close(fd[WRITE]); execCmd(Cmd_Data, 0); close(STDOUT_FILENO); } } else{ //Was an input file opened? if(fdIn != -1){ dup2(fdIn, STDIN_FILENO); //Make STDIN point at fdIn close(fdIn); //Not needed anymore } //Was an output file opened? if(fdOut != -1){ dup2(fdOut, STDOUT_FILENO); //Make STDOUT point at fdOut close(fdOut); //Not needed anymore } execCmd(Cmd_Data, 0); } } //Parent process else{ //Command running in background? if(!Cmd_Data->background){ for(int i = 0; i < 2; i++) wait(NULL); //Wait on the child process } //Close the fds in parent close(fdIn); close(fdOut); close(fd[0]); close(fd[1]); //Reset pipe variable pipeNeeded = 0; //Reset fd values fdIn = fdOut = fd[READ] = fd[WRITE] = -1; //Return STDIN and STDOUT to their proper fd's dup2(tempFdIn, STDIN_FILENO); dup2(tempFdOut, STDOUT_FILENO); } } //char* aux = getenv("DEBUG"); //if(*aux == "NO") print_info(Cmd_Data); } exit(0); }