构建 shell - IO 问题

发布于 2024-12-08 00:31:12 字数 1656 浏览 1 评论 0原文

我正在为系统编程课程开发 shell。我在文件重定向方面遇到了一些问题。我刚刚将输出重定向到工作,例如“ls > a”,但是当我在 shell 中键入“cat < a”之类的命令时,它会删除文件中的所有内容。我觉得问题源于第二个 if 语句 - “fdin = open(_inputFile,777)”

如果是这种情况,将非常感谢推荐教程/其他示例的链接。

顺便说一句,我包含了整个函数,但是在它创建管道时,我还没有测试任何东西。我也不相信它能正常工作,但这可能是由于另一个文件中的错误造成的。

<代码> void Command::execute(){

    if(_numberOfSimpleCommands == 0){

    prompt();
        return;
    }

    //save input/output
    int defaultin = dup(0);
    int defaultout = dup(1);

    //initial input
    int fdin;
    if(_inputFile){
        fdin = open(_inputFile,0777);
    }else{
        //use default input
        fdin = dup(defaultin);
    }

    //execution
    int pid;
    int fdout;
    for(int i = 0; i < _numberOfSimpleCommands; i++){
        dup2(fdin,0);
        close(fdin);

        //setoutput
        if(i == _numberOfSimpleCommands -1){
            if(_outFile){
                fdout = creat(_outFile,0666);
            }else{
                fdout = dup(defaultout);
            }
        }else{
            int fdpipe[2];
            pipe(fdpipe);
            fdout = fdpipe[0];
            fdin = fdpipe[1];
        }
        dup2(fdout,1);
        close(fdout);

        //create child
        pid = fork();
        if(pid == 0){
            execvp(_simpleCommands[0]->_arguments[0],_simpleCommands[0]->_arguments);
            perror("-myshell");
            _exit(1);
        }
    }
    //restore IO defaults
    dup2(defaultin,0);
    dup2(defaultout,1);
    close(defaultin);
    close(defaultout);

    if(!_background){
        waitpid(pid,0,0);
    }
}

I am working on a shell for a systems programming class. I have been having some trouble with the file redirection. I just got redirecting the output to work, e.x. "ls > a" however when I type a command like "cat < a" into my shell it deletes everything in the file. I feel like the problem stems from the second if statement- "fdin = open(_inputFile,777)"

If that is the case a link to a recommended tutorial / other examples would be much appreciated.

On a side note, I included the entire function, however at the point which it creates the pipe, I have not tested anything there yet. I don't believe it works properly either though, but that may be from a mistake in another file.


void Command:: execute(){

    if(_numberOfSimpleCommands == 0){

    prompt();
        return;
    }

    //save input/output
    int defaultin = dup(0);
    int defaultout = dup(1);

    //initial input
    int fdin;
    if(_inputFile){
        fdin = open(_inputFile,0777);
    }else{
        //use default input
        fdin = dup(defaultin);
    }

    //execution
    int pid;
    int fdout;
    for(int i = 0; i < _numberOfSimpleCommands; i++){
        dup2(fdin,0);
        close(fdin);

        //setoutput
        if(i == _numberOfSimpleCommands -1){
            if(_outFile){
                fdout = creat(_outFile,0666);
            }else{
                fdout = dup(defaultout);
            }
        }else{
            int fdpipe[2];
            pipe(fdpipe);
            fdout = fdpipe[0];
            fdin = fdpipe[1];
        }
        dup2(fdout,1);
        close(fdout);

        //create child
        pid = fork();
        if(pid == 0){
            execvp(_simpleCommands[0]->_arguments[0],_simpleCommands[0]->_arguments);
            perror("-myshell");
            _exit(1);
        }
    }
    //restore IO defaults
    dup2(defaultin,0);
    dup2(defaultout,1);
    close(defaultin);
    close(defaultout);

    if(!_background){
        waitpid(pid,0,0);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此岸叶落 2024-12-15 00:31:12

您的调用 open(_inputFile, 0777) 不正确。 open 的第二个参数应该包含值的按位或组合,这些值指定访问模式和文件创建标志等(O_RDONLYO_WRONLY 等)。由于您要传递 0777,因此最终可能会同时包含 O_CREATO_TRUNC,这会导致 _inputFile删除了。您可能需要open(_inputFile, O_RDONLY)

Your call open(_inputFile, 0777) is incorrect. The second argument to open is supposed to contain a bitwise or'd combination of values that specify access mode and file creation flags, among other things (O_RDONLY, O_WRONLY, etc). Since you're passing 0777, that probably ends up containing both O_CREAT and O_TRUNC, which causes _inputFile to be erased. You probably want open(_inputFile, O_RDONLY).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文