Use free Windows DDK and compile with latest MinGW releases.
[reactos.git] / reactos / lib / msvcrt / io / dup.c
1 /* $Id: dup.c,v 1.3 2002/09/07 15:12:30 chorns Exp $ */
2 #include <msvcrti.h>
3
4
5 int _dup(int handle)
6 {
7 HANDLE hFile;
8 HANDLE hProcess = GetCurrentProcess();
9 BOOL result;
10 int fd;
11
12 hFile = (HANDLE)_get_osfhandle(handle);
13 if (hFile == INVALID_HANDLE_VALUE)
14 return -1;
15 result = DuplicateHandle(hProcess,
16 hFile,
17 hProcess,
18 &hFile,
19 0,
20 TRUE,
21 DUPLICATE_SAME_ACCESS);
22 if (result == FALSE)
23 return -1;
24
25 fd = __fileno_alloc(hFile, __fileno_getmode(handle));
26 if (fd < 0)
27 {
28 CloseHandle(hFile);
29 }
30 return fd;
31 }