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