update for HEAD-2003091401
[reactos.git] / lib / msvcrt / io / dup.c
1 /* $Id$ */
2 #include <windows.h>
3 #include <msvcrt/io.h>
4 #include <msvcrt/internal/file.h>
5
6
7 /*
8  * @implemented
9  */
10 int _dup(int handle)
11 {
12   HANDLE hFile;
13   HANDLE hProcess = GetCurrentProcess();
14   BOOL result;
15   int fd;
16   
17   hFile = _get_osfhandle(handle);
18   if (hFile == INVALID_HANDLE_VALUE)
19           return -1;
20   result = DuplicateHandle(hProcess, 
21                            hFile, 
22                            hProcess, 
23                            &hFile, 
24                            0, 
25                            TRUE, 
26                            DUPLICATE_SAME_ACCESS);
27   if (result == FALSE)
28           return -1;
29
30   fd = __fileno_alloc(hFile, __fileno_getmode(handle));
31   if (fd < 0)
32   {
33           CloseHandle(hFile);
34   }
35   return fd;
36 }