操作系统小组作业,实现一个简易shell,shell实现了下列命令
exit------退出终端命令
clr-------清屏命令
time-----时间命令
myshell----欢迎命令
quit-----终止命令
pwd-----路径命令
cat-----查看文件命令
help---帮助命令
help [参数]---查看某个具体的命令的注释
ls -l-----查看目录下的文件命令
只实现了简单的几个命令,仅做参考,代码如下
1 #include<bits/stdc++.h> 2 #include<unistd.h> 3 #include<stdio.h> 4 #include<sys/types.h> 5 #include<pwd.h> 6 #include<time.h> 7 #include<dirent.h> 8 #include<stdlib.h> 9 #include<sys/stat.h> 10 #include<fcntl.h> 11 #include <grp.h> 12 #include <sys/wait.h> 13 #include<memory.h> 14 using namespace std; 15 #define MAX_LINE 80 16 #define MAX_NAME_LEN 50 17 #define MAX_PATH_LEN 1000 18 int cmd_cnt; 19 char *cmd_array[MAX_LINE/2+1]; 20 string st; 21 void mytime(){ 22 int weekday; 23 int month; 24 time_t tvar; 25 struct tm *tp; 26 time(&tvar); 27 tp=localtime(&tvar);//获取本地时间 28 weekday=tp->tm_wday; 29 switch(weekday){//根据不同的值打印不同的星期 30 case 1: 31 printf("Mon "); 32 break; 33 case 2: 34 printf("Tues "); 35 break; 36 case 3: 37 printf("Wed "); 38 break; 39 case 4: 40 printf("Thur "); 41 break; 42 case 5: 43 printf("Fri "); 44 break; 45 case 6: 46 printf("Sat "); 47 break; 48 case 7: 49 printf("Sun "); 50 break; 51 default: 52 break; 53 } 54 month=1+tp->tm_mon;//必须要加1,经过查阅资料:tm_mon比实际的值少了1 55 switch(month){//根据不同的值打印月份名 56 case 1: 57 printf("Jan "); 58 break; 59 case 2: 60 printf("Feb "); 61 break; 62 case 3: 63 printf("Mar "); 64 break; 65 case 4: 66 printf("Apr "); 67 break; 68 case 5: 69 printf("May "); 70 break; 71 case 6: 72 printf("Jun "); 73 break; 74 case 7: 75 printf("Jul "); 76 break; 77 case 8: 78 printf("Aug "); 79 break; 80 case 9: 81 printf("Sep "); 82 break; 83 case 10: 84 printf("Oct "); 85 break; 86 case 11: 87 printf("Nov "); 88 break; 89 case 12: 90 printf("Dec "); 91 break; 92 default: 93 break; 94 } 95 printf("%d ",tp->tm_mday);//日期 96 printf("%d:",tp->tm_hour);//小时 97 printf("%d:",tp->tm_min);//分钟 98 printf("%d ",tp->tm_sec);//秒 99 printf("CST ");//CST,意思是China Standard Time 100 printf("%d ",1900+tp->tm_year);//必须加上1900,返回的值并不是完整的年份,比真实值少了1900 101 } 102 void welcome(){ 103 //如下是欢迎信息 104 //为了是程序更友好,加入了颜色 105 //颜色是紫色,背景色与shell相同 106 printf("e[32mwelcome to myshelle[0m "); 107 printf("e[32mit's a Lunix-like shell program made by use[0m "); 108 printf("e[32mhope you have a good time with it :-)e[0m "); 109 } 110 void mypwd(){ 111 char pathname[MAX_PATH_LEN]; 112 if(getcwd(pathname,MAX_PATH_LEN)){//获取路径名 113 printf("%s ",pathname); 114 } 115 else{//如果出错 116 perror("myshell: getcwd");//报错 117 exit(1); 118 } 119 } 120 121 void myquit(){ 122 printf("Thanks for your using,bye-bye! "); 123 sleep(1);//暂停1s,看上去视觉效果好一些 124 exit(0); 125 } 126 void exit(){ 127 exit(0); 128 } 129 void myclr(){ 130 cout << "