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
35 #include "wine/winbase16.h"
36 #include "kernel16_private.h"
40 #include "wine/debug.h"
44 * - Most of the file related functions are wrong. NT's kernel32
45 * doesn't maintain a per drive current directory, while DOS does.
46 * We should in fact keep track in here of those per drive
47 * directories, and use this info while dealing with partial paths
48 * (drive defined, but only relative paths). This could even be
49 * created as an array of CDS (there should be an entry for that in
54 * Forward declarations.
56 static BOOL
INT21_RenameFile( CONTEXT
*context
);
58 WINE_DEFAULT_DEBUG_CHANNEL(int21
);
64 * Extended Drive Parameter Block.
65 * This structure is compatible with standard DOS4+ DPB and
68 typedef struct _INT21_DPB
{
69 BYTE drive
; /* 00 drive number (0=A, ...) */
70 BYTE unit
; /* 01 unit number within device driver */
71 WORD sector_bytes
; /* 02 bytes per sector */
72 BYTE cluster_sectors
; /* 04 highest sector number within a cluster */
73 BYTE shift
; /* 05 shift count to convert clusters into sectors */
74 WORD num_reserved
; /* 06 reserved sectors at beginning of drive */
75 BYTE num_FAT
; /* 08 number of FATs */
76 WORD num_root_entries
; /* 09 number of root directory entries */
77 WORD first_data_sector
; /* 0b number of first sector containing user data */
78 WORD num_clusters1
; /* 0d highest cluster number (number of data clusters + 1) */
79 WORD sectors_per_FAT
; /* 0f number of sectors per FAT */
80 WORD first_dir_sector
; /* 11 sector number of first directory sector */
81 SEGPTR driver_header
; /* 13 address of device driver header */
82 BYTE media_ID
; /* 17 media ID byte */
83 BYTE access_flag
; /* 18 0x00 if disk accessed, 0xff if not */
84 SEGPTR next
; /* 19 pointer to next DPB */
85 WORD search_cluster1
; /* 1d cluster at which to start search for free space */
86 WORD free_clusters_lo
; /* 1f number of free clusters on drive or 0xffff if unknown */
87 WORD free_clusters_hi
; /* 21 hiword of clusters_free */
88 WORD mirroring_flags
; /* 23 active FAT/mirroring flags */
89 WORD info_sector
; /* 25 sector number of file system info sector or 0xffff for none */
90 WORD spare_boot_sector
; /* 27 sector number of backup boot sector or 0xffff for none */
91 DWORD first_cluster_sector
; /* 29 sector number of the first cluster */
92 DWORD num_clusters2
; /* 2d maximum cluster number */
93 DWORD fat_clusters
; /* 31 number of clusters occupied by FAT */
94 DWORD root_cluster
; /* 35 cluster number of start of root directory */
95 DWORD search_cluster2
; /* 39 cluster at which to start searching for free space */
100 * Structure for DOS data that can be accessed directly from applications.
101 * Real and protected mode pointers will be returned to this structure so
102 * the structure must be correctly packed.
104 typedef struct _INT21_HEAP
{
105 WORD uppercase_size
; /* Size of the following table in bytes */
106 BYTE uppercase_table
[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
108 WORD lowercase_size
; /* Size of the following table in bytes */
109 BYTE lowercase_table
[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
111 WORD collating_size
; /* Size of the following table in bytes */
112 BYTE collating_table
[256]; /* Values used to sort characters from 0x00 to 0xff. */
114 WORD filename_size
; /* Size of the following filename data in bytes */
115 BYTE filename_reserved1
; /* 0x01 for MS-DOS 3.30-6.00 */
116 BYTE filename_lowest
; /* Lowest permissible character value for filename */
117 BYTE filename_highest
; /* Highest permissible character value for filename */
118 BYTE filename_reserved2
; /* 0x00 for MS-DOS 3.30-6.00 */
119 BYTE filename_exclude_first
; /* First illegal character in permissible range */
120 BYTE filename_exclude_last
; /* Last illegal character in permissible range */
121 BYTE filename_reserved3
; /* 0x02 for MS-DOS 3.30-6.00 */
122 BYTE filename_illegal_size
; /* Number of terminators in the following table */
123 BYTE filename_illegal_table
[16]; /* Characters which terminate a filename */
125 WORD dbcs_size
; /* Number of valid ranges in the following table */
126 BYTE dbcs_table
[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
128 BYTE misc_indos
; /* Interrupt 21 nesting flag */
129 WORD misc_selector
; /* Protected mode selector for INT21_HEAP */
130 INT21_DPB misc_dpb_list
[MAX_DOS_DRIVES
]; /* Drive parameter blocks for all drives */
138 CHAR file_extension
[3];
139 WORD current_block_number
;
140 WORD logical_record_size
;
142 WORD date_of_last_write
;
143 WORD time_of_last_write
;
146 WORD starting_cluster
;
147 WORD sequence_number
;
148 BYTE file_attributes
;
150 BYTE record_within_current_block
;
151 BYTE random_access_record_number
[4];
158 BYTE xfcb_file_attribute
;
162 /* DTA layout for FindFirst/FindNext */
165 BYTE drive
; /* 00 drive letter */
166 char mask
[11]; /* 01 search template */
167 BYTE search_attr
; /* 0c search attributes */
168 WORD count
; /* 0d entry count within directory */
169 WORD cluster
; /* 0f cluster of parent directory */
170 WCHAR
*fullPath
; /* 11 full path (was: reserved) */
171 BYTE fileattr
; /* 15 file attributes */
172 WORD filetime
; /* 16 file time */
173 WORD filedate
; /* 18 file date */
174 DWORD filesize
; /* 1a file size */
175 char filename
[13]; /* 1e file name + extension */
178 /* FCB layout for FindFirstFCB/FindNextFCB */
181 BYTE drive
; /* 00 drive letter */
182 char filename
[11]; /* 01 filename 8+3 format */
183 int count
; /* 0c entry count (was: reserved) */
184 WCHAR
*fullPath
; /* 10 full path (was: reserved) */
187 /* DOS directory entry for FindFirstFCB/FindNextFCB */
190 char filename
[11]; /* 00 filename 8+3 format */
191 BYTE fileattr
; /* 0b file attributes */
192 BYTE reserved
[10]; /* 0c reserved */
193 WORD filetime
; /* 16 file time */
194 WORD filedate
; /* 18 file date */
195 WORD cluster
; /* 1a file first cluster */
196 DWORD filesize
; /* 1c file size */
197 } DOS_DIRENTRY_LAYOUT
;
201 /* dos file attributes */
202 #define FA_NORMAL 0x00 /* Normal file, no attributes */
203 #define FA_RDONLY 0x01 /* Read only attribute */
204 #define FA_HIDDEN 0x02 /* Hidden file */
205 #define FA_SYSTEM 0x04 /* System file */
206 #define FA_LABEL 0x08 /* Volume label */
207 #define FA_DIRECTORY 0x10 /* Directory */
208 #define FA_ARCHIVE 0x20 /* Archive */
209 #define FA_UNUSED 0x40 /* Unused */
212 #define ER_NoNetwork 0x49
215 #define EC_OutOfResource 0x01
216 #define EC_Temporary 0x02
217 #define EC_AccessDenied 0x03
218 #define EC_InternalError 0x04
219 #define EC_HardwareFailure 0x05
220 #define EC_SystemFailure 0x06
221 #define EC_ProgramError 0x07
222 #define EC_NotFound 0x08
223 #define EC_MediaError 0x0b
224 #define EC_Exists 0x0c
225 #define EC_Unknown 0x0d
227 /* Suggested actions */
228 #define SA_Retry 0x01
229 #define SA_DelayedRetry 0x02
230 #define SA_Abort 0x04
231 #define SA_Ignore 0x06
232 #define SA_Ask4Retry 0x07
235 #define EL_Unknown 0x01
237 #define EL_Network 0x03
238 #define EL_Serial 0x04
239 #define EL_Memory 0x05
241 /* BIOS Keyboard Scancodes */
242 #define KEY_LEFT 0x4B
243 #define KEY_RIGHT 0x4D
245 #define KEY_DOWN 0x50
246 #define KEY_IC 0x52 /* insert char */
247 #define KEY_DC 0x53 /* delete char */
248 #define KEY_BACKSPACE 0x0E
249 #define KEY_HOME 0x47
251 #define KEY_NPAGE 0x49
252 #define KEY_PPAGE 0x51
256 static BYTE
drive_number( WCHAR letter
)
258 if (letter
>= 'A' && letter
<= 'Z') return letter
- 'A';
259 if (letter
>= 'a' && letter
<= 'z') return letter
- 'a';
260 return MAX_DOS_DRIVES
;
264 /* Many calls translate a drive argument like this:
265 drive number (00h = default, 01h = A:, etc)
267 /******************************************************************
270 * Many calls translate a drive argument like this:
271 * drive number (00h = default, 01h = A:, etc)
273 static const char *INT21_DriveName(int drive
)
277 if (drive
<= 26) return wine_dbg_sprintf("%c:", 'A' + drive
- 1);
278 else return wine_dbg_sprintf( "<Bad drive: %d>", drive
);
283 /***********************************************************************
284 * INT21_GetCurrentDrive
286 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
287 * MAX_DOS_DRIVES on error.
289 static BYTE
INT21_GetCurrentDrive(void)
291 WCHAR current_directory
[MAX_PATH
];
293 if (!GetCurrentDirectoryW( MAX_PATH
, current_directory
) ||
294 current_directory
[1] != ':')
296 TRACE( "Failed to get current drive.\n" );
297 return MAX_DOS_DRIVES
;
299 return drive_number( current_directory
[0] );
303 /***********************************************************************
306 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
307 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
309 static BYTE
INT21_MapDrive( BYTE drive
)
313 WCHAR drivespec
[] = {'A', ':', 0};
316 drivespec
[0] += drive
- 1;
317 drivetype
= GetDriveTypeW( drivespec
);
319 if (drivetype
== DRIVE_UNKNOWN
|| drivetype
== DRIVE_NO_ROOT_DIR
)
320 return MAX_DOS_DRIVES
;
325 return INT21_GetCurrentDrive();
329 /***********************************************************************
330 * INT21_SetCurrentDrive
332 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
334 static void INT21_SetCurrentDrive( BYTE drive
)
336 WCHAR drivespec
[] = {'A', ':', 0};
338 drivespec
[0] += drive
;
340 if (!SetCurrentDirectoryW( drivespec
))
341 TRACE( "Failed to set current drive.\n" );
345 /***********************************************************************
346 * INT21_GetSystemCountryCode
348 * Return DOS country code for default system locale.
350 static WORD
INT21_GetSystemCountryCode( void )
352 return GetSystemDefaultLangID();
356 /***********************************************************************
357 * INT21_FillCountryInformation
359 * Fill 34-byte buffer with country information data using
360 * default system locale.
362 static void INT21_FillCountryInformation( BYTE
*buffer
)
364 /* 00 - WORD: date format
369 *(WORD
*)(buffer
+ 0) = 0; /* FIXME: Get from locale */
371 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
372 buffer
[2] = '$'; /* FIXME: Get from locale */
375 /* 07 - BYTE[2]: ASCIIZ thousands separator */
376 buffer
[7] = 0; /* FIXME: Get from locale */
379 /* 09 - BYTE[2]: ASCIIZ decimal separator */
380 buffer
[9] = '.'; /* FIXME: Get from locale */
383 /* 11 - BYTE[2]: ASCIIZ date separator */
384 buffer
[11] = '/'; /* FIXME: Get from locale */
387 /* 13 - BYTE[2]: ASCIIZ time separator */
388 buffer
[13] = ':'; /* FIXME: Get from locale */
391 /* 15 - BYTE: Currency format
392 * bit 2 = set if currency symbol replaces decimal point
393 * bit 1 = number of spaces between value and currency symbol
394 * bit 0 = 0 if currency symbol precedes value
395 * 1 if currency symbol follows value
397 buffer
[15] = 0; /* FIXME: Get from locale */
399 /* 16 - BYTE: Number of digits after decimal in currency */
400 buffer
[16] = 0; /* FIXME: Get from locale */
402 /* 17 - BYTE: Time format
403 * bit 0 = 0 if 12-hour clock
406 buffer
[17] = 1; /* FIXME: Get from locale */
408 /* 18 - DWORD: Address of case map routine */
409 *(DWORD
*)(buffer
+ 18) = 0; /* FIXME: ptr to case map routine */
411 /* 22 - BYTE[2]: ASCIIZ data-list separator */
412 buffer
[22] = ','; /* FIXME: Get from locale */
415 /* 24 - BYTE[10]: Reserved */
416 memset( buffer
+ 24, 0, 10 );
420 /***********************************************************************
423 * Initialize DOS heap.
425 * Filename Terminator Table of w2k DE NTVDM:
426 * 16 00 01 00 FF 00 00 20-02 0E 2E 22 2F 5C 5B 5D ....... ..."/\[]
427 * 3A 7C 3C 3E 2B 3D 3B 2C-00 :|<>+=;,
429 static void INT21_FillHeap( INT21_HEAP
*heap
)
431 static const char terminators
[] = ".\"/\\[]:|<>+=;,";
437 heap
->uppercase_size
= 128;
438 for (i
= 0; i
< 128; i
++)
439 heap
->uppercase_table
[i
] = toupper( 128 + i
);
444 heap
->lowercase_size
= 256;
445 for (i
= 0; i
< 256; i
++)
446 heap
->lowercase_table
[i
] = tolower( i
);
451 heap
->collating_size
= 256;
452 for (i
= 0; i
< 256; i
++)
453 heap
->collating_table
[i
] = i
;
458 heap
->filename_size
= 8 + strlen(terminators
);
459 heap
->filename_illegal_size
= strlen(terminators
);
460 memcpy( heap
->filename_illegal_table
, terminators
, heap
->filename_illegal_size
);
462 heap
->filename_reserved1
= 0x01;
463 heap
->filename_lowest
= 0x00;
464 heap
->filename_highest
= 0xff;
465 heap
->filename_reserved2
= 0x00;
466 heap
->filename_exclude_first
= 0x00;
467 heap
->filename_exclude_last
= 0x20;
468 heap
->filename_reserved3
= 0x02;
471 * DBCS lead byte table. This table is empty.
474 memset( heap
->dbcs_table
, 0, sizeof(heap
->dbcs_table
) );
477 * Initialize InDos flag.
479 heap
->misc_indos
= 0;
482 * FIXME: Should drive parameter blocks (DPB) be
483 * initialized here and linked to DOS LOL?
488 /***********************************************************************
489 * INT21_GetHeapPointer
491 * Get pointer for DOS heap (INT21_HEAP).
492 * Creates and initializes heap on first call.
494 static INT21_HEAP
*INT21_GetHeapPointer( void )
496 static INT21_HEAP
*heap_pointer
= NULL
;
500 WORD heap_selector
= GlobalAlloc16( GMEM_FIXED
, sizeof(INT21_HEAP
) );
501 heap_pointer
= GlobalLock16( heap_selector
);
502 heap_pointer
->misc_selector
= heap_selector
;
503 INT21_FillHeap( heap_pointer
);
510 /***********************************************************************
511 * INT21_GetHeapSelector
513 * Get segment/selector for DOS heap (INT21_HEAP).
514 * Creates and initializes heap on first call.
516 static WORD
INT21_GetHeapSelector( CONTEXT
*context
)
518 INT21_HEAP
*heap
= INT21_GetHeapPointer();
519 return heap
->misc_selector
;
523 /***********************************************************************
526 * Fill DOS heap drive parameter block for the specified drive.
527 * Return TRUE if drive was valid and there were
528 * no errors while reading drive information.
530 static BOOL
INT21_FillDrivePB( BYTE drive
)
532 WCHAR drivespec
[] = {'A', ':', 0};
533 INT21_HEAP
*heap
= INT21_GetHeapPointer();
536 DWORD cluster_sectors
;
539 DWORD total_clusters
;
541 if (drive
>= MAX_DOS_DRIVES
)
544 dpb
= &heap
->misc_dpb_list
[drive
];
545 drivespec
[0] += drive
;
546 drivetype
= GetDriveTypeW( drivespec
);
549 * FIXME: Does this check work correctly with floppy/cdrom drives?
551 if (drivetype
== DRIVE_NO_ROOT_DIR
|| drivetype
== DRIVE_UNKNOWN
)
555 * FIXME: Does this check work correctly with floppy/cdrom drives?
557 if (!GetDiskFreeSpaceW( drivespec
, &cluster_sectors
, §or_bytes
,
558 &free_clusters
, &total_clusters
))
562 * FIXME: Most of the values listed below are incorrect.
563 * All values should be validated.
568 dpb
->sector_bytes
= sector_bytes
;
569 dpb
->cluster_sectors
= cluster_sectors
- 1;
572 while (cluster_sectors
> 1)
574 cluster_sectors
/= 2;
578 dpb
->num_reserved
= 0;
580 dpb
->num_root_entries
= 2;
581 dpb
->first_data_sector
= 2;
582 dpb
->num_clusters1
= total_clusters
;
583 dpb
->sectors_per_FAT
= 1;
584 dpb
->first_dir_sector
= 1;
585 dpb
->driver_header
= 0;
586 dpb
->media_ID
= (drivetype
== DRIVE_FIXED
) ? 0xF8 : 0xF0;
587 dpb
->access_flag
= 0;
589 dpb
->search_cluster1
= 0;
590 dpb
->free_clusters_lo
= LOWORD(free_clusters
);
591 dpb
->free_clusters_hi
= HIWORD(free_clusters
);
592 dpb
->mirroring_flags
= 0;
593 dpb
->info_sector
= 0xffff;
594 dpb
->spare_boot_sector
= 0xffff;
595 dpb
->first_cluster_sector
= 0;
596 dpb
->num_clusters2
= total_clusters
;
597 dpb
->fat_clusters
= 32;
598 dpb
->root_cluster
= 0;
599 dpb
->search_cluster2
= 0;
605 /***********************************************************************
606 * INT21_GetCurrentDirectory
610 * - subfunction 0x47 of function 0x71
612 static BOOL
INT21_GetCurrentDirectory( CONTEXT
*context
, BOOL islong
)
614 char *buffer
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
);
615 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
616 WCHAR pathW
[MAX_PATH
];
617 char pathA
[MAX_PATH
];
620 TRACE( "drive %d\n", DL_reg(context
) );
622 if (drive
== MAX_DOS_DRIVES
)
624 SetLastError(ERROR_INVALID_DRIVE
);
629 * Grab current directory.
632 if (!GetCurrentDirectoryW( MAX_PATH
, pathW
)) return FALSE
;
634 if (drive_number( pathW
[0] ) != drive
|| pathW
[1] != ':')
636 /* cwd is not on the requested drive, get the environment string instead */
641 env_var
[1] = 'A' + drive
;
644 if (!GetEnvironmentVariableW( env_var
, pathW
, MAX_PATH
))
646 /* return empty path */
653 * Convert into short format.
658 DWORD result
= GetShortPathNameW( pathW
, pathW
, MAX_PATH
);
661 if (result
> MAX_PATH
)
663 WARN( "Short path too long!\n" );
664 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
670 * The returned pathname does not include
671 * the drive letter, colon or leading backslash.
677 * FIXME: We should probably just strip host part from name...
679 FIXME( "UNC names are not supported.\n" );
680 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
683 else if (!ptr
[0] || ptr
[1] != ':' || ptr
[2] != '\\')
685 WARN( "Path is neither UNC nor DOS path: %s\n",
686 wine_dbgstr_w(ptr
) );
687 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
692 /* Remove drive letter, colon and leading backslash. */
697 * Convert into OEM string.
700 if (!WideCharToMultiByte(CP_OEMCP
, 0, ptr
, -1, pathA
,
701 MAX_PATH
, NULL
, NULL
))
703 WARN( "Cannot convert path!\n" );
704 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
714 /* Undocumented success code. */
715 SET_AX( context
, 0x0100 );
717 /* Truncate buffer to 64 bytes. */
721 TRACE( "%c:=%s\n", 'A' + drive
, pathA
);
723 strcpy( buffer
, pathA
);
728 /***********************************************************************
729 * INT21_SetCurrentDirectory
733 * - subfunction 0x3b of function 0x71
735 static BOOL
INT21_SetCurrentDirectory( CONTEXT
*context
)
737 WCHAR dirW
[MAX_PATH
];
740 char *dirA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
741 BYTE drive
= INT21_GetCurrentDrive();
744 TRACE( "SET CURRENT DIRECTORY %s\n", dirA
);
746 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
747 if (!GetFullPathNameW( dirW
, MAX_PATH
, dirW
, NULL
)) return FALSE
;
749 attr
= GetFileAttributesW( dirW
);
750 if (attr
== INVALID_FILE_ATTRIBUTES
|| !(attr
& FILE_ATTRIBUTE_DIRECTORY
))
752 SetLastError( ERROR_PATH_NOT_FOUND
);
757 env_var
[1] = dirW
[0];
760 result
= SetEnvironmentVariableW( env_var
, dirW
);
762 /* only set current directory if on the current drive */
763 if (result
&& (drive_number( dirW
[0] ) == drive
)) result
= SetCurrentDirectoryW( dirW
);
768 /***********************************************************************
776 * - subfunction 0x6c of function 0x71
778 static BOOL
INT21_CreateFile( CONTEXT
*context
,
785 char *pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, pathSegOff
);
786 WCHAR pathW
[MAX_PATH
];
793 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
794 "create flags=%04x, file=%s.\n",
795 AH_reg(context
), dosAction
, dosAccessShare
, CX_reg(context
), pathA
);
798 * Application tried to create/open a file whose name
799 * ends with a backslash. This is not allowed.
801 * FIXME: This needs to be validated, especially the return value.
803 if (pathA
[strlen(pathA
) - 1] == '/')
805 SetLastError( ERROR_FILE_NOT_FOUND
);
810 * Convert DOS action flags into Win32 creation disposition parameter.
815 winMode
= OPEN_EXISTING
;
818 winMode
= TRUNCATE_EXISTING
;
821 winMode
= CREATE_NEW
;
824 winMode
= OPEN_ALWAYS
;
827 winMode
= CREATE_ALWAYS
;
830 SetLastError( ERROR_INVALID_PARAMETER
);
835 * Convert DOS access/share flags into Win32 desired access parameter.
837 switch(dosAccessShare
& 0x07)
840 winAccess
= GENERIC_READ
;
843 winAccess
= GENERIC_WRITE
;
846 winAccess
= GENERIC_READ
| GENERIC_WRITE
;
850 * Read-only, do not modify file's last-access time (DOS7).
852 * FIXME: How to prevent modification of last-access time?
854 winAccess
= GENERIC_READ
;
861 * Convert DOS access/share flags into Win32 share mode parameter.
863 switch(dosAccessShare
& 0x70)
865 case OF_SHARE_EXCLUSIVE
:
868 case OF_SHARE_DENY_WRITE
:
869 winSharing
= FILE_SHARE_READ
;
871 case OF_SHARE_DENY_READ
:
872 winSharing
= FILE_SHARE_WRITE
;
874 case OF_SHARE_DENY_NONE
:
875 case OF_SHARE_COMPAT
:
877 winSharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
881 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
882 * What to do with this bit?
883 * FIXME: Bits in the high byte of dosAccessShare are not supported.
884 * See both function 0x6c and subfunction 0x6c of function 0x71 for
885 * definition of these bits.
889 * Convert DOS create attributes into Win32 flags and attributes parameter.
891 if (winMode
== OPEN_EXISTING
|| winMode
== TRUNCATE_EXISTING
)
897 WORD dosAttributes
= CX_reg(context
);
899 if (dosAttributes
& FA_LABEL
)
902 * Application tried to create volume label entry.
903 * This is difficult to support so we do not allow it.
905 * FIXME: If volume does not already have a label,
906 * this function is supposed to succeed.
908 SetLastError( ERROR_ACCESS_DENIED
);
912 winAttributes
= dosAttributes
&
913 (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
|
914 FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_ARCHIVE
);
920 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
922 winHandle
= CreateFileW( pathW
, winAccess
, winSharing
, NULL
, winMode
, winAttributes
, 0 );
923 /* DOS allows opening files on a CDROM R/W */
924 if( winHandle
== INVALID_HANDLE_VALUE
&&
925 (GetLastError() == ERROR_WRITE_PROTECT
||
926 GetLastError() == ERROR_ACCESS_DENIED
)) {
927 winHandle
= CreateFileW( pathW
, winAccess
& ~GENERIC_WRITE
,
928 winSharing
, NULL
, winMode
, winAttributes
, 0 );
931 if (winHandle
== INVALID_HANDLE_VALUE
)
935 * Determine DOS file status.
946 case TRUNCATE_EXISTING
:
953 dosStatus
= (GetLastError() == ERROR_ALREADY_EXISTS
) ? 1 : 2;
956 dosStatus
= (GetLastError() == ERROR_ALREADY_EXISTS
) ? 3 : 2;
963 * Return DOS file handle and DOS status.
965 SET_AX( context
, Win32HandleToDosFileHandle(winHandle
) );
968 SET_CX( context
, dosStatus
);
970 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
971 AX_reg(context
), dosStatus
);
977 /***********************************************************************
978 * INT21_GetCurrentDTA
980 static BYTE
*INT21_GetCurrentDTA( CONTEXT
*context
)
982 TDB
*pTask
= GlobalLock16(GetCurrentTask());
984 /* FIXME: This assumes DTA was set correctly! */
985 return CTX_SEG_OFF_TO_LIN( context
, SELECTOROF(pTask
->dta
),
986 OFFSETOF(pTask
->dta
) );
990 /***********************************************************************
991 * INT21_OpenFileUsingFCB
993 * Handler for function 0x0f.
996 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
1003 * Opens a FCB file for read/write in compatibility mode. Upon calling
1004 * the FCB must have the drive_number, file_name, and file_extension
1005 * fields filled and all other bytes cleared.
1007 static void INT21_OpenFileUsingFCB( CONTEXT
*context
)
1015 BY_HANDLE_FILE_INFORMATION info
;
1018 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1019 if (fcb
->drive_number
== 0xff) {
1020 xfcb
= (struct XFCB
*) fcb
;
1021 fcb
= (struct FCB
*) xfcb
->fcb
;
1025 file_path
[0] = 'A' + INT21_MapDrive( fcb
->drive_number
);
1027 if (AL_result
== 0) {
1029 pos
= &file_path
[2];
1030 memcpy(pos
, fcb
->file_name
, 8);
1033 pos
= strchr(pos
, ' ');
1036 memcpy(pos
, fcb
->file_extension
, 3);
1039 pos
= strchr(pos
, ' ');
1042 handle
= (HANDLE
) _lopen(file_path
, OF_READWRITE
);
1043 if (handle
== INVALID_HANDLE_VALUE
) {
1044 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path
);
1045 AL_result
= 0xff; /* failed */
1047 hfile16
= Win32HandleToDosFileHandle(handle
);
1048 if (hfile16
== HFILE_ERROR16
) {
1049 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle
);
1050 CloseHandle(handle
);
1051 AL_result
= 0xff; /* failed */
1052 } else if (hfile16
> 255) {
1053 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16
, file_path
);
1055 AL_result
= 0xff; /* failed */
1057 if (!GetFileInformationByHandle(handle
, &info
)) {
1058 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1059 hfile16
, handle
, file_path
);
1061 AL_result
= 0xff; /* failed */
1063 fcb
->drive_number
= file_path
[0] - 'A' + 1;
1064 fcb
->current_block_number
= 0;
1065 fcb
->logical_record_size
= 128;
1066 fcb
->file_size
= info
.nFileSizeLow
;
1067 FileTimeToDosDateTime(&info
.ftLastWriteTime
,
1068 &fcb
->date_of_last_write
, &fcb
->time_of_last_write
);
1069 fcb
->file_number
= hfile16
;
1070 fcb
->attributes
= 0xc2;
1071 fcb
->starting_cluster
= 0; /* don't know correct init value */
1072 fcb
->sequence_number
= 0; /* don't know correct init value */
1073 fcb
->file_attributes
= info
.dwFileAttributes
;
1074 /* The following fields are not initialized */
1075 /* by the native function: */
1077 /* record_within_current_block */
1078 /* random_access_record_number */
1080 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1081 file_path
, hfile16
, handle
);
1082 AL_result
= 0x00; /* successful */
1087 SET_AL(context
, AL_result
);
1091 /***********************************************************************
1092 * INT21_CloseFileUsingFCB
1094 * Handler for function 0x10.
1097 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1104 * Closes a FCB file.
1106 static void INT21_CloseFileUsingFCB( CONTEXT
*context
)
1112 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1113 if (fcb
->drive_number
== 0xff) {
1114 xfcb
= (struct XFCB
*) fcb
;
1115 fcb
= (struct FCB
*) xfcb
->fcb
;
1118 if (_lclose16((HFILE16
) fcb
->file_number
) != 0) {
1119 TRACE("_lclose16(%d) failed\n", fcb
->file_number
);
1120 AL_result
= 0xff; /* failed */
1122 TRACE("successful closed file %d\n", fcb
->file_number
);
1123 AL_result
= 0x00; /* successful */
1125 SET_AL(context
, AL_result
);
1129 /***********************************************************************
1130 * INT21_SequentialReadFromFCB
1132 * Handler for function 0x14.
1135 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1139 * 1: end of file, no data read
1140 * 2: segment wrap in DTA, no data read (not returned now)
1141 * 3: end of file, partial record read
1144 * Reads a record with the size FCB->logical_record_size from the FCB
1145 * to the disk transfer area. The position of the record is specified
1146 * with FCB->current_block_number and FCB->record_within_current_block.
1147 * Then FCB->current_block_number and FCB->record_within_current_block
1148 * are updated to point to the next record. If a partial record is
1149 * read, it is filled with zeros up to the FCB->logical_record_size.
1151 static void INT21_SequentialReadFromFCB( CONTEXT
*context
)
1156 DWORD record_number
;
1158 BYTE
*disk_transfer_area
;
1162 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1163 if (fcb
->drive_number
== 0xff) {
1164 xfcb
= (struct XFCB
*) fcb
;
1165 fcb
= (struct FCB
*) xfcb
->fcb
;
1168 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1169 if (handle
== INVALID_HANDLE_VALUE
) {
1170 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1172 AL_result
= 0x01; /* end of file, no data read */
1174 record_number
= 128 * fcb
->current_block_number
+ fcb
->record_within_current_block
;
1175 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1176 if (position
!= record_number
* fcb
->logical_record_size
) {
1177 TRACE("seek(%d, %d, 0) failed with %u\n",
1178 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1179 AL_result
= 0x01; /* end of file, no data read */
1181 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1182 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, fcb
->logical_record_size
);
1183 if (bytes_read
!= fcb
->logical_record_size
) {
1184 TRACE("_lread(%d, %p, %d) failed with %d\n",
1185 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_read
);
1186 if (bytes_read
== 0) {
1187 AL_result
= 0x01; /* end of file, no data read */
1189 memset(&disk_transfer_area
[bytes_read
], 0, fcb
->logical_record_size
- bytes_read
);
1190 AL_result
= 0x03; /* end of file, partial record read */
1193 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1194 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1195 AL_result
= 0x00; /* successful */
1199 if (AL_result
== 0x00 || AL_result
== 0x03) {
1200 if (fcb
->record_within_current_block
== 127) {
1201 fcb
->record_within_current_block
= 0;
1202 fcb
->current_block_number
++;
1204 fcb
->record_within_current_block
++;
1207 SET_AL(context
, AL_result
);
1211 /***********************************************************************
1212 * INT21_SequentialWriteToFCB
1214 * Handler for function 0x15.
1217 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1222 * 2: segment wrap in DTA (not returned now)
1225 * Writes a record with the size FCB->logical_record_size from the disk
1226 * transfer area to the FCB. The position of the record is specified
1227 * with FCB->current_block_number and FCB->record_within_current_block.
1228 * Then FCB->current_block_number and FCB->record_within_current_block
1229 * are updated to point to the next record.
1231 static void INT21_SequentialWriteToFCB( CONTEXT
*context
)
1236 DWORD record_number
;
1238 BYTE
*disk_transfer_area
;
1242 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1243 if (fcb
->drive_number
== 0xff) {
1244 xfcb
= (struct XFCB
*) fcb
;
1245 fcb
= (struct FCB
*) xfcb
->fcb
;
1248 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1249 if (handle
== INVALID_HANDLE_VALUE
) {
1250 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1252 AL_result
= 0x01; /* disk full */
1254 record_number
= 128 * fcb
->current_block_number
+ fcb
->record_within_current_block
;
1255 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1256 if (position
!= record_number
* fcb
->logical_record_size
) {
1257 TRACE("seek(%d, %d, 0) failed with %u\n",
1258 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1259 AL_result
= 0x01; /* disk full */
1261 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1262 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, fcb
->logical_record_size
);
1263 if (bytes_written
!= fcb
->logical_record_size
) {
1264 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1265 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_written
);
1266 AL_result
= 0x01; /* disk full */
1268 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1269 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1270 AL_result
= 0x00; /* successful */
1274 if (AL_result
== 0x00) {
1275 if (fcb
->record_within_current_block
== 127) {
1276 fcb
->record_within_current_block
= 0;
1277 fcb
->current_block_number
++;
1279 fcb
->record_within_current_block
++;
1282 SET_AL(context
, AL_result
);
1286 /***********************************************************************
1287 * INT21_ReadRandomRecordFromFCB
1289 * Handler for function 0x21.
1292 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1296 * 1: end of file, no data read
1297 * 2: segment wrap in DTA, no data read (not returned now)
1298 * 3: end of file, partial record read
1301 * Reads a record with the size FCB->logical_record_size from
1302 * the FCB to the disk transfer area. The position of the record
1303 * is specified with FCB->random_access_record_number. The
1304 * FCB->random_access_record_number is not updated. If a partial record
1305 * is read, it is filled with zeros up to the FCB->logical_record_size.
1307 static void INT21_ReadRandomRecordFromFCB( CONTEXT
*context
)
1312 DWORD record_number
;
1314 BYTE
*disk_transfer_area
;
1318 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1319 if (fcb
->drive_number
== 0xff) {
1320 xfcb
= (struct XFCB
*) fcb
;
1321 fcb
= (struct FCB
*) xfcb
->fcb
;
1324 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1325 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1326 if (handle
== INVALID_HANDLE_VALUE
) {
1327 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1329 AL_result
= 0x01; /* end of file, no data read */
1331 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1332 if (position
!= record_number
* fcb
->logical_record_size
) {
1333 TRACE("seek(%d, %d, 0) failed with %u\n",
1334 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1335 AL_result
= 0x01; /* end of file, no data read */
1337 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1338 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, fcb
->logical_record_size
);
1339 if (bytes_read
!= fcb
->logical_record_size
) {
1340 TRACE("_lread(%d, %p, %d) failed with %d\n",
1341 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_read
);
1342 if (bytes_read
== 0) {
1343 AL_result
= 0x01; /* end of file, no data read */
1345 memset(&disk_transfer_area
[bytes_read
], 0, fcb
->logical_record_size
- bytes_read
);
1346 AL_result
= 0x03; /* end of file, partial record read */
1349 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1350 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1351 AL_result
= 0x00; /* successful */
1355 fcb
->current_block_number
= record_number
/ 128;
1356 fcb
->record_within_current_block
= record_number
% 128;
1357 SET_AL(context
, AL_result
);
1361 /***********************************************************************
1362 * INT21_WriteRandomRecordToFCB
1364 * Handler for function 0x22.
1367 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1372 * 2: segment wrap in DTA (not returned now)
1375 * Writes a record with the size FCB->logical_record_size from
1376 * the disk transfer area to the FCB. The position of the record
1377 * is specified with FCB->random_access_record_number. The
1378 * FCB->random_access_record_number is not updated.
1380 static void INT21_WriteRandomRecordToFCB( CONTEXT
*context
)
1385 DWORD record_number
;
1387 BYTE
*disk_transfer_area
;
1391 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1392 if (fcb
->drive_number
== 0xff) {
1393 xfcb
= (struct XFCB
*) fcb
;
1394 fcb
= (struct FCB
*) xfcb
->fcb
;
1397 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1398 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1399 if (handle
== INVALID_HANDLE_VALUE
) {
1400 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1402 AL_result
= 0x01; /* disk full */
1404 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1405 if (position
!= record_number
* fcb
->logical_record_size
) {
1406 TRACE("seek(%d, %d, 0) failed with %u\n",
1407 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1408 AL_result
= 0x01; /* disk full */
1410 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1411 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, fcb
->logical_record_size
);
1412 if (bytes_written
!= fcb
->logical_record_size
) {
1413 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1414 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_written
);
1415 AL_result
= 0x01; /* disk full */
1417 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1418 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1419 AL_result
= 0x00; /* successful */
1423 fcb
->current_block_number
= record_number
/ 128;
1424 fcb
->record_within_current_block
= record_number
% 128;
1425 SET_AL(context
, AL_result
);
1429 /***********************************************************************
1430 * INT21_RandomBlockReadFromFCB
1432 * Handler for function 0x27.
1435 * CX [I/O] Number of records to read
1436 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1440 * 1: end of file, no data read
1441 * 2: segment wrap in DTA, no data read (not returned now)
1442 * 3: end of file, partial record read
1445 * Reads several records with the size FCB->logical_record_size from
1446 * the FCB to the disk transfer area. The number of records to be
1447 * read is specified in the CX register. The position of the first
1448 * record is specified with FCB->random_access_record_number. The
1449 * FCB->random_access_record_number, the FCB->current_block_number
1450 * and FCB->record_within_current_block are updated to point to the
1451 * next record after the records read. If a partial record is read,
1452 * it is filled with zeros up to the FCB->logical_record_size. The
1453 * CX register is set to the number of successfully read records.
1455 static void INT21_RandomBlockReadFromFCB( CONTEXT
*context
)
1460 DWORD record_number
;
1462 BYTE
*disk_transfer_area
;
1463 UINT records_requested
;
1464 UINT bytes_requested
;
1469 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1470 if (fcb
->drive_number
== 0xff) {
1471 xfcb
= (struct XFCB
*) fcb
;
1472 fcb
= (struct FCB
*) xfcb
->fcb
;
1475 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1476 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1477 if (handle
== INVALID_HANDLE_VALUE
) {
1478 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1481 AL_result
= 0x01; /* end of file, no data read */
1483 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1484 if (position
!= record_number
* fcb
->logical_record_size
) {
1485 TRACE("seek(%d, %d, 0) failed with %u\n",
1486 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1488 AL_result
= 0x01; /* end of file, no data read */
1490 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1491 records_requested
= CX_reg(context
);
1492 bytes_requested
= records_requested
* fcb
->logical_record_size
;
1493 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, bytes_requested
);
1494 if (bytes_read
!= bytes_requested
) {
1495 TRACE("_lread(%d, %p, %d) failed with %d\n",
1496 fcb
->file_number
, disk_transfer_area
, bytes_requested
, bytes_read
);
1497 records_read
= bytes_read
/ fcb
->logical_record_size
;
1498 if (bytes_read
% fcb
->logical_record_size
== 0) {
1499 AL_result
= 0x01; /* end of file, no data read */
1502 memset(&disk_transfer_area
[bytes_read
], 0, records_read
* fcb
->logical_record_size
- bytes_read
);
1503 AL_result
= 0x03; /* end of file, partial record read */
1506 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1507 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1508 records_read
= records_requested
;
1509 AL_result
= 0x00; /* successful */
1513 record_number
+= records_read
;
1514 memcpy(fcb
->random_access_record_number
, &record_number
, 4);
1515 fcb
->current_block_number
= record_number
/ 128;
1516 fcb
->record_within_current_block
= record_number
% 128;
1517 SET_CX(context
, records_read
);
1518 SET_AL(context
, AL_result
);
1522 /***********************************************************************
1523 * INT21_RandomBlockWriteToFCB
1525 * Handler for function 0x28.
1528 * CX [I/O] Number of records to write
1529 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1534 * 2: segment wrap in DTA (not returned now)
1537 * Writes several records with the size FCB->logical_record_size from
1538 * the disk transfer area to the FCB. The number of records to be
1539 * written is specified in the CX register. The position of the first
1540 * record is specified with FCB->random_access_record_number. The
1541 * FCB->random_access_record_number, the FCB->current_block_number
1542 * and FCB->record_within_current_block are updated to point to the
1543 * next record after the records written. The CX register is set to
1544 * the number of successfully written records.
1546 static void INT21_RandomBlockWriteToFCB( CONTEXT
*context
)
1551 DWORD record_number
;
1553 BYTE
*disk_transfer_area
;
1554 UINT records_requested
;
1555 UINT bytes_requested
;
1557 UINT records_written
;
1560 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1561 if (fcb
->drive_number
== 0xff) {
1562 xfcb
= (struct XFCB
*) fcb
;
1563 fcb
= (struct FCB
*) xfcb
->fcb
;
1566 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1567 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1568 if (handle
== INVALID_HANDLE_VALUE
) {
1569 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1571 records_written
= 0;
1572 AL_result
= 0x01; /* disk full */
1574 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1575 if (position
!= record_number
* fcb
->logical_record_size
) {
1576 TRACE("seek(%d, %d, 0) failed with %u\n",
1577 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1578 records_written
= 0;
1579 AL_result
= 0x01; /* disk full */
1581 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1582 records_requested
= CX_reg(context
);
1583 bytes_requested
= records_requested
* fcb
->logical_record_size
;
1584 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, bytes_requested
);
1585 if (bytes_written
!= bytes_requested
) {
1586 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1587 fcb
->file_number
, disk_transfer_area
, bytes_requested
, bytes_written
);
1588 records_written
= bytes_written
/ fcb
->logical_record_size
;
1589 AL_result
= 0x01; /* disk full */
1591 TRACE("successful write %d bytes from record %d (position %u) of file %d (handle %p)\n",
1592 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1593 records_written
= records_requested
;
1594 AL_result
= 0x00; /* successful */
1598 record_number
+= records_written
;
1599 memcpy(fcb
->random_access_record_number
, &record_number
, 4);
1600 fcb
->current_block_number
= record_number
/ 128;
1601 fcb
->record_within_current_block
= record_number
% 128;
1602 SET_CX(context
, records_written
);
1603 SET_AL(context
, AL_result
);
1607 /***********************************************************************
1608 * INT21_CreateDirectory
1612 * - subfunction 0x39 of function 0x71
1613 * - subfunction 0xff of function 0x43 (CL == 0x39)
1615 static BOOL
INT21_CreateDirectory( CONTEXT
*context
)
1617 WCHAR dirW
[MAX_PATH
];
1618 char *dirA
= CTX_SEG_OFF_TO_LIN(context
,
1622 TRACE( "CREATE DIRECTORY %s\n", dirA
);
1624 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
1626 if (CreateDirectoryW(dirW
, NULL
))
1630 * FIXME: CreateDirectory's LastErrors will clash with the ones
1631 * used by DOS. AH=39 only returns 3 (path not found) and
1632 * 5 (access denied), while CreateDirectory return several
1633 * ones. Remap some of them. -Marcus
1635 switch (GetLastError())
1637 case ERROR_ALREADY_EXISTS
:
1638 case ERROR_FILENAME_EXCED_RANGE
:
1639 case ERROR_DISK_FULL
:
1640 SetLastError(ERROR_ACCESS_DENIED
);
1650 /***********************************************************************
1651 * INT21_ExtendedCountryInformation
1653 * Handler for function 0x65.
1655 static void INT21_ExtendedCountryInformation( CONTEXT
*context
)
1657 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN( context
, context
->SegEs
, context
->Edi
);
1658 BYTE buffsize
= CX_reg (context
);
1660 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1664 * Check subfunctions that are passed country and code page.
1666 if (AL_reg(context
) >= 0x01 && AL_reg(context
) <= 0x07)
1668 WORD country
= DX_reg(context
);
1669 WORD codepage
= BX_reg(context
);
1671 if (country
!= 0xffff && country
!= INT21_GetSystemCountryCode())
1672 FIXME( "Requested info on non-default country %04x\n", country
);
1674 if (codepage
!= 0xffff && codepage
!= GetOEMCP())
1675 FIXME( "Requested info on non-default code page %04x\n", codepage
);
1678 switch (AL_reg(context
)) {
1679 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1680 INT_BARF( context
, 0x21 );
1681 SET_CFLAG( context
);
1684 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1685 TRACE( "Get general internationalization info\n" );
1686 dataptr
[0] = 0x01; /* Info ID */
1687 *(WORD
*)(dataptr
+1) = 38; /* Size of the following info */
1688 *(WORD
*)(dataptr
+3) = INT21_GetSystemCountryCode(); /* Country ID */
1689 *(WORD
*)(dataptr
+5) = GetOEMCP(); /* Code page */
1690 /* FIXME: fill buffer partially up to buffsize bytes*/
1691 if (buffsize
>= 0x29){
1692 INT21_FillCountryInformation( dataptr
+ 7 );
1693 SET_CX( context
, 0x29 ); /* Size of returned info */
1695 SET_CX( context
, 0x07 ); /* Size of returned info */
1699 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1700 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1701 TRACE( "Get pointer to uppercase table\n" );
1702 dataptr
[0] = AL_reg(context
); /* Info ID */
1703 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1704 offsetof(INT21_HEAP
, uppercase_size
) );
1705 SET_CX( context
, 5 ); /* Size of returned info */
1708 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1709 TRACE( "Get pointer to lowercase table\n" );
1710 dataptr
[0] = 0x03; /* Info ID */
1711 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1712 offsetof(INT21_HEAP
, lowercase_size
) );
1713 SET_CX( context
, 5 ); /* Size of returned info */
1716 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1717 TRACE("Get pointer to filename terminator table\n");
1718 dataptr
[0] = 0x05; /* Info ID */
1719 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1720 offsetof(INT21_HEAP
, filename_size
) );
1721 SET_CX( context
, 5 ); /* Size of returned info */
1724 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1725 TRACE("Get pointer to collating sequence table\n");
1726 dataptr
[0] = 0x06; /* Info ID */
1727 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1728 offsetof(INT21_HEAP
, collating_size
) );
1729 SET_CX( context
, 5 ); /* Size of returned info */
1732 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1733 TRACE("Get pointer to DBCS lead byte table\n");
1734 dataptr
[0] = 0x07; /* Info ID */
1735 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1736 offsetof(INT21_HEAP
, dbcs_size
) );
1737 SET_CX( context
, 5 ); /* Size of returned info */
1740 case 0x20: /* CAPITALIZE CHARACTER */
1741 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1742 TRACE("Convert char to uppercase\n");
1743 SET_DL( context
, toupper(DL_reg(context
)) );
1746 case 0x21: /* CAPITALIZE STRING */
1747 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1748 TRACE("Convert string to uppercase with length\n");
1750 char *ptr
= CTX_SEG_OFF_TO_LIN( context
, context
->SegDs
,
1752 WORD len
= CX_reg(context
);
1753 while (len
--) { *ptr
= toupper(*ptr
); ptr
++; }
1757 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1758 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1759 TRACE("Convert ASCIIZ string to uppercase\n");
1761 char *p
= CTX_SEG_OFF_TO_LIN( context
, context
->SegDs
, context
->Edx
);
1762 for ( ; *p
; p
++) *p
= toupper(*p
);
1766 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1767 INT_BARF( context
, 0x21 );
1768 SET_CFLAG( context
);
1772 INT_BARF( context
, 0x21 );
1779 /***********************************************************************
1780 * INT21_FileAttributes
1784 * - subfunction 0x43 of function 0x71
1786 static BOOL
INT21_FileAttributes( CONTEXT
*context
,
1790 WCHAR fileW
[MAX_PATH
];
1791 char *fileA
= CTX_SEG_OFF_TO_LIN(context
,
1801 switch (subfunction
)
1803 case 0x00: /* GET FILE ATTRIBUTES */
1804 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA
);
1805 len
= MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1807 /* Winbench 96 Disk Test fails if we don't complain
1808 * about a filename that ends in \
1810 if (!len
|| (fileW
[len
-1] == '/') || (fileW
[len
-1] == '\\'))
1813 result
= GetFileAttributesW( fileW
);
1814 if (result
== INVALID_FILE_ATTRIBUTES
)
1818 SET_CX( context
, (WORD
)result
);
1820 SET_AX( context
, (WORD
)result
); /* DR DOS */
1824 case 0x01: /* SET FILE ATTRIBUTES */
1825 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
1826 CX_reg(context
), fileA
);
1827 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1829 if (!SetFileAttributesW( fileW
, CX_reg(context
) ))
1833 case 0x02: /* GET COMPRESSED FILE SIZE */
1834 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA
);
1835 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1837 result
= GetCompressedFileSizeW( fileW
, NULL
);
1838 if (result
== INVALID_FILE_SIZE
)
1842 SET_AX( context
, LOWORD(result
) );
1843 SET_DX( context
, HIWORD(result
) );
1847 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
1849 INT_BARF( context
, 0x21 );
1852 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA
);
1853 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1855 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
1856 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1857 NULL
, OPEN_EXISTING
, 0, 0 );
1858 if (handle
== INVALID_HANDLE_VALUE
)
1861 DosDateTimeToFileTime( DI_reg(context
),
1864 status
= SetFileTime( handle
, NULL
, NULL
, &filetime
);
1866 CloseHandle( handle
);
1871 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
1873 INT_BARF( context
, 0x21 );
1876 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA
);
1877 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1879 handle
= CreateFileW( fileW
, GENERIC_READ
,
1880 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1881 NULL
, OPEN_EXISTING
, 0, 0 );
1882 if (handle
== INVALID_HANDLE_VALUE
)
1885 status
= GetFileTime( handle
, NULL
, NULL
, &filetime
);
1888 FileTimeToDosDateTime( &filetime
, &date
, &time
);
1889 SET_DI( context
, date
);
1890 SET_CX( context
, time
);
1893 CloseHandle( handle
);
1898 case 0x05: /* SET FILE LAST ACCESS DATE */
1900 INT_BARF( context
, 0x21 );
1903 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA
);
1904 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1906 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
1907 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1908 NULL
, OPEN_EXISTING
, 0, 0 );
1909 if (handle
== INVALID_HANDLE_VALUE
)
1912 DosDateTimeToFileTime( DI_reg(context
),
1915 status
= SetFileTime( handle
, NULL
, &filetime
, NULL
);
1917 CloseHandle( handle
);
1922 case 0x06: /* GET FILE LAST ACCESS DATE */
1924 INT_BARF( context
, 0x21 );
1927 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA
);
1928 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1930 handle
= CreateFileW( fileW
, GENERIC_READ
,
1931 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1932 NULL
, OPEN_EXISTING
, 0, 0 );
1933 if (handle
== INVALID_HANDLE_VALUE
)
1936 status
= GetFileTime( handle
, NULL
, &filetime
, NULL
);
1939 FileTimeToDosDateTime( &filetime
, &date
, NULL
);
1940 SET_DI( context
, date
);
1943 CloseHandle( handle
);
1948 case 0x07: /* SET FILE CREATION DATE AND TIME */
1950 INT_BARF( context
, 0x21 );
1953 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA
);
1954 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1956 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
1957 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1958 NULL
, OPEN_EXISTING
, 0, 0 );
1959 if (handle
== INVALID_HANDLE_VALUE
)
1963 * FIXME: SI has number of 10-millisecond units past time in CX.
1965 DosDateTimeToFileTime( DI_reg(context
),
1968 status
= SetFileTime( handle
, &filetime
, NULL
, NULL
);
1970 CloseHandle( handle
);
1975 case 0x08: /* GET FILE CREATION DATE AND TIME */
1977 INT_BARF( context
, 0x21 );
1980 TRACE( "GET FILE CREATION DATE AND TIME, file %s\n", fileA
);
1981 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1983 handle
= CreateFileW( fileW
, GENERIC_READ
,
1984 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1985 NULL
, OPEN_EXISTING
, 0, 0 );
1986 if (handle
== INVALID_HANDLE_VALUE
)
1989 status
= GetFileTime( handle
, &filetime
, NULL
, NULL
);
1992 FileTimeToDosDateTime( &filetime
, &date
, &time
);
1993 SET_DI( context
, date
);
1994 SET_CX( context
, time
);
1996 * FIXME: SI has number of 10-millisecond units past
1999 SET_SI( context
, 0 );
2002 CloseHandle(handle
);
2007 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2008 if (islong
|| context
->Ebp
!= 0x5053)
2009 INT_BARF( context
, 0x21 );
2012 switch(CL_reg(context
))
2015 if (!INT21_CreateDirectory( context
))
2020 if (!INT21_RenameFile( context
))
2025 INT_BARF( context
, 0x21 );
2031 INT_BARF( context
, 0x21 );
2038 /***********************************************************************
2039 * INT21_FileDateTime
2041 * Handler for function 0x57.
2043 static BOOL
INT21_FileDateTime( CONTEXT
*context
)
2045 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2049 switch (AL_reg(context
)) {
2050 case 0x00: /* Get last-written stamp */
2051 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2054 if (!GetFileTime( handle
, NULL
, NULL
, &filetime
))
2056 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2057 SET_DX( context
, date
);
2058 SET_CX( context
, time
);
2062 case 0x01: /* Set last-written stamp */
2063 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2066 DosDateTimeToFileTime( DX_reg(context
),
2069 if (!SetFileTime( handle
, NULL
, NULL
, &filetime
))
2074 case 0x04: /* Get last access stamp, DOS 7 */
2075 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2078 if (!GetFileTime( handle
, NULL
, &filetime
, NULL
))
2080 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2081 SET_DX( context
, date
);
2082 SET_CX( context
, time
);
2086 case 0x05: /* Set last access stamp, DOS 7 */
2087 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2090 DosDateTimeToFileTime( DX_reg(context
),
2093 if (!SetFileTime( handle
, NULL
, &filetime
, NULL
))
2098 case 0x06: /* Get creation stamp, DOS 7 */
2099 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2102 if (!GetFileTime( handle
, &filetime
, NULL
, NULL
))
2104 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2105 SET_DX( context
, date
);
2106 SET_CX( context
, time
);
2108 * FIXME: SI has number of 10-millisecond units past time in CX.
2110 SET_SI( context
, 0 );
2114 case 0x07: /* Set creation stamp, DOS 7 */
2115 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2119 * FIXME: SI has number of 10-millisecond units past time in CX.
2121 DosDateTimeToFileTime( DX_reg(context
),
2124 if (!SetFileTime( handle
, &filetime
, NULL
, NULL
))
2130 INT_BARF( context
, 0x21 );
2138 /***********************************************************************
2141 * Handler for functions 0x51 and 0x62.
2143 static void INT21_GetPSP( CONTEXT
*context
)
2145 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context
) );
2147 SET_BX( context
, LOWORD(GetCurrentPDB16()) );
2150 static inline void setword( BYTE
*ptr
, WORD w
)
2153 ptr
[1] = (BYTE
)(w
>> 8);
2156 static void CreateBPB(int drive
, BYTE
*data
, BOOL16 limited
)
2157 /* limited == TRUE is used with INT 0x21/0x440d */
2159 /* FIXME: we're forcing some values without checking that those are valid */
2164 setword(&data
[3], 0);
2166 setword(&data
[6], 240);
2167 setword(&data
[8], 64000);
2169 setword(&data
[0x0b], 40);
2170 setword(&data
[0x0d], 56);
2171 setword(&data
[0x0f], 2);
2172 setword(&data
[0x11], 0);
2175 setword(&data
[0x1f], 800);
2177 setword(&data
[0x22], 1);
2184 setword(&data
[3], 0);
2186 setword(&data
[6], 240);
2187 setword(&data
[8], 2880);
2189 setword(&data
[0x0b], 6);
2190 setword(&data
[0x0d], 18);
2191 setword(&data
[0x0f], 2);
2192 setword(&data
[0x11], 0);
2195 setword(&data
[0x1f], 80);
2197 setword(&data
[0x22], 2);
2202 static inline DWORD
INT21_Ioctl_CylHeadSect2Lin(DWORD cyl
, WORD head
, WORD sec
, WORD cyl_cnt
,
2203 WORD head_cnt
, WORD sec_cnt
)
2205 DWORD res
= (cyl
* head_cnt
*sec_cnt
+ head
* sec_cnt
+ sec
);
2209 /***********************************************************************
2212 * Handler for block device IOCTLs.
2214 static void INT21_Ioctl_Block( CONTEXT
*context
)
2217 BYTE drive
= INT21_MapDrive( BL_reg(context
) );
2218 WCHAR drivespec
[] = {'A', ':', '\\', 0};
2221 drivespec
[0] += drive
;
2222 drivetype
= GetDriveTypeW( drivespec
);
2224 RESET_CFLAG(context
);
2225 if (drivetype
== DRIVE_UNKNOWN
|| drivetype
== DRIVE_NO_ROOT_DIR
)
2227 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2228 AL_reg(context
), 'A' + drive
);
2229 SetLastError( ERROR_INVALID_DRIVE
);
2230 SET_AX( context
, ERROR_INVALID_DRIVE
);
2231 SET_CFLAG( context
);
2235 switch (AL_reg(context
))
2237 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2238 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2239 INT_BARF( context
, 0x21 );
2242 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2243 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2246 if (drivetype
== DRIVE_REMOVABLE
)
2247 SET_AX( context
, 0 ); /* removable */
2249 SET_AX( context
, 1 ); /* not removable */
2252 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2253 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2256 if (drivetype
== DRIVE_REMOTE
)
2257 SET_DX( context
, (1<<9) | (1<<12) ); /* remote + no direct IO */
2258 else if (drivetype
== DRIVE_CDROM
)
2259 /* CDROM should be set to remote. If it set the app will
2260 * call int2f to check if it cdrom or remote drive. */
2261 SET_DX( context
, (1<<12) );
2262 else if (drivetype
== DRIVE_FIXED
)
2263 /* This should define if drive support 0x0d, 0x0f and 0x08
2264 * requests. The local fixed drive should do. */
2265 SET_DX( context
, (1<<11) );
2267 SET_DX( context
, 0 ); /* FIXME: use driver attr here */
2270 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2271 /* Get pointer to IOCTL parameter block */
2272 dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2274 switch (CX_reg(context
))
2276 case 0x0841: /* write logical device track */
2277 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2280 WORD head
= *(WORD
*)(dataptr
+1);
2281 WORD cyl
= *(WORD
*)(dataptr
+3);
2282 WORD sect
= *(WORD
*)(dataptr
+5);
2283 WORD nrsect
= *(WORD
*)(dataptr
+7);
2284 BYTE
*data
= CTX_SEG_OFF_TO_LIN(context
, *(WORD
*)(dataptr
+11), *(WORD
*)(dataptr
+9));
2285 WORD cyl_cnt
, head_cnt
, sec_cnt
;
2287 /* FIXME: we're faking some values here */
2293 SET_AX( context
, ERROR_WRITE_FAULT
);
2304 if (!DOSVM_RawWrite(drive
, INT21_Ioctl_CylHeadSect2Lin(cyl
, head
, sect
, cyl_cnt
, head_cnt
, sec_cnt
), nrsect
, data
, FALSE
))
2306 SET_AX( context
, ERROR_WRITE_FAULT
);
2312 case 0x084a: /* lock logical volume */
2313 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2314 BH_reg(context
), DX_reg(context
), 'A' + drive
);
2317 case 0x0860: /* get device parameters */
2318 /* FIXME: we're faking some values here */
2319 /* used by w4wgrp's winfile */
2320 memset(dataptr
, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2322 dataptr
[6] = 0; /* media type */
2325 dataptr
[1] = 0x05; /* fixed disk */
2326 setword(&dataptr
[2], 0x01); /* non removable */
2327 setword(&dataptr
[4], 0x300); /* # of cylinders */
2331 dataptr
[1] = 0x07; /* block dev, floppy */
2332 setword(&dataptr
[2], 0x02); /* removable */
2333 setword(&dataptr
[4], 80); /* # of cylinders */
2335 CreateBPB(drive
, &dataptr
[7], TRUE
);
2336 RESET_CFLAG(context
);
2339 case 0x0861: /* read logical device track */
2340 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2343 WORD head
= *(WORD
*)(dataptr
+1);
2344 WORD cyl
= *(WORD
*)(dataptr
+3);
2345 WORD sect
= *(WORD
*)(dataptr
+5);
2346 WORD nrsect
= *(WORD
*)(dataptr
+7);
2347 BYTE
*data
= CTX_SEG_OFF_TO_LIN(context
, *(WORD
*)(dataptr
+11), *(WORD
*)(dataptr
+9));
2348 WORD cyl_cnt
, head_cnt
, sec_cnt
;
2350 /* FIXME: we're faking some values here */
2364 if (!DOSVM_RawRead(drive
, INT21_Ioctl_CylHeadSect2Lin(cyl
, head
, sect
, cyl_cnt
, head_cnt
, sec_cnt
), nrsect
, data
, FALSE
))
2366 SET_AX( context
, ERROR_READ_FAULT
);
2372 case 0x0866: /* get volume serial number */
2374 WCHAR label
[12],fsname
[9];
2376 TRACE( "GENERIC IOCTL - Get media id - %c:\n",
2379 GetVolumeInformationW(drivespec
, label
, 12, &serial
, NULL
, NULL
, fsname
, 9);
2380 *(WORD
*)dataptr
= 0;
2381 memcpy(dataptr
+2,&serial
,4);
2382 WideCharToMultiByte(CP_OEMCP
, 0, label
, 11, (LPSTR
)dataptr
+ 6, 11, NULL
, NULL
);
2383 WideCharToMultiByte(CP_OEMCP
, 0, fsname
, 8, (LPSTR
)dataptr
+ 17, 8, NULL
, NULL
);
2387 case 0x086a: /* unlock logical volume */
2388 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2392 case 0x086f: /* get drive map information */
2393 memset(dataptr
+1, '\0', dataptr
[0]-1);
2394 dataptr
[1] = dataptr
[0];
2395 dataptr
[2] = 0x07; /* protected mode driver; no eject; no notification */
2396 dataptr
[3] = 0xFF; /* no physical drive */
2400 /* Trial and error implementation */
2401 SET_AX( context
, drivetype
== DRIVE_UNKNOWN
? 0x0f : 0x01 );
2402 SET_CFLAG(context
); /* Seems to be set all the time */
2406 INT_BARF( context
, 0x21 );
2410 case 0x0e: /* GET LOGICAL DRIVE MAP */
2411 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2413 /* FIXME: this is not correct if drive has mappings */
2414 SET_AL( context
, 0 ); /* drive has no mapping */
2417 case 0x0f: /* SET LOGICAL DRIVE MAP */
2418 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2419 INT21_DriveName( BL_reg(context
)));
2420 /* FIXME: as of today, we don't support logical drive mapping... */
2421 SET_AL( context
, 0 );
2424 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2426 INT_BARF( context
, 0x21 );
2431 /***********************************************************************
2434 * Handler for character device IOCTLs.
2436 static void INT21_Ioctl_Char( CONTEXT
*context
)
2438 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2439 BOOL IsConsoleIOHandle
= VerifyConsoleIoHandle( handle
);
2441 switch (AL_reg(context
))
2443 case 0x00: /* GET DEVICE INFORMATION */
2444 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context
) );
2445 if (IsConsoleIOHandle
|| GetFileType(handle
) == FILE_TYPE_CHAR
)
2448 * Returns attribute word in DX:
2449 * Bit 14 - Device driver can process IOCTL requests.
2450 * Bit 13 - Output until busy supported.
2451 * Bit 11 - Driver supports OPEN/CLOSE calls.
2453 * Bit 7 - Set (indicates device).
2454 * Bit 6 - EOF on input.
2455 * Bit 5 - Raw (binary) mode.
2456 * Bit 4 - Device is special (uses int29).
2457 * Bit 3 - Clock device.
2458 * Bit 2 - NUL device.
2459 * Bit 1 - Console output device.
2460 * Bit 0 - Console input device.
2462 SET_DX( context
, IsConsoleIOHandle
? 0x80c3 : 0x80c0 /* FIXME */ );
2467 * Returns attribute word in DX:
2468 * Bit 15 - File is remote.
2469 * Bit 14 - Don't set file date/time on closing.
2470 * Bit 11 - Media not removable.
2471 * Bit 8 - Generate int24 if no disk space on write
2472 * or read past end of file
2473 * Bit 7 - Clear (indicates file).
2474 * Bit 6 - File has not been written.
2475 * Bit 5..0 - Drive number (0=A:,...)
2477 * FIXME: Should check if file is on remote or removable drive.
2478 * FIXME: Should use drive file is located on (and not current).
2480 SET_DX( context
, 0x0140 + INT21_GetCurrentDrive() );
2484 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2485 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context
) );
2487 * Returns attribute word in DX:
2488 * Bit 15 - Set if remote.
2489 * Bit 14 - Set if date/time not set on close.
2491 * FIXME: Should check if file is on remote drive.
2493 SET_DX( context
, 0 );
2496 case 0x01: /* SET DEVICE INFORMATION */
2497 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2498 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2499 case 0x06: /* GET INPUT STATUS */
2500 case 0x07: /* GET OUTPUT STATUS */
2501 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2502 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2504 INT_BARF( context
, 0x21 );
2510 /***********************************************************************
2513 * Handler for function 0x44.
2515 static void INT21_Ioctl( CONTEXT
*context
)
2517 switch (AL_reg(context
))
2523 INT21_Ioctl_Char( context
);
2528 INT21_Ioctl_Block( context
);
2533 INT21_Ioctl_Char( context
);
2538 INT21_Ioctl_Block( context
);
2542 INT21_Ioctl_Char( context
);
2545 case 0x0b: /* SET SHARING RETRY COUNT */
2546 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2547 CX_reg(context
), DX_reg(context
) );
2548 if (!CX_reg(context
))
2550 SET_AX( context
, 1 );
2551 SET_CFLAG( context
);
2555 RESET_CFLAG( context
);
2560 INT21_Ioctl_Char( context
);
2566 INT21_Ioctl_Block( context
);
2570 INT21_Ioctl_Char( context
);
2574 INT21_Ioctl_Block( context
);
2577 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2578 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2579 SET_CFLAG(context
); /* Error / This is not DR DOS. */
2580 SET_AX( context
, 0x0001 ); /* Invalid function */
2583 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2584 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2585 SET_CFLAG(context
); /* Error / This is not DR DOS. */
2586 SET_AX( context
, 0x0001 ); /* Invalid function */
2589 case 0xe0: /* Sun PC-NFS API */
2590 TRACE( "Sun PC-NFS API\n" );
2595 INT_BARF( context
, 0x21 );
2600 /***********************************************************************
2603 * Handler for function 0x73.
2605 static BOOL
INT21_Fat32( CONTEXT
*context
)
2607 switch (AL_reg(context
))
2609 case 0x02: /* FAT32 - GET EXTENDED DPB */
2611 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
2612 WORD
*ptr
= CTX_SEG_OFF_TO_LIN(context
,
2613 context
->SegEs
, context
->Edi
);
2614 INT21_DPB
*target
= (INT21_DPB
*)(ptr
+ 1);
2617 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context
) );
2619 if ( CX_reg(context
) < sizeof(INT21_DPB
) + 2 || *ptr
< sizeof(INT21_DPB
) )
2621 SetLastError( ERROR_BAD_LENGTH
);
2625 if ( !INT21_FillDrivePB( drive
) )
2627 SetLastError( ERROR_INVALID_DRIVE
);
2631 source
= &INT21_GetHeapPointer()->misc_dpb_list
[drive
];
2633 *ptr
= sizeof(INT21_DPB
);
2636 if (LOWORD(context
->Esi
) != 0xF1A6)
2638 target
->driver_header
= 0;
2643 FIXME( "Caller requested driver and next DPB pointers!\n" );
2648 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2650 WCHAR dirW
[MAX_PATH
];
2651 char *dirA
= CTX_SEG_OFF_TO_LIN( context
,
2652 context
->SegDs
, context
->Edx
);
2653 BYTE
*data
= CTX_SEG_OFF_TO_LIN( context
,
2654 context
->SegEs
, context
->Edi
);
2655 DWORD cluster_sectors
;
2657 DWORD free_clusters
;
2658 DWORD total_clusters
;
2660 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA
);
2661 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
2663 if (CX_reg(context
) < 44)
2665 SetLastError( ERROR_BAD_LENGTH
);
2669 if (!GetDiskFreeSpaceW( dirW
, &cluster_sectors
, §or_bytes
,
2670 &free_clusters
, &total_clusters
))
2673 *(WORD
*) (data
+ 0) = 44; /* size of structure */
2674 *(WORD
*) (data
+ 2) = 0; /* version */
2675 *(DWORD
*)(data
+ 4) = cluster_sectors
;
2676 *(DWORD
*)(data
+ 8) = sector_bytes
;
2677 *(DWORD
*)(data
+ 12) = free_clusters
;
2678 *(DWORD
*)(data
+ 16) = total_clusters
;
2681 * Below we have free/total sectors and
2682 * free/total allocation units without adjustment
2683 * for compression. We fake both using cluster information.
2685 *(DWORD
*)(data
+ 20) = free_clusters
* cluster_sectors
;
2686 *(DWORD
*)(data
+ 24) = total_clusters
* cluster_sectors
;
2687 *(DWORD
*)(data
+ 28) = free_clusters
;
2688 *(DWORD
*)(data
+ 32) = total_clusters
;
2691 * Between (data + 36) and (data + 43) there
2692 * are eight reserved bytes.
2698 INT_BARF( context
, 0x21 );
2704 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA
*dataA
,
2705 const WIN32_FIND_DATAW
*dataW
)
2707 dataA
->dwFileAttributes
= dataW
->dwFileAttributes
;
2708 dataA
->ftCreationTime
= dataW
->ftCreationTime
;
2709 dataA
->ftLastAccessTime
= dataW
->ftLastAccessTime
;
2710 dataA
->ftLastWriteTime
= dataW
->ftLastWriteTime
;
2711 dataA
->nFileSizeHigh
= dataW
->nFileSizeHigh
;
2712 dataA
->nFileSizeLow
= dataW
->nFileSizeLow
;
2713 WideCharToMultiByte( CP_OEMCP
, 0, dataW
->cFileName
, -1,
2714 dataA
->cFileName
, sizeof(dataA
->cFileName
), NULL
, NULL
);
2715 WideCharToMultiByte( CP_OEMCP
, 0, dataW
->cAlternateFileName
, -1,
2716 dataA
->cAlternateFileName
, sizeof(dataA
->cAlternateFileName
), NULL
, NULL
);
2719 /***********************************************************************
2720 * INT21_LongFilename
2722 * Handler for function 0x71.
2724 static void INT21_LongFilename( CONTEXT
*context
)
2726 BOOL bSetDOSExtendedError
= FALSE
;
2727 WCHAR pathW
[MAX_PATH
];
2730 switch (AL_reg(context
))
2732 case 0x0d: /* RESET DRIVE */
2733 INT_BARF( context
, 0x21 );
2736 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
2737 if (!INT21_CreateDirectory( context
))
2738 bSetDOSExtendedError
= TRUE
;
2741 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
2742 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2744 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA
);
2745 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2746 if (!RemoveDirectoryW( pathW
)) bSetDOSExtendedError
= TRUE
;
2749 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
2750 if (!INT21_SetCurrentDirectory( context
))
2751 bSetDOSExtendedError
= TRUE
;
2754 case 0x41: /* LONG FILENAME - DELETE FILE */
2755 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2757 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA
);
2758 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2760 if (!DeleteFileW( pathW
)) bSetDOSExtendedError
= TRUE
;
2763 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
2764 if (!INT21_FileAttributes( context
, BL_reg(context
), TRUE
))
2765 bSetDOSExtendedError
= TRUE
;
2768 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
2769 if (!INT21_GetCurrentDirectory( context
, TRUE
))
2770 bSetDOSExtendedError
= TRUE
;
2773 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
2777 WIN32_FIND_DATAW dataW
;
2778 WIN32_FIND_DATAA
* dataA
;
2780 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Edx
);
2781 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA
);
2783 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2784 handle
= FindFirstFileW(pathW
, &dataW
);
2786 dataA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
2787 if (handle
!= INVALID_HANDLE_VALUE
&&
2788 (h16
= GlobalAlloc16(GMEM_MOVEABLE
, sizeof(handle
))))
2790 HANDLE
* ptr
= GlobalLock16( h16
);
2792 GlobalUnlock16( h16
);
2793 SET_AX( context
, h16
);
2794 INT21_ConvertFindDataWtoA(dataA
, &dataW
);
2798 if (handle
!= INVALID_HANDLE_VALUE
) FindClose(handle
);
2799 SET_AX( context
, INVALID_HANDLE_VALUE16
);
2800 bSetDOSExtendedError
= TRUE
;
2805 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
2807 HGLOBAL16 h16
= BX_reg(context
);
2809 WIN32_FIND_DATAW dataW
;
2810 WIN32_FIND_DATAA
* dataA
;
2812 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
2815 dataA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
2817 if (h16
!= INVALID_HANDLE_VALUE16
&& (ptr
= GlobalLock16( h16
)))
2819 if (!FindNextFileW(*ptr
, &dataW
)) bSetDOSExtendedError
= TRUE
;
2820 else INT21_ConvertFindDataWtoA(dataA
, &dataW
);
2821 GlobalUnlock16( h16
);
2825 SetLastError( ERROR_INVALID_HANDLE
);
2826 bSetDOSExtendedError
= TRUE
;
2831 case 0x56: /* LONG FILENAME - RENAME FILE */
2832 if (!INT21_RenameFile(context
))
2833 bSetDOSExtendedError
= TRUE
;
2836 case 0x60: /* LONG FILENAME - CONVERT PATH */
2838 WCHAR res
[MAX_PATH
];
2840 switch (CL_reg(context
))
2842 case 0x00: /* "truename" - Canonicalize path */
2844 * FIXME: This is not 100% equal to 0x01 case,
2845 * if you fix this, fix int21 subfunction 0x60, too.
2848 case 0x01: /* Get short filename or path */
2849 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
2850 if (!GetShortPathNameW(pathW
, res
, 67))
2851 bSetDOSExtendedError
= TRUE
;
2854 SET_AX( context
, 0 );
2855 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
2856 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
2861 case 0x02: /* Get canonical long filename or path */
2862 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
2863 if (!GetFullPathNameW(pathW
, 128, res
, NULL
))
2864 bSetDOSExtendedError
= TRUE
;
2867 SET_AX( context
, 0 );
2868 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
2869 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
2874 FIXME("Unimplemented long file name function:\n");
2875 INT_BARF( context
, 0x21 );
2877 SET_AL( context
, 0 );
2883 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
2884 if (!INT21_CreateFile( context
, context
->Esi
, TRUE
,
2885 BX_reg(context
), DL_reg(context
) ))
2886 bSetDOSExtendedError
= TRUE
;
2889 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
2891 DWORD filename_len
, flags
;
2894 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Edx
);
2896 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA
);
2897 SET_AX( context
, 0 );
2898 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2899 if (!GetVolumeInformationW( pathW
, NULL
, 0, NULL
, &filename_len
,
2902 INT_BARF( context
, 0x21 );
2906 SET_BX( context
, flags
| 0x4000 ); /* support for LFN functions */
2907 SET_CX( context
, filename_len
);
2908 SET_DX( context
, MAX_PATH
); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
2909 WideCharToMultiByte(CP_OEMCP
, 0, dstW
, -1,
2910 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
2915 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
2917 HGLOBAL16 h16
= BX_reg(context
);
2920 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
2922 if (h16
!= INVALID_HANDLE_VALUE16
&& (ptr
= GlobalLock16( h16
)))
2924 if (!FindClose( *ptr
)) bSetDOSExtendedError
= TRUE
;
2925 GlobalUnlock16( h16
);
2926 GlobalFree16( h16
);
2930 SetLastError( ERROR_INVALID_HANDLE
);
2931 bSetDOSExtendedError
= TRUE
;
2936 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
2938 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2939 BY_HANDLE_FILE_INFORMATION
*info
=
2940 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2942 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
2944 if (!GetFileInformationByHandle(handle
, info
))
2945 bSetDOSExtendedError
= TRUE
;
2949 case 0xa7: /* LONG FILENAME - CONVERT TIME */
2950 switch (BL_reg(context
))
2952 case 0x00: /* FILE TIME TO DOS TIME */
2955 FILETIME
*filetime
= CTX_SEG_OFF_TO_LIN(context
,
2959 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
2961 FileTimeToDosDateTime( filetime
, &date
, &time
);
2963 SET_DX( context
, date
);
2964 SET_CX( context
, time
);
2967 * FIXME: BH has number of 10-millisecond units
2970 SET_BH( context
, 0 );
2974 case 0x01: /* DOS TIME TO FILE TIME */
2976 FILETIME
*filetime
= CTX_SEG_OFF_TO_LIN(context
,
2980 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
2983 * FIXME: BH has number of 10-millisecond units
2986 DosDateTimeToFileTime( DX_reg(context
), CX_reg(context
),
2992 INT_BARF( context
, 0x21 );
2997 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
2998 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
2999 case 0xaa: /* LONG FILENAME - SUBST */
3001 FIXME("Unimplemented long file name function:\n");
3002 INT_BARF( context
, 0x21 );
3004 SET_AL( context
, 0 );
3008 if (bSetDOSExtendedError
)
3010 SET_AX( context
, GetLastError() );
3011 SET_CFLAG( context
);
3016 /***********************************************************************
3021 * - subfunction 0x56 of function 0x71
3022 * - subfunction 0xff of function 0x43 (CL == 0x56)
3024 static BOOL
INT21_RenameFile( CONTEXT
*context
)
3026 WCHAR fromW
[MAX_PATH
];
3027 WCHAR toW
[MAX_PATH
];
3028 char *fromA
= CTX_SEG_OFF_TO_LIN(context
,
3029 context
->SegDs
,context
->Edx
);
3030 char *toA
= CTX_SEG_OFF_TO_LIN(context
,
3031 context
->SegEs
,context
->Edi
);
3033 TRACE( "RENAME FILE %s to %s\n", fromA
, toA
);
3034 MultiByteToWideChar(CP_OEMCP
, 0, fromA
, -1, fromW
, MAX_PATH
);
3035 MultiByteToWideChar(CP_OEMCP
, 0, toA
, -1, toW
, MAX_PATH
);
3037 return MoveFileW( fromW
, toW
);
3041 /***********************************************************************
3047 static BOOL
INT21_NetworkFunc (CONTEXT
*context
)
3049 switch (AL_reg(context
))
3051 case 0x00: /* Get machine name. */
3053 WCHAR dstW
[MAX_COMPUTERNAME_LENGTH
+ 1];
3054 DWORD s
= ARRAY_SIZE(dstW
);
3057 char *dst
= CTX_SEG_OFF_TO_LIN (context
,context
->SegDs
,context
->Edx
);
3058 TRACE("getting machine name to %p\n", dst
);
3059 if (!GetComputerNameW(dstW
, &s
) ||
3060 !WideCharToMultiByte(CP_OEMCP
, 0, dstW
, -1, dst
, 16, NULL
, NULL
))
3063 SetLastError( ER_NoNetwork
);
3066 for (len
= strlen(dst
); len
< 15; len
++) dst
[len
] = ' ';
3068 SET_CH( context
, 1 ); /* Valid */
3069 SET_CL( context
, 1 ); /* NETbios number??? */
3070 TRACE("returning %s\n", debugstr_an(dst
, 16));
3075 SetLastError( ER_NoNetwork
);
3080 /******************************************************************
3081 * INT21_GetDiskSerialNumber
3084 static int INT21_GetDiskSerialNumber( CONTEXT
*context
)
3086 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3087 WCHAR path
[] = {'A',':','\\',0}, label
[11];
3090 path
[0] += INT21_MapDrive(BL_reg(context
));
3091 if (!GetVolumeInformationW( path
, label
, 11, &serial
, NULL
, NULL
, NULL
, 0))
3093 SetLastError( ERROR_INVALID_DRIVE
);
3097 *(WORD
*)dataptr
= 0;
3098 memcpy(dataptr
+ 2, &serial
, sizeof(DWORD
));
3099 WideCharToMultiByte(CP_OEMCP
, 0, label
, 11, (LPSTR
)dataptr
+ 6, 11, NULL
, NULL
);
3100 memcpy(dataptr
+ 17, "FAT16 ", 8);
3105 /******************************************************************
3106 * INT21_SetDiskSerialNumber
3109 static BOOL
INT21_SetDiskSerialNumber( CONTEXT
*context
)
3112 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3113 int drive
= INT21_MapDrive(BL_reg(context
));
3115 if (!is_valid_drive(drive
))
3117 SetLastError( ERROR_INVALID_DRIVE
);
3121 DRIVE_SetSerialNumber( drive
, *(DWORD
*)(dataptr
+ 2) );
3124 FIXME("Setting drive serial number is no longer supported\n");
3125 SetLastError( ERROR_NOT_SUPPORTED
);
3131 /******************************************************************
3132 * INT21_GetFreeDiskSpace
3135 static BOOL
INT21_GetFreeDiskSpace( CONTEXT
*context
)
3137 DWORD cluster_sectors
, sector_bytes
, free_clusters
, total_clusters
;
3138 WCHAR root
[] = {'A',':','\\',0};
3139 const DWORD max_clusters
= 0x3d83;
3140 const DWORD max_sectors_per_cluster
= 0x7f;
3141 const DWORD max_bytes_per_sector
= 0x200;
3143 root
[0] += INT21_MapDrive(DL_reg(context
));
3144 if (!GetDiskFreeSpaceW( root
, &cluster_sectors
, §or_bytes
,
3145 &free_clusters
, &total_clusters
))
3148 /* Some old win31 apps (Lotus SmartSuite 5.1) crap out if there's too
3149 * much disk space, so Windows XP seems to apply the following limits:
3150 * cluster_sectors <= 0x7f
3151 * sector size <= 0x200
3152 * clusters <= 0x3D83
3153 * This means total reported space is limited to about 1GB.
3156 /* Make sure bytes-per-sector is in [, max] */
3157 while (sector_bytes
> max_bytes_per_sector
) {
3159 free_clusters
<<= 1;
3160 total_clusters
<<= 1;
3162 /* Then make sure sectors-per-cluster is in [max/2, max]. */
3163 while (cluster_sectors
<= max_sectors_per_cluster
/2) {
3164 cluster_sectors
<<= 1;
3165 free_clusters
>>= 1;
3166 total_clusters
>>= 1;
3168 while (cluster_sectors
> max_sectors_per_cluster
) {
3169 cluster_sectors
>>= 1;
3170 free_clusters
<<= 1;
3171 total_clusters
<<= 1;
3174 /* scale up sectors_per_cluster to exactly max_sectors_per_cluster.
3175 * We could skip this, but that would impose an artificially low
3176 * limit on reported disk space.
3177 * To avoid overflow, first apply a preliminary cap on sector count;
3178 * this will not affect the correctness of the final result,
3179 * because if the preliminary cap hits, the final one will, too.
3181 if (total_clusters
> 4 * max_clusters
)
3182 total_clusters
= 4 * max_clusters
;
3183 if (free_clusters
> 4 * max_clusters
)
3184 free_clusters
= 4 * max_clusters
;
3185 if (cluster_sectors
< max_sectors_per_cluster
) {
3186 free_clusters
*= cluster_sectors
;
3187 free_clusters
/= max_sectors_per_cluster
;
3188 total_clusters
*= cluster_sectors
;
3189 total_clusters
/= max_sectors_per_cluster
;
3190 cluster_sectors
= max_sectors_per_cluster
;
3193 /* Finally, apply real cluster count cap. */
3194 if (total_clusters
> max_clusters
)
3195 total_clusters
= max_clusters
;
3196 if (free_clusters
> max_clusters
)
3197 free_clusters
= max_clusters
;
3199 SET_AX( context
, cluster_sectors
);
3200 SET_BX( context
, free_clusters
);
3201 SET_CX( context
, sector_bytes
);
3202 SET_DX( context
, total_clusters
);
3206 /******************************************************************
3207 * INT21_GetDriveAllocInfo
3210 static BOOL
INT21_GetDriveAllocInfo( CONTEXT
*context
, BYTE drive
)
3214 drive
= INT21_MapDrive( drive
);
3215 if (!INT21_FillDrivePB( drive
)) return FALSE
;
3216 dpb
= &(INT21_GetHeapPointer()->misc_dpb_list
[drive
]);
3217 SET_AL( context
, dpb
->cluster_sectors
+ 1 );
3218 SET_CX( context
, dpb
->sector_bytes
);
3219 SET_DX( context
, dpb
->num_clusters1
);
3221 context
->SegDs
= INT21_GetHeapSelector( context
);
3222 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
].media_ID
) );
3226 /***********************************************************************
3227 * INT21_GetExtendedError
3229 static void INT21_GetExtendedError( CONTEXT
*context
)
3231 BYTE
class, action
, locus
;
3232 WORD error
= GetLastError();
3237 class = action
= locus
= 0;
3239 case ERROR_DIR_NOT_EMPTY
:
3244 case ERROR_ACCESS_DENIED
:
3245 class = EC_AccessDenied
;
3249 case ERROR_CANNOT_MAKE
:
3250 class = EC_AccessDenied
;
3254 case ERROR_DISK_FULL
:
3255 case ERROR_HANDLE_DISK_FULL
:
3256 class = EC_MediaError
;
3260 case ERROR_FILE_EXISTS
:
3261 case ERROR_ALREADY_EXISTS
:
3266 case ERROR_FILE_NOT_FOUND
:
3267 case ERROR_PATH_NOT_FOUND
:
3268 case ERROR_INVALID_NAME
:
3269 class = EC_NotFound
;
3273 case ERROR_GEN_FAILURE
:
3274 class = EC_SystemFailure
;
3278 case ERROR_INVALID_DRIVE
:
3279 class = EC_MediaError
;
3283 case ERROR_INVALID_HANDLE
:
3284 class = EC_ProgramError
;
3288 case ERROR_LOCK_VIOLATION
:
3289 class = EC_AccessDenied
;
3293 case ERROR_NO_MORE_FILES
:
3294 class = EC_MediaError
;
3299 class = EC_NotFound
;
3303 case ERROR_NOT_ENOUGH_MEMORY
:
3304 class = EC_OutOfResource
;
3309 class = EC_NotFound
;
3313 case ERROR_SHARING_VIOLATION
:
3314 class = EC_Temporary
;
3318 case ERROR_TOO_MANY_OPEN_FILES
:
3319 class = EC_ProgramError
;
3324 FIXME("Unknown error %d\n", error
);
3325 class = EC_SystemFailure
;
3330 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3331 error
, class, action
, locus
);
3332 SET_AX( context
, error
);
3333 SET_BH( context
, class );
3334 SET_BL( context
, action
);
3335 SET_CH( context
, locus
);
3338 static BOOL
INT21_CreateTempFile( CONTEXT
*context
)
3340 static int counter
= 0;
3341 char *name
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3342 char *p
= name
+ strlen(name
);
3344 /* despite what Ralf Brown says, some programs seem to call without
3345 * ending backslash (DOS accepts that, so we accept it too) */
3346 if ((p
== name
) || (p
[-1] != '\\')) *p
++ = '\\';
3350 sprintf( p
, "wine%04x.%03d", GetCurrentThreadId(), counter
);
3351 counter
= (counter
+ 1) % 1000;
3354 Win32HandleToDosFileHandle(
3355 CreateFileA( name
, GENERIC_READ
| GENERIC_WRITE
,
3356 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
3357 CREATE_NEW
, 0, 0 ) ) );
3358 if (AX_reg(context
) != HFILE_ERROR16
)
3360 TRACE("created %s\n", name
);
3363 if (GetLastError() != ERROR_FILE_EXISTS
) return FALSE
;
3367 /***********************************************************************
3368 * DOSFS_ToDosFCBFormat
3370 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3371 * expanding wild cards and converting to upper-case in the process.
3372 * File name can be terminated by '\0', '\\' or '/'.
3373 * Return FALSE if the name is not a valid DOS name.
3374 * 'buffer' must be at least 12 characters long.
3376 /* Chars we don't want to see in DOS file names */
3377 static BOOL
INT21_ToDosFCBFormat( LPCWSTR name
, LPWSTR buffer
)
3379 static const WCHAR invalid_chars
[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3383 /* Check for "." and ".." */
3388 for(i
= 1; i
< 11; i
++) buffer
[i
] = ' ';
3395 return (!*p
|| (*p
== '/') || (*p
== '\\'));
3398 for (i
= 0; i
< 8; i
++)
3415 if (wcschr( invalid_chars
, *p
)) return FALSE
;
3423 /* Skip all chars after wildcard up to first dot */
3424 while (*p
&& (*p
!= '/') && (*p
!= '\\') && (*p
!= '.')) p
++;
3428 /* Check if name too long */
3429 if (*p
&& (*p
!= '/') && (*p
!= '\\') && (*p
!= '.')) return FALSE
;
3431 if (*p
== '.') p
++; /* Skip dot */
3433 for (i
= 8; i
< 11; i
++)
3443 return FALSE
; /* Second extension not allowed */
3451 if (wcschr( invalid_chars
, *p
)) return FALSE
;
3459 /* at most 3 character of the extension are processed
3460 * is something behind this ?
3462 while (*p
== '*' || *p
== ' ') p
++; /* skip wildcards and spaces */
3463 return (!*p
|| (*p
== '/') || (*p
== '\\'));
3466 static HANDLE INT21_FindHandle
;
3467 static const WCHAR
*INT21_FindPath
; /* will point to current dta->fullPath search */
3469 /******************************************************************
3472 static BOOL
INT21_FindFirst( CONTEXT
*context
)
3476 FINDFILE_DTA
*dta
= (FINDFILE_DTA
*)INT21_GetCurrentDTA(context
);
3477 WCHAR maskW
[12], pathW
[MAX_PATH
];
3478 static const WCHAR wildcardW
[] = {'*','.','*',0};
3480 path
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3481 MultiByteToWideChar(CP_OEMCP
, 0, path
, -1, pathW
, MAX_PATH
);
3483 p
= wcsrchr( pathW
, '\\');
3484 q
= wcsrchr( pathW
, '/');
3488 if (pathW
[0] && pathW
[1] == ':') p
= pathW
+ 2;
3493 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3494 * (doesn't matter as it is set below anyway)
3496 if (!INT21_ToDosFCBFormat( p
, maskW
))
3498 SetLastError( ERROR_FILE_NOT_FOUND
);
3499 SET_AX( context
, ERROR_FILE_NOT_FOUND
);
3503 WideCharToMultiByte(CP_OEMCP
, 0, maskW
, 12, dta
->mask
, sizeof(dta
->mask
), NULL
, NULL
);
3505 dta
->fullPath
= HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW
) + (p
- pathW
)*sizeof(WCHAR
) );
3506 memcpy( dta
->fullPath
, pathW
, (p
- pathW
) * sizeof(WCHAR
) );
3507 memcpy( dta
->fullPath
+ (p
- pathW
), wildcardW
, sizeof(wildcardW
) );
3508 /* we must have a fully qualified file name in dta->fullPath
3509 * (we could have a UNC path, but this would lead to some errors later on)
3511 dta
->drive
= drive_number( dta
->fullPath
[0] );
3513 dta
->search_attr
= CL_reg(context
);
3517 /******************************************************************
3520 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3522 static BOOL
match_short(LPCWSTR shortW
, LPCSTR maskA
)
3524 WCHAR mask
[11], file
[12];
3527 if (!INT21_ToDosFCBFormat( shortW
, file
)) return FALSE
;
3528 MultiByteToWideChar(CP_OEMCP
, 0, maskA
, 11, mask
, 11);
3529 for (i
= 0; i
< 11; i
++)
3530 if (mask
[i
] != '?' && mask
[i
] != file
[i
]) return FALSE
;
3534 static unsigned INT21_FindHelper(LPCWSTR fullPath
, unsigned drive
, unsigned count
,
3535 LPCSTR mask
, unsigned search_attr
,
3536 WIN32_FIND_DATAW
* entry
)
3540 if ((search_attr
& ~(FA_UNUSED
| FA_ARCHIVE
| FA_RDONLY
)) == FA_LABEL
)
3542 WCHAR path
[] = {' ',':','\\',0};
3544 if (count
) return 0;
3545 path
[0] = drive
+ 'A';
3546 if (!GetVolumeInformationW(path
, entry
->cAlternateFileName
, 13, NULL
, NULL
, NULL
, NULL
, 0)) return 0;
3547 if (!entry
->cAlternateFileName
[0]) return 0;
3548 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftCreationTime
);
3549 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftLastAccessTime
);
3550 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftLastWriteTime
);
3551 entry
->dwFileAttributes
= FA_LABEL
;
3552 entry
->nFileSizeHigh
= entry
->nFileSizeLow
= 0;
3553 TRACE("returning %s as label\n", debugstr_w(entry
->cAlternateFileName
));
3557 if (!INT21_FindHandle
|| INT21_FindPath
!= fullPath
|| count
== 0)
3559 if (INT21_FindHandle
) FindClose(INT21_FindHandle
);
3560 INT21_FindHandle
= FindFirstFileW(fullPath
, entry
);
3561 if (INT21_FindHandle
== INVALID_HANDLE_VALUE
)
3563 INT21_FindHandle
= 0;
3566 INT21_FindPath
= fullPath
;
3567 /* we need to resync search */
3572 while (ncalls
-- != 0)
3574 if (!FindNextFileW(INT21_FindHandle
, entry
))
3576 FindClose(INT21_FindHandle
); INT21_FindHandle
= 0;
3580 while (count
< 0xffff)
3583 /* Check the file attributes, and path */
3584 if (!(entry
->dwFileAttributes
& ~search_attr
) &&
3585 match_short(entry
->cAlternateFileName
[0] ? entry
->cAlternateFileName
: entry
->cFileName
,
3590 if (!FindNextFileW(INT21_FindHandle
, entry
))
3592 FindClose(INT21_FindHandle
); INT21_FindHandle
= 0;
3596 WARN("Too many directory entries in %s\n", debugstr_w(fullPath
) );
3600 /******************************************************************
3603 static BOOL
INT21_FindNext( CONTEXT
*context
)
3605 FINDFILE_DTA
*dta
= (FINDFILE_DTA
*)INT21_GetCurrentDTA(context
);
3606 DWORD attr
= dta
->search_attr
| FA_UNUSED
| FA_ARCHIVE
| FA_RDONLY
;
3607 WIN32_FIND_DATAW entry
;
3610 if (!dta
->fullPath
) return FALSE
;
3612 n
= INT21_FindHelper(dta
->fullPath
, dta
->drive
, dta
->count
,
3613 dta
->mask
, attr
, &entry
);
3616 dta
->fileattr
= entry
.dwFileAttributes
;
3617 dta
->filesize
= entry
.nFileSizeLow
;
3618 FileTimeToDosDateTime( &entry
.ftLastWriteTime
, &dta
->filedate
, &dta
->filetime
);
3619 if (entry
.cAlternateFileName
[0])
3620 WideCharToMultiByte(CP_OEMCP
, 0, entry
.cAlternateFileName
, -1,
3621 dta
->filename
, 13, NULL
, NULL
);
3623 WideCharToMultiByte(CP_OEMCP
, 0, entry
.cFileName
, -1, dta
->filename
, 13, NULL
, NULL
);
3625 if (!memchr(dta
->mask
,'?',11))
3627 /* wildcardless search, release resources in case no findnext will
3628 * be issued, and as a workaround in case file creation messes up
3629 * findnext, as sometimes happens with pkunzip
3631 HeapFree( GetProcessHeap(), 0, dta
->fullPath
);
3632 INT21_FindPath
= dta
->fullPath
= NULL
;
3637 HeapFree( GetProcessHeap(), 0, dta
->fullPath
);
3638 INT21_FindPath
= dta
->fullPath
= NULL
;
3642 /* microsoft's programmers should be shot for using CP/M style int21
3643 calls in Windows for Workgroup's winfile.exe */
3645 /******************************************************************
3646 * INT21_FindFirstFCB
3649 static BOOL
INT21_FindFirstFCB( CONTEXT
*context
)
3651 BYTE
*fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3654 WCHAR p
[] = {' ',':',};
3656 if (*fcb
== 0xff) pFCB
= (FINDFILE_FCB
*)(fcb
+ 7);
3657 else pFCB
= (FINDFILE_FCB
*)fcb
;
3658 drive
= INT21_MapDrive( pFCB
->drive
);
3659 if (drive
== MAX_DOS_DRIVES
) return FALSE
;
3662 pFCB
->fullPath
= HeapAlloc(GetProcessHeap(), 0, MAX_PATH
* sizeof(WCHAR
));
3663 if (!pFCB
->fullPath
) return FALSE
;
3664 GetLongPathNameW(p
, pFCB
->fullPath
, MAX_PATH
);
3669 /******************************************************************
3673 static BOOL
INT21_FindNextFCB( CONTEXT
*context
)
3675 BYTE
*fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3677 LPBYTE pResult
= INT21_GetCurrentDTA(context
);
3678 DOS_DIRENTRY_LAYOUT
*ddl
;
3679 WIN32_FIND_DATAW entry
;
3684 if (*fcb
== 0xff) /* extended FCB ? */
3687 pFCB
= (FINDFILE_FCB
*)(fcb
+ 7);
3692 pFCB
= (FINDFILE_FCB
*)fcb
;
3695 if (!pFCB
->fullPath
) return FALSE
;
3696 n
= INT21_FindHelper(pFCB
->fullPath
, INT21_MapDrive( pFCB
->drive
),
3697 pFCB
->count
, pFCB
->filename
, attr
, &entry
);
3700 HeapFree( GetProcessHeap(), 0, pFCB
->fullPath
);
3701 INT21_FindPath
= pFCB
->fullPath
= NULL
;
3708 /* place extended FCB header before pResult if called with extended FCB */
3710 pResult
+= 6; /* leave reserved field behind */
3711 *pResult
++ = entry
.dwFileAttributes
;
3713 *pResult
++ = INT21_MapDrive( pFCB
->drive
); /* DOS_DIRENTRY_LAYOUT after current drive number */
3714 ddl
= (DOS_DIRENTRY_LAYOUT
*)pResult
;
3715 ddl
->fileattr
= entry
.dwFileAttributes
;
3716 ddl
->cluster
= 0; /* what else? */
3717 ddl
->filesize
= entry
.nFileSizeLow
;
3718 memset( ddl
->reserved
, 0, sizeof(ddl
->reserved
) );
3719 FileTimeToDosDateTime( &entry
.ftLastWriteTime
,
3720 &ddl
->filedate
, &ddl
->filetime
);
3722 /* Convert file name to FCB format */
3723 if (entry
.cAlternateFileName
[0])
3724 INT21_ToDosFCBFormat( entry
.cAlternateFileName
, nameW
);
3726 INT21_ToDosFCBFormat( entry
.cFileName
, nameW
);
3727 WideCharToMultiByte(CP_OEMCP
, 0, nameW
, 11, ddl
->filename
, 11, NULL
, NULL
);
3732 /******************************************************************
3733 * INT21_ParseFileNameIntoFCB
3736 static void INT21_ParseFileNameIntoFCB( CONTEXT
*context
)
3739 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
);
3741 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
3745 INT buffer_len
, len
;
3747 SET_AL( context
, 0xff ); /* failed */
3749 TRACE("filename: '%s'\n", filename
);
3752 while (*s
&& (*s
!= ' ') && (*s
!= '\r') && (*s
!= '\n'))
3756 buffer_len
= MultiByteToWideChar(CP_OEMCP
, 0, filename
, len
, NULL
, 0);
3757 buffer
= HeapAlloc( GetProcessHeap(), 0, (buffer_len
+ 1) * sizeof(WCHAR
));
3758 len
= MultiByteToWideChar(CP_OEMCP
, 0, filename
, len
, buffer
, buffer_len
);
3760 INT21_ToDosFCBFormat(buffer
, fcbW
);
3761 HeapFree(GetProcessHeap(), 0, buffer
);
3762 WideCharToMultiByte(CP_OEMCP
, 0, fcbW
, 12, fcb
+ 1, 12, NULL
, NULL
);
3764 TRACE("FCB: '%s'\n", fcb
+ 1);
3766 SET_AL( context
, ((strchr(filename
, '*')) || (strchr(filename
, '$'))) != 0 );
3768 /* point DS:SI to first unparsed character */
3769 SET_SI( context
, context
->Esi
+ (int)s
- (int)filename
);
3772 static BOOL
INT21_Dup2(HFILE16 hFile1
, HFILE16 hFile2
)
3774 HFILE16 res
= HFILE_ERROR16
;
3775 HANDLE handle
, new_handle
;
3776 #define DOS_TABLE_SIZE 256
3777 DWORD map
[DOS_TABLE_SIZE
/ 32];
3780 handle
= DosFileHandleToWin32Handle(hFile1
);
3781 if (handle
== INVALID_HANDLE_VALUE
)
3785 /* now loop to allocate the same one... */
3786 memset(map
, 0, sizeof(map
));
3787 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
3789 if (!DuplicateHandle(GetCurrentProcess(), handle
,
3790 GetCurrentProcess(), &new_handle
,
3791 0, FALSE
, DUPLICATE_SAME_ACCESS
))
3793 res
= HFILE_ERROR16
;
3796 res
= Win32HandleToDosFileHandle(new_handle
);
3797 if (res
== HFILE_ERROR16
|| res
== hFile2
) break;
3798 map
[res
/ 32] |= 1 << (res
% 32);
3800 /* clean up the allocated slots */
3801 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
3803 if (map
[i
/ 32] & (1 << (i
% 32)))
3804 _lclose16((HFILE16
)i
);
3806 return res
== hFile2
;
3810 /***********************************************************************
3811 * DOSVM_Int21Handler
3813 * Interrupt 0x21 handler.
3815 void WINAPI
DOSVM_Int21Handler( CONTEXT
*context
)
3817 BOOL bSetDOSExtendedError
= FALSE
;
3819 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
3820 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
3821 AX_reg(context
), BX_reg(context
),
3822 CX_reg(context
), DX_reg(context
),
3823 SI_reg(context
), DI_reg(context
),
3824 (WORD
)context
->SegDs
, (WORD
)context
->SegEs
,
3828 * Extended error is used by (at least) functions 0x2f to 0x62.
3829 * Function 0x59 returns extended error and error should not
3830 * be cleared before handling the function.
3832 if (AH_reg(context
) >= 0x2f && AH_reg(context
) != 0x59)
3835 RESET_CFLAG(context
); /* Not sure if this is a good idea. */
3837 switch(AH_reg(context
))
3839 case 0x00: /* TERMINATE PROGRAM */
3840 TRACE("TERMINATE PROGRAM\n");
3844 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
3845 TRACE("DIRECT CHARACTER INPUT WITH ECHO - not supported\n");
3848 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
3849 TRACE("Write Character to Standard Output\n");
3852 case 0x03: /* READ CHARACTER FROM STDAUX */
3853 case 0x04: /* WRITE CHARACTER TO STDAUX */
3854 case 0x05: /* WRITE CHARACTER TO PRINTER */
3855 INT_BARF( context
, 0x21 );
3858 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
3859 if (DL_reg(context
) == 0xff)
3861 TRACE("Direct Console Input\n");
3863 /* no character available */
3864 SET_AL( context
, 0 );
3865 SET_ZFLAG( context
);
3869 TRACE("Direct Console Output\n");
3871 * At least DOS versions 2.1-7.0 return character
3872 * that was written in AL register.
3874 SET_AL( context
, DL_reg(context
) );
3878 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
3879 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO - not supported\n");
3880 SET_AL( context
, 0 );
3883 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
3884 TRACE("CHARACTER INPUT WITHOUT ECHO - not supported\n");
3885 SET_AL( context
, 0 );
3888 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
3889 TRACE("WRITE '$'-terminated string from %04X:%04X to stdout\n",
3890 context
->SegDs
, DX_reg(context
) );
3892 LPSTR data
= CTX_SEG_OFF_TO_LIN( context
,
3893 context
->SegDs
, context
->Edx
);
3897 * Do NOT use strchr() to calculate the string length,
3898 * as '\0' is valid string content, too!
3899 * Maybe we should check for non-'$' strings, but DOS doesn't.
3901 while (*p
!= '$') p
++;
3903 WriteFile( DosFileHandleToWin32Handle(1), data
, p
- data
, &w
, NULL
);
3904 SET_AL( context
, '$' ); /* yes, '$' (0x24) gets returned in AL */
3908 case 0x0a: /* BUFFERED INPUT */
3909 TRACE( "BUFFERED INPUT - not supported\n" );
3912 case 0x0b: /* GET STDIN STATUS */
3913 TRACE( "GET STDIN STATUS - not supported\n" );
3914 SET_AL( context
, 0 ); /* no character available */
3917 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
3919 BYTE al
= AL_reg(context
); /* Input function to execute after flush. */
3921 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al
);
3923 /* FIXME: buffers are not flushed */
3926 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
3927 * int21 function identified by AL will be called.
3929 if(al
== 0x01 || al
== 0x06 || al
== 0x07 || al
== 0x08 || al
== 0x0a)
3931 SET_AH( context
, al
);
3932 DOSVM_Int21Handler( context
);
3937 case 0x0d: /* DISK BUFFER FLUSH */
3938 TRACE("DISK BUFFER FLUSH ignored\n");
3941 case 0x0e: /* SELECT DEFAULT DRIVE */
3942 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context
) );
3943 INT21_SetCurrentDrive( DL_reg(context
) );
3944 SET_AL( context
, MAX_DOS_DRIVES
);
3947 case 0x0f: /* OPEN FILE USING FCB */
3948 INT21_OpenFileUsingFCB( context
);
3951 case 0x10: /* CLOSE FILE USING FCB */
3952 INT21_CloseFileUsingFCB( context
);
3955 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
3956 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
3957 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
));
3958 if (!INT21_FindFirstFCB(context
))
3960 SET_AL( context
, 0xff );
3963 /* else fall through */
3965 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
3966 SET_AL( context
, INT21_FindNextFCB(context
) ? 0x00 : 0xff );
3969 case 0x13: /* DELETE FILE USING FCB */
3970 INT_BARF( context
, 0x21 );
3973 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
3974 INT21_SequentialReadFromFCB( context
);
3977 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
3978 INT21_SequentialWriteToFCB( context
);
3981 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
3982 case 0x17: /* RENAME FILE USING FCB */
3983 INT_BARF( context
, 0x21 );
3986 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
3987 SET_AL( context
, 0 );
3990 case 0x19: /* GET CURRENT DEFAULT DRIVE */
3991 SET_AL( context
, INT21_GetCurrentDrive() );
3992 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context
) );
3995 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
3996 TRACE( "SET DISK TRANSFER AREA ADDRESS %04X:%04X\n",
3997 context
->SegDs
, DX_reg(context
) );
3999 TDB
*task
= GlobalLock16( GetCurrentTask() );
4000 task
->dta
= MAKESEGPTR( context
->SegDs
, DX_reg(context
) );
4004 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4005 if (!INT21_GetDriveAllocInfo(context
, 0))
4006 SET_AX( context
, 0xffff );
4009 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4010 if (!INT21_GetDriveAllocInfo(context
, DL_reg(context
)))
4011 SET_AX( context
, 0xffff );
4014 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4015 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4016 SET_AL( context
, 0 );
4019 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4021 BYTE drive
= INT21_MapDrive( 0 );
4022 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4024 if (INT21_FillDrivePB( drive
))
4026 SET_AL( context
, 0x00 ); /* success */
4027 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
] ) );
4028 context
->SegDs
= INT21_GetHeapSelector( context
);
4032 SET_AL( context
, 0xff ); /* invalid or network drive */
4037 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4038 SET_AL( context
, 0 );
4041 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4042 INT21_ReadRandomRecordFromFCB( context
);
4045 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4046 INT21_WriteRandomRecordToFCB( context
);
4049 case 0x23: /* GET FILE SIZE FOR FCB */
4050 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4051 INT_BARF( context
, 0x21 );
4054 case 0x25: /* SET INTERRUPT VECTOR */
4055 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context
));
4057 FARPROC16 ptr
= (FARPROC16
)MAKESEGPTR( context
->SegDs
, DX_reg(context
) );
4058 DOSVM_SetPMHandler16( AL_reg(context
), ptr
);
4062 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4063 INT_BARF( context
, 0x21 );
4066 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4067 INT21_RandomBlockReadFromFCB( context
);
4070 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4071 INT21_RandomBlockWriteToFCB( context
);
4074 case 0x29: /* PARSE FILENAME INTO FCB */
4075 INT21_ParseFileNameIntoFCB(context
);
4078 case 0x2a: /* GET SYSTEM DATE */
4079 TRACE( "GET SYSTEM DATE\n" );
4082 GetLocalTime( &systime
);
4083 SET_CX( context
, systime
.wYear
);
4084 SET_DH( context
, systime
.wMonth
);
4085 SET_DL( context
, systime
.wDay
);
4086 SET_AL( context
, systime
.wDayOfWeek
);
4090 case 0x2b: /* SET SYSTEM DATE */
4091 TRACE( "SET SYSTEM DATE\n" );
4093 WORD year
= CX_reg(context
);
4094 BYTE month
= DH_reg(context
);
4095 BYTE day
= DL_reg(context
);
4097 if (year
>= 1980 && year
<= 2099 &&
4098 month
>= 1 && month
<= 12 &&
4099 day
>= 1 && day
<= 31)
4101 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4103 SET_AL( context
, 0 ); /* Let's pretend we succeeded */
4107 SET_AL( context
, 0xff ); /* invalid date */
4108 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4114 case 0x2c: /* GET SYSTEM TIME */
4115 TRACE( "GET SYSTEM TIME\n" );
4118 GetLocalTime( &systime
);
4119 SET_CH( context
, systime
.wHour
);
4120 SET_CL( context
, systime
.wMinute
);
4121 SET_DH( context
, systime
.wSecond
);
4122 SET_DL( context
, systime
.wMilliseconds
/ 10 );
4126 case 0x2d: /* SET SYSTEM TIME */
4127 if( CH_reg(context
) >= 24 || CL_reg(context
) >= 60 || DH_reg(context
) >= 60 || DL_reg(context
) >= 100 ) {
4128 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4129 CH_reg(context
), CL_reg(context
),
4130 DH_reg(context
), DL_reg(context
) );
4131 SET_AL( context
, 0xFF );
4135 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4136 CH_reg(context
), CL_reg(context
),
4137 DH_reg(context
), DL_reg(context
) );
4138 SET_AL( context
, 0 ); /* Let's pretend we succeeded */
4142 case 0x2e: /* SET VERIFY FLAG */
4143 TRACE("SET VERIFY FLAG ignored\n");
4144 /* we cannot change the behaviour anyway, so just ignore it */
4147 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4148 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4150 TDB
*task
= GlobalLock16( GetCurrentTask() );
4151 context
->SegEs
= SELECTOROF( task
->dta
);
4152 SET_BX( context
, OFFSETOF( task
->dta
) );
4156 case 0x30: /* GET DOS VERSION */
4157 TRACE( "GET DOS VERSION - %s requested\n",
4158 (AL_reg(context
) == 0x00) ? "OEM number" : "version flag" );
4160 if (AL_reg(context
) == 0x00)
4161 SET_BH( context
, 0xff ); /* OEM number => undefined */
4163 SET_BH( context
, 0x08 ); /* version flag => DOS is in ROM */
4165 SET_AL( context
, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4166 SET_AH( context
, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4168 SET_BL( context
, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4169 SET_CX( context
, 0x3456 );
4172 case 0x31: /* TERMINATE AND STAY RESIDENT */
4173 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4176 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4178 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
4179 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context
) );
4181 if (INT21_FillDrivePB( drive
))
4183 SET_AL( context
, 0x00 ); /* success */
4184 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
] ) );
4185 context
->SegDs
= INT21_GetHeapSelector( context
);
4189 SET_AL( context
, 0xff ); /* invalid or network drive */
4194 case 0x33: /* MULTIPLEXED */
4195 switch (AL_reg(context
))
4197 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4198 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4199 SET_DL( context
, brk_flag
);
4202 case 0x01: /* SET EXTENDED BREAK STATE */
4203 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4204 brk_flag
= (DL_reg(context
) > 0) ? 1 : 0;
4207 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4208 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4209 /* ugly coding in order to stay reentrant */
4210 if (DL_reg(context
))
4212 SET_DL( context
, brk_flag
);
4217 SET_DL( context
, brk_flag
);
4222 case 0x05: /* GET BOOT DRIVE */
4223 TRACE("GET BOOT DRIVE\n");
4224 SET_DL( context
, 3 );
4225 /* c: is Wine's bootdrive (a: is 1)*/
4228 case 0x06: /* GET TRUE VERSION NUMBER */
4229 TRACE("GET TRUE VERSION NUMBER\n");
4230 SET_BL( context
, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4231 SET_BH( context
, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4232 SET_DL( context
, 0x00 ); /* revision */
4233 SET_DH( context
, 0x08 ); /* DOS is in ROM */
4237 INT_BARF( context
, 0x21 );
4242 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4243 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4244 context
->SegEs
= INT21_GetHeapSelector( context
);
4245 SET_BX( context
, offsetof(INT21_HEAP
, misc_indos
) );
4248 case 0x35: /* GET INTERRUPT VECTOR */
4249 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context
));
4251 FARPROC16 addr
= DOSVM_GetPMHandler16( AL_reg(context
) );
4252 context
->SegEs
= SELECTOROF(addr
);
4253 SET_BX( context
, OFFSETOF(addr
) );
4257 case 0x36: /* GET FREE DISK SPACE */
4258 TRACE("GET FREE DISK SPACE FOR DRIVE %s (limited to about 1GB)\n",
4259 INT21_DriveName( DL_reg(context
) ));
4260 if (!INT21_GetFreeDiskSpace(context
)) SET_AX( context
, 0xffff );
4263 case 0x37: /* SWITCHAR */
4265 switch (AL_reg(context
))
4267 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4268 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4269 SET_AL( context
, 0x00 ); /* success*/
4270 SET_DL( context
, '/' );
4272 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4273 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4275 SET_AL( context
, 0x00 ); /* success*/
4278 INT_BARF( context
, 0x21 );
4284 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4285 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4286 if (AL_reg(context
))
4288 WORD country
= AL_reg(context
);
4289 if (country
== 0xff)
4290 country
= BX_reg(context
);
4291 if (country
!= INT21_GetSystemCountryCode()) {
4292 FIXME( "Requested info on non-default country %04x\n", country
);
4297 if(AX_reg(context
) != 2 )
4299 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context
,
4302 SET_AX( context
, INT21_GetSystemCountryCode() );
4303 SET_BX( context
, INT21_GetSystemCountryCode() );
4304 RESET_CFLAG(context
);
4308 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4309 if (!INT21_CreateDirectory( context
))
4310 bSetDOSExtendedError
= TRUE
;
4312 RESET_CFLAG(context
);
4315 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4317 WCHAR dirW
[MAX_PATH
];
4318 char *dirA
= CTX_SEG_OFF_TO_LIN(context
,
4319 context
->SegDs
, context
->Edx
);
4321 TRACE( "REMOVE DIRECTORY %s\n", dirA
);
4323 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
4325 if (!RemoveDirectoryW( dirW
))
4326 bSetDOSExtendedError
= TRUE
;
4328 RESET_CFLAG(context
);
4332 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4333 if (!INT21_SetCurrentDirectory( context
))
4334 bSetDOSExtendedError
= TRUE
;
4336 RESET_CFLAG(context
);
4339 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4340 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4341 OF_READWRITE
| OF_SHARE_COMPAT
, 0x12 ))
4342 bSetDOSExtendedError
= TRUE
;
4344 RESET_CFLAG(context
);
4347 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4348 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4349 AL_reg(context
), 0x01 ))
4350 bSetDOSExtendedError
= TRUE
;
4352 RESET_CFLAG(context
);
4355 case 0x3e: /* "CLOSE" - CLOSE FILE */
4356 TRACE( "CLOSE handle %d\n", BX_reg(context
) );
4357 if (_lclose16( BX_reg(context
) ) == HFILE_ERROR16
)
4358 bSetDOSExtendedError
= TRUE
;
4360 RESET_CFLAG(context
);
4363 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4364 TRACE( "READ from %d to %04X:%04X for %d bytes\n",
4371 WORD count
= CX_reg(context
);
4372 BYTE
*buffer
= CTX_SEG_OFF_TO_LIN( context
,
4376 /* Some programs pass a count larger than the allocated buffer */
4377 DWORD maxcount
= GetSelectorLimit16( context
->SegDs
) - DX_reg(context
) + 1;
4378 if (count
> maxcount
) count
= maxcount
;
4381 * FIXME: Reading from console (BX=1) in DOS mode
4382 * does not work as it is supposed to work.
4385 RESET_CFLAG(context
); /* set if error */
4386 if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context
)), buffer
, count
, &result
, NULL
))
4387 SET_AX( context
, (WORD
)result
);
4389 bSetDOSExtendedError
= TRUE
;
4393 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4394 TRACE( "WRITE from %04X:%04X to handle %d for %d byte\n",
4395 context
->SegDs
, DX_reg(context
),
4396 BX_reg(context
), CX_reg(context
) );
4398 char *ptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
4399 HFILE handle
= (HFILE
)DosFileHandleToWin32Handle(BX_reg(context
));
4400 LONG result
= _hwrite( handle
, ptr
, CX_reg(context
) );
4401 if (result
== HFILE_ERROR
)
4402 bSetDOSExtendedError
= TRUE
;
4405 SET_AX( context
, (WORD
)result
);
4406 RESET_CFLAG(context
);
4411 case 0x41: /* "UNLINK" - DELETE FILE */
4413 WCHAR fileW
[MAX_PATH
];
4414 char *fileA
= CTX_SEG_OFF_TO_LIN(context
,
4418 TRACE( "UNLINK %s\n", fileA
);
4419 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
4421 if (!DeleteFileW( fileW
))
4422 bSetDOSExtendedError
= TRUE
;
4424 RESET_CFLAG(context
);
4428 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4429 TRACE( "LSEEK handle %d offset %d from %s\n",
4431 MAKELONG( DX_reg(context
), CX_reg(context
) ),
4432 (AL_reg(context
) == 0) ?
4433 "start of file" : ((AL_reg(context
) == 1) ?
4434 "current file position" : "end of file") );
4436 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
4437 LONG offset
= MAKELONG( DX_reg(context
), CX_reg(context
) );
4438 DWORD status
= SetFilePointer( handle
, offset
,
4439 NULL
, AL_reg(context
) );
4440 if (status
== INVALID_SET_FILE_POINTER
)
4441 bSetDOSExtendedError
= TRUE
;
4444 SET_AX( context
, LOWORD(status
) );
4445 SET_DX( context
, HIWORD(status
) );
4446 RESET_CFLAG(context
);
4451 case 0x43: /* FILE ATTRIBUTES */
4452 if (!INT21_FileAttributes( context
, AL_reg(context
), FALSE
))
4453 bSetDOSExtendedError
= TRUE
;
4455 RESET_CFLAG(context
);
4458 case 0x44: /* IOCTL */
4459 INT21_Ioctl( context
);
4462 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4463 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context
) );
4466 HFILE handle16
= HFILE_ERROR
;
4468 if (DuplicateHandle( GetCurrentProcess(),
4469 DosFileHandleToWin32Handle(BX_reg(context
)),
4470 GetCurrentProcess(),
4472 0, TRUE
, DUPLICATE_SAME_ACCESS
))
4473 handle16
= Win32HandleToDosFileHandle(handle32
);
4475 if (handle16
== HFILE_ERROR
)
4476 bSetDOSExtendedError
= TRUE
;
4479 SET_AX( context
, handle16
);
4480 RESET_CFLAG(context
);
4485 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4486 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4487 BX_reg(context
), CX_reg(context
) );
4488 if (!INT21_Dup2(BX_reg(context
), CX_reg(context
)))
4489 bSetDOSExtendedError
= TRUE
;
4491 RESET_CFLAG(context
);
4494 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4495 if (!INT21_GetCurrentDirectory( context
, FALSE
))
4496 bSetDOSExtendedError
= TRUE
;
4498 RESET_CFLAG(context
);
4501 case 0x48: /* ALLOCATE MEMORY */
4502 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context
) );
4504 DWORD bytes
= (DWORD
)BX_reg(context
) << 4;
4505 DWORD rv
= GlobalDOSAlloc16( bytes
);
4506 WORD selector
= LOWORD( rv
);
4510 SET_AX( context
, selector
);
4511 RESET_CFLAG(context
);
4516 SET_AX( context
, 0x0008 ); /* insufficient memory */
4517 SET_BX( context
, DOSMEM_Available() >> 4 );
4522 case 0x49: /* FREE MEMORY */
4523 TRACE( "FREE MEMORY segment %04X\n", context
->SegEs
);
4525 BOOL ok
= !GlobalDOSFree16( context
->SegEs
);
4527 /* If we don't reset ES_reg, we will fail in the relay code */
4528 if (ok
) context
->SegEs
= 0;
4531 TRACE("FREE MEMORY failed\n");
4533 SET_AX( context
, 0x0009 ); /* memory block address invalid */
4538 case 0x4a: /* RESIZE MEMORY BLOCK */
4539 TRACE( "RESIZE MEMORY segment %04X to %d paragraphs\n",
4540 context
->SegEs
, BX_reg(context
) );
4542 FIXME( "Resize memory block - unsupported under Win16\n" );
4547 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4549 char *program
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
4550 HINSTANCE16 instance
;
4552 TRACE( "EXEC %s\n", program
);
4554 RESET_CFLAG(context
);
4555 instance
= WinExec16( program
, SW_NORMAL
);
4558 SET_CFLAG( context
);
4559 SET_AX( context
, instance
);
4564 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
4565 TRACE( "EXIT with return code %d\n", AL_reg(context
) );
4566 DOSVM_Exit( AL_reg(context
) );
4569 case 0x4d: /* GET RETURN CODE */
4570 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
4571 SET_AX( context
, 0 );
4574 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
4575 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context
),
4576 (LPCSTR
)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
));
4577 if (!INT21_FindFirst(context
)) break;
4580 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
4581 TRACE("FINDNEXT\n");
4582 if (!INT21_FindNext(context
))
4584 SetLastError( ERROR_NO_MORE_FILES
);
4585 SET_AX( context
, ERROR_NO_MORE_FILES
);
4588 else SET_AX( context
, 0 ); /* OK */
4591 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
4592 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
4593 DOSVM_psp
= BX_reg(context
);
4596 case 0x51: /* GET PSP ADDRESS */
4597 INT21_GetPSP( context
);
4600 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
4601 TRACE("Get List of Lists - not supported\n");
4603 SET_BX( context
, 0 );
4606 case 0x54: /* Get Verify Flag */
4607 TRACE("Get Verify Flag - Not Supported\n");
4608 SET_AL( context
, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
4611 case 0x56: /* "RENAME" - RENAME FILE */
4612 if (!INT21_RenameFile( context
))
4613 bSetDOSExtendedError
= TRUE
;
4615 RESET_CFLAG(context
);
4618 case 0x57: /* FILE DATE AND TIME */
4619 if (!INT21_FileDateTime( context
))
4620 bSetDOSExtendedError
= TRUE
;
4622 RESET_CFLAG(context
);
4625 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
4626 switch (AL_reg(context
))
4628 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
4629 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
4630 SET_AX( context
, 0 ); /* low memory first fit */
4633 case 0x01: /* SET ALLOCATION STRATEGY */
4634 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
4638 case 0x02: /* GET UMB LINK STATE */
4639 TRACE( "GET UMB LINK STATE\n" );
4640 SET_AL( context
, 0 ); /* UMBs not part of DOS memory chain */
4643 case 0x03: /* SET UMB LINK STATE */
4644 TRACE( "SET UMB LINK STATE to %d - ignored\n",
4649 INT_BARF( context
, 0x21 );
4653 case 0x59: /* GET EXTENDED ERROR INFO */
4654 INT21_GetExtendedError( context
);
4657 case 0x5a: /* CREATE TEMPORARY FILE */
4658 TRACE("CREATE TEMPORARY FILE\n");
4659 bSetDOSExtendedError
= !INT21_CreateTempFile(context
);
4662 case 0x5b: /* CREATE NEW FILE */
4663 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4664 OF_READWRITE
| OF_SHARE_COMPAT
, 0x10 ))
4665 bSetDOSExtendedError
= TRUE
;
4667 RESET_CFLAG(context
);
4670 case 0x5c: /* "FLOCK" - RECORD LOCKING */
4672 DWORD offset
= MAKELONG(DX_reg(context
), CX_reg(context
));
4673 DWORD length
= MAKELONG(DI_reg(context
), SI_reg(context
));
4674 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
4676 RESET_CFLAG(context
);
4677 switch (AL_reg(context
))
4679 case 0x00: /* LOCK */
4680 TRACE( "lock handle %d offset %d length %d\n",
4681 BX_reg(context
), offset
, length
);
4682 if (!LockFile( handle
, offset
, 0, length
, 0 ))
4683 bSetDOSExtendedError
= TRUE
;
4686 case 0x01: /* UNLOCK */
4687 TRACE( "unlock handle %d offset %d length %d\n",
4688 BX_reg(context
), offset
, length
);
4689 if (!UnlockFile( handle
, offset
, 0, length
, 0 ))
4690 bSetDOSExtendedError
= TRUE
;
4694 INT_BARF( context
, 0x21 );
4699 case 0x5d: /* NETWORK 5D */
4700 FIXME( "Network function 5D not implemented.\n" );
4701 SetLastError( ER_NoNetwork
);
4702 bSetDOSExtendedError
= TRUE
;
4705 case 0x5e: /* NETWORK 5E */
4706 bSetDOSExtendedError
= INT21_NetworkFunc( context
);
4709 case 0x5f: /* NETWORK 5F */
4710 /* FIXME: supporting this would need to 1:
4711 * - implement per drive current directory (as kernel32 doesn't)
4712 * - assign enabled/disabled flag on a per drive basis
4714 /* network software not installed */
4715 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context
));
4716 SetLastError( ER_NoNetwork
);
4717 bSetDOSExtendedError
= TRUE
;
4720 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
4722 WCHAR pathW
[MAX_PATH
], res
[MAX_PATH
];
4723 /* FIXME: likely to be broken */
4725 TRACE("TRUENAME %s\n",
4726 (LPCSTR
)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Esi
));
4727 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
4728 if (!GetFullPathNameW( pathW
, 128, res
, NULL
))
4729 bSetDOSExtendedError
= TRUE
;
4732 SET_AX( context
, 0 );
4733 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
4734 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
4740 case 0x61: /* UNUSED */
4741 SET_AL( context
, 0 );
4744 case 0x62: /* GET PSP ADDRESS */
4745 INT21_GetPSP( context
);
4748 case 0x63: /* MISC. LANGUAGE SUPPORT */
4749 switch (AL_reg(context
)) {
4750 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
4751 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
4752 context
->SegDs
= INT21_GetHeapSelector( context
);
4753 SET_SI( context
, offsetof(INT21_HEAP
, dbcs_table
) );
4754 SET_AL( context
, 0 ); /* success */
4759 case 0x64: /* OS/2 DOS BOX */
4760 INT_BARF( context
, 0x21 );
4764 case 0x65: /* EXTENDED COUNTRY INFORMATION */
4765 INT21_ExtendedCountryInformation( context
);
4768 case 0x66: /* GLOBAL CODE PAGE TABLE */
4769 switch (AL_reg(context
))
4772 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
4773 SET_BX( context
, GetOEMCP() );
4774 SET_DX( context
, GetOEMCP() );
4777 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
4778 BX_reg(context
), DX_reg(context
) );
4783 case 0x67: /* SET HANDLE COUNT */
4784 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context
) );
4785 if (SetHandleCount( BX_reg(context
) ) < BX_reg(context
) )
4786 bSetDOSExtendedError
= TRUE
;
4789 case 0x68: /* "FFLUSH" - COMMIT FILE */
4790 TRACE( "FFLUSH - handle %d\n", BX_reg(context
) );
4791 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context
)) ) &&
4792 GetLastError() != ERROR_ACCESS_DENIED
)
4793 bSetDOSExtendedError
= TRUE
;
4796 case 0x69: /* DISK SERIAL NUMBER */
4797 switch (AL_reg(context
))
4800 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
4801 INT21_DriveName(BL_reg(context
)));
4802 if (!INT21_GetDiskSerialNumber(context
)) bSetDOSExtendedError
= TRUE
;
4803 else SET_AX( context
, 0 );
4807 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
4808 INT21_DriveName(BL_reg(context
)));
4809 if (!INT21_SetDiskSerialNumber(context
)) bSetDOSExtendedError
= TRUE
;
4810 else SET_AX( context
, 1 );
4815 case 0x6a: /* COMMIT FILE */
4816 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context
) );
4817 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context
)) ) &&
4818 GetLastError() != ERROR_ACCESS_DENIED
)
4819 bSetDOSExtendedError
= TRUE
;
4822 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4823 SET_AL( context
, 0 );
4826 case 0x6c: /* EXTENDED OPEN/CREATE */
4827 if (!INT21_CreateFile( context
, context
->Esi
, TRUE
,
4828 BX_reg(context
), DL_reg(context
) ))
4829 bSetDOSExtendedError
= TRUE
;
4832 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
4833 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
4834 SET_CFLAG( context
);
4835 SET_AL( context
, 0 );
4838 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
4839 INT21_LongFilename( context
);
4842 case 0x73: /* MSDOS7 - FAT32 */
4843 RESET_CFLAG( context
);
4844 if (!INT21_Fat32( context
))
4845 bSetDOSExtendedError
= TRUE
;
4848 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
4849 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
4852 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
4853 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
4856 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
4857 /* we don't implement a DOS32 extender */
4858 TRACE( "DOS32 EXTENDER API - ignored\n" );
4862 INT_BARF( context
, 0x21 );
4865 } /* END OF SWITCH */
4867 /* Set general error condition. */
4868 if (bSetDOSExtendedError
)
4870 SET_AX( context
, GetLastError() );
4871 SET_CFLAG( context
);
4874 /* Print error code if carry flag is set. */
4875 if (context
->EFlags
& 0x0001)
4876 TRACE("failed, error %d\n", GetLastError() );
4878 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
4879 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
4880 AX_reg(context
), BX_reg(context
),
4881 CX_reg(context
), DX_reg(context
),
4882 SI_reg(context
), DI_reg(context
),
4883 (WORD
)context
->SegDs
, (WORD
)context
->SegEs
,