38

I'm writing a function, which, given an argument, will either redirect the stdout to a file or read the stdin from a file. To do this I close the file descriptor associated with the stdout or stdin, so that when I open the file it opens under the descriptor that I just closed. This works, but the problem is that once this is done, I need to restore the stdout and stdin to what they should really be.

What I can do for stdout is open("/dev/tty",O_WRONLY); but I'm not sure why this works, and more importantly I don't know of an equivalent statement for stdin.

So I have, for stdout

close(1);
if (creat(filePath, O_RDWR) == -1)
{
    exit(1);
}

and for stdin

close(0);
if (open(filePath, O_RDONLY) == -1)
{
    exit(1);
}
1
  • O_RDWR is for open flags parameter not for creat mode parameter. Commented Mar 21, 2016 at 12:22

3 Answers 3

47

You should use dup() and dup2() to clone a file descriptor.

int stdin_copy = dup(0);
int stdout_copy = dup(1);
close(0);
close(1);

int file1 = open(...);
int file2 = open(...);

< do your work. file1 and file2 must be 0 and 1, because open always returns lowest unused fd >

close(file1);
close(file2);
dup2(stdin_copy, 0);
dup2(stdout_copy, 1);
close(stdin_copy);
close(stdout_copy);

However, there's a minor detail you might want to be careful with (from man dup):

The two descriptors do not share file descriptor flags (the close-on-execflag). The close-on-exec flag (FD_CLOEXEC; see fcntl(2)) for the duplicate descriptor is off.

If this is a problem, you might have to restore the close-on-exec flag, possibly using dup3() instead of dup2() to avoid race conditions.

Also, be aware that if your program is multi-threaded, other threads may accidentally write/read to your remapped stdin/stdout.

2
  • 2
    There are other problems if you are multi-threaded - if another thread opens a file between your close(0) (or close(1)) and the corresponding open(...), then its file will become stdin (or stdout). It's much better to use dup2() (or dup3) to force the file descriptor to be 0 (or 1), rather than relying on the "lowest unused" behaviour.
    – psmears
    Commented Sep 6, 2015 at 22:26
  • Is there any way to do that without cloning it(assume we can't use dup or dup2)?
    – milad
    Commented Oct 29, 2019 at 6:49
22

I think you can "save" the descriptors before redirecting:

int save_in, save_out;

save_in = dup(STDIN_FILENO);
save_out = dup(STDOUT_FILENO);

Later on you can use dup2 to restore them:

/* Time passes, STDIN_FILENO isn't what it used to be. */
dup2(save_in, STDIN_FILENO);

I am not doing any error checking in that example - you should.

1
  • Clean and to-the-point of what OP is asking for. Commented Aug 17, 2023 at 8:07
0

You could create a child process, and set up the redirection inside the child only. Then wait for the child to terminate, and continue working in the parent process. That way you don't have to worry about reversing your redirection at all.

Just look for examples of code using fork() and wait ().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.