3 * _dup.c - duplicate a file descriptor (SAS/C)
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
14 #define USE_BUILTIN_MATH
18 #include <proto/dos.h>
20 #include <bsdsocket.h>
22 /****** net.lib/dup ***********************************************************
25 dup, dup2 - duplicate an existing file descriptor
32 int dup2(int oldd, int newd)
35 Dup() duplicates an existing object descriptor and returns its value
36 to the calling program (newd = dup(oldd)). The argument oldd is a
37 small nonnegative integer index in the program's descriptor table.
38 The value must be less than the size of the table, which is returned
39 by getdtablesize(). The new descriptor returned by the call is the
40 lowest numbered descriptor currently not in use by the program.
42 The object referenced by the descriptor does not distinguish between
43 oldd and newd in any way. Thus if newd and oldd are duplicate
44 references to an open file, read() and write() calls all move a single
45 pointer into the file, and append mode, non-blocking I/O and
46 asynchronous I/O options are shared between the references. If a
47 separate pointer into the file is desired, a different object
48 reference to the file must be obtained by issuing an additional open()
49 call. The close-on-exec flag on the new file descriptor is unset.
51 In dup2(), the value of the new descriptor newd is specified. If this
52 descriptor is already in use, the descriptor is first deallocated as
53 if a close() call had been done first.
56 The value -1 is returned if an error occurs in either call. The
57 external variable errno indicates the cause of the error.
60 The current UFB implementation for SAS C allows only sockets to be
64 Dup() and dup2() fail if:
66 [EBADF] Oldd or newd is not a valid active descriptor
68 [EMFILE] Too many descriptors are active.
71 accept(), open(), close(), socket(), getdtablesize()
74 Dup() and dup2() are expected to conform to IEEE Std 1003.1-1988
78 This manual page is copyright © 1980, 1991 Regents of the
79 University of California. All rights reserved.
81 *******************************************************************************
91 * Check for the break signals
96 * Find the ufb * for the given FD
98 if ((ufb
= __chkufb(old_fd
)) == NULL
) {
103 ufbflg
= ufb
->ufbflg
;
106 * The brain dead UFB system won't allow duplicating ordinary files
108 if ((ufbflg
& UFB_SOCK
) == UFB_SOCK
) {
109 return Dup2Socket(old_fd
, -1);