dup() and dup2()(12.2)
newFD = dup( oldFD );
if( newFD < 0 ) { perror(“dup”); exit(1); }
or, to force the newFD to have a specific number:
returnCode = dup2( oldFD, newFD );
if(returnCode < 0) { perror(“dup2”); exit(1);}
In both cases, oldFD and newFD now refer to the same file
For dup2(), if newFD is open, it is first automatically closed
Note that dup() and dup2() refer to fd’s and not streams
- A useful system call to convert a stream to a fd is