Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

accept(2)

close(2)

fcntl(2V)

getdtablesize(2)

lseek(2)

open(2V)

pipe(2)

read(2V)

socket(2)

socketpair(2)

write(2V)

DUP(2)  —  SYSTEM CALLS

NAME

dup, dup2 − duplicate a descriptor

SYNOPSIS

int dup(oldd)
int oldd;

int dup2(oldd, newd)
int oldd, newd;

DESCRIPTION

dup() duplicates an existing object descriptor.  The argument oldd is a small non-negative integer index in the per-process descriptor table.  The value must be less than the size of the table, which is returned by getdtablesize(2).  The new descriptor returned by the call is the lowest numbered descriptor that is not currently in use by the process. 

In the second form of the call, the value of the new descriptor desired is specified.  If that descriptor is already in use, the descriptor is first deallocated as if a close(2) call had been done first. 

The new descriptor has the following in common with the original:

• It refers to the same object that the old descriptor referred to. 

• It uses the same file pointer as the old descriptor.  (that is, both file descriptors share one file pointer). 

• It has the same access mode (read, write or read/write) as the old descriptor. 

Thus if newd and oldd are duplicate references to an open file, read(2V), write(2V) and lseek(2) calls all move a single pointer into the file, and append mode, non-blocking I/O and asynchronous I/O options are shared between the references.  If a separate pointer into the file is desired, a different object reference to the file must be obtained by issuing an additional open(2V) call.  The close-on-exec flag on the new file descriptor is unset. 

The new file descriptor is set to remain open across exec system calls.  See fcntl(2V). 

RETURN VALUE

The value −1 is returned if an error occurs in either call.  The external variable errno indicates the cause of the error. 

ERRORS

dup() and dup2() fail if:

EBADF oldd or newd is not a valid active descriptor. 

EMFILE Too many descriptors are active. 

SEE ALSO

accept(2), close(2), fcntl(2V), getdtablesize(2), lseek(2), open(2V), pipe(2), read(2V), socket(2), socketpair(2), write(2V)

Sun Release 4.0  —  Last change: 22 March 1989

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026