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"
49 BOOL WINAPI VerifyConsoleIoHandle(HANDLE);
52 * - Most of the file related functions are wrong. NT's kernel32
53 * doesn't maintain a per drive current directory, while DOS does.
54 * We should in fact keep track in here of those per drive
55 * directories, and use this info while dealing with partial paths
56 * (drive defined, but only relative paths). This could even be
57 * created as an array of CDS (there should be an entry for that in
62 * Forward declarations.
64 static BOOL INT21_RenameFile( CONTEXT86 *context );
66 WINE_DEFAULT_DEBUG_CHANNEL(int21);
72 * Extended Drive Parameter Block.
73 * This structure is compatible with standard DOS4+ DPB and
76 typedef struct _INT21_DPB {
77 BYTE drive; /* 00 drive number (0=A, ...) */
78 BYTE unit; /* 01 unit number within device driver */
79 WORD sector_bytes; /* 02 bytes per sector */
80 BYTE cluster_sectors; /* 04 highest sector number within a cluster */
81 BYTE shift; /* 05 shift count to convert clusters into sectors */
82 WORD num_reserved; /* 06 reserved sectors at beginning of drive */
83 BYTE num_FAT; /* 08 number of FATs */
84 WORD num_root_entries; /* 09 number of root directory entries */
85 WORD first_data_sector; /* 0b number of first sector containing user data */
86 WORD num_clusters1; /* 0d highest cluster number (number of data clusters + 1) */
87 WORD sectors_per_FAT; /* 0f number of sectors per FAT */
88 WORD first_dir_sector; /* 11 sector number of first directory sector */
89 SEGPTR driver_header; /* 13 address of device driver header */
90 BYTE media_ID; /* 17 media ID byte */
91 BYTE access_flag; /* 18 0x00 if disk accessed, 0xff if not */
92 SEGPTR next; /* 19 pointer to next DPB */
93 WORD search_cluster1; /* 1d cluster at which to start search for free space */
94 WORD free_clusters_lo; /* 1f number of free clusters on drive or 0xffff if unknown */
95 WORD free_clusters_hi; /* 21 hiword of clusters_free */
96 WORD mirroring_flags; /* 23 active FAT/mirroring flags */
97 WORD info_sector; /* 25 sector number of file system info sector or 0xffff for none */
98 WORD spare_boot_sector; /* 27 sector number of backup boot sector or 0xffff for none */
99 DWORD first_cluster_sector; /* 29 sector number of the first cluster */
100 DWORD num_clusters2; /* 2d maximum cluster number */
101 DWORD fat_clusters; /* 31 number of clusters occupied by FAT */
102 DWORD root_cluster; /* 35 cluster number of start of root directory */
103 DWORD search_cluster2; /* 39 cluster at which to start searching for free space */
108 * Structure for DOS data that can be accessed directly from applications.
109 * Real and protected mode pointers will be returned to this structure so
110 * the structure must be correctly packed.
112 typedef struct _INT21_HEAP {
113 WORD uppercase_size; /* Size of the following table in bytes */
114 BYTE uppercase_table[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
116 WORD lowercase_size; /* Size of the following table in bytes */
117 BYTE lowercase_table[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
119 WORD collating_size; /* Size of the following table in bytes */
120 BYTE collating_table[256]; /* Values used to sort characters from 0x00 to 0xff. */
122 WORD filename_size; /* Size of the following filename data in bytes */
123 BYTE filename_reserved1; /* 0x01 for MS-DOS 3.30-6.00 */
124 BYTE filename_lowest; /* Lowest permissible character value for filename */
125 BYTE filename_highest; /* Highest permissible character value for filename */
126 BYTE filename_reserved2; /* 0x00 for MS-DOS 3.30-6.00 */
127 BYTE filename_exclude_first; /* First illegal character in permissible range */
128 BYTE filename_exclude_last; /* Last illegal character in permissible range */
129 BYTE filename_reserved3; /* 0x02 for MS-DOS 3.30-6.00 */
130 BYTE filename_illegal_size; /* Number of terminators in the following table */
131 BYTE filename_illegal_table[16]; /* Characters which terminate a filename */
133 WORD dbcs_size; /* Number of valid ranges in the following table */
134 BYTE dbcs_table[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
136 BYTE misc_indos; /* Interrupt 21 nesting flag */
137 WORD misc_segment; /* Real mode segment for INT21_HEAP */
138 WORD misc_selector; /* Protected mode selector for INT21_HEAP */
139 INT21_DPB misc_dpb_list[MAX_DOS_DRIVES]; /* Drive parameter blocks for all drives */
147 CHAR file_extension[3];
148 WORD current_block_number;
149 WORD logical_record_size;
151 WORD date_of_last_write;
152 WORD time_of_last_write;
155 WORD starting_cluster;
156 WORD sequence_number;
157 BYTE file_attributes;
159 BYTE record_within_current_block;
160 BYTE random_access_record_number[4];
167 BYTE xfcb_file_attribute;
171 /* DTA layout for FindFirst/FindNext */
174 BYTE drive; /* 00 drive letter */
175 char mask[11]; /* 01 search template */
176 BYTE search_attr; /* 0c search attributes */
177 WORD count; /* 0d entry count within directory */
178 WORD cluster; /* 0f cluster of parent directory */
179 WCHAR *fullPath; /* 11 full path (was: reserved) */
180 BYTE fileattr; /* 15 file attributes */
181 WORD filetime; /* 16 file time */
182 WORD filedate; /* 18 file date */
183 DWORD filesize; /* 1a file size */
184 char filename[13]; /* 1e file name + extension */
187 /* FCB layout for FindFirstFCB/FindNextFCB */
190 BYTE drive; /* 00 drive letter */
191 char filename[11]; /* 01 filename 8+3 format */
192 int count; /* 0c entry count (was: reserved) */
193 WCHAR *fullPath; /* 10 full path (was: reserved) */
196 /* DOS directory entry for FindFirstFCB/FindNextFCB */
199 char filename[11]; /* 00 filename 8+3 format */
200 BYTE fileattr; /* 0b file attributes */
201 BYTE reserved[10]; /* 0c reserved */
202 WORD filetime; /* 16 file time */
203 WORD filedate; /* 18 file date */
204 WORD cluster; /* 1a file first cluster */
205 DWORD filesize; /* 1c file size */
206 } DOS_DIRENTRY_LAYOUT;
210 /* dos file attributes */
211 #define FA_NORMAL 0x00 /* Normal file, no attributes */
212 #define FA_RDONLY 0x01 /* Read only attribute */
213 #define FA_HIDDEN 0x02 /* Hidden file */
214 #define FA_SYSTEM 0x04 /* System file */
215 #define FA_LABEL 0x08 /* Volume label */
216 #define FA_DIRECTORY 0x10 /* Directory */
217 #define FA_ARCHIVE 0x20 /* Archive */
218 #define FA_UNUSED 0x40 /* Unused */
221 #define ER_NoNetwork 0x49
224 #define EC_OutOfResource 0x01
225 #define EC_Temporary 0x02
226 #define EC_AccessDenied 0x03
227 #define EC_InternalError 0x04
228 #define EC_HardwareFailure 0x05
229 #define EC_SystemFailure 0x06
230 #define EC_ProgramError 0x07
231 #define EC_NotFound 0x08
232 #define EC_MediaError 0x0b
233 #define EC_Exists 0x0c
234 #define EC_Unknown 0x0d
236 /* Suggested actions */
237 #define SA_Retry 0x01
238 #define SA_DelayedRetry 0x02
239 #define SA_Abort 0x04
240 #define SA_Ignore 0x06
241 #define SA_Ask4Retry 0x07
244 #define EL_Unknown 0x01
246 #define EL_Network 0x03
247 #define EL_Serial 0x04
248 #define EL_Memory 0x05
250 /* BIOS Keyboard Scancodes */
251 #define KEY_LEFT 0x4B
252 #define KEY_RIGHT 0x4D
254 #define KEY_DOWN 0x50
255 #define KEY_IC 0x52 /* insert char */
256 #define KEY_DC 0x53 /* delete char */
257 #define KEY_BACKSPACE 0x0E
258 #define KEY_HOME 0x47
260 #define KEY_NPAGE 0x49
261 #define KEY_PPAGE 0x51
270 void (*ioctl_handler)(CONTEXT86 *);
273 static void INT21_IoctlScsiMgrHandler( CONTEXT86 * );
274 static void INT21_IoctlEMSHandler( CONTEXT86 * );
275 static void INT21_IoctlHPScanHandler( CONTEXT86 * );
277 static struct magic_device magic_devices[] =
279 { {'s','c','s','i','m','g','r','$',0}, NULL, 0, 0, INT21_IoctlScsiMgrHandler },
280 { {'e','m','m','x','x','x','x','0',0}, NULL, 0, 0, INT21_IoctlEMSHandler },
281 { {'h','p','s','c','a','n',0}, NULL, 0, 0, INT21_IoctlHPScanHandler },
284 #define NB_MAGIC_DEVICES (sizeof(magic_devices)/sizeof(magic_devices[0]))
287 /* Many calls translate a drive argument like this:
288 drive number (00h = default, 01h = A:, etc)
290 /******************************************************************
293 * Many calls translate a drive argument like this:
294 * drive number (00h = default, 01h = A:, etc)
296 static const char *INT21_DriveName(int drive)
300 if (drive <= 26) return wine_dbg_sprintf("%c:", 'A' + drive - 1);
301 else return wine_dbg_sprintf( "<Bad drive: %d>", drive);
306 /***********************************************************************
307 * INT21_GetCurrentDrive
309 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
310 * MAX_DOS_DRIVES on error.
312 static BYTE INT21_GetCurrentDrive(void)
314 WCHAR current_directory[MAX_PATH];
316 if (!GetCurrentDirectoryW( MAX_PATH, current_directory ) ||
317 current_directory[1] != ':')
319 TRACE( "Failed to get current drive.\n" );
320 return MAX_DOS_DRIVES;
323 return toupperW( current_directory[0] ) - 'A';
327 /***********************************************************************
330 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
331 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
333 static BYTE INT21_MapDrive( BYTE drive )
337 WCHAR drivespec[3] = {'A', ':', 0};
340 drivespec[0] += drive - 1;
341 drivetype = GetDriveTypeW( drivespec );
343 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
344 return MAX_DOS_DRIVES;
349 return INT21_GetCurrentDrive();
353 /***********************************************************************
354 * INT21_SetCurrentDrive
356 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
358 static void INT21_SetCurrentDrive( BYTE drive )
360 WCHAR drivespec[3] = {'A', ':', 0};
362 drivespec[0] += drive;
364 if (!SetCurrentDirectoryW( drivespec ))
365 TRACE( "Failed to set current drive.\n" );
369 /***********************************************************************
372 * Reads a character from the standard input.
373 * Extended keycodes will be returned as two separate characters.
375 static BOOL INT21_ReadChar( BYTE *input, CONTEXT86 *waitctx )
377 static BYTE pending_scan = 0;
382 *input = pending_scan;
391 if (!DOSVM_Int16ReadChar( &ascii, &scan, waitctx ))
396 if (waitctx && !ascii)
403 /***********************************************************************
404 * INT21_GetSystemCountryCode
406 * Return DOS country code for default system locale.
408 static WORD INT21_GetSystemCountryCode( void )
411 * FIXME: Determine country code. We should probably use
412 * DOSCONF structure for that.
414 return GetSystemDefaultLangID();
418 /***********************************************************************
419 * INT21_FillCountryInformation
421 * Fill 34-byte buffer with country information data using
422 * default system locale.
424 static void INT21_FillCountryInformation( BYTE *buffer )
426 /* 00 - WORD: date format
431 *(WORD*)(buffer + 0) = 0; /* FIXME: Get from locale */
433 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
434 buffer[2] = '$'; /* FIXME: Get from locale */
437 /* 07 - BYTE[2]: ASCIIZ thousands separator */
438 buffer[7] = 0; /* FIXME: Get from locale */
441 /* 09 - BYTE[2]: ASCIIZ decimal separator */
442 buffer[9] = '.'; /* FIXME: Get from locale */
445 /* 11 - BYTE[2]: ASCIIZ date separator */
446 buffer[11] = '/'; /* FIXME: Get from locale */
449 /* 13 - BYTE[2]: ASCIIZ time separator */
450 buffer[13] = ':'; /* FIXME: Get from locale */
453 /* 15 - BYTE: Currency format
454 * bit 2 = set if currency symbol replaces decimal point
455 * bit 1 = number of spaces between value and currency symbol
456 * bit 0 = 0 if currency symbol precedes value
457 * 1 if currency symbol follows value
459 buffer[15] = 0; /* FIXME: Get from locale */
461 /* 16 - BYTE: Number of digits after decimal in currency */
462 buffer[16] = 0; /* FIXME: Get from locale */
464 /* 17 - BYTE: Time format
465 * bit 0 = 0 if 12-hour clock
468 buffer[17] = 1; /* FIXME: Get from locale */
470 /* 18 - DWORD: Address of case map routine */
471 *(DWORD*)(buffer + 18) = 0; /* FIXME: ptr to case map routine */
473 /* 22 - BYTE[2]: ASCIIZ data-list separator */
474 buffer[22] = ','; /* FIXME: Get from locale */
477 /* 24 - BYTE[10]: Reserved */
478 memset( buffer + 24, 0, 10 );
482 /***********************************************************************
485 * Initialize DOS heap.
487 * Filename Terminator Table of w2k DE NTVDM:
488 * 16 00 01 00 FF 00 00 20-02 0E 2E 22 2F 5C 5B 5D ....... ..."/\[]
489 * 3A 7C 3C 3E 2B 3D 3B 2C-00 :|<>+=;,
491 static void INT21_FillHeap( INT21_HEAP *heap )
493 static const char terminators[] = ".\"/\\[]:|<>+=;,";
499 heap->uppercase_size = 128;
500 for (i = 0; i < 128; i++)
501 heap->uppercase_table[i] = toupper( 128 + i );
506 heap->lowercase_size = 256;
507 for (i = 0; i < 256; i++)
508 heap->lowercase_table[i] = tolower( i );
513 heap->collating_size = 256;
514 for (i = 0; i < 256; i++)
515 heap->collating_table[i] = i;
520 heap->filename_size = 8 + strlen(terminators);
521 heap->filename_illegal_size = strlen(terminators);
522 strcpy( heap->filename_illegal_table, terminators );
524 heap->filename_reserved1 = 0x01;
525 heap->filename_lowest = 0x00;
526 heap->filename_highest = 0xff;
527 heap->filename_reserved2 = 0x00;
528 heap->filename_exclude_first = 0x00;
529 heap->filename_exclude_last = 0x20;
530 heap->filename_reserved3 = 0x02;
533 * DBCS lead byte table. This table is empty.
536 memset( heap->dbcs_table, 0, sizeof(heap->dbcs_table) );
539 * Initialize InDos flag.
541 heap->misc_indos = 0;
544 * FIXME: Should drive parameter blocks (DPB) be
545 * initialized here and linked to DOS LOL?
550 /***********************************************************************
551 * INT21_GetHeapPointer
553 * Get pointer for DOS heap (INT21_HEAP).
554 * Creates and initializes heap on first call.
556 static INT21_HEAP *INT21_GetHeapPointer( void )
558 static INT21_HEAP *heap_pointer = NULL;
565 heap_pointer = DOSVM_AllocDataUMB( sizeof(INT21_HEAP),
569 heap_pointer->misc_segment = heap_segment;
570 heap_pointer->misc_selector = heap_selector;
571 INT21_FillHeap( heap_pointer );
578 /***********************************************************************
579 * INT21_GetHeapSelector
581 * Get segment/selector for DOS heap (INT21_HEAP).
582 * Creates and initializes heap on first call.
584 static WORD INT21_GetHeapSelector( CONTEXT86 *context )
586 INT21_HEAP *heap = INT21_GetHeapPointer();
588 if (!ISV86(context) && DOSVM_IsWin16())
589 return heap->misc_selector;
591 return heap->misc_segment;
595 /***********************************************************************
598 * Fill DOS heap drive parameter block for the specified drive.
599 * Return TRUE if drive was valid and there were
600 * no errors while reading drive information.
602 static BOOL INT21_FillDrivePB( BYTE drive )
604 WCHAR drivespec[3] = {'A', ':', 0};
605 INT21_HEAP *heap = INT21_GetHeapPointer();
608 DWORD cluster_sectors;
611 DWORD total_clusters;
613 if (drive >= MAX_DOS_DRIVES)
616 dpb = &heap->misc_dpb_list[drive];
617 drivespec[0] += drive;
618 drivetype = GetDriveTypeW( drivespec );
621 * FIXME: Does this check work correctly with floppy/cdrom drives?
623 if (drivetype == DRIVE_NO_ROOT_DIR || drivetype == DRIVE_UNKNOWN)
627 * FIXME: Does this check work correctly with floppy/cdrom drives?
629 if (!GetDiskFreeSpaceW( drivespec, &cluster_sectors, §or_bytes,
630 &free_clusters, &total_clusters ))
634 * FIXME: Most of the values listed below are incorrect.
635 * All values should be validated.
640 dpb->sector_bytes = sector_bytes;
641 dpb->cluster_sectors = cluster_sectors - 1;
644 while (cluster_sectors > 1)
646 cluster_sectors /= 2;
650 dpb->num_reserved = 0;
652 dpb->num_root_entries = 2;
653 dpb->first_data_sector = 2;
654 dpb->num_clusters1 = total_clusters;
655 dpb->sectors_per_FAT = 1;
656 dpb->first_dir_sector = 1;
657 dpb->driver_header = 0;
658 dpb->media_ID = (drivetype == DRIVE_FIXED) ? 0xF8 : 0xF0;
659 dpb->access_flag = 0;
661 dpb->search_cluster1 = 0;
662 dpb->free_clusters_lo = LOWORD(free_clusters);
663 dpb->free_clusters_hi = HIWORD(free_clusters);
664 dpb->mirroring_flags = 0;
665 dpb->info_sector = 0xffff;
666 dpb->spare_boot_sector = 0xffff;
667 dpb->first_cluster_sector = 0;
668 dpb->num_clusters2 = total_clusters;
669 dpb->fat_clusters = 32;
670 dpb->root_cluster = 0;
671 dpb->search_cluster2 = 0;
677 /***********************************************************************
678 * INT21_GetCurrentDirectory
682 * - subfunction 0x47 of function 0x71
684 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context, BOOL islong )
686 char *buffer = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi);
687 BYTE drive = INT21_MapDrive( DL_reg(context) );
688 WCHAR pathW[MAX_PATH];
689 char pathA[MAX_PATH];
692 TRACE( "drive %d\n", DL_reg(context) );
694 if (drive == MAX_DOS_DRIVES)
696 SetLastError(ERROR_INVALID_DRIVE);
701 * Grab current directory.
704 if (!GetCurrentDirectoryW( MAX_PATH, pathW )) return FALSE;
706 if (toupperW(pathW[0]) - 'A' != drive || pathW[1] != ':')
708 /* cwd is not on the requested drive, get the environment string instead */
713 env_var[1] = 'A' + drive;
716 if (!GetEnvironmentVariableW( env_var, pathW, MAX_PATH ))
718 /* return empty path */
725 * Convert into short format.
730 DWORD result = GetShortPathNameW( pathW, pathW, MAX_PATH );
733 if (result > MAX_PATH)
735 WARN( "Short path too long!\n" );
736 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
742 * The returned pathname does not include
743 * the drive letter, colon or leading backslash.
749 * FIXME: We should probably just strip host part from name...
751 FIXME( "UNC names are not supported.\n" );
752 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
755 else if (!ptr[0] || ptr[1] != ':' || ptr[2] != '\\')
757 WARN( "Path is neither UNC nor DOS path: %s\n",
758 wine_dbgstr_w(ptr) );
759 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
764 /* Remove drive letter, colon and leading backslash. */
769 * Convert into OEM string.
772 if (!WideCharToMultiByte(CP_OEMCP, 0, ptr, -1, pathA,
773 MAX_PATH, NULL, NULL))
775 WARN( "Cannot convert path!\n" );
776 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
786 /* Undocumented success code. */
787 SET_AX( context, 0x0100 );
789 /* Truncate buffer to 64 bytes. */
793 TRACE( "%c:=%s\n", 'A' + drive, pathA );
795 strcpy( buffer, pathA );
800 /***********************************************************************
801 * INT21_SetCurrentDirectory
805 * - subfunction 0x3b of function 0x71
807 static BOOL INT21_SetCurrentDirectory( CONTEXT86 *context )
809 WCHAR dirW[MAX_PATH];
812 char *dirA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
813 BYTE drive = INT21_GetCurrentDrive();
816 TRACE( "SET CURRENT DIRECTORY %s\n", dirA );
818 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
819 if (!GetFullPathNameW( dirW, MAX_PATH, dirW, NULL )) return FALSE;
821 attr = GetFileAttributesW( dirW );
822 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
824 SetLastError( ERROR_PATH_NOT_FOUND );
829 env_var[1] = dirW[0];
832 result = SetEnvironmentVariableW( env_var, dirW );
834 /* only set current directory if on the current drive */
835 if (result && (toupperW(dirW[0]) - 'A' == drive)) result = SetCurrentDirectoryW( dirW );
840 /***********************************************************************
841 * INT21_CreateMagicDeviceHandle
843 * Create a dummy file handle for a "magic" device.
845 static HANDLE INT21_CreateMagicDeviceHandle( LPCWSTR name )
847 const char *dir = wine_get_server_dir();
851 OBJECT_ATTRIBUTES attr;
852 UNICODE_STRING nameW;
855 len = MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, NULL, 0 );
856 nameW.Length = (len + 1 + strlenW( name )) * sizeof(WCHAR);
857 nameW.MaximumLength = nameW.Length + sizeof(WCHAR);
858 if (!(nameW.Buffer = HeapAlloc( GetProcessHeap(), 0, nameW.Length )))
860 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
863 MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, nameW.Buffer, len );
864 nameW.Buffer[len-1] = '/';
865 strcpyW( nameW.Buffer + len, name );
867 attr.Length = sizeof(attr);
868 attr.RootDirectory = 0;
870 attr.ObjectName = &nameW;
871 attr.SecurityDescriptor = NULL;
872 attr.SecurityQualityOfService = NULL;
874 status = NtCreateFile( &ret, GENERIC_READ|GENERIC_WRITE, &attr, &io, NULL, 0,
875 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF,
876 FILE_SYNCHRONOUS_IO_ALERT, NULL, 0 );
880 SetLastError( RtlNtStatusToDosError(status) );
882 RtlFreeUnicodeString( &nameW );
887 /***********************************************************************
888 * INT21_OpenMagicDevice
890 * Open a file handle for "magic" devices like EMMXXXX0.
892 static HANDLE INT21_OpenMagicDevice( LPCWSTR name, DWORD access )
898 if (name[0] && (name[1] == ':')) name += 2;
899 if ((p = strrchrW( name, '/' ))) name = p + 1;
900 if ((p = strrchrW( name, '\\' ))) name = p + 1;
902 for (i = 0; i < NB_MAGIC_DEVICES; i++)
904 int len = strlenW( magic_devices[i].name );
905 if (!strncmpiW( magic_devices[i].name, name, len ) &&
906 (!name[len] || name[len] == '.' || name[len] == ':')) break;
908 if (i == NB_MAGIC_DEVICES) return 0;
910 if (!magic_devices[i].handle) /* need to open it */
915 if (!(handle = INT21_CreateMagicDeviceHandle( magic_devices[i].name ))) return 0;
916 wine_server_handle_to_fd( handle, 0, &fd, NULL );
918 wine_server_release_fd( handle, fd );
919 magic_devices[i].dev = st.st_dev;
920 magic_devices[i].ino = st.st_ino;
921 magic_devices[i].handle = handle;
923 if (!DuplicateHandle( GetCurrentProcess(), magic_devices[i].handle,
924 GetCurrentProcess(), &handle, access, FALSE, 0 )) handle = 0;
929 /***********************************************************************
937 * - subfunction 0x6c of function 0x71
939 static BOOL INT21_CreateFile( CONTEXT86 *context,
946 char *pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, pathSegOff);
947 WCHAR pathW[MAX_PATH];
954 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
955 "create flags=%04x, file=%s.\n",
956 AH_reg(context), dosAction, dosAccessShare, CX_reg(context), pathA );
959 * Application tried to create/open a file whose name
960 * ends with a backslash. This is not allowed.
962 * FIXME: This needs to be validated, especially the return value.
964 if (pathA[strlen(pathA) - 1] == '/')
966 SetLastError( ERROR_FILE_NOT_FOUND );
971 * Convert DOS action flags into Win32 creation disposition parameter.
976 winMode = OPEN_EXISTING;
979 winMode = TRUNCATE_EXISTING;
982 winMode = CREATE_NEW;
985 winMode = OPEN_ALWAYS;
988 winMode = CREATE_ALWAYS;
991 SetLastError( ERROR_INVALID_PARAMETER );
996 * Convert DOS access/share flags into Win32 desired access parameter.
998 switch(dosAccessShare & 0x07)
1001 winAccess = GENERIC_READ;
1004 winAccess = GENERIC_WRITE;
1007 winAccess = GENERIC_READ | GENERIC_WRITE;
1011 * Read-only, do not modify file's last-access time (DOS7).
1013 * FIXME: How to prevent modification of last-access time?
1015 winAccess = GENERIC_READ;
1022 * Convert DOS access/share flags into Win32 share mode parameter.
1024 switch(dosAccessShare & 0x70)
1026 case OF_SHARE_EXCLUSIVE:
1029 case OF_SHARE_DENY_WRITE:
1030 winSharing = FILE_SHARE_READ;
1032 case OF_SHARE_DENY_READ:
1033 winSharing = FILE_SHARE_WRITE;
1035 case OF_SHARE_DENY_NONE:
1036 case OF_SHARE_COMPAT:
1038 winSharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1042 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
1043 * What to do with this bit?
1044 * FIXME: Bits in the high byte of dosAccessShare are not supported.
1045 * See both function 0x6c and subfunction 0x6c of function 0x71 for
1046 * definition of these bits.
1050 * Convert DOS create attributes into Win32 flags and attributes parameter.
1052 if (winMode == OPEN_EXISTING || winMode == TRUNCATE_EXISTING)
1058 WORD dosAttributes = CX_reg(context);
1060 if (dosAttributes & FA_LABEL)
1063 * Application tried to create volume label entry.
1064 * This is difficult to support so we do not allow it.
1066 * FIXME: If volume does not already have a label,
1067 * this function is supposed to succeed.
1069 SetLastError( ERROR_ACCESS_DENIED );
1073 winAttributes = dosAttributes &
1074 (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
1075 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE);
1081 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
1083 if ((winHandle = INT21_OpenMagicDevice( pathW, winAccess )))
1089 winHandle = CreateFileW( pathW, winAccess, winSharing, NULL,
1090 winMode, winAttributes, 0 );
1092 if (winHandle == INVALID_HANDLE_VALUE)
1096 * Determine DOS file status.
1107 case TRUNCATE_EXISTING:
1114 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 1 : 2;
1117 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 3 : 2;
1125 * Return DOS file handle and DOS status.
1127 SET_AX( context, Win32HandleToDosFileHandle(winHandle) );
1130 SET_CX( context, dosStatus );
1132 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
1133 AX_reg(context), dosStatus );
1139 /***********************************************************************
1140 * INT21_BufferedInput
1142 * Handler for function 0x0a and reading from console using
1145 * Reads a string of characters from standard input until
1146 * enter key is pressed. Returns either number of characters
1147 * read from console including terminating CR or
1148 * zero if capacity was zero.
1150 static WORD INT21_BufferedInput( CONTEXT86 *context, BYTE *ptr, WORD capacity )
1155 * Return immediately if capacity is zero.
1165 DOSVM_Int16ReadChar( &ascii, &scan, context );
1167 if (ascii == '\r' || ascii == '\n')
1174 * DOS handles only backspace and KEY_LEFT
1175 * perhaps we should do more
1177 if (ascii == '\b' || scan == KEY_LEFT)
1179 if (length==0) continue;
1180 DOSVM_PutChar( '\b' );
1186 * If the buffer becomes filled to within one byte of
1187 * capacity, DOS rejects all further characters up to,
1188 * but not including, the terminating carriage return.
1190 if (ascii != 0 && length < capacity-1)
1192 DOSVM_PutChar( ascii );
1193 ptr[length] = ascii;
1200 /***********************************************************************
1201 * INT21_GetCurrentDTA
1203 static BYTE *INT21_GetCurrentDTA( CONTEXT86 *context )
1205 TDB *pTask = GlobalLock16(GetCurrentTask());
1207 /* FIXME: This assumes DTA was set correctly! */
1208 return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
1209 (DWORD)OFFSETOF(pTask->dta) );
1213 /***********************************************************************
1214 * INT21_OpenFileUsingFCB
1216 * Handler for function 0x0f.
1219 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
1226 * Opens a FCB file for read/write in compatibility mode. Upon calling
1227 * the FCB must have the drive_number, file_name, and file_extension
1228 * fields filled and all other bytes cleared.
1230 static void INT21_OpenFileUsingFCB( CONTEXT86 *context )
1238 BY_HANDLE_FILE_INFORMATION info;
1241 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1242 if (fcb->drive_number == 0xff) {
1243 xfcb = (struct XFCB *) fcb;
1244 fcb = (struct FCB *) xfcb->fcb;
1248 file_path[0] = 'A' + INT21_MapDrive( fcb->drive_number );
1250 if (AL_result == 0) {
1252 pos = &file_path[2];
1253 memcpy(pos, fcb->file_name, 8);
1256 pos = strchr(pos, ' ');
1259 memcpy(pos, fcb->file_extension, 3);
1262 pos = strchr(pos, ' ');
1265 handle = (HANDLE) _lopen(file_path, OF_READWRITE);
1266 if (handle == INVALID_HANDLE_VALUE) {
1267 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path);
1268 AL_result = 0xff; /* failed */
1270 hfile16 = Win32HandleToDosFileHandle(handle);
1271 if (hfile16 == HFILE_ERROR16) {
1272 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle);
1273 CloseHandle(handle);
1274 AL_result = 0xff; /* failed */
1275 } else if (hfile16 > 255) {
1276 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16, file_path);
1278 AL_result = 0xff; /* failed */
1280 if (!GetFileInformationByHandle(handle, &info)) {
1281 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1282 hfile16, handle, file_path);
1284 AL_result = 0xff; /* failed */
1286 fcb->drive_number = file_path[0] - 'A' + 1;
1287 fcb->current_block_number = 0;
1288 fcb->logical_record_size = 128;
1289 fcb->file_size = info.nFileSizeLow;
1290 FileTimeToDosDateTime(&info.ftLastWriteTime,
1291 &fcb->date_of_last_write, &fcb->time_of_last_write);
1292 fcb->file_number = hfile16;
1293 fcb->attributes = 0xc2;
1294 fcb->starting_cluster = 0; /* don't know correct init value */
1295 fcb->sequence_number = 0; /* don't know correct init value */
1296 fcb->file_attributes = info.dwFileAttributes;
1297 /* The following fields are not initialized */
1298 /* by the native function: */
1300 /* record_within_current_block */
1301 /* random_access_record_number */
1303 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1304 file_path, hfile16, handle);
1305 AL_result = 0x00; /* successful */
1310 SET_AL(context, AL_result);
1314 /***********************************************************************
1315 * INT21_CloseFileUsingFCB
1317 * Handler for function 0x10.
1320 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1327 * Closes a FCB file.
1329 static void INT21_CloseFileUsingFCB( CONTEXT86 *context )
1335 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1336 if (fcb->drive_number == 0xff) {
1337 xfcb = (struct XFCB *) fcb;
1338 fcb = (struct FCB *) xfcb->fcb;
1341 if (_lclose16((HFILE16) fcb->file_number) != 0) {
1342 TRACE("_lclose16(%d) failed\n", fcb->file_number);
1343 AL_result = 0xff; /* failed */
1345 TRACE("successful closed file %d\n", fcb->file_number);
1346 AL_result = 0x00; /* successful */
1348 SET_AL(context, AL_result);
1352 /***********************************************************************
1353 * INT21_SequentialReadFromFCB
1355 * Handler for function 0x14.
1358 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1362 * 1: end of file, no data read
1363 * 2: segment wrap in DTA, no data read (not returned now)
1364 * 3: end of file, partial record read
1367 * Reads a record with the size FCB->logical_record_size from the FCB
1368 * to the disk transfer area. The position of the record is specified
1369 * with FCB->current_block_number and FCB->record_within_current_block.
1370 * Then FCB->current_block_number and FCB->record_within_current_block
1371 * are updated to point to the next record. If a partial record is
1372 * read, it is filled with zeros up to the FCB->logical_record_size.
1374 static void INT21_SequentialReadFromFCB( CONTEXT86 *context )
1379 DWORD record_number;
1381 BYTE *disk_transfer_area;
1385 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1386 if (fcb->drive_number == 0xff) {
1387 xfcb = (struct XFCB *) fcb;
1388 fcb = (struct FCB *) xfcb->fcb;
1391 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1392 if (handle == INVALID_HANDLE_VALUE) {
1393 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1395 AL_result = 0x01; /* end of file, no data read */
1397 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1398 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1399 if (position != record_number * fcb->logical_record_size) {
1400 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1401 fcb->file_number, record_number * fcb->logical_record_size, position);
1402 AL_result = 0x01; /* end of file, no data read */
1404 disk_transfer_area = INT21_GetCurrentDTA(context);
1405 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1406 if (bytes_read != fcb->logical_record_size) {
1407 TRACE("_lread(%d, %p, %d) failed with %d\n",
1408 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1409 if (bytes_read == 0) {
1410 AL_result = 0x01; /* end of file, no data read */
1412 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1413 AL_result = 0x03; /* end of file, partial record read */
1416 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1417 bytes_read, record_number, position, fcb->file_number, handle);
1418 AL_result = 0x00; /* successful */
1422 if (AL_result == 0x00 || AL_result == 0x03) {
1423 if (fcb->record_within_current_block == 127) {
1424 fcb->record_within_current_block = 0;
1425 fcb->current_block_number++;
1427 fcb->record_within_current_block++;
1430 SET_AL(context, AL_result);
1434 /***********************************************************************
1435 * INT21_SequentialWriteToFCB
1437 * Handler for function 0x15.
1440 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1445 * 2: segment wrap in DTA (not returned now)
1448 * Writes a record with the size FCB->logical_record_size from the disk
1449 * transfer area to the FCB. The position of the record is specified
1450 * with FCB->current_block_number and FCB->record_within_current_block.
1451 * Then FCB->current_block_number and FCB->record_within_current_block
1452 * are updated to point to the next record.
1454 static void INT21_SequentialWriteToFCB( CONTEXT86 *context )
1459 DWORD record_number;
1461 BYTE *disk_transfer_area;
1465 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1466 if (fcb->drive_number == 0xff) {
1467 xfcb = (struct XFCB *) fcb;
1468 fcb = (struct FCB *) xfcb->fcb;
1471 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1472 if (handle == INVALID_HANDLE_VALUE) {
1473 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1475 AL_result = 0x01; /* disk full */
1477 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1478 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1479 if (position != record_number * fcb->logical_record_size) {
1480 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1481 fcb->file_number, record_number * fcb->logical_record_size, position);
1482 AL_result = 0x01; /* disk full */
1484 disk_transfer_area = INT21_GetCurrentDTA(context);
1485 bytes_written = _lwrite((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1486 if (bytes_written != fcb->logical_record_size) {
1487 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1488 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1489 AL_result = 0x01; /* disk full */
1491 TRACE("successful written %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1492 bytes_written, record_number, position, fcb->file_number, handle);
1493 AL_result = 0x00; /* successful */
1497 if (AL_result == 0x00) {
1498 if (fcb->record_within_current_block == 127) {
1499 fcb->record_within_current_block = 0;
1500 fcb->current_block_number++;
1502 fcb->record_within_current_block++;
1505 SET_AL(context, AL_result);
1509 /***********************************************************************
1510 * INT21_ReadRandomRecordFromFCB
1512 * Handler for function 0x21.
1515 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1519 * 1: end of file, no data read
1520 * 2: segment wrap in DTA, no data read (not returned now)
1521 * 3: end of file, partial record read
1524 * Reads a record with the size FCB->logical_record_size from
1525 * the FCB to the disk transfer area. The position of the record
1526 * is specified with FCB->random_access_record_number. The
1527 * FCB->random_access_record_number is not updated. If a partial record
1528 * is read, it is filled with zeros up to the FCB->logical_record_size.
1530 static void INT21_ReadRandomRecordFromFCB( CONTEXT86 *context )
1535 DWORD record_number;
1537 BYTE *disk_transfer_area;
1541 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1542 if (fcb->drive_number == 0xff) {
1543 xfcb = (struct XFCB *) fcb;
1544 fcb = (struct FCB *) xfcb->fcb;
1547 memcpy(&record_number, fcb->random_access_record_number, 4);
1548 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1549 if (handle == INVALID_HANDLE_VALUE) {
1550 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1552 AL_result = 0x01; /* end of file, no data read */
1554 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1555 if (position != record_number * fcb->logical_record_size) {
1556 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1557 fcb->file_number, record_number * fcb->logical_record_size, position);
1558 AL_result = 0x01; /* end of file, no data read */
1560 disk_transfer_area = INT21_GetCurrentDTA(context);
1561 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1562 if (bytes_read != fcb->logical_record_size) {
1563 TRACE("_lread(%d, %p, %d) failed with %d\n",
1564 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1565 if (bytes_read == 0) {
1566 AL_result = 0x01; /* end of file, no data read */
1568 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1569 AL_result = 0x03; /* end of file, partial record read */
1572 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1573 bytes_read, record_number, position, fcb->file_number, handle);
1574 AL_result = 0x00; /* successful */
1578 fcb->current_block_number = record_number / 128;
1579 fcb->record_within_current_block = record_number % 128;
1580 SET_AL(context, AL_result);
1584 /***********************************************************************
1585 * INT21_WriteRandomRecordToFCB
1587 * Handler for function 0x22.
1590 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1595 * 2: segment wrap in DTA (not returned now)
1598 * Writes a record with the size FCB->logical_record_size from
1599 * the disk transfer area to the FCB. The position of the record
1600 * is specified with FCB->random_access_record_number. The
1601 * FCB->random_access_record_number is not updated.
1603 static void INT21_WriteRandomRecordToFCB( CONTEXT86 *context )
1608 DWORD record_number;
1610 BYTE *disk_transfer_area;
1614 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1615 if (fcb->drive_number == 0xff) {
1616 xfcb = (struct XFCB *) fcb;
1617 fcb = (struct FCB *) xfcb->fcb;
1620 memcpy(&record_number, fcb->random_access_record_number, 4);
1621 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1622 if (handle == INVALID_HANDLE_VALUE) {
1623 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1625 AL_result = 0x01; /* disk full */
1627 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1628 if (position != record_number * fcb->logical_record_size) {
1629 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1630 fcb->file_number, record_number * fcb->logical_record_size, position);
1631 AL_result = 0x01; /* disk full */
1633 disk_transfer_area = INT21_GetCurrentDTA(context);
1634 bytes_written = _lwrite((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1635 if (bytes_written != fcb->logical_record_size) {
1636 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1637 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1638 AL_result = 0x01; /* disk full */
1640 TRACE("successful written %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1641 bytes_written, record_number, position, fcb->file_number, handle);
1642 AL_result = 0x00; /* successful */
1646 fcb->current_block_number = record_number / 128;
1647 fcb->record_within_current_block = record_number % 128;
1648 SET_AL(context, AL_result);
1652 /***********************************************************************
1653 * INT21_RandomBlockReadFromFCB
1655 * Handler for function 0x27.
1658 * CX [I/O] Number of records to read
1659 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1663 * 1: end of file, no data read
1664 * 2: segment wrap in DTA, no data read (not returned now)
1665 * 3: end of file, partial record read
1668 * Reads several records with the size FCB->logical_record_size from
1669 * the FCB to the disk transfer area. The number of records to be
1670 * read is specified in the CX register. The position of the first
1671 * record is specified with FCB->random_access_record_number. The
1672 * FCB->random_access_record_number, the FCB->current_block_number
1673 * and FCB->record_within_current_block are updated to point to the
1674 * next record after the records read. If a partial record is read,
1675 * it is filled with zeros up to the FCB->logical_record_size. The
1676 * CX register is set to the number of successfully read records.
1678 static void INT21_RandomBlockReadFromFCB( CONTEXT86 *context )
1683 DWORD record_number;
1685 BYTE *disk_transfer_area;
1686 UINT records_requested;
1687 UINT bytes_requested;
1692 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1693 if (fcb->drive_number == 0xff) {
1694 xfcb = (struct XFCB *) fcb;
1695 fcb = (struct FCB *) xfcb->fcb;
1698 memcpy(&record_number, fcb->random_access_record_number, 4);
1699 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1700 if (handle == INVALID_HANDLE_VALUE) {
1701 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1704 AL_result = 0x01; /* end of file, no data read */
1706 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1707 if (position != record_number * fcb->logical_record_size) {
1708 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1709 fcb->file_number, record_number * fcb->logical_record_size, position);
1711 AL_result = 0x01; /* end of file, no data read */
1713 disk_transfer_area = INT21_GetCurrentDTA(context);
1714 records_requested = CX_reg(context);
1715 bytes_requested = (UINT) records_requested * fcb->logical_record_size;
1716 bytes_read = _lread((HFILE) handle, disk_transfer_area, bytes_requested);
1717 if (bytes_read != bytes_requested) {
1718 TRACE("_lread(%d, %p, %d) failed with %d\n",
1719 fcb->file_number, disk_transfer_area, bytes_requested, bytes_read);
1720 records_read = bytes_read / fcb->logical_record_size;
1721 if (bytes_read % fcb->logical_record_size == 0) {
1722 AL_result = 0x01; /* end of file, no data read */
1725 memset(&disk_transfer_area[bytes_read], 0, records_read * fcb->logical_record_size - bytes_read);
1726 AL_result = 0x03; /* end of file, partial record read */
1729 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1730 bytes_read, record_number, position, fcb->file_number, handle);
1731 records_read = records_requested;
1732 AL_result = 0x00; /* successful */
1736 record_number += records_read;
1737 memcpy(fcb->random_access_record_number, &record_number, 4);
1738 fcb->current_block_number = record_number / 128;
1739 fcb->record_within_current_block = record_number % 128;
1740 SET_CX(context, records_read);
1741 SET_AL(context, AL_result);
1745 /***********************************************************************
1746 * INT21_RandomBlockWriteToFCB
1748 * Handler for function 0x28.
1751 * CX [I/O] Number of records to write
1752 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1757 * 2: segment wrap in DTA (not returned now)
1760 * Writes several records with the size FCB->logical_record_size from
1761 * the disk transfer area to the FCB. The number of records to be
1762 * written is specified in the CX register. The position of the first
1763 * record is specified with FCB->random_access_record_number. The
1764 * FCB->random_access_record_number, the FCB->current_block_number
1765 * and FCB->record_within_current_block are updated to point to the
1766 * next record after the records written. The CX register is set to
1767 * the number of successfully written records.
1769 static void INT21_RandomBlockWriteToFCB( CONTEXT86 *context )
1774 DWORD record_number;
1776 BYTE *disk_transfer_area;
1777 UINT records_requested;
1778 UINT bytes_requested;
1780 UINT records_written;
1783 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1784 if (fcb->drive_number == 0xff) {
1785 xfcb = (struct XFCB *) fcb;
1786 fcb = (struct FCB *) xfcb->fcb;
1789 memcpy(&record_number, fcb->random_access_record_number, 4);
1790 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1791 if (handle == INVALID_HANDLE_VALUE) {
1792 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1794 records_written = 0;
1795 AL_result = 0x01; /* disk full */
1797 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1798 if (position != record_number * fcb->logical_record_size) {
1799 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1800 fcb->file_number, record_number * fcb->logical_record_size, position);
1801 records_written = 0;
1802 AL_result = 0x01; /* disk full */
1804 disk_transfer_area = INT21_GetCurrentDTA(context);
1805 records_requested = CX_reg(context);
1806 bytes_requested = (UINT) records_requested * fcb->logical_record_size;
1807 bytes_written = _lwrite((HFILE) handle, disk_transfer_area, bytes_requested);
1808 if (bytes_written != bytes_requested) {
1809 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1810 fcb->file_number, disk_transfer_area, bytes_requested, bytes_written);
1811 records_written = bytes_written / fcb->logical_record_size;
1812 AL_result = 0x01; /* disk full */
1814 TRACE("successful write %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1815 bytes_written, record_number, position, fcb->file_number, handle);
1816 records_written = records_requested;
1817 AL_result = 0x00; /* successful */
1821 record_number += records_written;
1822 memcpy(fcb->random_access_record_number, &record_number, 4);
1823 fcb->current_block_number = record_number / 128;
1824 fcb->record_within_current_block = record_number % 128;
1825 SET_CX(context, records_written);
1826 SET_AL(context, AL_result);
1830 /***********************************************************************
1831 * INT21_CreateDirectory
1835 * - subfunction 0x39 of function 0x71
1836 * - subfunction 0xff of function 0x43 (CL == 0x39)
1838 static BOOL INT21_CreateDirectory( CONTEXT86 *context )
1840 WCHAR dirW[MAX_PATH];
1841 char *dirA = CTX_SEG_OFF_TO_LIN(context,
1845 TRACE( "CREATE DIRECTORY %s\n", dirA );
1847 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
1849 if (CreateDirectoryW(dirW, NULL))
1853 * FIXME: CreateDirectory's LastErrors will clash with the ones
1854 * used by DOS. AH=39 only returns 3 (path not found) and
1855 * 5 (access denied), while CreateDirectory return several
1856 * ones. Remap some of them. -Marcus
1858 switch (GetLastError())
1860 case ERROR_ALREADY_EXISTS:
1861 case ERROR_FILENAME_EXCED_RANGE:
1862 case ERROR_DISK_FULL:
1863 SetLastError(ERROR_ACCESS_DENIED);
1873 /***********************************************************************
1874 * INT21_ExtendedCountryInformation
1876 * Handler for function 0x65.
1878 static void INT21_ExtendedCountryInformation( CONTEXT86 *context )
1880 BYTE *dataptr = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
1881 BYTE buffsize = CX_reg (context);
1883 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1887 * Check subfunctions that are passed country and code page.
1889 if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
1891 WORD country = DX_reg(context);
1892 WORD codepage = BX_reg(context);
1894 if (country != 0xffff && country != INT21_GetSystemCountryCode())
1895 FIXME( "Requested info on non-default country %04x\n", country );
1897 if (codepage != 0xffff && codepage != GetOEMCP())
1898 FIXME( "Requested info on non-default code page %04x\n", codepage );
1901 switch (AL_reg(context)) {
1902 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1903 INT_BARF( context, 0x21 );
1904 SET_CFLAG( context );
1907 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1908 TRACE( "Get general internationalization info\n" );
1909 dataptr[0] = 0x01; /* Info ID */
1910 *(WORD*)(dataptr+1) = 38; /* Size of the following info */
1911 *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
1912 *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
1913 /* FIXME: fill buffer partially up to buffsize bytes*/
1914 if (buffsize >= 0x29){
1915 INT21_FillCountryInformation( dataptr + 7 );
1916 SET_CX( context, 0x29 ); /* Size of returned info */
1918 SET_CX( context, 0x07 ); /* Size of returned info */
1922 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1923 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1924 TRACE( "Get pointer to uppercase table\n" );
1925 dataptr[0] = AL_reg(context); /* Info ID */
1926 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1927 offsetof(INT21_HEAP, uppercase_size) );
1928 SET_CX( context, 5 ); /* Size of returned info */
1931 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1932 TRACE( "Get pointer to lowercase table\n" );
1933 dataptr[0] = 0x03; /* Info ID */
1934 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1935 offsetof(INT21_HEAP, lowercase_size) );
1936 SET_CX( context, 5 ); /* Size of returned info */
1939 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1940 TRACE("Get pointer to filename terminator table\n");
1941 dataptr[0] = 0x05; /* Info ID */
1942 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1943 offsetof(INT21_HEAP, filename_size) );
1944 SET_CX( context, 5 ); /* Size of returned info */
1947 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1948 TRACE("Get pointer to collating sequence table\n");
1949 dataptr[0] = 0x06; /* Info ID */
1950 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1951 offsetof(INT21_HEAP, collating_size) );
1952 SET_CX( context, 5 ); /* Size of returned info */
1955 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1956 TRACE("Get pointer to DBCS lead byte table\n");
1957 dataptr[0] = 0x07; /* Info ID */
1958 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1959 offsetof(INT21_HEAP, dbcs_size) );
1960 SET_CX( context, 5 ); /* Size of returned info */
1963 case 0x20: /* CAPITALIZE CHARACTER */
1964 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1965 TRACE("Convert char to uppercase\n");
1966 SET_DL( context, toupper(DL_reg(context)) );
1969 case 0x21: /* CAPITALIZE STRING */
1970 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1971 TRACE("Convert string to uppercase with length\n");
1973 char *ptr = (char *)CTX_SEG_OFF_TO_LIN( context,
1976 WORD len = CX_reg(context);
1977 while (len--) { *ptr = toupper(*ptr); ptr++; }
1981 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1982 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1983 TRACE("Convert ASCIIZ string to uppercase\n");
1984 _strupr( (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx) );
1987 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1988 INT_BARF( context, 0x21 );
1989 SET_CFLAG( context );
1993 INT_BARF( context, 0x21 );
2000 /***********************************************************************
2001 * INT21_FileAttributes
2005 * - subfunction 0x43 of function 0x71
2007 static BOOL INT21_FileAttributes( CONTEXT86 *context,
2011 WCHAR fileW[MAX_PATH];
2012 char *fileA = CTX_SEG_OFF_TO_LIN(context,
2021 switch (subfunction)
2023 case 0x00: /* GET FILE ATTRIBUTES */
2024 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA );
2025 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2027 result = GetFileAttributesW( fileW );
2028 if (result == INVALID_FILE_ATTRIBUTES)
2032 SET_CX( context, (WORD)result );
2034 SET_AX( context, (WORD)result ); /* DR DOS */
2038 case 0x01: /* SET FILE ATTRIBUTES */
2039 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
2040 CX_reg(context), fileA );
2041 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2043 if (!SetFileAttributesW( fileW, CX_reg(context) ))
2047 case 0x02: /* GET COMPRESSED FILE SIZE */
2048 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA );
2049 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2051 result = GetCompressedFileSizeW( fileW, NULL );
2052 if (result == INVALID_FILE_SIZE)
2056 SET_AX( context, LOWORD(result) );
2057 SET_DX( context, HIWORD(result) );
2061 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
2063 INT_BARF( context, 0x21 );
2066 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
2067 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2069 handle = CreateFileW( fileW, GENERIC_WRITE,
2070 FILE_SHARE_READ | FILE_SHARE_WRITE,
2071 NULL, OPEN_EXISTING, 0, 0 );
2072 if (handle == INVALID_HANDLE_VALUE)
2075 DosDateTimeToFileTime( DI_reg(context),
2078 status = SetFileTime( handle, NULL, NULL, &filetime );
2080 CloseHandle( handle );
2085 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
2087 INT_BARF( context, 0x21 );
2090 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
2091 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2093 handle = CreateFileW( fileW, GENERIC_READ,
2094 FILE_SHARE_READ | FILE_SHARE_WRITE,
2095 NULL, OPEN_EXISTING, 0, 0 );
2096 if (handle == INVALID_HANDLE_VALUE)
2099 status = GetFileTime( handle, NULL, NULL, &filetime );
2102 FileTimeToDosDateTime( &filetime, &date, &time );
2103 SET_DI( context, date );
2104 SET_CX( context, time );
2107 CloseHandle( handle );
2112 case 0x05: /* SET FILE LAST ACCESS DATE */
2114 INT_BARF( context, 0x21 );
2117 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA );
2118 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2120 handle = CreateFileW( fileW, GENERIC_WRITE,
2121 FILE_SHARE_READ | FILE_SHARE_WRITE,
2122 NULL, OPEN_EXISTING, 0, 0 );
2123 if (handle == INVALID_HANDLE_VALUE)
2126 DosDateTimeToFileTime( DI_reg(context),
2129 status = SetFileTime( handle, NULL, &filetime, NULL );
2131 CloseHandle( handle );
2136 case 0x06: /* GET FILE LAST ACCESS DATE */
2138 INT_BARF( context, 0x21 );
2141 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA );
2142 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2144 handle = CreateFileW( fileW, GENERIC_READ,
2145 FILE_SHARE_READ | FILE_SHARE_WRITE,
2146 NULL, OPEN_EXISTING, 0, 0 );
2147 if (handle == INVALID_HANDLE_VALUE)
2150 status = GetFileTime( handle, NULL, &filetime, NULL );
2153 FileTimeToDosDateTime( &filetime, &date, NULL );
2154 SET_DI( context, date );
2157 CloseHandle( handle );
2162 case 0x07: /* SET FILE CREATION DATE AND TIME */
2164 INT_BARF( context, 0x21 );
2167 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA );
2169 handle = CreateFileW( fileW, GENERIC_WRITE,
2170 FILE_SHARE_READ | FILE_SHARE_WRITE,
2171 NULL, OPEN_EXISTING, 0, 0 );
2172 if (handle == INVALID_HANDLE_VALUE)
2176 * FIXME: SI has number of 10-millisecond units past time in CX.
2178 DosDateTimeToFileTime( DI_reg(context),
2181 status = SetFileTime( handle, &filetime, NULL, NULL );
2183 CloseHandle( handle );
2188 case 0x08: /* GET FILE CREATION DATE AND TIME */
2190 INT_BARF( context, 0x21 );
2193 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2196 handle = CreateFileW( fileW, GENERIC_READ,
2197 FILE_SHARE_READ | FILE_SHARE_WRITE,
2198 NULL, OPEN_EXISTING, 0, 0 );
2199 if (handle == INVALID_HANDLE_VALUE)
2202 status = GetFileTime( handle, &filetime, NULL, NULL );
2205 FileTimeToDosDateTime( &filetime, &date, &time );
2206 SET_DI( context, date );
2207 SET_CX( context, time );
2209 * FIXME: SI has number of 10-millisecond units past
2212 SET_SI( context, 0 );
2215 CloseHandle(handle);
2220 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2221 if (islong || context->Ebp != 0x5053)
2222 INT_BARF( context, 0x21 );
2225 switch(CL_reg(context))
2228 if (!INT21_CreateDirectory( context ))
2233 if (!INT21_RenameFile( context ))
2238 INT_BARF( context, 0x21 );
2244 INT_BARF( context, 0x21 );
2251 /***********************************************************************
2252 * INT21_FileDateTime
2254 * Handler for function 0x57.
2256 static BOOL INT21_FileDateTime( CONTEXT86 *context )
2258 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2262 switch (AL_reg(context)) {
2263 case 0x00: /* Get last-written stamp */
2264 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2267 if (!GetFileTime( handle, NULL, NULL, &filetime ))
2269 FileTimeToDosDateTime( &filetime, &date, &time );
2270 SET_DX( context, date );
2271 SET_CX( context, time );
2275 case 0x01: /* Set last-written stamp */
2276 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2279 DosDateTimeToFileTime( DX_reg(context),
2282 if (!SetFileTime( handle, NULL, NULL, &filetime ))
2287 case 0x04: /* Get last access stamp, DOS 7 */
2288 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2291 if (!GetFileTime( handle, NULL, &filetime, NULL ))
2293 FileTimeToDosDateTime( &filetime, &date, &time );
2294 SET_DX( context, date );
2295 SET_CX( context, time );
2299 case 0x05: /* Set last access stamp, DOS 7 */
2300 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2303 DosDateTimeToFileTime( DX_reg(context),
2306 if (!SetFileTime( handle, NULL, &filetime, NULL ))
2311 case 0x06: /* Get creation stamp, DOS 7 */
2312 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2315 if (!GetFileTime( handle, &filetime, NULL, NULL ))
2317 FileTimeToDosDateTime( &filetime, &date, &time );
2318 SET_DX( context, date );
2319 SET_CX( context, time );
2321 * FIXME: SI has number of 10-millisecond units past time in CX.
2323 SET_SI( context, 0 );
2327 case 0x07: /* Set creation stamp, DOS 7 */
2328 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2332 * FIXME: SI has number of 10-millisecond units past time in CX.
2334 DosDateTimeToFileTime( DX_reg(context),
2337 if (!SetFileTime( handle, &filetime, NULL, NULL ))
2343 INT_BARF( context, 0x21 );
2351 /***********************************************************************
2354 * Handler for functions 0x51 and 0x62.
2356 static void INT21_GetPSP( CONTEXT86 *context )
2358 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
2361 * FIXME: should we return the original DOS PSP upon
2364 if (!ISV86(context) && DOSVM_IsWin16())
2365 SET_BX( context, LOWORD(GetCurrentPDB16()) );
2367 SET_BX( context, DOSVM_psp );
2370 static inline void setword( BYTE *ptr, WORD w )
2373 ptr[1] = (BYTE)(w >> 8);
2376 static void CreateBPB(int drive, BYTE *data, BOOL16 limited)
2377 /* limited == TRUE is used with INT 0x21/0x440d */
2379 /* FIXME: we're forcing some values without checking that those are valid */
2384 setword(&data[3], 0);
2386 setword(&data[6], 240);
2387 setword(&data[8], 64000);
2389 setword(&data[0x0b], 40);
2390 setword(&data[0x0d], 56);
2391 setword(&data[0x0f], 2);
2392 setword(&data[0x11], 0);
2395 setword(&data[0x1f], 800);
2397 setword(&data[0x22], 1);
2404 setword(&data[3], 0);
2406 setword(&data[6], 240);
2407 setword(&data[8], 2880);
2409 setword(&data[0x0b], 6);
2410 setword(&data[0x0d], 18);
2411 setword(&data[0x0f], 2);
2412 setword(&data[0x11], 0);
2415 setword(&data[0x1f], 80);
2417 setword(&data[0x22], 2);
2422 inline DWORD INT21_Ioctl_CylHeadSect2Lin(DWORD cyl, WORD head, WORD sec, WORD cyl_cnt, WORD head_cnt, WORD sec_cnt)
2424 DWORD res = (cyl * head_cnt*sec_cnt + head * sec_cnt + sec);
2428 /***********************************************************************
2431 * Handler for block device IOCTLs.
2433 static void INT21_Ioctl_Block( CONTEXT86 *context )
2436 BYTE drive = INT21_MapDrive( BL_reg(context) );
2437 WCHAR drivespec[4] = {'A', ':', '\\', 0};
2440 drivespec[0] += drive;
2441 drivetype = GetDriveTypeW( drivespec );
2443 RESET_CFLAG(context);
2444 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
2446 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2447 AL_reg(context), 'A' + drive );
2448 SetLastError( ERROR_INVALID_DRIVE );
2449 SET_AX( context, ERROR_INVALID_DRIVE );
2450 SET_CFLAG( context );
2454 switch (AL_reg(context))
2456 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2457 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2458 INT_BARF( context, 0x21 );
2461 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2462 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2465 if (drivetype == DRIVE_REMOVABLE)
2466 SET_AX( context, 0 ); /* removable */
2468 SET_AX( context, 1 ); /* not removable */
2471 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2472 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2475 if (drivetype == DRIVE_REMOTE)
2476 SET_DX( context, (1<<9) | (1<<12) ); /* remote + no direct IO */
2478 SET_DX( context, 0 ); /* FIXME: use driver attr here */
2481 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2482 /* Get pointer to IOCTL parameter block */
2483 dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2485 switch (CX_reg(context))
2487 case 0x0841: /* write logical device track */
2488 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2491 WORD head = *(WORD *)(dataptr+1);
2492 WORD cyl = *(WORD *)(dataptr+3);
2493 WORD sect = *(WORD *)(dataptr+5);
2494 WORD nrsect = *(WORD *)(dataptr+7);
2495 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2496 WORD cyl_cnt, head_cnt, sec_cnt;
2498 /* FIXME: we're faking some values here */
2504 SET_AX( context, ERROR_WRITE_FAULT );
2515 if (!DOSVM_RawWrite(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2517 SET_AX( context, ERROR_WRITE_FAULT );
2523 case 0x084a: /* lock logical volume */
2524 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2525 BH_reg(context), DX_reg(context), 'A' + drive );
2528 case 0x0860: /* get device parameters */
2529 /* FIXME: we're faking some values here */
2530 /* used by w4wgrp's winfile */
2531 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2533 dataptr[6] = 0; /* media type */
2536 dataptr[1] = 0x05; /* fixed disk */
2537 setword(&dataptr[2], 0x01); /* non removable */
2538 setword(&dataptr[4], 0x300); /* # of cylinders */
2542 dataptr[1] = 0x07; /* block dev, floppy */
2543 setword(&dataptr[2], 0x02); /* removable */
2544 setword(&dataptr[4], 80); /* # of cylinders */
2546 CreateBPB(drive, &dataptr[7], TRUE);
2547 RESET_CFLAG(context);
2550 case 0x0861: /* read logical device track */
2551 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2554 WORD head = *(WORD *)(dataptr+1);
2555 WORD cyl = *(WORD *)(dataptr+3);
2556 WORD sect = *(WORD *)(dataptr+5);
2557 WORD nrsect = *(WORD *)(dataptr+7);
2558 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2559 WORD cyl_cnt, head_cnt, sec_cnt;
2561 /* FIXME: we're faking some values here */
2575 if (!DOSVM_RawRead(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2577 SET_AX( context, ERROR_READ_FAULT );
2583 case 0x0866: /* get volume serial number */
2585 WCHAR label[12],fsname[9];
2588 drivespec[0] += drive;
2589 GetVolumeInformationW(drivespec, label, 12, &serial, NULL, NULL, fsname, 9);
2590 *(WORD*)dataptr = 0;
2591 memcpy(dataptr+2,&serial,4);
2592 WideCharToMultiByte(CP_OEMCP, 0, label, 11, dataptr + 6, 11, NULL, NULL);
2593 WideCharToMultiByte(CP_OEMCP, 0, fsname, 8, dataptr + 17, 8, NULL, NULL);
2597 case 0x086a: /* unlock logical volume */
2598 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2602 case 0x086f: /* get drive map information */
2603 memset(dataptr+1, '\0', dataptr[0]-1);
2604 dataptr[1] = dataptr[0];
2605 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
2606 dataptr[3] = 0xFF; /* no physical drive */
2610 /* Trial and error implementation */
2611 SET_AX( context, drivetype == DRIVE_UNKNOWN ? 0x0f : 0x01 );
2612 SET_CFLAG(context); /* Seems to be set all the time */
2616 INT_BARF( context, 0x21 );
2620 case 0x0e: /* GET LOGICAL DRIVE MAP */
2621 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2623 /* FIXME: this is not correct if drive has mappings */
2624 SET_AL( context, 0 ); /* drive has no mapping */
2627 case 0x0f: /* SET LOGICAL DRIVE MAP */
2629 WCHAR dev[3], tgt[4];
2631 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2632 INT21_DriveName( BL_reg(context)));
2633 dev[0] = 'A' + drive; dev[1] = ':'; dev[2] = 0;
2634 tgt[0] = 'A' + drive + 1; tgt[1] = ':'; tgt[2] = '\\'; tgt[3] = 0;
2635 if (!DefineDosDeviceW(DDD_RAW_TARGET_PATH, dev, tgt))
2638 SET_AX( context, 0x000F ); /* invalid drive */
2643 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2645 INT_BARF( context, 0x21 );
2650 /***********************************************************************
2651 * INT21_IoctlScsiMgrHandler
2653 * IOCTL handler for the SCSIMGR device.
2655 static void INT21_IoctlScsiMgrHandler( CONTEXT86 *context )
2657 switch (AL_reg(context))
2659 case 0x00: /* GET DEVICE INFORMATION */
2660 SET_DX( context, 0xc0c0 );
2663 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2664 DOSVM_ASPIHandler(context);
2667 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2668 SET_DX( context, 0 );
2671 case 0x01: /* SET DEVICE INFORMATION */
2672 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2673 case 0x06: /* GET INPUT STATUS */
2674 case 0x07: /* GET OUTPUT STATUS */
2675 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2676 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2678 INT_BARF( context, 0x21 );
2684 /***********************************************************************
2685 * INT21_IoctlEMSHandler
2687 * IOCTL handler for the EMXXXX0 device.
2689 static void INT21_IoctlEMSHandler( CONTEXT86 *context )
2691 EMS_Ioctl_Handler(context);
2695 /***********************************************************************
2696 * INT21_IoctlHPScanHandler
2698 * IOCTL handler for the HPSCAN device.
2700 static void INT21_IoctlHPScanHandler( CONTEXT86 *context )
2702 switch (AL_reg(context))
2704 case 0x00: /* GET DEVICE INFORMATION */
2705 SET_DX( context, 0xc0c0 );
2708 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2709 SET_DX( context, 0 );
2712 case 0x01: /* SET DEVICE INFORMATION */
2713 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2714 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2715 case 0x06: /* GET INPUT STATUS */
2716 case 0x07: /* GET OUTPUT STATUS */
2717 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2718 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2720 INT_BARF( context, 0x21 );
2726 /***********************************************************************
2729 * Handler for character device IOCTLs.
2731 static void INT21_Ioctl_Char( CONTEXT86 *context )
2735 int IsConsoleIOHandle = 0;
2736 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2738 status = wine_server_handle_to_fd( handle, 0, &fd, NULL );
2741 if( VerifyConsoleIoHandle( handle))
2742 IsConsoleIOHandle = 1;
2744 SET_AX( context, RtlNtStatusToDosError(status) );
2745 SET_CFLAG( context );
2750 IsConsoleIOHandle = isatty( fd);
2751 wine_server_release_fd( handle, fd );
2752 for (i = 0; i < NB_MAGIC_DEVICES; i++)
2754 if (!magic_devices[i].handle) continue;
2755 if (magic_devices[i].dev == st.st_dev &&
2756 magic_devices[i].ino == st.st_ino)
2759 magic_devices[i].ioctl_handler( context );
2765 /* no magic device found, do default handling */
2767 switch (AL_reg(context))
2769 case 0x00: /* GET DEVICE INFORMATION */
2770 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context) );
2771 if (IsConsoleIOHandle || S_ISCHR(st.st_mode))
2774 * Returns attribute word in DX:
2775 * Bit 14 - Device driver can process IOCTL requests.
2776 * Bit 13 - Output until busy supported.
2777 * Bit 11 - Driver supports OPEN/CLOSE calls.
2779 * Bit 7 - Set (indicates device).
2780 * Bit 6 - EOF on input.
2781 * Bit 5 - Raw (binary) mode.
2782 * Bit 4 - Device is special (uses int29).
2783 * Bit 3 - Clock device.
2784 * Bit 2 - NUL device.
2785 * Bit 1 - Console output device.
2786 * Bit 0 - Console input device.
2788 SET_DX( context, IsConsoleIOHandle ? 0x80c3 : 0x80c0 /* FIXME */ );
2793 * Returns attribute word in DX:
2794 * Bit 15 - File is remote.
2795 * Bit 14 - Don't set file date/time on closing.
2796 * Bit 11 - Media not removable.
2797 * Bit 8 - Generate int24 if no disk space on write
2798 * or read past end of file
2799 * Bit 7 - Clear (indicates file).
2800 * Bit 6 - File has not been written.
2801 * Bit 5..0 - Drive number (0=A:,...)
2803 * FIXME: Should check if file is on remote or removable drive.
2804 * FIXME: Should use drive file is located on (and not current).
2806 SET_DX( context, 0x0140 + INT21_GetCurrentDrive() );
2810 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2811 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context) );
2813 * Returns attribute word in DX:
2814 * Bit 15 - Set if remote.
2815 * Bit 14 - Set if date/time not set on close.
2817 * FIXME: Should check if file is on remote drive.
2819 SET_DX( context, 0 );
2822 case 0x01: /* SET DEVICE INFORMATION */
2823 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2824 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2825 case 0x06: /* GET INPUT STATUS */
2826 case 0x07: /* GET OUTPUT STATUS */
2827 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2828 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2830 INT_BARF( context, 0x21 );
2836 /***********************************************************************
2839 * Handler for function 0x44.
2841 static void INT21_Ioctl( CONTEXT86 *context )
2843 switch (AL_reg(context))
2849 INT21_Ioctl_Char( context );
2854 INT21_Ioctl_Block( context );
2859 INT21_Ioctl_Char( context );
2864 INT21_Ioctl_Block( context );
2868 INT21_Ioctl_Char( context );
2871 case 0x0b: /* SET SHARING RETRY COUNT */
2872 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2873 CX_reg(context), DX_reg(context) );
2874 if (!CX_reg(context))
2876 SET_AX( context, 1 );
2877 SET_CFLAG( context );
2881 DOSDEV_SetSharingRetry( CX_reg(context), DX_reg(context) );
2882 RESET_CFLAG( context );
2887 INT21_Ioctl_Char( context );
2893 INT21_Ioctl_Block( context );
2897 INT21_Ioctl_Char( context );
2901 INT21_Ioctl_Block( context );
2904 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2905 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2906 SET_CFLAG(context); /* Error / This is not DR DOS. */
2907 SET_AX( context, 0x0001 ); /* Invalid function */
2910 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2911 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2912 SET_CFLAG(context); /* Error / This is not DR DOS. */
2913 SET_AX( context, 0x0001 ); /* Invalid function */
2916 case 0xe0: /* Sun PC-NFS API */
2917 TRACE( "Sun PC-NFS API\n" );
2922 INT_BARF( context, 0x21 );
2927 /***********************************************************************
2930 * Handler for function 0x73.
2932 static BOOL INT21_Fat32( CONTEXT86 *context )
2934 switch (AL_reg(context))
2936 case 0x02: /* FAT32 - GET EXTENDED DPB */
2938 BYTE drive = INT21_MapDrive( DL_reg(context) );
2939 WORD *ptr = CTX_SEG_OFF_TO_LIN(context,
2940 context->SegEs, context->Edi);
2941 INT21_DPB *target = (INT21_DPB*)(ptr + 1);
2944 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context) );
2946 if ( CX_reg(context) < sizeof(INT21_DPB) + 2 || *ptr < sizeof(INT21_DPB) )
2948 SetLastError( ERROR_BAD_LENGTH );
2952 if ( !INT21_FillDrivePB( drive ) )
2954 SetLastError( ERROR_INVALID_DRIVE );
2958 source = &INT21_GetHeapPointer()->misc_dpb_list[drive];
2960 *ptr = sizeof(INT21_DPB);
2961 memcpy( target, source, sizeof(INT21_DPB));
2963 if (LOWORD(context->Esi) != 0xF1A6)
2965 target->driver_header = 0;
2970 FIXME( "Caller requested driver and next DPB pointers!\n" );
2975 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2977 WCHAR dirW[MAX_PATH];
2978 char *dirA = CTX_SEG_OFF_TO_LIN( context,
2979 context->SegDs, context->Edx );
2980 BYTE *data = CTX_SEG_OFF_TO_LIN( context,
2981 context->SegEs, context->Edi );
2982 DWORD cluster_sectors;
2984 DWORD free_clusters;
2985 DWORD total_clusters;
2987 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA );
2988 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
2990 if (CX_reg(context) < 44)
2992 SetLastError( ERROR_BAD_LENGTH );
2996 if (!GetDiskFreeSpaceW( dirW, &cluster_sectors, §or_bytes,
2997 &free_clusters, &total_clusters ))
3000 *(WORD*) (data + 0) = 44; /* size of structure */
3001 *(WORD*) (data + 2) = 0; /* version */
3002 *(DWORD*)(data + 4) = cluster_sectors;
3003 *(DWORD*)(data + 8) = sector_bytes;
3004 *(DWORD*)(data + 12) = free_clusters;
3005 *(DWORD*)(data + 16) = total_clusters;
3008 * Below we have free/total sectors and
3009 * free/total allocation units without adjustment
3010 * for compression. We fake both using cluster information.
3012 *(DWORD*)(data + 20) = free_clusters * cluster_sectors;
3013 *(DWORD*)(data + 24) = total_clusters * cluster_sectors;
3014 *(DWORD*)(data + 28) = free_clusters;
3015 *(DWORD*)(data + 32) = total_clusters;
3018 * Between (data + 36) and (data + 43) there
3019 * are eight reserved bytes.
3025 INT_BARF( context, 0x21 );
3031 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA *dataA,
3032 const WIN32_FIND_DATAW *dataW)
3034 dataA->dwFileAttributes = dataW->dwFileAttributes;
3035 dataA->ftCreationTime = dataW->ftCreationTime;
3036 dataA->ftLastAccessTime = dataW->ftLastAccessTime;
3037 dataA->ftLastWriteTime = dataW->ftLastWriteTime;
3038 dataA->nFileSizeHigh = dataW->nFileSizeHigh;
3039 dataA->nFileSizeLow = dataW->nFileSizeLow;
3040 WideCharToMultiByte( CP_OEMCP, 0, dataW->cFileName, -1,
3041 dataA->cFileName, sizeof(dataA->cFileName), NULL, NULL );
3042 WideCharToMultiByte( CP_OEMCP, 0, dataW->cAlternateFileName, -1,
3043 dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName), NULL, NULL );
3046 /***********************************************************************
3047 * INT21_LongFilename
3049 * Handler for function 0x71.
3051 static void INT21_LongFilename( CONTEXT86 *context )
3053 BOOL bSetDOSExtendedError = FALSE;
3054 WCHAR pathW[MAX_PATH];
3057 if (HIBYTE(HIWORD(GetVersion16())) < 0x07)
3059 TRACE( "LONG FILENAME - functions supported only under DOS7\n" );
3060 SET_CFLAG( context );
3061 SET_AL( context, 0 );
3065 switch (AL_reg(context))
3067 case 0x0d: /* RESET DRIVE */
3068 INT_BARF( context, 0x21 );
3071 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
3072 if (!INT21_CreateDirectory( context ))
3073 bSetDOSExtendedError = TRUE;
3076 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
3077 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3079 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA);
3080 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3081 if (!RemoveDirectoryW( pathW )) bSetDOSExtendedError = TRUE;
3084 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
3085 if (!INT21_SetCurrentDirectory( context ))
3086 bSetDOSExtendedError = TRUE;
3089 case 0x41: /* LONG FILENAME - DELETE FILE */
3090 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3092 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA );
3093 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3095 if (!DeleteFileW( pathW )) bSetDOSExtendedError = TRUE;
3098 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
3099 if (!INT21_FileAttributes( context, BL_reg(context), TRUE ))
3100 bSetDOSExtendedError = TRUE;
3103 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
3104 if (!INT21_GetCurrentDirectory( context, TRUE ))
3105 bSetDOSExtendedError = TRUE;
3108 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
3112 WIN32_FIND_DATAW dataW;
3113 WIN32_FIND_DATAA* dataA;
3115 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
3116 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA);
3118 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3119 handle = FindFirstFileW(pathW, &dataW);
3121 dataA = (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
3123 if (handle != INVALID_HANDLE_VALUE &&
3124 (h16 = GlobalAlloc16(GMEM_MOVEABLE, sizeof(handle))))
3126 HANDLE* ptr = GlobalLock16( h16 );
3128 GlobalUnlock16( h16 );
3129 SET_AX( context, h16 );
3130 INT21_ConvertFindDataWtoA(dataA, &dataW);
3134 if (handle != INVALID_HANDLE_VALUE) FindClose(handle);
3135 SET_AX( context, INVALID_HANDLE_VALUE16);
3136 bSetDOSExtendedError = TRUE;
3141 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
3143 HGLOBAL16 h16 = BX_reg(context);
3145 WIN32_FIND_DATAW dataW;
3146 WIN32_FIND_DATAA* dataA;
3148 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
3151 dataA = (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
3154 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
3156 if (!FindNextFileW(*ptr, &dataW)) bSetDOSExtendedError = TRUE;
3157 else INT21_ConvertFindDataWtoA(dataA, &dataW);
3158 GlobalUnlock16( h16 );
3162 SetLastError( ERROR_INVALID_HANDLE );
3163 bSetDOSExtendedError = TRUE;
3168 case 0x56: /* LONG FILENAME - RENAME FILE */
3169 if (!INT21_RenameFile(context))
3170 bSetDOSExtendedError = TRUE;
3173 case 0x60: /* LONG FILENAME - CONVERT PATH */
3175 WCHAR res[MAX_PATH];
3177 switch (CL_reg(context))
3179 case 0x00: /* "truename" - Canonicalize path */
3181 * FIXME: This is not 100% equal to 0x01 case,
3182 * if you fix this, fix int21 subfunction 0x60, too.
3185 case 0x01: /* Get short filename or path */
3186 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
3187 if (!GetShortPathNameW(pathW, res, 67))
3188 bSetDOSExtendedError = TRUE;
3191 SET_AX( context, 0 );
3192 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
3193 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3198 case 0x02: /* Get canonical long filename or path */
3199 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
3200 if (!GetFullPathNameW(pathW, 128, res, NULL))
3201 bSetDOSExtendedError = TRUE;
3204 SET_AX( context, 0 );
3205 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
3206 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3211 FIXME("Unimplemented long file name function:\n");
3212 INT_BARF( context, 0x21 );
3214 SET_AL( context, 0 );
3220 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
3221 if (!INT21_CreateFile( context, context->Esi, TRUE,
3222 BX_reg(context), DL_reg(context) ))
3223 bSetDOSExtendedError = TRUE;
3226 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
3228 DWORD filename_len, flags;
3231 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
3233 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA);
3234 SET_AX( context, 0 );
3235 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3236 if (!GetVolumeInformationW( pathW, NULL, 0, NULL, &filename_len,
3239 INT_BARF( context, 0x21 );
3243 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
3244 SET_CX( context, filename_len );
3245 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
3246 WideCharToMultiByte(CP_OEMCP, 0, dstW, -1,
3247 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3252 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
3254 HGLOBAL16 h16 = BX_reg(context);
3257 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
3259 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
3261 if (!FindClose( *ptr )) bSetDOSExtendedError = TRUE;
3262 GlobalUnlock16( h16 );
3263 GlobalFree16( h16 );
3267 SetLastError( ERROR_INVALID_HANDLE );
3268 bSetDOSExtendedError = TRUE;
3273 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
3275 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
3276 BY_HANDLE_FILE_INFORMATION *info =
3277 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3279 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
3281 if (!GetFileInformationByHandle(handle, info))
3282 bSetDOSExtendedError = TRUE;
3286 case 0xa7: /* LONG FILENAME - CONVERT TIME */
3287 switch (BL_reg(context))
3289 case 0x00: /* FILE TIME TO DOS TIME */
3292 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3296 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
3298 FileTimeToDosDateTime( filetime, &date, &time );
3300 SET_DX( context, date );
3301 SET_CX( context, time );
3304 * FIXME: BH has number of 10-millisecond units
3307 SET_BH( context, 0 );
3311 case 0x01: /* DOS TIME TO FILE TIME */
3313 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3317 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
3320 * FIXME: BH has number of 10-millisecond units
3323 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
3329 INT_BARF( context, 0x21 );
3334 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
3335 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3336 case 0xaa: /* LONG FILENAME - SUBST */
3338 FIXME("Unimplemented long file name function:\n");
3339 INT_BARF( context, 0x21 );
3341 SET_AL( context, 0 );
3345 if (bSetDOSExtendedError)
3347 SET_AX( context, GetLastError() );
3348 SET_CFLAG( context );
3353 /***********************************************************************
3358 * - subfunction 0x56 of function 0x71
3359 * - subfunction 0xff of function 0x43 (CL == 0x56)
3361 static BOOL INT21_RenameFile( CONTEXT86 *context )
3363 WCHAR fromW[MAX_PATH];
3364 WCHAR toW[MAX_PATH];
3365 char *fromA = CTX_SEG_OFF_TO_LIN(context,
3366 context->SegDs,context->Edx);
3367 char *toA = CTX_SEG_OFF_TO_LIN(context,
3368 context->SegEs,context->Edi);
3370 TRACE( "RENAME FILE %s to %s\n", fromA, toA );
3371 MultiByteToWideChar(CP_OEMCP, 0, fromA, -1, fromW, MAX_PATH);
3372 MultiByteToWideChar(CP_OEMCP, 0, toA, -1, toW, MAX_PATH);
3374 return MoveFileW( fromW, toW );
3378 /***********************************************************************
3384 static BOOL INT21_NetworkFunc (CONTEXT86 *context)
3386 switch (AL_reg(context))
3388 case 0x00: /* Get machine name. */
3390 WCHAR dstW[MAX_COMPUTERNAME_LENGTH + 1];
3391 DWORD s = sizeof(dstW) / sizeof(WCHAR);
3394 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
3395 TRACE("getting machine name to %p\n", dst);
3396 if (!GetComputerNameW(dstW, &s) ||
3397 !WideCharToMultiByte(CP_OEMCP, 0, dstW, -1, dst, 16, NULL, NULL))
3400 SetLastError( ER_NoNetwork );
3403 for (len = strlen(dst); len < 15; len++) dst[len] = ' ';
3405 SET_CH( context, 1 ); /* Valid */
3406 SET_CL( context, 1 ); /* NETbios number??? */
3407 TRACE("returning %s\n", debugstr_an(dst, 16));
3412 SetLastError( ER_NoNetwork );
3417 /******************************************************************
3418 * INT21_GetDiskSerialNumber
3421 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
3423 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3424 WCHAR path[] = {'A',':',0}, label[11];
3427 path[0] += INT21_MapDrive(BL_reg(context));
3428 if (!GetVolumeInformationW( path, label, 11, &serial, NULL, NULL, NULL, 0))
3430 SetLastError( ERROR_INVALID_DRIVE );
3434 *(WORD *)dataptr = 0;
3435 memcpy(dataptr + 2, &serial, sizeof(DWORD));
3436 WideCharToMultiByte(CP_OEMCP, 0, label, 11, dataptr + 6, 11, NULL, NULL);
3437 strncpy(dataptr + 17, "FAT16 ", 8);
3442 /******************************************************************
3443 * INT21_SetDiskSerialNumber
3446 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
3449 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3450 int drive = INT21_MapDrive(BL_reg(context));
3452 if (!is_valid_drive(drive))
3454 SetLastError( ERROR_INVALID_DRIVE );
3458 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
3461 FIXME("Setting drive serial number is no longer supported\n");
3462 SetLastError( ERROR_NOT_SUPPORTED );
3468 /******************************************************************
3469 * INT21_GetFreeDiskSpace
3472 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
3474 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
3475 WCHAR root[] = {'A',':','\\',0};
3477 root[0] += INT21_MapDrive(DL_reg(context));
3478 if (!GetDiskFreeSpaceW( root, &cluster_sectors, §or_bytes,
3479 &free_clusters, &total_clusters )) return 0;
3480 /* make sure that the number of clusters fits in a 16 bits value */
3481 while( total_clusters & 0xffff0000) {
3482 cluster_sectors <<= 1;
3483 free_clusters >>= 1;
3484 total_clusters >>= 1;
3486 SET_AX( context, cluster_sectors );
3487 SET_BX( context, free_clusters );
3488 SET_CX( context, sector_bytes );
3489 SET_DX( context, total_clusters );
3493 /******************************************************************
3494 * INT21_GetDriveAllocInfo
3497 static int INT21_GetDriveAllocInfo( CONTEXT86 *context, BYTE drive )
3501 drive = INT21_MapDrive( drive );
3502 if (!INT21_FillDrivePB( drive )) return 0;
3503 dpb = &(INT21_GetHeapPointer()->misc_dpb_list[drive]);
3504 SET_AL( context, dpb->cluster_sectors + 1 );
3505 SET_CX( context, dpb->sector_bytes );
3506 SET_DX( context, dpb->num_clusters1 );
3508 context->SegDs = INT21_GetHeapSelector( context );
3509 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive].media_ID ) );
3513 /***********************************************************************
3514 * INT21_GetExtendedError
3516 static void INT21_GetExtendedError( CONTEXT86 *context )
3518 BYTE class, action, locus;
3519 WORD error = GetLastError();
3524 class = action = locus = 0;
3526 case ERROR_DIR_NOT_EMPTY:
3531 case ERROR_ACCESS_DENIED:
3532 class = EC_AccessDenied;
3536 case ERROR_CANNOT_MAKE:
3537 class = EC_AccessDenied;
3541 case ERROR_DISK_FULL:
3542 case ERROR_HANDLE_DISK_FULL:
3543 class = EC_MediaError;
3547 case ERROR_FILE_EXISTS:
3548 case ERROR_ALREADY_EXISTS:
3553 case ERROR_FILE_NOT_FOUND:
3554 class = EC_NotFound;
3558 case ERROR_GEN_FAILURE:
3559 class = EC_SystemFailure;
3563 case ERROR_INVALID_DRIVE:
3564 class = EC_MediaError;
3568 case ERROR_INVALID_HANDLE:
3569 class = EC_ProgramError;
3573 case ERROR_LOCK_VIOLATION:
3574 class = EC_AccessDenied;
3578 case ERROR_NO_MORE_FILES:
3579 class = EC_MediaError;
3584 class = EC_NotFound;
3588 case ERROR_NOT_ENOUGH_MEMORY:
3589 class = EC_OutOfResource;
3593 case ERROR_PATH_NOT_FOUND:
3594 class = EC_NotFound;
3599 class = EC_NotFound;
3603 case ERROR_SHARING_VIOLATION:
3604 class = EC_Temporary;
3608 case ERROR_TOO_MANY_OPEN_FILES:
3609 class = EC_ProgramError;
3614 FIXME("Unknown error %d\n", error );
3615 class = EC_SystemFailure;
3620 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3621 error, class, action, locus );
3622 SET_AX( context, error );
3623 SET_BH( context, class );
3624 SET_BL( context, action );
3625 SET_CH( context, locus );
3628 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
3630 static int counter = 0;
3631 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
3632 char *p = name + strlen(name);
3634 /* despite what Ralf Brown says, some programs seem to call without
3635 * ending backslash (DOS accepts that, so we accept it too) */
3636 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
3640 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
3641 counter = (counter + 1) % 1000;
3644 Win32HandleToDosFileHandle(
3645 CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
3646 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
3647 CREATE_NEW, 0, 0 ) ) );
3648 if (AX_reg(context) != HFILE_ERROR16)
3650 TRACE("created %s\n", name );
3653 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
3657 /***********************************************************************
3658 * DOSFS_ToDosFCBFormat
3660 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3661 * expanding wild cards and converting to upper-case in the process.
3662 * File name can be terminated by '\0', '\\' or '/'.
3663 * Return FALSE if the name is not a valid DOS name.
3664 * 'buffer' must be at least 12 characters long.
3666 /* Chars we don't want to see in DOS file names */
3667 static BOOL INT21_ToDosFCBFormat( LPCWSTR name, LPWSTR buffer )
3669 static const WCHAR invalid_chars[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3673 /* Check for "." and ".." */
3678 for(i = 1; i < 11; i++) buffer[i] = ' ';
3685 return (!*p || (*p == '/') || (*p == '\\'));
3688 for (i = 0; i < 8; i++)
3705 if (strchrW( invalid_chars, *p )) return FALSE;
3706 buffer[i] = toupperW(*p);
3714 /* Skip all chars after wildcard up to first dot */
3715 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
3719 /* Check if name too long */
3720 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return FALSE;
3722 if (*p == '.') p++; /* Skip dot */
3724 for (i = 8; i < 11; i++)
3734 return FALSE; /* Second extension not allowed */
3742 if (strchrW( invalid_chars, *p )) return FALSE;
3743 buffer[i] = toupperW(*p);
3750 /* at most 3 character of the extension are processed
3751 * is something behind this ?
3753 while (*p == '*' || *p == ' ') p++; /* skip wildcards and spaces */
3754 return (!*p || (*p == '/') || (*p == '\\'));
3757 static HANDLE INT21_FindHandle;
3758 static const WCHAR *INT21_FindPath; /* will point to current dta->fullPath search */
3760 /******************************************************************
3763 static int INT21_FindFirst( CONTEXT86 *context )
3767 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3768 WCHAR maskW[12], pathW[MAX_PATH];
3769 static const WCHAR wildcardW[] = {'*','.','*',0};
3771 path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3772 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
3774 p = strrchrW( pathW, '\\');
3775 q = strrchrW( pathW, '/');
3779 if (pathW[0] && pathW[1] == ':') p = pathW + 2;
3784 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3785 * (doesn't matter as it is set below anyway)
3787 if (!INT21_ToDosFCBFormat( p, maskW ))
3789 SetLastError( ERROR_FILE_NOT_FOUND );
3790 SET_AX( context, ERROR_FILE_NOT_FOUND );
3794 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
3796 dta->fullPath = HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW) + (p - pathW)*sizeof(WCHAR) );
3797 memcpy( dta->fullPath, pathW, (p - pathW) * sizeof(WCHAR) );
3798 memcpy( dta->fullPath + (p - pathW), wildcardW, sizeof(wildcardW) );
3799 /* we must have a fully qualified file name in dta->fullPath
3800 * (we could have a UNC path, but this would lead to some errors later on)
3802 dta->drive = toupperW(dta->fullPath[0]) - 'A';
3804 dta->search_attr = CL_reg(context);
3808 /******************************************************************
3811 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3813 static BOOL match_short(LPCWSTR shortW, LPCSTR maskA)
3815 WCHAR mask[11], file[12];
3818 if (!INT21_ToDosFCBFormat( shortW, file )) return FALSE;
3819 MultiByteToWideChar(CP_OEMCP, 0, maskA, 11, mask, 11);
3820 for (i = 0; i < 11; i++)
3821 if (mask[i] != '?' && mask[i] != file[i]) return FALSE;
3825 static unsigned INT21_FindHelper(LPCWSTR fullPath, unsigned drive, unsigned count,
3826 LPCSTR mask, unsigned search_attr,
3827 WIN32_FIND_DATAW* entry)
3831 if ((search_attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
3833 WCHAR path[] = {' ',':',0};
3835 if (count) return 0;
3836 path[0] = drive + 'A';
3837 if (!GetVolumeInformationW(path, entry->cAlternateFileName, 13, NULL, NULL, NULL, NULL, 0)) return 0;
3838 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftCreationTime );
3839 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftLastAccessTime );
3840 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftLastWriteTime );
3841 entry->dwFileAttributes = FA_LABEL;
3842 entry->nFileSizeHigh = entry->nFileSizeLow = 0;
3843 TRACE("returning %s as label\n", debugstr_w(entry->cAlternateFileName));
3847 if (!INT21_FindHandle || INT21_FindPath != fullPath || count == 0)
3849 if (INT21_FindHandle) FindClose(INT21_FindHandle);
3850 INT21_FindHandle = FindFirstFileW(fullPath, entry);
3851 if (INT21_FindHandle == INVALID_HANDLE_VALUE)
3853 INT21_FindHandle = 0;
3856 INT21_FindPath = fullPath;
3857 /* we need to resync search */
3862 while (ncalls-- != 0)
3864 if (!FindNextFileW(INT21_FindHandle, entry))
3866 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3870 while (count < 0xffff)
3873 /* Check the file attributes, and path */
3874 if (!(entry->dwFileAttributes & ~search_attr) &&
3875 match_short(entry->cAlternateFileName[0] ? entry->cAlternateFileName : entry->cFileName,
3880 if (!FindNextFileW(INT21_FindHandle, entry))
3882 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3886 WARN("Too many directory entries in %s\n", debugstr_w(fullPath) );
3890 /******************************************************************
3893 static int INT21_FindNext( CONTEXT86 *context )
3895 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3896 DWORD attr = dta->search_attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
3897 WIN32_FIND_DATAW entry;
3900 if (!dta->fullPath) return 0;
3902 n = INT21_FindHelper(dta->fullPath, dta->drive, dta->count,
3903 dta->mask, attr, &entry);
3906 dta->fileattr = entry.dwFileAttributes;
3907 dta->filesize = entry.nFileSizeLow;
3908 FileTimeToDosDateTime( &entry.ftLastWriteTime, &dta->filedate, &dta->filetime );
3909 if (entry.cAlternateFileName[0])
3910 WideCharToMultiByte(CP_OEMCP, 0, entry.cAlternateFileName, -1,
3911 dta->filename, 13, NULL, NULL);
3913 WideCharToMultiByte(CP_OEMCP, 0, entry.cFileName, -1, dta->filename, 13, NULL, NULL);
3915 if (!memchr(dta->mask,'?',11))
3917 /* wildcardless search, release resources in case no findnext will
3918 * be issued, and as a workaround in case file creation messes up
3919 * findnext, as sometimes happens with pkunzip
3921 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3922 INT21_FindPath = dta->fullPath = NULL;
3927 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3928 INT21_FindPath = dta->fullPath = NULL;
3932 /* microsoft's programmers should be shot for using CP/M style int21
3933 calls in Windows for Workgroup's winfile.exe */
3935 /******************************************************************
3936 * INT21_FindFirstFCB
3939 static int INT21_FindFirstFCB( CONTEXT86 *context )
3941 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3944 WCHAR p[] = {' ',':',};
3946 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
3947 else pFCB = (FINDFILE_FCB *)fcb;
3948 drive = INT21_MapDrive( pFCB->drive );
3949 if (drive == MAX_DOS_DRIVES) return 0;
3952 pFCB->fullPath = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
3953 if (!pFCB->fullPath) return 0;
3954 GetLongPathNameW(p, pFCB->fullPath, MAX_PATH);
3959 /******************************************************************
3963 static int INT21_FindNextFCB( CONTEXT86 *context )
3965 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3967 LPBYTE pResult = INT21_GetCurrentDTA(context);
3968 DOS_DIRENTRY_LAYOUT *ddl;
3969 WIN32_FIND_DATAW entry;
3974 if (*fcb == 0xff) /* extended FCB ? */
3977 pFCB = (FINDFILE_FCB *)(fcb + 7);
3982 pFCB = (FINDFILE_FCB *)fcb;
3985 if (!pFCB->fullPath) return 0;
3986 n = INT21_FindHelper(pFCB->fullPath, INT21_MapDrive( pFCB->drive ),
3987 pFCB->count, pFCB->filename, attr, &entry);
3990 HeapFree( GetProcessHeap(), 0, pFCB->fullPath );
3991 INT21_FindPath = pFCB->fullPath = NULL;
3998 /* place extended FCB header before pResult if called with extended FCB */
4000 pResult += 6; /* leave reserved field behind */
4001 *pResult++ = entry.dwFileAttributes;
4003 *pResult++ = INT21_MapDrive( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
4004 ddl = (DOS_DIRENTRY_LAYOUT*)pResult;
4005 ddl->fileattr = entry.dwFileAttributes;
4006 ddl->cluster = 0; /* what else? */
4007 ddl->filesize = entry.nFileSizeLow;
4008 memset( ddl->reserved, 0, sizeof(ddl->reserved) );
4009 FileTimeToDosDateTime( &entry.ftLastWriteTime,
4010 &ddl->filedate, &ddl->filetime );
4012 /* Convert file name to FCB format */
4013 if (entry.cAlternateFileName[0])
4014 INT21_ToDosFCBFormat( entry.cAlternateFileName, nameW );
4016 INT21_ToDosFCBFormat( entry.cFileName, nameW );
4017 WideCharToMultiByte(CP_OEMCP, 0, nameW, 11, ddl->filename, 11, NULL, NULL);
4022 /******************************************************************
4023 * INT21_ParseFileNameIntoFCB
4026 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
4029 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
4031 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
4035 INT buffer_len, len;
4037 SET_AL( context, 0xff ); /* failed */
4039 TRACE("filename: '%s'\n", filename);
4042 while (*s && (*s != ' ') && (*s != '\r') && (*s != '\n'))
4046 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
4047 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
4048 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
4050 INT21_ToDosFCBFormat(buffer, fcbW);
4051 HeapFree(GetProcessHeap(), 0, buffer);
4052 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
4054 TRACE("FCB: '%s'\n", fcb + 1);
4056 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
4058 /* point DS:SI to first unparsed character */
4059 SET_SI( context, context->Esi + (int)s - (int)filename );
4062 static BOOL INT21_Dup2(HFILE16 hFile1, HFILE16 hFile2)
4064 HFILE16 res = HFILE_ERROR16;
4065 HANDLE handle, new_handle;
4066 #define DOS_TABLE_SIZE 256
4067 DWORD map[DOS_TABLE_SIZE / 32];
4070 handle = DosFileHandleToWin32Handle(hFile1);
4071 if (handle == INVALID_HANDLE_VALUE)
4075 /* now loop to allocate the same one... */
4076 memset(map, 0, sizeof(map));
4077 for (i = 0; i < DOS_TABLE_SIZE; i++)
4079 if (!DuplicateHandle(GetCurrentProcess(), handle,
4080 GetCurrentProcess(), &new_handle,
4081 0, FALSE, DUPLICATE_SAME_ACCESS))
4083 res = HFILE_ERROR16;
4086 res = Win32HandleToDosFileHandle(new_handle);
4087 if (res == HFILE_ERROR16 || res == hFile2) break;
4088 map[res / 32] |= 1 << (res % 32);
4090 /* clean up the allocated slots */
4091 for (i = 0; i < DOS_TABLE_SIZE; i++)
4093 if (map[i / 32] & (1 << (i % 32)))
4094 _lclose16((HFILE16)i);
4096 return res == hFile2;
4100 /***********************************************************************
4101 * DOSVM_Int21Handler
4103 * Interrupt 0x21 handler.
4105 void WINAPI DOSVM_Int21Handler( CONTEXT86 *context )
4107 BOOL bSetDOSExtendedError = FALSE;
4109 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
4110 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
4111 AX_reg(context), BX_reg(context),
4112 CX_reg(context), DX_reg(context),
4113 SI_reg(context), DI_reg(context),
4114 (WORD)context->SegDs, (WORD)context->SegEs,
4118 * Extended error is used by (at least) functions 0x2f to 0x62.
4119 * Function 0x59 returns extended error and error should not
4120 * be cleared before handling the function.
4122 if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59)
4125 RESET_CFLAG(context); /* Not sure if this is a good idea. */
4127 switch(AH_reg(context))
4129 case 0x00: /* TERMINATE PROGRAM */
4130 TRACE("TERMINATE PROGRAM\n");
4131 if (DOSVM_IsWin16())
4133 else if(ISV86(context))
4134 MZ_Exit( context, FALSE, 0 );
4136 ERR( "Called from DOS protected mode\n" );
4139 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
4142 TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
4143 INT21_ReadChar( &ascii, context );
4144 SET_AL( context, ascii );
4146 * FIXME: What to echo when extended keycodes are read?
4148 DOSVM_PutChar(AL_reg(context));
4152 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
4153 TRACE("Write Character to Standard Output\n");
4154 DOSVM_PutChar(DL_reg(context));
4157 case 0x03: /* READ CHARACTER FROM STDAUX */
4158 case 0x04: /* WRITE CHARACTER TO STDAUX */
4159 case 0x05: /* WRITE CHARACTER TO PRINTER */
4160 INT_BARF( context, 0x21 );
4163 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
4164 if (DL_reg(context) == 0xff)
4166 TRACE("Direct Console Input\n");
4168 if (INT21_ReadChar( NULL, NULL ))
4171 INT21_ReadChar( &ascii, context );
4172 SET_AL( context, ascii );
4173 RESET_ZFLAG( context );
4177 /* no character available */
4178 SET_AL( context, 0 );
4179 SET_ZFLAG( context );
4184 TRACE("Direct Console Output\n");
4185 DOSVM_PutChar(DL_reg(context));
4187 * At least DOS versions 2.1-7.0 return character
4188 * that was written in AL register.
4190 SET_AL( context, DL_reg(context) );
4194 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
4197 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
4198 INT21_ReadChar( &ascii, context );
4199 SET_AL( context, ascii );
4203 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
4206 TRACE("CHARACTER INPUT WITHOUT ECHO\n");
4207 INT21_ReadChar( &ascii, context );
4208 SET_AL( context, ascii );
4212 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
4213 TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
4214 context->SegDs, DX_reg(context) );
4216 LPSTR data = CTX_SEG_OFF_TO_LIN( context,
4217 context->SegDs, context->Edx );
4221 * Do NOT use strchr() to calculate the string length,
4222 * as '\0' is valid string content, too!
4223 * Maybe we should check for non-'$' strings, but DOS doesn't.
4225 while (*p != '$') p++;
4227 if (DOSVM_IsWin16())
4228 WriteFile( DosFileHandleToWin32Handle(1),
4229 data, p - data, &w, NULL );
4231 for(; data != p; data++)
4232 DOSVM_PutChar( *data );
4234 SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
4238 case 0x0a: /* BUFFERED INPUT */
4240 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context,
4245 TRACE( "BUFFERED INPUT (size=%d)\n", ptr[0] );
4248 * FIXME: Some documents state that
4249 * ptr[1] holds number of chars from last input which
4250 * may be recalled on entry, other documents do not mention
4254 TRACE( "Handle old chars in buffer!\n" );
4257 * ptr[0] - capacity (includes terminating CR)
4258 * ptr[1] - characters read (excludes terminating CR)
4260 result = INT21_BufferedInput( context, ptr + 2, ptr[0] );
4262 ptr[1] = (BYTE)result - 1;
4268 case 0x0b: /* GET STDIN STATUS */
4269 TRACE( "GET STDIN STATUS\n" );
4271 if (INT21_ReadChar( NULL, NULL ))
4272 SET_AL( context, 0xff ); /* character available */
4274 SET_AL( context, 0 ); /* no character available */
4278 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
4280 BYTE al = AL_reg(context); /* Input function to execute after flush. */
4282 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
4284 /* FIXME: buffers are not flushed */
4287 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
4288 * int21 function identified by AL will be called.
4290 if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
4292 SET_AH( context, al );
4293 DOSVM_Int21Handler( context );
4298 case 0x0d: /* DISK BUFFER FLUSH */
4299 TRACE("DISK BUFFER FLUSH ignored\n");
4302 case 0x0e: /* SELECT DEFAULT DRIVE */
4303 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context) );
4304 INT21_SetCurrentDrive( DL_reg(context) );
4305 SET_AL( context, MAX_DOS_DRIVES );
4308 case 0x0f: /* OPEN FILE USING FCB */
4309 INT21_OpenFileUsingFCB( context );
4312 case 0x10: /* CLOSE FILE USING FCB */
4313 INT21_CloseFileUsingFCB( context );
4316 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
4317 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
4318 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
4319 if (!INT21_FindFirstFCB(context))
4321 SET_AL( context, 0xff );
4324 /* else fall through */
4326 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
4327 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
4330 case 0x13: /* DELETE FILE USING FCB */
4331 INT_BARF( context, 0x21 );
4334 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
4335 INT21_SequentialReadFromFCB( context );
4338 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
4339 INT21_SequentialWriteToFCB( context );
4342 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
4343 case 0x17: /* RENAME FILE USING FCB */
4344 INT_BARF( context, 0x21 );
4347 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4348 SET_AL( context, 0 );
4351 case 0x19: /* GET CURRENT DEFAULT DRIVE */
4352 SET_AL( context, INT21_GetCurrentDrive() );
4353 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context ) );
4356 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
4357 TRACE( "SET DISK TRANSFER AREA ADDRESS %04lX:%04X\n",
4358 context->SegDs, DX_reg(context) );
4360 TDB *task = GlobalLock16( GetCurrentTask() );
4361 task->dta = MAKESEGPTR( context->SegDs, DX_reg(context) );
4365 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4366 if (!INT21_GetDriveAllocInfo(context, 0))
4367 SET_AX( context, 0xffff );
4370 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4371 if (!INT21_GetDriveAllocInfo(context, DL_reg(context)))
4372 SET_AX( context, 0xffff );
4375 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4376 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4377 SET_AL( context, 0 );
4380 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4382 BYTE drive = INT21_MapDrive( 0 );
4383 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4385 if (INT21_FillDrivePB( drive ))
4387 SET_AL( context, 0x00 ); /* success */
4388 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4389 context->SegDs = INT21_GetHeapSelector( context );
4393 SET_AL( context, 0xff ); /* invalid or network drive */
4398 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4399 SET_AL( context, 0 );
4402 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4403 INT21_ReadRandomRecordFromFCB( context );
4406 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4407 INT21_WriteRandomRecordToFCB( context );
4410 case 0x23: /* GET FILE SIZE FOR FCB */
4411 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4412 INT_BARF( context, 0x21 );
4415 case 0x25: /* SET INTERRUPT VECTOR */
4416 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4418 FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
4419 if (!ISV86(context) && DOSVM_IsWin16())
4420 DOSVM_SetPMHandler16( AL_reg(context), ptr );
4422 DOSVM_SetRMHandler( AL_reg(context), ptr );
4426 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4427 INT_BARF( context, 0x21 );
4430 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4431 INT21_RandomBlockReadFromFCB( context );
4434 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4435 INT21_RandomBlockWriteToFCB( context );
4438 case 0x29: /* PARSE FILENAME INTO FCB */
4439 INT21_ParseFileNameIntoFCB(context);
4442 case 0x2a: /* GET SYSTEM DATE */
4443 TRACE( "GET SYSTEM DATE\n" );
4446 GetLocalTime( &systime );
4447 SET_CX( context, systime.wYear );
4448 SET_DH( context, systime.wMonth );
4449 SET_DL( context, systime.wDay );
4450 SET_AL( context, systime.wDayOfWeek );
4454 case 0x2b: /* SET SYSTEM DATE */
4455 TRACE( "SET SYSTEM DATE\n" );
4457 WORD year = CX_reg(context);
4458 BYTE month = DH_reg(context);
4459 BYTE day = DL_reg(context);
4461 if (year >= 1980 && year <= 2099 &&
4462 month >= 1 && month <= 12 &&
4463 day >= 1 && day <= 31)
4465 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4467 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4471 SET_AL( context, 0xff ); /* invalid date */
4472 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4478 case 0x2c: /* GET SYSTEM TIME */
4479 TRACE( "GET SYSTEM TIME\n" );
4482 GetLocalTime( &systime );
4483 SET_CH( context, systime.wHour );
4484 SET_CL( context, systime.wMinute );
4485 SET_DH( context, systime.wSecond );
4486 SET_DL( context, systime.wMilliseconds / 10 );
4490 case 0x2d: /* SET SYSTEM TIME */
4491 if( CH_reg(context) >= 24 || CL_reg(context) >= 60 || DH_reg(context) >= 60 || DL_reg(context) >= 100 ) {
4492 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4493 CH_reg(context), CL_reg(context),
4494 DH_reg(context), DL_reg(context) );
4495 SET_AL( context, 0xFF );
4499 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4500 CH_reg(context), CL_reg(context),
4501 DH_reg(context), DL_reg(context) );
4502 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4506 case 0x2e: /* SET VERIFY FLAG */
4507 TRACE("SET VERIFY FLAG ignored\n");
4508 /* we cannot change the behaviour anyway, so just ignore it */
4511 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4512 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4514 TDB *task = GlobalLock16( GetCurrentTask() );
4515 context->SegEs = SELECTOROF( task->dta );
4516 SET_BX( context, OFFSETOF( task->dta ) );
4520 case 0x30: /* GET DOS VERSION */
4521 TRACE( "GET DOS VERSION - %s requested\n",
4522 (AL_reg(context) == 0x00) ? "OEM number" : "version flag" );
4524 if (AL_reg(context) == 0x00)
4525 SET_BH( context, 0xff ); /* OEM number => undefined */
4527 SET_BH( context, 0x08 ); /* version flag => DOS is in ROM */
4529 SET_AL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4530 SET_AH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4532 SET_BL( context, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4533 SET_CX( context, 0x3456 );
4536 case 0x31: /* TERMINATE AND STAY RESIDENT */
4537 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4540 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4542 BYTE drive = INT21_MapDrive( DL_reg(context) );
4543 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context) );
4545 if (INT21_FillDrivePB( drive ))
4547 SET_AL( context, 0x00 ); /* success */
4548 SET_DX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4549 context->SegDs = INT21_GetHeapSelector( context );
4553 SET_AL( context, 0xff ); /* invalid or network drive */
4558 case 0x33: /* MULTIPLEXED */
4559 switch (AL_reg(context))
4561 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4562 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4563 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4566 case 0x01: /* SET EXTENDED BREAK STATE */
4567 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4568 DOSCONF_GetConfig()->brk_flag = (DL_reg(context) > 0) ? 1 : 0;
4571 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4572 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4573 /* ugly coding in order to stay reentrant */
4574 if (DL_reg(context))
4576 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4577 DOSCONF_GetConfig()->brk_flag = 1;
4581 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4582 DOSCONF_GetConfig()->brk_flag = 0;
4586 case 0x05: /* GET BOOT DRIVE */
4587 TRACE("GET BOOT DRIVE\n");
4588 SET_DL( context, 3 );
4589 /* c: is Wine's bootdrive (a: is 1)*/
4592 case 0x06: /* GET TRUE VERSION NUMBER */
4593 TRACE("GET TRUE VERSION NUMBER\n");
4594 SET_BL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4595 SET_BH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4596 SET_DL( context, 0x00 ); /* revision */
4597 SET_DH( context, 0x08 ); /* DOS is in ROM */
4601 INT_BARF( context, 0x21 );
4606 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4607 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4608 context->SegEs = INT21_GetHeapSelector( context );
4609 SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
4612 case 0x35: /* GET INTERRUPT VECTOR */
4613 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4616 if (!ISV86(context) && DOSVM_IsWin16())
4617 addr = DOSVM_GetPMHandler16( AL_reg(context) );
4619 addr = DOSVM_GetRMHandler( AL_reg(context) );
4620 context->SegEs = SELECTOROF(addr);
4621 SET_BX( context, OFFSETOF(addr) );
4625 case 0x36: /* GET FREE DISK SPACE */
4626 TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
4627 INT21_DriveName( DL_reg(context) ));
4628 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
4631 case 0x37: /* SWITCHAR */
4633 switch (AL_reg(context))
4635 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4636 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4637 SET_AL( context, 0x00 ); /* success*/
4638 SET_DL( context, '/' );
4640 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4641 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4643 SET_AL( context, 0x00 ); /* success*/
4646 INT_BARF( context, 0x21 );
4652 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4653 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4654 if (AL_reg(context))
4656 WORD country = AL_reg(context);
4657 if (country == 0xff)
4658 country = BX_reg(context);
4659 if (country != INT21_GetSystemCountryCode()) {
4660 FIXME( "Requested info on non-default country %04x\n", country );
4665 if(AX_reg(context) != 2 )
4667 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context,
4670 SET_AX( context, INT21_GetSystemCountryCode() );
4671 SET_BX( context, INT21_GetSystemCountryCode() );
4672 RESET_CFLAG(context);
4676 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4677 if (!INT21_CreateDirectory( context ))
4678 bSetDOSExtendedError = TRUE;
4680 RESET_CFLAG(context);
4683 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4685 WCHAR dirW[MAX_PATH];
4686 char *dirA = CTX_SEG_OFF_TO_LIN(context,
4687 context->SegDs, context->Edx);
4689 TRACE( "REMOVE DIRECTORY %s\n", dirA );
4691 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
4693 if (!RemoveDirectoryW( dirW ))
4694 bSetDOSExtendedError = TRUE;
4696 RESET_CFLAG(context);
4700 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4701 if (!INT21_SetCurrentDirectory( context ))
4702 bSetDOSExtendedError = TRUE;
4704 RESET_CFLAG(context);
4707 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4708 if (!INT21_CreateFile( context, context->Edx, FALSE,
4709 OF_READWRITE | OF_SHARE_COMPAT, 0x12 ))
4710 bSetDOSExtendedError = TRUE;
4712 RESET_CFLAG(context);
4715 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4716 if (!INT21_CreateFile( context, context->Edx, FALSE,
4717 AL_reg(context), 0x01 ))
4718 bSetDOSExtendedError = TRUE;
4720 RESET_CFLAG(context);
4723 case 0x3e: /* "CLOSE" - CLOSE FILE */
4724 TRACE( "CLOSE handle %d\n", BX_reg(context) );
4725 if (_lclose16( BX_reg(context) ) == HFILE_ERROR16)
4726 bSetDOSExtendedError = TRUE;
4728 RESET_CFLAG(context);
4731 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4732 TRACE( "READ from %d to %04lX:%04X for %d bytes\n",
4739 WORD count = CX_reg(context);
4740 BYTE *buffer = CTX_SEG_OFF_TO_LIN( context,
4744 /* Some programs pass a count larger than the allocated buffer */
4745 if (DOSVM_IsWin16())
4747 WORD maxcount = GetSelectorLimit16( context->SegDs )
4748 - DX_reg(context) + 1;
4749 if (count > maxcount)
4754 * FIXME: Reading from console (BX=1) in DOS mode
4755 * does not work as it is supposed to work.
4758 RESET_CFLAG(context); /* set if error */
4759 if (!DOSVM_IsWin16() && BX_reg(context) == 0)
4761 result = INT21_BufferedInput( context, buffer, count );
4762 SET_AX( context, (WORD)result );
4764 else if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context)),
4765 buffer, count, &result, NULL ))
4766 SET_AX( context, (WORD)result );
4768 bSetDOSExtendedError = TRUE;
4772 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4773 TRACE( "WRITE from %04lX:%04X to handle %d for %d byte\n",
4774 context->SegDs, DX_reg(context),
4775 BX_reg(context), CX_reg(context) );
4777 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4779 if (!DOSVM_IsWin16() &&
4780 (BX_reg(context) == 1 || BX_reg(context) == 2))
4783 for(i=0; i<CX_reg(context); i++)
4784 DOSVM_PutChar(ptr[i]);
4785 SET_AX(context, CX_reg(context));
4786 RESET_CFLAG(context);
4790 HFILE handle = (HFILE)DosFileHandleToWin32Handle(BX_reg(context));
4791 LONG result = _hwrite( handle, ptr, CX_reg(context) );
4792 if (result == HFILE_ERROR)
4793 bSetDOSExtendedError = TRUE;
4796 SET_AX( context, (WORD)result );
4797 RESET_CFLAG(context);
4803 case 0x41: /* "UNLINK" - DELETE FILE */
4805 WCHAR fileW[MAX_PATH];
4806 char *fileA = CTX_SEG_OFF_TO_LIN(context,
4810 TRACE( "UNLINK %s\n", fileA );
4811 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
4813 if (!DeleteFileW( fileW ))
4814 bSetDOSExtendedError = TRUE;
4816 RESET_CFLAG(context);
4820 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4821 TRACE( "LSEEK handle %d offset %ld from %s\n",
4823 MAKELONG( DX_reg(context), CX_reg(context) ),
4824 (AL_reg(context) == 0) ?
4825 "start of file" : ((AL_reg(context) == 1) ?
4826 "current file position" : "end of file") );
4828 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4829 LONG offset = MAKELONG( DX_reg(context), CX_reg(context) );
4830 DWORD status = SetFilePointer( handle, offset,
4831 NULL, AL_reg(context) );
4832 if (status == INVALID_SET_FILE_POINTER)
4833 bSetDOSExtendedError = TRUE;
4836 SET_AX( context, LOWORD(status) );
4837 SET_DX( context, HIWORD(status) );
4838 RESET_CFLAG(context);
4843 case 0x43: /* FILE ATTRIBUTES */
4844 if (!INT21_FileAttributes( context, AL_reg(context), FALSE ))
4845 bSetDOSExtendedError = TRUE;
4847 RESET_CFLAG(context);
4850 case 0x44: /* IOCTL */
4851 INT21_Ioctl( context );
4854 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4855 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context) );
4858 HFILE handle16 = HFILE_ERROR;
4860 if (DuplicateHandle( GetCurrentProcess(),
4861 DosFileHandleToWin32Handle(BX_reg(context)),
4862 GetCurrentProcess(),
4864 0, TRUE, DUPLICATE_SAME_ACCESS ))
4865 handle16 = Win32HandleToDosFileHandle(handle32);
4867 if (handle16 == HFILE_ERROR)
4868 bSetDOSExtendedError = TRUE;
4871 SET_AX( context, handle16 );
4872 RESET_CFLAG(context);
4877 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4878 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4879 BX_reg(context), CX_reg(context) );
4880 if (!INT21_Dup2(BX_reg(context), CX_reg(context)))
4881 bSetDOSExtendedError = TRUE;
4883 RESET_CFLAG(context);
4886 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4887 if (!INT21_GetCurrentDirectory( context, FALSE ))
4888 bSetDOSExtendedError = TRUE;
4890 RESET_CFLAG(context);
4893 case 0x48: /* ALLOCATE MEMORY */
4894 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
4897 DWORD bytes = (DWORD)BX_reg(context) << 4;
4899 if (!ISV86(context) && DOSVM_IsWin16())
4901 DWORD rv = GlobalDOSAlloc16( bytes );
4902 selector = LOWORD( rv );
4905 DOSMEM_GetBlock( bytes, &selector );
4909 SET_AX( context, selector );
4910 RESET_CFLAG(context);
4915 SET_AX( context, 0x0008 ); /* insufficient memory */
4916 SET_BX( context, DOSMEM_Available() >> 4 );
4921 case 0x49: /* FREE MEMORY */
4922 TRACE( "FREE MEMORY segment %04lX\n", context->SegEs );
4926 if (!ISV86(context) && DOSVM_IsWin16())
4928 ok = !GlobalDOSFree16( context->SegEs );
4930 /* If we don't reset ES_reg, we will fail in the relay code */
4935 ok = DOSMEM_FreeBlock( (void*)((DWORD)context->SegEs << 4) );
4939 TRACE("FREE MEMORY failed\n");
4941 SET_AX( context, 0x0009 ); /* memory block address invalid */
4946 case 0x4a: /* RESIZE MEMORY BLOCK */
4947 TRACE( "RESIZE MEMORY segment %04lX to %d paragraphs\n",
4948 context->SegEs, BX_reg(context) );
4950 DWORD newsize = (DWORD)BX_reg(context) << 4;
4952 if (!ISV86(context) && DOSVM_IsWin16())
4954 FIXME( "Resize memory block - unsupported under Win16\n" );
4959 LPVOID address = (void*)((DWORD)context->SegEs << 4);
4960 UINT blocksize = DOSMEM_ResizeBlock( address, newsize, FALSE );
4962 RESET_CFLAG(context);
4963 if (blocksize == (UINT)-1)
4965 SET_CFLAG( context );
4966 SET_AX( context, 0x0009 ); /* illegal address */
4968 else if(blocksize != newsize)
4970 SET_CFLAG( context );
4971 SET_AX( context, 0x0008 ); /* insufficient memory */
4972 SET_BX( context, blocksize >> 4 ); /* new block size */
4978 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4980 BYTE *program = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4981 BYTE *paramblk = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx);
4983 TRACE( "EXEC %s\n", program );
4985 RESET_CFLAG(context);
4986 if (DOSVM_IsWin16())
4988 HINSTANCE16 instance = WinExec16( program, SW_NORMAL );
4991 SET_CFLAG( context );
4992 SET_AX( context, instance );
4997 if (!MZ_Exec( context, program, AL_reg(context), paramblk))
4998 bSetDOSExtendedError = TRUE;
5003 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
5004 TRACE( "EXIT with return code %d\n", AL_reg(context) );
5005 if (DOSVM_IsWin16())
5006 ExitThread( AL_reg(context) );
5007 else if(ISV86(context))
5008 MZ_Exit( context, FALSE, AL_reg(context) );
5014 DWORD rv = AL_reg(context);
5015 RaiseException( EXCEPTION_VM86_INTx, 0, 1, &rv );
5019 case 0x4d: /* GET RETURN CODE */
5020 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
5021 SET_AX( context, DOSVM_retval );
5025 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
5026 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
5027 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
5028 if (!INT21_FindFirst(context)) break;
5031 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
5032 TRACE("FINDNEXT\n");
5033 if (!INT21_FindNext(context))
5035 SetLastError( ERROR_NO_MORE_FILES );
5036 SET_AX( context, ERROR_NO_MORE_FILES );
5039 else SET_AX( context, 0 ); /* OK */
5042 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
5043 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
5044 DOSVM_psp = BX_reg(context);
5047 case 0x51: /* GET PSP ADDRESS */
5048 INT21_GetPSP( context );
5051 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
5053 SEGPTR ptr = DOSDEV_GetLOL( ISV86(context) || !DOSVM_IsWin16() );
5054 context->SegEs = SELECTOROF(ptr);
5055 SET_BX( context, OFFSETOF(ptr) );
5059 case 0x54: /* Get Verify Flag */
5060 TRACE("Get Verify Flag - Not Supported\n");
5061 SET_AL( context, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
5064 case 0x56: /* "RENAME" - RENAME FILE */
5065 if (!INT21_RenameFile( context ))
5066 bSetDOSExtendedError = TRUE;
5068 RESET_CFLAG(context);
5071 case 0x57: /* FILE DATE AND TIME */
5072 if (!INT21_FileDateTime( context ))
5073 bSetDOSExtendedError = TRUE;
5075 RESET_CFLAG(context);
5078 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
5079 switch (AL_reg(context))
5081 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
5082 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
5083 SET_AX( context, 0 ); /* low memory first fit */
5086 case 0x01: /* SET ALLOCATION STRATEGY */
5087 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
5091 case 0x02: /* GET UMB LINK STATE */
5092 TRACE( "GET UMB LINK STATE\n" );
5093 SET_AL( context, 0 ); /* UMBs not part of DOS memory chain */
5096 case 0x03: /* SET UMB LINK STATE */
5097 TRACE( "SET UMB LINK STATE to %d - ignored\n",
5102 INT_BARF( context, 0x21 );
5106 case 0x59: /* GET EXTENDED ERROR INFO */
5107 INT21_GetExtendedError( context );
5110 case 0x5a: /* CREATE TEMPORARY FILE */
5111 TRACE("CREATE TEMPORARY FILE\n");
5112 bSetDOSExtendedError = !INT21_CreateTempFile(context);
5115 case 0x5b: /* CREATE NEW FILE */
5116 if (!INT21_CreateFile( context, context->Edx, FALSE,
5117 OF_READWRITE | OF_SHARE_COMPAT, 0x10 ))
5118 bSetDOSExtendedError = TRUE;
5120 RESET_CFLAG(context);
5123 case 0x5c: /* "FLOCK" - RECORD LOCKING */
5125 DWORD offset = MAKELONG(DX_reg(context), CX_reg(context));
5126 DWORD length = MAKELONG(DI_reg(context), SI_reg(context));
5127 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
5129 RESET_CFLAG(context);
5130 switch (AL_reg(context))
5132 case 0x00: /* LOCK */
5133 TRACE( "lock handle %d offset %ld length %ld\n",
5134 BX_reg(context), offset, length );
5135 if (!LockFile( handle, offset, 0, length, 0 ))
5136 bSetDOSExtendedError = TRUE;
5139 case 0x01: /* UNLOCK */
5140 TRACE( "unlock handle %d offset %ld length %ld\n",
5141 BX_reg(context), offset, length );
5142 if (!UnlockFile( handle, offset, 0, length, 0 ))
5143 bSetDOSExtendedError = TRUE;
5147 INT_BARF( context, 0x21 );
5152 case 0x5d: /* NETWORK 5D */
5153 FIXME( "Network function 5D not implemented.\n" );
5154 SetLastError( ER_NoNetwork );
5155 bSetDOSExtendedError = TRUE;
5158 case 0x5e: /* NETWORK 5E */
5159 bSetDOSExtendedError = INT21_NetworkFunc( context);
5162 case 0x5f: /* NETWORK 5F */
5163 /* FIXME: supporting this would need to 1:
5164 * - implement per drive current directory (as kernel32 doesn't)
5165 * - assign enabled/disabled flag on a per drive basis
5167 /* network software not installed */
5168 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
5169 SetLastError( ER_NoNetwork );
5170 bSetDOSExtendedError = TRUE;
5173 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
5175 WCHAR pathW[MAX_PATH], res[MAX_PATH];
5176 /* FIXME: likely to be broken */
5178 TRACE("TRUENAME %s\n",
5179 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
5180 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
5181 if (!GetFullPathNameW( pathW, 128, res, NULL ))
5182 bSetDOSExtendedError = TRUE;
5185 SET_AX( context, 0 );
5186 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
5187 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
5193 case 0x61: /* UNUSED */
5194 SET_AL( context, 0 );
5197 case 0x62: /* GET PSP ADDRESS */
5198 INT21_GetPSP( context );
5201 case 0x63: /* MISC. LANGUAGE SUPPORT */
5202 switch (AL_reg(context)) {
5203 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
5204 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
5205 context->SegDs = INT21_GetHeapSelector( context );
5206 SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
5207 SET_AL( context, 0 ); /* success */
5212 case 0x64: /* OS/2 DOS BOX */
5213 INT_BARF( context, 0x21 );
5217 case 0x65: /* EXTENDED COUNTRY INFORMATION */
5218 INT21_ExtendedCountryInformation( context );
5221 case 0x66: /* GLOBAL CODE PAGE TABLE */
5222 switch (AL_reg(context))
5225 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
5226 SET_BX( context, GetOEMCP() );
5227 SET_DX( context, GetOEMCP() );
5230 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
5231 BX_reg(context), DX_reg(context) );
5236 case 0x67: /* SET HANDLE COUNT */
5237 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
5238 if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
5239 bSetDOSExtendedError = TRUE;
5242 case 0x68: /* "FFLUSH" - COMMIT FILE */
5243 TRACE( "FFLUSH - handle %d\n", BX_reg(context) );
5244 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
5245 bSetDOSExtendedError = TRUE;
5248 case 0x69: /* DISK SERIAL NUMBER */
5249 switch (AL_reg(context))
5252 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
5253 INT21_DriveName(BL_reg(context)));
5254 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
5255 else SET_AX( context, 0 );
5259 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
5260 INT21_DriveName(BL_reg(context)));
5261 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
5262 else SET_AX( context, 1 );
5267 case 0x6a: /* COMMIT FILE */
5268 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context) );
5269 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
5270 bSetDOSExtendedError = TRUE;
5273 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
5274 SET_AL( context, 0 );
5277 case 0x6c: /* EXTENDED OPEN/CREATE */
5278 if (!INT21_CreateFile( context, context->Esi, TRUE,
5279 BX_reg(context), DL_reg(context) ))
5280 bSetDOSExtendedError = TRUE;
5283 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
5284 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
5285 SET_CFLAG( context );
5286 SET_AL( context, 0 );
5289 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
5290 INT21_LongFilename( context );
5293 case 0x73: /* MSDOS7 - FAT32 */
5294 RESET_CFLAG( context );
5295 if (!INT21_Fat32( context ))
5296 bSetDOSExtendedError = TRUE;
5299 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
5300 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
5303 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
5304 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
5307 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
5308 /* we don't implement a DOS32 extender */
5309 TRACE( "DOS32 EXTENDER API - ignored\n" );
5313 INT_BARF( context, 0x21 );
5316 } /* END OF SWITCH */
5318 /* Set general error condition. */
5319 if (bSetDOSExtendedError)
5321 SET_AX( context, GetLastError() );
5322 SET_CFLAG( context );
5325 /* Print error code if carry flag is set. */
5326 if (context->EFlags & 0x0001)
5327 TRACE("failed, error %ld\n", GetLastError() );
5329 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
5330 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
5331 AX_reg(context), BX_reg(context),
5332 CX_reg(context), DX_reg(context),
5333 SI_reg(context), DI_reg(context),
5334 (WORD)context->SegDs, (WORD)context->SegEs,