void proc(void) { int status,i; char *command = NULL; char **parameters; char prompt[MAX_PROMPT]; parameters = malloc(sizeof(char *)*(MAXARG+2)); buffer = malloc(sizeof(char) * MAXLINE); if(parameters == NULL || buffer == NULL) { printf("Rshell error:malloc failed.\n"); return; } //arg[0] is command //arg[MAXARG+1] is NULL while(TRUE) { type_prompt(prompt); if(-1 == read_command(&command,parameters,prompt)) continue; if(builtin_command(command,parameters)) continue; if(fork()!=0) { waitpid(-1,&status,0); } else { execvp(command,parameters); } } }
int main(){ //主函数 char command[100]; char *args[1000]; int size, i; char value[1050]; char shellname[1050]; //初始化,增加环境变量 getcwd(first, 999); //获取当前目录 strcpy(value, "shell="); strcpy(shellname, first); strcat(shellname, "/myshell"); strcat(value, shellname); putenv(value); while(true){ type_prompt(); //输出命令提示符 //读取指令 size = read_command(command, args, stdin); //分词并调用指令 backstage(command, args, size); //释放内存 clear_command(args, size); } }
void interactive(){ while (TRUE){ char *command; int i; char *parameters[32]; struct timeval initial_clock; struct timeval final_clock; int num_params; do{ type_prompt();// displays prompt num_params = read_command(command, parameters);//get input from user and parse it into command and parameters if(num_params > 32){ printf("\nOnly 32 arguments allowed.\n"); } } while(num_params > 32 || num_params == -2); char **params = malloc((num_params + 1) * sizeof(char *)); for(i = 0; i < num_params; i++) params[i] = parameters[i]; params[i] = NULL; command = params[0]; if(strcmp("exit", command) == 0) exit(4); if(strcmp("cd", command) == 0){ chdir(params[1]); continue; } if(gettimeofday(&initial_clock, NULL) == -1) printf("\nCould not get time.\n"); pid_t pId = fork(); if(pId == -1){ //fork failed printf("Fork failed. Please try running again. %s\n", strerror(errno)); } if(pId != 0){ //parent process int status; if(waitpid(pId,&status, 0) != pId) printf("waitpid failed\n"); // once child process is done, print usage statistics if(gettimeofday(&final_clock, NULL) == -1) printf("\nCould not get time.\n"); long seconds_elapsed = final_clock.tv_sec - initial_clock.tv_sec; long microseconds_elapsed = final_clock.tv_usec - initial_clock.tv_usec; long milliseconds_elapsed = seconds_elapsed*1000 + microseconds_elapsed*0.001; printf("\nWall clock time for the command to execute:\n%ld milliseconds\n", milliseconds_elapsed); print_statistics(); } else{ //child process int returnnum; returnnum = execvp(command, params); if(returnnum == -1){ //error occurred during execvp printf("Your command didn't work: %s\n", strerror(errno)); prev_usr_milliseconds = 0; prev_sys_milliseconds = 0; prev_stats.ru_nivcsw = 0; prev_stats.ru_nvcsw = 0; prev_stats.ru_majflt = 0; prev_stats.ru_minflt = 0; } } } }
int main(int argc, const char *argv[]) { int RUN = TRUE; char command[100] = ""; char params[100] = ""; int PIDstatus = 0; while (RUN) {/* Endlosschleife */ strcpy(command, ""); type_prompt(); /* Prompt ausgeben */ read_command(command, params); /* Eingabezeile von Tastatur lesen */ if(!strcmp(command, "quit")) { quit_shell(&RUN); } else if (!strcmp(command, "version")) { get_version(); } else if (!strcmp(command, "/")) { changedir(params); } else if (!strcmp(command, "help")) { print_help(); } else { /* PIDstatus = fork(); [> Kind erzeugen <]*/ /* if (PIDstatus < 0) {*/ /* printf("Unable to fork"); [> Fehlerbedingung <]*/ /* continue; [> Schleife wiederholen <]*/ /* }*/ /* if (PIDstatus > 0) {*/ /* waitpid (PIDstatus, &status, 0); [> Elternprozess wartet auf Kind <]*/ /* } else {*/ /* execve(command, params, 0); [> Das Kind-Programm ausführen <]*/ /* }*/ PIDstatus = fork(); if (PIDstatus < 0) { printf("Sorry, unable to fork"); } else if (PIDstatus > 0) { if (params[strlen(params)-1] != '&') { waitpid(PIDstatus,NULL,0); } } else { RUN = FALSE; // stop loop for this process if (params[strlen(params)-1] != '&') { printf("\n"); // beautify output } if (execlp(command, (char const*)params, 0) == -1) { printf("Programm nicht gefunden!\n"); } } } } return 0; }
int main() { char prompt[MAXLINE]; char *cmd; char **param; struct parse_info parse; param=malloc(sizeof(char *)*(MAXARG+2)); open(".history",O_WRONLY|O_TRUNC,0666); open(".alias",O_WRONLY|O_TRUNC,0666); while(1){ int fd[2],in_fd,out_fd,status; pid_t pid,pid2; type_prompt(prompt); int count=read_cmd(&cmd,param,prompt); if(count==-1) continue; parsing(param,count,&parse); //printf("%d\n%s\n%s\n%s\n",parse.flag,parse.in_file,parse.out_file,parse.cmd2); if(built_in(cmd,param)) continue; if(parse.flag & IS_PIPED){ if(pipe(fd)<0){ printf("myshell error: pipe failed.\n"); exit(0); } } if((pid=fork())==0){ if(parse.flag & IS_PIPED){ if(!(parse.flag & OUT_REDIRECT)&&!(parse.flag & OUT_REDIRECT_APPEND)){ close(fd[0]); close(fileno(stdout)); dup2(fd[1],fileno(stdout)); close(fd[1]); } else{ close(fd[0]); close(fd[1]); if(parse.flag & OUT_REDIRECT) out_fd=open(parse.out_file,O_WRONLY|O_CREAT|O_TRUNC,0666); else out_fd=open(parse.out_file,O_WRONLY|O_CREAT|O_APPEND,0666); close(fileno(stdout)); dup2(out_fd,fileno(stdout)); close(out_fd); } } else{ if(parse.flag & OUT_REDIRECT){ out_fd=open(parse.out_file,O_WRONLY|O_CREAT|O_TRUNC,0666); close(fileno(stdout)); dup2(out_fd,fileno(stdout)); close(out_fd); } if(parse.flag & OUT_REDIRECT_APPEND){ out_fd=open(parse.out_file,O_WRONLY|O_CREAT|O_APPEND,0666); close(fileno(stdout)); dup2(out_fd,fileno(stdout)); close(out_fd); } } if(parse.flag & IN_REDIRECT){ in_fd=open(parse.in_file,O_RDONLY|O_CREAT,0666); close(fileno(stdin)); dup2(in_fd,fileno(stdin)); close(in_fd); } execvp(cmd,param); } else{ if(parse.flag & IS_PIPED){ if((pid2=fork())==0){ close(fd[1]); close(fileno(stdin)); dup2(fd[0],fileno(stdin)); close(fd[0]); execvp(parse.cmd2,parse.param2); } else{ close(fd[0]); close(fd[1]); waitpid(pid2,&status,0); } } if(parse.flag & BACKGROUND){ printf("Child pid: %u\n",pid); } else waitpid(pid,&status,0); } if(env_modify){ char str[1024]; env_modify=0; FILE *fp; fp=fopen(".environment","r"); while(!feof(fp)){ fgets(str,1024,fp); if(!strncmp(str,str_env,strlen(str_env))){ printf("%s",str+strlen(str_env)+1); break; } } fclose(fp); } } return 0; }
/* * Run the shell. * */ int main(int argc, char *argv[]){ while(TRUE){ // Read from stdin int flag_background = FALSE; type_prompt(); CmdArg *cmd = malloc(sizeof(CmdArg)); cmd->args = malloc((MAX_ARGS+1) * sizeof(char*)); if (read_command(cmd) > 0){ if (lastCharIsAmp(cmd->args, cmd->n_args)){ flag_background = TRUE; cmd->n_args --; } // Check if command is exit if (strcmp(cmd->args[0], "exit") == 0){ //TODO //wait_for_children(1); printf("Exiting the shell.\n"); return(EXIT_SUCCESS); } // Check if command is cd else if (strcmp(cmd->args[0], "cd") == 0){ chdir(cmd->args[1]); char cwd[1024]; if (DEBUG){ if (getcwd(cwd, sizeof(cwd)) != NULL) fprintf(stdout, "Current working dir: %s\n", cwd); } } // if command is jobs, print all running processes else if (strcmp(cmd->args[0], "jobs") == 0){ printf("write jobs handler here\n"); // go through list of processes and print each one int i; if (num_processes == 0) printf("No running processes.\n"); else { printf("Running jobs are: \n"); for (i = 0; i < num_processes; i++){ if (processes[i].pid != 0) printf("[%d] Command: [%s]\n", processes[i].pid, processes[i].cmd); } } } else { execute(cmd->args, flag_background); } free_args(cmd); } } return 0; }
int main(int argc, const char *argv[]) { char linebuf[LIMIT+1]; char *read; char **tokens; int len, bg; #if SIGDET == 0 /*pid_t pid; int status;*/ #endif register_sighandler(SIGINT, parent_sigint); register_sighandler(SIGTSTP, parent_sigtstp); /* SIGDET = 1 means that the child is responsible for reporting to its parent when it's done so we register a sighandler for that */ #if SIGDET == 1 register_sighandler(SIGCHLD, parent_sigchld); #endif while (TRUE) { read = NULL; tokens = 0; bg = FALSE; #if SIGDET == 0 poll_childs(); #endif type_prompt(); read = fgets(linebuf, LIMIT, stdin); if (read != linebuf) { /* end of file - quit as with exit command */ if (feof(stdin)) { printf("^D\n"); strcpy(linebuf, "exit\n"); } else { /* some interrupt, proceed to next read */ continue; } } /* trim whitespace right (in order to read flag later) */ len = strlen(linebuf); while ((len > 0) && (linebuf[len-1] == '\n' || linebuf[len-1] == ' ')) { linebuf[len-1] = '\0'; len -= 1; } /* check for background flag */ if (len > 0 && linebuf[len-1] == '&') { bg = TRUE; linebuf[len-1] = '\0'; len -= 1; } tokens = tokenize(linebuf, '|'); exec_command(tokens, bg); } return 0; }