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

标签 c fork stdout dup2 dup

我有一项作业正在处理,但我很难完成它。这个想法是编写一个程序 if.c 来执行一个程序,如果成功则执行第二个程序。我应该抑制第一个程序的标准输出并取消抑制第二个程序的标准输出。我在多项测试中收到错误消息。例如:“./if echo no then 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);
            }  
        }  
    }  

}  

最佳答案

你的错误在这里:

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

要将devNull用作STDOUT_FILENO,必须打开它进行写入:

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

关于c - 在 C 中,如何使用 dup2 将 STDOUT_FILENO 重定向到/dev/null,然后再重定向回其原始值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14846768/

相关文章:

c - 如何在 C 中创建正弦函数

c - 如何将参数传递给 C 语言中的 main() 函数?

C Linux 你如何将参数传递给另一个程序?

linux - 捕获其他程序stderr和stdout的程序

c - GCC 无法对 printf 中的 Var 声明进行垃圾回收

c - 这个 Makefile 有什么问题?

c++ - 当一个线程 forks() 时其他线程会发生什么?

您可以选择哪个子进程接收来自父进程的消息吗?

java - 将字节流输出到标准输出

http - 将 stdout 流式传输到网页