📄 myshell.c
字号:
/*Copyright:CaiShaowei@Computer Science College Of SCUT,2004(2),NO.200433100067** *************this program makes a "Shell" of Unix Operation System*************/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <dirent.h>#include <string.h>#include <sys/wait.h>#define CD 1#define ENVIRON 2#define ECHO 3#define HELP 4#define JOBS 5#define QUIT 6#define PWD 7#define RUN_PROCESS 8#define order_max 100 //the max length of an order#define echo_len 100 //the number of the strings can be in the "echo" command#define echo_max 300 //the max length of every string of "echo" command#define dir_max 100 //the max length of the path of directory or fileint input_order();void cd();void environ();void echo();void help();void jobs();void pwd();void run_process();static char order[order_max]; //order string got from the user(keyboard)static char dir[dir_max]; //directoryint main(){ int choice = 0;//the choice that user input char hostname[50] ;//hostname of computer char user[50];//user name strcpy(hostname, getenv("HOSTNAME")); strcpy(user,getenv("USER")); strcpy(dir,getenv("PWD"));//get the pwd of the Operation System in the beginning. printf("[%s %s]# ",user,dir); while(1) { choice = input_order(); switch(choice) { case CD: cd(); break; case ENVIRON: environ(); break; case ECHO: echo(); break; case HELP: help(); break; case JOBS: jobs(); break; case QUIT: printf("Byebye!\n"); return 0; case PWD: pwd(); break; case RUN_PROCESS: run_process(); break; } printf("[%s %s]# ",user,dir); } return 0;}int input_order(){ scanf("%s255",&order); if(strcmp(order,"cd")==0) { return CD; } else if(strcmp(order,"environ")==0) { return ENVIRON; } else if(strcmp(order,"echo")==0) { return ECHO; } else if(strcmp(order,"help")==0) { return HELP; } else if(strcmp(order,"jobs")==0) { return JOBS; } else if(strcmp(order,"pwd")==0) { return PWD; } else if(strcmp(order,"quit")==0||strcmp(order,"exit")==0 ||strcmp(order,"bye")==0) { return QUIT; } else { return RUN_PROCESS; }}/***function:cd . to change the current directory.***/ void cd(){ scanf("%sdir_max",&dir); if(chdir(dir) == -1)//change the direction { perror("this is not a valid directory"); if(getcwd(dir,dir_max)==NULL)//if the input string is not a valid directory,rewrite "dir" as current directory { perror("wrong:"); exit(1); } }}/****function:environ , to print the environment set in the Operation System.****/void environ(){ printf("HOSTNAME=%s\n",getenv("HOSTNAME")); printf("TERM=%s\n",getenv("TERM")); printf("HISTSIZE=%s\n",getenv("HISTSIZE")); printf("WINDOWID=%s\n",getenv("WINDOWID")); printf("PWD=%s\n",getenv("PWD")); printf("MAIL=%s\n",getenv("MAIL")); printf("USER=%s\n",getenv("USER")); printf("HOME=%s\n",getenv("HOME")); printf("PATH=%s\n",getenv("PATH"));}/*** functio:echo. to print string the user input.***/ void echo(){ char echo_string[echo_len][echo_max];//used to store the strings of the "echo" command int echo_num = 0; while(getchar()!='\n') //read ' ' or '\n' from keyboard,stop inputing echo_string when meets '\n' { scanf("%s300",&echo_string[echo_num]); echo_num++; } int i; for(i=0;i<echo_num;i++) { printf("%s ",echo_string[i]); } printf("\n");}/***function:help. to print the help information of the program.***/void help(){ printf(" *****shell by ShaoweiCai@2004(2),SCUT version 1.0****\n"); printf("**********************************************************\n"); printf("cd [dir] : Change the current direction to dir.\n"); printf("echo [string]: Display a line of text,follow by a new line.\n"); printf("environ : Print the environment settings of the Operation System.\n"); printf("exit : Cause the shell to exit.\n"); printf("help : Display helpful information about commands of the shell program.\n"); printf("jobs : This is a jobs order.\n"); printf("quit : Cause the shell to exit.\n"); printf("bye : Cause the shell to exit.\n"); printf("[path] : path can be the path of a command or a excute file,cause to excute the command or the excute file,'./' means the current directory.\n"); }/***function:jobs ***/void jobs(){ printf("this is jobs order.\n");}/***function: pwd. to display the current path.***/void pwd(){ char name[dir_max]; if(getcwd(name,dir_max)==NULL) { printf("error:getcwd\n"); exit(1); } printf("%s\n",name);}/***function: run_process.to fork a process of a excuting file***/void run_process(){ pid_t childpid; childpid = fork();//fork a child process if(childpid==-1) { perror("Failed to fork"); return; } if(childpid==0) { if(order[0]=='.'&&order[1]=='/')//the excuting file in current directory { int proname_len = strlen(order)-1; int i; char process_name[proname_len]; for(i = 0;i<proname_len;i++) { process_name[i] = order[i+1]; } if(getcwd(order,order_max)==NULL)//get the current directory(the path of the excuting file { printf("error:getcwd\n"); exit(1); } strcat(order,process_name);//get the exact name of the cxcuting file,and write it into the "order" array. } execl(order,NULL);//excute the child process printf("Invalid input:it is not a executive file"); return; } if(childpid!=wait(NULL))//the parent wait untill the child process ends. { perror("parent failed to wait due to signal or error"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -