5 ms·
>Since everything is a file descriptor on UNIX, you can just call dup2(2) and this won't have any repercussion on other parts of the program. You chose an unfo
by reubenmorais 12y ago
>Since everything is a file descriptor on UNIX, you can just call dup2(2) and this won't have any repercussion on other parts of the program.
You chose an unfortunate example. From the dup2 documentation:
The object referenced by the descriptor does not distinguish between fildes and fildes2 in any way.
Thus if fildes2 and fildes are duplicate references to an open file, read(2), write(2) and lseek(2)
calls all move a single pointer into the file, and append mode, non-blocking I/O and asynchronous I/O
options are shared between the references. If a separate pointer into the file is desired, a different
object reference to the file must be obtained by issuing an additional open(2) call. The close-on-exec
flag on the new file descriptor is unset.
- evmar 12y agoAnd on top of that, you have to worry about CLOEXEC if your process can fork, so you need to consider using dup3.