int execSimple(simpleCmd* cmd, int* pipeIn, int* pipeOut) { if (strcmp(cmd->name, CD_STR) == 0 || strcmp(cmd->name, SET_STR) == 0) { executeBuiltin(cmd); return getpid(); } else { int pid = fork(); if (pid == 0) { if(pipeIn != NULL) { dup2(pipeIn[0], STDIN_FILENO); close(pipeIn[0]); close(pipeIn[1]); } if(pipeOut != NULL) { dup2(pipeOut[1], STDOUT_FILENO); close(pipeOut[0]); close(pipeOut[1]); } if(isBuiltin(cmd)) { executeBuiltin(cmd); } else { // use execvp -- automatically finds based on $PATH if (execvp(cmd->name, cmd->args) < 0) { fprintf(stderr, "Something went wrong!\n"); if (errno == ENOENT) { fprintf(stderr, "command not found: %s\n", cmd->name); } } printf("This is the exec call's return value: %d\n", execv(cmd->name, cmd->args)); printf("With errno=%d\n",errno); } exit(0); } else if (pipeIn != NULL) { close(pipeIn[0]); close(pipeIn[1]); } return pid; } };
/*! * \brief Funcao que executa a Shell. */ void shooSH_run (void) { Job* job; Parser p; Executor executor; bool exited = false; while (!exited) { std::cout << "shooSH> "; job = p.parseLine (); history.push_back (job->getCommand()); if (job->hasFailed ()) { std::cout << "Erro de sintaxe" << std::endl; } else { if (!(job->isNop()||job->hasExited())) { if (!job->hasPipe()) { if (executeBuiltin (job->getProcess(0)) == -1) { job->setID (++currID); jobList.push_back (job); executor.execute (job); } } else { job->setID (++currID); jobList.push_back (job); executor.execute (job); } } else { exited = job->hasExited(); } } job = NULL; } shooSH_clean(); }
/** * Runs the command specified by the user. This is the main starting point * for command execution. * * @param commandString The command entered into the prompt * * @returns nothing */ void runCommand(char* commandString, shellInternal* env) { env->piping = 0; env->background = 0; // Parse the given command before running it char* arguments[MAX_ARGUMENTS]; parseCommand(commandString, arguments, env); char* command = arguments[0]; if(!executeBuiltin(command, arguments, env)) { if(env->piping) { runPipeline(arguments, env); } else { pid_t pid = fork(); if(pid == 0) { execute(arguments); exit(0); } else { // Check for running a background process if(!env->background) { waitpid(pid, NULL, 0); } else { sleep(1); printf("%d\n", pid); } } } } clearData(arguments, MAX_ARGUMENTS); }