// Our shell main loop void sh_loop(void) { char * line; char ** args; int status; do { char hostname[HOST_NAME_MAX + 1]; char pathname[PATH_MAX + 1]; struct passwd * p = getpwuid(getuid()); if(p == NULL) // If we can't get the user_id we should quit. { fprintf(stderr, "Could not get username.\n"); exit(EXIT_FAILURE); } // Set process effective ID to user ID. setreuid(getuid(), getuid()); gethostname(hostname, sizeof(hostname)); // Get the hostname. char * username = p->pw_name; char * current_dir = getcwd(pathname, sizeof(pathname)); /* Print prompt in the format: * [username@hostname current_working_directory] % */ // If the current working directory is the user's home directory // we should just print ~ to let them know they are home. if(strcmp(current_dir, p->pw_dir) == 0) { print_userhost(username, hostname, "~"); } else { print_userhost(username, hostname, current_dir); } line = sh_get_cmd(); args = sh_tokenize_cmd(line); status = sh_launch(args); // Free the memory we used. free(line); free(args); } while(status); }
int main(void) { /* print PIDs */ print_pid("P0"); /* User/Hostname */ print_userhost(); /* Time */ print_time(); /* CWD */ print_cwd("P0"); /* Create ENV Var */ putenv(env_var); // to change env variable, change actual character pointer handle_processes(); return 0; }