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
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/ttywhich 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
dupthat file handle before you close it so you have a usable copy. Then, you can usedup2to get it back. Details for Linux here, or you could also executeman duporman dup2.