void executePipe(char** args, int isBackground, int pgid){ setpgid(getpid(), pgid); if(!isProperPipe(args)){ //Error! printf("Error: Malformed pipe command.\n"); exit(-1); } int status; int numOfPipes = 1; char** buffer = NULL; LinkedList* arguments= getPipeQueue(args); //Get boths sides of pipe char** leftSide = LinkedList_next(arguments); LinkedList_next(arguments); char** rightSide = LinkedList_next(arguments); //Check for errors if(isRedirection(rightSide)){ if(!isProperRedirection(rightSide)){ printf("Error: Malformed redirection command.\n"); exit(-1); } } if(isRedirection(leftSide)){ if(!isProperRedirection(leftSide)){ printf("Error: Malformed redirection command.\n"); exit(-1); } } //Make pipe int fd[2]; pipe(fd); int pid = fork(); if (pid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (!pid){ close(fd[1]); dup2(fd[0], STDOUT_FILENO); if(isRedirection(leftSide)){ executeRedirection(leftSide, isBackground); }else{ execvp(leftSide[0], leftSide); } exit(0); }else{ close(fd[0]); waitpid(pid, &status, 0); dup2(fd[1], STDIN_FILENO); if(isRedirection(rightSide)){ executeRedirection(rightSide, isBackground); }else{ execvp(rightSide[0], rightSide); } exit(0); } }
void commandDecider (CMD_t *inputCommand) { char *commandName = inputCommand -> argv[0]; if (ifRedirection (inputCommand)) { // Send to Redirection executeRedirection (inputCommand); return; } if (ifPiping (inputCommand)) { // Send to Piping executePiping (inputCommand); return; } if (ifReturn (inputCommand)) { // Don't do anything return; } if (!strcmp (commandName, "help")) { // Send to help executeHelp (inputCommand); return; } if (!strcmp (commandName, "clear")) { // Send to clear executeClear (inputCommand); return; } if (!strcmp (commandName, "pwd")) { // Send to pwd showPWD (); return; } if (!strcmp (commandName, "ls")) { // Send to ls executeLS (inputCommand); return; } if (!strcmp (commandName, "whoami")) { // Send to whoami showWhoami (); return; } if (!strcmp (commandName, "cd")) { // Send to cd executeCD (inputCommand); return; } if (!strcmp (commandName, "pid")) { // Send to pid executePID (inputCommand); return; } if (!strcmp (commandName, "history")) { // Send to history executeHistory (inputCommand); return; } if (!strcmp (commandName, "kill")) { // Send to kill executeKill (inputCommand); return; } if (!strcmp (commandName, "exit")) { clearHistory (); exit (0); } printf ("ERROR: INVALID_COMMAND :: \"%s\" is not a recognised command.. Type \"help\" for more information\n", commandName); return; }