Search code examples
cglibc

Change default output stream on argp


By default argp uses stdout.

In the argp-parse.c source code, you can see that in the init function the value is initialized to stdout:

parser->state.out_stream = stdout;

The struct argp_state is internally used argp and stores the value out_stream.

I would like to change the default behaviour and redirect the stream to another file descriptor.


Solution

  • What you can do is redirect stdout and restore it later:

    int orig_stdout = dup(STDOUT_FILENO);
    extern int my_other_file_descriptor;
    dup2(my_other_file_descriptor, STDOUT_FILENO);
    
    // parse args
    
    dup2(orig_stdout, STDOUT_FILENO);