2 * DOS interrupt 21h handler
4 * Copyright 1993, 1994 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1997 Andreas Mohr
7 * Copyright 1998 Uwe Bonnes
8 * Copyright 1998, 1999 Ove Kaaven
9 * Copyright 2003 Thomas Mertes
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/port.h"
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
42 #include "wine/winbase16.h"
43 #include "kernel16_private.h"
47 #include "wine/unicode.h"
48 #include "wine/server.h"
49 #include "wine/debug.h"
50 #include "wine/exception.h"
52 BOOL WINAPI
VerifyConsoleIoHandle(HANDLE
);
55 * - Most of the file related functions are wrong. NT's kernel32
56 * doesn't maintain a per drive current directory, while DOS does.
57 * We should in fact keep track in here of those per drive
58 * directories, and use this info while dealing with partial paths
59 * (drive defined, but only relative paths). This could even be
60 * created as an array of CDS (there should be an entry for that in
65 * Forward declarations.
67 static BOOL
INT21_RenameFile( CONTEXT
*context
);
69 WINE_DEFAULT_DEBUG_CHANNEL(int21
);
75 * Extended Drive Parameter Block.
76 * This structure is compatible with standard DOS4+ DPB and
79 typedef struct _INT21_DPB
{
80 BYTE drive
; /* 00 drive number (0=A, ...) */
81 BYTE unit
; /* 01 unit number within device driver */
82 WORD sector_bytes
; /* 02 bytes per sector */
83 BYTE cluster_sectors
; /* 04 highest sector number within a cluster */
84 BYTE shift
; /* 05 shift count to convert clusters into sectors */
85 WORD num_reserved
; /* 06 reserved sectors at beginning of drive */
86 BYTE num_FAT
; /* 08 number of FATs */
87 WORD num_root_entries
; /* 09 number of root directory entries */
88 WORD first_data_sector
; /* 0b number of first sector containing user data */
89 WORD num_clusters1
; /* 0d highest cluster number (number of data clusters + 1) */
90 WORD sectors_per_FAT
; /* 0f number of sectors per FAT */
91 WORD first_dir_sector
; /* 11 sector number of first directory sector */
92 SEGPTR driver_header
; /* 13 address of device driver header */
93 BYTE media_ID
; /* 17 media ID byte */
94 BYTE access_flag
; /* 18 0x00 if disk accessed, 0xff if not */
95 SEGPTR next
; /* 19 pointer to next DPB */
96 WORD search_cluster1
; /* 1d cluster at which to start search for free space */
97 WORD free_clusters_lo
; /* 1f number of free clusters on drive or 0xffff if unknown */
98 WORD free_clusters_hi
; /* 21 hiword of clusters_free */
99 WORD mirroring_flags
; /* 23 active FAT/mirroring flags */
100 WORD info_sector
; /* 25 sector number of file system info sector or 0xffff for none */
101 WORD spare_boot_sector
; /* 27 sector number of backup boot sector or 0xffff for none */
102 DWORD first_cluster_sector
; /* 29 sector number of the first cluster */
103 DWORD num_clusters2
; /* 2d maximum cluster number */
104 DWORD fat_clusters
; /* 31 number of clusters occupied by FAT */
105 DWORD root_cluster
; /* 35 cluster number of start of root directory */
106 DWORD search_cluster2
; /* 39 cluster at which to start searching for free space */
111 * Structure for DOS data that can be accessed directly from applications.
112 * Real and protected mode pointers will be returned to this structure so
113 * the structure must be correctly packed.
115 typedef struct _INT21_HEAP
{
116 WORD uppercase_size
; /* Size of the following table in bytes */
117 BYTE uppercase_table
[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
119 WORD lowercase_size
; /* Size of the following table in bytes */
120 BYTE lowercase_table
[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
122 WORD collating_size
; /* Size of the following table in bytes */
123 BYTE collating_table
[256]; /* Values used to sort characters from 0x00 to 0xff. */
125 WORD filename_size
; /* Size of the following filename data in bytes */
126 BYTE filename_reserved1
; /* 0x01 for MS-DOS 3.30-6.00 */
127 BYTE filename_lowest
; /* Lowest permissible character value for filename */
128 BYTE filename_highest
; /* Highest permissible character value for filename */
129 BYTE filename_reserved2
; /* 0x00 for MS-DOS 3.30-6.00 */
130 BYTE filename_exclude_first
; /* First illegal character in permissible range */
131 BYTE filename_exclude_last
; /* Last illegal character in permissible range */
132 BYTE filename_reserved3
; /* 0x02 for MS-DOS 3.30-6.00 */
133 BYTE filename_illegal_size
; /* Number of terminators in the following table */
134 BYTE filename_illegal_table
[16]; /* Characters which terminate a filename */
136 WORD dbcs_size
; /* Number of valid ranges in the following table */
137 BYTE dbcs_table
[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
139 BYTE misc_indos
; /* Interrupt 21 nesting flag */
140 WORD misc_selector
; /* Protected mode selector for INT21_HEAP */
141 INT21_DPB misc_dpb_list
[MAX_DOS_DRIVES
]; /* Drive parameter blocks for all drives */
149 CHAR file_extension
[3];
150 WORD current_block_number
;
151 WORD logical_record_size
;
153 WORD date_of_last_write
;
154 WORD time_of_last_write
;
157 WORD starting_cluster
;
158 WORD sequence_number
;
159 BYTE file_attributes
;
161 BYTE record_within_current_block
;
162 BYTE random_access_record_number
[4];
169 BYTE xfcb_file_attribute
;
173 /* DTA layout for FindFirst/FindNext */
176 BYTE drive
; /* 00 drive letter */
177 char mask
[11]; /* 01 search template */
178 BYTE search_attr
; /* 0c search attributes */
179 WORD count
; /* 0d entry count within directory */
180 WORD cluster
; /* 0f cluster of parent directory */
181 WCHAR
*fullPath
; /* 11 full path (was: reserved) */
182 BYTE fileattr
; /* 15 file attributes */
183 WORD filetime
; /* 16 file time */
184 WORD filedate
; /* 18 file date */
185 DWORD filesize
; /* 1a file size */
186 char filename
[13]; /* 1e file name + extension */
189 /* FCB layout for FindFirstFCB/FindNextFCB */
192 BYTE drive
; /* 00 drive letter */
193 char filename
[11]; /* 01 filename 8+3 format */
194 int count
; /* 0c entry count (was: reserved) */
195 WCHAR
*fullPath
; /* 10 full path (was: reserved) */
198 /* DOS directory entry for FindFirstFCB/FindNextFCB */
201 char filename
[11]; /* 00 filename 8+3 format */
202 BYTE fileattr
; /* 0b file attributes */
203 BYTE reserved
[10]; /* 0c reserved */
204 WORD filetime
; /* 16 file time */
205 WORD filedate
; /* 18 file date */
206 WORD cluster
; /* 1a file first cluster */
207 DWORD filesize
; /* 1c file size */
208 } DOS_DIRENTRY_LAYOUT
;
212 /* dos file attributes */
213 #define FA_NORMAL 0x00 /* Normal file, no attributes */
214 #define FA_RDONLY 0x01 /* Read only attribute */
215 #define FA_HIDDEN 0x02 /* Hidden file */
216 #define FA_SYSTEM 0x04 /* System file */
217 #define FA_LABEL 0x08 /* Volume label */
218 #define FA_DIRECTORY 0x10 /* Directory */
219 #define FA_ARCHIVE 0x20 /* Archive */
220 #define FA_UNUSED 0x40 /* Unused */
223 #define ER_NoNetwork 0x49
226 #define EC_OutOfResource 0x01
227 #define EC_Temporary 0x02
228 #define EC_AccessDenied 0x03
229 #define EC_InternalError 0x04
230 #define EC_HardwareFailure 0x05
231 #define EC_SystemFailure 0x06
232 #define EC_ProgramError 0x07
233 #define EC_NotFound 0x08
234 #define EC_MediaError 0x0b
235 #define EC_Exists 0x0c
236 #define EC_Unknown 0x0d
238 /* Suggested actions */
239 #define SA_Retry 0x01
240 #define SA_DelayedRetry 0x02
241 #define SA_Abort 0x04
242 #define SA_Ignore 0x06
243 #define SA_Ask4Retry 0x07
246 #define EL_Unknown 0x01
248 #define EL_Network 0x03
249 #define EL_Serial 0x04
250 #define EL_Memory 0x05
252 /* BIOS Keyboard Scancodes */
253 #define KEY_LEFT 0x4B
254 #define KEY_RIGHT 0x4D
256 #define KEY_DOWN 0x50
257 #define KEY_IC 0x52 /* insert char */
258 #define KEY_DC 0x53 /* delete char */
259 #define KEY_BACKSPACE 0x0E
260 #define KEY_HOME 0x47
262 #define KEY_NPAGE 0x49
263 #define KEY_PPAGE 0x51
272 void (*ioctl_handler
)(CONTEXT
*);
275 static void INT21_IoctlScsiMgrHandler( CONTEXT
* );
276 static void INT21_IoctlHPScanHandler( CONTEXT
* );
278 static struct magic_device magic_devices
[] =
280 { {'s','c','s','i','m','g','r','$',0}, NULL
, { { 0, 0 } }, INT21_IoctlScsiMgrHandler
},
281 { {'e','m','m','x','x','x','x','0',0}, NULL
, { { 0, 0 } }, EMS_Ioctl_Handler
},
282 { {'h','p','s','c','a','n',0}, NULL
, { { 0, 0 } }, INT21_IoctlHPScanHandler
},
285 #define NB_MAGIC_DEVICES (sizeof(magic_devices)/sizeof(magic_devices[0]))
288 /* Many calls translate a drive argument like this:
289 drive number (00h = default, 01h = A:, etc)
291 /******************************************************************
294 * Many calls translate a drive argument like this:
295 * drive number (00h = default, 01h = A:, etc)
297 static const char *INT21_DriveName(int drive
)
301 if (drive
<= 26) return wine_dbg_sprintf("%c:", 'A' + drive
- 1);
302 else return wine_dbg_sprintf( "<Bad drive: %d>", drive
);
307 /***********************************************************************
308 * INT21_GetCurrentDrive
310 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
311 * MAX_DOS_DRIVES on error.
313 static BYTE
INT21_GetCurrentDrive(void)
315 WCHAR current_directory
[MAX_PATH
];
317 if (!GetCurrentDirectoryW( MAX_PATH
, current_directory
) ||
318 current_directory
[1] != ':')
320 TRACE( "Failed to get current drive.\n" );
321 return MAX_DOS_DRIVES
;
324 return toupperW( current_directory
[0] ) - 'A';
328 /***********************************************************************
331 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
332 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
334 static BYTE
INT21_MapDrive( BYTE drive
)
338 WCHAR drivespec
[] = {'A', ':', 0};
341 drivespec
[0] += drive
- 1;
342 drivetype
= GetDriveTypeW( drivespec
);
344 if (drivetype
== DRIVE_UNKNOWN
|| drivetype
== DRIVE_NO_ROOT_DIR
)
345 return MAX_DOS_DRIVES
;
350 return INT21_GetCurrentDrive();
354 /***********************************************************************
355 * INT21_SetCurrentDrive
357 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
359 static void INT21_SetCurrentDrive( BYTE drive
)
361 WCHAR drivespec
[] = {'A', ':', 0};
363 drivespec
[0] += drive
;
365 if (!SetCurrentDirectoryW( drivespec
))
366 TRACE( "Failed to set current drive.\n" );
370 /***********************************************************************
371 * INT21_GetSystemCountryCode
373 * Return DOS country code for default system locale.
375 static WORD
INT21_GetSystemCountryCode( void )
377 return GetSystemDefaultLangID();
381 /***********************************************************************
382 * INT21_FillCountryInformation
384 * Fill 34-byte buffer with country information data using
385 * default system locale.
387 static void INT21_FillCountryInformation( BYTE
*buffer
)
389 /* 00 - WORD: date format
394 *(WORD
*)(buffer
+ 0) = 0; /* FIXME: Get from locale */
396 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
397 buffer
[2] = '$'; /* FIXME: Get from locale */
400 /* 07 - BYTE[2]: ASCIIZ thousands separator */
401 buffer
[7] = 0; /* FIXME: Get from locale */
404 /* 09 - BYTE[2]: ASCIIZ decimal separator */
405 buffer
[9] = '.'; /* FIXME: Get from locale */
408 /* 11 - BYTE[2]: ASCIIZ date separator */
409 buffer
[11] = '/'; /* FIXME: Get from locale */
412 /* 13 - BYTE[2]: ASCIIZ time separator */
413 buffer
[13] = ':'; /* FIXME: Get from locale */
416 /* 15 - BYTE: Currency format
417 * bit 2 = set if currency symbol replaces decimal point
418 * bit 1 = number of spaces between value and currency symbol
419 * bit 0 = 0 if currency symbol precedes value
420 * 1 if currency symbol follows value
422 buffer
[15] = 0; /* FIXME: Get from locale */
424 /* 16 - BYTE: Number of digits after decimal in currency */
425 buffer
[16] = 0; /* FIXME: Get from locale */
427 /* 17 - BYTE: Time format
428 * bit 0 = 0 if 12-hour clock
431 buffer
[17] = 1; /* FIXME: Get from locale */
433 /* 18 - DWORD: Address of case map routine */
434 *(DWORD
*)(buffer
+ 18) = 0; /* FIXME: ptr to case map routine */
436 /* 22 - BYTE[2]: ASCIIZ data-list separator */
437 buffer
[22] = ','; /* FIXME: Get from locale */
440 /* 24 - BYTE[10]: Reserved */
441 memset( buffer
+ 24, 0, 10 );
445 /***********************************************************************
448 * Initialize DOS heap.
450 * Filename Terminator Table of w2k DE NTVDM:
451 * 16 00 01 00 FF 00 00 20-02 0E 2E 22 2F 5C 5B 5D ....... ..."/\[]
452 * 3A 7C 3C 3E 2B 3D 3B 2C-00 :|<>+=;,
454 static void INT21_FillHeap( INT21_HEAP
*heap
)
456 static const char terminators
[] = ".\"/\\[]:|<>+=;,";
462 heap
->uppercase_size
= 128;
463 for (i
= 0; i
< 128; i
++)
464 heap
->uppercase_table
[i
] = toupper( 128 + i
);
469 heap
->lowercase_size
= 256;
470 for (i
= 0; i
< 256; i
++)
471 heap
->lowercase_table
[i
] = tolower( i
);
476 heap
->collating_size
= 256;
477 for (i
= 0; i
< 256; i
++)
478 heap
->collating_table
[i
] = i
;
483 heap
->filename_size
= 8 + strlen(terminators
);
484 heap
->filename_illegal_size
= strlen(terminators
);
485 memcpy( heap
->filename_illegal_table
, terminators
, heap
->filename_illegal_size
);
487 heap
->filename_reserved1
= 0x01;
488 heap
->filename_lowest
= 0x00;
489 heap
->filename_highest
= 0xff;
490 heap
->filename_reserved2
= 0x00;
491 heap
->filename_exclude_first
= 0x00;
492 heap
->filename_exclude_last
= 0x20;
493 heap
->filename_reserved3
= 0x02;
496 * DBCS lead byte table. This table is empty.
499 memset( heap
->dbcs_table
, 0, sizeof(heap
->dbcs_table
) );
502 * Initialize InDos flag.
504 heap
->misc_indos
= 0;
507 * FIXME: Should drive parameter blocks (DPB) be
508 * initialized here and linked to DOS LOL?
513 /***********************************************************************
514 * INT21_GetHeapPointer
516 * Get pointer for DOS heap (INT21_HEAP).
517 * Creates and initializes heap on first call.
519 static INT21_HEAP
*INT21_GetHeapPointer( void )
521 static INT21_HEAP
*heap_pointer
= NULL
;
527 heap_pointer
= DOSVM_AllocDataUMB( sizeof(INT21_HEAP
), &heap_selector
);
528 heap_pointer
->misc_selector
= heap_selector
;
529 INT21_FillHeap( heap_pointer
);
536 /***********************************************************************
537 * INT21_GetHeapSelector
539 * Get segment/selector for DOS heap (INT21_HEAP).
540 * Creates and initializes heap on first call.
542 static WORD
INT21_GetHeapSelector( CONTEXT
*context
)
544 INT21_HEAP
*heap
= INT21_GetHeapPointer();
545 return heap
->misc_selector
;
549 /***********************************************************************
552 * Fill DOS heap drive parameter block for the specified drive.
553 * Return TRUE if drive was valid and there were
554 * no errors while reading drive information.
556 static BOOL
INT21_FillDrivePB( BYTE drive
)
558 WCHAR drivespec
[] = {'A', ':', 0};
559 INT21_HEAP
*heap
= INT21_GetHeapPointer();
562 DWORD cluster_sectors
;
565 DWORD total_clusters
;
567 if (drive
>= MAX_DOS_DRIVES
)
570 dpb
= &heap
->misc_dpb_list
[drive
];
571 drivespec
[0] += drive
;
572 drivetype
= GetDriveTypeW( drivespec
);
575 * FIXME: Does this check work correctly with floppy/cdrom drives?
577 if (drivetype
== DRIVE_NO_ROOT_DIR
|| drivetype
== DRIVE_UNKNOWN
)
581 * FIXME: Does this check work correctly with floppy/cdrom drives?
583 if (!GetDiskFreeSpaceW( drivespec
, &cluster_sectors
, §or_bytes
,
584 &free_clusters
, &total_clusters
))
588 * FIXME: Most of the values listed below are incorrect.
589 * All values should be validated.
594 dpb
->sector_bytes
= sector_bytes
;
595 dpb
->cluster_sectors
= cluster_sectors
- 1;
598 while (cluster_sectors
> 1)
600 cluster_sectors
/= 2;
604 dpb
->num_reserved
= 0;
606 dpb
->num_root_entries
= 2;
607 dpb
->first_data_sector
= 2;
608 dpb
->num_clusters1
= total_clusters
;
609 dpb
->sectors_per_FAT
= 1;
610 dpb
->first_dir_sector
= 1;
611 dpb
->driver_header
= 0;
612 dpb
->media_ID
= (drivetype
== DRIVE_FIXED
) ? 0xF8 : 0xF0;
613 dpb
->access_flag
= 0;
615 dpb
->search_cluster1
= 0;
616 dpb
->free_clusters_lo
= LOWORD(free_clusters
);
617 dpb
->free_clusters_hi
= HIWORD(free_clusters
);
618 dpb
->mirroring_flags
= 0;
619 dpb
->info_sector
= 0xffff;
620 dpb
->spare_boot_sector
= 0xffff;
621 dpb
->first_cluster_sector
= 0;
622 dpb
->num_clusters2
= total_clusters
;
623 dpb
->fat_clusters
= 32;
624 dpb
->root_cluster
= 0;
625 dpb
->search_cluster2
= 0;
631 /***********************************************************************
632 * INT21_GetCurrentDirectory
636 * - subfunction 0x47 of function 0x71
638 static BOOL
INT21_GetCurrentDirectory( CONTEXT
*context
, BOOL islong
)
640 char *buffer
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
);
641 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
642 WCHAR pathW
[MAX_PATH
];
643 char pathA
[MAX_PATH
];
646 TRACE( "drive %d\n", DL_reg(context
) );
648 if (drive
== MAX_DOS_DRIVES
)
650 SetLastError(ERROR_INVALID_DRIVE
);
655 * Grab current directory.
658 if (!GetCurrentDirectoryW( MAX_PATH
, pathW
)) return FALSE
;
660 if (toupperW(pathW
[0]) - 'A' != drive
|| pathW
[1] != ':')
662 /* cwd is not on the requested drive, get the environment string instead */
667 env_var
[1] = 'A' + drive
;
670 if (!GetEnvironmentVariableW( env_var
, pathW
, MAX_PATH
))
672 /* return empty path */
679 * Convert into short format.
684 DWORD result
= GetShortPathNameW( pathW
, pathW
, MAX_PATH
);
687 if (result
> MAX_PATH
)
689 WARN( "Short path too long!\n" );
690 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
696 * The returned pathname does not include
697 * the drive letter, colon or leading backslash.
703 * FIXME: We should probably just strip host part from name...
705 FIXME( "UNC names are not supported.\n" );
706 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
709 else if (!ptr
[0] || ptr
[1] != ':' || ptr
[2] != '\\')
711 WARN( "Path is neither UNC nor DOS path: %s\n",
712 wine_dbgstr_w(ptr
) );
713 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
718 /* Remove drive letter, colon and leading backslash. */
723 * Convert into OEM string.
726 if (!WideCharToMultiByte(CP_OEMCP
, 0, ptr
, -1, pathA
,
727 MAX_PATH
, NULL
, NULL
))
729 WARN( "Cannot convert path!\n" );
730 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
740 /* Undocumented success code. */
741 SET_AX( context
, 0x0100 );
743 /* Truncate buffer to 64 bytes. */
747 TRACE( "%c:=%s\n", 'A' + drive
, pathA
);
749 strcpy( buffer
, pathA
);
754 /***********************************************************************
755 * INT21_SetCurrentDirectory
759 * - subfunction 0x3b of function 0x71
761 static BOOL
INT21_SetCurrentDirectory( CONTEXT
*context
)
763 WCHAR dirW
[MAX_PATH
];
766 char *dirA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
767 BYTE drive
= INT21_GetCurrentDrive();
770 TRACE( "SET CURRENT DIRECTORY %s\n", dirA
);
772 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
773 if (!GetFullPathNameW( dirW
, MAX_PATH
, dirW
, NULL
)) return FALSE
;
775 attr
= GetFileAttributesW( dirW
);
776 if (attr
== INVALID_FILE_ATTRIBUTES
|| !(attr
& FILE_ATTRIBUTE_DIRECTORY
))
778 SetLastError( ERROR_PATH_NOT_FOUND
);
783 env_var
[1] = dirW
[0];
786 result
= SetEnvironmentVariableW( env_var
, dirW
);
788 /* only set current directory if on the current drive */
789 if (result
&& (toupperW(dirW
[0]) - 'A' == drive
)) result
= SetCurrentDirectoryW( dirW
);
794 /***********************************************************************
795 * INT21_CreateMagicDeviceHandle
797 * Create a dummy file handle for a "magic" device.
799 static HANDLE
INT21_CreateMagicDeviceHandle( LPCWSTR name
)
801 static const WCHAR prefixW
[] = {'\\','?','?','\\','u','n','i','x'};
802 const char *dir
= wine_get_server_dir();
806 OBJECT_ATTRIBUTES attr
;
807 UNICODE_STRING nameW
;
810 len
= MultiByteToWideChar( CP_UNIXCP
, 0, dir
, -1, NULL
, 0 );
811 nameW
.Length
= sizeof(prefixW
) + (len
+ strlenW( name
)) * sizeof(WCHAR
);
812 nameW
.MaximumLength
= nameW
.Length
+ sizeof(WCHAR
);
813 if (!(nameW
.Buffer
= HeapAlloc( GetProcessHeap(), 0, nameW
.MaximumLength
)))
815 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
818 memcpy( nameW
.Buffer
, prefixW
, sizeof(prefixW
) );
819 MultiByteToWideChar( CP_UNIXCP
, 0, dir
, -1, nameW
.Buffer
+ sizeof(prefixW
)/sizeof(WCHAR
), len
);
820 len
+= sizeof(prefixW
) / sizeof(WCHAR
);
821 nameW
.Buffer
[len
-1] = '/';
822 strcpyW( nameW
.Buffer
+ len
, name
);
824 attr
.Length
= sizeof(attr
);
825 attr
.RootDirectory
= 0;
827 attr
.ObjectName
= &nameW
;
828 attr
.SecurityDescriptor
= NULL
;
829 attr
.SecurityQualityOfService
= NULL
;
831 status
= NtCreateFile( &ret
, GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
, &attr
, &io
, NULL
, 0,
832 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN_IF
,
833 FILE_SYNCHRONOUS_IO_ALERT
, NULL
, 0 );
837 SetLastError( RtlNtStatusToDosError(status
) );
839 RtlFreeUnicodeString( &nameW
);
844 /***********************************************************************
845 * INT21_OpenMagicDevice
847 * Open a file handle for "magic" devices like EMMXXXX0.
849 static HANDLE
INT21_OpenMagicDevice( LPCWSTR name
, DWORD access
)
855 if (name
[0] && (name
[1] == ':')) name
+= 2;
856 if ((p
= strrchrW( name
, '/' ))) name
= p
+ 1;
857 if ((p
= strrchrW( name
, '\\' ))) name
= p
+ 1;
859 for (i
= 0; i
< NB_MAGIC_DEVICES
; i
++)
861 int len
= strlenW( magic_devices
[i
].name
);
862 if (!strncmpiW( magic_devices
[i
].name
, name
, len
) &&
863 (!name
[len
] || name
[len
] == '.' || name
[len
] == ':')) break;
865 if (i
== NB_MAGIC_DEVICES
) return 0;
867 if (!magic_devices
[i
].handle
) /* need to open it */
870 FILE_INTERNAL_INFORMATION info
;
872 if (!(handle
= INT21_CreateMagicDeviceHandle( magic_devices
[i
].name
))) return 0;
874 NtQueryInformationFile( handle
, &io
, &info
, sizeof(info
), FileInternalInformation
);
875 magic_devices
[i
].index
= info
.IndexNumber
;
876 magic_devices
[i
].handle
= handle
;
878 if (!DuplicateHandle( GetCurrentProcess(), magic_devices
[i
].handle
,
879 GetCurrentProcess(), &handle
, access
, FALSE
, 0 )) handle
= 0;
884 /***********************************************************************
892 * - subfunction 0x6c of function 0x71
894 static BOOL
INT21_CreateFile( CONTEXT
*context
,
901 char *pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, pathSegOff
);
902 WCHAR pathW
[MAX_PATH
];
909 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
910 "create flags=%04x, file=%s.\n",
911 AH_reg(context
), dosAction
, dosAccessShare
, CX_reg(context
), pathA
);
914 * Application tried to create/open a file whose name
915 * ends with a backslash. This is not allowed.
917 * FIXME: This needs to be validated, especially the return value.
919 if (pathA
[strlen(pathA
) - 1] == '/')
921 SetLastError( ERROR_FILE_NOT_FOUND
);
926 * Convert DOS action flags into Win32 creation disposition parameter.
931 winMode
= OPEN_EXISTING
;
934 winMode
= TRUNCATE_EXISTING
;
937 winMode
= CREATE_NEW
;
940 winMode
= OPEN_ALWAYS
;
943 winMode
= CREATE_ALWAYS
;
946 SetLastError( ERROR_INVALID_PARAMETER
);
951 * Convert DOS access/share flags into Win32 desired access parameter.
953 switch(dosAccessShare
& 0x07)
956 winAccess
= GENERIC_READ
;
959 winAccess
= GENERIC_WRITE
;
962 winAccess
= GENERIC_READ
| GENERIC_WRITE
;
966 * Read-only, do not modify file's last-access time (DOS7).
968 * FIXME: How to prevent modification of last-access time?
970 winAccess
= GENERIC_READ
;
977 * Convert DOS access/share flags into Win32 share mode parameter.
979 switch(dosAccessShare
& 0x70)
981 case OF_SHARE_EXCLUSIVE
:
984 case OF_SHARE_DENY_WRITE
:
985 winSharing
= FILE_SHARE_READ
;
987 case OF_SHARE_DENY_READ
:
988 winSharing
= FILE_SHARE_WRITE
;
990 case OF_SHARE_DENY_NONE
:
991 case OF_SHARE_COMPAT
:
993 winSharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
997 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
998 * What to do with this bit?
999 * FIXME: Bits in the high byte of dosAccessShare are not supported.
1000 * See both function 0x6c and subfunction 0x6c of function 0x71 for
1001 * definition of these bits.
1005 * Convert DOS create attributes into Win32 flags and attributes parameter.
1007 if (winMode
== OPEN_EXISTING
|| winMode
== TRUNCATE_EXISTING
)
1013 WORD dosAttributes
= CX_reg(context
);
1015 if (dosAttributes
& FA_LABEL
)
1018 * Application tried to create volume label entry.
1019 * This is difficult to support so we do not allow it.
1021 * FIXME: If volume does not already have a label,
1022 * this function is supposed to succeed.
1024 SetLastError( ERROR_ACCESS_DENIED
);
1028 winAttributes
= dosAttributes
&
1029 (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
|
1030 FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_ARCHIVE
);
1036 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
1038 if ((winHandle
= INT21_OpenMagicDevice( pathW
, winAccess
)))
1044 winHandle
= CreateFileW( pathW
, winAccess
, winSharing
, NULL
,
1045 winMode
, winAttributes
, 0 );
1046 /* DOS allows opening files on a CDROM R/W */
1047 if( winHandle
== INVALID_HANDLE_VALUE
&&
1048 (GetLastError() == ERROR_WRITE_PROTECT
||
1049 GetLastError() == ERROR_ACCESS_DENIED
)) {
1050 winHandle
= CreateFileW( pathW
, winAccess
& ~GENERIC_WRITE
,
1051 winSharing
, NULL
, winMode
, winAttributes
, 0 );
1054 if (winHandle
== INVALID_HANDLE_VALUE
)
1058 * Determine DOS file status.
1069 case TRUNCATE_EXISTING
:
1076 dosStatus
= (GetLastError() == ERROR_ALREADY_EXISTS
) ? 1 : 2;
1079 dosStatus
= (GetLastError() == ERROR_ALREADY_EXISTS
) ? 3 : 2;
1087 * Return DOS file handle and DOS status.
1089 SET_AX( context
, Win32HandleToDosFileHandle(winHandle
) );
1092 SET_CX( context
, dosStatus
);
1094 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
1095 AX_reg(context
), dosStatus
);
1101 /***********************************************************************
1102 * INT21_GetCurrentDTA
1104 static BYTE
*INT21_GetCurrentDTA( CONTEXT
*context
)
1106 TDB
*pTask
= GlobalLock16(GetCurrentTask());
1108 /* FIXME: This assumes DTA was set correctly! */
1109 return CTX_SEG_OFF_TO_LIN( context
, SELECTOROF(pTask
->dta
),
1110 OFFSETOF(pTask
->dta
) );
1114 /***********************************************************************
1115 * INT21_OpenFileUsingFCB
1117 * Handler for function 0x0f.
1120 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
1127 * Opens a FCB file for read/write in compatibility mode. Upon calling
1128 * the FCB must have the drive_number, file_name, and file_extension
1129 * fields filled and all other bytes cleared.
1131 static void INT21_OpenFileUsingFCB( CONTEXT
*context
)
1139 BY_HANDLE_FILE_INFORMATION info
;
1142 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1143 if (fcb
->drive_number
== 0xff) {
1144 xfcb
= (struct XFCB
*) fcb
;
1145 fcb
= (struct FCB
*) xfcb
->fcb
;
1149 file_path
[0] = 'A' + INT21_MapDrive( fcb
->drive_number
);
1151 if (AL_result
== 0) {
1153 pos
= &file_path
[2];
1154 memcpy(pos
, fcb
->file_name
, 8);
1157 pos
= strchr(pos
, ' ');
1160 memcpy(pos
, fcb
->file_extension
, 3);
1163 pos
= strchr(pos
, ' ');
1166 handle
= (HANDLE
) _lopen(file_path
, OF_READWRITE
);
1167 if (handle
== INVALID_HANDLE_VALUE
) {
1168 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path
);
1169 AL_result
= 0xff; /* failed */
1171 hfile16
= Win32HandleToDosFileHandle(handle
);
1172 if (hfile16
== HFILE_ERROR16
) {
1173 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle
);
1174 CloseHandle(handle
);
1175 AL_result
= 0xff; /* failed */
1176 } else if (hfile16
> 255) {
1177 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16
, file_path
);
1179 AL_result
= 0xff; /* failed */
1181 if (!GetFileInformationByHandle(handle
, &info
)) {
1182 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1183 hfile16
, handle
, file_path
);
1185 AL_result
= 0xff; /* failed */
1187 fcb
->drive_number
= file_path
[0] - 'A' + 1;
1188 fcb
->current_block_number
= 0;
1189 fcb
->logical_record_size
= 128;
1190 fcb
->file_size
= info
.nFileSizeLow
;
1191 FileTimeToDosDateTime(&info
.ftLastWriteTime
,
1192 &fcb
->date_of_last_write
, &fcb
->time_of_last_write
);
1193 fcb
->file_number
= hfile16
;
1194 fcb
->attributes
= 0xc2;
1195 fcb
->starting_cluster
= 0; /* don't know correct init value */
1196 fcb
->sequence_number
= 0; /* don't know correct init value */
1197 fcb
->file_attributes
= info
.dwFileAttributes
;
1198 /* The following fields are not initialized */
1199 /* by the native function: */
1201 /* record_within_current_block */
1202 /* random_access_record_number */
1204 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1205 file_path
, hfile16
, handle
);
1206 AL_result
= 0x00; /* successful */
1211 SET_AL(context
, AL_result
);
1215 /***********************************************************************
1216 * INT21_CloseFileUsingFCB
1218 * Handler for function 0x10.
1221 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1228 * Closes a FCB file.
1230 static void INT21_CloseFileUsingFCB( CONTEXT
*context
)
1236 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1237 if (fcb
->drive_number
== 0xff) {
1238 xfcb
= (struct XFCB
*) fcb
;
1239 fcb
= (struct FCB
*) xfcb
->fcb
;
1242 if (_lclose16((HFILE16
) fcb
->file_number
) != 0) {
1243 TRACE("_lclose16(%d) failed\n", fcb
->file_number
);
1244 AL_result
= 0xff; /* failed */
1246 TRACE("successful closed file %d\n", fcb
->file_number
);
1247 AL_result
= 0x00; /* successful */
1249 SET_AL(context
, AL_result
);
1253 /***********************************************************************
1254 * INT21_SequentialReadFromFCB
1256 * Handler for function 0x14.
1259 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1263 * 1: end of file, no data read
1264 * 2: segment wrap in DTA, no data read (not returned now)
1265 * 3: end of file, partial record read
1268 * Reads a record with the size FCB->logical_record_size from the FCB
1269 * to the disk transfer area. The position of the record is specified
1270 * with FCB->current_block_number and FCB->record_within_current_block.
1271 * Then FCB->current_block_number and FCB->record_within_current_block
1272 * are updated to point to the next record. If a partial record is
1273 * read, it is filled with zeros up to the FCB->logical_record_size.
1275 static void INT21_SequentialReadFromFCB( CONTEXT
*context
)
1280 DWORD record_number
;
1282 BYTE
*disk_transfer_area
;
1286 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1287 if (fcb
->drive_number
== 0xff) {
1288 xfcb
= (struct XFCB
*) fcb
;
1289 fcb
= (struct FCB
*) xfcb
->fcb
;
1292 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1293 if (handle
== INVALID_HANDLE_VALUE
) {
1294 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1296 AL_result
= 0x01; /* end of file, no data read */
1298 record_number
= 128 * fcb
->current_block_number
+ fcb
->record_within_current_block
;
1299 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1300 if (position
!= record_number
* fcb
->logical_record_size
) {
1301 TRACE("seek(%d, %d, 0) failed with %u\n",
1302 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1303 AL_result
= 0x01; /* end of file, no data read */
1305 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1306 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, fcb
->logical_record_size
);
1307 if (bytes_read
!= fcb
->logical_record_size
) {
1308 TRACE("_lread(%d, %p, %d) failed with %d\n",
1309 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_read
);
1310 if (bytes_read
== 0) {
1311 AL_result
= 0x01; /* end of file, no data read */
1313 memset(&disk_transfer_area
[bytes_read
], 0, fcb
->logical_record_size
- bytes_read
);
1314 AL_result
= 0x03; /* end of file, partial record read */
1317 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1318 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1319 AL_result
= 0x00; /* successful */
1323 if (AL_result
== 0x00 || AL_result
== 0x03) {
1324 if (fcb
->record_within_current_block
== 127) {
1325 fcb
->record_within_current_block
= 0;
1326 fcb
->current_block_number
++;
1328 fcb
->record_within_current_block
++;
1331 SET_AL(context
, AL_result
);
1335 /***********************************************************************
1336 * INT21_SequentialWriteToFCB
1338 * Handler for function 0x15.
1341 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1346 * 2: segment wrap in DTA (not returned now)
1349 * Writes a record with the size FCB->logical_record_size from the disk
1350 * transfer area to the FCB. The position of the record is specified
1351 * with FCB->current_block_number and FCB->record_within_current_block.
1352 * Then FCB->current_block_number and FCB->record_within_current_block
1353 * are updated to point to the next record.
1355 static void INT21_SequentialWriteToFCB( CONTEXT
*context
)
1360 DWORD record_number
;
1362 BYTE
*disk_transfer_area
;
1366 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1367 if (fcb
->drive_number
== 0xff) {
1368 xfcb
= (struct XFCB
*) fcb
;
1369 fcb
= (struct FCB
*) xfcb
->fcb
;
1372 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1373 if (handle
== INVALID_HANDLE_VALUE
) {
1374 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1376 AL_result
= 0x01; /* disk full */
1378 record_number
= 128 * fcb
->current_block_number
+ fcb
->record_within_current_block
;
1379 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1380 if (position
!= record_number
* fcb
->logical_record_size
) {
1381 TRACE("seek(%d, %d, 0) failed with %u\n",
1382 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1383 AL_result
= 0x01; /* disk full */
1385 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1386 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, fcb
->logical_record_size
);
1387 if (bytes_written
!= fcb
->logical_record_size
) {
1388 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1389 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_written
);
1390 AL_result
= 0x01; /* disk full */
1392 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1393 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1394 AL_result
= 0x00; /* successful */
1398 if (AL_result
== 0x00) {
1399 if (fcb
->record_within_current_block
== 127) {
1400 fcb
->record_within_current_block
= 0;
1401 fcb
->current_block_number
++;
1403 fcb
->record_within_current_block
++;
1406 SET_AL(context
, AL_result
);
1410 /***********************************************************************
1411 * INT21_ReadRandomRecordFromFCB
1413 * Handler for function 0x21.
1416 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1420 * 1: end of file, no data read
1421 * 2: segment wrap in DTA, no data read (not returned now)
1422 * 3: end of file, partial record read
1425 * Reads a record with the size FCB->logical_record_size from
1426 * the FCB to the disk transfer area. The position of the record
1427 * is specified with FCB->random_access_record_number. The
1428 * FCB->random_access_record_number is not updated. If a partial record
1429 * is read, it is filled with zeros up to the FCB->logical_record_size.
1431 static void INT21_ReadRandomRecordFromFCB( CONTEXT
*context
)
1436 DWORD record_number
;
1438 BYTE
*disk_transfer_area
;
1442 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1443 if (fcb
->drive_number
== 0xff) {
1444 xfcb
= (struct XFCB
*) fcb
;
1445 fcb
= (struct FCB
*) xfcb
->fcb
;
1448 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1449 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1450 if (handle
== INVALID_HANDLE_VALUE
) {
1451 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1453 AL_result
= 0x01; /* end of file, no data read */
1455 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1456 if (position
!= record_number
* fcb
->logical_record_size
) {
1457 TRACE("seek(%d, %d, 0) failed with %u\n",
1458 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1459 AL_result
= 0x01; /* end of file, no data read */
1461 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1462 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, fcb
->logical_record_size
);
1463 if (bytes_read
!= fcb
->logical_record_size
) {
1464 TRACE("_lread(%d, %p, %d) failed with %d\n",
1465 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_read
);
1466 if (bytes_read
== 0) {
1467 AL_result
= 0x01; /* end of file, no data read */
1469 memset(&disk_transfer_area
[bytes_read
], 0, fcb
->logical_record_size
- bytes_read
);
1470 AL_result
= 0x03; /* end of file, partial record read */
1473 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1474 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1475 AL_result
= 0x00; /* successful */
1479 fcb
->current_block_number
= record_number
/ 128;
1480 fcb
->record_within_current_block
= record_number
% 128;
1481 SET_AL(context
, AL_result
);
1485 /***********************************************************************
1486 * INT21_WriteRandomRecordToFCB
1488 * Handler for function 0x22.
1491 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1496 * 2: segment wrap in DTA (not returned now)
1499 * Writes a record with the size FCB->logical_record_size from
1500 * the disk transfer area to the FCB. The position of the record
1501 * is specified with FCB->random_access_record_number. The
1502 * FCB->random_access_record_number is not updated.
1504 static void INT21_WriteRandomRecordToFCB( CONTEXT
*context
)
1509 DWORD record_number
;
1511 BYTE
*disk_transfer_area
;
1515 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1516 if (fcb
->drive_number
== 0xff) {
1517 xfcb
= (struct XFCB
*) fcb
;
1518 fcb
= (struct FCB
*) xfcb
->fcb
;
1521 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1522 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1523 if (handle
== INVALID_HANDLE_VALUE
) {
1524 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1526 AL_result
= 0x01; /* disk full */
1528 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1529 if (position
!= record_number
* fcb
->logical_record_size
) {
1530 TRACE("seek(%d, %d, 0) failed with %u\n",
1531 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1532 AL_result
= 0x01; /* disk full */
1534 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1535 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, fcb
->logical_record_size
);
1536 if (bytes_written
!= fcb
->logical_record_size
) {
1537 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1538 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_written
);
1539 AL_result
= 0x01; /* disk full */
1541 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1542 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1543 AL_result
= 0x00; /* successful */
1547 fcb
->current_block_number
= record_number
/ 128;
1548 fcb
->record_within_current_block
= record_number
% 128;
1549 SET_AL(context
, AL_result
);
1553 /***********************************************************************
1554 * INT21_RandomBlockReadFromFCB
1556 * Handler for function 0x27.
1559 * CX [I/O] Number of records to read
1560 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1564 * 1: end of file, no data read
1565 * 2: segment wrap in DTA, no data read (not returned now)
1566 * 3: end of file, partial record read
1569 * Reads several records with the size FCB->logical_record_size from
1570 * the FCB to the disk transfer area. The number of records to be
1571 * read is specified in the CX register. The position of the first
1572 * record is specified with FCB->random_access_record_number. The
1573 * FCB->random_access_record_number, the FCB->current_block_number
1574 * and FCB->record_within_current_block are updated to point to the
1575 * next record after the records read. If a partial record is read,
1576 * it is filled with zeros up to the FCB->logical_record_size. The
1577 * CX register is set to the number of successfully read records.
1579 static void INT21_RandomBlockReadFromFCB( CONTEXT
*context
)
1584 DWORD record_number
;
1586 BYTE
*disk_transfer_area
;
1587 UINT records_requested
;
1588 UINT bytes_requested
;
1593 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1594 if (fcb
->drive_number
== 0xff) {
1595 xfcb
= (struct XFCB
*) fcb
;
1596 fcb
= (struct FCB
*) xfcb
->fcb
;
1599 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1600 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1601 if (handle
== INVALID_HANDLE_VALUE
) {
1602 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1605 AL_result
= 0x01; /* end of file, no data read */
1607 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1608 if (position
!= record_number
* fcb
->logical_record_size
) {
1609 TRACE("seek(%d, %d, 0) failed with %u\n",
1610 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1612 AL_result
= 0x01; /* end of file, no data read */
1614 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1615 records_requested
= CX_reg(context
);
1616 bytes_requested
= records_requested
* fcb
->logical_record_size
;
1617 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, bytes_requested
);
1618 if (bytes_read
!= bytes_requested
) {
1619 TRACE("_lread(%d, %p, %d) failed with %d\n",
1620 fcb
->file_number
, disk_transfer_area
, bytes_requested
, bytes_read
);
1621 records_read
= bytes_read
/ fcb
->logical_record_size
;
1622 if (bytes_read
% fcb
->logical_record_size
== 0) {
1623 AL_result
= 0x01; /* end of file, no data read */
1626 memset(&disk_transfer_area
[bytes_read
], 0, records_read
* fcb
->logical_record_size
- bytes_read
);
1627 AL_result
= 0x03; /* end of file, partial record read */
1630 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1631 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1632 records_read
= records_requested
;
1633 AL_result
= 0x00; /* successful */
1637 record_number
+= records_read
;
1638 memcpy(fcb
->random_access_record_number
, &record_number
, 4);
1639 fcb
->current_block_number
= record_number
/ 128;
1640 fcb
->record_within_current_block
= record_number
% 128;
1641 SET_CX(context
, records_read
);
1642 SET_AL(context
, AL_result
);
1646 /***********************************************************************
1647 * INT21_RandomBlockWriteToFCB
1649 * Handler for function 0x28.
1652 * CX [I/O] Number of records to write
1653 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1658 * 2: segment wrap in DTA (not returned now)
1661 * Writes several records with the size FCB->logical_record_size from
1662 * the disk transfer area to the FCB. The number of records to be
1663 * written is specified in the CX register. The position of the first
1664 * record is specified with FCB->random_access_record_number. The
1665 * FCB->random_access_record_number, the FCB->current_block_number
1666 * and FCB->record_within_current_block are updated to point to the
1667 * next record after the records written. The CX register is set to
1668 * the number of successfully written records.
1670 static void INT21_RandomBlockWriteToFCB( CONTEXT
*context
)
1675 DWORD record_number
;
1677 BYTE
*disk_transfer_area
;
1678 UINT records_requested
;
1679 UINT bytes_requested
;
1681 UINT records_written
;
1684 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1685 if (fcb
->drive_number
== 0xff) {
1686 xfcb
= (struct XFCB
*) fcb
;
1687 fcb
= (struct FCB
*) xfcb
->fcb
;
1690 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1691 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1692 if (handle
== INVALID_HANDLE_VALUE
) {
1693 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1695 records_written
= 0;
1696 AL_result
= 0x01; /* disk full */
1698 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1699 if (position
!= record_number
* fcb
->logical_record_size
) {
1700 TRACE("seek(%d, %d, 0) failed with %u\n",
1701 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1702 records_written
= 0;
1703 AL_result
= 0x01; /* disk full */
1705 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1706 records_requested
= CX_reg(context
);
1707 bytes_requested
= records_requested
* fcb
->logical_record_size
;
1708 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, bytes_requested
);
1709 if (bytes_written
!= bytes_requested
) {
1710 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1711 fcb
->file_number
, disk_transfer_area
, bytes_requested
, bytes_written
);
1712 records_written
= bytes_written
/ fcb
->logical_record_size
;
1713 AL_result
= 0x01; /* disk full */
1715 TRACE("successful write %d bytes from record %d (position %u) of file %d (handle %p)\n",
1716 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1717 records_written
= records_requested
;
1718 AL_result
= 0x00; /* successful */
1722 record_number
+= records_written
;
1723 memcpy(fcb
->random_access_record_number
, &record_number
, 4);
1724 fcb
->current_block_number
= record_number
/ 128;
1725 fcb
->record_within_current_block
= record_number
% 128;
1726 SET_CX(context
, records_written
);
1727 SET_AL(context
, AL_result
);
1731 /***********************************************************************
1732 * INT21_CreateDirectory
1736 * - subfunction 0x39 of function 0x71
1737 * - subfunction 0xff of function 0x43 (CL == 0x39)
1739 static BOOL
INT21_CreateDirectory( CONTEXT
*context
)
1741 WCHAR dirW
[MAX_PATH
];
1742 char *dirA
= CTX_SEG_OFF_TO_LIN(context
,
1746 TRACE( "CREATE DIRECTORY %s\n", dirA
);
1748 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
1750 if (CreateDirectoryW(dirW
, NULL
))
1754 * FIXME: CreateDirectory's LastErrors will clash with the ones
1755 * used by DOS. AH=39 only returns 3 (path not found) and
1756 * 5 (access denied), while CreateDirectory return several
1757 * ones. Remap some of them. -Marcus
1759 switch (GetLastError())
1761 case ERROR_ALREADY_EXISTS
:
1762 case ERROR_FILENAME_EXCED_RANGE
:
1763 case ERROR_DISK_FULL
:
1764 SetLastError(ERROR_ACCESS_DENIED
);
1774 /***********************************************************************
1775 * INT21_ExtendedCountryInformation
1777 * Handler for function 0x65.
1779 static void INT21_ExtendedCountryInformation( CONTEXT
*context
)
1781 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN( context
, context
->SegEs
, context
->Edi
);
1782 BYTE buffsize
= CX_reg (context
);
1784 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1788 * Check subfunctions that are passed country and code page.
1790 if (AL_reg(context
) >= 0x01 && AL_reg(context
) <= 0x07)
1792 WORD country
= DX_reg(context
);
1793 WORD codepage
= BX_reg(context
);
1795 if (country
!= 0xffff && country
!= INT21_GetSystemCountryCode())
1796 FIXME( "Requested info on non-default country %04x\n", country
);
1798 if (codepage
!= 0xffff && codepage
!= GetOEMCP())
1799 FIXME( "Requested info on non-default code page %04x\n", codepage
);
1802 switch (AL_reg(context
)) {
1803 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1804 INT_BARF( context
, 0x21 );
1805 SET_CFLAG( context
);
1808 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1809 TRACE( "Get general internationalization info\n" );
1810 dataptr
[0] = 0x01; /* Info ID */
1811 *(WORD
*)(dataptr
+1) = 38; /* Size of the following info */
1812 *(WORD
*)(dataptr
+3) = INT21_GetSystemCountryCode(); /* Country ID */
1813 *(WORD
*)(dataptr
+5) = GetOEMCP(); /* Code page */
1814 /* FIXME: fill buffer partially up to buffsize bytes*/
1815 if (buffsize
>= 0x29){
1816 INT21_FillCountryInformation( dataptr
+ 7 );
1817 SET_CX( context
, 0x29 ); /* Size of returned info */
1819 SET_CX( context
, 0x07 ); /* Size of returned info */
1823 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1824 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1825 TRACE( "Get pointer to uppercase table\n" );
1826 dataptr
[0] = AL_reg(context
); /* Info ID */
1827 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1828 offsetof(INT21_HEAP
, uppercase_size
) );
1829 SET_CX( context
, 5 ); /* Size of returned info */
1832 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1833 TRACE( "Get pointer to lowercase table\n" );
1834 dataptr
[0] = 0x03; /* Info ID */
1835 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1836 offsetof(INT21_HEAP
, lowercase_size
) );
1837 SET_CX( context
, 5 ); /* Size of returned info */
1840 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1841 TRACE("Get pointer to filename terminator table\n");
1842 dataptr
[0] = 0x05; /* Info ID */
1843 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1844 offsetof(INT21_HEAP
, filename_size
) );
1845 SET_CX( context
, 5 ); /* Size of returned info */
1848 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1849 TRACE("Get pointer to collating sequence table\n");
1850 dataptr
[0] = 0x06; /* Info ID */
1851 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1852 offsetof(INT21_HEAP
, collating_size
) );
1853 SET_CX( context
, 5 ); /* Size of returned info */
1856 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1857 TRACE("Get pointer to DBCS lead byte table\n");
1858 dataptr
[0] = 0x07; /* Info ID */
1859 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1860 offsetof(INT21_HEAP
, dbcs_size
) );
1861 SET_CX( context
, 5 ); /* Size of returned info */
1864 case 0x20: /* CAPITALIZE CHARACTER */
1865 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1866 TRACE("Convert char to uppercase\n");
1867 SET_DL( context
, toupper(DL_reg(context
)) );
1870 case 0x21: /* CAPITALIZE STRING */
1871 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1872 TRACE("Convert string to uppercase with length\n");
1874 char *ptr
= CTX_SEG_OFF_TO_LIN( context
, context
->SegDs
,
1876 WORD len
= CX_reg(context
);
1877 while (len
--) { *ptr
= toupper(*ptr
); ptr
++; }
1881 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1882 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1883 TRACE("Convert ASCIIZ string to uppercase\n");
1885 char *p
= CTX_SEG_OFF_TO_LIN( context
, context
->SegDs
, context
->Edx
);
1886 for ( ; *p
; p
++) *p
= toupper(*p
);
1890 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1891 INT_BARF( context
, 0x21 );
1892 SET_CFLAG( context
);
1896 INT_BARF( context
, 0x21 );
1903 /***********************************************************************
1904 * INT21_FileAttributes
1908 * - subfunction 0x43 of function 0x71
1910 static BOOL
INT21_FileAttributes( CONTEXT
*context
,
1914 WCHAR fileW
[MAX_PATH
];
1915 char *fileA
= CTX_SEG_OFF_TO_LIN(context
,
1925 switch (subfunction
)
1927 case 0x00: /* GET FILE ATTRIBUTES */
1928 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA
);
1929 len
= MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1931 /* Winbench 96 Disk Test fails if we don't complain
1932 * about a filename that ends in \
1934 if (!len
|| (fileW
[len
-1] == '/') || (fileW
[len
-1] == '\\'))
1937 result
= GetFileAttributesW( fileW
);
1938 if (result
== INVALID_FILE_ATTRIBUTES
)
1942 SET_CX( context
, (WORD
)result
);
1944 SET_AX( context
, (WORD
)result
); /* DR DOS */
1948 case 0x01: /* SET FILE ATTRIBUTES */
1949 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
1950 CX_reg(context
), fileA
);
1951 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1953 if (!SetFileAttributesW( fileW
, CX_reg(context
) ))
1957 case 0x02: /* GET COMPRESSED FILE SIZE */
1958 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA
);
1959 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1961 result
= GetCompressedFileSizeW( fileW
, NULL
);
1962 if (result
== INVALID_FILE_SIZE
)
1966 SET_AX( context
, LOWORD(result
) );
1967 SET_DX( context
, HIWORD(result
) );
1971 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
1973 INT_BARF( context
, 0x21 );
1976 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA
);
1977 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1979 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
1980 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1981 NULL
, OPEN_EXISTING
, 0, 0 );
1982 if (handle
== INVALID_HANDLE_VALUE
)
1985 DosDateTimeToFileTime( DI_reg(context
),
1988 status
= SetFileTime( handle
, NULL
, NULL
, &filetime
);
1990 CloseHandle( handle
);
1995 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
1997 INT_BARF( context
, 0x21 );
2000 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA
);
2001 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
2003 handle
= CreateFileW( fileW
, GENERIC_READ
,
2004 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2005 NULL
, OPEN_EXISTING
, 0, 0 );
2006 if (handle
== INVALID_HANDLE_VALUE
)
2009 status
= GetFileTime( handle
, NULL
, NULL
, &filetime
);
2012 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2013 SET_DI( context
, date
);
2014 SET_CX( context
, time
);
2017 CloseHandle( handle
);
2022 case 0x05: /* SET FILE LAST ACCESS DATE */
2024 INT_BARF( context
, 0x21 );
2027 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA
);
2028 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
2030 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
2031 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2032 NULL
, OPEN_EXISTING
, 0, 0 );
2033 if (handle
== INVALID_HANDLE_VALUE
)
2036 DosDateTimeToFileTime( DI_reg(context
),
2039 status
= SetFileTime( handle
, NULL
, &filetime
, NULL
);
2041 CloseHandle( handle
);
2046 case 0x06: /* GET FILE LAST ACCESS DATE */
2048 INT_BARF( context
, 0x21 );
2051 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA
);
2052 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
2054 handle
= CreateFileW( fileW
, GENERIC_READ
,
2055 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2056 NULL
, OPEN_EXISTING
, 0, 0 );
2057 if (handle
== INVALID_HANDLE_VALUE
)
2060 status
= GetFileTime( handle
, NULL
, &filetime
, NULL
);
2063 FileTimeToDosDateTime( &filetime
, &date
, NULL
);
2064 SET_DI( context
, date
);
2067 CloseHandle( handle
);
2072 case 0x07: /* SET FILE CREATION DATE AND TIME */
2074 INT_BARF( context
, 0x21 );
2077 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA
);
2078 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
2080 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
2081 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2082 NULL
, OPEN_EXISTING
, 0, 0 );
2083 if (handle
== INVALID_HANDLE_VALUE
)
2087 * FIXME: SI has number of 10-millisecond units past time in CX.
2089 DosDateTimeToFileTime( DI_reg(context
),
2092 status
= SetFileTime( handle
, &filetime
, NULL
, NULL
);
2094 CloseHandle( handle
);
2099 case 0x08: /* GET FILE CREATION DATE AND TIME */
2101 INT_BARF( context
, 0x21 );
2104 TRACE( "GET FILE CREATION DATE AND TIME, file %s\n", fileA
);
2105 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
2107 handle
= CreateFileW( fileW
, GENERIC_READ
,
2108 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2109 NULL
, OPEN_EXISTING
, 0, 0 );
2110 if (handle
== INVALID_HANDLE_VALUE
)
2113 status
= GetFileTime( handle
, &filetime
, NULL
, NULL
);
2116 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2117 SET_DI( context
, date
);
2118 SET_CX( context
, time
);
2120 * FIXME: SI has number of 10-millisecond units past
2123 SET_SI( context
, 0 );
2126 CloseHandle(handle
);
2131 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2132 if (islong
|| context
->Ebp
!= 0x5053)
2133 INT_BARF( context
, 0x21 );
2136 switch(CL_reg(context
))
2139 if (!INT21_CreateDirectory( context
))
2144 if (!INT21_RenameFile( context
))
2149 INT_BARF( context
, 0x21 );
2155 INT_BARF( context
, 0x21 );
2162 /***********************************************************************
2163 * INT21_FileDateTime
2165 * Handler for function 0x57.
2167 static BOOL
INT21_FileDateTime( CONTEXT
*context
)
2169 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2173 switch (AL_reg(context
)) {
2174 case 0x00: /* Get last-written stamp */
2175 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2178 if (!GetFileTime( handle
, NULL
, NULL
, &filetime
))
2180 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2181 SET_DX( context
, date
);
2182 SET_CX( context
, time
);
2186 case 0x01: /* Set last-written stamp */
2187 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2190 DosDateTimeToFileTime( DX_reg(context
),
2193 if (!SetFileTime( handle
, NULL
, NULL
, &filetime
))
2198 case 0x04: /* Get last access stamp, DOS 7 */
2199 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2202 if (!GetFileTime( handle
, NULL
, &filetime
, NULL
))
2204 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2205 SET_DX( context
, date
);
2206 SET_CX( context
, time
);
2210 case 0x05: /* Set last access stamp, DOS 7 */
2211 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2214 DosDateTimeToFileTime( DX_reg(context
),
2217 if (!SetFileTime( handle
, NULL
, &filetime
, NULL
))
2222 case 0x06: /* Get creation stamp, DOS 7 */
2223 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2226 if (!GetFileTime( handle
, &filetime
, NULL
, NULL
))
2228 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2229 SET_DX( context
, date
);
2230 SET_CX( context
, time
);
2232 * FIXME: SI has number of 10-millisecond units past time in CX.
2234 SET_SI( context
, 0 );
2238 case 0x07: /* Set creation stamp, DOS 7 */
2239 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2243 * FIXME: SI has number of 10-millisecond units past time in CX.
2245 DosDateTimeToFileTime( DX_reg(context
),
2248 if (!SetFileTime( handle
, &filetime
, NULL
, NULL
))
2254 INT_BARF( context
, 0x21 );
2262 /***********************************************************************
2265 * Handler for functions 0x51 and 0x62.
2267 static void INT21_GetPSP( CONTEXT
*context
)
2269 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context
) );
2271 SET_BX( context
, LOWORD(GetCurrentPDB16()) );
2274 static inline void setword( BYTE
*ptr
, WORD w
)
2277 ptr
[1] = (BYTE
)(w
>> 8);
2280 static void CreateBPB(int drive
, BYTE
*data
, BOOL16 limited
)
2281 /* limited == TRUE is used with INT 0x21/0x440d */
2283 /* FIXME: we're forcing some values without checking that those are valid */
2288 setword(&data
[3], 0);
2290 setword(&data
[6], 240);
2291 setword(&data
[8], 64000);
2293 setword(&data
[0x0b], 40);
2294 setword(&data
[0x0d], 56);
2295 setword(&data
[0x0f], 2);
2296 setword(&data
[0x11], 0);
2299 setword(&data
[0x1f], 800);
2301 setword(&data
[0x22], 1);
2308 setword(&data
[3], 0);
2310 setword(&data
[6], 240);
2311 setword(&data
[8], 2880);
2313 setword(&data
[0x0b], 6);
2314 setword(&data
[0x0d], 18);
2315 setword(&data
[0x0f], 2);
2316 setword(&data
[0x11], 0);
2319 setword(&data
[0x1f], 80);
2321 setword(&data
[0x22], 2);
2326 static inline DWORD
INT21_Ioctl_CylHeadSect2Lin(DWORD cyl
, WORD head
, WORD sec
, WORD cyl_cnt
,
2327 WORD head_cnt
, WORD sec_cnt
)
2329 DWORD res
= (cyl
* head_cnt
*sec_cnt
+ head
* sec_cnt
+ sec
);
2333 /***********************************************************************
2336 * Handler for block device IOCTLs.
2338 static void INT21_Ioctl_Block( CONTEXT
*context
)
2341 BYTE drive
= INT21_MapDrive( BL_reg(context
) );
2342 WCHAR drivespec
[] = {'A', ':', '\\', 0};
2345 drivespec
[0] += drive
;
2346 drivetype
= GetDriveTypeW( drivespec
);
2348 RESET_CFLAG(context
);
2349 if (drivetype
== DRIVE_UNKNOWN
|| drivetype
== DRIVE_NO_ROOT_DIR
)
2351 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2352 AL_reg(context
), 'A' + drive
);
2353 SetLastError( ERROR_INVALID_DRIVE
);
2354 SET_AX( context
, ERROR_INVALID_DRIVE
);
2355 SET_CFLAG( context
);
2359 switch (AL_reg(context
))
2361 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2362 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2363 INT_BARF( context
, 0x21 );
2366 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2367 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2370 if (drivetype
== DRIVE_REMOVABLE
)
2371 SET_AX( context
, 0 ); /* removable */
2373 SET_AX( context
, 1 ); /* not removable */
2376 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2377 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2380 if (drivetype
== DRIVE_REMOTE
)
2381 SET_DX( context
, (1<<9) | (1<<12) ); /* remote + no direct IO */
2382 else if (drivetype
== DRIVE_CDROM
)
2383 /* CDROM should be set to remote. If it set the app will
2384 * call int2f to check if it cdrom or remote drive. */
2385 SET_DX( context
, (1<<12) );
2386 else if (drivetype
== DRIVE_FIXED
)
2387 /* This should define if drive support 0x0d, 0x0f and 0x08
2388 * requests. The local fixed drive should do. */
2389 SET_DX( context
, (1<<11) );
2391 SET_DX( context
, 0 ); /* FIXME: use driver attr here */
2394 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2395 /* Get pointer to IOCTL parameter block */
2396 dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2398 switch (CX_reg(context
))
2400 case 0x0841: /* write logical device track */
2401 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2404 WORD head
= *(WORD
*)(dataptr
+1);
2405 WORD cyl
= *(WORD
*)(dataptr
+3);
2406 WORD sect
= *(WORD
*)(dataptr
+5);
2407 WORD nrsect
= *(WORD
*)(dataptr
+7);
2408 BYTE
*data
= CTX_SEG_OFF_TO_LIN(context
, *(WORD
*)(dataptr
+11), *(WORD
*)(dataptr
+9));
2409 WORD cyl_cnt
, head_cnt
, sec_cnt
;
2411 /* FIXME: we're faking some values here */
2417 SET_AX( context
, ERROR_WRITE_FAULT
);
2428 if (!DOSVM_RawWrite(drive
, INT21_Ioctl_CylHeadSect2Lin(cyl
, head
, sect
, cyl_cnt
, head_cnt
, sec_cnt
), nrsect
, data
, FALSE
))
2430 SET_AX( context
, ERROR_WRITE_FAULT
);
2436 case 0x084a: /* lock logical volume */
2437 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2438 BH_reg(context
), DX_reg(context
), 'A' + drive
);
2441 case 0x0860: /* get device parameters */
2442 /* FIXME: we're faking some values here */
2443 /* used by w4wgrp's winfile */
2444 memset(dataptr
, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2446 dataptr
[6] = 0; /* media type */
2449 dataptr
[1] = 0x05; /* fixed disk */
2450 setword(&dataptr
[2], 0x01); /* non removable */
2451 setword(&dataptr
[4], 0x300); /* # of cylinders */
2455 dataptr
[1] = 0x07; /* block dev, floppy */
2456 setword(&dataptr
[2], 0x02); /* removable */
2457 setword(&dataptr
[4], 80); /* # of cylinders */
2459 CreateBPB(drive
, &dataptr
[7], TRUE
);
2460 RESET_CFLAG(context
);
2463 case 0x0861: /* read logical device track */
2464 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2467 WORD head
= *(WORD
*)(dataptr
+1);
2468 WORD cyl
= *(WORD
*)(dataptr
+3);
2469 WORD sect
= *(WORD
*)(dataptr
+5);
2470 WORD nrsect
= *(WORD
*)(dataptr
+7);
2471 BYTE
*data
= CTX_SEG_OFF_TO_LIN(context
, *(WORD
*)(dataptr
+11), *(WORD
*)(dataptr
+9));
2472 WORD cyl_cnt
, head_cnt
, sec_cnt
;
2474 /* FIXME: we're faking some values here */
2488 if (!DOSVM_RawRead(drive
, INT21_Ioctl_CylHeadSect2Lin(cyl
, head
, sect
, cyl_cnt
, head_cnt
, sec_cnt
), nrsect
, data
, FALSE
))
2490 SET_AX( context
, ERROR_READ_FAULT
);
2496 case 0x0866: /* get volume serial number */
2498 WCHAR label
[12],fsname
[9];
2500 TRACE( "GENERIC IOCTL - Get media id - %c:\n",
2503 GetVolumeInformationW(drivespec
, label
, 12, &serial
, NULL
, NULL
, fsname
, 9);
2504 *(WORD
*)dataptr
= 0;
2505 memcpy(dataptr
+2,&serial
,4);
2506 WideCharToMultiByte(CP_OEMCP
, 0, label
, 11, (LPSTR
)dataptr
+ 6, 11, NULL
, NULL
);
2507 WideCharToMultiByte(CP_OEMCP
, 0, fsname
, 8, (LPSTR
)dataptr
+ 17, 8, NULL
, NULL
);
2511 case 0x086a: /* unlock logical volume */
2512 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2516 case 0x086f: /* get drive map information */
2517 memset(dataptr
+1, '\0', dataptr
[0]-1);
2518 dataptr
[1] = dataptr
[0];
2519 dataptr
[2] = 0x07; /* protected mode driver; no eject; no notification */
2520 dataptr
[3] = 0xFF; /* no physical drive */
2524 /* Trial and error implementation */
2525 SET_AX( context
, drivetype
== DRIVE_UNKNOWN
? 0x0f : 0x01 );
2526 SET_CFLAG(context
); /* Seems to be set all the time */
2530 INT_BARF( context
, 0x21 );
2534 case 0x0e: /* GET LOGICAL DRIVE MAP */
2535 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2537 /* FIXME: this is not correct if drive has mappings */
2538 SET_AL( context
, 0 ); /* drive has no mapping */
2541 case 0x0f: /* SET LOGICAL DRIVE MAP */
2542 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2543 INT21_DriveName( BL_reg(context
)));
2544 /* FIXME: as of today, we don't support logical drive mapping... */
2545 SET_AL( context
, 0 );
2548 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2550 INT_BARF( context
, 0x21 );
2555 /***********************************************************************
2556 * INT21_IoctlScsiMgrHandler
2558 * IOCTL handler for the SCSIMGR device.
2560 static void INT21_IoctlScsiMgrHandler( CONTEXT
*context
)
2562 switch (AL_reg(context
))
2564 case 0x00: /* GET DEVICE INFORMATION */
2565 SET_DX( context
, 0xc0c0 );
2568 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2569 SET_DX( context
, 0 );
2572 case 0x01: /* SET DEVICE INFORMATION */
2573 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2574 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2575 case 0x06: /* GET INPUT STATUS */
2576 case 0x07: /* GET OUTPUT STATUS */
2577 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2578 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2580 INT_BARF( context
, 0x21 );
2586 /***********************************************************************
2587 * INT21_IoctlHPScanHandler
2589 * IOCTL handler for the HPSCAN device.
2591 static void INT21_IoctlHPScanHandler( CONTEXT
*context
)
2593 switch (AL_reg(context
))
2595 case 0x00: /* GET DEVICE INFORMATION */
2596 SET_DX( context
, 0xc0c0 );
2599 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2600 SET_DX( context
, 0 );
2603 case 0x01: /* SET DEVICE INFORMATION */
2604 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2605 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2606 case 0x06: /* GET INPUT STATUS */
2607 case 0x07: /* GET OUTPUT STATUS */
2608 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2609 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2611 INT_BARF( context
, 0x21 );
2617 /***********************************************************************
2620 * Handler for character device IOCTLs.
2622 static void INT21_Ioctl_Char( CONTEXT
*context
)
2625 BOOL IsConsoleIOHandle
= FALSE
;
2627 FILE_INTERNAL_INFORMATION info
;
2628 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2630 status
= NtQueryInformationFile( handle
, &io
, &info
, sizeof(info
), FileInternalInformation
);
2633 if( VerifyConsoleIoHandle( handle
))
2634 IsConsoleIOHandle
= TRUE
;
2636 SET_AX( context
, RtlNtStatusToDosError(status
) );
2637 SET_CFLAG( context
);
2642 for (i
= 0; i
< NB_MAGIC_DEVICES
; i
++)
2644 if (!magic_devices
[i
].handle
) continue;
2645 if (magic_devices
[i
].index
.QuadPart
== info
.IndexNumber
.QuadPart
)
2648 magic_devices
[i
].ioctl_handler( context
);
2654 /* no magic device found, do default handling */
2656 switch (AL_reg(context
))
2658 case 0x00: /* GET DEVICE INFORMATION */
2659 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context
) );
2660 if (IsConsoleIOHandle
|| GetFileType(handle
) == FILE_TYPE_CHAR
)
2663 * Returns attribute word in DX:
2664 * Bit 14 - Device driver can process IOCTL requests.
2665 * Bit 13 - Output until busy supported.
2666 * Bit 11 - Driver supports OPEN/CLOSE calls.
2668 * Bit 7 - Set (indicates device).
2669 * Bit 6 - EOF on input.
2670 * Bit 5 - Raw (binary) mode.
2671 * Bit 4 - Device is special (uses int29).
2672 * Bit 3 - Clock device.
2673 * Bit 2 - NUL device.
2674 * Bit 1 - Console output device.
2675 * Bit 0 - Console input device.
2677 SET_DX( context
, IsConsoleIOHandle
? 0x80c3 : 0x80c0 /* FIXME */ );
2682 * Returns attribute word in DX:
2683 * Bit 15 - File is remote.
2684 * Bit 14 - Don't set file date/time on closing.
2685 * Bit 11 - Media not removable.
2686 * Bit 8 - Generate int24 if no disk space on write
2687 * or read past end of file
2688 * Bit 7 - Clear (indicates file).
2689 * Bit 6 - File has not been written.
2690 * Bit 5..0 - Drive number (0=A:,...)
2692 * FIXME: Should check if file is on remote or removable drive.
2693 * FIXME: Should use drive file is located on (and not current).
2695 SET_DX( context
, 0x0140 + INT21_GetCurrentDrive() );
2699 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2700 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context
) );
2702 * Returns attribute word in DX:
2703 * Bit 15 - Set if remote.
2704 * Bit 14 - Set if date/time not set on close.
2706 * FIXME: Should check if file is on remote drive.
2708 SET_DX( context
, 0 );
2711 case 0x01: /* SET DEVICE INFORMATION */
2712 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2713 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2714 case 0x06: /* GET INPUT STATUS */
2715 case 0x07: /* GET OUTPUT STATUS */
2716 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2717 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2719 INT_BARF( context
, 0x21 );
2725 /***********************************************************************
2728 * Handler for function 0x44.
2730 static void INT21_Ioctl( CONTEXT
*context
)
2732 switch (AL_reg(context
))
2738 INT21_Ioctl_Char( context
);
2743 INT21_Ioctl_Block( context
);
2748 INT21_Ioctl_Char( context
);
2753 INT21_Ioctl_Block( context
);
2757 INT21_Ioctl_Char( context
);
2760 case 0x0b: /* SET SHARING RETRY COUNT */
2761 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2762 CX_reg(context
), DX_reg(context
) );
2763 if (!CX_reg(context
))
2765 SET_AX( context
, 1 );
2766 SET_CFLAG( context
);
2770 RESET_CFLAG( context
);
2775 INT21_Ioctl_Char( context
);
2781 INT21_Ioctl_Block( context
);
2785 INT21_Ioctl_Char( context
);
2789 INT21_Ioctl_Block( context
);
2792 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2793 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2794 SET_CFLAG(context
); /* Error / This is not DR DOS. */
2795 SET_AX( context
, 0x0001 ); /* Invalid function */
2798 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2799 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2800 SET_CFLAG(context
); /* Error / This is not DR DOS. */
2801 SET_AX( context
, 0x0001 ); /* Invalid function */
2804 case 0xe0: /* Sun PC-NFS API */
2805 TRACE( "Sun PC-NFS API\n" );
2810 INT_BARF( context
, 0x21 );
2815 /***********************************************************************
2818 * Handler for function 0x73.
2820 static BOOL
INT21_Fat32( CONTEXT
*context
)
2822 switch (AL_reg(context
))
2824 case 0x02: /* FAT32 - GET EXTENDED DPB */
2826 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
2827 WORD
*ptr
= CTX_SEG_OFF_TO_LIN(context
,
2828 context
->SegEs
, context
->Edi
);
2829 INT21_DPB
*target
= (INT21_DPB
*)(ptr
+ 1);
2832 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context
) );
2834 if ( CX_reg(context
) < sizeof(INT21_DPB
) + 2 || *ptr
< sizeof(INT21_DPB
) )
2836 SetLastError( ERROR_BAD_LENGTH
);
2840 if ( !INT21_FillDrivePB( drive
) )
2842 SetLastError( ERROR_INVALID_DRIVE
);
2846 source
= &INT21_GetHeapPointer()->misc_dpb_list
[drive
];
2848 *ptr
= sizeof(INT21_DPB
);
2851 if (LOWORD(context
->Esi
) != 0xF1A6)
2853 target
->driver_header
= 0;
2858 FIXME( "Caller requested driver and next DPB pointers!\n" );
2863 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2865 WCHAR dirW
[MAX_PATH
];
2866 char *dirA
= CTX_SEG_OFF_TO_LIN( context
,
2867 context
->SegDs
, context
->Edx
);
2868 BYTE
*data
= CTX_SEG_OFF_TO_LIN( context
,
2869 context
->SegEs
, context
->Edi
);
2870 DWORD cluster_sectors
;
2872 DWORD free_clusters
;
2873 DWORD total_clusters
;
2875 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA
);
2876 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
2878 if (CX_reg(context
) < 44)
2880 SetLastError( ERROR_BAD_LENGTH
);
2884 if (!GetDiskFreeSpaceW( dirW
, &cluster_sectors
, §or_bytes
,
2885 &free_clusters
, &total_clusters
))
2888 *(WORD
*) (data
+ 0) = 44; /* size of structure */
2889 *(WORD
*) (data
+ 2) = 0; /* version */
2890 *(DWORD
*)(data
+ 4) = cluster_sectors
;
2891 *(DWORD
*)(data
+ 8) = sector_bytes
;
2892 *(DWORD
*)(data
+ 12) = free_clusters
;
2893 *(DWORD
*)(data
+ 16) = total_clusters
;
2896 * Below we have free/total sectors and
2897 * free/total allocation units without adjustment
2898 * for compression. We fake both using cluster information.
2900 *(DWORD
*)(data
+ 20) = free_clusters
* cluster_sectors
;
2901 *(DWORD
*)(data
+ 24) = total_clusters
* cluster_sectors
;
2902 *(DWORD
*)(data
+ 28) = free_clusters
;
2903 *(DWORD
*)(data
+ 32) = total_clusters
;
2906 * Between (data + 36) and (data + 43) there
2907 * are eight reserved bytes.
2913 INT_BARF( context
, 0x21 );
2919 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA
*dataA
,
2920 const WIN32_FIND_DATAW
*dataW
)
2922 dataA
->dwFileAttributes
= dataW
->dwFileAttributes
;
2923 dataA
->ftCreationTime
= dataW
->ftCreationTime
;
2924 dataA
->ftLastAccessTime
= dataW
->ftLastAccessTime
;
2925 dataA
->ftLastWriteTime
= dataW
->ftLastWriteTime
;
2926 dataA
->nFileSizeHigh
= dataW
->nFileSizeHigh
;
2927 dataA
->nFileSizeLow
= dataW
->nFileSizeLow
;
2928 WideCharToMultiByte( CP_OEMCP
, 0, dataW
->cFileName
, -1,
2929 dataA
->cFileName
, sizeof(dataA
->cFileName
), NULL
, NULL
);
2930 WideCharToMultiByte( CP_OEMCP
, 0, dataW
->cAlternateFileName
, -1,
2931 dataA
->cAlternateFileName
, sizeof(dataA
->cAlternateFileName
), NULL
, NULL
);
2934 /***********************************************************************
2935 * INT21_LongFilename
2937 * Handler for function 0x71.
2939 static void INT21_LongFilename( CONTEXT
*context
)
2941 BOOL bSetDOSExtendedError
= FALSE
;
2942 WCHAR pathW
[MAX_PATH
];
2945 switch (AL_reg(context
))
2947 case 0x0d: /* RESET DRIVE */
2948 INT_BARF( context
, 0x21 );
2951 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
2952 if (!INT21_CreateDirectory( context
))
2953 bSetDOSExtendedError
= TRUE
;
2956 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
2957 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2959 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA
);
2960 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2961 if (!RemoveDirectoryW( pathW
)) bSetDOSExtendedError
= TRUE
;
2964 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
2965 if (!INT21_SetCurrentDirectory( context
))
2966 bSetDOSExtendedError
= TRUE
;
2969 case 0x41: /* LONG FILENAME - DELETE FILE */
2970 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2972 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA
);
2973 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2975 if (!DeleteFileW( pathW
)) bSetDOSExtendedError
= TRUE
;
2978 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
2979 if (!INT21_FileAttributes( context
, BL_reg(context
), TRUE
))
2980 bSetDOSExtendedError
= TRUE
;
2983 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
2984 if (!INT21_GetCurrentDirectory( context
, TRUE
))
2985 bSetDOSExtendedError
= TRUE
;
2988 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
2992 WIN32_FIND_DATAW dataW
;
2993 WIN32_FIND_DATAA
* dataA
;
2995 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Edx
);
2996 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA
);
2998 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2999 handle
= FindFirstFileW(pathW
, &dataW
);
3001 dataA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
3002 if (handle
!= INVALID_HANDLE_VALUE
&&
3003 (h16
= GlobalAlloc16(GMEM_MOVEABLE
, sizeof(handle
))))
3005 HANDLE
* ptr
= GlobalLock16( h16
);
3007 GlobalUnlock16( h16
);
3008 SET_AX( context
, h16
);
3009 INT21_ConvertFindDataWtoA(dataA
, &dataW
);
3013 if (handle
!= INVALID_HANDLE_VALUE
) FindClose(handle
);
3014 SET_AX( context
, INVALID_HANDLE_VALUE16
);
3015 bSetDOSExtendedError
= TRUE
;
3020 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
3022 HGLOBAL16 h16
= BX_reg(context
);
3024 WIN32_FIND_DATAW dataW
;
3025 WIN32_FIND_DATAA
* dataA
;
3027 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
3030 dataA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
3032 if (h16
!= INVALID_HANDLE_VALUE16
&& (ptr
= GlobalLock16( h16
)))
3034 if (!FindNextFileW(*ptr
, &dataW
)) bSetDOSExtendedError
= TRUE
;
3035 else INT21_ConvertFindDataWtoA(dataA
, &dataW
);
3036 GlobalUnlock16( h16
);
3040 SetLastError( ERROR_INVALID_HANDLE
);
3041 bSetDOSExtendedError
= TRUE
;
3046 case 0x56: /* LONG FILENAME - RENAME FILE */
3047 if (!INT21_RenameFile(context
))
3048 bSetDOSExtendedError
= TRUE
;
3051 case 0x60: /* LONG FILENAME - CONVERT PATH */
3053 WCHAR res
[MAX_PATH
];
3055 switch (CL_reg(context
))
3057 case 0x00: /* "truename" - Canonicalize path */
3059 * FIXME: This is not 100% equal to 0x01 case,
3060 * if you fix this, fix int21 subfunction 0x60, too.
3063 case 0x01: /* Get short filename or path */
3064 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
3065 if (!GetShortPathNameW(pathW
, res
, 67))
3066 bSetDOSExtendedError
= TRUE
;
3069 SET_AX( context
, 0 );
3070 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
3071 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
3076 case 0x02: /* Get canonical long filename or path */
3077 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
3078 if (!GetFullPathNameW(pathW
, 128, res
, NULL
))
3079 bSetDOSExtendedError
= TRUE
;
3082 SET_AX( context
, 0 );
3083 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
3084 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
3089 FIXME("Unimplemented long file name function:\n");
3090 INT_BARF( context
, 0x21 );
3092 SET_AL( context
, 0 );
3098 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
3099 if (!INT21_CreateFile( context
, context
->Esi
, TRUE
,
3100 BX_reg(context
), DL_reg(context
) ))
3101 bSetDOSExtendedError
= TRUE
;
3104 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
3106 DWORD filename_len
, flags
;
3109 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Edx
);
3111 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA
);
3112 SET_AX( context
, 0 );
3113 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
3114 if (!GetVolumeInformationW( pathW
, NULL
, 0, NULL
, &filename_len
,
3117 INT_BARF( context
, 0x21 );
3121 SET_BX( context
, flags
| 0x4000 ); /* support for LFN functions */
3122 SET_CX( context
, filename_len
);
3123 SET_DX( context
, MAX_PATH
); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
3124 WideCharToMultiByte(CP_OEMCP
, 0, dstW
, -1,
3125 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
3130 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
3132 HGLOBAL16 h16
= BX_reg(context
);
3135 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
3137 if (h16
!= INVALID_HANDLE_VALUE16
&& (ptr
= GlobalLock16( h16
)))
3139 if (!FindClose( *ptr
)) bSetDOSExtendedError
= TRUE
;
3140 GlobalUnlock16( h16
);
3141 GlobalFree16( h16
);
3145 SetLastError( ERROR_INVALID_HANDLE
);
3146 bSetDOSExtendedError
= TRUE
;
3151 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
3153 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
3154 BY_HANDLE_FILE_INFORMATION
*info
=
3155 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3157 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
3159 if (!GetFileInformationByHandle(handle
, info
))
3160 bSetDOSExtendedError
= TRUE
;
3164 case 0xa7: /* LONG FILENAME - CONVERT TIME */
3165 switch (BL_reg(context
))
3167 case 0x00: /* FILE TIME TO DOS TIME */
3170 FILETIME
*filetime
= CTX_SEG_OFF_TO_LIN(context
,
3174 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
3176 FileTimeToDosDateTime( filetime
, &date
, &time
);
3178 SET_DX( context
, date
);
3179 SET_CX( context
, time
);
3182 * FIXME: BH has number of 10-millisecond units
3185 SET_BH( context
, 0 );
3189 case 0x01: /* DOS TIME TO FILE TIME */
3191 FILETIME
*filetime
= CTX_SEG_OFF_TO_LIN(context
,
3195 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
3198 * FIXME: BH has number of 10-millisecond units
3201 DosDateTimeToFileTime( DX_reg(context
), CX_reg(context
),
3207 INT_BARF( context
, 0x21 );
3212 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
3213 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3214 case 0xaa: /* LONG FILENAME - SUBST */
3216 FIXME("Unimplemented long file name function:\n");
3217 INT_BARF( context
, 0x21 );
3219 SET_AL( context
, 0 );
3223 if (bSetDOSExtendedError
)
3225 SET_AX( context
, GetLastError() );
3226 SET_CFLAG( context
);
3231 /***********************************************************************
3236 * - subfunction 0x56 of function 0x71
3237 * - subfunction 0xff of function 0x43 (CL == 0x56)
3239 static BOOL
INT21_RenameFile( CONTEXT
*context
)
3241 WCHAR fromW
[MAX_PATH
];
3242 WCHAR toW
[MAX_PATH
];
3243 char *fromA
= CTX_SEG_OFF_TO_LIN(context
,
3244 context
->SegDs
,context
->Edx
);
3245 char *toA
= CTX_SEG_OFF_TO_LIN(context
,
3246 context
->SegEs
,context
->Edi
);
3248 TRACE( "RENAME FILE %s to %s\n", fromA
, toA
);
3249 MultiByteToWideChar(CP_OEMCP
, 0, fromA
, -1, fromW
, MAX_PATH
);
3250 MultiByteToWideChar(CP_OEMCP
, 0, toA
, -1, toW
, MAX_PATH
);
3252 return MoveFileW( fromW
, toW
);
3256 /***********************************************************************
3262 static BOOL
INT21_NetworkFunc (CONTEXT
*context
)
3264 switch (AL_reg(context
))
3266 case 0x00: /* Get machine name. */
3268 WCHAR dstW
[MAX_COMPUTERNAME_LENGTH
+ 1];
3269 DWORD s
= sizeof(dstW
) / sizeof(WCHAR
);
3272 char *dst
= CTX_SEG_OFF_TO_LIN (context
,context
->SegDs
,context
->Edx
);
3273 TRACE("getting machine name to %p\n", dst
);
3274 if (!GetComputerNameW(dstW
, &s
) ||
3275 !WideCharToMultiByte(CP_OEMCP
, 0, dstW
, -1, dst
, 16, NULL
, NULL
))
3278 SetLastError( ER_NoNetwork
);
3281 for (len
= strlen(dst
); len
< 15; len
++) dst
[len
] = ' ';
3283 SET_CH( context
, 1 ); /* Valid */
3284 SET_CL( context
, 1 ); /* NETbios number??? */
3285 TRACE("returning %s\n", debugstr_an(dst
, 16));
3290 SetLastError( ER_NoNetwork
);
3295 /******************************************************************
3296 * INT21_GetDiskSerialNumber
3299 static int INT21_GetDiskSerialNumber( CONTEXT
*context
)
3301 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3302 WCHAR path
[] = {'A',':','\\',0}, label
[11];
3305 path
[0] += INT21_MapDrive(BL_reg(context
));
3306 if (!GetVolumeInformationW( path
, label
, 11, &serial
, NULL
, NULL
, NULL
, 0))
3308 SetLastError( ERROR_INVALID_DRIVE
);
3312 *(WORD
*)dataptr
= 0;
3313 memcpy(dataptr
+ 2, &serial
, sizeof(DWORD
));
3314 WideCharToMultiByte(CP_OEMCP
, 0, label
, 11, (LPSTR
)dataptr
+ 6, 11, NULL
, NULL
);
3315 memcpy(dataptr
+ 17, "FAT16 ", 8);
3320 /******************************************************************
3321 * INT21_SetDiskSerialNumber
3324 static BOOL
INT21_SetDiskSerialNumber( CONTEXT
*context
)
3327 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3328 int drive
= INT21_MapDrive(BL_reg(context
));
3330 if (!is_valid_drive(drive
))
3332 SetLastError( ERROR_INVALID_DRIVE
);
3336 DRIVE_SetSerialNumber( drive
, *(DWORD
*)(dataptr
+ 2) );
3339 FIXME("Setting drive serial number is no longer supported\n");
3340 SetLastError( ERROR_NOT_SUPPORTED
);
3346 /******************************************************************
3347 * INT21_GetFreeDiskSpace
3350 static BOOL
INT21_GetFreeDiskSpace( CONTEXT
*context
)
3352 DWORD cluster_sectors
, sector_bytes
, free_clusters
, total_clusters
;
3353 WCHAR root
[] = {'A',':','\\',0};
3354 const DWORD max_clusters
= 0x3d83;
3355 const DWORD max_sectors_per_cluster
= 0x7f;
3356 const DWORD max_bytes_per_sector
= 0x200;
3358 root
[0] += INT21_MapDrive(DL_reg(context
));
3359 if (!GetDiskFreeSpaceW( root
, &cluster_sectors
, §or_bytes
,
3360 &free_clusters
, &total_clusters
))
3363 /* Some old win31 apps (Lotus SmartSuite 5.1) crap out if there's too
3364 * much disk space, so Windows XP seems to apply the following limits:
3365 * cluster_sectors <= 0x7f
3366 * sector size <= 0x200
3367 * clusters <= 0x3D83
3368 * This means total reported space is limited to about 1GB.
3371 /* Make sure bytes-per-sector is in [, max] */
3372 while (sector_bytes
> max_bytes_per_sector
) {
3374 free_clusters
<<= 1;
3375 total_clusters
<<= 1;
3377 /* Then make sure sectors-per-cluster is in [max/2, max]. */
3378 while (cluster_sectors
<= max_sectors_per_cluster
/2) {
3379 cluster_sectors
<<= 1;
3380 free_clusters
>>= 1;
3381 total_clusters
>>= 1;
3383 while (cluster_sectors
> max_sectors_per_cluster
) {
3384 cluster_sectors
>>= 1;
3385 free_clusters
<<= 1;
3386 total_clusters
<<= 1;
3389 /* scale up sectors_per_cluster to exactly max_sectors_per_cluster.
3390 * We could skip this, but that would impose an artificially low
3391 * limit on reported disk space.
3392 * To avoid overflow, first apply a preliminary cap on sector count;
3393 * this will not affect the correctness of the final result,
3394 * because if the preliminary cap hits, the final one will, too.
3396 if (total_clusters
> 4 * max_clusters
)
3397 total_clusters
= 4 * max_clusters
;
3398 if (free_clusters
> 4 * max_clusters
)
3399 free_clusters
= 4 * max_clusters
;
3400 if (cluster_sectors
< max_sectors_per_cluster
) {
3401 free_clusters
*= cluster_sectors
;
3402 free_clusters
/= max_sectors_per_cluster
;
3403 total_clusters
*= cluster_sectors
;
3404 total_clusters
/= max_sectors_per_cluster
;
3405 cluster_sectors
= max_sectors_per_cluster
;
3408 /* Finally, apply real cluster count cap. */
3409 if (total_clusters
> max_clusters
)
3410 total_clusters
= max_clusters
;
3411 if (free_clusters
> max_clusters
)
3412 free_clusters
= max_clusters
;
3414 SET_AX( context
, cluster_sectors
);
3415 SET_BX( context
, free_clusters
);
3416 SET_CX( context
, sector_bytes
);
3417 SET_DX( context
, total_clusters
);
3421 /******************************************************************
3422 * INT21_GetDriveAllocInfo
3425 static BOOL
INT21_GetDriveAllocInfo( CONTEXT
*context
, BYTE drive
)
3429 drive
= INT21_MapDrive( drive
);
3430 if (!INT21_FillDrivePB( drive
)) return FALSE
;
3431 dpb
= &(INT21_GetHeapPointer()->misc_dpb_list
[drive
]);
3432 SET_AL( context
, dpb
->cluster_sectors
+ 1 );
3433 SET_CX( context
, dpb
->sector_bytes
);
3434 SET_DX( context
, dpb
->num_clusters1
);
3436 context
->SegDs
= INT21_GetHeapSelector( context
);
3437 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
].media_ID
) );
3441 /***********************************************************************
3442 * INT21_GetExtendedError
3444 static void INT21_GetExtendedError( CONTEXT
*context
)
3446 BYTE
class, action
, locus
;
3447 WORD error
= GetLastError();
3452 class = action
= locus
= 0;
3454 case ERROR_DIR_NOT_EMPTY
:
3459 case ERROR_ACCESS_DENIED
:
3460 class = EC_AccessDenied
;
3464 case ERROR_CANNOT_MAKE
:
3465 class = EC_AccessDenied
;
3469 case ERROR_DISK_FULL
:
3470 case ERROR_HANDLE_DISK_FULL
:
3471 class = EC_MediaError
;
3475 case ERROR_FILE_EXISTS
:
3476 case ERROR_ALREADY_EXISTS
:
3481 case ERROR_FILE_NOT_FOUND
:
3482 case ERROR_PATH_NOT_FOUND
:
3483 case ERROR_INVALID_NAME
:
3484 class = EC_NotFound
;
3488 case ERROR_GEN_FAILURE
:
3489 class = EC_SystemFailure
;
3493 case ERROR_INVALID_DRIVE
:
3494 class = EC_MediaError
;
3498 case ERROR_INVALID_HANDLE
:
3499 class = EC_ProgramError
;
3503 case ERROR_LOCK_VIOLATION
:
3504 class = EC_AccessDenied
;
3508 case ERROR_NO_MORE_FILES
:
3509 class = EC_MediaError
;
3514 class = EC_NotFound
;
3518 case ERROR_NOT_ENOUGH_MEMORY
:
3519 class = EC_OutOfResource
;
3524 class = EC_NotFound
;
3528 case ERROR_SHARING_VIOLATION
:
3529 class = EC_Temporary
;
3533 case ERROR_TOO_MANY_OPEN_FILES
:
3534 class = EC_ProgramError
;
3539 FIXME("Unknown error %d\n", error
);
3540 class = EC_SystemFailure
;
3545 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3546 error
, class, action
, locus
);
3547 SET_AX( context
, error
);
3548 SET_BH( context
, class );
3549 SET_BL( context
, action
);
3550 SET_CH( context
, locus
);
3553 static BOOL
INT21_CreateTempFile( CONTEXT
*context
)
3555 static int counter
= 0;
3556 char *name
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3557 char *p
= name
+ strlen(name
);
3559 /* despite what Ralf Brown says, some programs seem to call without
3560 * ending backslash (DOS accepts that, so we accept it too) */
3561 if ((p
== name
) || (p
[-1] != '\\')) *p
++ = '\\';
3565 sprintf( p
, "wine%04x.%03d", (int)getpid(), counter
);
3566 counter
= (counter
+ 1) % 1000;
3569 Win32HandleToDosFileHandle(
3570 CreateFileA( name
, GENERIC_READ
| GENERIC_WRITE
,
3571 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
3572 CREATE_NEW
, 0, 0 ) ) );
3573 if (AX_reg(context
) != HFILE_ERROR16
)
3575 TRACE("created %s\n", name
);
3578 if (GetLastError() != ERROR_FILE_EXISTS
) return FALSE
;
3582 /***********************************************************************
3583 * DOSFS_ToDosFCBFormat
3585 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3586 * expanding wild cards and converting to upper-case in the process.
3587 * File name can be terminated by '\0', '\\' or '/'.
3588 * Return FALSE if the name is not a valid DOS name.
3589 * 'buffer' must be at least 12 characters long.
3591 /* Chars we don't want to see in DOS file names */
3592 static BOOL
INT21_ToDosFCBFormat( LPCWSTR name
, LPWSTR buffer
)
3594 static const WCHAR invalid_chars
[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3598 /* Check for "." and ".." */
3603 for(i
= 1; i
< 11; i
++) buffer
[i
] = ' ';
3610 return (!*p
|| (*p
== '/') || (*p
== '\\'));
3613 for (i
= 0; i
< 8; i
++)
3630 if (strchrW( invalid_chars
, *p
)) return FALSE
;
3631 buffer
[i
] = toupperW(*p
);
3639 /* Skip all chars after wildcard up to first dot */
3640 while (*p
&& (*p
!= '/') && (*p
!= '\\') && (*p
!= '.')) p
++;
3644 /* Check if name too long */
3645 if (*p
&& (*p
!= '/') && (*p
!= '\\') && (*p
!= '.')) return FALSE
;
3647 if (*p
== '.') p
++; /* Skip dot */
3649 for (i
= 8; i
< 11; i
++)
3659 return FALSE
; /* Second extension not allowed */
3667 if (strchrW( invalid_chars
, *p
)) return FALSE
;
3668 buffer
[i
] = toupperW(*p
);
3675 /* at most 3 character of the extension are processed
3676 * is something behind this ?
3678 while (*p
== '*' || *p
== ' ') p
++; /* skip wildcards and spaces */
3679 return (!*p
|| (*p
== '/') || (*p
== '\\'));
3682 static HANDLE INT21_FindHandle
;
3683 static const WCHAR
*INT21_FindPath
; /* will point to current dta->fullPath search */
3685 /******************************************************************
3688 static BOOL
INT21_FindFirst( CONTEXT
*context
)
3692 FINDFILE_DTA
*dta
= (FINDFILE_DTA
*)INT21_GetCurrentDTA(context
);
3693 WCHAR maskW
[12], pathW
[MAX_PATH
];
3694 static const WCHAR wildcardW
[] = {'*','.','*',0};
3696 path
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3697 MultiByteToWideChar(CP_OEMCP
, 0, path
, -1, pathW
, MAX_PATH
);
3699 p
= strrchrW( pathW
, '\\');
3700 q
= strrchrW( pathW
, '/');
3704 if (pathW
[0] && pathW
[1] == ':') p
= pathW
+ 2;
3709 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3710 * (doesn't matter as it is set below anyway)
3712 if (!INT21_ToDosFCBFormat( p
, maskW
))
3714 SetLastError( ERROR_FILE_NOT_FOUND
);
3715 SET_AX( context
, ERROR_FILE_NOT_FOUND
);
3719 WideCharToMultiByte(CP_OEMCP
, 0, maskW
, 12, dta
->mask
, sizeof(dta
->mask
), NULL
, NULL
);
3721 dta
->fullPath
= HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW
) + (p
- pathW
)*sizeof(WCHAR
) );
3722 memcpy( dta
->fullPath
, pathW
, (p
- pathW
) * sizeof(WCHAR
) );
3723 memcpy( dta
->fullPath
+ (p
- pathW
), wildcardW
, sizeof(wildcardW
) );
3724 /* we must have a fully qualified file name in dta->fullPath
3725 * (we could have a UNC path, but this would lead to some errors later on)
3727 dta
->drive
= toupperW(dta
->fullPath
[0]) - 'A';
3729 dta
->search_attr
= CL_reg(context
);
3733 /******************************************************************
3736 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3738 static BOOL
match_short(LPCWSTR shortW
, LPCSTR maskA
)
3740 WCHAR mask
[11], file
[12];
3743 if (!INT21_ToDosFCBFormat( shortW
, file
)) return FALSE
;
3744 MultiByteToWideChar(CP_OEMCP
, 0, maskA
, 11, mask
, 11);
3745 for (i
= 0; i
< 11; i
++)
3746 if (mask
[i
] != '?' && mask
[i
] != file
[i
]) return FALSE
;
3750 static unsigned INT21_FindHelper(LPCWSTR fullPath
, unsigned drive
, unsigned count
,
3751 LPCSTR mask
, unsigned search_attr
,
3752 WIN32_FIND_DATAW
* entry
)
3756 if ((search_attr
& ~(FA_UNUSED
| FA_ARCHIVE
| FA_RDONLY
)) == FA_LABEL
)
3758 WCHAR path
[] = {' ',':','\\',0};
3760 if (count
) return 0;
3761 path
[0] = drive
+ 'A';
3762 if (!GetVolumeInformationW(path
, entry
->cAlternateFileName
, 13, NULL
, NULL
, NULL
, NULL
, 0)) return 0;
3763 if (!entry
->cAlternateFileName
[0]) return 0;
3764 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftCreationTime
);
3765 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftLastAccessTime
);
3766 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftLastWriteTime
);
3767 entry
->dwFileAttributes
= FA_LABEL
;
3768 entry
->nFileSizeHigh
= entry
->nFileSizeLow
= 0;
3769 TRACE("returning %s as label\n", debugstr_w(entry
->cAlternateFileName
));
3773 if (!INT21_FindHandle
|| INT21_FindPath
!= fullPath
|| count
== 0)
3775 if (INT21_FindHandle
) FindClose(INT21_FindHandle
);
3776 INT21_FindHandle
= FindFirstFileW(fullPath
, entry
);
3777 if (INT21_FindHandle
== INVALID_HANDLE_VALUE
)
3779 INT21_FindHandle
= 0;
3782 INT21_FindPath
= fullPath
;
3783 /* we need to resync search */
3788 while (ncalls
-- != 0)
3790 if (!FindNextFileW(INT21_FindHandle
, entry
))
3792 FindClose(INT21_FindHandle
); INT21_FindHandle
= 0;
3796 while (count
< 0xffff)
3799 /* Check the file attributes, and path */
3800 if (!(entry
->dwFileAttributes
& ~search_attr
) &&
3801 match_short(entry
->cAlternateFileName
[0] ? entry
->cAlternateFileName
: entry
->cFileName
,
3806 if (!FindNextFileW(INT21_FindHandle
, entry
))
3808 FindClose(INT21_FindHandle
); INT21_FindHandle
= 0;
3812 WARN("Too many directory entries in %s\n", debugstr_w(fullPath
) );
3816 /******************************************************************
3819 static BOOL
INT21_FindNext( CONTEXT
*context
)
3821 FINDFILE_DTA
*dta
= (FINDFILE_DTA
*)INT21_GetCurrentDTA(context
);
3822 DWORD attr
= dta
->search_attr
| FA_UNUSED
| FA_ARCHIVE
| FA_RDONLY
;
3823 WIN32_FIND_DATAW entry
;
3826 if (!dta
->fullPath
) return FALSE
;
3828 n
= INT21_FindHelper(dta
->fullPath
, dta
->drive
, dta
->count
,
3829 dta
->mask
, attr
, &entry
);
3832 dta
->fileattr
= entry
.dwFileAttributes
;
3833 dta
->filesize
= entry
.nFileSizeLow
;
3834 FileTimeToDosDateTime( &entry
.ftLastWriteTime
, &dta
->filedate
, &dta
->filetime
);
3835 if (entry
.cAlternateFileName
[0])
3836 WideCharToMultiByte(CP_OEMCP
, 0, entry
.cAlternateFileName
, -1,
3837 dta
->filename
, 13, NULL
, NULL
);
3839 WideCharToMultiByte(CP_OEMCP
, 0, entry
.cFileName
, -1, dta
->filename
, 13, NULL
, NULL
);
3841 if (!memchr(dta
->mask
,'?',11))
3843 /* wildcardless search, release resources in case no findnext will
3844 * be issued, and as a workaround in case file creation messes up
3845 * findnext, as sometimes happens with pkunzip
3847 HeapFree( GetProcessHeap(), 0, dta
->fullPath
);
3848 INT21_FindPath
= dta
->fullPath
= NULL
;
3853 HeapFree( GetProcessHeap(), 0, dta
->fullPath
);
3854 INT21_FindPath
= dta
->fullPath
= NULL
;
3858 /* microsoft's programmers should be shot for using CP/M style int21
3859 calls in Windows for Workgroup's winfile.exe */
3861 /******************************************************************
3862 * INT21_FindFirstFCB
3865 static BOOL
INT21_FindFirstFCB( CONTEXT
*context
)
3867 BYTE
*fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3870 WCHAR p
[] = {' ',':',};
3872 if (*fcb
== 0xff) pFCB
= (FINDFILE_FCB
*)(fcb
+ 7);
3873 else pFCB
= (FINDFILE_FCB
*)fcb
;
3874 drive
= INT21_MapDrive( pFCB
->drive
);
3875 if (drive
== MAX_DOS_DRIVES
) return FALSE
;
3878 pFCB
->fullPath
= HeapAlloc(GetProcessHeap(), 0, MAX_PATH
* sizeof(WCHAR
));
3879 if (!pFCB
->fullPath
) return FALSE
;
3880 GetLongPathNameW(p
, pFCB
->fullPath
, MAX_PATH
);
3885 /******************************************************************
3889 static BOOL
INT21_FindNextFCB( CONTEXT
*context
)
3891 BYTE
*fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3893 LPBYTE pResult
= INT21_GetCurrentDTA(context
);
3894 DOS_DIRENTRY_LAYOUT
*ddl
;
3895 WIN32_FIND_DATAW entry
;
3900 if (*fcb
== 0xff) /* extended FCB ? */
3903 pFCB
= (FINDFILE_FCB
*)(fcb
+ 7);
3908 pFCB
= (FINDFILE_FCB
*)fcb
;
3911 if (!pFCB
->fullPath
) return FALSE
;
3912 n
= INT21_FindHelper(pFCB
->fullPath
, INT21_MapDrive( pFCB
->drive
),
3913 pFCB
->count
, pFCB
->filename
, attr
, &entry
);
3916 HeapFree( GetProcessHeap(), 0, pFCB
->fullPath
);
3917 INT21_FindPath
= pFCB
->fullPath
= NULL
;
3924 /* place extended FCB header before pResult if called with extended FCB */
3926 pResult
+= 6; /* leave reserved field behind */
3927 *pResult
++ = entry
.dwFileAttributes
;
3929 *pResult
++ = INT21_MapDrive( pFCB
->drive
); /* DOS_DIRENTRY_LAYOUT after current drive number */
3930 ddl
= (DOS_DIRENTRY_LAYOUT
*)pResult
;
3931 ddl
->fileattr
= entry
.dwFileAttributes
;
3932 ddl
->cluster
= 0; /* what else? */
3933 ddl
->filesize
= entry
.nFileSizeLow
;
3934 memset( ddl
->reserved
, 0, sizeof(ddl
->reserved
) );
3935 FileTimeToDosDateTime( &entry
.ftLastWriteTime
,
3936 &ddl
->filedate
, &ddl
->filetime
);
3938 /* Convert file name to FCB format */
3939 if (entry
.cAlternateFileName
[0])
3940 INT21_ToDosFCBFormat( entry
.cAlternateFileName
, nameW
);
3942 INT21_ToDosFCBFormat( entry
.cFileName
, nameW
);
3943 WideCharToMultiByte(CP_OEMCP
, 0, nameW
, 11, ddl
->filename
, 11, NULL
, NULL
);
3948 /******************************************************************
3949 * INT21_ParseFileNameIntoFCB
3952 static void INT21_ParseFileNameIntoFCB( CONTEXT
*context
)
3955 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
);
3957 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
3961 INT buffer_len
, len
;
3963 SET_AL( context
, 0xff ); /* failed */
3965 TRACE("filename: '%s'\n", filename
);
3968 while (*s
&& (*s
!= ' ') && (*s
!= '\r') && (*s
!= '\n'))
3972 buffer_len
= MultiByteToWideChar(CP_OEMCP
, 0, filename
, len
, NULL
, 0);
3973 buffer
= HeapAlloc( GetProcessHeap(), 0, (buffer_len
+ 1) * sizeof(WCHAR
));
3974 len
= MultiByteToWideChar(CP_OEMCP
, 0, filename
, len
, buffer
, buffer_len
);
3976 INT21_ToDosFCBFormat(buffer
, fcbW
);
3977 HeapFree(GetProcessHeap(), 0, buffer
);
3978 WideCharToMultiByte(CP_OEMCP
, 0, fcbW
, 12, fcb
+ 1, 12, NULL
, NULL
);
3980 TRACE("FCB: '%s'\n", fcb
+ 1);
3982 SET_AL( context
, ((strchr(filename
, '*')) || (strchr(filename
, '$'))) != 0 );
3984 /* point DS:SI to first unparsed character */
3985 SET_SI( context
, context
->Esi
+ (int)s
- (int)filename
);
3988 static BOOL
INT21_Dup2(HFILE16 hFile1
, HFILE16 hFile2
)
3990 HFILE16 res
= HFILE_ERROR16
;
3991 HANDLE handle
, new_handle
;
3992 #define DOS_TABLE_SIZE 256
3993 DWORD map
[DOS_TABLE_SIZE
/ 32];
3996 handle
= DosFileHandleToWin32Handle(hFile1
);
3997 if (handle
== INVALID_HANDLE_VALUE
)
4001 /* now loop to allocate the same one... */
4002 memset(map
, 0, sizeof(map
));
4003 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
4005 if (!DuplicateHandle(GetCurrentProcess(), handle
,
4006 GetCurrentProcess(), &new_handle
,
4007 0, FALSE
, DUPLICATE_SAME_ACCESS
))
4009 res
= HFILE_ERROR16
;
4012 res
= Win32HandleToDosFileHandle(new_handle
);
4013 if (res
== HFILE_ERROR16
|| res
== hFile2
) break;
4014 map
[res
/ 32] |= 1 << (res
% 32);
4016 /* clean up the allocated slots */
4017 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
4019 if (map
[i
/ 32] & (1 << (i
% 32)))
4020 _lclose16((HFILE16
)i
);
4022 return res
== hFile2
;
4026 /***********************************************************************
4027 * DOSVM_Int21Handler
4029 * Interrupt 0x21 handler.
4031 void WINAPI
DOSVM_Int21Handler( CONTEXT
*context
)
4033 BOOL bSetDOSExtendedError
= FALSE
;
4035 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
4036 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
4037 AX_reg(context
), BX_reg(context
),
4038 CX_reg(context
), DX_reg(context
),
4039 SI_reg(context
), DI_reg(context
),
4040 (WORD
)context
->SegDs
, (WORD
)context
->SegEs
,
4044 * Extended error is used by (at least) functions 0x2f to 0x62.
4045 * Function 0x59 returns extended error and error should not
4046 * be cleared before handling the function.
4048 if (AH_reg(context
) >= 0x2f && AH_reg(context
) != 0x59)
4051 RESET_CFLAG(context
); /* Not sure if this is a good idea. */
4053 switch(AH_reg(context
))
4055 case 0x00: /* TERMINATE PROGRAM */
4056 TRACE("TERMINATE PROGRAM\n");
4060 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
4061 TRACE("DIRECT CHARACTER INPUT WITH ECHO - not supported\n");
4064 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
4065 TRACE("Write Character to Standard Output\n");
4068 case 0x03: /* READ CHARACTER FROM STDAUX */
4069 case 0x04: /* WRITE CHARACTER TO STDAUX */
4070 case 0x05: /* WRITE CHARACTER TO PRINTER */
4071 INT_BARF( context
, 0x21 );
4074 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
4075 if (DL_reg(context
) == 0xff)
4077 TRACE("Direct Console Input\n");
4079 /* no character available */
4080 SET_AL( context
, 0 );
4081 SET_ZFLAG( context
);
4085 TRACE("Direct Console Output\n");
4087 * At least DOS versions 2.1-7.0 return character
4088 * that was written in AL register.
4090 SET_AL( context
, DL_reg(context
) );
4094 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
4095 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO - not supported\n");
4096 SET_AL( context
, 0 );
4099 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
4100 TRACE("CHARACTER INPUT WITHOUT ECHO - not supported\n");
4101 SET_AL( context
, 0 );
4104 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
4105 TRACE("WRITE '$'-terminated string from %04X:%04X to stdout\n",
4106 context
->SegDs
, DX_reg(context
) );
4108 LPSTR data
= CTX_SEG_OFF_TO_LIN( context
,
4109 context
->SegDs
, context
->Edx
);
4113 * Do NOT use strchr() to calculate the string length,
4114 * as '\0' is valid string content, too!
4115 * Maybe we should check for non-'$' strings, but DOS doesn't.
4117 while (*p
!= '$') p
++;
4119 WriteFile( DosFileHandleToWin32Handle(1), data
, p
- data
, &w
, NULL
);
4120 SET_AL( context
, '$' ); /* yes, '$' (0x24) gets returned in AL */
4124 case 0x0a: /* BUFFERED INPUT */
4125 TRACE( "BUFFERED INPUT - not supported\n" );
4128 case 0x0b: /* GET STDIN STATUS */
4129 TRACE( "GET STDIN STATUS - not supported\n" );
4130 SET_AL( context
, 0 ); /* no character available */
4133 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
4135 BYTE al
= AL_reg(context
); /* Input function to execute after flush. */
4137 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al
);
4139 /* FIXME: buffers are not flushed */
4142 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
4143 * int21 function identified by AL will be called.
4145 if(al
== 0x01 || al
== 0x06 || al
== 0x07 || al
== 0x08 || al
== 0x0a)
4147 SET_AH( context
, al
);
4148 DOSVM_Int21Handler( context
);
4153 case 0x0d: /* DISK BUFFER FLUSH */
4154 TRACE("DISK BUFFER FLUSH ignored\n");
4157 case 0x0e: /* SELECT DEFAULT DRIVE */
4158 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context
) );
4159 INT21_SetCurrentDrive( DL_reg(context
) );
4160 SET_AL( context
, MAX_DOS_DRIVES
);
4163 case 0x0f: /* OPEN FILE USING FCB */
4164 INT21_OpenFileUsingFCB( context
);
4167 case 0x10: /* CLOSE FILE USING FCB */
4168 INT21_CloseFileUsingFCB( context
);
4171 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
4172 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
4173 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
));
4174 if (!INT21_FindFirstFCB(context
))
4176 SET_AL( context
, 0xff );
4179 /* else fall through */
4181 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
4182 SET_AL( context
, INT21_FindNextFCB(context
) ? 0x00 : 0xff );
4185 case 0x13: /* DELETE FILE USING FCB */
4186 INT_BARF( context
, 0x21 );
4189 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
4190 INT21_SequentialReadFromFCB( context
);
4193 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
4194 INT21_SequentialWriteToFCB( context
);
4197 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
4198 case 0x17: /* RENAME FILE USING FCB */
4199 INT_BARF( context
, 0x21 );
4202 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4203 SET_AL( context
, 0 );
4206 case 0x19: /* GET CURRENT DEFAULT DRIVE */
4207 SET_AL( context
, INT21_GetCurrentDrive() );
4208 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context
) );
4211 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
4212 TRACE( "SET DISK TRANSFER AREA ADDRESS %04X:%04X\n",
4213 context
->SegDs
, DX_reg(context
) );
4215 TDB
*task
= GlobalLock16( GetCurrentTask() );
4216 task
->dta
= MAKESEGPTR( context
->SegDs
, DX_reg(context
) );
4220 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4221 if (!INT21_GetDriveAllocInfo(context
, 0))
4222 SET_AX( context
, 0xffff );
4225 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4226 if (!INT21_GetDriveAllocInfo(context
, DL_reg(context
)))
4227 SET_AX( context
, 0xffff );
4230 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4231 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4232 SET_AL( context
, 0 );
4235 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4237 BYTE drive
= INT21_MapDrive( 0 );
4238 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4240 if (INT21_FillDrivePB( drive
))
4242 SET_AL( context
, 0x00 ); /* success */
4243 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
] ) );
4244 context
->SegDs
= INT21_GetHeapSelector( context
);
4248 SET_AL( context
, 0xff ); /* invalid or network drive */
4253 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4254 SET_AL( context
, 0 );
4257 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4258 INT21_ReadRandomRecordFromFCB( context
);
4261 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4262 INT21_WriteRandomRecordToFCB( context
);
4265 case 0x23: /* GET FILE SIZE FOR FCB */
4266 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4267 INT_BARF( context
, 0x21 );
4270 case 0x25: /* SET INTERRUPT VECTOR */
4271 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context
));
4273 FARPROC16 ptr
= (FARPROC16
)MAKESEGPTR( context
->SegDs
, DX_reg(context
) );
4274 DOSVM_SetPMHandler16( AL_reg(context
), ptr
);
4278 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4279 INT_BARF( context
, 0x21 );
4282 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4283 INT21_RandomBlockReadFromFCB( context
);
4286 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4287 INT21_RandomBlockWriteToFCB( context
);
4290 case 0x29: /* PARSE FILENAME INTO FCB */
4291 INT21_ParseFileNameIntoFCB(context
);
4294 case 0x2a: /* GET SYSTEM DATE */
4295 TRACE( "GET SYSTEM DATE\n" );
4298 GetLocalTime( &systime
);
4299 SET_CX( context
, systime
.wYear
);
4300 SET_DH( context
, systime
.wMonth
);
4301 SET_DL( context
, systime
.wDay
);
4302 SET_AL( context
, systime
.wDayOfWeek
);
4306 case 0x2b: /* SET SYSTEM DATE */
4307 TRACE( "SET SYSTEM DATE\n" );
4309 WORD year
= CX_reg(context
);
4310 BYTE month
= DH_reg(context
);
4311 BYTE day
= DL_reg(context
);
4313 if (year
>= 1980 && year
<= 2099 &&
4314 month
>= 1 && month
<= 12 &&
4315 day
>= 1 && day
<= 31)
4317 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4319 SET_AL( context
, 0 ); /* Let's pretend we succeeded */
4323 SET_AL( context
, 0xff ); /* invalid date */
4324 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4330 case 0x2c: /* GET SYSTEM TIME */
4331 TRACE( "GET SYSTEM TIME\n" );
4334 GetLocalTime( &systime
);
4335 SET_CH( context
, systime
.wHour
);
4336 SET_CL( context
, systime
.wMinute
);
4337 SET_DH( context
, systime
.wSecond
);
4338 SET_DL( context
, systime
.wMilliseconds
/ 10 );
4342 case 0x2d: /* SET SYSTEM TIME */
4343 if( CH_reg(context
) >= 24 || CL_reg(context
) >= 60 || DH_reg(context
) >= 60 || DL_reg(context
) >= 100 ) {
4344 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4345 CH_reg(context
), CL_reg(context
),
4346 DH_reg(context
), DL_reg(context
) );
4347 SET_AL( context
, 0xFF );
4351 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4352 CH_reg(context
), CL_reg(context
),
4353 DH_reg(context
), DL_reg(context
) );
4354 SET_AL( context
, 0 ); /* Let's pretend we succeeded */
4358 case 0x2e: /* SET VERIFY FLAG */
4359 TRACE("SET VERIFY FLAG ignored\n");
4360 /* we cannot change the behaviour anyway, so just ignore it */
4363 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4364 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4366 TDB
*task
= GlobalLock16( GetCurrentTask() );
4367 context
->SegEs
= SELECTOROF( task
->dta
);
4368 SET_BX( context
, OFFSETOF( task
->dta
) );
4372 case 0x30: /* GET DOS VERSION */
4373 TRACE( "GET DOS VERSION - %s requested\n",
4374 (AL_reg(context
) == 0x00) ? "OEM number" : "version flag" );
4376 if (AL_reg(context
) == 0x00)
4377 SET_BH( context
, 0xff ); /* OEM number => undefined */
4379 SET_BH( context
, 0x08 ); /* version flag => DOS is in ROM */
4381 SET_AL( context
, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4382 SET_AH( context
, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4384 SET_BL( context
, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4385 SET_CX( context
, 0x3456 );
4388 case 0x31: /* TERMINATE AND STAY RESIDENT */
4389 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4392 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4394 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
4395 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context
) );
4397 if (INT21_FillDrivePB( drive
))
4399 SET_AL( context
, 0x00 ); /* success */
4400 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
] ) );
4401 context
->SegDs
= INT21_GetHeapSelector( context
);
4405 SET_AL( context
, 0xff ); /* invalid or network drive */
4410 case 0x33: /* MULTIPLEXED */
4411 switch (AL_reg(context
))
4413 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4414 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4415 SET_DL( context
, brk_flag
);
4418 case 0x01: /* SET EXTENDED BREAK STATE */
4419 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4420 brk_flag
= (DL_reg(context
) > 0) ? 1 : 0;
4423 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4424 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4425 /* ugly coding in order to stay reentrant */
4426 if (DL_reg(context
))
4428 SET_DL( context
, brk_flag
);
4433 SET_DL( context
, brk_flag
);
4438 case 0x05: /* GET BOOT DRIVE */
4439 TRACE("GET BOOT DRIVE\n");
4440 SET_DL( context
, 3 );
4441 /* c: is Wine's bootdrive (a: is 1)*/
4444 case 0x06: /* GET TRUE VERSION NUMBER */
4445 TRACE("GET TRUE VERSION NUMBER\n");
4446 SET_BL( context
, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4447 SET_BH( context
, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4448 SET_DL( context
, 0x00 ); /* revision */
4449 SET_DH( context
, 0x08 ); /* DOS is in ROM */
4453 INT_BARF( context
, 0x21 );
4458 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4459 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4460 context
->SegEs
= INT21_GetHeapSelector( context
);
4461 SET_BX( context
, offsetof(INT21_HEAP
, misc_indos
) );
4464 case 0x35: /* GET INTERRUPT VECTOR */
4465 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context
));
4467 FARPROC16 addr
= DOSVM_GetPMHandler16( AL_reg(context
) );
4468 context
->SegEs
= SELECTOROF(addr
);
4469 SET_BX( context
, OFFSETOF(addr
) );
4473 case 0x36: /* GET FREE DISK SPACE */
4474 TRACE("GET FREE DISK SPACE FOR DRIVE %s (limited to about 1GB)\n",
4475 INT21_DriveName( DL_reg(context
) ));
4476 if (!INT21_GetFreeDiskSpace(context
)) SET_AX( context
, 0xffff );
4479 case 0x37: /* SWITCHAR */
4481 switch (AL_reg(context
))
4483 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4484 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4485 SET_AL( context
, 0x00 ); /* success*/
4486 SET_DL( context
, '/' );
4488 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4489 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4491 SET_AL( context
, 0x00 ); /* success*/
4494 INT_BARF( context
, 0x21 );
4500 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4501 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4502 if (AL_reg(context
))
4504 WORD country
= AL_reg(context
);
4505 if (country
== 0xff)
4506 country
= BX_reg(context
);
4507 if (country
!= INT21_GetSystemCountryCode()) {
4508 FIXME( "Requested info on non-default country %04x\n", country
);
4513 if(AX_reg(context
) != 2 )
4515 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context
,
4518 SET_AX( context
, INT21_GetSystemCountryCode() );
4519 SET_BX( context
, INT21_GetSystemCountryCode() );
4520 RESET_CFLAG(context
);
4524 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4525 if (!INT21_CreateDirectory( context
))
4526 bSetDOSExtendedError
= TRUE
;
4528 RESET_CFLAG(context
);
4531 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4533 WCHAR dirW
[MAX_PATH
];
4534 char *dirA
= CTX_SEG_OFF_TO_LIN(context
,
4535 context
->SegDs
, context
->Edx
);
4537 TRACE( "REMOVE DIRECTORY %s\n", dirA
);
4539 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
4541 if (!RemoveDirectoryW( dirW
))
4542 bSetDOSExtendedError
= TRUE
;
4544 RESET_CFLAG(context
);
4548 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4549 if (!INT21_SetCurrentDirectory( context
))
4550 bSetDOSExtendedError
= TRUE
;
4552 RESET_CFLAG(context
);
4555 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4556 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4557 OF_READWRITE
| OF_SHARE_COMPAT
, 0x12 ))
4558 bSetDOSExtendedError
= TRUE
;
4560 RESET_CFLAG(context
);
4563 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4564 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4565 AL_reg(context
), 0x01 ))
4566 bSetDOSExtendedError
= TRUE
;
4568 RESET_CFLAG(context
);
4571 case 0x3e: /* "CLOSE" - CLOSE FILE */
4572 TRACE( "CLOSE handle %d\n", BX_reg(context
) );
4573 if (_lclose16( BX_reg(context
) ) == HFILE_ERROR16
)
4574 bSetDOSExtendedError
= TRUE
;
4576 RESET_CFLAG(context
);
4579 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4580 TRACE( "READ from %d to %04X:%04X for %d bytes\n",
4587 WORD count
= CX_reg(context
);
4588 BYTE
*buffer
= CTX_SEG_OFF_TO_LIN( context
,
4592 /* Some programs pass a count larger than the allocated buffer */
4593 DWORD maxcount
= GetSelectorLimit16( context
->SegDs
) - DX_reg(context
) + 1;
4594 if (count
> maxcount
) count
= maxcount
;
4597 * FIXME: Reading from console (BX=1) in DOS mode
4598 * does not work as it is supposed to work.
4601 RESET_CFLAG(context
); /* set if error */
4602 if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context
)), buffer
, count
, &result
, NULL
))
4603 SET_AX( context
, (WORD
)result
);
4605 bSetDOSExtendedError
= TRUE
;
4609 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4610 TRACE( "WRITE from %04X:%04X to handle %d for %d byte\n",
4611 context
->SegDs
, DX_reg(context
),
4612 BX_reg(context
), CX_reg(context
) );
4614 char *ptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
4615 HFILE handle
= (HFILE
)DosFileHandleToWin32Handle(BX_reg(context
));
4616 LONG result
= _hwrite( handle
, ptr
, CX_reg(context
) );
4617 if (result
== HFILE_ERROR
)
4618 bSetDOSExtendedError
= TRUE
;
4621 SET_AX( context
, (WORD
)result
);
4622 RESET_CFLAG(context
);
4627 case 0x41: /* "UNLINK" - DELETE FILE */
4629 WCHAR fileW
[MAX_PATH
];
4630 char *fileA
= CTX_SEG_OFF_TO_LIN(context
,
4634 TRACE( "UNLINK %s\n", fileA
);
4635 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
4637 if (!DeleteFileW( fileW
))
4638 bSetDOSExtendedError
= TRUE
;
4640 RESET_CFLAG(context
);
4644 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4645 TRACE( "LSEEK handle %d offset %d from %s\n",
4647 MAKELONG( DX_reg(context
), CX_reg(context
) ),
4648 (AL_reg(context
) == 0) ?
4649 "start of file" : ((AL_reg(context
) == 1) ?
4650 "current file position" : "end of file") );
4652 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
4653 LONG offset
= MAKELONG( DX_reg(context
), CX_reg(context
) );
4654 DWORD status
= SetFilePointer( handle
, offset
,
4655 NULL
, AL_reg(context
) );
4656 if (status
== INVALID_SET_FILE_POINTER
)
4657 bSetDOSExtendedError
= TRUE
;
4660 SET_AX( context
, LOWORD(status
) );
4661 SET_DX( context
, HIWORD(status
) );
4662 RESET_CFLAG(context
);
4667 case 0x43: /* FILE ATTRIBUTES */
4668 if (!INT21_FileAttributes( context
, AL_reg(context
), FALSE
))
4669 bSetDOSExtendedError
= TRUE
;
4671 RESET_CFLAG(context
);
4674 case 0x44: /* IOCTL */
4675 INT21_Ioctl( context
);
4678 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4679 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context
) );
4682 HFILE handle16
= HFILE_ERROR
;
4684 if (DuplicateHandle( GetCurrentProcess(),
4685 DosFileHandleToWin32Handle(BX_reg(context
)),
4686 GetCurrentProcess(),
4688 0, TRUE
, DUPLICATE_SAME_ACCESS
))
4689 handle16
= Win32HandleToDosFileHandle(handle32
);
4691 if (handle16
== HFILE_ERROR
)
4692 bSetDOSExtendedError
= TRUE
;
4695 SET_AX( context
, handle16
);
4696 RESET_CFLAG(context
);
4701 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4702 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4703 BX_reg(context
), CX_reg(context
) );
4704 if (!INT21_Dup2(BX_reg(context
), CX_reg(context
)))
4705 bSetDOSExtendedError
= TRUE
;
4707 RESET_CFLAG(context
);
4710 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4711 if (!INT21_GetCurrentDirectory( context
, FALSE
))
4712 bSetDOSExtendedError
= TRUE
;
4714 RESET_CFLAG(context
);
4717 case 0x48: /* ALLOCATE MEMORY */
4718 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context
) );
4720 DWORD bytes
= (DWORD
)BX_reg(context
) << 4;
4721 DWORD rv
= GlobalDOSAlloc16( bytes
);
4722 WORD selector
= LOWORD( rv
);
4726 SET_AX( context
, selector
);
4727 RESET_CFLAG(context
);
4732 SET_AX( context
, 0x0008 ); /* insufficient memory */
4733 SET_BX( context
, DOSMEM_Available() >> 4 );
4738 case 0x49: /* FREE MEMORY */
4739 TRACE( "FREE MEMORY segment %04X\n", context
->SegEs
);
4741 BOOL ok
= !GlobalDOSFree16( context
->SegEs
);
4743 /* If we don't reset ES_reg, we will fail in the relay code */
4744 if (ok
) context
->SegEs
= 0;
4747 TRACE("FREE MEMORY failed\n");
4749 SET_AX( context
, 0x0009 ); /* memory block address invalid */
4754 case 0x4a: /* RESIZE MEMORY BLOCK */
4755 TRACE( "RESIZE MEMORY segment %04X to %d paragraphs\n",
4756 context
->SegEs
, BX_reg(context
) );
4758 FIXME( "Resize memory block - unsupported under Win16\n" );
4763 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4765 char *program
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
4766 HINSTANCE16 instance
;
4768 TRACE( "EXEC %s\n", program
);
4770 RESET_CFLAG(context
);
4771 instance
= WinExec16( program
, SW_NORMAL
);
4774 SET_CFLAG( context
);
4775 SET_AX( context
, instance
);
4780 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
4781 TRACE( "EXIT with return code %d\n", AL_reg(context
) );
4782 DOSVM_Exit( AL_reg(context
) );
4785 case 0x4d: /* GET RETURN CODE */
4786 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
4787 SET_AX( context
, DOSVM_retval
);
4791 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
4792 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context
),
4793 (LPCSTR
)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
));
4794 if (!INT21_FindFirst(context
)) break;
4797 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
4798 TRACE("FINDNEXT\n");
4799 if (!INT21_FindNext(context
))
4801 SetLastError( ERROR_NO_MORE_FILES
);
4802 SET_AX( context
, ERROR_NO_MORE_FILES
);
4805 else SET_AX( context
, 0 ); /* OK */
4808 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
4809 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
4810 DOSVM_psp
= BX_reg(context
);
4813 case 0x51: /* GET PSP ADDRESS */
4814 INT21_GetPSP( context
);
4817 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
4818 TRACE("Get List of Lists - not supported\n");
4820 SET_BX( context
, 0 );
4823 case 0x54: /* Get Verify Flag */
4824 TRACE("Get Verify Flag - Not Supported\n");
4825 SET_AL( context
, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
4828 case 0x56: /* "RENAME" - RENAME FILE */
4829 if (!INT21_RenameFile( context
))
4830 bSetDOSExtendedError
= TRUE
;
4832 RESET_CFLAG(context
);
4835 case 0x57: /* FILE DATE AND TIME */
4836 if (!INT21_FileDateTime( context
))
4837 bSetDOSExtendedError
= TRUE
;
4839 RESET_CFLAG(context
);
4842 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
4843 switch (AL_reg(context
))
4845 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
4846 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
4847 SET_AX( context
, 0 ); /* low memory first fit */
4850 case 0x01: /* SET ALLOCATION STRATEGY */
4851 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
4855 case 0x02: /* GET UMB LINK STATE */
4856 TRACE( "GET UMB LINK STATE\n" );
4857 SET_AL( context
, 0 ); /* UMBs not part of DOS memory chain */
4860 case 0x03: /* SET UMB LINK STATE */
4861 TRACE( "SET UMB LINK STATE to %d - ignored\n",
4866 INT_BARF( context
, 0x21 );
4870 case 0x59: /* GET EXTENDED ERROR INFO */
4871 INT21_GetExtendedError( context
);
4874 case 0x5a: /* CREATE TEMPORARY FILE */
4875 TRACE("CREATE TEMPORARY FILE\n");
4876 bSetDOSExtendedError
= !INT21_CreateTempFile(context
);
4879 case 0x5b: /* CREATE NEW FILE */
4880 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4881 OF_READWRITE
| OF_SHARE_COMPAT
, 0x10 ))
4882 bSetDOSExtendedError
= TRUE
;
4884 RESET_CFLAG(context
);
4887 case 0x5c: /* "FLOCK" - RECORD LOCKING */
4889 DWORD offset
= MAKELONG(DX_reg(context
), CX_reg(context
));
4890 DWORD length
= MAKELONG(DI_reg(context
), SI_reg(context
));
4891 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
4893 RESET_CFLAG(context
);
4894 switch (AL_reg(context
))
4896 case 0x00: /* LOCK */
4897 TRACE( "lock handle %d offset %d length %d\n",
4898 BX_reg(context
), offset
, length
);
4899 if (!LockFile( handle
, offset
, 0, length
, 0 ))
4900 bSetDOSExtendedError
= TRUE
;
4903 case 0x01: /* UNLOCK */
4904 TRACE( "unlock handle %d offset %d length %d\n",
4905 BX_reg(context
), offset
, length
);
4906 if (!UnlockFile( handle
, offset
, 0, length
, 0 ))
4907 bSetDOSExtendedError
= TRUE
;
4911 INT_BARF( context
, 0x21 );
4916 case 0x5d: /* NETWORK 5D */
4917 FIXME( "Network function 5D not implemented.\n" );
4918 SetLastError( ER_NoNetwork
);
4919 bSetDOSExtendedError
= TRUE
;
4922 case 0x5e: /* NETWORK 5E */
4923 bSetDOSExtendedError
= INT21_NetworkFunc( context
);
4926 case 0x5f: /* NETWORK 5F */
4927 /* FIXME: supporting this would need to 1:
4928 * - implement per drive current directory (as kernel32 doesn't)
4929 * - assign enabled/disabled flag on a per drive basis
4931 /* network software not installed */
4932 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context
));
4933 SetLastError( ER_NoNetwork
);
4934 bSetDOSExtendedError
= TRUE
;
4937 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
4939 WCHAR pathW
[MAX_PATH
], res
[MAX_PATH
];
4940 /* FIXME: likely to be broken */
4942 TRACE("TRUENAME %s\n",
4943 (LPCSTR
)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Esi
));
4944 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
4945 if (!GetFullPathNameW( pathW
, 128, res
, NULL
))
4946 bSetDOSExtendedError
= TRUE
;
4949 SET_AX( context
, 0 );
4950 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
4951 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
4957 case 0x61: /* UNUSED */
4958 SET_AL( context
, 0 );
4961 case 0x62: /* GET PSP ADDRESS */
4962 INT21_GetPSP( context
);
4965 case 0x63: /* MISC. LANGUAGE SUPPORT */
4966 switch (AL_reg(context
)) {
4967 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
4968 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
4969 context
->SegDs
= INT21_GetHeapSelector( context
);
4970 SET_SI( context
, offsetof(INT21_HEAP
, dbcs_table
) );
4971 SET_AL( context
, 0 ); /* success */
4976 case 0x64: /* OS/2 DOS BOX */
4977 INT_BARF( context
, 0x21 );
4981 case 0x65: /* EXTENDED COUNTRY INFORMATION */
4982 INT21_ExtendedCountryInformation( context
);
4985 case 0x66: /* GLOBAL CODE PAGE TABLE */
4986 switch (AL_reg(context
))
4989 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
4990 SET_BX( context
, GetOEMCP() );
4991 SET_DX( context
, GetOEMCP() );
4994 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
4995 BX_reg(context
), DX_reg(context
) );
5000 case 0x67: /* SET HANDLE COUNT */
5001 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context
) );
5002 if (SetHandleCount( BX_reg(context
) ) < BX_reg(context
) )
5003 bSetDOSExtendedError
= TRUE
;
5006 case 0x68: /* "FFLUSH" - COMMIT FILE */
5007 TRACE( "FFLUSH - handle %d\n", BX_reg(context
) );
5008 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context
)) ) &&
5009 GetLastError() != ERROR_ACCESS_DENIED
)
5010 bSetDOSExtendedError
= TRUE
;
5013 case 0x69: /* DISK SERIAL NUMBER */
5014 switch (AL_reg(context
))
5017 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
5018 INT21_DriveName(BL_reg(context
)));
5019 if (!INT21_GetDiskSerialNumber(context
)) bSetDOSExtendedError
= TRUE
;
5020 else SET_AX( context
, 0 );
5024 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
5025 INT21_DriveName(BL_reg(context
)));
5026 if (!INT21_SetDiskSerialNumber(context
)) bSetDOSExtendedError
= TRUE
;
5027 else SET_AX( context
, 1 );
5032 case 0x6a: /* COMMIT FILE */
5033 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context
) );
5034 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context
)) ) &&
5035 GetLastError() != ERROR_ACCESS_DENIED
)
5036 bSetDOSExtendedError
= TRUE
;
5039 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
5040 SET_AL( context
, 0 );
5043 case 0x6c: /* EXTENDED OPEN/CREATE */
5044 if (!INT21_CreateFile( context
, context
->Esi
, TRUE
,
5045 BX_reg(context
), DL_reg(context
) ))
5046 bSetDOSExtendedError
= TRUE
;
5049 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
5050 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
5051 SET_CFLAG( context
);
5052 SET_AL( context
, 0 );
5055 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
5056 INT21_LongFilename( context
);
5059 case 0x73: /* MSDOS7 - FAT32 */
5060 RESET_CFLAG( context
);
5061 if (!INT21_Fat32( context
))
5062 bSetDOSExtendedError
= TRUE
;
5065 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
5066 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
5069 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
5070 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
5073 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
5074 /* we don't implement a DOS32 extender */
5075 TRACE( "DOS32 EXTENDER API - ignored\n" );
5079 INT_BARF( context
, 0x21 );
5082 } /* END OF SWITCH */
5084 /* Set general error condition. */
5085 if (bSetDOSExtendedError
)
5087 SET_AX( context
, GetLastError() );
5088 SET_CFLAG( context
);
5091 /* Print error code if carry flag is set. */
5092 if (context
->EFlags
& 0x0001)
5093 TRACE("failed, error %d\n", GetLastError() );
5095 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
5096 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
5097 AX_reg(context
), BX_reg(context
),
5098 CX_reg(context
), DX_reg(context
),
5099 SI_reg(context
), DI_reg(context
),
5100 (WORD
)context
->SegDs
, (WORD
)context
->SegEs
,