在C中,如何使用dup2将STDOUT_FILENO重定向到/dev/null,然后稍后再重定向回其原始值?

在C中,如何使用dup2将STDOUT_FILENO重定向到/dev/null,然后稍后再重定向回其原始值?,c,fork,stdout,dup2,dup,C,Fork,Stdout,Dup2,Dup,我正在做一项作业,完成起来很困难。其思想是编写一个程序if.c执行一个程序,如果成功,则执行第二个程序。我应该抑制第一个程序的标准输出,并按下第二个程序的标准输出。我在多个测试中收到错误消息。例如:“./如果echo no,则echo yes”返回“echo:write error:Bad file descriptor”。我试着在网上找到我做错了什么,但没有成功 这是我的密码: #include <fcntl.h> #include <sys/wait.h> #incl

我正在做一项作业,完成起来很困难。其思想是编写一个程序if.c执行一个程序,如果成功,则执行第二个程序。我应该抑制第一个程序的标准输出,并按下第二个程序的标准输出。我在多个测试中收到错误消息。例如:“./如果echo no,则echo yes”返回“echo:write error:Bad file descriptor”。我试着在网上找到我做错了什么,但没有成功

这是我的密码:

#include <fcntl.h>
#include <sys/wait.h>
#include <stdio.h>
#include "tlpi_hdr.h"

int main(int argc, char *argv[])
{
    if(argc < 4){
        fprintf(stderr,"Incorrect number of arguments.\n");
        exit(EXIT_FAILURE);
    }

    int thenArg = 0;
    char then[4];
    strcpy(then,"then");
    for(int x=1; x<argc; x++){
        if(strncmp(argv[x], then, 4) == 0) thenArg = x;
    }

    if(thenArg == 0){
        fprintf(stderr,"No 'then' argument found.\n");
        exit(EXIT_FAILURE);
    }

    int save_out = dup(STDOUT_FILENO);
    if(save_out == -1){
        fprintf(stderr,"Error in dup(STDOUT_FILENO)\n");
        exit(EXIT_FAILURE);
    }

    int devNull = open("/dev/null",0);
    if(devNull == -1){
        fprintf(stderr,"Error in open('/dev/null',0)\n");
        exit(EXIT_FAILURE);
    }

    int dup2Result = dup2(devNull, STDOUT_FILENO);
    if(dup2Result == -1) {
        fprintf(stderr,"Error in dup2(devNull, STDOUT_FILENO)\n");
        exit(EXIT_FAILURE);
    }

    int program1argLocation = 1;
    int program2argLocation = thenArg + 1;
    int program1argCount = thenArg-1;
    int program2argCount = argc-(program2argLocation);
    char *program1args[program1argCount+1];
    char *program2args[program2argCount+1];

    for(int i=0; i<program1argCount; i++){
        program1args[i]=argv[program1argLocation + i];
    }
    program1args[program1argCount] = NULL;
    for(int i=0; i<program2argCount; i++){
        program2args[i]=argv[program2argLocation + i];
    }
    program2args[program2argCount] = NULL;

    pid_t pid = fork();
    int child_status;
    switch (pid) {
    case -1:
        fprintf(stderr,"Fork failed\n");
        exit(EXIT_FAILURE);

    case 0: //child
        //child will run program 1
        if(execvp(program1args[0],&program1args[0]) == -1){
            fprintf(stderr,"Program 1 Failed.\n");
            exit(EXIT_FAILURE);
        }  

    default: //parent
        //parent will run program2
        pid = wait(&child_status);

        if(WEXITSTATUS(child_status) == 0){
            dup2(save_out, STDOUT_FILENO);

            int prog2status = execvp(program2args[0],&program2args[0]);
            if(prog2status == -1) {
                fprintf(stderr,"Program 2 failed.\n");
                exit(EXIT_FAILURE);
            }  
        }  
    }  

}  
#包括
#包括
#包括
#包括“tlpi_hdr.h”
int main(int argc,char*argv[])
{
如果(argc<4){
fprintf(stderr,“参数数量不正确。\n”);
退出(退出失败);
}
int-thenArg=0;
char-then[4];
strcpy(then,“then”);
对于(int x=1;x您的错误如下:

int devNull = open("/dev/null",0);
要将
devNull
用作
STDOUT\u FILENO
,必须将其打开以进行写入:

int devNull = open("/dev/null", O_WRONLY);

char-then[4];strcpy(then,“then”);
是缓冲区溢出。为什么不直接使用
if(0==strcmp(argv[x],“then”)
if(0==strncmp(argv[x],“then”,4))
取而代之的是?我想我在第一次实现它时就尝试过了,但遇到了一些错误。我会记住这一点,下次遇到同样的问题时再参考它。谢谢!还不习惯C,所以像这样的输入都很棒。