2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
5 * PURPOSE: VDM DOS Kernel
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
9 /* INCLUDES *******************************************************************/
19 #include "registers.h"
21 /* PRIVATE VARIABLES **********************************************************/
23 static WORD CurrentPsp
= SYSTEM_PSP
;
24 static WORD DosLastError
= 0;
25 static DWORD DiskTransferArea
;
26 static BYTE CurrentDrive
;
27 static CHAR LastDrive
= 'E';
28 static CHAR CurrentDirectories
[NUM_DRIVES
][DOS_DIR_LENGTH
];
29 static HANDLE DosSystemFileTable
[DOS_SFT_SIZE
];
30 static WORD DosSftRefCount
[DOS_SFT_SIZE
];
31 static BYTE DosAllocStrategy
= DOS_ALLOC_BEST_FIT
;
32 static BOOLEAN DosUmbLinked
= FALSE
;
33 static WORD DosErrorLevel
= 0x0000;
36 #define BOP_DOS 0x50 // DOS System BOP (for NTIO.SYS and NTDOS.SYS)
37 #define BOP_CMD 0x54 // DOS Command Interpreter BOP (for COMMAND.COM)
39 /* PRIVATE FUNCTIONS **********************************************************/
41 /* Taken from base/shell/cmd/console.c */
42 static BOOL
IsConsoleHandle(HANDLE hHandle
)
46 /* Check whether the handle may be that of a console... */
47 if ((GetFileType(hHandle
) & FILE_TYPE_CHAR
) == 0) return FALSE
;
50 * It may be. Perform another test... The idea comes from the
51 * MSDN description of the WriteConsole API:
53 * "WriteConsole fails if it is used with a standard handle
54 * that is redirected to a file. If an application processes
55 * multilingual output that can be redirected, determine whether
56 * the output handle is a console handle (one method is to call
57 * the GetConsoleMode function and check whether it succeeds).
58 * If the handle is a console handle, call WriteConsole. If the
59 * handle is not a console handle, the output is redirected and
60 * you should call WriteFile to perform the I/O."
62 return GetConsoleMode(hHandle
, &dwMode
);
65 static VOID
DosCombineFreeBlocks(WORD StartBlock
)
67 PDOS_MCB CurrentMcb
= SEGMENT_TO_MCB(StartBlock
), NextMcb
;
69 /* If this is the last block or it's not free, quit */
70 if (CurrentMcb
->BlockType
== 'Z' || CurrentMcb
->OwnerPsp
!= 0) return;
74 /* Get a pointer to the next MCB */
75 NextMcb
= SEGMENT_TO_MCB(StartBlock
+ CurrentMcb
->Size
+ 1);
77 /* Check if the next MCB is free */
78 if (NextMcb
->OwnerPsp
== 0)
81 CurrentMcb
->Size
+= NextMcb
->Size
+ 1;
82 CurrentMcb
->BlockType
= NextMcb
->BlockType
;
83 NextMcb
->BlockType
= 'I';
87 /* No more adjoining free blocks */
93 static WORD
DosCopyEnvironmentBlock(WORD SourceSegment
, LPCSTR ProgramName
)
95 PCHAR Ptr
, SourceBuffer
, DestBuffer
= NULL
;
99 Ptr
= SourceBuffer
= (PCHAR
)SEG_OFF_TO_PTR(SourceSegment
, 0);
101 /* Calculate the size of the environment block */
104 TotalSize
+= strlen(Ptr
) + 1;
105 Ptr
+= strlen(Ptr
) + 1;
109 /* Add the string buffer size */
110 TotalSize
+= strlen(ProgramName
) + 1;
112 /* Allocate the memory for the environment block */
113 DestSegment
= DosAllocateMemory((WORD
)((TotalSize
+ 0x0F) >> 4), NULL
);
114 if (!DestSegment
) return 0;
118 DestBuffer
= (PCHAR
)SEG_OFF_TO_PTR(DestSegment
, 0);
121 /* Copy the string */
122 strcpy(DestBuffer
, Ptr
);
124 /* Advance to the next string */
125 DestBuffer
+= strlen(Ptr
);
126 Ptr
+= strlen(Ptr
) + 1;
128 /* Put a zero after the string */
132 /* Set the final zero */
135 /* Copy the program name after the environment block */
136 strcpy(DestBuffer
, ProgramName
);
141 static VOID
DosChangeMemoryOwner(WORD Segment
, WORD NewOwner
)
143 PDOS_MCB Mcb
= SEGMENT_TO_MCB(Segment
- 1);
145 /* Just set the owner */
146 Mcb
->OwnerPsp
= NewOwner
;
149 static WORD
DosOpenHandle(HANDLE Handle
)
156 /* The system PSP has no handle table */
157 if (CurrentPsp
== SYSTEM_PSP
) return INVALID_DOS_HANDLE
;
159 /* Get a pointer to the handle table */
160 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
161 HandleTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
163 /* Find a free entry in the JFT */
164 for (DosHandle
= 0; DosHandle
< PspBlock
->HandleTableSize
; DosHandle
++)
166 if (HandleTable
[DosHandle
] == 0xFF) break;
169 /* If there are no free entries, fail */
170 if (DosHandle
== PspBlock
->HandleTableSize
) return INVALID_DOS_HANDLE
;
172 /* Check if the handle is already in the SFT */
173 for (i
= 0; i
< DOS_SFT_SIZE
; i
++)
175 /* Check if this is the same handle */
176 if (DosSystemFileTable
[i
] != Handle
) continue;
178 /* Already in the table, reference it */
181 /* Set the JFT entry to that SFT index */
182 HandleTable
[DosHandle
] = i
;
184 /* Return the new handle */
188 /* Add the handle to the SFT */
189 for (i
= 0; i
< DOS_SFT_SIZE
; i
++)
191 /* Make sure this is an empty table entry */
192 if (DosSystemFileTable
[i
] != INVALID_HANDLE_VALUE
) continue;
194 /* Initialize the empty table entry */
195 DosSystemFileTable
[i
] = Handle
;
196 DosSftRefCount
[i
] = 1;
198 /* Set the JFT entry to that SFT index */
199 HandleTable
[DosHandle
] = i
;
201 /* Return the new handle */
205 /* The SFT is full */
206 return INVALID_DOS_HANDLE
;
209 static HANDLE
DosGetRealHandle(WORD DosHandle
)
214 /* The system PSP has no handle table */
215 if (CurrentPsp
== SYSTEM_PSP
) return INVALID_HANDLE_VALUE
;
217 /* Get a pointer to the handle table */
218 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
219 HandleTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
221 /* Make sure the handle is open */
222 if (HandleTable
[DosHandle
] == 0xFF) return INVALID_HANDLE_VALUE
;
224 /* Return the Win32 handle */
225 return DosSystemFileTable
[HandleTable
[DosHandle
]];
228 static VOID
DosCopyHandleTable(LPBYTE DestinationTable
)
234 /* Clear the table first */
235 for (i
= 0; i
< 20; i
++) DestinationTable
[i
] = 0xFF;
237 /* Check if this is the initial process */
238 if (CurrentPsp
== SYSTEM_PSP
)
240 /* Set up the standard I/O devices */
241 for (i
= 0; i
<= 2; i
++)
243 /* Set the index in the SFT */
244 DestinationTable
[i
] = (BYTE
)i
;
246 /* Increase the reference count */
254 /* Get the parent PSP block and handle table */
255 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
256 SourceTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
258 /* Copy the first 20 handles into the new table */
259 for (i
= 0; i
< 20; i
++)
261 DestinationTable
[i
] = SourceTable
[i
];
263 /* Increase the reference count */
264 DosSftRefCount
[SourceTable
[i
]]++;
268 /* PUBLIC FUNCTIONS ***********************************************************/
270 WORD
DosAllocateMemory(WORD Size
, WORD
*MaxAvailable
)
272 WORD Result
= 0, Segment
= FIRST_MCB_SEGMENT
, MaxSize
= 0;
273 PDOS_MCB CurrentMcb
, NextMcb
;
274 BOOLEAN SearchUmb
= FALSE
;
276 DPRINT("DosAllocateMemory: Size 0x%04X\n", Size
);
278 if (DosUmbLinked
&& (DosAllocStrategy
& (DOS_ALLOC_HIGH
| DOS_ALLOC_HIGH_LOW
)))
280 /* Search UMB first */
281 Segment
= UMB_START_SEGMENT
;
287 /* Get a pointer to the MCB */
288 CurrentMcb
= SEGMENT_TO_MCB(Segment
);
290 /* Make sure it's valid */
291 if (CurrentMcb
->BlockType
!= 'M' && CurrentMcb
->BlockType
!= 'Z')
293 DPRINT("The DOS memory arena is corrupted!\n");
294 DosLastError
= ERROR_ARENA_TRASHED
;
298 /* Only check free blocks */
299 if (CurrentMcb
->OwnerPsp
!= 0) goto Next
;
301 /* Combine this free block with adjoining free blocks */
302 DosCombineFreeBlocks(Segment
);
304 /* Update the maximum block size */
305 if (CurrentMcb
->Size
> MaxSize
) MaxSize
= CurrentMcb
->Size
;
307 /* Check if this block is big enough */
308 if (CurrentMcb
->Size
< Size
) goto Next
;
310 switch (DosAllocStrategy
& 0x3F)
312 case DOS_ALLOC_FIRST_FIT
:
314 /* For first fit, stop immediately */
319 case DOS_ALLOC_BEST_FIT
:
321 /* For best fit, update the smallest block found so far */
322 if ((Result
== 0) || (CurrentMcb
->Size
< SEGMENT_TO_MCB(Result
)->Size
))
330 case DOS_ALLOC_LAST_FIT
:
332 /* For last fit, make the current block the result, but keep searching */
339 /* If this was the last MCB in the chain, quit */
340 if (CurrentMcb
->BlockType
== 'Z')
342 /* Check if nothing was found while searching through UMBs */
343 if ((Result
== 0) && SearchUmb
&& (DosAllocStrategy
& DOS_ALLOC_HIGH_LOW
))
345 /* Search low memory */
346 Segment
= FIRST_MCB_SEGMENT
;
353 /* Otherwise, update the segment and continue */
354 Segment
+= CurrentMcb
->Size
+ 1;
359 /* If we didn't find a free block, return 0 */
362 DosLastError
= ERROR_NOT_ENOUGH_MEMORY
;
363 if (MaxAvailable
) *MaxAvailable
= MaxSize
;
367 /* Get a pointer to the MCB */
368 CurrentMcb
= SEGMENT_TO_MCB(Result
);
370 /* Check if the block is larger than requested */
371 if (CurrentMcb
->Size
> Size
)
373 /* It is, split it into two blocks */
374 NextMcb
= SEGMENT_TO_MCB(Result
+ Size
+ 1);
376 /* Initialize the new MCB structure */
377 NextMcb
->BlockType
= CurrentMcb
->BlockType
;
378 NextMcb
->Size
= CurrentMcb
->Size
- Size
- 1;
379 NextMcb
->OwnerPsp
= 0;
381 /* Update the current block */
382 CurrentMcb
->BlockType
= 'M';
383 CurrentMcb
->Size
= Size
;
386 /* Take ownership of the block */
387 CurrentMcb
->OwnerPsp
= CurrentPsp
;
389 /* Return the segment of the data portion of the block */
393 BOOLEAN
DosResizeMemory(WORD BlockData
, WORD NewSize
, WORD
*MaxAvailable
)
395 BOOLEAN Success
= TRUE
;
396 WORD Segment
= BlockData
- 1, ReturnSize
= 0, NextSegment
;
397 PDOS_MCB Mcb
= SEGMENT_TO_MCB(Segment
), NextMcb
;
399 DPRINT("DosResizeMemory: BlockData 0x%04X, NewSize 0x%04X\n",
403 /* Make sure this is a valid, allocated block */
404 if ((Mcb
->BlockType
!= 'M' && Mcb
->BlockType
!= 'Z') || Mcb
->OwnerPsp
== 0)
407 DosLastError
= ERROR_INVALID_HANDLE
;
411 ReturnSize
= Mcb
->Size
;
413 /* Check if we need to expand or contract the block */
414 if (NewSize
> Mcb
->Size
)
416 /* We can't expand the last block */
417 if (Mcb
->BlockType
!= 'M')
423 /* Get the pointer and segment of the next MCB */
424 NextSegment
= Segment
+ Mcb
->Size
+ 1;
425 NextMcb
= SEGMENT_TO_MCB(NextSegment
);
427 /* Make sure the next segment is free */
428 if (NextMcb
->OwnerPsp
!= 0)
430 DPRINT("Cannot expand memory block: next segment is not free!\n");
431 DosLastError
= ERROR_NOT_ENOUGH_MEMORY
;
436 /* Combine this free block with adjoining free blocks */
437 DosCombineFreeBlocks(NextSegment
);
439 /* Set the maximum possible size of the block */
440 ReturnSize
+= NextMcb
->Size
+ 1;
442 /* Maximize the current block */
443 Mcb
->Size
= ReturnSize
;
444 Mcb
->BlockType
= NextMcb
->BlockType
;
446 /* Invalidate the next block */
447 NextMcb
->BlockType
= 'I';
449 /* Check if the block is larger than requested */
450 if (Mcb
->Size
> NewSize
)
452 DPRINT("Block too large, reducing size from 0x%04X to 0x%04X\n",
456 /* It is, split it into two blocks */
457 NextMcb
= SEGMENT_TO_MCB(Segment
+ NewSize
+ 1);
459 /* Initialize the new MCB structure */
460 NextMcb
->BlockType
= Mcb
->BlockType
;
461 NextMcb
->Size
= Mcb
->Size
- NewSize
- 1;
462 NextMcb
->OwnerPsp
= 0;
464 /* Update the current block */
465 Mcb
->BlockType
= 'M';
469 else if (NewSize
< Mcb
->Size
)
471 DPRINT("Shrinking block from 0x%04X to 0x%04X\n",
475 /* Just split the block */
476 NextMcb
= SEGMENT_TO_MCB(Segment
+ NewSize
+ 1);
477 NextMcb
->BlockType
= Mcb
->BlockType
;
478 NextMcb
->Size
= Mcb
->Size
- NewSize
- 1;
479 NextMcb
->OwnerPsp
= 0;
482 Mcb
->BlockType
= 'M';
487 /* Check if the operation failed */
490 DPRINT("DosResizeMemory FAILED. Maximum available: 0x%04X\n",
493 /* Return the maximum possible size */
494 if (MaxAvailable
) *MaxAvailable
= ReturnSize
;
500 BOOLEAN
DosFreeMemory(WORD BlockData
)
502 PDOS_MCB Mcb
= SEGMENT_TO_MCB(BlockData
- 1);
504 DPRINT("DosFreeMemory: BlockData 0x%04X\n", BlockData
);
506 /* Make sure the MCB is valid */
507 if (Mcb
->BlockType
!= 'M' && Mcb
->BlockType
!= 'Z')
509 DPRINT("MCB block type '%c' not valid!\n", Mcb
->BlockType
);
513 /* Mark the block as free */
519 BOOLEAN
DosLinkUmb(VOID
)
521 DWORD Segment
= FIRST_MCB_SEGMENT
;
522 PDOS_MCB Mcb
= SEGMENT_TO_MCB(Segment
);
524 DPRINT("Linking UMB\n");
526 /* Check if UMBs are already linked */
527 if (DosUmbLinked
) return FALSE
;
529 /* Find the last block */
530 while ((Mcb
->BlockType
== 'M') && (Segment
<= 0xFFFF))
532 Segment
+= Mcb
->Size
+ 1;
533 Mcb
= SEGMENT_TO_MCB(Segment
);
536 /* Make sure it's valid */
537 if (Mcb
->BlockType
!= 'Z') return FALSE
;
539 /* Connect the MCB with the UMB chain */
540 Mcb
->BlockType
= 'M';
546 BOOLEAN
DosUnlinkUmb(VOID
)
548 DWORD Segment
= FIRST_MCB_SEGMENT
;
549 PDOS_MCB Mcb
= SEGMENT_TO_MCB(Segment
);
551 DPRINT("Unlinking UMB\n");
553 /* Check if UMBs are already unlinked */
554 if (!DosUmbLinked
) return FALSE
;
556 /* Find the block preceding the MCB that links it with the UMB chain */
557 while (Segment
<= 0xFFFF)
559 if ((Segment
+ Mcb
->Size
) == (FIRST_MCB_SEGMENT
+ USER_MEMORY_SIZE
))
561 /* This is the last non-UMB segment */
565 /* Advance to the next MCB */
566 Segment
+= Mcb
->Size
+ 1;
567 Mcb
= SEGMENT_TO_MCB(Segment
);
570 /* Mark the MCB as the last MCB */
571 Mcb
->BlockType
= 'Z';
573 DosUmbLinked
= FALSE
;
577 WORD
DosCreateFile(LPWORD Handle
, LPCSTR FilePath
, WORD Attributes
)
582 DPRINT("DosCreateFile: FilePath \"%s\", Attributes 0x%04X\n",
586 /* Create the file */
587 FileHandle
= CreateFileA(FilePath
,
588 GENERIC_READ
| GENERIC_WRITE
,
589 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
595 if (FileHandle
== INVALID_HANDLE_VALUE
)
597 /* Return the error code */
598 return (WORD
)GetLastError();
601 /* Open the DOS handle */
602 DosHandle
= DosOpenHandle(FileHandle
);
604 if (DosHandle
== INVALID_DOS_HANDLE
)
606 /* Close the handle */
607 CloseHandle(FileHandle
);
609 /* Return the error code */
610 return ERROR_TOO_MANY_OPEN_FILES
;
613 /* It was successful */
615 return ERROR_SUCCESS
;
618 WORD
DosOpenFile(LPWORD Handle
, LPCSTR FilePath
, BYTE AccessMode
)
621 ACCESS_MASK Access
= 0;
624 DPRINT("DosOpenFile: FilePath \"%s\", AccessMode 0x%04X\n",
628 /* Parse the access mode */
629 switch (AccessMode
& 3)
634 Access
= GENERIC_READ
;
641 Access
= GENERIC_WRITE
;
648 Access
= GENERIC_READ
| GENERIC_WRITE
;
655 return ERROR_INVALID_PARAMETER
;
660 FileHandle
= CreateFileA(FilePath
,
662 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
665 FILE_ATTRIBUTE_NORMAL
,
668 if (FileHandle
== INVALID_HANDLE_VALUE
)
670 /* Return the error code */
671 return (WORD
)GetLastError();
674 /* Open the DOS handle */
675 DosHandle
= DosOpenHandle(FileHandle
);
677 if (DosHandle
== INVALID_DOS_HANDLE
)
679 /* Close the handle */
680 CloseHandle(FileHandle
);
682 /* Return the error code */
683 return ERROR_TOO_MANY_OPEN_FILES
;
686 /* It was successful */
688 return ERROR_SUCCESS
;
691 WORD
DosReadFile(WORD FileHandle
, LPVOID Buffer
, WORD Count
, LPWORD BytesRead
)
693 WORD Result
= ERROR_SUCCESS
;
694 DWORD BytesRead32
= 0;
695 HANDLE Handle
= DosGetRealHandle(FileHandle
);
697 DPRINT("DosReadFile: FileHandle 0x%04X, Count 0x%04X\n", FileHandle
, Count
);
699 /* Make sure the handle is valid */
700 if (Handle
== INVALID_HANDLE_VALUE
) return ERROR_INVALID_HANDLE
;
703 if (!ReadFile(Handle
, Buffer
, Count
, &BytesRead32
, NULL
))
705 /* Store the error code */
706 Result
= (WORD
)GetLastError();
709 /* The number of bytes read is always 16-bit */
710 *BytesRead
= LOWORD(BytesRead32
);
712 /* Return the error code */
716 WORD
DosWriteFile(WORD FileHandle
, LPVOID Buffer
, WORD Count
, LPWORD BytesWritten
)
718 WORD Result
= ERROR_SUCCESS
;
719 DWORD BytesWritten32
= 0;
720 HANDLE Handle
= DosGetRealHandle(FileHandle
);
723 DPRINT("DosWriteFile: FileHandle 0x%04X, Count 0x%04X\n",
727 /* Make sure the handle is valid */
728 if (Handle
== INVALID_HANDLE_VALUE
) return ERROR_INVALID_HANDLE
;
730 if (IsConsoleHandle(Handle
))
732 for (i
= 0; i
< Count
; i
++)
734 /* Call the BIOS to print the character */
735 BiosPrintCharacter(((LPBYTE
)Buffer
)[i
], DOS_CHAR_ATTRIBUTE
, Bda
->VideoPage
);
742 if (!WriteFile(Handle
, Buffer
, Count
, &BytesWritten32
, NULL
))
744 /* Store the error code */
745 Result
= (WORD
)GetLastError();
749 /* The number of bytes written is always 16-bit */
750 *BytesWritten
= LOWORD(BytesWritten32
);
752 /* Return the error code */
756 WORD
DosSeekFile(WORD FileHandle
, LONG Offset
, BYTE Origin
, LPDWORD NewOffset
)
758 WORD Result
= ERROR_SUCCESS
;
760 HANDLE Handle
= DosGetRealHandle(FileHandle
);
762 DPRINT("DosSeekFile: FileHandle 0x%04X, Offset 0x%08X, Origin 0x%02X\n",
767 /* Make sure the handle is valid */
768 if (Handle
== INVALID_HANDLE_VALUE
) return ERROR_INVALID_HANDLE
;
770 /* Check if the origin is valid */
771 if (Origin
!= FILE_BEGIN
&& Origin
!= FILE_CURRENT
&& Origin
!= FILE_END
)
773 return ERROR_INVALID_FUNCTION
;
776 /* Move the file pointer */
777 FilePointer
= SetFilePointer(Handle
, Offset
, NULL
, Origin
);
779 /* Check if there's a possibility the operation failed */
780 if (FilePointer
== INVALID_SET_FILE_POINTER
)
782 /* Get the real error code */
783 Result
= (WORD
)GetLastError();
786 if (Result
!= ERROR_SUCCESS
)
788 /* The operation did fail */
792 /* Return the file pointer, if requested */
793 if (NewOffset
) *NewOffset
= FilePointer
;
796 return ERROR_SUCCESS
;
799 BOOLEAN
DosFlushFileBuffers(WORD FileHandle
)
801 HANDLE Handle
= DosGetRealHandle(FileHandle
);
803 /* Make sure the handle is valid */
804 if (Handle
== INVALID_HANDLE_VALUE
) return FALSE
;
807 * No need to check whether the handle is a console handle since
808 * FlushFileBuffers() automatically does this check and calls
809 * FlushConsoleInputBuffer() for us.
811 // if (IsConsoleHandle(Handle))
812 // return (BOOLEAN)FlushConsoleInputBuffer(Handle);
814 return (BOOLEAN
)FlushFileBuffers(Handle
);
817 BOOLEAN
DosDuplicateHandle(WORD OldHandle
, WORD NewHandle
)
823 DPRINT("DosDuplicateHandle: OldHandle 0x%04X, NewHandle 0x%04X\n",
827 /* The system PSP has no handle table */
828 if (CurrentPsp
== SYSTEM_PSP
) return FALSE
;
830 /* Get a pointer to the handle table */
831 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
832 HandleTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
834 /* Make sure the old handle is open */
835 if (HandleTable
[OldHandle
] == 0xFF) return FALSE
;
837 /* Check if the new handle is open */
838 if (HandleTable
[NewHandle
] != 0xFF)
841 DosCloseHandle(NewHandle
);
844 /* Increment the reference count of the SFT entry */
845 SftIndex
= HandleTable
[OldHandle
];
846 DosSftRefCount
[SftIndex
]++;
848 /* Make the new handle point to that SFT entry */
849 HandleTable
[NewHandle
] = SftIndex
;
855 BOOLEAN
DosCloseHandle(WORD DosHandle
)
861 DPRINT("DosCloseHandle: DosHandle 0x%04X\n", DosHandle
);
863 /* The system PSP has no handle table */
864 if (CurrentPsp
== SYSTEM_PSP
) return FALSE
;
866 /* Get a pointer to the handle table */
867 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
868 HandleTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
870 /* Make sure the handle is open */
871 if (HandleTable
[DosHandle
] == 0xFF) return FALSE
;
873 /* Decrement the reference count of the SFT entry */
874 SftIndex
= HandleTable
[DosHandle
];
875 DosSftRefCount
[SftIndex
]--;
877 /* Check if the reference count fell to zero */
878 if (!DosSftRefCount
[SftIndex
])
880 /* Close the file, it's no longer needed */
881 CloseHandle(DosSystemFileTable
[SftIndex
]);
883 /* Clear the handle */
884 DosSystemFileTable
[SftIndex
] = INVALID_HANDLE_VALUE
;
887 /* Clear the entry in the JFT */
888 HandleTable
[DosHandle
] = 0xFF;
893 BOOLEAN
DosChangeDrive(BYTE Drive
)
895 WCHAR DirectoryPath
[DOS_CMDLINE_LENGTH
];
897 /* Make sure the drive exists */
898 if (Drive
> (LastDrive
- 'A')) return FALSE
;
900 /* Find the path to the new current directory */
901 swprintf(DirectoryPath
, L
"%c\\%S", Drive
+ 'A', CurrentDirectories
[Drive
]);
903 /* Change the current directory of the process */
904 if (!SetCurrentDirectory(DirectoryPath
)) return FALSE
;
906 /* Set the current drive */
907 CurrentDrive
= Drive
;
913 BOOLEAN
DosChangeDirectory(LPSTR Directory
)
919 /* Make sure the directory path is not too long */
920 if (strlen(Directory
) >= DOS_DIR_LENGTH
)
922 DosLastError
= ERROR_PATH_NOT_FOUND
;
926 /* Get the drive number */
927 DriveNumber
= Directory
[0] - 'A';
929 /* Make sure the drive exists */
930 if (DriveNumber
> (LastDrive
- 'A'))
932 DosLastError
= ERROR_PATH_NOT_FOUND
;
936 /* Get the file attributes */
937 Attributes
= GetFileAttributesA(Directory
);
939 /* Make sure the path exists and is a directory */
940 if ((Attributes
== INVALID_FILE_ATTRIBUTES
)
941 || !(Attributes
& FILE_ATTRIBUTE_DIRECTORY
))
943 DosLastError
= ERROR_PATH_NOT_FOUND
;
947 /* Check if this is the current drive */
948 if (DriveNumber
== CurrentDrive
)
950 /* Change the directory */
951 if (!SetCurrentDirectoryA(Directory
))
953 DosLastError
= LOWORD(GetLastError());
958 /* Get the directory part of the path */
959 Path
= strchr(Directory
, '\\');
962 /* Skip the backslash */
966 /* Set the directory for the drive */
969 strncpy(CurrentDirectories
[DriveNumber
], Path
, DOS_DIR_LENGTH
);
973 CurrentDirectories
[DriveNumber
][0] = '\0';
980 VOID
DosInitializePsp(WORD PspSegment
, LPCSTR CommandLine
, WORD ProgramSize
, WORD Environment
)
982 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(PspSegment
);
983 LPDWORD IntVecTable
= (LPDWORD
)((ULONG_PTR
)BaseAddress
);
985 ZeroMemory(PspBlock
, sizeof(DOS_PSP
));
987 /* Set the exit interrupt */
988 PspBlock
->Exit
[0] = 0xCD; // int 0x20
989 PspBlock
->Exit
[1] = 0x20;
991 /* Set the number of the last paragraph */
992 PspBlock
->LastParagraph
= PspSegment
+ ProgramSize
- 1;
994 /* Save the interrupt vectors */
995 PspBlock
->TerminateAddress
= IntVecTable
[0x22];
996 PspBlock
->BreakAddress
= IntVecTable
[0x23];
997 PspBlock
->CriticalAddress
= IntVecTable
[0x24];
999 /* Set the parent PSP */
1000 PspBlock
->ParentPsp
= CurrentPsp
;
1002 /* Copy the parent handle table */
1003 DosCopyHandleTable(PspBlock
->HandleTable
);
1005 /* Set the environment block */
1006 PspBlock
->EnvBlock
= Environment
;
1008 /* Set the handle table pointers to the internal handle table */
1009 PspBlock
->HandleTableSize
= 20;
1010 PspBlock
->HandleTablePtr
= MAKELONG(0x18, PspSegment
);
1012 /* Set the DOS version */
1013 PspBlock
->DosVersion
= DOS_VERSION
;
1015 /* Set the far call opcodes */
1016 PspBlock
->FarCall
[0] = 0xCD; // int 0x21
1017 PspBlock
->FarCall
[1] = 0x21;
1018 PspBlock
->FarCall
[2] = 0xCB; // retf
1020 /* Set the command line */
1021 PspBlock
->CommandLineSize
= (BYTE
)min(strlen(CommandLine
), DOS_CMDLINE_LENGTH
- 1);
1022 RtlCopyMemory(PspBlock
->CommandLine
, CommandLine
, PspBlock
->CommandLineSize
);
1023 PspBlock
->CommandLine
[PspBlock
->CommandLineSize
] = '\r';
1026 BOOLEAN
DosCreateProcess(LPCSTR CommandLine
, WORD EnvBlock
)
1028 BOOLEAN Success
= FALSE
, AllocatedEnvBlock
= FALSE
;
1029 HANDLE FileHandle
= INVALID_HANDLE_VALUE
, FileMapping
= NULL
;
1030 LPBYTE Address
= NULL
;
1031 LPSTR ProgramFilePath
, Parameters
[256];
1032 CHAR CommandLineCopy
[DOS_CMDLINE_LENGTH
];
1033 CHAR ParamString
[DOS_CMDLINE_LENGTH
];
1037 DWORD i
, FileSize
, ExeSize
;
1038 PIMAGE_DOS_HEADER Header
;
1039 PDWORD RelocationTable
;
1042 DPRINT("DosCreateProcess: CommandLine \"%s\", EnvBlock 0x%04X\n",
1046 /* Save a copy of the command line */
1047 strcpy(CommandLineCopy
, CommandLine
);
1049 /* Get the file name of the executable */
1050 ProgramFilePath
= strtok(CommandLineCopy
, " \t");
1052 /* Load the parameters in the local array */
1053 while ((ParamCount
< sizeof(Parameters
)/sizeof(Parameters
[0]))
1054 && ((Parameters
[ParamCount
] = strtok(NULL
, " \t")) != NULL
))
1059 ZeroMemory(ParamString
, sizeof(ParamString
));
1061 /* Store the parameters in a string */
1062 for (i
= 0; i
< ParamCount
; i
++)
1064 strncat(ParamString
, Parameters
[i
], DOS_CMDLINE_LENGTH
- strlen(ParamString
) - 1);
1065 strncat(ParamString
, " ", DOS_CMDLINE_LENGTH
- strlen(ParamString
) - 1);
1068 /* Open a handle to the executable */
1069 FileHandle
= CreateFileA(ProgramFilePath
,
1074 FILE_ATTRIBUTE_NORMAL
,
1076 if (FileHandle
== INVALID_HANDLE_VALUE
) goto Cleanup
;
1078 /* Get the file size */
1079 FileSize
= GetFileSize(FileHandle
, NULL
);
1081 /* Create a mapping object for the file */
1082 FileMapping
= CreateFileMapping(FileHandle
,
1088 if (FileMapping
== NULL
) goto Cleanup
;
1090 /* Map the file into memory */
1091 Address
= (LPBYTE
)MapViewOfFile(FileMapping
, FILE_MAP_READ
, 0, 0, 0);
1092 if (Address
== NULL
) goto Cleanup
;
1094 /* Did we get an environment segment? */
1097 /* Set a flag to know if the environment block was allocated here */
1098 AllocatedEnvBlock
= TRUE
;
1100 /* No, copy the one from the parent */
1101 EnvBlock
= DosCopyEnvironmentBlock((CurrentPsp
!= SYSTEM_PSP
)
1102 ? SEGMENT_TO_PSP(CurrentPsp
)->EnvBlock
1107 /* Check if this is an EXE file or a COM file */
1108 if (Address
[0] == 'M' && Address
[1] == 'Z')
1112 /* Get the MZ header */
1113 Header
= (PIMAGE_DOS_HEADER
)Address
;
1115 /* Get the base size of the file, in paragraphs (rounded up) */
1116 ExeSize
= (((Header
->e_cp
- 1) * 512) + Header
->e_cblp
+ 0x0F) >> 4;
1118 /* Add the PSP size, in paragraphs */
1119 ExeSize
+= sizeof(DOS_PSP
) >> 4;
1121 /* Add the maximum size that should be allocated */
1122 ExeSize
+= Header
->e_maxalloc
;
1124 /* Make sure it does not pass 0xFFFF */
1125 if (ExeSize
> 0xFFFF) ExeSize
= 0xFFFF;
1127 /* Reduce the size one by one until the allocation is successful */
1128 for (i
= Header
->e_maxalloc
; i
>= Header
->e_minalloc
; i
--, ExeSize
--)
1130 /* Try to allocate that much memory */
1131 Segment
= DosAllocateMemory((WORD
)ExeSize
, NULL
);
1132 if (Segment
!= 0) break;
1135 /* Check if at least the lowest allocation was successful */
1136 if (Segment
== 0) goto Cleanup
;
1138 /* Initialize the PSP */
1139 DosInitializePsp(Segment
,
1144 /* The process owns its own memory */
1145 DosChangeMemoryOwner(Segment
, Segment
);
1146 DosChangeMemoryOwner(EnvBlock
, Segment
);
1148 /* Copy the program to Segment:0100 */
1149 RtlCopyMemory(SEG_OFF_TO_PTR(Segment
, 0x100),
1150 Address
+ (Header
->e_cparhdr
<< 4),
1151 min(FileSize
- (Header
->e_cparhdr
<< 4),
1152 (ExeSize
<< 4) - sizeof(DOS_PSP
)));
1154 /* Get the relocation table */
1155 RelocationTable
= (PDWORD
)(Address
+ Header
->e_lfarlc
);
1157 /* Perform relocations */
1158 for (i
= 0; i
< Header
->e_crlc
; i
++)
1160 /* Get a pointer to the word that needs to be patched */
1161 RelocWord
= (PWORD
)SEG_OFF_TO_PTR(Segment
+ HIWORD(RelocationTable
[i
]),
1162 0x100 + LOWORD(RelocationTable
[i
]));
1164 /* Add the number of the EXE segment to it */
1165 *RelocWord
+= Segment
+ (sizeof(DOS_PSP
) >> 4);
1168 /* Set the initial segment registers */
1172 /* Set the stack to the location from the header */
1173 EmulatorSetStack(Segment
+ (sizeof(DOS_PSP
) >> 4) + Header
->e_ss
,
1177 CurrentPsp
= Segment
;
1178 DiskTransferArea
= MAKELONG(0x80, Segment
);
1179 EmulatorExecute(Segment
+ Header
->e_cs
+ (sizeof(DOS_PSP
) >> 4),
1188 /* Find the maximum amount of memory that can be allocated */
1189 DosAllocateMemory(0xFFFF, &MaxAllocSize
);
1191 /* Make sure it's enough for the whole program and the PSP */
1192 if (((DWORD
)MaxAllocSize
<< 4) < (FileSize
+ sizeof(DOS_PSP
))) goto Cleanup
;
1194 /* Allocate all of it */
1195 Segment
= DosAllocateMemory(MaxAllocSize
, NULL
);
1196 if (Segment
== 0) goto Cleanup
;
1198 /* The process owns its own memory */
1199 DosChangeMemoryOwner(Segment
, Segment
);
1200 DosChangeMemoryOwner(EnvBlock
, Segment
);
1202 /* Copy the program to Segment:0100 */
1203 RtlCopyMemory(SEG_OFF_TO_PTR(Segment
, 0x100),
1207 /* Initialize the PSP */
1208 DosInitializePsp(Segment
,
1213 /* Set the initial segment registers */
1217 /* Set the stack to the last word of the segment */
1218 EmulatorSetStack(Segment
, 0xFFFE);
1221 * Set the value on the stack to 0, so that a near return
1222 * jumps to PSP:0000 which has the exit code.
1224 *((LPWORD
)SEG_OFF_TO_PTR(Segment
, 0xFFFE)) = 0;
1227 CurrentPsp
= Segment
;
1228 DiskTransferArea
= MAKELONG(0x80, Segment
);
1229 EmulatorExecute(Segment
, 0x100);
1237 /* It was not successful, cleanup the DOS memory */
1238 if (AllocatedEnvBlock
) DosFreeMemory(EnvBlock
);
1239 if (Segment
) DosFreeMemory(Segment
);
1243 if (Address
!= NULL
) UnmapViewOfFile(Address
);
1245 /* Close the file mapping object */
1246 if (FileMapping
!= NULL
) CloseHandle(FileMapping
);
1248 /* Close the file handle */
1249 if (FileHandle
!= INVALID_HANDLE_VALUE
) CloseHandle(FileHandle
);
1254 VOID
DosTerminateProcess(WORD Psp
, BYTE ReturnCode
)
1257 WORD McbSegment
= FIRST_MCB_SEGMENT
;
1258 PDOS_MCB CurrentMcb
;
1259 LPDWORD IntVecTable
= (LPDWORD
)((ULONG_PTR
)BaseAddress
);
1260 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(Psp
);
1262 DPRINT("DosTerminateProcess: Psp 0x%04X, ReturnCode 0x%02X\n",
1266 /* Check if this PSP is it's own parent */
1267 if (PspBlock
->ParentPsp
== Psp
) goto Done
;
1269 for (i
= 0; i
< PspBlock
->HandleTableSize
; i
++)
1271 /* Close the handle */
1275 /* Free the memory used by the process */
1278 /* Get a pointer to the MCB */
1279 CurrentMcb
= SEGMENT_TO_MCB(McbSegment
);
1281 /* Make sure the MCB is valid */
1282 if (CurrentMcb
->BlockType
!= 'M' && CurrentMcb
->BlockType
!='Z') break;
1284 /* If this block was allocated by the process, free it */
1285 if (CurrentMcb
->OwnerPsp
== Psp
) DosFreeMemory(McbSegment
+ 1);
1287 /* If this was the last block, quit */
1288 if (CurrentMcb
->BlockType
== 'Z') break;
1290 /* Update the segment and continue */
1291 McbSegment
+= CurrentMcb
->Size
+ 1;
1295 /* Restore the interrupt vectors */
1296 IntVecTable
[0x22] = PspBlock
->TerminateAddress
;
1297 IntVecTable
[0x23] = PspBlock
->BreakAddress
;
1298 IntVecTable
[0x24] = PspBlock
->CriticalAddress
;
1300 /* Update the current PSP */
1301 if (Psp
== CurrentPsp
)
1303 CurrentPsp
= PspBlock
->ParentPsp
;
1304 if (CurrentPsp
== SYSTEM_PSP
) VdmRunning
= FALSE
;
1307 /* Save the return code - Normal termination */
1308 DosErrorLevel
= MAKEWORD(ReturnCode
, 0x00);
1310 /* Return control to the parent process */
1311 EmulatorExecute(HIWORD(PspBlock
->TerminateAddress
),
1312 LOWORD(PspBlock
->TerminateAddress
));
1315 CHAR
DosReadCharacter(VOID
)
1317 CHAR Character
= '\0';
1320 if (IsConsoleHandle(DosGetRealHandle(DOS_INPUT_HANDLE
)))
1323 Character
= LOBYTE(BiosGetCharacter());
1327 /* Use the file reading function */
1328 DosReadFile(DOS_INPUT_HANDLE
, &Character
, sizeof(CHAR
), &BytesRead
);
1334 BOOLEAN
DosCheckInput(VOID
)
1336 HANDLE Handle
= DosGetRealHandle(DOS_INPUT_HANDLE
);
1338 if (IsConsoleHandle(Handle
))
1341 return (BiosPeekCharacter() != 0xFFFF);
1346 DWORD FileSize
= GetFileSize(Handle
, &FileSizeHigh
);
1347 LONG LocationHigh
= 0;
1348 DWORD Location
= SetFilePointer(Handle
, 0, &LocationHigh
, FILE_CURRENT
);
1350 return ((Location
!= FileSize
) || (LocationHigh
!= FileSizeHigh
));
1354 VOID
DosPrintCharacter(CHAR Character
)
1358 /* Use the file writing function */
1359 DosWriteFile(DOS_OUTPUT_HANDLE
, &Character
, sizeof(CHAR
), &BytesWritten
);
1362 BOOLEAN
DosHandleIoctl(BYTE ControlCode
, WORD FileHandle
)
1364 HANDLE Handle
= DosGetRealHandle(FileHandle
);
1366 if (Handle
== INVALID_HANDLE_VALUE
)
1369 DosLastError
= ERROR_FILE_NOT_FOUND
;
1373 switch (ControlCode
)
1375 /* Get Device Information */
1380 if (Handle
== DosSystemFileTable
[0])
1385 else if (Handle
== DosSystemFileTable
[1])
1387 /* Console output */
1391 /* It is a character device */
1394 /* Return the device information word */
1399 /* Unsupported control code */
1402 DPRINT1("Unsupported IOCTL: 0x%02X\n", ControlCode
);
1404 DosLastError
= ERROR_INVALID_PARAMETER
;
1410 VOID WINAPI
DosSystemBop(LPWORD Stack
)
1412 /* Get the Function Number and skip it */
1413 BYTE FuncNum
= *(PBYTE
)SEG_OFF_TO_PTR(getCS(), getIP());
1416 DPRINT1("Unknown DOS System BOP Function: 0x%02X\n", FuncNum
);
1419 VOID WINAPI
DosCmdInterpreterBop(LPWORD Stack
)
1421 /* Get the Function Number and skip it */
1422 BYTE FuncNum
= *(PBYTE
)SEG_OFF_TO_PTR(getCS(), getIP());
1427 case 0x08: // Launch external command
1429 #define CMDLINE_LENGTH 1024
1434 LPSTR Command
= (LPSTR
)SEG_OFF_TO_PTR(getDS(), getSI());
1435 CHAR CommandLine
[CMDLINE_LENGTH
] = "";
1436 STARTUPINFOA StartupInfo
;
1437 PROCESS_INFORMATION ProcessInformation
;
1438 DPRINT1("CMD Run Command '%s'\n", Command
);
1440 Command
[strlen(Command
)-1] = 0;
1442 strcpy(CommandLine
, "cmd.exe /c ");
1443 strcat(CommandLine
, Command
);
1445 ZeroMemory(&StartupInfo
, sizeof(StartupInfo
));
1446 ZeroMemory(&ProcessInformation
, sizeof(ProcessInformation
));
1448 StartupInfo
.cb
= sizeof(StartupInfo
);
1450 DosPrintCharacter('\n');
1452 Result
= CreateProcessA(NULL
,
1461 &ProcessInformation
);
1464 DPRINT1("Command '%s' launched successfully\n");
1466 /* Wait for process termination */
1467 WaitForSingleObject(ProcessInformation
.hProcess
, INFINITE
);
1469 /* Get the exit code */
1470 GetExitCodeProcess(ProcessInformation
.hProcess
, &dwExitCode
);
1473 CloseHandle(ProcessInformation
.hThread
);
1474 CloseHandle(ProcessInformation
.hProcess
);
1478 DPRINT1("Failed when launched command '%s'\n");
1479 dwExitCode
= GetLastError();
1482 DosPrintCharacter('\n');
1484 setAL((UCHAR
)dwExitCode
);
1491 DPRINT1("Unknown DOS CMD Interpreter BOP Function: 0x%02X\n", FuncNum
);
1492 // setCF(1); // Disable, otherwise we enter an infinite loop
1498 VOID WINAPI
DosInt20h(LPWORD Stack
)
1500 /* This is the exit interrupt */
1501 DosTerminateProcess(Stack
[STACK_CS
], 0);
1504 VOID WINAPI
DosInt21h(LPWORD Stack
)
1507 SYSTEMTIME SystemTime
;
1509 PDOS_INPUT_BUFFER InputBuffer
;
1511 /* Check the value in the AH register */
1514 /* Terminate Program */
1517 DosTerminateProcess(Stack
[STACK_CS
], 0);
1521 /* Read Character from STDIN with Echo */
1524 Character
= DosReadCharacter();
1525 DosPrintCharacter(Character
);
1527 /* Let the BOP repeat if needed */
1534 /* Write Character to STDOUT */
1537 Character
= getDL();
1538 DosPrintCharacter(Character
);
1541 * We return the output character (DOS 2.1+).
1542 * Also, if we're going to output a TAB, then
1543 * don't return a TAB but a SPACE instead.
1544 * See Ralf Brown: http://www.ctyme.com/intr/rb-2554.htm
1545 * for more information.
1547 setAL(Character
== '\t' ? ' ' : Character
);
1551 /* Read Character from STDAUX */
1554 // FIXME: Really read it from STDAUX!
1555 DPRINT1("INT 16h, 03h: Read character from STDAUX is HALFPLEMENTED\n");
1556 setAL(DosReadCharacter());
1560 /* Write Character to STDAUX */
1563 // FIXME: Really write it to STDAUX!
1564 DPRINT1("INT 16h, 04h: Write character to STDAUX is HALFPLEMENTED\n");
1565 DosPrintCharacter(getDL());
1569 /* Write Character to Printer */
1572 // FIXME: Really write it to printer!
1573 DPRINT1("INT 16h, 05h: Write character to printer is HALFPLEMENTED -\n\n");
1574 DPRINT1("0x%p\n", getDL());
1575 DPRINT1("\n\n-----------\n\n");
1579 /* Direct Console I/O */
1582 Character
= getDL();
1584 if (Character
!= 0xFF)
1587 DosPrintCharacter(Character
);
1590 * We return the output character (DOS 2.1+).
1591 * See Ralf Brown: http://www.ctyme.com/intr/rb-2558.htm
1592 * for more information.
1599 if (DosCheckInput())
1601 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_ZF
;
1602 setAL(DosReadCharacter());
1606 /* No character available */
1607 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_ZF
;
1615 /* Character Input without Echo */
1619 Character
= DosReadCharacter();
1621 /* Let the BOP repeat if needed */
1628 /* Write string to STDOUT */
1631 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getDX());
1633 while (*String
!= '$')
1635 DosPrintCharacter(*String
);
1640 * We return the terminating character (DOS 2.1+).
1641 * See Ralf Brown: http://www.ctyme.com/intr/rb-2562.htm
1642 * for more information.
1648 /* Read Buffered Input */
1651 InputBuffer
= (PDOS_INPUT_BUFFER
)SEG_OFF_TO_PTR(getDS(), getDX());
1653 while (Stack
[STACK_COUNTER
] < InputBuffer
->MaxLength
)
1655 /* Try to read a character */
1656 Character
= DosReadCharacter();
1658 /* If it's not ready yet, let the BOP repeat */
1661 /* Echo the character and append it to the buffer */
1662 DosPrintCharacter(Character
);
1663 InputBuffer
->Buffer
[Stack
[STACK_COUNTER
]] = Character
;
1665 if (Character
== '\r') break;
1666 Stack
[STACK_COUNTER
]++;
1669 /* Update the length */
1670 InputBuffer
->Length
= Stack
[STACK_COUNTER
];
1674 /* Get STDIN Status */
1677 setAL(DosCheckInput() ? 0xFF : 0x00);
1681 /* Flush Buffer and Read STDIN */
1684 BYTE InputFunction
= getAL();
1686 /* Flush STDIN buffer */
1687 DosFlushFileBuffers(DOS_INPUT_HANDLE
); // Maybe just create a DosFlushInputBuffer...
1690 * If the input function number contained in AL is valid, i.e.
1691 * AL == 0x01 or 0x06 or 0x07 or 0x08 or 0x0A, call ourselves
1692 * recursively with AL == AH.
1694 if (InputFunction
== 0x01 || InputFunction
== 0x06 ||
1695 InputFunction
== 0x07 || InputFunction
== 0x08 ||
1696 InputFunction
== 0x0A)
1698 setAH(InputFunction
);
1700 * Instead of calling ourselves really recursively as in:
1702 * prefer resetting the CF flag to let the BOP repeat.
1712 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
1714 // TODO: Flush what's needed.
1715 DPRINT1("INT 21h, 0Dh is UNIMPLEMENTED\n");
1717 /* Clear CF in DOS 6 only */
1718 if (PspBlock
->DosVersion
== 0x0006)
1719 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
1724 /* Set Default Drive */
1727 DosChangeDrive(getDL());
1728 setAL(LastDrive
- 'A' + 1);
1732 /* NULL Function for CP/M Compatibility */
1736 * This function corresponds to the CP/M BDOS function
1737 * "get bit map of logged drives", which is meaningless
1740 * For: PTS-DOS 6.51 & S/DOS 1.0 - EXTENDED RENAME FILE USING FCB
1741 * See Ralf Brown: http://www.ctyme.com/intr/rb-2584.htm
1742 * for more information.
1748 /* Get Default Drive */
1751 setAL(CurrentDrive
);
1755 /* Set Disk Transfer Area */
1758 DiskTransferArea
= MAKELONG(getDX(), getDS());
1762 /* NULL Function for CP/M Compatibility */
1767 * Function 0x1D corresponds to the CP/M BDOS function
1768 * "get bit map of read-only drives", which is meaningless
1770 * See Ralf Brown: http://www.ctyme.com/intr/rb-2592.htm
1771 * for more information.
1773 * Function 0x1E corresponds to the CP/M BDOS function
1774 * "set file attributes", which was meaningless under MS-DOS 1.x.
1775 * See Ralf Brown: http://www.ctyme.com/intr/rb-2593.htm
1776 * for more information.
1782 /* NULL Function for CP/M Compatibility */
1786 * This function corresponds to the CP/M BDOS function
1787 * "get/set default user (sublibrary) number", which is meaningless
1790 * For: S/DOS 1.0+ & PTS-DOS 6.51+ - GET OEM REVISION
1791 * See Ralf Brown: http://www.ctyme.com/intr/rb-2596.htm
1792 * for more information.
1798 /* Set Interrupt Vector */
1801 DWORD FarPointer
= MAKELONG(getDX(), getDS());
1802 DPRINT1("Setting interrupt 0x%x ...\n", getAL());
1804 /* Write the new far pointer to the IDT */
1805 ((PDWORD
)BaseAddress
)[getAL()] = FarPointer
;
1809 /* Create New PSP */
1812 DPRINT1("INT 21h, 26h - Create New PSP is UNIMPLEMENTED\n");
1816 /* Get System Date */
1819 GetLocalTime(&SystemTime
);
1820 setCX(SystemTime
.wYear
);
1821 setDX(MAKEWORD(SystemTime
.wDay
, SystemTime
.wMonth
));
1822 setAL(SystemTime
.wDayOfWeek
);
1826 /* Set System Date */
1829 GetLocalTime(&SystemTime
);
1830 SystemTime
.wYear
= getCX();
1831 SystemTime
.wMonth
= getDH();
1832 SystemTime
.wDay
= getDL();
1834 /* Return success or failure */
1835 setAL(SetLocalTime(&SystemTime
) ? 0x00 : 0xFF);
1839 /* Get System Time */
1842 GetLocalTime(&SystemTime
);
1843 setCX(MAKEWORD(SystemTime
.wMinute
, SystemTime
.wHour
));
1844 setDX(MAKEWORD(SystemTime
.wMilliseconds
/ 10, SystemTime
.wSecond
));
1848 /* Set System Time */
1851 GetLocalTime(&SystemTime
);
1852 SystemTime
.wHour
= getCH();
1853 SystemTime
.wMinute
= getCL();
1854 SystemTime
.wSecond
= getDH();
1855 SystemTime
.wMilliseconds
= getDL() * 10; // In hundredths of seconds
1857 /* Return success or failure */
1858 setAL(SetLocalTime(&SystemTime
) ? 0x00 : 0xFF);
1862 /* Get Disk Transfer Area */
1865 setES(HIWORD(DiskTransferArea
));
1866 setBX(LOWORD(DiskTransferArea
));
1870 /* Get DOS Version */
1873 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
1876 * DOS 2+ - GET DOS VERSION
1877 * See Ralf Brown: http://www.ctyme.com/intr/rb-2711.htm
1878 * for more information.
1881 if (LOBYTE(PspBlock
->DosVersion
) < 5 || getAL() == 0x00)
1884 * Return DOS OEM number:
1885 * 0x00 for IBM PC-DOS
1886 * 0x02 for packaged MS-DOS
1891 if (LOBYTE(PspBlock
->DosVersion
) >= 5 && getAL() == 0x01)
1894 * Return version flag:
1895 * 1 << 3 if DOS is in ROM,
1896 * 0 (reserved) if not.
1901 /* Return DOS 24-bit user serial number in BL:CX */
1906 * Return DOS version: Minor:Major in AH:AL
1907 * The Windows NT DOS box returns version 5.00, subject to SETVER.
1909 setAX(PspBlock
->DosVersion
);
1914 /* Extended functionalities */
1917 if (getAL() == 0x06)
1920 * DOS 5+ - GET TRUE VERSION NUMBER
1921 * This function always returns the true version number, unlike
1922 * AH=30h, whose return value may be changed with SETVER.
1923 * See Ralf Brown: http://www.ctyme.com/intr/rb-2730.htm
1924 * for more information.
1928 * Return the true DOS version: Minor:Major in BH:BL
1929 * The Windows NT DOS box returns BX=3205h (version 5.50).
1931 setBX(NTDOS_VERSION
);
1933 /* DOS revision 0 */
1941 // /* Invalid subfunction */
1948 /* Get Interrupt Vector */
1951 DWORD FarPointer
= ((PDWORD
)BaseAddress
)[getAL()];
1953 /* Read the address from the IDT into ES:BX */
1954 setES(HIWORD(FarPointer
));
1955 setBX(LOWORD(FarPointer
));
1959 /* SWITCH character - AVAILDEV */
1962 if (getAL() == 0x00)
1965 * DOS 2+ - "SWITCHAR" - GET SWITCH CHARACTER
1966 * This setting is ignored by MS-DOS 4.0+.
1967 * MS-DOS 5+ always return AL=00h/DL=2Fh.
1968 * See Ralf Brown: http://www.ctyme.com/intr/rb-2752.htm
1969 * for more information.
1974 else if (getAL() == 0x01)
1977 * DOS 2+ - "SWITCHAR" - SET SWITCH CHARACTER
1978 * This setting is ignored by MS-DOS 5+.
1979 * See Ralf Brown: http://www.ctyme.com/intr/rb-2753.htm
1980 * for more information.
1985 else if (getAL() == 0x02)
1988 * DOS 2.x and 3.3+ only - "AVAILDEV" - SPECIFY \DEV\ PREFIX USE
1989 * See Ralf Brown: http://www.ctyme.com/intr/rb-2754.htm
1990 * for more information.
1995 else if (getAL() == 0x03)
1998 * DOS 2.x and 3.3+ only - "AVAILDEV" - SPECIFY \DEV\ PREFIX USE
1999 * See Ralf Brown: http://www.ctyme.com/intr/rb-2754.htm
2000 * for more information.
2007 /* Invalid subfunction */
2014 /* Create Directory */
2017 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getDX());
2019 if (CreateDirectoryA(String
, NULL
))
2021 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2025 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2026 setAX(LOWORD(GetLastError()));
2032 /* Remove Directory */
2035 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getDX());
2037 if (RemoveDirectoryA(String
))
2039 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2043 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2044 setAX(LOWORD(GetLastError()));
2050 /* Set Current Directory */
2053 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getDX());
2055 if (DosChangeDirectory(String
))
2057 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2061 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2062 setAX(DosLastError
);
2072 WORD ErrorCode
= DosCreateFile(&FileHandle
,
2073 (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getDX()),
2078 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2083 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2094 WORD ErrorCode
= DosOpenFile(&FileHandle
,
2095 (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getDX()),
2100 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2105 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2115 if (DosCloseHandle(getBX()))
2117 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2121 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2122 setAX(ERROR_INVALID_HANDLE
);
2128 /* Read from File or Device */
2131 WORD Handle
= getBX();
2132 LPBYTE Buffer
= (LPBYTE
)SEG_OFF_TO_PTR(getDS(), getDX());
2133 WORD Count
= getCX();
2135 WORD ErrorCode
= ERROR_SUCCESS
;
2138 if (IsConsoleHandle(DosGetRealHandle(Handle
)))
2140 while (Stack
[STACK_COUNTER
] < Count
)
2142 /* Read a character from the BIOS */
2143 Character
= LOBYTE(BiosGetCharacter());
2145 /* Stop if the BOP needs to be repeated */
2148 // FIXME: Security checks!
2149 DosPrintCharacter(Character
);
2150 Buffer
[Stack
[STACK_COUNTER
]++] = Character
;
2152 if (Character
== '\r')
2154 /* Stop on first carriage return */
2155 DosPrintCharacter('\n');
2160 if (Character
!= '\r')
2162 if (Stack
[STACK_COUNTER
] < Count
) ErrorCode
= ERROR_NOT_READY
;
2163 else BytesRead
= Count
;
2165 else BytesRead
= Stack
[STACK_COUNTER
];
2169 /* Use the file reading function */
2170 ErrorCode
= DosReadFile(Handle
, Buffer
, Count
, &BytesRead
);
2173 if (ErrorCode
== ERROR_SUCCESS
)
2175 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2178 else if (ErrorCode
!= ERROR_NOT_READY
)
2180 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2186 /* Write to File or Device */
2189 WORD BytesWritten
= 0;
2190 WORD ErrorCode
= DosWriteFile(getBX(),
2191 SEG_OFF_TO_PTR(getDS(), getDX()),
2195 if (ErrorCode
== ERROR_SUCCESS
)
2197 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2198 setAX(BytesWritten
);
2202 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2212 LPSTR FileName
= (LPSTR
)SEG_OFF_TO_PTR(getDS(), getDX());
2214 /* Call the API function */
2215 if (DeleteFileA(FileName
))
2217 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2219 * See Ralf Brown: http://www.ctyme.com/intr/rb-2797.htm
2220 * "AX destroyed (DOS 3.3) AL seems to be drive of deleted file."
2222 setAL(FileName
[0] - 'A');
2226 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2227 setAX(GetLastError());
2237 WORD ErrorCode
= DosSeekFile(getBX(),
2238 MAKELONG(getDX(), getCX()),
2242 if (ErrorCode
== ERROR_SUCCESS
)
2244 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2246 /* Return the new offset in DX:AX */
2247 setDX(HIWORD(NewLocation
));
2248 setAX(LOWORD(NewLocation
));
2252 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2259 /* Get/Set File Attributes */
2263 LPSTR FileName
= (LPSTR
)SEG_OFF_TO_PTR(getDS(), getDX());
2265 if (getAL() == 0x00)
2267 /* Get the attributes */
2268 Attributes
= GetFileAttributesA(FileName
);
2270 /* Check if it failed */
2271 if (Attributes
== INVALID_FILE_ATTRIBUTES
)
2273 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2274 setAX(GetLastError());
2278 /* Return the attributes that DOS can understand */
2279 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2280 setCX(Attributes
& 0x00FF);
2283 else if (getAL() == 0x01)
2285 /* Try to set the attributes */
2286 if (SetFileAttributesA(FileName
, getCL()))
2288 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2292 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2293 setAX(GetLastError());
2298 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2299 setAX(ERROR_INVALID_FUNCTION
);
2308 if (DosHandleIoctl(getAL(), getBX()))
2310 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2314 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2315 setAX(DosLastError
);
2321 /* Duplicate Handle */
2325 HANDLE Handle
= DosGetRealHandle(getBX());
2327 if (Handle
!= INVALID_HANDLE_VALUE
)
2329 /* The handle is invalid */
2330 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2331 setAX(ERROR_INVALID_HANDLE
);
2335 /* Open a new handle to the same entry */
2336 NewHandle
= DosOpenHandle(Handle
);
2338 if (NewHandle
== INVALID_DOS_HANDLE
)
2340 /* Too many files open */
2341 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2342 setAX(ERROR_TOO_MANY_OPEN_FILES
);
2346 /* Return the result */
2347 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2352 /* Force Duplicate Handle */
2355 if (DosDuplicateHandle(getBX(), getCX()))
2357 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2361 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2362 setAX(ERROR_INVALID_HANDLE
);
2368 /* Get Current Directory */
2371 BYTE DriveNumber
= getDL();
2372 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getSI());
2374 /* Get the real drive number */
2375 if (DriveNumber
== 0)
2377 DriveNumber
= CurrentDrive
;
2381 /* Decrement DriveNumber since it was 1-based */
2385 if (DriveNumber
<= LastDrive
- 'A')
2388 * Copy the current directory into the target buffer.
2389 * It doesn't contain the drive letter and the backslash.
2391 strncpy(String
, CurrentDirectories
[DriveNumber
], DOS_DIR_LENGTH
);
2392 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2393 setAX(0x0100); // Undocumented, see Ralf Brown: http://www.ctyme.com/intr/rb-2933.htm
2397 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2398 setAX(ERROR_INVALID_DRIVE
);
2404 /* Allocate Memory */
2407 WORD MaxAvailable
= 0;
2408 WORD Segment
= DosAllocateMemory(getBX(), &MaxAvailable
);
2412 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2417 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2418 setAX(DosLastError
);
2419 setBX(MaxAvailable
);
2428 if (DosFreeMemory(getES()))
2430 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2434 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2435 setAX(ERROR_ARENA_TRASHED
);
2441 /* Resize Memory Block */
2446 if (DosResizeMemory(getES(), getBX(), &Size
))
2448 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2452 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2453 setAX(DosLastError
);
2460 /* Terminate With Return Code */
2463 DosTerminateProcess(CurrentPsp
, getAL());
2467 /* Get Return Code (ERRORLEVEL) */
2471 * According to Ralf Brown: http://www.ctyme.com/intr/rb-2976.htm
2472 * DosErrorLevel is cleared after being read by this function.
2474 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2475 setAX(DosErrorLevel
);
2476 DosErrorLevel
= 0x0000; // Clear it
2480 /* Internal - Set Current Process ID (Set PSP Address) */
2483 // FIXME: Is it really what it's done ??
2484 CurrentPsp
= getBX();
2488 /* Internal - Get Current Process ID (Get PSP Address) */
2490 /* Get Current PSP Address */
2494 * Undocumented AH=51h is identical to the documented AH=62h.
2495 * See Ralf Brown: http://www.ctyme.com/intr/rb-2982.htm
2496 * and http://www.ctyme.com/intr/rb-3140.htm
2497 * for more information.
2503 /* Get/Set Memory Management Options */
2506 if (getAL() == 0x00)
2508 /* Get allocation strategy */
2509 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2510 setAX(DosAllocStrategy
);
2512 else if (getAL() == 0x01)
2514 /* Set allocation strategy */
2516 if ((getBL() & (DOS_ALLOC_HIGH
| DOS_ALLOC_HIGH_LOW
))
2517 == (DOS_ALLOC_HIGH
| DOS_ALLOC_HIGH_LOW
))
2519 /* Can't set both */
2520 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2521 setAX(ERROR_INVALID_PARAMETER
);
2525 if ((getBL() & 0x3F) > DOS_ALLOC_LAST_FIT
)
2527 /* Invalid allocation strategy */
2528 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2529 setAX(ERROR_INVALID_PARAMETER
);
2533 DosAllocStrategy
= getBL();
2534 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2536 else if (getAL() == 0x02)
2538 /* Get UMB link state */
2539 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2540 setAL(DosUmbLinked
? 0x01 : 0x00);
2542 else if (getAL() == 0x03)
2544 /* Set UMB link state */
2545 if (getBX()) DosLinkUmb();
2546 else DosUnlinkUmb();
2547 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2551 /* Invalid or unsupported function */
2552 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2553 setAX(ERROR_INVALID_FUNCTION
);
2562 DPRINT1("DOS Function INT 0x21, AH = %xh, AL = %xh NOT IMPLEMENTED!\n",
2565 setAL(0); // Some functions expect AL to be 0 when it's not supported.
2566 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2571 VOID WINAPI
DosBreakInterrupt(LPWORD Stack
)
2573 UNREFERENCED_PARAMETER(Stack
);
2579 VOID WINAPI
DosFastConOut(LPWORD Stack
)
2582 * This is the DOS 2+ Fast Console Output Interrupt.
2583 * See Ralf Brown: http://www.ctyme.com/intr/rb-4124.htm
2584 * for more information.
2586 UNREFERENCED_PARAMETER(Stack
);
2589 * The default handler under DOS 2.x and 3.x simply calls INT 10/AH=0Eh.
2590 * Do better and call directly BiosPrintCharacter: it's what INT 10/AH=0Eh
2591 * does. Otherwise we would have to set BL to DOS_CHAR_ATTRIBUTE and
2592 * BH to Bda->VideoPage.
2594 BiosPrintCharacter(getAL(), DOS_CHAR_ATTRIBUTE
, Bda
->VideoPage
);
2597 VOID WINAPI
DosInt2Fh(LPWORD Stack
)
2599 DPRINT1("DOS System Function INT 0x2F, AH = %xh, AL = %xh NOT IMPLEMENTED!\n",
2601 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2604 BOOLEAN
DosInitialize(VOID
)
2607 PDOS_MCB Mcb
= SEGMENT_TO_MCB(FIRST_MCB_SEGMENT
);
2610 LPWSTR SourcePtr
, Environment
;
2612 LPSTR DestPtr
= (LPSTR
)SEG_OFF_TO_PTR(SYSTEM_ENV_BLOCK
, 0);
2614 CHAR CurrentDirectory
[MAX_PATH
];
2615 CHAR DosDirectory
[DOS_DIR_LENGTH
];
2618 /* Initialize the MCB */
2619 Mcb
->BlockType
= 'Z';
2620 Mcb
->Size
= USER_MEMORY_SIZE
;
2623 /* Initialize the link MCB to the UMB area */
2624 Mcb
= SEGMENT_TO_MCB(FIRST_MCB_SEGMENT
+ USER_MEMORY_SIZE
+ 1);
2625 Mcb
->BlockType
= 'M';
2626 Mcb
->Size
= UMB_START_SEGMENT
- FIRST_MCB_SEGMENT
- USER_MEMORY_SIZE
- 2;
2627 Mcb
->OwnerPsp
= SYSTEM_PSP
;
2629 /* Initialize the UMB area */
2630 Mcb
= SEGMENT_TO_MCB(UMB_START_SEGMENT
);
2631 Mcb
->BlockType
= 'Z';
2632 Mcb
->Size
= UMB_END_SEGMENT
- UMB_START_SEGMENT
;
2635 /* Get the environment strings */
2636 SourcePtr
= Environment
= GetEnvironmentStringsW();
2637 if (Environment
== NULL
) return FALSE
;
2639 /* Fill the DOS system environment block */
2642 /* Get the size of the ASCII string */
2643 AsciiSize
= WideCharToMultiByte(CP_ACP
,
2652 /* Allocate memory for the ASCII string */
2653 AsciiString
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, AsciiSize
);
2654 if (AsciiString
== NULL
)
2656 FreeEnvironmentStringsW(Environment
);
2660 /* Convert to ASCII */
2661 WideCharToMultiByte(CP_ACP
,
2670 /* Copy the string into DOS memory */
2671 strcpy(DestPtr
, AsciiString
);
2673 /* Move to the next string */
2674 SourcePtr
+= wcslen(SourcePtr
) + 1;
2675 DestPtr
+= strlen(AsciiString
);
2678 /* Free the memory */
2679 HeapFree(GetProcessHeap(), 0, AsciiString
);
2683 /* Free the memory allocated for environment strings */
2684 FreeEnvironmentStringsW(Environment
);
2686 /* Clear the current directory buffer */
2687 ZeroMemory(CurrentDirectories
, sizeof(CurrentDirectories
));
2689 /* Get the current directory */
2690 if (!GetCurrentDirectoryA(MAX_PATH
, CurrentDirectory
))
2692 // TODO: Use some kind of default path?
2696 /* Convert that to a DOS path */
2697 if (!GetShortPathNameA(CurrentDirectory
, DosDirectory
, DOS_DIR_LENGTH
))
2699 // TODO: Use some kind of default path?
2704 CurrentDrive
= DosDirectory
[0] - 'A';
2706 /* Get the directory part of the path */
2707 Path
= strchr(DosDirectory
, '\\');
2710 /* Skip the backslash */
2714 /* Set the directory */
2717 strncpy(CurrentDirectories
[CurrentDrive
], Path
, DOS_DIR_LENGTH
);
2720 /* Read CONFIG.SYS */
2721 Stream
= _wfopen(DOS_CONFIG_PATH
, L
"r");
2724 while (fgetws(Buffer
, 256, Stream
))
2726 // TODO: Parse the line
2731 /* Initialize the SFT */
2732 for (i
= 0; i
< DOS_SFT_SIZE
; i
++)
2734 DosSystemFileTable
[i
] = INVALID_HANDLE_VALUE
;
2735 DosSftRefCount
[i
] = 0;
2738 /* Get handles to standard I/O devices */
2739 DosSystemFileTable
[0] = GetStdHandle(STD_INPUT_HANDLE
);
2740 DosSystemFileTable
[1] = GetStdHandle(STD_OUTPUT_HANDLE
);
2741 DosSystemFileTable
[2] = GetStdHandle(STD_ERROR_HANDLE
);
2743 /* Register the DOS BOPs */
2744 RegisterBop(BOP_DOS
, DosSystemBop
);
2745 RegisterBop(BOP_CMD
, DosCmdInterpreterBop
);
2747 /* Register the DOS 32-bit Interrupts */
2748 RegisterInt32(0x20, DosInt20h
);
2749 RegisterInt32(0x21, DosInt21h
);
2750 // RegisterInt32(0x22, DosInt22h ); // Termination
2751 RegisterInt32(0x23, DosBreakInterrupt
); // Ctrl-C / Ctrl-Break
2752 // RegisterInt32(0x24, DosInt24h ); // Critical Error
2753 RegisterInt32(0x29, DosFastConOut
); // DOS 2+ Fast Console Output
2754 RegisterInt32(0x2F, DosInt2Fh
);