Manually applying Gunnars patch because it's easier this way - and a good chance...
[reactos.git] / reactos / lib / kernel32 / file / lfile.c
1 /* $Id: lfile.c,v 1.7 2002/11/07 02:52:37 robd Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/file/lfile.c
6 * PURPOSE: Find functions
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
8 * UPDATE HISTORY:
9 * Created 01/11/98
10 */
11
12 #undef WIN32_LEAN_AND_MEAN
13 #include <windows.h>
14 #include <string.h>
15 #include <wchar.h>
16
17
18 long
19 STDCALL
20 _hread(
21 HFILE hFile,
22 LPVOID lpBuffer,
23 long lBytes
24 )
25 {
26 DWORD NumberOfBytesRead;
27
28 if (ReadFile(
29 (HANDLE) hFile,
30 (LPVOID) lpBuffer,
31 (DWORD) lBytes,
32 & NumberOfBytesRead,
33 NULL
34 ) == FALSE)
35 {
36 return -1;
37 }
38 return NumberOfBytesRead;
39 }
40
41
42 /*
43 //19990828.EA: aliased in DEF
44 UINT
45 STDCALL
46 _lread (
47 HFILE fd,
48 LPVOID buffer,
49 UINT count
50 )
51 {
52 return _hread(
53 fd,
54 buffer,
55 count
56 );
57 }
58 */
59
60
61 long
62 STDCALL
63 _hwrite (
64 HFILE hFile,
65 LPCSTR lpBuffer,
66 long lBytes
67 )
68 {
69 DWORD NumberOfBytesWritten;
70
71 if (lBytes == 0)
72 {
73 if ( SetEndOfFile((HANDLE) hFile ) == FALSE )
74 {
75 return -1;
76 }
77 return 0;
78 }
79 if ( WriteFile(
80 (HANDLE) hFile,
81 (LPVOID) lpBuffer,
82 (DWORD) lBytes,
83 & NumberOfBytesWritten,
84 NULL
85 ) == FALSE )
86 {
87 return -1;
88 }
89 return NumberOfBytesWritten;
90 }
91
92
93 /*
94 //19990828.EA: aliased in DEF
95
96 UINT
97 STDCALL
98 _lwrite(
99 HFILE hFile,
100 LPCSTR lpBuffer,
101 UINT uBytes
102 )
103 {
104 return _hwrite(hFile,lpBuffer,uBytes);
105 }
106 */
107
108
109 HFILE
110 STDCALL
111 _lopen (
112 LPCSTR lpPathName,
113 int iReadWrite
114 )
115 {
116 DWORD dwAccessMask = 0;
117 DWORD dwShareMode = 0;
118
119 if ( (iReadWrite & OF_READWRITE ) == OF_READWRITE )
120 dwAccessMask = GENERIC_READ | GENERIC_WRITE;
121 else if ( (iReadWrite & OF_READ ) == OF_READ )
122 dwAccessMask = GENERIC_READ;
123 else if ( (iReadWrite & OF_WRITE ) == OF_WRITE )
124 dwAccessMask = GENERIC_WRITE;
125
126 if ((iReadWrite & OF_SHARE_COMPAT) == OF_SHARE_COMPAT )
127 dwShareMode = FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE;
128 else if ((iReadWrite & OF_SHARE_DENY_NONE) == OF_SHARE_DENY_NONE)
129 dwShareMode = FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE;
130 else if ((iReadWrite & OF_SHARE_DENY_READ) == OF_SHARE_DENY_READ)
131 dwShareMode = FILE_SHARE_WRITE | FILE_SHARE_DELETE;
132 else if ((iReadWrite & OF_SHARE_DENY_WRITE) == OF_SHARE_DENY_WRITE )
133 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
134 else if ((iReadWrite & OF_SHARE_EXCLUSIVE) == OF_SHARE_EXCLUSIVE)
135 dwShareMode = 0;
136
137 SetLastError (ERROR_SUCCESS);
138 return (HFILE) CreateFileA(
139 lpPathName,
140 dwAccessMask,
141 dwShareMode,
142 NULL,
143 OPEN_EXISTING,
144 FILE_ATTRIBUTE_NORMAL,
145 NULL);
146 }
147
148
149 HFILE
150 STDCALL
151 _lcreat (
152 LPCSTR lpPathName,
153 int iAttribute
154 )
155 {
156
157 DWORD FileAttributes = 0;
158
159 if ( iAttribute == 0 )
160 FileAttributes |= FILE_ATTRIBUTE_NORMAL;
161 else if ( iAttribute == 1 )
162 FileAttributes |= FILE_ATTRIBUTE_READONLY;
163 else if ( iAttribute == 2 )
164 FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
165 else if ( iAttribute == 4 )
166 FileAttributes |= FILE_ATTRIBUTE_SYSTEM;
167
168 return (HFILE) CreateFileA(
169 lpPathName,
170 GENERIC_ALL,
171 (FILE_SHARE_READ | FILE_SHARE_WRITE),
172 NULL,
173 CREATE_ALWAYS,
174 iAttribute,
175 NULL);
176 }
177
178
179 int
180 STDCALL
181 _lclose (
182 HFILE hFile
183 )
184 {
185 if (CloseHandle ((HANDLE)hFile))
186 {
187 return 0;
188 }
189 return -1;
190 }
191
192
193 LONG
194 STDCALL
195 _llseek(
196 HFILE hFile,
197 LONG lOffset,
198 int iOrigin
199 )
200 {
201 return SetFilePointer (
202 (HANDLE) hFile,
203 lOffset,
204 NULL,
205 (DWORD) iOrigin);
206 }
207
208 /* EOF */