0
votes

I've seen this Question.

It says that we could make a copy of both the file descriptors of stdin and stdout, in order for us to reopen them at a later stage in the program.

My questions:

  • Could we open the file "/dev/tty" in O_WRONLY mode in order to reopen stdout?
  • Is this the correct method of doing it?
  • Is there any similar method to reopen stdin and stderr??
1
it says you should. not could. what's wrong with their proposed solution?Karoly Horvath
While reopening /dev/tty will work, it wouldn't handle situation when std* never were /dev/tty, but rather was redirected to other process/file by calling shell.keltar
I've seen using "/dev/null" for reopening stdin, stdout and stderr by just changing the opening modes. Here is that article. Why was /dev/null used??Abijith Kp
@KarolyHorvath There was no problem with the solution. I myself used that method till now. But the statement at the end was curious, and I wanted to know how it worked.Abijith Kp
It opens null to get valid std* descriptors. Reading/writing to null wouldn't fail (but will go nowhere, of course), as opposed to invalid file descriptor.keltar

1 Answers

1
votes

No, the method you posit is not the correct method. The /dev/tty "device" is your terminal device and it doesn't necessarily hold that that's where your standard output is going.

For example, if you run your program as:

yourprog >output.txt

then opening /dev/tty will not get you your starting standard output (which is the output.txt file).