dup2

Forces a file descriptor to reference the same open file as another file descriptor.

Library:LibC
Classification:POSIX
Service:File and Directory I/O

Syntax

  #include <unistd.h> 
   
  int dup2 (
     int   fildes1,   
     int   fildes2);
  

Parameters

fildes1

(IN) Specifies the file descriptor to be duplicated.

fildes2

(IN) Specifies the file descriptor to change so that it references the same file as the fildes1 parameter.

Return Values

If successful, returns a nonnegative integer that is the file descriptor. Otherwise, returns a -1 and sets errno to one of the following values:

Decimal

Constant

Description

4

EBADF

The fildes1 or fildes2 parameters aren't valid file descriptors.

11

EMFILE

Too many open files.

63

EINTR

A signal interrupted the call.

Remarks

The fildes1 parameter must be a file descriptor to a file that is already open. If fildes2 references a file that is already open, that file is closed before fildes2 is forced to reference the file for fildes1.

See Also