2 * DOS interrupt 21h handler
4 * Copyright 1993, 1994 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1997 Andreas Mohr
7 * Copyright 1998 Uwe Bonnes
8 * Copyright 1998, 1999 Ove Kaaven
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/winbase16.h"
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
42 * FIXME: Delete this reference when all int21 code has been moved to winedos.
44 extern void WINAPI INT_Int21Handler( CONTEXT86 *context );
47 * Forward declarations.
49 static BOOL INT21_RenameFile( CONTEXT86 *context );
51 WINE_DEFAULT_DEBUG_CHANNEL(int21);
57 * Structure for DOS data that can be accessed directly from applications.
58 * Real and protected mode pointers will be returned to this structure so
59 * the structure must be correctly packed.
61 typedef struct _INT21_HEAP {
62 WORD uppercase_size; /* Size of the following table in bytes */
63 BYTE uppercase_table[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
65 WORD lowercase_size; /* Size of the following table in bytes */
66 BYTE lowercase_table[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
68 WORD collating_size; /* Size of the following table in bytes */
69 BYTE collating_table[256]; /* Values used to sort characters from 0x00 to 0xff. */
71 WORD filename_size; /* Size of the following filename data in bytes */
72 BYTE filename_reserved1; /* 0x01 for MS-DOS 3.30-6.00 */
73 BYTE filename_lowest; /* Lowest permissible character value for filename */
74 BYTE filename_highest; /* Highest permissible character value for filename */
75 BYTE filename_reserved2; /* 0x00 for MS-DOS 3.30-6.00 */
76 BYTE filename_exclude_first; /* First illegal character in permissible range */
77 BYTE filename_exclude_last; /* Last illegal character in permissible range */
78 BYTE filename_reserved3; /* 0x02 for MS-DOS 3.30-6.00 */
79 BYTE filename_illegal_size; /* Number of terminators in the following table */
80 BYTE filename_illegal_table[16]; /* Characters which terminate a filename */
82 WORD dbcs_size; /* Number of valid ranges in the following table */
83 BYTE dbcs_table[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
85 BYTE misc_indos; /* Interrupt 21 nesting flag */
91 /***********************************************************************
94 * Reads a character from the standard input.
95 * Extended keycodes will be returned as two separate characters.
97 static BOOL INT21_ReadChar( BYTE *input, BOOL peek )
99 static BYTE pending_scan = 0;
104 *input = pending_scan;
113 if (!DOSVM_Int16ReadChar( &ascii, &scan, peek ))
125 /***********************************************************************
126 * INT21_GetSystemCountryCode
128 * Return DOS country code for default system locale.
130 static WORD INT21_GetSystemCountryCode()
133 * FIXME: Determine country code. We should probably use
134 * DOSCONF structure for that.
136 return GetSystemDefaultLangID();
140 /***********************************************************************
141 * INT21_FillCountryInformation
143 * Fill 34-byte buffer with country information data using
144 * default system locale.
146 static void INT21_FillCountryInformation( BYTE *buffer )
148 /* 00 - WORD: date format
153 *(WORD*)(buffer + 0) = 0; /* FIXME: Get from locale */
155 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
156 buffer[2] = '$'; /* FIXME: Get from locale */
159 /* 07 - BYTE[2]: ASCIIZ thousands separator */
160 buffer[7] = 0; /* FIXME: Get from locale */
163 /* 09 - BYTE[2]: ASCIIZ decimal separator */
164 buffer[9] = '.'; /* FIXME: Get from locale */
167 /* 11 - BYTE[2]: ASCIIZ date separator */
168 buffer[11] = '/'; /* FIXME: Get from locale */
171 /* 13 - BYTE[2]: ASCIIZ time separator */
172 buffer[13] = ':'; /* FIXME: Get from locale */
175 /* 15 - BYTE: Currency format
176 * bit 2 = set if currency symbol replaces decimal point
177 * bit 1 = number of spaces between value and currency symbol
178 * bit 0 = 0 if currency symbol precedes value
179 * 1 if currency symbol follows value
181 buffer[15] = 0; /* FIXME: Get from locale */
183 /* 16 - BYTE: Number of digits after decimal in currency */
184 buffer[16] = 0; /* FIXME: Get from locale */
186 /* 17 - BYTE: Time format
187 * bit 0 = 0 if 12-hour clock
190 buffer[17] = 1; /* FIXME: Get from locale */
192 /* 18 - DWORD: Address of case map routine */
193 *(DWORD*)(buffer + 18) = 0; /* FIXME: ptr to case map routine */
195 /* 22 - BYTE[2]: ASCIIZ data-list separator */
196 buffer[22] = ','; /* FIXME: Get from locale */
199 /* 24 - BYTE[10]: Reserved */
200 memset( buffer + 24, 0, 10 );
204 /***********************************************************************
207 * Initialize DOS heap.
209 static void INT21_FillHeap( INT21_HEAP *heap )
211 static const char terminators[] = "\"\\./[]:|<>+=;,";
217 heap->uppercase_size = 128;
218 for (i = 0; i < 128; i++)
219 heap->uppercase_table[i] = toupper( 128 + i );
224 heap->lowercase_size = 256;
225 for (i = 0; i < 256; i++)
226 heap->lowercase_table[i] = tolower( i );
231 heap->collating_size = 256;
232 for (i = 0; i < 256; i++)
233 heap->collating_table[i] = i;
238 heap->filename_size = 8 + strlen(terminators);
239 heap->filename_illegal_size = strlen(terminators);
240 strcpy( heap->filename_illegal_table, terminators );
242 heap->filename_reserved1 = 0x01;
243 heap->filename_lowest = 0; /* FIXME: correct value? */
244 heap->filename_highest = 0xff; /* FIXME: correct value? */
245 heap->filename_reserved2 = 0x00;
246 heap->filename_exclude_first = 0x00; /* FIXME: correct value? */
247 heap->filename_exclude_last = 0x00; /* FIXME: correct value? */
248 heap->filename_reserved3 = 0x02;
251 * DBCS lead byte table. This table is empty.
254 memset( heap->dbcs_table, 0, sizeof(heap->dbcs_table) );
257 * Initialize InDos flag.
259 heap->misc_indos = 0;
263 /***********************************************************************
264 * INT21_GetHeapSelector
266 * Get segment/selector for DOS heap (INT21_HEAP).
267 * Creates and initializes heap on first call.
269 static WORD INT21_GetHeapSelector( CONTEXT86 *context )
271 static WORD heap_segment = 0;
272 static WORD heap_selector = 0;
273 static BOOL heap_initialized = FALSE;
275 if (!heap_initialized)
277 INT21_HEAP *ptr = DOSVM_AllocDataUMB( sizeof(INT21_HEAP),
280 INT21_FillHeap( ptr );
281 heap_initialized = TRUE;
284 if (!ISV86(context) && DOSVM_IsWin16())
285 return heap_selector;
291 /***********************************************************************
292 * INT21_BufferedInput
294 * Handler for function 0x0a.
296 * Reads a string of characters from standard input until
297 * enter key is pressed.
299 static void INT21_BufferedInput( CONTEXT86 *context )
301 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context,
304 BYTE capacity = ptr[0]; /* includes CR */
305 BYTE length = 0; /* excludes CR */
307 TRACE( "BUFFERED INPUT (size=%d)\n", capacity );
310 * Return immediately if capacity is zero.
312 * FIXME: What to return to application?
318 * FIXME: Some documents state that
319 * ptr[1] holds number of chars from last input which
320 * may be recalled on entry, other documents do not mention
324 TRACE( "Handle old chars in buffer!\n" );
331 DOSVM_Int16ReadChar( &ascii, &scan, FALSE );
333 if (ascii == '\r' || ascii == '\n')
336 * FIXME: What should be echoed here?
338 DOSVM_PutChar( '\r' );
339 DOSVM_PutChar( '\n' );
341 ptr[2 + length] = '\r';
346 * FIXME: This function is supposed to support
347 * DOS editing keys...
351 * If the buffer becomes filled to within one byte of
352 * capacity, DOS rejects all further characters up to,
353 * but not including, the terminating carriage return.
355 if (ascii != 0 && length < capacity-1)
357 DOSVM_PutChar( ascii );
358 ptr[2 + length] = ascii;
365 /***********************************************************************
366 * INT21_CreateDirectory
370 * - subfunction 0x39 of function 0x71
371 * - subfunction 0xff of function 0x43 (CL == 0x39)
373 static BOOL INT21_CreateDirectory( CONTEXT86 *context )
375 WCHAR dirW[MAX_PATH];
376 char *dirA = CTX_SEG_OFF_TO_LIN(context,
380 TRACE( "CREATE DIRECTORY %s\n", dirA );
382 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
384 if (CreateDirectoryW(dirW, NULL))
388 * FIXME: CreateDirectory's LastErrors will clash with the ones
389 * used by DOS. AH=39 only returns 3 (path not found) and
390 * 5 (access denied), while CreateDirectory return several
391 * ones. Remap some of them. -Marcus
393 switch (GetLastError())
395 case ERROR_ALREADY_EXISTS:
396 case ERROR_FILENAME_EXCED_RANGE:
397 case ERROR_DISK_FULL:
398 SetLastError(ERROR_ACCESS_DENIED);
408 /***********************************************************************
409 * INT21_ExtendedCountryInformation
411 * Handler for function 0x65.
413 static void INT21_ExtendedCountryInformation( CONTEXT86 *context )
415 BYTE *dataptr = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
417 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
421 * Check subfunctions that are passed country and code page.
423 if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
425 WORD country = DX_reg(context);
426 WORD codepage = BX_reg(context);
428 if (country != 0xffff && country != INT21_GetSystemCountryCode())
429 FIXME( "Requested info on non-default country %04x\n", country );
431 if (codepage != 0xffff && codepage != GetOEMCP())
432 FIXME( "Requested info on non-default code page %04x\n", codepage );
435 switch (AL_reg(context)) {
436 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
437 INT_BARF( context, 0x21 );
438 SET_CFLAG( context );
441 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
442 TRACE( "Get general internationalization info\n" );
443 dataptr[0] = 0x01; /* Info ID */
444 *(WORD*)(dataptr+1) = 38; /* Size of the following info */
445 *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
446 *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
447 INT21_FillCountryInformation( dataptr + 7 );
448 SET_CX( context, 41 ); /* Size of returned info */
451 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
452 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
453 TRACE( "Get pointer to uppercase table\n" );
454 dataptr[0] = AL_reg(context); /* Info ID */
455 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
456 offsetof(INT21_HEAP, uppercase_size) );
457 SET_CX( context, 5 ); /* Size of returned info */
460 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
461 TRACE( "Get pointer to lowercase table\n" );
462 dataptr[0] = 0x03; /* Info ID */
463 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
464 offsetof(INT21_HEAP, lowercase_size) );
465 SET_CX( context, 5 ); /* Size of returned info */
468 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
469 TRACE("Get pointer to filename terminator table\n");
470 dataptr[0] = 0x05; /* Info ID */
471 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
472 offsetof(INT21_HEAP, filename_size) );
473 SET_CX( context, 5 ); /* Size of returned info */
476 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
477 TRACE("Get pointer to collating sequence table\n");
478 dataptr[0] = 0x06; /* Info ID */
479 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
480 offsetof(INT21_HEAP, collating_size) );
481 SET_CX( context, 5 ); /* Size of returned info */
484 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
485 TRACE("Get pointer to DBCS lead byte table\n");
486 dataptr[0] = 0x07; /* Info ID */
487 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
488 offsetof(INT21_HEAP, dbcs_size) );
489 SET_CX( context, 5 ); /* Size of returned info */
492 case 0x20: /* CAPITALIZE CHARACTER */
493 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
494 TRACE("Convert char to uppercase\n");
495 SET_DL( context, toupper(DL_reg(context)) );
498 case 0x21: /* CAPITALIZE STRING */
499 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
500 TRACE("Convert string to uppercase with length\n");
502 char *ptr = (char *)CTX_SEG_OFF_TO_LIN( context,
505 WORD len = CX_reg(context);
506 while (len--) { *ptr = toupper(*ptr); ptr++; }
510 case 0x22: /* CAPITALIZE ASCIIZ STRING */
511 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
512 TRACE("Convert ASCIIZ string to uppercase\n");
513 _strupr( (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx) );
516 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
517 INT_BARF( context, 0x21 );
518 SET_CFLAG( context );
522 INT_BARF( context, 0x21 );
529 /***********************************************************************
530 * INT21_FileAttributes
534 * - subfunction 0x43 of function 0x71
536 static BOOL INT21_FileAttributes( CONTEXT86 *context,
540 WCHAR fileW[MAX_PATH];
541 char *fileA = CTX_SEG_OFF_TO_LIN(context,
552 case 0x00: /* GET FILE ATTRIBUTES */
553 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA );
554 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
556 result = GetFileAttributesW( fileW );
561 SET_CX( context, (WORD)result );
563 SET_AX( context, (WORD)result ); /* DR DOS */
567 case 0x01: /* SET FILE ATTRIBUTES */
568 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
569 CX_reg(context), fileA );
570 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
572 if (!SetFileAttributesW( fileW, CX_reg(context) ))
576 case 0x02: /* GET COMPRESSED FILE SIZE */
577 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA );
578 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
580 result = GetCompressedFileSizeW( fileW, NULL );
581 if (result == INVALID_FILE_SIZE)
585 SET_AX( context, LOWORD(result) );
586 SET_DX( context, HIWORD(result) );
590 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
592 INT_BARF( context, 0x21 );
595 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
596 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
598 handle = CreateFileW( fileW, GENERIC_WRITE,
599 FILE_SHARE_READ | FILE_SHARE_WRITE,
600 NULL, OPEN_EXISTING, 0, 0 );
601 if (handle == INVALID_HANDLE_VALUE)
604 DosDateTimeToFileTime( DI_reg(context),
607 status = SetFileTime( handle, NULL, NULL, &filetime );
609 CloseHandle( handle );
614 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
616 INT_BARF( context, 0x21 );
619 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
620 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
622 handle = CreateFileW( fileW, GENERIC_READ,
623 FILE_SHARE_READ | FILE_SHARE_WRITE,
624 NULL, OPEN_EXISTING, 0, 0 );
625 if (handle == INVALID_HANDLE_VALUE)
628 status = GetFileTime( handle, NULL, NULL, &filetime );
631 FileTimeToDosDateTime( &filetime, &date, &time );
632 SET_DI( context, date );
633 SET_CX( context, time );
636 CloseHandle( handle );
641 case 0x05: /* SET FILE LAST ACCESS DATE */
643 INT_BARF( context, 0x21 );
646 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA );
647 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
649 handle = CreateFileW( fileW, GENERIC_WRITE,
650 FILE_SHARE_READ | FILE_SHARE_WRITE,
651 NULL, OPEN_EXISTING, 0, 0 );
652 if (handle == INVALID_HANDLE_VALUE)
655 DosDateTimeToFileTime( DI_reg(context),
658 status = SetFileTime( handle, NULL, &filetime, NULL );
660 CloseHandle( handle );
665 case 0x06: /* GET FILE LAST ACCESS DATE */
667 INT_BARF( context, 0x21 );
670 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA );
671 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
673 handle = CreateFileW( fileW, GENERIC_READ,
674 FILE_SHARE_READ | FILE_SHARE_WRITE,
675 NULL, OPEN_EXISTING, 0, 0 );
676 if (handle == INVALID_HANDLE_VALUE)
679 status = GetFileTime( handle, NULL, &filetime, NULL );
682 FileTimeToDosDateTime( &filetime, &date, NULL );
683 SET_DI( context, date );
686 CloseHandle( handle );
691 case 0x07: /* SET FILE CREATION DATE AND TIME */
693 INT_BARF( context, 0x21 );
696 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA );
698 handle = CreateFileW( fileW, GENERIC_WRITE,
699 FILE_SHARE_READ | FILE_SHARE_WRITE,
700 NULL, OPEN_EXISTING, 0, 0 );
701 if (handle == INVALID_HANDLE_VALUE)
705 * FIXME: SI has number of 10-millisecond units past time in CX.
707 DosDateTimeToFileTime( DI_reg(context),
710 status = SetFileTime( handle, &filetime, NULL, NULL );
712 CloseHandle( handle );
717 case 0x08: /* GET FILE CREATION DATE AND TIME */
719 INT_BARF( context, 0x21 );
722 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
725 handle = CreateFileW( fileW, GENERIC_READ,
726 FILE_SHARE_READ | FILE_SHARE_WRITE,
727 NULL, OPEN_EXISTING, 0, 0 );
728 if (handle == INVALID_HANDLE_VALUE)
731 status = GetFileTime( handle, &filetime, NULL, NULL );
734 FileTimeToDosDateTime( &filetime, &date, &time );
735 SET_DI( context, date );
736 SET_CX( context, time );
738 * FIXME: SI has number of 10-millisecond units past
741 SET_SI( context, 0 );
749 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
750 if (islong || context->Ebp != 0x5053)
751 INT_BARF( context, 0x21 );
754 switch(CL_reg(context))
757 if (!INT21_CreateDirectory( context ))
762 if (!INT21_RenameFile( context ))
767 INT_BARF( context, 0x21 );
773 INT_BARF( context, 0x21 );
780 /***********************************************************************
783 * Handler for function 0x57.
785 static BOOL INT21_FileDateTime( CONTEXT86 *context )
787 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
791 switch (AL_reg(context)) {
792 case 0x00: /* Get last-written stamp */
793 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
796 if (!GetFileTime( handle, NULL, NULL, &filetime ))
798 FileTimeToDosDateTime( &filetime, &date, &time );
799 SET_DX( context, date );
800 SET_CX( context, time );
804 case 0x01: /* Set last-written stamp */
805 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
808 DosDateTimeToFileTime( DX_reg(context),
811 if (!SetFileTime( handle, NULL, NULL, &filetime ))
816 case 0x04: /* Get last access stamp, DOS 7 */
817 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
820 if (!GetFileTime( handle, NULL, &filetime, NULL ))
822 FileTimeToDosDateTime( &filetime, &date, &time );
823 SET_DX( context, date );
824 SET_CX( context, time );
828 case 0x05: /* Set last access stamp, DOS 7 */
829 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
832 DosDateTimeToFileTime( DX_reg(context),
835 if (!SetFileTime( handle, NULL, &filetime, NULL ))
840 case 0x06: /* Get creation stamp, DOS 7 */
841 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
844 if (!GetFileTime( handle, &filetime, NULL, NULL ))
846 FileTimeToDosDateTime( &filetime, &date, &time );
847 SET_DX( context, date );
848 SET_CX( context, time );
850 * FIXME: SI has number of 10-millisecond units past time in CX.
852 SET_SI( context, 0 );
856 case 0x07: /* Set creation stamp, DOS 7 */
857 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
861 * FIXME: SI has number of 10-millisecond units past time in CX.
863 DosDateTimeToFileTime( DX_reg(context),
866 if (!SetFileTime( handle, &filetime, NULL, NULL ))
872 INT_BARF( context, 0x21 );
880 /***********************************************************************
883 * Handler for functions 0x51 and 0x62.
885 static void INT21_GetPSP( CONTEXT86 *context )
887 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
890 * FIXME: should we return the original DOS PSP upon
893 if (!ISV86(context) && DOSVM_IsWin16())
894 SET_BX( context, LOWORD(GetCurrentPDB16()) );
896 SET_BX( context, DOSVM_psp );
900 /***********************************************************************
903 * Handler for block device IOCTLs.
905 static void INT21_Ioctl_Block( CONTEXT86 *context )
907 INT_Int21Handler( context );
911 /***********************************************************************
914 * Handler for character device IOCTLs.
916 static void INT21_Ioctl_Char( CONTEXT86 *context )
918 static const WCHAR emmxxxx0W[] = {'E','M','M','X','X','X','X','0',0};
919 static const WCHAR scsimgrW[] = {'S','C','S','I','M','G','R','$',0};
921 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
922 const DOS_DEVICE *dev = DOSFS_GetDeviceByHandle(handle);
924 if (dev && !strcmpiW( dev->name, emmxxxx0W ))
926 EMS_Ioctl_Handler(context);
930 if (dev && !strcmpiW( dev->name, scsimgrW ) && AL_reg(context) == 2)
932 DOSVM_ASPIHandler(context);
936 INT_Int21Handler( context );
940 /***********************************************************************
943 * Handler for function 0x44.
945 static void INT21_Ioctl( CONTEXT86 *context )
947 switch (AL_reg(context))
953 INT21_Ioctl_Char( context );
958 INT21_Ioctl_Block( context );
963 INT21_Ioctl_Char( context );
968 INT21_Ioctl_Block( context );
972 INT21_Ioctl_Char( context );
975 case 0x0b: /* SET SHARING RETRY COUNT */
976 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
977 CX_reg(context), DX_reg(context) );
978 if (!CX_reg(context))
980 SET_AX( context, 1 );
981 SET_CFLAG( context );
985 DOSMEM_LOL()->sharing_retry_delay = CX_reg(context);
987 DOSMEM_LOL()->sharing_retry_count = DX_reg(context);
988 RESET_CFLAG( context );
993 INT21_Ioctl_Char( context );
999 INT21_Ioctl_Block( context );
1003 INT21_Ioctl_Char( context );
1007 INT21_Ioctl_Block( context );
1010 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
1011 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
1012 SET_CFLAG(context); /* Error / This is not DR DOS. */
1013 SET_AX( context, 0x0001 ); /* Invalid function */
1016 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
1017 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
1018 SET_CFLAG(context); /* Error / This is not DR DOS. */
1019 SET_AX( context, 0x0001 ); /* Invalid function */
1022 case 0xe0: /* Sun PC-NFS API */
1023 TRACE( "Sun PC-NFS API\n" );
1028 INT_BARF( context, 0x21 );
1033 /***********************************************************************
1034 * INT21_LongFilename
1036 * Handler for function 0x71.
1038 static void INT21_LongFilename( CONTEXT86 *context )
1040 BOOL bSetDOSExtendedError = FALSE;
1042 if (HIBYTE(HIWORD(GetVersion16())) < 0x07)
1044 TRACE( "LONG FILENAME - functions supported only under DOS7\n" );
1045 SET_CFLAG( context );
1046 SET_AL( context, 0 );
1050 switch (AL_reg(context))
1052 case 0x0d: /* RESET DRIVE */
1053 INT_Int21Handler( context );
1056 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
1057 if (!INT21_CreateDirectory( context ))
1058 bSetDOSExtendedError = TRUE;
1061 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
1063 WCHAR dirW[MAX_PATH];
1064 char *dirA = CTX_SEG_OFF_TO_LIN(context,
1065 context->SegDs, context->Edx);
1067 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", dirA );
1068 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
1070 if (!RemoveDirectoryW( dirW ))
1071 bSetDOSExtendedError = TRUE;
1075 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
1076 INT_Int21Handler( context );
1079 case 0x41: /* LONG FILENAME - DELETE FILE */
1081 WCHAR fileW[MAX_PATH];
1082 char *fileA = CTX_SEG_OFF_TO_LIN(context,
1083 context->SegDs, context->Edx);
1085 TRACE( "LONG FILENAME - DELETE FILE %s\n", fileA );
1086 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1088 if (!DeleteFileW( fileW ))
1089 bSetDOSExtendedError = TRUE;
1093 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
1094 if (!INT21_FileAttributes( context, BL_reg(context), TRUE ))
1095 bSetDOSExtendedError = TRUE;
1098 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
1099 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
1100 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
1101 INT_Int21Handler( context );
1104 case 0x56: /* LONG FILENAME - RENAME FILE */
1105 if (!INT21_RenameFile(context))
1106 bSetDOSExtendedError = TRUE;
1109 case 0x60: /* LONG FILENAME - CONVERT PATH */
1110 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
1111 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
1112 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
1113 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
1114 case 0xa7: /* LONG FILENAME - CONVERT TIME */
1115 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
1116 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
1117 case 0xaa: /* LONG FILENAME - SUBST */
1118 INT_Int21Handler( context );
1122 INT_BARF( context, 0x21 );
1125 if (bSetDOSExtendedError)
1127 SET_AX( context, GetLastError() );
1128 SET_CFLAG( context );
1133 /***********************************************************************
1138 * - subfunction 0x56 of function 0x71
1139 * - subfunction 0xff of function 0x43 (CL == 0x56)
1141 static BOOL INT21_RenameFile( CONTEXT86 *context )
1143 WCHAR fromW[MAX_PATH];
1144 WCHAR toW[MAX_PATH];
1145 char *fromA = CTX_SEG_OFF_TO_LIN(context,
1146 context->SegDs,context->Edx);
1147 char *toA = CTX_SEG_OFF_TO_LIN(context,
1148 context->SegEs,context->Edi);
1150 TRACE( "RENAME FILE %s to %s\n", fromA, toA );
1151 MultiByteToWideChar(CP_OEMCP, 0, fromA, -1, fromW, MAX_PATH);
1152 MultiByteToWideChar(CP_OEMCP, 0, toA, -1, toW, MAX_PATH);
1154 return MoveFileW( fromW, toW );
1158 /***********************************************************************
1159 * INT21_GetExtendedError
1161 static void INT21_GetExtendedError( CONTEXT86 *context )
1163 BYTE class, action, locus;
1164 WORD error = GetLastError();
1169 class = action = locus = 0;
1171 case ERROR_DIR_NOT_EMPTY:
1176 case ERROR_ACCESS_DENIED:
1177 class = EC_AccessDenied;
1181 case ERROR_CANNOT_MAKE:
1182 class = EC_AccessDenied;
1186 case ERROR_DISK_FULL:
1187 case ERROR_HANDLE_DISK_FULL:
1188 class = EC_MediaError;
1192 case ERROR_FILE_EXISTS:
1193 case ERROR_ALREADY_EXISTS:
1198 case ERROR_FILE_NOT_FOUND:
1199 class = EC_NotFound;
1203 case ER_GeneralFailure:
1204 class = EC_SystemFailure;
1208 case ERROR_INVALID_DRIVE:
1209 class = EC_MediaError;
1213 case ERROR_INVALID_HANDLE:
1214 class = EC_ProgramError;
1218 case ERROR_LOCK_VIOLATION:
1219 class = EC_AccessDenied;
1223 case ERROR_NO_MORE_FILES:
1224 class = EC_MediaError;
1229 class = EC_NotFound;
1233 case ERROR_NOT_ENOUGH_MEMORY:
1234 class = EC_OutOfResource;
1238 case ERROR_PATH_NOT_FOUND:
1239 class = EC_NotFound;
1244 class = EC_NotFound;
1248 case ERROR_SHARING_VIOLATION:
1249 class = EC_Temporary;
1253 case ERROR_TOO_MANY_OPEN_FILES:
1254 class = EC_ProgramError;
1259 FIXME("Unknown error %d\n", error );
1260 class = EC_SystemFailure;
1265 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
1266 error, class, action, locus );
1267 SET_AX( context, error );
1268 SET_BH( context, class );
1269 SET_BL( context, action );
1270 SET_CH( context, locus );
1274 /***********************************************************************
1275 * DOSVM_Int21Handler
1277 * Interrupt 0x21 handler.
1279 void WINAPI DOSVM_Int21Handler( CONTEXT86 *context )
1281 BOOL bSetDOSExtendedError = FALSE;
1283 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
1284 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1285 AX_reg(context), BX_reg(context),
1286 CX_reg(context), DX_reg(context),
1287 SI_reg(context), DI_reg(context),
1288 (WORD)context->SegDs, (WORD)context->SegEs,
1292 * Extended error is used by (at least) functions 0x2f to 0x62.
1293 * Function 0x59 returns extended error and error should not
1294 * be cleared before handling the function.
1296 if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59)
1299 RESET_CFLAG(context); /* Not sure if this is a good idea. */
1301 switch(AH_reg(context))
1303 case 0x00: /* TERMINATE PROGRAM */
1304 TRACE("TERMINATE PROGRAM\n");
1305 if (DOSVM_IsWin16())
1308 MZ_Exit( context, FALSE, 0 );
1311 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
1314 TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
1315 INT21_ReadChar( &ascii, FALSE );
1316 SET_AL( context, ascii );
1318 * FIXME: What to echo when extended keycodes are read?
1320 DOSVM_PutChar(AL_reg(context));
1324 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
1325 TRACE("Write Character to Standard Output\n");
1326 DOSVM_PutChar(DL_reg(context));
1329 case 0x03: /* READ CHARACTER FROM STDAUX */
1330 case 0x04: /* WRITE CHARACTER TO STDAUX */
1331 case 0x05: /* WRITE CHARACTER TO PRINTER */
1332 INT_BARF( context, 0x21 );
1335 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
1336 if (DL_reg(context) == 0xff)
1338 TRACE("Direct Console Input\n");
1340 if (INT21_ReadChar( NULL, TRUE ))
1343 INT21_ReadChar( &ascii, FALSE );
1344 SET_AL( context, ascii );
1345 RESET_ZFLAG( context );
1349 /* no character available */
1350 SET_AL( context, 0 );
1351 SET_ZFLAG( context );
1356 TRACE("Direct Console Output\n");
1357 DOSVM_PutChar(DL_reg(context));
1359 * At least DOS versions 2.1-7.0 return character
1360 * that was written in AL register.
1362 SET_AL( context, DL_reg(context) );
1366 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
1369 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
1370 INT21_ReadChar( &ascii, FALSE );
1371 SET_AL( context, ascii );
1375 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
1378 TRACE("CHARACTER INPUT WITHOUT ECHO\n");
1379 INT21_ReadChar( &ascii, FALSE );
1380 SET_AL( context, ascii );
1384 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
1385 TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
1386 context->SegDs, DX_reg(context) );
1388 LPSTR data = CTX_SEG_OFF_TO_LIN( context,
1389 context->SegDs, context->Edx );
1393 * Do NOT use strchr() to calculate the string length,
1394 * as '\0' is valid string content, too!
1395 * Maybe we should check for non-'$' strings, but DOS doesn't.
1397 while (*p != '$') p++;
1399 if (DOSVM_IsWin16())
1400 WriteFile( DosFileHandleToWin32Handle(1),
1401 data, p - data, 0, 0 );
1403 for(; data != p; data++)
1404 DOSVM_PutChar( *data );
1406 SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
1410 case 0x0a: /* BUFFERED INPUT */
1411 INT21_BufferedInput( context );
1414 case 0x0b: /* GET STDIN STATUS */
1415 TRACE( "GET STDIN STATUS\n" );
1417 if (INT21_ReadChar( NULL, TRUE ))
1418 SET_AL( context, 0xff ); /* character available */
1420 SET_AL( context, 0 ); /* no character available */
1424 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
1426 BYTE al = AL_reg(context); /* Input function to execute after flush. */
1428 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
1430 /* FIXME: buffers are not flushed */
1433 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
1434 * int21 function identified by AL will be called.
1436 if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
1438 SET_AH( context, al );
1439 DOSVM_Int21Handler( context );
1444 case 0x0d: /* DISK BUFFER FLUSH */
1445 TRACE("DISK BUFFER FLUSH ignored\n");
1448 case 0x0e: /* SELECT DEFAULT DRIVE */
1449 INT_Int21Handler( context );
1452 case 0x0f: /* OPEN FILE USING FCB */
1453 case 0x10: /* CLOSE FILE USING FCB */
1454 INT_BARF( context, 0x21 );
1457 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1458 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1459 INT_Int21Handler( context );
1462 case 0x13: /* DELETE FILE USING FCB */
1463 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
1464 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
1465 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
1466 case 0x17: /* RENAME FILE USING FCB */
1467 INT_BARF( context, 0x21 );
1470 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
1471 SET_AL( context, 0 );
1474 case 0x19: /* GET CURRENT DEFAULT DRIVE */
1475 INT_Int21Handler( context );
1478 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1479 TRACE( "SET DISK TRANSFER AREA ADDRESS %04lX:%04X\n",
1480 context->SegDs, DX_reg(context) );
1482 TDB *task = GlobalLock16( GetCurrentTask() );
1483 task->dta = MAKESEGPTR( context->SegDs, DX_reg(context) );
1487 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1488 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1489 INT_Int21Handler( context );
1492 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
1493 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
1494 SET_AL( context, 0 );
1497 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1498 INT_Int21Handler( context );
1501 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
1502 SET_AL( context, 0 );
1505 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
1506 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
1507 case 0x23: /* GET FILE SIZE FOR FCB */
1508 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
1509 INT_BARF( context, 0x21 );
1512 case 0x25: /* SET INTERRUPT VECTOR */
1513 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
1515 FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
1516 if (!ISV86(context) && DOSVM_IsWin16())
1517 DOSVM_SetPMHandler16( AL_reg(context), ptr );
1519 DOSVM_SetRMHandler( AL_reg(context), ptr );
1523 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
1524 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
1525 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
1526 INT_BARF( context, 0x21 );
1529 case 0x29: /* PARSE FILENAME INTO FCB */
1530 INT_Int21Handler( context );
1533 case 0x2a: /* GET SYSTEM DATE */
1534 TRACE( "GET SYSTEM DATE\n" );
1537 GetLocalTime( &systime );
1538 SET_CX( context, systime.wYear );
1539 SET_DH( context, systime.wMonth );
1540 SET_DL( context, systime.wDay );
1541 SET_AL( context, systime.wDayOfWeek );
1545 case 0x2b: /* SET SYSTEM DATE */
1546 FIXME("SetSystemDate(%02d/%02d/%04d): not allowed\n",
1547 DL_reg(context), DH_reg(context), CX_reg(context) );
1548 SET_AL( context, 0 ); /* Let's pretend we succeeded */
1551 case 0x2c: /* GET SYSTEM TIME */
1552 TRACE( "GET SYSTEM TIME\n" );
1555 GetLocalTime( &systime );
1556 SET_CH( context, systime.wHour );
1557 SET_CL( context, systime.wMinute );
1558 SET_DH( context, systime.wSecond );
1559 SET_DL( context, systime.wMilliseconds / 10 );
1563 case 0x2d: /* SET SYSTEM TIME */
1564 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1565 CH_reg(context), CL_reg(context),
1566 DH_reg(context), DL_reg(context) );
1567 SET_AL( context, 0 ); /* Let's pretend we succeeded */
1570 case 0x2e: /* SET VERIFY FLAG */
1571 TRACE("SET VERIFY FLAG ignored\n");
1572 /* we cannot change the behaviour anyway, so just ignore it */
1575 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1576 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
1578 TDB *task = GlobalLock16( GetCurrentTask() );
1579 context->SegEs = SELECTOROF( task->dta );
1580 SET_BX( context, OFFSETOF( task->dta ) );
1584 case 0x30: /* GET DOS VERSION */
1585 TRACE( "GET DOS VERSION - %s requested\n",
1586 (AL_reg(context) == 0x00) ? "OEM number" : "version flag" );
1588 if (AL_reg(context) == 0x00)
1589 SET_BH( context, 0xff ); /* OEM number => undefined */
1591 SET_BH( context, 0x08 ); /* version flag => DOS is in ROM */
1593 SET_AL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
1594 SET_AH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
1596 SET_BL( context, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
1597 SET_CX( context, 0x3456 );
1600 case 0x31: /* TERMINATE AND STAY RESIDENT */
1601 FIXME("TERMINATE AND STAY RESIDENT stub\n");
1604 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1605 case 0x33: /* MULTIPLEXED */
1606 INT_Int21Handler( context );
1609 case 0x34: /* GET ADDRESS OF INDOS FLAG */
1610 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
1611 context->SegEs = INT21_GetHeapSelector( context );
1612 SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
1615 case 0x35: /* GET INTERRUPT VECTOR */
1616 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
1619 if (!ISV86(context) && DOSVM_IsWin16())
1620 addr = DOSVM_GetPMHandler16( AL_reg(context) );
1622 addr = DOSVM_GetRMHandler( AL_reg(context) );
1623 context->SegEs = SELECTOROF(addr);
1624 SET_BX( context, OFFSETOF(addr) );
1628 case 0x36: /* GET FREE DISK SPACE */
1629 INT_Int21Handler( context );
1632 case 0x37: /* SWITCHAR */
1634 switch (AL_reg(context))
1636 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
1637 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
1638 SET_AL( context, 0x00 ); /* success*/
1639 SET_DL( context, '/' );
1641 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
1642 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
1644 SET_AL( context, 0x00 ); /* success*/
1647 INT_BARF( context, 0x21 );
1653 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1654 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
1655 if (AL_reg(context))
1657 WORD country = AL_reg(context);
1658 if (country == 0xff)
1659 country = BX_reg(context);
1660 if (country != INT21_GetSystemCountryCode())
1661 FIXME( "Requested info on non-default country %04x\n", country );
1663 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context,
1666 SET_AX( context, INT21_GetSystemCountryCode() );
1667 SET_BX( context, INT21_GetSystemCountryCode() );
1670 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1671 if (!INT21_CreateDirectory( context ))
1672 bSetDOSExtendedError = TRUE;
1675 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
1677 WCHAR dirW[MAX_PATH];
1678 char *dirA = CTX_SEG_OFF_TO_LIN(context,
1679 context->SegDs, context->Edx);
1681 TRACE( "REMOVE DIRECTORY %s\n", dirA );
1683 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
1685 if (!RemoveDirectoryW( dirW ))
1686 bSetDOSExtendedError = TRUE;
1690 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1691 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1692 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1693 INT_Int21Handler( context );
1696 case 0x3e: /* "CLOSE" - CLOSE FILE */
1697 TRACE( "CLOSE handle %d\n", BX_reg(context) );
1698 if (_lclose16( BX_reg(context) ) == HFILE_ERROR16)
1699 bSetDOSExtendedError = TRUE;
1702 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1703 TRACE( "READ from %d to %04lX:%04X for %d bytes\n",
1710 WORD count = CX_reg(context);
1711 BYTE *buffer = CTX_SEG_OFF_TO_LIN( context,
1715 /* Some programs pass a count larger than the allocated buffer */
1716 if (DOSVM_IsWin16())
1718 WORD maxcount = GetSelectorLimit16( context->SegDs )
1719 - DX_reg(context) + 1;
1720 if (count > maxcount)
1725 * FIXME: Reading from console (BX=1) in DOS mode
1726 * does not work as it is supposed to work.
1729 if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context)),
1730 buffer, count, &result, NULL ))
1731 SET_AX( context, (WORD)result );
1733 bSetDOSExtendedError = TRUE;
1737 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1738 TRACE( "WRITE from %04lX:%04X to handle %d for %d byte\n",
1739 context->SegDs, DX_reg(context),
1740 BX_reg(context), CX_reg(context) );
1742 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1744 if (!DOSVM_IsWin16() && BX_reg(context) == 1)
1747 for(i=0; i<CX_reg(context); i++)
1748 DOSVM_PutChar(ptr[i]);
1749 SET_AX(context, CX_reg(context));
1753 HFILE handle = (HFILE)DosFileHandleToWin32Handle(BX_reg(context));
1754 LONG result = _hwrite( handle, ptr, CX_reg(context) );
1755 if (result == HFILE_ERROR)
1756 bSetDOSExtendedError = TRUE;
1758 SET_AX( context, (WORD)result );
1763 case 0x41: /* "UNLINK" - DELETE FILE */
1765 WCHAR fileW[MAX_PATH];
1766 char *fileA = CTX_SEG_OFF_TO_LIN(context,
1770 TRACE( "UNLINK %s\n", fileA );
1771 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1773 if (!DeleteFileW( fileW ))
1774 bSetDOSExtendedError = TRUE;
1778 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1779 TRACE( "LSEEK handle %d offset %ld from %s\n",
1781 MAKELONG( DX_reg(context), CX_reg(context) ),
1782 (AL_reg(context) == 0) ?
1783 "start of file" : ((AL_reg(context) == 1) ?
1784 "current file position" : "end of file") );
1786 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
1787 LONG offset = MAKELONG( DX_reg(context), CX_reg(context) );
1788 DWORD status = SetFilePointer( handle, offset,
1789 NULL, AL_reg(context) );
1790 if (status == INVALID_SET_FILE_POINTER)
1791 bSetDOSExtendedError = TRUE;
1794 SET_AX( context, LOWORD(status) );
1795 SET_DX( context, HIWORD(status) );
1800 case 0x43: /* FILE ATTRIBUTES */
1801 if (!INT21_FileAttributes( context, AL_reg(context), FALSE ))
1802 bSetDOSExtendedError = TRUE;
1805 case 0x44: /* IOCTL */
1806 INT21_Ioctl( context );
1809 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1810 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context) );
1813 HFILE handle16 = HFILE_ERROR;
1815 if (DuplicateHandle( GetCurrentProcess(),
1816 DosFileHandleToWin32Handle(BX_reg(context)),
1817 GetCurrentProcess(),
1819 0, TRUE, DUPLICATE_SAME_ACCESS ))
1820 handle16 = Win32HandleToDosFileHandle(handle32);
1822 if (handle16 == HFILE_ERROR)
1823 bSetDOSExtendedError = TRUE;
1825 SET_AX( context, handle16 );
1829 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1830 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
1831 BX_reg(context), CX_reg(context) );
1832 if (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR16)
1833 bSetDOSExtendedError = TRUE;
1836 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1837 INT_Int21Handler( context );
1840 case 0x48: /* ALLOCATE MEMORY */
1841 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
1844 DWORD bytes = (DWORD)BX_reg(context) << 4;
1846 if (!ISV86(context) && DOSVM_IsWin16())
1848 DWORD rv = GlobalDOSAlloc16( bytes );
1849 selector = LOWORD( rv );
1852 DOSMEM_GetBlock( bytes, &selector );
1855 SET_AX( context, selector );
1859 SET_AX( context, 0x0008 ); /* insufficient memory */
1860 SET_BX( context, DOSMEM_Available() >> 4 );
1865 case 0x49: /* FREE MEMORY */
1866 TRACE( "FREE MEMORY segment %04lX\n", context->SegEs );
1870 if (!ISV86(context) && DOSVM_IsWin16())
1872 ok = !GlobalDOSFree16( context->SegEs );
1874 /* If we don't reset ES_reg, we will fail in the relay code */
1879 ok = DOSMEM_FreeBlock( (void*)((DWORD)context->SegEs << 4) );
1883 TRACE("FREE MEMORY failed\n");
1885 SET_AX( context, 0x0009 ); /* memory block address invalid */
1890 case 0x4a: /* RESIZE MEMORY BLOCK */
1891 TRACE( "RESIZE MEMORY segment %04lX to %d paragraphs\n",
1892 context->SegEs, BX_reg(context) );
1894 DWORD newsize = (DWORD)BX_reg(context) << 4;
1896 if (!ISV86(context) && DOSVM_IsWin16())
1898 FIXME( "Resize memory block - unsupported under Win16\n" );
1902 LPVOID address = (void*)((DWORD)context->SegEs << 4);
1903 UINT blocksize = DOSMEM_ResizeBlock( address, newsize, FALSE );
1905 if (blocksize == (UINT)-1)
1907 SET_CFLAG( context );
1908 SET_AX( context, 0x0009 ); /* illegal address */
1910 else if(blocksize != newsize)
1912 SET_CFLAG( context );
1913 SET_AX( context, 0x0008 ); /* insufficient memory */
1914 SET_BX( context, blocksize >> 4 ); /* new block size */
1920 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1922 BYTE *program = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1923 BYTE *paramblk = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx);
1925 TRACE( "EXEC %s\n", program );
1927 if (DOSVM_IsWin16())
1929 HINSTANCE16 instance = WinExec16( program, SW_NORMAL );
1932 SET_CFLAG( context );
1933 SET_AX( context, instance );
1938 if (!MZ_Exec( context, program, AL_reg(context), paramblk))
1939 bSetDOSExtendedError = TRUE;
1944 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1945 TRACE( "EXIT with return code %d\n", AL_reg(context) );
1946 if (DOSVM_IsWin16())
1947 ExitThread( AL_reg(context) );
1949 MZ_Exit( context, FALSE, AL_reg(context) );
1952 case 0x4d: /* GET RETURN CODE */
1953 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
1954 SET_AX( context, DOSVM_retval );
1958 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1959 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1960 INT_Int21Handler( context );
1963 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
1964 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
1965 DOSVM_psp = BX_reg(context);
1968 case 0x51: /* GET PSP ADDRESS */
1969 INT21_GetPSP( context );
1972 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1973 if (!ISV86(context) && DOSVM_IsWin16())
1975 SEGPTR ptr = DOSMEM_LOL()->wine_pm_lol;
1976 context->SegEs = SELECTOROF(ptr);
1977 SET_BX( context, OFFSETOF(ptr) );
1981 SEGPTR ptr = DOSMEM_LOL()->wine_rm_lol;
1982 context->SegEs = SELECTOROF(ptr);
1983 SET_BX( context, OFFSETOF(ptr) );
1987 case 0x54: /* Get Verify Flag */
1988 TRACE("Get Verify Flag - Not Supported\n");
1989 SET_AL( context, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
1992 case 0x56: /* "RENAME" - RENAME FILE */
1993 if (!INT21_RenameFile( context ))
1994 bSetDOSExtendedError = TRUE;
1997 case 0x57: /* FILE DATE AND TIME */
1998 if (!INT21_FileDateTime( context ))
1999 bSetDOSExtendedError = TRUE;
2002 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
2003 switch (AL_reg(context))
2005 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
2006 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
2007 SET_AX( context, 0 ); /* low memory first fit */
2010 case 0x01: /* SET ALLOCATION STRATEGY */
2011 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
2015 case 0x02: /* GET UMB LINK STATE */
2016 TRACE( "GET UMB LINK STATE\n" );
2017 SET_AL( context, 0 ); /* UMBs not part of DOS memory chain */
2020 case 0x03: /* SET UMB LINK STATE */
2021 TRACE( "SET UMB LINK STATE to %d - ignored\n",
2026 INT_BARF( context, 0x21 );
2030 case 0x59: /* GET EXTENDED ERROR INFO */
2031 INT21_GetExtendedError( context );
2034 case 0x5a: /* CREATE TEMPORARY FILE */
2035 case 0x5b: /* CREATE NEW FILE */
2036 INT_Int21Handler( context );
2039 case 0x5c: /* "FLOCK" - RECORD LOCKING */
2041 DWORD offset = MAKELONG(DX_reg(context), CX_reg(context));
2042 DWORD length = MAKELONG(DI_reg(context), SI_reg(context));
2043 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2045 switch (AL_reg(context))
2047 case 0x00: /* LOCK */
2048 TRACE( "lock handle %d offset %ld length %ld\n",
2049 BX_reg(context), offset, length );
2050 if (!LockFile( handle, offset, 0, length, 0 ))
2051 bSetDOSExtendedError = TRUE;
2054 case 0x01: /* UNLOCK */
2055 TRACE( "unlock handle %d offset %ld length %ld\n",
2056 BX_reg(context), offset, length );
2057 if (!UnlockFile( handle, offset, 0, length, 0 ))
2058 bSetDOSExtendedError = TRUE;
2062 INT_BARF( context, 0x21 );
2067 case 0x5d: /* NETWORK 5D */
2068 FIXME( "Network function 5D not implemented.\n" );
2069 SetLastError( ER_NoNetwork );
2070 bSetDOSExtendedError = TRUE;
2073 case 0x5e: /* NETWORK 5E */
2074 case 0x5f: /* NETWORK 5F */
2075 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
2076 INT_Int21Handler( context );
2079 case 0x61: /* UNUSED */
2080 SET_AL( context, 0 );
2083 case 0x62: /* GET PSP ADDRESS */
2084 INT21_GetPSP( context );
2087 case 0x63: /* MISC. LANGUAGE SUPPORT */
2088 switch (AL_reg(context)) {
2089 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
2090 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
2091 context->SegDs = INT21_GetHeapSelector( context );
2092 SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
2093 SET_AL( context, 0 ); /* success */
2098 case 0x64: /* OS/2 DOS BOX */
2099 INT_BARF( context, 0x21 );
2103 case 0x65: /* EXTENDED COUNTRY INFORMATION */
2104 INT21_ExtendedCountryInformation( context );
2107 case 0x66: /* GLOBAL CODE PAGE TABLE */
2108 switch (AL_reg(context))
2111 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
2112 SET_BX( context, GetOEMCP() );
2113 SET_DX( context, GetOEMCP() );
2116 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
2117 BX_reg(context), DX_reg(context) );
2122 case 0x67: /* SET HANDLE COUNT */
2123 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
2124 if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
2125 bSetDOSExtendedError = TRUE;
2128 case 0x68: /* "FFLUSH" - COMMIT FILE */
2129 TRACE( "FFLUSH - handle %d\n", BX_reg(context) );
2130 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
2131 bSetDOSExtendedError = TRUE;
2134 case 0x69: /* DISK SERIAL NUMBER */
2135 INT_Int21Handler( context );
2138 case 0x6a: /* COMMIT FILE */
2139 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context) );
2140 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
2141 bSetDOSExtendedError = TRUE;
2144 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
2145 SET_AL( context, 0 );
2148 case 0x6c: /* EXTENDED OPEN/CREATE */
2149 INT_Int21Handler( context );
2152 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
2153 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
2154 SET_CFLAG( context );
2155 SET_AL( context, 0 );
2158 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
2159 INT21_LongFilename( context );
2162 case 0x73: /* MSDOS7 - FAT32 */
2163 INT_Int21Handler( context );
2166 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
2167 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
2170 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
2171 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
2175 INT_BARF( context, 0x21 );
2178 } /* END OF SWITCH */
2180 /* Set general error condition. */
2181 if (bSetDOSExtendedError)
2183 SET_AX( context, GetLastError() );
2184 SET_CFLAG( context );
2187 /* Print error code if carry flag is set. */
2188 if (context->EFlags & 0x0001)
2189 TRACE("failed, error %ld\n", GetLastError() );
2191 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
2192 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
2193 AX_reg(context), BX_reg(context),
2194 CX_reg(context), DX_reg(context),
2195 SI_reg(context), DI_reg(context),
2196 (WORD)context->SegDs, (WORD)context->SegEs,