Open System Services System Calls Reference Manual (G06.25+, H06.03+)
dup2(2) OSS System Calls Ref
erence Manual
NAME
dup2 - Duplicates and controls an open file descriptor
LIBRARY
G-series native OSS processes: system library
H-series OSS processes: implicit libraries
SYNOPSIS
#include <unistd.h>
int dup2(
int filedes,
int new);
PARAMETERS
filedes Specifies an open file descriptor obtained from a successful call to the accept(),
creat(), dup( ), dup2( ), fcntl( ), open(), pipe( ), socket(),orsocketpair() func-
tion.
new Specifies the open file descriptor that is returned by the dup2() function. If this
descriptor is already in use, it is first deallocated as if it had been closed.
DESCRIPTION
The dup2( ) function returns a new file descriptor on the open file specified by the filedes parame-
ter. If new is less than 0 (zero) or greater than or equal to the maximum number of opens permit-
ted, dup2( ) returns -1 with errno set to [EBADF].
The new file descriptor:
• Is the value specified as the new parameter:
— If filedes is a valid file descriptor and is equal to new, dup2( ) returns new
without closing it.
— If filedes is not a valid file descriptor, dup2( ) returns -1 and does not close new.
— The value returned is equal to the value of new upon successful completion, or it
is -1 upon failure.
• References the same open
• Returns the same file pointer as the original file (that is, both file descriptors share one
file pointer if the object is a file)
• Returns the same access mode (read, write, or read/write)
• Returns the same file status flags (that is, both file descriptors share the same file status
flags)
• Clears the close-on-exec flag (FD_CLOEXEC bit) associated with the new file descrip-
tor so that the file remains open across calls to any function in the exec, tdm_exec, and
tdm_spawn sets of functions
1−40 Hewlett-Packard Company 527186-003