东南大学操作系统课程设计-Shell的实现_第1页
东南大学操作系统课程设计-Shell的实现_第2页
东南大学操作系统课程设计-Shell的实现_第3页
东南大学操作系统课程设计-Shell的实现_第4页
东南大学操作系统课程设计-Shell的实现_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

操作系统实验报告基本信息完成人姓名:学号:报告日期:实验内容Shell的实现实现具有管道、重定向功能的shell,能够执行一些简单的基本命令,如进程执行、列目录等实验目的通过实验,让学生了解Shell实现机制设计思路和流程图1.对输入的命令进行解析2.简单命令的执行3.输入输出重定向的实现4.管道功能的实现主要数据结构及其说明使用数组和指针存放命令源程序并附上注释(关键部分)#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<sys/types.h>#include<string.h>#include<sys/stat.h>#include<signal.h>#include<fcntl.h>#definehist_size1024char*hist[hist_size];intf=0;//tosavechangeindirectoryinthead=0,filled=0;//1-parseuser'sinputvoidparse(char*word,char**argv){intcount=0;memset(argv,0,sizeof(char*)*(64));//copies0tothefirst64charactersofthestringpointedtobytheargumentargv.char*lefts=NULL;constchar*split="";//settingdelimeterwhile(1){char*p=strtok_r(word,split,&lefts);//ref-"strtok-r":/view/6991509.htm?fr=aladdinif(p==NULL){break;}argv[count]=p;//argvisanarray,itstoreseachvalueofyourinputdividedby""word=lefts;//printf("%s\n",argv[count]);//printf("%s\n",word);count++;}if(strcmp(argv[0],"exit")==0)exit(0);elseif(strcmp(argv[0],"cd")==0){intch=chdir(argv[1]);//ref-"chdir":/view/653970.htm?fr=aladdin/*if(ch<0){printf("Nosuchfileordirectory%d\n.",ch);}*/f=1;}}//2-getthefirstwordchar*trim(char*string)//removeextraspaces{inti=0;intj=0;char*ptr=malloc(sizeof(char*)*strlen(string));for(i=0;string[i]!='\0';i++)if(string[i]!=''){ptr[j]=string[i];j++;}ptr[j]='\0';string=ptr;//printf("%s\n",string);//printf("%d\n",j);returnstring;}//3-executethebasicordervoidexecute(char**argv){pid_tpid;intstatus;//forkchildprocessif((pid=fork())<0){printf("error:forkfailed.\n");exit(1);}elseif(pid==0){if(execvp(argv[0],argv)<0&&strcmp(argv[0],"cd"))//ref-"execvp":/view/1745420.htm?fr=aladdinprintf("error:invalidcommand.\n");exit(0);}else{while(wait(&status)!=pid)//ref-"wait":/cpp/html/289.html;}}//4-outputredirect//output:fileinwhichweneedtoprinttheoutputvoidexecute_file(char**argv,char*output){//printf("output:%s\n",output);//printf("argv:%s\n",*argv);pid_tpid;intstatus,flag;char*file=NULL;if((pid=fork())<0){printf("error:forkfailed.\n");exit(1);//fclose(fd1);}elseif(pid==0){if(strstr(output,">")>0)//returnsapointertofirstoccurenceafter>ornull{char*p=strtok_r(output,">",&file);output+=1;//change2file=trim(file);//getthefirstwordoffile,thatistosay,getthefirstwordafter>flag=1;//printf("file:%soutput:%s\n",*argv,output);intold_stdout=dup(1);//printf("mark\n");//outputredirectFILE*fp1=freopen(output,"w+",stdout);//ref-"freopen":/view/656692.htm?fr=aladdin//printf("mark\n");execute_file(argv,file);fclose(stdout);FILE*fp2=fdopen(old_stdout,"w");//ref-"fdopen":/view/656646.htm?fr=aladdin*stdout=*fp2;exit(0);}if(strstr(output,"<")>0){char*p=strtok_r(output,"<",&file);file=trim(file);flag=1;intfd=open(file,O_RDONLY);//ref-"open":/cpp/html/238.htmlif(fd<0){printf("Nosuchfileordirectory.");exit(0);}}if(strstr(output,"|")>0){fflush(stdout);printf("here");fflush(stdout);char*p=strtok_r(output,"|",&file);file=trim(file);flag=1;//fflush(stdout);printf("%s",file);fflush(stdout);char*args[64];parse(file,args);execute(args);}intold_stdout=dup(1);FILE*fp1=freopen(output,"w+",stdout);if(execvp(argv[0],argv)<0)printf("error:inexec");fclose(stdout);FILE*fp2=fdopen(old_stdout,"w");*stdout=*fp2;exit(0);}else{while(wait(&status)!=pid);}}//5-inputredirectvoidexecute_input(char**argv,char*output){pid_tpid;intfd;char*file;intflag=0;intstatus;if((pid=fork())<0){printf("error:forkfailed\n");exit(1);}elseif(pid==0){if(strstr(output,"<")>0){char*p=strtok_r(output,"<",&file);file=trim(file);flag=1;//printf("file:%soutput:%s\n",file,output);fd=open(output,O_RDONLY);if(fd<0){printf("Nosuchfileordirectory.");exit(0);}output=file;}if(strstr(output,">")>0){char*p=strtok_r(output,">",&file);file=trim(file);flag=1;fflush(stdout);//printf("file:%soutput:%s\n",file,output);fflush(stdout);intold_stdout=dup(1);FILE*fp1=freopen(file,"w+",stdout);execute_input(argv,output);fclose(stdout);FILE*fp2=fdopen(old_stdout,"w");*stdout=*fp2;exit(0);}if(strstr(output,"|")>0){char*p=strtok_r(output,"|",&file);file=trim(file);flag=1;char*args[64];parse(file,args);intpfds[2];pid_tpid,pid2;intstatus,status2;pipe(pfds);intfl=0;if((pid=fork())<0){printf("error:forkfailed\n");exit(1);}if((pid2=fork())<0){printf("error:forkfailed\n");exit(1);}if(pid==0&&pid2!=0){close(1);dup(pfds[1]);close(pfds[0]);close(pfds[1]);fd=open(output,O_RDONLY);close(0);dup(fd);if(execvp(argv[0],argv)<0){close(pfds[0]);close(pfds[1]);printf("error:inexec");fl=1;exit(0);}close(fd);exit(0);}elseif(pid2==0&&pid!=0&&fl!=1){close(0);dup(pfds[0]);close(pfds[1]);close(pfds[0]);if(execvp(args[0],args)<0){close(pfds[0]);close(pfds[1]);printf("error:inexec");exit(0);}}else{close(pfds[0]);close(pfds[1]);while(wait(&status)!=pid);while(wait(&status2)!=pid2);}exit(0);}fd=open(output,O_RDONLY);close(0);dup(fd);if(execvp(argv[0],argv)<0){printf("error:inexec");}close(fd);exit(0);}else{while(wait(&status)!=pid);}}//6-implementpipevoidexecute_pipe(char**argv,char*output){intpfds[2],pf[2],flag;char*file;pid_tpid,pid2,pid3;intstatus,status2,old_stdout;pipe(pfds);//createpipe//pfds[0]:readpfds[1]:writeintblah=0;char*args[64];char*argp[64];intfl=0;if((pid=fork())<0){printf("error:forkfailed\n");exit(1);}if((pid2=fork())<0){printf("error:forkfailed\n");exit(1);}if(pid==0&&pid2!=0){close(1);dup(pfds[1]);close(pfds[0]);close(pfds[1]);if(execvp(argv[0],argv)<0)//runthecommand{close(pfds[0]);close(pfds[1]);printf("error:inexec");fl=1;kill(pid2,SIGUSR1);exit(0);}}elseif(pid2==0&&pid!=0){if(fl==1){exit(0);}if(strstr(output,"<")>0){char*p=strtok_r(output,"<",&file);file=trim(file);flag=1;parse(output,args);//divideoutputtothearrayargsexecute_input(args,file);close(pfds[0]);close(pfds[1]);exit(0);}if(strstr(output,">")>0){char*p=strtok_r(output,">",&file);file=trim(file);flag=1;//fflush(stdout);printf("file:%soutput:%s\n",file,output);fflush(stdout);parse(output,args);blah=1;}else{parse(output,args);}close(0);dup(pfds[0]);close(pfds[1]);close(pfds[0]);if(blah==1){old_stdout=dup(1);FILE*fp1=freopen(file,"w+",stdout);}if(execvp(args[0],args)<0){fflush(stdout);printf("error:inexec%d",pid);kill(pid,SIGUSR1);close(pfds[0]);close(pfds[1]);}fflush(stdout);printf("HERE");//kill(pid,SIGUSR1);if(blah==1){fclose(stdout);FILE*fp2=fdopen(old_stdout,"w");*stdout=*fp2;}}else{close(pfds[0]);close(pfds[1]);while(wait(&status)!=pid);while(wait(&status2)!=pid2);}}//7-implementpipevoidexecute_pipe2(char**argv,char**args,char**argp){intstatus;inti;intpipes[4];pipe(pipes);pipe(pipes+2);if(fork()==0){dup2(pipes[1],1);close(pipes[0]);close(pipes[1]);close(pipes[2]);close(pipes[3]);if(execvp(argv[0],argv)<0){fflush(stdout);printf("error:inexec");fflush(stdout);close(pipes[0]);close(pipes[1]);close(pipes[2]);close(pipes[3]);exit(1);}}else{if(fork()==0){dup2(pipes[0],0);dup2(pipes[3],1);close(pipes[0]);close(pipes[1]);close(pipes[2]);close(pipes[3]);if(execvp(args[0],args)<0){fflush(stdout);printf("error:inexec");fflush(stdout);close(pipes[0]);close(pipes[1]);close(pipes[2]);close(pipes[3]);exit(1);}}else{if(fork()==0){dup2(pipes[2],0);close(pipes[0]);close(pipes[1]);close(pipes[2]);close(pipes[3]);if(execvp(argp[0],argp)<0){fflush(stdout);printf("error:inexec");fflush(stdout);close(pipes[0]);close(pipes[1]);close(pipes[2]);close(pipes[3]);exit(1);}}}}close(pipes[0]);close(pipes[1]);close(pipes[2]);close(pipes[3]);for(i=0;i<3;i++)wait(&status);}/*intmain(){charword[1024]="ls>a.txt";char*argv[1024];char*file=NULL;char*p=strtok_r(word,">",&file);file=trim(file);parse(word,argv);execute_file(argv,file);}*/intmain(){charline[1024];char*argv[64];char*args[64];char*left;size_tsize=0;charch;intcount=0;char*tri;char*second;char*file;inti;for(i=0;i<hist_size;i++){hist[i]=(char*)malloc(150);}while(1){count=0;intflag=0;char*word=NULL;char*dire[]={"pwd"};fflush(stdout);printf("SHELL~");fflush(stdout);execute(dire);//printthecurrentdirectory,wecanalsousegetcwd()printf("$");intlen=getline(&word,&size,stdin);if(*word=='\n')continue;word[len-1]='\0';char*file=NULL;inti=0;char*temp=(char*)malloc(150);strcpy(temp,word);parse(temp,argv);strcpy(hist[(head+1)%hist_size],word);//storinganentryinhistoryhead=(head+1)%hist_size;filled=filled+1;for(i=0;word[i]!='\0';i++){if(word[i]=='>'){//printf("%s\n",word);//hastheinitialcommandchar*p=strtok_r(word,">",&file);file=trim(file);//printf("%s\n",file);flag=1;break;}elseif(word[i]=='<'){char*p=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论