Function dup2

Top 

Declaration:

int dup2(long oldhandle, long newhandle);

Description

Duplicates a file handle (oldhandle) onto an existing file handle (newhandle).

dup2 creates a new file handle that has the following common features with the original file handle:

Same open file or device

Same file pointer (that is, changing the file pointer of one changes the other)

Same access mode (read, write, read/write)

dup2 creates a new handle with the value of newhandle. If the file associated with newhandle is open, when dup2 is called, then the file will be closed.

newhandle and oldhandle are the file handles obtained from the creat, open, dup, or dup2 call.

Returned value

dup2 returns 0 on successful completion, and -1 otherwise. In the event of error, the errno global variable is set to one of the following values:

 EBADF   Bad file number

 EMFILE  Too many open files