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

上传人:rrsccc 文档编号:10423858 上传时间:2021-05-15 格式:DOCX 页数:14 大小:22.26KB
返回 下载 相关 举报
Linux下用C函数实现简单的交互式shell.docx_第1页
第1页 / 共14页
Linux下用C函数实现简单的交互式shell.docx_第2页
第2页 / 共14页
Linux下用C函数实现简单的交互式shell.docx_第3页
第3页 / 共14页
Linux下用C函数实现简单的交互式shell.docx_第4页
第4页 / 共14页
亲,该文档总共14页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《Linux下用C函数实现简单的交互式shell.docx》由会员分享,可在线阅读,更多相关《Linux下用C函数实现简单的交互式shell.docx(14页珍藏版)》请在三一文库上搜索。

1、. 专业整理.#include #include #include #include #include #include #include #include #include #define BUF_LEN 1024#define MAX_FILE_NAME 256#define MAX_PROCESS_NUM 2#define MAX_ARGC 10#define BOOL int#define TRUE 1#define FALSE 0#define DELIM |.学习帮手 . 专业整理.enumE_OPEN = 0,E_PIPE,E_DUP2,E_EXECVP,E_FORK;enumF

2、IRST = 0,SECOND;BOOL get_std_file(char *pos_std, char *std_file).学习帮手 . 专业整理.if (!pos_std)return FALSE;*pos_std = 0;*std_file = strtok(pos_std + 1, DELIM);if (*std_file)return TRUE;return FALSE;BOOL get_cmd_args(char *str, char *args)int i = 0;if (!str)return FALSE;if (!(argsi+ = strtok(str, DELIM)r

3、eturn FALSE;while(argsi+ = strtok(NULL, DELIM);.学习帮手 . 专业整理.return TRUE;int main(int argc, char *argv)char bufBUF_LEN;char *firstMAX_ARGC;char *secondMAX_ARGC;char *stdin_file;char *stdout_file;char *pos;char *pos_stdin;char *pos_stdout;char *pos_pipe;.学习帮手 . 专业整理.int i;int pfd2;int fd;pid_t pidsMAX

4、_PROCESS_NUM;pid_t pid;while(1)pos_stdin = NULL;pos_stdout = NULL;pos_pipe = NULL;buf0 = 0;printf(akaeducaihui-desktop:%s#, getcwd(buf, BUF_LEN);.学习帮手 . 专业整理.buf0 = 0;bzero(first, sizeof(first);bzero(second, sizeof(second);if (!(fgets(buf, BUF_LEN, stdin)break;if (buf0 = n | buf0 = 0)continue;pos =

5、strchr(buf, n);if (pos)*pos = 0;if (0 = strcmp(buf, exit)break;pos_pipe = strchr(buf, |);pos_stdin = strchr(buf, );if (pos_pipe).学习帮手 . 专业整理.*pos_pipe = |;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_a

6、rgs(buf, first)continue;if (pos_pipe)if (!get_cmd_args(pos_pipe + 1, second)continue;if (!pos_pipe)pid = fork();if (0 = pid)if (pos_stdin).学习帮手 . 专业整理.fd = open(stdin_file, O_RDONLY);if (fd 0)perror(open failn);exit(E_OPEN);dup2(fd, STDIN_FILENO);if (pos_stdout)fd = open(stdout_file, O_WRONLY | O_CR

7、EAT | O_TRUNC, 0644);if (fd 0)perror(open failn);exit(E_OPEN);.学习帮手 . 专业整理.dup2(fd ,STDOUT_FILENO);execvp(first0, first);perror(execvp failn);exit(E_EXECVP);elsewait(NULL);elseif (pipe(pfd) 0)perror(pipe failn);exit(E_PIPE);.学习帮手 . 专业整理.pid = getpid();for (i = 0; i 0; i+)pid = fork();if (pid 0)close

8、(pfd0);close(pfd1);for (i = 0; i MAX_PROCESS_NUM; i+)if (pidsi = getpid()switch (i)case FIRST:close(pfd0);if (pos_stdin).学习帮手 . 专业整理.fd = open(stdin_file, O_RDONLY);if (fd 0)perror(open failn);exit(E_OPEN);dup2(fd, STDIN_FILENO);execvp(first0, first);perror(execvp failn);exit(E_EXECVP);break;case SECOND:close(pfd1);if (pos_stdout).学习帮手 . 专业整理.fd = open(stdout_file, O_WRONLY | O_CREAT | O_TRUNC,0644);if (fd 0)for (i = 0; i MAX_PROCESS_NUM; i+)wait(NULL);return 0;.学习帮手 .

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1