2 * DOS interrupt 21h handler
4 * Copyright 1993, 1994 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1997 Andreas Mohr
7 * Copyright 1998 Uwe Bonnes
8 * Copyright 1998, 1999 Ove Kaaven
9 * Copyright 2003 Thomas Mertes
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/port.h"
40 #include "wine/winbase16.h"
44 #include "wine/unicode.h"
45 #include "wine/server.h"
46 #include "wine/debug.h"
47 #include "wine/exception.h"
51 * - Most of the file related functions are wrong. NT's kernel32
52 * doesn't maintain a per drive current directory, while DOS does.
53 * We should in fact keep track in here of those per driver
54 * directories, and use this infro while dealing with partial paths
55 * (drive defined, but only relative paths). This could even be
56 * created as an array of CDS (there should be an entry for that in
61 * Forward declarations.
63 static BOOL
INT21_RenameFile( CONTEXT86
*context
);
65 WINE_DEFAULT_DEBUG_CHANNEL(int21
);
71 * Extended Drive Parameter Block.
72 * This structure is compatible with standard DOS4+ DPB and
75 typedef struct _INT21_DPB
{
76 BYTE drive
; /* 00 drive number (0=A, ...) */
77 BYTE unit
; /* 01 unit number within device driver */
78 WORD sector_bytes
; /* 02 bytes per sector */
79 BYTE cluster_sectors
; /* 04 highest sector number within a cluster */
80 BYTE shift
; /* 05 shift count to convert clusters into sectors */
81 WORD num_reserved
; /* 06 reserved sectors at beginning of drive */
82 BYTE num_FAT
; /* 08 number of FATs */
83 WORD num_root_entries
; /* 09 number of root directory entries */
84 WORD first_data_sector
; /* 0b number of first sector containing user data */
85 WORD num_clusters1
; /* 0d highest cluster number (number of data clusters + 1) */
86 WORD sectors_per_FAT
; /* 0f number of sectors per FAT */
87 WORD first_dir_sector
; /* 11 sector number of first directory sector */
88 SEGPTR driver_header
; /* 13 address of device driver header */
89 BYTE media_ID
; /* 17 media ID byte */
90 BYTE access_flag
; /* 18 0x00 if disk accessed, 0xff if not */
91 SEGPTR next
; /* 19 pointer to next DPB */
92 WORD search_cluster1
; /* 1d cluster at which to start search for free space */
93 WORD free_clusters_lo
; /* 1f number of free clusters on drive or 0xffff if unknown */
94 WORD free_clusters_hi
; /* 21 hiword of clusters_free */
95 WORD mirroring_flags
; /* 23 active FAT/mirroring flags */
96 WORD info_sector
; /* 25 sector number of file system info sector or 0xffff for none */
97 WORD spare_boot_sector
; /* 27 sector number of backup boot sector or 0xffff for none */
98 DWORD first_cluster_sector
; /* 29 sector number of the first cluster */
99 DWORD num_clusters2
; /* 2d maximum cluster number */
100 DWORD fat_clusters
; /* 31 number of clusters occupied by FAT */
101 DWORD root_cluster
; /* 35 cluster number of start of root directory */
102 DWORD search_cluster2
; /* 39 cluster at which to start searching for free space */
107 * Structure for DOS data that can be accessed directly from applications.
108 * Real and protected mode pointers will be returned to this structure so
109 * the structure must be correctly packed.
111 typedef struct _INT21_HEAP
{
112 WORD uppercase_size
; /* Size of the following table in bytes */
113 BYTE uppercase_table
[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
115 WORD lowercase_size
; /* Size of the following table in bytes */
116 BYTE lowercase_table
[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
118 WORD collating_size
; /* Size of the following table in bytes */
119 BYTE collating_table
[256]; /* Values used to sort characters from 0x00 to 0xff. */
121 WORD filename_size
; /* Size of the following filename data in bytes */
122 BYTE filename_reserved1
; /* 0x01 for MS-DOS 3.30-6.00 */
123 BYTE filename_lowest
; /* Lowest permissible character value for filename */
124 BYTE filename_highest
; /* Highest permissible character value for filename */
125 BYTE filename_reserved2
; /* 0x00 for MS-DOS 3.30-6.00 */
126 BYTE filename_exclude_first
; /* First illegal character in permissible range */
127 BYTE filename_exclude_last
; /* Last illegal character in permissible range */
128 BYTE filename_reserved3
; /* 0x02 for MS-DOS 3.30-6.00 */
129 BYTE filename_illegal_size
; /* Number of terminators in the following table */
130 BYTE filename_illegal_table
[16]; /* Characters which terminate a filename */
132 WORD dbcs_size
; /* Number of valid ranges in the following table */
133 BYTE dbcs_table
[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
135 BYTE misc_indos
; /* Interrupt 21 nesting flag */
136 WORD misc_segment
; /* Real mode segment for INT21_HEAP */
137 WORD misc_selector
; /* Protected mode selector for INT21_HEAP */
138 INT21_DPB misc_dpb_list
[MAX_DOS_DRIVES
]; /* Drive parameter blocks for all drives */
146 CHAR file_extension
[3];
147 WORD current_block_number
;
148 WORD logical_record_size
;
150 WORD date_of_last_write
;
151 WORD time_of_last_write
;
154 WORD starting_cluster
;
155 WORD sequence_number
;
156 BYTE file_attributes
;
158 BYTE record_within_current_block
;
159 BYTE random_access_record_number
[4];
166 BYTE xfcb_file_attribute
;
170 /* DTA layout for FindFirst/FindNext */
173 BYTE drive
; /* 00 drive letter */
174 char mask
[11]; /* 01 search template */
175 BYTE search_attr
; /* 0c search attributes */
176 WORD count
; /* 0d entry count within directory */
177 WORD cluster
; /* 0f cluster of parent directory */
178 WCHAR
*fullPath
; /* 11 full path (was: reserved) */
179 BYTE fileattr
; /* 15 file attributes */
180 WORD filetime
; /* 16 file time */
181 WORD filedate
; /* 18 file date */
182 DWORD filesize
; /* 1a file size */
183 char filename
[13]; /* 1e file name + extension */
186 /* FCB layout for FindFirstFCB/FindNextFCB */
189 BYTE drive
; /* 00 drive letter */
190 char filename
[11]; /* 01 filename 8+3 format */
191 int count
; /* 0c entry count (was: reserved) */
192 WCHAR
*fullPath
; /* 10 full path (was: reserved) */
195 /* DOS directory entry for FindFirstFCB/FindNextFCB */
198 char filename
[11]; /* 00 filename 8+3 format */
199 BYTE fileattr
; /* 0b file attributes */
200 BYTE reserved
[10]; /* 0c reserved */
201 WORD filetime
; /* 16 file time */
202 WORD filedate
; /* 18 file date */
203 WORD cluster
; /* 1a file first cluster */
204 DWORD filesize
; /* 1c file size */
205 } DOS_DIRENTRY_LAYOUT
;
209 /* dos file attributes */
210 #define FA_NORMAL 0x00 /* Normal file, no attributes */
211 #define FA_RDONLY 0x01 /* Read only attribute */
212 #define FA_HIDDEN 0x02 /* Hidden file */
213 #define FA_SYSTEM 0x04 /* System file */
214 #define FA_LABEL 0x08 /* Volume label */
215 #define FA_DIRECTORY 0x10 /* Directory */
216 #define FA_ARCHIVE 0x20 /* Archive */
217 #define FA_UNUSED 0x40 /* Unused */
220 #define ER_NoNetwork 0x49
223 #define EC_OutOfResource 0x01
224 #define EC_Temporary 0x02
225 #define EC_AccessDenied 0x03
226 #define EC_InternalError 0x04
227 #define EC_HardwareFailure 0x05
228 #define EC_SystemFailure 0x06
229 #define EC_ProgramError 0x07
230 #define EC_NotFound 0x08
231 #define EC_MediaError 0x0b
232 #define EC_Exists 0x0c
233 #define EC_Unknown 0x0d
235 /* Suggested actions */
236 #define SA_Retry 0x01
237 #define SA_DelayedRetry 0x02
238 #define SA_Abort 0x04
239 #define SA_Ignore 0x06
240 #define SA_Ask4Retry 0x07
243 #define EL_Unknown 0x01
245 #define EL_Network 0x03
246 #define EL_Serial 0x04
247 #define EL_Memory 0x05
256 void (*ioctl_handler
)(CONTEXT86
*);
259 static void INT21_IoctlScsiMgrHandler( CONTEXT86
* );
260 static void INT21_IoctlEMSHandler( CONTEXT86
* );
261 static void INT21_IoctlHPScanHandler( CONTEXT86
* );
263 static struct magic_device magic_devices
[] =
265 { {'s','c','s','i','m','g','r','$',0}, NULL
, 0, 0, INT21_IoctlScsiMgrHandler
},
266 { {'e','m','m','x','x','x','x','0',0}, NULL
, 0, 0, INT21_IoctlEMSHandler
},
267 { {'h','p','s','c','a','n',0}, NULL
, 0, 0, INT21_IoctlHPScanHandler
},
270 #define NB_MAGIC_DEVICES (sizeof(magic_devices)/sizeof(magic_devices[0]))
273 /* Many calls translate a drive argument like this:
274 drive number (00h = default, 01h = A:, etc)
276 /******************************************************************
279 * Many calls translate a drive argument like this:
280 * drive number (00h = default, 01h = A:, etc)
282 static const char *INT21_DriveName(int drive
)
286 if (drive
<= 26) return wine_dbg_sprintf("%c:", 'A' + drive
- 1);
287 else return wine_dbg_sprintf( "<Bad drive: %d>", drive
);
292 /***********************************************************************
293 * INT21_GetCurrentDrive
295 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
296 * MAX_DOS_DRIVES on error.
298 static BYTE
INT21_GetCurrentDrive(void)
300 WCHAR current_directory
[MAX_PATH
];
302 if (!GetCurrentDirectoryW( MAX_PATH
, current_directory
) ||
303 current_directory
[1] != ':')
305 TRACE( "Failed to get current drive.\n" );
306 return MAX_DOS_DRIVES
;
309 return toupperW( current_directory
[0] ) - 'A';
313 /***********************************************************************
316 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
317 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
319 static BYTE
INT21_MapDrive( BYTE drive
)
323 WCHAR drivespec
[3] = {'A', ':', 0};
326 drivespec
[0] += drive
- 1;
327 drivetype
= GetDriveTypeW( drivespec
);
329 if (drivetype
== DRIVE_UNKNOWN
|| drivetype
== DRIVE_NO_ROOT_DIR
)
330 return MAX_DOS_DRIVES
;
335 return INT21_GetCurrentDrive();
339 /***********************************************************************
340 * INT21_SetCurrentDrive
342 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
344 static void INT21_SetCurrentDrive( BYTE drive
)
346 WCHAR drivespec
[3] = {'A', ':', 0};
348 drivespec
[0] += drive
;
350 if (!SetCurrentDirectoryW( drivespec
))
351 TRACE( "Failed to set current drive.\n" );
355 /***********************************************************************
358 * Reads a character from the standard input.
359 * Extended keycodes will be returned as two separate characters.
361 static BOOL
INT21_ReadChar( BYTE
*input
, CONTEXT86
*waitctx
)
363 static BYTE pending_scan
= 0;
368 *input
= pending_scan
;
377 if (!DOSVM_Int16ReadChar( &ascii
, &scan
, waitctx
))
382 if (waitctx
&& !ascii
)
389 /***********************************************************************
390 * INT21_GetSystemCountryCode
392 * Return DOS country code for default system locale.
394 static WORD
INT21_GetSystemCountryCode( void )
397 * FIXME: Determine country code. We should probably use
398 * DOSCONF structure for that.
400 return GetSystemDefaultLangID();
404 /***********************************************************************
405 * INT21_FillCountryInformation
407 * Fill 34-byte buffer with country information data using
408 * default system locale.
410 static void INT21_FillCountryInformation( BYTE
*buffer
)
412 /* 00 - WORD: date format
417 *(WORD
*)(buffer
+ 0) = 0; /* FIXME: Get from locale */
419 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
420 buffer
[2] = '$'; /* FIXME: Get from locale */
423 /* 07 - BYTE[2]: ASCIIZ thousands separator */
424 buffer
[7] = 0; /* FIXME: Get from locale */
427 /* 09 - BYTE[2]: ASCIIZ decimal separator */
428 buffer
[9] = '.'; /* FIXME: Get from locale */
431 /* 11 - BYTE[2]: ASCIIZ date separator */
432 buffer
[11] = '/'; /* FIXME: Get from locale */
435 /* 13 - BYTE[2]: ASCIIZ time separator */
436 buffer
[13] = ':'; /* FIXME: Get from locale */
439 /* 15 - BYTE: Currency format
440 * bit 2 = set if currency symbol replaces decimal point
441 * bit 1 = number of spaces between value and currency symbol
442 * bit 0 = 0 if currency symbol precedes value
443 * 1 if currency symbol follows value
445 buffer
[15] = 0; /* FIXME: Get from locale */
447 /* 16 - BYTE: Number of digits after decimal in currency */
448 buffer
[16] = 0; /* FIXME: Get from locale */
450 /* 17 - BYTE: Time format
451 * bit 0 = 0 if 12-hour clock
454 buffer
[17] = 1; /* FIXME: Get from locale */
456 /* 18 - DWORD: Address of case map routine */
457 *(DWORD
*)(buffer
+ 18) = 0; /* FIXME: ptr to case map routine */
459 /* 22 - BYTE[2]: ASCIIZ data-list separator */
460 buffer
[22] = ','; /* FIXME: Get from locale */
463 /* 24 - BYTE[10]: Reserved */
464 memset( buffer
+ 24, 0, 10 );
468 /***********************************************************************
471 * Initialize DOS heap.
473 static void INT21_FillHeap( INT21_HEAP
*heap
)
475 static const char terminators
[] = "\"\\./[]:|<>+=;,";
481 heap
->uppercase_size
= 128;
482 for (i
= 0; i
< 128; i
++)
483 heap
->uppercase_table
[i
] = toupper( 128 + i
);
488 heap
->lowercase_size
= 256;
489 for (i
= 0; i
< 256; i
++)
490 heap
->lowercase_table
[i
] = tolower( i
);
495 heap
->collating_size
= 256;
496 for (i
= 0; i
< 256; i
++)
497 heap
->collating_table
[i
] = i
;
502 heap
->filename_size
= 8 + strlen(terminators
);
503 heap
->filename_illegal_size
= strlen(terminators
);
504 strcpy( heap
->filename_illegal_table
, terminators
);
506 heap
->filename_reserved1
= 0x01;
507 heap
->filename_lowest
= 0; /* FIXME: correct value? */
508 heap
->filename_highest
= 0xff; /* FIXME: correct value? */
509 heap
->filename_reserved2
= 0x00;
510 heap
->filename_exclude_first
= 0x00; /* FIXME: correct value? */
511 heap
->filename_exclude_last
= 0x00; /* FIXME: correct value? */
512 heap
->filename_reserved3
= 0x02;
515 * DBCS lead byte table. This table is empty.
518 memset( heap
->dbcs_table
, 0, sizeof(heap
->dbcs_table
) );
521 * Initialize InDos flag.
523 heap
->misc_indos
= 0;
526 * FIXME: Should drive parameter blocks (DPB) be
527 * initialized here and linked to DOS LOL?
532 /***********************************************************************
533 * INT21_GetHeapPointer
535 * Get pointer for DOS heap (INT21_HEAP).
536 * Creates and initializes heap on first call.
538 static INT21_HEAP
*INT21_GetHeapPointer( void )
540 static INT21_HEAP
*heap_pointer
= NULL
;
547 heap_pointer
= DOSVM_AllocDataUMB( sizeof(INT21_HEAP
),
551 heap_pointer
->misc_segment
= heap_segment
;
552 heap_pointer
->misc_selector
= heap_selector
;
553 INT21_FillHeap( heap_pointer
);
560 /***********************************************************************
561 * INT21_GetHeapSelector
563 * Get segment/selector for DOS heap (INT21_HEAP).
564 * Creates and initializes heap on first call.
566 static WORD
INT21_GetHeapSelector( CONTEXT86
*context
)
568 INT21_HEAP
*heap
= INT21_GetHeapPointer();
570 if (!ISV86(context
) && DOSVM_IsWin16())
571 return heap
->misc_selector
;
573 return heap
->misc_segment
;
577 /***********************************************************************
580 * Fill DOS heap drive parameter block for the specified drive.
581 * Return TRUE if drive was valid and there were
582 * no errors while reading drive information.
584 static BOOL
INT21_FillDrivePB( BYTE drive
)
586 WCHAR drivespec
[3] = {'A', ':', 0};
587 INT21_HEAP
*heap
= INT21_GetHeapPointer();
590 DWORD cluster_sectors
;
593 DWORD total_clusters
;
595 if (drive
>= MAX_DOS_DRIVES
)
598 dpb
= &heap
->misc_dpb_list
[drive
];
599 drivespec
[0] += drive
;
600 drivetype
= GetDriveTypeW( drivespec
);
603 * FIXME: Does this check work correctly with floppy/cdrom drives?
605 if (drivetype
== DRIVE_NO_ROOT_DIR
|| drivetype
== DRIVE_UNKNOWN
)
609 * FIXME: Does this check work correctly with floppy/cdrom drives?
611 if (!GetDiskFreeSpaceW( drivespec
, &cluster_sectors
, §or_bytes
,
612 &free_clusters
, &total_clusters
))
616 * FIXME: Most of the values listed below are incorrect.
617 * All values should be validated.
622 dpb
->sector_bytes
= sector_bytes
;
623 dpb
->cluster_sectors
= cluster_sectors
- 1;
626 while (cluster_sectors
> 1)
628 cluster_sectors
/= 2;
632 dpb
->num_reserved
= 0;
634 dpb
->num_root_entries
= 2;
635 dpb
->first_data_sector
= 2;
636 dpb
->num_clusters1
= total_clusters
;
637 dpb
->sectors_per_FAT
= 1;
638 dpb
->first_dir_sector
= 1;
639 dpb
->driver_header
= 0;
640 dpb
->media_ID
= (drivetype
== DRIVE_FIXED
) ? 0xF8 : 0xF0;
641 dpb
->access_flag
= 0;
643 dpb
->search_cluster1
= 0;
644 dpb
->free_clusters_lo
= LOWORD(free_clusters
);
645 dpb
->free_clusters_hi
= HIWORD(free_clusters
);
646 dpb
->mirroring_flags
= 0;
647 dpb
->info_sector
= 0xffff;
648 dpb
->spare_boot_sector
= 0xffff;
649 dpb
->first_cluster_sector
= 0;
650 dpb
->num_clusters2
= total_clusters
;
651 dpb
->fat_clusters
= 32;
652 dpb
->root_cluster
= 0;
653 dpb
->search_cluster2
= 0;
659 /***********************************************************************
660 * INT21_GetCurrentDirectory
664 * - subfunction 0x47 of function 0x71
666 static BOOL
INT21_GetCurrentDirectory( CONTEXT86
*context
, BOOL islong
)
668 char *buffer
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
);
669 BYTE new_drive
= INT21_MapDrive( DL_reg(context
) );
670 BYTE old_drive
= INT21_GetCurrentDrive();
671 WCHAR pathW
[MAX_PATH
];
672 char pathA
[MAX_PATH
];
675 TRACE( "drive %d\n", DL_reg(context
) );
677 if (new_drive
== MAX_DOS_DRIVES
)
679 SetLastError(ERROR_INVALID_DRIVE
);
684 * Grab current directory.
687 INT21_SetCurrentDrive( new_drive
);
688 if (!GetCurrentDirectoryW( MAX_PATH
, pathW
))
690 INT21_SetCurrentDrive( old_drive
);
693 INT21_SetCurrentDrive( old_drive
);
696 * Convert into short format.
701 DWORD result
= GetShortPathNameW( pathW
, pathW
, MAX_PATH
);
704 if (result
> MAX_PATH
)
706 WARN( "Short path too long!\n" );
707 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
713 * The returned pathname does not include
714 * the drive letter, colon or leading backslash.
720 * FIXME: We should probably just strip host part from name...
722 FIXME( "UNC names are not supported.\n" );
723 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
726 else if (!ptr
[0] || ptr
[1] != ':' || ptr
[2] != '\\')
728 WARN( "Path is neither UNC nor DOS path: %s\n",
729 wine_dbgstr_w(ptr
) );
730 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
735 /* Remove drive letter, colon and leading backslash. */
740 * Convert into OEM string.
743 if (!WideCharToMultiByte(CP_OEMCP
, 0, ptr
, -1, pathA
,
744 MAX_PATH
, NULL
, NULL
))
746 WARN( "Cannot convert path!\n" );
747 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
757 /* Undocumented success code. */
758 SET_AX( context
, 0x0100 );
760 /* Truncate buffer to 64 bytes. */
764 TRACE( "%c:=%s\n", 'A' + new_drive
, pathA
);
766 strcpy( buffer
, pathA
);
771 /***********************************************************************
772 * INT21_SetCurrentDirectory
776 * - subfunction 0x3b of function 0x71
778 static BOOL
INT21_SetCurrentDirectory( CONTEXT86
*context
)
780 WCHAR dirW
[MAX_PATH
];
781 char *dirA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
782 BYTE drive
= INT21_GetCurrentDrive();
785 TRACE( "SET CURRENT DIRECTORY %s\n", dirA
);
787 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
788 result
= SetCurrentDirectoryW( dirW
);
790 /* This function must not change current drive. */
791 INT21_SetCurrentDrive( drive
);
796 /***********************************************************************
797 * INT21_CreateMagicDeviceHandle
799 * Create a dummy file handle for a "magic" device.
801 static HANDLE
INT21_CreateMagicDeviceHandle( LPCWSTR name
)
803 const char *dir
= wine_get_server_dir();
807 OBJECT_ATTRIBUTES attr
;
808 UNICODE_STRING nameW
;
811 len
= MultiByteToWideChar( CP_UNIXCP
, 0, dir
, -1, NULL
, 0 );
812 nameW
.Length
= (len
+ 1 + strlenW( name
)) * sizeof(WCHAR
);
813 nameW
.MaximumLength
= nameW
.Length
+ sizeof(WCHAR
);
814 if (!(nameW
.Buffer
= HeapAlloc( GetProcessHeap(), 0, nameW
.Length
)))
816 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
819 MultiByteToWideChar( CP_UNIXCP
, 0, dir
, -1, nameW
.Buffer
, len
);
820 nameW
.Buffer
[len
-1] = '/';
821 strcpyW( nameW
.Buffer
+ len
, name
);
823 attr
.Length
= sizeof(attr
);
824 attr
.RootDirectory
= 0;
826 attr
.ObjectName
= &nameW
;
827 attr
.SecurityDescriptor
= NULL
;
828 attr
.SecurityQualityOfService
= NULL
;
830 status
= NtCreateFile( &ret
, GENERIC_READ
|GENERIC_WRITE
, &attr
, &io
, NULL
, 0,
831 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN_IF
,
832 FILE_SYNCHRONOUS_IO_ALERT
, NULL
, 0 );
836 SetLastError( RtlNtStatusToDosError(status
) );
838 RtlFreeUnicodeString( &nameW
);
843 /***********************************************************************
844 * INT21_OpenMagicDevice
846 * Open a file handle for "magic" devices like EMMXXXX0.
848 static HANDLE
INT21_OpenMagicDevice( LPCWSTR name
, DWORD access
)
854 if (name
[0] && (name
[1] == ':')) name
+= 2;
855 if ((p
= strrchrW( name
, '/' ))) name
= p
+ 1;
856 if ((p
= strrchrW( name
, '\\' ))) name
= p
+ 1;
858 for (i
= 0; i
< NB_MAGIC_DEVICES
; i
++)
860 int len
= strlenW( magic_devices
[i
].name
);
861 if (!strncmpiW( magic_devices
[i
].name
, name
, len
) &&
862 (!name
[len
] || name
[len
] == '.' || name
[len
] == ':')) break;
864 if (i
== NB_MAGIC_DEVICES
) return 0;
866 if (!magic_devices
[i
].handle
) /* need to open it */
871 if (!(handle
= INT21_CreateMagicDeviceHandle( magic_devices
[i
].name
))) return 0;
872 wine_server_handle_to_fd( handle
, 0, &fd
, NULL
, NULL
);
874 wine_server_release_fd( handle
, fd
);
875 magic_devices
[i
].dev
= st
.st_dev
;
876 magic_devices
[i
].ino
= st
.st_ino
;
877 magic_devices
[i
].handle
= handle
;
879 if (!DuplicateHandle( GetCurrentProcess(), magic_devices
[i
].handle
,
880 GetCurrentProcess(), &handle
, access
, FALSE
, 0 )) handle
= 0;
885 /***********************************************************************
893 * - subfunction 0x6c of function 0x71
895 static BOOL
INT21_CreateFile( CONTEXT86
*context
,
902 char *pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, pathSegOff
);
903 WCHAR pathW
[MAX_PATH
];
910 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
911 "create flags=%04x, file=%s.\n",
912 AH_reg(context
), dosAction
, dosAccessShare
, CX_reg(context
), pathA
);
915 * Application tried to create/open a file whose name
916 * ends with a backslash. This is not allowed.
918 * FIXME: This needs to be validated, especially the return value.
920 if (pathA
[strlen(pathA
) - 1] == '/')
922 SetLastError( ERROR_FILE_NOT_FOUND
);
927 * Convert DOS action flags into Win32 creation disposition parameter.
932 winMode
= OPEN_EXISTING
;
935 winMode
= TRUNCATE_EXISTING
;
938 winMode
= CREATE_NEW
;
941 winMode
= OPEN_ALWAYS
;
944 winMode
= CREATE_ALWAYS
;
947 SetLastError( ERROR_INVALID_PARAMETER
);
952 * Convert DOS access/share flags into Win32 desired access parameter.
954 switch(dosAccessShare
& 0x07)
957 winAccess
= GENERIC_READ
;
960 winAccess
= GENERIC_WRITE
;
963 winAccess
= GENERIC_READ
| GENERIC_WRITE
;
967 * Read-only, do not modify file's last-access time (DOS7).
969 * FIXME: How to prevent modification of last-access time?
971 winAccess
= GENERIC_READ
;
978 * Convert DOS access/share flags into Win32 share mode parameter.
980 switch(dosAccessShare
& 0x70)
982 case OF_SHARE_EXCLUSIVE
:
985 case OF_SHARE_DENY_WRITE
:
986 winSharing
= FILE_SHARE_READ
;
988 case OF_SHARE_DENY_READ
:
989 winSharing
= FILE_SHARE_WRITE
;
991 case OF_SHARE_DENY_NONE
:
992 case OF_SHARE_COMPAT
:
994 winSharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
998 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
999 * What to do with this bit?
1000 * FIXME: Bits in the high byte of dosAccessShare are not supported.
1001 * See both function 0x6c and subfunction 0x6c of function 0x71 for
1002 * definition of these bits.
1006 * Convert DOS create attributes into Win32 flags and attributes parameter.
1008 if (winMode
== OPEN_EXISTING
|| winMode
== TRUNCATE_EXISTING
)
1014 WORD dosAttributes
= CX_reg(context
);
1016 if (dosAttributes
& FILE_ATTRIBUTE_LABEL
)
1019 * Application tried to create volume label entry.
1020 * This is difficult to support so we do not allow it.
1022 * FIXME: If volume does not already have a label,
1023 * this function is supposed to succeed.
1025 SetLastError( ERROR_ACCESS_DENIED
);
1029 winAttributes
= dosAttributes
&
1030 (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
|
1031 FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_ARCHIVE
);
1037 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
1039 if ((winHandle
= INT21_OpenMagicDevice( pathW
, winAccess
)))
1045 winHandle
= CreateFileW( pathW
, winAccess
, winSharing
, NULL
,
1046 winMode
, winAttributes
, 0 );
1048 if (winHandle
== INVALID_HANDLE_VALUE
)
1052 * Determine DOS file status.
1063 case TRUNCATE_EXISTING
:
1070 dosStatus
= (GetLastError() == ERROR_ALREADY_EXISTS
) ? 1 : 2;
1073 dosStatus
= (GetLastError() == ERROR_ALREADY_EXISTS
) ? 3 : 2;
1081 * Return DOS file handle and DOS status.
1083 SET_AX( context
, Win32HandleToDosFileHandle(winHandle
) );
1086 SET_CX( context
, dosStatus
);
1088 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
1089 AX_reg(context
), dosStatus
);
1095 /***********************************************************************
1096 * INT21_BufferedInput
1098 * Handler for function 0x0a and reading from console using
1101 * Reads a string of characters from standard input until
1102 * enter key is pressed. Returns either number of characters
1103 * read from console including terminating CR or
1104 * zero if capacity was zero.
1106 static WORD
INT21_BufferedInput( CONTEXT86
*context
, BYTE
*ptr
, WORD capacity
)
1111 * Return immediately if capacity is zero.
1121 DOSVM_Int16ReadChar( &ascii
, &scan
, context
);
1123 if (ascii
== '\r' || ascii
== '\n')
1126 * FIXME: What should be echoed here?
1128 DOSVM_PutChar( '\r' );
1129 DOSVM_PutChar( '\n' );
1135 * FIXME: This function is supposed to support
1136 * DOS editing keys...
1140 * If the buffer becomes filled to within one byte of
1141 * capacity, DOS rejects all further characters up to,
1142 * but not including, the terminating carriage return.
1144 if (ascii
!= 0 && length
< capacity
-1)
1146 DOSVM_PutChar( ascii
);
1147 ptr
[length
] = ascii
;
1154 /***********************************************************************
1155 * INT21_GetCurrentDTA
1157 static BYTE
*INT21_GetCurrentDTA( CONTEXT86
*context
)
1159 TDB
*pTask
= GlobalLock16(GetCurrentTask());
1161 /* FIXME: This assumes DTA was set correctly! */
1162 return (BYTE
*)CTX_SEG_OFF_TO_LIN( context
, SELECTOROF(pTask
->dta
),
1163 (DWORD
)OFFSETOF(pTask
->dta
) );
1167 /***********************************************************************
1168 * INT21_OpenFileUsingFCB
1170 * Handler for function 0x0f.
1173 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
1180 * Opens a FCB file for read/write in compatibility mode. Upon calling
1181 * the FCB must have the drive_number, file_name, and file_extension
1182 * fields filled and all other bytes cleared.
1184 static void INT21_OpenFileUsingFCB( CONTEXT86
*context
)
1192 BY_HANDLE_FILE_INFORMATION info
;
1195 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1196 if (fcb
->drive_number
== 0xff) {
1197 xfcb
= (struct XFCB
*) fcb
;
1198 fcb
= (struct FCB
*) xfcb
->fcb
;
1202 file_path
[0] = 'A' + INT21_MapDrive( fcb
->drive_number
);
1204 if (AL_result
== 0) {
1206 pos
= &file_path
[2];
1207 memcpy(pos
, fcb
->file_name
, 8);
1210 pos
= strchr(pos
, ' ');
1213 memcpy(pos
, fcb
->file_extension
, 3);
1216 pos
= strchr(pos
, ' ');
1219 handle
= (HANDLE
) _lopen(file_path
, OF_READWRITE
);
1220 if (handle
== INVALID_HANDLE_VALUE
) {
1221 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path
);
1222 AL_result
= 0xff; /* failed */
1224 hfile16
= Win32HandleToDosFileHandle(handle
);
1225 if (hfile16
== HFILE_ERROR16
) {
1226 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle
);
1227 CloseHandle(handle
);
1228 AL_result
= 0xff; /* failed */
1229 } else if (hfile16
> 255) {
1230 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16
, file_path
);
1232 AL_result
= 0xff; /* failed */
1234 if (!GetFileInformationByHandle(handle
, &info
)) {
1235 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1236 hfile16
, handle
, file_path
);
1238 AL_result
= 0xff; /* failed */
1240 fcb
->drive_number
= file_path
[0] - 'A' + 1;
1241 fcb
->current_block_number
= 0;
1242 fcb
->logical_record_size
= 128;
1243 fcb
->file_size
= info
.nFileSizeLow
;
1244 FileTimeToDosDateTime(&info
.ftLastWriteTime
,
1245 &fcb
->date_of_last_write
, &fcb
->time_of_last_write
);
1246 fcb
->file_number
= hfile16
;
1247 fcb
->attributes
= 0xc2;
1248 fcb
->starting_cluster
= 0; /* don't know correct init value */
1249 fcb
->sequence_number
= 0; /* don't know correct init value */
1250 fcb
->file_attributes
= info
.dwFileAttributes
;
1251 /* The following fields are not initialized */
1252 /* by the native function: */
1254 /* record_within_current_block */
1255 /* random_access_record_number */
1257 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1258 file_path
, hfile16
, handle
);
1259 AL_result
= 0x00; /* successful */
1264 SET_AL(context
, AL_result
);
1268 /***********************************************************************
1269 * INT21_CloseFileUsingFCB
1271 * Handler for function 0x10.
1274 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1281 * Closes a FCB file.
1283 static void INT21_CloseFileUsingFCB( CONTEXT86
*context
)
1289 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1290 if (fcb
->drive_number
== 0xff) {
1291 xfcb
= (struct XFCB
*) fcb
;
1292 fcb
= (struct FCB
*) xfcb
->fcb
;
1295 if (_lclose16((HFILE16
) fcb
->file_number
) != 0) {
1296 TRACE("_lclose16(%d) failed\n", fcb
->file_number
);
1297 AL_result
= 0xff; /* failed */
1299 TRACE("successful closed file %d\n", fcb
->file_number
);
1300 AL_result
= 0x00; /* successful */
1302 SET_AL(context
, AL_result
);
1306 /***********************************************************************
1307 * INT21_SequentialReadFromFCB
1309 * Handler for function 0x14.
1312 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1316 * 1: end of file, no data read
1317 * 2: segment wrap in DTA, no data read (not returned now)
1318 * 3: end of file, partial record read
1321 * Reads a record with the size FCB->logical_record_size from the FCB
1322 * to the disk transfer area. The position of the record is specified
1323 * with FCB->current_block_number and FCB->record_within_current_block.
1324 * Then FCB->current_block_number and FCB->record_within_current_block
1325 * are updated to point to the next record. If a partial record is
1326 * read, it is filled with zeros up to the FCB->logical_record_size.
1328 static void INT21_SequentialReadFromFCB( CONTEXT86
*context
)
1333 DWORD record_number
;
1335 BYTE
*disk_transfer_area
;
1339 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1340 if (fcb
->drive_number
== 0xff) {
1341 xfcb
= (struct XFCB
*) fcb
;
1342 fcb
= (struct FCB
*) xfcb
->fcb
;
1345 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1346 if (handle
== INVALID_HANDLE_VALUE
) {
1347 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1349 AL_result
= 0x01; /* end of file, no data read */
1351 record_number
= 128 * fcb
->current_block_number
+ fcb
->record_within_current_block
;
1352 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1353 if (position
!= record_number
* fcb
->logical_record_size
) {
1354 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1355 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1356 AL_result
= 0x01; /* end of file, no data read */
1358 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1359 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, fcb
->logical_record_size
);
1360 if (bytes_read
!= fcb
->logical_record_size
) {
1361 TRACE("_lread(%d, %p, %d) failed with %d\n",
1362 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_read
);
1363 if (bytes_read
== 0) {
1364 AL_result
= 0x01; /* end of file, no data read */
1366 memset(&disk_transfer_area
[bytes_read
], 0, fcb
->logical_record_size
- bytes_read
);
1367 AL_result
= 0x03; /* end of file, partial record read */
1370 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1371 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1372 AL_result
= 0x00; /* successful */
1376 if (AL_result
== 0x00 || AL_result
== 0x03) {
1377 if (fcb
->record_within_current_block
== 127) {
1378 fcb
->record_within_current_block
= 0;
1379 fcb
->current_block_number
++;
1381 fcb
->record_within_current_block
++;
1384 SET_AL(context
, AL_result
);
1388 /***********************************************************************
1389 * INT21_SequentialWriteToFCB
1391 * Handler for function 0x15.
1394 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1399 * 2: segment wrap in DTA (not returned now)
1402 * Writes a record with the size FCB->logical_record_size from the disk
1403 * transfer area to the FCB. The position of the record is specified
1404 * with FCB->current_block_number and FCB->record_within_current_block.
1405 * Then FCB->current_block_number and FCB->record_within_current_block
1406 * are updated to point to the next record.
1408 static void INT21_SequentialWriteToFCB( CONTEXT86
*context
)
1413 DWORD record_number
;
1415 BYTE
*disk_transfer_area
;
1419 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1420 if (fcb
->drive_number
== 0xff) {
1421 xfcb
= (struct XFCB
*) fcb
;
1422 fcb
= (struct FCB
*) xfcb
->fcb
;
1425 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1426 if (handle
== INVALID_HANDLE_VALUE
) {
1427 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1429 AL_result
= 0x01; /* disk full */
1431 record_number
= 128 * fcb
->current_block_number
+ fcb
->record_within_current_block
;
1432 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1433 if (position
!= record_number
* fcb
->logical_record_size
) {
1434 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1435 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1436 AL_result
= 0x01; /* disk full */
1438 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1439 bytes_written
= _lwrite((HFILE
) handle
, disk_transfer_area
, fcb
->logical_record_size
);
1440 if (bytes_written
!= fcb
->logical_record_size
) {
1441 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1442 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_written
);
1443 AL_result
= 0x01; /* disk full */
1445 TRACE("successful written %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1446 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1447 AL_result
= 0x00; /* successful */
1451 if (AL_result
== 0x00) {
1452 if (fcb
->record_within_current_block
== 127) {
1453 fcb
->record_within_current_block
= 0;
1454 fcb
->current_block_number
++;
1456 fcb
->record_within_current_block
++;
1459 SET_AL(context
, AL_result
);
1463 /***********************************************************************
1464 * INT21_ReadRandomRecordFromFCB
1466 * Handler for function 0x21.
1469 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1473 * 1: end of file, no data read
1474 * 2: segment wrap in DTA, no data read (not returned now)
1475 * 3: end of file, partial record read
1478 * Reads a record with the size FCB->logical_record_size from
1479 * the FCB to the disk transfer area. The position of the record
1480 * is specified with FCB->random_access_record_number. The
1481 * FCB->random_access_record_number is not updated. If a partial record
1482 * is read, it is filled with zeros up to the FCB->logical_record_size.
1484 static void INT21_ReadRandomRecordFromFCB( CONTEXT86
*context
)
1489 DWORD record_number
;
1491 BYTE
*disk_transfer_area
;
1495 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1496 if (fcb
->drive_number
== 0xff) {
1497 xfcb
= (struct XFCB
*) fcb
;
1498 fcb
= (struct FCB
*) xfcb
->fcb
;
1501 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1502 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1503 if (handle
== INVALID_HANDLE_VALUE
) {
1504 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1506 AL_result
= 0x01; /* end of file, no data read */
1508 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1509 if (position
!= record_number
* fcb
->logical_record_size
) {
1510 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1511 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1512 AL_result
= 0x01; /* end of file, no data read */
1514 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1515 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, fcb
->logical_record_size
);
1516 if (bytes_read
!= fcb
->logical_record_size
) {
1517 TRACE("_lread(%d, %p, %d) failed with %d\n",
1518 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_read
);
1519 if (bytes_read
== 0) {
1520 AL_result
= 0x01; /* end of file, no data read */
1522 memset(&disk_transfer_area
[bytes_read
], 0, fcb
->logical_record_size
- bytes_read
);
1523 AL_result
= 0x03; /* end of file, partial record read */
1526 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1527 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1528 AL_result
= 0x00; /* successful */
1532 fcb
->current_block_number
= record_number
/ 128;
1533 fcb
->record_within_current_block
= record_number
% 128;
1534 SET_AL(context
, AL_result
);
1538 /***********************************************************************
1539 * INT21_WriteRandomRecordToFCB
1541 * Handler for function 0x22.
1544 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1549 * 2: segment wrap in DTA (not returned now)
1552 * Writes a record with the size FCB->logical_record_size from
1553 * the disk transfer area to the FCB. The position of the record
1554 * is specified with FCB->random_access_record_number. The
1555 * FCB->random_access_record_number is not updated.
1557 static void INT21_WriteRandomRecordToFCB( CONTEXT86
*context
)
1562 DWORD record_number
;
1564 BYTE
*disk_transfer_area
;
1568 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1569 if (fcb
->drive_number
== 0xff) {
1570 xfcb
= (struct XFCB
*) fcb
;
1571 fcb
= (struct FCB
*) xfcb
->fcb
;
1574 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1575 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1576 if (handle
== INVALID_HANDLE_VALUE
) {
1577 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1579 AL_result
= 0x01; /* disk full */
1581 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1582 if (position
!= record_number
* fcb
->logical_record_size
) {
1583 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1584 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1585 AL_result
= 0x01; /* disk full */
1587 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1588 bytes_written
= _lwrite((HFILE
) handle
, disk_transfer_area
, fcb
->logical_record_size
);
1589 if (bytes_written
!= fcb
->logical_record_size
) {
1590 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1591 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_written
);
1592 AL_result
= 0x01; /* disk full */
1594 TRACE("successful written %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1595 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1596 AL_result
= 0x00; /* successful */
1600 fcb
->current_block_number
= record_number
/ 128;
1601 fcb
->record_within_current_block
= record_number
% 128;
1602 SET_AL(context
, AL_result
);
1606 /***********************************************************************
1607 * INT21_RandomBlockReadFromFCB
1609 * Handler for function 0x27.
1612 * CX [I/O] Number of records to read
1613 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1617 * 1: end of file, no data read
1618 * 2: segment wrap in DTA, no data read (not returned now)
1619 * 3: end of file, partial record read
1622 * Reads several records with the size FCB->logical_record_size from
1623 * the FCB to the disk transfer area. The number of records to be
1624 * read is specified in the CX register. The position of the first
1625 * record is specified with FCB->random_access_record_number. The
1626 * FCB->random_access_record_number, the FCB->current_block_number
1627 * and FCB->record_within_current_block are updated to point to the
1628 * next record after the records read. If a partial record is read,
1629 * it is filled with zeros up to the FCB->logical_record_size. The
1630 * CX register is set to the number of successfully read records.
1632 static void INT21_RandomBlockReadFromFCB( CONTEXT86
*context
)
1637 DWORD record_number
;
1639 BYTE
*disk_transfer_area
;
1640 UINT records_requested
;
1641 UINT bytes_requested
;
1646 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1647 if (fcb
->drive_number
== 0xff) {
1648 xfcb
= (struct XFCB
*) fcb
;
1649 fcb
= (struct FCB
*) xfcb
->fcb
;
1652 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1653 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1654 if (handle
== INVALID_HANDLE_VALUE
) {
1655 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1658 AL_result
= 0x01; /* end of file, no data read */
1660 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1661 if (position
!= record_number
* fcb
->logical_record_size
) {
1662 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1663 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1665 AL_result
= 0x01; /* end of file, no data read */
1667 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1668 records_requested
= CX_reg(context
);
1669 bytes_requested
= (UINT
) records_requested
* fcb
->logical_record_size
;
1670 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, bytes_requested
);
1671 if (bytes_read
!= bytes_requested
) {
1672 TRACE("_lread(%d, %p, %d) failed with %d\n",
1673 fcb
->file_number
, disk_transfer_area
, bytes_requested
, bytes_read
);
1674 records_read
= bytes_read
/ fcb
->logical_record_size
;
1675 if (bytes_read
% fcb
->logical_record_size
== 0) {
1676 AL_result
= 0x01; /* end of file, no data read */
1679 memset(&disk_transfer_area
[bytes_read
], 0, records_read
* fcb
->logical_record_size
- bytes_read
);
1680 AL_result
= 0x03; /* end of file, partial record read */
1683 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1684 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1685 records_read
= records_requested
;
1686 AL_result
= 0x00; /* successful */
1690 record_number
+= records_read
;
1691 memcpy(fcb
->random_access_record_number
, &record_number
, 4);
1692 fcb
->current_block_number
= record_number
/ 128;
1693 fcb
->record_within_current_block
= record_number
% 128;
1694 SET_CX(context
, records_read
);
1695 SET_AL(context
, AL_result
);
1699 /***********************************************************************
1700 * INT21_RandomBlockWriteToFCB
1702 * Handler for function 0x28.
1705 * CX [I/O] Number of records to write
1706 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1711 * 2: segment wrap in DTA (not returned now)
1714 * Writes several records with the size FCB->logical_record_size from
1715 * the disk transfer area to the FCB. The number of records to be
1716 * written is specified in the CX register. The position of the first
1717 * record is specified with FCB->random_access_record_number. The
1718 * FCB->random_access_record_number, the FCB->current_block_number
1719 * and FCB->record_within_current_block are updated to point to the
1720 * next record after the records written. The CX register is set to
1721 * the number of successfully written records.
1723 static void INT21_RandomBlockWriteToFCB( CONTEXT86
*context
)
1728 DWORD record_number
;
1730 BYTE
*disk_transfer_area
;
1731 UINT records_requested
;
1732 UINT bytes_requested
;
1734 UINT records_written
;
1737 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1738 if (fcb
->drive_number
== 0xff) {
1739 xfcb
= (struct XFCB
*) fcb
;
1740 fcb
= (struct FCB
*) xfcb
->fcb
;
1743 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1744 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1745 if (handle
== INVALID_HANDLE_VALUE
) {
1746 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1748 records_written
= 0;
1749 AL_result
= 0x01; /* disk full */
1751 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1752 if (position
!= record_number
* fcb
->logical_record_size
) {
1753 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1754 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1755 records_written
= 0;
1756 AL_result
= 0x01; /* disk full */
1758 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1759 records_requested
= CX_reg(context
);
1760 bytes_requested
= (UINT
) records_requested
* fcb
->logical_record_size
;
1761 bytes_written
= _lwrite((HFILE
) handle
, disk_transfer_area
, bytes_requested
);
1762 if (bytes_written
!= bytes_requested
) {
1763 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1764 fcb
->file_number
, disk_transfer_area
, bytes_requested
, bytes_written
);
1765 records_written
= bytes_written
/ fcb
->logical_record_size
;
1766 AL_result
= 0x01; /* disk full */
1768 TRACE("successful write %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1769 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1770 records_written
= records_requested
;
1771 AL_result
= 0x00; /* successful */
1775 record_number
+= records_written
;
1776 memcpy(fcb
->random_access_record_number
, &record_number
, 4);
1777 fcb
->current_block_number
= record_number
/ 128;
1778 fcb
->record_within_current_block
= record_number
% 128;
1779 SET_CX(context
, records_written
);
1780 SET_AL(context
, AL_result
);
1784 /***********************************************************************
1785 * INT21_CreateDirectory
1789 * - subfunction 0x39 of function 0x71
1790 * - subfunction 0xff of function 0x43 (CL == 0x39)
1792 static BOOL
INT21_CreateDirectory( CONTEXT86
*context
)
1794 WCHAR dirW
[MAX_PATH
];
1795 char *dirA
= CTX_SEG_OFF_TO_LIN(context
,
1799 TRACE( "CREATE DIRECTORY %s\n", dirA
);
1801 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
1803 if (CreateDirectoryW(dirW
, NULL
))
1807 * FIXME: CreateDirectory's LastErrors will clash with the ones
1808 * used by DOS. AH=39 only returns 3 (path not found) and
1809 * 5 (access denied), while CreateDirectory return several
1810 * ones. Remap some of them. -Marcus
1812 switch (GetLastError())
1814 case ERROR_ALREADY_EXISTS
:
1815 case ERROR_FILENAME_EXCED_RANGE
:
1816 case ERROR_DISK_FULL
:
1817 SetLastError(ERROR_ACCESS_DENIED
);
1827 /***********************************************************************
1828 * INT21_ExtendedCountryInformation
1830 * Handler for function 0x65.
1832 static void INT21_ExtendedCountryInformation( CONTEXT86
*context
)
1834 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN( context
, context
->SegEs
, context
->Edi
);
1836 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1840 * Check subfunctions that are passed country and code page.
1842 if (AL_reg(context
) >= 0x01 && AL_reg(context
) <= 0x07)
1844 WORD country
= DX_reg(context
);
1845 WORD codepage
= BX_reg(context
);
1847 if (country
!= 0xffff && country
!= INT21_GetSystemCountryCode())
1848 FIXME( "Requested info on non-default country %04x\n", country
);
1850 if (codepage
!= 0xffff && codepage
!= GetOEMCP())
1851 FIXME( "Requested info on non-default code page %04x\n", codepage
);
1854 switch (AL_reg(context
)) {
1855 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1856 INT_BARF( context
, 0x21 );
1857 SET_CFLAG( context
);
1860 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1861 TRACE( "Get general internationalization info\n" );
1862 dataptr
[0] = 0x01; /* Info ID */
1863 *(WORD
*)(dataptr
+1) = 38; /* Size of the following info */
1864 *(WORD
*)(dataptr
+3) = INT21_GetSystemCountryCode(); /* Country ID */
1865 *(WORD
*)(dataptr
+5) = GetOEMCP(); /* Code page */
1866 INT21_FillCountryInformation( dataptr
+ 7 );
1867 SET_CX( context
, 41 ); /* Size of returned info */
1870 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1871 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1872 TRACE( "Get pointer to uppercase table\n" );
1873 dataptr
[0] = AL_reg(context
); /* Info ID */
1874 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1875 offsetof(INT21_HEAP
, uppercase_size
) );
1876 SET_CX( context
, 5 ); /* Size of returned info */
1879 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1880 TRACE( "Get pointer to lowercase table\n" );
1881 dataptr
[0] = 0x03; /* Info ID */
1882 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1883 offsetof(INT21_HEAP
, lowercase_size
) );
1884 SET_CX( context
, 5 ); /* Size of returned info */
1887 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1888 TRACE("Get pointer to filename terminator table\n");
1889 dataptr
[0] = 0x05; /* Info ID */
1890 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1891 offsetof(INT21_HEAP
, filename_size
) );
1892 SET_CX( context
, 5 ); /* Size of returned info */
1895 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1896 TRACE("Get pointer to collating sequence table\n");
1897 dataptr
[0] = 0x06; /* Info ID */
1898 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1899 offsetof(INT21_HEAP
, collating_size
) );
1900 SET_CX( context
, 5 ); /* Size of returned info */
1903 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1904 TRACE("Get pointer to DBCS lead byte table\n");
1905 dataptr
[0] = 0x07; /* Info ID */
1906 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1907 offsetof(INT21_HEAP
, dbcs_size
) );
1908 SET_CX( context
, 5 ); /* Size of returned info */
1911 case 0x20: /* CAPITALIZE CHARACTER */
1912 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1913 TRACE("Convert char to uppercase\n");
1914 SET_DL( context
, toupper(DL_reg(context
)) );
1917 case 0x21: /* CAPITALIZE STRING */
1918 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1919 TRACE("Convert string to uppercase with length\n");
1921 char *ptr
= (char *)CTX_SEG_OFF_TO_LIN( context
,
1924 WORD len
= CX_reg(context
);
1925 while (len
--) { *ptr
= toupper(*ptr
); ptr
++; }
1929 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1930 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1931 TRACE("Convert ASCIIZ string to uppercase\n");
1932 _strupr( (LPSTR
)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
) );
1935 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1936 INT_BARF( context
, 0x21 );
1937 SET_CFLAG( context
);
1941 INT_BARF( context
, 0x21 );
1948 /***********************************************************************
1949 * INT21_FileAttributes
1953 * - subfunction 0x43 of function 0x71
1955 static BOOL
INT21_FileAttributes( CONTEXT86
*context
,
1959 WCHAR fileW
[MAX_PATH
];
1960 char *fileA
= CTX_SEG_OFF_TO_LIN(context
,
1969 switch (subfunction
)
1971 case 0x00: /* GET FILE ATTRIBUTES */
1972 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA
);
1973 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1975 result
= GetFileAttributesW( fileW
);
1976 if (result
== INVALID_FILE_ATTRIBUTES
)
1980 SET_CX( context
, (WORD
)result
);
1982 SET_AX( context
, (WORD
)result
); /* DR DOS */
1986 case 0x01: /* SET FILE ATTRIBUTES */
1987 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
1988 CX_reg(context
), fileA
);
1989 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1991 if (!SetFileAttributesW( fileW
, CX_reg(context
) ))
1995 case 0x02: /* GET COMPRESSED FILE SIZE */
1996 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA
);
1997 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1999 result
= GetCompressedFileSizeW( fileW
, NULL
);
2000 if (result
== INVALID_FILE_SIZE
)
2004 SET_AX( context
, LOWORD(result
) );
2005 SET_DX( context
, HIWORD(result
) );
2009 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
2011 INT_BARF( context
, 0x21 );
2014 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA
);
2015 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
2017 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
2018 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2019 NULL
, OPEN_EXISTING
, 0, 0 );
2020 if (handle
== INVALID_HANDLE_VALUE
)
2023 DosDateTimeToFileTime( DI_reg(context
),
2026 status
= SetFileTime( handle
, NULL
, NULL
, &filetime
);
2028 CloseHandle( handle
);
2033 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
2035 INT_BARF( context
, 0x21 );
2038 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA
);
2039 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
2041 handle
= CreateFileW( fileW
, GENERIC_READ
,
2042 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2043 NULL
, OPEN_EXISTING
, 0, 0 );
2044 if (handle
== INVALID_HANDLE_VALUE
)
2047 status
= GetFileTime( handle
, NULL
, NULL
, &filetime
);
2050 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2051 SET_DI( context
, date
);
2052 SET_CX( context
, time
);
2055 CloseHandle( handle
);
2060 case 0x05: /* SET FILE LAST ACCESS DATE */
2062 INT_BARF( context
, 0x21 );
2065 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA
);
2066 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
2068 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
2069 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2070 NULL
, OPEN_EXISTING
, 0, 0 );
2071 if (handle
== INVALID_HANDLE_VALUE
)
2074 DosDateTimeToFileTime( DI_reg(context
),
2077 status
= SetFileTime( handle
, NULL
, &filetime
, NULL
);
2079 CloseHandle( handle
);
2084 case 0x06: /* GET FILE LAST ACCESS DATE */
2086 INT_BARF( context
, 0x21 );
2089 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA
);
2090 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
2092 handle
= CreateFileW( fileW
, GENERIC_READ
,
2093 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2094 NULL
, OPEN_EXISTING
, 0, 0 );
2095 if (handle
== INVALID_HANDLE_VALUE
)
2098 status
= GetFileTime( handle
, NULL
, &filetime
, NULL
);
2101 FileTimeToDosDateTime( &filetime
, &date
, NULL
);
2102 SET_DI( context
, date
);
2105 CloseHandle( handle
);
2110 case 0x07: /* SET FILE CREATION DATE AND TIME */
2112 INT_BARF( context
, 0x21 );
2115 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA
);
2117 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
2118 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2119 NULL
, OPEN_EXISTING
, 0, 0 );
2120 if (handle
== INVALID_HANDLE_VALUE
)
2124 * FIXME: SI has number of 10-millisecond units past time in CX.
2126 DosDateTimeToFileTime( DI_reg(context
),
2129 status
= SetFileTime( handle
, &filetime
, NULL
, NULL
);
2131 CloseHandle( handle
);
2136 case 0x08: /* GET FILE CREATION DATE AND TIME */
2138 INT_BARF( context
, 0x21 );
2141 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
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
, &filetime
, NULL
, NULL
);
2153 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2154 SET_DI( context
, date
);
2155 SET_CX( context
, time
);
2157 * FIXME: SI has number of 10-millisecond units past
2160 SET_SI( context
, 0 );
2163 CloseHandle(handle
);
2168 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2169 if (islong
|| context
->Ebp
!= 0x5053)
2170 INT_BARF( context
, 0x21 );
2173 switch(CL_reg(context
))
2176 if (!INT21_CreateDirectory( context
))
2181 if (!INT21_RenameFile( context
))
2186 INT_BARF( context
, 0x21 );
2192 INT_BARF( context
, 0x21 );
2199 /***********************************************************************
2200 * INT21_FileDateTime
2202 * Handler for function 0x57.
2204 static BOOL
INT21_FileDateTime( CONTEXT86
*context
)
2206 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2210 switch (AL_reg(context
)) {
2211 case 0x00: /* Get last-written stamp */
2212 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2215 if (!GetFileTime( handle
, NULL
, NULL
, &filetime
))
2217 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2218 SET_DX( context
, date
);
2219 SET_CX( context
, time
);
2223 case 0x01: /* Set last-written stamp */
2224 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2227 DosDateTimeToFileTime( DX_reg(context
),
2230 if (!SetFileTime( handle
, NULL
, NULL
, &filetime
))
2235 case 0x04: /* Get last access stamp, DOS 7 */
2236 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2239 if (!GetFileTime( handle
, NULL
, &filetime
, NULL
))
2241 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2242 SET_DX( context
, date
);
2243 SET_CX( context
, time
);
2247 case 0x05: /* Set last access stamp, DOS 7 */
2248 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2251 DosDateTimeToFileTime( DX_reg(context
),
2254 if (!SetFileTime( handle
, NULL
, &filetime
, NULL
))
2259 case 0x06: /* Get creation stamp, DOS 7 */
2260 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2263 if (!GetFileTime( handle
, &filetime
, NULL
, NULL
))
2265 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2266 SET_DX( context
, date
);
2267 SET_CX( context
, time
);
2269 * FIXME: SI has number of 10-millisecond units past time in CX.
2271 SET_SI( context
, 0 );
2275 case 0x07: /* Set creation stamp, DOS 7 */
2276 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2280 * FIXME: SI has number of 10-millisecond units past time in CX.
2282 DosDateTimeToFileTime( DX_reg(context
),
2285 if (!SetFileTime( handle
, &filetime
, NULL
, NULL
))
2291 INT_BARF( context
, 0x21 );
2299 /***********************************************************************
2302 * Handler for functions 0x51 and 0x62.
2304 static void INT21_GetPSP( CONTEXT86
*context
)
2306 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context
) );
2309 * FIXME: should we return the original DOS PSP upon
2312 if (!ISV86(context
) && DOSVM_IsWin16())
2313 SET_BX( context
, LOWORD(GetCurrentPDB16()) );
2315 SET_BX( context
, DOSVM_psp
);
2318 static inline void setword( BYTE
*ptr
, WORD w
)
2321 ptr
[1] = (BYTE
)(w
>> 8);
2324 static void CreateBPB(int drive
, BYTE
*data
, BOOL16 limited
)
2325 /* limited == TRUE is used with INT 0x21/0x440d */
2327 /* FIXME: we're forcing some values without checking that those are valid */
2332 setword(&data
[3], 0);
2334 setword(&data
[6], 240);
2335 setword(&data
[8], 64000);
2337 setword(&data
[0x0b], 40);
2338 setword(&data
[0x0d], 56);
2339 setword(&data
[0x0f], 2);
2340 setword(&data
[0x11], 0);
2343 setword(&data
[0x1f], 800);
2345 setword(&data
[0x22], 1);
2352 setword(&data
[3], 0);
2354 setword(&data
[6], 240);
2355 setword(&data
[8], 2880);
2357 setword(&data
[0x0b], 6);
2358 setword(&data
[0x0d], 18);
2359 setword(&data
[0x0f], 2);
2360 setword(&data
[0x11], 0);
2363 setword(&data
[0x1f], 80);
2365 setword(&data
[0x22], 2);
2370 /***********************************************************************
2373 * Handler for block device IOCTLs.
2375 static void INT21_Ioctl_Block( CONTEXT86
*context
)
2378 BYTE drive
= INT21_MapDrive( BL_reg(context
) );
2379 WCHAR drivespec
[4] = {'A', ':', '\\', 0};
2382 drivespec
[0] += drive
;
2383 drivetype
= GetDriveTypeW( drivespec
);
2385 RESET_CFLAG(context
);
2386 if (drivetype
== DRIVE_UNKNOWN
|| drivetype
== DRIVE_NO_ROOT_DIR
)
2388 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2389 AL_reg(context
), 'A' + drive
);
2390 SetLastError( ERROR_INVALID_DRIVE
);
2391 SET_AX( context
, ERROR_INVALID_DRIVE
);
2392 SET_CFLAG( context
);
2396 switch (AL_reg(context
))
2398 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2399 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2400 INT_BARF( context
, 0x21 );
2403 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2404 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2407 if (drivetype
== DRIVE_REMOVABLE
)
2408 SET_AX( context
, 0 ); /* removable */
2410 SET_AX( context
, 1 ); /* not removable */
2413 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2414 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2417 if (drivetype
== DRIVE_REMOTE
)
2418 SET_DX( context
, (1<<9) | (1<<12) ); /* remote + no direct IO */
2420 SET_DX( context
, 0 ); /* FIXME: use driver attr here */
2423 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2424 /* Get pointer to IOCTL parameter block. */
2425 dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2427 switch (CX_reg(context
))
2429 case 0x0841: /* write logical device track */
2430 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2433 WORD head
= *(WORD
*)dataptr
+1;
2434 WORD cyl
= *(WORD
*)dataptr
+3;
2435 WORD sect
= *(WORD
*)dataptr
+5;
2436 WORD nrsect
= *(WORD
*)dataptr
+7;
2437 BYTE
*data
= (BYTE
*)dataptr
+9; /* FIXME: is this correct? */
2439 if (!DOSVM_RawWrite(drive
, head
*cyl
*sect
, nrsect
, data
, FALSE
))
2441 SET_AX( context
, ERROR_WRITE_FAULT
);
2447 case 0x084a: /* lock logical volume */
2448 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2449 BH_reg(context
), DX_reg(context
), 'A' + drive
);
2452 case 0x0860: /* get device parameters */
2453 /* FIXME: we're faking some values here */
2454 /* used by w4wgrp's winfile */
2455 memset(dataptr
, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2457 dataptr
[6] = 0; /* media type */
2460 dataptr
[1] = 0x05; /* fixed disk */
2461 setword(&dataptr
[2], 0x01); /* non removable */
2462 setword(&dataptr
[4], 0x300); /* # of cylinders */
2466 dataptr
[1] = 0x07; /* block dev, floppy */
2467 setword(&dataptr
[2], 0x02); /* removable */
2468 setword(&dataptr
[4], 80); /* # of cylinders */
2470 CreateBPB(drive
, &dataptr
[7], TRUE
);
2471 RESET_CFLAG(context
);
2474 case 0x0861: /* read logical device track */
2475 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2478 WORD head
= *(WORD
*)dataptr
+1;
2479 WORD cyl
= *(WORD
*)dataptr
+3;
2480 WORD sect
= *(WORD
*)dataptr
+5;
2481 WORD nrsect
= *(WORD
*)dataptr
+7;
2482 BYTE
*data
= (BYTE
*)dataptr
+9; /* FIXME: is this correct? */
2484 if (!DOSVM_RawRead(drive
, head
*cyl
*sect
, nrsect
, data
, FALSE
))
2486 SET_AX( context
, ERROR_READ_FAULT
);
2492 case 0x0866: /* get volume serial number */
2494 WCHAR label
[12],fsname
[9];
2497 drivespec
[0] += drive
;
2498 GetVolumeInformationW(drivespec
, label
, 12, &serial
, NULL
, NULL
, fsname
, 9);
2499 *(WORD
*)dataptr
= 0;
2500 memcpy(dataptr
+2,&serial
,4);
2501 WideCharToMultiByte(CP_OEMCP
, 0, label
, 11, dataptr
+ 6, 11, NULL
, NULL
);
2502 WideCharToMultiByte(CP_OEMCP
, 0, fsname
, 8, dataptr
+ 17, 8, NULL
, NULL
);
2506 case 0x086a: /* unlock logical volume */
2507 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2511 case 0x086f: /* get drive map information */
2512 memset(dataptr
+1, '\0', dataptr
[0]-1);
2513 dataptr
[1] = dataptr
[0];
2514 dataptr
[2] = 0x07; /* protected mode driver; no eject; no notification */
2515 dataptr
[3] = 0xFF; /* no physical drive */
2519 /* Trail on error implementation */
2520 SET_AX( context
, drivetype
== DRIVE_UNKNOWN
? 0x0f : 0x01 );
2521 SET_CFLAG(context
); /* Seems to be set all the time */
2525 INT_BARF( context
, 0x21 );
2529 case 0x0e: /* GET LOGICAL DRIVE MAP */
2530 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2532 /* FIXME: this is not correct if drive has mappings */
2533 SET_AL( context
, 0 ); /* drive has no mapping */
2536 case 0x0f: /* SET LOGICAL DRIVE MAP */
2538 WCHAR dev
[3], tgt
[4];
2540 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2541 INT21_DriveName( BL_reg(context
)));
2542 dev
[0] = 'A' + drive
; dev
[1] = ':'; dev
[2] = 0;
2543 tgt
[0] = 'A' + drive
+ 1; dev
[1] = ':'; dev
[2] = '\\'; dev
[3] = 0;
2544 if (!DefineDosDeviceW(DDD_RAW_TARGET_PATH
, dev
, tgt
))
2547 SET_AX( context
, 0x000F ); /* invalid drive */
2552 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2554 INT_BARF( context
, 0x21 );
2559 /***********************************************************************
2560 * INT21_IoctlScsiMgrHandler
2562 * IOCTL handler for the SCSIMGR device.
2564 static void INT21_IoctlScsiMgrHandler( CONTEXT86
*context
)
2566 switch (AL_reg(context
))
2568 case 0x00: /* GET DEVICE INFORMATION */
2569 SET_DX( context
, 0xc0c0 );
2572 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2573 DOSVM_ASPIHandler(context
);
2576 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2577 SET_DX( context
, 0 );
2580 case 0x01: /* SET DEVICE INFORMATION */
2581 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2582 case 0x06: /* GET INPUT STATUS */
2583 case 0x07: /* GET OUTPUT STATUS */
2584 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2585 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2587 INT_BARF( context
, 0x21 );
2593 /***********************************************************************
2594 * INT21_IoctlEMSHandler
2596 * IOCTL handler for the EMXXXX0 device.
2598 static void INT21_IoctlEMSHandler( CONTEXT86
*context
)
2600 EMS_Ioctl_Handler(context
);
2604 /***********************************************************************
2605 * INT21_IoctlHPScanHandler
2607 * IOCTL handler for the HPSCAN device.
2609 static void INT21_IoctlHPScanHandler( CONTEXT86
*context
)
2611 switch (AL_reg(context
))
2613 case 0x00: /* GET DEVICE INFORMATION */
2614 SET_DX( context
, 0xc0c0 );
2617 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2618 SET_DX( context
, 0 );
2621 case 0x01: /* SET DEVICE INFORMATION */
2622 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2623 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2624 case 0x06: /* GET INPUT STATUS */
2625 case 0x07: /* GET OUTPUT STATUS */
2626 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2627 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2629 INT_BARF( context
, 0x21 );
2635 /***********************************************************************
2638 * Handler for character device IOCTLs.
2640 static void INT21_Ioctl_Char( CONTEXT86
*context
)
2644 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2646 status
= wine_server_handle_to_fd( handle
, 0, &fd
, NULL
, NULL
);
2649 SET_AX( context
, RtlNtStatusToDosError(status
) );
2650 SET_CFLAG( context
);
2654 wine_server_release_fd( handle
, fd
);
2656 for (i
= 0; i
< NB_MAGIC_DEVICES
; i
++)
2658 if (!magic_devices
[i
].handle
) continue;
2659 if (magic_devices
[i
].dev
== st
.st_dev
&& magic_devices
[i
].ino
== st
.st_ino
)
2662 magic_devices
[i
].ioctl_handler( context
);
2667 /* no magic device found, do default handling */
2669 switch (AL_reg(context
))
2671 case 0x00: /* GET DEVICE INFORMATION */
2672 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context
) );
2673 if (S_ISCHR(st
.st_mode
))
2676 * Returns attribute word in DX:
2677 * Bit 14 - Device driver can process IOCTL requests.
2678 * Bit 13 - Output until busy supported.
2679 * Bit 11 - Driver supports OPEN/CLOSE calls.
2681 * Bit 7 - Set (indicates device).
2682 * Bit 6 - EOF on input.
2683 * Bit 5 - Raw (binary) mode.
2684 * Bit 4 - Device is special (uses int29).
2685 * Bit 3 - Clock device.
2686 * Bit 2 - NUL device.
2687 * Bit 1 - Standard output.
2688 * Bit 0 - Standard input.
2690 SET_DX( context
, 0x80c0 /* FIXME */ );
2695 * Returns attribute word in DX:
2696 * Bit 15 - File is remote.
2697 * Bit 14 - Don't set file date/time on closing.
2698 * Bit 11 - Media not removable.
2699 * Bit 8 - Generate int24 if no disk space on write
2700 * or read past end of file
2701 * Bit 7 - Clear (indicates file).
2702 * Bit 6 - File has not been written.
2703 * Bit 5..0 - Drive number (0=A:,...)
2705 * FIXME: Should check if file is on remote or removable drive.
2706 * FIXME: Should use drive file is located on (and not current).
2708 SET_DX( context
, 0x0140 + INT21_GetCurrentDrive() );
2712 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2713 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context
) );
2715 * Returns attribute word in DX:
2716 * Bit 15 - Set if remote.
2717 * Bit 14 - Set if date/time not set on close.
2719 * FIXME: Should check if file is on remote drive.
2721 SET_DX( context
, 0 );
2724 case 0x01: /* SET DEVICE INFORMATION */
2725 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2726 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2727 case 0x06: /* GET INPUT STATUS */
2728 case 0x07: /* GET OUTPUT STATUS */
2729 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2730 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2732 INT_BARF( context
, 0x21 );
2738 /***********************************************************************
2741 * Handler for function 0x44.
2743 static void INT21_Ioctl( CONTEXT86
*context
)
2745 switch (AL_reg(context
))
2751 INT21_Ioctl_Char( context
);
2756 INT21_Ioctl_Block( context
);
2761 INT21_Ioctl_Char( context
);
2766 INT21_Ioctl_Block( context
);
2770 INT21_Ioctl_Char( context
);
2773 case 0x0b: /* SET SHARING RETRY COUNT */
2774 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2775 CX_reg(context
), DX_reg(context
) );
2776 if (!CX_reg(context
))
2778 SET_AX( context
, 1 );
2779 SET_CFLAG( context
);
2783 DOSDEV_SetSharingRetry( CX_reg(context
), DX_reg(context
) );
2784 RESET_CFLAG( context
);
2789 INT21_Ioctl_Char( context
);
2795 INT21_Ioctl_Block( context
);
2799 INT21_Ioctl_Char( context
);
2803 INT21_Ioctl_Block( context
);
2806 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2807 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2808 SET_CFLAG(context
); /* Error / This is not DR DOS. */
2809 SET_AX( context
, 0x0001 ); /* Invalid function */
2812 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2813 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2814 SET_CFLAG(context
); /* Error / This is not DR DOS. */
2815 SET_AX( context
, 0x0001 ); /* Invalid function */
2818 case 0xe0: /* Sun PC-NFS API */
2819 TRACE( "Sun PC-NFS API\n" );
2824 INT_BARF( context
, 0x21 );
2829 /***********************************************************************
2832 * Handler for function 0x73.
2834 static BOOL
INT21_Fat32( CONTEXT86
*context
)
2836 switch (AL_reg(context
))
2838 case 0x02: /* FAT32 - GET EXTENDED DPB */
2840 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
2841 WORD
*ptr
= CTX_SEG_OFF_TO_LIN(context
,
2842 context
->SegEs
, context
->Edi
);
2843 INT21_DPB
*target
= (INT21_DPB
*)(ptr
+ 1);
2846 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context
) );
2848 if ( CX_reg(context
) < sizeof(INT21_DPB
) + 2 || *ptr
< sizeof(INT21_DPB
) )
2850 SetLastError( ERROR_BAD_LENGTH
);
2854 if ( !INT21_FillDrivePB( drive
) )
2856 SetLastError( ERROR_INVALID_DRIVE
);
2860 source
= &INT21_GetHeapPointer()->misc_dpb_list
[drive
];
2862 *ptr
= sizeof(INT21_DPB
);
2863 memcpy( target
, source
, sizeof(INT21_DPB
));
2865 if (LOWORD(context
->Esi
) != 0xF1A6)
2867 target
->driver_header
= 0;
2872 FIXME( "Caller requested driver and next DPB pointers!\n" );
2877 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2879 WCHAR dirW
[MAX_PATH
];
2880 char *dirA
= CTX_SEG_OFF_TO_LIN( context
,
2881 context
->SegDs
, context
->Edx
);
2882 BYTE
*data
= CTX_SEG_OFF_TO_LIN( context
,
2883 context
->SegEs
, context
->Edi
);
2884 DWORD cluster_sectors
;
2886 DWORD free_clusters
;
2887 DWORD total_clusters
;
2889 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA
);
2890 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
2892 if (CX_reg(context
) < 44)
2894 SetLastError( ERROR_BAD_LENGTH
);
2898 if (!GetDiskFreeSpaceW( dirW
, &cluster_sectors
, §or_bytes
,
2899 &free_clusters
, &total_clusters
))
2902 *(WORD
*) (data
+ 0) = 44; /* size of structure */
2903 *(WORD
*) (data
+ 2) = 0; /* version */
2904 *(DWORD
*)(data
+ 4) = cluster_sectors
;
2905 *(DWORD
*)(data
+ 8) = sector_bytes
;
2906 *(DWORD
*)(data
+ 12) = free_clusters
;
2907 *(DWORD
*)(data
+ 16) = total_clusters
;
2910 * Below we have free/total sectors and
2911 * free/total allocation units without adjustment
2912 * for compression. We fake both using cluster information.
2914 *(DWORD
*)(data
+ 20) = free_clusters
* cluster_sectors
;
2915 *(DWORD
*)(data
+ 24) = total_clusters
* cluster_sectors
;
2916 *(DWORD
*)(data
+ 28) = free_clusters
;
2917 *(DWORD
*)(data
+ 32) = total_clusters
;
2920 * Between (data + 36) and (data + 43) there
2921 * are eight reserved bytes.
2927 INT_BARF( context
, 0x21 );
2933 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA
*dataA
,
2934 const WIN32_FIND_DATAW
*dataW
)
2936 dataA
->dwFileAttributes
= dataW
->dwFileAttributes
;
2937 dataA
->ftCreationTime
= dataW
->ftCreationTime
;
2938 dataA
->ftLastAccessTime
= dataW
->ftLastAccessTime
;
2939 dataA
->ftLastWriteTime
= dataW
->ftLastWriteTime
;
2940 dataA
->nFileSizeHigh
= dataW
->nFileSizeHigh
;
2941 dataA
->nFileSizeLow
= dataW
->nFileSizeLow
;
2942 WideCharToMultiByte( CP_OEMCP
, 0, dataW
->cFileName
, -1,
2943 dataA
->cFileName
, sizeof(dataA
->cFileName
), NULL
, NULL
);
2944 WideCharToMultiByte( CP_OEMCP
, 0, dataW
->cAlternateFileName
, -1,
2945 dataA
->cAlternateFileName
, sizeof(dataA
->cAlternateFileName
), NULL
, NULL
);
2948 /***********************************************************************
2949 * INT21_LongFilename
2951 * Handler for function 0x71.
2953 static void INT21_LongFilename( CONTEXT86
*context
)
2955 BOOL bSetDOSExtendedError
= FALSE
;
2956 WCHAR pathW
[MAX_PATH
];
2959 if (HIBYTE(HIWORD(GetVersion16())) < 0x07)
2961 TRACE( "LONG FILENAME - functions supported only under DOS7\n" );
2962 SET_CFLAG( context
);
2963 SET_AL( context
, 0 );
2967 switch (AL_reg(context
))
2969 case 0x0d: /* RESET DRIVE */
2970 INT_BARF( context
, 0x21 );
2973 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
2974 if (!INT21_CreateDirectory( context
))
2975 bSetDOSExtendedError
= TRUE
;
2978 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
2979 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2981 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA
);
2982 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2983 if (!RemoveDirectoryW( pathW
)) bSetDOSExtendedError
= TRUE
;
2986 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
2987 if (!INT21_SetCurrentDirectory( context
))
2988 bSetDOSExtendedError
= TRUE
;
2991 case 0x41: /* LONG FILENAME - DELETE FILE */
2992 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2994 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA
);
2995 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2997 if (!DeleteFileW( pathW
)) bSetDOSExtendedError
= TRUE
;
3000 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
3001 if (!INT21_FileAttributes( context
, BL_reg(context
), TRUE
))
3002 bSetDOSExtendedError
= TRUE
;
3005 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
3006 if (!INT21_GetCurrentDirectory( context
, TRUE
))
3007 bSetDOSExtendedError
= TRUE
;
3010 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
3014 WIN32_FIND_DATAW dataW
;
3015 WIN32_FIND_DATAA
* dataA
;
3017 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Edx
);
3018 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA
);
3020 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
3021 handle
= FindFirstFileW(pathW
, &dataW
);
3023 dataA
= (WIN32_FIND_DATAA
*)CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
,
3025 if (handle
!= INVALID_HANDLE_VALUE
&&
3026 (h16
= GlobalAlloc16(GMEM_MOVEABLE
, sizeof(handle
))))
3028 HANDLE
* ptr
= GlobalLock16( h16
);
3030 GlobalUnlock16( h16
);
3031 SET_AX( context
, h16
);
3032 INT21_ConvertFindDataWtoA(dataA
, &dataW
);
3036 if (handle
!= INVALID_HANDLE_VALUE
) FindClose(handle
);
3037 SET_AX( context
, INVALID_HANDLE_VALUE16
);
3038 bSetDOSExtendedError
= TRUE
;
3043 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
3045 HGLOBAL16 h16
= BX_reg(context
);
3047 WIN32_FIND_DATAW dataW
;
3048 WIN32_FIND_DATAA
* dataA
;
3050 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
3053 dataA
= (WIN32_FIND_DATAA
*)CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
,
3056 if (h16
!= INVALID_HANDLE_VALUE16
&& (ptr
= GlobalLock16( h16
)))
3058 if (!FindNextFileW(*ptr
, &dataW
)) bSetDOSExtendedError
= TRUE
;
3059 else INT21_ConvertFindDataWtoA(dataA
, &dataW
);
3060 GlobalUnlock16( h16
);
3064 SetLastError( ERROR_INVALID_HANDLE
);
3065 bSetDOSExtendedError
= TRUE
;
3070 case 0x56: /* LONG FILENAME - RENAME FILE */
3071 if (!INT21_RenameFile(context
))
3072 bSetDOSExtendedError
= TRUE
;
3075 case 0x60: /* LONG FILENAME - CONVERT PATH */
3077 WCHAR res
[MAX_PATH
];
3079 switch (CL_reg(context
))
3081 case 0x01: /* Get short filename or path */
3082 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
3083 if (!GetShortPathNameW(pathW
, res
, 67))
3084 bSetDOSExtendedError
= TRUE
;
3087 SET_AX( context
, 0 );
3088 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
3089 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
3094 case 0x02: /* Get canonical long filename or path */
3095 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
3096 if (!GetFullPathNameW(pathW
, 128, res
, NULL
))
3097 bSetDOSExtendedError
= TRUE
;
3100 SET_AX( context
, 0 );
3101 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
3102 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
3107 FIXME("Unimplemented long file name function:\n");
3108 INT_BARF( context
, 0x21 );
3110 SET_AL( context
, 0 );
3116 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
3117 if (!INT21_CreateFile( context
, context
->Esi
, TRUE
,
3118 BX_reg(context
), DL_reg(context
) ))
3119 bSetDOSExtendedError
= TRUE
;
3122 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
3124 DWORD filename_len
, flags
;
3127 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Edx
);
3129 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA
);
3130 SET_AX( context
, 0 );
3131 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
3132 if (!GetVolumeInformationW( pathW
, NULL
, 0, NULL
, &filename_len
,
3135 INT_BARF( context
, 0x21 );
3139 SET_BX( context
, flags
| 0x4000 ); /* support for LFN functions */
3140 SET_CX( context
, filename_len
);
3141 SET_DX( context
, MAX_PATH
); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
3142 WideCharToMultiByte(CP_OEMCP
, 0, dstW
, -1,
3143 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
3148 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
3150 HGLOBAL16 h16
= BX_reg(context
);
3153 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
3155 if (h16
!= INVALID_HANDLE_VALUE16
&& (ptr
= GlobalLock16( h16
)))
3157 if (!FindClose( *ptr
)) bSetDOSExtendedError
= TRUE
;
3158 GlobalUnlock16( h16
);
3159 GlobalFree16( h16
);
3163 SetLastError( ERROR_INVALID_HANDLE
);
3164 bSetDOSExtendedError
= TRUE
;
3169 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
3171 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
3172 BY_HANDLE_FILE_INFORMATION
*info
=
3173 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3175 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
3177 if (!GetFileInformationByHandle(handle
, info
))
3178 bSetDOSExtendedError
= TRUE
;
3182 case 0xa7: /* LONG FILENAME - CONVERT TIME */
3183 switch (BL_reg(context
))
3185 case 0x00: /* FILE TIME TO DOS TIME */
3188 FILETIME
*filetime
= CTX_SEG_OFF_TO_LIN(context
,
3192 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
3194 FileTimeToDosDateTime( filetime
, &date
, &time
);
3196 SET_DX( context
, date
);
3197 SET_CX( context
, time
);
3200 * FIXME: BH has number of 10-millisecond units
3203 SET_BH( context
, 0 );
3207 case 0x01: /* DOS TIME TO FILE TIME */
3209 FILETIME
*filetime
= CTX_SEG_OFF_TO_LIN(context
,
3213 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
3216 * FIXME: BH has number of 10-millisecond units
3219 DosDateTimeToFileTime( DX_reg(context
), CX_reg(context
),
3225 INT_BARF( context
, 0x21 );
3230 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
3231 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3232 case 0xaa: /* LONG FILENAME - SUBST */
3234 FIXME("Unimplemented long file name function:\n");
3235 INT_BARF( context
, 0x21 );
3237 SET_AL( context
, 0 );
3241 if (bSetDOSExtendedError
)
3243 SET_AX( context
, GetLastError() );
3244 SET_CFLAG( context
);
3249 /***********************************************************************
3254 * - subfunction 0x56 of function 0x71
3255 * - subfunction 0xff of function 0x43 (CL == 0x56)
3257 static BOOL
INT21_RenameFile( CONTEXT86
*context
)
3259 WCHAR fromW
[MAX_PATH
];
3260 WCHAR toW
[MAX_PATH
];
3261 char *fromA
= CTX_SEG_OFF_TO_LIN(context
,
3262 context
->SegDs
,context
->Edx
);
3263 char *toA
= CTX_SEG_OFF_TO_LIN(context
,
3264 context
->SegEs
,context
->Edi
);
3266 TRACE( "RENAME FILE %s to %s\n", fromA
, toA
);
3267 MultiByteToWideChar(CP_OEMCP
, 0, fromA
, -1, fromW
, MAX_PATH
);
3268 MultiByteToWideChar(CP_OEMCP
, 0, toA
, -1, toW
, MAX_PATH
);
3270 return MoveFileW( fromW
, toW
);
3274 /***********************************************************************
3280 static BOOL
INT21_NetworkFunc (CONTEXT86
*context
)
3282 switch (AL_reg(context
))
3284 case 0x00: /* Get machine name. */
3286 WCHAR dstW
[MAX_COMPUTERNAME_LENGTH
+ 1];
3287 DWORD s
= sizeof(dstW
) / sizeof(WCHAR
);
3290 char *dst
= CTX_SEG_OFF_TO_LIN (context
,context
->SegDs
,context
->Edx
);
3291 TRACE("getting machine name to %p\n", dst
);
3292 if (!GetComputerNameW(dstW
, &s
) ||
3293 !WideCharToMultiByte(CP_OEMCP
, 0, dstW
, -1, dst
, 16, NULL
, NULL
))
3296 SetLastError( ER_NoNetwork
);
3299 for (len
= strlen(dst
); len
< 15; len
++) dst
[len
] = ' ';
3301 SET_CH( context
, 1 ); /* Valid */
3302 SET_CL( context
, 1 ); /* NETbios number??? */
3303 TRACE("returning %s\n", debugstr_an(dst
, 16));
3308 SetLastError( ER_NoNetwork
);
3313 /******************************************************************
3314 * INT21_GetDiskSerialNumber
3317 static int INT21_GetDiskSerialNumber( CONTEXT86
*context
)
3319 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3320 WCHAR path
[] = {'A',':',0}, label
[11];
3323 path
[0] += INT21_MapDrive(BL_reg(context
));
3324 if (!GetVolumeInformationW( path
, label
, 11, &serial
, NULL
, NULL
, NULL
, 0))
3326 SetLastError( ERROR_INVALID_DRIVE
);
3330 *(WORD
*)dataptr
= 0;
3331 memcpy(dataptr
+ 2, &serial
, sizeof(DWORD
));
3332 WideCharToMultiByte(CP_OEMCP
, 0, label
, 11, dataptr
+ 6, 11, NULL
, NULL
);
3333 strncpy(dataptr
+ 17, "FAT16 ", 8);
3338 /******************************************************************
3339 * INT21_SetDiskSerialNumber
3342 static int INT21_SetDiskSerialNumber( CONTEXT86
*context
)
3345 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3346 int drive
= INT21_MapDrive(BL_reg(context
));
3348 if (!is_valid_drive(drive
))
3350 SetLastError( ERROR_INVALID_DRIVE
);
3354 DRIVE_SetSerialNumber( drive
, *(DWORD
*)(dataptr
+ 2) );
3357 FIXME("Setting drive serial number is no longer supported\n");
3358 SetLastError( ERROR_NOT_SUPPORTED
);
3364 /******************************************************************
3365 * INT21_GetFreeDiskSpace
3368 static int INT21_GetFreeDiskSpace( CONTEXT86
*context
)
3370 DWORD cluster_sectors
, sector_bytes
, free_clusters
, total_clusters
;
3371 WCHAR root
[] = {'A',':','\\',0};
3373 root
[0] += INT21_MapDrive(DL_reg(context
));
3374 if (!GetDiskFreeSpaceW( root
, &cluster_sectors
, §or_bytes
,
3375 &free_clusters
, &total_clusters
)) return 0;
3376 SET_AX( context
, cluster_sectors
);
3377 SET_BX( context
, free_clusters
);
3378 SET_CX( context
, sector_bytes
);
3379 SET_DX( context
, total_clusters
);
3383 /******************************************************************
3384 * INT21_GetDriveAllocInfo
3387 static int INT21_GetDriveAllocInfo( CONTEXT86
*context
, int drive
)
3391 if (!INT21_FillDrivePB( drive
)) return 0;
3392 dpb
= &(INT21_GetHeapPointer()->misc_dpb_list
[drive
]);
3393 SET_AL( context
, dpb
->cluster_sectors
+ 1 );
3394 SET_CX( context
, dpb
->sector_bytes
);
3395 SET_DX( context
, dpb
->num_clusters1
);
3397 context
->SegDs
= INT21_GetHeapSelector( context
);
3398 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
].media_ID
) );
3402 /***********************************************************************
3403 * INT21_GetExtendedError
3405 static void INT21_GetExtendedError( CONTEXT86
*context
)
3407 BYTE
class, action
, locus
;
3408 WORD error
= GetLastError();
3413 class = action
= locus
= 0;
3415 case ERROR_DIR_NOT_EMPTY
:
3420 case ERROR_ACCESS_DENIED
:
3421 class = EC_AccessDenied
;
3425 case ERROR_CANNOT_MAKE
:
3426 class = EC_AccessDenied
;
3430 case ERROR_DISK_FULL
:
3431 case ERROR_HANDLE_DISK_FULL
:
3432 class = EC_MediaError
;
3436 case ERROR_FILE_EXISTS
:
3437 case ERROR_ALREADY_EXISTS
:
3442 case ERROR_FILE_NOT_FOUND
:
3443 class = EC_NotFound
;
3447 case ERROR_GEN_FAILURE
:
3448 class = EC_SystemFailure
;
3452 case ERROR_INVALID_DRIVE
:
3453 class = EC_MediaError
;
3457 case ERROR_INVALID_HANDLE
:
3458 class = EC_ProgramError
;
3462 case ERROR_LOCK_VIOLATION
:
3463 class = EC_AccessDenied
;
3467 case ERROR_NO_MORE_FILES
:
3468 class = EC_MediaError
;
3473 class = EC_NotFound
;
3477 case ERROR_NOT_ENOUGH_MEMORY
:
3478 class = EC_OutOfResource
;
3482 case ERROR_PATH_NOT_FOUND
:
3483 class = EC_NotFound
;
3488 class = EC_NotFound
;
3492 case ERROR_SHARING_VIOLATION
:
3493 class = EC_Temporary
;
3497 case ERROR_TOO_MANY_OPEN_FILES
:
3498 class = EC_ProgramError
;
3503 FIXME("Unknown error %d\n", error
);
3504 class = EC_SystemFailure
;
3509 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3510 error
, class, action
, locus
);
3511 SET_AX( context
, error
);
3512 SET_BH( context
, class );
3513 SET_BL( context
, action
);
3514 SET_CH( context
, locus
);
3517 static BOOL
INT21_CreateTempFile( CONTEXT86
*context
)
3519 static int counter
= 0;
3520 char *name
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3521 char *p
= name
+ strlen(name
);
3523 /* despite what Ralf Brown says, some programs seem to call without
3524 * ending backslash (DOS accepts that, so we accept it too) */
3525 if ((p
== name
) || (p
[-1] != '\\')) *p
++ = '\\';
3529 sprintf( p
, "wine%04x.%03d", (int)getpid(), counter
);
3530 counter
= (counter
+ 1) % 1000;
3533 Win32HandleToDosFileHandle(
3534 CreateFileA( name
, GENERIC_READ
| GENERIC_WRITE
,
3535 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
3536 CREATE_NEW
, 0, 0 ) ) );
3537 if (AX_reg(context
) != HFILE_ERROR16
)
3539 TRACE("created %s\n", name
);
3542 if (GetLastError() != ERROR_FILE_EXISTS
) return FALSE
;
3546 /***********************************************************************
3547 * DOSFS_ToDosFCBFormat
3549 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3550 * expanding wild cards and converting to upper-case in the process.
3551 * File name can be terminated by '\0', '\\' or '/'.
3552 * Return FALSE if the name is not a valid DOS name.
3553 * 'buffer' must be at least 12 characters long.
3555 /* Chars we don't want to see in DOS file names */
3556 static BOOL
INT21_ToDosFCBFormat( LPCWSTR name
, LPWSTR buffer
)
3558 static const WCHAR invalid_chars
[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3562 /* Check for "." and ".." */
3567 for(i
= 1; i
< 11; i
++) buffer
[i
] = ' ';
3574 return (!*p
|| (*p
== '/') || (*p
== '\\'));
3577 for (i
= 0; i
< 8; i
++)
3594 if (strchrW( invalid_chars
, *p
)) return FALSE
;
3595 buffer
[i
] = toupperW(*p
);
3603 /* Skip all chars after wildcard up to first dot */
3604 while (*p
&& (*p
!= '/') && (*p
!= '\\') && (*p
!= '.')) p
++;
3608 /* Check if name too long */
3609 if (*p
&& (*p
!= '/') && (*p
!= '\\') && (*p
!= '.')) return FALSE
;
3611 if (*p
== '.') p
++; /* Skip dot */
3613 for (i
= 8; i
< 11; i
++)
3623 return FALSE
; /* Second extension not allowed */
3631 if (strchrW( invalid_chars
, *p
)) return FALSE
;
3632 buffer
[i
] = toupperW(*p
);
3639 /* at most 3 character of the extension are processed
3640 * is something behind this ?
3642 while (*p
== '*' || *p
== ' ') p
++; /* skip wildcards and spaces */
3643 return (!*p
|| (*p
== '/') || (*p
== '\\'));
3646 static HANDLE INT21_FindHandle
;
3647 static const WCHAR
*INT21_FindPath
; /* will point to current dta->fullPath search */
3649 /******************************************************************
3652 static int INT21_FindFirst( CONTEXT86
*context
)
3656 FINDFILE_DTA
*dta
= (FINDFILE_DTA
*)INT21_GetCurrentDTA(context
);
3657 WCHAR maskW
[12], pathW
[MAX_PATH
];
3658 static const WCHAR wildcardW
[] = {'*','.','*',0};
3660 path
= (const char *)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3661 MultiByteToWideChar(CP_OEMCP
, 0, path
, -1, pathW
, MAX_PATH
);
3663 p
= strrchrW( pathW
, '\\');
3666 if (pathW
[0] && pathW
[1] == ':') p
= pathW
+ 2;
3671 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3672 * (doesn't matter as it is set below anyway)
3674 if (!INT21_ToDosFCBFormat( p
, maskW
))
3676 SetLastError( ERROR_FILE_NOT_FOUND
);
3677 SET_AX( context
, ERROR_FILE_NOT_FOUND
);
3681 WideCharToMultiByte(CP_OEMCP
, 0, maskW
, 12, dta
->mask
, sizeof(dta
->mask
), NULL
, NULL
);
3683 dta
->fullPath
= HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW
) + (p
- pathW
)*sizeof(WCHAR
) );
3684 memcpy( dta
->fullPath
, pathW
, (p
- pathW
) * sizeof(WCHAR
) );
3685 memcpy( dta
->fullPath
+ (p
- pathW
), wildcardW
, sizeof(wildcardW
) );
3686 /* we must have a fully qualified file name in dta->fullPath
3687 * (we could have a UNC path, but this would lead to some errors later on)
3689 dta
->drive
= toupperW(dta
->fullPath
[0]) - 'A';
3691 dta
->search_attr
= CL_reg(context
);
3695 /******************************************************************
3698 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3700 static BOOL
match_short(LPCWSTR shortW
, LPCSTR maskA
)
3702 WCHAR mask
[11], file
[12];
3705 if (!INT21_ToDosFCBFormat( shortW
, file
)) return FALSE
;
3706 MultiByteToWideChar(CP_OEMCP
, 0, maskA
, 11, mask
, 11);
3707 for (i
= 0; i
< 11; i
++)
3708 if (mask
[i
] != '?' && mask
[i
] != file
[i
]) return FALSE
;
3712 static unsigned INT21_FindHelper(LPCWSTR fullPath
, unsigned drive
, unsigned count
,
3713 LPCSTR mask
, unsigned search_attr
,
3714 WIN32_FIND_DATAW
* entry
)
3718 if ((search_attr
& ~(FA_UNUSED
| FA_ARCHIVE
| FA_RDONLY
)) == FA_LABEL
)
3720 WCHAR path
[] = {' ',':',0};
3722 if (count
) return 0;
3723 path
[0] = drive
+ 'A';
3724 if (!GetVolumeInformationW(path
, entry
->cAlternateFileName
, 13, NULL
, NULL
, NULL
, NULL
, 0)) return 0;
3725 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER
*)&entry
->ftCreationTime
);
3726 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER
*)&entry
->ftLastAccessTime
);
3727 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER
*)&entry
->ftLastWriteTime
);
3728 entry
->dwFileAttributes
= FILE_ATTRIBUTE_LABEL
;
3729 entry
->nFileSizeHigh
= entry
->nFileSizeLow
= 0;
3730 TRACE("returning %s as label\n", debugstr_w(entry
->cAlternateFileName
));
3734 if (!INT21_FindHandle
|| INT21_FindPath
!= fullPath
|| count
== 0)
3736 if (INT21_FindHandle
) FindClose(INT21_FindHandle
);
3737 INT21_FindHandle
= FindFirstFileW(fullPath
, entry
);
3738 if (INT21_FindHandle
== INVALID_HANDLE_VALUE
)
3740 INT21_FindHandle
= 0;
3743 INT21_FindPath
= fullPath
;
3744 /* we need to resync search */
3749 while (ncalls
-- != 0)
3751 if (!FindNextFileW(INT21_FindHandle
, entry
))
3753 FindClose(INT21_FindHandle
); INT21_FindHandle
= 0;
3757 while (count
< 0xffff)
3760 /* Check the file attributes, and path */
3761 if (!(entry
->dwFileAttributes
& ~search_attr
) &&
3762 match_short(entry
->cAlternateFileName
[0] ? entry
->cAlternateFileName
: entry
->cFileName
,
3767 if (!FindNextFileW(INT21_FindHandle
, entry
))
3769 FindClose(INT21_FindHandle
); INT21_FindHandle
= 0;
3773 WARN("Too many directory entries in %s\n", debugstr_w(fullPath
) );
3777 /******************************************************************
3780 static int INT21_FindNext( CONTEXT86
*context
)
3782 FINDFILE_DTA
*dta
= (FINDFILE_DTA
*)INT21_GetCurrentDTA(context
);
3783 DWORD attr
= dta
->search_attr
| FA_UNUSED
| FA_ARCHIVE
| FA_RDONLY
;
3784 WIN32_FIND_DATAW entry
;
3787 if (!dta
->fullPath
) return 0;
3789 n
= INT21_FindHelper(dta
->fullPath
, dta
->drive
, dta
->count
,
3790 dta
->mask
, attr
, &entry
);
3793 dta
->fileattr
= entry
.dwFileAttributes
;
3794 dta
->filesize
= entry
.nFileSizeLow
;
3795 FileTimeToDosDateTime( &entry
.ftLastWriteTime
, &dta
->filedate
, &dta
->filetime
);
3796 if (entry
.cAlternateFileName
[0])
3797 WideCharToMultiByte(CP_OEMCP
, 0, entry
.cAlternateFileName
, -1,
3798 dta
->filename
, 13, NULL
, NULL
);
3800 WideCharToMultiByte(CP_OEMCP
, 0, entry
.cFileName
, -1, dta
->filename
, 13, NULL
, NULL
);
3802 if (!memchr(dta
->mask
,'?',11))
3804 /* wildcardless search, release resources in case no findnext will
3805 * be issued, and as a workaround in case file creation messes up
3806 * findnext, as sometimes happens with pkunzip
3808 HeapFree( GetProcessHeap(), 0, dta
->fullPath
);
3809 INT21_FindPath
= dta
->fullPath
= NULL
;
3814 HeapFree( GetProcessHeap(), 0, dta
->fullPath
);
3815 INT21_FindPath
= dta
->fullPath
= NULL
;
3819 /* microsoft's programmers should be shot for using CP/M style int21
3820 calls in Windows for Workgroup's winfile.exe */
3822 /******************************************************************
3823 * INT21_FindFirstFCB
3826 static int INT21_FindFirstFCB( CONTEXT86
*context
)
3828 BYTE
*fcb
= (BYTE
*)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3831 WCHAR p
[] = {' ',':',};
3833 if (*fcb
== 0xff) pFCB
= (FINDFILE_FCB
*)(fcb
+ 7);
3834 else pFCB
= (FINDFILE_FCB
*)fcb
;
3835 drive
= INT21_MapDrive( pFCB
->drive
);
3836 if (drive
== MAX_DOS_DRIVES
) return 0;
3839 pFCB
->fullPath
= HeapAlloc(GetProcessHeap(), 0, MAX_PATH
* sizeof(WCHAR
));
3840 if (!pFCB
->fullPath
) return 0;
3841 GetLongPathNameW(p
, pFCB
->fullPath
, MAX_PATH
);
3846 /******************************************************************
3850 static int INT21_FindNextFCB( CONTEXT86
*context
)
3852 BYTE
*fcb
= (BYTE
*)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3854 DOS_DIRENTRY_LAYOUT
*pResult
= (DOS_DIRENTRY_LAYOUT
*)INT21_GetCurrentDTA(context
);
3855 WIN32_FIND_DATAW entry
;
3860 if (*fcb
== 0xff) /* extended FCB ? */
3863 pFCB
= (FINDFILE_FCB
*)(fcb
+ 7);
3868 pFCB
= (FINDFILE_FCB
*)fcb
;
3871 if (!pFCB
->fullPath
) return 0;
3872 n
= INT21_FindHelper(pFCB
->fullPath
, INT21_MapDrive( pFCB
->drive
),
3873 pFCB
->count
, pFCB
->filename
, attr
, &entry
);
3876 HeapFree( GetProcessHeap(), 0, pFCB
->fullPath
);
3877 INT21_FindPath
= pFCB
->fullPath
= NULL
;
3884 /* place extended FCB header before pResult if called with extended FCB */
3885 *(BYTE
*)pResult
= 0xff;
3886 (BYTE
*)pResult
+=6; /* leave reserved field behind */
3887 *(BYTE
*)pResult
= entry
.dwFileAttributes
;
3888 ((BYTE
*)pResult
)++;
3890 *(BYTE
*)pResult
= INT21_MapDrive( pFCB
->drive
); /* DOS_DIRENTRY_LAYOUT after current drive number */
3891 ((BYTE
*)pResult
)++;
3892 pResult
->fileattr
= entry
.dwFileAttributes
;
3893 pResult
->cluster
= 0; /* what else? */
3894 pResult
->filesize
= entry
.nFileSizeLow
;
3895 memset( pResult
->reserved
, 0, sizeof(pResult
->reserved
) );
3896 FileTimeToDosDateTime( &entry
.ftLastWriteTime
,
3897 &pResult
->filedate
, &pResult
->filetime
);
3899 /* Convert file name to FCB format */
3900 if (entry
.cAlternateFileName
[0])
3901 INT21_ToDosFCBFormat( entry
.cAlternateFileName
, nameW
);
3903 INT21_ToDosFCBFormat( entry
.cFileName
, nameW
);
3904 WideCharToMultiByte(CP_OEMCP
, 0, nameW
, 11, pResult
->filename
, 11, NULL
, NULL
);
3909 /******************************************************************
3910 * INT21_ParseFileNameIntoFCB
3913 static void INT21_ParseFileNameIntoFCB( CONTEXT86
*context
)
3916 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
);
3918 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
3922 INT buffer_len
, len
;
3924 SET_AL( context
, 0xff ); /* failed */
3926 TRACE("filename: '%s'\n", filename
);
3929 while (*s
&& (*s
!= ' ') && (*s
!= '\r') && (*s
!= '\n'))
3933 buffer_len
= MultiByteToWideChar(CP_OEMCP
, 0, filename
, len
, NULL
, 0);
3934 buffer
= HeapAlloc( GetProcessHeap(), 0, (buffer_len
+ 1) * sizeof(WCHAR
));
3935 len
= MultiByteToWideChar(CP_OEMCP
, 0, filename
, len
, buffer
, buffer_len
);
3937 INT21_ToDosFCBFormat(buffer
, fcbW
);
3938 HeapFree(GetProcessHeap(), 0, buffer
);
3939 WideCharToMultiByte(CP_OEMCP
, 0, fcbW
, 12, fcb
+ 1, 12, NULL
, NULL
);
3941 TRACE("FCB: '%s'\n", fcb
+ 1);
3943 SET_AL( context
, ((strchr(filename
, '*')) || (strchr(filename
, '$'))) != 0 );
3945 /* point DS:SI to first unparsed character */
3946 SET_SI( context
, context
->Esi
+ (int)s
- (int)filename
);
3949 static BOOL
INT21_Dup2(HFILE16 hFile1
, HFILE16 hFile2
)
3951 HFILE16 res
= HFILE_ERROR16
;
3952 HANDLE handle
, new_handle
;
3953 #define DOS_TABLE_SIZE 256
3954 DWORD map
[DOS_TABLE_SIZE
/ 32];
3957 handle
= DosFileHandleToWin32Handle(hFile1
);
3958 if (handle
== INVALID_HANDLE_VALUE
)
3962 /* now loop to allocate the same one... */
3963 memset(map
, 0, sizeof(map
));
3964 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
3966 if (!DuplicateHandle(GetCurrentProcess(), handle
,
3967 GetCurrentProcess(), &new_handle
,
3968 0, FALSE
, DUPLICATE_SAME_ACCESS
))
3970 res
= HFILE_ERROR16
;
3973 res
= Win32HandleToDosFileHandle(new_handle
);
3974 if (res
== HFILE_ERROR16
|| res
== hFile2
) break;
3975 map
[res
/ 32] |= 1 << (res
% 32);
3977 /* clean up the allocated slots */
3978 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
3980 if (map
[i
/ 32] & (1 << (i
% 32)))
3981 _lclose16((HFILE16
)i
);
3983 return res
== hFile2
;
3987 /***********************************************************************
3988 * DOSVM_Int21Handler
3990 * Interrupt 0x21 handler.
3992 void WINAPI
DOSVM_Int21Handler( CONTEXT86
*context
)
3994 BOOL bSetDOSExtendedError
= FALSE
;
3996 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
3997 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
3998 AX_reg(context
), BX_reg(context
),
3999 CX_reg(context
), DX_reg(context
),
4000 SI_reg(context
), DI_reg(context
),
4001 (WORD
)context
->SegDs
, (WORD
)context
->SegEs
,
4005 * Extended error is used by (at least) functions 0x2f to 0x62.
4006 * Function 0x59 returns extended error and error should not
4007 * be cleared before handling the function.
4009 if (AH_reg(context
) >= 0x2f && AH_reg(context
) != 0x59)
4012 RESET_CFLAG(context
); /* Not sure if this is a good idea. */
4014 switch(AH_reg(context
))
4016 case 0x00: /* TERMINATE PROGRAM */
4017 TRACE("TERMINATE PROGRAM\n");
4018 if (DOSVM_IsWin16())
4020 else if(ISV86(context
))
4021 MZ_Exit( context
, FALSE
, 0 );
4023 ERR( "Called from DOS protected mode\n" );
4026 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
4029 TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
4030 INT21_ReadChar( &ascii
, context
);
4031 SET_AL( context
, ascii
);
4033 * FIXME: What to echo when extended keycodes are read?
4035 DOSVM_PutChar(AL_reg(context
));
4039 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
4040 TRACE("Write Character to Standard Output\n");
4041 DOSVM_PutChar(DL_reg(context
));
4044 case 0x03: /* READ CHARACTER FROM STDAUX */
4045 case 0x04: /* WRITE CHARACTER TO STDAUX */
4046 case 0x05: /* WRITE CHARACTER TO PRINTER */
4047 INT_BARF( context
, 0x21 );
4050 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
4051 if (DL_reg(context
) == 0xff)
4053 TRACE("Direct Console Input\n");
4055 if (INT21_ReadChar( NULL
, NULL
))
4058 INT21_ReadChar( &ascii
, context
);
4059 SET_AL( context
, ascii
);
4060 RESET_ZFLAG( context
);
4064 /* no character available */
4065 SET_AL( context
, 0 );
4066 SET_ZFLAG( context
);
4071 TRACE("Direct Console Output\n");
4072 DOSVM_PutChar(DL_reg(context
));
4074 * At least DOS versions 2.1-7.0 return character
4075 * that was written in AL register.
4077 SET_AL( context
, DL_reg(context
) );
4081 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
4084 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
4085 INT21_ReadChar( &ascii
, context
);
4086 SET_AL( context
, ascii
);
4090 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
4093 TRACE("CHARACTER INPUT WITHOUT ECHO\n");
4094 INT21_ReadChar( &ascii
, context
);
4095 SET_AL( context
, ascii
);
4099 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
4100 TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
4101 context
->SegDs
, DX_reg(context
) );
4103 LPSTR data
= CTX_SEG_OFF_TO_LIN( context
,
4104 context
->SegDs
, context
->Edx
);
4108 * Do NOT use strchr() to calculate the string length,
4109 * as '\0' is valid string content, too!
4110 * Maybe we should check for non-'$' strings, but DOS doesn't.
4112 while (*p
!= '$') p
++;
4114 if (DOSVM_IsWin16())
4115 WriteFile( DosFileHandleToWin32Handle(1),
4116 data
, p
- data
, 0, 0 );
4118 for(; data
!= p
; data
++)
4119 DOSVM_PutChar( *data
);
4121 SET_AL( context
, '$' ); /* yes, '$' (0x24) gets returned in AL */
4125 case 0x0a: /* BUFFERED INPUT */
4127 BYTE
*ptr
= CTX_SEG_OFF_TO_LIN(context
,
4132 TRACE( "BUFFERED INPUT (size=%d)\n", ptr
[0] );
4135 * FIXME: Some documents state that
4136 * ptr[1] holds number of chars from last input which
4137 * may be recalled on entry, other documents do not mention
4141 TRACE( "Handle old chars in buffer!\n" );
4144 * ptr[0] - capacity (includes terminating CR)
4145 * ptr[1] - characters read (excludes terminating CR)
4147 result
= INT21_BufferedInput( context
, ptr
+ 2, ptr
[0] );
4149 ptr
[1] = (BYTE
)result
- 1;
4155 case 0x0b: /* GET STDIN STATUS */
4156 TRACE( "GET STDIN STATUS\n" );
4158 if (INT21_ReadChar( NULL
, NULL
))
4159 SET_AL( context
, 0xff ); /* character available */
4161 SET_AL( context
, 0 ); /* no character available */
4165 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
4167 BYTE al
= AL_reg(context
); /* Input function to execute after flush. */
4169 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al
);
4171 /* FIXME: buffers are not flushed */
4174 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
4175 * int21 function identified by AL will be called.
4177 if(al
== 0x01 || al
== 0x06 || al
== 0x07 || al
== 0x08 || al
== 0x0a)
4179 SET_AH( context
, al
);
4180 DOSVM_Int21Handler( context
);
4185 case 0x0d: /* DISK BUFFER FLUSH */
4186 TRACE("DISK BUFFER FLUSH ignored\n");
4189 case 0x0e: /* SELECT DEFAULT DRIVE */
4190 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context
) );
4191 INT21_SetCurrentDrive( DL_reg(context
) );
4192 SET_AL( context
, MAX_DOS_DRIVES
);
4195 case 0x0f: /* OPEN FILE USING FCB */
4196 INT21_OpenFileUsingFCB( context
);
4199 case 0x10: /* CLOSE FILE USING FCB */
4200 INT21_CloseFileUsingFCB( context
);
4203 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
4204 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
4205 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
));
4206 if (!INT21_FindFirstFCB(context
))
4208 SET_AL( context
, 0xff );
4211 /* else fall through */
4213 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
4214 SET_AL( context
, INT21_FindNextFCB(context
) ? 0x00 : 0xff );
4217 case 0x13: /* DELETE FILE USING FCB */
4218 INT_BARF( context
, 0x21 );
4221 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
4222 INT21_SequentialReadFromFCB( context
);
4225 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
4226 INT21_SequentialWriteToFCB( context
);
4229 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
4230 case 0x17: /* RENAME FILE USING FCB */
4231 INT_BARF( context
, 0x21 );
4234 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4235 SET_AL( context
, 0 );
4238 case 0x19: /* GET CURRENT DEFAULT DRIVE */
4239 SET_AL( context
, INT21_GetCurrentDrive() );
4240 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context
) );
4243 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
4244 TRACE( "SET DISK TRANSFER AREA ADDRESS %04lX:%04X\n",
4245 context
->SegDs
, DX_reg(context
) );
4247 TDB
*task
= GlobalLock16( GetCurrentTask() );
4248 task
->dta
= MAKESEGPTR( context
->SegDs
, DX_reg(context
) );
4252 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4253 if (!INT21_GetDriveAllocInfo(context
, 0))
4254 SET_AX( context
, 0xffff );
4257 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4258 if (!INT21_GetDriveAllocInfo(context
, DL_reg(context
)))
4259 SET_AX( context
, 0xffff );
4262 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4263 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4264 SET_AL( context
, 0 );
4267 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4269 BYTE drive
= INT21_MapDrive( 0 );
4270 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4272 if (INT21_FillDrivePB( drive
))
4274 SET_AL( context
, 0x00 ); /* success */
4275 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
] ) );
4276 context
->SegDs
= INT21_GetHeapSelector( context
);
4280 SET_AL( context
, 0xff ); /* invalid or network drive */
4285 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4286 SET_AL( context
, 0 );
4289 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4290 INT21_ReadRandomRecordFromFCB( context
);
4293 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4294 INT21_WriteRandomRecordToFCB( context
);
4297 case 0x23: /* GET FILE SIZE FOR FCB */
4298 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4299 INT_BARF( context
, 0x21 );
4302 case 0x25: /* SET INTERRUPT VECTOR */
4303 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context
));
4305 FARPROC16 ptr
= (FARPROC16
)MAKESEGPTR( context
->SegDs
, DX_reg(context
) );
4306 if (!ISV86(context
) && DOSVM_IsWin16())
4307 DOSVM_SetPMHandler16( AL_reg(context
), ptr
);
4309 DOSVM_SetRMHandler( AL_reg(context
), ptr
);
4313 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4314 INT_BARF( context
, 0x21 );
4317 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4318 INT21_RandomBlockReadFromFCB( context
);
4321 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4322 INT21_RandomBlockWriteToFCB( context
);
4325 case 0x29: /* PARSE FILENAME INTO FCB */
4326 INT21_ParseFileNameIntoFCB(context
);
4329 case 0x2a: /* GET SYSTEM DATE */
4330 TRACE( "GET SYSTEM DATE\n" );
4333 GetLocalTime( &systime
);
4334 SET_CX( context
, systime
.wYear
);
4335 SET_DH( context
, systime
.wMonth
);
4336 SET_DL( context
, systime
.wDay
);
4337 SET_AL( context
, systime
.wDayOfWeek
);
4341 case 0x2b: /* SET SYSTEM DATE */
4342 TRACE( "SET SYSTEM DATE\n" );
4344 WORD year
= CX_reg(context
);
4345 BYTE month
= DH_reg(context
);
4346 BYTE day
= DL_reg(context
);
4348 if (year
>= 1980 && year
<= 2099 &&
4349 month
>= 1 && month
<= 12 &&
4350 day
>= 1 && day
<= 31)
4352 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4354 SET_AL( context
, 0 ); /* Let's pretend we succeeded */
4358 SET_AL( context
, 0xff ); /* invalid date */
4359 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4365 case 0x2c: /* GET SYSTEM TIME */
4366 TRACE( "GET SYSTEM TIME\n" );
4369 GetLocalTime( &systime
);
4370 SET_CH( context
, systime
.wHour
);
4371 SET_CL( context
, systime
.wMinute
);
4372 SET_DH( context
, systime
.wSecond
);
4373 SET_DL( context
, systime
.wMilliseconds
/ 10 );
4377 case 0x2d: /* SET SYSTEM TIME */
4378 if( CH_reg(context
) >= 24 || CL_reg(context
) >= 60 || DH_reg(context
) >= 60 || DL_reg(context
) >= 100 ) {
4379 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4380 CH_reg(context
), CL_reg(context
),
4381 DH_reg(context
), DL_reg(context
) );
4382 SET_AL( context
, 0xFF );
4386 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4387 CH_reg(context
), CL_reg(context
),
4388 DH_reg(context
), DL_reg(context
) );
4389 SET_AL( context
, 0 ); /* Let's pretend we succeeded */
4393 case 0x2e: /* SET VERIFY FLAG */
4394 TRACE("SET VERIFY FLAG ignored\n");
4395 /* we cannot change the behaviour anyway, so just ignore it */
4398 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4399 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4401 TDB
*task
= GlobalLock16( GetCurrentTask() );
4402 context
->SegEs
= SELECTOROF( task
->dta
);
4403 SET_BX( context
, OFFSETOF( task
->dta
) );
4407 case 0x30: /* GET DOS VERSION */
4408 TRACE( "GET DOS VERSION - %s requested\n",
4409 (AL_reg(context
) == 0x00) ? "OEM number" : "version flag" );
4411 if (AL_reg(context
) == 0x00)
4412 SET_BH( context
, 0xff ); /* OEM number => undefined */
4414 SET_BH( context
, 0x08 ); /* version flag => DOS is in ROM */
4416 SET_AL( context
, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4417 SET_AH( context
, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4419 SET_BL( context
, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4420 SET_CX( context
, 0x3456 );
4423 case 0x31: /* TERMINATE AND STAY RESIDENT */
4424 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4427 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4429 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
4430 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context
) );
4432 if (INT21_FillDrivePB( drive
))
4434 SET_AL( context
, 0x00 ); /* success */
4435 SET_DX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
] ) );
4436 context
->SegDs
= INT21_GetHeapSelector( context
);
4440 SET_AL( context
, 0xff ); /* invalid or network drive */
4445 case 0x33: /* MULTIPLEXED */
4446 switch (AL_reg(context
))
4448 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4449 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4450 SET_DL( context
, DOSCONF_GetConfig()->brk_flag
);
4453 case 0x01: /* SET EXTENDED BREAK STATE */
4454 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4455 DOSCONF_GetConfig()->brk_flag
= (DL_reg(context
) > 0) ? 1 : 0;
4458 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4459 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4460 /* ugly coding in order to stay reentrant */
4461 if (DL_reg(context
))
4463 SET_DL( context
, DOSCONF_GetConfig()->brk_flag
);
4464 DOSCONF_GetConfig()->brk_flag
= 1;
4468 SET_DL( context
, DOSCONF_GetConfig()->brk_flag
);
4469 DOSCONF_GetConfig()->brk_flag
= 0;
4473 case 0x05: /* GET BOOT DRIVE */
4474 TRACE("GET BOOT DRIVE\n");
4475 SET_DL( context
, 3 );
4476 /* c: is Wine's bootdrive (a: is 1)*/
4479 case 0x06: /* GET TRUE VERSION NUMBER */
4480 TRACE("GET TRUE VERSION NUMBER\n");
4481 SET_BL( context
, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4482 SET_BH( context
, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4483 SET_DL( context
, 0x00 ); /* revision */
4484 SET_DH( context
, 0x08 ); /* DOS is in ROM */
4488 INT_BARF( context
, 0x21 );
4493 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4494 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4495 context
->SegEs
= INT21_GetHeapSelector( context
);
4496 SET_BX( context
, offsetof(INT21_HEAP
, misc_indos
) );
4499 case 0x35: /* GET INTERRUPT VECTOR */
4500 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context
));
4503 if (!ISV86(context
) && DOSVM_IsWin16())
4504 addr
= DOSVM_GetPMHandler16( AL_reg(context
) );
4506 addr
= DOSVM_GetRMHandler( AL_reg(context
) );
4507 context
->SegEs
= SELECTOROF(addr
);
4508 SET_BX( context
, OFFSETOF(addr
) );
4512 case 0x36: /* GET FREE DISK SPACE */
4513 TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
4514 INT21_DriveName( DL_reg(context
) ));
4515 if (!INT21_GetFreeDiskSpace(context
)) SET_AX( context
, 0xffff );
4518 case 0x37: /* SWITCHAR */
4520 switch (AL_reg(context
))
4522 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4523 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4524 SET_AL( context
, 0x00 ); /* success*/
4525 SET_DL( context
, '/' );
4527 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4528 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4530 SET_AL( context
, 0x00 ); /* success*/
4533 INT_BARF( context
, 0x21 );
4539 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4540 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4541 if (AL_reg(context
))
4543 WORD country
= AL_reg(context
);
4544 if (country
== 0xff)
4545 country
= BX_reg(context
);
4546 if (country
!= INT21_GetSystemCountryCode()) {
4547 FIXME( "Requested info on non-default country %04x\n", country
);
4552 if(AX_reg(context
) != 2 )
4554 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context
,
4557 SET_AX( context
, INT21_GetSystemCountryCode() );
4558 SET_BX( context
, INT21_GetSystemCountryCode() );
4559 RESET_CFLAG(context
);
4563 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4564 if (!INT21_CreateDirectory( context
))
4565 bSetDOSExtendedError
= TRUE
;
4567 RESET_CFLAG(context
);
4570 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4572 WCHAR dirW
[MAX_PATH
];
4573 char *dirA
= CTX_SEG_OFF_TO_LIN(context
,
4574 context
->SegDs
, context
->Edx
);
4576 TRACE( "REMOVE DIRECTORY %s\n", dirA
);
4578 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
4580 if (!RemoveDirectoryW( dirW
))
4581 bSetDOSExtendedError
= TRUE
;
4583 RESET_CFLAG(context
);
4587 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4588 if (!INT21_SetCurrentDirectory( context
))
4589 bSetDOSExtendedError
= TRUE
;
4591 RESET_CFLAG(context
);
4594 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4595 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4596 OF_READWRITE
| OF_SHARE_COMPAT
, 0x12 ))
4597 bSetDOSExtendedError
= TRUE
;
4599 RESET_CFLAG(context
);
4602 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4603 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4604 AL_reg(context
), 0x01 ))
4605 bSetDOSExtendedError
= TRUE
;
4607 RESET_CFLAG(context
);
4610 case 0x3e: /* "CLOSE" - CLOSE FILE */
4611 TRACE( "CLOSE handle %d\n", BX_reg(context
) );
4612 if (_lclose16( BX_reg(context
) ) == HFILE_ERROR16
)
4613 bSetDOSExtendedError
= TRUE
;
4615 RESET_CFLAG(context
);
4618 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4619 TRACE( "READ from %d to %04lX:%04X for %d bytes\n",
4626 WORD count
= CX_reg(context
);
4627 BYTE
*buffer
= CTX_SEG_OFF_TO_LIN( context
,
4631 /* Some programs pass a count larger than the allocated buffer */
4632 if (DOSVM_IsWin16())
4634 WORD maxcount
= GetSelectorLimit16( context
->SegDs
)
4635 - DX_reg(context
) + 1;
4636 if (count
> maxcount
)
4641 * FIXME: Reading from console (BX=1) in DOS mode
4642 * does not work as it is supposed to work.
4645 RESET_CFLAG(context
); /* set if error */
4646 if (!DOSVM_IsWin16() && BX_reg(context
) == 0)
4648 result
= INT21_BufferedInput( context
, buffer
, count
);
4649 SET_AX( context
, (WORD
)result
);
4651 else if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context
)),
4652 buffer
, count
, &result
, NULL
))
4653 SET_AX( context
, (WORD
)result
);
4655 bSetDOSExtendedError
= TRUE
;
4659 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4660 TRACE( "WRITE from %04lX:%04X to handle %d for %d byte\n",
4661 context
->SegDs
, DX_reg(context
),
4662 BX_reg(context
), CX_reg(context
) );
4664 BYTE
*ptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
4666 if (!DOSVM_IsWin16() &&
4667 (BX_reg(context
) == 1 || BX_reg(context
) == 2))
4670 for(i
=0; i
<CX_reg(context
); i
++)
4671 DOSVM_PutChar(ptr
[i
]);
4672 SET_AX(context
, CX_reg(context
));
4673 RESET_CFLAG(context
);
4677 HFILE handle
= (HFILE
)DosFileHandleToWin32Handle(BX_reg(context
));
4678 LONG result
= _hwrite( handle
, ptr
, CX_reg(context
) );
4679 if (result
== HFILE_ERROR
)
4680 bSetDOSExtendedError
= TRUE
;
4683 SET_AX( context
, (WORD
)result
);
4684 RESET_CFLAG(context
);
4690 case 0x41: /* "UNLINK" - DELETE FILE */
4692 WCHAR fileW
[MAX_PATH
];
4693 char *fileA
= CTX_SEG_OFF_TO_LIN(context
,
4697 TRACE( "UNLINK %s\n", fileA
);
4698 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
4700 if (!DeleteFileW( fileW
))
4701 bSetDOSExtendedError
= TRUE
;
4703 RESET_CFLAG(context
);
4707 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4708 TRACE( "LSEEK handle %d offset %ld from %s\n",
4710 MAKELONG( DX_reg(context
), CX_reg(context
) ),
4711 (AL_reg(context
) == 0) ?
4712 "start of file" : ((AL_reg(context
) == 1) ?
4713 "current file position" : "end of file") );
4715 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
4716 LONG offset
= MAKELONG( DX_reg(context
), CX_reg(context
) );
4717 DWORD status
= SetFilePointer( handle
, offset
,
4718 NULL
, AL_reg(context
) );
4719 if (status
== INVALID_SET_FILE_POINTER
)
4720 bSetDOSExtendedError
= TRUE
;
4723 SET_AX( context
, LOWORD(status
) );
4724 SET_DX( context
, HIWORD(status
) );
4725 RESET_CFLAG(context
);
4730 case 0x43: /* FILE ATTRIBUTES */
4731 if (!INT21_FileAttributes( context
, AL_reg(context
), FALSE
))
4732 bSetDOSExtendedError
= TRUE
;
4734 RESET_CFLAG(context
);
4737 case 0x44: /* IOCTL */
4738 INT21_Ioctl( context
);
4741 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4742 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context
) );
4745 HFILE handle16
= HFILE_ERROR
;
4747 if (DuplicateHandle( GetCurrentProcess(),
4748 DosFileHandleToWin32Handle(BX_reg(context
)),
4749 GetCurrentProcess(),
4751 0, TRUE
, DUPLICATE_SAME_ACCESS
))
4752 handle16
= Win32HandleToDosFileHandle(handle32
);
4754 if (handle16
== HFILE_ERROR
)
4755 bSetDOSExtendedError
= TRUE
;
4758 SET_AX( context
, handle16
);
4759 RESET_CFLAG(context
);
4764 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4765 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4766 BX_reg(context
), CX_reg(context
) );
4767 if (!INT21_Dup2(BX_reg(context
), CX_reg(context
)))
4768 bSetDOSExtendedError
= TRUE
;
4770 RESET_CFLAG(context
);
4773 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4774 if (!INT21_GetCurrentDirectory( context
, FALSE
))
4775 bSetDOSExtendedError
= TRUE
;
4777 RESET_CFLAG(context
);
4780 case 0x48: /* ALLOCATE MEMORY */
4781 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context
) );
4784 DWORD bytes
= (DWORD
)BX_reg(context
) << 4;
4786 if (!ISV86(context
) && DOSVM_IsWin16())
4788 DWORD rv
= GlobalDOSAlloc16( bytes
);
4789 selector
= LOWORD( rv
);
4792 DOSMEM_GetBlock( bytes
, &selector
);
4796 SET_AX( context
, selector
);
4797 RESET_CFLAG(context
);
4802 SET_AX( context
, 0x0008 ); /* insufficient memory */
4803 SET_BX( context
, DOSMEM_Available() >> 4 );
4808 case 0x49: /* FREE MEMORY */
4809 TRACE( "FREE MEMORY segment %04lX\n", context
->SegEs
);
4813 if (!ISV86(context
) && DOSVM_IsWin16())
4815 ok
= !GlobalDOSFree16( context
->SegEs
);
4817 /* If we don't reset ES_reg, we will fail in the relay code */
4822 ok
= DOSMEM_FreeBlock( (void*)((DWORD
)context
->SegEs
<< 4) );
4826 TRACE("FREE MEMORY failed\n");
4828 SET_AX( context
, 0x0009 ); /* memory block address invalid */
4833 case 0x4a: /* RESIZE MEMORY BLOCK */
4834 TRACE( "RESIZE MEMORY segment %04lX to %d paragraphs\n",
4835 context
->SegEs
, BX_reg(context
) );
4837 DWORD newsize
= (DWORD
)BX_reg(context
) << 4;
4839 if (!ISV86(context
) && DOSVM_IsWin16())
4841 FIXME( "Resize memory block - unsupported under Win16\n" );
4846 LPVOID address
= (void*)((DWORD
)context
->SegEs
<< 4);
4847 UINT blocksize
= DOSMEM_ResizeBlock( address
, newsize
, FALSE
);
4849 RESET_CFLAG(context
);
4850 if (blocksize
== (UINT
)-1)
4852 SET_CFLAG( context
);
4853 SET_AX( context
, 0x0009 ); /* illegal address */
4855 else if(blocksize
!= newsize
)
4857 SET_CFLAG( context
);
4858 SET_AX( context
, 0x0008 ); /* insufficient memory */
4859 SET_BX( context
, blocksize
>> 4 ); /* new block size */
4865 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4867 BYTE
*program
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
4868 BYTE
*paramblk
= CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Ebx
);
4870 TRACE( "EXEC %s\n", program
);
4872 RESET_CFLAG(context
);
4873 if (DOSVM_IsWin16())
4875 HINSTANCE16 instance
= WinExec16( program
, SW_NORMAL
);
4878 SET_CFLAG( context
);
4879 SET_AX( context
, instance
);
4884 if (!MZ_Exec( context
, program
, AL_reg(context
), paramblk
))
4885 bSetDOSExtendedError
= TRUE
;
4890 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
4891 TRACE( "EXIT with return code %d\n", AL_reg(context
) );
4892 if (DOSVM_IsWin16())
4893 ExitThread( AL_reg(context
) );
4894 else if(ISV86(context
))
4895 MZ_Exit( context
, FALSE
, AL_reg(context
) );
4901 DWORD rv
= AL_reg(context
);
4902 RaiseException( EXCEPTION_VM86_INTx
, 0, 1, &rv
);
4906 case 0x4d: /* GET RETURN CODE */
4907 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
4908 SET_AX( context
, DOSVM_retval
);
4912 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
4913 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context
),
4914 (LPCSTR
)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
));
4915 if (!INT21_FindFirst(context
)) break;
4918 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
4919 TRACE("FINDNEXT\n");
4920 if (!INT21_FindNext(context
))
4922 SetLastError( ERROR_NO_MORE_FILES
);
4923 SET_AX( context
, ERROR_NO_MORE_FILES
);
4926 else SET_AX( context
, 0 ); /* OK */
4929 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
4930 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
4931 DOSVM_psp
= BX_reg(context
);
4934 case 0x51: /* GET PSP ADDRESS */
4935 INT21_GetPSP( context
);
4938 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
4940 SEGPTR ptr
= DOSDEV_GetLOL( ISV86(context
) || !DOSVM_IsWin16() );
4941 context
->SegEs
= SELECTOROF(ptr
);
4942 SET_BX( context
, OFFSETOF(ptr
) );
4946 case 0x54: /* Get Verify Flag */
4947 TRACE("Get Verify Flag - Not Supported\n");
4948 SET_AL( context
, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
4951 case 0x56: /* "RENAME" - RENAME FILE */
4952 if (!INT21_RenameFile( context
))
4953 bSetDOSExtendedError
= TRUE
;
4955 RESET_CFLAG(context
);
4958 case 0x57: /* FILE DATE AND TIME */
4959 if (!INT21_FileDateTime( context
))
4960 bSetDOSExtendedError
= TRUE
;
4962 RESET_CFLAG(context
);
4965 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
4966 switch (AL_reg(context
))
4968 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
4969 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
4970 SET_AX( context
, 0 ); /* low memory first fit */
4973 case 0x01: /* SET ALLOCATION STRATEGY */
4974 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
4978 case 0x02: /* GET UMB LINK STATE */
4979 TRACE( "GET UMB LINK STATE\n" );
4980 SET_AL( context
, 0 ); /* UMBs not part of DOS memory chain */
4983 case 0x03: /* SET UMB LINK STATE */
4984 TRACE( "SET UMB LINK STATE to %d - ignored\n",
4989 INT_BARF( context
, 0x21 );
4993 case 0x59: /* GET EXTENDED ERROR INFO */
4994 INT21_GetExtendedError( context
);
4997 case 0x5a: /* CREATE TEMPORARY FILE */
4998 TRACE("CREATE TEMPORARY FILE\n");
4999 bSetDOSExtendedError
= !INT21_CreateTempFile(context
);
5002 case 0x5b: /* CREATE NEW FILE */
5003 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
5004 OF_READWRITE
| OF_SHARE_COMPAT
, 0x10 ))
5005 bSetDOSExtendedError
= TRUE
;
5007 RESET_CFLAG(context
);
5010 case 0x5c: /* "FLOCK" - RECORD LOCKING */
5012 DWORD offset
= MAKELONG(DX_reg(context
), CX_reg(context
));
5013 DWORD length
= MAKELONG(DI_reg(context
), SI_reg(context
));
5014 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
5016 RESET_CFLAG(context
);
5017 switch (AL_reg(context
))
5019 case 0x00: /* LOCK */
5020 TRACE( "lock handle %d offset %ld length %ld\n",
5021 BX_reg(context
), offset
, length
);
5022 if (!LockFile( handle
, offset
, 0, length
, 0 ))
5023 bSetDOSExtendedError
= TRUE
;
5026 case 0x01: /* UNLOCK */
5027 TRACE( "unlock handle %d offset %ld length %ld\n",
5028 BX_reg(context
), offset
, length
);
5029 if (!UnlockFile( handle
, offset
, 0, length
, 0 ))
5030 bSetDOSExtendedError
= TRUE
;
5034 INT_BARF( context
, 0x21 );
5039 case 0x5d: /* NETWORK 5D */
5040 FIXME( "Network function 5D not implemented.\n" );
5041 SetLastError( ER_NoNetwork
);
5042 bSetDOSExtendedError
= TRUE
;
5045 case 0x5e: /* NETWORK 5E */
5046 bSetDOSExtendedError
= INT21_NetworkFunc( context
);
5049 case 0x5f: /* NETWORK 5F */
5050 /* FIXME: supporting this would need to 1:
5051 * - implement per drive current directory (as kernel32 doesn't)
5052 * - assign enabled/disabled flag on a per drive basis
5054 /* network software not installed */
5055 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context
));
5056 SetLastError( ER_NoNetwork
);
5057 bSetDOSExtendedError
= TRUE
;
5060 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
5062 WCHAR pathW
[MAX_PATH
], res
[MAX_PATH
];
5063 /* FIXME: likely to be broken */
5065 TRACE("TRUENAME %s\n",
5066 (LPCSTR
)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Esi
));
5067 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
5068 if (!GetFullPathNameW( pathW
, 128, res
, NULL
))
5069 bSetDOSExtendedError
= TRUE
;
5072 SET_AX( context
, 0 );
5073 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
5074 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
5080 case 0x61: /* UNUSED */
5081 SET_AL( context
, 0 );
5084 case 0x62: /* GET PSP ADDRESS */
5085 INT21_GetPSP( context
);
5088 case 0x63: /* MISC. LANGUAGE SUPPORT */
5089 switch (AL_reg(context
)) {
5090 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
5091 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
5092 context
->SegDs
= INT21_GetHeapSelector( context
);
5093 SET_SI( context
, offsetof(INT21_HEAP
, dbcs_table
) );
5094 SET_AL( context
, 0 ); /* success */
5099 case 0x64: /* OS/2 DOS BOX */
5100 INT_BARF( context
, 0x21 );
5104 case 0x65: /* EXTENDED COUNTRY INFORMATION */
5105 INT21_ExtendedCountryInformation( context
);
5108 case 0x66: /* GLOBAL CODE PAGE TABLE */
5109 switch (AL_reg(context
))
5112 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
5113 SET_BX( context
, GetOEMCP() );
5114 SET_DX( context
, GetOEMCP() );
5117 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
5118 BX_reg(context
), DX_reg(context
) );
5123 case 0x67: /* SET HANDLE COUNT */
5124 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context
) );
5125 if (SetHandleCount( BX_reg(context
) ) < BX_reg(context
) )
5126 bSetDOSExtendedError
= TRUE
;
5129 case 0x68: /* "FFLUSH" - COMMIT FILE */
5130 TRACE( "FFLUSH - handle %d\n", BX_reg(context
) );
5131 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context
)) ))
5132 bSetDOSExtendedError
= TRUE
;
5135 case 0x69: /* DISK SERIAL NUMBER */
5136 switch (AL_reg(context
))
5139 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
5140 INT21_DriveName(BL_reg(context
)));
5141 if (!INT21_GetDiskSerialNumber(context
)) bSetDOSExtendedError
= TRUE
;
5142 else SET_AX( context
, 0 );
5146 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
5147 INT21_DriveName(BL_reg(context
)));
5148 if (!INT21_SetDiskSerialNumber(context
)) bSetDOSExtendedError
= TRUE
;
5149 else SET_AX( context
, 1 );
5154 case 0x6a: /* COMMIT FILE */
5155 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context
) );
5156 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context
)) ))
5157 bSetDOSExtendedError
= TRUE
;
5160 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
5161 SET_AL( context
, 0 );
5164 case 0x6c: /* EXTENDED OPEN/CREATE */
5165 if (!INT21_CreateFile( context
, context
->Esi
, TRUE
,
5166 BX_reg(context
), DL_reg(context
) ))
5167 bSetDOSExtendedError
= TRUE
;
5170 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
5171 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
5172 SET_CFLAG( context
);
5173 SET_AL( context
, 0 );
5176 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
5177 INT21_LongFilename( context
);
5180 case 0x73: /* MSDOS7 - FAT32 */
5181 RESET_CFLAG( context
);
5182 if (!INT21_Fat32( context
))
5183 bSetDOSExtendedError
= TRUE
;
5186 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
5187 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
5190 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
5191 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
5194 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
5195 /* we don't implement a DOS32 extender */
5196 TRACE( "DOS32 EXTENDER API - ignored\n" );
5200 INT_BARF( context
, 0x21 );
5203 } /* END OF SWITCH */
5205 /* Set general error condition. */
5206 if (bSetDOSExtendedError
)
5208 SET_AX( context
, GetLastError() );
5209 SET_CFLAG( context
);
5212 /* Print error code if carry flag is set. */
5213 if (context
->EFlags
& 0x0001)
5214 TRACE("failed, error %ld\n", GetLastError() );
5216 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
5217 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
5218 AX_reg(context
), BX_reg(context
),
5219 CX_reg(context
), DX_reg(context
),
5220 SI_reg(context
), DI_reg(context
),
5221 (WORD
)context
->SegDs
, (WORD
)context
->SegEs
,