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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/port.h"
40 #include "wine/winbase16.h"
44 #include "wine/unicode.h"
45 #include "wine/server.h"
46 #include "wine/debug.h"
47 #include "wine/exception.h"
51 * - Most of the file related functions are wrong. NT's kernel32
52 * doesn't maintain a per drive current directory, while DOS does.
53 * We should in fact keep track in here of those per driver
54 * directories, and use this infro while dealing with partial paths
55 * (drive defined, but only relative paths). This could even be
56 * created as an array of CDS (there should be an entry for that in
61 * Forward declarations.
63 static BOOL INT21_RenameFile( CONTEXT86 *context );
65 WINE_DEFAULT_DEBUG_CHANNEL(int21);
71 * Extended Drive Parameter Block.
72 * This structure is compatible with standard DOS4+ DPB and
75 typedef struct _INT21_DPB {
76 BYTE drive; /* 00 drive number (0=A, ...) */
77 BYTE unit; /* 01 unit number within device driver */
78 WORD sector_bytes; /* 02 bytes per sector */
79 BYTE cluster_sectors; /* 04 highest sector number within a cluster */
80 BYTE shift; /* 05 shift count to convert clusters into sectors */
81 WORD num_reserved; /* 06 reserved sectors at beginning of drive */
82 BYTE num_FAT; /* 08 number of FATs */
83 WORD num_root_entries; /* 09 number of root directory entries */
84 WORD first_data_sector; /* 0b number of first sector containing user data */
85 WORD num_clusters1; /* 0d highest cluster number (number of data clusters + 1) */
86 WORD sectors_per_FAT; /* 0f number of sectors per FAT */
87 WORD first_dir_sector; /* 11 sector number of first directory sector */
88 SEGPTR driver_header; /* 13 address of device driver header */
89 BYTE media_ID; /* 17 media ID byte */
90 BYTE access_flag; /* 18 0x00 if disk accessed, 0xff if not */
91 SEGPTR next; /* 19 pointer to next DPB */
92 WORD search_cluster1; /* 1d cluster at which to start search for free space */
93 WORD free_clusters_lo; /* 1f number of free clusters on drive or 0xffff if unknown */
94 WORD free_clusters_hi; /* 21 hiword of clusters_free */
95 WORD mirroring_flags; /* 23 active FAT/mirroring flags */
96 WORD info_sector; /* 25 sector number of file system info sector or 0xffff for none */
97 WORD spare_boot_sector; /* 27 sector number of backup boot sector or 0xffff for none */
98 DWORD first_cluster_sector; /* 29 sector number of the first cluster */
99 DWORD num_clusters2; /* 2d maximum cluster number */
100 DWORD fat_clusters; /* 31 number of clusters occupied by FAT */
101 DWORD root_cluster; /* 35 cluster number of start of root directory */
102 DWORD search_cluster2; /* 39 cluster at which to start searching for free space */
107 * Structure for DOS data that can be accessed directly from applications.
108 * Real and protected mode pointers will be returned to this structure so
109 * the structure must be correctly packed.
111 typedef struct _INT21_HEAP {
112 WORD uppercase_size; /* Size of the following table in bytes */
113 BYTE uppercase_table[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
115 WORD lowercase_size; /* Size of the following table in bytes */
116 BYTE lowercase_table[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
118 WORD collating_size; /* Size of the following table in bytes */
119 BYTE collating_table[256]; /* Values used to sort characters from 0x00 to 0xff. */
121 WORD filename_size; /* Size of the following filename data in bytes */
122 BYTE filename_reserved1; /* 0x01 for MS-DOS 3.30-6.00 */
123 BYTE filename_lowest; /* Lowest permissible character value for filename */
124 BYTE filename_highest; /* Highest permissible character value for filename */
125 BYTE filename_reserved2; /* 0x00 for MS-DOS 3.30-6.00 */
126 BYTE filename_exclude_first; /* First illegal character in permissible range */
127 BYTE filename_exclude_last; /* Last illegal character in permissible range */
128 BYTE filename_reserved3; /* 0x02 for MS-DOS 3.30-6.00 */
129 BYTE filename_illegal_size; /* Number of terminators in the following table */
130 BYTE filename_illegal_table[16]; /* Characters which terminate a filename */
132 WORD dbcs_size; /* Number of valid ranges in the following table */
133 BYTE dbcs_table[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
135 BYTE misc_indos; /* Interrupt 21 nesting flag */
136 WORD misc_segment; /* Real mode segment for INT21_HEAP */
137 WORD misc_selector; /* Protected mode selector for INT21_HEAP */
138 INT21_DPB misc_dpb_list[MAX_DOS_DRIVES]; /* Drive parameter blocks for all drives */
146 CHAR file_extension[3];
147 WORD current_block_number;
148 WORD logical_record_size;
150 WORD date_of_last_write;
151 WORD time_of_last_write;
154 WORD starting_cluster;
155 WORD sequence_number;
156 BYTE file_attributes;
158 BYTE record_within_current_block;
159 BYTE random_access_record_number[4];
166 BYTE xfcb_file_attribute;
170 /* DTA layout for FindFirst/FindNext */
173 BYTE drive; /* 00 drive letter */
174 char mask[11]; /* 01 search template */
175 BYTE search_attr; /* 0c search attributes */
176 WORD count; /* 0d entry count within directory */
177 WORD cluster; /* 0f cluster of parent directory */
178 WCHAR *fullPath; /* 11 full path (was: reserved) */
179 BYTE fileattr; /* 15 file attributes */
180 WORD filetime; /* 16 file time */
181 WORD filedate; /* 18 file date */
182 DWORD filesize; /* 1a file size */
183 char filename[13]; /* 1e file name + extension */
186 /* FCB layout for FindFirstFCB/FindNextFCB */
189 BYTE drive; /* 00 drive letter */
190 char filename[11]; /* 01 filename 8+3 format */
191 int count; /* 0c entry count (was: reserved) */
192 WCHAR *fullPath; /* 10 full path (was: reserved) */
195 /* DOS directory entry for FindFirstFCB/FindNextFCB */
198 char filename[11]; /* 00 filename 8+3 format */
199 BYTE fileattr; /* 0b file attributes */
200 BYTE reserved[10]; /* 0c reserved */
201 WORD filetime; /* 16 file time */
202 WORD filedate; /* 18 file date */
203 WORD cluster; /* 1a file first cluster */
204 DWORD filesize; /* 1c file size */
205 } DOS_DIRENTRY_LAYOUT;
209 /* dos file attributes */
210 #define FA_NORMAL 0x00 /* Normal file, no attributes */
211 #define FA_RDONLY 0x01 /* Read only attribute */
212 #define FA_HIDDEN 0x02 /* Hidden file */
213 #define FA_SYSTEM 0x04 /* System file */
214 #define FA_LABEL 0x08 /* Volume label */
215 #define FA_DIRECTORY 0x10 /* Directory */
216 #define FA_ARCHIVE 0x20 /* Archive */
217 #define FA_UNUSED 0x40 /* Unused */
220 #define ER_NoNetwork 0x49
223 #define EC_OutOfResource 0x01
224 #define EC_Temporary 0x02
225 #define EC_AccessDenied 0x03
226 #define EC_InternalError 0x04
227 #define EC_HardwareFailure 0x05
228 #define EC_SystemFailure 0x06
229 #define EC_ProgramError 0x07
230 #define EC_NotFound 0x08
231 #define EC_MediaError 0x0b
232 #define EC_Exists 0x0c
233 #define EC_Unknown 0x0d
235 /* Suggested actions */
236 #define SA_Retry 0x01
237 #define SA_DelayedRetry 0x02
238 #define SA_Abort 0x04
239 #define SA_Ignore 0x06
240 #define SA_Ask4Retry 0x07
243 #define EL_Unknown 0x01
245 #define EL_Network 0x03
246 #define EL_Serial 0x04
247 #define EL_Memory 0x05
256 void (*ioctl_handler)(CONTEXT86 *);
259 static void INT21_IoctlScsiMgrHandler( CONTEXT86 * );
260 static void INT21_IoctlEMSHandler( CONTEXT86 * );
261 static void INT21_IoctlHPScanHandler( CONTEXT86 * );
263 static struct magic_device magic_devices[] =
265 { {'s','c','s','i','m','g','r','$',0}, NULL, 0, 0, INT21_IoctlScsiMgrHandler },
266 { {'e','m','m','x','x','x','x','0',0}, NULL, 0, 0, INT21_IoctlEMSHandler },
267 { {'h','p','s','c','a','n',0}, NULL, 0, 0, INT21_IoctlHPScanHandler },
270 #define NB_MAGIC_DEVICES (sizeof(magic_devices)/sizeof(magic_devices[0]))
273 /* Many calls translate a drive argument like this:
274 drive number (00h = default, 01h = A:, etc)
276 /******************************************************************
279 * Many calls translate a drive argument like this:
280 * drive number (00h = default, 01h = A:, etc)
282 static const char *INT21_DriveName(int drive)
286 if (drive <= 26) return wine_dbg_sprintf("%c:", 'A' + drive - 1);
287 else return wine_dbg_sprintf( "<Bad drive: %d>", drive);
292 /***********************************************************************
293 * INT21_GetCurrentDrive
295 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
296 * MAX_DOS_DRIVES on error.
298 static BYTE INT21_GetCurrentDrive(void)
300 WCHAR current_directory[MAX_PATH];
302 if (!GetCurrentDirectoryW( MAX_PATH, current_directory ) ||
303 current_directory[1] != ':')
305 TRACE( "Failed to get current drive.\n" );
306 return MAX_DOS_DRIVES;
309 return toupperW( current_directory[0] ) - 'A';
313 /***********************************************************************
316 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
317 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
319 static BYTE INT21_MapDrive( BYTE drive )
323 WCHAR drivespec[3] = {'A', ':', 0};
326 drivespec[0] += drive - 1;
327 drivetype = GetDriveTypeW( drivespec );
329 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
330 return MAX_DOS_DRIVES;
335 return INT21_GetCurrentDrive();
339 /***********************************************************************
340 * INT21_SetCurrentDrive
342 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
344 static void INT21_SetCurrentDrive( BYTE drive )
346 WCHAR drivespec[3] = {'A', ':', 0};
348 drivespec[0] += drive;
350 if (!SetCurrentDirectoryW( drivespec ))
351 TRACE( "Failed to set current drive.\n" );
355 /***********************************************************************
358 * Reads a character from the standard input.
359 * Extended keycodes will be returned as two separate characters.
361 static BOOL INT21_ReadChar( BYTE *input, CONTEXT86 *waitctx )
363 static BYTE pending_scan = 0;
368 *input = pending_scan;
377 if (!DOSVM_Int16ReadChar( &ascii, &scan, waitctx ))
382 if (waitctx && !ascii)
389 /***********************************************************************
390 * INT21_GetSystemCountryCode
392 * Return DOS country code for default system locale.
394 static WORD INT21_GetSystemCountryCode( void )
397 * FIXME: Determine country code. We should probably use
398 * DOSCONF structure for that.
400 return GetSystemDefaultLangID();
404 /***********************************************************************
405 * INT21_FillCountryInformation
407 * Fill 34-byte buffer with country information data using
408 * default system locale.
410 static void INT21_FillCountryInformation( BYTE *buffer )
412 /* 00 - WORD: date format
417 *(WORD*)(buffer + 0) = 0; /* FIXME: Get from locale */
419 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
420 buffer[2] = '$'; /* FIXME: Get from locale */
423 /* 07 - BYTE[2]: ASCIIZ thousands separator */
424 buffer[7] = 0; /* FIXME: Get from locale */
427 /* 09 - BYTE[2]: ASCIIZ decimal separator */
428 buffer[9] = '.'; /* FIXME: Get from locale */
431 /* 11 - BYTE[2]: ASCIIZ date separator */
432 buffer[11] = '/'; /* FIXME: Get from locale */
435 /* 13 - BYTE[2]: ASCIIZ time separator */
436 buffer[13] = ':'; /* FIXME: Get from locale */
439 /* 15 - BYTE: Currency format
440 * bit 2 = set if currency symbol replaces decimal point
441 * bit 1 = number of spaces between value and currency symbol
442 * bit 0 = 0 if currency symbol precedes value
443 * 1 if currency symbol follows value
445 buffer[15] = 0; /* FIXME: Get from locale */
447 /* 16 - BYTE: Number of digits after decimal in currency */
448 buffer[16] = 0; /* FIXME: Get from locale */
450 /* 17 - BYTE: Time format
451 * bit 0 = 0 if 12-hour clock
454 buffer[17] = 1; /* FIXME: Get from locale */
456 /* 18 - DWORD: Address of case map routine */
457 *(DWORD*)(buffer + 18) = 0; /* FIXME: ptr to case map routine */
459 /* 22 - BYTE[2]: ASCIIZ data-list separator */
460 buffer[22] = ','; /* FIXME: Get from locale */
463 /* 24 - BYTE[10]: Reserved */
464 memset( buffer + 24, 0, 10 );
468 /***********************************************************************
471 * Initialize DOS heap.
473 static void INT21_FillHeap( INT21_HEAP *heap )
475 static const char terminators[] = "\"\\./[]:|<>+=;,";
481 heap->uppercase_size = 128;
482 for (i = 0; i < 128; i++)
483 heap->uppercase_table[i] = toupper( 128 + i );
488 heap->lowercase_size = 256;
489 for (i = 0; i < 256; i++)
490 heap->lowercase_table[i] = tolower( i );
495 heap->collating_size = 256;
496 for (i = 0; i < 256; i++)
497 heap->collating_table[i] = i;
502 heap->filename_size = 8 + strlen(terminators);
503 heap->filename_illegal_size = strlen(terminators);
504 strcpy( heap->filename_illegal_table, terminators );
506 heap->filename_reserved1 = 0x01;
507 heap->filename_lowest = 0; /* FIXME: correct value? */
508 heap->filename_highest = 0xff; /* FIXME: correct value? */
509 heap->filename_reserved2 = 0x00;
510 heap->filename_exclude_first = 0x00; /* FIXME: correct value? */
511 heap->filename_exclude_last = 0x00; /* FIXME: correct value? */
512 heap->filename_reserved3 = 0x02;
515 * DBCS lead byte table. This table is empty.
518 memset( heap->dbcs_table, 0, sizeof(heap->dbcs_table) );
521 * Initialize InDos flag.
523 heap->misc_indos = 0;
526 * FIXME: Should drive parameter blocks (DPB) be
527 * initialized here and linked to DOS LOL?
532 /***********************************************************************
533 * INT21_GetHeapPointer
535 * Get pointer for DOS heap (INT21_HEAP).
536 * Creates and initializes heap on first call.
538 static INT21_HEAP *INT21_GetHeapPointer( void )
540 static INT21_HEAP *heap_pointer = NULL;
547 heap_pointer = DOSVM_AllocDataUMB( sizeof(INT21_HEAP),
551 heap_pointer->misc_segment = heap_segment;
552 heap_pointer->misc_selector = heap_selector;
553 INT21_FillHeap( heap_pointer );
560 /***********************************************************************
561 * INT21_GetHeapSelector
563 * Get segment/selector for DOS heap (INT21_HEAP).
564 * Creates and initializes heap on first call.
566 static WORD INT21_GetHeapSelector( CONTEXT86 *context )
568 INT21_HEAP *heap = INT21_GetHeapPointer();
570 if (!ISV86(context) && DOSVM_IsWin16())
571 return heap->misc_selector;
573 return heap->misc_segment;
577 /***********************************************************************
580 * Fill DOS heap drive parameter block for the specified drive.
581 * Return TRUE if drive was valid and there were
582 * no errors while reading drive information.
584 static BOOL INT21_FillDrivePB( BYTE drive )
586 WCHAR drivespec[3] = {'A', ':', 0};
587 INT21_HEAP *heap = INT21_GetHeapPointer();
590 DWORD cluster_sectors;
593 DWORD total_clusters;
595 if (drive >= MAX_DOS_DRIVES)
598 dpb = &heap->misc_dpb_list[drive];
599 drivespec[0] += drive;
600 drivetype = GetDriveTypeW( drivespec );
603 * FIXME: Does this check work correctly with floppy/cdrom drives?
605 if (drivetype == DRIVE_NO_ROOT_DIR || drivetype == DRIVE_UNKNOWN)
609 * FIXME: Does this check work correctly with floppy/cdrom drives?
611 if (!GetDiskFreeSpaceW( drivespec, &cluster_sectors, §or_bytes,
612 &free_clusters, &total_clusters ))
616 * FIXME: Most of the values listed below are incorrect.
617 * All values should be validated.
622 dpb->sector_bytes = sector_bytes;
623 dpb->cluster_sectors = cluster_sectors - 1;
626 while (cluster_sectors > 1)
628 cluster_sectors /= 2;
632 dpb->num_reserved = 0;
634 dpb->num_root_entries = 2;
635 dpb->first_data_sector = 2;
636 dpb->num_clusters1 = total_clusters;
637 dpb->sectors_per_FAT = 1;
638 dpb->first_dir_sector = 1;
639 dpb->driver_header = 0;
640 dpb->media_ID = (drivetype == DRIVE_FIXED) ? 0xF8 : 0xF0;
641 dpb->access_flag = 0;
643 dpb->search_cluster1 = 0;
644 dpb->free_clusters_lo = LOWORD(free_clusters);
645 dpb->free_clusters_hi = HIWORD(free_clusters);
646 dpb->mirroring_flags = 0;
647 dpb->info_sector = 0xffff;
648 dpb->spare_boot_sector = 0xffff;
649 dpb->first_cluster_sector = 0;
650 dpb->num_clusters2 = total_clusters;
651 dpb->fat_clusters = 32;
652 dpb->root_cluster = 0;
653 dpb->search_cluster2 = 0;
659 /***********************************************************************
660 * INT21_GetCurrentDirectory
664 * - subfunction 0x47 of function 0x71
666 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context, BOOL islong )
668 char *buffer = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi);
669 BYTE drive = INT21_MapDrive( DL_reg(context) );
670 WCHAR pathW[MAX_PATH];
671 char pathA[MAX_PATH];
674 TRACE( "drive %d\n", DL_reg(context) );
676 if (drive == MAX_DOS_DRIVES)
678 SetLastError(ERROR_INVALID_DRIVE);
683 * Grab current directory.
686 if (!GetCurrentDirectoryW( MAX_PATH, pathW )) return FALSE;
688 if (toupperW(pathW[0]) - 'A' != drive || pathW[1] != ':')
690 /* cwd is not on the requested drive, get the environment string instead */
695 env_var[1] = 'A' + drive;
698 if (!GetEnvironmentVariableW( env_var, pathW, MAX_PATH ))
700 /* return empty path */
707 * Convert into short format.
712 DWORD result = GetShortPathNameW( pathW, pathW, MAX_PATH );
715 if (result > MAX_PATH)
717 WARN( "Short path too long!\n" );
718 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
724 * The returned pathname does not include
725 * the drive letter, colon or leading backslash.
731 * FIXME: We should probably just strip host part from name...
733 FIXME( "UNC names are not supported.\n" );
734 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
737 else if (!ptr[0] || ptr[1] != ':' || ptr[2] != '\\')
739 WARN( "Path is neither UNC nor DOS path: %s\n",
740 wine_dbgstr_w(ptr) );
741 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
746 /* Remove drive letter, colon and leading backslash. */
751 * Convert into OEM string.
754 if (!WideCharToMultiByte(CP_OEMCP, 0, ptr, -1, pathA,
755 MAX_PATH, NULL, NULL))
757 WARN( "Cannot convert path!\n" );
758 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
768 /* Undocumented success code. */
769 SET_AX( context, 0x0100 );
771 /* Truncate buffer to 64 bytes. */
775 TRACE( "%c:=%s\n", 'A' + drive, pathA );
777 strcpy( buffer, pathA );
782 /***********************************************************************
783 * INT21_SetCurrentDirectory
787 * - subfunction 0x3b of function 0x71
789 static BOOL INT21_SetCurrentDirectory( CONTEXT86 *context )
791 WCHAR dirW[MAX_PATH];
794 char *dirA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
795 BYTE drive = INT21_GetCurrentDrive();
798 TRACE( "SET CURRENT DIRECTORY %s\n", dirA );
800 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
801 if (!GetFullPathNameW( dirW, MAX_PATH, dirW, NULL )) return FALSE;
803 attr = GetFileAttributesW( dirW );
804 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
806 SetLastError( ERROR_PATH_NOT_FOUND );
811 env_var[1] = dirW[0];
814 result = SetEnvironmentVariableW( env_var, dirW );
816 /* only set current directory if on the current drive */
817 if (result && (toupperW(dirW[0]) - 'A' == drive)) result = SetCurrentDirectoryW( dirW );
822 /***********************************************************************
823 * INT21_CreateMagicDeviceHandle
825 * Create a dummy file handle for a "magic" device.
827 static HANDLE INT21_CreateMagicDeviceHandle( LPCWSTR name )
829 const char *dir = wine_get_server_dir();
833 OBJECT_ATTRIBUTES attr;
834 UNICODE_STRING nameW;
837 len = MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, NULL, 0 );
838 nameW.Length = (len + 1 + strlenW( name )) * sizeof(WCHAR);
839 nameW.MaximumLength = nameW.Length + sizeof(WCHAR);
840 if (!(nameW.Buffer = HeapAlloc( GetProcessHeap(), 0, nameW.Length )))
842 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
845 MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, nameW.Buffer, len );
846 nameW.Buffer[len-1] = '/';
847 strcpyW( nameW.Buffer + len, name );
849 attr.Length = sizeof(attr);
850 attr.RootDirectory = 0;
852 attr.ObjectName = &nameW;
853 attr.SecurityDescriptor = NULL;
854 attr.SecurityQualityOfService = NULL;
856 status = NtCreateFile( &ret, GENERIC_READ|GENERIC_WRITE, &attr, &io, NULL, 0,
857 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF,
858 FILE_SYNCHRONOUS_IO_ALERT, NULL, 0 );
862 SetLastError( RtlNtStatusToDosError(status) );
864 RtlFreeUnicodeString( &nameW );
869 /***********************************************************************
870 * INT21_OpenMagicDevice
872 * Open a file handle for "magic" devices like EMMXXXX0.
874 static HANDLE INT21_OpenMagicDevice( LPCWSTR name, DWORD access )
880 if (name[0] && (name[1] == ':')) name += 2;
881 if ((p = strrchrW( name, '/' ))) name = p + 1;
882 if ((p = strrchrW( name, '\\' ))) name = p + 1;
884 for (i = 0; i < NB_MAGIC_DEVICES; i++)
886 int len = strlenW( magic_devices[i].name );
887 if (!strncmpiW( magic_devices[i].name, name, len ) &&
888 (!name[len] || name[len] == '.' || name[len] == ':')) break;
890 if (i == NB_MAGIC_DEVICES) return 0;
892 if (!magic_devices[i].handle) /* need to open it */
897 if (!(handle = INT21_CreateMagicDeviceHandle( magic_devices[i].name ))) return 0;
898 wine_server_handle_to_fd( handle, 0, &fd, NULL, NULL );
900 wine_server_release_fd( handle, fd );
901 magic_devices[i].dev = st.st_dev;
902 magic_devices[i].ino = st.st_ino;
903 magic_devices[i].handle = handle;
905 if (!DuplicateHandle( GetCurrentProcess(), magic_devices[i].handle,
906 GetCurrentProcess(), &handle, access, FALSE, 0 )) handle = 0;
911 /***********************************************************************
919 * - subfunction 0x6c of function 0x71
921 static BOOL INT21_CreateFile( CONTEXT86 *context,
928 char *pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, pathSegOff);
929 WCHAR pathW[MAX_PATH];
936 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
937 "create flags=%04x, file=%s.\n",
938 AH_reg(context), dosAction, dosAccessShare, CX_reg(context), pathA );
941 * Application tried to create/open a file whose name
942 * ends with a backslash. This is not allowed.
944 * FIXME: This needs to be validated, especially the return value.
946 if (pathA[strlen(pathA) - 1] == '/')
948 SetLastError( ERROR_FILE_NOT_FOUND );
953 * Convert DOS action flags into Win32 creation disposition parameter.
958 winMode = OPEN_EXISTING;
961 winMode = TRUNCATE_EXISTING;
964 winMode = CREATE_NEW;
967 winMode = OPEN_ALWAYS;
970 winMode = CREATE_ALWAYS;
973 SetLastError( ERROR_INVALID_PARAMETER );
978 * Convert DOS access/share flags into Win32 desired access parameter.
980 switch(dosAccessShare & 0x07)
983 winAccess = GENERIC_READ;
986 winAccess = GENERIC_WRITE;
989 winAccess = GENERIC_READ | GENERIC_WRITE;
993 * Read-only, do not modify file's last-access time (DOS7).
995 * FIXME: How to prevent modification of last-access time?
997 winAccess = GENERIC_READ;
1004 * Convert DOS access/share flags into Win32 share mode parameter.
1006 switch(dosAccessShare & 0x70)
1008 case OF_SHARE_EXCLUSIVE:
1011 case OF_SHARE_DENY_WRITE:
1012 winSharing = FILE_SHARE_READ;
1014 case OF_SHARE_DENY_READ:
1015 winSharing = FILE_SHARE_WRITE;
1017 case OF_SHARE_DENY_NONE:
1018 case OF_SHARE_COMPAT:
1020 winSharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1024 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
1025 * What to do with this bit?
1026 * FIXME: Bits in the high byte of dosAccessShare are not supported.
1027 * See both function 0x6c and subfunction 0x6c of function 0x71 for
1028 * definition of these bits.
1032 * Convert DOS create attributes into Win32 flags and attributes parameter.
1034 if (winMode == OPEN_EXISTING || winMode == TRUNCATE_EXISTING)
1040 WORD dosAttributes = CX_reg(context);
1042 if (dosAttributes & FA_LABEL)
1045 * Application tried to create volume label entry.
1046 * This is difficult to support so we do not allow it.
1048 * FIXME: If volume does not already have a label,
1049 * this function is supposed to succeed.
1051 SetLastError( ERROR_ACCESS_DENIED );
1055 winAttributes = dosAttributes &
1056 (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
1057 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE);
1063 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
1065 if ((winHandle = INT21_OpenMagicDevice( pathW, winAccess )))
1071 winHandle = CreateFileW( pathW, winAccess, winSharing, NULL,
1072 winMode, winAttributes, 0 );
1074 if (winHandle == INVALID_HANDLE_VALUE)
1078 * Determine DOS file status.
1089 case TRUNCATE_EXISTING:
1096 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 1 : 2;
1099 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 3 : 2;
1107 * Return DOS file handle and DOS status.
1109 SET_AX( context, Win32HandleToDosFileHandle(winHandle) );
1112 SET_CX( context, dosStatus );
1114 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
1115 AX_reg(context), dosStatus );
1121 /***********************************************************************
1122 * INT21_BufferedInput
1124 * Handler for function 0x0a and reading from console using
1127 * Reads a string of characters from standard input until
1128 * enter key is pressed. Returns either number of characters
1129 * read from console including terminating CR or
1130 * zero if capacity was zero.
1132 static WORD INT21_BufferedInput( CONTEXT86 *context, BYTE *ptr, WORD capacity )
1137 * Return immediately if capacity is zero.
1147 DOSVM_Int16ReadChar( &ascii, &scan, context );
1149 if (ascii == '\r' || ascii == '\n')
1152 * FIXME: What should be echoed here?
1154 DOSVM_PutChar( '\r' );
1155 DOSVM_PutChar( '\n' );
1161 * FIXME: This function is supposed to support
1162 * DOS editing keys...
1166 * If the buffer becomes filled to within one byte of
1167 * capacity, DOS rejects all further characters up to,
1168 * but not including, the terminating carriage return.
1170 if (ascii != 0 && length < capacity-1)
1172 DOSVM_PutChar( ascii );
1173 ptr[length] = ascii;
1180 /***********************************************************************
1181 * INT21_GetCurrentDTA
1183 static BYTE *INT21_GetCurrentDTA( CONTEXT86 *context )
1185 TDB *pTask = GlobalLock16(GetCurrentTask());
1187 /* FIXME: This assumes DTA was set correctly! */
1188 return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
1189 (DWORD)OFFSETOF(pTask->dta) );
1193 /***********************************************************************
1194 * INT21_OpenFileUsingFCB
1196 * Handler for function 0x0f.
1199 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
1206 * Opens a FCB file for read/write in compatibility mode. Upon calling
1207 * the FCB must have the drive_number, file_name, and file_extension
1208 * fields filled and all other bytes cleared.
1210 static void INT21_OpenFileUsingFCB( CONTEXT86 *context )
1218 BY_HANDLE_FILE_INFORMATION info;
1221 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1222 if (fcb->drive_number == 0xff) {
1223 xfcb = (struct XFCB *) fcb;
1224 fcb = (struct FCB *) xfcb->fcb;
1228 file_path[0] = 'A' + INT21_MapDrive( fcb->drive_number );
1230 if (AL_result == 0) {
1232 pos = &file_path[2];
1233 memcpy(pos, fcb->file_name, 8);
1236 pos = strchr(pos, ' ');
1239 memcpy(pos, fcb->file_extension, 3);
1242 pos = strchr(pos, ' ');
1245 handle = (HANDLE) _lopen(file_path, OF_READWRITE);
1246 if (handle == INVALID_HANDLE_VALUE) {
1247 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path);
1248 AL_result = 0xff; /* failed */
1250 hfile16 = Win32HandleToDosFileHandle(handle);
1251 if (hfile16 == HFILE_ERROR16) {
1252 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle);
1253 CloseHandle(handle);
1254 AL_result = 0xff; /* failed */
1255 } else if (hfile16 > 255) {
1256 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16, file_path);
1258 AL_result = 0xff; /* failed */
1260 if (!GetFileInformationByHandle(handle, &info)) {
1261 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1262 hfile16, handle, file_path);
1264 AL_result = 0xff; /* failed */
1266 fcb->drive_number = file_path[0] - 'A' + 1;
1267 fcb->current_block_number = 0;
1268 fcb->logical_record_size = 128;
1269 fcb->file_size = info.nFileSizeLow;
1270 FileTimeToDosDateTime(&info.ftLastWriteTime,
1271 &fcb->date_of_last_write, &fcb->time_of_last_write);
1272 fcb->file_number = hfile16;
1273 fcb->attributes = 0xc2;
1274 fcb->starting_cluster = 0; /* don't know correct init value */
1275 fcb->sequence_number = 0; /* don't know correct init value */
1276 fcb->file_attributes = info.dwFileAttributes;
1277 /* The following fields are not initialized */
1278 /* by the native function: */
1280 /* record_within_current_block */
1281 /* random_access_record_number */
1283 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1284 file_path, hfile16, handle);
1285 AL_result = 0x00; /* successful */
1290 SET_AL(context, AL_result);
1294 /***********************************************************************
1295 * INT21_CloseFileUsingFCB
1297 * Handler for function 0x10.
1300 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1307 * Closes a FCB file.
1309 static void INT21_CloseFileUsingFCB( CONTEXT86 *context )
1315 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1316 if (fcb->drive_number == 0xff) {
1317 xfcb = (struct XFCB *) fcb;
1318 fcb = (struct FCB *) xfcb->fcb;
1321 if (_lclose16((HFILE16) fcb->file_number) != 0) {
1322 TRACE("_lclose16(%d) failed\n", fcb->file_number);
1323 AL_result = 0xff; /* failed */
1325 TRACE("successful closed file %d\n", fcb->file_number);
1326 AL_result = 0x00; /* successful */
1328 SET_AL(context, AL_result);
1332 /***********************************************************************
1333 * INT21_SequentialReadFromFCB
1335 * Handler for function 0x14.
1338 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1342 * 1: end of file, no data read
1343 * 2: segment wrap in DTA, no data read (not returned now)
1344 * 3: end of file, partial record read
1347 * Reads a record with the size FCB->logical_record_size from the FCB
1348 * to the disk transfer area. The position of the record is specified
1349 * with FCB->current_block_number and FCB->record_within_current_block.
1350 * Then FCB->current_block_number and FCB->record_within_current_block
1351 * are updated to point to the next record. If a partial record is
1352 * read, it is filled with zeros up to the FCB->logical_record_size.
1354 static void INT21_SequentialReadFromFCB( CONTEXT86 *context )
1359 DWORD record_number;
1361 BYTE *disk_transfer_area;
1365 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1366 if (fcb->drive_number == 0xff) {
1367 xfcb = (struct XFCB *) fcb;
1368 fcb = (struct FCB *) xfcb->fcb;
1371 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1372 if (handle == INVALID_HANDLE_VALUE) {
1373 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1375 AL_result = 0x01; /* end of file, no data read */
1377 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1378 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1379 if (position != record_number * fcb->logical_record_size) {
1380 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1381 fcb->file_number, record_number * fcb->logical_record_size, position);
1382 AL_result = 0x01; /* end of file, no data read */
1384 disk_transfer_area = INT21_GetCurrentDTA(context);
1385 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1386 if (bytes_read != fcb->logical_record_size) {
1387 TRACE("_lread(%d, %p, %d) failed with %d\n",
1388 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1389 if (bytes_read == 0) {
1390 AL_result = 0x01; /* end of file, no data read */
1392 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1393 AL_result = 0x03; /* end of file, partial record read */
1396 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1397 bytes_read, record_number, position, fcb->file_number, handle);
1398 AL_result = 0x00; /* successful */
1402 if (AL_result == 0x00 || AL_result == 0x03) {
1403 if (fcb->record_within_current_block == 127) {
1404 fcb->record_within_current_block = 0;
1405 fcb->current_block_number++;
1407 fcb->record_within_current_block++;
1410 SET_AL(context, AL_result);
1414 /***********************************************************************
1415 * INT21_SequentialWriteToFCB
1417 * Handler for function 0x15.
1420 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1425 * 2: segment wrap in DTA (not returned now)
1428 * Writes a record with the size FCB->logical_record_size from the disk
1429 * transfer area to the FCB. The position of the record is specified
1430 * with FCB->current_block_number and FCB->record_within_current_block.
1431 * Then FCB->current_block_number and FCB->record_within_current_block
1432 * are updated to point to the next record.
1434 static void INT21_SequentialWriteToFCB( CONTEXT86 *context )
1439 DWORD record_number;
1441 BYTE *disk_transfer_area;
1445 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1446 if (fcb->drive_number == 0xff) {
1447 xfcb = (struct XFCB *) fcb;
1448 fcb = (struct FCB *) xfcb->fcb;
1451 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1452 if (handle == INVALID_HANDLE_VALUE) {
1453 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1455 AL_result = 0x01; /* disk full */
1457 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1458 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1459 if (position != record_number * fcb->logical_record_size) {
1460 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1461 fcb->file_number, record_number * fcb->logical_record_size, position);
1462 AL_result = 0x01; /* disk full */
1464 disk_transfer_area = INT21_GetCurrentDTA(context);
1465 bytes_written = _lwrite((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1466 if (bytes_written != fcb->logical_record_size) {
1467 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1468 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1469 AL_result = 0x01; /* disk full */
1471 TRACE("successful written %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1472 bytes_written, record_number, position, fcb->file_number, handle);
1473 AL_result = 0x00; /* successful */
1477 if (AL_result == 0x00) {
1478 if (fcb->record_within_current_block == 127) {
1479 fcb->record_within_current_block = 0;
1480 fcb->current_block_number++;
1482 fcb->record_within_current_block++;
1485 SET_AL(context, AL_result);
1489 /***********************************************************************
1490 * INT21_ReadRandomRecordFromFCB
1492 * Handler for function 0x21.
1495 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1499 * 1: end of file, no data read
1500 * 2: segment wrap in DTA, no data read (not returned now)
1501 * 3: end of file, partial record read
1504 * Reads a record with the size FCB->logical_record_size from
1505 * the FCB to the disk transfer area. The position of the record
1506 * is specified with FCB->random_access_record_number. The
1507 * FCB->random_access_record_number is not updated. If a partial record
1508 * is read, it is filled with zeros up to the FCB->logical_record_size.
1510 static void INT21_ReadRandomRecordFromFCB( CONTEXT86 *context )
1515 DWORD record_number;
1517 BYTE *disk_transfer_area;
1521 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1522 if (fcb->drive_number == 0xff) {
1523 xfcb = (struct XFCB *) fcb;
1524 fcb = (struct FCB *) xfcb->fcb;
1527 memcpy(&record_number, fcb->random_access_record_number, 4);
1528 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1529 if (handle == INVALID_HANDLE_VALUE) {
1530 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1532 AL_result = 0x01; /* end of file, no data read */
1534 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1535 if (position != record_number * fcb->logical_record_size) {
1536 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1537 fcb->file_number, record_number * fcb->logical_record_size, position);
1538 AL_result = 0x01; /* end of file, no data read */
1540 disk_transfer_area = INT21_GetCurrentDTA(context);
1541 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1542 if (bytes_read != fcb->logical_record_size) {
1543 TRACE("_lread(%d, %p, %d) failed with %d\n",
1544 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1545 if (bytes_read == 0) {
1546 AL_result = 0x01; /* end of file, no data read */
1548 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1549 AL_result = 0x03; /* end of file, partial record read */
1552 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1553 bytes_read, record_number, position, fcb->file_number, handle);
1554 AL_result = 0x00; /* successful */
1558 fcb->current_block_number = record_number / 128;
1559 fcb->record_within_current_block = record_number % 128;
1560 SET_AL(context, AL_result);
1564 /***********************************************************************
1565 * INT21_WriteRandomRecordToFCB
1567 * Handler for function 0x22.
1570 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1575 * 2: segment wrap in DTA (not returned now)
1578 * Writes a record with the size FCB->logical_record_size from
1579 * the disk transfer area to the FCB. The position of the record
1580 * is specified with FCB->random_access_record_number. The
1581 * FCB->random_access_record_number is not updated.
1583 static void INT21_WriteRandomRecordToFCB( CONTEXT86 *context )
1588 DWORD record_number;
1590 BYTE *disk_transfer_area;
1594 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1595 if (fcb->drive_number == 0xff) {
1596 xfcb = (struct XFCB *) fcb;
1597 fcb = (struct FCB *) xfcb->fcb;
1600 memcpy(&record_number, fcb->random_access_record_number, 4);
1601 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1602 if (handle == INVALID_HANDLE_VALUE) {
1603 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1605 AL_result = 0x01; /* disk full */
1607 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1608 if (position != record_number * fcb->logical_record_size) {
1609 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1610 fcb->file_number, record_number * fcb->logical_record_size, position);
1611 AL_result = 0x01; /* disk full */
1613 disk_transfer_area = INT21_GetCurrentDTA(context);
1614 bytes_written = _lwrite((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1615 if (bytes_written != fcb->logical_record_size) {
1616 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1617 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1618 AL_result = 0x01; /* disk full */
1620 TRACE("successful written %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1621 bytes_written, record_number, position, fcb->file_number, handle);
1622 AL_result = 0x00; /* successful */
1626 fcb->current_block_number = record_number / 128;
1627 fcb->record_within_current_block = record_number % 128;
1628 SET_AL(context, AL_result);
1632 /***********************************************************************
1633 * INT21_RandomBlockReadFromFCB
1635 * Handler for function 0x27.
1638 * CX [I/O] Number of records to read
1639 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1643 * 1: end of file, no data read
1644 * 2: segment wrap in DTA, no data read (not returned now)
1645 * 3: end of file, partial record read
1648 * Reads several records with the size FCB->logical_record_size from
1649 * the FCB to the disk transfer area. The number of records to be
1650 * read is specified in the CX register. The position of the first
1651 * record is specified with FCB->random_access_record_number. The
1652 * FCB->random_access_record_number, the FCB->current_block_number
1653 * and FCB->record_within_current_block are updated to point to the
1654 * next record after the records read. If a partial record is read,
1655 * it is filled with zeros up to the FCB->logical_record_size. The
1656 * CX register is set to the number of successfully read records.
1658 static void INT21_RandomBlockReadFromFCB( CONTEXT86 *context )
1663 DWORD record_number;
1665 BYTE *disk_transfer_area;
1666 UINT records_requested;
1667 UINT bytes_requested;
1672 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1673 if (fcb->drive_number == 0xff) {
1674 xfcb = (struct XFCB *) fcb;
1675 fcb = (struct FCB *) xfcb->fcb;
1678 memcpy(&record_number, fcb->random_access_record_number, 4);
1679 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1680 if (handle == INVALID_HANDLE_VALUE) {
1681 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1684 AL_result = 0x01; /* end of file, no data read */
1686 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1687 if (position != record_number * fcb->logical_record_size) {
1688 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1689 fcb->file_number, record_number * fcb->logical_record_size, position);
1691 AL_result = 0x01; /* end of file, no data read */
1693 disk_transfer_area = INT21_GetCurrentDTA(context);
1694 records_requested = CX_reg(context);
1695 bytes_requested = (UINT) records_requested * fcb->logical_record_size;
1696 bytes_read = _lread((HFILE) handle, disk_transfer_area, bytes_requested);
1697 if (bytes_read != bytes_requested) {
1698 TRACE("_lread(%d, %p, %d) failed with %d\n",
1699 fcb->file_number, disk_transfer_area, bytes_requested, bytes_read);
1700 records_read = bytes_read / fcb->logical_record_size;
1701 if (bytes_read % fcb->logical_record_size == 0) {
1702 AL_result = 0x01; /* end of file, no data read */
1705 memset(&disk_transfer_area[bytes_read], 0, records_read * fcb->logical_record_size - bytes_read);
1706 AL_result = 0x03; /* end of file, partial record read */
1709 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1710 bytes_read, record_number, position, fcb->file_number, handle);
1711 records_read = records_requested;
1712 AL_result = 0x00; /* successful */
1716 record_number += records_read;
1717 memcpy(fcb->random_access_record_number, &record_number, 4);
1718 fcb->current_block_number = record_number / 128;
1719 fcb->record_within_current_block = record_number % 128;
1720 SET_CX(context, records_read);
1721 SET_AL(context, AL_result);
1725 /***********************************************************************
1726 * INT21_RandomBlockWriteToFCB
1728 * Handler for function 0x28.
1731 * CX [I/O] Number of records to write
1732 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1737 * 2: segment wrap in DTA (not returned now)
1740 * Writes several records with the size FCB->logical_record_size from
1741 * the disk transfer area to the FCB. The number of records to be
1742 * written is specified in the CX register. The position of the first
1743 * record is specified with FCB->random_access_record_number. The
1744 * FCB->random_access_record_number, the FCB->current_block_number
1745 * and FCB->record_within_current_block are updated to point to the
1746 * next record after the records written. The CX register is set to
1747 * the number of successfully written records.
1749 static void INT21_RandomBlockWriteToFCB( CONTEXT86 *context )
1754 DWORD record_number;
1756 BYTE *disk_transfer_area;
1757 UINT records_requested;
1758 UINT bytes_requested;
1760 UINT records_written;
1763 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1764 if (fcb->drive_number == 0xff) {
1765 xfcb = (struct XFCB *) fcb;
1766 fcb = (struct FCB *) xfcb->fcb;
1769 memcpy(&record_number, fcb->random_access_record_number, 4);
1770 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1771 if (handle == INVALID_HANDLE_VALUE) {
1772 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1774 records_written = 0;
1775 AL_result = 0x01; /* disk full */
1777 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1778 if (position != record_number * fcb->logical_record_size) {
1779 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1780 fcb->file_number, record_number * fcb->logical_record_size, position);
1781 records_written = 0;
1782 AL_result = 0x01; /* disk full */
1784 disk_transfer_area = INT21_GetCurrentDTA(context);
1785 records_requested = CX_reg(context);
1786 bytes_requested = (UINT) records_requested * fcb->logical_record_size;
1787 bytes_written = _lwrite((HFILE) handle, disk_transfer_area, bytes_requested);
1788 if (bytes_written != bytes_requested) {
1789 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1790 fcb->file_number, disk_transfer_area, bytes_requested, bytes_written);
1791 records_written = bytes_written / fcb->logical_record_size;
1792 AL_result = 0x01; /* disk full */
1794 TRACE("successful write %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1795 bytes_written, record_number, position, fcb->file_number, handle);
1796 records_written = records_requested;
1797 AL_result = 0x00; /* successful */
1801 record_number += records_written;
1802 memcpy(fcb->random_access_record_number, &record_number, 4);
1803 fcb->current_block_number = record_number / 128;
1804 fcb->record_within_current_block = record_number % 128;
1805 SET_CX(context, records_written);
1806 SET_AL(context, AL_result);
1810 /***********************************************************************
1811 * INT21_CreateDirectory
1815 * - subfunction 0x39 of function 0x71
1816 * - subfunction 0xff of function 0x43 (CL == 0x39)
1818 static BOOL INT21_CreateDirectory( CONTEXT86 *context )
1820 WCHAR dirW[MAX_PATH];
1821 char *dirA = CTX_SEG_OFF_TO_LIN(context,
1825 TRACE( "CREATE DIRECTORY %s\n", dirA );
1827 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
1829 if (CreateDirectoryW(dirW, NULL))
1833 * FIXME: CreateDirectory's LastErrors will clash with the ones
1834 * used by DOS. AH=39 only returns 3 (path not found) and
1835 * 5 (access denied), while CreateDirectory return several
1836 * ones. Remap some of them. -Marcus
1838 switch (GetLastError())
1840 case ERROR_ALREADY_EXISTS:
1841 case ERROR_FILENAME_EXCED_RANGE:
1842 case ERROR_DISK_FULL:
1843 SetLastError(ERROR_ACCESS_DENIED);
1853 /***********************************************************************
1854 * INT21_ExtendedCountryInformation
1856 * Handler for function 0x65.
1858 static void INT21_ExtendedCountryInformation( CONTEXT86 *context )
1860 BYTE *dataptr = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
1862 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1866 * Check subfunctions that are passed country and code page.
1868 if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
1870 WORD country = DX_reg(context);
1871 WORD codepage = BX_reg(context);
1873 if (country != 0xffff && country != INT21_GetSystemCountryCode())
1874 FIXME( "Requested info on non-default country %04x\n", country );
1876 if (codepage != 0xffff && codepage != GetOEMCP())
1877 FIXME( "Requested info on non-default code page %04x\n", codepage );
1880 switch (AL_reg(context)) {
1881 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1882 INT_BARF( context, 0x21 );
1883 SET_CFLAG( context );
1886 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1887 TRACE( "Get general internationalization info\n" );
1888 dataptr[0] = 0x01; /* Info ID */
1889 *(WORD*)(dataptr+1) = 38; /* Size of the following info */
1890 *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
1891 *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
1892 INT21_FillCountryInformation( dataptr + 7 );
1893 SET_CX( context, 41 ); /* Size of returned info */
1896 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1897 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1898 TRACE( "Get pointer to uppercase table\n" );
1899 dataptr[0] = AL_reg(context); /* Info ID */
1900 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1901 offsetof(INT21_HEAP, uppercase_size) );
1902 SET_CX( context, 5 ); /* Size of returned info */
1905 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1906 TRACE( "Get pointer to lowercase table\n" );
1907 dataptr[0] = 0x03; /* Info ID */
1908 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1909 offsetof(INT21_HEAP, lowercase_size) );
1910 SET_CX( context, 5 ); /* Size of returned info */
1913 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1914 TRACE("Get pointer to filename terminator table\n");
1915 dataptr[0] = 0x05; /* Info ID */
1916 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1917 offsetof(INT21_HEAP, filename_size) );
1918 SET_CX( context, 5 ); /* Size of returned info */
1921 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1922 TRACE("Get pointer to collating sequence table\n");
1923 dataptr[0] = 0x06; /* Info ID */
1924 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1925 offsetof(INT21_HEAP, collating_size) );
1926 SET_CX( context, 5 ); /* Size of returned info */
1929 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1930 TRACE("Get pointer to DBCS lead byte table\n");
1931 dataptr[0] = 0x07; /* Info ID */
1932 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1933 offsetof(INT21_HEAP, dbcs_size) );
1934 SET_CX( context, 5 ); /* Size of returned info */
1937 case 0x20: /* CAPITALIZE CHARACTER */
1938 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1939 TRACE("Convert char to uppercase\n");
1940 SET_DL( context, toupper(DL_reg(context)) );
1943 case 0x21: /* CAPITALIZE STRING */
1944 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1945 TRACE("Convert string to uppercase with length\n");
1947 char *ptr = (char *)CTX_SEG_OFF_TO_LIN( context,
1950 WORD len = CX_reg(context);
1951 while (len--) { *ptr = toupper(*ptr); ptr++; }
1955 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1956 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1957 TRACE("Convert ASCIIZ string to uppercase\n");
1958 _strupr( (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx) );
1961 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1962 INT_BARF( context, 0x21 );
1963 SET_CFLAG( context );
1967 INT_BARF( context, 0x21 );
1974 /***********************************************************************
1975 * INT21_FileAttributes
1979 * - subfunction 0x43 of function 0x71
1981 static BOOL INT21_FileAttributes( CONTEXT86 *context,
1985 WCHAR fileW[MAX_PATH];
1986 char *fileA = CTX_SEG_OFF_TO_LIN(context,
1995 switch (subfunction)
1997 case 0x00: /* GET FILE ATTRIBUTES */
1998 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA );
1999 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2001 result = GetFileAttributesW( fileW );
2002 if (result == INVALID_FILE_ATTRIBUTES)
2006 SET_CX( context, (WORD)result );
2008 SET_AX( context, (WORD)result ); /* DR DOS */
2012 case 0x01: /* SET FILE ATTRIBUTES */
2013 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
2014 CX_reg(context), fileA );
2015 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2017 if (!SetFileAttributesW( fileW, CX_reg(context) ))
2021 case 0x02: /* GET COMPRESSED FILE SIZE */
2022 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA );
2023 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2025 result = GetCompressedFileSizeW( fileW, NULL );
2026 if (result == INVALID_FILE_SIZE)
2030 SET_AX( context, LOWORD(result) );
2031 SET_DX( context, HIWORD(result) );
2035 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
2037 INT_BARF( context, 0x21 );
2040 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
2041 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2043 handle = CreateFileW( fileW, GENERIC_WRITE,
2044 FILE_SHARE_READ | FILE_SHARE_WRITE,
2045 NULL, OPEN_EXISTING, 0, 0 );
2046 if (handle == INVALID_HANDLE_VALUE)
2049 DosDateTimeToFileTime( DI_reg(context),
2052 status = SetFileTime( handle, NULL, NULL, &filetime );
2054 CloseHandle( handle );
2059 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
2061 INT_BARF( context, 0x21 );
2064 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
2065 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2067 handle = CreateFileW( fileW, GENERIC_READ,
2068 FILE_SHARE_READ | FILE_SHARE_WRITE,
2069 NULL, OPEN_EXISTING, 0, 0 );
2070 if (handle == INVALID_HANDLE_VALUE)
2073 status = GetFileTime( handle, NULL, NULL, &filetime );
2076 FileTimeToDosDateTime( &filetime, &date, &time );
2077 SET_DI( context, date );
2078 SET_CX( context, time );
2081 CloseHandle( handle );
2086 case 0x05: /* SET FILE LAST ACCESS DATE */
2088 INT_BARF( context, 0x21 );
2091 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA );
2092 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2094 handle = CreateFileW( fileW, GENERIC_WRITE,
2095 FILE_SHARE_READ | FILE_SHARE_WRITE,
2096 NULL, OPEN_EXISTING, 0, 0 );
2097 if (handle == INVALID_HANDLE_VALUE)
2100 DosDateTimeToFileTime( DI_reg(context),
2103 status = SetFileTime( handle, NULL, &filetime, NULL );
2105 CloseHandle( handle );
2110 case 0x06: /* GET FILE LAST ACCESS DATE */
2112 INT_BARF( context, 0x21 );
2115 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA );
2116 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2118 handle = CreateFileW( fileW, GENERIC_READ,
2119 FILE_SHARE_READ | FILE_SHARE_WRITE,
2120 NULL, OPEN_EXISTING, 0, 0 );
2121 if (handle == INVALID_HANDLE_VALUE)
2124 status = GetFileTime( handle, NULL, &filetime, NULL );
2127 FileTimeToDosDateTime( &filetime, &date, NULL );
2128 SET_DI( context, date );
2131 CloseHandle( handle );
2136 case 0x07: /* SET FILE CREATION DATE AND TIME */
2138 INT_BARF( context, 0x21 );
2141 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA );
2143 handle = CreateFileW( fileW, GENERIC_WRITE,
2144 FILE_SHARE_READ | FILE_SHARE_WRITE,
2145 NULL, OPEN_EXISTING, 0, 0 );
2146 if (handle == INVALID_HANDLE_VALUE)
2150 * FIXME: SI has number of 10-millisecond units past time in CX.
2152 DosDateTimeToFileTime( DI_reg(context),
2155 status = SetFileTime( handle, &filetime, NULL, NULL );
2157 CloseHandle( handle );
2162 case 0x08: /* GET FILE CREATION DATE AND TIME */
2164 INT_BARF( context, 0x21 );
2167 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2170 handle = CreateFileW( fileW, GENERIC_READ,
2171 FILE_SHARE_READ | FILE_SHARE_WRITE,
2172 NULL, OPEN_EXISTING, 0, 0 );
2173 if (handle == INVALID_HANDLE_VALUE)
2176 status = GetFileTime( handle, &filetime, NULL, NULL );
2179 FileTimeToDosDateTime( &filetime, &date, &time );
2180 SET_DI( context, date );
2181 SET_CX( context, time );
2183 * FIXME: SI has number of 10-millisecond units past
2186 SET_SI( context, 0 );
2189 CloseHandle(handle);
2194 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2195 if (islong || context->Ebp != 0x5053)
2196 INT_BARF( context, 0x21 );
2199 switch(CL_reg(context))
2202 if (!INT21_CreateDirectory( context ))
2207 if (!INT21_RenameFile( context ))
2212 INT_BARF( context, 0x21 );
2218 INT_BARF( context, 0x21 );
2225 /***********************************************************************
2226 * INT21_FileDateTime
2228 * Handler for function 0x57.
2230 static BOOL INT21_FileDateTime( CONTEXT86 *context )
2232 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2236 switch (AL_reg(context)) {
2237 case 0x00: /* Get last-written stamp */
2238 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2241 if (!GetFileTime( handle, NULL, NULL, &filetime ))
2243 FileTimeToDosDateTime( &filetime, &date, &time );
2244 SET_DX( context, date );
2245 SET_CX( context, time );
2249 case 0x01: /* Set last-written stamp */
2250 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2253 DosDateTimeToFileTime( DX_reg(context),
2256 if (!SetFileTime( handle, NULL, NULL, &filetime ))
2261 case 0x04: /* Get last access stamp, DOS 7 */
2262 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2265 if (!GetFileTime( handle, NULL, &filetime, NULL ))
2267 FileTimeToDosDateTime( &filetime, &date, &time );
2268 SET_DX( context, date );
2269 SET_CX( context, time );
2273 case 0x05: /* Set last access stamp, DOS 7 */
2274 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2277 DosDateTimeToFileTime( DX_reg(context),
2280 if (!SetFileTime( handle, NULL, &filetime, NULL ))
2285 case 0x06: /* Get creation stamp, DOS 7 */
2286 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2289 if (!GetFileTime( handle, &filetime, NULL, NULL ))
2291 FileTimeToDosDateTime( &filetime, &date, &time );
2292 SET_DX( context, date );
2293 SET_CX( context, time );
2295 * FIXME: SI has number of 10-millisecond units past time in CX.
2297 SET_SI( context, 0 );
2301 case 0x07: /* Set creation stamp, DOS 7 */
2302 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2306 * FIXME: SI has number of 10-millisecond units past time in CX.
2308 DosDateTimeToFileTime( DX_reg(context),
2311 if (!SetFileTime( handle, &filetime, NULL, NULL ))
2317 INT_BARF( context, 0x21 );
2325 /***********************************************************************
2328 * Handler for functions 0x51 and 0x62.
2330 static void INT21_GetPSP( CONTEXT86 *context )
2332 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
2335 * FIXME: should we return the original DOS PSP upon
2338 if (!ISV86(context) && DOSVM_IsWin16())
2339 SET_BX( context, LOWORD(GetCurrentPDB16()) );
2341 SET_BX( context, DOSVM_psp );
2344 static inline void setword( BYTE *ptr, WORD w )
2347 ptr[1] = (BYTE)(w >> 8);
2350 static void CreateBPB(int drive, BYTE *data, BOOL16 limited)
2351 /* limited == TRUE is used with INT 0x21/0x440d */
2353 /* FIXME: we're forcing some values without checking that those are valid */
2358 setword(&data[3], 0);
2360 setword(&data[6], 240);
2361 setword(&data[8], 64000);
2363 setword(&data[0x0b], 40);
2364 setword(&data[0x0d], 56);
2365 setword(&data[0x0f], 2);
2366 setword(&data[0x11], 0);
2369 setword(&data[0x1f], 800);
2371 setword(&data[0x22], 1);
2378 setword(&data[3], 0);
2380 setword(&data[6], 240);
2381 setword(&data[8], 2880);
2383 setword(&data[0x0b], 6);
2384 setword(&data[0x0d], 18);
2385 setword(&data[0x0f], 2);
2386 setword(&data[0x11], 0);
2389 setword(&data[0x1f], 80);
2391 setword(&data[0x22], 2);
2396 /***********************************************************************
2399 * Handler for block device IOCTLs.
2401 static void INT21_Ioctl_Block( CONTEXT86 *context )
2404 BYTE drive = INT21_MapDrive( BL_reg(context) );
2405 WCHAR drivespec[4] = {'A', ':', '\\', 0};
2408 drivespec[0] += drive;
2409 drivetype = GetDriveTypeW( drivespec );
2411 RESET_CFLAG(context);
2412 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
2414 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2415 AL_reg(context), 'A' + drive );
2416 SetLastError( ERROR_INVALID_DRIVE );
2417 SET_AX( context, ERROR_INVALID_DRIVE );
2418 SET_CFLAG( context );
2422 switch (AL_reg(context))
2424 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2425 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2426 INT_BARF( context, 0x21 );
2429 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2430 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2433 if (drivetype == DRIVE_REMOVABLE)
2434 SET_AX( context, 0 ); /* removable */
2436 SET_AX( context, 1 ); /* not removable */
2439 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2440 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2443 if (drivetype == DRIVE_REMOTE)
2444 SET_DX( context, (1<<9) | (1<<12) ); /* remote + no direct IO */
2446 SET_DX( context, 0 ); /* FIXME: use driver attr here */
2449 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2450 /* Get pointer to IOCTL parameter block. */
2451 dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2453 switch (CX_reg(context))
2455 case 0x0841: /* write logical device track */
2456 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2459 WORD head = *(WORD *)dataptr+1;
2460 WORD cyl = *(WORD *)dataptr+3;
2461 WORD sect = *(WORD *)dataptr+5;
2462 WORD nrsect = *(WORD *)dataptr+7;
2463 BYTE *data = (BYTE *)dataptr+9; /* FIXME: is this correct? */
2465 if (!DOSVM_RawWrite(drive, head*cyl*sect, nrsect, data, FALSE))
2467 SET_AX( context, ERROR_WRITE_FAULT );
2473 case 0x084a: /* lock logical volume */
2474 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2475 BH_reg(context), DX_reg(context), 'A' + drive );
2478 case 0x0860: /* get device parameters */
2479 /* FIXME: we're faking some values here */
2480 /* used by w4wgrp's winfile */
2481 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2483 dataptr[6] = 0; /* media type */
2486 dataptr[1] = 0x05; /* fixed disk */
2487 setword(&dataptr[2], 0x01); /* non removable */
2488 setword(&dataptr[4], 0x300); /* # of cylinders */
2492 dataptr[1] = 0x07; /* block dev, floppy */
2493 setword(&dataptr[2], 0x02); /* removable */
2494 setword(&dataptr[4], 80); /* # of cylinders */
2496 CreateBPB(drive, &dataptr[7], TRUE);
2497 RESET_CFLAG(context);
2500 case 0x0861: /* read logical device track */
2501 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2504 WORD head = *(WORD *)dataptr+1;
2505 WORD cyl = *(WORD *)dataptr+3;
2506 WORD sect = *(WORD *)dataptr+5;
2507 WORD nrsect = *(WORD *)dataptr+7;
2508 BYTE *data = (BYTE *)dataptr+9; /* FIXME: is this correct? */
2510 if (!DOSVM_RawRead(drive, head*cyl*sect, nrsect, data, FALSE))
2512 SET_AX( context, ERROR_READ_FAULT );
2518 case 0x0866: /* get volume serial number */
2520 WCHAR label[12],fsname[9];
2523 drivespec[0] += drive;
2524 GetVolumeInformationW(drivespec, label, 12, &serial, NULL, NULL, fsname, 9);
2525 *(WORD*)dataptr = 0;
2526 memcpy(dataptr+2,&serial,4);
2527 WideCharToMultiByte(CP_OEMCP, 0, label, 11, dataptr + 6, 11, NULL, NULL);
2528 WideCharToMultiByte(CP_OEMCP, 0, fsname, 8, dataptr + 17, 8, NULL, NULL);
2532 case 0x086a: /* unlock logical volume */
2533 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2537 case 0x086f: /* get drive map information */
2538 memset(dataptr+1, '\0', dataptr[0]-1);
2539 dataptr[1] = dataptr[0];
2540 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
2541 dataptr[3] = 0xFF; /* no physical drive */
2545 /* Trail on error implementation */
2546 SET_AX( context, drivetype == DRIVE_UNKNOWN ? 0x0f : 0x01 );
2547 SET_CFLAG(context); /* Seems to be set all the time */
2551 INT_BARF( context, 0x21 );
2555 case 0x0e: /* GET LOGICAL DRIVE MAP */
2556 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2558 /* FIXME: this is not correct if drive has mappings */
2559 SET_AL( context, 0 ); /* drive has no mapping */
2562 case 0x0f: /* SET LOGICAL DRIVE MAP */
2564 WCHAR dev[3], tgt[4];
2566 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2567 INT21_DriveName( BL_reg(context)));
2568 dev[0] = 'A' + drive; dev[1] = ':'; dev[2] = 0;
2569 tgt[0] = 'A' + drive + 1; dev[1] = ':'; dev[2] = '\\'; dev[3] = 0;
2570 if (!DefineDosDeviceW(DDD_RAW_TARGET_PATH, dev, tgt))
2573 SET_AX( context, 0x000F ); /* invalid drive */
2578 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2580 INT_BARF( context, 0x21 );
2585 /***********************************************************************
2586 * INT21_IoctlScsiMgrHandler
2588 * IOCTL handler for the SCSIMGR device.
2590 static void INT21_IoctlScsiMgrHandler( CONTEXT86 *context )
2592 switch (AL_reg(context))
2594 case 0x00: /* GET DEVICE INFORMATION */
2595 SET_DX( context, 0xc0c0 );
2598 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2599 DOSVM_ASPIHandler(context);
2602 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2603 SET_DX( context, 0 );
2606 case 0x01: /* SET DEVICE INFORMATION */
2607 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2608 case 0x06: /* GET INPUT STATUS */
2609 case 0x07: /* GET OUTPUT STATUS */
2610 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2611 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2613 INT_BARF( context, 0x21 );
2619 /***********************************************************************
2620 * INT21_IoctlEMSHandler
2622 * IOCTL handler for the EMXXXX0 device.
2624 static void INT21_IoctlEMSHandler( CONTEXT86 *context )
2626 EMS_Ioctl_Handler(context);
2630 /***********************************************************************
2631 * INT21_IoctlHPScanHandler
2633 * IOCTL handler for the HPSCAN device.
2635 static void INT21_IoctlHPScanHandler( CONTEXT86 *context )
2637 switch (AL_reg(context))
2639 case 0x00: /* GET DEVICE INFORMATION */
2640 SET_DX( context, 0xc0c0 );
2643 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2644 SET_DX( context, 0 );
2647 case 0x01: /* SET DEVICE INFORMATION */
2648 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2649 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2650 case 0x06: /* GET INPUT STATUS */
2651 case 0x07: /* GET OUTPUT STATUS */
2652 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2653 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2655 INT_BARF( context, 0x21 );
2661 /***********************************************************************
2664 * Handler for character device IOCTLs.
2666 static void INT21_Ioctl_Char( CONTEXT86 *context )
2670 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2672 status = wine_server_handle_to_fd( handle, 0, &fd, NULL, NULL );
2675 SET_AX( context, RtlNtStatusToDosError(status) );
2676 SET_CFLAG( context );
2680 wine_server_release_fd( handle, fd );
2682 for (i = 0; i < NB_MAGIC_DEVICES; i++)
2684 if (!magic_devices[i].handle) continue;
2685 if (magic_devices[i].dev == st.st_dev && magic_devices[i].ino == st.st_ino)
2688 magic_devices[i].ioctl_handler( context );
2693 /* no magic device found, do default handling */
2695 switch (AL_reg(context))
2697 case 0x00: /* GET DEVICE INFORMATION */
2698 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context) );
2699 if (S_ISCHR(st.st_mode))
2702 * Returns attribute word in DX:
2703 * Bit 14 - Device driver can process IOCTL requests.
2704 * Bit 13 - Output until busy supported.
2705 * Bit 11 - Driver supports OPEN/CLOSE calls.
2707 * Bit 7 - Set (indicates device).
2708 * Bit 6 - EOF on input.
2709 * Bit 5 - Raw (binary) mode.
2710 * Bit 4 - Device is special (uses int29).
2711 * Bit 3 - Clock device.
2712 * Bit 2 - NUL device.
2713 * Bit 1 - Standard output.
2714 * Bit 0 - Standard input.
2716 SET_DX( context, 0x80c0 /* FIXME */ );
2721 * Returns attribute word in DX:
2722 * Bit 15 - File is remote.
2723 * Bit 14 - Don't set file date/time on closing.
2724 * Bit 11 - Media not removable.
2725 * Bit 8 - Generate int24 if no disk space on write
2726 * or read past end of file
2727 * Bit 7 - Clear (indicates file).
2728 * Bit 6 - File has not been written.
2729 * Bit 5..0 - Drive number (0=A:,...)
2731 * FIXME: Should check if file is on remote or removable drive.
2732 * FIXME: Should use drive file is located on (and not current).
2734 SET_DX( context, 0x0140 + INT21_GetCurrentDrive() );
2738 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2739 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context) );
2741 * Returns attribute word in DX:
2742 * Bit 15 - Set if remote.
2743 * Bit 14 - Set if date/time not set on close.
2745 * FIXME: Should check if file is on remote drive.
2747 SET_DX( context, 0 );
2750 case 0x01: /* SET DEVICE INFORMATION */
2751 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2752 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2753 case 0x06: /* GET INPUT STATUS */
2754 case 0x07: /* GET OUTPUT STATUS */
2755 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2756 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2758 INT_BARF( context, 0x21 );
2764 /***********************************************************************
2767 * Handler for function 0x44.
2769 static void INT21_Ioctl( CONTEXT86 *context )
2771 switch (AL_reg(context))
2777 INT21_Ioctl_Char( context );
2782 INT21_Ioctl_Block( context );
2787 INT21_Ioctl_Char( context );
2792 INT21_Ioctl_Block( context );
2796 INT21_Ioctl_Char( context );
2799 case 0x0b: /* SET SHARING RETRY COUNT */
2800 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2801 CX_reg(context), DX_reg(context) );
2802 if (!CX_reg(context))
2804 SET_AX( context, 1 );
2805 SET_CFLAG( context );
2809 DOSDEV_SetSharingRetry( CX_reg(context), DX_reg(context) );
2810 RESET_CFLAG( context );
2815 INT21_Ioctl_Char( context );
2821 INT21_Ioctl_Block( context );
2825 INT21_Ioctl_Char( context );
2829 INT21_Ioctl_Block( context );
2832 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2833 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2834 SET_CFLAG(context); /* Error / This is not DR DOS. */
2835 SET_AX( context, 0x0001 ); /* Invalid function */
2838 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2839 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2840 SET_CFLAG(context); /* Error / This is not DR DOS. */
2841 SET_AX( context, 0x0001 ); /* Invalid function */
2844 case 0xe0: /* Sun PC-NFS API */
2845 TRACE( "Sun PC-NFS API\n" );
2850 INT_BARF( context, 0x21 );
2855 /***********************************************************************
2858 * Handler for function 0x73.
2860 static BOOL INT21_Fat32( CONTEXT86 *context )
2862 switch (AL_reg(context))
2864 case 0x02: /* FAT32 - GET EXTENDED DPB */
2866 BYTE drive = INT21_MapDrive( DL_reg(context) );
2867 WORD *ptr = CTX_SEG_OFF_TO_LIN(context,
2868 context->SegEs, context->Edi);
2869 INT21_DPB *target = (INT21_DPB*)(ptr + 1);
2872 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context) );
2874 if ( CX_reg(context) < sizeof(INT21_DPB) + 2 || *ptr < sizeof(INT21_DPB) )
2876 SetLastError( ERROR_BAD_LENGTH );
2880 if ( !INT21_FillDrivePB( drive ) )
2882 SetLastError( ERROR_INVALID_DRIVE );
2886 source = &INT21_GetHeapPointer()->misc_dpb_list[drive];
2888 *ptr = sizeof(INT21_DPB);
2889 memcpy( target, source, sizeof(INT21_DPB));
2891 if (LOWORD(context->Esi) != 0xF1A6)
2893 target->driver_header = 0;
2898 FIXME( "Caller requested driver and next DPB pointers!\n" );
2903 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2905 WCHAR dirW[MAX_PATH];
2906 char *dirA = CTX_SEG_OFF_TO_LIN( context,
2907 context->SegDs, context->Edx );
2908 BYTE *data = CTX_SEG_OFF_TO_LIN( context,
2909 context->SegEs, context->Edi );
2910 DWORD cluster_sectors;
2912 DWORD free_clusters;
2913 DWORD total_clusters;
2915 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA );
2916 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
2918 if (CX_reg(context) < 44)
2920 SetLastError( ERROR_BAD_LENGTH );
2924 if (!GetDiskFreeSpaceW( dirW, &cluster_sectors, §or_bytes,
2925 &free_clusters, &total_clusters ))
2928 *(WORD*) (data + 0) = 44; /* size of structure */
2929 *(WORD*) (data + 2) = 0; /* version */
2930 *(DWORD*)(data + 4) = cluster_sectors;
2931 *(DWORD*)(data + 8) = sector_bytes;
2932 *(DWORD*)(data + 12) = free_clusters;
2933 *(DWORD*)(data + 16) = total_clusters;
2936 * Below we have free/total sectors and
2937 * free/total allocation units without adjustment
2938 * for compression. We fake both using cluster information.
2940 *(DWORD*)(data + 20) = free_clusters * cluster_sectors;
2941 *(DWORD*)(data + 24) = total_clusters * cluster_sectors;
2942 *(DWORD*)(data + 28) = free_clusters;
2943 *(DWORD*)(data + 32) = total_clusters;
2946 * Between (data + 36) and (data + 43) there
2947 * are eight reserved bytes.
2953 INT_BARF( context, 0x21 );
2959 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA *dataA,
2960 const WIN32_FIND_DATAW *dataW)
2962 dataA->dwFileAttributes = dataW->dwFileAttributes;
2963 dataA->ftCreationTime = dataW->ftCreationTime;
2964 dataA->ftLastAccessTime = dataW->ftLastAccessTime;
2965 dataA->ftLastWriteTime = dataW->ftLastWriteTime;
2966 dataA->nFileSizeHigh = dataW->nFileSizeHigh;
2967 dataA->nFileSizeLow = dataW->nFileSizeLow;
2968 WideCharToMultiByte( CP_OEMCP, 0, dataW->cFileName, -1,
2969 dataA->cFileName, sizeof(dataA->cFileName), NULL, NULL );
2970 WideCharToMultiByte( CP_OEMCP, 0, dataW->cAlternateFileName, -1,
2971 dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName), NULL, NULL );
2974 /***********************************************************************
2975 * INT21_LongFilename
2977 * Handler for function 0x71.
2979 static void INT21_LongFilename( CONTEXT86 *context )
2981 BOOL bSetDOSExtendedError = FALSE;
2982 WCHAR pathW[MAX_PATH];
2985 if (HIBYTE(HIWORD(GetVersion16())) < 0x07)
2987 TRACE( "LONG FILENAME - functions supported only under DOS7\n" );
2988 SET_CFLAG( context );
2989 SET_AL( context, 0 );
2993 switch (AL_reg(context))
2995 case 0x0d: /* RESET DRIVE */
2996 INT_BARF( context, 0x21 );
2999 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
3000 if (!INT21_CreateDirectory( context ))
3001 bSetDOSExtendedError = TRUE;
3004 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
3005 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3007 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA);
3008 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3009 if (!RemoveDirectoryW( pathW )) bSetDOSExtendedError = TRUE;
3012 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
3013 if (!INT21_SetCurrentDirectory( context ))
3014 bSetDOSExtendedError = TRUE;
3017 case 0x41: /* LONG FILENAME - DELETE FILE */
3018 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3020 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA );
3021 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3023 if (!DeleteFileW( pathW )) bSetDOSExtendedError = TRUE;
3026 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
3027 if (!INT21_FileAttributes( context, BL_reg(context), TRUE ))
3028 bSetDOSExtendedError = TRUE;
3031 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
3032 if (!INT21_GetCurrentDirectory( context, TRUE ))
3033 bSetDOSExtendedError = TRUE;
3036 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
3040 WIN32_FIND_DATAW dataW;
3041 WIN32_FIND_DATAA* dataA;
3043 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
3044 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA);
3046 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3047 handle = FindFirstFileW(pathW, &dataW);
3049 dataA = (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
3051 if (handle != INVALID_HANDLE_VALUE &&
3052 (h16 = GlobalAlloc16(GMEM_MOVEABLE, sizeof(handle))))
3054 HANDLE* ptr = GlobalLock16( h16 );
3056 GlobalUnlock16( h16 );
3057 SET_AX( context, h16 );
3058 INT21_ConvertFindDataWtoA(dataA, &dataW);
3062 if (handle != INVALID_HANDLE_VALUE) FindClose(handle);
3063 SET_AX( context, INVALID_HANDLE_VALUE16);
3064 bSetDOSExtendedError = TRUE;
3069 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
3071 HGLOBAL16 h16 = BX_reg(context);
3073 WIN32_FIND_DATAW dataW;
3074 WIN32_FIND_DATAA* dataA;
3076 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
3079 dataA = (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
3082 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
3084 if (!FindNextFileW(*ptr, &dataW)) bSetDOSExtendedError = TRUE;
3085 else INT21_ConvertFindDataWtoA(dataA, &dataW);
3086 GlobalUnlock16( h16 );
3090 SetLastError( ERROR_INVALID_HANDLE );
3091 bSetDOSExtendedError = TRUE;
3096 case 0x56: /* LONG FILENAME - RENAME FILE */
3097 if (!INT21_RenameFile(context))
3098 bSetDOSExtendedError = TRUE;
3101 case 0x60: /* LONG FILENAME - CONVERT PATH */
3103 WCHAR res[MAX_PATH];
3105 switch (CL_reg(context))
3107 case 0x00: /* "truename" - Canonicalize path */
3109 * FIXME: This is not 100% equal to 0x01 case,
3110 * if you fix this, fix int21 subfunction 0x60, too.
3113 case 0x01: /* Get short filename or path */
3114 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
3115 if (!GetShortPathNameW(pathW, res, 67))
3116 bSetDOSExtendedError = TRUE;
3119 SET_AX( context, 0 );
3120 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
3121 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3126 case 0x02: /* Get canonical long filename or path */
3127 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
3128 if (!GetFullPathNameW(pathW, 128, res, NULL))
3129 bSetDOSExtendedError = TRUE;
3132 SET_AX( context, 0 );
3133 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
3134 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3139 FIXME("Unimplemented long file name function:\n");
3140 INT_BARF( context, 0x21 );
3142 SET_AL( context, 0 );
3148 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
3149 if (!INT21_CreateFile( context, context->Esi, TRUE,
3150 BX_reg(context), DL_reg(context) ))
3151 bSetDOSExtendedError = TRUE;
3154 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
3156 DWORD filename_len, flags;
3159 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
3161 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA);
3162 SET_AX( context, 0 );
3163 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3164 if (!GetVolumeInformationW( pathW, NULL, 0, NULL, &filename_len,
3167 INT_BARF( context, 0x21 );
3171 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
3172 SET_CX( context, filename_len );
3173 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
3174 WideCharToMultiByte(CP_OEMCP, 0, dstW, -1,
3175 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3180 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
3182 HGLOBAL16 h16 = BX_reg(context);
3185 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
3187 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
3189 if (!FindClose( *ptr )) bSetDOSExtendedError = TRUE;
3190 GlobalUnlock16( h16 );
3191 GlobalFree16( h16 );
3195 SetLastError( ERROR_INVALID_HANDLE );
3196 bSetDOSExtendedError = TRUE;
3201 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
3203 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
3204 BY_HANDLE_FILE_INFORMATION *info =
3205 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3207 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
3209 if (!GetFileInformationByHandle(handle, info))
3210 bSetDOSExtendedError = TRUE;
3214 case 0xa7: /* LONG FILENAME - CONVERT TIME */
3215 switch (BL_reg(context))
3217 case 0x00: /* FILE TIME TO DOS TIME */
3220 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3224 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
3226 FileTimeToDosDateTime( filetime, &date, &time );
3228 SET_DX( context, date );
3229 SET_CX( context, time );
3232 * FIXME: BH has number of 10-millisecond units
3235 SET_BH( context, 0 );
3239 case 0x01: /* DOS TIME TO FILE TIME */
3241 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3245 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
3248 * FIXME: BH has number of 10-millisecond units
3251 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
3257 INT_BARF( context, 0x21 );
3262 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
3263 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3264 case 0xaa: /* LONG FILENAME - SUBST */
3266 FIXME("Unimplemented long file name function:\n");
3267 INT_BARF( context, 0x21 );
3269 SET_AL( context, 0 );
3273 if (bSetDOSExtendedError)
3275 SET_AX( context, GetLastError() );
3276 SET_CFLAG( context );
3281 /***********************************************************************
3286 * - subfunction 0x56 of function 0x71
3287 * - subfunction 0xff of function 0x43 (CL == 0x56)
3289 static BOOL INT21_RenameFile( CONTEXT86 *context )
3291 WCHAR fromW[MAX_PATH];
3292 WCHAR toW[MAX_PATH];
3293 char *fromA = CTX_SEG_OFF_TO_LIN(context,
3294 context->SegDs,context->Edx);
3295 char *toA = CTX_SEG_OFF_TO_LIN(context,
3296 context->SegEs,context->Edi);
3298 TRACE( "RENAME FILE %s to %s\n", fromA, toA );
3299 MultiByteToWideChar(CP_OEMCP, 0, fromA, -1, fromW, MAX_PATH);
3300 MultiByteToWideChar(CP_OEMCP, 0, toA, -1, toW, MAX_PATH);
3302 return MoveFileW( fromW, toW );
3306 /***********************************************************************
3312 static BOOL INT21_NetworkFunc (CONTEXT86 *context)
3314 switch (AL_reg(context))
3316 case 0x00: /* Get machine name. */
3318 WCHAR dstW[MAX_COMPUTERNAME_LENGTH + 1];
3319 DWORD s = sizeof(dstW) / sizeof(WCHAR);
3322 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
3323 TRACE("getting machine name to %p\n", dst);
3324 if (!GetComputerNameW(dstW, &s) ||
3325 !WideCharToMultiByte(CP_OEMCP, 0, dstW, -1, dst, 16, NULL, NULL))
3328 SetLastError( ER_NoNetwork );
3331 for (len = strlen(dst); len < 15; len++) dst[len] = ' ';
3333 SET_CH( context, 1 ); /* Valid */
3334 SET_CL( context, 1 ); /* NETbios number??? */
3335 TRACE("returning %s\n", debugstr_an(dst, 16));
3340 SetLastError( ER_NoNetwork );
3345 /******************************************************************
3346 * INT21_GetDiskSerialNumber
3349 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
3351 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3352 WCHAR path[] = {'A',':',0}, label[11];
3355 path[0] += INT21_MapDrive(BL_reg(context));
3356 if (!GetVolumeInformationW( path, label, 11, &serial, NULL, NULL, NULL, 0))
3358 SetLastError( ERROR_INVALID_DRIVE );
3362 *(WORD *)dataptr = 0;
3363 memcpy(dataptr + 2, &serial, sizeof(DWORD));
3364 WideCharToMultiByte(CP_OEMCP, 0, label, 11, dataptr + 6, 11, NULL, NULL);
3365 strncpy(dataptr + 17, "FAT16 ", 8);
3370 /******************************************************************
3371 * INT21_SetDiskSerialNumber
3374 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
3377 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3378 int drive = INT21_MapDrive(BL_reg(context));
3380 if (!is_valid_drive(drive))
3382 SetLastError( ERROR_INVALID_DRIVE );
3386 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
3389 FIXME("Setting drive serial number is no longer supported\n");
3390 SetLastError( ERROR_NOT_SUPPORTED );
3396 /******************************************************************
3397 * INT21_GetFreeDiskSpace
3400 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
3402 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
3403 WCHAR root[] = {'A',':','\\',0};
3405 root[0] += INT21_MapDrive(DL_reg(context));
3406 if (!GetDiskFreeSpaceW( root, &cluster_sectors, §or_bytes,
3407 &free_clusters, &total_clusters )) return 0;
3408 /* make sure that the number of clusters fits in a 16 bits value */
3409 while( total_clusters & 0xffff0000) {
3410 cluster_sectors <<= 1;
3411 free_clusters >>= 1;
3412 total_clusters >>= 1;
3414 SET_AX( context, cluster_sectors );
3415 SET_BX( context, free_clusters );
3416 SET_CX( context, sector_bytes );
3417 SET_DX( context, total_clusters );
3421 /******************************************************************
3422 * INT21_GetDriveAllocInfo
3425 static int INT21_GetDriveAllocInfo( CONTEXT86 *context, int drive )
3429 if (!INT21_FillDrivePB( drive )) return 0;
3430 dpb = &(INT21_GetHeapPointer()->misc_dpb_list[drive]);
3431 SET_AL( context, dpb->cluster_sectors + 1 );
3432 SET_CX( context, dpb->sector_bytes );
3433 SET_DX( context, dpb->num_clusters1 );
3435 context->SegDs = INT21_GetHeapSelector( context );
3436 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive].media_ID ) );
3440 /***********************************************************************
3441 * INT21_GetExtendedError
3443 static void INT21_GetExtendedError( CONTEXT86 *context )
3445 BYTE class, action, locus;
3446 WORD error = GetLastError();
3451 class = action = locus = 0;
3453 case ERROR_DIR_NOT_EMPTY:
3458 case ERROR_ACCESS_DENIED:
3459 class = EC_AccessDenied;
3463 case ERROR_CANNOT_MAKE:
3464 class = EC_AccessDenied;
3468 case ERROR_DISK_FULL:
3469 case ERROR_HANDLE_DISK_FULL:
3470 class = EC_MediaError;
3474 case ERROR_FILE_EXISTS:
3475 case ERROR_ALREADY_EXISTS:
3480 case ERROR_FILE_NOT_FOUND:
3481 class = EC_NotFound;
3485 case ERROR_GEN_FAILURE:
3486 class = EC_SystemFailure;
3490 case ERROR_INVALID_DRIVE:
3491 class = EC_MediaError;
3495 case ERROR_INVALID_HANDLE:
3496 class = EC_ProgramError;
3500 case ERROR_LOCK_VIOLATION:
3501 class = EC_AccessDenied;
3505 case ERROR_NO_MORE_FILES:
3506 class = EC_MediaError;
3511 class = EC_NotFound;
3515 case ERROR_NOT_ENOUGH_MEMORY:
3516 class = EC_OutOfResource;
3520 case ERROR_PATH_NOT_FOUND:
3521 class = EC_NotFound;
3526 class = EC_NotFound;
3530 case ERROR_SHARING_VIOLATION:
3531 class = EC_Temporary;
3535 case ERROR_TOO_MANY_OPEN_FILES:
3536 class = EC_ProgramError;
3541 FIXME("Unknown error %d\n", error );
3542 class = EC_SystemFailure;
3547 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3548 error, class, action, locus );
3549 SET_AX( context, error );
3550 SET_BH( context, class );
3551 SET_BL( context, action );
3552 SET_CH( context, locus );
3555 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
3557 static int counter = 0;
3558 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
3559 char *p = name + strlen(name);
3561 /* despite what Ralf Brown says, some programs seem to call without
3562 * ending backslash (DOS accepts that, so we accept it too) */
3563 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
3567 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
3568 counter = (counter + 1) % 1000;
3571 Win32HandleToDosFileHandle(
3572 CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
3573 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
3574 CREATE_NEW, 0, 0 ) ) );
3575 if (AX_reg(context) != HFILE_ERROR16)
3577 TRACE("created %s\n", name );
3580 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
3584 /***********************************************************************
3585 * DOSFS_ToDosFCBFormat
3587 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3588 * expanding wild cards and converting to upper-case in the process.
3589 * File name can be terminated by '\0', '\\' or '/'.
3590 * Return FALSE if the name is not a valid DOS name.
3591 * 'buffer' must be at least 12 characters long.
3593 /* Chars we don't want to see in DOS file names */
3594 static BOOL INT21_ToDosFCBFormat( LPCWSTR name, LPWSTR buffer )
3596 static const WCHAR invalid_chars[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3600 /* Check for "." and ".." */
3605 for(i = 1; i < 11; i++) buffer[i] = ' ';
3612 return (!*p || (*p == '/') || (*p == '\\'));
3615 for (i = 0; i < 8; i++)
3632 if (strchrW( invalid_chars, *p )) return FALSE;
3633 buffer[i] = toupperW(*p);
3641 /* Skip all chars after wildcard up to first dot */
3642 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
3646 /* Check if name too long */
3647 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return FALSE;
3649 if (*p == '.') p++; /* Skip dot */
3651 for (i = 8; i < 11; i++)
3661 return FALSE; /* Second extension not allowed */
3669 if (strchrW( invalid_chars, *p )) return FALSE;
3670 buffer[i] = toupperW(*p);
3677 /* at most 3 character of the extension are processed
3678 * is something behind this ?
3680 while (*p == '*' || *p == ' ') p++; /* skip wildcards and spaces */
3681 return (!*p || (*p == '/') || (*p == '\\'));
3684 static HANDLE INT21_FindHandle;
3685 static const WCHAR *INT21_FindPath; /* will point to current dta->fullPath search */
3687 /******************************************************************
3690 static int INT21_FindFirst( CONTEXT86 *context )
3694 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3695 WCHAR maskW[12], pathW[MAX_PATH];
3696 static const WCHAR wildcardW[] = {'*','.','*',0};
3698 path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3699 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
3701 p = strrchrW( pathW, '\\');
3704 if (pathW[0] && pathW[1] == ':') p = pathW + 2;
3709 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3710 * (doesn't matter as it is set below anyway)
3712 if (!INT21_ToDosFCBFormat( p, maskW ))
3714 SetLastError( ERROR_FILE_NOT_FOUND );
3715 SET_AX( context, ERROR_FILE_NOT_FOUND );
3719 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
3721 dta->fullPath = HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW) + (p - pathW)*sizeof(WCHAR) );
3722 memcpy( dta->fullPath, pathW, (p - pathW) * sizeof(WCHAR) );
3723 memcpy( dta->fullPath + (p - pathW), wildcardW, sizeof(wildcardW) );
3724 /* we must have a fully qualified file name in dta->fullPath
3725 * (we could have a UNC path, but this would lead to some errors later on)
3727 dta->drive = toupperW(dta->fullPath[0]) - 'A';
3729 dta->search_attr = CL_reg(context);
3733 /******************************************************************
3736 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3738 static BOOL match_short(LPCWSTR shortW, LPCSTR maskA)
3740 WCHAR mask[11], file[12];
3743 if (!INT21_ToDosFCBFormat( shortW, file )) return FALSE;
3744 MultiByteToWideChar(CP_OEMCP, 0, maskA, 11, mask, 11);
3745 for (i = 0; i < 11; i++)
3746 if (mask[i] != '?' && mask[i] != file[i]) return FALSE;
3750 static unsigned INT21_FindHelper(LPCWSTR fullPath, unsigned drive, unsigned count,
3751 LPCSTR mask, unsigned search_attr,
3752 WIN32_FIND_DATAW* entry)
3756 if ((search_attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
3758 WCHAR path[] = {' ',':',0};
3760 if (count) return 0;
3761 path[0] = drive + 'A';
3762 if (!GetVolumeInformationW(path, entry->cAlternateFileName, 13, NULL, NULL, NULL, NULL, 0)) return 0;
3763 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftCreationTime );
3764 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftLastAccessTime );
3765 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftLastWriteTime );
3766 entry->dwFileAttributes = FA_LABEL;
3767 entry->nFileSizeHigh = entry->nFileSizeLow = 0;
3768 TRACE("returning %s as label\n", debugstr_w(entry->cAlternateFileName));
3772 if (!INT21_FindHandle || INT21_FindPath != fullPath || count == 0)
3774 if (INT21_FindHandle) FindClose(INT21_FindHandle);
3775 INT21_FindHandle = FindFirstFileW(fullPath, entry);
3776 if (INT21_FindHandle == INVALID_HANDLE_VALUE)
3778 INT21_FindHandle = 0;
3781 INT21_FindPath = fullPath;
3782 /* we need to resync search */
3787 while (ncalls-- != 0)
3789 if (!FindNextFileW(INT21_FindHandle, entry))
3791 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3795 while (count < 0xffff)
3798 /* Check the file attributes, and path */
3799 if (!(entry->dwFileAttributes & ~search_attr) &&
3800 match_short(entry->cAlternateFileName[0] ? entry->cAlternateFileName : entry->cFileName,
3805 if (!FindNextFileW(INT21_FindHandle, entry))
3807 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3811 WARN("Too many directory entries in %s\n", debugstr_w(fullPath) );
3815 /******************************************************************
3818 static int INT21_FindNext( CONTEXT86 *context )
3820 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3821 DWORD attr = dta->search_attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
3822 WIN32_FIND_DATAW entry;
3825 if (!dta->fullPath) return 0;
3827 n = INT21_FindHelper(dta->fullPath, dta->drive, dta->count,
3828 dta->mask, attr, &entry);
3831 dta->fileattr = entry.dwFileAttributes;
3832 dta->filesize = entry.nFileSizeLow;
3833 FileTimeToDosDateTime( &entry.ftLastWriteTime, &dta->filedate, &dta->filetime );
3834 if (entry.cAlternateFileName[0])
3835 WideCharToMultiByte(CP_OEMCP, 0, entry.cAlternateFileName, -1,
3836 dta->filename, 13, NULL, NULL);
3838 WideCharToMultiByte(CP_OEMCP, 0, entry.cFileName, -1, dta->filename, 13, NULL, NULL);
3840 if (!memchr(dta->mask,'?',11))
3842 /* wildcardless search, release resources in case no findnext will
3843 * be issued, and as a workaround in case file creation messes up
3844 * findnext, as sometimes happens with pkunzip
3846 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3847 INT21_FindPath = dta->fullPath = NULL;
3852 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3853 INT21_FindPath = dta->fullPath = NULL;
3857 /* microsoft's programmers should be shot for using CP/M style int21
3858 calls in Windows for Workgroup's winfile.exe */
3860 /******************************************************************
3861 * INT21_FindFirstFCB
3864 static int INT21_FindFirstFCB( CONTEXT86 *context )
3866 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3869 WCHAR p[] = {' ',':',};
3871 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
3872 else pFCB = (FINDFILE_FCB *)fcb;
3873 drive = INT21_MapDrive( pFCB->drive );
3874 if (drive == MAX_DOS_DRIVES) return 0;
3877 pFCB->fullPath = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
3878 if (!pFCB->fullPath) return 0;
3879 GetLongPathNameW(p, pFCB->fullPath, MAX_PATH);
3884 /******************************************************************
3888 static int INT21_FindNextFCB( CONTEXT86 *context )
3890 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3892 LPBYTE pResult = INT21_GetCurrentDTA(context);
3893 DOS_DIRENTRY_LAYOUT *ddl;
3894 WIN32_FIND_DATAW entry;
3899 if (*fcb == 0xff) /* extended FCB ? */
3902 pFCB = (FINDFILE_FCB *)(fcb + 7);
3907 pFCB = (FINDFILE_FCB *)fcb;
3910 if (!pFCB->fullPath) return 0;
3911 n = INT21_FindHelper(pFCB->fullPath, INT21_MapDrive( pFCB->drive ),
3912 pFCB->count, pFCB->filename, attr, &entry);
3915 HeapFree( GetProcessHeap(), 0, pFCB->fullPath );
3916 INT21_FindPath = pFCB->fullPath = NULL;
3923 /* place extended FCB header before pResult if called with extended FCB */
3925 pResult += 6; /* leave reserved field behind */
3926 *pResult++ = entry.dwFileAttributes;
3928 *pResult++ = INT21_MapDrive( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
3929 ddl = (DOS_DIRENTRY_LAYOUT*)pResult;
3930 ddl->fileattr = entry.dwFileAttributes;
3931 ddl->cluster = 0; /* what else? */
3932 ddl->filesize = entry.nFileSizeLow;
3933 memset( ddl->reserved, 0, sizeof(ddl->reserved) );
3934 FileTimeToDosDateTime( &entry.ftLastWriteTime,
3935 &ddl->filedate, &ddl->filetime );
3937 /* Convert file name to FCB format */
3938 if (entry.cAlternateFileName[0])
3939 INT21_ToDosFCBFormat( entry.cAlternateFileName, nameW );
3941 INT21_ToDosFCBFormat( entry.cFileName, nameW );
3942 WideCharToMultiByte(CP_OEMCP, 0, nameW, 11, ddl->filename, 11, NULL, NULL);
3947 /******************************************************************
3948 * INT21_ParseFileNameIntoFCB
3951 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
3954 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
3956 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
3960 INT buffer_len, len;
3962 SET_AL( context, 0xff ); /* failed */
3964 TRACE("filename: '%s'\n", filename);
3967 while (*s && (*s != ' ') && (*s != '\r') && (*s != '\n'))
3971 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
3972 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
3973 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
3975 INT21_ToDosFCBFormat(buffer, fcbW);
3976 HeapFree(GetProcessHeap(), 0, buffer);
3977 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
3979 TRACE("FCB: '%s'\n", fcb + 1);
3981 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
3983 /* point DS:SI to first unparsed character */
3984 SET_SI( context, context->Esi + (int)s - (int)filename );
3987 static BOOL INT21_Dup2(HFILE16 hFile1, HFILE16 hFile2)
3989 HFILE16 res = HFILE_ERROR16;
3990 HANDLE handle, new_handle;
3991 #define DOS_TABLE_SIZE 256
3992 DWORD map[DOS_TABLE_SIZE / 32];
3995 handle = DosFileHandleToWin32Handle(hFile1);
3996 if (handle == INVALID_HANDLE_VALUE)
4000 /* now loop to allocate the same one... */
4001 memset(map, 0, sizeof(map));
4002 for (i = 0; i < DOS_TABLE_SIZE; i++)
4004 if (!DuplicateHandle(GetCurrentProcess(), handle,
4005 GetCurrentProcess(), &new_handle,
4006 0, FALSE, DUPLICATE_SAME_ACCESS))
4008 res = HFILE_ERROR16;
4011 res = Win32HandleToDosFileHandle(new_handle);
4012 if (res == HFILE_ERROR16 || res == hFile2) break;
4013 map[res / 32] |= 1 << (res % 32);
4015 /* clean up the allocated slots */
4016 for (i = 0; i < DOS_TABLE_SIZE; i++)
4018 if (map[i / 32] & (1 << (i % 32)))
4019 _lclose16((HFILE16)i);
4021 return res == hFile2;
4025 /***********************************************************************
4026 * DOSVM_Int21Handler
4028 * Interrupt 0x21 handler.
4030 void WINAPI DOSVM_Int21Handler( CONTEXT86 *context )
4032 BOOL bSetDOSExtendedError = FALSE;
4034 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
4035 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
4036 AX_reg(context), BX_reg(context),
4037 CX_reg(context), DX_reg(context),
4038 SI_reg(context), DI_reg(context),
4039 (WORD)context->SegDs, (WORD)context->SegEs,
4043 * Extended error is used by (at least) functions 0x2f to 0x62.
4044 * Function 0x59 returns extended error and error should not
4045 * be cleared before handling the function.
4047 if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59)
4050 RESET_CFLAG(context); /* Not sure if this is a good idea. */
4052 switch(AH_reg(context))
4054 case 0x00: /* TERMINATE PROGRAM */
4055 TRACE("TERMINATE PROGRAM\n");
4056 if (DOSVM_IsWin16())
4058 else if(ISV86(context))
4059 MZ_Exit( context, FALSE, 0 );
4061 ERR( "Called from DOS protected mode\n" );
4064 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
4067 TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
4068 INT21_ReadChar( &ascii, context );
4069 SET_AL( context, ascii );
4071 * FIXME: What to echo when extended keycodes are read?
4073 DOSVM_PutChar(AL_reg(context));
4077 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
4078 TRACE("Write Character to Standard Output\n");
4079 DOSVM_PutChar(DL_reg(context));
4082 case 0x03: /* READ CHARACTER FROM STDAUX */
4083 case 0x04: /* WRITE CHARACTER TO STDAUX */
4084 case 0x05: /* WRITE CHARACTER TO PRINTER */
4085 INT_BARF( context, 0x21 );
4088 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
4089 if (DL_reg(context) == 0xff)
4091 TRACE("Direct Console Input\n");
4093 if (INT21_ReadChar( NULL, NULL ))
4096 INT21_ReadChar( &ascii, context );
4097 SET_AL( context, ascii );
4098 RESET_ZFLAG( context );
4102 /* no character available */
4103 SET_AL( context, 0 );
4104 SET_ZFLAG( context );
4109 TRACE("Direct Console Output\n");
4110 DOSVM_PutChar(DL_reg(context));
4112 * At least DOS versions 2.1-7.0 return character
4113 * that was written in AL register.
4115 SET_AL( context, DL_reg(context) );
4119 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
4122 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
4123 INT21_ReadChar( &ascii, context );
4124 SET_AL( context, ascii );
4128 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
4131 TRACE("CHARACTER INPUT WITHOUT ECHO\n");
4132 INT21_ReadChar( &ascii, context );
4133 SET_AL( context, ascii );
4137 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
4138 TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
4139 context->SegDs, DX_reg(context) );
4141 LPSTR data = CTX_SEG_OFF_TO_LIN( context,
4142 context->SegDs, context->Edx );
4146 * Do NOT use strchr() to calculate the string length,
4147 * as '\0' is valid string content, too!
4148 * Maybe we should check for non-'$' strings, but DOS doesn't.
4150 while (*p != '$') p++;
4152 if (DOSVM_IsWin16())
4153 WriteFile( DosFileHandleToWin32Handle(1),
4154 data, p - data, 0, 0 );
4156 for(; data != p; data++)
4157 DOSVM_PutChar( *data );
4159 SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
4163 case 0x0a: /* BUFFERED INPUT */
4165 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context,
4170 TRACE( "BUFFERED INPUT (size=%d)\n", ptr[0] );
4173 * FIXME: Some documents state that
4174 * ptr[1] holds number of chars from last input which
4175 * may be recalled on entry, other documents do not mention
4179 TRACE( "Handle old chars in buffer!\n" );
4182 * ptr[0] - capacity (includes terminating CR)
4183 * ptr[1] - characters read (excludes terminating CR)
4185 result = INT21_BufferedInput( context, ptr + 2, ptr[0] );
4187 ptr[1] = (BYTE)result - 1;
4193 case 0x0b: /* GET STDIN STATUS */
4194 TRACE( "GET STDIN STATUS\n" );
4196 if (INT21_ReadChar( NULL, NULL ))
4197 SET_AL( context, 0xff ); /* character available */
4199 SET_AL( context, 0 ); /* no character available */
4203 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
4205 BYTE al = AL_reg(context); /* Input function to execute after flush. */
4207 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
4209 /* FIXME: buffers are not flushed */
4212 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
4213 * int21 function identified by AL will be called.
4215 if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
4217 SET_AH( context, al );
4218 DOSVM_Int21Handler( context );
4223 case 0x0d: /* DISK BUFFER FLUSH */
4224 TRACE("DISK BUFFER FLUSH ignored\n");
4227 case 0x0e: /* SELECT DEFAULT DRIVE */
4228 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context) );
4229 INT21_SetCurrentDrive( DL_reg(context) );
4230 SET_AL( context, MAX_DOS_DRIVES );
4233 case 0x0f: /* OPEN FILE USING FCB */
4234 INT21_OpenFileUsingFCB( context );
4237 case 0x10: /* CLOSE FILE USING FCB */
4238 INT21_CloseFileUsingFCB( context );
4241 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
4242 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
4243 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
4244 if (!INT21_FindFirstFCB(context))
4246 SET_AL( context, 0xff );
4249 /* else fall through */
4251 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
4252 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
4255 case 0x13: /* DELETE FILE USING FCB */
4256 INT_BARF( context, 0x21 );
4259 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
4260 INT21_SequentialReadFromFCB( context );
4263 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
4264 INT21_SequentialWriteToFCB( context );
4267 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
4268 case 0x17: /* RENAME FILE USING FCB */
4269 INT_BARF( context, 0x21 );
4272 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4273 SET_AL( context, 0 );
4276 case 0x19: /* GET CURRENT DEFAULT DRIVE */
4277 SET_AL( context, INT21_GetCurrentDrive() );
4278 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context ) );
4281 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
4282 TRACE( "SET DISK TRANSFER AREA ADDRESS %04lX:%04X\n",
4283 context->SegDs, DX_reg(context) );
4285 TDB *task = GlobalLock16( GetCurrentTask() );
4286 task->dta = MAKESEGPTR( context->SegDs, DX_reg(context) );
4290 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4291 if (!INT21_GetDriveAllocInfo(context, 0))
4292 SET_AX( context, 0xffff );
4295 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4296 if (!INT21_GetDriveAllocInfo(context, DL_reg(context)))
4297 SET_AX( context, 0xffff );
4300 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4301 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4302 SET_AL( context, 0 );
4305 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4307 BYTE drive = INT21_MapDrive( 0 );
4308 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4310 if (INT21_FillDrivePB( drive ))
4312 SET_AL( context, 0x00 ); /* success */
4313 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4314 context->SegDs = INT21_GetHeapSelector( context );
4318 SET_AL( context, 0xff ); /* invalid or network drive */
4323 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4324 SET_AL( context, 0 );
4327 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4328 INT21_ReadRandomRecordFromFCB( context );
4331 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4332 INT21_WriteRandomRecordToFCB( context );
4335 case 0x23: /* GET FILE SIZE FOR FCB */
4336 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4337 INT_BARF( context, 0x21 );
4340 case 0x25: /* SET INTERRUPT VECTOR */
4341 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4343 FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
4344 if (!ISV86(context) && DOSVM_IsWin16())
4345 DOSVM_SetPMHandler16( AL_reg(context), ptr );
4347 DOSVM_SetRMHandler( AL_reg(context), ptr );
4351 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4352 INT_BARF( context, 0x21 );
4355 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4356 INT21_RandomBlockReadFromFCB( context );
4359 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4360 INT21_RandomBlockWriteToFCB( context );
4363 case 0x29: /* PARSE FILENAME INTO FCB */
4364 INT21_ParseFileNameIntoFCB(context);
4367 case 0x2a: /* GET SYSTEM DATE */
4368 TRACE( "GET SYSTEM DATE\n" );
4371 GetLocalTime( &systime );
4372 SET_CX( context, systime.wYear );
4373 SET_DH( context, systime.wMonth );
4374 SET_DL( context, systime.wDay );
4375 SET_AL( context, systime.wDayOfWeek );
4379 case 0x2b: /* SET SYSTEM DATE */
4380 TRACE( "SET SYSTEM DATE\n" );
4382 WORD year = CX_reg(context);
4383 BYTE month = DH_reg(context);
4384 BYTE day = DL_reg(context);
4386 if (year >= 1980 && year <= 2099 &&
4387 month >= 1 && month <= 12 &&
4388 day >= 1 && day <= 31)
4390 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4392 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4396 SET_AL( context, 0xff ); /* invalid date */
4397 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4403 case 0x2c: /* GET SYSTEM TIME */
4404 TRACE( "GET SYSTEM TIME\n" );
4407 GetLocalTime( &systime );
4408 SET_CH( context, systime.wHour );
4409 SET_CL( context, systime.wMinute );
4410 SET_DH( context, systime.wSecond );
4411 SET_DL( context, systime.wMilliseconds / 10 );
4415 case 0x2d: /* SET SYSTEM TIME */
4416 if( CH_reg(context) >= 24 || CL_reg(context) >= 60 || DH_reg(context) >= 60 || DL_reg(context) >= 100 ) {
4417 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4418 CH_reg(context), CL_reg(context),
4419 DH_reg(context), DL_reg(context) );
4420 SET_AL( context, 0xFF );
4424 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4425 CH_reg(context), CL_reg(context),
4426 DH_reg(context), DL_reg(context) );
4427 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4431 case 0x2e: /* SET VERIFY FLAG */
4432 TRACE("SET VERIFY FLAG ignored\n");
4433 /* we cannot change the behaviour anyway, so just ignore it */
4436 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4437 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4439 TDB *task = GlobalLock16( GetCurrentTask() );
4440 context->SegEs = SELECTOROF( task->dta );
4441 SET_BX( context, OFFSETOF( task->dta ) );
4445 case 0x30: /* GET DOS VERSION */
4446 TRACE( "GET DOS VERSION - %s requested\n",
4447 (AL_reg(context) == 0x00) ? "OEM number" : "version flag" );
4449 if (AL_reg(context) == 0x00)
4450 SET_BH( context, 0xff ); /* OEM number => undefined */
4452 SET_BH( context, 0x08 ); /* version flag => DOS is in ROM */
4454 SET_AL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4455 SET_AH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4457 SET_BL( context, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4458 SET_CX( context, 0x3456 );
4461 case 0x31: /* TERMINATE AND STAY RESIDENT */
4462 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4465 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4467 BYTE drive = INT21_MapDrive( DL_reg(context) );
4468 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context) );
4470 if (INT21_FillDrivePB( drive ))
4472 SET_AL( context, 0x00 ); /* success */
4473 SET_DX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4474 context->SegDs = INT21_GetHeapSelector( context );
4478 SET_AL( context, 0xff ); /* invalid or network drive */
4483 case 0x33: /* MULTIPLEXED */
4484 switch (AL_reg(context))
4486 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4487 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4488 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4491 case 0x01: /* SET EXTENDED BREAK STATE */
4492 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4493 DOSCONF_GetConfig()->brk_flag = (DL_reg(context) > 0) ? 1 : 0;
4496 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4497 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4498 /* ugly coding in order to stay reentrant */
4499 if (DL_reg(context))
4501 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4502 DOSCONF_GetConfig()->brk_flag = 1;
4506 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4507 DOSCONF_GetConfig()->brk_flag = 0;
4511 case 0x05: /* GET BOOT DRIVE */
4512 TRACE("GET BOOT DRIVE\n");
4513 SET_DL( context, 3 );
4514 /* c: is Wine's bootdrive (a: is 1)*/
4517 case 0x06: /* GET TRUE VERSION NUMBER */
4518 TRACE("GET TRUE VERSION NUMBER\n");
4519 SET_BL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4520 SET_BH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4521 SET_DL( context, 0x00 ); /* revision */
4522 SET_DH( context, 0x08 ); /* DOS is in ROM */
4526 INT_BARF( context, 0x21 );
4531 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4532 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4533 context->SegEs = INT21_GetHeapSelector( context );
4534 SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
4537 case 0x35: /* GET INTERRUPT VECTOR */
4538 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4541 if (!ISV86(context) && DOSVM_IsWin16())
4542 addr = DOSVM_GetPMHandler16( AL_reg(context) );
4544 addr = DOSVM_GetRMHandler( AL_reg(context) );
4545 context->SegEs = SELECTOROF(addr);
4546 SET_BX( context, OFFSETOF(addr) );
4550 case 0x36: /* GET FREE DISK SPACE */
4551 TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
4552 INT21_DriveName( DL_reg(context) ));
4553 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
4556 case 0x37: /* SWITCHAR */
4558 switch (AL_reg(context))
4560 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4561 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4562 SET_AL( context, 0x00 ); /* success*/
4563 SET_DL( context, '/' );
4565 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4566 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4568 SET_AL( context, 0x00 ); /* success*/
4571 INT_BARF( context, 0x21 );
4577 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4578 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4579 if (AL_reg(context))
4581 WORD country = AL_reg(context);
4582 if (country == 0xff)
4583 country = BX_reg(context);
4584 if (country != INT21_GetSystemCountryCode()) {
4585 FIXME( "Requested info on non-default country %04x\n", country );
4590 if(AX_reg(context) != 2 )
4592 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context,
4595 SET_AX( context, INT21_GetSystemCountryCode() );
4596 SET_BX( context, INT21_GetSystemCountryCode() );
4597 RESET_CFLAG(context);
4601 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4602 if (!INT21_CreateDirectory( context ))
4603 bSetDOSExtendedError = TRUE;
4605 RESET_CFLAG(context);
4608 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4610 WCHAR dirW[MAX_PATH];
4611 char *dirA = CTX_SEG_OFF_TO_LIN(context,
4612 context->SegDs, context->Edx);
4614 TRACE( "REMOVE DIRECTORY %s\n", dirA );
4616 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
4618 if (!RemoveDirectoryW( dirW ))
4619 bSetDOSExtendedError = TRUE;
4621 RESET_CFLAG(context);
4625 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4626 if (!INT21_SetCurrentDirectory( context ))
4627 bSetDOSExtendedError = TRUE;
4629 RESET_CFLAG(context);
4632 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4633 if (!INT21_CreateFile( context, context->Edx, FALSE,
4634 OF_READWRITE | OF_SHARE_COMPAT, 0x12 ))
4635 bSetDOSExtendedError = TRUE;
4637 RESET_CFLAG(context);
4640 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4641 if (!INT21_CreateFile( context, context->Edx, FALSE,
4642 AL_reg(context), 0x01 ))
4643 bSetDOSExtendedError = TRUE;
4645 RESET_CFLAG(context);
4648 case 0x3e: /* "CLOSE" - CLOSE FILE */
4649 TRACE( "CLOSE handle %d\n", BX_reg(context) );
4650 if (_lclose16( BX_reg(context) ) == HFILE_ERROR16)
4651 bSetDOSExtendedError = TRUE;
4653 RESET_CFLAG(context);
4656 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4657 TRACE( "READ from %d to %04lX:%04X for %d bytes\n",
4664 WORD count = CX_reg(context);
4665 BYTE *buffer = CTX_SEG_OFF_TO_LIN( context,
4669 /* Some programs pass a count larger than the allocated buffer */
4670 if (DOSVM_IsWin16())
4672 WORD maxcount = GetSelectorLimit16( context->SegDs )
4673 - DX_reg(context) + 1;
4674 if (count > maxcount)
4679 * FIXME: Reading from console (BX=1) in DOS mode
4680 * does not work as it is supposed to work.
4683 RESET_CFLAG(context); /* set if error */
4684 if (!DOSVM_IsWin16() && BX_reg(context) == 0)
4686 result = INT21_BufferedInput( context, buffer, count );
4687 SET_AX( context, (WORD)result );
4689 else if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context)),
4690 buffer, count, &result, NULL ))
4691 SET_AX( context, (WORD)result );
4693 bSetDOSExtendedError = TRUE;
4697 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4698 TRACE( "WRITE from %04lX:%04X to handle %d for %d byte\n",
4699 context->SegDs, DX_reg(context),
4700 BX_reg(context), CX_reg(context) );
4702 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4704 if (!DOSVM_IsWin16() &&
4705 (BX_reg(context) == 1 || BX_reg(context) == 2))
4708 for(i=0; i<CX_reg(context); i++)
4709 DOSVM_PutChar(ptr[i]);
4710 SET_AX(context, CX_reg(context));
4711 RESET_CFLAG(context);
4715 HFILE handle = (HFILE)DosFileHandleToWin32Handle(BX_reg(context));
4716 LONG result = _hwrite( handle, ptr, CX_reg(context) );
4717 if (result == HFILE_ERROR)
4718 bSetDOSExtendedError = TRUE;
4721 SET_AX( context, (WORD)result );
4722 RESET_CFLAG(context);
4728 case 0x41: /* "UNLINK" - DELETE FILE */
4730 WCHAR fileW[MAX_PATH];
4731 char *fileA = CTX_SEG_OFF_TO_LIN(context,
4735 TRACE( "UNLINK %s\n", fileA );
4736 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
4738 if (!DeleteFileW( fileW ))
4739 bSetDOSExtendedError = TRUE;
4741 RESET_CFLAG(context);
4745 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4746 TRACE( "LSEEK handle %d offset %ld from %s\n",
4748 MAKELONG( DX_reg(context), CX_reg(context) ),
4749 (AL_reg(context) == 0) ?
4750 "start of file" : ((AL_reg(context) == 1) ?
4751 "current file position" : "end of file") );
4753 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4754 LONG offset = MAKELONG( DX_reg(context), CX_reg(context) );
4755 DWORD status = SetFilePointer( handle, offset,
4756 NULL, AL_reg(context) );
4757 if (status == INVALID_SET_FILE_POINTER)
4758 bSetDOSExtendedError = TRUE;
4761 SET_AX( context, LOWORD(status) );
4762 SET_DX( context, HIWORD(status) );
4763 RESET_CFLAG(context);
4768 case 0x43: /* FILE ATTRIBUTES */
4769 if (!INT21_FileAttributes( context, AL_reg(context), FALSE ))
4770 bSetDOSExtendedError = TRUE;
4772 RESET_CFLAG(context);
4775 case 0x44: /* IOCTL */
4776 INT21_Ioctl( context );
4779 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4780 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context) );
4783 HFILE handle16 = HFILE_ERROR;
4785 if (DuplicateHandle( GetCurrentProcess(),
4786 DosFileHandleToWin32Handle(BX_reg(context)),
4787 GetCurrentProcess(),
4789 0, TRUE, DUPLICATE_SAME_ACCESS ))
4790 handle16 = Win32HandleToDosFileHandle(handle32);
4792 if (handle16 == HFILE_ERROR)
4793 bSetDOSExtendedError = TRUE;
4796 SET_AX( context, handle16 );
4797 RESET_CFLAG(context);
4802 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4803 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4804 BX_reg(context), CX_reg(context) );
4805 if (!INT21_Dup2(BX_reg(context), CX_reg(context)))
4806 bSetDOSExtendedError = TRUE;
4808 RESET_CFLAG(context);
4811 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4812 if (!INT21_GetCurrentDirectory( context, FALSE ))
4813 bSetDOSExtendedError = TRUE;
4815 RESET_CFLAG(context);
4818 case 0x48: /* ALLOCATE MEMORY */
4819 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
4822 DWORD bytes = (DWORD)BX_reg(context) << 4;
4824 if (!ISV86(context) && DOSVM_IsWin16())
4826 DWORD rv = GlobalDOSAlloc16( bytes );
4827 selector = LOWORD( rv );
4830 DOSMEM_GetBlock( bytes, &selector );
4834 SET_AX( context, selector );
4835 RESET_CFLAG(context);
4840 SET_AX( context, 0x0008 ); /* insufficient memory */
4841 SET_BX( context, DOSMEM_Available() >> 4 );
4846 case 0x49: /* FREE MEMORY */
4847 TRACE( "FREE MEMORY segment %04lX\n", context->SegEs );
4851 if (!ISV86(context) && DOSVM_IsWin16())
4853 ok = !GlobalDOSFree16( context->SegEs );
4855 /* If we don't reset ES_reg, we will fail in the relay code */
4860 ok = DOSMEM_FreeBlock( (void*)((DWORD)context->SegEs << 4) );
4864 TRACE("FREE MEMORY failed\n");
4866 SET_AX( context, 0x0009 ); /* memory block address invalid */
4871 case 0x4a: /* RESIZE MEMORY BLOCK */
4872 TRACE( "RESIZE MEMORY segment %04lX to %d paragraphs\n",
4873 context->SegEs, BX_reg(context) );
4875 DWORD newsize = (DWORD)BX_reg(context) << 4;
4877 if (!ISV86(context) && DOSVM_IsWin16())
4879 FIXME( "Resize memory block - unsupported under Win16\n" );
4884 LPVOID address = (void*)((DWORD)context->SegEs << 4);
4885 UINT blocksize = DOSMEM_ResizeBlock( address, newsize, FALSE );
4887 RESET_CFLAG(context);
4888 if (blocksize == (UINT)-1)
4890 SET_CFLAG( context );
4891 SET_AX( context, 0x0009 ); /* illegal address */
4893 else if(blocksize != newsize)
4895 SET_CFLAG( context );
4896 SET_AX( context, 0x0008 ); /* insufficient memory */
4897 SET_BX( context, blocksize >> 4 ); /* new block size */
4903 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4905 BYTE *program = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4906 BYTE *paramblk = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx);
4908 TRACE( "EXEC %s\n", program );
4910 RESET_CFLAG(context);
4911 if (DOSVM_IsWin16())
4913 HINSTANCE16 instance = WinExec16( program, SW_NORMAL );
4916 SET_CFLAG( context );
4917 SET_AX( context, instance );
4922 if (!MZ_Exec( context, program, AL_reg(context), paramblk))
4923 bSetDOSExtendedError = TRUE;
4928 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
4929 TRACE( "EXIT with return code %d\n", AL_reg(context) );
4930 if (DOSVM_IsWin16())
4931 ExitThread( AL_reg(context) );
4932 else if(ISV86(context))
4933 MZ_Exit( context, FALSE, AL_reg(context) );
4939 DWORD rv = AL_reg(context);
4940 RaiseException( EXCEPTION_VM86_INTx, 0, 1, &rv );
4944 case 0x4d: /* GET RETURN CODE */
4945 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
4946 SET_AX( context, DOSVM_retval );
4950 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
4951 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
4952 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
4953 if (!INT21_FindFirst(context)) break;
4956 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
4957 TRACE("FINDNEXT\n");
4958 if (!INT21_FindNext(context))
4960 SetLastError( ERROR_NO_MORE_FILES );
4961 SET_AX( context, ERROR_NO_MORE_FILES );
4964 else SET_AX( context, 0 ); /* OK */
4967 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
4968 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
4969 DOSVM_psp = BX_reg(context);
4972 case 0x51: /* GET PSP ADDRESS */
4973 INT21_GetPSP( context );
4976 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
4978 SEGPTR ptr = DOSDEV_GetLOL( ISV86(context) || !DOSVM_IsWin16() );
4979 context->SegEs = SELECTOROF(ptr);
4980 SET_BX( context, OFFSETOF(ptr) );
4984 case 0x54: /* Get Verify Flag */
4985 TRACE("Get Verify Flag - Not Supported\n");
4986 SET_AL( context, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
4989 case 0x56: /* "RENAME" - RENAME FILE */
4990 if (!INT21_RenameFile( context ))
4991 bSetDOSExtendedError = TRUE;
4993 RESET_CFLAG(context);
4996 case 0x57: /* FILE DATE AND TIME */
4997 if (!INT21_FileDateTime( context ))
4998 bSetDOSExtendedError = TRUE;
5000 RESET_CFLAG(context);
5003 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
5004 switch (AL_reg(context))
5006 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
5007 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
5008 SET_AX( context, 0 ); /* low memory first fit */
5011 case 0x01: /* SET ALLOCATION STRATEGY */
5012 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
5016 case 0x02: /* GET UMB LINK STATE */
5017 TRACE( "GET UMB LINK STATE\n" );
5018 SET_AL( context, 0 ); /* UMBs not part of DOS memory chain */
5021 case 0x03: /* SET UMB LINK STATE */
5022 TRACE( "SET UMB LINK STATE to %d - ignored\n",
5027 INT_BARF( context, 0x21 );
5031 case 0x59: /* GET EXTENDED ERROR INFO */
5032 INT21_GetExtendedError( context );
5035 case 0x5a: /* CREATE TEMPORARY FILE */
5036 TRACE("CREATE TEMPORARY FILE\n");
5037 bSetDOSExtendedError = !INT21_CreateTempFile(context);
5040 case 0x5b: /* CREATE NEW FILE */
5041 if (!INT21_CreateFile( context, context->Edx, FALSE,
5042 OF_READWRITE | OF_SHARE_COMPAT, 0x10 ))
5043 bSetDOSExtendedError = TRUE;
5045 RESET_CFLAG(context);
5048 case 0x5c: /* "FLOCK" - RECORD LOCKING */
5050 DWORD offset = MAKELONG(DX_reg(context), CX_reg(context));
5051 DWORD length = MAKELONG(DI_reg(context), SI_reg(context));
5052 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
5054 RESET_CFLAG(context);
5055 switch (AL_reg(context))
5057 case 0x00: /* LOCK */
5058 TRACE( "lock handle %d offset %ld length %ld\n",
5059 BX_reg(context), offset, length );
5060 if (!LockFile( handle, offset, 0, length, 0 ))
5061 bSetDOSExtendedError = TRUE;
5064 case 0x01: /* UNLOCK */
5065 TRACE( "unlock handle %d offset %ld length %ld\n",
5066 BX_reg(context), offset, length );
5067 if (!UnlockFile( handle, offset, 0, length, 0 ))
5068 bSetDOSExtendedError = TRUE;
5072 INT_BARF( context, 0x21 );
5077 case 0x5d: /* NETWORK 5D */
5078 FIXME( "Network function 5D not implemented.\n" );
5079 SetLastError( ER_NoNetwork );
5080 bSetDOSExtendedError = TRUE;
5083 case 0x5e: /* NETWORK 5E */
5084 bSetDOSExtendedError = INT21_NetworkFunc( context);
5087 case 0x5f: /* NETWORK 5F */
5088 /* FIXME: supporting this would need to 1:
5089 * - implement per drive current directory (as kernel32 doesn't)
5090 * - assign enabled/disabled flag on a per drive basis
5092 /* network software not installed */
5093 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
5094 SetLastError( ER_NoNetwork );
5095 bSetDOSExtendedError = TRUE;
5098 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
5100 WCHAR pathW[MAX_PATH], res[MAX_PATH];
5101 /* FIXME: likely to be broken */
5103 TRACE("TRUENAME %s\n",
5104 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
5105 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
5106 if (!GetFullPathNameW( pathW, 128, res, NULL ))
5107 bSetDOSExtendedError = TRUE;
5110 SET_AX( context, 0 );
5111 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
5112 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
5118 case 0x61: /* UNUSED */
5119 SET_AL( context, 0 );
5122 case 0x62: /* GET PSP ADDRESS */
5123 INT21_GetPSP( context );
5126 case 0x63: /* MISC. LANGUAGE SUPPORT */
5127 switch (AL_reg(context)) {
5128 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
5129 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
5130 context->SegDs = INT21_GetHeapSelector( context );
5131 SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
5132 SET_AL( context, 0 ); /* success */
5137 case 0x64: /* OS/2 DOS BOX */
5138 INT_BARF( context, 0x21 );
5142 case 0x65: /* EXTENDED COUNTRY INFORMATION */
5143 INT21_ExtendedCountryInformation( context );
5146 case 0x66: /* GLOBAL CODE PAGE TABLE */
5147 switch (AL_reg(context))
5150 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
5151 SET_BX( context, GetOEMCP() );
5152 SET_DX( context, GetOEMCP() );
5155 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
5156 BX_reg(context), DX_reg(context) );
5161 case 0x67: /* SET HANDLE COUNT */
5162 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
5163 if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
5164 bSetDOSExtendedError = TRUE;
5167 case 0x68: /* "FFLUSH" - COMMIT FILE */
5168 TRACE( "FFLUSH - handle %d\n", BX_reg(context) );
5169 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
5170 bSetDOSExtendedError = TRUE;
5173 case 0x69: /* DISK SERIAL NUMBER */
5174 switch (AL_reg(context))
5177 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
5178 INT21_DriveName(BL_reg(context)));
5179 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
5180 else SET_AX( context, 0 );
5184 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
5185 INT21_DriveName(BL_reg(context)));
5186 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
5187 else SET_AX( context, 1 );
5192 case 0x6a: /* COMMIT FILE */
5193 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context) );
5194 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
5195 bSetDOSExtendedError = TRUE;
5198 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
5199 SET_AL( context, 0 );
5202 case 0x6c: /* EXTENDED OPEN/CREATE */
5203 if (!INT21_CreateFile( context, context->Esi, TRUE,
5204 BX_reg(context), DL_reg(context) ))
5205 bSetDOSExtendedError = TRUE;
5208 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
5209 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
5210 SET_CFLAG( context );
5211 SET_AL( context, 0 );
5214 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
5215 INT21_LongFilename( context );
5218 case 0x73: /* MSDOS7 - FAT32 */
5219 RESET_CFLAG( context );
5220 if (!INT21_Fat32( context ))
5221 bSetDOSExtendedError = TRUE;
5224 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
5225 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
5228 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
5229 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
5232 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
5233 /* we don't implement a DOS32 extender */
5234 TRACE( "DOS32 EXTENDER API - ignored\n" );
5238 INT_BARF( context, 0x21 );
5241 } /* END OF SWITCH */
5243 /* Set general error condition. */
5244 if (bSetDOSExtendedError)
5246 SET_AX( context, GetLastError() );
5247 SET_CFLAG( context );
5250 /* Print error code if carry flag is set. */
5251 if (context->EFlags & 0x0001)
5252 TRACE("failed, error %ld\n", GetLastError() );
5254 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
5255 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
5256 AX_reg(context), BX_reg(context),
5257 CX_reg(context), DX_reg(context),
5258 SI_reg(context), DI_reg(context),
5259 (WORD)context->SegDs, (WORD)context->SegEs,