Issue #7701: Fix crash in binascii.b2a_uu() in debug mode when given a
[python.git] / Python / dup2.c
blobba7413a0ca7b5265f1dfb30f8843c91d4f3b8836
1 /*
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 <fcntl.h>
16 #define BADEXIT -1
18 int
19 dup2(int fd1, int fd2)
21 if (fd1 != fd2) {
22 if (fcntl(fd1, F_GETFL) < 0)
23 return BADEXIT;
24 if (fcntl(fd2, F_GETFL) >= 0)
25 close(fd2);
26 if (fcntl(fd1, F_DUPFD, fd2) < 0)
27 return BADEXIT;
29 return fd2;