| 1 | n/a | /* |
|---|
| 2 | n/a | * Public domain dup2() lookalike |
|---|
| 3 | n/a | * by Curtis Jackson @ AT&T Technologies, Burlington, NC |
|---|
| 4 | n/a | * electronic address: burl!rcj |
|---|
| 5 | n/a | * |
|---|
| 6 | n/a | * dup2 performs the following functions: |
|---|
| 7 | n/a | * |
|---|
| 8 | n/a | * Check to make sure that fd1 is a valid open file descriptor. |
|---|
| 9 | n/a | * Check to see if fd2 is already open; if so, close it. |
|---|
| 10 | n/a | * Duplicate fd1 onto fd2; checking to make sure fd2 is a valid fd. |
|---|
| 11 | n/a | * Return fd2 if all went well; return BADEXIT otherwise. |
|---|
| 12 | n/a | */ |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | #include <fcntl.h> |
|---|
| 15 | n/a | #include <unistd.h> |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | #define BADEXIT -1 |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | int |
|---|
| 20 | n/a | dup2(int fd1, int fd2) |
|---|
| 21 | n/a | { |
|---|
| 22 | n/a | if (fd1 != fd2) { |
|---|
| 23 | n/a | if (fcntl(fd1, F_GETFL) < 0) |
|---|
| 24 | n/a | return BADEXIT; |
|---|
| 25 | n/a | if (fcntl(fd2, F_GETFL) >= 0) |
|---|
| 26 | n/a | close(fd2); |
|---|
| 27 | n/a | if (fcntl(fd1, F_DUPFD, fd2) < 0) |
|---|
| 28 | n/a | return BADEXIT; |
|---|
| 29 | n/a | } |
|---|
| 30 | n/a | return fd2; |
|---|
| 31 | n/a | } |
|---|