修改之前的myshell使之支持输入输出重定向

1.open函数
这里写图片描述
    这个函数是打开一个文件(文件名叫pathname),以 flag 权限打开,flag 包括了以下几种 O_RDONLY(只读), O_WRONLY(只写), O_RDWR(读写),当文件打开成功时,函数返回所要打开的文件名, 当函数执行失败时,函数返回 -1.
2.write函数
这里写图片描述
     write函数是打开一个文件描述符为 fd 的文件,并将该文件的内容写到 buf 中, 同时期望写 count 个字节,当函数执行成功时返回往 buf 中所写的字节数, 失败时返回值为 -1
3.之前编写的自主shell进行修改,使其支持输入/输出/追加重定向

#include<stdio.h>
#include<fcntl.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>

int main()
{
        char buf[1024] = { 0 };
        while(1)
        {
        printf("mysell#");
        fflush(stdout);
        size_t s = read( 0, buf, sizeof( buf ) );
        if(s > 0)
        {
            buf[s - 1] = '\0';
            printf("%s\n", buf);
        }
        char* start = buf;
        char* _argv[32];
        char* argnext[32];
        _argv[0] = buf;
        int i = 1;
        while(*start)
        {
            if(*start == ' ')
            {
                *start = '\0';
                start ++;
                _argv[i++] = start;
            }
            else
            {
                start ++;
            }
        }
        _argv[i] = NULL;

            pid_t pid;
            pid = fork();
            if(pid < 0)
            {
                perror("fork");
                exit(1);
            }
            if(pid == 0)
            {
                int i = 0;
                int fd = 0;
                for(i = 0; _argv[i] != NULL; i++)
                {
                    if(strcmp(_argv[i], ">") == 0)
                    {
                        char* file_name = _argv[i + 1];
                        _argv[i] = NULL;
                        close(1);
                        fd = open(file_name, O_CREAT | O_WRONLY, 0664);
                        break;
                    }
                }
                execvp(_argv[0], _argv);
                close(fd);
            }
            else if(pid > 0)
            {
                int st = 0;
                waitpid(pid, NULL, 0);
            }
    }
    return 0;
}

    上一次写了一个简单的shell, 这次给其加上输入重定向
                            这里写图片描述
这里写图片描述
    虽然加入了输出重定向,但还没有加入输入重定向以及追加重定向,以及感到,在下一篇当中将会给其加入输入重定向, 追加重定向以及管道。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在myshell中实现输入重定向功能和输出重定向功能,可以使用dup2()函数来实现文件描述符的重定向,下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #define MAX_CMD_LEN 256 int main() { char cmd[MAX_CMD_LEN]; char *args[MAX_CMD_LEN]; char *input_file, *output_file; int input_fd, output_fd; int i, j; while (1) { printf("myshell> "); fgets(cmd, MAX_CMD_LEN, stdin); // 读取命令行输入 if (feof(stdin)) { // 用户输入Ctrl+D,退出程序 printf("\n"); break; } // 解析命令行参数 args[0] = strtok(cmd, " \n"); i = 1; while ((args[i] = strtok(NULL, " \n")) != NULL) { i++; } args[i] = NULL; // 查找输入输出重定向符号 input_file = NULL; output_file = NULL; for (j = 0; j < i; j++) { if (strcmp(args[j], "<") == 0) { // 输入重定向符号 input_file = args[j+1]; args[j] = NULL; break; } if (strcmp(args[j], ">") == 0) { // 输出重定向符号 output_file = args[j+1]; args[j] = NULL; break; } } // 执行命令 pid_t pid = fork(); if (pid == -1) { perror("fork"); exit(1); } else if (pid == 0) { // 子进程 // 输入重定向 if (input_file != NULL) { input_fd = open(input_file, O_RDONLY); if (input_fd == -1) { perror("open input file"); exit(1); } if (dup2(input_fd, STDIN_FILENO) == -1) { perror("dup2 input"); exit(1); } close(input_fd); } // 输出重定向 if (output_file != NULL) { output_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (output_fd == -1) { perror("open output file"); exit(1); } if (dup2(output_fd, STDOUT_FILENO) == -1) { perror("dup2 output"); exit(1); } close(output_fd); } // 执行命令 execvp(args[0], args); perror(args[0]); exit(1); } else { // 父进程 waitpid(pid, NULL, 0); } } return 0; } ``` 注意,在解析命令行参数的过程中,需要查找输入输出重定向符号,并将其后面的文件名作为重定向的文件。然后,在子进程中使用dup2()函数将文件描述符复制到标准输入或标准输出的文件描述符上。最后,关闭不需要的文件描述符。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值