update for HEAD-2003091401
[reactos.git] / lib / crtdll / stdio / fsopen.c
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS system libraries
4  * FILE:        lib/crtdll/conio/kbhit.c
5  * PURPOSE:     Checks for keyboard hits
6  * PROGRAMER:   Boudewijn Dekker
7  * UPDATE HISTORY:
8  *              28/12/98: Created
9  */
10 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
11
12 #include <msvcrt/sys/types.h>
13 #include <msvcrt/stdio.h>
14 #include <msvcrt/io.h>
15 #include <msvcrt/fcntl.h>
16 #include <msvcrt/share.h>
17 #include <msvcrt/internal/file.h>
18
19
20 FILE* __alloc_file(void);
21
22
23 /*
24  * @implemented
25  */
26 FILE* _fsopen(const char* file, const char* mode, int shflag)
27 {
28   FILE* f;
29   int fd, rw, oflags = 0;
30   char tbchar;
31    
32   int shf;
33
34   if (file == 0)
35     return 0;
36   if (mode == 0)
37     return 0;
38
39   f = __alloc_file();
40   if (f == NULL)
41     return NULL;
42
43   rw = (mode[1] == '+') || (mode[1] && (mode[2] == '+'));
44
45   switch (*mode)
46   {
47   case 'a':
48     oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
49     break;
50   case 'r':
51     oflags = rw ? O_RDWR : O_RDONLY;
52     break;
53   case 'w':
54     oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
55     break;
56   default:
57     return (NULL);
58   }
59   if (mode[1] == '+')
60     tbchar = mode[2];
61   else
62     tbchar = mode[1];
63   if (tbchar == 't')
64     oflags |= O_TEXT;
65   else if (tbchar == 'b')
66     oflags |= O_BINARY;
67   else
68     oflags |= (_fmode & (O_TEXT|O_BINARY));
69
70   if ( shflag == _SH_DENYNO )
71     shf = _S_IREAD | _S_IWRITE;
72   else if( shflag == _SH_DENYRD )
73     shf =  _S_IWRITE;
74   else if( shflag == _SH_DENYRW )
75     shf =  0;
76   else if( shflag == _SH_DENYWR )
77     shf =  _S_IREAD;
78   else
79     shf = _S_IREAD | _S_IWRITE;
80
81   fd = _open(file, oflags, shf);
82   if (fd < 0)
83     return NULL;
84
85 // ms crtdll ensures that writes will end up at the end of file in append mode
86 // we just move the file pointer to the end of file initially
87   if (*mode == 'a')
88     lseek(fd, 0, SEEK_END);
89
90   f->_cnt = 0;
91   f->_file = fd;
92   f->_bufsiz = 0;
93   if (rw)
94     f->_flag = _IOREAD | _IOWRT;
95   else if (*mode == 'r')
96     f->_flag = _IOREAD;
97   else
98     f->_flag = _IOWRT;
99
100   f->_base = f->_ptr = NULL;
101   return f;
102 }