2 * DOS interrupt 21h handler
4 * Copyright 1993, 1994 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1997 Andreas Mohr
7 * Copyright 1998 Uwe Bonnes
8 * Copyright 1998, 1999 Ove Kaaven
9 * Copyright 2003 Thomas Mertes
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/port.h"
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
43 #include "wine/winbase16.h"
44 #include "kernel16_private.h"
48 #include "wine/unicode.h"
49 #include "wine/server.h"
50 #include "wine/debug.h"
51 #include "wine/exception.h"
55 * - Most of the file related functions are wrong. NT's kernel32
56 * doesn't maintain a per drive current directory, while DOS does.
57 * We should in fact keep track in here of those per drive
58 * directories, and use this info while dealing with partial paths
59 * (drive defined, but only relative paths). This could even be
60 * created as an array of CDS (there should be an entry for that in
65 * Forward declarations.
67 static BOOL
INT21_RenameFile( CONTEXT
*context
);
69 WINE_DEFAULT_DEBUG_CHANNEL(int21
);
75 * Extended Drive Parameter Block.
76 * This structure is compatible with standard DOS4+ DPB and
79 typedef struct _INT21_DPB
{
80 BYTE drive
; /* 00 drive number (0=A, ...) */
81 BYTE unit
; /* 01 unit number within device driver */
82 WORD sector_bytes
; /* 02 bytes per sector */
83 BYTE cluster_sectors
; /* 04 highest sector number within a cluster */
84 BYTE shift
; /* 05 shift count to convert clusters into sectors */
85 WORD num_reserved
; /* 06 reserved sectors at beginning of drive */
86 BYTE num_FAT
; /* 08 number of FATs */
87 WORD num_root_entries
; /* 09 number of root directory entries */
88 WORD first_data_sector
; /* 0b number of first sector containing user data */
89 WORD num_clusters1
; /* 0d highest cluster number (number of data clusters + 1) */
90 WORD sectors_per_FAT
; /* 0f number of sectors per FAT */
91 WORD first_dir_sector
; /* 11 sector number of first directory sector */
92 SEGPTR driver_header
; /* 13 address of device driver header */
93 BYTE media_ID
; /* 17 media ID byte */
94 BYTE access_flag
; /* 18 0x00 if disk accessed, 0xff if not */
95 SEGPTR next
; /* 19 pointer to next DPB */
96 WORD search_cluster1
; /* 1d cluster at which to start search for free space */
97 WORD free_clusters_lo
; /* 1f number of free clusters on drive or 0xffff if unknown */
98 WORD free_clusters_hi
; /* 21 hiword of clusters_free */
99 WORD mirroring_flags
; /* 23 active FAT/mirroring flags */
100 WORD info_sector
; /* 25 sector number of file system info sector or 0xffff for none */
101 WORD spare_boot_sector
; /* 27 sector number of backup boot sector or 0xffff for none */
102 DWORD first_cluster_sector
; /* 29 sector number of the first cluster */
103 DWORD num_clusters2
; /* 2d maximum cluster number */
104 DWORD fat_clusters
; /* 31 number of clusters occupied by FAT */
105 DWORD root_cluster
; /* 35 cluster number of start of root directory */
106 DWORD search_cluster2
; /* 39 cluster at which to start searching for free space */
111 * Structure for DOS data that can be accessed directly from applications.
112 * Real and protected mode pointers will be returned to this structure so
113 * the structure must be correctly packed.
115 typedef struct _INT21_HEAP
{
116 WORD uppercase_size
; /* Size of the following table in bytes */
117 BYTE uppercase_table
[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
119 WORD lowercase_size
; /* Size of the following table in bytes */
120 BYTE lowercase_table
[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
122 WORD collating_size
; /* Size of the following table in bytes */
123 BYTE collating_table
[256]; /* Values used to sort characters from 0x00 to 0xff. */
125 WORD filename_size
; /* Size of the following filename data in bytes */
126 BYTE filename_reserved1
; /* 0x01 for MS-DOS 3.30-6.00 */
127 BYTE filename_lowest
; /* Lowest permissible character value for filename */
128 BYTE filename_highest
; /* Highest permissible character value for filename */
129 BYTE filename_reserved2
; /* 0x00 for MS-DOS 3.30-6.00 */
130 BYTE filename_exclude_first
; /* First illegal character in permissible range */
131 BYTE filename_exclude_last
; /* Last illegal character in permissible range */
132 BYTE filename_reserved3
; /* 0x02 for MS-DOS 3.30-6.00 */
133 BYTE filename_illegal_size
; /* Number of terminators in the following table */
134 BYTE filename_illegal_table
[16]; /* Characters which terminate a filename */
136 WORD dbcs_size
; /* Number of valid ranges in the following table */
137 BYTE dbcs_table
[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
139 BYTE misc_indos
; /* Interrupt 21 nesting flag */
140 WORD misc_selector
; /* Protected mode selector for INT21_HEAP */
141 INT21_DPB misc_dpb_list
[MAX_DOS_DRIVES
]; /* Drive parameter blocks for all drives */
149 CHAR file_extension
[3];
150 WORD current_block_number
;
151 WORD logical_record_size
;
153 WORD date_of_last_write
;
154 WORD time_of_last_write
;
157 WORD starting_cluster
;
158 WORD sequence_number
;
159 BYTE file_attributes
;
161 BYTE record_within_current_block
;
162 BYTE random_access_record_number
[4];
169 BYTE xfcb_file_attribute
;
173 /* DTA layout for FindFirst/FindNext */
176 BYTE drive
; /* 00 drive letter */
177 char mask
[11]; /* 01 search template */
178 BYTE search_attr
; /* 0c search attributes */
179 WORD count
; /* 0d entry count within directory */
180 WORD cluster
; /* 0f cluster of parent directory */
181 WCHAR
*fullPath
; /* 11 full path (was: reserved) */
182 BYTE fileattr
; /* 15 file attributes */
183 WORD filetime
; /* 16 file time */
184 WORD filedate
; /* 18 file date */
185 DWORD filesize
; /* 1a file size */
186 char filename
[13]; /* 1e file name + extension */
189 /* FCB layout for FindFirstFCB/FindNextFCB */
192 BYTE drive
; /* 00 drive letter */
193 char filename
[11]; /* 01 filename 8+3 format */
194 int count
; /* 0c entry count (was: reserved) */
195 WCHAR
*fullPath
; /* 10 full path (was: reserved) */
198 /* DOS directory entry for FindFirstFCB/FindNextFCB */
201 char filename
[11]; /* 00 filename 8+3 format */
202 BYTE fileattr
; /* 0b file attributes */
203 BYTE reserved
[10]; /* 0c reserved */
204 WORD filetime
; /* 16 file time */
205 WORD filedate
; /* 18 file date */
206 WORD cluster
; /* 1a file first cluster */
207 DWORD filesize
; /* 1c file size */
208 } DOS_DIRENTRY_LAYOUT
;
212 /* dos file attributes */
213 #define FA_NORMAL 0x00 /* Normal file, no attributes */
214 #define FA_RDONLY 0x01 /* Read only attribute */
215 #define FA_HIDDEN 0x02 /* Hidden file */
216 #define FA_SYSTEM 0x04 /* System file */
217 #define FA_LABEL 0x08 /* Volume label */
218 #define FA_DIRECTORY 0x10 /* Directory */
219 #define FA_ARCHIVE 0x20 /* Archive */
220 #define FA_UNUSED 0x40 /* Unused */
223 #define ER_NoNetwork 0x49
226 #define EC_OutOfResource 0x01
227 #define EC_Temporary 0x02
228 #define EC_AccessDenied 0x03
229 #define EC_InternalError 0x04
230 #define EC_HardwareFailure 0x05
231 #define EC_SystemFailure 0x06
232 #define EC_ProgramError 0x07
233 #define EC_NotFound 0x08
234 #define EC_MediaError 0x0b
235 #define EC_Exists 0x0c
236 #define EC_Unknown 0x0d
238 /* Suggested actions */
239 #define SA_Retry 0x01
240 #define SA_DelayedRetry 0x02
241 #define SA_Abort 0x04
242 #define SA_Ignore 0x06
243 #define SA_Ask4Retry 0x07
246 #define EL_Unknown 0x01
248 #define EL_Network 0x03
249 #define EL_Serial 0x04
250 #define EL_Memory 0x05
252 /* BIOS Keyboard Scancodes */
253 #define KEY_LEFT 0x4B
254 #define KEY_RIGHT 0x4D
256 #define KEY_DOWN 0x50
257 #define KEY_IC 0x52 /* insert char */
258 #define KEY_DC 0x53 /* delete char */
259 #define KEY_BACKSPACE 0x0E
260 #define KEY_HOME 0x47
262 #define KEY_NPAGE 0x49
263 #define KEY_PPAGE 0x51
268 /* Many calls translate a drive argument like this:
269 drive number (00h = default, 01h = A:, etc)
271 /******************************************************************
274 * Many calls translate a drive argument like this:
275 * drive number (00h = default, 01h = A:, etc)
277 static const char *INT21_DriveName(int drive
)
281 if (drive
<= 26) return wine_dbg_sprintf("%c:", 'A' + drive
- 1);
282 else return wine_dbg_sprintf( "<Bad drive: %d>", drive
);
287 /***********************************************************************
288 * INT21_GetCurrentDrive
290 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
291 * MAX_DOS_DRIVES on error.
293 static BYTE
INT21_GetCurrentDrive(void)
295 WCHAR current_directory
[MAX_PATH
];
297 if (!GetCurrentDirectoryW( MAX_PATH
, current_directory
) ||
298 current_directory
[1] != ':')
300 TRACE( "Failed to get current drive.\n" );
301 return MAX_DOS_DRIVES
;
304 return toupperW( current_directory
[0] ) - 'A';
308 /***********************************************************************
311 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
312 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
314 static BYTE
INT21_MapDrive( BYTE drive
)
318 WCHAR drivespec
[] = {'A', ':', 0};
321 drivespec
[0] += drive
- 1;
322 drivetype
= GetDriveTypeW( drivespec
);
324 if (drivetype
== DRIVE_UNKNOWN
|| drivetype
== DRIVE_NO_ROOT_DIR
)
325 return MAX_DOS_DRIVES
;
330 return INT21_GetCurrentDrive();
334 /***********************************************************************
335 * INT21_SetCurrentDrive
337 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
339 static void INT21_SetCurrentDrive( BYTE drive
)
341 WCHAR drivespec
[] = {'A', ':', 0};
343 drivespec
[0] += drive
;
345 if (!SetCurrentDirectoryW( drivespec
))
346 TRACE( "Failed to set current drive.\n" );
350 /***********************************************************************
351 * INT21_GetSystemCountryCode
353 * Return DOS country code for default system locale.
355 static WORD
INT21_GetSystemCountryCode( void )
357 return GetSystemDefaultLangID();
361 /***********************************************************************
362 * INT21_FillCountryInformation
364 * Fill 34-byte buffer with country information data using
365 * default system locale.
367 static void INT21_FillCountryInformation( BYTE
*buffer
)
369 /* 00 - WORD: date format
374 *(WORD
*)(buffer
+ 0) = 0; /* FIXME: Get from locale */
376 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
377 buffer
[2] = '$'; /* FIXME: Get from locale */
380 /* 07 - BYTE[2]: ASCIIZ thousands separator */
381 buffer
[7] = 0; /* FIXME: Get from locale */
384 /* 09 - BYTE[2]: ASCIIZ decimal separator */
385 buffer
[9] = '.'; /* FIXME: Get from locale */
388 /* 11 - BYTE[2]: ASCIIZ date separator */
389 buffer
[11] = '/'; /* FIXME: Get from locale */
392 /* 13 - BYTE[2]: ASCIIZ time separator */
393 buffer
[13] = ':'; /* FIXME: Get from locale */
396 /* 15 - BYTE: Currency format
397 * bit 2 = set if currency symbol replaces decimal point
398 * bit 1 = number of spaces between value and currency symbol
399 * bit 0 = 0 if currency symbol precedes value
400 * 1 if currency symbol follows value
402 buffer
[15] = 0; /* FIXME: Get from locale */
404 /* 16 - BYTE: Number of digits after decimal in currency */
405 buffer
[16] = 0; /* FIXME: Get from locale */
407 /* 17 - BYTE: Time format
408 * bit 0 = 0 if 12-hour clock
411 buffer
[17] = 1; /* FIXME: Get from locale */
413 /* 18 - DWORD: Address of case map routine */
414 *(DWORD
*)(buffer
+ 18) = 0; /* FIXME: ptr to case map routine */
416 /* 22 - BYTE[2]: ASCIIZ data-list separator */
417 buffer
[22] = ','; /* FIXME: Get from locale */
420 /* 24 - BYTE[10]: Reserved */
421 memset( buffer
+ 24, 0, 10 );
425 /***********************************************************************
428 * Initialize DOS heap.
430 * Filename Terminator Table of w2k DE NTVDM:
431 * 16 00 01 00 FF 00 00 20-02 0E 2E 22 2F 5C 5B 5D ....... ..."/\[]
432 * 3A 7C 3C 3E 2B 3D 3B 2C-00 :|<>+=;,
434 static void INT21_FillHeap( INT21_HEAP
*heap
)
436 static const char terminators
[] = ".\"/\\[]:|<>+=;,";
442 heap
->uppercase_size
= 128;
443 for (i
= 0; i
< 128; i
++)
444 heap
->uppercase_table
[i
] = toupper( 128 + i
);
449 heap
->lowercase_size
= 256;
450 for (i
= 0; i
< 256; i
++)
451 heap
->lowercase_table
[i
] = tolower( i
);
456 heap
->collating_size
= 256;
457 for (i
= 0; i
< 256; i
++)
458 heap
->collating_table
[i
] = i
;
463 heap
->filename_size
= 8 + strlen(terminators
);
464 heap
->filename_illegal_size
= strlen(terminators
);
465 memcpy( heap
->filename_illegal_table
, terminators
, heap
->filename_illegal_size
);
467 heap
->filename_reserved1
= 0x01;
468 heap
->filename_lowest
= 0x00;
469 heap
->filename_highest
= 0xff;
470 heap
->filename_reserved2
= 0x00;
471 heap
->filename_exclude_first
= 0x00;
472 heap
->filename_exclude_last
= 0x20;
473 heap
->filename_reserved3
= 0x02;
476 * DBCS lead byte table. This table is empty.
479 memset( heap
->dbcs_table
, 0, sizeof(heap
->dbcs_table
) );
482 * Initialize InDos flag.
484 heap
->misc_indos
= 0;
487 * FIXME: Should drive parameter blocks (DPB) be
488 * initialized here and linked to DOS LOL?
493 /***********************************************************************
494 * INT21_GetHeapPointer
496 * Get pointer for DOS heap (INT21_HEAP).
497 * Creates and initializes heap on first call.
499 static INT21_HEAP
*INT21_GetHeapPointer( void )
501 static INT21_HEAP
*heap_pointer
= NULL
;
505 WORD heap_selector
= GlobalAlloc16( GMEM_FIXED
, sizeof(INT21_HEAP
) );
506 heap_pointer
= GlobalLock16( heap_selector
);
507 heap_pointer
->misc_selector
= heap_selector
;
508 INT21_FillHeap( heap_pointer
);
515 /***********************************************************************
516 * INT21_GetHeapSelector
518 * Get segment/selector for DOS heap (INT21_HEAP).
519 * Creates and initializes heap on first call.
521 static WORD
INT21_GetHeapSelector( CONTEXT
*context
)
523 INT21_HEAP
*heap
= INT21_GetHeapPointer();
524 return heap
->misc_selector
;
528 /***********************************************************************
531 * Fill DOS heap drive parameter block for the specified drive.
532 * Return TRUE if drive was valid and there were
533 * no errors while reading drive information.
535 static BOOL
INT21_FillDrivePB( BYTE drive
)
537 WCHAR drivespec
[] = {'A', ':', 0};
538 INT21_HEAP
*heap
= INT21_GetHeapPointer();
541 DWORD cluster_sectors
;
544 DWORD total_clusters
;
546 if (drive
>= MAX_DOS_DRIVES
)
549 dpb
= &heap
->misc_dpb_list
[drive
];
550 drivespec
[0] += drive
;
551 drivetype
= GetDriveTypeW( drivespec
);
554 * FIXME: Does this check work correctly with floppy/cdrom drives?
556 if (drivetype
== DRIVE_NO_ROOT_DIR
|| drivetype
== DRIVE_UNKNOWN
)
560 * FIXME: Does this check work correctly with floppy/cdrom drives?
562 if (!GetDiskFreeSpaceW( drivespec
, &cluster_sectors
, §or_bytes
,
563 &free_clusters
, &total_clusters
))
567 * FIXME: Most of the values listed below are incorrect.
568 * All values should be validated.
573 dpb
->sector_bytes
= sector_bytes
;
574 dpb
->cluster_sectors
= cluster_sectors
- 1;
577 while (cluster_sectors
> 1)
579 cluster_sectors
/= 2;
583 dpb
->num_reserved
= 0;
585 dpb
->num_root_entries
= 2;
586 dpb
->first_data_sector
= 2;
587 dpb
->num_clusters1
= total_clusters
;
588 dpb
->sectors_per_FAT
= 1;
589 dpb
->first_dir_sector
= 1;
590 dpb
->driver_header
= 0;
591 dpb
->media_ID
= (drivetype
== DRIVE_FIXED
) ? 0xF8 : 0xF0;
592 dpb
->access_flag
= 0;
594 dpb
->search_cluster1
= 0;
595 dpb
->free_clusters_lo
= LOWORD(free_clusters
);
596 dpb
->free_clusters_hi
= HIWORD(free_clusters
);
597 dpb
->mirroring_flags
= 0;
598 dpb
->info_sector
= 0xffff;
599 dpb
->spare_boot_sector
= 0xffff;
600 dpb
->first_cluster_sector
= 0;
601 dpb
->num_clusters2
= total_clusters
;
602 dpb
->fat_clusters
= 32;
603 dpb
->root_cluster
= 0;
604 dpb
->search_cluster2
= 0;
610 /***********************************************************************
611 * INT21_GetCurrentDirectory
615 * - subfunction 0x47 of function 0x71
617 static BOOL
INT21_GetCurrentDirectory( CONTEXT
*context
, BOOL islong
)
619 char *buffer
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
);
620 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
621 WCHAR pathW
[MAX_PATH
];
622 char pathA
[MAX_PATH
];
625 TRACE( "drive %d\n", DL_reg(context
) );
627 if (drive
== MAX_DOS_DRIVES
)
629 SetLastError(ERROR_INVALID_DRIVE
);
634 * Grab current directory.
637 if (!GetCurrentDirectoryW( MAX_PATH
, pathW
)) return FALSE
;
639 if (toupperW(pathW
[0]) - 'A' != drive
|| pathW
[1] != ':')
641 /* cwd is not on the requested drive, get the environment string instead */
646 env_var
[1] = 'A' + drive
;
649 if (!GetEnvironmentVariableW( env_var
, pathW
, MAX_PATH
))
651 /* return empty path */
658 * Convert into short format.
663 DWORD result
= GetShortPathNameW( pathW
, pathW
, MAX_PATH
);
666 if (result
> MAX_PATH
)
668 WARN( "Short path too long!\n" );
669 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
675 * The returned pathname does not include
676 * the drive letter, colon or leading backslash.
682 * FIXME: We should probably just strip host part from name...
684 FIXME( "UNC names are not supported.\n" );
685 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
688 else if (!ptr
[0] || ptr
[1] != ':' || ptr
[2] != '\\')
690 WARN( "Path is neither UNC nor DOS path: %s\n",
691 wine_dbgstr_w(ptr
) );
692 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
697 /* Remove drive letter, colon and leading backslash. */
702 * Convert into OEM string.
705 if (!WideCharToMultiByte(CP_OEMCP
, 0, ptr
, -1, pathA
,
706 MAX_PATH
, NULL
, NULL
))
708 WARN( "Cannot convert path!\n" );
709 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
719 /* Undocumented success code. */
720 SET_AX( context
, 0x0100 );
722 /* Truncate buffer to 64 bytes. */
726 TRACE( "%c:=%s\n", 'A' + drive
, pathA
);
728 strcpy( buffer
, pathA
);
733 /***********************************************************************
734 * INT21_SetCurrentDirectory
738 * - subfunction 0x3b of function 0x71
740 static BOOL
INT21_SetCurrentDirectory( CONTEXT
*context
)
742 WCHAR dirW
[MAX_PATH
];
745 char *dirA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
746 BYTE drive
= INT21_GetCurrentDrive();
749 TRACE( "SET CURRENT DIRECTORY %s\n", dirA
);
751 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
752 if (!GetFullPathNameW( dirW
, MAX_PATH
, dirW
, NULL
)) return FALSE
;
754 attr
= GetFileAttributesW( dirW
);
755 if (attr
== INVALID_FILE_ATTRIBUTES
|| !(attr
& FILE_ATTRIBUTE_DIRECTORY
))
757 SetLastError( ERROR_PATH_NOT_FOUND
);
762 env_var
[1] = dirW
[0];
765 result
= SetEnvironmentVariableW( env_var
, dirW
);
767 /* only set current directory if on the current drive */
768 if (result
&& (toupperW(dirW
[0]) - 'A' == drive
)) result
= SetCurrentDirectoryW( dirW
);
773 /***********************************************************************
781 * - subfunction 0x6c of function 0x71
783 static BOOL
INT21_CreateFile( CONTEXT
*context
,
790 char *pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, pathSegOff
);
791 WCHAR pathW
[MAX_PATH
];
798 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
799 "create flags=%04x, file=%s.\n",
800 AH_reg(context
), dosAction
, dosAccessShare
, CX_reg(context
), pathA
);
803 * Application tried to create/open a file whose name
804 * ends with a backslash. This is not allowed.
806 * FIXME: This needs to be validated, especially the return value.
808 if (pathA
[strlen(pathA
) - 1] == '/')
810 SetLastError( ERROR_FILE_NOT_FOUND
);
815 * Convert DOS action flags into Win32 creation disposition parameter.
820 winMode
= OPEN_EXISTING
;
823 winMode
= TRUNCATE_EXISTING
;
826 winMode
= CREATE_NEW
;
829 winMode
= OPEN_ALWAYS
;
832 winMode
= CREATE_ALWAYS
;
835 SetLastError( ERROR_INVALID_PARAMETER
);
840 * Convert DOS access/share flags into Win32 desired access parameter.
842 switch(dosAccessShare
& 0x07)
845 winAccess
= GENERIC_READ
;
848 winAccess
= GENERIC_WRITE
;
851 winAccess
= GENERIC_READ
| GENERIC_WRITE
;
855 * Read-only, do not modify file's last-access time (DOS7).
857 * FIXME: How to prevent modification of last-access time?
859 winAccess
= GENERIC_READ
;
866 * Convert DOS access/share flags into Win32 share mode parameter.
868 switch(dosAccessShare
& 0x70)
870 case OF_SHARE_EXCLUSIVE
:
873 case OF_SHARE_DENY_WRITE
:
874 winSharing
= FILE_SHARE_READ
;
876 case OF_SHARE_DENY_READ
:
877 winSharing
= FILE_SHARE_WRITE
;
879 case OF_SHARE_DENY_NONE
:
880 case OF_SHARE_COMPAT
:
882 winSharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
886 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
887 * What to do with this bit?
888 * FIXME: Bits in the high byte of dosAccessShare are not supported.
889 * See both function 0x6c and subfunction 0x6c of function 0x71 for
890 * definition of these bits.
894 * Convert DOS create attributes into Win32 flags and attributes parameter.
896 if (winMode
== OPEN_EXISTING
|| winMode
== TRUNCATE_EXISTING
)
902 WORD dosAttributes
= CX_reg(context
);
904 if (dosAttributes
& FA_LABEL
)
907 * Application tried to create volume label entry.
908 * This is difficult to support so we do not allow it.
910 * FIXME: If volume does not already have a label,
911 * this function is supposed to succeed.
913 SetLastError( ERROR_ACCESS_DENIED
);
917 winAttributes
= dosAttributes
&
918 (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
|
919 FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_ARCHIVE
);
925 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
927 winHandle
= CreateFileW( pathW
, winAccess
, winSharing
, NULL
, winMode
, winAttributes
, 0 );
928 /* DOS allows opening files on a CDROM R/W */
929 if( winHandle
== INVALID_HANDLE_VALUE
&&
930 (GetLastError() == ERROR_WRITE_PROTECT
||
931 GetLastError() == ERROR_ACCESS_DENIED
)) {
932 winHandle
= CreateFileW( pathW
, winAccess
& ~GENERIC_WRITE
,
933 winSharing
, NULL
, winMode
, winAttributes
, 0 );
936 if (winHandle
== INVALID_HANDLE_VALUE
)
940 * Determine DOS file status.
951 case TRUNCATE_EXISTING
:
958 dosStatus
= (GetLastError() == ERROR_ALREADY_EXISTS
) ? 1 : 2;
961 dosStatus
= (GetLastError() == ERROR_ALREADY_EXISTS
) ? 3 : 2;
968 * Return DOS file handle and DOS status.
970 SET_AX( context
, Win32HandleToDosFileHandle(winHandle
) );
973 SET_CX( context
, dosStatus
);
975 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
976 AX_reg(context
), dosStatus
);
982 /***********************************************************************
983 * INT21_GetCurrentDTA
985 static BYTE
*INT21_GetCurrentDTA( CONTEXT
*context
)
987 TDB
*pTask
= GlobalLock16(GetCurrentTask());
989 /* FIXME: This assumes DTA was set correctly! */
990 return CTX_SEG_OFF_TO_LIN( context
, SELECTOROF(pTask
->dta
),
991 OFFSETOF(pTask
->dta
) );
995 /***********************************************************************
996 * INT21_OpenFileUsingFCB
998 * Handler for function 0x0f.
1001 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
1008 * Opens a FCB file for read/write in compatibility mode. Upon calling
1009 * the FCB must have the drive_number, file_name, and file_extension
1010 * fields filled and all other bytes cleared.
1012 static void INT21_OpenFileUsingFCB( CONTEXT
*context
)
1020 BY_HANDLE_FILE_INFORMATION info
;
1023 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1024 if (fcb
->drive_number
== 0xff) {
1025 xfcb
= (struct XFCB
*) fcb
;
1026 fcb
= (struct FCB
*) xfcb
->fcb
;
1030 file_path
[0] = 'A' + INT21_MapDrive( fcb
->drive_number
);
1032 if (AL_result
== 0) {
1034 pos
= &file_path
[2];
1035 memcpy(pos
, fcb
->file_name
, 8);
1038 pos
= strchr(pos
, ' ');
1041 memcpy(pos
, fcb
->file_extension
, 3);
1044 pos
= strchr(pos
, ' ');
1047 handle
= (HANDLE
) _lopen(file_path
, OF_READWRITE
);
1048 if (handle
== INVALID_HANDLE_VALUE
) {
1049 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path
);
1050 AL_result
= 0xff; /* failed */
1052 hfile16
= Win32HandleToDosFileHandle(handle
);
1053 if (hfile16
== HFILE_ERROR16
) {
1054 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle
);
1055 CloseHandle(handle
);
1056 AL_result
= 0xff; /* failed */
1057 } else if (hfile16
> 255) {
1058 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16
, file_path
);
1060 AL_result
= 0xff; /* failed */
1062 if (!GetFileInformationByHandle(handle
, &info
)) {
1063 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1064 hfile16
, handle
, file_path
);
1066 AL_result
= 0xff; /* failed */
1068 fcb
->drive_number
= file_path
[0] - 'A' + 1;
1069 fcb
->current_block_number
= 0;
1070 fcb
->logical_record_size
= 128;
1071 fcb
->file_size
= info
.nFileSizeLow
;
1072 FileTimeToDosDateTime(&info
.ftLastWriteTime
,
1073 &fcb
->date_of_last_write
, &fcb
->time_of_last_write
);
1074 fcb
->file_number
= hfile16
;
1075 fcb
->attributes
= 0xc2;
1076 fcb
->starting_cluster
= 0; /* don't know correct init value */
1077 fcb
->sequence_number
= 0; /* don't know correct init value */
1078 fcb
->file_attributes
= info
.dwFileAttributes
;
1079 /* The following fields are not initialized */
1080 /* by the native function: */
1082 /* record_within_current_block */
1083 /* random_access_record_number */
1085 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1086 file_path
, hfile16
, handle
);
1087 AL_result
= 0x00; /* successful */
1092 SET_AL(context
, AL_result
);
1096 /***********************************************************************
1097 * INT21_CloseFileUsingFCB
1099 * Handler for function 0x10.
1102 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1109 * Closes a FCB file.
1111 static void INT21_CloseFileUsingFCB( CONTEXT
*context
)
1117 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1118 if (fcb
->drive_number
== 0xff) {
1119 xfcb
= (struct XFCB
*) fcb
;
1120 fcb
= (struct FCB
*) xfcb
->fcb
;
1123 if (_lclose16((HFILE16
) fcb
->file_number
) != 0) {
1124 TRACE("_lclose16(%d) failed\n", fcb
->file_number
);
1125 AL_result
= 0xff; /* failed */
1127 TRACE("successful closed file %d\n", fcb
->file_number
);
1128 AL_result
= 0x00; /* successful */
1130 SET_AL(context
, AL_result
);
1134 /***********************************************************************
1135 * INT21_SequentialReadFromFCB
1137 * Handler for function 0x14.
1140 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1144 * 1: end of file, no data read
1145 * 2: segment wrap in DTA, no data read (not returned now)
1146 * 3: end of file, partial record read
1149 * Reads a record with the size FCB->logical_record_size from the FCB
1150 * to the disk transfer area. The position of the record is specified
1151 * with FCB->current_block_number and FCB->record_within_current_block.
1152 * Then FCB->current_block_number and FCB->record_within_current_block
1153 * are updated to point to the next record. If a partial record is
1154 * read, it is filled with zeros up to the FCB->logical_record_size.
1156 static void INT21_SequentialReadFromFCB( CONTEXT
*context
)
1161 DWORD record_number
;
1163 BYTE
*disk_transfer_area
;
1167 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1168 if (fcb
->drive_number
== 0xff) {
1169 xfcb
= (struct XFCB
*) fcb
;
1170 fcb
= (struct FCB
*) xfcb
->fcb
;
1173 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1174 if (handle
== INVALID_HANDLE_VALUE
) {
1175 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1177 AL_result
= 0x01; /* end of file, no data read */
1179 record_number
= 128 * fcb
->current_block_number
+ fcb
->record_within_current_block
;
1180 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1181 if (position
!= record_number
* fcb
->logical_record_size
) {
1182 TRACE("seek(%d, %d, 0) failed with %u\n",
1183 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1184 AL_result
= 0x01; /* end of file, no data read */
1186 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1187 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, fcb
->logical_record_size
);
1188 if (bytes_read
!= fcb
->logical_record_size
) {
1189 TRACE("_lread(%d, %p, %d) failed with %d\n",
1190 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_read
);
1191 if (bytes_read
== 0) {
1192 AL_result
= 0x01; /* end of file, no data read */
1194 memset(&disk_transfer_area
[bytes_read
], 0, fcb
->logical_record_size
- bytes_read
);
1195 AL_result
= 0x03; /* end of file, partial record read */
1198 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1199 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1200 AL_result
= 0x00; /* successful */
1204 if (AL_result
== 0x00 || AL_result
== 0x03) {
1205 if (fcb
->record_within_current_block
== 127) {
1206 fcb
->record_within_current_block
= 0;
1207 fcb
->current_block_number
++;
1209 fcb
->record_within_current_block
++;
1212 SET_AL(context
, AL_result
);
1216 /***********************************************************************
1217 * INT21_SequentialWriteToFCB
1219 * Handler for function 0x15.
1222 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1227 * 2: segment wrap in DTA (not returned now)
1230 * Writes a record with the size FCB->logical_record_size from the disk
1231 * transfer area to the FCB. The position of the record is specified
1232 * with FCB->current_block_number and FCB->record_within_current_block.
1233 * Then FCB->current_block_number and FCB->record_within_current_block
1234 * are updated to point to the next record.
1236 static void INT21_SequentialWriteToFCB( CONTEXT
*context
)
1241 DWORD record_number
;
1243 BYTE
*disk_transfer_area
;
1247 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1248 if (fcb
->drive_number
== 0xff) {
1249 xfcb
= (struct XFCB
*) fcb
;
1250 fcb
= (struct FCB
*) xfcb
->fcb
;
1253 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1254 if (handle
== INVALID_HANDLE_VALUE
) {
1255 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1257 AL_result
= 0x01; /* disk full */
1259 record_number
= 128 * fcb
->current_block_number
+ fcb
->record_within_current_block
;
1260 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1261 if (position
!= record_number
* fcb
->logical_record_size
) {
1262 TRACE("seek(%d, %d, 0) failed with %u\n",
1263 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1264 AL_result
= 0x01; /* disk full */
1266 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1267 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, fcb
->logical_record_size
);
1268 if (bytes_written
!= fcb
->logical_record_size
) {
1269 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1270 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_written
);
1271 AL_result
= 0x01; /* disk full */
1273 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1274 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1275 AL_result
= 0x00; /* successful */
1279 if (AL_result
== 0x00) {
1280 if (fcb
->record_within_current_block
== 127) {
1281 fcb
->record_within_current_block
= 0;
1282 fcb
->current_block_number
++;
1284 fcb
->record_within_current_block
++;
1287 SET_AL(context
, AL_result
);
1291 /***********************************************************************
1292 * INT21_ReadRandomRecordFromFCB
1294 * Handler for function 0x21.
1297 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1301 * 1: end of file, no data read
1302 * 2: segment wrap in DTA, no data read (not returned now)
1303 * 3: end of file, partial record read
1306 * Reads a record with the size FCB->logical_record_size from
1307 * the FCB to the disk transfer area. The position of the record
1308 * is specified with FCB->random_access_record_number. The
1309 * FCB->random_access_record_number is not updated. If a partial record
1310 * is read, it is filled with zeros up to the FCB->logical_record_size.
1312 static void INT21_ReadRandomRecordFromFCB( CONTEXT
*context
)
1317 DWORD record_number
;
1319 BYTE
*disk_transfer_area
;
1323 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1324 if (fcb
->drive_number
== 0xff) {
1325 xfcb
= (struct XFCB
*) fcb
;
1326 fcb
= (struct FCB
*) xfcb
->fcb
;
1329 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1330 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1331 if (handle
== INVALID_HANDLE_VALUE
) {
1332 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1334 AL_result
= 0x01; /* end of file, no data read */
1336 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1337 if (position
!= record_number
* fcb
->logical_record_size
) {
1338 TRACE("seek(%d, %d, 0) failed with %u\n",
1339 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1340 AL_result
= 0x01; /* end of file, no data read */
1342 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1343 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, fcb
->logical_record_size
);
1344 if (bytes_read
!= fcb
->logical_record_size
) {
1345 TRACE("_lread(%d, %p, %d) failed with %d\n",
1346 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_read
);
1347 if (bytes_read
== 0) {
1348 AL_result
= 0x01; /* end of file, no data read */
1350 memset(&disk_transfer_area
[bytes_read
], 0, fcb
->logical_record_size
- bytes_read
);
1351 AL_result
= 0x03; /* end of file, partial record read */
1354 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1355 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1356 AL_result
= 0x00; /* successful */
1360 fcb
->current_block_number
= record_number
/ 128;
1361 fcb
->record_within_current_block
= record_number
% 128;
1362 SET_AL(context
, AL_result
);
1366 /***********************************************************************
1367 * INT21_WriteRandomRecordToFCB
1369 * Handler for function 0x22.
1372 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1377 * 2: segment wrap in DTA (not returned now)
1380 * Writes a record with the size FCB->logical_record_size from
1381 * the disk transfer area to the FCB. The position of the record
1382 * is specified with FCB->random_access_record_number. The
1383 * FCB->random_access_record_number is not updated.
1385 static void INT21_WriteRandomRecordToFCB( CONTEXT
*context
)
1390 DWORD record_number
;
1392 BYTE
*disk_transfer_area
;
1396 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1397 if (fcb
->drive_number
== 0xff) {
1398 xfcb
= (struct XFCB
*) fcb
;
1399 fcb
= (struct FCB
*) xfcb
->fcb
;
1402 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1403 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1404 if (handle
== INVALID_HANDLE_VALUE
) {
1405 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1407 AL_result
= 0x01; /* disk full */
1409 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1410 if (position
!= record_number
* fcb
->logical_record_size
) {
1411 TRACE("seek(%d, %d, 0) failed with %u\n",
1412 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1413 AL_result
= 0x01; /* disk full */
1415 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1416 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, fcb
->logical_record_size
);
1417 if (bytes_written
!= fcb
->logical_record_size
) {
1418 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1419 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_written
);
1420 AL_result
= 0x01; /* disk full */
1422 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1423 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1424 AL_result
= 0x00; /* successful */
1428 fcb
->current_block_number
= record_number
/ 128;
1429 fcb
->record_within_current_block
= record_number
% 128;
1430 SET_AL(context
, AL_result
);
1434 /***********************************************************************
1435 * INT21_RandomBlockReadFromFCB
1437 * Handler for function 0x27.
1440 * CX [I/O] Number of records to read
1441 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1445 * 1: end of file, no data read
1446 * 2: segment wrap in DTA, no data read (not returned now)
1447 * 3: end of file, partial record read
1450 * Reads several records with the size FCB->logical_record_size from
1451 * the FCB to the disk transfer area. The number of records to be
1452 * read is specified in the CX register. The position of the first
1453 * record is specified with FCB->random_access_record_number. The
1454 * FCB->random_access_record_number, the FCB->current_block_number
1455 * and FCB->record_within_current_block are updated to point to the
1456 * next record after the records read. If a partial record is read,
1457 * it is filled with zeros up to the FCB->logical_record_size. The
1458 * CX register is set to the number of successfully read records.
1460 static void INT21_RandomBlockReadFromFCB( CONTEXT
*context
)
1465 DWORD record_number
;
1467 BYTE
*disk_transfer_area
;
1468 UINT records_requested
;
1469 UINT bytes_requested
;
1474 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1475 if (fcb
->drive_number
== 0xff) {
1476 xfcb
= (struct XFCB
*) fcb
;
1477 fcb
= (struct FCB
*) xfcb
->fcb
;
1480 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1481 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1482 if (handle
== INVALID_HANDLE_VALUE
) {
1483 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1486 AL_result
= 0x01; /* end of file, no data read */
1488 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1489 if (position
!= record_number
* fcb
->logical_record_size
) {
1490 TRACE("seek(%d, %d, 0) failed with %u\n",
1491 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1493 AL_result
= 0x01; /* end of file, no data read */
1495 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1496 records_requested
= CX_reg(context
);
1497 bytes_requested
= records_requested
* fcb
->logical_record_size
;
1498 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, bytes_requested
);
1499 if (bytes_read
!= bytes_requested
) {
1500 TRACE("_lread(%d, %p, %d) failed with %d\n",
1501 fcb
->file_number
, disk_transfer_area
, bytes_requested
, bytes_read
);
1502 records_read
= bytes_read
/ fcb
->logical_record_size
;
1503 if (bytes_read
% fcb
->logical_record_size
== 0) {
1504 AL_result
= 0x01; /* end of file, no data read */
1507 memset(&disk_transfer_area
[bytes_read
], 0, records_read
* fcb
->logical_record_size
- bytes_read
);
1508 AL_result
= 0x03; /* end of file, partial record read */
1511 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1512 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1513 records_read
= records_requested
;
1514 AL_result
= 0x00; /* successful */
1518 record_number
+= records_read
;
1519 memcpy(fcb
->random_access_record_number
, &record_number
, 4);
1520 fcb
->current_block_number
= record_number
/ 128;
1521 fcb
->record_within_current_block
= record_number
% 128;
1522 SET_CX(context
, records_read
);
1523 SET_AL(context
, AL_result
);
1527 /***********************************************************************
1528 * INT21_RandomBlockWriteToFCB
1530 * Handler for function 0x28.
1533 * CX [I/O] Number of records to write
1534 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1539 * 2: segment wrap in DTA (not returned now)
1542 * Writes several records with the size FCB->logical_record_size from
1543 * the disk transfer area to the FCB. The number of records to be
1544 * written is specified in the CX register. The position of the first
1545 * record is specified with FCB->random_access_record_number. The
1546 * FCB->random_access_record_number, the FCB->current_block_number
1547 * and FCB->record_within_current_block are updated to point to the
1548 * next record after the records written. The CX register is set to
1549 * the number of successfully written records.
1551 static void INT21_RandomBlockWriteToFCB( CONTEXT
*context
)
1556 DWORD record_number
;
1558 BYTE
*disk_transfer_area
;
1559 UINT records_requested
;
1560 UINT bytes_requested
;
1562 UINT records_written
;
1565 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1566 if (fcb
->drive_number
== 0xff) {
1567 xfcb
= (struct XFCB
*) fcb
;
1568 fcb
= (struct FCB
*) xfcb
->fcb
;
1571 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1572 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1573 if (handle
== INVALID_HANDLE_VALUE
) {
1574 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1576 records_written
= 0;
1577 AL_result
= 0x01; /* disk full */
1579 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1580 if (position
!= record_number
* fcb
->logical_record_size
) {
1581 TRACE("seek(%d, %d, 0) failed with %u\n",
1582 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1583 records_written
= 0;
1584 AL_result
= 0x01; /* disk full */
1586 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1587 records_requested
= CX_reg(context
);
1588 bytes_requested
= records_requested
* fcb
->logical_record_size
;
1589 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, bytes_requested
);
1590 if (bytes_written
!= bytes_requested
) {
1591 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1592 fcb
->file_number
, disk_transfer_area
, bytes_requested
, bytes_written
);
1593 records_written
= bytes_written
/ fcb
->logical_record_size
;
1594 AL_result
= 0x01; /* disk full */
1596 TRACE("successful write %d bytes from record %d (position %u) of file %d (handle %p)\n",
1597 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1598 records_written
= records_requested
;
1599 AL_result
= 0x00; /* successful */
1603 record_number
+= records_written
;
1604 memcpy(fcb
->random_access_record_number
, &record_number
, 4);
1605 fcb
->current_block_number
= record_number
/ 128;
1606 fcb
->record_within_current_block
= record_number
% 128;
1607 SET_CX(context
, records_written
);
1608 SET_AL(context
, AL_result
);
1612 /***********************************************************************
1613 * INT21_CreateDirectory
1617 * - subfunction 0x39 of function 0x71
1618 * - subfunction 0xff of function 0x43 (CL == 0x39)
1620 static BOOL
INT21_CreateDirectory( CONTEXT
*context
)
1622 WCHAR dirW
[MAX_PATH
];
1623 char *dirA
= CTX_SEG_OFF_TO_LIN(context
,
1627 TRACE( "CREATE DIRECTORY %s\n", dirA
);
1629 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
1631 if (CreateDirectoryW(dirW
, NULL
))
1635 * FIXME: CreateDirectory's LastErrors will clash with the ones
1636 * used by DOS. AH=39 only returns 3 (path not found) and
1637 * 5 (access denied), while CreateDirectory return several
1638 * ones. Remap some of them. -Marcus
1640 switch (GetLastError())
1642 case ERROR_ALREADY_EXISTS
:
1643 case ERROR_FILENAME_EXCED_RANGE
:
1644 case ERROR_DISK_FULL
:
1645 SetLastError(ERROR_ACCESS_DENIED
);
1655 /***********************************************************************
1656 * INT21_ExtendedCountryInformation
1658 * Handler for function 0x65.
1660 static void INT21_ExtendedCountryInformation( CONTEXT
*context
)
1662 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN( context
, context
->SegEs
, context
->Edi
);
1663 BYTE buffsize
= CX_reg (context
);
1665 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1669 * Check subfunctions that are passed country and code page.
1671 if (AL_reg(context
) >= 0x01 && AL_reg(context
) <= 0x07)
1673 WORD country
= DX_reg(context
);
1674 WORD codepage
= BX_reg(context
);
1676 if (country
!= 0xffff && country
!= INT21_GetSystemCountryCode())
1677 FIXME( "Requested info on non-default country %04x\n", country
);
1679 if (codepage
!= 0xffff && codepage
!= GetOEMCP())
1680 FIXME( "Requested info on non-default code page %04x\n", codepage
);
1683 switch (AL_reg(context
)) {
1684 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1685 INT_BARF( context
, 0x21 );
1686 SET_CFLAG( context
);
1689 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1690 TRACE( "Get general internationalization info\n" );
1691 dataptr
[0] = 0x01; /* Info ID */
1692 *(WORD
*)(dataptr
+1) = 38; /* Size of the following info */
1693 *(WORD
*)(dataptr
+3) = INT21_GetSystemCountryCode(); /* Country ID */
1694 *(WORD
*)(dataptr
+5) = GetOEMCP(); /* Code page */
1695 /* FIXME: fill buffer partially up to buffsize bytes*/
1696 if (buffsize
>= 0x29){
1697 INT21_FillCountryInformation( dataptr
+ 7 );
1698 SET_CX( context
, 0x29 ); /* Size of returned info */
1700 SET_CX( context
, 0x07 ); /* Size of returned info */
1704 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1705 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1706 TRACE( "Get pointer to uppercase table\n" );
1707 dataptr
[0] = AL_reg(context
); /* Info ID */
1708 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1709 offsetof(INT21_HEAP
, uppercase_size
) );
1710 SET_CX( context
, 5 ); /* Size of returned info */
1713 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1714 TRACE( "Get pointer to lowercase table\n" );
1715 dataptr
[0] = 0x03; /* Info ID */
1716 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1717 offsetof(INT21_HEAP
, lowercase_size
) );
1718 SET_CX( context
, 5 ); /* Size of returned info */
1721 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1722 TRACE("Get pointer to filename terminator table\n");
1723 dataptr
[0] = 0x05; /* Info ID */
1724 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1725 offsetof(INT21_HEAP
, filename_size
) );
1726 SET_CX( context
, 5 ); /* Size of returned info */
1729 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1730 TRACE("Get pointer to collating sequence table\n");
1731 dataptr
[0] = 0x06; /* Info ID */
1732 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1733 offsetof(INT21_HEAP
, collating_size
) );
1734 SET_CX( context
, 5 ); /* Size of returned info */
1737 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1738 TRACE("Get pointer to DBCS lead byte table\n");
1739 dataptr
[0] = 0x07; /* Info ID */
1740 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1741 offsetof(INT21_HEAP
, dbcs_size
) );
1742 SET_CX( context
, 5 ); /* Size of returned info */
1745 case 0x20: /* CAPITALIZE CHARACTER */
1746 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1747 TRACE("Convert char to uppercase\n");
1748 SET_DL( context
, toupper(DL_reg(context
)) );
1751 case 0x21: /* CAPITALIZE STRING */
1752 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1753 TRACE("Convert string to uppercase with length\n");
1755 char *ptr
= CTX_SEG_OFF_TO_LIN( context
, context
->SegDs
,
1757 WORD len
= CX_reg(context
);
1758 while (len
--) { *ptr
= toupper(*ptr
); ptr
++; }
1762 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1763 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1764 TRACE("Convert ASCIIZ string to uppercase\n");
1766 char *p
= CTX_SEG_OFF_TO_LIN( context
, context
->SegDs
, context
->Edx
);
1767 for ( ; *p
; p
++) *p
= toupper(*p
);
1771 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1772 INT_BARF( context
, 0x21 );
1773 SET_CFLAG( context
);
1777 INT_BARF( context
, 0x21 );
1784 /***********************************************************************
1785 * INT21_FileAttributes
1789 * - subfunction 0x43 of function 0x71
1791 static BOOL
INT21_FileAttributes( CONTEXT
*context
,
1795 WCHAR fileW
[MAX_PATH
];
1796 char *fileA
= CTX_SEG_OFF_TO_LIN(context
,
1806 switch (subfunction
)
1808 case 0x00: /* GET FILE ATTRIBUTES */
1809 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA
);
1810 len
= MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1812 /* Winbench 96 Disk Test fails if we don't complain
1813 * about a filename that ends in \
1815 if (!len
|| (fileW
[len
-1] == '/') || (fileW
[len
-1] == '\\'))
1818 result
= GetFileAttributesW( fileW
);
1819 if (result
== INVALID_FILE_ATTRIBUTES
)
1823 SET_CX( context
, (WORD
)result
);
1825 SET_AX( context
, (WORD
)result
); /* DR DOS */
1829 case 0x01: /* SET FILE ATTRIBUTES */
1830 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
1831 CX_reg(context
), fileA
);
1832 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1834 if (!SetFileAttributesW( fileW
, CX_reg(context
) ))
1838 case 0x02: /* GET COMPRESSED FILE SIZE */
1839 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA
);
1840 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1842 result
= GetCompressedFileSizeW( fileW
, NULL
);
1843 if (result
== INVALID_FILE_SIZE
)
1847 SET_AX( context
, LOWORD(result
) );
1848 SET_DX( context
, HIWORD(result
) );
1852 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
1854 INT_BARF( context
, 0x21 );
1857 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA
);
1858 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1860 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
1861 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1862 NULL
, OPEN_EXISTING
, 0, 0 );
1863 if (handle
== INVALID_HANDLE_VALUE
)
1866 DosDateTimeToFileTime( DI_reg(context
),
1869 status
= SetFileTime( handle
, NULL
, NULL
, &filetime
);
1871 CloseHandle( handle
);
1876 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
1878 INT_BARF( context
, 0x21 );
1881 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA
);
1882 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1884 handle
= CreateFileW( fileW
, GENERIC_READ
,
1885 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1886 NULL
, OPEN_EXISTING
, 0, 0 );
1887 if (handle
== INVALID_HANDLE_VALUE
)
1890 status
= GetFileTime( handle
, NULL
, NULL
, &filetime
);
1893 FileTimeToDosDateTime( &filetime
, &date
, &time
);
1894 SET_DI( context
, date
);
1895 SET_CX( context
, time
);
1898 CloseHandle( handle
);
1903 case 0x05: /* SET FILE LAST ACCESS DATE */
1905 INT_BARF( context
, 0x21 );
1908 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA
);
1909 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1911 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
1912 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1913 NULL
, OPEN_EXISTING
, 0, 0 );
1914 if (handle
== INVALID_HANDLE_VALUE
)
1917 DosDateTimeToFileTime( DI_reg(context
),
1920 status
= SetFileTime( handle
, NULL
, &filetime
, NULL
);
1922 CloseHandle( handle
);
1927 case 0x06: /* GET FILE LAST ACCESS DATE */
1929 INT_BARF( context
, 0x21 );
1932 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA
);
1933 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1935 handle
= CreateFileW( fileW
, GENERIC_READ
,
1936 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1937 NULL
, OPEN_EXISTING
, 0, 0 );
1938 if (handle
== INVALID_HANDLE_VALUE
)
1941 status
= GetFileTime( handle
, NULL
, &filetime
, NULL
);
1944 FileTimeToDosDateTime( &filetime
, &date
, NULL
);
1945 SET_DI( context
, date
);
1948 CloseHandle( handle
);
1953 case 0x07: /* SET FILE CREATION DATE AND TIME */
1955 INT_BARF( context
, 0x21 );
1958 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA
);
1959 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1961 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
1962 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1963 NULL
, OPEN_EXISTING
, 0, 0 );
1964 if (handle
== INVALID_HANDLE_VALUE
)
1968 * FIXME: SI has number of 10-millisecond units past time in CX.
1970 DosDateTimeToFileTime( DI_reg(context
),
1973 status
= SetFileTime( handle
, &filetime
, NULL
, NULL
);
1975 CloseHandle( handle
);
1980 case 0x08: /* GET FILE CREATION DATE AND TIME */
1982 INT_BARF( context
, 0x21 );
1985 TRACE( "GET FILE CREATION DATE AND TIME, file %s\n", fileA
);
1986 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1988 handle
= CreateFileW( fileW
, GENERIC_READ
,
1989 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1990 NULL
, OPEN_EXISTING
, 0, 0 );
1991 if (handle
== INVALID_HANDLE_VALUE
)
1994 status
= GetFileTime( handle
, &filetime
, NULL
, NULL
);
1997 FileTimeToDosDateTime( &filetime
, &date
, &time
);
1998 SET_DI( context
, date
);
1999 SET_CX( context
, time
);
2001 * FIXME: SI has number of 10-millisecond units past
2004 SET_SI( context
, 0 );
2007 CloseHandle(handle
);
2012 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2013 if (islong
|| context
->Ebp
!= 0x5053)
2014 INT_BARF( context
, 0x21 );
2017 switch(CL_reg(context
))
2020 if (!INT21_CreateDirectory( context
))
2025 if (!INT21_RenameFile( context
))
2030 INT_BARF( context
, 0x21 );
2036 INT_BARF( context
, 0x21 );
2043 /***********************************************************************
2044 * INT21_FileDateTime
2046 * Handler for function 0x57.
2048 static BOOL
INT21_FileDateTime( CONTEXT
*context
)
2050 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2054 switch (AL_reg(context
)) {
2055 case 0x00: /* Get last-written stamp */
2056 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2059 if (!GetFileTime( handle
, NULL
, NULL
, &filetime
))
2061 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2062 SET_DX( context
, date
);
2063 SET_CX( context
, time
);
2067 case 0x01: /* Set last-written stamp */
2068 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2071 DosDateTimeToFileTime( DX_reg(context
),
2074 if (!SetFileTime( handle
, NULL
, NULL
, &filetime
))
2079 case 0x04: /* Get last access stamp, DOS 7 */
2080 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2083 if (!GetFileTime( handle
, NULL
, &filetime
, NULL
))
2085 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2086 SET_DX( context
, date
);
2087 SET_CX( context
, time
);
2091 case 0x05: /* Set last access stamp, DOS 7 */
2092 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2095 DosDateTimeToFileTime( DX_reg(context
),
2098 if (!SetFileTime( handle
, NULL
, &filetime
, NULL
))
2103 case 0x06: /* Get creation stamp, DOS 7 */
2104 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2107 if (!GetFileTime( handle
, &filetime
, NULL
, NULL
))
2109 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2110 SET_DX( context
, date
);
2111 SET_CX( context
, time
);
2113 * FIXME: SI has number of 10-millisecond units past time in CX.
2115 SET_SI( context
, 0 );
2119 case 0x07: /* Set creation stamp, DOS 7 */
2120 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2124 * FIXME: SI has number of 10-millisecond units past time in CX.
2126 DosDateTimeToFileTime( DX_reg(context
),
2129 if (!SetFileTime( handle
, &filetime
, NULL
, NULL
))
2135 INT_BARF( context
, 0x21 );
2143 /***********************************************************************
2146 * Handler for functions 0x51 and 0x62.
2148 static void INT21_GetPSP( CONTEXT
*context
)
2150 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context
) );
2152 SET_BX( context
, LOWORD(GetCurrentPDB16()) );
2155 static inline void setword( BYTE
*ptr
, WORD w
)
2158 ptr
[1] = (BYTE
)(w
>> 8);
2161 static void CreateBPB(int drive
, BYTE
*data
, BOOL16 limited
)
2162 /* limited == TRUE is used with INT 0x21/0x440d */
2164 /* FIXME: we're forcing some values without checking that those are valid */
2169 setword(&data
[3], 0);
2171 setword(&data
[6], 240);
2172 setword(&data
[8], 64000);
2174 setword(&data
[0x0b], 40);
2175 setword(&data
[0x0d], 56);
2176 setword(&data
[0x0f], 2);
2177 setword(&data
[0x11], 0);
2180 setword(&data
[0x1f], 800);
2182 setword(&data
[0x22], 1);
2189 setword(&data
[3], 0);
2191 setword(&data
[6], 240);
2192 setword(&data
[8], 2880);
2194 setword(&data
[0x0b], 6);
2195 setword(&data
[0x0d], 18);
2196 setword(&data
[0x0f], 2);
2197 setword(&data
[0x11], 0);
2200 setword(&data
[0x1f], 80);
2202 setword(&data
[0x22], 2);
2207 static inline DWORD
INT21_Ioctl_CylHeadSect2Lin(DWORD cyl
, WORD head
, WORD sec
, WORD cyl_cnt
,
2208 WORD head_cnt
, WORD sec_cnt
)
2210 DWORD res
= (cyl
* head_cnt
*sec_cnt
+ head
* sec_cnt
+ sec
);
2214 /***********************************************************************
2217 * Handler for block device IOCTLs.
2219 static void INT21_Ioctl_Block( CONTEXT
*context
)
2222 BYTE drive
= INT21_MapDrive( BL_reg(context
) );
2223 WCHAR drivespec
[] = {'A', ':', '\\', 0};
2226 drivespec
[0] += drive
;
2227 drivetype
= GetDriveTypeW( drivespec
);
2229 RESET_CFLAG(context
);
2230 if (drivetype
== DRIVE_UNKNOWN
|| drivetype
== DRIVE_NO_ROOT_DIR
)
2232 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2233 AL_reg(context
), 'A' + drive
);
2234 SetLastError( ERROR_INVALID_DRIVE
);
2235 SET_AX( context
, ERROR_INVALID_DRIVE
);
2236 SET_CFLAG( context
);
2240 switch (AL_reg(context
))
2242 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2243 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2244 INT_BARF( context
, 0x21 );
2247 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2248 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2251 if (drivetype
== DRIVE_REMOVABLE
)
2252 SET_AX( context
, 0 ); /* removable */
2254 SET_AX( context
, 1 ); /* not removable */
2257 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2258 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2261 if (drivetype
== DRIVE_REMOTE
)
2262 SET_DX( context
, (1<<9) | (1<<12) ); /* remote + no direct IO */
2263 else if (drivetype
== DRIVE_CDROM
)
2264 /* CDROM should be set to remote. If it set the app will
2265 * call int2f to check if it cdrom or remote drive. */
2266 SET_DX( context
, (1<<12) );
2267 else if (drivetype
== DRIVE_FIXED
)
2268 /* This should define if drive support 0x0d, 0x0f and 0x08
2269 * requests. The local fixed drive should do. */
2270 SET_DX( context
, (1<<11) );
2272 SET_DX( context
, 0 ); /* FIXME: use driver attr here */
2275 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2276 /* Get pointer to IOCTL parameter block */
2277 dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2279 switch (CX_reg(context
))
2281 case 0x0841: /* write logical device track */
2282 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2285 WORD head
= *(WORD
*)(dataptr
+1);
2286 WORD cyl
= *(WORD
*)(dataptr
+3);
2287 WORD sect
= *(WORD
*)(dataptr
+5);
2288 WORD nrsect
= *(WORD
*)(dataptr
+7);
2289 BYTE
*data
= CTX_SEG_OFF_TO_LIN(context
, *(WORD
*)(dataptr
+11), *(WORD
*)(dataptr
+9));
2290 WORD cyl_cnt
, head_cnt
, sec_cnt
;
2292 /* FIXME: we're faking some values here */
2298 SET_AX( context
, ERROR_WRITE_FAULT
);
2309 if (!DOSVM_RawWrite(drive
, INT21_Ioctl_CylHeadSect2Lin(cyl
, head
, sect
, cyl_cnt
, head_cnt
, sec_cnt
), nrsect
, data
, FALSE
))
2311 SET_AX( context
, ERROR_WRITE_FAULT
);
2317 case 0x084a: /* lock logical volume */
2318 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2319 BH_reg(context
), DX_reg(context
), 'A' + drive
);
2322 case 0x0860: /* get device parameters */
2323 /* FIXME: we're faking some values here */
2324 /* used by w4wgrp's winfile */
2325 memset(dataptr
, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2327 dataptr
[6] = 0; /* media type */
2330 dataptr
[1] = 0x05; /* fixed disk */
2331 setword(&dataptr
[2], 0x01); /* non removable */
2332 setword(&dataptr
[4], 0x300); /* # of cylinders */
2336 dataptr
[1] = 0x07; /* block dev, floppy */
2337 setword(&dataptr
[2], 0x02); /* removable */
2338 setword(&dataptr
[4], 80); /* # of cylinders */
2340 CreateBPB(drive
, &dataptr
[7], TRUE
);
2341 RESET_CFLAG(context
);
2344 case 0x0861: /* read logical device track */
2345 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2348 WORD head
= *(WORD
*)(dataptr
+1);
2349 WORD cyl
= *(WORD
*)(dataptr
+3);
2350 WORD sect
= *(WORD
*)(dataptr
+5);
2351 WORD nrsect
= *(WORD
*)(dataptr
+7);
2352 BYTE
*data
= CTX_SEG_OFF_TO_LIN(context
, *(WORD
*)(dataptr
+11), *(WORD
*)(dataptr
+9));
2353 WORD cyl_cnt
, head_cnt
, sec_cnt
;
2355 /* FIXME: we're faking some values here */
2369 if (!DOSVM_RawRead(drive
, INT21_Ioctl_CylHeadSect2Lin(cyl
, head
, sect
, cyl_cnt
, head_cnt
, sec_cnt
), nrsect
, data
, FALSE
))
2371 SET_AX( context
, ERROR_READ_FAULT
);
2377 case 0x0866: /* get volume serial number */
2379 WCHAR label
[12],fsname
[9];
2381 TRACE( "GENERIC IOCTL - Get media id - %c:\n",
2384 GetVolumeInformationW(drivespec
, label
, 12, &serial
, NULL
, NULL
, fsname
, 9);
2385 *(WORD
*)dataptr
= 0;
2386 memcpy(dataptr
+2,&serial
,4);
2387 WideCharToMultiByte(CP_OEMCP
, 0, label
, 11, (LPSTR
)dataptr
+ 6, 11, NULL
, NULL
);
2388 WideCharToMultiByte(CP_OEMCP
, 0, fsname
, 8, (LPSTR
)dataptr
+ 17, 8, NULL
, NULL
);
2392 case 0x086a: /* unlock logical volume */
2393 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2397 case 0x086f: /* get drive map information */
2398 memset(dataptr
+1, '\0', dataptr
[0]-1);
2399 dataptr
[1] = dataptr
[0];
2400 dataptr
[2] = 0x07; /* protected mode driver; no eject; no notification */
2401 dataptr
[3] = 0xFF; /* no physical drive */
2405 /* Trial and error implementation */
2406 SET_AX( context
, drivetype
== DRIVE_UNKNOWN
? 0x0f : 0x01 );
2407 SET_CFLAG(context
); /* Seems to be set all the time */
2411 INT_BARF( context
, 0x21 );
2415 case 0x0e: /* GET LOGICAL DRIVE MAP */
2416 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2418 /* FIXME: this is not correct if drive has mappings */
2419 SET_AL( context
, 0 ); /* drive has no mapping */
2422 case 0x0f: /* SET LOGICAL DRIVE MAP */
2423 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2424 INT21_DriveName( BL_reg(context
)));
2425 /* FIXME: as of today, we don't support logical drive mapping... */
2426 SET_AL( context
, 0 );
2429 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2431 INT_BARF( context
, 0x21 );
2436 /***********************************************************************
2439 * Handler for character device IOCTLs.
2441 static void INT21_Ioctl_Char( CONTEXT
*context
)
2443 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2444 BOOL IsConsoleIOHandle
= VerifyConsoleIoHandle( handle
);
2446 switch (AL_reg(context
))
2448 case 0x00: /* GET DEVICE INFORMATION */
2449 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context
) );
2450 if (IsConsoleIOHandle
|| GetFileType(handle
) == FILE_TYPE_CHAR
)
2453 * Returns attribute word in DX:
2454 * Bit 14 - Device driver can process IOCTL requests.
2455 * Bit 13 - Output until busy supported.
2456 * Bit 11 - Driver supports OPEN/CLOSE calls.
2458 * Bit 7 - Set (indicates device).
2459 * Bit 6 - EOF on input.
2460 * Bit 5 - Raw (binary) mode.
2461 * Bit 4 - Device is special (uses int29).
2462 * Bit 3 - Clock device.
2463 * Bit 2 - NUL device.
2464 * Bit 1 - Console output device.
2465 * Bit 0 - Console input device.
2467 SET_DX( context
, IsConsoleIOHandle
? 0x80c3 : 0x80c0 /* FIXME */ );
2472 * Returns attribute word in DX:
2473 * Bit 15 - File is remote.
2474 * Bit 14 - Don't set file date/time on closing.
2475 * Bit 11 - Media not removable.
2476 * Bit 8 - Generate int24 if no disk space on write
2477 * or read past end of file
2478 * Bit 7 - Clear (indicates file).
2479 * Bit 6 - File has not been written.
2480 * Bit 5..0 - Drive number (0=A:,...)
2482 * FIXME: Should check if file is on remote or removable drive.
2483 * FIXME: Should use drive file is located on (and not current).
2485 SET_DX( context
, 0x0140 + INT21_GetCurrentDrive() );
2489 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2490 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context
) );
2492 * Returns attribute word in DX:
2493 * Bit 15 - Set if remote.
2494 * Bit 14 - Set if date/time not set on close.
2496 * FIXME: Should check if file is on remote drive.
2498 SET_DX( context
, 0 );
2501 case 0x01: /* SET DEVICE INFORMATION */
2502 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2503 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2504 case 0x06: /* GET INPUT STATUS */
2505 case 0x07: /* GET OUTPUT STATUS */
2506 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2507 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2509 INT_BARF( context
, 0x21 );
2515 /***********************************************************************
2518 * Handler for function 0x44.
2520 static void INT21_Ioctl( CONTEXT
*context
)
2522 switch (AL_reg(context
))
2528 INT21_Ioctl_Char( context
);
2533 INT21_Ioctl_Block( context
);
2538 INT21_Ioctl_Char( context
);
2543 INT21_Ioctl_Block( context
);
2547 INT21_Ioctl_Char( context
);
2550 case 0x0b: /* SET SHARING RETRY COUNT */
2551 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2552 CX_reg(context
), DX_reg(context
) );
2553 if (!CX_reg(context
))
2555 SET_AX( context
, 1 );
2556 SET_CFLAG( context
);
2560 RESET_CFLAG( context
);
2565 INT21_Ioctl_Char( context
);
2571 INT21_Ioctl_Block( context
);
2575 INT21_Ioctl_Char( context
);
2579 INT21_Ioctl_Block( context
);
2582 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2583 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2584 SET_CFLAG(context
); /* Error / This is not DR DOS. */
2585 SET_AX( context
, 0x0001 ); /* Invalid function */
2588 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2589 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2590 SET_CFLAG(context
); /* Error / This is not DR DOS. */
2591 SET_AX( context
, 0x0001 ); /* Invalid function */
2594 case 0xe0: /* Sun PC-NFS API */
2595 TRACE( "Sun PC-NFS API\n" );
2600 INT_BARF( context
, 0x21 );
2605 /***********************************************************************
2608 * Handler for function 0x73.
2610 static BOOL
INT21_Fat32( CONTEXT
*context
)
2612 switch (AL_reg(context
))
2614 case 0x02: /* FAT32 - GET EXTENDED DPB */
2616 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
2617 WORD
*ptr
= CTX_SEG_OFF_TO_LIN(context
,
2618 context
->SegEs
, context
->Edi
);
2619 INT21_DPB
*target
= (INT21_DPB
*)(ptr
+ 1);
2622 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context
) );
2624 if ( CX_reg(context
) < sizeof(INT21_DPB
) + 2 || *ptr
< sizeof(INT21_DPB
) )
2626 SetLastError( ERROR_BAD_LENGTH
);
2630 if ( !INT21_FillDrivePB( drive
) )
2632 SetLastError( ERROR_INVALID_DRIVE
);
2636 source
= &INT21_GetHeapPointer()->misc_dpb_list
[drive
];
2638 *ptr
= sizeof(INT21_DPB
);
2641 if (LOWORD(context
->Esi
) != 0xF1A6)
2643 target
->driver_header
= 0;
2648 FIXME( "Caller requested driver and next DPB pointers!\n" );
2653 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2655 WCHAR dirW
[MAX_PATH
];
2656 char *dirA
= CTX_SEG_OFF_TO_LIN( context
,
2657 context
->SegDs
, context
->Edx
);
2658 BYTE
*data
= CTX_SEG_OFF_TO_LIN( context
,
2659 context
->SegEs
, context
->Edi
);
2660 DWORD cluster_sectors
;
2662 DWORD free_clusters
;
2663 DWORD total_clusters
;
2665 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA
);
2666 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
2668 if (CX_reg(context
) < 44)
2670 SetLastError( ERROR_BAD_LENGTH
);
2674 if (!GetDiskFreeSpaceW( dirW
, &cluster_sectors
, §or_bytes
,
2675 &free_clusters
, &total_clusters
))
2678 *(WORD
*) (data
+ 0) = 44; /* size of structure */
2679 *(WORD
*) (data
+ 2) = 0; /* version */
2680 *(DWORD
*)(data
+ 4) = cluster_sectors
;
2681 *(DWORD
*)(data
+ 8) = sector_bytes
;
2682 *(DWORD
*)(data
+ 12) = free_clusters
;
2683 *(DWORD
*)(data
+ 16) = total_clusters
;
2686 * Below we have free/total sectors and
2687 * free/total allocation units without adjustment
2688 * for compression. We fake both using cluster information.
2690 *(DWORD
*)(data
+ 20) = free_clusters
* cluster_sectors
;
2691 *(DWORD
*)(data
+ 24) = total_clusters
* cluster_sectors
;
2692 *(DWORD
*)(data
+ 28) = free_clusters
;
2693 *(DWORD
*)(data
+ 32) = total_clusters
;
2696 * Between (data + 36) and (data + 43) there
2697 * are eight reserved bytes.
2703 INT_BARF( context
, 0x21 );
2709 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA
*dataA
,
2710 const WIN32_FIND_DATAW
*dataW
)
2712 dataA
->dwFileAttributes
= dataW
->dwFileAttributes
;
2713 dataA
->ftCreationTime
= dataW
->ftCreationTime
;
2714 dataA
->ftLastAccessTime
= dataW
->ftLastAccessTime
;
2715 dataA
->ftLastWriteTime
= dataW
->ftLastWriteTime
;
2716 dataA
->nFileSizeHigh
= dataW
->nFileSizeHigh
;
2717 dataA
->nFileSizeLow
= dataW
->nFileSizeLow
;
2718 WideCharToMultiByte( CP_OEMCP
, 0, dataW
->cFileName
, -1,
2719 dataA
->cFileName
, sizeof(dataA
->cFileName
), NULL
, NULL
);
2720 WideCharToMultiByte( CP_OEMCP
, 0, dataW
->cAlternateFileName
, -1,
2721 dataA
->cAlternateFileName
, sizeof(dataA
->cAlternateFileName
), NULL
, NULL
);
2724 /***********************************************************************
2725 * INT21_LongFilename
2727 * Handler for function 0x71.
2729 static void INT21_LongFilename( CONTEXT
*context
)
2731 BOOL bSetDOSExtendedError
= FALSE
;
2732 WCHAR pathW
[MAX_PATH
];
2735 switch (AL_reg(context
))
2737 case 0x0d: /* RESET DRIVE */
2738 INT_BARF( context
, 0x21 );
2741 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
2742 if (!INT21_CreateDirectory( context
))
2743 bSetDOSExtendedError
= TRUE
;
2746 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
2747 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2749 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA
);
2750 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2751 if (!RemoveDirectoryW( pathW
)) bSetDOSExtendedError
= TRUE
;
2754 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
2755 if (!INT21_SetCurrentDirectory( context
))
2756 bSetDOSExtendedError
= TRUE
;
2759 case 0x41: /* LONG FILENAME - DELETE FILE */
2760 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2762 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA
);
2763 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2765 if (!DeleteFileW( pathW
)) bSetDOSExtendedError
= TRUE
;
2768 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
2769 if (!INT21_FileAttributes( context
, BL_reg(context
), TRUE
))
2770 bSetDOSExtendedError
= TRUE
;
2773 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
2774 if (!INT21_GetCurrentDirectory( context
, TRUE
))
2775 bSetDOSExtendedError
= TRUE
;
2778 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
2782 WIN32_FIND_DATAW dataW
;
2783 WIN32_FIND_DATAA
* dataA
;
2785 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Edx
);
2786 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA
);
2788 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2789 handle
= FindFirstFileW(pathW
, &dataW
);
2791 dataA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
2792 if (handle
!= INVALID_HANDLE_VALUE
&&
2793 (h16
= GlobalAlloc16(GMEM_MOVEABLE
, sizeof(handle
))))
2795 HANDLE
* ptr
= GlobalLock16( h16
);
2797 GlobalUnlock16( h16
);
2798 SET_AX( context
, h16
);
2799 INT21_ConvertFindDataWtoA(dataA
, &dataW
);
2803 if (handle
!= INVALID_HANDLE_VALUE
) FindClose(handle
);
2804 SET_AX( context
, INVALID_HANDLE_VALUE16
);
2805 bSetDOSExtendedError
= TRUE
;
2810 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
2812 HGLOBAL16 h16
= BX_reg(context
);
2814 WIN32_FIND_DATAW dataW
;
2815 WIN32_FIND_DATAA
* dataA
;
2817 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
2820 dataA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
2822 if (h16
!= INVALID_HANDLE_VALUE16
&& (ptr
= GlobalLock16( h16
)))
2824 if (!FindNextFileW(*ptr
, &dataW
)) bSetDOSExtendedError
= TRUE
;
2825 else INT21_ConvertFindDataWtoA(dataA
, &dataW
);
2826 GlobalUnlock16( h16
);
2830 SetLastError( ERROR_INVALID_HANDLE
);
2831 bSetDOSExtendedError
= TRUE
;
2836 case 0x56: /* LONG FILENAME - RENAME FILE */
2837 if (!INT21_RenameFile(context
))
2838 bSetDOSExtendedError
= TRUE
;
2841 case 0x60: /* LONG FILENAME - CONVERT PATH */
2843 WCHAR res
[MAX_PATH
];
2845 switch (CL_reg(context
))
2847 case 0x00: /* "truename" - Canonicalize path */
2849 * FIXME: This is not 100% equal to 0x01 case,
2850 * if you fix this, fix int21 subfunction 0x60, too.
2853 case 0x01: /* Get short filename or path */
2854 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
2855 if (!GetShortPathNameW(pathW
, res
, 67))
2856 bSetDOSExtendedError
= TRUE
;
2859 SET_AX( context
, 0 );
2860 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
2861 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
2866 case 0x02: /* Get canonical long filename or path */
2867 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
2868 if (!GetFullPathNameW(pathW
, 128, res
, NULL
))
2869 bSetDOSExtendedError
= TRUE
;
2872 SET_AX( context
, 0 );
2873 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
2874 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
2879 FIXME("Unimplemented long file name function:\n");
2880 INT_BARF( context
, 0x21 );
2882 SET_AL( context
, 0 );
2888 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
2889 if (!INT21_CreateFile( context
, context
->Esi
, TRUE
,
2890 BX_reg(context
), DL_reg(context
) ))
2891 bSetDOSExtendedError
= TRUE
;
2894 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
2896 DWORD filename_len
, flags
;
2899 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Edx
);
2901 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA
);
2902 SET_AX( context
, 0 );
2903 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2904 if (!GetVolumeInformationW( pathW
, NULL
, 0, NULL
, &filename_len
,
2907 INT_BARF( context
, 0x21 );
2911 SET_BX( context
, flags
| 0x4000 ); /* support for LFN functions */
2912 SET_CX( context
, filename_len
);
2913 SET_DX( context
, MAX_PATH
); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
2914 WideCharToMultiByte(CP_OEMCP
, 0, dstW
, -1,
2915 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
2920 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
2922 HGLOBAL16 h16
= BX_reg(context
);
2925 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
2927 if (h16
!= INVALID_HANDLE_VALUE16
&& (ptr
= GlobalLock16( h16
)))
2929 if (!FindClose( *ptr
)) bSetDOSExtendedError
= TRUE
;
2930 GlobalUnlock16( h16
);
2931 GlobalFree16( h16
);
2935 SetLastError( ERROR_INVALID_HANDLE
);
2936 bSetDOSExtendedError
= TRUE
;
2941 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
2943 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2944 BY_HANDLE_FILE_INFORMATION
*info
=
2945 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2947 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
2949 if (!GetFileInformationByHandle(handle
, info
))
2950 bSetDOSExtendedError
= TRUE
;
2954 case 0xa7: /* LONG FILENAME - CONVERT TIME */
2955 switch (BL_reg(context
))
2957 case 0x00: /* FILE TIME TO DOS TIME */
2960 FILETIME
*filetime
= CTX_SEG_OFF_TO_LIN(context
,
2964 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
2966 FileTimeToDosDateTime( filetime
, &date
, &time
);
2968 SET_DX( context
, date
);
2969 SET_CX( context
, time
);
2972 * FIXME: BH has number of 10-millisecond units
2975 SET_BH( context
, 0 );
2979 case 0x01: /* DOS TIME TO FILE TIME */
2981 FILETIME
*filetime
= CTX_SEG_OFF_TO_LIN(context
,
2985 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
2988 * FIXME: BH has number of 10-millisecond units
2991 DosDateTimeToFileTime( DX_reg(context
), CX_reg(context
),
2997 INT_BARF( context
, 0x21 );
3002 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
3003 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3004 case 0xaa: /* LONG FILENAME - SUBST */
3006 FIXME("Unimplemented long file name function:\n");
3007 INT_BARF( context
, 0x21 );
3009 SET_AL( context
, 0 );
3013 if (bSetDOSExtendedError
)
3015 SET_AX( context
, GetLastError() );
3016 SET_CFLAG( context
);
3021 /***********************************************************************
3026 * - subfunction 0x56 of function 0x71
3027 * - subfunction 0xff of function 0x43 (CL == 0x56)
3029 static BOOL
INT21_RenameFile( CONTEXT
*context
)
3031 WCHAR fromW
[MAX_PATH
];
3032 WCHAR toW
[MAX_PATH
];
3033 char *fromA
= CTX_SEG_OFF_TO_LIN(context
,
3034 context
->SegDs
,context
->Edx
);
3035 char *toA
= CTX_SEG_OFF_TO_LIN(context
,
3036 context
->SegEs
,context
->Edi
);
3038 TRACE( "RENAME FILE %s to %s\n", fromA
, toA
);
3039 MultiByteToWideChar(CP_OEMCP
, 0, fromA
, -1, fromW
, MAX_PATH
);
3040 MultiByteToWideChar(CP_OEMCP
, 0, toA
, -1, toW
, MAX_PATH
);
3042 return MoveFileW( fromW
, toW
);
3046 /***********************************************************************
3052 static BOOL
INT21_NetworkFunc (CONTEXT
*context
)
3054 switch (AL_reg(context
))
3056 case 0x00: /* Get machine name. */
3058 WCHAR dstW
[MAX_COMPUTERNAME_LENGTH
+ 1];
3059 DWORD s
= ARRAY_SIZE(dstW
);
3062 char *dst
= CTX_SEG_OFF_TO_LIN (context
,context
->SegDs
,context
->Edx
);
3063 TRACE("getting machine name to %p\n", dst
);
3064 if (!GetComputerNameW(dstW
, &s
) ||
3065 !WideCharToMultiByte(CP_OEMCP
, 0, dstW
, -1, dst
, 16, NULL
, NULL
))
3068 SetLastError( ER_NoNetwork
);
3071 for (len
= strlen(dst
); len
< 15; len
++) dst
[len
] = ' ';
3073 SET_CH( context
, 1 ); /* Valid */
3074 SET_CL( context
, 1 ); /* NETbios number??? */
3075 TRACE("returning %s\n", debugstr_an(dst
, 16));
3080 SetLastError( ER_NoNetwork
);
3085 /******************************************************************
3086 * INT21_GetDiskSerialNumber
3089 static int INT21_GetDiskSerialNumber( CONTEXT
*context
)
3091 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3092 WCHAR path
[] = {'A',':','\\',0}, label
[11];
3095 path
[0] += INT21_MapDrive(BL_reg(context
));
3096 if (!GetVolumeInformationW( path
, label
, 11, &serial
, NULL
, NULL
, NULL
, 0))
3098 SetLastError( ERROR_INVALID_DRIVE
);
3102 *(WORD
*)dataptr
= 0;
3103 memcpy(dataptr
+ 2, &serial
, sizeof(DWORD
));
3104 WideCharToMultiByte(CP_OEMCP
, 0, label
, 11, (LPSTR
)dataptr
+ 6, 11, NULL
, NULL
);
3105 memcpy(dataptr
+ 17, "FAT16 ", 8);
3110 /******************************************************************
3111 * INT21_SetDiskSerialNumber
3114 static BOOL
INT21_SetDiskSerialNumber( CONTEXT
*context
)
3117 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3118 int drive
= INT21_MapDrive(BL_reg(context
));
3120 if (!is_valid_drive(drive
))
3122 SetLastError( ERROR_INVALID_DRIVE
);
3126 DRIVE_SetSerialNumber( drive
, *(DWORD
*)(dataptr
+ 2) );
3129 FIXME("Setting drive serial number is no longer supported\n");
3130 SetLastError( ERROR_NOT_SUPPORTED
);
3136 /******************************************************************
3137 * INT21_GetFreeDiskSpace
3140 static BOOL
INT21_GetFreeDiskSpace( CONTEXT
*context
)
3142 DWORD cluster_sectors
, sector_bytes
, free_clusters
, total_clusters
;
3143 WCHAR root
[] = {'A',':','\\',0};
3144 const DWORD max_clusters
= 0x3d83;
3145 const DWORD max_sectors_per_cluster
= 0x7f;
3146 const DWORD max_bytes_per_sector
= 0x200;
3148 root
[0] += INT21_MapDrive(DL_reg(context
));
3149 if (!GetDiskFreeSpaceW( root
, &cluster_sectors
, §or_bytes
,
3150 &free_clusters
, &total_clusters
))
3153 /* Some old win31 apps (Lotus SmartSuite 5.1) crap out if there's too
3154 * much disk space, so Windows XP seems to apply the following limits:
3155 * cluster_sectors <= 0x7f
3156 * sector size <= 0x200
3157 * clusters <= 0x3D83
3158 * This means total reported space is limited to about 1GB.
3161 /* Make sure bytes-per-sector is in [, max] */
3162 while (sector_bytes
> max_bytes_per_sector
) {
3164 free_clusters
<<= 1;
3165 total_clusters
<<= 1;
3167 /* Then make sure sectors-per-cluster is in [max/2, max]. */
3168 while (cluster_sectors
<= max_sectors_per_cluster
/2) {
3169 cluster_sectors
<<= 1;
3170 free_clusters
>>= 1;
3171 total_clusters
>>= 1;
3173 while (cluster_sectors
> max_sectors_per_cluster
) {
3174 cluster_sectors
>>= 1;
3175 free_clusters
<<= 1;
3176 total_clusters
<<= 1;
3179 /* scale up sectors_per_cluster to exactly max_sectors_per_cluster.
3180 * We could skip this, but that would impose an artificially low
3181 * limit on reported disk space.
3182 * To avoid overflow, first apply a preliminary cap on sector count;
3183 * this will not affect the correctness of the final result,
3184 * because if the preliminary cap hits, the final one will, too.
3186 if (total_clusters
> 4 * max_clusters
)
3187 total_clusters
= 4 * max_clusters
;
3188 if (free_clusters
> 4 * max_clusters
)
3189 free_clusters
= 4 * max_clusters
;
3190 if (cluster_sectors
< max_sectors_per_cluster
) {
3191 free_clusters
*= cluster_sectors
;
3192 free_clusters
/= max_sectors_per_cluster
;
3193 total_clusters
*= cluster_sectors
;
3194 total_clusters
/= max_sectors_per_cluster
;
3195 cluster_sectors
= max_sectors_per_cluster
;
3198 /* Finally, apply real cluster count cap. */
3199 if (total_clusters
> max_clusters
)
3200 total_clusters
= max_clusters
;
3201 if (free_clusters
> max_clusters
)
3202 free_clusters
= max_clusters
;
3204 SET_AX( context
, cluster_sectors
);
3205 SET_BX( context
, free_clusters
);
3206 SET_CX( context
, sector_bytes
);
3207 SET_DX( context
, total_clusters
);
3211 /******************************************************************
3212 * INT21_GetDriveAllocInfo
3215 static BOOL
INT21_GetDriveAllocInfo( CONTEXT
*context
, BYTE drive
)
3219 drive
= INT21_MapDrive( drive
);
3220 if (!INT21_FillDrivePB( drive
)) return FALSE
;
3221 dpb
= &(INT21_GetHeapPointer()->misc_dpb_list
[drive
]);
3222 SET_AL( context
, dpb
->cluster_sectors
+ 1 );
3223 SET_CX( context
, dpb
->sector_bytes
);
3224 SET_DX( context
, dpb
->num_clusters1
);
3226 context
->SegDs
= INT21_GetHeapSelector( context
);
3227 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
].media_ID
) );
3231 /***********************************************************************
3232 * INT21_GetExtendedError
3234 static void INT21_GetExtendedError( CONTEXT
*context
)
3236 BYTE
class, action
, locus
;
3237 WORD error
= GetLastError();
3242 class = action
= locus
= 0;
3244 case ERROR_DIR_NOT_EMPTY
:
3249 case ERROR_ACCESS_DENIED
:
3250 class = EC_AccessDenied
;
3254 case ERROR_CANNOT_MAKE
:
3255 class = EC_AccessDenied
;
3259 case ERROR_DISK_FULL
:
3260 case ERROR_HANDLE_DISK_FULL
:
3261 class = EC_MediaError
;
3265 case ERROR_FILE_EXISTS
:
3266 case ERROR_ALREADY_EXISTS
:
3271 case ERROR_FILE_NOT_FOUND
:
3272 case ERROR_PATH_NOT_FOUND
:
3273 case ERROR_INVALID_NAME
:
3274 class = EC_NotFound
;
3278 case ERROR_GEN_FAILURE
:
3279 class = EC_SystemFailure
;
3283 case ERROR_INVALID_DRIVE
:
3284 class = EC_MediaError
;
3288 case ERROR_INVALID_HANDLE
:
3289 class = EC_ProgramError
;
3293 case ERROR_LOCK_VIOLATION
:
3294 class = EC_AccessDenied
;
3298 case ERROR_NO_MORE_FILES
:
3299 class = EC_MediaError
;
3304 class = EC_NotFound
;
3308 case ERROR_NOT_ENOUGH_MEMORY
:
3309 class = EC_OutOfResource
;
3314 class = EC_NotFound
;
3318 case ERROR_SHARING_VIOLATION
:
3319 class = EC_Temporary
;
3323 case ERROR_TOO_MANY_OPEN_FILES
:
3324 class = EC_ProgramError
;
3329 FIXME("Unknown error %d\n", error
);
3330 class = EC_SystemFailure
;
3335 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3336 error
, class, action
, locus
);
3337 SET_AX( context
, error
);
3338 SET_BH( context
, class );
3339 SET_BL( context
, action
);
3340 SET_CH( context
, locus
);
3343 static BOOL
INT21_CreateTempFile( CONTEXT
*context
)
3345 static int counter
= 0;
3346 char *name
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3347 char *p
= name
+ strlen(name
);
3349 /* despite what Ralf Brown says, some programs seem to call without
3350 * ending backslash (DOS accepts that, so we accept it too) */
3351 if ((p
== name
) || (p
[-1] != '\\')) *p
++ = '\\';
3355 sprintf( p
, "wine%04x.%03d", (int)getpid(), counter
);
3356 counter
= (counter
+ 1) % 1000;
3359 Win32HandleToDosFileHandle(
3360 CreateFileA( name
, GENERIC_READ
| GENERIC_WRITE
,
3361 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
3362 CREATE_NEW
, 0, 0 ) ) );
3363 if (AX_reg(context
) != HFILE_ERROR16
)
3365 TRACE("created %s\n", name
);
3368 if (GetLastError() != ERROR_FILE_EXISTS
) return FALSE
;
3372 /***********************************************************************
3373 * DOSFS_ToDosFCBFormat
3375 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3376 * expanding wild cards and converting to upper-case in the process.
3377 * File name can be terminated by '\0', '\\' or '/'.
3378 * Return FALSE if the name is not a valid DOS name.
3379 * 'buffer' must be at least 12 characters long.
3381 /* Chars we don't want to see in DOS file names */
3382 static BOOL
INT21_ToDosFCBFormat( LPCWSTR name
, LPWSTR buffer
)
3384 static const WCHAR invalid_chars
[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3388 /* Check for "." and ".." */
3393 for(i
= 1; i
< 11; i
++) buffer
[i
] = ' ';
3400 return (!*p
|| (*p
== '/') || (*p
== '\\'));
3403 for (i
= 0; i
< 8; i
++)
3420 if (strchrW( invalid_chars
, *p
)) return FALSE
;
3421 buffer
[i
] = toupperW(*p
);
3429 /* Skip all chars after wildcard up to first dot */
3430 while (*p
&& (*p
!= '/') && (*p
!= '\\') && (*p
!= '.')) p
++;
3434 /* Check if name too long */
3435 if (*p
&& (*p
!= '/') && (*p
!= '\\') && (*p
!= '.')) return FALSE
;
3437 if (*p
== '.') p
++; /* Skip dot */
3439 for (i
= 8; i
< 11; i
++)
3449 return FALSE
; /* Second extension not allowed */
3457 if (strchrW( invalid_chars
, *p
)) return FALSE
;
3458 buffer
[i
] = toupperW(*p
);
3465 /* at most 3 character of the extension are processed
3466 * is something behind this ?
3468 while (*p
== '*' || *p
== ' ') p
++; /* skip wildcards and spaces */
3469 return (!*p
|| (*p
== '/') || (*p
== '\\'));
3472 static HANDLE INT21_FindHandle
;
3473 static const WCHAR
*INT21_FindPath
; /* will point to current dta->fullPath search */
3475 /******************************************************************
3478 static BOOL
INT21_FindFirst( CONTEXT
*context
)
3482 FINDFILE_DTA
*dta
= (FINDFILE_DTA
*)INT21_GetCurrentDTA(context
);
3483 WCHAR maskW
[12], pathW
[MAX_PATH
];
3484 static const WCHAR wildcardW
[] = {'*','.','*',0};
3486 path
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3487 MultiByteToWideChar(CP_OEMCP
, 0, path
, -1, pathW
, MAX_PATH
);
3489 p
= strrchrW( pathW
, '\\');
3490 q
= strrchrW( pathW
, '/');
3494 if (pathW
[0] && pathW
[1] == ':') p
= pathW
+ 2;
3499 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3500 * (doesn't matter as it is set below anyway)
3502 if (!INT21_ToDosFCBFormat( p
, maskW
))
3504 SetLastError( ERROR_FILE_NOT_FOUND
);
3505 SET_AX( context
, ERROR_FILE_NOT_FOUND
);
3509 WideCharToMultiByte(CP_OEMCP
, 0, maskW
, 12, dta
->mask
, sizeof(dta
->mask
), NULL
, NULL
);
3511 dta
->fullPath
= HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW
) + (p
- pathW
)*sizeof(WCHAR
) );
3512 memcpy( dta
->fullPath
, pathW
, (p
- pathW
) * sizeof(WCHAR
) );
3513 memcpy( dta
->fullPath
+ (p
- pathW
), wildcardW
, sizeof(wildcardW
) );
3514 /* we must have a fully qualified file name in dta->fullPath
3515 * (we could have a UNC path, but this would lead to some errors later on)
3517 dta
->drive
= toupperW(dta
->fullPath
[0]) - 'A';
3519 dta
->search_attr
= CL_reg(context
);
3523 /******************************************************************
3526 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3528 static BOOL
match_short(LPCWSTR shortW
, LPCSTR maskA
)
3530 WCHAR mask
[11], file
[12];
3533 if (!INT21_ToDosFCBFormat( shortW
, file
)) return FALSE
;
3534 MultiByteToWideChar(CP_OEMCP
, 0, maskA
, 11, mask
, 11);
3535 for (i
= 0; i
< 11; i
++)
3536 if (mask
[i
] != '?' && mask
[i
] != file
[i
]) return FALSE
;
3540 static unsigned INT21_FindHelper(LPCWSTR fullPath
, unsigned drive
, unsigned count
,
3541 LPCSTR mask
, unsigned search_attr
,
3542 WIN32_FIND_DATAW
* entry
)
3546 if ((search_attr
& ~(FA_UNUSED
| FA_ARCHIVE
| FA_RDONLY
)) == FA_LABEL
)
3548 WCHAR path
[] = {' ',':','\\',0};
3550 if (count
) return 0;
3551 path
[0] = drive
+ 'A';
3552 if (!GetVolumeInformationW(path
, entry
->cAlternateFileName
, 13, NULL
, NULL
, NULL
, NULL
, 0)) return 0;
3553 if (!entry
->cAlternateFileName
[0]) return 0;
3554 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftCreationTime
);
3555 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftLastAccessTime
);
3556 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftLastWriteTime
);
3557 entry
->dwFileAttributes
= FA_LABEL
;
3558 entry
->nFileSizeHigh
= entry
->nFileSizeLow
= 0;
3559 TRACE("returning %s as label\n", debugstr_w(entry
->cAlternateFileName
));
3563 if (!INT21_FindHandle
|| INT21_FindPath
!= fullPath
|| count
== 0)
3565 if (INT21_FindHandle
) FindClose(INT21_FindHandle
);
3566 INT21_FindHandle
= FindFirstFileW(fullPath
, entry
);
3567 if (INT21_FindHandle
== INVALID_HANDLE_VALUE
)
3569 INT21_FindHandle
= 0;
3572 INT21_FindPath
= fullPath
;
3573 /* we need to resync search */
3578 while (ncalls
-- != 0)
3580 if (!FindNextFileW(INT21_FindHandle
, entry
))
3582 FindClose(INT21_FindHandle
); INT21_FindHandle
= 0;
3586 while (count
< 0xffff)
3589 /* Check the file attributes, and path */
3590 if (!(entry
->dwFileAttributes
& ~search_attr
) &&
3591 match_short(entry
->cAlternateFileName
[0] ? entry
->cAlternateFileName
: entry
->cFileName
,
3596 if (!FindNextFileW(INT21_FindHandle
, entry
))
3598 FindClose(INT21_FindHandle
); INT21_FindHandle
= 0;
3602 WARN("Too many directory entries in %s\n", debugstr_w(fullPath
) );
3606 /******************************************************************
3609 static BOOL
INT21_FindNext( CONTEXT
*context
)
3611 FINDFILE_DTA
*dta
= (FINDFILE_DTA
*)INT21_GetCurrentDTA(context
);
3612 DWORD attr
= dta
->search_attr
| FA_UNUSED
| FA_ARCHIVE
| FA_RDONLY
;
3613 WIN32_FIND_DATAW entry
;
3616 if (!dta
->fullPath
) return FALSE
;
3618 n
= INT21_FindHelper(dta
->fullPath
, dta
->drive
, dta
->count
,
3619 dta
->mask
, attr
, &entry
);
3622 dta
->fileattr
= entry
.dwFileAttributes
;
3623 dta
->filesize
= entry
.nFileSizeLow
;
3624 FileTimeToDosDateTime( &entry
.ftLastWriteTime
, &dta
->filedate
, &dta
->filetime
);
3625 if (entry
.cAlternateFileName
[0])
3626 WideCharToMultiByte(CP_OEMCP
, 0, entry
.cAlternateFileName
, -1,
3627 dta
->filename
, 13, NULL
, NULL
);
3629 WideCharToMultiByte(CP_OEMCP
, 0, entry
.cFileName
, -1, dta
->filename
, 13, NULL
, NULL
);
3631 if (!memchr(dta
->mask
,'?',11))
3633 /* wildcardless search, release resources in case no findnext will
3634 * be issued, and as a workaround in case file creation messes up
3635 * findnext, as sometimes happens with pkunzip
3637 HeapFree( GetProcessHeap(), 0, dta
->fullPath
);
3638 INT21_FindPath
= dta
->fullPath
= NULL
;
3643 HeapFree( GetProcessHeap(), 0, dta
->fullPath
);
3644 INT21_FindPath
= dta
->fullPath
= NULL
;
3648 /* microsoft's programmers should be shot for using CP/M style int21
3649 calls in Windows for Workgroup's winfile.exe */
3651 /******************************************************************
3652 * INT21_FindFirstFCB
3655 static BOOL
INT21_FindFirstFCB( CONTEXT
*context
)
3657 BYTE
*fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3660 WCHAR p
[] = {' ',':',};
3662 if (*fcb
== 0xff) pFCB
= (FINDFILE_FCB
*)(fcb
+ 7);
3663 else pFCB
= (FINDFILE_FCB
*)fcb
;
3664 drive
= INT21_MapDrive( pFCB
->drive
);
3665 if (drive
== MAX_DOS_DRIVES
) return FALSE
;
3668 pFCB
->fullPath
= HeapAlloc(GetProcessHeap(), 0, MAX_PATH
* sizeof(WCHAR
));
3669 if (!pFCB
->fullPath
) return FALSE
;
3670 GetLongPathNameW(p
, pFCB
->fullPath
, MAX_PATH
);
3675 /******************************************************************
3679 static BOOL
INT21_FindNextFCB( CONTEXT
*context
)
3681 BYTE
*fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3683 LPBYTE pResult
= INT21_GetCurrentDTA(context
);
3684 DOS_DIRENTRY_LAYOUT
*ddl
;
3685 WIN32_FIND_DATAW entry
;
3690 if (*fcb
== 0xff) /* extended FCB ? */
3693 pFCB
= (FINDFILE_FCB
*)(fcb
+ 7);
3698 pFCB
= (FINDFILE_FCB
*)fcb
;
3701 if (!pFCB
->fullPath
) return FALSE
;
3702 n
= INT21_FindHelper(pFCB
->fullPath
, INT21_MapDrive( pFCB
->drive
),
3703 pFCB
->count
, pFCB
->filename
, attr
, &entry
);
3706 HeapFree( GetProcessHeap(), 0, pFCB
->fullPath
);
3707 INT21_FindPath
= pFCB
->fullPath
= NULL
;
3714 /* place extended FCB header before pResult if called with extended FCB */
3716 pResult
+= 6; /* leave reserved field behind */
3717 *pResult
++ = entry
.dwFileAttributes
;
3719 *pResult
++ = INT21_MapDrive( pFCB
->drive
); /* DOS_DIRENTRY_LAYOUT after current drive number */
3720 ddl
= (DOS_DIRENTRY_LAYOUT
*)pResult
;
3721 ddl
->fileattr
= entry
.dwFileAttributes
;
3722 ddl
->cluster
= 0; /* what else? */
3723 ddl
->filesize
= entry
.nFileSizeLow
;
3724 memset( ddl
->reserved
, 0, sizeof(ddl
->reserved
) );
3725 FileTimeToDosDateTime( &entry
.ftLastWriteTime
,
3726 &ddl
->filedate
, &ddl
->filetime
);
3728 /* Convert file name to FCB format */
3729 if (entry
.cAlternateFileName
[0])
3730 INT21_ToDosFCBFormat( entry
.cAlternateFileName
, nameW
);
3732 INT21_ToDosFCBFormat( entry
.cFileName
, nameW
);
3733 WideCharToMultiByte(CP_OEMCP
, 0, nameW
, 11, ddl
->filename
, 11, NULL
, NULL
);
3738 /******************************************************************
3739 * INT21_ParseFileNameIntoFCB
3742 static void INT21_ParseFileNameIntoFCB( CONTEXT
*context
)
3745 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
);
3747 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
3751 INT buffer_len
, len
;
3753 SET_AL( context
, 0xff ); /* failed */
3755 TRACE("filename: '%s'\n", filename
);
3758 while (*s
&& (*s
!= ' ') && (*s
!= '\r') && (*s
!= '\n'))
3762 buffer_len
= MultiByteToWideChar(CP_OEMCP
, 0, filename
, len
, NULL
, 0);
3763 buffer
= HeapAlloc( GetProcessHeap(), 0, (buffer_len
+ 1) * sizeof(WCHAR
));
3764 len
= MultiByteToWideChar(CP_OEMCP
, 0, filename
, len
, buffer
, buffer_len
);
3766 INT21_ToDosFCBFormat(buffer
, fcbW
);
3767 HeapFree(GetProcessHeap(), 0, buffer
);
3768 WideCharToMultiByte(CP_OEMCP
, 0, fcbW
, 12, fcb
+ 1, 12, NULL
, NULL
);
3770 TRACE("FCB: '%s'\n", fcb
+ 1);
3772 SET_AL( context
, ((strchr(filename
, '*')) || (strchr(filename
, '$'))) != 0 );
3774 /* point DS:SI to first unparsed character */
3775 SET_SI( context
, context
->Esi
+ (int)s
- (int)filename
);
3778 static BOOL
INT21_Dup2(HFILE16 hFile1
, HFILE16 hFile2
)
3780 HFILE16 res
= HFILE_ERROR16
;
3781 HANDLE handle
, new_handle
;
3782 #define DOS_TABLE_SIZE 256
3783 DWORD map
[DOS_TABLE_SIZE
/ 32];
3786 handle
= DosFileHandleToWin32Handle(hFile1
);
3787 if (handle
== INVALID_HANDLE_VALUE
)
3791 /* now loop to allocate the same one... */
3792 memset(map
, 0, sizeof(map
));
3793 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
3795 if (!DuplicateHandle(GetCurrentProcess(), handle
,
3796 GetCurrentProcess(), &new_handle
,
3797 0, FALSE
, DUPLICATE_SAME_ACCESS
))
3799 res
= HFILE_ERROR16
;
3802 res
= Win32HandleToDosFileHandle(new_handle
);
3803 if (res
== HFILE_ERROR16
|| res
== hFile2
) break;
3804 map
[res
/ 32] |= 1 << (res
% 32);
3806 /* clean up the allocated slots */
3807 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
3809 if (map
[i
/ 32] & (1 << (i
% 32)))
3810 _lclose16((HFILE16
)i
);
3812 return res
== hFile2
;
3816 /***********************************************************************
3817 * DOSVM_Int21Handler
3819 * Interrupt 0x21 handler.
3821 void WINAPI
DOSVM_Int21Handler( CONTEXT
*context
)
3823 BOOL bSetDOSExtendedError
= FALSE
;
3825 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
3826 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
3827 AX_reg(context
), BX_reg(context
),
3828 CX_reg(context
), DX_reg(context
),
3829 SI_reg(context
), DI_reg(context
),
3830 (WORD
)context
->SegDs
, (WORD
)context
->SegEs
,
3834 * Extended error is used by (at least) functions 0x2f to 0x62.
3835 * Function 0x59 returns extended error and error should not
3836 * be cleared before handling the function.
3838 if (AH_reg(context
) >= 0x2f && AH_reg(context
) != 0x59)
3841 RESET_CFLAG(context
); /* Not sure if this is a good idea. */
3843 switch(AH_reg(context
))
3845 case 0x00: /* TERMINATE PROGRAM */
3846 TRACE("TERMINATE PROGRAM\n");
3850 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
3851 TRACE("DIRECT CHARACTER INPUT WITH ECHO - not supported\n");
3854 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
3855 TRACE("Write Character to Standard Output\n");
3858 case 0x03: /* READ CHARACTER FROM STDAUX */
3859 case 0x04: /* WRITE CHARACTER TO STDAUX */
3860 case 0x05: /* WRITE CHARACTER TO PRINTER */
3861 INT_BARF( context
, 0x21 );
3864 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
3865 if (DL_reg(context
) == 0xff)
3867 TRACE("Direct Console Input\n");
3869 /* no character available */
3870 SET_AL( context
, 0 );
3871 SET_ZFLAG( context
);
3875 TRACE("Direct Console Output\n");
3877 * At least DOS versions 2.1-7.0 return character
3878 * that was written in AL register.
3880 SET_AL( context
, DL_reg(context
) );
3884 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
3885 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO - not supported\n");
3886 SET_AL( context
, 0 );
3889 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
3890 TRACE("CHARACTER INPUT WITHOUT ECHO - not supported\n");
3891 SET_AL( context
, 0 );
3894 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
3895 TRACE("WRITE '$'-terminated string from %04X:%04X to stdout\n",
3896 context
->SegDs
, DX_reg(context
) );
3898 LPSTR data
= CTX_SEG_OFF_TO_LIN( context
,
3899 context
->SegDs
, context
->Edx
);
3903 * Do NOT use strchr() to calculate the string length,
3904 * as '\0' is valid string content, too!
3905 * Maybe we should check for non-'$' strings, but DOS doesn't.
3907 while (*p
!= '$') p
++;
3909 WriteFile( DosFileHandleToWin32Handle(1), data
, p
- data
, &w
, NULL
);
3910 SET_AL( context
, '$' ); /* yes, '$' (0x24) gets returned in AL */
3914 case 0x0a: /* BUFFERED INPUT */
3915 TRACE( "BUFFERED INPUT - not supported\n" );
3918 case 0x0b: /* GET STDIN STATUS */
3919 TRACE( "GET STDIN STATUS - not supported\n" );
3920 SET_AL( context
, 0 ); /* no character available */
3923 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
3925 BYTE al
= AL_reg(context
); /* Input function to execute after flush. */
3927 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al
);
3929 /* FIXME: buffers are not flushed */
3932 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
3933 * int21 function identified by AL will be called.
3935 if(al
== 0x01 || al
== 0x06 || al
== 0x07 || al
== 0x08 || al
== 0x0a)
3937 SET_AH( context
, al
);
3938 DOSVM_Int21Handler( context
);
3943 case 0x0d: /* DISK BUFFER FLUSH */
3944 TRACE("DISK BUFFER FLUSH ignored\n");
3947 case 0x0e: /* SELECT DEFAULT DRIVE */
3948 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context
) );
3949 INT21_SetCurrentDrive( DL_reg(context
) );
3950 SET_AL( context
, MAX_DOS_DRIVES
);
3953 case 0x0f: /* OPEN FILE USING FCB */
3954 INT21_OpenFileUsingFCB( context
);
3957 case 0x10: /* CLOSE FILE USING FCB */
3958 INT21_CloseFileUsingFCB( context
);
3961 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
3962 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
3963 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
));
3964 if (!INT21_FindFirstFCB(context
))
3966 SET_AL( context
, 0xff );
3969 /* else fall through */
3971 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
3972 SET_AL( context
, INT21_FindNextFCB(context
) ? 0x00 : 0xff );
3975 case 0x13: /* DELETE FILE USING FCB */
3976 INT_BARF( context
, 0x21 );
3979 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
3980 INT21_SequentialReadFromFCB( context
);
3983 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
3984 INT21_SequentialWriteToFCB( context
);
3987 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
3988 case 0x17: /* RENAME FILE USING FCB */
3989 INT_BARF( context
, 0x21 );
3992 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
3993 SET_AL( context
, 0 );
3996 case 0x19: /* GET CURRENT DEFAULT DRIVE */
3997 SET_AL( context
, INT21_GetCurrentDrive() );
3998 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context
) );
4001 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
4002 TRACE( "SET DISK TRANSFER AREA ADDRESS %04X:%04X\n",
4003 context
->SegDs
, DX_reg(context
) );
4005 TDB
*task
= GlobalLock16( GetCurrentTask() );
4006 task
->dta
= MAKESEGPTR( context
->SegDs
, DX_reg(context
) );
4010 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4011 if (!INT21_GetDriveAllocInfo(context
, 0))
4012 SET_AX( context
, 0xffff );
4015 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4016 if (!INT21_GetDriveAllocInfo(context
, DL_reg(context
)))
4017 SET_AX( context
, 0xffff );
4020 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4021 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4022 SET_AL( context
, 0 );
4025 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4027 BYTE drive
= INT21_MapDrive( 0 );
4028 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4030 if (INT21_FillDrivePB( drive
))
4032 SET_AL( context
, 0x00 ); /* success */
4033 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
] ) );
4034 context
->SegDs
= INT21_GetHeapSelector( context
);
4038 SET_AL( context
, 0xff ); /* invalid or network drive */
4043 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4044 SET_AL( context
, 0 );
4047 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4048 INT21_ReadRandomRecordFromFCB( context
);
4051 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4052 INT21_WriteRandomRecordToFCB( context
);
4055 case 0x23: /* GET FILE SIZE FOR FCB */
4056 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4057 INT_BARF( context
, 0x21 );
4060 case 0x25: /* SET INTERRUPT VECTOR */
4061 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context
));
4063 FARPROC16 ptr
= (FARPROC16
)MAKESEGPTR( context
->SegDs
, DX_reg(context
) );
4064 DOSVM_SetPMHandler16( AL_reg(context
), ptr
);
4068 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4069 INT_BARF( context
, 0x21 );
4072 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4073 INT21_RandomBlockReadFromFCB( context
);
4076 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4077 INT21_RandomBlockWriteToFCB( context
);
4080 case 0x29: /* PARSE FILENAME INTO FCB */
4081 INT21_ParseFileNameIntoFCB(context
);
4084 case 0x2a: /* GET SYSTEM DATE */
4085 TRACE( "GET SYSTEM DATE\n" );
4088 GetLocalTime( &systime
);
4089 SET_CX( context
, systime
.wYear
);
4090 SET_DH( context
, systime
.wMonth
);
4091 SET_DL( context
, systime
.wDay
);
4092 SET_AL( context
, systime
.wDayOfWeek
);
4096 case 0x2b: /* SET SYSTEM DATE */
4097 TRACE( "SET SYSTEM DATE\n" );
4099 WORD year
= CX_reg(context
);
4100 BYTE month
= DH_reg(context
);
4101 BYTE day
= DL_reg(context
);
4103 if (year
>= 1980 && year
<= 2099 &&
4104 month
>= 1 && month
<= 12 &&
4105 day
>= 1 && day
<= 31)
4107 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4109 SET_AL( context
, 0 ); /* Let's pretend we succeeded */
4113 SET_AL( context
, 0xff ); /* invalid date */
4114 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4120 case 0x2c: /* GET SYSTEM TIME */
4121 TRACE( "GET SYSTEM TIME\n" );
4124 GetLocalTime( &systime
);
4125 SET_CH( context
, systime
.wHour
);
4126 SET_CL( context
, systime
.wMinute
);
4127 SET_DH( context
, systime
.wSecond
);
4128 SET_DL( context
, systime
.wMilliseconds
/ 10 );
4132 case 0x2d: /* SET SYSTEM TIME */
4133 if( CH_reg(context
) >= 24 || CL_reg(context
) >= 60 || DH_reg(context
) >= 60 || DL_reg(context
) >= 100 ) {
4134 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4135 CH_reg(context
), CL_reg(context
),
4136 DH_reg(context
), DL_reg(context
) );
4137 SET_AL( context
, 0xFF );
4141 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4142 CH_reg(context
), CL_reg(context
),
4143 DH_reg(context
), DL_reg(context
) );
4144 SET_AL( context
, 0 ); /* Let's pretend we succeeded */
4148 case 0x2e: /* SET VERIFY FLAG */
4149 TRACE("SET VERIFY FLAG ignored\n");
4150 /* we cannot change the behaviour anyway, so just ignore it */
4153 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4154 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4156 TDB
*task
= GlobalLock16( GetCurrentTask() );
4157 context
->SegEs
= SELECTOROF( task
->dta
);
4158 SET_BX( context
, OFFSETOF( task
->dta
) );
4162 case 0x30: /* GET DOS VERSION */
4163 TRACE( "GET DOS VERSION - %s requested\n",
4164 (AL_reg(context
) == 0x00) ? "OEM number" : "version flag" );
4166 if (AL_reg(context
) == 0x00)
4167 SET_BH( context
, 0xff ); /* OEM number => undefined */
4169 SET_BH( context
, 0x08 ); /* version flag => DOS is in ROM */
4171 SET_AL( context
, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4172 SET_AH( context
, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4174 SET_BL( context
, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4175 SET_CX( context
, 0x3456 );
4178 case 0x31: /* TERMINATE AND STAY RESIDENT */
4179 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4182 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4184 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
4185 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context
) );
4187 if (INT21_FillDrivePB( drive
))
4189 SET_AL( context
, 0x00 ); /* success */
4190 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
] ) );
4191 context
->SegDs
= INT21_GetHeapSelector( context
);
4195 SET_AL( context
, 0xff ); /* invalid or network drive */
4200 case 0x33: /* MULTIPLEXED */
4201 switch (AL_reg(context
))
4203 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4204 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4205 SET_DL( context
, brk_flag
);
4208 case 0x01: /* SET EXTENDED BREAK STATE */
4209 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4210 brk_flag
= (DL_reg(context
) > 0) ? 1 : 0;
4213 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4214 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4215 /* ugly coding in order to stay reentrant */
4216 if (DL_reg(context
))
4218 SET_DL( context
, brk_flag
);
4223 SET_DL( context
, brk_flag
);
4228 case 0x05: /* GET BOOT DRIVE */
4229 TRACE("GET BOOT DRIVE\n");
4230 SET_DL( context
, 3 );
4231 /* c: is Wine's bootdrive (a: is 1)*/
4234 case 0x06: /* GET TRUE VERSION NUMBER */
4235 TRACE("GET TRUE VERSION NUMBER\n");
4236 SET_BL( context
, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4237 SET_BH( context
, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4238 SET_DL( context
, 0x00 ); /* revision */
4239 SET_DH( context
, 0x08 ); /* DOS is in ROM */
4243 INT_BARF( context
, 0x21 );
4248 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4249 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4250 context
->SegEs
= INT21_GetHeapSelector( context
);
4251 SET_BX( context
, offsetof(INT21_HEAP
, misc_indos
) );
4254 case 0x35: /* GET INTERRUPT VECTOR */
4255 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context
));
4257 FARPROC16 addr
= DOSVM_GetPMHandler16( AL_reg(context
) );
4258 context
->SegEs
= SELECTOROF(addr
);
4259 SET_BX( context
, OFFSETOF(addr
) );
4263 case 0x36: /* GET FREE DISK SPACE */
4264 TRACE("GET FREE DISK SPACE FOR DRIVE %s (limited to about 1GB)\n",
4265 INT21_DriveName( DL_reg(context
) ));
4266 if (!INT21_GetFreeDiskSpace(context
)) SET_AX( context
, 0xffff );
4269 case 0x37: /* SWITCHAR */
4271 switch (AL_reg(context
))
4273 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4274 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4275 SET_AL( context
, 0x00 ); /* success*/
4276 SET_DL( context
, '/' );
4278 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4279 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4281 SET_AL( context
, 0x00 ); /* success*/
4284 INT_BARF( context
, 0x21 );
4290 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4291 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4292 if (AL_reg(context
))
4294 WORD country
= AL_reg(context
);
4295 if (country
== 0xff)
4296 country
= BX_reg(context
);
4297 if (country
!= INT21_GetSystemCountryCode()) {
4298 FIXME( "Requested info on non-default country %04x\n", country
);
4303 if(AX_reg(context
) != 2 )
4305 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context
,
4308 SET_AX( context
, INT21_GetSystemCountryCode() );
4309 SET_BX( context
, INT21_GetSystemCountryCode() );
4310 RESET_CFLAG(context
);
4314 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4315 if (!INT21_CreateDirectory( context
))
4316 bSetDOSExtendedError
= TRUE
;
4318 RESET_CFLAG(context
);
4321 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4323 WCHAR dirW
[MAX_PATH
];
4324 char *dirA
= CTX_SEG_OFF_TO_LIN(context
,
4325 context
->SegDs
, context
->Edx
);
4327 TRACE( "REMOVE DIRECTORY %s\n", dirA
);
4329 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
4331 if (!RemoveDirectoryW( dirW
))
4332 bSetDOSExtendedError
= TRUE
;
4334 RESET_CFLAG(context
);
4338 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4339 if (!INT21_SetCurrentDirectory( context
))
4340 bSetDOSExtendedError
= TRUE
;
4342 RESET_CFLAG(context
);
4345 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4346 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4347 OF_READWRITE
| OF_SHARE_COMPAT
, 0x12 ))
4348 bSetDOSExtendedError
= TRUE
;
4350 RESET_CFLAG(context
);
4353 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4354 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4355 AL_reg(context
), 0x01 ))
4356 bSetDOSExtendedError
= TRUE
;
4358 RESET_CFLAG(context
);
4361 case 0x3e: /* "CLOSE" - CLOSE FILE */
4362 TRACE( "CLOSE handle %d\n", BX_reg(context
) );
4363 if (_lclose16( BX_reg(context
) ) == HFILE_ERROR16
)
4364 bSetDOSExtendedError
= TRUE
;
4366 RESET_CFLAG(context
);
4369 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4370 TRACE( "READ from %d to %04X:%04X for %d bytes\n",
4377 WORD count
= CX_reg(context
);
4378 BYTE
*buffer
= CTX_SEG_OFF_TO_LIN( context
,
4382 /* Some programs pass a count larger than the allocated buffer */
4383 DWORD maxcount
= GetSelectorLimit16( context
->SegDs
) - DX_reg(context
) + 1;
4384 if (count
> maxcount
) count
= maxcount
;
4387 * FIXME: Reading from console (BX=1) in DOS mode
4388 * does not work as it is supposed to work.
4391 RESET_CFLAG(context
); /* set if error */
4392 if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context
)), buffer
, count
, &result
, NULL
))
4393 SET_AX( context
, (WORD
)result
);
4395 bSetDOSExtendedError
= TRUE
;
4399 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4400 TRACE( "WRITE from %04X:%04X to handle %d for %d byte\n",
4401 context
->SegDs
, DX_reg(context
),
4402 BX_reg(context
), CX_reg(context
) );
4404 char *ptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
4405 HFILE handle
= (HFILE
)DosFileHandleToWin32Handle(BX_reg(context
));
4406 LONG result
= _hwrite( handle
, ptr
, CX_reg(context
) );
4407 if (result
== HFILE_ERROR
)
4408 bSetDOSExtendedError
= TRUE
;
4411 SET_AX( context
, (WORD
)result
);
4412 RESET_CFLAG(context
);
4417 case 0x41: /* "UNLINK" - DELETE FILE */
4419 WCHAR fileW
[MAX_PATH
];
4420 char *fileA
= CTX_SEG_OFF_TO_LIN(context
,
4424 TRACE( "UNLINK %s\n", fileA
);
4425 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
4427 if (!DeleteFileW( fileW
))
4428 bSetDOSExtendedError
= TRUE
;
4430 RESET_CFLAG(context
);
4434 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4435 TRACE( "LSEEK handle %d offset %d from %s\n",
4437 MAKELONG( DX_reg(context
), CX_reg(context
) ),
4438 (AL_reg(context
) == 0) ?
4439 "start of file" : ((AL_reg(context
) == 1) ?
4440 "current file position" : "end of file") );
4442 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
4443 LONG offset
= MAKELONG( DX_reg(context
), CX_reg(context
) );
4444 DWORD status
= SetFilePointer( handle
, offset
,
4445 NULL
, AL_reg(context
) );
4446 if (status
== INVALID_SET_FILE_POINTER
)
4447 bSetDOSExtendedError
= TRUE
;
4450 SET_AX( context
, LOWORD(status
) );
4451 SET_DX( context
, HIWORD(status
) );
4452 RESET_CFLAG(context
);
4457 case 0x43: /* FILE ATTRIBUTES */
4458 if (!INT21_FileAttributes( context
, AL_reg(context
), FALSE
))
4459 bSetDOSExtendedError
= TRUE
;
4461 RESET_CFLAG(context
);
4464 case 0x44: /* IOCTL */
4465 INT21_Ioctl( context
);
4468 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4469 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context
) );
4472 HFILE handle16
= HFILE_ERROR
;
4474 if (DuplicateHandle( GetCurrentProcess(),
4475 DosFileHandleToWin32Handle(BX_reg(context
)),
4476 GetCurrentProcess(),
4478 0, TRUE
, DUPLICATE_SAME_ACCESS
))
4479 handle16
= Win32HandleToDosFileHandle(handle32
);
4481 if (handle16
== HFILE_ERROR
)
4482 bSetDOSExtendedError
= TRUE
;
4485 SET_AX( context
, handle16
);
4486 RESET_CFLAG(context
);
4491 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4492 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4493 BX_reg(context
), CX_reg(context
) );
4494 if (!INT21_Dup2(BX_reg(context
), CX_reg(context
)))
4495 bSetDOSExtendedError
= TRUE
;
4497 RESET_CFLAG(context
);
4500 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4501 if (!INT21_GetCurrentDirectory( context
, FALSE
))
4502 bSetDOSExtendedError
= TRUE
;
4504 RESET_CFLAG(context
);
4507 case 0x48: /* ALLOCATE MEMORY */
4508 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context
) );
4510 DWORD bytes
= (DWORD
)BX_reg(context
) << 4;
4511 DWORD rv
= GlobalDOSAlloc16( bytes
);
4512 WORD selector
= LOWORD( rv
);
4516 SET_AX( context
, selector
);
4517 RESET_CFLAG(context
);
4522 SET_AX( context
, 0x0008 ); /* insufficient memory */
4523 SET_BX( context
, DOSMEM_Available() >> 4 );
4528 case 0x49: /* FREE MEMORY */
4529 TRACE( "FREE MEMORY segment %04X\n", context
->SegEs
);
4531 BOOL ok
= !GlobalDOSFree16( context
->SegEs
);
4533 /* If we don't reset ES_reg, we will fail in the relay code */
4534 if (ok
) context
->SegEs
= 0;
4537 TRACE("FREE MEMORY failed\n");
4539 SET_AX( context
, 0x0009 ); /* memory block address invalid */
4544 case 0x4a: /* RESIZE MEMORY BLOCK */
4545 TRACE( "RESIZE MEMORY segment %04X to %d paragraphs\n",
4546 context
->SegEs
, BX_reg(context
) );
4548 FIXME( "Resize memory block - unsupported under Win16\n" );
4553 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4555 char *program
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
4556 HINSTANCE16 instance
;
4558 TRACE( "EXEC %s\n", program
);
4560 RESET_CFLAG(context
);
4561 instance
= WinExec16( program
, SW_NORMAL
);
4564 SET_CFLAG( context
);
4565 SET_AX( context
, instance
);
4570 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
4571 TRACE( "EXIT with return code %d\n", AL_reg(context
) );
4572 DOSVM_Exit( AL_reg(context
) );
4575 case 0x4d: /* GET RETURN CODE */
4576 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
4577 SET_AX( context
, 0 );
4580 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
4581 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context
),
4582 (LPCSTR
)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
));
4583 if (!INT21_FindFirst(context
)) break;
4586 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
4587 TRACE("FINDNEXT\n");
4588 if (!INT21_FindNext(context
))
4590 SetLastError( ERROR_NO_MORE_FILES
);
4591 SET_AX( context
, ERROR_NO_MORE_FILES
);
4594 else SET_AX( context
, 0 ); /* OK */
4597 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
4598 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
4599 DOSVM_psp
= BX_reg(context
);
4602 case 0x51: /* GET PSP ADDRESS */
4603 INT21_GetPSP( context
);
4606 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
4607 TRACE("Get List of Lists - not supported\n");
4609 SET_BX( context
, 0 );
4612 case 0x54: /* Get Verify Flag */
4613 TRACE("Get Verify Flag - Not Supported\n");
4614 SET_AL( context
, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
4617 case 0x56: /* "RENAME" - RENAME FILE */
4618 if (!INT21_RenameFile( context
))
4619 bSetDOSExtendedError
= TRUE
;
4621 RESET_CFLAG(context
);
4624 case 0x57: /* FILE DATE AND TIME */
4625 if (!INT21_FileDateTime( context
))
4626 bSetDOSExtendedError
= TRUE
;
4628 RESET_CFLAG(context
);
4631 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
4632 switch (AL_reg(context
))
4634 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
4635 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
4636 SET_AX( context
, 0 ); /* low memory first fit */
4639 case 0x01: /* SET ALLOCATION STRATEGY */
4640 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
4644 case 0x02: /* GET UMB LINK STATE */
4645 TRACE( "GET UMB LINK STATE\n" );
4646 SET_AL( context
, 0 ); /* UMBs not part of DOS memory chain */
4649 case 0x03: /* SET UMB LINK STATE */
4650 TRACE( "SET UMB LINK STATE to %d - ignored\n",
4655 INT_BARF( context
, 0x21 );
4659 case 0x59: /* GET EXTENDED ERROR INFO */
4660 INT21_GetExtendedError( context
);
4663 case 0x5a: /* CREATE TEMPORARY FILE */
4664 TRACE("CREATE TEMPORARY FILE\n");
4665 bSetDOSExtendedError
= !INT21_CreateTempFile(context
);
4668 case 0x5b: /* CREATE NEW FILE */
4669 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4670 OF_READWRITE
| OF_SHARE_COMPAT
, 0x10 ))
4671 bSetDOSExtendedError
= TRUE
;
4673 RESET_CFLAG(context
);
4676 case 0x5c: /* "FLOCK" - RECORD LOCKING */
4678 DWORD offset
= MAKELONG(DX_reg(context
), CX_reg(context
));
4679 DWORD length
= MAKELONG(DI_reg(context
), SI_reg(context
));
4680 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
4682 RESET_CFLAG(context
);
4683 switch (AL_reg(context
))
4685 case 0x00: /* LOCK */
4686 TRACE( "lock handle %d offset %d length %d\n",
4687 BX_reg(context
), offset
, length
);
4688 if (!LockFile( handle
, offset
, 0, length
, 0 ))
4689 bSetDOSExtendedError
= TRUE
;
4692 case 0x01: /* UNLOCK */
4693 TRACE( "unlock handle %d offset %d length %d\n",
4694 BX_reg(context
), offset
, length
);
4695 if (!UnlockFile( handle
, offset
, 0, length
, 0 ))
4696 bSetDOSExtendedError
= TRUE
;
4700 INT_BARF( context
, 0x21 );
4705 case 0x5d: /* NETWORK 5D */
4706 FIXME( "Network function 5D not implemented.\n" );
4707 SetLastError( ER_NoNetwork
);
4708 bSetDOSExtendedError
= TRUE
;
4711 case 0x5e: /* NETWORK 5E */
4712 bSetDOSExtendedError
= INT21_NetworkFunc( context
);
4715 case 0x5f: /* NETWORK 5F */
4716 /* FIXME: supporting this would need to 1:
4717 * - implement per drive current directory (as kernel32 doesn't)
4718 * - assign enabled/disabled flag on a per drive basis
4720 /* network software not installed */
4721 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context
));
4722 SetLastError( ER_NoNetwork
);
4723 bSetDOSExtendedError
= TRUE
;
4726 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
4728 WCHAR pathW
[MAX_PATH
], res
[MAX_PATH
];
4729 /* FIXME: likely to be broken */
4731 TRACE("TRUENAME %s\n",
4732 (LPCSTR
)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Esi
));
4733 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
4734 if (!GetFullPathNameW( pathW
, 128, res
, NULL
))
4735 bSetDOSExtendedError
= TRUE
;
4738 SET_AX( context
, 0 );
4739 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
4740 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
4746 case 0x61: /* UNUSED */
4747 SET_AL( context
, 0 );
4750 case 0x62: /* GET PSP ADDRESS */
4751 INT21_GetPSP( context
);
4754 case 0x63: /* MISC. LANGUAGE SUPPORT */
4755 switch (AL_reg(context
)) {
4756 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
4757 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
4758 context
->SegDs
= INT21_GetHeapSelector( context
);
4759 SET_SI( context
, offsetof(INT21_HEAP
, dbcs_table
) );
4760 SET_AL( context
, 0 ); /* success */
4765 case 0x64: /* OS/2 DOS BOX */
4766 INT_BARF( context
, 0x21 );
4770 case 0x65: /* EXTENDED COUNTRY INFORMATION */
4771 INT21_ExtendedCountryInformation( context
);
4774 case 0x66: /* GLOBAL CODE PAGE TABLE */
4775 switch (AL_reg(context
))
4778 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
4779 SET_BX( context
, GetOEMCP() );
4780 SET_DX( context
, GetOEMCP() );
4783 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
4784 BX_reg(context
), DX_reg(context
) );
4789 case 0x67: /* SET HANDLE COUNT */
4790 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context
) );
4791 if (SetHandleCount( BX_reg(context
) ) < BX_reg(context
) )
4792 bSetDOSExtendedError
= TRUE
;
4795 case 0x68: /* "FFLUSH" - COMMIT FILE */
4796 TRACE( "FFLUSH - handle %d\n", BX_reg(context
) );
4797 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context
)) ) &&
4798 GetLastError() != ERROR_ACCESS_DENIED
)
4799 bSetDOSExtendedError
= TRUE
;
4802 case 0x69: /* DISK SERIAL NUMBER */
4803 switch (AL_reg(context
))
4806 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
4807 INT21_DriveName(BL_reg(context
)));
4808 if (!INT21_GetDiskSerialNumber(context
)) bSetDOSExtendedError
= TRUE
;
4809 else SET_AX( context
, 0 );
4813 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
4814 INT21_DriveName(BL_reg(context
)));
4815 if (!INT21_SetDiskSerialNumber(context
)) bSetDOSExtendedError
= TRUE
;
4816 else SET_AX( context
, 1 );
4821 case 0x6a: /* COMMIT FILE */
4822 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context
) );
4823 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context
)) ) &&
4824 GetLastError() != ERROR_ACCESS_DENIED
)
4825 bSetDOSExtendedError
= TRUE
;
4828 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4829 SET_AL( context
, 0 );
4832 case 0x6c: /* EXTENDED OPEN/CREATE */
4833 if (!INT21_CreateFile( context
, context
->Esi
, TRUE
,
4834 BX_reg(context
), DL_reg(context
) ))
4835 bSetDOSExtendedError
= TRUE
;
4838 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
4839 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
4840 SET_CFLAG( context
);
4841 SET_AL( context
, 0 );
4844 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
4845 INT21_LongFilename( context
);
4848 case 0x73: /* MSDOS7 - FAT32 */
4849 RESET_CFLAG( context
);
4850 if (!INT21_Fat32( context
))
4851 bSetDOSExtendedError
= TRUE
;
4854 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
4855 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
4858 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
4859 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
4862 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
4863 /* we don't implement a DOS32 extender */
4864 TRACE( "DOS32 EXTENDER API - ignored\n" );
4868 INT_BARF( context
, 0x21 );
4871 } /* END OF SWITCH */
4873 /* Set general error condition. */
4874 if (bSetDOSExtendedError
)
4876 SET_AX( context
, GetLastError() );
4877 SET_CFLAG( context
);
4880 /* Print error code if carry flag is set. */
4881 if (context
->EFlags
& 0x0001)
4882 TRACE("failed, error %d\n", GetLastError() );
4884 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
4885 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
4886 AX_reg(context
), BX_reg(context
),
4887 CX_reg(context
), DX_reg(context
),
4888 SI_reg(context
), DI_reg(context
),
4889 (WORD
)context
->SegDs
, (WORD
)context
->SegEs
,