f6d44ace5b4cd690dc15eaad34dc72638d42db4d
[reactos.git] / lib / msvcrt / stdio / fdopen.c
1 #include <msvcrt/stdio.h>
2 #include <msvcrt/internal/file.h>
3
4 FILE* __alloc_file(void);
5
6
7 /*
8  * @implemented
9  */
10 FILE* _fdopen(int handle, char* mode)
11 {
12   FILE* file;
13   int rw;
14
15   if (handle == 0)
16     return stdin;
17
18   if (handle == 1)
19     return stdout;
20
21   if (handle == 2)
22     return stderr;
23
24   if (handle == 3)
25     return stdaux;
26
27   if (handle == 4)
28     return stdprn;
29
30   file = __alloc_file();
31   if (file == NULL)
32     return NULL;
33   file->_file = handle;
34
35   rw = (mode[1] == '+') || (mode[1] && (mode[2] == '+'));
36
37   if (*mode == 'a')
38     _lseek(handle, 0, SEEK_END);
39
40   file->_cnt = 0;
41   file->_file = handle;
42   file->_bufsiz = 0;
43
44 // The mode of the stream must be compatible with the mode of the file descriptor.
45 // this should be checked.
46
47   if (rw)
48     file->_flag = _IOREAD | _IOWRT;
49   else if (*mode == 'r')
50     file->_flag = _IOREAD;
51   else
52     file->_flag = _IOWRT;
53
54   file->_base = file->_ptr = NULL;
55
56   return file;
57 }