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/server.h"
41 #include "wine/debug.h"
42 #include "wine/exception.h"
46 * - Most of the file related functions are wrong. NT's kernel32
47 * doesn't maintain a per drive current directory, while DOS does.
48 * We should in fact keep track in here of those per drive
49 * directories, and use this info while dealing with partial paths
50 * (drive defined, but only relative paths). This could even be
51 * created as an array of CDS (there should be an entry for that in
56 * Forward declarations.
58 static BOOL
INT21_RenameFile( CONTEXT
*context
);
60 WINE_DEFAULT_DEBUG_CHANNEL(int21
);
66 * Extended Drive Parameter Block.
67 * This structure is compatible with standard DOS4+ DPB and
70 typedef struct _INT21_DPB
{
71 BYTE drive
; /* 00 drive number (0=A, ...) */
72 BYTE unit
; /* 01 unit number within device driver */
73 WORD sector_bytes
; /* 02 bytes per sector */
74 BYTE cluster_sectors
; /* 04 highest sector number within a cluster */
75 BYTE shift
; /* 05 shift count to convert clusters into sectors */
76 WORD num_reserved
; /* 06 reserved sectors at beginning of drive */
77 BYTE num_FAT
; /* 08 number of FATs */
78 WORD num_root_entries
; /* 09 number of root directory entries */
79 WORD first_data_sector
; /* 0b number of first sector containing user data */
80 WORD num_clusters1
; /* 0d highest cluster number (number of data clusters + 1) */
81 WORD sectors_per_FAT
; /* 0f number of sectors per FAT */
82 WORD first_dir_sector
; /* 11 sector number of first directory sector */
83 SEGPTR driver_header
; /* 13 address of device driver header */
84 BYTE media_ID
; /* 17 media ID byte */
85 BYTE access_flag
; /* 18 0x00 if disk accessed, 0xff if not */
86 SEGPTR next
; /* 19 pointer to next DPB */
87 WORD search_cluster1
; /* 1d cluster at which to start search for free space */
88 WORD free_clusters_lo
; /* 1f number of free clusters on drive or 0xffff if unknown */
89 WORD free_clusters_hi
; /* 21 hiword of clusters_free */
90 WORD mirroring_flags
; /* 23 active FAT/mirroring flags */
91 WORD info_sector
; /* 25 sector number of file system info sector or 0xffff for none */
92 WORD spare_boot_sector
; /* 27 sector number of backup boot sector or 0xffff for none */
93 DWORD first_cluster_sector
; /* 29 sector number of the first cluster */
94 DWORD num_clusters2
; /* 2d maximum cluster number */
95 DWORD fat_clusters
; /* 31 number of clusters occupied by FAT */
96 DWORD root_cluster
; /* 35 cluster number of start of root directory */
97 DWORD search_cluster2
; /* 39 cluster at which to start searching for free space */
102 * Structure for DOS data that can be accessed directly from applications.
103 * Real and protected mode pointers will be returned to this structure so
104 * the structure must be correctly packed.
106 typedef struct _INT21_HEAP
{
107 WORD uppercase_size
; /* Size of the following table in bytes */
108 BYTE uppercase_table
[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
110 WORD lowercase_size
; /* Size of the following table in bytes */
111 BYTE lowercase_table
[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
113 WORD collating_size
; /* Size of the following table in bytes */
114 BYTE collating_table
[256]; /* Values used to sort characters from 0x00 to 0xff. */
116 WORD filename_size
; /* Size of the following filename data in bytes */
117 BYTE filename_reserved1
; /* 0x01 for MS-DOS 3.30-6.00 */
118 BYTE filename_lowest
; /* Lowest permissible character value for filename */
119 BYTE filename_highest
; /* Highest permissible character value for filename */
120 BYTE filename_reserved2
; /* 0x00 for MS-DOS 3.30-6.00 */
121 BYTE filename_exclude_first
; /* First illegal character in permissible range */
122 BYTE filename_exclude_last
; /* Last illegal character in permissible range */
123 BYTE filename_reserved3
; /* 0x02 for MS-DOS 3.30-6.00 */
124 BYTE filename_illegal_size
; /* Number of terminators in the following table */
125 BYTE filename_illegal_table
[16]; /* Characters which terminate a filename */
127 WORD dbcs_size
; /* Number of valid ranges in the following table */
128 BYTE dbcs_table
[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
130 BYTE misc_indos
; /* Interrupt 21 nesting flag */
131 WORD misc_selector
; /* Protected mode selector for INT21_HEAP */
132 INT21_DPB misc_dpb_list
[MAX_DOS_DRIVES
]; /* Drive parameter blocks for all drives */
140 CHAR file_extension
[3];
141 WORD current_block_number
;
142 WORD logical_record_size
;
144 WORD date_of_last_write
;
145 WORD time_of_last_write
;
148 WORD starting_cluster
;
149 WORD sequence_number
;
150 BYTE file_attributes
;
152 BYTE record_within_current_block
;
153 BYTE random_access_record_number
[4];
160 BYTE xfcb_file_attribute
;
164 /* DTA layout for FindFirst/FindNext */
167 BYTE drive
; /* 00 drive letter */
168 char mask
[11]; /* 01 search template */
169 BYTE search_attr
; /* 0c search attributes */
170 WORD count
; /* 0d entry count within directory */
171 WORD cluster
; /* 0f cluster of parent directory */
172 WCHAR
*fullPath
; /* 11 full path (was: reserved) */
173 BYTE fileattr
; /* 15 file attributes */
174 WORD filetime
; /* 16 file time */
175 WORD filedate
; /* 18 file date */
176 DWORD filesize
; /* 1a file size */
177 char filename
[13]; /* 1e file name + extension */
180 /* FCB layout for FindFirstFCB/FindNextFCB */
183 BYTE drive
; /* 00 drive letter */
184 char filename
[11]; /* 01 filename 8+3 format */
185 int count
; /* 0c entry count (was: reserved) */
186 WCHAR
*fullPath
; /* 10 full path (was: reserved) */
189 /* DOS directory entry for FindFirstFCB/FindNextFCB */
192 char filename
[11]; /* 00 filename 8+3 format */
193 BYTE fileattr
; /* 0b file attributes */
194 BYTE reserved
[10]; /* 0c reserved */
195 WORD filetime
; /* 16 file time */
196 WORD filedate
; /* 18 file date */
197 WORD cluster
; /* 1a file first cluster */
198 DWORD filesize
; /* 1c file size */
199 } DOS_DIRENTRY_LAYOUT
;
203 /* dos file attributes */
204 #define FA_NORMAL 0x00 /* Normal file, no attributes */
205 #define FA_RDONLY 0x01 /* Read only attribute */
206 #define FA_HIDDEN 0x02 /* Hidden file */
207 #define FA_SYSTEM 0x04 /* System file */
208 #define FA_LABEL 0x08 /* Volume label */
209 #define FA_DIRECTORY 0x10 /* Directory */
210 #define FA_ARCHIVE 0x20 /* Archive */
211 #define FA_UNUSED 0x40 /* Unused */
214 #define ER_NoNetwork 0x49
217 #define EC_OutOfResource 0x01
218 #define EC_Temporary 0x02
219 #define EC_AccessDenied 0x03
220 #define EC_InternalError 0x04
221 #define EC_HardwareFailure 0x05
222 #define EC_SystemFailure 0x06
223 #define EC_ProgramError 0x07
224 #define EC_NotFound 0x08
225 #define EC_MediaError 0x0b
226 #define EC_Exists 0x0c
227 #define EC_Unknown 0x0d
229 /* Suggested actions */
230 #define SA_Retry 0x01
231 #define SA_DelayedRetry 0x02
232 #define SA_Abort 0x04
233 #define SA_Ignore 0x06
234 #define SA_Ask4Retry 0x07
237 #define EL_Unknown 0x01
239 #define EL_Network 0x03
240 #define EL_Serial 0x04
241 #define EL_Memory 0x05
243 /* BIOS Keyboard Scancodes */
244 #define KEY_LEFT 0x4B
245 #define KEY_RIGHT 0x4D
247 #define KEY_DOWN 0x50
248 #define KEY_IC 0x52 /* insert char */
249 #define KEY_DC 0x53 /* delete char */
250 #define KEY_BACKSPACE 0x0E
251 #define KEY_HOME 0x47
253 #define KEY_NPAGE 0x49
254 #define KEY_PPAGE 0x51
258 static BYTE
drive_number( WCHAR letter
)
260 if (letter
>= 'A' && letter
<= 'Z') return letter
- 'A';
261 if (letter
>= 'a' && letter
<= 'z') return letter
- 'a';
262 return MAX_DOS_DRIVES
;
266 /* Many calls translate a drive argument like this:
267 drive number (00h = default, 01h = A:, etc)
269 /******************************************************************
272 * Many calls translate a drive argument like this:
273 * drive number (00h = default, 01h = A:, etc)
275 static const char *INT21_DriveName(int drive
)
279 if (drive
<= 26) return wine_dbg_sprintf("%c:", 'A' + drive
- 1);
280 else return wine_dbg_sprintf( "<Bad drive: %d>", drive
);
285 /***********************************************************************
286 * INT21_GetCurrentDrive
288 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
289 * MAX_DOS_DRIVES on error.
291 static BYTE
INT21_GetCurrentDrive(void)
293 WCHAR current_directory
[MAX_PATH
];
295 if (!GetCurrentDirectoryW( MAX_PATH
, current_directory
) ||
296 current_directory
[1] != ':')
298 TRACE( "Failed to get current drive.\n" );
299 return MAX_DOS_DRIVES
;
301 return drive_number( current_directory
[0] );
305 /***********************************************************************
308 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
309 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
311 static BYTE
INT21_MapDrive( BYTE drive
)
315 WCHAR drivespec
[] = {'A', ':', 0};
318 drivespec
[0] += drive
- 1;
319 drivetype
= GetDriveTypeW( drivespec
);
321 if (drivetype
== DRIVE_UNKNOWN
|| drivetype
== DRIVE_NO_ROOT_DIR
)
322 return MAX_DOS_DRIVES
;
327 return INT21_GetCurrentDrive();
331 /***********************************************************************
332 * INT21_SetCurrentDrive
334 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
336 static void INT21_SetCurrentDrive( BYTE drive
)
338 WCHAR drivespec
[] = {'A', ':', 0};
340 drivespec
[0] += drive
;
342 if (!SetCurrentDirectoryW( drivespec
))
343 TRACE( "Failed to set current drive.\n" );
347 /***********************************************************************
348 * INT21_GetSystemCountryCode
350 * Return DOS country code for default system locale.
352 static WORD
INT21_GetSystemCountryCode( void )
354 return GetSystemDefaultLangID();
358 /***********************************************************************
359 * INT21_FillCountryInformation
361 * Fill 34-byte buffer with country information data using
362 * default system locale.
364 static void INT21_FillCountryInformation( BYTE
*buffer
)
366 /* 00 - WORD: date format
371 *(WORD
*)(buffer
+ 0) = 0; /* FIXME: Get from locale */
373 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
374 buffer
[2] = '$'; /* FIXME: Get from locale */
377 /* 07 - BYTE[2]: ASCIIZ thousands separator */
378 buffer
[7] = 0; /* FIXME: Get from locale */
381 /* 09 - BYTE[2]: ASCIIZ decimal separator */
382 buffer
[9] = '.'; /* FIXME: Get from locale */
385 /* 11 - BYTE[2]: ASCIIZ date separator */
386 buffer
[11] = '/'; /* FIXME: Get from locale */
389 /* 13 - BYTE[2]: ASCIIZ time separator */
390 buffer
[13] = ':'; /* FIXME: Get from locale */
393 /* 15 - BYTE: Currency format
394 * bit 2 = set if currency symbol replaces decimal point
395 * bit 1 = number of spaces between value and currency symbol
396 * bit 0 = 0 if currency symbol precedes value
397 * 1 if currency symbol follows value
399 buffer
[15] = 0; /* FIXME: Get from locale */
401 /* 16 - BYTE: Number of digits after decimal in currency */
402 buffer
[16] = 0; /* FIXME: Get from locale */
404 /* 17 - BYTE: Time format
405 * bit 0 = 0 if 12-hour clock
408 buffer
[17] = 1; /* FIXME: Get from locale */
410 /* 18 - DWORD: Address of case map routine */
411 *(DWORD
*)(buffer
+ 18) = 0; /* FIXME: ptr to case map routine */
413 /* 22 - BYTE[2]: ASCIIZ data-list separator */
414 buffer
[22] = ','; /* FIXME: Get from locale */
417 /* 24 - BYTE[10]: Reserved */
418 memset( buffer
+ 24, 0, 10 );
422 /***********************************************************************
425 * Initialize DOS heap.
427 * Filename Terminator Table of w2k DE NTVDM:
428 * 16 00 01 00 FF 00 00 20-02 0E 2E 22 2F 5C 5B 5D ....... ..."/\[]
429 * 3A 7C 3C 3E 2B 3D 3B 2C-00 :|<>+=;,
431 static void INT21_FillHeap( INT21_HEAP
*heap
)
433 static const char terminators
[] = ".\"/\\[]:|<>+=;,";
439 heap
->uppercase_size
= 128;
440 for (i
= 0; i
< 128; i
++)
441 heap
->uppercase_table
[i
] = toupper( 128 + i
);
446 heap
->lowercase_size
= 256;
447 for (i
= 0; i
< 256; i
++)
448 heap
->lowercase_table
[i
] = tolower( i
);
453 heap
->collating_size
= 256;
454 for (i
= 0; i
< 256; i
++)
455 heap
->collating_table
[i
] = i
;
460 heap
->filename_size
= 8 + strlen(terminators
);
461 heap
->filename_illegal_size
= strlen(terminators
);
462 memcpy( heap
->filename_illegal_table
, terminators
, heap
->filename_illegal_size
);
464 heap
->filename_reserved1
= 0x01;
465 heap
->filename_lowest
= 0x00;
466 heap
->filename_highest
= 0xff;
467 heap
->filename_reserved2
= 0x00;
468 heap
->filename_exclude_first
= 0x00;
469 heap
->filename_exclude_last
= 0x20;
470 heap
->filename_reserved3
= 0x02;
473 * DBCS lead byte table. This table is empty.
476 memset( heap
->dbcs_table
, 0, sizeof(heap
->dbcs_table
) );
479 * Initialize InDos flag.
481 heap
->misc_indos
= 0;
484 * FIXME: Should drive parameter blocks (DPB) be
485 * initialized here and linked to DOS LOL?
490 /***********************************************************************
491 * INT21_GetHeapPointer
493 * Get pointer for DOS heap (INT21_HEAP).
494 * Creates and initializes heap on first call.
496 static INT21_HEAP
*INT21_GetHeapPointer( void )
498 static INT21_HEAP
*heap_pointer
= NULL
;
502 WORD heap_selector
= GlobalAlloc16( GMEM_FIXED
, sizeof(INT21_HEAP
) );
503 heap_pointer
= GlobalLock16( heap_selector
);
504 heap_pointer
->misc_selector
= heap_selector
;
505 INT21_FillHeap( heap_pointer
);
512 /***********************************************************************
513 * INT21_GetHeapSelector
515 * Get segment/selector for DOS heap (INT21_HEAP).
516 * Creates and initializes heap on first call.
518 static WORD
INT21_GetHeapSelector( CONTEXT
*context
)
520 INT21_HEAP
*heap
= INT21_GetHeapPointer();
521 return heap
->misc_selector
;
525 /***********************************************************************
528 * Fill DOS heap drive parameter block for the specified drive.
529 * Return TRUE if drive was valid and there were
530 * no errors while reading drive information.
532 static BOOL
INT21_FillDrivePB( BYTE drive
)
534 WCHAR drivespec
[] = {'A', ':', 0};
535 INT21_HEAP
*heap
= INT21_GetHeapPointer();
538 DWORD cluster_sectors
;
541 DWORD total_clusters
;
543 if (drive
>= MAX_DOS_DRIVES
)
546 dpb
= &heap
->misc_dpb_list
[drive
];
547 drivespec
[0] += drive
;
548 drivetype
= GetDriveTypeW( drivespec
);
551 * FIXME: Does this check work correctly with floppy/cdrom drives?
553 if (drivetype
== DRIVE_NO_ROOT_DIR
|| drivetype
== DRIVE_UNKNOWN
)
557 * FIXME: Does this check work correctly with floppy/cdrom drives?
559 if (!GetDiskFreeSpaceW( drivespec
, &cluster_sectors
, §or_bytes
,
560 &free_clusters
, &total_clusters
))
564 * FIXME: Most of the values listed below are incorrect.
565 * All values should be validated.
570 dpb
->sector_bytes
= sector_bytes
;
571 dpb
->cluster_sectors
= cluster_sectors
- 1;
574 while (cluster_sectors
> 1)
576 cluster_sectors
/= 2;
580 dpb
->num_reserved
= 0;
582 dpb
->num_root_entries
= 2;
583 dpb
->first_data_sector
= 2;
584 dpb
->num_clusters1
= total_clusters
;
585 dpb
->sectors_per_FAT
= 1;
586 dpb
->first_dir_sector
= 1;
587 dpb
->driver_header
= 0;
588 dpb
->media_ID
= (drivetype
== DRIVE_FIXED
) ? 0xF8 : 0xF0;
589 dpb
->access_flag
= 0;
591 dpb
->search_cluster1
= 0;
592 dpb
->free_clusters_lo
= LOWORD(free_clusters
);
593 dpb
->free_clusters_hi
= HIWORD(free_clusters
);
594 dpb
->mirroring_flags
= 0;
595 dpb
->info_sector
= 0xffff;
596 dpb
->spare_boot_sector
= 0xffff;
597 dpb
->first_cluster_sector
= 0;
598 dpb
->num_clusters2
= total_clusters
;
599 dpb
->fat_clusters
= 32;
600 dpb
->root_cluster
= 0;
601 dpb
->search_cluster2
= 0;
607 /***********************************************************************
608 * INT21_GetCurrentDirectory
612 * - subfunction 0x47 of function 0x71
614 static BOOL
INT21_GetCurrentDirectory( CONTEXT
*context
, BOOL islong
)
616 char *buffer
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
);
617 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
618 WCHAR pathW
[MAX_PATH
];
619 char pathA
[MAX_PATH
];
622 TRACE( "drive %d\n", DL_reg(context
) );
624 if (drive
== MAX_DOS_DRIVES
)
626 SetLastError(ERROR_INVALID_DRIVE
);
631 * Grab current directory.
634 if (!GetCurrentDirectoryW( MAX_PATH
, pathW
)) return FALSE
;
636 if (drive_number( pathW
[0] ) != drive
|| pathW
[1] != ':')
638 /* cwd is not on the requested drive, get the environment string instead */
643 env_var
[1] = 'A' + drive
;
646 if (!GetEnvironmentVariableW( env_var
, pathW
, MAX_PATH
))
648 /* return empty path */
655 * Convert into short format.
660 DWORD result
= GetShortPathNameW( pathW
, pathW
, MAX_PATH
);
663 if (result
> MAX_PATH
)
665 WARN( "Short path too long!\n" );
666 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
672 * The returned pathname does not include
673 * the drive letter, colon or leading backslash.
679 * FIXME: We should probably just strip host part from name...
681 FIXME( "UNC names are not supported.\n" );
682 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
685 else if (!ptr
[0] || ptr
[1] != ':' || ptr
[2] != '\\')
687 WARN( "Path is neither UNC nor DOS path: %s\n",
688 wine_dbgstr_w(ptr
) );
689 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
694 /* Remove drive letter, colon and leading backslash. */
699 * Convert into OEM string.
702 if (!WideCharToMultiByte(CP_OEMCP
, 0, ptr
, -1, pathA
,
703 MAX_PATH
, NULL
, NULL
))
705 WARN( "Cannot convert path!\n" );
706 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
716 /* Undocumented success code. */
717 SET_AX( context
, 0x0100 );
719 /* Truncate buffer to 64 bytes. */
723 TRACE( "%c:=%s\n", 'A' + drive
, pathA
);
725 strcpy( buffer
, pathA
);
730 /***********************************************************************
731 * INT21_SetCurrentDirectory
735 * - subfunction 0x3b of function 0x71
737 static BOOL
INT21_SetCurrentDirectory( CONTEXT
*context
)
739 WCHAR dirW
[MAX_PATH
];
742 char *dirA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
743 BYTE drive
= INT21_GetCurrentDrive();
746 TRACE( "SET CURRENT DIRECTORY %s\n", dirA
);
748 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
749 if (!GetFullPathNameW( dirW
, MAX_PATH
, dirW
, NULL
)) return FALSE
;
751 attr
= GetFileAttributesW( dirW
);
752 if (attr
== INVALID_FILE_ATTRIBUTES
|| !(attr
& FILE_ATTRIBUTE_DIRECTORY
))
754 SetLastError( ERROR_PATH_NOT_FOUND
);
759 env_var
[1] = dirW
[0];
762 result
= SetEnvironmentVariableW( env_var
, dirW
);
764 /* only set current directory if on the current drive */
765 if (result
&& (drive_number( dirW
[0] ) == drive
)) result
= SetCurrentDirectoryW( dirW
);
770 /***********************************************************************
778 * - subfunction 0x6c of function 0x71
780 static BOOL
INT21_CreateFile( CONTEXT
*context
,
787 char *pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, pathSegOff
);
788 WCHAR pathW
[MAX_PATH
];
795 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
796 "create flags=%04x, file=%s.\n",
797 AH_reg(context
), dosAction
, dosAccessShare
, CX_reg(context
), pathA
);
800 * Application tried to create/open a file whose name
801 * ends with a backslash. This is not allowed.
803 * FIXME: This needs to be validated, especially the return value.
805 if (pathA
[strlen(pathA
) - 1] == '/')
807 SetLastError( ERROR_FILE_NOT_FOUND
);
812 * Convert DOS action flags into Win32 creation disposition parameter.
817 winMode
= OPEN_EXISTING
;
820 winMode
= TRUNCATE_EXISTING
;
823 winMode
= CREATE_NEW
;
826 winMode
= OPEN_ALWAYS
;
829 winMode
= CREATE_ALWAYS
;
832 SetLastError( ERROR_INVALID_PARAMETER
);
837 * Convert DOS access/share flags into Win32 desired access parameter.
839 switch(dosAccessShare
& 0x07)
842 winAccess
= GENERIC_READ
;
845 winAccess
= GENERIC_WRITE
;
848 winAccess
= GENERIC_READ
| GENERIC_WRITE
;
852 * Read-only, do not modify file's last-access time (DOS7).
854 * FIXME: How to prevent modification of last-access time?
856 winAccess
= GENERIC_READ
;
863 * Convert DOS access/share flags into Win32 share mode parameter.
865 switch(dosAccessShare
& 0x70)
867 case OF_SHARE_EXCLUSIVE
:
870 case OF_SHARE_DENY_WRITE
:
871 winSharing
= FILE_SHARE_READ
;
873 case OF_SHARE_DENY_READ
:
874 winSharing
= FILE_SHARE_WRITE
;
876 case OF_SHARE_DENY_NONE
:
877 case OF_SHARE_COMPAT
:
879 winSharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
883 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
884 * What to do with this bit?
885 * FIXME: Bits in the high byte of dosAccessShare are not supported.
886 * See both function 0x6c and subfunction 0x6c of function 0x71 for
887 * definition of these bits.
891 * Convert DOS create attributes into Win32 flags and attributes parameter.
893 if (winMode
== OPEN_EXISTING
|| winMode
== TRUNCATE_EXISTING
)
899 WORD dosAttributes
= CX_reg(context
);
901 if (dosAttributes
& FA_LABEL
)
904 * Application tried to create volume label entry.
905 * This is difficult to support so we do not allow it.
907 * FIXME: If volume does not already have a label,
908 * this function is supposed to succeed.
910 SetLastError( ERROR_ACCESS_DENIED
);
914 winAttributes
= dosAttributes
&
915 (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
|
916 FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_ARCHIVE
);
922 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
924 winHandle
= CreateFileW( pathW
, winAccess
, winSharing
, NULL
, winMode
, winAttributes
, 0 );
925 /* DOS allows opening files on a CDROM R/W */
926 if( winHandle
== INVALID_HANDLE_VALUE
&&
927 (GetLastError() == ERROR_WRITE_PROTECT
||
928 GetLastError() == ERROR_ACCESS_DENIED
)) {
929 winHandle
= CreateFileW( pathW
, winAccess
& ~GENERIC_WRITE
,
930 winSharing
, NULL
, winMode
, winAttributes
, 0 );
933 if (winHandle
== INVALID_HANDLE_VALUE
)
937 * Determine DOS file status.
948 case TRUNCATE_EXISTING
:
955 dosStatus
= (GetLastError() == ERROR_ALREADY_EXISTS
) ? 1 : 2;
958 dosStatus
= (GetLastError() == ERROR_ALREADY_EXISTS
) ? 3 : 2;
965 * Return DOS file handle and DOS status.
967 SET_AX( context
, Win32HandleToDosFileHandle(winHandle
) );
970 SET_CX( context
, dosStatus
);
972 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
973 AX_reg(context
), dosStatus
);
979 /***********************************************************************
980 * INT21_GetCurrentDTA
982 static BYTE
*INT21_GetCurrentDTA( CONTEXT
*context
)
984 TDB
*pTask
= GlobalLock16(GetCurrentTask());
986 /* FIXME: This assumes DTA was set correctly! */
987 return CTX_SEG_OFF_TO_LIN( context
, SELECTOROF(pTask
->dta
),
988 OFFSETOF(pTask
->dta
) );
992 /***********************************************************************
993 * INT21_OpenFileUsingFCB
995 * Handler for function 0x0f.
998 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
1005 * Opens a FCB file for read/write in compatibility mode. Upon calling
1006 * the FCB must have the drive_number, file_name, and file_extension
1007 * fields filled and all other bytes cleared.
1009 static void INT21_OpenFileUsingFCB( CONTEXT
*context
)
1017 BY_HANDLE_FILE_INFORMATION info
;
1020 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1021 if (fcb
->drive_number
== 0xff) {
1022 xfcb
= (struct XFCB
*) fcb
;
1023 fcb
= (struct FCB
*) xfcb
->fcb
;
1027 file_path
[0] = 'A' + INT21_MapDrive( fcb
->drive_number
);
1029 if (AL_result
== 0) {
1031 pos
= &file_path
[2];
1032 memcpy(pos
, fcb
->file_name
, 8);
1035 pos
= strchr(pos
, ' ');
1038 memcpy(pos
, fcb
->file_extension
, 3);
1041 pos
= strchr(pos
, ' ');
1044 handle
= (HANDLE
) _lopen(file_path
, OF_READWRITE
);
1045 if (handle
== INVALID_HANDLE_VALUE
) {
1046 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path
);
1047 AL_result
= 0xff; /* failed */
1049 hfile16
= Win32HandleToDosFileHandle(handle
);
1050 if (hfile16
== HFILE_ERROR16
) {
1051 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle
);
1052 CloseHandle(handle
);
1053 AL_result
= 0xff; /* failed */
1054 } else if (hfile16
> 255) {
1055 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16
, file_path
);
1057 AL_result
= 0xff; /* failed */
1059 if (!GetFileInformationByHandle(handle
, &info
)) {
1060 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1061 hfile16
, handle
, file_path
);
1063 AL_result
= 0xff; /* failed */
1065 fcb
->drive_number
= file_path
[0] - 'A' + 1;
1066 fcb
->current_block_number
= 0;
1067 fcb
->logical_record_size
= 128;
1068 fcb
->file_size
= info
.nFileSizeLow
;
1069 FileTimeToDosDateTime(&info
.ftLastWriteTime
,
1070 &fcb
->date_of_last_write
, &fcb
->time_of_last_write
);
1071 fcb
->file_number
= hfile16
;
1072 fcb
->attributes
= 0xc2;
1073 fcb
->starting_cluster
= 0; /* don't know correct init value */
1074 fcb
->sequence_number
= 0; /* don't know correct init value */
1075 fcb
->file_attributes
= info
.dwFileAttributes
;
1076 /* The following fields are not initialized */
1077 /* by the native function: */
1079 /* record_within_current_block */
1080 /* random_access_record_number */
1082 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1083 file_path
, hfile16
, handle
);
1084 AL_result
= 0x00; /* successful */
1089 SET_AL(context
, AL_result
);
1093 /***********************************************************************
1094 * INT21_CloseFileUsingFCB
1096 * Handler for function 0x10.
1099 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1106 * Closes a FCB file.
1108 static void INT21_CloseFileUsingFCB( CONTEXT
*context
)
1114 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1115 if (fcb
->drive_number
== 0xff) {
1116 xfcb
= (struct XFCB
*) fcb
;
1117 fcb
= (struct FCB
*) xfcb
->fcb
;
1120 if (_lclose16((HFILE16
) fcb
->file_number
) != 0) {
1121 TRACE("_lclose16(%d) failed\n", fcb
->file_number
);
1122 AL_result
= 0xff; /* failed */
1124 TRACE("successful closed file %d\n", fcb
->file_number
);
1125 AL_result
= 0x00; /* successful */
1127 SET_AL(context
, AL_result
);
1131 /***********************************************************************
1132 * INT21_SequentialReadFromFCB
1134 * Handler for function 0x14.
1137 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1141 * 1: end of file, no data read
1142 * 2: segment wrap in DTA, no data read (not returned now)
1143 * 3: end of file, partial record read
1146 * Reads a record with the size FCB->logical_record_size from the FCB
1147 * to the disk transfer area. The position of the record is specified
1148 * with FCB->current_block_number and FCB->record_within_current_block.
1149 * Then FCB->current_block_number and FCB->record_within_current_block
1150 * are updated to point to the next record. If a partial record is
1151 * read, it is filled with zeros up to the FCB->logical_record_size.
1153 static void INT21_SequentialReadFromFCB( CONTEXT
*context
)
1158 DWORD record_number
;
1160 BYTE
*disk_transfer_area
;
1164 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1165 if (fcb
->drive_number
== 0xff) {
1166 xfcb
= (struct XFCB
*) fcb
;
1167 fcb
= (struct FCB
*) xfcb
->fcb
;
1170 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1171 if (handle
== INVALID_HANDLE_VALUE
) {
1172 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1174 AL_result
= 0x01; /* end of file, no data read */
1176 record_number
= 128 * fcb
->current_block_number
+ fcb
->record_within_current_block
;
1177 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1178 if (position
!= record_number
* fcb
->logical_record_size
) {
1179 TRACE("seek(%d, %d, 0) failed with %u\n",
1180 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1181 AL_result
= 0x01; /* end of file, no data read */
1183 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1184 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, fcb
->logical_record_size
);
1185 if (bytes_read
!= fcb
->logical_record_size
) {
1186 TRACE("_lread(%d, %p, %d) failed with %d\n",
1187 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_read
);
1188 if (bytes_read
== 0) {
1189 AL_result
= 0x01; /* end of file, no data read */
1191 memset(&disk_transfer_area
[bytes_read
], 0, fcb
->logical_record_size
- bytes_read
);
1192 AL_result
= 0x03; /* end of file, partial record read */
1195 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1196 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1197 AL_result
= 0x00; /* successful */
1201 if (AL_result
== 0x00 || AL_result
== 0x03) {
1202 if (fcb
->record_within_current_block
== 127) {
1203 fcb
->record_within_current_block
= 0;
1204 fcb
->current_block_number
++;
1206 fcb
->record_within_current_block
++;
1209 SET_AL(context
, AL_result
);
1213 /***********************************************************************
1214 * INT21_SequentialWriteToFCB
1216 * Handler for function 0x15.
1219 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1224 * 2: segment wrap in DTA (not returned now)
1227 * Writes a record with the size FCB->logical_record_size from the disk
1228 * transfer area to the FCB. The position of the record is specified
1229 * with FCB->current_block_number and FCB->record_within_current_block.
1230 * Then FCB->current_block_number and FCB->record_within_current_block
1231 * are updated to point to the next record.
1233 static void INT21_SequentialWriteToFCB( CONTEXT
*context
)
1238 DWORD record_number
;
1240 BYTE
*disk_transfer_area
;
1244 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1245 if (fcb
->drive_number
== 0xff) {
1246 xfcb
= (struct XFCB
*) fcb
;
1247 fcb
= (struct FCB
*) xfcb
->fcb
;
1250 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1251 if (handle
== INVALID_HANDLE_VALUE
) {
1252 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1254 AL_result
= 0x01; /* disk full */
1256 record_number
= 128 * fcb
->current_block_number
+ fcb
->record_within_current_block
;
1257 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1258 if (position
!= record_number
* fcb
->logical_record_size
) {
1259 TRACE("seek(%d, %d, 0) failed with %u\n",
1260 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1261 AL_result
= 0x01; /* disk full */
1263 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1264 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, fcb
->logical_record_size
);
1265 if (bytes_written
!= fcb
->logical_record_size
) {
1266 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1267 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_written
);
1268 AL_result
= 0x01; /* disk full */
1270 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1271 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1272 AL_result
= 0x00; /* successful */
1276 if (AL_result
== 0x00) {
1277 if (fcb
->record_within_current_block
== 127) {
1278 fcb
->record_within_current_block
= 0;
1279 fcb
->current_block_number
++;
1281 fcb
->record_within_current_block
++;
1284 SET_AL(context
, AL_result
);
1288 /***********************************************************************
1289 * INT21_ReadRandomRecordFromFCB
1291 * Handler for function 0x21.
1294 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1298 * 1: end of file, no data read
1299 * 2: segment wrap in DTA, no data read (not returned now)
1300 * 3: end of file, partial record read
1303 * Reads a record with the size FCB->logical_record_size from
1304 * the FCB to the disk transfer area. The position of the record
1305 * is specified with FCB->random_access_record_number. The
1306 * FCB->random_access_record_number is not updated. If a partial record
1307 * is read, it is filled with zeros up to the FCB->logical_record_size.
1309 static void INT21_ReadRandomRecordFromFCB( CONTEXT
*context
)
1314 DWORD record_number
;
1316 BYTE
*disk_transfer_area
;
1320 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1321 if (fcb
->drive_number
== 0xff) {
1322 xfcb
= (struct XFCB
*) fcb
;
1323 fcb
= (struct FCB
*) xfcb
->fcb
;
1326 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1327 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1328 if (handle
== INVALID_HANDLE_VALUE
) {
1329 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1331 AL_result
= 0x01; /* end of file, no data read */
1333 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1334 if (position
!= record_number
* fcb
->logical_record_size
) {
1335 TRACE("seek(%d, %d, 0) failed with %u\n",
1336 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1337 AL_result
= 0x01; /* end of file, no data read */
1339 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1340 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, fcb
->logical_record_size
);
1341 if (bytes_read
!= fcb
->logical_record_size
) {
1342 TRACE("_lread(%d, %p, %d) failed with %d\n",
1343 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_read
);
1344 if (bytes_read
== 0) {
1345 AL_result
= 0x01; /* end of file, no data read */
1347 memset(&disk_transfer_area
[bytes_read
], 0, fcb
->logical_record_size
- bytes_read
);
1348 AL_result
= 0x03; /* end of file, partial record read */
1351 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1352 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1353 AL_result
= 0x00; /* successful */
1357 fcb
->current_block_number
= record_number
/ 128;
1358 fcb
->record_within_current_block
= record_number
% 128;
1359 SET_AL(context
, AL_result
);
1363 /***********************************************************************
1364 * INT21_WriteRandomRecordToFCB
1366 * Handler for function 0x22.
1369 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1374 * 2: segment wrap in DTA (not returned now)
1377 * Writes a record with the size FCB->logical_record_size from
1378 * the disk transfer area to the FCB. The position of the record
1379 * is specified with FCB->random_access_record_number. The
1380 * FCB->random_access_record_number is not updated.
1382 static void INT21_WriteRandomRecordToFCB( CONTEXT
*context
)
1387 DWORD record_number
;
1389 BYTE
*disk_transfer_area
;
1393 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1394 if (fcb
->drive_number
== 0xff) {
1395 xfcb
= (struct XFCB
*) fcb
;
1396 fcb
= (struct FCB
*) xfcb
->fcb
;
1399 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1400 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1401 if (handle
== INVALID_HANDLE_VALUE
) {
1402 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1404 AL_result
= 0x01; /* disk full */
1406 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1407 if (position
!= record_number
* fcb
->logical_record_size
) {
1408 TRACE("seek(%d, %d, 0) failed with %u\n",
1409 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1410 AL_result
= 0x01; /* disk full */
1412 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1413 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, fcb
->logical_record_size
);
1414 if (bytes_written
!= fcb
->logical_record_size
) {
1415 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1416 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_written
);
1417 AL_result
= 0x01; /* disk full */
1419 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1420 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1421 AL_result
= 0x00; /* successful */
1425 fcb
->current_block_number
= record_number
/ 128;
1426 fcb
->record_within_current_block
= record_number
% 128;
1427 SET_AL(context
, AL_result
);
1431 /***********************************************************************
1432 * INT21_RandomBlockReadFromFCB
1434 * Handler for function 0x27.
1437 * CX [I/O] Number of records to read
1438 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1442 * 1: end of file, no data read
1443 * 2: segment wrap in DTA, no data read (not returned now)
1444 * 3: end of file, partial record read
1447 * Reads several records with the size FCB->logical_record_size from
1448 * the FCB to the disk transfer area. The number of records to be
1449 * read is specified in the CX register. The position of the first
1450 * record is specified with FCB->random_access_record_number. The
1451 * FCB->random_access_record_number, the FCB->current_block_number
1452 * and FCB->record_within_current_block are updated to point to the
1453 * next record after the records read. If a partial record is read,
1454 * it is filled with zeros up to the FCB->logical_record_size. The
1455 * CX register is set to the number of successfully read records.
1457 static void INT21_RandomBlockReadFromFCB( CONTEXT
*context
)
1462 DWORD record_number
;
1464 BYTE
*disk_transfer_area
;
1465 UINT records_requested
;
1466 UINT bytes_requested
;
1471 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1472 if (fcb
->drive_number
== 0xff) {
1473 xfcb
= (struct XFCB
*) fcb
;
1474 fcb
= (struct FCB
*) xfcb
->fcb
;
1477 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1478 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1479 if (handle
== INVALID_HANDLE_VALUE
) {
1480 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1483 AL_result
= 0x01; /* end of file, no data read */
1485 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1486 if (position
!= record_number
* fcb
->logical_record_size
) {
1487 TRACE("seek(%d, %d, 0) failed with %u\n",
1488 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1490 AL_result
= 0x01; /* end of file, no data read */
1492 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1493 records_requested
= CX_reg(context
);
1494 bytes_requested
= records_requested
* fcb
->logical_record_size
;
1495 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, bytes_requested
);
1496 if (bytes_read
!= bytes_requested
) {
1497 TRACE("_lread(%d, %p, %d) failed with %d\n",
1498 fcb
->file_number
, disk_transfer_area
, bytes_requested
, bytes_read
);
1499 records_read
= bytes_read
/ fcb
->logical_record_size
;
1500 if (bytes_read
% fcb
->logical_record_size
== 0) {
1501 AL_result
= 0x01; /* end of file, no data read */
1504 memset(&disk_transfer_area
[bytes_read
], 0, records_read
* fcb
->logical_record_size
- bytes_read
);
1505 AL_result
= 0x03; /* end of file, partial record read */
1508 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1509 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1510 records_read
= records_requested
;
1511 AL_result
= 0x00; /* successful */
1515 record_number
+= records_read
;
1516 memcpy(fcb
->random_access_record_number
, &record_number
, 4);
1517 fcb
->current_block_number
= record_number
/ 128;
1518 fcb
->record_within_current_block
= record_number
% 128;
1519 SET_CX(context
, records_read
);
1520 SET_AL(context
, AL_result
);
1524 /***********************************************************************
1525 * INT21_RandomBlockWriteToFCB
1527 * Handler for function 0x28.
1530 * CX [I/O] Number of records to write
1531 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1536 * 2: segment wrap in DTA (not returned now)
1539 * Writes several records with the size FCB->logical_record_size from
1540 * the disk transfer area to the FCB. The number of records to be
1541 * written is specified in the CX register. The position of the first
1542 * record is specified with FCB->random_access_record_number. The
1543 * FCB->random_access_record_number, the FCB->current_block_number
1544 * and FCB->record_within_current_block are updated to point to the
1545 * next record after the records written. The CX register is set to
1546 * the number of successfully written records.
1548 static void INT21_RandomBlockWriteToFCB( CONTEXT
*context
)
1553 DWORD record_number
;
1555 BYTE
*disk_transfer_area
;
1556 UINT records_requested
;
1557 UINT bytes_requested
;
1559 UINT records_written
;
1562 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1563 if (fcb
->drive_number
== 0xff) {
1564 xfcb
= (struct XFCB
*) fcb
;
1565 fcb
= (struct FCB
*) xfcb
->fcb
;
1568 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1569 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1570 if (handle
== INVALID_HANDLE_VALUE
) {
1571 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1573 records_written
= 0;
1574 AL_result
= 0x01; /* disk full */
1576 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1577 if (position
!= record_number
* fcb
->logical_record_size
) {
1578 TRACE("seek(%d, %d, 0) failed with %u\n",
1579 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1580 records_written
= 0;
1581 AL_result
= 0x01; /* disk full */
1583 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1584 records_requested
= CX_reg(context
);
1585 bytes_requested
= records_requested
* fcb
->logical_record_size
;
1586 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, bytes_requested
);
1587 if (bytes_written
!= bytes_requested
) {
1588 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1589 fcb
->file_number
, disk_transfer_area
, bytes_requested
, bytes_written
);
1590 records_written
= bytes_written
/ fcb
->logical_record_size
;
1591 AL_result
= 0x01; /* disk full */
1593 TRACE("successful write %d bytes from record %d (position %u) of file %d (handle %p)\n",
1594 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1595 records_written
= records_requested
;
1596 AL_result
= 0x00; /* successful */
1600 record_number
+= records_written
;
1601 memcpy(fcb
->random_access_record_number
, &record_number
, 4);
1602 fcb
->current_block_number
= record_number
/ 128;
1603 fcb
->record_within_current_block
= record_number
% 128;
1604 SET_CX(context
, records_written
);
1605 SET_AL(context
, AL_result
);
1609 /***********************************************************************
1610 * INT21_CreateDirectory
1614 * - subfunction 0x39 of function 0x71
1615 * - subfunction 0xff of function 0x43 (CL == 0x39)
1617 static BOOL
INT21_CreateDirectory( CONTEXT
*context
)
1619 WCHAR dirW
[MAX_PATH
];
1620 char *dirA
= CTX_SEG_OFF_TO_LIN(context
,
1624 TRACE( "CREATE DIRECTORY %s\n", dirA
);
1626 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
1628 if (CreateDirectoryW(dirW
, NULL
))
1632 * FIXME: CreateDirectory's LastErrors will clash with the ones
1633 * used by DOS. AH=39 only returns 3 (path not found) and
1634 * 5 (access denied), while CreateDirectory return several
1635 * ones. Remap some of them. -Marcus
1637 switch (GetLastError())
1639 case ERROR_ALREADY_EXISTS
:
1640 case ERROR_FILENAME_EXCED_RANGE
:
1641 case ERROR_DISK_FULL
:
1642 SetLastError(ERROR_ACCESS_DENIED
);
1652 /***********************************************************************
1653 * INT21_ExtendedCountryInformation
1655 * Handler for function 0x65.
1657 static void INT21_ExtendedCountryInformation( CONTEXT
*context
)
1659 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN( context
, context
->SegEs
, context
->Edi
);
1660 BYTE buffsize
= CX_reg (context
);
1662 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1666 * Check subfunctions that are passed country and code page.
1668 if (AL_reg(context
) >= 0x01 && AL_reg(context
) <= 0x07)
1670 WORD country
= DX_reg(context
);
1671 WORD codepage
= BX_reg(context
);
1673 if (country
!= 0xffff && country
!= INT21_GetSystemCountryCode())
1674 FIXME( "Requested info on non-default country %04x\n", country
);
1676 if (codepage
!= 0xffff && codepage
!= GetOEMCP())
1677 FIXME( "Requested info on non-default code page %04x\n", codepage
);
1680 switch (AL_reg(context
)) {
1681 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1682 INT_BARF( context
, 0x21 );
1683 SET_CFLAG( context
);
1686 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1687 TRACE( "Get general internationalization info\n" );
1688 dataptr
[0] = 0x01; /* Info ID */
1689 *(WORD
*)(dataptr
+1) = 38; /* Size of the following info */
1690 *(WORD
*)(dataptr
+3) = INT21_GetSystemCountryCode(); /* Country ID */
1691 *(WORD
*)(dataptr
+5) = GetOEMCP(); /* Code page */
1692 /* FIXME: fill buffer partially up to buffsize bytes*/
1693 if (buffsize
>= 0x29){
1694 INT21_FillCountryInformation( dataptr
+ 7 );
1695 SET_CX( context
, 0x29 ); /* Size of returned info */
1697 SET_CX( context
, 0x07 ); /* Size of returned info */
1701 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1702 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1703 TRACE( "Get pointer to uppercase table\n" );
1704 dataptr
[0] = AL_reg(context
); /* Info ID */
1705 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1706 offsetof(INT21_HEAP
, uppercase_size
) );
1707 SET_CX( context
, 5 ); /* Size of returned info */
1710 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1711 TRACE( "Get pointer to lowercase table\n" );
1712 dataptr
[0] = 0x03; /* Info ID */
1713 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1714 offsetof(INT21_HEAP
, lowercase_size
) );
1715 SET_CX( context
, 5 ); /* Size of returned info */
1718 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1719 TRACE("Get pointer to filename terminator table\n");
1720 dataptr
[0] = 0x05; /* Info ID */
1721 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1722 offsetof(INT21_HEAP
, filename_size
) );
1723 SET_CX( context
, 5 ); /* Size of returned info */
1726 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1727 TRACE("Get pointer to collating sequence table\n");
1728 dataptr
[0] = 0x06; /* Info ID */
1729 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1730 offsetof(INT21_HEAP
, collating_size
) );
1731 SET_CX( context
, 5 ); /* Size of returned info */
1734 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1735 TRACE("Get pointer to DBCS lead byte table\n");
1736 dataptr
[0] = 0x07; /* Info ID */
1737 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1738 offsetof(INT21_HEAP
, dbcs_size
) );
1739 SET_CX( context
, 5 ); /* Size of returned info */
1742 case 0x20: /* CAPITALIZE CHARACTER */
1743 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1744 TRACE("Convert char to uppercase\n");
1745 SET_DL( context
, toupper(DL_reg(context
)) );
1748 case 0x21: /* CAPITALIZE STRING */
1749 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1750 TRACE("Convert string to uppercase with length\n");
1752 char *ptr
= CTX_SEG_OFF_TO_LIN( context
, context
->SegDs
,
1754 WORD len
= CX_reg(context
);
1755 while (len
--) { *ptr
= toupper(*ptr
); ptr
++; }
1759 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1760 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1761 TRACE("Convert ASCIIZ string to uppercase\n");
1763 char *p
= CTX_SEG_OFF_TO_LIN( context
, context
->SegDs
, context
->Edx
);
1764 for ( ; *p
; p
++) *p
= toupper(*p
);
1768 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1769 INT_BARF( context
, 0x21 );
1770 SET_CFLAG( context
);
1774 INT_BARF( context
, 0x21 );
1781 /***********************************************************************
1782 * INT21_FileAttributes
1786 * - subfunction 0x43 of function 0x71
1788 static BOOL
INT21_FileAttributes( CONTEXT
*context
,
1792 WCHAR fileW
[MAX_PATH
];
1793 char *fileA
= CTX_SEG_OFF_TO_LIN(context
,
1803 switch (subfunction
)
1805 case 0x00: /* GET FILE ATTRIBUTES */
1806 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA
);
1807 len
= MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1809 /* Winbench 96 Disk Test fails if we don't complain
1810 * about a filename that ends in \
1812 if (!len
|| (fileW
[len
-1] == '/') || (fileW
[len
-1] == '\\'))
1815 result
= GetFileAttributesW( fileW
);
1816 if (result
== INVALID_FILE_ATTRIBUTES
)
1820 SET_CX( context
, (WORD
)result
);
1822 SET_AX( context
, (WORD
)result
); /* DR DOS */
1826 case 0x01: /* SET FILE ATTRIBUTES */
1827 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
1828 CX_reg(context
), fileA
);
1829 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1831 if (!SetFileAttributesW( fileW
, CX_reg(context
) ))
1835 case 0x02: /* GET COMPRESSED FILE SIZE */
1836 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA
);
1837 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1839 result
= GetCompressedFileSizeW( fileW
, NULL
);
1840 if (result
== INVALID_FILE_SIZE
)
1844 SET_AX( context
, LOWORD(result
) );
1845 SET_DX( context
, HIWORD(result
) );
1849 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
1851 INT_BARF( context
, 0x21 );
1854 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA
);
1855 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1857 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
1858 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1859 NULL
, OPEN_EXISTING
, 0, 0 );
1860 if (handle
== INVALID_HANDLE_VALUE
)
1863 DosDateTimeToFileTime( DI_reg(context
),
1866 status
= SetFileTime( handle
, NULL
, NULL
, &filetime
);
1868 CloseHandle( handle
);
1873 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
1875 INT_BARF( context
, 0x21 );
1878 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA
);
1879 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1881 handle
= CreateFileW( fileW
, GENERIC_READ
,
1882 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1883 NULL
, OPEN_EXISTING
, 0, 0 );
1884 if (handle
== INVALID_HANDLE_VALUE
)
1887 status
= GetFileTime( handle
, NULL
, NULL
, &filetime
);
1890 FileTimeToDosDateTime( &filetime
, &date
, &time
);
1891 SET_DI( context
, date
);
1892 SET_CX( context
, time
);
1895 CloseHandle( handle
);
1900 case 0x05: /* SET FILE LAST ACCESS DATE */
1902 INT_BARF( context
, 0x21 );
1905 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA
);
1906 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1908 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
1909 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1910 NULL
, OPEN_EXISTING
, 0, 0 );
1911 if (handle
== INVALID_HANDLE_VALUE
)
1914 DosDateTimeToFileTime( DI_reg(context
),
1917 status
= SetFileTime( handle
, NULL
, &filetime
, NULL
);
1919 CloseHandle( handle
);
1924 case 0x06: /* GET FILE LAST ACCESS DATE */
1926 INT_BARF( context
, 0x21 );
1929 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA
);
1930 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1932 handle
= CreateFileW( fileW
, GENERIC_READ
,
1933 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1934 NULL
, OPEN_EXISTING
, 0, 0 );
1935 if (handle
== INVALID_HANDLE_VALUE
)
1938 status
= GetFileTime( handle
, NULL
, &filetime
, NULL
);
1941 FileTimeToDosDateTime( &filetime
, &date
, NULL
);
1942 SET_DI( context
, date
);
1945 CloseHandle( handle
);
1950 case 0x07: /* SET FILE CREATION DATE AND TIME */
1952 INT_BARF( context
, 0x21 );
1955 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA
);
1956 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1958 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
1959 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1960 NULL
, OPEN_EXISTING
, 0, 0 );
1961 if (handle
== INVALID_HANDLE_VALUE
)
1965 * FIXME: SI has number of 10-millisecond units past time in CX.
1967 DosDateTimeToFileTime( DI_reg(context
),
1970 status
= SetFileTime( handle
, &filetime
, NULL
, NULL
);
1972 CloseHandle( handle
);
1977 case 0x08: /* GET FILE CREATION DATE AND TIME */
1979 INT_BARF( context
, 0x21 );
1982 TRACE( "GET FILE CREATION DATE AND TIME, file %s\n", fileA
);
1983 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1985 handle
= CreateFileW( fileW
, GENERIC_READ
,
1986 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1987 NULL
, OPEN_EXISTING
, 0, 0 );
1988 if (handle
== INVALID_HANDLE_VALUE
)
1991 status
= GetFileTime( handle
, &filetime
, NULL
, NULL
);
1994 FileTimeToDosDateTime( &filetime
, &date
, &time
);
1995 SET_DI( context
, date
);
1996 SET_CX( context
, time
);
1998 * FIXME: SI has number of 10-millisecond units past
2001 SET_SI( context
, 0 );
2004 CloseHandle(handle
);
2009 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2010 if (islong
|| context
->Ebp
!= 0x5053)
2011 INT_BARF( context
, 0x21 );
2014 switch(CL_reg(context
))
2017 if (!INT21_CreateDirectory( context
))
2022 if (!INT21_RenameFile( context
))
2027 INT_BARF( context
, 0x21 );
2033 INT_BARF( context
, 0x21 );
2040 /***********************************************************************
2041 * INT21_FileDateTime
2043 * Handler for function 0x57.
2045 static BOOL
INT21_FileDateTime( CONTEXT
*context
)
2047 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2051 switch (AL_reg(context
)) {
2052 case 0x00: /* Get last-written stamp */
2053 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2056 if (!GetFileTime( handle
, NULL
, NULL
, &filetime
))
2058 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2059 SET_DX( context
, date
);
2060 SET_CX( context
, time
);
2064 case 0x01: /* Set last-written stamp */
2065 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2068 DosDateTimeToFileTime( DX_reg(context
),
2071 if (!SetFileTime( handle
, NULL
, NULL
, &filetime
))
2076 case 0x04: /* Get last access stamp, DOS 7 */
2077 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2080 if (!GetFileTime( handle
, NULL
, &filetime
, NULL
))
2082 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2083 SET_DX( context
, date
);
2084 SET_CX( context
, time
);
2088 case 0x05: /* Set last access stamp, DOS 7 */
2089 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2092 DosDateTimeToFileTime( DX_reg(context
),
2095 if (!SetFileTime( handle
, NULL
, &filetime
, NULL
))
2100 case 0x06: /* Get creation stamp, DOS 7 */
2101 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2104 if (!GetFileTime( handle
, &filetime
, NULL
, NULL
))
2106 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2107 SET_DX( context
, date
);
2108 SET_CX( context
, time
);
2110 * FIXME: SI has number of 10-millisecond units past time in CX.
2112 SET_SI( context
, 0 );
2116 case 0x07: /* Set creation stamp, DOS 7 */
2117 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2121 * FIXME: SI has number of 10-millisecond units past time in CX.
2123 DosDateTimeToFileTime( DX_reg(context
),
2126 if (!SetFileTime( handle
, &filetime
, NULL
, NULL
))
2132 INT_BARF( context
, 0x21 );
2140 /***********************************************************************
2143 * Handler for functions 0x51 and 0x62.
2145 static void INT21_GetPSP( CONTEXT
*context
)
2147 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context
) );
2149 SET_BX( context
, LOWORD(GetCurrentPDB16()) );
2152 static inline void setword( BYTE
*ptr
, WORD w
)
2155 ptr
[1] = (BYTE
)(w
>> 8);
2158 static void CreateBPB(int drive
, BYTE
*data
, BOOL16 limited
)
2159 /* limited == TRUE is used with INT 0x21/0x440d */
2161 /* FIXME: we're forcing some values without checking that those are valid */
2166 setword(&data
[3], 0);
2168 setword(&data
[6], 240);
2169 setword(&data
[8], 64000);
2171 setword(&data
[0x0b], 40);
2172 setword(&data
[0x0d], 56);
2173 setword(&data
[0x0f], 2);
2174 setword(&data
[0x11], 0);
2177 setword(&data
[0x1f], 800);
2179 setword(&data
[0x22], 1);
2186 setword(&data
[3], 0);
2188 setword(&data
[6], 240);
2189 setword(&data
[8], 2880);
2191 setword(&data
[0x0b], 6);
2192 setword(&data
[0x0d], 18);
2193 setword(&data
[0x0f], 2);
2194 setword(&data
[0x11], 0);
2197 setword(&data
[0x1f], 80);
2199 setword(&data
[0x22], 2);
2204 static inline DWORD
INT21_Ioctl_CylHeadSect2Lin(DWORD cyl
, WORD head
, WORD sec
, WORD cyl_cnt
,
2205 WORD head_cnt
, WORD sec_cnt
)
2207 DWORD res
= (cyl
* head_cnt
*sec_cnt
+ head
* sec_cnt
+ sec
);
2211 /***********************************************************************
2214 * Handler for block device IOCTLs.
2216 static void INT21_Ioctl_Block( CONTEXT
*context
)
2219 BYTE drive
= INT21_MapDrive( BL_reg(context
) );
2220 WCHAR drivespec
[] = {'A', ':', '\\', 0};
2223 drivespec
[0] += drive
;
2224 drivetype
= GetDriveTypeW( drivespec
);
2226 RESET_CFLAG(context
);
2227 if (drivetype
== DRIVE_UNKNOWN
|| drivetype
== DRIVE_NO_ROOT_DIR
)
2229 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2230 AL_reg(context
), 'A' + drive
);
2231 SetLastError( ERROR_INVALID_DRIVE
);
2232 SET_AX( context
, ERROR_INVALID_DRIVE
);
2233 SET_CFLAG( context
);
2237 switch (AL_reg(context
))
2239 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2240 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2241 INT_BARF( context
, 0x21 );
2244 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2245 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2248 if (drivetype
== DRIVE_REMOVABLE
)
2249 SET_AX( context
, 0 ); /* removable */
2251 SET_AX( context
, 1 ); /* not removable */
2254 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2255 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2258 if (drivetype
== DRIVE_REMOTE
)
2259 SET_DX( context
, (1<<9) | (1<<12) ); /* remote + no direct IO */
2260 else if (drivetype
== DRIVE_CDROM
)
2261 /* CDROM should be set to remote. If it set the app will
2262 * call int2f to check if it cdrom or remote drive. */
2263 SET_DX( context
, (1<<12) );
2264 else if (drivetype
== DRIVE_FIXED
)
2265 /* This should define if drive support 0x0d, 0x0f and 0x08
2266 * requests. The local fixed drive should do. */
2267 SET_DX( context
, (1<<11) );
2269 SET_DX( context
, 0 ); /* FIXME: use driver attr here */
2272 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2273 /* Get pointer to IOCTL parameter block */
2274 dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2276 switch (CX_reg(context
))
2278 case 0x0841: /* write logical device track */
2279 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2282 WORD head
= *(WORD
*)(dataptr
+1);
2283 WORD cyl
= *(WORD
*)(dataptr
+3);
2284 WORD sect
= *(WORD
*)(dataptr
+5);
2285 WORD nrsect
= *(WORD
*)(dataptr
+7);
2286 BYTE
*data
= CTX_SEG_OFF_TO_LIN(context
, *(WORD
*)(dataptr
+11), *(WORD
*)(dataptr
+9));
2287 WORD cyl_cnt
, head_cnt
, sec_cnt
;
2289 /* FIXME: we're faking some values here */
2295 SET_AX( context
, ERROR_WRITE_FAULT
);
2306 if (!DOSVM_RawWrite(drive
, INT21_Ioctl_CylHeadSect2Lin(cyl
, head
, sect
, cyl_cnt
, head_cnt
, sec_cnt
), nrsect
, data
, FALSE
))
2308 SET_AX( context
, ERROR_WRITE_FAULT
);
2314 case 0x084a: /* lock logical volume */
2315 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2316 BH_reg(context
), DX_reg(context
), 'A' + drive
);
2319 case 0x0860: /* get device parameters */
2320 /* FIXME: we're faking some values here */
2321 /* used by w4wgrp's winfile */
2322 memset(dataptr
, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2324 dataptr
[6] = 0; /* media type */
2327 dataptr
[1] = 0x05; /* fixed disk */
2328 setword(&dataptr
[2], 0x01); /* non removable */
2329 setword(&dataptr
[4], 0x300); /* # of cylinders */
2333 dataptr
[1] = 0x07; /* block dev, floppy */
2334 setword(&dataptr
[2], 0x02); /* removable */
2335 setword(&dataptr
[4], 80); /* # of cylinders */
2337 CreateBPB(drive
, &dataptr
[7], TRUE
);
2338 RESET_CFLAG(context
);
2341 case 0x0861: /* read logical device track */
2342 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2345 WORD head
= *(WORD
*)(dataptr
+1);
2346 WORD cyl
= *(WORD
*)(dataptr
+3);
2347 WORD sect
= *(WORD
*)(dataptr
+5);
2348 WORD nrsect
= *(WORD
*)(dataptr
+7);
2349 BYTE
*data
= CTX_SEG_OFF_TO_LIN(context
, *(WORD
*)(dataptr
+11), *(WORD
*)(dataptr
+9));
2350 WORD cyl_cnt
, head_cnt
, sec_cnt
;
2352 /* FIXME: we're faking some values here */
2366 if (!DOSVM_RawRead(drive
, INT21_Ioctl_CylHeadSect2Lin(cyl
, head
, sect
, cyl_cnt
, head_cnt
, sec_cnt
), nrsect
, data
, FALSE
))
2368 SET_AX( context
, ERROR_READ_FAULT
);
2374 case 0x0866: /* get volume serial number */
2376 WCHAR label
[12],fsname
[9];
2378 TRACE( "GENERIC IOCTL - Get media id - %c:\n",
2381 GetVolumeInformationW(drivespec
, label
, 12, &serial
, NULL
, NULL
, fsname
, 9);
2382 *(WORD
*)dataptr
= 0;
2383 memcpy(dataptr
+2,&serial
,4);
2384 WideCharToMultiByte(CP_OEMCP
, 0, label
, 11, (LPSTR
)dataptr
+ 6, 11, NULL
, NULL
);
2385 WideCharToMultiByte(CP_OEMCP
, 0, fsname
, 8, (LPSTR
)dataptr
+ 17, 8, NULL
, NULL
);
2389 case 0x086a: /* unlock logical volume */
2390 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2394 case 0x086f: /* get drive map information */
2395 memset(dataptr
+1, '\0', dataptr
[0]-1);
2396 dataptr
[1] = dataptr
[0];
2397 dataptr
[2] = 0x07; /* protected mode driver; no eject; no notification */
2398 dataptr
[3] = 0xFF; /* no physical drive */
2402 /* Trial and error implementation */
2403 SET_AX( context
, drivetype
== DRIVE_UNKNOWN
? 0x0f : 0x01 );
2404 SET_CFLAG(context
); /* Seems to be set all the time */
2408 INT_BARF( context
, 0x21 );
2412 case 0x0e: /* GET LOGICAL DRIVE MAP */
2413 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2415 /* FIXME: this is not correct if drive has mappings */
2416 SET_AL( context
, 0 ); /* drive has no mapping */
2419 case 0x0f: /* SET LOGICAL DRIVE MAP */
2420 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2421 INT21_DriveName( BL_reg(context
)));
2422 /* FIXME: as of today, we don't support logical drive mapping... */
2423 SET_AL( context
, 0 );
2426 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2428 INT_BARF( context
, 0x21 );
2433 /***********************************************************************
2436 * Handler for character device IOCTLs.
2438 static void INT21_Ioctl_Char( CONTEXT
*context
)
2440 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2441 BOOL IsConsoleIOHandle
= VerifyConsoleIoHandle( handle
);
2443 switch (AL_reg(context
))
2445 case 0x00: /* GET DEVICE INFORMATION */
2446 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context
) );
2447 if (IsConsoleIOHandle
|| GetFileType(handle
) == FILE_TYPE_CHAR
)
2450 * Returns attribute word in DX:
2451 * Bit 14 - Device driver can process IOCTL requests.
2452 * Bit 13 - Output until busy supported.
2453 * Bit 11 - Driver supports OPEN/CLOSE calls.
2455 * Bit 7 - Set (indicates device).
2456 * Bit 6 - EOF on input.
2457 * Bit 5 - Raw (binary) mode.
2458 * Bit 4 - Device is special (uses int29).
2459 * Bit 3 - Clock device.
2460 * Bit 2 - NUL device.
2461 * Bit 1 - Console output device.
2462 * Bit 0 - Console input device.
2464 SET_DX( context
, IsConsoleIOHandle
? 0x80c3 : 0x80c0 /* FIXME */ );
2469 * Returns attribute word in DX:
2470 * Bit 15 - File is remote.
2471 * Bit 14 - Don't set file date/time on closing.
2472 * Bit 11 - Media not removable.
2473 * Bit 8 - Generate int24 if no disk space on write
2474 * or read past end of file
2475 * Bit 7 - Clear (indicates file).
2476 * Bit 6 - File has not been written.
2477 * Bit 5..0 - Drive number (0=A:,...)
2479 * FIXME: Should check if file is on remote or removable drive.
2480 * FIXME: Should use drive file is located on (and not current).
2482 SET_DX( context
, 0x0140 + INT21_GetCurrentDrive() );
2486 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2487 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context
) );
2489 * Returns attribute word in DX:
2490 * Bit 15 - Set if remote.
2491 * Bit 14 - Set if date/time not set on close.
2493 * FIXME: Should check if file is on remote drive.
2495 SET_DX( context
, 0 );
2498 case 0x01: /* SET DEVICE INFORMATION */
2499 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2500 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2501 case 0x06: /* GET INPUT STATUS */
2502 case 0x07: /* GET OUTPUT STATUS */
2503 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2504 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2506 INT_BARF( context
, 0x21 );
2512 /***********************************************************************
2515 * Handler for function 0x44.
2517 static void INT21_Ioctl( CONTEXT
*context
)
2519 switch (AL_reg(context
))
2525 INT21_Ioctl_Char( context
);
2530 INT21_Ioctl_Block( context
);
2535 INT21_Ioctl_Char( context
);
2540 INT21_Ioctl_Block( context
);
2544 INT21_Ioctl_Char( context
);
2547 case 0x0b: /* SET SHARING RETRY COUNT */
2548 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2549 CX_reg(context
), DX_reg(context
) );
2550 if (!CX_reg(context
))
2552 SET_AX( context
, 1 );
2553 SET_CFLAG( context
);
2557 RESET_CFLAG( context
);
2562 INT21_Ioctl_Char( context
);
2568 INT21_Ioctl_Block( context
);
2572 INT21_Ioctl_Char( context
);
2576 INT21_Ioctl_Block( context
);
2579 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2580 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2581 SET_CFLAG(context
); /* Error / This is not DR DOS. */
2582 SET_AX( context
, 0x0001 ); /* Invalid function */
2585 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2586 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2587 SET_CFLAG(context
); /* Error / This is not DR DOS. */
2588 SET_AX( context
, 0x0001 ); /* Invalid function */
2591 case 0xe0: /* Sun PC-NFS API */
2592 TRACE( "Sun PC-NFS API\n" );
2597 INT_BARF( context
, 0x21 );
2602 /***********************************************************************
2605 * Handler for function 0x73.
2607 static BOOL
INT21_Fat32( CONTEXT
*context
)
2609 switch (AL_reg(context
))
2611 case 0x02: /* FAT32 - GET EXTENDED DPB */
2613 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
2614 WORD
*ptr
= CTX_SEG_OFF_TO_LIN(context
,
2615 context
->SegEs
, context
->Edi
);
2616 INT21_DPB
*target
= (INT21_DPB
*)(ptr
+ 1);
2619 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context
) );
2621 if ( CX_reg(context
) < sizeof(INT21_DPB
) + 2 || *ptr
< sizeof(INT21_DPB
) )
2623 SetLastError( ERROR_BAD_LENGTH
);
2627 if ( !INT21_FillDrivePB( drive
) )
2629 SetLastError( ERROR_INVALID_DRIVE
);
2633 source
= &INT21_GetHeapPointer()->misc_dpb_list
[drive
];
2635 *ptr
= sizeof(INT21_DPB
);
2638 if (LOWORD(context
->Esi
) != 0xF1A6)
2640 target
->driver_header
= 0;
2645 FIXME( "Caller requested driver and next DPB pointers!\n" );
2650 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2652 WCHAR dirW
[MAX_PATH
];
2653 char *dirA
= CTX_SEG_OFF_TO_LIN( context
,
2654 context
->SegDs
, context
->Edx
);
2655 BYTE
*data
= CTX_SEG_OFF_TO_LIN( context
,
2656 context
->SegEs
, context
->Edi
);
2657 DWORD cluster_sectors
;
2659 DWORD free_clusters
;
2660 DWORD total_clusters
;
2662 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA
);
2663 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
2665 if (CX_reg(context
) < 44)
2667 SetLastError( ERROR_BAD_LENGTH
);
2671 if (!GetDiskFreeSpaceW( dirW
, &cluster_sectors
, §or_bytes
,
2672 &free_clusters
, &total_clusters
))
2675 *(WORD
*) (data
+ 0) = 44; /* size of structure */
2676 *(WORD
*) (data
+ 2) = 0; /* version */
2677 *(DWORD
*)(data
+ 4) = cluster_sectors
;
2678 *(DWORD
*)(data
+ 8) = sector_bytes
;
2679 *(DWORD
*)(data
+ 12) = free_clusters
;
2680 *(DWORD
*)(data
+ 16) = total_clusters
;
2683 * Below we have free/total sectors and
2684 * free/total allocation units without adjustment
2685 * for compression. We fake both using cluster information.
2687 *(DWORD
*)(data
+ 20) = free_clusters
* cluster_sectors
;
2688 *(DWORD
*)(data
+ 24) = total_clusters
* cluster_sectors
;
2689 *(DWORD
*)(data
+ 28) = free_clusters
;
2690 *(DWORD
*)(data
+ 32) = total_clusters
;
2693 * Between (data + 36) and (data + 43) there
2694 * are eight reserved bytes.
2700 INT_BARF( context
, 0x21 );
2706 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA
*dataA
,
2707 const WIN32_FIND_DATAW
*dataW
)
2709 dataA
->dwFileAttributes
= dataW
->dwFileAttributes
;
2710 dataA
->ftCreationTime
= dataW
->ftCreationTime
;
2711 dataA
->ftLastAccessTime
= dataW
->ftLastAccessTime
;
2712 dataA
->ftLastWriteTime
= dataW
->ftLastWriteTime
;
2713 dataA
->nFileSizeHigh
= dataW
->nFileSizeHigh
;
2714 dataA
->nFileSizeLow
= dataW
->nFileSizeLow
;
2715 WideCharToMultiByte( CP_OEMCP
, 0, dataW
->cFileName
, -1,
2716 dataA
->cFileName
, sizeof(dataA
->cFileName
), NULL
, NULL
);
2717 WideCharToMultiByte( CP_OEMCP
, 0, dataW
->cAlternateFileName
, -1,
2718 dataA
->cAlternateFileName
, sizeof(dataA
->cAlternateFileName
), NULL
, NULL
);
2721 /***********************************************************************
2722 * INT21_LongFilename
2724 * Handler for function 0x71.
2726 static void INT21_LongFilename( CONTEXT
*context
)
2728 BOOL bSetDOSExtendedError
= FALSE
;
2729 WCHAR pathW
[MAX_PATH
];
2732 switch (AL_reg(context
))
2734 case 0x0d: /* RESET DRIVE */
2735 INT_BARF( context
, 0x21 );
2738 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
2739 if (!INT21_CreateDirectory( context
))
2740 bSetDOSExtendedError
= TRUE
;
2743 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
2744 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2746 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA
);
2747 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2748 if (!RemoveDirectoryW( pathW
)) bSetDOSExtendedError
= TRUE
;
2751 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
2752 if (!INT21_SetCurrentDirectory( context
))
2753 bSetDOSExtendedError
= TRUE
;
2756 case 0x41: /* LONG FILENAME - DELETE FILE */
2757 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2759 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA
);
2760 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2762 if (!DeleteFileW( pathW
)) bSetDOSExtendedError
= TRUE
;
2765 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
2766 if (!INT21_FileAttributes( context
, BL_reg(context
), TRUE
))
2767 bSetDOSExtendedError
= TRUE
;
2770 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
2771 if (!INT21_GetCurrentDirectory( context
, TRUE
))
2772 bSetDOSExtendedError
= TRUE
;
2775 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
2779 WIN32_FIND_DATAW dataW
;
2780 WIN32_FIND_DATAA
* dataA
;
2782 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Edx
);
2783 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA
);
2785 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2786 handle
= FindFirstFileW(pathW
, &dataW
);
2788 dataA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
2789 if (handle
!= INVALID_HANDLE_VALUE
&&
2790 (h16
= GlobalAlloc16(GMEM_MOVEABLE
, sizeof(handle
))))
2792 HANDLE
* ptr
= GlobalLock16( h16
);
2794 GlobalUnlock16( h16
);
2795 SET_AX( context
, h16
);
2796 INT21_ConvertFindDataWtoA(dataA
, &dataW
);
2800 if (handle
!= INVALID_HANDLE_VALUE
) FindClose(handle
);
2801 SET_AX( context
, INVALID_HANDLE_VALUE16
);
2802 bSetDOSExtendedError
= TRUE
;
2807 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
2809 HGLOBAL16 h16
= BX_reg(context
);
2811 WIN32_FIND_DATAW dataW
;
2812 WIN32_FIND_DATAA
* dataA
;
2814 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
2817 dataA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
2819 if (h16
!= INVALID_HANDLE_VALUE16
&& (ptr
= GlobalLock16( h16
)))
2821 if (!FindNextFileW(*ptr
, &dataW
)) bSetDOSExtendedError
= TRUE
;
2822 else INT21_ConvertFindDataWtoA(dataA
, &dataW
);
2823 GlobalUnlock16( h16
);
2827 SetLastError( ERROR_INVALID_HANDLE
);
2828 bSetDOSExtendedError
= TRUE
;
2833 case 0x56: /* LONG FILENAME - RENAME FILE */
2834 if (!INT21_RenameFile(context
))
2835 bSetDOSExtendedError
= TRUE
;
2838 case 0x60: /* LONG FILENAME - CONVERT PATH */
2840 WCHAR res
[MAX_PATH
];
2842 switch (CL_reg(context
))
2844 case 0x00: /* "truename" - Canonicalize path */
2846 * FIXME: This is not 100% equal to 0x01 case,
2847 * if you fix this, fix int21 subfunction 0x60, too.
2850 case 0x01: /* Get short filename or path */
2851 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
2852 if (!GetShortPathNameW(pathW
, res
, 67))
2853 bSetDOSExtendedError
= TRUE
;
2856 SET_AX( context
, 0 );
2857 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
2858 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
2863 case 0x02: /* Get canonical long filename or path */
2864 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
2865 if (!GetFullPathNameW(pathW
, 128, res
, NULL
))
2866 bSetDOSExtendedError
= TRUE
;
2869 SET_AX( context
, 0 );
2870 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
2871 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
2876 FIXME("Unimplemented long file name function:\n");
2877 INT_BARF( context
, 0x21 );
2879 SET_AL( context
, 0 );
2885 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
2886 if (!INT21_CreateFile( context
, context
->Esi
, TRUE
,
2887 BX_reg(context
), DL_reg(context
) ))
2888 bSetDOSExtendedError
= TRUE
;
2891 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
2893 DWORD filename_len
, flags
;
2896 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Edx
);
2898 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA
);
2899 SET_AX( context
, 0 );
2900 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2901 if (!GetVolumeInformationW( pathW
, NULL
, 0, NULL
, &filename_len
,
2904 INT_BARF( context
, 0x21 );
2908 SET_BX( context
, flags
| 0x4000 ); /* support for LFN functions */
2909 SET_CX( context
, filename_len
);
2910 SET_DX( context
, MAX_PATH
); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
2911 WideCharToMultiByte(CP_OEMCP
, 0, dstW
, -1,
2912 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
2917 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
2919 HGLOBAL16 h16
= BX_reg(context
);
2922 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
2924 if (h16
!= INVALID_HANDLE_VALUE16
&& (ptr
= GlobalLock16( h16
)))
2926 if (!FindClose( *ptr
)) bSetDOSExtendedError
= TRUE
;
2927 GlobalUnlock16( h16
);
2928 GlobalFree16( h16
);
2932 SetLastError( ERROR_INVALID_HANDLE
);
2933 bSetDOSExtendedError
= TRUE
;
2938 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
2940 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2941 BY_HANDLE_FILE_INFORMATION
*info
=
2942 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2944 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
2946 if (!GetFileInformationByHandle(handle
, info
))
2947 bSetDOSExtendedError
= TRUE
;
2951 case 0xa7: /* LONG FILENAME - CONVERT TIME */
2952 switch (BL_reg(context
))
2954 case 0x00: /* FILE TIME TO DOS TIME */
2957 FILETIME
*filetime
= CTX_SEG_OFF_TO_LIN(context
,
2961 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
2963 FileTimeToDosDateTime( filetime
, &date
, &time
);
2965 SET_DX( context
, date
);
2966 SET_CX( context
, time
);
2969 * FIXME: BH has number of 10-millisecond units
2972 SET_BH( context
, 0 );
2976 case 0x01: /* DOS TIME TO FILE TIME */
2978 FILETIME
*filetime
= CTX_SEG_OFF_TO_LIN(context
,
2982 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
2985 * FIXME: BH has number of 10-millisecond units
2988 DosDateTimeToFileTime( DX_reg(context
), CX_reg(context
),
2994 INT_BARF( context
, 0x21 );
2999 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
3000 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3001 case 0xaa: /* LONG FILENAME - SUBST */
3003 FIXME("Unimplemented long file name function:\n");
3004 INT_BARF( context
, 0x21 );
3006 SET_AL( context
, 0 );
3010 if (bSetDOSExtendedError
)
3012 SET_AX( context
, GetLastError() );
3013 SET_CFLAG( context
);
3018 /***********************************************************************
3023 * - subfunction 0x56 of function 0x71
3024 * - subfunction 0xff of function 0x43 (CL == 0x56)
3026 static BOOL
INT21_RenameFile( CONTEXT
*context
)
3028 WCHAR fromW
[MAX_PATH
];
3029 WCHAR toW
[MAX_PATH
];
3030 char *fromA
= CTX_SEG_OFF_TO_LIN(context
,
3031 context
->SegDs
,context
->Edx
);
3032 char *toA
= CTX_SEG_OFF_TO_LIN(context
,
3033 context
->SegEs
,context
->Edi
);
3035 TRACE( "RENAME FILE %s to %s\n", fromA
, toA
);
3036 MultiByteToWideChar(CP_OEMCP
, 0, fromA
, -1, fromW
, MAX_PATH
);
3037 MultiByteToWideChar(CP_OEMCP
, 0, toA
, -1, toW
, MAX_PATH
);
3039 return MoveFileW( fromW
, toW
);
3043 /***********************************************************************
3049 static BOOL
INT21_NetworkFunc (CONTEXT
*context
)
3051 switch (AL_reg(context
))
3053 case 0x00: /* Get machine name. */
3055 WCHAR dstW
[MAX_COMPUTERNAME_LENGTH
+ 1];
3056 DWORD s
= ARRAY_SIZE(dstW
);
3059 char *dst
= CTX_SEG_OFF_TO_LIN (context
,context
->SegDs
,context
->Edx
);
3060 TRACE("getting machine name to %p\n", dst
);
3061 if (!GetComputerNameW(dstW
, &s
) ||
3062 !WideCharToMultiByte(CP_OEMCP
, 0, dstW
, -1, dst
, 16, NULL
, NULL
))
3065 SetLastError( ER_NoNetwork
);
3068 for (len
= strlen(dst
); len
< 15; len
++) dst
[len
] = ' ';
3070 SET_CH( context
, 1 ); /* Valid */
3071 SET_CL( context
, 1 ); /* NETbios number??? */
3072 TRACE("returning %s\n", debugstr_an(dst
, 16));
3077 SetLastError( ER_NoNetwork
);
3082 /******************************************************************
3083 * INT21_GetDiskSerialNumber
3086 static int INT21_GetDiskSerialNumber( CONTEXT
*context
)
3088 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3089 WCHAR path
[] = {'A',':','\\',0}, label
[11];
3092 path
[0] += INT21_MapDrive(BL_reg(context
));
3093 if (!GetVolumeInformationW( path
, label
, 11, &serial
, NULL
, NULL
, NULL
, 0))
3095 SetLastError( ERROR_INVALID_DRIVE
);
3099 *(WORD
*)dataptr
= 0;
3100 memcpy(dataptr
+ 2, &serial
, sizeof(DWORD
));
3101 WideCharToMultiByte(CP_OEMCP
, 0, label
, 11, (LPSTR
)dataptr
+ 6, 11, NULL
, NULL
);
3102 memcpy(dataptr
+ 17, "FAT16 ", 8);
3107 /******************************************************************
3108 * INT21_SetDiskSerialNumber
3111 static BOOL
INT21_SetDiskSerialNumber( CONTEXT
*context
)
3114 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3115 int drive
= INT21_MapDrive(BL_reg(context
));
3117 if (!is_valid_drive(drive
))
3119 SetLastError( ERROR_INVALID_DRIVE
);
3123 DRIVE_SetSerialNumber( drive
, *(DWORD
*)(dataptr
+ 2) );
3126 FIXME("Setting drive serial number is no longer supported\n");
3127 SetLastError( ERROR_NOT_SUPPORTED
);
3133 /******************************************************************
3134 * INT21_GetFreeDiskSpace
3137 static BOOL
INT21_GetFreeDiskSpace( CONTEXT
*context
)
3139 DWORD cluster_sectors
, sector_bytes
, free_clusters
, total_clusters
;
3140 WCHAR root
[] = {'A',':','\\',0};
3141 const DWORD max_clusters
= 0x3d83;
3142 const DWORD max_sectors_per_cluster
= 0x7f;
3143 const DWORD max_bytes_per_sector
= 0x200;
3145 root
[0] += INT21_MapDrive(DL_reg(context
));
3146 if (!GetDiskFreeSpaceW( root
, &cluster_sectors
, §or_bytes
,
3147 &free_clusters
, &total_clusters
))
3150 /* Some old win31 apps (Lotus SmartSuite 5.1) crap out if there's too
3151 * much disk space, so Windows XP seems to apply the following limits:
3152 * cluster_sectors <= 0x7f
3153 * sector size <= 0x200
3154 * clusters <= 0x3D83
3155 * This means total reported space is limited to about 1GB.
3158 /* Make sure bytes-per-sector is in [, max] */
3159 while (sector_bytes
> max_bytes_per_sector
) {
3161 free_clusters
<<= 1;
3162 total_clusters
<<= 1;
3164 /* Then make sure sectors-per-cluster is in [max/2, max]. */
3165 while (cluster_sectors
<= max_sectors_per_cluster
/2) {
3166 cluster_sectors
<<= 1;
3167 free_clusters
>>= 1;
3168 total_clusters
>>= 1;
3170 while (cluster_sectors
> max_sectors_per_cluster
) {
3171 cluster_sectors
>>= 1;
3172 free_clusters
<<= 1;
3173 total_clusters
<<= 1;
3176 /* scale up sectors_per_cluster to exactly max_sectors_per_cluster.
3177 * We could skip this, but that would impose an artificially low
3178 * limit on reported disk space.
3179 * To avoid overflow, first apply a preliminary cap on sector count;
3180 * this will not affect the correctness of the final result,
3181 * because if the preliminary cap hits, the final one will, too.
3183 if (total_clusters
> 4 * max_clusters
)
3184 total_clusters
= 4 * max_clusters
;
3185 if (free_clusters
> 4 * max_clusters
)
3186 free_clusters
= 4 * max_clusters
;
3187 if (cluster_sectors
< max_sectors_per_cluster
) {
3188 free_clusters
*= cluster_sectors
;
3189 free_clusters
/= max_sectors_per_cluster
;
3190 total_clusters
*= cluster_sectors
;
3191 total_clusters
/= max_sectors_per_cluster
;
3192 cluster_sectors
= max_sectors_per_cluster
;
3195 /* Finally, apply real cluster count cap. */
3196 if (total_clusters
> max_clusters
)
3197 total_clusters
= max_clusters
;
3198 if (free_clusters
> max_clusters
)
3199 free_clusters
= max_clusters
;
3201 SET_AX( context
, cluster_sectors
);
3202 SET_BX( context
, free_clusters
);
3203 SET_CX( context
, sector_bytes
);
3204 SET_DX( context
, total_clusters
);
3208 /******************************************************************
3209 * INT21_GetDriveAllocInfo
3212 static BOOL
INT21_GetDriveAllocInfo( CONTEXT
*context
, BYTE drive
)
3216 drive
= INT21_MapDrive( drive
);
3217 if (!INT21_FillDrivePB( drive
)) return FALSE
;
3218 dpb
= &(INT21_GetHeapPointer()->misc_dpb_list
[drive
]);
3219 SET_AL( context
, dpb
->cluster_sectors
+ 1 );
3220 SET_CX( context
, dpb
->sector_bytes
);
3221 SET_DX( context
, dpb
->num_clusters1
);
3223 context
->SegDs
= INT21_GetHeapSelector( context
);
3224 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
].media_ID
) );
3228 /***********************************************************************
3229 * INT21_GetExtendedError
3231 static void INT21_GetExtendedError( CONTEXT
*context
)
3233 BYTE
class, action
, locus
;
3234 WORD error
= GetLastError();
3239 class = action
= locus
= 0;
3241 case ERROR_DIR_NOT_EMPTY
:
3246 case ERROR_ACCESS_DENIED
:
3247 class = EC_AccessDenied
;
3251 case ERROR_CANNOT_MAKE
:
3252 class = EC_AccessDenied
;
3256 case ERROR_DISK_FULL
:
3257 case ERROR_HANDLE_DISK_FULL
:
3258 class = EC_MediaError
;
3262 case ERROR_FILE_EXISTS
:
3263 case ERROR_ALREADY_EXISTS
:
3268 case ERROR_FILE_NOT_FOUND
:
3269 case ERROR_PATH_NOT_FOUND
:
3270 case ERROR_INVALID_NAME
:
3271 class = EC_NotFound
;
3275 case ERROR_GEN_FAILURE
:
3276 class = EC_SystemFailure
;
3280 case ERROR_INVALID_DRIVE
:
3281 class = EC_MediaError
;
3285 case ERROR_INVALID_HANDLE
:
3286 class = EC_ProgramError
;
3290 case ERROR_LOCK_VIOLATION
:
3291 class = EC_AccessDenied
;
3295 case ERROR_NO_MORE_FILES
:
3296 class = EC_MediaError
;
3301 class = EC_NotFound
;
3305 case ERROR_NOT_ENOUGH_MEMORY
:
3306 class = EC_OutOfResource
;
3311 class = EC_NotFound
;
3315 case ERROR_SHARING_VIOLATION
:
3316 class = EC_Temporary
;
3320 case ERROR_TOO_MANY_OPEN_FILES
:
3321 class = EC_ProgramError
;
3326 FIXME("Unknown error %d\n", error
);
3327 class = EC_SystemFailure
;
3332 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3333 error
, class, action
, locus
);
3334 SET_AX( context
, error
);
3335 SET_BH( context
, class );
3336 SET_BL( context
, action
);
3337 SET_CH( context
, locus
);
3340 static BOOL
INT21_CreateTempFile( CONTEXT
*context
)
3342 static int counter
= 0;
3343 char *name
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3344 char *p
= name
+ strlen(name
);
3346 /* despite what Ralf Brown says, some programs seem to call without
3347 * ending backslash (DOS accepts that, so we accept it too) */
3348 if ((p
== name
) || (p
[-1] != '\\')) *p
++ = '\\';
3352 sprintf( p
, "wine%04x.%03d", GetCurrentThreadId(), counter
);
3353 counter
= (counter
+ 1) % 1000;
3356 Win32HandleToDosFileHandle(
3357 CreateFileA( name
, GENERIC_READ
| GENERIC_WRITE
,
3358 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
3359 CREATE_NEW
, 0, 0 ) ) );
3360 if (AX_reg(context
) != HFILE_ERROR16
)
3362 TRACE("created %s\n", name
);
3365 if (GetLastError() != ERROR_FILE_EXISTS
) return FALSE
;
3369 /***********************************************************************
3370 * DOSFS_ToDosFCBFormat
3372 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3373 * expanding wild cards and converting to upper-case in the process.
3374 * File name can be terminated by '\0', '\\' or '/'.
3375 * Return FALSE if the name is not a valid DOS name.
3376 * 'buffer' must be at least 12 characters long.
3378 /* Chars we don't want to see in DOS file names */
3379 static BOOL
INT21_ToDosFCBFormat( LPCWSTR name
, LPWSTR buffer
)
3381 static const WCHAR invalid_chars
[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3385 /* Check for "." and ".." */
3390 for(i
= 1; i
< 11; i
++) buffer
[i
] = ' ';
3397 return (!*p
|| (*p
== '/') || (*p
== '\\'));
3400 for (i
= 0; i
< 8; i
++)
3417 if (wcschr( invalid_chars
, *p
)) return FALSE
;
3425 /* Skip all chars after wildcard up to first dot */
3426 while (*p
&& (*p
!= '/') && (*p
!= '\\') && (*p
!= '.')) p
++;
3430 /* Check if name too long */
3431 if (*p
&& (*p
!= '/') && (*p
!= '\\') && (*p
!= '.')) return FALSE
;
3433 if (*p
== '.') p
++; /* Skip dot */
3435 for (i
= 8; i
< 11; i
++)
3445 return FALSE
; /* Second extension not allowed */
3453 if (wcschr( invalid_chars
, *p
)) return FALSE
;
3461 /* at most 3 character of the extension are processed
3462 * is something behind this ?
3464 while (*p
== '*' || *p
== ' ') p
++; /* skip wildcards and spaces */
3465 return (!*p
|| (*p
== '/') || (*p
== '\\'));
3468 static HANDLE INT21_FindHandle
;
3469 static const WCHAR
*INT21_FindPath
; /* will point to current dta->fullPath search */
3471 /******************************************************************
3474 static BOOL
INT21_FindFirst( CONTEXT
*context
)
3478 FINDFILE_DTA
*dta
= (FINDFILE_DTA
*)INT21_GetCurrentDTA(context
);
3479 WCHAR maskW
[12], pathW
[MAX_PATH
];
3480 static const WCHAR wildcardW
[] = {'*','.','*',0};
3482 path
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3483 MultiByteToWideChar(CP_OEMCP
, 0, path
, -1, pathW
, MAX_PATH
);
3485 p
= wcsrchr( pathW
, '\\');
3486 q
= wcsrchr( pathW
, '/');
3490 if (pathW
[0] && pathW
[1] == ':') p
= pathW
+ 2;
3495 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3496 * (doesn't matter as it is set below anyway)
3498 if (!INT21_ToDosFCBFormat( p
, maskW
))
3500 SetLastError( ERROR_FILE_NOT_FOUND
);
3501 SET_AX( context
, ERROR_FILE_NOT_FOUND
);
3505 WideCharToMultiByte(CP_OEMCP
, 0, maskW
, 12, dta
->mask
, sizeof(dta
->mask
), NULL
, NULL
);
3507 dta
->fullPath
= HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW
) + (p
- pathW
)*sizeof(WCHAR
) );
3508 memcpy( dta
->fullPath
, pathW
, (p
- pathW
) * sizeof(WCHAR
) );
3509 memcpy( dta
->fullPath
+ (p
- pathW
), wildcardW
, sizeof(wildcardW
) );
3510 /* we must have a fully qualified file name in dta->fullPath
3511 * (we could have a UNC path, but this would lead to some errors later on)
3513 dta
->drive
= drive_number( dta
->fullPath
[0] );
3515 dta
->search_attr
= CL_reg(context
);
3519 /******************************************************************
3522 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3524 static BOOL
match_short(LPCWSTR shortW
, LPCSTR maskA
)
3526 WCHAR mask
[11], file
[12];
3529 if (!INT21_ToDosFCBFormat( shortW
, file
)) return FALSE
;
3530 MultiByteToWideChar(CP_OEMCP
, 0, maskA
, 11, mask
, 11);
3531 for (i
= 0; i
< 11; i
++)
3532 if (mask
[i
] != '?' && mask
[i
] != file
[i
]) return FALSE
;
3536 static unsigned INT21_FindHelper(LPCWSTR fullPath
, unsigned drive
, unsigned count
,
3537 LPCSTR mask
, unsigned search_attr
,
3538 WIN32_FIND_DATAW
* entry
)
3542 if ((search_attr
& ~(FA_UNUSED
| FA_ARCHIVE
| FA_RDONLY
)) == FA_LABEL
)
3544 WCHAR path
[] = {' ',':','\\',0};
3546 if (count
) return 0;
3547 path
[0] = drive
+ 'A';
3548 if (!GetVolumeInformationW(path
, entry
->cAlternateFileName
, 13, NULL
, NULL
, NULL
, NULL
, 0)) return 0;
3549 if (!entry
->cAlternateFileName
[0]) return 0;
3550 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftCreationTime
);
3551 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftLastAccessTime
);
3552 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftLastWriteTime
);
3553 entry
->dwFileAttributes
= FA_LABEL
;
3554 entry
->nFileSizeHigh
= entry
->nFileSizeLow
= 0;
3555 TRACE("returning %s as label\n", debugstr_w(entry
->cAlternateFileName
));
3559 if (!INT21_FindHandle
|| INT21_FindPath
!= fullPath
|| count
== 0)
3561 if (INT21_FindHandle
) FindClose(INT21_FindHandle
);
3562 INT21_FindHandle
= FindFirstFileW(fullPath
, entry
);
3563 if (INT21_FindHandle
== INVALID_HANDLE_VALUE
)
3565 INT21_FindHandle
= 0;
3568 INT21_FindPath
= fullPath
;
3569 /* we need to resync search */
3574 while (ncalls
-- != 0)
3576 if (!FindNextFileW(INT21_FindHandle
, entry
))
3578 FindClose(INT21_FindHandle
); INT21_FindHandle
= 0;
3582 while (count
< 0xffff)
3585 /* Check the file attributes, and path */
3586 if (!(entry
->dwFileAttributes
& ~search_attr
) &&
3587 match_short(entry
->cAlternateFileName
[0] ? entry
->cAlternateFileName
: entry
->cFileName
,
3592 if (!FindNextFileW(INT21_FindHandle
, entry
))
3594 FindClose(INT21_FindHandle
); INT21_FindHandle
= 0;
3598 WARN("Too many directory entries in %s\n", debugstr_w(fullPath
) );
3602 /******************************************************************
3605 static BOOL
INT21_FindNext( CONTEXT
*context
)
3607 FINDFILE_DTA
*dta
= (FINDFILE_DTA
*)INT21_GetCurrentDTA(context
);
3608 DWORD attr
= dta
->search_attr
| FA_UNUSED
| FA_ARCHIVE
| FA_RDONLY
;
3609 WIN32_FIND_DATAW entry
;
3612 if (!dta
->fullPath
) return FALSE
;
3614 n
= INT21_FindHelper(dta
->fullPath
, dta
->drive
, dta
->count
,
3615 dta
->mask
, attr
, &entry
);
3618 dta
->fileattr
= entry
.dwFileAttributes
;
3619 dta
->filesize
= entry
.nFileSizeLow
;
3620 FileTimeToDosDateTime( &entry
.ftLastWriteTime
, &dta
->filedate
, &dta
->filetime
);
3621 if (entry
.cAlternateFileName
[0])
3622 WideCharToMultiByte(CP_OEMCP
, 0, entry
.cAlternateFileName
, -1,
3623 dta
->filename
, 13, NULL
, NULL
);
3625 WideCharToMultiByte(CP_OEMCP
, 0, entry
.cFileName
, -1, dta
->filename
, 13, NULL
, NULL
);
3627 if (!memchr(dta
->mask
,'?',11))
3629 /* wildcardless search, release resources in case no findnext will
3630 * be issued, and as a workaround in case file creation messes up
3631 * findnext, as sometimes happens with pkunzip
3633 HeapFree( GetProcessHeap(), 0, dta
->fullPath
);
3634 INT21_FindPath
= dta
->fullPath
= NULL
;
3639 HeapFree( GetProcessHeap(), 0, dta
->fullPath
);
3640 INT21_FindPath
= dta
->fullPath
= NULL
;
3644 /* microsoft's programmers should be shot for using CP/M style int21
3645 calls in Windows for Workgroup's winfile.exe */
3647 /******************************************************************
3648 * INT21_FindFirstFCB
3651 static BOOL
INT21_FindFirstFCB( CONTEXT
*context
)
3653 BYTE
*fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3656 WCHAR p
[] = {' ',':',};
3658 if (*fcb
== 0xff) pFCB
= (FINDFILE_FCB
*)(fcb
+ 7);
3659 else pFCB
= (FINDFILE_FCB
*)fcb
;
3660 drive
= INT21_MapDrive( pFCB
->drive
);
3661 if (drive
== MAX_DOS_DRIVES
) return FALSE
;
3664 pFCB
->fullPath
= HeapAlloc(GetProcessHeap(), 0, MAX_PATH
* sizeof(WCHAR
));
3665 if (!pFCB
->fullPath
) return FALSE
;
3666 GetLongPathNameW(p
, pFCB
->fullPath
, MAX_PATH
);
3671 /******************************************************************
3675 static BOOL
INT21_FindNextFCB( CONTEXT
*context
)
3677 BYTE
*fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3679 LPBYTE pResult
= INT21_GetCurrentDTA(context
);
3680 DOS_DIRENTRY_LAYOUT
*ddl
;
3681 WIN32_FIND_DATAW entry
;
3686 if (*fcb
== 0xff) /* extended FCB ? */
3689 pFCB
= (FINDFILE_FCB
*)(fcb
+ 7);
3694 pFCB
= (FINDFILE_FCB
*)fcb
;
3697 if (!pFCB
->fullPath
) return FALSE
;
3698 n
= INT21_FindHelper(pFCB
->fullPath
, INT21_MapDrive( pFCB
->drive
),
3699 pFCB
->count
, pFCB
->filename
, attr
, &entry
);
3702 HeapFree( GetProcessHeap(), 0, pFCB
->fullPath
);
3703 INT21_FindPath
= pFCB
->fullPath
= NULL
;
3710 /* place extended FCB header before pResult if called with extended FCB */
3712 pResult
+= 6; /* leave reserved field behind */
3713 *pResult
++ = entry
.dwFileAttributes
;
3715 *pResult
++ = INT21_MapDrive( pFCB
->drive
); /* DOS_DIRENTRY_LAYOUT after current drive number */
3716 ddl
= (DOS_DIRENTRY_LAYOUT
*)pResult
;
3717 ddl
->fileattr
= entry
.dwFileAttributes
;
3718 ddl
->cluster
= 0; /* what else? */
3719 ddl
->filesize
= entry
.nFileSizeLow
;
3720 memset( ddl
->reserved
, 0, sizeof(ddl
->reserved
) );
3721 FileTimeToDosDateTime( &entry
.ftLastWriteTime
,
3722 &ddl
->filedate
, &ddl
->filetime
);
3724 /* Convert file name to FCB format */
3725 if (entry
.cAlternateFileName
[0])
3726 INT21_ToDosFCBFormat( entry
.cAlternateFileName
, nameW
);
3728 INT21_ToDosFCBFormat( entry
.cFileName
, nameW
);
3729 WideCharToMultiByte(CP_OEMCP
, 0, nameW
, 11, ddl
->filename
, 11, NULL
, NULL
);
3734 /******************************************************************
3735 * INT21_ParseFileNameIntoFCB
3738 static void INT21_ParseFileNameIntoFCB( CONTEXT
*context
)
3741 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
);
3743 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
3747 INT buffer_len
, len
;
3749 SET_AL( context
, 0xff ); /* failed */
3751 TRACE("filename: '%s'\n", filename
);
3754 while (*s
&& (*s
!= ' ') && (*s
!= '\r') && (*s
!= '\n'))
3758 buffer_len
= MultiByteToWideChar(CP_OEMCP
, 0, filename
, len
, NULL
, 0);
3759 buffer
= HeapAlloc( GetProcessHeap(), 0, (buffer_len
+ 1) * sizeof(WCHAR
));
3760 len
= MultiByteToWideChar(CP_OEMCP
, 0, filename
, len
, buffer
, buffer_len
);
3762 INT21_ToDosFCBFormat(buffer
, fcbW
);
3763 HeapFree(GetProcessHeap(), 0, buffer
);
3764 WideCharToMultiByte(CP_OEMCP
, 0, fcbW
, 12, fcb
+ 1, 12, NULL
, NULL
);
3766 TRACE("FCB: '%s'\n", fcb
+ 1);
3768 SET_AL( context
, ((strchr(filename
, '*')) || (strchr(filename
, '$'))) != 0 );
3770 /* point DS:SI to first unparsed character */
3771 SET_SI( context
, context
->Esi
+ (int)s
- (int)filename
);
3774 static BOOL
INT21_Dup2(HFILE16 hFile1
, HFILE16 hFile2
)
3776 HFILE16 res
= HFILE_ERROR16
;
3777 HANDLE handle
, new_handle
;
3778 #define DOS_TABLE_SIZE 256
3779 DWORD map
[DOS_TABLE_SIZE
/ 32];
3782 handle
= DosFileHandleToWin32Handle(hFile1
);
3783 if (handle
== INVALID_HANDLE_VALUE
)
3787 /* now loop to allocate the same one... */
3788 memset(map
, 0, sizeof(map
));
3789 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
3791 if (!DuplicateHandle(GetCurrentProcess(), handle
,
3792 GetCurrentProcess(), &new_handle
,
3793 0, FALSE
, DUPLICATE_SAME_ACCESS
))
3795 res
= HFILE_ERROR16
;
3798 res
= Win32HandleToDosFileHandle(new_handle
);
3799 if (res
== HFILE_ERROR16
|| res
== hFile2
) break;
3800 map
[res
/ 32] |= 1 << (res
% 32);
3802 /* clean up the allocated slots */
3803 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
3805 if (map
[i
/ 32] & (1 << (i
% 32)))
3806 _lclose16((HFILE16
)i
);
3808 return res
== hFile2
;
3812 /***********************************************************************
3813 * DOSVM_Int21Handler
3815 * Interrupt 0x21 handler.
3817 void WINAPI
DOSVM_Int21Handler( CONTEXT
*context
)
3819 BOOL bSetDOSExtendedError
= FALSE
;
3821 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
3822 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
3823 AX_reg(context
), BX_reg(context
),
3824 CX_reg(context
), DX_reg(context
),
3825 SI_reg(context
), DI_reg(context
),
3826 (WORD
)context
->SegDs
, (WORD
)context
->SegEs
,
3830 * Extended error is used by (at least) functions 0x2f to 0x62.
3831 * Function 0x59 returns extended error and error should not
3832 * be cleared before handling the function.
3834 if (AH_reg(context
) >= 0x2f && AH_reg(context
) != 0x59)
3837 RESET_CFLAG(context
); /* Not sure if this is a good idea. */
3839 switch(AH_reg(context
))
3841 case 0x00: /* TERMINATE PROGRAM */
3842 TRACE("TERMINATE PROGRAM\n");
3846 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
3847 TRACE("DIRECT CHARACTER INPUT WITH ECHO - not supported\n");
3850 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
3851 TRACE("Write Character to Standard Output\n");
3854 case 0x03: /* READ CHARACTER FROM STDAUX */
3855 case 0x04: /* WRITE CHARACTER TO STDAUX */
3856 case 0x05: /* WRITE CHARACTER TO PRINTER */
3857 INT_BARF( context
, 0x21 );
3860 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
3861 if (DL_reg(context
) == 0xff)
3863 TRACE("Direct Console Input\n");
3865 /* no character available */
3866 SET_AL( context
, 0 );
3867 SET_ZFLAG( context
);
3871 TRACE("Direct Console Output\n");
3873 * At least DOS versions 2.1-7.0 return character
3874 * that was written in AL register.
3876 SET_AL( context
, DL_reg(context
) );
3880 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
3881 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO - not supported\n");
3882 SET_AL( context
, 0 );
3885 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
3886 TRACE("CHARACTER INPUT WITHOUT ECHO - not supported\n");
3887 SET_AL( context
, 0 );
3890 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
3891 TRACE("WRITE '$'-terminated string from %04X:%04X to stdout\n",
3892 context
->SegDs
, DX_reg(context
) );
3894 LPSTR data
= CTX_SEG_OFF_TO_LIN( context
,
3895 context
->SegDs
, context
->Edx
);
3899 * Do NOT use strchr() to calculate the string length,
3900 * as '\0' is valid string content, too!
3901 * Maybe we should check for non-'$' strings, but DOS doesn't.
3903 while (*p
!= '$') p
++;
3905 WriteFile( DosFileHandleToWin32Handle(1), data
, p
- data
, &w
, NULL
);
3906 SET_AL( context
, '$' ); /* yes, '$' (0x24) gets returned in AL */
3910 case 0x0a: /* BUFFERED INPUT */
3911 TRACE( "BUFFERED INPUT - not supported\n" );
3914 case 0x0b: /* GET STDIN STATUS */
3915 TRACE( "GET STDIN STATUS - not supported\n" );
3916 SET_AL( context
, 0 ); /* no character available */
3919 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
3921 BYTE al
= AL_reg(context
); /* Input function to execute after flush. */
3923 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al
);
3925 /* FIXME: buffers are not flushed */
3928 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
3929 * int21 function identified by AL will be called.
3931 if(al
== 0x01 || al
== 0x06 || al
== 0x07 || al
== 0x08 || al
== 0x0a)
3933 SET_AH( context
, al
);
3934 DOSVM_Int21Handler( context
);
3939 case 0x0d: /* DISK BUFFER FLUSH */
3940 TRACE("DISK BUFFER FLUSH ignored\n");
3943 case 0x0e: /* SELECT DEFAULT DRIVE */
3944 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context
) );
3945 INT21_SetCurrentDrive( DL_reg(context
) );
3946 SET_AL( context
, MAX_DOS_DRIVES
);
3949 case 0x0f: /* OPEN FILE USING FCB */
3950 INT21_OpenFileUsingFCB( context
);
3953 case 0x10: /* CLOSE FILE USING FCB */
3954 INT21_CloseFileUsingFCB( context
);
3957 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
3958 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
3959 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
));
3960 if (!INT21_FindFirstFCB(context
))
3962 SET_AL( context
, 0xff );
3965 /* else fall through */
3967 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
3968 SET_AL( context
, INT21_FindNextFCB(context
) ? 0x00 : 0xff );
3971 case 0x13: /* DELETE FILE USING FCB */
3972 INT_BARF( context
, 0x21 );
3975 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
3976 INT21_SequentialReadFromFCB( context
);
3979 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
3980 INT21_SequentialWriteToFCB( context
);
3983 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
3984 case 0x17: /* RENAME FILE USING FCB */
3985 INT_BARF( context
, 0x21 );
3988 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
3989 SET_AL( context
, 0 );
3992 case 0x19: /* GET CURRENT DEFAULT DRIVE */
3993 SET_AL( context
, INT21_GetCurrentDrive() );
3994 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context
) );
3997 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
3998 TRACE( "SET DISK TRANSFER AREA ADDRESS %04X:%04X\n",
3999 context
->SegDs
, DX_reg(context
) );
4001 TDB
*task
= GlobalLock16( GetCurrentTask() );
4002 task
->dta
= MAKESEGPTR( context
->SegDs
, DX_reg(context
) );
4006 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4007 if (!INT21_GetDriveAllocInfo(context
, 0))
4008 SET_AX( context
, 0xffff );
4011 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4012 if (!INT21_GetDriveAllocInfo(context
, DL_reg(context
)))
4013 SET_AX( context
, 0xffff );
4016 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4017 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4018 SET_AL( context
, 0 );
4021 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4023 BYTE drive
= INT21_MapDrive( 0 );
4024 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4026 if (INT21_FillDrivePB( drive
))
4028 SET_AL( context
, 0x00 ); /* success */
4029 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
] ) );
4030 context
->SegDs
= INT21_GetHeapSelector( context
);
4034 SET_AL( context
, 0xff ); /* invalid or network drive */
4039 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4040 SET_AL( context
, 0 );
4043 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4044 INT21_ReadRandomRecordFromFCB( context
);
4047 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4048 INT21_WriteRandomRecordToFCB( context
);
4051 case 0x23: /* GET FILE SIZE FOR FCB */
4052 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4053 INT_BARF( context
, 0x21 );
4056 case 0x25: /* SET INTERRUPT VECTOR */
4057 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context
));
4059 FARPROC16 ptr
= (FARPROC16
)MAKESEGPTR( context
->SegDs
, DX_reg(context
) );
4060 DOSVM_SetPMHandler16( AL_reg(context
), ptr
);
4064 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4065 INT_BARF( context
, 0x21 );
4068 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4069 INT21_RandomBlockReadFromFCB( context
);
4072 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4073 INT21_RandomBlockWriteToFCB( context
);
4076 case 0x29: /* PARSE FILENAME INTO FCB */
4077 INT21_ParseFileNameIntoFCB(context
);
4080 case 0x2a: /* GET SYSTEM DATE */
4081 TRACE( "GET SYSTEM DATE\n" );
4084 GetLocalTime( &systime
);
4085 SET_CX( context
, systime
.wYear
);
4086 SET_DH( context
, systime
.wMonth
);
4087 SET_DL( context
, systime
.wDay
);
4088 SET_AL( context
, systime
.wDayOfWeek
);
4092 case 0x2b: /* SET SYSTEM DATE */
4093 TRACE( "SET SYSTEM DATE\n" );
4095 WORD year
= CX_reg(context
);
4096 BYTE month
= DH_reg(context
);
4097 BYTE day
= DL_reg(context
);
4099 if (year
>= 1980 && year
<= 2099 &&
4100 month
>= 1 && month
<= 12 &&
4101 day
>= 1 && day
<= 31)
4103 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4105 SET_AL( context
, 0 ); /* Let's pretend we succeeded */
4109 SET_AL( context
, 0xff ); /* invalid date */
4110 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4116 case 0x2c: /* GET SYSTEM TIME */
4117 TRACE( "GET SYSTEM TIME\n" );
4120 GetLocalTime( &systime
);
4121 SET_CH( context
, systime
.wHour
);
4122 SET_CL( context
, systime
.wMinute
);
4123 SET_DH( context
, systime
.wSecond
);
4124 SET_DL( context
, systime
.wMilliseconds
/ 10 );
4128 case 0x2d: /* SET SYSTEM TIME */
4129 if( CH_reg(context
) >= 24 || CL_reg(context
) >= 60 || DH_reg(context
) >= 60 || DL_reg(context
) >= 100 ) {
4130 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4131 CH_reg(context
), CL_reg(context
),
4132 DH_reg(context
), DL_reg(context
) );
4133 SET_AL( context
, 0xFF );
4137 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4138 CH_reg(context
), CL_reg(context
),
4139 DH_reg(context
), DL_reg(context
) );
4140 SET_AL( context
, 0 ); /* Let's pretend we succeeded */
4144 case 0x2e: /* SET VERIFY FLAG */
4145 TRACE("SET VERIFY FLAG ignored\n");
4146 /* we cannot change the behaviour anyway, so just ignore it */
4149 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4150 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4152 TDB
*task
= GlobalLock16( GetCurrentTask() );
4153 context
->SegEs
= SELECTOROF( task
->dta
);
4154 SET_BX( context
, OFFSETOF( task
->dta
) );
4158 case 0x30: /* GET DOS VERSION */
4159 TRACE( "GET DOS VERSION - %s requested\n",
4160 (AL_reg(context
) == 0x00) ? "OEM number" : "version flag" );
4162 if (AL_reg(context
) == 0x00)
4163 SET_BH( context
, 0xff ); /* OEM number => undefined */
4165 SET_BH( context
, 0x08 ); /* version flag => DOS is in ROM */
4167 SET_AL( context
, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4168 SET_AH( context
, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4170 SET_BL( context
, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4171 SET_CX( context
, 0x3456 );
4174 case 0x31: /* TERMINATE AND STAY RESIDENT */
4175 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4178 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4180 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
4181 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context
) );
4183 if (INT21_FillDrivePB( drive
))
4185 SET_AL( context
, 0x00 ); /* success */
4186 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
] ) );
4187 context
->SegDs
= INT21_GetHeapSelector( context
);
4191 SET_AL( context
, 0xff ); /* invalid or network drive */
4196 case 0x33: /* MULTIPLEXED */
4197 switch (AL_reg(context
))
4199 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4200 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4201 SET_DL( context
, brk_flag
);
4204 case 0x01: /* SET EXTENDED BREAK STATE */
4205 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4206 brk_flag
= (DL_reg(context
) > 0) ? 1 : 0;
4209 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4210 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4211 /* ugly coding in order to stay reentrant */
4212 if (DL_reg(context
))
4214 SET_DL( context
, brk_flag
);
4219 SET_DL( context
, brk_flag
);
4224 case 0x05: /* GET BOOT DRIVE */
4225 TRACE("GET BOOT DRIVE\n");
4226 SET_DL( context
, 3 );
4227 /* c: is Wine's bootdrive (a: is 1)*/
4230 case 0x06: /* GET TRUE VERSION NUMBER */
4231 TRACE("GET TRUE VERSION NUMBER\n");
4232 SET_BL( context
, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4233 SET_BH( context
, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4234 SET_DL( context
, 0x00 ); /* revision */
4235 SET_DH( context
, 0x08 ); /* DOS is in ROM */
4239 INT_BARF( context
, 0x21 );
4244 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4245 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4246 context
->SegEs
= INT21_GetHeapSelector( context
);
4247 SET_BX( context
, offsetof(INT21_HEAP
, misc_indos
) );
4250 case 0x35: /* GET INTERRUPT VECTOR */
4251 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context
));
4253 FARPROC16 addr
= DOSVM_GetPMHandler16( AL_reg(context
) );
4254 context
->SegEs
= SELECTOROF(addr
);
4255 SET_BX( context
, OFFSETOF(addr
) );
4259 case 0x36: /* GET FREE DISK SPACE */
4260 TRACE("GET FREE DISK SPACE FOR DRIVE %s (limited to about 1GB)\n",
4261 INT21_DriveName( DL_reg(context
) ));
4262 if (!INT21_GetFreeDiskSpace(context
)) SET_AX( context
, 0xffff );
4265 case 0x37: /* SWITCHAR */
4267 switch (AL_reg(context
))
4269 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4270 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4271 SET_AL( context
, 0x00 ); /* success*/
4272 SET_DL( context
, '/' );
4274 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4275 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4277 SET_AL( context
, 0x00 ); /* success*/
4280 INT_BARF( context
, 0x21 );
4286 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4287 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4288 if (AL_reg(context
))
4290 WORD country
= AL_reg(context
);
4291 if (country
== 0xff)
4292 country
= BX_reg(context
);
4293 if (country
!= INT21_GetSystemCountryCode()) {
4294 FIXME( "Requested info on non-default country %04x\n", country
);
4299 if(AX_reg(context
) != 2 )
4301 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context
,
4304 SET_AX( context
, INT21_GetSystemCountryCode() );
4305 SET_BX( context
, INT21_GetSystemCountryCode() );
4306 RESET_CFLAG(context
);
4310 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4311 if (!INT21_CreateDirectory( context
))
4312 bSetDOSExtendedError
= TRUE
;
4314 RESET_CFLAG(context
);
4317 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4319 WCHAR dirW
[MAX_PATH
];
4320 char *dirA
= CTX_SEG_OFF_TO_LIN(context
,
4321 context
->SegDs
, context
->Edx
);
4323 TRACE( "REMOVE DIRECTORY %s\n", dirA
);
4325 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
4327 if (!RemoveDirectoryW( dirW
))
4328 bSetDOSExtendedError
= TRUE
;
4330 RESET_CFLAG(context
);
4334 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4335 if (!INT21_SetCurrentDirectory( context
))
4336 bSetDOSExtendedError
= TRUE
;
4338 RESET_CFLAG(context
);
4341 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4342 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4343 OF_READWRITE
| OF_SHARE_COMPAT
, 0x12 ))
4344 bSetDOSExtendedError
= TRUE
;
4346 RESET_CFLAG(context
);
4349 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4350 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4351 AL_reg(context
), 0x01 ))
4352 bSetDOSExtendedError
= TRUE
;
4354 RESET_CFLAG(context
);
4357 case 0x3e: /* "CLOSE" - CLOSE FILE */
4358 TRACE( "CLOSE handle %d\n", BX_reg(context
) );
4359 if (_lclose16( BX_reg(context
) ) == HFILE_ERROR16
)
4360 bSetDOSExtendedError
= TRUE
;
4362 RESET_CFLAG(context
);
4365 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4366 TRACE( "READ from %d to %04X:%04X for %d bytes\n",
4373 WORD count
= CX_reg(context
);
4374 BYTE
*buffer
= CTX_SEG_OFF_TO_LIN( context
,
4378 /* Some programs pass a count larger than the allocated buffer */
4379 DWORD maxcount
= GetSelectorLimit16( context
->SegDs
) - DX_reg(context
) + 1;
4380 if (count
> maxcount
) count
= maxcount
;
4383 * FIXME: Reading from console (BX=1) in DOS mode
4384 * does not work as it is supposed to work.
4387 RESET_CFLAG(context
); /* set if error */
4388 if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context
)), buffer
, count
, &result
, NULL
))
4389 SET_AX( context
, (WORD
)result
);
4391 bSetDOSExtendedError
= TRUE
;
4395 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4396 TRACE( "WRITE from %04X:%04X to handle %d for %d byte\n",
4397 context
->SegDs
, DX_reg(context
),
4398 BX_reg(context
), CX_reg(context
) );
4400 char *ptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
4401 HFILE handle
= (HFILE
)DosFileHandleToWin32Handle(BX_reg(context
));
4402 LONG result
= _hwrite( handle
, ptr
, CX_reg(context
) );
4403 if (result
== HFILE_ERROR
)
4404 bSetDOSExtendedError
= TRUE
;
4407 SET_AX( context
, (WORD
)result
);
4408 RESET_CFLAG(context
);
4413 case 0x41: /* "UNLINK" - DELETE FILE */
4415 WCHAR fileW
[MAX_PATH
];
4416 char *fileA
= CTX_SEG_OFF_TO_LIN(context
,
4420 TRACE( "UNLINK %s\n", fileA
);
4421 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
4423 if (!DeleteFileW( fileW
))
4424 bSetDOSExtendedError
= TRUE
;
4426 RESET_CFLAG(context
);
4430 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4431 TRACE( "LSEEK handle %d offset %d from %s\n",
4433 MAKELONG( DX_reg(context
), CX_reg(context
) ),
4434 (AL_reg(context
) == 0) ?
4435 "start of file" : ((AL_reg(context
) == 1) ?
4436 "current file position" : "end of file") );
4438 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
4439 LONG offset
= MAKELONG( DX_reg(context
), CX_reg(context
) );
4440 DWORD status
= SetFilePointer( handle
, offset
,
4441 NULL
, AL_reg(context
) );
4442 if (status
== INVALID_SET_FILE_POINTER
)
4443 bSetDOSExtendedError
= TRUE
;
4446 SET_AX( context
, LOWORD(status
) );
4447 SET_DX( context
, HIWORD(status
) );
4448 RESET_CFLAG(context
);
4453 case 0x43: /* FILE ATTRIBUTES */
4454 if (!INT21_FileAttributes( context
, AL_reg(context
), FALSE
))
4455 bSetDOSExtendedError
= TRUE
;
4457 RESET_CFLAG(context
);
4460 case 0x44: /* IOCTL */
4461 INT21_Ioctl( context
);
4464 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4465 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context
) );
4468 HFILE handle16
= HFILE_ERROR
;
4470 if (DuplicateHandle( GetCurrentProcess(),
4471 DosFileHandleToWin32Handle(BX_reg(context
)),
4472 GetCurrentProcess(),
4474 0, TRUE
, DUPLICATE_SAME_ACCESS
))
4475 handle16
= Win32HandleToDosFileHandle(handle32
);
4477 if (handle16
== HFILE_ERROR
)
4478 bSetDOSExtendedError
= TRUE
;
4481 SET_AX( context
, handle16
);
4482 RESET_CFLAG(context
);
4487 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4488 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4489 BX_reg(context
), CX_reg(context
) );
4490 if (!INT21_Dup2(BX_reg(context
), CX_reg(context
)))
4491 bSetDOSExtendedError
= TRUE
;
4493 RESET_CFLAG(context
);
4496 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4497 if (!INT21_GetCurrentDirectory( context
, FALSE
))
4498 bSetDOSExtendedError
= TRUE
;
4500 RESET_CFLAG(context
);
4503 case 0x48: /* ALLOCATE MEMORY */
4504 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context
) );
4506 DWORD bytes
= (DWORD
)BX_reg(context
) << 4;
4507 DWORD rv
= GlobalDOSAlloc16( bytes
);
4508 WORD selector
= LOWORD( rv
);
4512 SET_AX( context
, selector
);
4513 RESET_CFLAG(context
);
4518 SET_AX( context
, 0x0008 ); /* insufficient memory */
4519 SET_BX( context
, DOSMEM_Available() >> 4 );
4524 case 0x49: /* FREE MEMORY */
4525 TRACE( "FREE MEMORY segment %04X\n", context
->SegEs
);
4527 BOOL ok
= !GlobalDOSFree16( context
->SegEs
);
4529 /* If we don't reset ES_reg, we will fail in the relay code */
4530 if (ok
) context
->SegEs
= 0;
4533 TRACE("FREE MEMORY failed\n");
4535 SET_AX( context
, 0x0009 ); /* memory block address invalid */
4540 case 0x4a: /* RESIZE MEMORY BLOCK */
4541 TRACE( "RESIZE MEMORY segment %04X to %d paragraphs\n",
4542 context
->SegEs
, BX_reg(context
) );
4544 FIXME( "Resize memory block - unsupported under Win16\n" );
4549 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4551 char *program
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
4552 HINSTANCE16 instance
;
4554 TRACE( "EXEC %s\n", program
);
4556 RESET_CFLAG(context
);
4557 instance
= WinExec16( program
, SW_NORMAL
);
4560 SET_CFLAG( context
);
4561 SET_AX( context
, instance
);
4566 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
4567 TRACE( "EXIT with return code %d\n", AL_reg(context
) );
4568 DOSVM_Exit( AL_reg(context
) );
4571 case 0x4d: /* GET RETURN CODE */
4572 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
4573 SET_AX( context
, 0 );
4576 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
4577 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context
),
4578 (LPCSTR
)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
));
4579 if (!INT21_FindFirst(context
)) break;
4582 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
4583 TRACE("FINDNEXT\n");
4584 if (!INT21_FindNext(context
))
4586 SetLastError( ERROR_NO_MORE_FILES
);
4587 SET_AX( context
, ERROR_NO_MORE_FILES
);
4590 else SET_AX( context
, 0 ); /* OK */
4593 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
4594 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
4595 DOSVM_psp
= BX_reg(context
);
4598 case 0x51: /* GET PSP ADDRESS */
4599 INT21_GetPSP( context
);
4602 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
4603 TRACE("Get List of Lists - not supported\n");
4605 SET_BX( context
, 0 );
4608 case 0x54: /* Get Verify Flag */
4609 TRACE("Get Verify Flag - Not Supported\n");
4610 SET_AL( context
, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
4613 case 0x56: /* "RENAME" - RENAME FILE */
4614 if (!INT21_RenameFile( context
))
4615 bSetDOSExtendedError
= TRUE
;
4617 RESET_CFLAG(context
);
4620 case 0x57: /* FILE DATE AND TIME */
4621 if (!INT21_FileDateTime( context
))
4622 bSetDOSExtendedError
= TRUE
;
4624 RESET_CFLAG(context
);
4627 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
4628 switch (AL_reg(context
))
4630 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
4631 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
4632 SET_AX( context
, 0 ); /* low memory first fit */
4635 case 0x01: /* SET ALLOCATION STRATEGY */
4636 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
4640 case 0x02: /* GET UMB LINK STATE */
4641 TRACE( "GET UMB LINK STATE\n" );
4642 SET_AL( context
, 0 ); /* UMBs not part of DOS memory chain */
4645 case 0x03: /* SET UMB LINK STATE */
4646 TRACE( "SET UMB LINK STATE to %d - ignored\n",
4651 INT_BARF( context
, 0x21 );
4655 case 0x59: /* GET EXTENDED ERROR INFO */
4656 INT21_GetExtendedError( context
);
4659 case 0x5a: /* CREATE TEMPORARY FILE */
4660 TRACE("CREATE TEMPORARY FILE\n");
4661 bSetDOSExtendedError
= !INT21_CreateTempFile(context
);
4664 case 0x5b: /* CREATE NEW FILE */
4665 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4666 OF_READWRITE
| OF_SHARE_COMPAT
, 0x10 ))
4667 bSetDOSExtendedError
= TRUE
;
4669 RESET_CFLAG(context
);
4672 case 0x5c: /* "FLOCK" - RECORD LOCKING */
4674 DWORD offset
= MAKELONG(DX_reg(context
), CX_reg(context
));
4675 DWORD length
= MAKELONG(DI_reg(context
), SI_reg(context
));
4676 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
4678 RESET_CFLAG(context
);
4679 switch (AL_reg(context
))
4681 case 0x00: /* LOCK */
4682 TRACE( "lock handle %d offset %d length %d\n",
4683 BX_reg(context
), offset
, length
);
4684 if (!LockFile( handle
, offset
, 0, length
, 0 ))
4685 bSetDOSExtendedError
= TRUE
;
4688 case 0x01: /* UNLOCK */
4689 TRACE( "unlock handle %d offset %d length %d\n",
4690 BX_reg(context
), offset
, length
);
4691 if (!UnlockFile( handle
, offset
, 0, length
, 0 ))
4692 bSetDOSExtendedError
= TRUE
;
4696 INT_BARF( context
, 0x21 );
4701 case 0x5d: /* NETWORK 5D */
4702 FIXME( "Network function 5D not implemented.\n" );
4703 SetLastError( ER_NoNetwork
);
4704 bSetDOSExtendedError
= TRUE
;
4707 case 0x5e: /* NETWORK 5E */
4708 bSetDOSExtendedError
= INT21_NetworkFunc( context
);
4711 case 0x5f: /* NETWORK 5F */
4712 /* FIXME: supporting this would need to 1:
4713 * - implement per drive current directory (as kernel32 doesn't)
4714 * - assign enabled/disabled flag on a per drive basis
4716 /* network software not installed */
4717 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context
));
4718 SetLastError( ER_NoNetwork
);
4719 bSetDOSExtendedError
= TRUE
;
4722 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
4724 WCHAR pathW
[MAX_PATH
], res
[MAX_PATH
];
4725 /* FIXME: likely to be broken */
4727 TRACE("TRUENAME %s\n",
4728 (LPCSTR
)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Esi
));
4729 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
4730 if (!GetFullPathNameW( pathW
, 128, res
, NULL
))
4731 bSetDOSExtendedError
= TRUE
;
4734 SET_AX( context
, 0 );
4735 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
4736 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
4742 case 0x61: /* UNUSED */
4743 SET_AL( context
, 0 );
4746 case 0x62: /* GET PSP ADDRESS */
4747 INT21_GetPSP( context
);
4750 case 0x63: /* MISC. LANGUAGE SUPPORT */
4751 switch (AL_reg(context
)) {
4752 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
4753 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
4754 context
->SegDs
= INT21_GetHeapSelector( context
);
4755 SET_SI( context
, offsetof(INT21_HEAP
, dbcs_table
) );
4756 SET_AL( context
, 0 ); /* success */
4761 case 0x64: /* OS/2 DOS BOX */
4762 INT_BARF( context
, 0x21 );
4766 case 0x65: /* EXTENDED COUNTRY INFORMATION */
4767 INT21_ExtendedCountryInformation( context
);
4770 case 0x66: /* GLOBAL CODE PAGE TABLE */
4771 switch (AL_reg(context
))
4774 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
4775 SET_BX( context
, GetOEMCP() );
4776 SET_DX( context
, GetOEMCP() );
4779 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
4780 BX_reg(context
), DX_reg(context
) );
4785 case 0x67: /* SET HANDLE COUNT */
4786 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context
) );
4787 if (SetHandleCount( BX_reg(context
) ) < BX_reg(context
) )
4788 bSetDOSExtendedError
= TRUE
;
4791 case 0x68: /* "FFLUSH" - COMMIT FILE */
4792 TRACE( "FFLUSH - handle %d\n", BX_reg(context
) );
4793 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context
)) ) &&
4794 GetLastError() != ERROR_ACCESS_DENIED
)
4795 bSetDOSExtendedError
= TRUE
;
4798 case 0x69: /* DISK SERIAL NUMBER */
4799 switch (AL_reg(context
))
4802 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
4803 INT21_DriveName(BL_reg(context
)));
4804 if (!INT21_GetDiskSerialNumber(context
)) bSetDOSExtendedError
= TRUE
;
4805 else SET_AX( context
, 0 );
4809 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
4810 INT21_DriveName(BL_reg(context
)));
4811 if (!INT21_SetDiskSerialNumber(context
)) bSetDOSExtendedError
= TRUE
;
4812 else SET_AX( context
, 1 );
4817 case 0x6a: /* COMMIT FILE */
4818 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context
) );
4819 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context
)) ) &&
4820 GetLastError() != ERROR_ACCESS_DENIED
)
4821 bSetDOSExtendedError
= TRUE
;
4824 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4825 SET_AL( context
, 0 );
4828 case 0x6c: /* EXTENDED OPEN/CREATE */
4829 if (!INT21_CreateFile( context
, context
->Esi
, TRUE
,
4830 BX_reg(context
), DL_reg(context
) ))
4831 bSetDOSExtendedError
= TRUE
;
4834 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
4835 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
4836 SET_CFLAG( context
);
4837 SET_AL( context
, 0 );
4840 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
4841 INT21_LongFilename( context
);
4844 case 0x73: /* MSDOS7 - FAT32 */
4845 RESET_CFLAG( context
);
4846 if (!INT21_Fat32( context
))
4847 bSetDOSExtendedError
= TRUE
;
4850 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
4851 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
4854 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
4855 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
4858 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
4859 /* we don't implement a DOS32 extender */
4860 TRACE( "DOS32 EXTENDER API - ignored\n" );
4864 INT_BARF( context
, 0x21 );
4867 } /* END OF SWITCH */
4869 /* Set general error condition. */
4870 if (bSetDOSExtendedError
)
4872 SET_AX( context
, GetLastError() );
4873 SET_CFLAG( context
);
4876 /* Print error code if carry flag is set. */
4877 if (context
->EFlags
& 0x0001)
4878 TRACE("failed, error %d\n", GetLastError() );
4880 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
4881 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
4882 AX_reg(context
), BX_reg(context
),
4883 CX_reg(context
), DX_reg(context
),
4884 SI_reg(context
), DI_reg(context
),
4885 (WORD
)context
->SegDs
, (WORD
)context
->SegEs
,