define __KERNEL_STRICT_NAMES to avoid inclusion of kernel types on systems that carry...
[cake.git] / compiler / clib / dup2.c
blob2fe020fb2205155dfb34c896b22fa085be20987a
1 /*
2 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
3 $Id$
5 ANSI C function dup2().
6 */
8 #include <stdlib.h>
9 #include <errno.h>
10 #include "__open.h"
12 /*****************************************************************************
14 NAME */
15 #include <unistd.h>
17 int dup2 (
19 /* SYNOPSIS */
20 int oldfd,
21 int newfd
24 /* FUNCTION
25 Duplicates a file descriptor.
27 The object referenced by the descriptor does not distinguish between oldd
28 and newd in any way. Thus if newd and oldd are duplicate references to
29 an open file, read(), write() and lseek() calls all move a single
30 pointer into the file, and append mode, non-blocking I/O and asynchronous
31 I/O options are shared between the references. If a separate pointer
32 into the file is desired, a different object reference to the file must be
33 obtained by issuing an additional open(2) call. The close-on-exec flag
34 on the new file descriptor is unset.
36 INPUTS
37 oldfd - The file descriptor to be duplicated
38 newfd - The value of the new descriptor we want the old one to be duplicated in
40 RESULT
41 -1 for error or newfd on success
43 NOTES
44 This function must not be used in a shared library or
45 in a threaded application.
47 EXAMPLE
49 BUGS
51 SEE ALSO
52 bsdsocket.library/accept(), open(), close(), fcntl(), pipe()
53 bsdsocket.library/socket()
55 INTERNALS
57 ******************************************************************************/
59 fdesc *oldfdesc;
60 fdesc *newfdesc;
62 oldfdesc = __getfdesc(oldfd);
63 if (!oldfdesc)
65 errno = EBADF;
66 return -1;
69 newfdesc = __alloc_fdesc();
70 if(!newfdesc)
72 errno = ENOMEM;
73 return -1;
76 newfdesc->fdflags = 0;
77 newfdesc->fcb = oldfdesc->fcb;
79 newfd =__getfdslot(newfd);
80 if (newfd != -1)
82 newfdesc->fcb->opencount++;
83 __setfdesc(newfd, newfdesc);
86 return newfd;