2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: dos/dos32krnl/dosfiles.c
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
9 /* INCLUDES *******************************************************************/
14 // #include "callback.h"
19 #include "bios/bios.h"
21 /* PRIVATE VARIABLES **********************************************************/
23 /* PUBLIC FUNCTIONS ***********************************************************/
25 WORD
DosCreateFile(LPWORD Handle
, LPCSTR FilePath
, WORD Attributes
)
30 DPRINT("DosCreateFile: FilePath \"%s\", Attributes 0x%04X\n",
35 FileHandle
= CreateFileA(FilePath
,
36 GENERIC_READ
| GENERIC_WRITE
,
37 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
43 if (FileHandle
== INVALID_HANDLE_VALUE
)
45 /* Return the error code */
46 return (WORD
)GetLastError();
49 /* Open the DOS handle */
50 DosHandle
= DosOpenHandle(FileHandle
);
52 if (DosHandle
== INVALID_DOS_HANDLE
)
54 /* Close the handle */
55 CloseHandle(FileHandle
);
57 /* Return the error code */
58 return ERROR_TOO_MANY_OPEN_FILES
;
61 /* It was successful */
66 WORD
DosOpenFile(LPWORD Handle
, LPCSTR FilePath
, BYTE AccessMode
)
69 ACCESS_MASK Access
= 0;
72 DPRINT("DosOpenFile: FilePath \"%s\", AccessMode 0x%04X\n",
76 /* Parse the access mode */
77 switch (AccessMode
& 3)
82 Access
= GENERIC_READ
;
89 Access
= GENERIC_WRITE
;
96 Access
= GENERIC_READ
| GENERIC_WRITE
;
103 return ERROR_INVALID_PARAMETER
;
108 FileHandle
= CreateFileA(FilePath
,
110 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
113 FILE_ATTRIBUTE_NORMAL
,
116 if (FileHandle
== INVALID_HANDLE_VALUE
)
118 /* Return the error code */
119 return (WORD
)GetLastError();
122 /* Open the DOS handle */
123 DosHandle
= DosOpenHandle(FileHandle
);
125 if (DosHandle
== INVALID_DOS_HANDLE
)
127 /* Close the handle */
128 CloseHandle(FileHandle
);
130 /* Return the error code */
131 return ERROR_TOO_MANY_OPEN_FILES
;
134 /* It was successful */
136 return ERROR_SUCCESS
;
139 WORD
DosReadFile(WORD FileHandle
, LPVOID Buffer
, WORD Count
, LPWORD BytesRead
)
141 WORD Result
= ERROR_SUCCESS
;
142 DWORD BytesRead32
= 0;
143 HANDLE Handle
= DosGetRealHandle(FileHandle
);
145 DPRINT1("DosReadFile: FileHandle 0x%04X, Count 0x%04X\n", FileHandle
, Count
);
147 /* Make sure the handle is valid */
148 if (Handle
== INVALID_HANDLE_VALUE
) return ERROR_INVALID_HANDLE
;
150 if (IsConsoleHandle(Handle
))
155 * Use BIOS Get Keystroke function
161 for (BytesRead32
= 0; BytesRead32
< Count
; BytesRead32
++)
163 /* Call the BIOS INT 16h, AH=00h "Get Keystroke" */
165 Int32Call(&DosContext
, BIOS_KBD_INTERRUPT
);
167 /* Retrieve the character in AL (scan code is in AH) */
170 // FIXME: Sometimes we need echo, some other times not.
171 // DosPrintCharacter(DOS_OUTPUT_HANDLE, Character);
173 ((PCHAR
)Buffer
)[BytesRead32
] = Character
;
175 /* Stop on first carriage return */
176 if (Character
== '\r')
178 // DosPrintCharacter(DOS_OUTPUT_HANDLE, '\n');
191 if (!ReadFile(Handle
, Buffer
, Count
/* * sizeof(CHAR) */, &BytesRead32
, NULL
))
193 /* Store the error code */
194 Result
= (WORD
)GetLastError();
198 /* The number of bytes read is always 16-bit */
199 *BytesRead
= LOWORD(BytesRead32
);
201 /* Return the error code */
205 WORD
DosWriteFile(WORD FileHandle
, LPVOID Buffer
, WORD Count
, LPWORD BytesWritten
)
207 WORD Result
= ERROR_SUCCESS
;
208 DWORD BytesWritten32
= 0;
209 HANDLE Handle
= DosGetRealHandle(FileHandle
);
211 DPRINT1("DosWriteFile: FileHandle 0x%04X, Count 0x%04X\n",
215 /* Make sure the handle is valid */
216 if (Handle
== INVALID_HANDLE_VALUE
) return ERROR_INVALID_HANDLE
;
218 if (IsConsoleHandle(Handle
))
221 * Use BIOS Teletype function
228 // FIXME: Use BIOS Write String function INT 10h, AH=13h ??
230 for (BytesWritten32
= 0; BytesWritten32
< Count
; BytesWritten32
++)
232 /* Set the parameters */
233 setAL(((PCHAR
)Buffer
)[BytesWritten32
]);
234 setBL(DOS_CHAR_ATTRIBUTE
);
235 setBH(Bda
->VideoPage
);
237 /* Call the BIOS INT 10h, AH=0Eh "Teletype Output" */
239 Int32Call(&DosContext
, BIOS_VIDEO_INTERRUPT
);
244 /* Restore AX and BX */
251 if (!WriteFile(Handle
, Buffer
, Count
/* * sizeof(CHAR) */, &BytesWritten32
, NULL
))
253 /* Store the error code */
254 Result
= (WORD
)GetLastError();
258 /* The number of bytes written is always 16-bit */
259 *BytesWritten
= LOWORD(BytesWritten32
);
261 /* Return the error code */
265 WORD
DosSeekFile(WORD FileHandle
, LONG Offset
, BYTE Origin
, LPDWORD NewOffset
)
267 WORD Result
= ERROR_SUCCESS
;
269 HANDLE Handle
= DosGetRealHandle(FileHandle
);
271 DPRINT("DosSeekFile: FileHandle 0x%04X, Offset 0x%08X, Origin 0x%02X\n",
276 /* Make sure the handle is valid */
277 if (Handle
== INVALID_HANDLE_VALUE
) return ERROR_INVALID_HANDLE
;
279 /* Check if the origin is valid */
280 if (Origin
!= FILE_BEGIN
&& Origin
!= FILE_CURRENT
&& Origin
!= FILE_END
)
282 return ERROR_INVALID_FUNCTION
;
285 /* Move the file pointer */
286 if (IsConsoleHandle(Handle
))
288 /* Always succeeds when seeking a console handle */
290 Result
= ERROR_SUCCESS
;
294 FilePointer
= SetFilePointer(Handle
, Offset
, NULL
, Origin
);
297 /* Check if there's a possibility the operation failed */
298 if (FilePointer
== INVALID_SET_FILE_POINTER
)
300 /* Get the real error code */
301 Result
= (WORD
)GetLastError();
304 if (Result
!= ERROR_SUCCESS
)
306 /* The operation did fail */
310 /* Return the file pointer, if requested */
311 if (NewOffset
) *NewOffset
= FilePointer
;
314 return ERROR_SUCCESS
;
317 // This function is almost exclusively used as a DosFlushInputBuffer
318 BOOL
DosFlushFileBuffers(WORD FileHandle
)
320 HANDLE Handle
= DosGetRealHandle(FileHandle
);
322 /* Make sure the handle is valid */
323 if (Handle
== INVALID_HANDLE_VALUE
) return FALSE
;
326 * No need to check whether the handle is a console handle since
327 * FlushFileBuffers() automatically does this check and calls
328 * FlushConsoleInputBuffer() for us.
330 return FlushFileBuffers(Handle
);