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
9 * Copyright 2003 Thomas Mertes
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/port.h"
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
42 #include "wine/winbase16.h"
43 #include "kernel16_private.h"
47 #include "wine/unicode.h"
48 #include "wine/server.h"
49 #include "wine/debug.h"
50 #include "wine/exception.h"
52 BOOL WINAPI VerifyConsoleIoHandle(HANDLE);
55 * - Most of the file related functions are wrong. NT's kernel32
56 * doesn't maintain a per drive current directory, while DOS does.
57 * We should in fact keep track in here of those per drive
58 * directories, and use this info while dealing with partial paths
59 * (drive defined, but only relative paths). This could even be
60 * created as an array of CDS (there should be an entry for that in
65 * Forward declarations.
67 static BOOL INT21_RenameFile( CONTEXT86 *context );
69 WINE_DEFAULT_DEBUG_CHANNEL(int21);
75 * Extended Drive Parameter Block.
76 * This structure is compatible with standard DOS4+ DPB and
79 typedef struct _INT21_DPB {
80 BYTE drive; /* 00 drive number (0=A, ...) */
81 BYTE unit; /* 01 unit number within device driver */
82 WORD sector_bytes; /* 02 bytes per sector */
83 BYTE cluster_sectors; /* 04 highest sector number within a cluster */
84 BYTE shift; /* 05 shift count to convert clusters into sectors */
85 WORD num_reserved; /* 06 reserved sectors at beginning of drive */
86 BYTE num_FAT; /* 08 number of FATs */
87 WORD num_root_entries; /* 09 number of root directory entries */
88 WORD first_data_sector; /* 0b number of first sector containing user data */
89 WORD num_clusters1; /* 0d highest cluster number (number of data clusters + 1) */
90 WORD sectors_per_FAT; /* 0f number of sectors per FAT */
91 WORD first_dir_sector; /* 11 sector number of first directory sector */
92 SEGPTR driver_header; /* 13 address of device driver header */
93 BYTE media_ID; /* 17 media ID byte */
94 BYTE access_flag; /* 18 0x00 if disk accessed, 0xff if not */
95 SEGPTR next; /* 19 pointer to next DPB */
96 WORD search_cluster1; /* 1d cluster at which to start search for free space */
97 WORD free_clusters_lo; /* 1f number of free clusters on drive or 0xffff if unknown */
98 WORD free_clusters_hi; /* 21 hiword of clusters_free */
99 WORD mirroring_flags; /* 23 active FAT/mirroring flags */
100 WORD info_sector; /* 25 sector number of file system info sector or 0xffff for none */
101 WORD spare_boot_sector; /* 27 sector number of backup boot sector or 0xffff for none */
102 DWORD first_cluster_sector; /* 29 sector number of the first cluster */
103 DWORD num_clusters2; /* 2d maximum cluster number */
104 DWORD fat_clusters; /* 31 number of clusters occupied by FAT */
105 DWORD root_cluster; /* 35 cluster number of start of root directory */
106 DWORD search_cluster2; /* 39 cluster at which to start searching for free space */
111 * Structure for DOS data that can be accessed directly from applications.
112 * Real and protected mode pointers will be returned to this structure so
113 * the structure must be correctly packed.
115 typedef struct _INT21_HEAP {
116 WORD uppercase_size; /* Size of the following table in bytes */
117 BYTE uppercase_table[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
119 WORD lowercase_size; /* Size of the following table in bytes */
120 BYTE lowercase_table[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
122 WORD collating_size; /* Size of the following table in bytes */
123 BYTE collating_table[256]; /* Values used to sort characters from 0x00 to 0xff. */
125 WORD filename_size; /* Size of the following filename data in bytes */
126 BYTE filename_reserved1; /* 0x01 for MS-DOS 3.30-6.00 */
127 BYTE filename_lowest; /* Lowest permissible character value for filename */
128 BYTE filename_highest; /* Highest permissible character value for filename */
129 BYTE filename_reserved2; /* 0x00 for MS-DOS 3.30-6.00 */
130 BYTE filename_exclude_first; /* First illegal character in permissible range */
131 BYTE filename_exclude_last; /* Last illegal character in permissible range */
132 BYTE filename_reserved3; /* 0x02 for MS-DOS 3.30-6.00 */
133 BYTE filename_illegal_size; /* Number of terminators in the following table */
134 BYTE filename_illegal_table[16]; /* Characters which terminate a filename */
136 WORD dbcs_size; /* Number of valid ranges in the following table */
137 BYTE dbcs_table[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
139 BYTE misc_indos; /* Interrupt 21 nesting flag */
140 WORD misc_segment; /* Real mode segment for INT21_HEAP */
141 WORD misc_selector; /* Protected mode selector for INT21_HEAP */
142 INT21_DPB misc_dpb_list[MAX_DOS_DRIVES]; /* Drive parameter blocks for all drives */
150 CHAR file_extension[3];
151 WORD current_block_number;
152 WORD logical_record_size;
154 WORD date_of_last_write;
155 WORD time_of_last_write;
158 WORD starting_cluster;
159 WORD sequence_number;
160 BYTE file_attributes;
162 BYTE record_within_current_block;
163 BYTE random_access_record_number[4];
170 BYTE xfcb_file_attribute;
174 /* DTA layout for FindFirst/FindNext */
177 BYTE drive; /* 00 drive letter */
178 char mask[11]; /* 01 search template */
179 BYTE search_attr; /* 0c search attributes */
180 WORD count; /* 0d entry count within directory */
181 WORD cluster; /* 0f cluster of parent directory */
182 WCHAR *fullPath; /* 11 full path (was: reserved) */
183 BYTE fileattr; /* 15 file attributes */
184 WORD filetime; /* 16 file time */
185 WORD filedate; /* 18 file date */
186 DWORD filesize; /* 1a file size */
187 char filename[13]; /* 1e file name + extension */
190 /* FCB layout for FindFirstFCB/FindNextFCB */
193 BYTE drive; /* 00 drive letter */
194 char filename[11]; /* 01 filename 8+3 format */
195 int count; /* 0c entry count (was: reserved) */
196 WCHAR *fullPath; /* 10 full path (was: reserved) */
199 /* DOS directory entry for FindFirstFCB/FindNextFCB */
202 char filename[11]; /* 00 filename 8+3 format */
203 BYTE fileattr; /* 0b file attributes */
204 BYTE reserved[10]; /* 0c reserved */
205 WORD filetime; /* 16 file time */
206 WORD filedate; /* 18 file date */
207 WORD cluster; /* 1a file first cluster */
208 DWORD filesize; /* 1c file size */
209 } DOS_DIRENTRY_LAYOUT;
213 /* dos file attributes */
214 #define FA_NORMAL 0x00 /* Normal file, no attributes */
215 #define FA_RDONLY 0x01 /* Read only attribute */
216 #define FA_HIDDEN 0x02 /* Hidden file */
217 #define FA_SYSTEM 0x04 /* System file */
218 #define FA_LABEL 0x08 /* Volume label */
219 #define FA_DIRECTORY 0x10 /* Directory */
220 #define FA_ARCHIVE 0x20 /* Archive */
221 #define FA_UNUSED 0x40 /* Unused */
224 #define ER_NoNetwork 0x49
227 #define EC_OutOfResource 0x01
228 #define EC_Temporary 0x02
229 #define EC_AccessDenied 0x03
230 #define EC_InternalError 0x04
231 #define EC_HardwareFailure 0x05
232 #define EC_SystemFailure 0x06
233 #define EC_ProgramError 0x07
234 #define EC_NotFound 0x08
235 #define EC_MediaError 0x0b
236 #define EC_Exists 0x0c
237 #define EC_Unknown 0x0d
239 /* Suggested actions */
240 #define SA_Retry 0x01
241 #define SA_DelayedRetry 0x02
242 #define SA_Abort 0x04
243 #define SA_Ignore 0x06
244 #define SA_Ask4Retry 0x07
247 #define EL_Unknown 0x01
249 #define EL_Network 0x03
250 #define EL_Serial 0x04
251 #define EL_Memory 0x05
253 /* BIOS Keyboard Scancodes */
254 #define KEY_LEFT 0x4B
255 #define KEY_RIGHT 0x4D
257 #define KEY_DOWN 0x50
258 #define KEY_IC 0x52 /* insert char */
259 #define KEY_DC 0x53 /* delete char */
260 #define KEY_BACKSPACE 0x0E
261 #define KEY_HOME 0x47
263 #define KEY_NPAGE 0x49
264 #define KEY_PPAGE 0x51
272 void (*ioctl_handler)(CONTEXT86 *);
275 static void INT21_IoctlScsiMgrHandler( CONTEXT86 * );
276 static void INT21_IoctlHPScanHandler( CONTEXT86 * );
278 static struct magic_device magic_devices[] =
280 { {'s','c','s','i','m','g','r','$',0}, NULL, { { 0, 0 } }, INT21_IoctlScsiMgrHandler },
281 { {'e','m','m','x','x','x','x','0',0}, NULL, { { 0, 0 } }, EMS_Ioctl_Handler },
282 { {'h','p','s','c','a','n',0}, NULL, { { 0, 0 } }, INT21_IoctlHPScanHandler },
285 #define NB_MAGIC_DEVICES (sizeof(magic_devices)/sizeof(magic_devices[0]))
288 /* Many calls translate a drive argument like this:
289 drive number (00h = default, 01h = A:, etc)
291 /******************************************************************
294 * Many calls translate a drive argument like this:
295 * drive number (00h = default, 01h = A:, etc)
297 static const char *INT21_DriveName(int drive)
301 if (drive <= 26) return wine_dbg_sprintf("%c:", 'A' + drive - 1);
302 else return wine_dbg_sprintf( "<Bad drive: %d>", drive);
307 /***********************************************************************
308 * INT21_GetCurrentDrive
310 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
311 * MAX_DOS_DRIVES on error.
313 static BYTE INT21_GetCurrentDrive(void)
315 WCHAR current_directory[MAX_PATH];
317 if (!GetCurrentDirectoryW( MAX_PATH, current_directory ) ||
318 current_directory[1] != ':')
320 TRACE( "Failed to get current drive.\n" );
321 return MAX_DOS_DRIVES;
324 return toupperW( current_directory[0] ) - 'A';
328 /***********************************************************************
331 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
332 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
334 static BYTE INT21_MapDrive( BYTE drive )
338 WCHAR drivespec[3] = {'A', ':', 0};
341 drivespec[0] += drive - 1;
342 drivetype = GetDriveTypeW( drivespec );
344 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
345 return MAX_DOS_DRIVES;
350 return INT21_GetCurrentDrive();
354 /***********************************************************************
355 * INT21_SetCurrentDrive
357 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
359 static void INT21_SetCurrentDrive( BYTE drive )
361 WCHAR drivespec[3] = {'A', ':', 0};
363 drivespec[0] += drive;
365 if (!SetCurrentDirectoryW( drivespec ))
366 TRACE( "Failed to set current drive.\n" );
370 /***********************************************************************
373 * Reads a character from the standard input.
374 * Extended keycodes will be returned as two separate characters.
376 static BOOL INT21_ReadChar( BYTE *input, CONTEXT86 *waitctx )
378 static BYTE pending_scan = 0;
383 *input = pending_scan;
392 if (!DOSVM_Int16ReadChar( &ascii, &scan, waitctx ))
397 if (waitctx && !ascii)
404 /***********************************************************************
405 * INT21_GetSystemCountryCode
407 * Return DOS country code for default system locale.
409 static WORD INT21_GetSystemCountryCode( void )
412 * FIXME: Determine country code. We should probably use
413 * DOSCONF structure for that.
415 return GetSystemDefaultLangID();
419 /***********************************************************************
420 * INT21_FillCountryInformation
422 * Fill 34-byte buffer with country information data using
423 * default system locale.
425 static void INT21_FillCountryInformation( BYTE *buffer )
427 /* 00 - WORD: date format
432 *(WORD*)(buffer + 0) = 0; /* FIXME: Get from locale */
434 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
435 buffer[2] = '$'; /* FIXME: Get from locale */
438 /* 07 - BYTE[2]: ASCIIZ thousands separator */
439 buffer[7] = 0; /* FIXME: Get from locale */
442 /* 09 - BYTE[2]: ASCIIZ decimal separator */
443 buffer[9] = '.'; /* FIXME: Get from locale */
446 /* 11 - BYTE[2]: ASCIIZ date separator */
447 buffer[11] = '/'; /* FIXME: Get from locale */
450 /* 13 - BYTE[2]: ASCIIZ time separator */
451 buffer[13] = ':'; /* FIXME: Get from locale */
454 /* 15 - BYTE: Currency format
455 * bit 2 = set if currency symbol replaces decimal point
456 * bit 1 = number of spaces between value and currency symbol
457 * bit 0 = 0 if currency symbol precedes value
458 * 1 if currency symbol follows value
460 buffer[15] = 0; /* FIXME: Get from locale */
462 /* 16 - BYTE: Number of digits after decimal in currency */
463 buffer[16] = 0; /* FIXME: Get from locale */
465 /* 17 - BYTE: Time format
466 * bit 0 = 0 if 12-hour clock
469 buffer[17] = 1; /* FIXME: Get from locale */
471 /* 18 - DWORD: Address of case map routine */
472 *(DWORD*)(buffer + 18) = 0; /* FIXME: ptr to case map routine */
474 /* 22 - BYTE[2]: ASCIIZ data-list separator */
475 buffer[22] = ','; /* FIXME: Get from locale */
478 /* 24 - BYTE[10]: Reserved */
479 memset( buffer + 24, 0, 10 );
483 /***********************************************************************
486 * Initialize DOS heap.
488 * Filename Terminator Table of w2k DE NTVDM:
489 * 16 00 01 00 FF 00 00 20-02 0E 2E 22 2F 5C 5B 5D ....... ..."/\[]
490 * 3A 7C 3C 3E 2B 3D 3B 2C-00 :|<>+=;,
492 static void INT21_FillHeap( INT21_HEAP *heap )
494 static const char terminators[] = ".\"/\\[]:|<>+=;,";
500 heap->uppercase_size = 128;
501 for (i = 0; i < 128; i++)
502 heap->uppercase_table[i] = toupper( 128 + i );
507 heap->lowercase_size = 256;
508 for (i = 0; i < 256; i++)
509 heap->lowercase_table[i] = tolower( i );
514 heap->collating_size = 256;
515 for (i = 0; i < 256; i++)
516 heap->collating_table[i] = i;
521 heap->filename_size = 8 + strlen(terminators);
522 heap->filename_illegal_size = strlen(terminators);
523 memcpy( heap->filename_illegal_table, terminators, heap->filename_illegal_size );
525 heap->filename_reserved1 = 0x01;
526 heap->filename_lowest = 0x00;
527 heap->filename_highest = 0xff;
528 heap->filename_reserved2 = 0x00;
529 heap->filename_exclude_first = 0x00;
530 heap->filename_exclude_last = 0x20;
531 heap->filename_reserved3 = 0x02;
534 * DBCS lead byte table. This table is empty.
537 memset( heap->dbcs_table, 0, sizeof(heap->dbcs_table) );
540 * Initialize InDos flag.
542 heap->misc_indos = 0;
545 * FIXME: Should drive parameter blocks (DPB) be
546 * initialized here and linked to DOS LOL?
551 /***********************************************************************
552 * INT21_GetHeapPointer
554 * Get pointer for DOS heap (INT21_HEAP).
555 * Creates and initializes heap on first call.
557 static INT21_HEAP *INT21_GetHeapPointer( void )
559 static INT21_HEAP *heap_pointer = NULL;
566 heap_pointer = DOSVM_AllocDataUMB( sizeof(INT21_HEAP),
570 heap_pointer->misc_segment = heap_segment;
571 heap_pointer->misc_selector = heap_selector;
572 INT21_FillHeap( heap_pointer );
579 /***********************************************************************
580 * INT21_GetHeapSelector
582 * Get segment/selector for DOS heap (INT21_HEAP).
583 * Creates and initializes heap on first call.
585 static WORD INT21_GetHeapSelector( CONTEXT86 *context )
587 INT21_HEAP *heap = INT21_GetHeapPointer();
589 if (!ISV86(context) && DOSVM_IsWin16())
590 return heap->misc_selector;
592 return heap->misc_segment;
596 /***********************************************************************
599 * Fill DOS heap drive parameter block for the specified drive.
600 * Return TRUE if drive was valid and there were
601 * no errors while reading drive information.
603 static BOOL INT21_FillDrivePB( BYTE drive )
605 WCHAR drivespec[3] = {'A', ':', 0};
606 INT21_HEAP *heap = INT21_GetHeapPointer();
609 DWORD cluster_sectors;
612 DWORD total_clusters;
614 if (drive >= MAX_DOS_DRIVES)
617 dpb = &heap->misc_dpb_list[drive];
618 drivespec[0] += drive;
619 drivetype = GetDriveTypeW( drivespec );
622 * FIXME: Does this check work correctly with floppy/cdrom drives?
624 if (drivetype == DRIVE_NO_ROOT_DIR || drivetype == DRIVE_UNKNOWN)
628 * FIXME: Does this check work correctly with floppy/cdrom drives?
630 if (!GetDiskFreeSpaceW( drivespec, &cluster_sectors, §or_bytes,
631 &free_clusters, &total_clusters ))
635 * FIXME: Most of the values listed below are incorrect.
636 * All values should be validated.
641 dpb->sector_bytes = sector_bytes;
642 dpb->cluster_sectors = cluster_sectors - 1;
645 while (cluster_sectors > 1)
647 cluster_sectors /= 2;
651 dpb->num_reserved = 0;
653 dpb->num_root_entries = 2;
654 dpb->first_data_sector = 2;
655 dpb->num_clusters1 = total_clusters;
656 dpb->sectors_per_FAT = 1;
657 dpb->first_dir_sector = 1;
658 dpb->driver_header = 0;
659 dpb->media_ID = (drivetype == DRIVE_FIXED) ? 0xF8 : 0xF0;
660 dpb->access_flag = 0;
662 dpb->search_cluster1 = 0;
663 dpb->free_clusters_lo = LOWORD(free_clusters);
664 dpb->free_clusters_hi = HIWORD(free_clusters);
665 dpb->mirroring_flags = 0;
666 dpb->info_sector = 0xffff;
667 dpb->spare_boot_sector = 0xffff;
668 dpb->first_cluster_sector = 0;
669 dpb->num_clusters2 = total_clusters;
670 dpb->fat_clusters = 32;
671 dpb->root_cluster = 0;
672 dpb->search_cluster2 = 0;
678 /***********************************************************************
679 * INT21_GetCurrentDirectory
683 * - subfunction 0x47 of function 0x71
685 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context, BOOL islong )
687 char *buffer = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi);
688 BYTE drive = INT21_MapDrive( DL_reg(context) );
689 WCHAR pathW[MAX_PATH];
690 char pathA[MAX_PATH];
693 TRACE( "drive %d\n", DL_reg(context) );
695 if (drive == MAX_DOS_DRIVES)
697 SetLastError(ERROR_INVALID_DRIVE);
702 * Grab current directory.
705 if (!GetCurrentDirectoryW( MAX_PATH, pathW )) return FALSE;
707 if (toupperW(pathW[0]) - 'A' != drive || pathW[1] != ':')
709 /* cwd is not on the requested drive, get the environment string instead */
714 env_var[1] = 'A' + drive;
717 if (!GetEnvironmentVariableW( env_var, pathW, MAX_PATH ))
719 /* return empty path */
726 * Convert into short format.
731 DWORD result = GetShortPathNameW( pathW, pathW, MAX_PATH );
734 if (result > MAX_PATH)
736 WARN( "Short path too long!\n" );
737 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
743 * The returned pathname does not include
744 * the drive letter, colon or leading backslash.
750 * FIXME: We should probably just strip host part from name...
752 FIXME( "UNC names are not supported.\n" );
753 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
756 else if (!ptr[0] || ptr[1] != ':' || ptr[2] != '\\')
758 WARN( "Path is neither UNC nor DOS path: %s\n",
759 wine_dbgstr_w(ptr) );
760 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
765 /* Remove drive letter, colon and leading backslash. */
770 * Convert into OEM string.
773 if (!WideCharToMultiByte(CP_OEMCP, 0, ptr, -1, pathA,
774 MAX_PATH, NULL, NULL))
776 WARN( "Cannot convert path!\n" );
777 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
787 /* Undocumented success code. */
788 SET_AX( context, 0x0100 );
790 /* Truncate buffer to 64 bytes. */
794 TRACE( "%c:=%s\n", 'A' + drive, pathA );
796 strcpy( buffer, pathA );
801 /***********************************************************************
802 * INT21_SetCurrentDirectory
806 * - subfunction 0x3b of function 0x71
808 static BOOL INT21_SetCurrentDirectory( CONTEXT86 *context )
810 WCHAR dirW[MAX_PATH];
813 char *dirA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
814 BYTE drive = INT21_GetCurrentDrive();
817 TRACE( "SET CURRENT DIRECTORY %s\n", dirA );
819 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
820 if (!GetFullPathNameW( dirW, MAX_PATH, dirW, NULL )) return FALSE;
822 attr = GetFileAttributesW( dirW );
823 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
825 SetLastError( ERROR_PATH_NOT_FOUND );
830 env_var[1] = dirW[0];
833 result = SetEnvironmentVariableW( env_var, dirW );
835 /* only set current directory if on the current drive */
836 if (result && (toupperW(dirW[0]) - 'A' == drive)) result = SetCurrentDirectoryW( dirW );
841 /***********************************************************************
842 * INT21_CreateMagicDeviceHandle
844 * Create a dummy file handle for a "magic" device.
846 static HANDLE INT21_CreateMagicDeviceHandle( LPCWSTR name )
848 static const WCHAR prefixW[] = {'\\','?','?','\\','u','n','i','x'};
849 const char *dir = wine_get_server_dir();
853 OBJECT_ATTRIBUTES attr;
854 UNICODE_STRING nameW;
857 len = MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, NULL, 0 );
858 nameW.Length = sizeof(prefixW) + (len + strlenW( name )) * sizeof(WCHAR);
859 nameW.MaximumLength = nameW.Length + sizeof(WCHAR);
860 if (!(nameW.Buffer = HeapAlloc( GetProcessHeap(), 0, nameW.MaximumLength )))
862 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
865 memcpy( nameW.Buffer, prefixW, sizeof(prefixW) );
866 MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, nameW.Buffer + sizeof(prefixW)/sizeof(WCHAR), len );
867 len += sizeof(prefixW) / sizeof(WCHAR);
868 nameW.Buffer[len-1] = '/';
869 strcpyW( nameW.Buffer + len, name );
871 attr.Length = sizeof(attr);
872 attr.RootDirectory = 0;
874 attr.ObjectName = &nameW;
875 attr.SecurityDescriptor = NULL;
876 attr.SecurityQualityOfService = NULL;
878 status = NtCreateFile( &ret, GENERIC_READ|GENERIC_WRITE, &attr, &io, NULL, 0,
879 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF,
880 FILE_SYNCHRONOUS_IO_ALERT, NULL, 0 );
884 SetLastError( RtlNtStatusToDosError(status) );
886 RtlFreeUnicodeString( &nameW );
891 /***********************************************************************
892 * INT21_OpenMagicDevice
894 * Open a file handle for "magic" devices like EMMXXXX0.
896 static HANDLE INT21_OpenMagicDevice( LPCWSTR name, DWORD access )
902 if (name[0] && (name[1] == ':')) name += 2;
903 if ((p = strrchrW( name, '/' ))) name = p + 1;
904 if ((p = strrchrW( name, '\\' ))) name = p + 1;
906 for (i = 0; i < NB_MAGIC_DEVICES; i++)
908 int len = strlenW( magic_devices[i].name );
909 if (!strncmpiW( magic_devices[i].name, name, len ) &&
910 (!name[len] || name[len] == '.' || name[len] == ':')) break;
912 if (i == NB_MAGIC_DEVICES) return 0;
914 if (!magic_devices[i].handle) /* need to open it */
917 FILE_INTERNAL_INFORMATION info;
919 if (!(handle = INT21_CreateMagicDeviceHandle( magic_devices[i].name ))) return 0;
921 NtQueryInformationFile( handle, &io, &info, sizeof(info), FileInternalInformation );
922 magic_devices[i].index = info.IndexNumber;
923 magic_devices[i].handle = handle;
925 if (!DuplicateHandle( GetCurrentProcess(), magic_devices[i].handle,
926 GetCurrentProcess(), &handle, access, FALSE, 0 )) handle = 0;
931 /***********************************************************************
939 * - subfunction 0x6c of function 0x71
941 static BOOL INT21_CreateFile( CONTEXT86 *context,
948 char *pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, pathSegOff);
949 WCHAR pathW[MAX_PATH];
956 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
957 "create flags=%04x, file=%s.\n",
958 AH_reg(context), dosAction, dosAccessShare, CX_reg(context), pathA );
961 * Application tried to create/open a file whose name
962 * ends with a backslash. This is not allowed.
964 * FIXME: This needs to be validated, especially the return value.
966 if (pathA[strlen(pathA) - 1] == '/')
968 SetLastError( ERROR_FILE_NOT_FOUND );
973 * Convert DOS action flags into Win32 creation disposition parameter.
978 winMode = OPEN_EXISTING;
981 winMode = TRUNCATE_EXISTING;
984 winMode = CREATE_NEW;
987 winMode = OPEN_ALWAYS;
990 winMode = CREATE_ALWAYS;
993 SetLastError( ERROR_INVALID_PARAMETER );
998 * Convert DOS access/share flags into Win32 desired access parameter.
1000 switch(dosAccessShare & 0x07)
1003 winAccess = GENERIC_READ;
1006 winAccess = GENERIC_WRITE;
1009 winAccess = GENERIC_READ | GENERIC_WRITE;
1013 * Read-only, do not modify file's last-access time (DOS7).
1015 * FIXME: How to prevent modification of last-access time?
1017 winAccess = GENERIC_READ;
1024 * Convert DOS access/share flags into Win32 share mode parameter.
1026 switch(dosAccessShare & 0x70)
1028 case OF_SHARE_EXCLUSIVE:
1031 case OF_SHARE_DENY_WRITE:
1032 winSharing = FILE_SHARE_READ;
1034 case OF_SHARE_DENY_READ:
1035 winSharing = FILE_SHARE_WRITE;
1037 case OF_SHARE_DENY_NONE:
1038 case OF_SHARE_COMPAT:
1040 winSharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1044 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
1045 * What to do with this bit?
1046 * FIXME: Bits in the high byte of dosAccessShare are not supported.
1047 * See both function 0x6c and subfunction 0x6c of function 0x71 for
1048 * definition of these bits.
1052 * Convert DOS create attributes into Win32 flags and attributes parameter.
1054 if (winMode == OPEN_EXISTING || winMode == TRUNCATE_EXISTING)
1060 WORD dosAttributes = CX_reg(context);
1062 if (dosAttributes & FA_LABEL)
1065 * Application tried to create volume label entry.
1066 * This is difficult to support so we do not allow it.
1068 * FIXME: If volume does not already have a label,
1069 * this function is supposed to succeed.
1071 SetLastError( ERROR_ACCESS_DENIED );
1075 winAttributes = dosAttributes &
1076 (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
1077 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE);
1083 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
1085 if ((winHandle = INT21_OpenMagicDevice( pathW, winAccess )))
1091 winHandle = CreateFileW( pathW, winAccess, winSharing, NULL,
1092 winMode, winAttributes, 0 );
1093 /* DOS allows to open files on a CDROM R/W */
1094 if( winHandle == INVALID_HANDLE_VALUE &&
1095 GetLastError()== ERROR_WRITE_PROTECT) {
1096 winHandle = CreateFileW( pathW, winAccess & ~GENERIC_WRITE,
1097 winSharing, NULL, winMode, winAttributes, 0 );
1100 if (winHandle == INVALID_HANDLE_VALUE)
1104 * Determine DOS file status.
1115 case TRUNCATE_EXISTING:
1122 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 1 : 2;
1125 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 3 : 2;
1133 * Return DOS file handle and DOS status.
1135 SET_AX( context, Win32HandleToDosFileHandle(winHandle) );
1138 SET_CX( context, dosStatus );
1140 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
1141 AX_reg(context), dosStatus );
1147 /***********************************************************************
1148 * INT21_BufferedInput
1150 * Handler for function 0x0a and reading from console using
1153 * Reads a string of characters from standard input until
1154 * enter key is pressed. Returns either number of characters
1155 * read from console including terminating CR or
1156 * zero if capacity was zero.
1158 static WORD INT21_BufferedInput( CONTEXT86 *context, BYTE *ptr, WORD capacity )
1163 * Return immediately if capacity is zero.
1173 DOSVM_Int16ReadChar( &ascii, &scan, context );
1175 if (ascii == '\r' || ascii == '\n')
1182 * DOS handles only backspace and KEY_LEFT
1183 * perhaps we should do more
1185 if (ascii == '\b' || scan == KEY_LEFT)
1187 if (length==0) continue;
1188 DOSVM_PutChar( '\b' );
1194 * If the buffer becomes filled to within one byte of
1195 * capacity, DOS rejects all further characters up to,
1196 * but not including, the terminating carriage return.
1198 if (ascii != 0 && length < capacity-1)
1200 DOSVM_PutChar( ascii );
1201 ptr[length] = ascii;
1208 /***********************************************************************
1209 * INT21_GetCurrentDTA
1211 static BYTE *INT21_GetCurrentDTA( CONTEXT86 *context )
1213 TDB *pTask = GlobalLock16(GetCurrentTask());
1215 /* FIXME: This assumes DTA was set correctly! */
1216 return CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
1217 OFFSETOF(pTask->dta) );
1221 /***********************************************************************
1222 * INT21_OpenFileUsingFCB
1224 * Handler for function 0x0f.
1227 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
1234 * Opens a FCB file for read/write in compatibility mode. Upon calling
1235 * the FCB must have the drive_number, file_name, and file_extension
1236 * fields filled and all other bytes cleared.
1238 static void INT21_OpenFileUsingFCB( CONTEXT86 *context )
1246 BY_HANDLE_FILE_INFORMATION info;
1249 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1250 if (fcb->drive_number == 0xff) {
1251 xfcb = (struct XFCB *) fcb;
1252 fcb = (struct FCB *) xfcb->fcb;
1256 file_path[0] = 'A' + INT21_MapDrive( fcb->drive_number );
1258 if (AL_result == 0) {
1260 pos = &file_path[2];
1261 memcpy(pos, fcb->file_name, 8);
1264 pos = strchr(pos, ' ');
1267 memcpy(pos, fcb->file_extension, 3);
1270 pos = strchr(pos, ' ');
1273 handle = (HANDLE) _lopen(file_path, OF_READWRITE);
1274 if (handle == INVALID_HANDLE_VALUE) {
1275 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path);
1276 AL_result = 0xff; /* failed */
1278 hfile16 = Win32HandleToDosFileHandle(handle);
1279 if (hfile16 == HFILE_ERROR16) {
1280 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle);
1281 CloseHandle(handle);
1282 AL_result = 0xff; /* failed */
1283 } else if (hfile16 > 255) {
1284 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16, file_path);
1286 AL_result = 0xff; /* failed */
1288 if (!GetFileInformationByHandle(handle, &info)) {
1289 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1290 hfile16, handle, file_path);
1292 AL_result = 0xff; /* failed */
1294 fcb->drive_number = file_path[0] - 'A' + 1;
1295 fcb->current_block_number = 0;
1296 fcb->logical_record_size = 128;
1297 fcb->file_size = info.nFileSizeLow;
1298 FileTimeToDosDateTime(&info.ftLastWriteTime,
1299 &fcb->date_of_last_write, &fcb->time_of_last_write);
1300 fcb->file_number = hfile16;
1301 fcb->attributes = 0xc2;
1302 fcb->starting_cluster = 0; /* don't know correct init value */
1303 fcb->sequence_number = 0; /* don't know correct init value */
1304 fcb->file_attributes = info.dwFileAttributes;
1305 /* The following fields are not initialized */
1306 /* by the native function: */
1308 /* record_within_current_block */
1309 /* random_access_record_number */
1311 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1312 file_path, hfile16, handle);
1313 AL_result = 0x00; /* successful */
1318 SET_AL(context, AL_result);
1322 /***********************************************************************
1323 * INT21_CloseFileUsingFCB
1325 * Handler for function 0x10.
1328 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1335 * Closes a FCB file.
1337 static void INT21_CloseFileUsingFCB( CONTEXT86 *context )
1343 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1344 if (fcb->drive_number == 0xff) {
1345 xfcb = (struct XFCB *) fcb;
1346 fcb = (struct FCB *) xfcb->fcb;
1349 if (_lclose16((HFILE16) fcb->file_number) != 0) {
1350 TRACE("_lclose16(%d) failed\n", fcb->file_number);
1351 AL_result = 0xff; /* failed */
1353 TRACE("successful closed file %d\n", fcb->file_number);
1354 AL_result = 0x00; /* successful */
1356 SET_AL(context, AL_result);
1360 /***********************************************************************
1361 * INT21_SequentialReadFromFCB
1363 * Handler for function 0x14.
1366 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1370 * 1: end of file, no data read
1371 * 2: segment wrap in DTA, no data read (not returned now)
1372 * 3: end of file, partial record read
1375 * Reads a record with the size FCB->logical_record_size from the FCB
1376 * to the disk transfer area. The position of the record is specified
1377 * with FCB->current_block_number and FCB->record_within_current_block.
1378 * Then FCB->current_block_number and FCB->record_within_current_block
1379 * are updated to point to the next record. If a partial record is
1380 * read, it is filled with zeros up to the FCB->logical_record_size.
1382 static void INT21_SequentialReadFromFCB( CONTEXT86 *context )
1387 DWORD record_number;
1389 BYTE *disk_transfer_area;
1393 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1394 if (fcb->drive_number == 0xff) {
1395 xfcb = (struct XFCB *) fcb;
1396 fcb = (struct FCB *) xfcb->fcb;
1399 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1400 if (handle == INVALID_HANDLE_VALUE) {
1401 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1403 AL_result = 0x01; /* end of file, no data read */
1405 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1406 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1407 if (position != record_number * fcb->logical_record_size) {
1408 TRACE("seek(%d, %d, 0) failed with %u\n",
1409 fcb->file_number, record_number * fcb->logical_record_size, position);
1410 AL_result = 0x01; /* end of file, no data read */
1412 disk_transfer_area = INT21_GetCurrentDTA(context);
1413 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1414 if (bytes_read != fcb->logical_record_size) {
1415 TRACE("_lread(%d, %p, %d) failed with %d\n",
1416 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1417 if (bytes_read == 0) {
1418 AL_result = 0x01; /* end of file, no data read */
1420 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1421 AL_result = 0x03; /* end of file, partial record read */
1424 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1425 bytes_read, record_number, position, fcb->file_number, handle);
1426 AL_result = 0x00; /* successful */
1430 if (AL_result == 0x00 || AL_result == 0x03) {
1431 if (fcb->record_within_current_block == 127) {
1432 fcb->record_within_current_block = 0;
1433 fcb->current_block_number++;
1435 fcb->record_within_current_block++;
1438 SET_AL(context, AL_result);
1442 /***********************************************************************
1443 * INT21_SequentialWriteToFCB
1445 * Handler for function 0x15.
1448 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1453 * 2: segment wrap in DTA (not returned now)
1456 * Writes a record with the size FCB->logical_record_size from the disk
1457 * transfer area to the FCB. The position of the record is specified
1458 * with FCB->current_block_number and FCB->record_within_current_block.
1459 * Then FCB->current_block_number and FCB->record_within_current_block
1460 * are updated to point to the next record.
1462 static void INT21_SequentialWriteToFCB( CONTEXT86 *context )
1467 DWORD record_number;
1469 BYTE *disk_transfer_area;
1473 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1474 if (fcb->drive_number == 0xff) {
1475 xfcb = (struct XFCB *) fcb;
1476 fcb = (struct FCB *) xfcb->fcb;
1479 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1480 if (handle == INVALID_HANDLE_VALUE) {
1481 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1483 AL_result = 0x01; /* disk full */
1485 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1486 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1487 if (position != record_number * fcb->logical_record_size) {
1488 TRACE("seek(%d, %d, 0) failed with %u\n",
1489 fcb->file_number, record_number * fcb->logical_record_size, position);
1490 AL_result = 0x01; /* disk full */
1492 disk_transfer_area = INT21_GetCurrentDTA(context);
1493 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, fcb->logical_record_size);
1494 if (bytes_written != fcb->logical_record_size) {
1495 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1496 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1497 AL_result = 0x01; /* disk full */
1499 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1500 bytes_written, record_number, position, fcb->file_number, handle);
1501 AL_result = 0x00; /* successful */
1505 if (AL_result == 0x00) {
1506 if (fcb->record_within_current_block == 127) {
1507 fcb->record_within_current_block = 0;
1508 fcb->current_block_number++;
1510 fcb->record_within_current_block++;
1513 SET_AL(context, AL_result);
1517 /***********************************************************************
1518 * INT21_ReadRandomRecordFromFCB
1520 * Handler for function 0x21.
1523 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1527 * 1: end of file, no data read
1528 * 2: segment wrap in DTA, no data read (not returned now)
1529 * 3: end of file, partial record read
1532 * Reads a record with the size FCB->logical_record_size from
1533 * the FCB to the disk transfer area. The position of the record
1534 * is specified with FCB->random_access_record_number. The
1535 * FCB->random_access_record_number is not updated. If a partial record
1536 * is read, it is filled with zeros up to the FCB->logical_record_size.
1538 static void INT21_ReadRandomRecordFromFCB( CONTEXT86 *context )
1543 DWORD record_number;
1545 BYTE *disk_transfer_area;
1549 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1550 if (fcb->drive_number == 0xff) {
1551 xfcb = (struct XFCB *) fcb;
1552 fcb = (struct FCB *) xfcb->fcb;
1555 memcpy(&record_number, fcb->random_access_record_number, 4);
1556 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1557 if (handle == INVALID_HANDLE_VALUE) {
1558 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1560 AL_result = 0x01; /* end of file, no data read */
1562 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1563 if (position != record_number * fcb->logical_record_size) {
1564 TRACE("seek(%d, %d, 0) failed with %u\n",
1565 fcb->file_number, record_number * fcb->logical_record_size, position);
1566 AL_result = 0x01; /* end of file, no data read */
1568 disk_transfer_area = INT21_GetCurrentDTA(context);
1569 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1570 if (bytes_read != fcb->logical_record_size) {
1571 TRACE("_lread(%d, %p, %d) failed with %d\n",
1572 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1573 if (bytes_read == 0) {
1574 AL_result = 0x01; /* end of file, no data read */
1576 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1577 AL_result = 0x03; /* end of file, partial record read */
1580 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1581 bytes_read, record_number, position, fcb->file_number, handle);
1582 AL_result = 0x00; /* successful */
1586 fcb->current_block_number = record_number / 128;
1587 fcb->record_within_current_block = record_number % 128;
1588 SET_AL(context, AL_result);
1592 /***********************************************************************
1593 * INT21_WriteRandomRecordToFCB
1595 * Handler for function 0x22.
1598 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1603 * 2: segment wrap in DTA (not returned now)
1606 * Writes a record with the size FCB->logical_record_size from
1607 * the disk transfer area to the FCB. The position of the record
1608 * is specified with FCB->random_access_record_number. The
1609 * FCB->random_access_record_number is not updated.
1611 static void INT21_WriteRandomRecordToFCB( CONTEXT86 *context )
1616 DWORD record_number;
1618 BYTE *disk_transfer_area;
1622 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1623 if (fcb->drive_number == 0xff) {
1624 xfcb = (struct XFCB *) fcb;
1625 fcb = (struct FCB *) xfcb->fcb;
1628 memcpy(&record_number, fcb->random_access_record_number, 4);
1629 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1630 if (handle == INVALID_HANDLE_VALUE) {
1631 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1633 AL_result = 0x01; /* disk full */
1635 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1636 if (position != record_number * fcb->logical_record_size) {
1637 TRACE("seek(%d, %d, 0) failed with %u\n",
1638 fcb->file_number, record_number * fcb->logical_record_size, position);
1639 AL_result = 0x01; /* disk full */
1641 disk_transfer_area = INT21_GetCurrentDTA(context);
1642 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, fcb->logical_record_size);
1643 if (bytes_written != fcb->logical_record_size) {
1644 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1645 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1646 AL_result = 0x01; /* disk full */
1648 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1649 bytes_written, record_number, position, fcb->file_number, handle);
1650 AL_result = 0x00; /* successful */
1654 fcb->current_block_number = record_number / 128;
1655 fcb->record_within_current_block = record_number % 128;
1656 SET_AL(context, AL_result);
1660 /***********************************************************************
1661 * INT21_RandomBlockReadFromFCB
1663 * Handler for function 0x27.
1666 * CX [I/O] Number of records to read
1667 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1671 * 1: end of file, no data read
1672 * 2: segment wrap in DTA, no data read (not returned now)
1673 * 3: end of file, partial record read
1676 * Reads several records with the size FCB->logical_record_size from
1677 * the FCB to the disk transfer area. The number of records to be
1678 * read is specified in the CX register. The position of the first
1679 * record is specified with FCB->random_access_record_number. The
1680 * FCB->random_access_record_number, the FCB->current_block_number
1681 * and FCB->record_within_current_block are updated to point to the
1682 * next record after the records read. If a partial record is read,
1683 * it is filled with zeros up to the FCB->logical_record_size. The
1684 * CX register is set to the number of successfully read records.
1686 static void INT21_RandomBlockReadFromFCB( CONTEXT86 *context )
1691 DWORD record_number;
1693 BYTE *disk_transfer_area;
1694 UINT records_requested;
1695 UINT bytes_requested;
1700 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1701 if (fcb->drive_number == 0xff) {
1702 xfcb = (struct XFCB *) fcb;
1703 fcb = (struct FCB *) xfcb->fcb;
1706 memcpy(&record_number, fcb->random_access_record_number, 4);
1707 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1708 if (handle == INVALID_HANDLE_VALUE) {
1709 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1712 AL_result = 0x01; /* end of file, no data read */
1714 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1715 if (position != record_number * fcb->logical_record_size) {
1716 TRACE("seek(%d, %d, 0) failed with %u\n",
1717 fcb->file_number, record_number * fcb->logical_record_size, position);
1719 AL_result = 0x01; /* end of file, no data read */
1721 disk_transfer_area = INT21_GetCurrentDTA(context);
1722 records_requested = CX_reg(context);
1723 bytes_requested = records_requested * fcb->logical_record_size;
1724 bytes_read = _lread((HFILE) handle, disk_transfer_area, bytes_requested);
1725 if (bytes_read != bytes_requested) {
1726 TRACE("_lread(%d, %p, %d) failed with %d\n",
1727 fcb->file_number, disk_transfer_area, bytes_requested, bytes_read);
1728 records_read = bytes_read / fcb->logical_record_size;
1729 if (bytes_read % fcb->logical_record_size == 0) {
1730 AL_result = 0x01; /* end of file, no data read */
1733 memset(&disk_transfer_area[bytes_read], 0, records_read * fcb->logical_record_size - bytes_read);
1734 AL_result = 0x03; /* end of file, partial record read */
1737 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1738 bytes_read, record_number, position, fcb->file_number, handle);
1739 records_read = records_requested;
1740 AL_result = 0x00; /* successful */
1744 record_number += records_read;
1745 memcpy(fcb->random_access_record_number, &record_number, 4);
1746 fcb->current_block_number = record_number / 128;
1747 fcb->record_within_current_block = record_number % 128;
1748 SET_CX(context, records_read);
1749 SET_AL(context, AL_result);
1753 /***********************************************************************
1754 * INT21_RandomBlockWriteToFCB
1756 * Handler for function 0x28.
1759 * CX [I/O] Number of records to write
1760 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1765 * 2: segment wrap in DTA (not returned now)
1768 * Writes several records with the size FCB->logical_record_size from
1769 * the disk transfer area to the FCB. The number of records to be
1770 * written is specified in the CX register. The position of the first
1771 * record is specified with FCB->random_access_record_number. The
1772 * FCB->random_access_record_number, the FCB->current_block_number
1773 * and FCB->record_within_current_block are updated to point to the
1774 * next record after the records written. The CX register is set to
1775 * the number of successfully written records.
1777 static void INT21_RandomBlockWriteToFCB( CONTEXT86 *context )
1782 DWORD record_number;
1784 BYTE *disk_transfer_area;
1785 UINT records_requested;
1786 UINT bytes_requested;
1788 UINT records_written;
1791 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1792 if (fcb->drive_number == 0xff) {
1793 xfcb = (struct XFCB *) fcb;
1794 fcb = (struct FCB *) xfcb->fcb;
1797 memcpy(&record_number, fcb->random_access_record_number, 4);
1798 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1799 if (handle == INVALID_HANDLE_VALUE) {
1800 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1802 records_written = 0;
1803 AL_result = 0x01; /* disk full */
1805 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1806 if (position != record_number * fcb->logical_record_size) {
1807 TRACE("seek(%d, %d, 0) failed with %u\n",
1808 fcb->file_number, record_number * fcb->logical_record_size, position);
1809 records_written = 0;
1810 AL_result = 0x01; /* disk full */
1812 disk_transfer_area = INT21_GetCurrentDTA(context);
1813 records_requested = CX_reg(context);
1814 bytes_requested = records_requested * fcb->logical_record_size;
1815 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, bytes_requested);
1816 if (bytes_written != bytes_requested) {
1817 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1818 fcb->file_number, disk_transfer_area, bytes_requested, bytes_written);
1819 records_written = bytes_written / fcb->logical_record_size;
1820 AL_result = 0x01; /* disk full */
1822 TRACE("successful write %d bytes from record %d (position %u) of file %d (handle %p)\n",
1823 bytes_written, record_number, position, fcb->file_number, handle);
1824 records_written = records_requested;
1825 AL_result = 0x00; /* successful */
1829 record_number += records_written;
1830 memcpy(fcb->random_access_record_number, &record_number, 4);
1831 fcb->current_block_number = record_number / 128;
1832 fcb->record_within_current_block = record_number % 128;
1833 SET_CX(context, records_written);
1834 SET_AL(context, AL_result);
1838 /***********************************************************************
1839 * INT21_CreateDirectory
1843 * - subfunction 0x39 of function 0x71
1844 * - subfunction 0xff of function 0x43 (CL == 0x39)
1846 static BOOL INT21_CreateDirectory( CONTEXT86 *context )
1848 WCHAR dirW[MAX_PATH];
1849 char *dirA = CTX_SEG_OFF_TO_LIN(context,
1853 TRACE( "CREATE DIRECTORY %s\n", dirA );
1855 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
1857 if (CreateDirectoryW(dirW, NULL))
1861 * FIXME: CreateDirectory's LastErrors will clash with the ones
1862 * used by DOS. AH=39 only returns 3 (path not found) and
1863 * 5 (access denied), while CreateDirectory return several
1864 * ones. Remap some of them. -Marcus
1866 switch (GetLastError())
1868 case ERROR_ALREADY_EXISTS:
1869 case ERROR_FILENAME_EXCED_RANGE:
1870 case ERROR_DISK_FULL:
1871 SetLastError(ERROR_ACCESS_DENIED);
1881 /***********************************************************************
1882 * INT21_ExtendedCountryInformation
1884 * Handler for function 0x65.
1886 static void INT21_ExtendedCountryInformation( CONTEXT86 *context )
1888 BYTE *dataptr = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
1889 BYTE buffsize = CX_reg (context);
1891 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1895 * Check subfunctions that are passed country and code page.
1897 if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
1899 WORD country = DX_reg(context);
1900 WORD codepage = BX_reg(context);
1902 if (country != 0xffff && country != INT21_GetSystemCountryCode())
1903 FIXME( "Requested info on non-default country %04x\n", country );
1905 if (codepage != 0xffff && codepage != GetOEMCP())
1906 FIXME( "Requested info on non-default code page %04x\n", codepage );
1909 switch (AL_reg(context)) {
1910 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1911 INT_BARF( context, 0x21 );
1912 SET_CFLAG( context );
1915 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1916 TRACE( "Get general internationalization info\n" );
1917 dataptr[0] = 0x01; /* Info ID */
1918 *(WORD*)(dataptr+1) = 38; /* Size of the following info */
1919 *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
1920 *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
1921 /* FIXME: fill buffer partially up to buffsize bytes*/
1922 if (buffsize >= 0x29){
1923 INT21_FillCountryInformation( dataptr + 7 );
1924 SET_CX( context, 0x29 ); /* Size of returned info */
1926 SET_CX( context, 0x07 ); /* Size of returned info */
1930 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1931 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1932 TRACE( "Get pointer to uppercase table\n" );
1933 dataptr[0] = AL_reg(context); /* Info ID */
1934 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1935 offsetof(INT21_HEAP, uppercase_size) );
1936 SET_CX( context, 5 ); /* Size of returned info */
1939 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1940 TRACE( "Get pointer to lowercase table\n" );
1941 dataptr[0] = 0x03; /* Info ID */
1942 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1943 offsetof(INT21_HEAP, lowercase_size) );
1944 SET_CX( context, 5 ); /* Size of returned info */
1947 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1948 TRACE("Get pointer to filename terminator table\n");
1949 dataptr[0] = 0x05; /* Info ID */
1950 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1951 offsetof(INT21_HEAP, filename_size) );
1952 SET_CX( context, 5 ); /* Size of returned info */
1955 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1956 TRACE("Get pointer to collating sequence table\n");
1957 dataptr[0] = 0x06; /* Info ID */
1958 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1959 offsetof(INT21_HEAP, collating_size) );
1960 SET_CX( context, 5 ); /* Size of returned info */
1963 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1964 TRACE("Get pointer to DBCS lead byte table\n");
1965 dataptr[0] = 0x07; /* Info ID */
1966 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1967 offsetof(INT21_HEAP, dbcs_size) );
1968 SET_CX( context, 5 ); /* Size of returned info */
1971 case 0x20: /* CAPITALIZE CHARACTER */
1972 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1973 TRACE("Convert char to uppercase\n");
1974 SET_DL( context, toupper(DL_reg(context)) );
1977 case 0x21: /* CAPITALIZE STRING */
1978 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1979 TRACE("Convert string to uppercase with length\n");
1981 char *ptr = CTX_SEG_OFF_TO_LIN( context, context->SegDs,
1983 WORD len = CX_reg(context);
1984 while (len--) { *ptr = toupper(*ptr); ptr++; }
1988 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1989 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1990 TRACE("Convert ASCIIZ string to uppercase\n");
1992 char *p = CTX_SEG_OFF_TO_LIN( context, context->SegDs, context->Edx );
1993 for ( ; *p; p++) *p = toupper(*p);
1997 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1998 INT_BARF( context, 0x21 );
1999 SET_CFLAG( context );
2003 INT_BARF( context, 0x21 );
2010 /***********************************************************************
2011 * INT21_FileAttributes
2015 * - subfunction 0x43 of function 0x71
2017 static BOOL INT21_FileAttributes( CONTEXT86 *context,
2021 WCHAR fileW[MAX_PATH];
2022 char *fileA = CTX_SEG_OFF_TO_LIN(context,
2032 switch (subfunction)
2034 case 0x00: /* GET FILE ATTRIBUTES */
2035 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA );
2036 len = MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2038 /* Winbench 96 Disk Test fails if we don't complain
2039 * about a filename that ends in \
2041 if (!len || (fileW[len-1] == '/') || (fileW[len-1] == '\\'))
2044 result = GetFileAttributesW( fileW );
2045 if (result == INVALID_FILE_ATTRIBUTES)
2049 SET_CX( context, (WORD)result );
2051 SET_AX( context, (WORD)result ); /* DR DOS */
2055 case 0x01: /* SET FILE ATTRIBUTES */
2056 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
2057 CX_reg(context), fileA );
2058 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2060 if (!SetFileAttributesW( fileW, CX_reg(context) ))
2064 case 0x02: /* GET COMPRESSED FILE SIZE */
2065 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA );
2066 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2068 result = GetCompressedFileSizeW( fileW, NULL );
2069 if (result == INVALID_FILE_SIZE)
2073 SET_AX( context, LOWORD(result) );
2074 SET_DX( context, HIWORD(result) );
2078 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
2080 INT_BARF( context, 0x21 );
2083 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
2084 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2086 handle = CreateFileW( fileW, GENERIC_WRITE,
2087 FILE_SHARE_READ | FILE_SHARE_WRITE,
2088 NULL, OPEN_EXISTING, 0, 0 );
2089 if (handle == INVALID_HANDLE_VALUE)
2092 DosDateTimeToFileTime( DI_reg(context),
2095 status = SetFileTime( handle, NULL, NULL, &filetime );
2097 CloseHandle( handle );
2102 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
2104 INT_BARF( context, 0x21 );
2107 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
2108 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2110 handle = CreateFileW( fileW, GENERIC_READ,
2111 FILE_SHARE_READ | FILE_SHARE_WRITE,
2112 NULL, OPEN_EXISTING, 0, 0 );
2113 if (handle == INVALID_HANDLE_VALUE)
2116 status = GetFileTime( handle, NULL, NULL, &filetime );
2119 FileTimeToDosDateTime( &filetime, &date, &time );
2120 SET_DI( context, date );
2121 SET_CX( context, time );
2124 CloseHandle( handle );
2129 case 0x05: /* SET FILE LAST ACCESS DATE */
2131 INT_BARF( context, 0x21 );
2134 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA );
2135 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2137 handle = CreateFileW( fileW, GENERIC_WRITE,
2138 FILE_SHARE_READ | FILE_SHARE_WRITE,
2139 NULL, OPEN_EXISTING, 0, 0 );
2140 if (handle == INVALID_HANDLE_VALUE)
2143 DosDateTimeToFileTime( DI_reg(context),
2146 status = SetFileTime( handle, NULL, &filetime, NULL );
2148 CloseHandle( handle );
2153 case 0x06: /* GET FILE LAST ACCESS DATE */
2155 INT_BARF( context, 0x21 );
2158 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA );
2159 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2161 handle = CreateFileW( fileW, GENERIC_READ,
2162 FILE_SHARE_READ | FILE_SHARE_WRITE,
2163 NULL, OPEN_EXISTING, 0, 0 );
2164 if (handle == INVALID_HANDLE_VALUE)
2167 status = GetFileTime( handle, NULL, &filetime, NULL );
2170 FileTimeToDosDateTime( &filetime, &date, NULL );
2171 SET_DI( context, date );
2174 CloseHandle( handle );
2179 case 0x07: /* SET FILE CREATION DATE AND TIME */
2181 INT_BARF( context, 0x21 );
2184 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA );
2185 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2187 handle = CreateFileW( fileW, GENERIC_WRITE,
2188 FILE_SHARE_READ | FILE_SHARE_WRITE,
2189 NULL, OPEN_EXISTING, 0, 0 );
2190 if (handle == INVALID_HANDLE_VALUE)
2194 * FIXME: SI has number of 10-millisecond units past time in CX.
2196 DosDateTimeToFileTime( DI_reg(context),
2199 status = SetFileTime( handle, &filetime, NULL, NULL );
2201 CloseHandle( handle );
2206 case 0x08: /* GET FILE CREATION DATE AND TIME */
2208 INT_BARF( context, 0x21 );
2211 TRACE( "GET FILE CREATION DATE AND TIME, file %s\n", fileA );
2212 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2214 handle = CreateFileW( fileW, GENERIC_READ,
2215 FILE_SHARE_READ | FILE_SHARE_WRITE,
2216 NULL, OPEN_EXISTING, 0, 0 );
2217 if (handle == INVALID_HANDLE_VALUE)
2220 status = GetFileTime( handle, &filetime, NULL, NULL );
2223 FileTimeToDosDateTime( &filetime, &date, &time );
2224 SET_DI( context, date );
2225 SET_CX( context, time );
2227 * FIXME: SI has number of 10-millisecond units past
2230 SET_SI( context, 0 );
2233 CloseHandle(handle);
2238 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2239 if (islong || context->Ebp != 0x5053)
2240 INT_BARF( context, 0x21 );
2243 switch(CL_reg(context))
2246 if (!INT21_CreateDirectory( context ))
2251 if (!INT21_RenameFile( context ))
2256 INT_BARF( context, 0x21 );
2262 INT_BARF( context, 0x21 );
2269 /***********************************************************************
2270 * INT21_FileDateTime
2272 * Handler for function 0x57.
2274 static BOOL INT21_FileDateTime( CONTEXT86 *context )
2276 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2280 switch (AL_reg(context)) {
2281 case 0x00: /* Get last-written stamp */
2282 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2285 if (!GetFileTime( handle, NULL, NULL, &filetime ))
2287 FileTimeToDosDateTime( &filetime, &date, &time );
2288 SET_DX( context, date );
2289 SET_CX( context, time );
2293 case 0x01: /* Set last-written stamp */
2294 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2297 DosDateTimeToFileTime( DX_reg(context),
2300 if (!SetFileTime( handle, NULL, NULL, &filetime ))
2305 case 0x04: /* Get last access stamp, DOS 7 */
2306 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2309 if (!GetFileTime( handle, NULL, &filetime, NULL ))
2311 FileTimeToDosDateTime( &filetime, &date, &time );
2312 SET_DX( context, date );
2313 SET_CX( context, time );
2317 case 0x05: /* Set last access stamp, DOS 7 */
2318 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2321 DosDateTimeToFileTime( DX_reg(context),
2324 if (!SetFileTime( handle, NULL, &filetime, NULL ))
2329 case 0x06: /* Get creation stamp, DOS 7 */
2330 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2333 if (!GetFileTime( handle, &filetime, NULL, NULL ))
2335 FileTimeToDosDateTime( &filetime, &date, &time );
2336 SET_DX( context, date );
2337 SET_CX( context, time );
2339 * FIXME: SI has number of 10-millisecond units past time in CX.
2341 SET_SI( context, 0 );
2345 case 0x07: /* Set creation stamp, DOS 7 */
2346 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2350 * FIXME: SI has number of 10-millisecond units past time in CX.
2352 DosDateTimeToFileTime( DX_reg(context),
2355 if (!SetFileTime( handle, &filetime, NULL, NULL ))
2361 INT_BARF( context, 0x21 );
2369 /***********************************************************************
2372 * Handler for functions 0x51 and 0x62.
2374 static void INT21_GetPSP( CONTEXT86 *context )
2376 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
2379 * FIXME: should we return the original DOS PSP upon
2382 if (!ISV86(context) && DOSVM_IsWin16())
2383 SET_BX( context, LOWORD(GetCurrentPDB16()) );
2385 SET_BX( context, DOSVM_psp );
2388 static inline void setword( BYTE *ptr, WORD w )
2391 ptr[1] = (BYTE)(w >> 8);
2394 static void CreateBPB(int drive, BYTE *data, BOOL16 limited)
2395 /* limited == TRUE is used with INT 0x21/0x440d */
2397 /* FIXME: we're forcing some values without checking that those are valid */
2402 setword(&data[3], 0);
2404 setword(&data[6], 240);
2405 setword(&data[8], 64000);
2407 setword(&data[0x0b], 40);
2408 setword(&data[0x0d], 56);
2409 setword(&data[0x0f], 2);
2410 setword(&data[0x11], 0);
2413 setword(&data[0x1f], 800);
2415 setword(&data[0x22], 1);
2422 setword(&data[3], 0);
2424 setword(&data[6], 240);
2425 setword(&data[8], 2880);
2427 setword(&data[0x0b], 6);
2428 setword(&data[0x0d], 18);
2429 setword(&data[0x0f], 2);
2430 setword(&data[0x11], 0);
2433 setword(&data[0x1f], 80);
2435 setword(&data[0x22], 2);
2440 static inline DWORD INT21_Ioctl_CylHeadSect2Lin(DWORD cyl, WORD head, WORD sec, WORD cyl_cnt,
2441 WORD head_cnt, WORD sec_cnt)
2443 DWORD res = (cyl * head_cnt*sec_cnt + head * sec_cnt + sec);
2447 /***********************************************************************
2450 * Handler for block device IOCTLs.
2452 static void INT21_Ioctl_Block( CONTEXT86 *context )
2455 BYTE drive = INT21_MapDrive( BL_reg(context) );
2456 WCHAR drivespec[4] = {'A', ':', '\\', 0};
2459 drivespec[0] += drive;
2460 drivetype = GetDriveTypeW( drivespec );
2462 RESET_CFLAG(context);
2463 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
2465 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2466 AL_reg(context), 'A' + drive );
2467 SetLastError( ERROR_INVALID_DRIVE );
2468 SET_AX( context, ERROR_INVALID_DRIVE );
2469 SET_CFLAG( context );
2473 switch (AL_reg(context))
2475 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2476 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2477 INT_BARF( context, 0x21 );
2480 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2481 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2484 if (drivetype == DRIVE_REMOVABLE)
2485 SET_AX( context, 0 ); /* removable */
2487 SET_AX( context, 1 ); /* not removable */
2490 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2491 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2494 if (drivetype == DRIVE_REMOTE)
2495 SET_DX( context, (1<<9) | (1<<12) ); /* remote + no direct IO */
2497 SET_DX( context, 0 ); /* FIXME: use driver attr here */
2500 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2501 /* Get pointer to IOCTL parameter block */
2502 dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2504 switch (CX_reg(context))
2506 case 0x0841: /* write logical device track */
2507 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2510 WORD head = *(WORD *)(dataptr+1);
2511 WORD cyl = *(WORD *)(dataptr+3);
2512 WORD sect = *(WORD *)(dataptr+5);
2513 WORD nrsect = *(WORD *)(dataptr+7);
2514 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2515 WORD cyl_cnt, head_cnt, sec_cnt;
2517 /* FIXME: we're faking some values here */
2523 SET_AX( context, ERROR_WRITE_FAULT );
2534 if (!DOSVM_RawWrite(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2536 SET_AX( context, ERROR_WRITE_FAULT );
2542 case 0x084a: /* lock logical volume */
2543 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2544 BH_reg(context), DX_reg(context), 'A' + drive );
2547 case 0x0860: /* get device parameters */
2548 /* FIXME: we're faking some values here */
2549 /* used by w4wgrp's winfile */
2550 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2552 dataptr[6] = 0; /* media type */
2555 dataptr[1] = 0x05; /* fixed disk */
2556 setword(&dataptr[2], 0x01); /* non removable */
2557 setword(&dataptr[4], 0x300); /* # of cylinders */
2561 dataptr[1] = 0x07; /* block dev, floppy */
2562 setword(&dataptr[2], 0x02); /* removable */
2563 setword(&dataptr[4], 80); /* # of cylinders */
2565 CreateBPB(drive, &dataptr[7], TRUE);
2566 RESET_CFLAG(context);
2569 case 0x0861: /* read logical device track */
2570 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2573 WORD head = *(WORD *)(dataptr+1);
2574 WORD cyl = *(WORD *)(dataptr+3);
2575 WORD sect = *(WORD *)(dataptr+5);
2576 WORD nrsect = *(WORD *)(dataptr+7);
2577 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2578 WORD cyl_cnt, head_cnt, sec_cnt;
2580 /* FIXME: we're faking some values here */
2594 if (!DOSVM_RawRead(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2596 SET_AX( context, ERROR_READ_FAULT );
2602 case 0x0866: /* get volume serial number */
2604 WCHAR label[12],fsname[9];
2607 drivespec[0] += drive;
2608 GetVolumeInformationW(drivespec, label, 12, &serial, NULL, NULL, fsname, 9);
2609 *(WORD*)dataptr = 0;
2610 memcpy(dataptr+2,&serial,4);
2611 WideCharToMultiByte(CP_OEMCP, 0, label, 11, (LPSTR)dataptr + 6, 11, NULL, NULL);
2612 WideCharToMultiByte(CP_OEMCP, 0, fsname, 8, (LPSTR)dataptr + 17, 8, NULL, NULL);
2616 case 0x086a: /* unlock logical volume */
2617 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2621 case 0x086f: /* get drive map information */
2622 memset(dataptr+1, '\0', dataptr[0]-1);
2623 dataptr[1] = dataptr[0];
2624 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
2625 dataptr[3] = 0xFF; /* no physical drive */
2629 /* Trial and error implementation */
2630 SET_AX( context, drivetype == DRIVE_UNKNOWN ? 0x0f : 0x01 );
2631 SET_CFLAG(context); /* Seems to be set all the time */
2635 INT_BARF( context, 0x21 );
2639 case 0x0e: /* GET LOGICAL DRIVE MAP */
2640 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2642 /* FIXME: this is not correct if drive has mappings */
2643 SET_AL( context, 0 ); /* drive has no mapping */
2646 case 0x0f: /* SET LOGICAL DRIVE MAP */
2647 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2648 INT21_DriveName( BL_reg(context)));
2649 /* FIXME: as of today, we don't support logical drive mapping... */
2650 SET_AL( context, 0 );
2653 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2655 INT_BARF( context, 0x21 );
2660 /***********************************************************************
2661 * INT21_IoctlScsiMgrHandler
2663 * IOCTL handler for the SCSIMGR device.
2665 static void INT21_IoctlScsiMgrHandler( CONTEXT86 *context )
2667 switch (AL_reg(context))
2669 case 0x00: /* GET DEVICE INFORMATION */
2670 SET_DX( context, 0xc0c0 );
2673 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2674 DOSVM_ASPIHandler(context);
2677 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2678 SET_DX( context, 0 );
2681 case 0x01: /* SET DEVICE INFORMATION */
2682 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2683 case 0x06: /* GET INPUT STATUS */
2684 case 0x07: /* GET OUTPUT STATUS */
2685 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2686 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2688 INT_BARF( context, 0x21 );
2694 /***********************************************************************
2695 * INT21_IoctlHPScanHandler
2697 * IOCTL handler for the HPSCAN device.
2699 static void INT21_IoctlHPScanHandler( CONTEXT86 *context )
2701 switch (AL_reg(context))
2703 case 0x00: /* GET DEVICE INFORMATION */
2704 SET_DX( context, 0xc0c0 );
2707 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2708 SET_DX( context, 0 );
2711 case 0x01: /* SET DEVICE INFORMATION */
2712 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2713 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2714 case 0x06: /* GET INPUT STATUS */
2715 case 0x07: /* GET OUTPUT STATUS */
2716 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2717 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2719 INT_BARF( context, 0x21 );
2725 /***********************************************************************
2728 * Handler for character device IOCTLs.
2730 static void INT21_Ioctl_Char( CONTEXT86 *context )
2733 int IsConsoleIOHandle = 0;
2735 FILE_INTERNAL_INFORMATION info;
2736 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2738 status = NtQueryInformationFile( handle, &io, &info, sizeof(info), FileInternalInformation );
2741 if( VerifyConsoleIoHandle( handle))
2742 IsConsoleIOHandle = 1;
2744 SET_AX( context, RtlNtStatusToDosError(status) );
2745 SET_CFLAG( context );
2750 for (i = 0; i < NB_MAGIC_DEVICES; i++)
2752 if (!magic_devices[i].handle) continue;
2753 if (magic_devices[i].index.QuadPart == info.IndexNumber.QuadPart)
2756 magic_devices[i].ioctl_handler( context );
2762 /* no magic device found, do default handling */
2764 switch (AL_reg(context))
2766 case 0x00: /* GET DEVICE INFORMATION */
2767 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context) );
2768 if (IsConsoleIOHandle || GetFileType(handle) == FILE_TYPE_CHAR)
2771 * Returns attribute word in DX:
2772 * Bit 14 - Device driver can process IOCTL requests.
2773 * Bit 13 - Output until busy supported.
2774 * Bit 11 - Driver supports OPEN/CLOSE calls.
2776 * Bit 7 - Set (indicates device).
2777 * Bit 6 - EOF on input.
2778 * Bit 5 - Raw (binary) mode.
2779 * Bit 4 - Device is special (uses int29).
2780 * Bit 3 - Clock device.
2781 * Bit 2 - NUL device.
2782 * Bit 1 - Console output device.
2783 * Bit 0 - Console input device.
2785 SET_DX( context, IsConsoleIOHandle ? 0x80c3 : 0x80c0 /* FIXME */ );
2790 * Returns attribute word in DX:
2791 * Bit 15 - File is remote.
2792 * Bit 14 - Don't set file date/time on closing.
2793 * Bit 11 - Media not removable.
2794 * Bit 8 - Generate int24 if no disk space on write
2795 * or read past end of file
2796 * Bit 7 - Clear (indicates file).
2797 * Bit 6 - File has not been written.
2798 * Bit 5..0 - Drive number (0=A:,...)
2800 * FIXME: Should check if file is on remote or removable drive.
2801 * FIXME: Should use drive file is located on (and not current).
2803 SET_DX( context, 0x0140 + INT21_GetCurrentDrive() );
2807 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2808 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context) );
2810 * Returns attribute word in DX:
2811 * Bit 15 - Set if remote.
2812 * Bit 14 - Set if date/time not set on close.
2814 * FIXME: Should check if file is on remote drive.
2816 SET_DX( context, 0 );
2819 case 0x01: /* SET DEVICE INFORMATION */
2820 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2821 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2822 case 0x06: /* GET INPUT STATUS */
2823 case 0x07: /* GET OUTPUT STATUS */
2824 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2825 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2827 INT_BARF( context, 0x21 );
2833 /***********************************************************************
2836 * Handler for function 0x44.
2838 static void INT21_Ioctl( CONTEXT86 *context )
2840 switch (AL_reg(context))
2846 INT21_Ioctl_Char( context );
2851 INT21_Ioctl_Block( context );
2856 INT21_Ioctl_Char( context );
2861 INT21_Ioctl_Block( context );
2865 INT21_Ioctl_Char( context );
2868 case 0x0b: /* SET SHARING RETRY COUNT */
2869 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2870 CX_reg(context), DX_reg(context) );
2871 if (!CX_reg(context))
2873 SET_AX( context, 1 );
2874 SET_CFLAG( context );
2878 DOSDEV_SetSharingRetry( CX_reg(context), DX_reg(context) );
2879 RESET_CFLAG( context );
2884 INT21_Ioctl_Char( context );
2890 INT21_Ioctl_Block( context );
2894 INT21_Ioctl_Char( context );
2898 INT21_Ioctl_Block( context );
2901 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2902 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2903 SET_CFLAG(context); /* Error / This is not DR DOS. */
2904 SET_AX( context, 0x0001 ); /* Invalid function */
2907 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2908 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2909 SET_CFLAG(context); /* Error / This is not DR DOS. */
2910 SET_AX( context, 0x0001 ); /* Invalid function */
2913 case 0xe0: /* Sun PC-NFS API */
2914 TRACE( "Sun PC-NFS API\n" );
2919 INT_BARF( context, 0x21 );
2924 /***********************************************************************
2927 * Handler for function 0x73.
2929 static BOOL INT21_Fat32( CONTEXT86 *context )
2931 switch (AL_reg(context))
2933 case 0x02: /* FAT32 - GET EXTENDED DPB */
2935 BYTE drive = INT21_MapDrive( DL_reg(context) );
2936 WORD *ptr = CTX_SEG_OFF_TO_LIN(context,
2937 context->SegEs, context->Edi);
2938 INT21_DPB *target = (INT21_DPB*)(ptr + 1);
2941 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context) );
2943 if ( CX_reg(context) < sizeof(INT21_DPB) + 2 || *ptr < sizeof(INT21_DPB) )
2945 SetLastError( ERROR_BAD_LENGTH );
2949 if ( !INT21_FillDrivePB( drive ) )
2951 SetLastError( ERROR_INVALID_DRIVE );
2955 source = &INT21_GetHeapPointer()->misc_dpb_list[drive];
2957 *ptr = sizeof(INT21_DPB);
2960 if (LOWORD(context->Esi) != 0xF1A6)
2962 target->driver_header = 0;
2967 FIXME( "Caller requested driver and next DPB pointers!\n" );
2972 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2974 WCHAR dirW[MAX_PATH];
2975 char *dirA = CTX_SEG_OFF_TO_LIN( context,
2976 context->SegDs, context->Edx );
2977 BYTE *data = CTX_SEG_OFF_TO_LIN( context,
2978 context->SegEs, context->Edi );
2979 DWORD cluster_sectors;
2981 DWORD free_clusters;
2982 DWORD total_clusters;
2984 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA );
2985 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
2987 if (CX_reg(context) < 44)
2989 SetLastError( ERROR_BAD_LENGTH );
2993 if (!GetDiskFreeSpaceW( dirW, &cluster_sectors, §or_bytes,
2994 &free_clusters, &total_clusters ))
2997 *(WORD*) (data + 0) = 44; /* size of structure */
2998 *(WORD*) (data + 2) = 0; /* version */
2999 *(DWORD*)(data + 4) = cluster_sectors;
3000 *(DWORD*)(data + 8) = sector_bytes;
3001 *(DWORD*)(data + 12) = free_clusters;
3002 *(DWORD*)(data + 16) = total_clusters;
3005 * Below we have free/total sectors and
3006 * free/total allocation units without adjustment
3007 * for compression. We fake both using cluster information.
3009 *(DWORD*)(data + 20) = free_clusters * cluster_sectors;
3010 *(DWORD*)(data + 24) = total_clusters * cluster_sectors;
3011 *(DWORD*)(data + 28) = free_clusters;
3012 *(DWORD*)(data + 32) = total_clusters;
3015 * Between (data + 36) and (data + 43) there
3016 * are eight reserved bytes.
3022 INT_BARF( context, 0x21 );
3028 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA *dataA,
3029 const WIN32_FIND_DATAW *dataW)
3031 dataA->dwFileAttributes = dataW->dwFileAttributes;
3032 dataA->ftCreationTime = dataW->ftCreationTime;
3033 dataA->ftLastAccessTime = dataW->ftLastAccessTime;
3034 dataA->ftLastWriteTime = dataW->ftLastWriteTime;
3035 dataA->nFileSizeHigh = dataW->nFileSizeHigh;
3036 dataA->nFileSizeLow = dataW->nFileSizeLow;
3037 WideCharToMultiByte( CP_OEMCP, 0, dataW->cFileName, -1,
3038 dataA->cFileName, sizeof(dataA->cFileName), NULL, NULL );
3039 WideCharToMultiByte( CP_OEMCP, 0, dataW->cAlternateFileName, -1,
3040 dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName), NULL, NULL );
3043 /***********************************************************************
3044 * INT21_LongFilename
3046 * Handler for function 0x71.
3048 static void INT21_LongFilename( CONTEXT86 *context )
3050 BOOL bSetDOSExtendedError = FALSE;
3051 WCHAR pathW[MAX_PATH];
3054 switch (AL_reg(context))
3056 case 0x0d: /* RESET DRIVE */
3057 INT_BARF( context, 0x21 );
3060 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
3061 if (!INT21_CreateDirectory( context ))
3062 bSetDOSExtendedError = TRUE;
3065 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
3066 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3068 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA);
3069 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3070 if (!RemoveDirectoryW( pathW )) bSetDOSExtendedError = TRUE;
3073 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
3074 if (!INT21_SetCurrentDirectory( context ))
3075 bSetDOSExtendedError = TRUE;
3078 case 0x41: /* LONG FILENAME - DELETE FILE */
3079 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3081 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA );
3082 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3084 if (!DeleteFileW( pathW )) bSetDOSExtendedError = TRUE;
3087 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
3088 if (!INT21_FileAttributes( context, BL_reg(context), TRUE ))
3089 bSetDOSExtendedError = TRUE;
3092 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
3093 if (!INT21_GetCurrentDirectory( context, TRUE ))
3094 bSetDOSExtendedError = TRUE;
3097 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
3101 WIN32_FIND_DATAW dataW;
3102 WIN32_FIND_DATAA* dataA;
3104 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
3105 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA);
3107 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3108 handle = FindFirstFileW(pathW, &dataW);
3110 dataA = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
3111 if (handle != INVALID_HANDLE_VALUE &&
3112 (h16 = GlobalAlloc16(GMEM_MOVEABLE, sizeof(handle))))
3114 HANDLE* ptr = GlobalLock16( h16 );
3116 GlobalUnlock16( h16 );
3117 SET_AX( context, h16 );
3118 INT21_ConvertFindDataWtoA(dataA, &dataW);
3122 if (handle != INVALID_HANDLE_VALUE) FindClose(handle);
3123 SET_AX( context, INVALID_HANDLE_VALUE16);
3124 bSetDOSExtendedError = TRUE;
3129 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
3131 HGLOBAL16 h16 = BX_reg(context);
3133 WIN32_FIND_DATAW dataW;
3134 WIN32_FIND_DATAA* dataA;
3136 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
3139 dataA = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
3141 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
3143 if (!FindNextFileW(*ptr, &dataW)) bSetDOSExtendedError = TRUE;
3144 else INT21_ConvertFindDataWtoA(dataA, &dataW);
3145 GlobalUnlock16( h16 );
3149 SetLastError( ERROR_INVALID_HANDLE );
3150 bSetDOSExtendedError = TRUE;
3155 case 0x56: /* LONG FILENAME - RENAME FILE */
3156 if (!INT21_RenameFile(context))
3157 bSetDOSExtendedError = TRUE;
3160 case 0x60: /* LONG FILENAME - CONVERT PATH */
3162 WCHAR res[MAX_PATH];
3164 switch (CL_reg(context))
3166 case 0x00: /* "truename" - Canonicalize path */
3168 * FIXME: This is not 100% equal to 0x01 case,
3169 * if you fix this, fix int21 subfunction 0x60, too.
3172 case 0x01: /* Get short filename or path */
3173 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
3174 if (!GetShortPathNameW(pathW, res, 67))
3175 bSetDOSExtendedError = TRUE;
3178 SET_AX( context, 0 );
3179 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
3180 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3185 case 0x02: /* Get canonical long filename or path */
3186 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
3187 if (!GetFullPathNameW(pathW, 128, res, NULL))
3188 bSetDOSExtendedError = TRUE;
3191 SET_AX( context, 0 );
3192 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
3193 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3198 FIXME("Unimplemented long file name function:\n");
3199 INT_BARF( context, 0x21 );
3201 SET_AL( context, 0 );
3207 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
3208 if (!INT21_CreateFile( context, context->Esi, TRUE,
3209 BX_reg(context), DL_reg(context) ))
3210 bSetDOSExtendedError = TRUE;
3213 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
3215 DWORD filename_len, flags;
3218 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
3220 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA);
3221 SET_AX( context, 0 );
3222 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3223 if (!GetVolumeInformationW( pathW, NULL, 0, NULL, &filename_len,
3226 INT_BARF( context, 0x21 );
3230 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
3231 SET_CX( context, filename_len );
3232 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
3233 WideCharToMultiByte(CP_OEMCP, 0, dstW, -1,
3234 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3239 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
3241 HGLOBAL16 h16 = BX_reg(context);
3244 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
3246 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
3248 if (!FindClose( *ptr )) bSetDOSExtendedError = TRUE;
3249 GlobalUnlock16( h16 );
3250 GlobalFree16( h16 );
3254 SetLastError( ERROR_INVALID_HANDLE );
3255 bSetDOSExtendedError = TRUE;
3260 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
3262 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
3263 BY_HANDLE_FILE_INFORMATION *info =
3264 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3266 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
3268 if (!GetFileInformationByHandle(handle, info))
3269 bSetDOSExtendedError = TRUE;
3273 case 0xa7: /* LONG FILENAME - CONVERT TIME */
3274 switch (BL_reg(context))
3276 case 0x00: /* FILE TIME TO DOS TIME */
3279 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3283 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
3285 FileTimeToDosDateTime( filetime, &date, &time );
3287 SET_DX( context, date );
3288 SET_CX( context, time );
3291 * FIXME: BH has number of 10-millisecond units
3294 SET_BH( context, 0 );
3298 case 0x01: /* DOS TIME TO FILE TIME */
3300 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3304 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
3307 * FIXME: BH has number of 10-millisecond units
3310 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
3316 INT_BARF( context, 0x21 );
3321 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
3322 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3323 case 0xaa: /* LONG FILENAME - SUBST */
3325 FIXME("Unimplemented long file name function:\n");
3326 INT_BARF( context, 0x21 );
3328 SET_AL( context, 0 );
3332 if (bSetDOSExtendedError)
3334 SET_AX( context, GetLastError() );
3335 SET_CFLAG( context );
3340 /***********************************************************************
3345 * - subfunction 0x56 of function 0x71
3346 * - subfunction 0xff of function 0x43 (CL == 0x56)
3348 static BOOL INT21_RenameFile( CONTEXT86 *context )
3350 WCHAR fromW[MAX_PATH];
3351 WCHAR toW[MAX_PATH];
3352 char *fromA = CTX_SEG_OFF_TO_LIN(context,
3353 context->SegDs,context->Edx);
3354 char *toA = CTX_SEG_OFF_TO_LIN(context,
3355 context->SegEs,context->Edi);
3357 TRACE( "RENAME FILE %s to %s\n", fromA, toA );
3358 MultiByteToWideChar(CP_OEMCP, 0, fromA, -1, fromW, MAX_PATH);
3359 MultiByteToWideChar(CP_OEMCP, 0, toA, -1, toW, MAX_PATH);
3361 return MoveFileW( fromW, toW );
3365 /***********************************************************************
3371 static BOOL INT21_NetworkFunc (CONTEXT86 *context)
3373 switch (AL_reg(context))
3375 case 0x00: /* Get machine name. */
3377 WCHAR dstW[MAX_COMPUTERNAME_LENGTH + 1];
3378 DWORD s = sizeof(dstW) / sizeof(WCHAR);
3381 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
3382 TRACE("getting machine name to %p\n", dst);
3383 if (!GetComputerNameW(dstW, &s) ||
3384 !WideCharToMultiByte(CP_OEMCP, 0, dstW, -1, dst, 16, NULL, NULL))
3387 SetLastError( ER_NoNetwork );
3390 for (len = strlen(dst); len < 15; len++) dst[len] = ' ';
3392 SET_CH( context, 1 ); /* Valid */
3393 SET_CL( context, 1 ); /* NETbios number??? */
3394 TRACE("returning %s\n", debugstr_an(dst, 16));
3399 SetLastError( ER_NoNetwork );
3404 /******************************************************************
3405 * INT21_GetDiskSerialNumber
3408 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
3410 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3411 WCHAR path[] = {'A',':','\\',0}, label[11];
3414 path[0] += INT21_MapDrive(BL_reg(context));
3415 if (!GetVolumeInformationW( path, label, 11, &serial, NULL, NULL, NULL, 0))
3417 SetLastError( ERROR_INVALID_DRIVE );
3421 *(WORD *)dataptr = 0;
3422 memcpy(dataptr + 2, &serial, sizeof(DWORD));
3423 WideCharToMultiByte(CP_OEMCP, 0, label, 11, (LPSTR)dataptr + 6, 11, NULL, NULL);
3424 memcpy(dataptr + 17, "FAT16 ", 8);
3429 /******************************************************************
3430 * INT21_SetDiskSerialNumber
3433 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
3436 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3437 int drive = INT21_MapDrive(BL_reg(context));
3439 if (!is_valid_drive(drive))
3441 SetLastError( ERROR_INVALID_DRIVE );
3445 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
3448 FIXME("Setting drive serial number is no longer supported\n");
3449 SetLastError( ERROR_NOT_SUPPORTED );
3455 /******************************************************************
3456 * INT21_GetFreeDiskSpace
3459 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
3461 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
3462 WCHAR root[] = {'A',':','\\',0};
3463 const DWORD max_clusters = 0x3d83;
3464 const DWORD max_sectors_per_cluster = 0x7f;
3465 const DWORD max_bytes_per_sector = 0x200;
3467 root[0] += INT21_MapDrive(DL_reg(context));
3468 if (!GetDiskFreeSpaceW( root, &cluster_sectors, §or_bytes,
3469 &free_clusters, &total_clusters )) return 0;
3471 /* Some old win31 apps (Lotus SmartSuite 5.1) crap out if there's too
3472 * much disk space, so Windows XP seems to apply the following limits:
3473 * cluster_sectors <= 0x7f
3474 * sector size <= 0x200
3475 * clusters <= 0x3D83
3476 * This means total reported space is limited to about 1GB.
3479 /* Make sure bytes-per-sector is in [, max] */
3480 while (sector_bytes > max_bytes_per_sector) {
3482 free_clusters <<= 1;
3483 total_clusters <<= 1;
3485 /* Then make sure sectors-per-cluster is in [max/2, max]. */
3486 while (cluster_sectors <= max_sectors_per_cluster/2) {
3487 cluster_sectors <<= 1;
3488 free_clusters >>= 1;
3489 total_clusters >>= 1;
3491 while (cluster_sectors > max_sectors_per_cluster) {
3492 cluster_sectors >>= 1;
3493 free_clusters <<= 1;
3494 total_clusters <<= 1;
3497 /* scale up sectors_per_cluster to exactly max_sectors_per_cluster.
3498 * We could skip this, but that would impose an artificially low
3499 * limit on reported disk space.
3500 * To avoid overflow, first apply a preliminary cap on sector count;
3501 * this will not affect the correctness of the final result,
3502 * because if the preliminary cap hits, the final one will, too.
3504 if (total_clusters > 4 * max_clusters)
3505 total_clusters = 4 * max_clusters;
3506 if (free_clusters > 4 * max_clusters)
3507 free_clusters = 4 * max_clusters;
3508 if (cluster_sectors < max_sectors_per_cluster) {
3509 free_clusters *= cluster_sectors;
3510 free_clusters /= max_sectors_per_cluster;
3511 total_clusters *= cluster_sectors;
3512 total_clusters /= max_sectors_per_cluster;
3513 cluster_sectors = max_sectors_per_cluster;
3516 /* Finally, apply real cluster count cap. */
3517 if (total_clusters > max_clusters)
3518 total_clusters = max_clusters;
3519 if (free_clusters > max_clusters)
3520 free_clusters = max_clusters;
3522 SET_AX( context, cluster_sectors );
3523 SET_BX( context, free_clusters );
3524 SET_CX( context, sector_bytes );
3525 SET_DX( context, total_clusters );
3529 /******************************************************************
3530 * INT21_GetDriveAllocInfo
3533 static int INT21_GetDriveAllocInfo( CONTEXT86 *context, BYTE drive )
3537 drive = INT21_MapDrive( drive );
3538 if (!INT21_FillDrivePB( drive )) return 0;
3539 dpb = &(INT21_GetHeapPointer()->misc_dpb_list[drive]);
3540 SET_AL( context, dpb->cluster_sectors + 1 );
3541 SET_CX( context, dpb->sector_bytes );
3542 SET_DX( context, dpb->num_clusters1 );
3544 context->SegDs = INT21_GetHeapSelector( context );
3545 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive].media_ID ) );
3549 /***********************************************************************
3550 * INT21_GetExtendedError
3552 static void INT21_GetExtendedError( CONTEXT86 *context )
3554 BYTE class, action, locus;
3555 WORD error = GetLastError();
3560 class = action = locus = 0;
3562 case ERROR_DIR_NOT_EMPTY:
3567 case ERROR_ACCESS_DENIED:
3568 class = EC_AccessDenied;
3572 case ERROR_CANNOT_MAKE:
3573 class = EC_AccessDenied;
3577 case ERROR_DISK_FULL:
3578 case ERROR_HANDLE_DISK_FULL:
3579 class = EC_MediaError;
3583 case ERROR_FILE_EXISTS:
3584 case ERROR_ALREADY_EXISTS:
3589 case ERROR_FILE_NOT_FOUND:
3590 class = EC_NotFound;
3594 case ERROR_GEN_FAILURE:
3595 class = EC_SystemFailure;
3599 case ERROR_INVALID_DRIVE:
3600 class = EC_MediaError;
3604 case ERROR_INVALID_HANDLE:
3605 class = EC_ProgramError;
3609 case ERROR_LOCK_VIOLATION:
3610 class = EC_AccessDenied;
3614 case ERROR_NO_MORE_FILES:
3615 class = EC_MediaError;
3620 class = EC_NotFound;
3624 case ERROR_NOT_ENOUGH_MEMORY:
3625 class = EC_OutOfResource;
3629 case ERROR_PATH_NOT_FOUND:
3630 class = EC_NotFound;
3635 class = EC_NotFound;
3639 case ERROR_SHARING_VIOLATION:
3640 class = EC_Temporary;
3644 case ERROR_TOO_MANY_OPEN_FILES:
3645 class = EC_ProgramError;
3650 FIXME("Unknown error %d\n", error );
3651 class = EC_SystemFailure;
3656 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3657 error, class, action, locus );
3658 SET_AX( context, error );
3659 SET_BH( context, class );
3660 SET_BL( context, action );
3661 SET_CH( context, locus );
3664 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
3666 static int counter = 0;
3667 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
3668 char *p = name + strlen(name);
3670 /* despite what Ralf Brown says, some programs seem to call without
3671 * ending backslash (DOS accepts that, so we accept it too) */
3672 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
3676 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
3677 counter = (counter + 1) % 1000;
3680 Win32HandleToDosFileHandle(
3681 CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
3682 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
3683 CREATE_NEW, 0, 0 ) ) );
3684 if (AX_reg(context) != HFILE_ERROR16)
3686 TRACE("created %s\n", name );
3689 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
3693 /***********************************************************************
3694 * DOSFS_ToDosFCBFormat
3696 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3697 * expanding wild cards and converting to upper-case in the process.
3698 * File name can be terminated by '\0', '\\' or '/'.
3699 * Return FALSE if the name is not a valid DOS name.
3700 * 'buffer' must be at least 12 characters long.
3702 /* Chars we don't want to see in DOS file names */
3703 static BOOL INT21_ToDosFCBFormat( LPCWSTR name, LPWSTR buffer )
3705 static const WCHAR invalid_chars[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3709 /* Check for "." and ".." */
3714 for(i = 1; i < 11; i++) buffer[i] = ' ';
3721 return (!*p || (*p == '/') || (*p == '\\'));
3724 for (i = 0; i < 8; i++)
3741 if (strchrW( invalid_chars, *p )) return FALSE;
3742 buffer[i] = toupperW(*p);
3750 /* Skip all chars after wildcard up to first dot */
3751 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
3755 /* Check if name too long */
3756 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return FALSE;
3758 if (*p == '.') p++; /* Skip dot */
3760 for (i = 8; i < 11; i++)
3770 return FALSE; /* Second extension not allowed */
3778 if (strchrW( invalid_chars, *p )) return FALSE;
3779 buffer[i] = toupperW(*p);
3786 /* at most 3 character of the extension are processed
3787 * is something behind this ?
3789 while (*p == '*' || *p == ' ') p++; /* skip wildcards and spaces */
3790 return (!*p || (*p == '/') || (*p == '\\'));
3793 static HANDLE INT21_FindHandle;
3794 static const WCHAR *INT21_FindPath; /* will point to current dta->fullPath search */
3796 /******************************************************************
3799 static int INT21_FindFirst( CONTEXT86 *context )
3803 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3804 WCHAR maskW[12], pathW[MAX_PATH];
3805 static const WCHAR wildcardW[] = {'*','.','*',0};
3807 path = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3808 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
3810 p = strrchrW( pathW, '\\');
3811 q = strrchrW( pathW, '/');
3815 if (pathW[0] && pathW[1] == ':') p = pathW + 2;
3820 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3821 * (doesn't matter as it is set below anyway)
3823 if (!INT21_ToDosFCBFormat( p, maskW ))
3825 SetLastError( ERROR_FILE_NOT_FOUND );
3826 SET_AX( context, ERROR_FILE_NOT_FOUND );
3830 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
3832 dta->fullPath = HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW) + (p - pathW)*sizeof(WCHAR) );
3833 memcpy( dta->fullPath, pathW, (p - pathW) * sizeof(WCHAR) );
3834 memcpy( dta->fullPath + (p - pathW), wildcardW, sizeof(wildcardW) );
3835 /* we must have a fully qualified file name in dta->fullPath
3836 * (we could have a UNC path, but this would lead to some errors later on)
3838 dta->drive = toupperW(dta->fullPath[0]) - 'A';
3840 dta->search_attr = CL_reg(context);
3844 /******************************************************************
3847 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3849 static BOOL match_short(LPCWSTR shortW, LPCSTR maskA)
3851 WCHAR mask[11], file[12];
3854 if (!INT21_ToDosFCBFormat( shortW, file )) return FALSE;
3855 MultiByteToWideChar(CP_OEMCP, 0, maskA, 11, mask, 11);
3856 for (i = 0; i < 11; i++)
3857 if (mask[i] != '?' && mask[i] != file[i]) return FALSE;
3861 static unsigned INT21_FindHelper(LPCWSTR fullPath, unsigned drive, unsigned count,
3862 LPCSTR mask, unsigned search_attr,
3863 WIN32_FIND_DATAW* entry)
3867 if ((search_attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
3869 WCHAR path[] = {' ',':','\\',0};
3871 if (count) return 0;
3872 path[0] = drive + 'A';
3873 if (!GetVolumeInformationW(path, entry->cAlternateFileName, 13, NULL, NULL, NULL, NULL, 0)) return 0;
3874 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftCreationTime );
3875 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftLastAccessTime );
3876 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftLastWriteTime );
3877 entry->dwFileAttributes = FA_LABEL;
3878 entry->nFileSizeHigh = entry->nFileSizeLow = 0;
3879 TRACE("returning %s as label\n", debugstr_w(entry->cAlternateFileName));
3883 if (!INT21_FindHandle || INT21_FindPath != fullPath || count == 0)
3885 if (INT21_FindHandle) FindClose(INT21_FindHandle);
3886 INT21_FindHandle = FindFirstFileW(fullPath, entry);
3887 if (INT21_FindHandle == INVALID_HANDLE_VALUE)
3889 INT21_FindHandle = 0;
3892 INT21_FindPath = fullPath;
3893 /* we need to resync search */
3898 while (ncalls-- != 0)
3900 if (!FindNextFileW(INT21_FindHandle, entry))
3902 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3906 while (count < 0xffff)
3909 /* Check the file attributes, and path */
3910 if (!(entry->dwFileAttributes & ~search_attr) &&
3911 match_short(entry->cAlternateFileName[0] ? entry->cAlternateFileName : entry->cFileName,
3916 if (!FindNextFileW(INT21_FindHandle, entry))
3918 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3922 WARN("Too many directory entries in %s\n", debugstr_w(fullPath) );
3926 /******************************************************************
3929 static int INT21_FindNext( CONTEXT86 *context )
3931 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3932 DWORD attr = dta->search_attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
3933 WIN32_FIND_DATAW entry;
3936 if (!dta->fullPath) return 0;
3938 n = INT21_FindHelper(dta->fullPath, dta->drive, dta->count,
3939 dta->mask, attr, &entry);
3942 dta->fileattr = entry.dwFileAttributes;
3943 dta->filesize = entry.nFileSizeLow;
3944 FileTimeToDosDateTime( &entry.ftLastWriteTime, &dta->filedate, &dta->filetime );
3945 if (entry.cAlternateFileName[0])
3946 WideCharToMultiByte(CP_OEMCP, 0, entry.cAlternateFileName, -1,
3947 dta->filename, 13, NULL, NULL);
3949 WideCharToMultiByte(CP_OEMCP, 0, entry.cFileName, -1, dta->filename, 13, NULL, NULL);
3951 if (!memchr(dta->mask,'?',11))
3953 /* wildcardless search, release resources in case no findnext will
3954 * be issued, and as a workaround in case file creation messes up
3955 * findnext, as sometimes happens with pkunzip
3957 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3958 INT21_FindPath = dta->fullPath = NULL;
3963 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3964 INT21_FindPath = dta->fullPath = NULL;
3968 /* microsoft's programmers should be shot for using CP/M style int21
3969 calls in Windows for Workgroup's winfile.exe */
3971 /******************************************************************
3972 * INT21_FindFirstFCB
3975 static int INT21_FindFirstFCB( CONTEXT86 *context )
3977 BYTE *fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3980 WCHAR p[] = {' ',':',};
3982 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
3983 else pFCB = (FINDFILE_FCB *)fcb;
3984 drive = INT21_MapDrive( pFCB->drive );
3985 if (drive == MAX_DOS_DRIVES) return 0;
3988 pFCB->fullPath = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
3989 if (!pFCB->fullPath) return 0;
3990 GetLongPathNameW(p, pFCB->fullPath, MAX_PATH);
3995 /******************************************************************
3999 static int INT21_FindNextFCB( CONTEXT86 *context )
4001 BYTE *fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4003 LPBYTE pResult = INT21_GetCurrentDTA(context);
4004 DOS_DIRENTRY_LAYOUT *ddl;
4005 WIN32_FIND_DATAW entry;
4010 if (*fcb == 0xff) /* extended FCB ? */
4013 pFCB = (FINDFILE_FCB *)(fcb + 7);
4018 pFCB = (FINDFILE_FCB *)fcb;
4021 if (!pFCB->fullPath) return 0;
4022 n = INT21_FindHelper(pFCB->fullPath, INT21_MapDrive( pFCB->drive ),
4023 pFCB->count, pFCB->filename, attr, &entry);
4026 HeapFree( GetProcessHeap(), 0, pFCB->fullPath );
4027 INT21_FindPath = pFCB->fullPath = NULL;
4034 /* place extended FCB header before pResult if called with extended FCB */
4036 pResult += 6; /* leave reserved field behind */
4037 *pResult++ = entry.dwFileAttributes;
4039 *pResult++ = INT21_MapDrive( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
4040 ddl = (DOS_DIRENTRY_LAYOUT*)pResult;
4041 ddl->fileattr = entry.dwFileAttributes;
4042 ddl->cluster = 0; /* what else? */
4043 ddl->filesize = entry.nFileSizeLow;
4044 memset( ddl->reserved, 0, sizeof(ddl->reserved) );
4045 FileTimeToDosDateTime( &entry.ftLastWriteTime,
4046 &ddl->filedate, &ddl->filetime );
4048 /* Convert file name to FCB format */
4049 if (entry.cAlternateFileName[0])
4050 INT21_ToDosFCBFormat( entry.cAlternateFileName, nameW );
4052 INT21_ToDosFCBFormat( entry.cFileName, nameW );
4053 WideCharToMultiByte(CP_OEMCP, 0, nameW, 11, ddl->filename, 11, NULL, NULL);
4058 /******************************************************************
4059 * INT21_ParseFileNameIntoFCB
4062 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
4065 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
4067 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
4071 INT buffer_len, len;
4073 SET_AL( context, 0xff ); /* failed */
4075 TRACE("filename: '%s'\n", filename);
4078 while (*s && (*s != ' ') && (*s != '\r') && (*s != '\n'))
4082 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
4083 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
4084 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
4086 INT21_ToDosFCBFormat(buffer, fcbW);
4087 HeapFree(GetProcessHeap(), 0, buffer);
4088 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
4090 TRACE("FCB: '%s'\n", fcb + 1);
4092 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
4094 /* point DS:SI to first unparsed character */
4095 SET_SI( context, context->Esi + (int)s - (int)filename );
4098 static BOOL INT21_Dup2(HFILE16 hFile1, HFILE16 hFile2)
4100 HFILE16 res = HFILE_ERROR16;
4101 HANDLE handle, new_handle;
4102 #define DOS_TABLE_SIZE 256
4103 DWORD map[DOS_TABLE_SIZE / 32];
4106 handle = DosFileHandleToWin32Handle(hFile1);
4107 if (handle == INVALID_HANDLE_VALUE)
4111 /* now loop to allocate the same one... */
4112 memset(map, 0, sizeof(map));
4113 for (i = 0; i < DOS_TABLE_SIZE; i++)
4115 if (!DuplicateHandle(GetCurrentProcess(), handle,
4116 GetCurrentProcess(), &new_handle,
4117 0, FALSE, DUPLICATE_SAME_ACCESS))
4119 res = HFILE_ERROR16;
4122 res = Win32HandleToDosFileHandle(new_handle);
4123 if (res == HFILE_ERROR16 || res == hFile2) break;
4124 map[res / 32] |= 1 << (res % 32);
4126 /* clean up the allocated slots */
4127 for (i = 0; i < DOS_TABLE_SIZE; i++)
4129 if (map[i / 32] & (1 << (i % 32)))
4130 _lclose16((HFILE16)i);
4132 return res == hFile2;
4136 /***********************************************************************
4137 * DOSVM_Int21Handler
4139 * Interrupt 0x21 handler.
4141 void WINAPI DOSVM_Int21Handler( CONTEXT86 *context )
4143 BOOL bSetDOSExtendedError = FALSE;
4145 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
4146 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
4147 AX_reg(context), BX_reg(context),
4148 CX_reg(context), DX_reg(context),
4149 SI_reg(context), DI_reg(context),
4150 (WORD)context->SegDs, (WORD)context->SegEs,
4154 * Extended error is used by (at least) functions 0x2f to 0x62.
4155 * Function 0x59 returns extended error and error should not
4156 * be cleared before handling the function.
4158 if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59)
4161 RESET_CFLAG(context); /* Not sure if this is a good idea. */
4163 switch(AH_reg(context))
4165 case 0x00: /* TERMINATE PROGRAM */
4166 TRACE("TERMINATE PROGRAM\n");
4167 if (DOSVM_IsWin16())
4169 else if(ISV86(context))
4170 MZ_Exit( context, FALSE, 0 );
4172 ERR( "Called from DOS protected mode\n" );
4175 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
4178 TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
4179 INT21_ReadChar( &ascii, context );
4180 SET_AL( context, ascii );
4182 * FIXME: What to echo when extended keycodes are read?
4184 DOSVM_PutChar(AL_reg(context));
4188 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
4189 TRACE("Write Character to Standard Output\n");
4190 DOSVM_PutChar(DL_reg(context));
4193 case 0x03: /* READ CHARACTER FROM STDAUX */
4194 case 0x04: /* WRITE CHARACTER TO STDAUX */
4195 case 0x05: /* WRITE CHARACTER TO PRINTER */
4196 INT_BARF( context, 0x21 );
4199 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
4200 if (DL_reg(context) == 0xff)
4202 TRACE("Direct Console Input\n");
4204 if (INT21_ReadChar( NULL, NULL ))
4207 INT21_ReadChar( &ascii, context );
4208 SET_AL( context, ascii );
4209 RESET_ZFLAG( context );
4213 /* no character available */
4214 SET_AL( context, 0 );
4215 SET_ZFLAG( context );
4220 TRACE("Direct Console Output\n");
4221 DOSVM_PutChar(DL_reg(context));
4223 * At least DOS versions 2.1-7.0 return character
4224 * that was written in AL register.
4226 SET_AL( context, DL_reg(context) );
4230 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
4233 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
4234 INT21_ReadChar( &ascii, context );
4235 SET_AL( context, ascii );
4239 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
4242 TRACE("CHARACTER INPUT WITHOUT ECHO\n");
4243 INT21_ReadChar( &ascii, context );
4244 SET_AL( context, ascii );
4248 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
4249 TRACE("WRITE '$'-terminated string from %04X:%04X to stdout\n",
4250 context->SegDs, DX_reg(context) );
4252 LPSTR data = CTX_SEG_OFF_TO_LIN( context,
4253 context->SegDs, context->Edx );
4257 * Do NOT use strchr() to calculate the string length,
4258 * as '\0' is valid string content, too!
4259 * Maybe we should check for non-'$' strings, but DOS doesn't.
4261 while (*p != '$') p++;
4263 if (DOSVM_IsWin16())
4264 WriteFile( DosFileHandleToWin32Handle(1),
4265 data, p - data, &w, NULL );
4267 for(; data != p; data++)
4268 DOSVM_PutChar( *data );
4270 SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
4274 case 0x0a: /* BUFFERED INPUT */
4276 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context,
4281 TRACE( "BUFFERED INPUT (size=%d)\n", ptr[0] );
4284 * FIXME: Some documents state that
4285 * ptr[1] holds number of chars from last input which
4286 * may be recalled on entry, other documents do not mention
4290 TRACE( "Handle old chars in buffer!\n" );
4293 * ptr[0] - capacity (includes terminating CR)
4294 * ptr[1] - characters read (excludes terminating CR)
4296 result = INT21_BufferedInput( context, ptr + 2, ptr[0] );
4298 ptr[1] = (BYTE)result - 1;
4304 case 0x0b: /* GET STDIN STATUS */
4305 TRACE( "GET STDIN STATUS\n" );
4307 if (INT21_ReadChar( NULL, NULL ))
4308 SET_AL( context, 0xff ); /* character available */
4310 SET_AL( context, 0 ); /* no character available */
4314 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
4316 BYTE al = AL_reg(context); /* Input function to execute after flush. */
4318 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
4320 /* FIXME: buffers are not flushed */
4323 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
4324 * int21 function identified by AL will be called.
4326 if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
4328 SET_AH( context, al );
4329 DOSVM_Int21Handler( context );
4334 case 0x0d: /* DISK BUFFER FLUSH */
4335 TRACE("DISK BUFFER FLUSH ignored\n");
4338 case 0x0e: /* SELECT DEFAULT DRIVE */
4339 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context) );
4340 INT21_SetCurrentDrive( DL_reg(context) );
4341 SET_AL( context, MAX_DOS_DRIVES );
4344 case 0x0f: /* OPEN FILE USING FCB */
4345 INT21_OpenFileUsingFCB( context );
4348 case 0x10: /* CLOSE FILE USING FCB */
4349 INT21_CloseFileUsingFCB( context );
4352 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
4353 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
4354 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
4355 if (!INT21_FindFirstFCB(context))
4357 SET_AL( context, 0xff );
4360 /* else fall through */
4362 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
4363 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
4366 case 0x13: /* DELETE FILE USING FCB */
4367 INT_BARF( context, 0x21 );
4370 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
4371 INT21_SequentialReadFromFCB( context );
4374 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
4375 INT21_SequentialWriteToFCB( context );
4378 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
4379 case 0x17: /* RENAME FILE USING FCB */
4380 INT_BARF( context, 0x21 );
4383 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4384 SET_AL( context, 0 );
4387 case 0x19: /* GET CURRENT DEFAULT DRIVE */
4388 SET_AL( context, INT21_GetCurrentDrive() );
4389 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context ) );
4392 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
4393 TRACE( "SET DISK TRANSFER AREA ADDRESS %04X:%04X\n",
4394 context->SegDs, DX_reg(context) );
4396 TDB *task = GlobalLock16( GetCurrentTask() );
4397 task->dta = MAKESEGPTR( context->SegDs, DX_reg(context) );
4401 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4402 if (!INT21_GetDriveAllocInfo(context, 0))
4403 SET_AX( context, 0xffff );
4406 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4407 if (!INT21_GetDriveAllocInfo(context, DL_reg(context)))
4408 SET_AX( context, 0xffff );
4411 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4412 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4413 SET_AL( context, 0 );
4416 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4418 BYTE drive = INT21_MapDrive( 0 );
4419 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4421 if (INT21_FillDrivePB( drive ))
4423 SET_AL( context, 0x00 ); /* success */
4424 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4425 context->SegDs = INT21_GetHeapSelector( context );
4429 SET_AL( context, 0xff ); /* invalid or network drive */
4434 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4435 SET_AL( context, 0 );
4438 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4439 INT21_ReadRandomRecordFromFCB( context );
4442 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4443 INT21_WriteRandomRecordToFCB( context );
4446 case 0x23: /* GET FILE SIZE FOR FCB */
4447 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4448 INT_BARF( context, 0x21 );
4451 case 0x25: /* SET INTERRUPT VECTOR */
4452 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4454 FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
4455 if (!ISV86(context) && DOSVM_IsWin16())
4456 DOSVM_SetPMHandler16( AL_reg(context), ptr );
4458 DOSVM_SetRMHandler( AL_reg(context), ptr );
4462 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4463 INT_BARF( context, 0x21 );
4466 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4467 INT21_RandomBlockReadFromFCB( context );
4470 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4471 INT21_RandomBlockWriteToFCB( context );
4474 case 0x29: /* PARSE FILENAME INTO FCB */
4475 INT21_ParseFileNameIntoFCB(context);
4478 case 0x2a: /* GET SYSTEM DATE */
4479 TRACE( "GET SYSTEM DATE\n" );
4482 GetLocalTime( &systime );
4483 SET_CX( context, systime.wYear );
4484 SET_DH( context, systime.wMonth );
4485 SET_DL( context, systime.wDay );
4486 SET_AL( context, systime.wDayOfWeek );
4490 case 0x2b: /* SET SYSTEM DATE */
4491 TRACE( "SET SYSTEM DATE\n" );
4493 WORD year = CX_reg(context);
4494 BYTE month = DH_reg(context);
4495 BYTE day = DL_reg(context);
4497 if (year >= 1980 && year <= 2099 &&
4498 month >= 1 && month <= 12 &&
4499 day >= 1 && day <= 31)
4501 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4503 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4507 SET_AL( context, 0xff ); /* invalid date */
4508 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4514 case 0x2c: /* GET SYSTEM TIME */
4515 TRACE( "GET SYSTEM TIME\n" );
4518 GetLocalTime( &systime );
4519 SET_CH( context, systime.wHour );
4520 SET_CL( context, systime.wMinute );
4521 SET_DH( context, systime.wSecond );
4522 SET_DL( context, systime.wMilliseconds / 10 );
4526 case 0x2d: /* SET SYSTEM TIME */
4527 if( CH_reg(context) >= 24 || CL_reg(context) >= 60 || DH_reg(context) >= 60 || DL_reg(context) >= 100 ) {
4528 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4529 CH_reg(context), CL_reg(context),
4530 DH_reg(context), DL_reg(context) );
4531 SET_AL( context, 0xFF );
4535 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4536 CH_reg(context), CL_reg(context),
4537 DH_reg(context), DL_reg(context) );
4538 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4542 case 0x2e: /* SET VERIFY FLAG */
4543 TRACE("SET VERIFY FLAG ignored\n");
4544 /* we cannot change the behaviour anyway, so just ignore it */
4547 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4548 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4550 TDB *task = GlobalLock16( GetCurrentTask() );
4551 context->SegEs = SELECTOROF( task->dta );
4552 SET_BX( context, OFFSETOF( task->dta ) );
4556 case 0x30: /* GET DOS VERSION */
4557 TRACE( "GET DOS VERSION - %s requested\n",
4558 (AL_reg(context) == 0x00) ? "OEM number" : "version flag" );
4560 if (AL_reg(context) == 0x00)
4561 SET_BH( context, 0xff ); /* OEM number => undefined */
4563 SET_BH( context, 0x08 ); /* version flag => DOS is in ROM */
4565 SET_AL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4566 SET_AH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4568 SET_BL( context, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4569 SET_CX( context, 0x3456 );
4572 case 0x31: /* TERMINATE AND STAY RESIDENT */
4573 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4576 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4578 BYTE drive = INT21_MapDrive( DL_reg(context) );
4579 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context) );
4581 if (INT21_FillDrivePB( drive ))
4583 SET_AL( context, 0x00 ); /* success */
4584 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4585 context->SegDs = INT21_GetHeapSelector( context );
4589 SET_AL( context, 0xff ); /* invalid or network drive */
4594 case 0x33: /* MULTIPLEXED */
4595 switch (AL_reg(context))
4597 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4598 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4599 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4602 case 0x01: /* SET EXTENDED BREAK STATE */
4603 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4604 DOSCONF_GetConfig()->brk_flag = (DL_reg(context) > 0) ? 1 : 0;
4607 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4608 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4609 /* ugly coding in order to stay reentrant */
4610 if (DL_reg(context))
4612 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4613 DOSCONF_GetConfig()->brk_flag = 1;
4617 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4618 DOSCONF_GetConfig()->brk_flag = 0;
4622 case 0x05: /* GET BOOT DRIVE */
4623 TRACE("GET BOOT DRIVE\n");
4624 SET_DL( context, 3 );
4625 /* c: is Wine's bootdrive (a: is 1)*/
4628 case 0x06: /* GET TRUE VERSION NUMBER */
4629 TRACE("GET TRUE VERSION NUMBER\n");
4630 SET_BL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4631 SET_BH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4632 SET_DL( context, 0x00 ); /* revision */
4633 SET_DH( context, 0x08 ); /* DOS is in ROM */
4637 INT_BARF( context, 0x21 );
4642 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4643 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4644 context->SegEs = INT21_GetHeapSelector( context );
4645 SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
4648 case 0x35: /* GET INTERRUPT VECTOR */
4649 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4652 if (!ISV86(context) && DOSVM_IsWin16())
4653 addr = DOSVM_GetPMHandler16( AL_reg(context) );
4655 addr = DOSVM_GetRMHandler( AL_reg(context) );
4656 context->SegEs = SELECTOROF(addr);
4657 SET_BX( context, OFFSETOF(addr) );
4661 case 0x36: /* GET FREE DISK SPACE */
4662 TRACE("GET FREE DISK SPACE FOR DRIVE %s (limited to about 1GB)\n",
4663 INT21_DriveName( DL_reg(context) ));
4664 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
4667 case 0x37: /* SWITCHAR */
4669 switch (AL_reg(context))
4671 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4672 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4673 SET_AL( context, 0x00 ); /* success*/
4674 SET_DL( context, '/' );
4676 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4677 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4679 SET_AL( context, 0x00 ); /* success*/
4682 INT_BARF( context, 0x21 );
4688 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4689 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4690 if (AL_reg(context))
4692 WORD country = AL_reg(context);
4693 if (country == 0xff)
4694 country = BX_reg(context);
4695 if (country != INT21_GetSystemCountryCode()) {
4696 FIXME( "Requested info on non-default country %04x\n", country );
4701 if(AX_reg(context) != 2 )
4703 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context,
4706 SET_AX( context, INT21_GetSystemCountryCode() );
4707 SET_BX( context, INT21_GetSystemCountryCode() );
4708 RESET_CFLAG(context);
4712 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4713 if (!INT21_CreateDirectory( context ))
4714 bSetDOSExtendedError = TRUE;
4716 RESET_CFLAG(context);
4719 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4721 WCHAR dirW[MAX_PATH];
4722 char *dirA = CTX_SEG_OFF_TO_LIN(context,
4723 context->SegDs, context->Edx);
4725 TRACE( "REMOVE DIRECTORY %s\n", dirA );
4727 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
4729 if (!RemoveDirectoryW( dirW ))
4730 bSetDOSExtendedError = TRUE;
4732 RESET_CFLAG(context);
4736 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4737 if (!INT21_SetCurrentDirectory( context ))
4738 bSetDOSExtendedError = TRUE;
4740 RESET_CFLAG(context);
4743 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4744 if (!INT21_CreateFile( context, context->Edx, FALSE,
4745 OF_READWRITE | OF_SHARE_COMPAT, 0x12 ))
4746 bSetDOSExtendedError = TRUE;
4748 RESET_CFLAG(context);
4751 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4752 if (!INT21_CreateFile( context, context->Edx, FALSE,
4753 AL_reg(context), 0x01 ))
4754 bSetDOSExtendedError = TRUE;
4756 RESET_CFLAG(context);
4759 case 0x3e: /* "CLOSE" - CLOSE FILE */
4760 TRACE( "CLOSE handle %d\n", BX_reg(context) );
4761 if (_lclose16( BX_reg(context) ) == HFILE_ERROR16)
4762 bSetDOSExtendedError = TRUE;
4764 RESET_CFLAG(context);
4767 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4768 TRACE( "READ from %d to %04X:%04X for %d bytes\n",
4775 WORD count = CX_reg(context);
4776 BYTE *buffer = CTX_SEG_OFF_TO_LIN( context,
4780 /* Some programs pass a count larger than the allocated buffer */
4781 if (DOSVM_IsWin16())
4783 DWORD maxcount = GetSelectorLimit16( context->SegDs )
4784 - DX_reg(context) + 1;
4785 if (count > maxcount)
4790 * FIXME: Reading from console (BX=1) in DOS mode
4791 * does not work as it is supposed to work.
4794 RESET_CFLAG(context); /* set if error */
4795 if (!DOSVM_IsWin16() && BX_reg(context) == 0)
4797 result = INT21_BufferedInput( context, buffer, count );
4798 SET_AX( context, (WORD)result );
4800 else if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context)),
4801 buffer, count, &result, NULL ))
4802 SET_AX( context, (WORD)result );
4804 bSetDOSExtendedError = TRUE;
4808 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4809 TRACE( "WRITE from %04X:%04X to handle %d for %d byte\n",
4810 context->SegDs, DX_reg(context),
4811 BX_reg(context), CX_reg(context) );
4813 char *ptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4815 if (!DOSVM_IsWin16() &&
4816 (BX_reg(context) == 1 || BX_reg(context) == 2))
4819 for(i=0; i<CX_reg(context); i++)
4820 DOSVM_PutChar(ptr[i]);
4821 SET_AX(context, CX_reg(context));
4822 RESET_CFLAG(context);
4826 HFILE handle = (HFILE)DosFileHandleToWin32Handle(BX_reg(context));
4827 LONG result = _hwrite( handle, ptr, CX_reg(context) );
4828 if (result == HFILE_ERROR)
4829 bSetDOSExtendedError = TRUE;
4832 SET_AX( context, (WORD)result );
4833 RESET_CFLAG(context);
4839 case 0x41: /* "UNLINK" - DELETE FILE */
4841 WCHAR fileW[MAX_PATH];
4842 char *fileA = CTX_SEG_OFF_TO_LIN(context,
4846 TRACE( "UNLINK %s\n", fileA );
4847 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
4849 if (!DeleteFileW( fileW ))
4850 bSetDOSExtendedError = TRUE;
4852 RESET_CFLAG(context);
4856 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4857 TRACE( "LSEEK handle %d offset %d from %s\n",
4859 MAKELONG( DX_reg(context), CX_reg(context) ),
4860 (AL_reg(context) == 0) ?
4861 "start of file" : ((AL_reg(context) == 1) ?
4862 "current file position" : "end of file") );
4864 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4865 LONG offset = MAKELONG( DX_reg(context), CX_reg(context) );
4866 DWORD status = SetFilePointer( handle, offset,
4867 NULL, AL_reg(context) );
4868 if (status == INVALID_SET_FILE_POINTER)
4869 bSetDOSExtendedError = TRUE;
4872 SET_AX( context, LOWORD(status) );
4873 SET_DX( context, HIWORD(status) );
4874 RESET_CFLAG(context);
4879 case 0x43: /* FILE ATTRIBUTES */
4880 if (!INT21_FileAttributes( context, AL_reg(context), FALSE ))
4881 bSetDOSExtendedError = TRUE;
4883 RESET_CFLAG(context);
4886 case 0x44: /* IOCTL */
4887 INT21_Ioctl( context );
4890 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4891 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context) );
4894 HFILE handle16 = HFILE_ERROR;
4896 if (DuplicateHandle( GetCurrentProcess(),
4897 DosFileHandleToWin32Handle(BX_reg(context)),
4898 GetCurrentProcess(),
4900 0, TRUE, DUPLICATE_SAME_ACCESS ))
4901 handle16 = Win32HandleToDosFileHandle(handle32);
4903 if (handle16 == HFILE_ERROR)
4904 bSetDOSExtendedError = TRUE;
4907 SET_AX( context, handle16 );
4908 RESET_CFLAG(context);
4913 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4914 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4915 BX_reg(context), CX_reg(context) );
4916 if (!INT21_Dup2(BX_reg(context), CX_reg(context)))
4917 bSetDOSExtendedError = TRUE;
4919 RESET_CFLAG(context);
4922 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4923 if (!INT21_GetCurrentDirectory( context, FALSE ))
4924 bSetDOSExtendedError = TRUE;
4926 RESET_CFLAG(context);
4929 case 0x48: /* ALLOCATE MEMORY */
4930 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
4933 DWORD bytes = (DWORD)BX_reg(context) << 4;
4935 if (!ISV86(context) && DOSVM_IsWin16())
4937 DWORD rv = GlobalDOSAlloc16( bytes );
4938 selector = LOWORD( rv );
4941 DOSMEM_AllocBlock( bytes, &selector );
4945 SET_AX( context, selector );
4946 RESET_CFLAG(context);
4951 SET_AX( context, 0x0008 ); /* insufficient memory */
4952 SET_BX( context, DOSMEM_Available() >> 4 );
4957 case 0x49: /* FREE MEMORY */
4958 TRACE( "FREE MEMORY segment %04X\n", context->SegEs );
4962 if (!ISV86(context) && DOSVM_IsWin16())
4964 ok = !GlobalDOSFree16( context->SegEs );
4966 /* If we don't reset ES_reg, we will fail in the relay code */
4971 ok = DOSMEM_FreeBlock( PTR_REAL_TO_LIN(context->SegEs, 0) );
4975 TRACE("FREE MEMORY failed\n");
4977 SET_AX( context, 0x0009 ); /* memory block address invalid */
4982 case 0x4a: /* RESIZE MEMORY BLOCK */
4983 TRACE( "RESIZE MEMORY segment %04X to %d paragraphs\n",
4984 context->SegEs, BX_reg(context) );
4986 DWORD newsize = (DWORD)BX_reg(context) << 4;
4988 if (!ISV86(context) && DOSVM_IsWin16())
4990 FIXME( "Resize memory block - unsupported under Win16\n" );
4995 LPVOID address = (void*)(context->SegEs << 4);
4996 UINT blocksize = DOSMEM_ResizeBlock( address, newsize, FALSE );
4998 RESET_CFLAG(context);
4999 if (blocksize == (UINT)-1)
5001 SET_CFLAG( context );
5002 SET_AX( context, 0x0009 ); /* illegal address */
5004 else if(blocksize != newsize)
5006 SET_CFLAG( context );
5007 SET_AX( context, 0x0008 ); /* insufficient memory */
5008 SET_BX( context, blocksize >> 4 ); /* new block size */
5014 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
5016 char *program = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
5017 BYTE *paramblk = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx);
5019 TRACE( "EXEC %s\n", program );
5021 RESET_CFLAG(context);
5022 if (DOSVM_IsWin16())
5024 HINSTANCE16 instance = WinExec16( program, SW_NORMAL );
5027 SET_CFLAG( context );
5028 SET_AX( context, instance );
5033 if (!MZ_Exec( context, program, AL_reg(context), paramblk))
5034 bSetDOSExtendedError = TRUE;
5039 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
5040 TRACE( "EXIT with return code %d\n", AL_reg(context) );
5041 if (DOSVM_IsWin16())
5042 DOSVM_Exit( AL_reg(context) );
5043 else if(ISV86(context))
5044 MZ_Exit( context, FALSE, AL_reg(context) );
5050 ULONG_PTR rv = AL_reg(context);
5051 RaiseException( EXCEPTION_VM86_INTx, 0, 1, &rv );
5055 case 0x4d: /* GET RETURN CODE */
5056 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
5057 SET_AX( context, DOSVM_retval );
5061 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
5062 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
5063 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
5064 if (!INT21_FindFirst(context)) break;
5067 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
5068 TRACE("FINDNEXT\n");
5069 if (!INT21_FindNext(context))
5071 SetLastError( ERROR_NO_MORE_FILES );
5072 SET_AX( context, ERROR_NO_MORE_FILES );
5075 else SET_AX( context, 0 ); /* OK */
5078 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
5079 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
5080 DOSVM_psp = BX_reg(context);
5083 case 0x51: /* GET PSP ADDRESS */
5084 INT21_GetPSP( context );
5087 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
5089 SEGPTR ptr = DOSDEV_GetLOL( ISV86(context) || !DOSVM_IsWin16() );
5090 context->SegEs = SELECTOROF(ptr);
5091 SET_BX( context, OFFSETOF(ptr) );
5095 case 0x54: /* Get Verify Flag */
5096 TRACE("Get Verify Flag - Not Supported\n");
5097 SET_AL( context, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
5100 case 0x56: /* "RENAME" - RENAME FILE */
5101 if (!INT21_RenameFile( context ))
5102 bSetDOSExtendedError = TRUE;
5104 RESET_CFLAG(context);
5107 case 0x57: /* FILE DATE AND TIME */
5108 if (!INT21_FileDateTime( context ))
5109 bSetDOSExtendedError = TRUE;
5111 RESET_CFLAG(context);
5114 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
5115 switch (AL_reg(context))
5117 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
5118 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
5119 SET_AX( context, 0 ); /* low memory first fit */
5122 case 0x01: /* SET ALLOCATION STRATEGY */
5123 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
5127 case 0x02: /* GET UMB LINK STATE */
5128 TRACE( "GET UMB LINK STATE\n" );
5129 SET_AL( context, 0 ); /* UMBs not part of DOS memory chain */
5132 case 0x03: /* SET UMB LINK STATE */
5133 TRACE( "SET UMB LINK STATE to %d - ignored\n",
5138 INT_BARF( context, 0x21 );
5142 case 0x59: /* GET EXTENDED ERROR INFO */
5143 INT21_GetExtendedError( context );
5146 case 0x5a: /* CREATE TEMPORARY FILE */
5147 TRACE("CREATE TEMPORARY FILE\n");
5148 bSetDOSExtendedError = !INT21_CreateTempFile(context);
5151 case 0x5b: /* CREATE NEW FILE */
5152 if (!INT21_CreateFile( context, context->Edx, FALSE,
5153 OF_READWRITE | OF_SHARE_COMPAT, 0x10 ))
5154 bSetDOSExtendedError = TRUE;
5156 RESET_CFLAG(context);
5159 case 0x5c: /* "FLOCK" - RECORD LOCKING */
5161 DWORD offset = MAKELONG(DX_reg(context), CX_reg(context));
5162 DWORD length = MAKELONG(DI_reg(context), SI_reg(context));
5163 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
5165 RESET_CFLAG(context);
5166 switch (AL_reg(context))
5168 case 0x00: /* LOCK */
5169 TRACE( "lock handle %d offset %d length %d\n",
5170 BX_reg(context), offset, length );
5171 if (!LockFile( handle, offset, 0, length, 0 ))
5172 bSetDOSExtendedError = TRUE;
5175 case 0x01: /* UNLOCK */
5176 TRACE( "unlock handle %d offset %d length %d\n",
5177 BX_reg(context), offset, length );
5178 if (!UnlockFile( handle, offset, 0, length, 0 ))
5179 bSetDOSExtendedError = TRUE;
5183 INT_BARF( context, 0x21 );
5188 case 0x5d: /* NETWORK 5D */
5189 FIXME( "Network function 5D not implemented.\n" );
5190 SetLastError( ER_NoNetwork );
5191 bSetDOSExtendedError = TRUE;
5194 case 0x5e: /* NETWORK 5E */
5195 bSetDOSExtendedError = INT21_NetworkFunc( context);
5198 case 0x5f: /* NETWORK 5F */
5199 /* FIXME: supporting this would need to 1:
5200 * - implement per drive current directory (as kernel32 doesn't)
5201 * - assign enabled/disabled flag on a per drive basis
5203 /* network software not installed */
5204 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
5205 SetLastError( ER_NoNetwork );
5206 bSetDOSExtendedError = TRUE;
5209 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
5211 WCHAR pathW[MAX_PATH], res[MAX_PATH];
5212 /* FIXME: likely to be broken */
5214 TRACE("TRUENAME %s\n",
5215 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
5216 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
5217 if (!GetFullPathNameW( pathW, 128, res, NULL ))
5218 bSetDOSExtendedError = TRUE;
5221 SET_AX( context, 0 );
5222 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
5223 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
5229 case 0x61: /* UNUSED */
5230 SET_AL( context, 0 );
5233 case 0x62: /* GET PSP ADDRESS */
5234 INT21_GetPSP( context );
5237 case 0x63: /* MISC. LANGUAGE SUPPORT */
5238 switch (AL_reg(context)) {
5239 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
5240 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
5241 context->SegDs = INT21_GetHeapSelector( context );
5242 SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
5243 SET_AL( context, 0 ); /* success */
5248 case 0x64: /* OS/2 DOS BOX */
5249 INT_BARF( context, 0x21 );
5253 case 0x65: /* EXTENDED COUNTRY INFORMATION */
5254 INT21_ExtendedCountryInformation( context );
5257 case 0x66: /* GLOBAL CODE PAGE TABLE */
5258 switch (AL_reg(context))
5261 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
5262 SET_BX( context, GetOEMCP() );
5263 SET_DX( context, GetOEMCP() );
5266 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
5267 BX_reg(context), DX_reg(context) );
5272 case 0x67: /* SET HANDLE COUNT */
5273 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
5274 if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
5275 bSetDOSExtendedError = TRUE;
5278 case 0x68: /* "FFLUSH" - COMMIT FILE */
5279 TRACE( "FFLUSH - handle %d\n", BX_reg(context) );
5280 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
5281 bSetDOSExtendedError = TRUE;
5284 case 0x69: /* DISK SERIAL NUMBER */
5285 switch (AL_reg(context))
5288 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
5289 INT21_DriveName(BL_reg(context)));
5290 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
5291 else SET_AX( context, 0 );
5295 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
5296 INT21_DriveName(BL_reg(context)));
5297 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
5298 else SET_AX( context, 1 );
5303 case 0x6a: /* COMMIT FILE */
5304 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context) );
5305 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
5306 bSetDOSExtendedError = TRUE;
5309 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
5310 SET_AL( context, 0 );
5313 case 0x6c: /* EXTENDED OPEN/CREATE */
5314 if (!INT21_CreateFile( context, context->Esi, TRUE,
5315 BX_reg(context), DL_reg(context) ))
5316 bSetDOSExtendedError = TRUE;
5319 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
5320 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
5321 SET_CFLAG( context );
5322 SET_AL( context, 0 );
5325 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
5326 INT21_LongFilename( context );
5329 case 0x73: /* MSDOS7 - FAT32 */
5330 RESET_CFLAG( context );
5331 if (!INT21_Fat32( context ))
5332 bSetDOSExtendedError = TRUE;
5335 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
5336 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
5339 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
5340 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
5343 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
5344 /* we don't implement a DOS32 extender */
5345 TRACE( "DOS32 EXTENDER API - ignored\n" );
5349 INT_BARF( context, 0x21 );
5352 } /* END OF SWITCH */
5354 /* Set general error condition. */
5355 if (bSetDOSExtendedError)
5357 SET_AX( context, GetLastError() );
5358 SET_CFLAG( context );
5361 /* Print error code if carry flag is set. */
5362 if (context->EFlags & 0x0001)
5363 TRACE("failed, error %d\n", GetLastError() );
5365 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
5366 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
5367 AX_reg(context), BX_reg(context),
5368 CX_reg(context), DX_reg(context),
5369 SI_reg(context), DI_reg(context),
5370 (WORD)context->SegDs, (WORD)context->SegEs,