int execute_simple_command(simple_command *cmd) { pid_t pid; int status; int result = 0; if(cmd->builtin == BUILTIN_CD){ return execute_cd(cmd->tokens); } else if(cmd->builtin == BUILTIN_EXIT){ exit(0); } else if(cmd->builtin == 0){ if((pid = fork()) == -1){ perror("fork"); exit(EXIT_FAILURE); } else if(pid > 0){ waitpid(pid, &status, 0); return status; } else if(pid == 0){ execute_nonbuiltin(cmd); exit(0); } } else{ printf("execute_simple error"); exit(EXIT_FAILURE); } return result; }
/** * Executes a simple command (no pipes). */ int execute_simple_command(simple_command *cmd) { /** * Check if the command is builtin. * 1. If it is, then handle BUILTIN_CD (see execute_cd function provided) * and BUILTIN_EXIT (simply exit with an appropriate exit status). * 2. If it isn't, then you must execute the non-builtin command. * - Fork a process to execute the nonbuiltin command * (see execute_nonbuiltin function above). * - The parent should wait for the child. * (see wait man pages). */ int command_number; /* Handle builtin commands. */ if((command_number = is_builtin(*(cmd->tokens)))) { switch(command_number) { case(BUILTIN_CD): if(execute_cd(cmd->tokens) != EXIT_SUCCESS) { printf("No such file or directory.\n"); } return 0; break; case(BUILTIN_EXIT): return -1; break; } /* Handle nonbuiltin commands. * Fork a process and use execute_nonbuiltin to execute the command. */ } else { int pid; if((pid = fork()) < 0) { perror("fork"); return EXIT_FAILURE; } else if (pid == 0) { //child /* The exit status of the child process will be the return value * of execute_nonbuiltin (if there is an error), or the exit * status of the command exec'd. */ exit(execute_nonbuiltin(cmd)); } else { //parent int status; /* Wait for the command to return, then return its exit status*/ if(wait(&status) != -1) { if(WIFEXITED(status)) { return WEXITSTATUS(status); } else { return EXIT_FAILURE; } } else { perror("wait"); return EXIT_FAILURE; } } } return EXIT_FAILURE; }
static void process_command(void) { int pos = -1; char buffer[BUF_SIZE]; print_prompt(); do { char ch = getchar(); if (ch == 127) { /* Delete/BackSpace */ if (pos >= 0) { pos--; printf("\b \b"); } } else if (ch == '\r' && pos < BUF_SIZE - 1) { /* Return */ buffer[++pos] = 0; break; } else if (pos < BUF_SIZE - 2) { buffer[++pos] = ch; putchar(buffer[pos]); } } while (buffer[pos] != '\r'); buffer[pos] = 0; newline(); if (pos == 0) return; /* Built-in commands */ if (strncmp(buffer, "cd", 2) == 0) execute_cd(buffer); else if (strncmp(buffer, "pwd", 3) == 0) execute_pwd(buffer); else if (strncmp(buffer, "exit", 4) == 0) flag_exit = 1; else execute_command(buffer); }
/** * Executes a simple command (no pipes). */ int execute_simple_command(simple_command *cmd) { /** * TODO: * Check if the command is builtin. * 1. If it is, then handle BUILTIN_CD (see execute_cd function provided) * and BUILTIN_EXIT (simply exit with an appropriate exit status). * 2. If it isn't, then you must execute the non-builtin command. * - Fork a process to execute the nonbuiltin command * (see execute_nonbuiltin function above). * - The parent should wait for the child. * (see wait man pages). */ if(cmd->builtin != 0){ if(is_builtin(cmd->tokens[0]) == BUILTIN_CD){ execute_cd(cmd->tokens); }else if(is_builtin(cmd->tokens[0]) == BUILTIN_EXIT){ return -1; } }else{ int r,status; if ((r = fork()) == -1){ // fork failed perror("fork"); exit(-1); } else if (r > 1){ // parent process wait(&status); } else if (r == 0){ // child process execute_nonbuiltin(cmd); exit(1); } } return 0; }