简体   繁体   English

在C中的自定义外壳中的Stdin重定向?

[英]Stdin redirection in custom shell in C?

So, Basically, I'm creating my own shell in C, which can handle redirection. 因此,基本上,我正在用C创建自己的shell,该shell可以处理重定向。 My stdout redirection is working but my stdin redirection does not work. 我的stdout重定向有效,但我的stdin重定向无效。

I'm trying to redirect stdin for Examples: 我正在尝试重定向标准输入示例:

            hw9> cat < out.txt
            hw9> grep if < out.txt

My code: 我的代码:

            if(strcmp(token[i],"<")==0)
            {
                token[i]=NULL;
                int fd0;

                if ((fd0 = open(token[i + 1], O_RDONLY)) < 0)
                {
                    perror("Couldn't open input file");
                    exit(0);
                }
                close(0);
                // dup2() copies content of fdo in input of preceeding file
                dup2(fd0, 0); // STDIN_FILENO here can be replaced by 0

                close(fd0); // necessary
            }

This is my if block to check and perform stdin redirection but this doesn't seem to work. 这是我的if块,用于检查和执行stdin重定向,但这似乎不起作用。 The expected output for cat out.txt is "helloo" and that should be the same output for cat < out.txt because the redirection will correspond to cat out.txt . cat out.txt的预期输出是"helloo" ,并且应该与cat < out.txt输出相同,因为重定向将对应于cat out.txt But cat < out.txt doesn't give any output. 但是cat < out.txt没有给出任何输出。 The content of the file out.txt is "helloo" . 文件out.txt的内容为"helloo"

hw9> cat out.txt

Tokens:
[0] 'cat'
[1] 'out.txt'
"helloo"
hw9> cat < out.txt

Tokens:
[0] 'cat'
[1] '<'
[2] 'out.txt'
hw9>

As you can see, the first call provides the correct output as expected but the second call does not prove any output. 如您所见,第一个调用提供了预期的正确输出,但是第二个调用没有证明任何输出。 Let me know if you need any more information. 让我知道您是否需要更多信息。 I tried multiple other ways, but nothing seems to work. 我尝试了多种其他方法,但似乎无济于事。 Thanks in advance! 提前致谢!

Edit: I got it to work! 编辑:我有它的工作! My mistake was calling the execvp() outside the if block. 我的错误是在if块外调用execvp() I called it inside the if-block, and it seems to work! 我在if块中调用它,它似乎起作用了! Thanks guys! 多谢你们!

Final code: 最终代码:

            if(strcmp(token[i],"<")==0)
            {
                token[i]=NULL;
                int fd0;

                if ((fd0 = open(token[i + 1], O_RDONLY)) < 0)
                {
                    perror("Couldn't open input file");
                    exit(0);
                }
                close(0);
                // dup2() copies content of fdo in input of preceeding file
                dup2(fd0, 0); // STDIN_FILENO here can be replaced by 0
                close(fd0); // necessary
                execvp(*token, token);
                perror("execvp");

            }

There isn't anything wrong with your code as far as I can tell. 据我所知,您的代码没有任何问题。 There error probably lies elsewhere, perhaps where you are calling exec . 错误可能出在其他地方,也许是您在调用exec For example, 例如,

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

int main(int argc, char *argv[]) {
    int fd0;
    char *args[3] = { NULL };
    args[0] = argv[1];

    if (strcmp(argv[2], "<") == 0) {
        if ((fd0 = open(argv[3], O_RDONLY)) < 0) {
            perror("open");
            exit(1);
        }

        dup2(fd0, STDIN_FILENO);
        close(fd0);

        execvp(argv[1], args);

    } else {
        args[1] = argv[2];
        execvp(argv[1], args);
    }

    return EXIT_SUCCESS;
}

Calling the above test.out I see the expected output, calling without redirection, 调用上面的test.out我看到了预期的输出,调用时没有重定向,

./test.out cat test.c

And with redirection (quoted since the shell would do its own redirection) 并带有重定向(被引用,因为shell会进行自己的重定向)

./test.out cat '<' test.c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM