DUP(3)                     POSIX Programmer's Manual                    DUP(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.


NNAAMMEE
       dup, dup2 -- duplicate an open file descriptor

SSYYNNOOPPSSIISS
       #include <unistd.h>

       int dup(int _f_i_l_d_e_s);
       int dup2(int _f_i_l_d_e_s, int _f_i_l_d_e_s_2);

DDEESSCCRRIIPPTTIIOONN
       The _d_u_p() function provides an alternative interface to the service
       provided by _f_c_n_t_l() using the F_DUPFD command. The call _d_u_p(_f_i_l_d_e_s)
       shall be equivalent to:

           ffccnnttll((ffiillddeess,, FF__DDUUPPFFDD,, 00));;

       The _d_u_p_2() function shall cause the file descriptor _f_i_l_d_e_s_2 to refer to
       the same open file description as the file descriptor _f_i_l_d_e_s and to
       share any locks, and shall return _f_i_l_d_e_s_2.  If _f_i_l_d_e_s_2 is already a
       valid open file descriptor, it shall be closed first, unless _f_i_l_d_e_s is
       equal to _f_i_l_d_e_s_2 in which case _d_u_p_2() shall return _f_i_l_d_e_s_2 without
       closing it. If the close operation fails to close _f_i_l_d_e_s_2, _d_u_p_2() shall
       return -1 without changing the open file description to which _f_i_l_d_e_s_2
       refers. If _f_i_l_d_e_s is not a valid file descriptor, _d_u_p_2() shall return
       -1 and shall not close _f_i_l_d_e_s_2.  If _f_i_l_d_e_s_2 is less than 0 or greater
       than or equal to {OPEN_MAX}, _d_u_p_2() shall return -1 with _e_r_r_n_o set to
       [[EEBBAADDFF]].

       Upon successful completion, if _f_i_l_d_e_s is not equal to _f_i_l_d_e_s_2, the
       FD_CLOEXEC flag associated with _f_i_l_d_e_s_2 shall be cleared. If _f_i_l_d_e_s is
       equal to _f_i_l_d_e_s_2, the FD_CLOEXEC flag associated with _f_i_l_d_e_s_2 shall not
       be changed.

       If _f_i_l_d_e_s refers to a typed memory object, the result of the _d_u_p_2()
       function is unspecified.

RREETTUURRNN VVAALLUUEE
       Upon successful completion a non-negative integer, namely the file
       descriptor, shall be returned; otherwise, -1 shall be returned and
       _e_r_r_n_o set to indicate the error.

EERRRROORRSS
       The _d_u_p() function shall fail if:

       EEBBAADDFF  The _f_i_l_d_e_s argument is not a valid open file descriptor.

       EEMMFFIILLEE All file descriptors available to the process are currently
              open.

       The _d_u_p_2() function shall fail if:

       EEBBAADDFF  The _f_i_l_d_e_s argument is not a valid open file descriptor or the
              argument _f_i_l_d_e_s_2 is negative or greater than or equal to
              {OPEN_MAX}.

       EEIINNTTRR  The _d_u_p_2() function was interrupted by a signal.

       The _d_u_p_2() function may fail if:

       EEIIOO    An I/O error occurred while attempting to close _f_i_l_d_e_s_2.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
   RReeddiirreeccttiinngg SSttaannddaarrdd OOuuttppuutt ttoo aa FFiillee SS
       The following example closes standard output for the current processes,
       re-assigns standard output to go to the file referenced by _p_f_d, and
       closes the original file descriptor to clean up.

           ##iinncclluuddee <<uunniissttdd..hh>>
           ......
           iinntt ppffdd;;
           ......
           cclloossee((11));;
           dduupp((ppffdd));;
           cclloossee((ppffdd));;
           ......

   RReeddiirreeccttiinngg EErrrroorr MMeessssaaggeess
       The following example redirects messages from _s_t_d_e_r_r to _s_t_d_o_u_t.

           ##iinncclluuddee <<uunniissttdd..hh>>
           ......
           dduupp22((11,, 22));;
           ......

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       Implementations may use file descriptors that must be inherited into
       child processes for the child process to remain conforming, such as for
       message catalog or tracing purposes. Therefore, an application that
       calls _d_u_p_2() with an arbitrary integer for _f_i_l_d_e_s_2 risks non-conforming
       behavior, and _d_u_p_2() can only portably be used to overwrite file
       descriptor values that the application has obtained through explicit
       actions, or for the three file descriptors corresponding to the
       standard file streams. In order to avoid a race condition of leaking an
       unintended file descriptor into a child process, an application should
       consider opening all file descriptors with the FD_CLOEXEC bit set
       unless the file descriptor is intended to be inherited across _e_x_e_c.

RRAATTIIOONNAALLEE
       The _d_u_p() function is redundant. Its services are also provided by the
       _f_c_n_t_l() function. It has been included in this volume of POSIX.1-2008
       primarily for historical reasons, since many existing applications use
       it. On the other hand, the _d_u_p_2() function provides unique services, as
       no other interface is able to atomically replace an existing file
       descriptor.

       The _d_u_p_2() function is not marked obsolescent because it presents a
       type-safe version of functionality provided in a type-unsafe version by
       _f_c_n_t_l().  It is used in the POSIX Ada binding.

       The _d_u_p_2() function is not intended for use in critical regions as a
       synchronization mechanism.

       In the description of [[EEBBAADDFF]], the case of _f_i_l_d_e_s being out of range is
       covered by the given case of _f_i_l_d_e_s not being valid. The descriptions
       for _f_i_l_d_e_s and _f_i_l_d_e_s_2 are different because the only kind of
       invalidity that is relevant for _f_i_l_d_e_s_2 is whether it is out of range;
       that is, it does not matter whether _f_i_l_d_e_s_2 refers to an open file when
       the _d_u_p_2() call is made.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _c_l_o_s_e(), _f_c_n_t_l(), _o_p_e_n()

       The Base Definitions volume of POSIX.1-2008, <<uunniissttdd..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                               DUP(3)
