int eval_node_pipe(NodeType *pn) { NodeType *cmd1 = pipe_cmd(pipe_pipe(pn)); NodeType *cmd2 = pipe_cmd(pn); pid_t pid1; if ((pid1 = fork()) < 0) { err_sys("fork error"); } else if (pid1 == 0) { // child pid_t pid2; int fd[2]; if (pipe(fd) < 0) err_sys("pipe failed"); if ((pid2 = fork()) < 0) { err_sys("fork error"); return -1; } else if (pid2 == 0) { // child close(STDOUT_FILENO); dup(fd[1]); // fd[1] is the write end, This call dup fd[1] to stdout close(fd[0]); close(fd[1]); // write to stdout return eval_cmd_in_proc(cmd1); // output of cmd1 will directed to pipe write end } else { // parent close(STDIN_FILENO); dup(fd[0]); // fd[0] is the read end. close(fd[0]); close(fd[1]); // read from(stdin) an empty pipe blocks reader. return eval_cmd_in_proc(cmd2); } } else { // parent if (waitpid(pid1, NULL, 0) < 0) { err_sys("wait error"); } } return 0; }
int main(int argc, char *argv[]){ char *argv[argc], *cmd1[argc], *cmd2[argc]; /*separate the commands in two*/ pipe_cmd(cmd1, cmd2); }
int and_or_cmd (CMD *cmd) { int status = SUCCESS; if (!cmd) return status; if (cmd->type != SEP_AND && cmd->type != SEP_OR) status = pipe_cmd(cmd); else if (cmd->type == SEP_OR) { status = and_or_cmd(cmd->left); if (status == ERROR) //short circuit otherwise status = pipe_cmd(cmd->right); } else if (cmd->type == SEP_AND) { status = and_or_cmd(cmd->left); if (status == SUCCESS) status = pipe_cmd(cmd->right); } return status; }
int main( int argc, char **argv ) { pipe_cmd(); return 0; }
int main(int argc, char *argv[]) { char cmd [MAXCMD]; char* arg [MAXARG]; int* arg_count = new int(0), * current_count = new int(0); char the_path[256]; getcwd(the_path, 255); name_shell(the_path); signal(SIGINT, signalHandler); system("clear"); do { strcpy(cmd , ""); //clears the input (solves a problem I was having). printf("%s", shell_name.c_str()); //reprint shell name std::cin.getline(cmd, MAXCMD, '\n'); // get input parse(cmd, arg, arg_count); // Only processes stuff that was entered into the shell if(strcmp(cmd, "\0") != 0) { if(is_there_next_pipe(arg, *current_count, *arg_count)) { // TODO: Fix handling more than one pipe process_pipe(current_count); // seperate commands for piping char *cmd1[MAXARG], *cmd2[MAXARG]; // Left program for(int i=0;i<*current_count;++i) { cmd1[i] = arg[i];//cmd1[i] = arg[i]; } // Right program for(int i=*current_count;i<*arg_count;++i) { cmd2[i-(*current_count)] = arg[i];//cmd2[i-(*current_count)] = arg[i-(*current_count)]; } pipe_cmd(cmd1, cmd2); //printf("Pipe: %s into %s\n", arg[*current_count-2], arg[*current_count]); } // Check if these are internal commands, which do not need forking else if(is_internal_cmd(cmd)) process_internal_cmd(cmd, arg); // Run process else run_cmd(arg, arg_count); } } while(true); // "exit" will stop the shell return 0; }