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