Reputation: 20360
I am trying to make a mini shell where commands are piped to each other. At some points in the code I do:
close(1) //closing stdout
and
close(0) //closing stdin
However later on I am forking so I want my new subprocess to reset everything. So how would I:
*function_name_here*(1) //reopening stdout
and
*function_name_here*(0) //reopening stdin
Upvotes: 1
Views: 1735
Reputation: 882756
If you're on a UNIX-type system (and you most likely are if you're calling fork
), you can generally do one of two things.
The first is to open /dev/tty
which will give you access to your terminal device (assuming the terminal device is what you want rather than the original file handle).
The second is to dup
that file handle before you close it so you have a usable copy. Then, you can use dup2
to get it back. Details for Linux here, or you could also execute man dup
or man dup2
.
Upvotes: 6