Linux下用C函数实现简单的交互式shell.doc

Linux下用C函数实现简单的交互式shell.doc

ID:50953883

大小:30.50 KB

页数:9页

时间:2020-03-16

Linux下用C函数实现简单的交互式shell.doc_第1页
Linux下用C函数实现简单的交互式shell.doc_第2页
Linux下用C函数实现简单的交互式shell.doc_第3页
Linux下用C函数实现简单的交互式shell.doc_第4页
Linux下用C函数实现简单的交互式shell.doc_第5页
资源描述:

《Linux下用C函数实现简单的交互式shell.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、.专业整理.#include #include #include #include #include #include #include #include #include #defineBUF_LEN1024 #defineMAX_FILE_NAME256 #defineMAX_PROCESS_NUM2 #defineMAX_ARGC10 #defineBOOLint #defineTRUE1

2、 #defineFALSE0 #defineDELIM"

3、<>" enum { E_OPEN=0, E_PIPE, E_DUP2, E_EXECVP, E_FORK }; .学习帮手..专业整理. enum { FIRST=0, SECOND }; BOOLget_std_file(char*pos_std,char**std_file) { if(!pos_std) returnFALSE; *pos_std=''; *std_file=strtok(pos_std+1,DELIM); if(*std_file) returnTRUE; returnFALSE; } BOO

4、Lget_cmd_args(char*str,char**args) { inti=0; if(!str) returnFALSE; if(!(args[i++]=strtok(str,DELIM))) returnFALSE; while((args[i++]=strtok(NULL,DELIM))); .学习帮手..专业整理.returnTRUE; } intmain(intargc,char*argv[]) { charbuf[BUF_LEN]; char*first[MAX_ARGC]; char*second[MAX_ARGC]; char*stdin_file; ch

5、ar*stdout_file; char*pos; char*pos_stdin; char*pos_stdout; char*pos_pipe; inti; intpfd[2]; intfd; pid_tpids[MAX_PROCESS_NUM]; pid_tpid; while(1) { .学习帮手..专业整理.pos_stdin=NULL; pos_stdout=NULL; pos_pipe=NULL; buf[0]=''; printf("akaedu@caihui-desktop:%s#",getcwd(buf,BUF_LEN)); buf[0]='';

6、 bzero(first,sizeof(first)); bzero(second,sizeof(second)); if(!(fgets(buf,BUF_LEN,stdin))) break; if(buf[0]==''

7、

8、buf[0]=='') continue; pos=strchr(buf,''); if(pos) *pos=''; if(0==strcmp(buf,"exit")) break; pos_pipe=strchr(buf,'

9、'); pos_stdin=strchr(buf,'<'); pos_stdout=strchr(buf,'>'); i

10、f(pos_pipe) *pos_pipe='

11、'; .学习帮手..专业整理.if(pos_stdin) if(!get_std_file(pos_stdin,&stdin_file)) continue; if(pos_stdout) if(!get_std_file(pos_stdout,&stdout_file)) continue; if(!get_cmd_args(buf,first)) continue; if(pos_pipe) if(!get_cmd_args(pos_pipe+1,second)) continue; if(!pos_pipe) { pid=fork(

12、); if(0==pid) { if(pos_stdin) { fd=open(stdin_file,O_RDONLY); if(fd<0) { perror("openfail"); exit(E_OPEN); } dup2(fd,STDIN_FILENO); } if(pos_stdout) .学习帮手..专业整理.{ fd=open(stdout_file,O_WRONLY

13、O_CREAT

14、O_TRUNC,0644); if(fd<0

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。