2 * Public domain dup2() lookalike
3 * by Curtis Jackson @ AT&T Technologies, Burlington, NC
4 * electronic address: burl!rcj
6 * dup2 performs the following functions:
8 * Check to make sure that fd1 is a valid open file descriptor.
9 * Check to see if fd2 is already open; if so, close it.
10 * Duplicate fd1 onto fd2; checking to make sure fd2 is a valid fd.
11 * Return fd2 if all went well; return BADEXIT otherwise.
14 #include "ruby/config.h"
16 #if defined(HAVE_FCNTL)
20 #if !defined(HAVE_FCNTL) || !defined(F_DUPFD)
27 dup2(int fd1
, int fd2
)
29 #if defined(HAVE_FCNTL) && defined(F_DUPFD)
32 if (fcntl(fd1
, F_GETFL
) < 0)
34 if (fcntl(fd2
, F_GETFL
) >= 0)
39 if (fcntl(fd1
, F_DUPFD
, fd2
) < 0)
47 if (fd1
== fd2
) return 0;
49 for (i
=0; i
<256; i
++) {
50 fd
= fds
[i
] = dup(fd1
);
56 if (fd
== fd2
) return 0;