blob: ee1a7ad690d4cd2714b8f11abf5b3d4486c805c5 [file] [log] [blame]
Guido van Rossumbe0e9421993-12-24 10:32:00 +00001/*
2 * Public domain dup2() lookalike
3 * by Curtis Jackson @ AT&T Technologies, Burlington, NC
4 * electronic address: burl!rcj
5 *
6 * dup2 performs the following functions:
7 *
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.
12 */
13
14#include <fcntl.h>
15
16#define BADEXIT -1
17
18int
19dup2(fd1, fd2)
20int fd1, fd2;
21{
22 if (fd1 != fd2) {
Guido van Rossumb6775db1994-08-01 11:34:53 +000023#ifdef MPW
24 close (fd2); /* XXX RJW MPW does not implement F_GETFL but it does have dup */
25 fd2 = dup(fd1);
26#else
Guido van Rossumbe0e9421993-12-24 10:32:00 +000027 if (fcntl(fd1, F_GETFL) < 0)
28 return BADEXIT;
29 if (fcntl(fd2, F_GETFL) >= 0)
30 close(fd2);
31 if (fcntl(fd1, F_DUPFD, fd2) < 0)
32 return BADEXIT;
Guido van Rossumb6775db1994-08-01 11:34:53 +000033#endif
Guido van Rossumbe0e9421993-12-24 10:32:00 +000034 }
35 return fd2;
36}