Commit | Line | Data |
---|---|---|
44b4867e WJ |
1 | /* |
2 | dup2 -- 7th Edition UNIX system call emulation for UNIX System V | |
3 | ||
4 | last edit: 11-Feb-1987 D A Gwyn | |
5 | */ | |
6 | ||
7 | #include <errno.h> | |
8 | #include <fcntl.h> | |
9 | ||
10 | extern int close(), fcntl(); | |
11 | ||
12 | int | |
13 | dup2( oldfd, newfd ) | |
14 | int oldfd; /* already-open file descriptor */ | |
15 | int newfd; /* desired duplicate descriptor */ | |
16 | { | |
17 | register int ret; /* for fcntl() return value */ | |
18 | register int save; /* for saving entry errno */ | |
19 | ||
20 | if ( oldfd == newfd ) | |
21 | return oldfd; /* be careful not to close() */ | |
22 | ||
23 | save = errno; /* save entry errno */ | |
24 | (void) close( newfd ); /* in case newfd is open */ | |
25 | /* (may have just clobbered the original errno value) */ | |
26 | ||
27 | ret = fcntl( oldfd, F_DUPFD, newfd ); /* dupe it */ | |
28 | ||
29 | if ( ret >= 0 ) | |
30 | errno = save; /* restore entry errno */ | |
31 | else /* fcntl() returned error */ | |
32 | if ( errno == EINVAL ) | |
33 | errno = EBADF; /* we think of everything */ | |
34 | ||
35 | return ret; /* return file descriptor */ | |
36 | } |