int main( int argc, char** argv ) { command_t cmd; char line[MAXSTRLEN]; char fullpath[MAXSTRLEN]; int done = FALSE; while (!done) { printf(">> "); fgets(line, MAXSTRLEN, stdin); line[my_strlen(line)-1] = '\0'; // get rid of newline parse(line, &cmd); if (my_strequal(cmd.name, "exit")) { done = TRUE; } else if (is_builtin(&cmd)) { do_builtin(&cmd); } else if (find_fullpath(fullpath, &cmd)) { // NOTE: find_fullpath() is called again in execute execute(&cmd); } else if (line[0] != '\0') { // non-blank line entered printf("invalid command\n"); } cleanup(&cmd); } return 0; } // end main function
// ----------------------------------- // Main function // Arguments: argc = number of arguments suppled by user // argv = array of argument values // // int main( int argc, char** argv ) { int done; char input[255]; command_t temp_command; char tempPath[255]; //if statements for signal handling if (signal(SIGCHLD, sig_child_handler) == SIG_ERR) { perror("Unable to create new SIGCHLD signal handler!"); exit(-1); } // //if statements for ctrl-c handling // if (signal(SIGINT, sig_int_handler) == SIG_ERR) { // perror("Unable to create new SIGCHLD signal handler!"); // exit(-1); // } done = FALSE; while (!done) { printf("$"); fflush(stdout); gets(input); parse(input, &temp_command); if (stringCompare(temp_command.name,"exit")==1) { done = TRUE; } else if (is_builtin(&temp_command)) { do_builtin(&temp_command); } else if (find_fullpath(tempPath, &temp_command)) { execute(&temp_command); } else { printf("Command not found.\n"); } cleanup(&temp_command); } // printf( "CSCI 340 Homework Assignment 2 - Have Fun!\n" ); return 0; } // end main function
void parse(char* line, command_t* p_cmd) { int position = 0; char **args = malloc(BUFFER * sizeof(char*)); char *arg; // Allocate space for arguments arg = strtok(line, " "); while (arg != NULL) { args[position] = arg; position++; p_cmd->name = args[0]; p_cmd->argc = sizeof(args); p_cmd->argv = args; // Point values to our command_t struct } int execute(command_t* p_cmd) { int status = FALSE; char* fullpath; status = find_fullpath(fullpath, p_cmd); if (status) { if (fork() == 0) { execv(fullpath, p_cmd->argv); perror("Execute terminated with an error condition!\n"); exit(TRUE); } wait(&status); } else { perror("Command not found!\n"); } return TRUE; // Thanks to Tony Leclerc for example code }