tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / netlib / _dup.c
blob4ff7989d4e61535740ba0c8308bd13770e8c5853
1 /* $Id$
3 * _dup.c - duplicate a file descriptor (SAS/C)
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
7 * All rights reserved.
8 */
10 #include <ios1.h>
11 #include <fcntl.h>
12 #include <stdlib.h>
13 #include <dos.h>
14 #define USE_BUILTIN_MATH
15 #include <string.h>
16 #include <errno.h>
17 #include <dos/dos.h>
18 #include <proto/dos.h>
20 #include <bsdsocket.h>
22 /****** net.lib/dup ***********************************************************
24 NAME
25 dup, dup2 - duplicate an existing file descriptor
27 SYNOPSIS
28 #include <unistd.h>
30 int dup(int oldd)
32 int dup2(int oldd, int newd)
34 FUNCTION
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.
55 RETURN VALUES
56 The value -1 is returned if an error occurs in either call. The
57 external variable errno indicates the cause of the error.
59 BUGS
60 The current UFB implementation for SAS C allows only sockets to be
61 duplicated.
63 ERRORS
64 Dup() and dup2() fail if:
66 [EBADF] Oldd or newd is not a valid active descriptor
68 [EMFILE] Too many descriptors are active.
70 SEE ALSO
71 accept(), open(), close(), socket(), getdtablesize()
73 STANDARDS
74 Dup() and dup2() are expected to conform to IEEE Std 1003.1-1988
75 (``POSIX'').
77 COPYRIGHT
78 This manual page is copyright © 1980, 1991 Regents of the
79 University of California. All rights reserved.
81 *******************************************************************************
85 int
86 dup(int old_fd)
88 struct UFB *ufb;
89 int ufbflg;
91 * Check for the break signals
93 __chkabort();
96 * Find the ufb * for the given FD
98 if ((ufb = __chkufb(old_fd)) == NULL) {
99 errno = EBADF;
100 return -1;
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);
110 } else {
111 errno = EBADF;
112 return -1;