Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Watcom C/C++ v10.0 : C library - <b>synopsis:</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
Synopsis:
    #include <io.h>
    int dup2( int handle, int handle2 );

Description:
    The dup2 function duplicates the file handle given by the argument
    handle.  The new file handle is identical to the original in that it
    references the same file or device, it has the same open mode (read
    and/or write) and it will have identical file position to the original
    (changing the position with one handle will result in a changed position
    in the other).

    The number of the new handle is handle2.  If a file already is opened
    with this handle, the file is closed before the duplication is
    attempted.

Returns:
    The dup2 function returns zero if successful.  Otherwise, -1 is returned
    and  errno is set to indicate the error.

Errors:
    When an error has occurred,  errno contains a value indicating the type
    of error that has been detected.

    EBADF
        The argument handle is not a valid open file handle or handle2 is
        out of range.

    EMFILE
        The number of file handles would exceed {OPEN_MAX}, or no file
        handles above handle2 are available.


See Also:
    chsize, close, creat, dup, eof, exec Functions Functions, filelength, fileno,
    fstat, isatty, lseek, open, read, setmode, sopen, stat, tell, write,
    umask

Example:
    #include <fcntl.h>
    #include <io.h>

    void main()
      {
        int handle, dup_handle;

        handle = open( "file",
                    O_WRONLY | O_CREAT | O_TRUNC | O_TEXT,
                    S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
        if( handle != -1 ) {
          dup_handle = 4;
          if( dup2( handle, dup_handle ) != -1 ) {

            /* process file */

            close( dup_handle );
          }
          close( handle );
        }
      }

Classification:
    POSIX 1003.1

Systems:
    All

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson