ReactOS 0.4.16-dev-2610-ge2c92c0
rw.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/client/file/rw.c
5 * PURPOSE: Read/write functions
6 * PROGRAMMER: Ariadne (ariadne@xs4all.nl)
7 * UPDATE HISTORY:
8 * Created 01/11/98
9 */
10
11/* INCLUDES ****************************************************************/
12
13#include <k32.h>
14#define NDEBUG
15#include <debug.h>
17
18/* FUNCTIONS ****************************************************************/
19
20/*
21 * @implemented
22 */
27 OUT LPDWORD lpNumberOfBytesWritten,
29{
31
32 TRACE("WriteFile(hFile %p)\n", hFile);
33
34 if (lpNumberOfBytesWritten != NULL) *lpNumberOfBytesWritten = 0;
35
37
39 {
40 return WriteConsoleA(hFile,
43 lpNumberOfBytesWritten,
45 }
46
47 if (lpOverlapped != NULL)
48 {
51
52 Offset.u.LowPart = lpOverlapped->Offset;
53 Offset.u.HighPart = lpOverlapped->OffsetHigh;
54 lpOverlapped->Internal = STATUS_PENDING;
55 ApcContext = (((ULONG_PTR)lpOverlapped->hEvent & 0x1) ? NULL : lpOverlapped);
56
58 lpOverlapped->hEvent,
59 NULL,
64 &Offset,
65 NULL);
66
67 /* return FALSE in case of failure and pending operations! */
69 {
71 return FALSE;
72 }
73
74 if (lpNumberOfBytesWritten != NULL)
75 *lpNumberOfBytesWritten = lpOverlapped->InternalHigh;
76 }
77 else
78 {
80
82 NULL,
83 NULL,
84 NULL,
85 &Iosb,
88 NULL,
89 NULL);
90
91 /* Wait in case operation is pending */
93 {
95 if (NT_SUCCESS(Status)) Status = Iosb.Status;
96 }
97
98 /*
99 * lpNumberOfBytesWritten must not be NULL here, in fact Win doesn't
100 * check that case either and crashes (only after the operation
101 * completed).
102 */
103 *lpNumberOfBytesWritten = Iosb.Information;
104
105 if (!NT_SUCCESS(Status))
106 {
108 return FALSE;
109 }
110 }
111
112 TRACE("WriteFile() succeeded\n");
113 return TRUE;
114}
115
116
117/*
118 * @implemented
119 */
123 IN DWORD nNumberOfBytesToRead,
124 OUT LPDWORD lpNumberOfBytesRead OPTIONAL,
126{
128
129 TRACE("ReadFile(hFile %p)\n", hFile);
130
131 if (lpNumberOfBytesRead != NULL) *lpNumberOfBytesRead = 0;
132
134
136 {
138 lpBuffer,
139 nNumberOfBytesToRead,
140 lpNumberOfBytesRead,
141 NULL))
142 {
143 DWORD dwMode;
144 GetConsoleMode(hFile, &dwMode);
145 if ((dwMode & ENABLE_PROCESSED_INPUT) && *(PCHAR)lpBuffer == 0x1a)
146 {
147 /* EOF character entered; simulate end-of-file */
148 *lpNumberOfBytesRead = 0;
149 }
150 return TRUE;
151 }
152 return FALSE;
153 }
154
155 if (lpOverlapped != NULL)
156 {
159
160 Offset.u.LowPart = lpOverlapped->Offset;
161 Offset.u.HighPart = lpOverlapped->OffsetHigh;
162 lpOverlapped->Internal = STATUS_PENDING;
163 ApcContext = (((ULONG_PTR)lpOverlapped->hEvent & 0x1) ? NULL : lpOverlapped);
164
166 lpOverlapped->hEvent,
167 NULL,
170 lpBuffer,
171 nNumberOfBytesToRead,
172 &Offset,
173 NULL);
174
175 /* return FALSE in case of failure and pending operations! */
177 {
178 if (Status == STATUS_END_OF_FILE && lpNumberOfBytesRead != NULL)
179 *lpNumberOfBytesRead = 0;
180
182 return FALSE;
183 }
184
185 if (lpNumberOfBytesRead != NULL)
186 *lpNumberOfBytesRead = lpOverlapped->InternalHigh;
187 }
188 else
189 {
191
193 NULL,
194 NULL,
195 NULL,
196 &Iosb,
197 lpBuffer,
198 nNumberOfBytesToRead,
199 NULL,
200 NULL);
201
202 /* Wait in case operation is pending */
203 if (Status == STATUS_PENDING)
204 {
206 if (NT_SUCCESS(Status)) Status = Iosb.Status;
207 }
208
210 {
211 /*
212 * lpNumberOfBytesRead must not be NULL here, in fact Win doesn't
213 * check that case either and crashes (only after the operation
214 * completed).
215 */
216 *lpNumberOfBytesRead = 0;
217 return TRUE;
218 }
219
220 /*
221 * lpNumberOfBytesRead must not be NULL here, in fact Win doesn't
222 * check that case either and crashes (only after the operation
223 * completed).
224 */
225 *lpNumberOfBytesRead = Iosb.Information;
226
227 if (!NT_SUCCESS(Status))
228 {
230 return FALSE;
231 }
232 }
233
234 TRACE("ReadFile() succeeded\n");
235 return TRUE;
236}
237
242{
243 DWORD dwErrorCode;
244 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine =
246
248 lpCompletionRoutine(dwErrorCode,
251}
252
253
254/*
255 * @implemented
256 */
262 IN LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
263{
266
267 Offset.u.LowPart = lpOverlapped->Offset;
268 Offset.u.HighPart = lpOverlapped->OffsetHigh;
269 lpOverlapped->Internal = STATUS_PENDING;
270
272 NULL,
274 lpCompletionRoutine,
278 &Offset,
279 NULL);
280
281 if (!NT_SUCCESS(Status))
282 {
284 return FALSE;
285 }
286
287 return TRUE;
288}
289
290
291/*
292 * @implemented
293 */
297 IN DWORD nNumberOfBytesToRead OPTIONAL,
299 IN LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
300{
303
304 Offset.u.LowPart = lpOverlapped->Offset;
305 Offset.u.HighPart = lpOverlapped->OffsetHigh;
306 lpOverlapped->Internal = STATUS_PENDING;
307
309 NULL,
311 lpCompletionRoutine,
313 lpBuffer,
314 nNumberOfBytesToRead,
315 &Offset,
316 NULL);
317
318 if (!NT_SUCCESS(Status))
319 {
321 return FALSE;
322 }
323
324 return TRUE;
325}
326
327
328/*
329 * @implemented
330 */
331BOOL
332WINAPI
334 FILE_SEGMENT_ELEMENT aSegmentArray[],
335 DWORD nNumberOfBytesToRead,
338{
339 PIO_STATUS_BLOCK pIOStatus;
342
343 DPRINT("(%p %p %u %p)\n", hFile, aSegmentArray, nNumberOfBytesToRead, lpOverlapped);
344
345 Offset.LowPart = lpOverlapped->Offset;
346 Offset.HighPart = lpOverlapped->OffsetHigh;
347 pIOStatus = (PIO_STATUS_BLOCK) lpOverlapped;
348 pIOStatus->Status = STATUS_PENDING;
349 pIOStatus->Information = 0;
350
352 NULL,
353 NULL,
354 NULL,
355 pIOStatus,
356 aSegmentArray,
357 nNumberOfBytesToRead,
358 &Offset,
359 NULL);
360
361 if (!NT_SUCCESS(Status))
362 {
364 return FALSE;
365 }
366
367 return TRUE;
368}
369
370/*
371 * @implemented
372 */
373BOOL
374WINAPI
376 FILE_SEGMENT_ELEMENT aSegmentArray[],
380{
381 PIO_STATUS_BLOCK IOStatus;
384
385 DPRINT("%p %p %u %p\n", hFile, aSegmentArray, nNumberOfBytesToWrite, lpOverlapped);
386
387 Offset.LowPart = lpOverlapped->Offset;
388 Offset.HighPart = lpOverlapped->OffsetHigh;
389 IOStatus = (PIO_STATUS_BLOCK) lpOverlapped;
390 IOStatus->Status = STATUS_PENDING;
391 IOStatus->Information = 0;
392
394 NULL,
395 NULL,
396 NULL,
397 IOStatus,
398 aSegmentArray,
400 &Offset,
401 NULL);
402
403 if (!NT_SUCCESS(Status))
404 {
406 return FALSE;
407 }
408
409 return TRUE;
410}
411
412/* EOF */
LONG NTSTATUS
Definition: precomp.h:26
#define DEBUG_CHANNEL(args)
Definition: rdesktop.h:159
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define SetLastError(x)
Definition: compat.h:752
BOOL WINAPI GetConsoleMode(HANDLE hConsoleHandle, LPDWORD lpMode)
Definition: console.c:1571
BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleA(IN HANDLE hConsoleOutput, IN CONST VOID *lpBuffer, IN DWORD nNumberOfCharsToWrite, OUT LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
Definition: readwrite.c:1468
BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleA(IN HANDLE hConsoleInput, OUT LPVOID lpBuffer, IN DWORD nNumberOfCharsToRead, OUT LPDWORD lpNumberOfCharsRead, IN PCONSOLE_READCONSOLE_CONTROL pInputControl OPTIONAL)
Definition: readwrite.c:1195
BOOL WINAPI WriteFileGather(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[], DWORD nNumberOfBytesToWrite, LPDWORD lpReserved, LPOVERLAPPED lpOverlapped)
Definition: rw.c:375
BOOL WINAPI ReadFileScatter(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[], DWORD nNumberOfBytesToRead, LPDWORD lpReserved, LPOVERLAPPED lpOverlapped)
Definition: rw.c:333
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
BOOL WINAPI ReadFileEx(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead OPTIONAL, IN LPOVERLAPPED lpOverlapped, IN LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
Definition: rw.c:295
BOOL WINAPI WriteFileEx(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, IN LPOVERLAPPED lpOverlapped, IN LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
Definition: rw.c:258
HANDLE TranslateStdHandle(IN HANDLE hHandle)
Definition: handle.c:19
return Iosb
Definition: create.c:4403
#define ULONG_PTR
Definition: config.h:101
struct _IO_STATUS_BLOCK * PIO_STATUS_BLOCK
Definition: change.c:34
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
Status
Definition: gdiplustypes.h:25
NTSTATUS NTAPI NtReadFileScatter(IN HANDLE FileHandle, IN HANDLE Event OPTIONAL, IN PIO_APC_ROUTINE UserApcRoutine OPTIONAL, IN PVOID UserApcContext OPTIONAL, OUT PIO_STATUS_BLOCK UserIoStatusBlock, IN FILE_SEGMENT_ELEMENT BufferDescription[], IN ULONG BufferLength, IN PLARGE_INTEGER ByteOffset, IN PULONG Key OPTIONAL)
Definition: iofunc.c:3063
NTSTATUS NTAPI NtWriteFileGather(IN HANDLE FileHandle, IN HANDLE Event OPTIONAL, IN PIO_APC_ROUTINE UserApcRoutine OPTIONAL, IN PVOID UserApcContext OPTIONAL, OUT PIO_STATUS_BLOCK UserIoStatusBlock, IN FILE_SEGMENT_ELEMENT BufferDescription[], IN ULONG BufferLength, IN PLARGE_INTEGER ByteOffset, IN PULONG Key OPTIONAL)
Definition: iofunc.c:4132
#define kernel32file
Definition: kernel32.h:6
VOID(WINAPI * LPOVERLAPPED_COMPLETION_ROUTINE)(_In_ DWORD dwErrorCode, _In_ DWORD dwNumberOfBytesTransfered, _Inout_ LPOVERLAPPED lpOverlapped)
Definition: minwinbase.h:386
CONST void * LPCVOID
Definition: minwindef.h:164
_In_ HANDLE hFile
Definition: mswsock.h:90
_In_ HANDLE _In_ DWORD nNumberOfBytesToWrite
Definition: mswsock.h:91
_In_ HANDLE _In_ DWORD _In_ DWORD _Inout_opt_ LPOVERLAPPED lpOverlapped
Definition: mswsock.h:93
_In_opt_ HANDLE _In_opt_ PIO_APC_ROUTINE _In_opt_ PVOID ApcContext
Definition: iofuncs.h:727
_In_opt_ HANDLE _In_opt_ PIO_APC_ROUTINE ApcRoutine
Definition: iofuncs.h:726
_Out_ LPWSTR lpBuffer
Definition: netsh.h:68
NTSYSAPI NTSTATUS NTAPI NtWriteFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN PVOID WriteBuffer, IN ULONG WriteBufferLength, IN PLARGE_INTEGER FileOffset OPTIONAL, IN PULONG LockOperationKey OPTIONAL)
NTSYSAPI NTSTATUS NTAPI NtWaitForSingleObject(IN HANDLE hObject, IN BOOLEAN bAlertable, IN PLARGE_INTEGER Timeout)
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:167
#define IsConsoleHandle(h)
Definition: console.h:14
#define STATUS_END_OF_FILE
Definition: shellext.h:67
NTSTATUS NTAPI NtReadFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key)
#define DPRINT
Definition: sndvol32.h:73
#define TRACE(s)
Definition: solgame.cpp:4
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68
#define STATUS_PENDING
Definition: telnetd.h:14
uint32_t * LPDWORD
Definition: typedefs.h:59
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
char * PCHAR
Definition: typedefs.h:51
#define ENABLE_PROCESSED_INPUT
Definition: wincon.h:107
_Reserved_ PVOID Reserved
Definition: winddi.h:3974
#define WINAPI
Definition: msvc.h:6
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
_In_ DWORD _In_ int _In_ int _In_opt_ LPNLSVERSIONINFO _In_opt_ LPVOID lpReserved
Definition: winnls.h:1268