Warning: main(/www/www/htdocs/style/globals.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/d/dup2.html on line 1

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/globals.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/d/dup2.html on line 1

Warning: main(/www/www/htdocs/style/header.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/d/dup2.html on line 8

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/header.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/d/dup2.html on line 8

dup2()

Duplicate a file descriptor, specifying the new descriptor

Synopsis:

#include <unistd.h>

int dup2( int filedes, 
          int filedes2 );

Arguments:

filedes
The file descriptor that you want to duplicate.
filedes2
The number that you want to use for the new file descriptor.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The dup2() function duplicates the file descriptor specified by filedes. The number of the new file descriptor will be filedes2. If a file already is opened with this descriptor, the file is closed before the duplication is attempted.

The new file descriptor:

Calling:

dup_filedes = dup2( filedes, filedes2 );

Is the same as:

close( filedes2 );
dup_filedes = fcntl( filedes , F_DUPFD, filedes2 );

Returns:

The value of filedes2 for success, or -1 if an error occurs (errno is set).

Errors:

EBADF
The file descriptor, filedes isn't a valid open file descriptor, or filedes2 is out of range.
EMFILE
There are already OPEN_MAX file descriptors in use.
ENOSYS
The dup2() function isn't implemented for the filesystem specified by filedes.

Examples:

#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>

int main( void )
{
    int filedes , dup_filedes ;

    filedes = open( "file",
        O_WRONLY | O_CREAT | O_TRUNC,
        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );

    if( filedes != -1 ) {
        dup_filedes = 4;
        if( dup2( filedes, dup_filedes ) != -1 ) {
            /* process file */
            /* ... */
        
            close( dup_filedes );
        }
        close( filedes );
        
        return EXIT_SUCCESS;
    }
    
    return EXIT_FAILURE;
}

Classification:

POSIX 1003.1

Safety:
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

chsize(), close(), creat(), dup(), eof(), errno, execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), fcntl(), fileno(), fstat(), isatty(), lseek(), open(), read(), sopen(), stat(), tell(), umask(), write()


Warning: main(/www/www/htdocs/style/footer.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/d/dup2.html on line 222

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/footer.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/d/dup2.html on line 222