dup(2) CLIX dup(2)
NAME
dup, dup2 - Duplicates an open file descriptor
LIBRARY
Standard C Library (libc.a)
SYNOPSIS
int dup(
int fildes );
int dup2(
int old ,
int new );
PARAMETERS
fildes Specifies an open file descriptor to duplicate.
old Specifies an open file descriptor.
new Specifies an open file descriptor that is returned by the dup2()
function.
DESCRIPTION
The fildes parameter is a file descriptor obtained from the creat(),
open(), dup(), fcntl(), or pipe() functions. The dup() function returns a
new file descriptor having the following in common with the original:
⊕ Same open file (or pipe).
⊕ Same file pointer (that is, both file descriptors share one file
pointer).
⊕ Same access mode (read, write or read/write).
The new file descriptor is set to remain open across exec() functions.
(See the fcntl() function.)
The file descriptor returned is the lowest one available.
The dup() and dup2() functions provide an alternate interface to the
service provided by the fcntl() function using the F_DUPFD command. The
call:
fid = dup (fildes);
is equivalent to:
2/94 - Intergraph Corporation 1
dup(2) CLIX dup(2)
fid = fcntl (fildes, F_DUPFD, 0);
The call:
fid = dup2 (old, new);
is equivalent to:
close (new);
fid = fcntl (fildes, F_DUPFD, new);
EXAMPLES
To duplicate an existing file descriptor:
new_fd = dup(existing_fd);
if (new_fd == -1)
perror("dup failed");
RETURN VALUES
Upon successful completion a non-negative integer (the file descriptor) is
returned. Otherwise, a value of -1 is returned and errno is set to
indicate the error.
ERRORS
The dup() and dup2 functions fail if one or both of the following are
true:
[EBADF] The old or fildes parameter is not a valid open file
descriptor.
[EMFILE] The NOFILES (defined in master.d) file descriptors are
currently open.
[EINTR] A signal was caught during the dup() functions.
[ENOLINK] The fildes parameter is on a remote machine and the link to
that machine is no longer active.
RELATED INFORMATION
Functions: close(2), creat(2), exec(2), fcntl(2), open(2), pipe(2),
lockf(3)
2 Intergraph Corporation - 2/94