2 * DOS interrupt 21h handler
4 * Copyright 1993, 1994 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1997 Andreas Mohr
7 * Copyright 1998 Uwe Bonnes
8 * Copyright 1998, 1999 Ove Kaaven
9 * Copyright 2003 Thomas Mertes
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #include "wine/winbase16.h"
36 #include "kernel16_private.h"
40 #include "wine/debug.h"
41 #include "wine/exception.h"
45 * - Most of the file related functions are wrong. NT's kernel32
46 * doesn't maintain a per drive current directory, while DOS does.
47 * We should in fact keep track in here of those per drive
48 * directories, and use this info while dealing with partial paths
49 * (drive defined, but only relative paths). This could even be
50 * created as an array of CDS (there should be an entry for that in
55 * Forward declarations.
57 static BOOL
INT21_RenameFile( CONTEXT
*context
);
59 WINE_DEFAULT_DEBUG_CHANNEL(int21
);
65 * Extended Drive Parameter Block.
66 * This structure is compatible with standard DOS4+ DPB and
69 typedef struct _INT21_DPB
{
70 BYTE drive
; /* 00 drive number (0=A, ...) */
71 BYTE unit
; /* 01 unit number within device driver */
72 WORD sector_bytes
; /* 02 bytes per sector */
73 BYTE cluster_sectors
; /* 04 highest sector number within a cluster */
74 BYTE shift
; /* 05 shift count to convert clusters into sectors */
75 WORD num_reserved
; /* 06 reserved sectors at beginning of drive */
76 BYTE num_FAT
; /* 08 number of FATs */
77 WORD num_root_entries
; /* 09 number of root directory entries */
78 WORD first_data_sector
; /* 0b number of first sector containing user data */
79 WORD num_clusters1
; /* 0d highest cluster number (number of data clusters + 1) */
80 WORD sectors_per_FAT
; /* 0f number of sectors per FAT */
81 WORD first_dir_sector
; /* 11 sector number of first directory sector */
82 SEGPTR driver_header
; /* 13 address of device driver header */
83 BYTE media_ID
; /* 17 media ID byte */
84 BYTE access_flag
; /* 18 0x00 if disk accessed, 0xff if not */
85 SEGPTR next
; /* 19 pointer to next DPB */
86 WORD search_cluster1
; /* 1d cluster at which to start search for free space */
87 WORD free_clusters_lo
; /* 1f number of free clusters on drive or 0xffff if unknown */
88 WORD free_clusters_hi
; /* 21 hiword of clusters_free */
89 WORD mirroring_flags
; /* 23 active FAT/mirroring flags */
90 WORD info_sector
; /* 25 sector number of file system info sector or 0xffff for none */
91 WORD spare_boot_sector
; /* 27 sector number of backup boot sector or 0xffff for none */
92 DWORD first_cluster_sector
; /* 29 sector number of the first cluster */
93 DWORD num_clusters2
; /* 2d maximum cluster number */
94 DWORD fat_clusters
; /* 31 number of clusters occupied by FAT */
95 DWORD root_cluster
; /* 35 cluster number of start of root directory */
96 DWORD search_cluster2
; /* 39 cluster at which to start searching for free space */
101 * Structure for DOS data that can be accessed directly from applications.
102 * Real and protected mode pointers will be returned to this structure so
103 * the structure must be correctly packed.
105 typedef struct _INT21_HEAP
{
106 WORD uppercase_size
; /* Size of the following table in bytes */
107 BYTE uppercase_table
[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
109 WORD lowercase_size
; /* Size of the following table in bytes */
110 BYTE lowercase_table
[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
112 WORD collating_size
; /* Size of the following table in bytes */
113 BYTE collating_table
[256]; /* Values used to sort characters from 0x00 to 0xff. */
115 WORD filename_size
; /* Size of the following filename data in bytes */
116 BYTE filename_reserved1
; /* 0x01 for MS-DOS 3.30-6.00 */
117 BYTE filename_lowest
; /* Lowest permissible character value for filename */
118 BYTE filename_highest
; /* Highest permissible character value for filename */
119 BYTE filename_reserved2
; /* 0x00 for MS-DOS 3.30-6.00 */
120 BYTE filename_exclude_first
; /* First illegal character in permissible range */
121 BYTE filename_exclude_last
; /* Last illegal character in permissible range */
122 BYTE filename_reserved3
; /* 0x02 for MS-DOS 3.30-6.00 */
123 BYTE filename_illegal_size
; /* Number of terminators in the following table */
124 BYTE filename_illegal_table
[16]; /* Characters which terminate a filename */
126 WORD dbcs_size
; /* Number of valid ranges in the following table */
127 BYTE dbcs_table
[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
129 BYTE misc_indos
; /* Interrupt 21 nesting flag */
130 WORD misc_selector
; /* Protected mode selector for INT21_HEAP */
131 INT21_DPB misc_dpb_list
[MAX_DOS_DRIVES
]; /* Drive parameter blocks for all drives */
139 CHAR file_extension
[3];
140 WORD current_block_number
;
141 WORD logical_record_size
;
143 WORD date_of_last_write
;
144 WORD time_of_last_write
;
147 WORD starting_cluster
;
148 WORD sequence_number
;
149 BYTE file_attributes
;
151 BYTE record_within_current_block
;
152 BYTE random_access_record_number
[4];
159 BYTE xfcb_file_attribute
;
163 /* DTA layout for FindFirst/FindNext */
166 BYTE drive
; /* 00 drive letter */
167 char mask
[11]; /* 01 search template */
168 BYTE search_attr
; /* 0c search attributes */
169 WORD count
; /* 0d entry count within directory */
170 WORD cluster
; /* 0f cluster of parent directory */
171 WCHAR
*fullPath
; /* 11 full path (was: reserved) */
172 BYTE fileattr
; /* 15 file attributes */
173 WORD filetime
; /* 16 file time */
174 WORD filedate
; /* 18 file date */
175 DWORD filesize
; /* 1a file size */
176 char filename
[13]; /* 1e file name + extension */
179 /* FCB layout for FindFirstFCB/FindNextFCB */
182 BYTE drive
; /* 00 drive letter */
183 char filename
[11]; /* 01 filename 8+3 format */
184 int count
; /* 0c entry count (was: reserved) */
185 WCHAR
*fullPath
; /* 10 full path (was: reserved) */
188 /* DOS directory entry for FindFirstFCB/FindNextFCB */
191 char filename
[11]; /* 00 filename 8+3 format */
192 BYTE fileattr
; /* 0b file attributes */
193 BYTE reserved
[10]; /* 0c reserved */
194 WORD filetime
; /* 16 file time */
195 WORD filedate
; /* 18 file date */
196 WORD cluster
; /* 1a file first cluster */
197 DWORD filesize
; /* 1c file size */
198 } DOS_DIRENTRY_LAYOUT
;
202 /* dos file attributes */
203 #define FA_NORMAL 0x00 /* Normal file, no attributes */
204 #define FA_RDONLY 0x01 /* Read only attribute */
205 #define FA_HIDDEN 0x02 /* Hidden file */
206 #define FA_SYSTEM 0x04 /* System file */
207 #define FA_LABEL 0x08 /* Volume label */
208 #define FA_DIRECTORY 0x10 /* Directory */
209 #define FA_ARCHIVE 0x20 /* Archive */
210 #define FA_UNUSED 0x40 /* Unused */
213 #define ER_NoNetwork 0x49
216 #define EC_OutOfResource 0x01
217 #define EC_Temporary 0x02
218 #define EC_AccessDenied 0x03
219 #define EC_InternalError 0x04
220 #define EC_HardwareFailure 0x05
221 #define EC_SystemFailure 0x06
222 #define EC_ProgramError 0x07
223 #define EC_NotFound 0x08
224 #define EC_MediaError 0x0b
225 #define EC_Exists 0x0c
226 #define EC_Unknown 0x0d
228 /* Suggested actions */
229 #define SA_Retry 0x01
230 #define SA_DelayedRetry 0x02
231 #define SA_Abort 0x04
232 #define SA_Ignore 0x06
233 #define SA_Ask4Retry 0x07
236 #define EL_Unknown 0x01
238 #define EL_Network 0x03
239 #define EL_Serial 0x04
240 #define EL_Memory 0x05
242 /* BIOS Keyboard Scancodes */
243 #define KEY_LEFT 0x4B
244 #define KEY_RIGHT 0x4D
246 #define KEY_DOWN 0x50
247 #define KEY_IC 0x52 /* insert char */
248 #define KEY_DC 0x53 /* delete char */
249 #define KEY_BACKSPACE 0x0E
250 #define KEY_HOME 0x47
252 #define KEY_NPAGE 0x49
253 #define KEY_PPAGE 0x51
257 static BYTE
drive_number( WCHAR letter
)
259 if (letter
>= 'A' && letter
<= 'Z') return letter
- 'A';
260 if (letter
>= 'a' && letter
<= 'z') return letter
- 'a';
261 return MAX_DOS_DRIVES
;
265 /* Many calls translate a drive argument like this:
266 drive number (00h = default, 01h = A:, etc)
268 /******************************************************************
271 * Many calls translate a drive argument like this:
272 * drive number (00h = default, 01h = A:, etc)
274 static const char *INT21_DriveName(int drive
)
278 if (drive
<= 26) return wine_dbg_sprintf("%c:", 'A' + drive
- 1);
279 else return wine_dbg_sprintf( "<Bad drive: %d>", drive
);
284 /***********************************************************************
285 * INT21_GetCurrentDrive
287 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
288 * MAX_DOS_DRIVES on error.
290 static BYTE
INT21_GetCurrentDrive(void)
292 WCHAR current_directory
[MAX_PATH
];
294 if (!GetCurrentDirectoryW( MAX_PATH
, current_directory
) ||
295 current_directory
[1] != ':')
297 TRACE( "Failed to get current drive.\n" );
298 return MAX_DOS_DRIVES
;
300 return drive_number( current_directory
[0] );
304 /***********************************************************************
307 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
308 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
310 static BYTE
INT21_MapDrive( BYTE drive
)
314 WCHAR drivespec
[] = {'A', ':', 0};
317 drivespec
[0] += drive
- 1;
318 drivetype
= GetDriveTypeW( drivespec
);
320 if (drivetype
== DRIVE_UNKNOWN
|| drivetype
== DRIVE_NO_ROOT_DIR
)
321 return MAX_DOS_DRIVES
;
326 return INT21_GetCurrentDrive();
330 /***********************************************************************
331 * INT21_SetCurrentDrive
333 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
335 static void INT21_SetCurrentDrive( BYTE drive
)
337 WCHAR drivespec
[] = {'A', ':', 0};
339 drivespec
[0] += drive
;
341 if (!SetCurrentDirectoryW( drivespec
))
342 TRACE( "Failed to set current drive.\n" );
346 /***********************************************************************
347 * INT21_GetSystemCountryCode
349 * Return DOS country code for default system locale.
351 static WORD
INT21_GetSystemCountryCode( void )
353 return GetSystemDefaultLangID();
357 /***********************************************************************
358 * INT21_FillCountryInformation
360 * Fill 34-byte buffer with country information data using
361 * default system locale.
363 static void INT21_FillCountryInformation( BYTE
*buffer
)
365 /* 00 - WORD: date format
370 *(WORD
*)(buffer
+ 0) = 0; /* FIXME: Get from locale */
372 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
373 buffer
[2] = '$'; /* FIXME: Get from locale */
376 /* 07 - BYTE[2]: ASCIIZ thousands separator */
377 buffer
[7] = 0; /* FIXME: Get from locale */
380 /* 09 - BYTE[2]: ASCIIZ decimal separator */
381 buffer
[9] = '.'; /* FIXME: Get from locale */
384 /* 11 - BYTE[2]: ASCIIZ date separator */
385 buffer
[11] = '/'; /* FIXME: Get from locale */
388 /* 13 - BYTE[2]: ASCIIZ time separator */
389 buffer
[13] = ':'; /* FIXME: Get from locale */
392 /* 15 - BYTE: Currency format
393 * bit 2 = set if currency symbol replaces decimal point
394 * bit 1 = number of spaces between value and currency symbol
395 * bit 0 = 0 if currency symbol precedes value
396 * 1 if currency symbol follows value
398 buffer
[15] = 0; /* FIXME: Get from locale */
400 /* 16 - BYTE: Number of digits after decimal in currency */
401 buffer
[16] = 0; /* FIXME: Get from locale */
403 /* 17 - BYTE: Time format
404 * bit 0 = 0 if 12-hour clock
407 buffer
[17] = 1; /* FIXME: Get from locale */
409 /* 18 - DWORD: Address of case map routine */
410 *(DWORD
*)(buffer
+ 18) = 0; /* FIXME: ptr to case map routine */
412 /* 22 - BYTE[2]: ASCIIZ data-list separator */
413 buffer
[22] = ','; /* FIXME: Get from locale */
416 /* 24 - BYTE[10]: Reserved */
417 memset( buffer
+ 24, 0, 10 );
421 /***********************************************************************
424 * Initialize DOS heap.
426 * Filename Terminator Table of w2k DE NTVDM:
427 * 16 00 01 00 FF 00 00 20-02 0E 2E 22 2F 5C 5B 5D ....... ..."/\[]
428 * 3A 7C 3C 3E 2B 3D 3B 2C-00 :|<>+=;,
430 static void INT21_FillHeap( INT21_HEAP
*heap
)
432 static const char terminators
[] = ".\"/\\[]:|<>+=;,";
438 heap
->uppercase_size
= 128;
439 for (i
= 0; i
< 128; i
++)
440 heap
->uppercase_table
[i
] = toupper( 128 + i
);
445 heap
->lowercase_size
= 256;
446 for (i
= 0; i
< 256; i
++)
447 heap
->lowercase_table
[i
] = tolower( i
);
452 heap
->collating_size
= 256;
453 for (i
= 0; i
< 256; i
++)
454 heap
->collating_table
[i
] = i
;
459 heap
->filename_size
= 8 + strlen(terminators
);
460 heap
->filename_illegal_size
= strlen(terminators
);
461 memcpy( heap
->filename_illegal_table
, terminators
, heap
->filename_illegal_size
);
463 heap
->filename_reserved1
= 0x01;
464 heap
->filename_lowest
= 0x00;
465 heap
->filename_highest
= 0xff;
466 heap
->filename_reserved2
= 0x00;
467 heap
->filename_exclude_first
= 0x00;
468 heap
->filename_exclude_last
= 0x20;
469 heap
->filename_reserved3
= 0x02;
472 * DBCS lead byte table. This table is empty.
475 memset( heap
->dbcs_table
, 0, sizeof(heap
->dbcs_table
) );
478 * Initialize InDos flag.
480 heap
->misc_indos
= 0;
483 * FIXME: Should drive parameter blocks (DPB) be
484 * initialized here and linked to DOS LOL?
489 /***********************************************************************
490 * INT21_GetHeapPointer
492 * Get pointer for DOS heap (INT21_HEAP).
493 * Creates and initializes heap on first call.
495 static INT21_HEAP
*INT21_GetHeapPointer( void )
497 static INT21_HEAP
*heap_pointer
= NULL
;
501 WORD heap_selector
= GlobalAlloc16( GMEM_FIXED
, sizeof(INT21_HEAP
) );
502 heap_pointer
= GlobalLock16( heap_selector
);
503 heap_pointer
->misc_selector
= heap_selector
;
504 INT21_FillHeap( heap_pointer
);
511 /***********************************************************************
512 * INT21_GetHeapSelector
514 * Get segment/selector for DOS heap (INT21_HEAP).
515 * Creates and initializes heap on first call.
517 static WORD
INT21_GetHeapSelector( CONTEXT
*context
)
519 INT21_HEAP
*heap
= INT21_GetHeapPointer();
520 return heap
->misc_selector
;
524 /***********************************************************************
527 * Fill DOS heap drive parameter block for the specified drive.
528 * Return TRUE if drive was valid and there were
529 * no errors while reading drive information.
531 static BOOL
INT21_FillDrivePB( BYTE drive
)
533 WCHAR drivespec
[] = {'A', ':', 0};
534 INT21_HEAP
*heap
= INT21_GetHeapPointer();
537 DWORD cluster_sectors
;
540 DWORD total_clusters
;
542 if (drive
>= MAX_DOS_DRIVES
)
545 dpb
= &heap
->misc_dpb_list
[drive
];
546 drivespec
[0] += drive
;
547 drivetype
= GetDriveTypeW( drivespec
);
550 * FIXME: Does this check work correctly with floppy/cdrom drives?
552 if (drivetype
== DRIVE_NO_ROOT_DIR
|| drivetype
== DRIVE_UNKNOWN
)
556 * FIXME: Does this check work correctly with floppy/cdrom drives?
558 if (!GetDiskFreeSpaceW( drivespec
, &cluster_sectors
, §or_bytes
,
559 &free_clusters
, &total_clusters
))
563 * FIXME: Most of the values listed below are incorrect.
564 * All values should be validated.
569 dpb
->sector_bytes
= sector_bytes
;
570 dpb
->cluster_sectors
= cluster_sectors
- 1;
573 while (cluster_sectors
> 1)
575 cluster_sectors
/= 2;
579 dpb
->num_reserved
= 0;
581 dpb
->num_root_entries
= 2;
582 dpb
->first_data_sector
= 2;
583 dpb
->num_clusters1
= total_clusters
;
584 dpb
->sectors_per_FAT
= 1;
585 dpb
->first_dir_sector
= 1;
586 dpb
->driver_header
= 0;
587 dpb
->media_ID
= (drivetype
== DRIVE_FIXED
) ? 0xF8 : 0xF0;
588 dpb
->access_flag
= 0;
590 dpb
->search_cluster1
= 0;
591 dpb
->free_clusters_lo
= LOWORD(free_clusters
);
592 dpb
->free_clusters_hi
= HIWORD(free_clusters
);
593 dpb
->mirroring_flags
= 0;
594 dpb
->info_sector
= 0xffff;
595 dpb
->spare_boot_sector
= 0xffff;
596 dpb
->first_cluster_sector
= 0;
597 dpb
->num_clusters2
= total_clusters
;
598 dpb
->fat_clusters
= 32;
599 dpb
->root_cluster
= 0;
600 dpb
->search_cluster2
= 0;
606 /***********************************************************************
607 * INT21_GetCurrentDirectory
611 * - subfunction 0x47 of function 0x71
613 static BOOL
INT21_GetCurrentDirectory( CONTEXT
*context
, BOOL islong
)
615 char *buffer
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
);
616 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
617 WCHAR pathW
[MAX_PATH
];
618 char pathA
[MAX_PATH
];
621 TRACE( "drive %d\n", DL_reg(context
) );
623 if (drive
== MAX_DOS_DRIVES
)
625 SetLastError(ERROR_INVALID_DRIVE
);
630 * Grab current directory.
633 if (!GetCurrentDirectoryW( MAX_PATH
, pathW
)) return FALSE
;
635 if (drive_number( pathW
[0] ) != drive
|| pathW
[1] != ':')
637 /* cwd is not on the requested drive, get the environment string instead */
642 env_var
[1] = 'A' + drive
;
645 if (!GetEnvironmentVariableW( env_var
, pathW
, MAX_PATH
))
647 /* return empty path */
654 * Convert into short format.
659 DWORD result
= GetShortPathNameW( pathW
, pathW
, MAX_PATH
);
662 if (result
> MAX_PATH
)
664 WARN( "Short path too long!\n" );
665 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
671 * The returned pathname does not include
672 * the drive letter, colon or leading backslash.
678 * FIXME: We should probably just strip host part from name...
680 FIXME( "UNC names are not supported.\n" );
681 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
684 else if (!ptr
[0] || ptr
[1] != ':' || ptr
[2] != '\\')
686 WARN( "Path is neither UNC nor DOS path: %s\n",
687 wine_dbgstr_w(ptr
) );
688 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
693 /* Remove drive letter, colon and leading backslash. */
698 * Convert into OEM string.
701 if (!WideCharToMultiByte(CP_OEMCP
, 0, ptr
, -1, pathA
,
702 MAX_PATH
, NULL
, NULL
))
704 WARN( "Cannot convert path!\n" );
705 SetLastError(ERROR_NETWORK_BUSY
); /* Internal Wine error. */
715 /* Undocumented success code. */
716 SET_AX( context
, 0x0100 );
718 /* Truncate buffer to 64 bytes. */
722 TRACE( "%c:=%s\n", 'A' + drive
, pathA
);
724 strcpy( buffer
, pathA
);
729 /***********************************************************************
730 * INT21_SetCurrentDirectory
734 * - subfunction 0x3b of function 0x71
736 static BOOL
INT21_SetCurrentDirectory( CONTEXT
*context
)
738 WCHAR dirW
[MAX_PATH
];
741 char *dirA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
742 BYTE drive
= INT21_GetCurrentDrive();
745 TRACE( "SET CURRENT DIRECTORY %s\n", dirA
);
747 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
748 if (!GetFullPathNameW( dirW
, MAX_PATH
, dirW
, NULL
)) return FALSE
;
750 attr
= GetFileAttributesW( dirW
);
751 if (attr
== INVALID_FILE_ATTRIBUTES
|| !(attr
& FILE_ATTRIBUTE_DIRECTORY
))
753 SetLastError( ERROR_PATH_NOT_FOUND
);
758 env_var
[1] = dirW
[0];
761 result
= SetEnvironmentVariableW( env_var
, dirW
);
763 /* only set current directory if on the current drive */
764 if (result
&& (drive_number( dirW
[0] ) == drive
)) result
= SetCurrentDirectoryW( dirW
);
769 /***********************************************************************
777 * - subfunction 0x6c of function 0x71
779 static BOOL
INT21_CreateFile( CONTEXT
*context
,
786 char *pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, pathSegOff
);
787 WCHAR pathW
[MAX_PATH
];
794 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
795 "create flags=%04x, file=%s.\n",
796 AH_reg(context
), dosAction
, dosAccessShare
, CX_reg(context
), pathA
);
799 * Application tried to create/open a file whose name
800 * ends with a backslash. This is not allowed.
802 * FIXME: This needs to be validated, especially the return value.
804 if (pathA
[strlen(pathA
) - 1] == '/')
806 SetLastError( ERROR_FILE_NOT_FOUND
);
811 * Convert DOS action flags into Win32 creation disposition parameter.
816 winMode
= OPEN_EXISTING
;
819 winMode
= TRUNCATE_EXISTING
;
822 winMode
= CREATE_NEW
;
825 winMode
= OPEN_ALWAYS
;
828 winMode
= CREATE_ALWAYS
;
831 SetLastError( ERROR_INVALID_PARAMETER
);
836 * Convert DOS access/share flags into Win32 desired access parameter.
838 switch(dosAccessShare
& 0x07)
841 winAccess
= GENERIC_READ
;
844 winAccess
= GENERIC_WRITE
;
847 winAccess
= GENERIC_READ
| GENERIC_WRITE
;
851 * Read-only, do not modify file's last-access time (DOS7).
853 * FIXME: How to prevent modification of last-access time?
855 winAccess
= GENERIC_READ
;
862 * Convert DOS access/share flags into Win32 share mode parameter.
864 switch(dosAccessShare
& 0x70)
866 case OF_SHARE_EXCLUSIVE
:
869 case OF_SHARE_DENY_WRITE
:
870 winSharing
= FILE_SHARE_READ
;
872 case OF_SHARE_DENY_READ
:
873 winSharing
= FILE_SHARE_WRITE
;
875 case OF_SHARE_DENY_NONE
:
876 case OF_SHARE_COMPAT
:
878 winSharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
882 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
883 * What to do with this bit?
884 * FIXME: Bits in the high byte of dosAccessShare are not supported.
885 * See both function 0x6c and subfunction 0x6c of function 0x71 for
886 * definition of these bits.
890 * Convert DOS create attributes into Win32 flags and attributes parameter.
892 if (winMode
== OPEN_EXISTING
|| winMode
== TRUNCATE_EXISTING
)
898 WORD dosAttributes
= CX_reg(context
);
900 if (dosAttributes
& FA_LABEL
)
903 * Application tried to create volume label entry.
904 * This is difficult to support so we do not allow it.
906 * FIXME: If volume does not already have a label,
907 * this function is supposed to succeed.
909 SetLastError( ERROR_ACCESS_DENIED
);
913 winAttributes
= dosAttributes
&
914 (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
|
915 FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_ARCHIVE
);
921 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
923 winHandle
= CreateFileW( pathW
, winAccess
, winSharing
, NULL
, winMode
, winAttributes
, 0 );
924 /* DOS allows opening files on a CDROM R/W */
925 if( winHandle
== INVALID_HANDLE_VALUE
&&
926 (GetLastError() == ERROR_WRITE_PROTECT
||
927 GetLastError() == ERROR_ACCESS_DENIED
)) {
928 winHandle
= CreateFileW( pathW
, winAccess
& ~GENERIC_WRITE
,
929 winSharing
, NULL
, winMode
, winAttributes
, 0 );
932 if (winHandle
== INVALID_HANDLE_VALUE
)
936 * Determine DOS file status.
947 case TRUNCATE_EXISTING
:
954 dosStatus
= (GetLastError() == ERROR_ALREADY_EXISTS
) ? 1 : 2;
957 dosStatus
= (GetLastError() == ERROR_ALREADY_EXISTS
) ? 3 : 2;
964 * Return DOS file handle and DOS status.
966 SET_AX( context
, Win32HandleToDosFileHandle(winHandle
) );
969 SET_CX( context
, dosStatus
);
971 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
972 AX_reg(context
), dosStatus
);
978 /***********************************************************************
979 * INT21_GetCurrentDTA
981 static BYTE
*INT21_GetCurrentDTA( CONTEXT
*context
)
983 TDB
*pTask
= GlobalLock16(GetCurrentTask());
985 /* FIXME: This assumes DTA was set correctly! */
986 return CTX_SEG_OFF_TO_LIN( context
, SELECTOROF(pTask
->dta
),
987 OFFSETOF(pTask
->dta
) );
991 /***********************************************************************
992 * INT21_OpenFileUsingFCB
994 * Handler for function 0x0f.
997 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
1004 * Opens a FCB file for read/write in compatibility mode. Upon calling
1005 * the FCB must have the drive_number, file_name, and file_extension
1006 * fields filled and all other bytes cleared.
1008 static void INT21_OpenFileUsingFCB( CONTEXT
*context
)
1016 BY_HANDLE_FILE_INFORMATION info
;
1019 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1020 if (fcb
->drive_number
== 0xff) {
1021 xfcb
= (struct XFCB
*) fcb
;
1022 fcb
= (struct FCB
*) xfcb
->fcb
;
1026 file_path
[0] = 'A' + INT21_MapDrive( fcb
->drive_number
);
1028 if (AL_result
== 0) {
1030 pos
= &file_path
[2];
1031 memcpy(pos
, fcb
->file_name
, 8);
1034 pos
= strchr(pos
, ' ');
1037 memcpy(pos
, fcb
->file_extension
, 3);
1040 pos
= strchr(pos
, ' ');
1043 handle
= (HANDLE
) _lopen(file_path
, OF_READWRITE
);
1044 if (handle
== INVALID_HANDLE_VALUE
) {
1045 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path
);
1046 AL_result
= 0xff; /* failed */
1048 hfile16
= Win32HandleToDosFileHandle(handle
);
1049 if (hfile16
== HFILE_ERROR16
) {
1050 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle
);
1051 CloseHandle(handle
);
1052 AL_result
= 0xff; /* failed */
1053 } else if (hfile16
> 255) {
1054 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16
, file_path
);
1056 AL_result
= 0xff; /* failed */
1058 if (!GetFileInformationByHandle(handle
, &info
)) {
1059 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1060 hfile16
, handle
, file_path
);
1062 AL_result
= 0xff; /* failed */
1064 fcb
->drive_number
= file_path
[0] - 'A' + 1;
1065 fcb
->current_block_number
= 0;
1066 fcb
->logical_record_size
= 128;
1067 fcb
->file_size
= info
.nFileSizeLow
;
1068 FileTimeToDosDateTime(&info
.ftLastWriteTime
,
1069 &fcb
->date_of_last_write
, &fcb
->time_of_last_write
);
1070 fcb
->file_number
= hfile16
;
1071 fcb
->attributes
= 0xc2;
1072 fcb
->starting_cluster
= 0; /* don't know correct init value */
1073 fcb
->sequence_number
= 0; /* don't know correct init value */
1074 fcb
->file_attributes
= info
.dwFileAttributes
;
1075 /* The following fields are not initialized */
1076 /* by the native function: */
1078 /* record_within_current_block */
1079 /* random_access_record_number */
1081 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1082 file_path
, hfile16
, handle
);
1083 AL_result
= 0x00; /* successful */
1088 SET_AL(context
, AL_result
);
1092 /***********************************************************************
1093 * INT21_CloseFileUsingFCB
1095 * Handler for function 0x10.
1098 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1105 * Closes a FCB file.
1107 static void INT21_CloseFileUsingFCB( CONTEXT
*context
)
1113 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1114 if (fcb
->drive_number
== 0xff) {
1115 xfcb
= (struct XFCB
*) fcb
;
1116 fcb
= (struct FCB
*) xfcb
->fcb
;
1119 if (_lclose16((HFILE16
) fcb
->file_number
) != 0) {
1120 TRACE("_lclose16(%d) failed\n", fcb
->file_number
);
1121 AL_result
= 0xff; /* failed */
1123 TRACE("successful closed file %d\n", fcb
->file_number
);
1124 AL_result
= 0x00; /* successful */
1126 SET_AL(context
, AL_result
);
1130 /***********************************************************************
1131 * INT21_SequentialReadFromFCB
1133 * Handler for function 0x14.
1136 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1140 * 1: end of file, no data read
1141 * 2: segment wrap in DTA, no data read (not returned now)
1142 * 3: end of file, partial record read
1145 * Reads a record with the size FCB->logical_record_size from the FCB
1146 * to the disk transfer area. The position of the record is specified
1147 * with FCB->current_block_number and FCB->record_within_current_block.
1148 * Then FCB->current_block_number and FCB->record_within_current_block
1149 * are updated to point to the next record. If a partial record is
1150 * read, it is filled with zeros up to the FCB->logical_record_size.
1152 static void INT21_SequentialReadFromFCB( CONTEXT
*context
)
1157 DWORD record_number
;
1159 BYTE
*disk_transfer_area
;
1163 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1164 if (fcb
->drive_number
== 0xff) {
1165 xfcb
= (struct XFCB
*) fcb
;
1166 fcb
= (struct FCB
*) xfcb
->fcb
;
1169 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1170 if (handle
== INVALID_HANDLE_VALUE
) {
1171 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1173 AL_result
= 0x01; /* end of file, no data read */
1175 record_number
= 128 * fcb
->current_block_number
+ fcb
->record_within_current_block
;
1176 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1177 if (position
!= record_number
* fcb
->logical_record_size
) {
1178 TRACE("seek(%d, %d, 0) failed with %u\n",
1179 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1180 AL_result
= 0x01; /* end of file, no data read */
1182 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1183 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, fcb
->logical_record_size
);
1184 if (bytes_read
!= fcb
->logical_record_size
) {
1185 TRACE("_lread(%d, %p, %d) failed with %d\n",
1186 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_read
);
1187 if (bytes_read
== 0) {
1188 AL_result
= 0x01; /* end of file, no data read */
1190 memset(&disk_transfer_area
[bytes_read
], 0, fcb
->logical_record_size
- bytes_read
);
1191 AL_result
= 0x03; /* end of file, partial record read */
1194 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1195 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1196 AL_result
= 0x00; /* successful */
1200 if (AL_result
== 0x00 || AL_result
== 0x03) {
1201 if (fcb
->record_within_current_block
== 127) {
1202 fcb
->record_within_current_block
= 0;
1203 fcb
->current_block_number
++;
1205 fcb
->record_within_current_block
++;
1208 SET_AL(context
, AL_result
);
1212 /***********************************************************************
1213 * INT21_SequentialWriteToFCB
1215 * Handler for function 0x15.
1218 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1223 * 2: segment wrap in DTA (not returned now)
1226 * Writes a record with the size FCB->logical_record_size from the disk
1227 * transfer area to the FCB. The position of the record is specified
1228 * with FCB->current_block_number and FCB->record_within_current_block.
1229 * Then FCB->current_block_number and FCB->record_within_current_block
1230 * are updated to point to the next record.
1232 static void INT21_SequentialWriteToFCB( CONTEXT
*context
)
1237 DWORD record_number
;
1239 BYTE
*disk_transfer_area
;
1243 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1244 if (fcb
->drive_number
== 0xff) {
1245 xfcb
= (struct XFCB
*) fcb
;
1246 fcb
= (struct FCB
*) xfcb
->fcb
;
1249 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1250 if (handle
== INVALID_HANDLE_VALUE
) {
1251 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1253 AL_result
= 0x01; /* disk full */
1255 record_number
= 128 * fcb
->current_block_number
+ fcb
->record_within_current_block
;
1256 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1257 if (position
!= record_number
* fcb
->logical_record_size
) {
1258 TRACE("seek(%d, %d, 0) failed with %u\n",
1259 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1260 AL_result
= 0x01; /* disk full */
1262 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1263 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, fcb
->logical_record_size
);
1264 if (bytes_written
!= fcb
->logical_record_size
) {
1265 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1266 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_written
);
1267 AL_result
= 0x01; /* disk full */
1269 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1270 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1271 AL_result
= 0x00; /* successful */
1275 if (AL_result
== 0x00) {
1276 if (fcb
->record_within_current_block
== 127) {
1277 fcb
->record_within_current_block
= 0;
1278 fcb
->current_block_number
++;
1280 fcb
->record_within_current_block
++;
1283 SET_AL(context
, AL_result
);
1287 /***********************************************************************
1288 * INT21_ReadRandomRecordFromFCB
1290 * Handler for function 0x21.
1293 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1297 * 1: end of file, no data read
1298 * 2: segment wrap in DTA, no data read (not returned now)
1299 * 3: end of file, partial record read
1302 * Reads a record with the size FCB->logical_record_size from
1303 * the FCB to the disk transfer area. The position of the record
1304 * is specified with FCB->random_access_record_number. The
1305 * FCB->random_access_record_number is not updated. If a partial record
1306 * is read, it is filled with zeros up to the FCB->logical_record_size.
1308 static void INT21_ReadRandomRecordFromFCB( CONTEXT
*context
)
1313 DWORD record_number
;
1315 BYTE
*disk_transfer_area
;
1319 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1320 if (fcb
->drive_number
== 0xff) {
1321 xfcb
= (struct XFCB
*) fcb
;
1322 fcb
= (struct FCB
*) xfcb
->fcb
;
1325 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1326 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1327 if (handle
== INVALID_HANDLE_VALUE
) {
1328 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1330 AL_result
= 0x01; /* end of file, no data read */
1332 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1333 if (position
!= record_number
* fcb
->logical_record_size
) {
1334 TRACE("seek(%d, %d, 0) failed with %u\n",
1335 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1336 AL_result
= 0x01; /* end of file, no data read */
1338 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1339 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, fcb
->logical_record_size
);
1340 if (bytes_read
!= fcb
->logical_record_size
) {
1341 TRACE("_lread(%d, %p, %d) failed with %d\n",
1342 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_read
);
1343 if (bytes_read
== 0) {
1344 AL_result
= 0x01; /* end of file, no data read */
1346 memset(&disk_transfer_area
[bytes_read
], 0, fcb
->logical_record_size
- bytes_read
);
1347 AL_result
= 0x03; /* end of file, partial record read */
1350 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1351 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1352 AL_result
= 0x00; /* successful */
1356 fcb
->current_block_number
= record_number
/ 128;
1357 fcb
->record_within_current_block
= record_number
% 128;
1358 SET_AL(context
, AL_result
);
1362 /***********************************************************************
1363 * INT21_WriteRandomRecordToFCB
1365 * Handler for function 0x22.
1368 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1373 * 2: segment wrap in DTA (not returned now)
1376 * Writes a record with the size FCB->logical_record_size from
1377 * the disk transfer area to the FCB. The position of the record
1378 * is specified with FCB->random_access_record_number. The
1379 * FCB->random_access_record_number is not updated.
1381 static void INT21_WriteRandomRecordToFCB( CONTEXT
*context
)
1386 DWORD record_number
;
1388 BYTE
*disk_transfer_area
;
1392 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1393 if (fcb
->drive_number
== 0xff) {
1394 xfcb
= (struct XFCB
*) fcb
;
1395 fcb
= (struct FCB
*) xfcb
->fcb
;
1398 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1399 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1400 if (handle
== INVALID_HANDLE_VALUE
) {
1401 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1403 AL_result
= 0x01; /* disk full */
1405 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1406 if (position
!= record_number
* fcb
->logical_record_size
) {
1407 TRACE("seek(%d, %d, 0) failed with %u\n",
1408 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1409 AL_result
= 0x01; /* disk full */
1411 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1412 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, fcb
->logical_record_size
);
1413 if (bytes_written
!= fcb
->logical_record_size
) {
1414 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1415 fcb
->file_number
, disk_transfer_area
, fcb
->logical_record_size
, bytes_written
);
1416 AL_result
= 0x01; /* disk full */
1418 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1419 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1420 AL_result
= 0x00; /* successful */
1424 fcb
->current_block_number
= record_number
/ 128;
1425 fcb
->record_within_current_block
= record_number
% 128;
1426 SET_AL(context
, AL_result
);
1430 /***********************************************************************
1431 * INT21_RandomBlockReadFromFCB
1433 * Handler for function 0x27.
1436 * CX [I/O] Number of records to read
1437 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1441 * 1: end of file, no data read
1442 * 2: segment wrap in DTA, no data read (not returned now)
1443 * 3: end of file, partial record read
1446 * Reads several records with the size FCB->logical_record_size from
1447 * the FCB to the disk transfer area. The number of records to be
1448 * read is specified in the CX register. The position of the first
1449 * record is specified with FCB->random_access_record_number. The
1450 * FCB->random_access_record_number, the FCB->current_block_number
1451 * and FCB->record_within_current_block are updated to point to the
1452 * next record after the records read. If a partial record is read,
1453 * it is filled with zeros up to the FCB->logical_record_size. The
1454 * CX register is set to the number of successfully read records.
1456 static void INT21_RandomBlockReadFromFCB( CONTEXT
*context
)
1461 DWORD record_number
;
1463 BYTE
*disk_transfer_area
;
1464 UINT records_requested
;
1465 UINT bytes_requested
;
1470 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1471 if (fcb
->drive_number
== 0xff) {
1472 xfcb
= (struct XFCB
*) fcb
;
1473 fcb
= (struct FCB
*) xfcb
->fcb
;
1476 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1477 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1478 if (handle
== INVALID_HANDLE_VALUE
) {
1479 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1482 AL_result
= 0x01; /* end of file, no data read */
1484 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1485 if (position
!= record_number
* fcb
->logical_record_size
) {
1486 TRACE("seek(%d, %d, 0) failed with %u\n",
1487 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1489 AL_result
= 0x01; /* end of file, no data read */
1491 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1492 records_requested
= CX_reg(context
);
1493 bytes_requested
= records_requested
* fcb
->logical_record_size
;
1494 bytes_read
= _lread((HFILE
) handle
, disk_transfer_area
, bytes_requested
);
1495 if (bytes_read
!= bytes_requested
) {
1496 TRACE("_lread(%d, %p, %d) failed with %d\n",
1497 fcb
->file_number
, disk_transfer_area
, bytes_requested
, bytes_read
);
1498 records_read
= bytes_read
/ fcb
->logical_record_size
;
1499 if (bytes_read
% fcb
->logical_record_size
== 0) {
1500 AL_result
= 0x01; /* end of file, no data read */
1503 memset(&disk_transfer_area
[bytes_read
], 0, records_read
* fcb
->logical_record_size
- bytes_read
);
1504 AL_result
= 0x03; /* end of file, partial record read */
1507 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1508 bytes_read
, record_number
, position
, fcb
->file_number
, handle
);
1509 records_read
= records_requested
;
1510 AL_result
= 0x00; /* successful */
1514 record_number
+= records_read
;
1515 memcpy(fcb
->random_access_record_number
, &record_number
, 4);
1516 fcb
->current_block_number
= record_number
/ 128;
1517 fcb
->record_within_current_block
= record_number
% 128;
1518 SET_CX(context
, records_read
);
1519 SET_AL(context
, AL_result
);
1523 /***********************************************************************
1524 * INT21_RandomBlockWriteToFCB
1526 * Handler for function 0x28.
1529 * CX [I/O] Number of records to write
1530 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1535 * 2: segment wrap in DTA (not returned now)
1538 * Writes several records with the size FCB->logical_record_size from
1539 * the disk transfer area to the FCB. The number of records to be
1540 * written is specified in the CX register. The position of the first
1541 * record is specified with FCB->random_access_record_number. The
1542 * FCB->random_access_record_number, the FCB->current_block_number
1543 * and FCB->record_within_current_block are updated to point to the
1544 * next record after the records written. The CX register is set to
1545 * the number of successfully written records.
1547 static void INT21_RandomBlockWriteToFCB( CONTEXT
*context
)
1552 DWORD record_number
;
1554 BYTE
*disk_transfer_area
;
1555 UINT records_requested
;
1556 UINT bytes_requested
;
1558 UINT records_written
;
1561 fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
1562 if (fcb
->drive_number
== 0xff) {
1563 xfcb
= (struct XFCB
*) fcb
;
1564 fcb
= (struct FCB
*) xfcb
->fcb
;
1567 memcpy(&record_number
, fcb
->random_access_record_number
, 4);
1568 handle
= DosFileHandleToWin32Handle((HFILE16
) fcb
->file_number
);
1569 if (handle
== INVALID_HANDLE_VALUE
) {
1570 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1572 records_written
= 0;
1573 AL_result
= 0x01; /* disk full */
1575 position
= SetFilePointer(handle
, record_number
* fcb
->logical_record_size
, NULL
, 0);
1576 if (position
!= record_number
* fcb
->logical_record_size
) {
1577 TRACE("seek(%d, %d, 0) failed with %u\n",
1578 fcb
->file_number
, record_number
* fcb
->logical_record_size
, position
);
1579 records_written
= 0;
1580 AL_result
= 0x01; /* disk full */
1582 disk_transfer_area
= INT21_GetCurrentDTA(context
);
1583 records_requested
= CX_reg(context
);
1584 bytes_requested
= records_requested
* fcb
->logical_record_size
;
1585 bytes_written
= _lwrite((HFILE
) handle
, (LPCSTR
)disk_transfer_area
, bytes_requested
);
1586 if (bytes_written
!= bytes_requested
) {
1587 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1588 fcb
->file_number
, disk_transfer_area
, bytes_requested
, bytes_written
);
1589 records_written
= bytes_written
/ fcb
->logical_record_size
;
1590 AL_result
= 0x01; /* disk full */
1592 TRACE("successful write %d bytes from record %d (position %u) of file %d (handle %p)\n",
1593 bytes_written
, record_number
, position
, fcb
->file_number
, handle
);
1594 records_written
= records_requested
;
1595 AL_result
= 0x00; /* successful */
1599 record_number
+= records_written
;
1600 memcpy(fcb
->random_access_record_number
, &record_number
, 4);
1601 fcb
->current_block_number
= record_number
/ 128;
1602 fcb
->record_within_current_block
= record_number
% 128;
1603 SET_CX(context
, records_written
);
1604 SET_AL(context
, AL_result
);
1608 /***********************************************************************
1609 * INT21_CreateDirectory
1613 * - subfunction 0x39 of function 0x71
1614 * - subfunction 0xff of function 0x43 (CL == 0x39)
1616 static BOOL
INT21_CreateDirectory( CONTEXT
*context
)
1618 WCHAR dirW
[MAX_PATH
];
1619 char *dirA
= CTX_SEG_OFF_TO_LIN(context
,
1623 TRACE( "CREATE DIRECTORY %s\n", dirA
);
1625 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
1627 if (CreateDirectoryW(dirW
, NULL
))
1631 * FIXME: CreateDirectory's LastErrors will clash with the ones
1632 * used by DOS. AH=39 only returns 3 (path not found) and
1633 * 5 (access denied), while CreateDirectory return several
1634 * ones. Remap some of them. -Marcus
1636 switch (GetLastError())
1638 case ERROR_ALREADY_EXISTS
:
1639 case ERROR_FILENAME_EXCED_RANGE
:
1640 case ERROR_DISK_FULL
:
1641 SetLastError(ERROR_ACCESS_DENIED
);
1651 /***********************************************************************
1652 * INT21_ExtendedCountryInformation
1654 * Handler for function 0x65.
1656 static void INT21_ExtendedCountryInformation( CONTEXT
*context
)
1658 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN( context
, context
->SegEs
, context
->Edi
);
1659 BYTE buffsize
= CX_reg (context
);
1661 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1665 * Check subfunctions that are passed country and code page.
1667 if (AL_reg(context
) >= 0x01 && AL_reg(context
) <= 0x07)
1669 WORD country
= DX_reg(context
);
1670 WORD codepage
= BX_reg(context
);
1672 if (country
!= 0xffff && country
!= INT21_GetSystemCountryCode())
1673 FIXME( "Requested info on non-default country %04x\n", country
);
1675 if (codepage
!= 0xffff && codepage
!= GetOEMCP())
1676 FIXME( "Requested info on non-default code page %04x\n", codepage
);
1679 switch (AL_reg(context
)) {
1680 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1681 INT_BARF( context
, 0x21 );
1682 SET_CFLAG( context
);
1685 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1686 TRACE( "Get general internationalization info\n" );
1687 dataptr
[0] = 0x01; /* Info ID */
1688 *(WORD
*)(dataptr
+1) = 38; /* Size of the following info */
1689 *(WORD
*)(dataptr
+3) = INT21_GetSystemCountryCode(); /* Country ID */
1690 *(WORD
*)(dataptr
+5) = GetOEMCP(); /* Code page */
1691 /* FIXME: fill buffer partially up to buffsize bytes*/
1692 if (buffsize
>= 0x29){
1693 INT21_FillCountryInformation( dataptr
+ 7 );
1694 SET_CX( context
, 0x29 ); /* Size of returned info */
1696 SET_CX( context
, 0x07 ); /* Size of returned info */
1700 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1701 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1702 TRACE( "Get pointer to uppercase table\n" );
1703 dataptr
[0] = AL_reg(context
); /* Info ID */
1704 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1705 offsetof(INT21_HEAP
, uppercase_size
) );
1706 SET_CX( context
, 5 ); /* Size of returned info */
1709 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1710 TRACE( "Get pointer to lowercase table\n" );
1711 dataptr
[0] = 0x03; /* Info ID */
1712 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1713 offsetof(INT21_HEAP
, lowercase_size
) );
1714 SET_CX( context
, 5 ); /* Size of returned info */
1717 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1718 TRACE("Get pointer to filename terminator table\n");
1719 dataptr
[0] = 0x05; /* Info ID */
1720 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1721 offsetof(INT21_HEAP
, filename_size
) );
1722 SET_CX( context
, 5 ); /* Size of returned info */
1725 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1726 TRACE("Get pointer to collating sequence table\n");
1727 dataptr
[0] = 0x06; /* Info ID */
1728 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1729 offsetof(INT21_HEAP
, collating_size
) );
1730 SET_CX( context
, 5 ); /* Size of returned info */
1733 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1734 TRACE("Get pointer to DBCS lead byte table\n");
1735 dataptr
[0] = 0x07; /* Info ID */
1736 *(DWORD
*)(dataptr
+1) = MAKESEGPTR( INT21_GetHeapSelector( context
),
1737 offsetof(INT21_HEAP
, dbcs_size
) );
1738 SET_CX( context
, 5 ); /* Size of returned info */
1741 case 0x20: /* CAPITALIZE CHARACTER */
1742 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1743 TRACE("Convert char to uppercase\n");
1744 SET_DL( context
, toupper(DL_reg(context
)) );
1747 case 0x21: /* CAPITALIZE STRING */
1748 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1749 TRACE("Convert string to uppercase with length\n");
1751 char *ptr
= CTX_SEG_OFF_TO_LIN( context
, context
->SegDs
,
1753 WORD len
= CX_reg(context
);
1754 while (len
--) { *ptr
= toupper(*ptr
); ptr
++; }
1758 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1759 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1760 TRACE("Convert ASCIIZ string to uppercase\n");
1762 char *p
= CTX_SEG_OFF_TO_LIN( context
, context
->SegDs
, context
->Edx
);
1763 for ( ; *p
; p
++) *p
= toupper(*p
);
1767 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1768 INT_BARF( context
, 0x21 );
1769 SET_CFLAG( context
);
1773 INT_BARF( context
, 0x21 );
1780 /***********************************************************************
1781 * INT21_FileAttributes
1785 * - subfunction 0x43 of function 0x71
1787 static BOOL
INT21_FileAttributes( CONTEXT
*context
,
1791 WCHAR fileW
[MAX_PATH
];
1792 char *fileA
= CTX_SEG_OFF_TO_LIN(context
,
1802 switch (subfunction
)
1804 case 0x00: /* GET FILE ATTRIBUTES */
1805 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA
);
1806 len
= MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1808 /* Winbench 96 Disk Test fails if we don't complain
1809 * about a filename that ends in \
1811 if (!len
|| (fileW
[len
-1] == '/') || (fileW
[len
-1] == '\\'))
1814 result
= GetFileAttributesW( fileW
);
1815 if (result
== INVALID_FILE_ATTRIBUTES
)
1819 SET_CX( context
, (WORD
)result
);
1821 SET_AX( context
, (WORD
)result
); /* DR DOS */
1825 case 0x01: /* SET FILE ATTRIBUTES */
1826 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
1827 CX_reg(context
), fileA
);
1828 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1830 if (!SetFileAttributesW( fileW
, CX_reg(context
) ))
1834 case 0x02: /* GET COMPRESSED FILE SIZE */
1835 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA
);
1836 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1838 result
= GetCompressedFileSizeW( fileW
, NULL
);
1839 if (result
== INVALID_FILE_SIZE
)
1843 SET_AX( context
, LOWORD(result
) );
1844 SET_DX( context
, HIWORD(result
) );
1848 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
1850 INT_BARF( context
, 0x21 );
1853 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA
);
1854 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1856 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
1857 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1858 NULL
, OPEN_EXISTING
, 0, 0 );
1859 if (handle
== INVALID_HANDLE_VALUE
)
1862 DosDateTimeToFileTime( DI_reg(context
),
1865 status
= SetFileTime( handle
, NULL
, NULL
, &filetime
);
1867 CloseHandle( handle
);
1872 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
1874 INT_BARF( context
, 0x21 );
1877 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA
);
1878 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1880 handle
= CreateFileW( fileW
, GENERIC_READ
,
1881 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1882 NULL
, OPEN_EXISTING
, 0, 0 );
1883 if (handle
== INVALID_HANDLE_VALUE
)
1886 status
= GetFileTime( handle
, NULL
, NULL
, &filetime
);
1889 FileTimeToDosDateTime( &filetime
, &date
, &time
);
1890 SET_DI( context
, date
);
1891 SET_CX( context
, time
);
1894 CloseHandle( handle
);
1899 case 0x05: /* SET FILE LAST ACCESS DATE */
1901 INT_BARF( context
, 0x21 );
1904 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA
);
1905 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1907 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
1908 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1909 NULL
, OPEN_EXISTING
, 0, 0 );
1910 if (handle
== INVALID_HANDLE_VALUE
)
1913 DosDateTimeToFileTime( DI_reg(context
),
1916 status
= SetFileTime( handle
, NULL
, &filetime
, NULL
);
1918 CloseHandle( handle
);
1923 case 0x06: /* GET FILE LAST ACCESS DATE */
1925 INT_BARF( context
, 0x21 );
1928 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA
);
1929 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1931 handle
= CreateFileW( fileW
, GENERIC_READ
,
1932 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1933 NULL
, OPEN_EXISTING
, 0, 0 );
1934 if (handle
== INVALID_HANDLE_VALUE
)
1937 status
= GetFileTime( handle
, NULL
, &filetime
, NULL
);
1940 FileTimeToDosDateTime( &filetime
, &date
, NULL
);
1941 SET_DI( context
, date
);
1944 CloseHandle( handle
);
1949 case 0x07: /* SET FILE CREATION DATE AND TIME */
1951 INT_BARF( context
, 0x21 );
1954 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA
);
1955 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1957 handle
= CreateFileW( fileW
, GENERIC_WRITE
,
1958 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1959 NULL
, OPEN_EXISTING
, 0, 0 );
1960 if (handle
== INVALID_HANDLE_VALUE
)
1964 * FIXME: SI has number of 10-millisecond units past time in CX.
1966 DosDateTimeToFileTime( DI_reg(context
),
1969 status
= SetFileTime( handle
, &filetime
, NULL
, NULL
);
1971 CloseHandle( handle
);
1976 case 0x08: /* GET FILE CREATION DATE AND TIME */
1978 INT_BARF( context
, 0x21 );
1981 TRACE( "GET FILE CREATION DATE AND TIME, file %s\n", fileA
);
1982 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
1984 handle
= CreateFileW( fileW
, GENERIC_READ
,
1985 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1986 NULL
, OPEN_EXISTING
, 0, 0 );
1987 if (handle
== INVALID_HANDLE_VALUE
)
1990 status
= GetFileTime( handle
, &filetime
, NULL
, NULL
);
1993 FileTimeToDosDateTime( &filetime
, &date
, &time
);
1994 SET_DI( context
, date
);
1995 SET_CX( context
, time
);
1997 * FIXME: SI has number of 10-millisecond units past
2000 SET_SI( context
, 0 );
2003 CloseHandle(handle
);
2008 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2009 if (islong
|| context
->Ebp
!= 0x5053)
2010 INT_BARF( context
, 0x21 );
2013 switch(CL_reg(context
))
2016 if (!INT21_CreateDirectory( context
))
2021 if (!INT21_RenameFile( context
))
2026 INT_BARF( context
, 0x21 );
2032 INT_BARF( context
, 0x21 );
2039 /***********************************************************************
2040 * INT21_FileDateTime
2042 * Handler for function 0x57.
2044 static BOOL
INT21_FileDateTime( CONTEXT
*context
)
2046 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2050 switch (AL_reg(context
)) {
2051 case 0x00: /* Get last-written stamp */
2052 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2055 if (!GetFileTime( handle
, NULL
, NULL
, &filetime
))
2057 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2058 SET_DX( context
, date
);
2059 SET_CX( context
, time
);
2063 case 0x01: /* Set last-written stamp */
2064 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2067 DosDateTimeToFileTime( DX_reg(context
),
2070 if (!SetFileTime( handle
, NULL
, NULL
, &filetime
))
2075 case 0x04: /* Get last access stamp, DOS 7 */
2076 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2079 if (!GetFileTime( handle
, NULL
, &filetime
, NULL
))
2081 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2082 SET_DX( context
, date
);
2083 SET_CX( context
, time
);
2087 case 0x05: /* Set last access stamp, DOS 7 */
2088 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2091 DosDateTimeToFileTime( DX_reg(context
),
2094 if (!SetFileTime( handle
, NULL
, &filetime
, NULL
))
2099 case 0x06: /* Get creation stamp, DOS 7 */
2100 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2103 if (!GetFileTime( handle
, &filetime
, NULL
, NULL
))
2105 FileTimeToDosDateTime( &filetime
, &date
, &time
);
2106 SET_DX( context
, date
);
2107 SET_CX( context
, time
);
2109 * FIXME: SI has number of 10-millisecond units past time in CX.
2111 SET_SI( context
, 0 );
2115 case 0x07: /* Set creation stamp, DOS 7 */
2116 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2120 * FIXME: SI has number of 10-millisecond units past time in CX.
2122 DosDateTimeToFileTime( DX_reg(context
),
2125 if (!SetFileTime( handle
, &filetime
, NULL
, NULL
))
2131 INT_BARF( context
, 0x21 );
2139 /***********************************************************************
2142 * Handler for functions 0x51 and 0x62.
2144 static void INT21_GetPSP( CONTEXT
*context
)
2146 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context
) );
2148 SET_BX( context
, LOWORD(GetCurrentPDB16()) );
2151 static inline void setword( BYTE
*ptr
, WORD w
)
2154 ptr
[1] = (BYTE
)(w
>> 8);
2157 static void CreateBPB(int drive
, BYTE
*data
, BOOL16 limited
)
2158 /* limited == TRUE is used with INT 0x21/0x440d */
2160 /* FIXME: we're forcing some values without checking that those are valid */
2165 setword(&data
[3], 0);
2167 setword(&data
[6], 240);
2168 setword(&data
[8], 64000);
2170 setword(&data
[0x0b], 40);
2171 setword(&data
[0x0d], 56);
2172 setword(&data
[0x0f], 2);
2173 setword(&data
[0x11], 0);
2176 setword(&data
[0x1f], 800);
2178 setword(&data
[0x22], 1);
2185 setword(&data
[3], 0);
2187 setword(&data
[6], 240);
2188 setword(&data
[8], 2880);
2190 setword(&data
[0x0b], 6);
2191 setword(&data
[0x0d], 18);
2192 setword(&data
[0x0f], 2);
2193 setword(&data
[0x11], 0);
2196 setword(&data
[0x1f], 80);
2198 setword(&data
[0x22], 2);
2203 static inline DWORD
INT21_Ioctl_CylHeadSect2Lin(DWORD cyl
, WORD head
, WORD sec
, WORD cyl_cnt
,
2204 WORD head_cnt
, WORD sec_cnt
)
2206 DWORD res
= (cyl
* head_cnt
*sec_cnt
+ head
* sec_cnt
+ sec
);
2210 /***********************************************************************
2213 * Handler for block device IOCTLs.
2215 static void INT21_Ioctl_Block( CONTEXT
*context
)
2218 BYTE drive
= INT21_MapDrive( BL_reg(context
) );
2219 WCHAR drivespec
[] = {'A', ':', '\\', 0};
2222 drivespec
[0] += drive
;
2223 drivetype
= GetDriveTypeW( drivespec
);
2225 RESET_CFLAG(context
);
2226 if (drivetype
== DRIVE_UNKNOWN
|| drivetype
== DRIVE_NO_ROOT_DIR
)
2228 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2229 AL_reg(context
), 'A' + drive
);
2230 SetLastError( ERROR_INVALID_DRIVE
);
2231 SET_AX( context
, ERROR_INVALID_DRIVE
);
2232 SET_CFLAG( context
);
2236 switch (AL_reg(context
))
2238 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2239 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2240 INT_BARF( context
, 0x21 );
2243 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2244 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2247 if (drivetype
== DRIVE_REMOVABLE
)
2248 SET_AX( context
, 0 ); /* removable */
2250 SET_AX( context
, 1 ); /* not removable */
2253 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2254 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2257 if (drivetype
== DRIVE_REMOTE
)
2258 SET_DX( context
, (1<<9) | (1<<12) ); /* remote + no direct IO */
2259 else if (drivetype
== DRIVE_CDROM
)
2260 /* CDROM should be set to remote. If it set the app will
2261 * call int2f to check if it cdrom or remote drive. */
2262 SET_DX( context
, (1<<12) );
2263 else if (drivetype
== DRIVE_FIXED
)
2264 /* This should define if drive support 0x0d, 0x0f and 0x08
2265 * requests. The local fixed drive should do. */
2266 SET_DX( context
, (1<<11) );
2268 SET_DX( context
, 0 ); /* FIXME: use driver attr here */
2271 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2272 /* Get pointer to IOCTL parameter block */
2273 dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2275 switch (CX_reg(context
))
2277 case 0x0841: /* write logical device track */
2278 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2281 WORD head
= *(WORD
*)(dataptr
+1);
2282 WORD cyl
= *(WORD
*)(dataptr
+3);
2283 WORD sect
= *(WORD
*)(dataptr
+5);
2284 WORD nrsect
= *(WORD
*)(dataptr
+7);
2285 BYTE
*data
= CTX_SEG_OFF_TO_LIN(context
, *(WORD
*)(dataptr
+11), *(WORD
*)(dataptr
+9));
2286 WORD cyl_cnt
, head_cnt
, sec_cnt
;
2288 /* FIXME: we're faking some values here */
2294 SET_AX( context
, ERROR_WRITE_FAULT
);
2305 if (!DOSVM_RawWrite(drive
, INT21_Ioctl_CylHeadSect2Lin(cyl
, head
, sect
, cyl_cnt
, head_cnt
, sec_cnt
), nrsect
, data
, FALSE
))
2307 SET_AX( context
, ERROR_WRITE_FAULT
);
2313 case 0x084a: /* lock logical volume */
2314 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2315 BH_reg(context
), DX_reg(context
), 'A' + drive
);
2318 case 0x0860: /* get device parameters */
2319 /* FIXME: we're faking some values here */
2320 /* used by w4wgrp's winfile */
2321 memset(dataptr
, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2323 dataptr
[6] = 0; /* media type */
2326 dataptr
[1] = 0x05; /* fixed disk */
2327 setword(&dataptr
[2], 0x01); /* non removable */
2328 setword(&dataptr
[4], 0x300); /* # of cylinders */
2332 dataptr
[1] = 0x07; /* block dev, floppy */
2333 setword(&dataptr
[2], 0x02); /* removable */
2334 setword(&dataptr
[4], 80); /* # of cylinders */
2336 CreateBPB(drive
, &dataptr
[7], TRUE
);
2337 RESET_CFLAG(context
);
2340 case 0x0861: /* read logical device track */
2341 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2344 WORD head
= *(WORD
*)(dataptr
+1);
2345 WORD cyl
= *(WORD
*)(dataptr
+3);
2346 WORD sect
= *(WORD
*)(dataptr
+5);
2347 WORD nrsect
= *(WORD
*)(dataptr
+7);
2348 BYTE
*data
= CTX_SEG_OFF_TO_LIN(context
, *(WORD
*)(dataptr
+11), *(WORD
*)(dataptr
+9));
2349 WORD cyl_cnt
, head_cnt
, sec_cnt
;
2351 /* FIXME: we're faking some values here */
2365 if (!DOSVM_RawRead(drive
, INT21_Ioctl_CylHeadSect2Lin(cyl
, head
, sect
, cyl_cnt
, head_cnt
, sec_cnt
), nrsect
, data
, FALSE
))
2367 SET_AX( context
, ERROR_READ_FAULT
);
2373 case 0x0866: /* get volume serial number */
2375 WCHAR label
[12],fsname
[9];
2377 TRACE( "GENERIC IOCTL - Get media id - %c:\n",
2380 GetVolumeInformationW(drivespec
, label
, 12, &serial
, NULL
, NULL
, fsname
, 9);
2381 *(WORD
*)dataptr
= 0;
2382 memcpy(dataptr
+2,&serial
,4);
2383 WideCharToMultiByte(CP_OEMCP
, 0, label
, 11, (LPSTR
)dataptr
+ 6, 11, NULL
, NULL
);
2384 WideCharToMultiByte(CP_OEMCP
, 0, fsname
, 8, (LPSTR
)dataptr
+ 17, 8, NULL
, NULL
);
2388 case 0x086a: /* unlock logical volume */
2389 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2393 case 0x086f: /* get drive map information */
2394 memset(dataptr
+1, '\0', dataptr
[0]-1);
2395 dataptr
[1] = dataptr
[0];
2396 dataptr
[2] = 0x07; /* protected mode driver; no eject; no notification */
2397 dataptr
[3] = 0xFF; /* no physical drive */
2401 /* Trial and error implementation */
2402 SET_AX( context
, drivetype
== DRIVE_UNKNOWN
? 0x0f : 0x01 );
2403 SET_CFLAG(context
); /* Seems to be set all the time */
2407 INT_BARF( context
, 0x21 );
2411 case 0x0e: /* GET LOGICAL DRIVE MAP */
2412 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2414 /* FIXME: this is not correct if drive has mappings */
2415 SET_AL( context
, 0 ); /* drive has no mapping */
2418 case 0x0f: /* SET LOGICAL DRIVE MAP */
2419 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2420 INT21_DriveName( BL_reg(context
)));
2421 /* FIXME: as of today, we don't support logical drive mapping... */
2422 SET_AL( context
, 0 );
2425 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2427 INT_BARF( context
, 0x21 );
2432 /***********************************************************************
2435 * Handler for character device IOCTLs.
2437 static void INT21_Ioctl_Char( CONTEXT
*context
)
2439 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2440 BOOL IsConsoleIOHandle
= VerifyConsoleIoHandle( handle
);
2442 switch (AL_reg(context
))
2444 case 0x00: /* GET DEVICE INFORMATION */
2445 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context
) );
2446 if (IsConsoleIOHandle
|| GetFileType(handle
) == FILE_TYPE_CHAR
)
2449 * Returns attribute word in DX:
2450 * Bit 14 - Device driver can process IOCTL requests.
2451 * Bit 13 - Output until busy supported.
2452 * Bit 11 - Driver supports OPEN/CLOSE calls.
2454 * Bit 7 - Set (indicates device).
2455 * Bit 6 - EOF on input.
2456 * Bit 5 - Raw (binary) mode.
2457 * Bit 4 - Device is special (uses int29).
2458 * Bit 3 - Clock device.
2459 * Bit 2 - NUL device.
2460 * Bit 1 - Console output device.
2461 * Bit 0 - Console input device.
2463 SET_DX( context
, IsConsoleIOHandle
? 0x80c3 : 0x80c0 /* FIXME */ );
2468 * Returns attribute word in DX:
2469 * Bit 15 - File is remote.
2470 * Bit 14 - Don't set file date/time on closing.
2471 * Bit 11 - Media not removable.
2472 * Bit 8 - Generate int24 if no disk space on write
2473 * or read past end of file
2474 * Bit 7 - Clear (indicates file).
2475 * Bit 6 - File has not been written.
2476 * Bit 5..0 - Drive number (0=A:,...)
2478 * FIXME: Should check if file is on remote or removable drive.
2479 * FIXME: Should use drive file is located on (and not current).
2481 SET_DX( context
, 0x0140 + INT21_GetCurrentDrive() );
2485 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2486 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context
) );
2488 * Returns attribute word in DX:
2489 * Bit 15 - Set if remote.
2490 * Bit 14 - Set if date/time not set on close.
2492 * FIXME: Should check if file is on remote drive.
2494 SET_DX( context
, 0 );
2497 case 0x01: /* SET DEVICE INFORMATION */
2498 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2499 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2500 case 0x06: /* GET INPUT STATUS */
2501 case 0x07: /* GET OUTPUT STATUS */
2502 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2503 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2505 INT_BARF( context
, 0x21 );
2511 /***********************************************************************
2514 * Handler for function 0x44.
2516 static void INT21_Ioctl( CONTEXT
*context
)
2518 switch (AL_reg(context
))
2524 INT21_Ioctl_Char( context
);
2529 INT21_Ioctl_Block( context
);
2534 INT21_Ioctl_Char( context
);
2539 INT21_Ioctl_Block( context
);
2543 INT21_Ioctl_Char( context
);
2546 case 0x0b: /* SET SHARING RETRY COUNT */
2547 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2548 CX_reg(context
), DX_reg(context
) );
2549 if (!CX_reg(context
))
2551 SET_AX( context
, 1 );
2552 SET_CFLAG( context
);
2556 RESET_CFLAG( context
);
2561 INT21_Ioctl_Char( context
);
2567 INT21_Ioctl_Block( context
);
2571 INT21_Ioctl_Char( context
);
2575 INT21_Ioctl_Block( context
);
2578 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2579 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2580 SET_CFLAG(context
); /* Error / This is not DR DOS. */
2581 SET_AX( context
, 0x0001 ); /* Invalid function */
2584 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2585 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2586 SET_CFLAG(context
); /* Error / This is not DR DOS. */
2587 SET_AX( context
, 0x0001 ); /* Invalid function */
2590 case 0xe0: /* Sun PC-NFS API */
2591 TRACE( "Sun PC-NFS API\n" );
2596 INT_BARF( context
, 0x21 );
2601 /***********************************************************************
2604 * Handler for function 0x73.
2606 static BOOL
INT21_Fat32( CONTEXT
*context
)
2608 switch (AL_reg(context
))
2610 case 0x02: /* FAT32 - GET EXTENDED DPB */
2612 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
2613 WORD
*ptr
= CTX_SEG_OFF_TO_LIN(context
,
2614 context
->SegEs
, context
->Edi
);
2615 INT21_DPB
*target
= (INT21_DPB
*)(ptr
+ 1);
2618 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context
) );
2620 if ( CX_reg(context
) < sizeof(INT21_DPB
) + 2 || *ptr
< sizeof(INT21_DPB
) )
2622 SetLastError( ERROR_BAD_LENGTH
);
2626 if ( !INT21_FillDrivePB( drive
) )
2628 SetLastError( ERROR_INVALID_DRIVE
);
2632 source
= &INT21_GetHeapPointer()->misc_dpb_list
[drive
];
2634 *ptr
= sizeof(INT21_DPB
);
2637 if (LOWORD(context
->Esi
) != 0xF1A6)
2639 target
->driver_header
= 0;
2644 FIXME( "Caller requested driver and next DPB pointers!\n" );
2649 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2651 WCHAR dirW
[MAX_PATH
];
2652 char *dirA
= CTX_SEG_OFF_TO_LIN( context
,
2653 context
->SegDs
, context
->Edx
);
2654 BYTE
*data
= CTX_SEG_OFF_TO_LIN( context
,
2655 context
->SegEs
, context
->Edi
);
2656 DWORD cluster_sectors
;
2658 DWORD free_clusters
;
2659 DWORD total_clusters
;
2661 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA
);
2662 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
2664 if (CX_reg(context
) < 44)
2666 SetLastError( ERROR_BAD_LENGTH
);
2670 if (!GetDiskFreeSpaceW( dirW
, &cluster_sectors
, §or_bytes
,
2671 &free_clusters
, &total_clusters
))
2674 *(WORD
*) (data
+ 0) = 44; /* size of structure */
2675 *(WORD
*) (data
+ 2) = 0; /* version */
2676 *(DWORD
*)(data
+ 4) = cluster_sectors
;
2677 *(DWORD
*)(data
+ 8) = sector_bytes
;
2678 *(DWORD
*)(data
+ 12) = free_clusters
;
2679 *(DWORD
*)(data
+ 16) = total_clusters
;
2682 * Below we have free/total sectors and
2683 * free/total allocation units without adjustment
2684 * for compression. We fake both using cluster information.
2686 *(DWORD
*)(data
+ 20) = free_clusters
* cluster_sectors
;
2687 *(DWORD
*)(data
+ 24) = total_clusters
* cluster_sectors
;
2688 *(DWORD
*)(data
+ 28) = free_clusters
;
2689 *(DWORD
*)(data
+ 32) = total_clusters
;
2692 * Between (data + 36) and (data + 43) there
2693 * are eight reserved bytes.
2699 INT_BARF( context
, 0x21 );
2705 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA
*dataA
,
2706 const WIN32_FIND_DATAW
*dataW
)
2708 dataA
->dwFileAttributes
= dataW
->dwFileAttributes
;
2709 dataA
->ftCreationTime
= dataW
->ftCreationTime
;
2710 dataA
->ftLastAccessTime
= dataW
->ftLastAccessTime
;
2711 dataA
->ftLastWriteTime
= dataW
->ftLastWriteTime
;
2712 dataA
->nFileSizeHigh
= dataW
->nFileSizeHigh
;
2713 dataA
->nFileSizeLow
= dataW
->nFileSizeLow
;
2714 WideCharToMultiByte( CP_OEMCP
, 0, dataW
->cFileName
, -1,
2715 dataA
->cFileName
, sizeof(dataA
->cFileName
), NULL
, NULL
);
2716 WideCharToMultiByte( CP_OEMCP
, 0, dataW
->cAlternateFileName
, -1,
2717 dataA
->cAlternateFileName
, sizeof(dataA
->cAlternateFileName
), NULL
, NULL
);
2720 /***********************************************************************
2721 * INT21_LongFilename
2723 * Handler for function 0x71.
2725 static void INT21_LongFilename( CONTEXT
*context
)
2727 BOOL bSetDOSExtendedError
= FALSE
;
2728 WCHAR pathW
[MAX_PATH
];
2731 switch (AL_reg(context
))
2733 case 0x0d: /* RESET DRIVE */
2734 INT_BARF( context
, 0x21 );
2737 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
2738 if (!INT21_CreateDirectory( context
))
2739 bSetDOSExtendedError
= TRUE
;
2742 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
2743 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2745 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA
);
2746 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2747 if (!RemoveDirectoryW( pathW
)) bSetDOSExtendedError
= TRUE
;
2750 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
2751 if (!INT21_SetCurrentDirectory( context
))
2752 bSetDOSExtendedError
= TRUE
;
2755 case 0x41: /* LONG FILENAME - DELETE FILE */
2756 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2758 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA
);
2759 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2761 if (!DeleteFileW( pathW
)) bSetDOSExtendedError
= TRUE
;
2764 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
2765 if (!INT21_FileAttributes( context
, BL_reg(context
), TRUE
))
2766 bSetDOSExtendedError
= TRUE
;
2769 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
2770 if (!INT21_GetCurrentDirectory( context
, TRUE
))
2771 bSetDOSExtendedError
= TRUE
;
2774 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
2778 WIN32_FIND_DATAW dataW
;
2779 WIN32_FIND_DATAA
* dataA
;
2781 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Edx
);
2782 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA
);
2784 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2785 handle
= FindFirstFileW(pathW
, &dataW
);
2787 dataA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
2788 if (handle
!= INVALID_HANDLE_VALUE
&&
2789 (h16
= GlobalAlloc16(GMEM_MOVEABLE
, sizeof(handle
))))
2791 HANDLE
* ptr
= GlobalLock16( h16
);
2793 GlobalUnlock16( h16
);
2794 SET_AX( context
, h16
);
2795 INT21_ConvertFindDataWtoA(dataA
, &dataW
);
2799 if (handle
!= INVALID_HANDLE_VALUE
) FindClose(handle
);
2800 SET_AX( context
, INVALID_HANDLE_VALUE16
);
2801 bSetDOSExtendedError
= TRUE
;
2806 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
2808 HGLOBAL16 h16
= BX_reg(context
);
2810 WIN32_FIND_DATAW dataW
;
2811 WIN32_FIND_DATAA
* dataA
;
2813 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
2816 dataA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
2818 if (h16
!= INVALID_HANDLE_VALUE16
&& (ptr
= GlobalLock16( h16
)))
2820 if (!FindNextFileW(*ptr
, &dataW
)) bSetDOSExtendedError
= TRUE
;
2821 else INT21_ConvertFindDataWtoA(dataA
, &dataW
);
2822 GlobalUnlock16( h16
);
2826 SetLastError( ERROR_INVALID_HANDLE
);
2827 bSetDOSExtendedError
= TRUE
;
2832 case 0x56: /* LONG FILENAME - RENAME FILE */
2833 if (!INT21_RenameFile(context
))
2834 bSetDOSExtendedError
= TRUE
;
2837 case 0x60: /* LONG FILENAME - CONVERT PATH */
2839 WCHAR res
[MAX_PATH
];
2841 switch (CL_reg(context
))
2843 case 0x00: /* "truename" - Canonicalize path */
2845 * FIXME: This is not 100% equal to 0x01 case,
2846 * if you fix this, fix int21 subfunction 0x60, too.
2849 case 0x01: /* Get short filename or path */
2850 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
2851 if (!GetShortPathNameW(pathW
, res
, 67))
2852 bSetDOSExtendedError
= TRUE
;
2855 SET_AX( context
, 0 );
2856 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
2857 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
2862 case 0x02: /* Get canonical long filename or path */
2863 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
2864 if (!GetFullPathNameW(pathW
, 128, res
, NULL
))
2865 bSetDOSExtendedError
= TRUE
;
2868 SET_AX( context
, 0 );
2869 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
2870 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
2875 FIXME("Unimplemented long file name function:\n");
2876 INT_BARF( context
, 0x21 );
2878 SET_AL( context
, 0 );
2884 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
2885 if (!INT21_CreateFile( context
, context
->Esi
, TRUE
,
2886 BX_reg(context
), DL_reg(context
) ))
2887 bSetDOSExtendedError
= TRUE
;
2890 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
2892 DWORD filename_len
, flags
;
2895 pathA
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Edx
);
2897 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA
);
2898 SET_AX( context
, 0 );
2899 MultiByteToWideChar(CP_OEMCP
, 0, pathA
, -1, pathW
, MAX_PATH
);
2900 if (!GetVolumeInformationW( pathW
, NULL
, 0, NULL
, &filename_len
,
2903 INT_BARF( context
, 0x21 );
2907 SET_BX( context
, flags
| 0x4000 ); /* support for LFN functions */
2908 SET_CX( context
, filename_len
);
2909 SET_DX( context
, MAX_PATH
); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
2910 WideCharToMultiByte(CP_OEMCP
, 0, dstW
, -1,
2911 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
2916 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
2918 HGLOBAL16 h16
= BX_reg(context
);
2921 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
2923 if (h16
!= INVALID_HANDLE_VALUE16
&& (ptr
= GlobalLock16( h16
)))
2925 if (!FindClose( *ptr
)) bSetDOSExtendedError
= TRUE
;
2926 GlobalUnlock16( h16
);
2927 GlobalFree16( h16
);
2931 SetLastError( ERROR_INVALID_HANDLE
);
2932 bSetDOSExtendedError
= TRUE
;
2937 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
2939 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
2940 BY_HANDLE_FILE_INFORMATION
*info
=
2941 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
2943 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
2945 if (!GetFileInformationByHandle(handle
, info
))
2946 bSetDOSExtendedError
= TRUE
;
2950 case 0xa7: /* LONG FILENAME - CONVERT TIME */
2951 switch (BL_reg(context
))
2953 case 0x00: /* FILE TIME TO DOS TIME */
2956 FILETIME
*filetime
= CTX_SEG_OFF_TO_LIN(context
,
2960 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
2962 FileTimeToDosDateTime( filetime
, &date
, &time
);
2964 SET_DX( context
, date
);
2965 SET_CX( context
, time
);
2968 * FIXME: BH has number of 10-millisecond units
2971 SET_BH( context
, 0 );
2975 case 0x01: /* DOS TIME TO FILE TIME */
2977 FILETIME
*filetime
= CTX_SEG_OFF_TO_LIN(context
,
2981 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
2984 * FIXME: BH has number of 10-millisecond units
2987 DosDateTimeToFileTime( DX_reg(context
), CX_reg(context
),
2993 INT_BARF( context
, 0x21 );
2998 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
2999 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3000 case 0xaa: /* LONG FILENAME - SUBST */
3002 FIXME("Unimplemented long file name function:\n");
3003 INT_BARF( context
, 0x21 );
3005 SET_AL( context
, 0 );
3009 if (bSetDOSExtendedError
)
3011 SET_AX( context
, GetLastError() );
3012 SET_CFLAG( context
);
3017 /***********************************************************************
3022 * - subfunction 0x56 of function 0x71
3023 * - subfunction 0xff of function 0x43 (CL == 0x56)
3025 static BOOL
INT21_RenameFile( CONTEXT
*context
)
3027 WCHAR fromW
[MAX_PATH
];
3028 WCHAR toW
[MAX_PATH
];
3029 char *fromA
= CTX_SEG_OFF_TO_LIN(context
,
3030 context
->SegDs
,context
->Edx
);
3031 char *toA
= CTX_SEG_OFF_TO_LIN(context
,
3032 context
->SegEs
,context
->Edi
);
3034 TRACE( "RENAME FILE %s to %s\n", fromA
, toA
);
3035 MultiByteToWideChar(CP_OEMCP
, 0, fromA
, -1, fromW
, MAX_PATH
);
3036 MultiByteToWideChar(CP_OEMCP
, 0, toA
, -1, toW
, MAX_PATH
);
3038 return MoveFileW( fromW
, toW
);
3042 /***********************************************************************
3048 static BOOL
INT21_NetworkFunc (CONTEXT
*context
)
3050 switch (AL_reg(context
))
3052 case 0x00: /* Get machine name. */
3054 WCHAR dstW
[MAX_COMPUTERNAME_LENGTH
+ 1];
3055 DWORD s
= ARRAY_SIZE(dstW
);
3058 char *dst
= CTX_SEG_OFF_TO_LIN (context
,context
->SegDs
,context
->Edx
);
3059 TRACE("getting machine name to %p\n", dst
);
3060 if (!GetComputerNameW(dstW
, &s
) ||
3061 !WideCharToMultiByte(CP_OEMCP
, 0, dstW
, -1, dst
, 16, NULL
, NULL
))
3064 SetLastError( ER_NoNetwork
);
3067 for (len
= strlen(dst
); len
< 15; len
++) dst
[len
] = ' ';
3069 SET_CH( context
, 1 ); /* Valid */
3070 SET_CL( context
, 1 ); /* NETbios number??? */
3071 TRACE("returning %s\n", debugstr_an(dst
, 16));
3076 SetLastError( ER_NoNetwork
);
3081 /******************************************************************
3082 * INT21_GetDiskSerialNumber
3085 static int INT21_GetDiskSerialNumber( CONTEXT
*context
)
3087 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3088 WCHAR path
[] = {'A',':','\\',0}, label
[11];
3091 path
[0] += INT21_MapDrive(BL_reg(context
));
3092 if (!GetVolumeInformationW( path
, label
, 11, &serial
, NULL
, NULL
, NULL
, 0))
3094 SetLastError( ERROR_INVALID_DRIVE
);
3098 *(WORD
*)dataptr
= 0;
3099 memcpy(dataptr
+ 2, &serial
, sizeof(DWORD
));
3100 WideCharToMultiByte(CP_OEMCP
, 0, label
, 11, (LPSTR
)dataptr
+ 6, 11, NULL
, NULL
);
3101 memcpy(dataptr
+ 17, "FAT16 ", 8);
3106 /******************************************************************
3107 * INT21_SetDiskSerialNumber
3110 static BOOL
INT21_SetDiskSerialNumber( CONTEXT
*context
)
3113 BYTE
*dataptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3114 int drive
= INT21_MapDrive(BL_reg(context
));
3116 if (!is_valid_drive(drive
))
3118 SetLastError( ERROR_INVALID_DRIVE
);
3122 DRIVE_SetSerialNumber( drive
, *(DWORD
*)(dataptr
+ 2) );
3125 FIXME("Setting drive serial number is no longer supported\n");
3126 SetLastError( ERROR_NOT_SUPPORTED
);
3132 /******************************************************************
3133 * INT21_GetFreeDiskSpace
3136 static BOOL
INT21_GetFreeDiskSpace( CONTEXT
*context
)
3138 DWORD cluster_sectors
, sector_bytes
, free_clusters
, total_clusters
;
3139 WCHAR root
[] = {'A',':','\\',0};
3140 const DWORD max_clusters
= 0x3d83;
3141 const DWORD max_sectors_per_cluster
= 0x7f;
3142 const DWORD max_bytes_per_sector
= 0x200;
3144 root
[0] += INT21_MapDrive(DL_reg(context
));
3145 if (!GetDiskFreeSpaceW( root
, &cluster_sectors
, §or_bytes
,
3146 &free_clusters
, &total_clusters
))
3149 /* Some old win31 apps (Lotus SmartSuite 5.1) crap out if there's too
3150 * much disk space, so Windows XP seems to apply the following limits:
3151 * cluster_sectors <= 0x7f
3152 * sector size <= 0x200
3153 * clusters <= 0x3D83
3154 * This means total reported space is limited to about 1GB.
3157 /* Make sure bytes-per-sector is in [, max] */
3158 while (sector_bytes
> max_bytes_per_sector
) {
3160 free_clusters
<<= 1;
3161 total_clusters
<<= 1;
3163 /* Then make sure sectors-per-cluster is in [max/2, max]. */
3164 while (cluster_sectors
<= max_sectors_per_cluster
/2) {
3165 cluster_sectors
<<= 1;
3166 free_clusters
>>= 1;
3167 total_clusters
>>= 1;
3169 while (cluster_sectors
> max_sectors_per_cluster
) {
3170 cluster_sectors
>>= 1;
3171 free_clusters
<<= 1;
3172 total_clusters
<<= 1;
3175 /* scale up sectors_per_cluster to exactly max_sectors_per_cluster.
3176 * We could skip this, but that would impose an artificially low
3177 * limit on reported disk space.
3178 * To avoid overflow, first apply a preliminary cap on sector count;
3179 * this will not affect the correctness of the final result,
3180 * because if the preliminary cap hits, the final one will, too.
3182 if (total_clusters
> 4 * max_clusters
)
3183 total_clusters
= 4 * max_clusters
;
3184 if (free_clusters
> 4 * max_clusters
)
3185 free_clusters
= 4 * max_clusters
;
3186 if (cluster_sectors
< max_sectors_per_cluster
) {
3187 free_clusters
*= cluster_sectors
;
3188 free_clusters
/= max_sectors_per_cluster
;
3189 total_clusters
*= cluster_sectors
;
3190 total_clusters
/= max_sectors_per_cluster
;
3191 cluster_sectors
= max_sectors_per_cluster
;
3194 /* Finally, apply real cluster count cap. */
3195 if (total_clusters
> max_clusters
)
3196 total_clusters
= max_clusters
;
3197 if (free_clusters
> max_clusters
)
3198 free_clusters
= max_clusters
;
3200 SET_AX( context
, cluster_sectors
);
3201 SET_BX( context
, free_clusters
);
3202 SET_CX( context
, sector_bytes
);
3203 SET_DX( context
, total_clusters
);
3207 /******************************************************************
3208 * INT21_GetDriveAllocInfo
3211 static BOOL
INT21_GetDriveAllocInfo( CONTEXT
*context
, BYTE drive
)
3215 drive
= INT21_MapDrive( drive
);
3216 if (!INT21_FillDrivePB( drive
)) return FALSE
;
3217 dpb
= &(INT21_GetHeapPointer()->misc_dpb_list
[drive
]);
3218 SET_AL( context
, dpb
->cluster_sectors
+ 1 );
3219 SET_CX( context
, dpb
->sector_bytes
);
3220 SET_DX( context
, dpb
->num_clusters1
);
3222 context
->SegDs
= INT21_GetHeapSelector( context
);
3223 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
].media_ID
) );
3227 /***********************************************************************
3228 * INT21_GetExtendedError
3230 static void INT21_GetExtendedError( CONTEXT
*context
)
3232 BYTE
class, action
, locus
;
3233 WORD error
= GetLastError();
3238 class = action
= locus
= 0;
3240 case ERROR_DIR_NOT_EMPTY
:
3245 case ERROR_ACCESS_DENIED
:
3246 class = EC_AccessDenied
;
3250 case ERROR_CANNOT_MAKE
:
3251 class = EC_AccessDenied
;
3255 case ERROR_DISK_FULL
:
3256 case ERROR_HANDLE_DISK_FULL
:
3257 class = EC_MediaError
;
3261 case ERROR_FILE_EXISTS
:
3262 case ERROR_ALREADY_EXISTS
:
3267 case ERROR_FILE_NOT_FOUND
:
3268 case ERROR_PATH_NOT_FOUND
:
3269 case ERROR_INVALID_NAME
:
3270 class = EC_NotFound
;
3274 case ERROR_GEN_FAILURE
:
3275 class = EC_SystemFailure
;
3279 case ERROR_INVALID_DRIVE
:
3280 class = EC_MediaError
;
3284 case ERROR_INVALID_HANDLE
:
3285 class = EC_ProgramError
;
3289 case ERROR_LOCK_VIOLATION
:
3290 class = EC_AccessDenied
;
3294 case ERROR_NO_MORE_FILES
:
3295 class = EC_MediaError
;
3300 class = EC_NotFound
;
3304 case ERROR_NOT_ENOUGH_MEMORY
:
3305 class = EC_OutOfResource
;
3310 class = EC_NotFound
;
3314 case ERROR_SHARING_VIOLATION
:
3315 class = EC_Temporary
;
3319 case ERROR_TOO_MANY_OPEN_FILES
:
3320 class = EC_ProgramError
;
3325 FIXME("Unknown error %d\n", error
);
3326 class = EC_SystemFailure
;
3331 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3332 error
, class, action
, locus
);
3333 SET_AX( context
, error
);
3334 SET_BH( context
, class );
3335 SET_BL( context
, action
);
3336 SET_CH( context
, locus
);
3339 static BOOL
INT21_CreateTempFile( CONTEXT
*context
)
3341 static int counter
= 0;
3342 char *name
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3343 char *p
= name
+ strlen(name
);
3345 /* despite what Ralf Brown says, some programs seem to call without
3346 * ending backslash (DOS accepts that, so we accept it too) */
3347 if ((p
== name
) || (p
[-1] != '\\')) *p
++ = '\\';
3351 sprintf( p
, "wine%04x.%03d", GetCurrentThreadId(), counter
);
3352 counter
= (counter
+ 1) % 1000;
3355 Win32HandleToDosFileHandle(
3356 CreateFileA( name
, GENERIC_READ
| GENERIC_WRITE
,
3357 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
3358 CREATE_NEW
, 0, 0 ) ) );
3359 if (AX_reg(context
) != HFILE_ERROR16
)
3361 TRACE("created %s\n", name
);
3364 if (GetLastError() != ERROR_FILE_EXISTS
) return FALSE
;
3368 /***********************************************************************
3369 * DOSFS_ToDosFCBFormat
3371 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3372 * expanding wild cards and converting to upper-case in the process.
3373 * File name can be terminated by '\0', '\\' or '/'.
3374 * Return FALSE if the name is not a valid DOS name.
3375 * 'buffer' must be at least 12 characters long.
3377 /* Chars we don't want to see in DOS file names */
3378 static BOOL
INT21_ToDosFCBFormat( LPCWSTR name
, LPWSTR buffer
)
3380 static const WCHAR invalid_chars
[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3384 /* Check for "." and ".." */
3389 for(i
= 1; i
< 11; i
++) buffer
[i
] = ' ';
3396 return (!*p
|| (*p
== '/') || (*p
== '\\'));
3399 for (i
= 0; i
< 8; i
++)
3416 if (wcschr( invalid_chars
, *p
)) return FALSE
;
3424 /* Skip all chars after wildcard up to first dot */
3425 while (*p
&& (*p
!= '/') && (*p
!= '\\') && (*p
!= '.')) p
++;
3429 /* Check if name too long */
3430 if (*p
&& (*p
!= '/') && (*p
!= '\\') && (*p
!= '.')) return FALSE
;
3432 if (*p
== '.') p
++; /* Skip dot */
3434 for (i
= 8; i
< 11; i
++)
3444 return FALSE
; /* Second extension not allowed */
3452 if (wcschr( invalid_chars
, *p
)) return FALSE
;
3460 /* at most 3 character of the extension are processed
3461 * is something behind this ?
3463 while (*p
== '*' || *p
== ' ') p
++; /* skip wildcards and spaces */
3464 return (!*p
|| (*p
== '/') || (*p
== '\\'));
3467 static HANDLE INT21_FindHandle
;
3468 static const WCHAR
*INT21_FindPath
; /* will point to current dta->fullPath search */
3470 /******************************************************************
3473 static BOOL
INT21_FindFirst( CONTEXT
*context
)
3477 FINDFILE_DTA
*dta
= (FINDFILE_DTA
*)INT21_GetCurrentDTA(context
);
3478 WCHAR maskW
[12], pathW
[MAX_PATH
];
3479 static const WCHAR wildcardW
[] = {'*','.','*',0};
3481 path
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3482 MultiByteToWideChar(CP_OEMCP
, 0, path
, -1, pathW
, MAX_PATH
);
3484 p
= wcsrchr( pathW
, '\\');
3485 q
= wcsrchr( pathW
, '/');
3489 if (pathW
[0] && pathW
[1] == ':') p
= pathW
+ 2;
3494 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3495 * (doesn't matter as it is set below anyway)
3497 if (!INT21_ToDosFCBFormat( p
, maskW
))
3499 SetLastError( ERROR_FILE_NOT_FOUND
);
3500 SET_AX( context
, ERROR_FILE_NOT_FOUND
);
3504 WideCharToMultiByte(CP_OEMCP
, 0, maskW
, 12, dta
->mask
, sizeof(dta
->mask
), NULL
, NULL
);
3506 dta
->fullPath
= HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW
) + (p
- pathW
)*sizeof(WCHAR
) );
3507 memcpy( dta
->fullPath
, pathW
, (p
- pathW
) * sizeof(WCHAR
) );
3508 memcpy( dta
->fullPath
+ (p
- pathW
), wildcardW
, sizeof(wildcardW
) );
3509 /* we must have a fully qualified file name in dta->fullPath
3510 * (we could have a UNC path, but this would lead to some errors later on)
3512 dta
->drive
= drive_number( dta
->fullPath
[0] );
3514 dta
->search_attr
= CL_reg(context
);
3518 /******************************************************************
3521 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3523 static BOOL
match_short(LPCWSTR shortW
, LPCSTR maskA
)
3525 WCHAR mask
[11], file
[12];
3528 if (!INT21_ToDosFCBFormat( shortW
, file
)) return FALSE
;
3529 MultiByteToWideChar(CP_OEMCP
, 0, maskA
, 11, mask
, 11);
3530 for (i
= 0; i
< 11; i
++)
3531 if (mask
[i
] != '?' && mask
[i
] != file
[i
]) return FALSE
;
3535 static unsigned INT21_FindHelper(LPCWSTR fullPath
, unsigned drive
, unsigned count
,
3536 LPCSTR mask
, unsigned search_attr
,
3537 WIN32_FIND_DATAW
* entry
)
3541 if ((search_attr
& ~(FA_UNUSED
| FA_ARCHIVE
| FA_RDONLY
)) == FA_LABEL
)
3543 WCHAR path
[] = {' ',':','\\',0};
3545 if (count
) return 0;
3546 path
[0] = drive
+ 'A';
3547 if (!GetVolumeInformationW(path
, entry
->cAlternateFileName
, 13, NULL
, NULL
, NULL
, NULL
, 0)) return 0;
3548 if (!entry
->cAlternateFileName
[0]) return 0;
3549 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftCreationTime
);
3550 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftLastAccessTime
);
3551 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER
*)&entry
->ftLastWriteTime
);
3552 entry
->dwFileAttributes
= FA_LABEL
;
3553 entry
->nFileSizeHigh
= entry
->nFileSizeLow
= 0;
3554 TRACE("returning %s as label\n", debugstr_w(entry
->cAlternateFileName
));
3558 if (!INT21_FindHandle
|| INT21_FindPath
!= fullPath
|| count
== 0)
3560 if (INT21_FindHandle
) FindClose(INT21_FindHandle
);
3561 INT21_FindHandle
= FindFirstFileW(fullPath
, entry
);
3562 if (INT21_FindHandle
== INVALID_HANDLE_VALUE
)
3564 INT21_FindHandle
= 0;
3567 INT21_FindPath
= fullPath
;
3568 /* we need to resync search */
3573 while (ncalls
-- != 0)
3575 if (!FindNextFileW(INT21_FindHandle
, entry
))
3577 FindClose(INT21_FindHandle
); INT21_FindHandle
= 0;
3581 while (count
< 0xffff)
3584 /* Check the file attributes, and path */
3585 if (!(entry
->dwFileAttributes
& ~search_attr
) &&
3586 match_short(entry
->cAlternateFileName
[0] ? entry
->cAlternateFileName
: entry
->cFileName
,
3591 if (!FindNextFileW(INT21_FindHandle
, entry
))
3593 FindClose(INT21_FindHandle
); INT21_FindHandle
= 0;
3597 WARN("Too many directory entries in %s\n", debugstr_w(fullPath
) );
3601 /******************************************************************
3604 static BOOL
INT21_FindNext( CONTEXT
*context
)
3606 FINDFILE_DTA
*dta
= (FINDFILE_DTA
*)INT21_GetCurrentDTA(context
);
3607 DWORD attr
= dta
->search_attr
| FA_UNUSED
| FA_ARCHIVE
| FA_RDONLY
;
3608 WIN32_FIND_DATAW entry
;
3611 if (!dta
->fullPath
) return FALSE
;
3613 n
= INT21_FindHelper(dta
->fullPath
, dta
->drive
, dta
->count
,
3614 dta
->mask
, attr
, &entry
);
3617 dta
->fileattr
= entry
.dwFileAttributes
;
3618 dta
->filesize
= entry
.nFileSizeLow
;
3619 FileTimeToDosDateTime( &entry
.ftLastWriteTime
, &dta
->filedate
, &dta
->filetime
);
3620 if (entry
.cAlternateFileName
[0])
3621 WideCharToMultiByte(CP_OEMCP
, 0, entry
.cAlternateFileName
, -1,
3622 dta
->filename
, 13, NULL
, NULL
);
3624 WideCharToMultiByte(CP_OEMCP
, 0, entry
.cFileName
, -1, dta
->filename
, 13, NULL
, NULL
);
3626 if (!memchr(dta
->mask
,'?',11))
3628 /* wildcardless search, release resources in case no findnext will
3629 * be issued, and as a workaround in case file creation messes up
3630 * findnext, as sometimes happens with pkunzip
3632 HeapFree( GetProcessHeap(), 0, dta
->fullPath
);
3633 INT21_FindPath
= dta
->fullPath
= NULL
;
3638 HeapFree( GetProcessHeap(), 0, dta
->fullPath
);
3639 INT21_FindPath
= dta
->fullPath
= NULL
;
3643 /* microsoft's programmers should be shot for using CP/M style int21
3644 calls in Windows for Workgroup's winfile.exe */
3646 /******************************************************************
3647 * INT21_FindFirstFCB
3650 static BOOL
INT21_FindFirstFCB( CONTEXT
*context
)
3652 BYTE
*fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3655 WCHAR p
[] = {' ',':',};
3657 if (*fcb
== 0xff) pFCB
= (FINDFILE_FCB
*)(fcb
+ 7);
3658 else pFCB
= (FINDFILE_FCB
*)fcb
;
3659 drive
= INT21_MapDrive( pFCB
->drive
);
3660 if (drive
== MAX_DOS_DRIVES
) return FALSE
;
3663 pFCB
->fullPath
= HeapAlloc(GetProcessHeap(), 0, MAX_PATH
* sizeof(WCHAR
));
3664 if (!pFCB
->fullPath
) return FALSE
;
3665 GetLongPathNameW(p
, pFCB
->fullPath
, MAX_PATH
);
3670 /******************************************************************
3674 static BOOL
INT21_FindNextFCB( CONTEXT
*context
)
3676 BYTE
*fcb
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
3678 LPBYTE pResult
= INT21_GetCurrentDTA(context
);
3679 DOS_DIRENTRY_LAYOUT
*ddl
;
3680 WIN32_FIND_DATAW entry
;
3685 if (*fcb
== 0xff) /* extended FCB ? */
3688 pFCB
= (FINDFILE_FCB
*)(fcb
+ 7);
3693 pFCB
= (FINDFILE_FCB
*)fcb
;
3696 if (!pFCB
->fullPath
) return FALSE
;
3697 n
= INT21_FindHelper(pFCB
->fullPath
, INT21_MapDrive( pFCB
->drive
),
3698 pFCB
->count
, pFCB
->filename
, attr
, &entry
);
3701 HeapFree( GetProcessHeap(), 0, pFCB
->fullPath
);
3702 INT21_FindPath
= pFCB
->fullPath
= NULL
;
3709 /* place extended FCB header before pResult if called with extended FCB */
3711 pResult
+= 6; /* leave reserved field behind */
3712 *pResult
++ = entry
.dwFileAttributes
;
3714 *pResult
++ = INT21_MapDrive( pFCB
->drive
); /* DOS_DIRENTRY_LAYOUT after current drive number */
3715 ddl
= (DOS_DIRENTRY_LAYOUT
*)pResult
;
3716 ddl
->fileattr
= entry
.dwFileAttributes
;
3717 ddl
->cluster
= 0; /* what else? */
3718 ddl
->filesize
= entry
.nFileSizeLow
;
3719 memset( ddl
->reserved
, 0, sizeof(ddl
->reserved
) );
3720 FileTimeToDosDateTime( &entry
.ftLastWriteTime
,
3721 &ddl
->filedate
, &ddl
->filetime
);
3723 /* Convert file name to FCB format */
3724 if (entry
.cAlternateFileName
[0])
3725 INT21_ToDosFCBFormat( entry
.cAlternateFileName
, nameW
);
3727 INT21_ToDosFCBFormat( entry
.cFileName
, nameW
);
3728 WideCharToMultiByte(CP_OEMCP
, 0, nameW
, 11, ddl
->filename
, 11, NULL
, NULL
);
3733 /******************************************************************
3734 * INT21_ParseFileNameIntoFCB
3737 static void INT21_ParseFileNameIntoFCB( CONTEXT
*context
)
3740 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
);
3742 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
);
3746 INT buffer_len
, len
;
3748 SET_AL( context
, 0xff ); /* failed */
3750 TRACE("filename: '%s'\n", filename
);
3753 while (*s
&& (*s
!= ' ') && (*s
!= '\r') && (*s
!= '\n'))
3757 buffer_len
= MultiByteToWideChar(CP_OEMCP
, 0, filename
, len
, NULL
, 0);
3758 buffer
= HeapAlloc( GetProcessHeap(), 0, (buffer_len
+ 1) * sizeof(WCHAR
));
3759 len
= MultiByteToWideChar(CP_OEMCP
, 0, filename
, len
, buffer
, buffer_len
);
3761 INT21_ToDosFCBFormat(buffer
, fcbW
);
3762 HeapFree(GetProcessHeap(), 0, buffer
);
3763 WideCharToMultiByte(CP_OEMCP
, 0, fcbW
, 12, fcb
+ 1, 12, NULL
, NULL
);
3765 TRACE("FCB: '%s'\n", fcb
+ 1);
3767 SET_AL( context
, ((strchr(filename
, '*')) || (strchr(filename
, '$'))) != 0 );
3769 /* point DS:SI to first unparsed character */
3770 SET_SI( context
, context
->Esi
+ (int)s
- (int)filename
);
3773 static BOOL
INT21_Dup2(HFILE16 hFile1
, HFILE16 hFile2
)
3775 HFILE16 res
= HFILE_ERROR16
;
3776 HANDLE handle
, new_handle
;
3777 #define DOS_TABLE_SIZE 256
3778 DWORD map
[DOS_TABLE_SIZE
/ 32];
3781 handle
= DosFileHandleToWin32Handle(hFile1
);
3782 if (handle
== INVALID_HANDLE_VALUE
)
3786 /* now loop to allocate the same one... */
3787 memset(map
, 0, sizeof(map
));
3788 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
3790 if (!DuplicateHandle(GetCurrentProcess(), handle
,
3791 GetCurrentProcess(), &new_handle
,
3792 0, FALSE
, DUPLICATE_SAME_ACCESS
))
3794 res
= HFILE_ERROR16
;
3797 res
= Win32HandleToDosFileHandle(new_handle
);
3798 if (res
== HFILE_ERROR16
|| res
== hFile2
) break;
3799 map
[res
/ 32] |= 1 << (res
% 32);
3801 /* clean up the allocated slots */
3802 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
3804 if (map
[i
/ 32] & (1 << (i
% 32)))
3805 _lclose16((HFILE16
)i
);
3807 return res
== hFile2
;
3811 /***********************************************************************
3812 * DOSVM_Int21Handler
3814 * Interrupt 0x21 handler.
3816 void WINAPI
DOSVM_Int21Handler( CONTEXT
*context
)
3818 BOOL bSetDOSExtendedError
= FALSE
;
3820 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
3821 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
3822 AX_reg(context
), BX_reg(context
),
3823 CX_reg(context
), DX_reg(context
),
3824 SI_reg(context
), DI_reg(context
),
3825 (WORD
)context
->SegDs
, (WORD
)context
->SegEs
,
3829 * Extended error is used by (at least) functions 0x2f to 0x62.
3830 * Function 0x59 returns extended error and error should not
3831 * be cleared before handling the function.
3833 if (AH_reg(context
) >= 0x2f && AH_reg(context
) != 0x59)
3836 RESET_CFLAG(context
); /* Not sure if this is a good idea. */
3838 switch(AH_reg(context
))
3840 case 0x00: /* TERMINATE PROGRAM */
3841 TRACE("TERMINATE PROGRAM\n");
3845 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
3846 TRACE("DIRECT CHARACTER INPUT WITH ECHO - not supported\n");
3849 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
3850 TRACE("Write Character to Standard Output\n");
3853 case 0x03: /* READ CHARACTER FROM STDAUX */
3854 case 0x04: /* WRITE CHARACTER TO STDAUX */
3855 case 0x05: /* WRITE CHARACTER TO PRINTER */
3856 INT_BARF( context
, 0x21 );
3859 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
3860 if (DL_reg(context
) == 0xff)
3862 TRACE("Direct Console Input\n");
3864 /* no character available */
3865 SET_AL( context
, 0 );
3866 SET_ZFLAG( context
);
3870 TRACE("Direct Console Output\n");
3872 * At least DOS versions 2.1-7.0 return character
3873 * that was written in AL register.
3875 SET_AL( context
, DL_reg(context
) );
3879 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
3880 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO - not supported\n");
3881 SET_AL( context
, 0 );
3884 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
3885 TRACE("CHARACTER INPUT WITHOUT ECHO - not supported\n");
3886 SET_AL( context
, 0 );
3889 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
3890 TRACE("WRITE '$'-terminated string from %04X:%04X to stdout\n",
3891 context
->SegDs
, DX_reg(context
) );
3893 LPSTR data
= CTX_SEG_OFF_TO_LIN( context
,
3894 context
->SegDs
, context
->Edx
);
3898 * Do NOT use strchr() to calculate the string length,
3899 * as '\0' is valid string content, too!
3900 * Maybe we should check for non-'$' strings, but DOS doesn't.
3902 while (*p
!= '$') p
++;
3904 WriteFile( DosFileHandleToWin32Handle(1), data
, p
- data
, &w
, NULL
);
3905 SET_AL( context
, '$' ); /* yes, '$' (0x24) gets returned in AL */
3909 case 0x0a: /* BUFFERED INPUT */
3910 TRACE( "BUFFERED INPUT - not supported\n" );
3913 case 0x0b: /* GET STDIN STATUS */
3914 TRACE( "GET STDIN STATUS - not supported\n" );
3915 SET_AL( context
, 0 ); /* no character available */
3918 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
3920 BYTE al
= AL_reg(context
); /* Input function to execute after flush. */
3922 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al
);
3924 /* FIXME: buffers are not flushed */
3927 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
3928 * int21 function identified by AL will be called.
3930 if(al
== 0x01 || al
== 0x06 || al
== 0x07 || al
== 0x08 || al
== 0x0a)
3932 SET_AH( context
, al
);
3933 DOSVM_Int21Handler( context
);
3938 case 0x0d: /* DISK BUFFER FLUSH */
3939 TRACE("DISK BUFFER FLUSH ignored\n");
3942 case 0x0e: /* SELECT DEFAULT DRIVE */
3943 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context
) );
3944 INT21_SetCurrentDrive( DL_reg(context
) );
3945 SET_AL( context
, MAX_DOS_DRIVES
);
3948 case 0x0f: /* OPEN FILE USING FCB */
3949 INT21_OpenFileUsingFCB( context
);
3952 case 0x10: /* CLOSE FILE USING FCB */
3953 INT21_CloseFileUsingFCB( context
);
3956 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
3957 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
3958 CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
));
3959 if (!INT21_FindFirstFCB(context
))
3961 SET_AL( context
, 0xff );
3964 /* else fall through */
3966 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
3967 SET_AL( context
, INT21_FindNextFCB(context
) ? 0x00 : 0xff );
3970 case 0x13: /* DELETE FILE USING FCB */
3971 INT_BARF( context
, 0x21 );
3974 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
3975 INT21_SequentialReadFromFCB( context
);
3978 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
3979 INT21_SequentialWriteToFCB( context
);
3982 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
3983 case 0x17: /* RENAME FILE USING FCB */
3984 INT_BARF( context
, 0x21 );
3987 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
3988 SET_AL( context
, 0 );
3991 case 0x19: /* GET CURRENT DEFAULT DRIVE */
3992 SET_AL( context
, INT21_GetCurrentDrive() );
3993 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context
) );
3996 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
3997 TRACE( "SET DISK TRANSFER AREA ADDRESS %04X:%04X\n",
3998 context
->SegDs
, DX_reg(context
) );
4000 TDB
*task
= GlobalLock16( GetCurrentTask() );
4001 task
->dta
= MAKESEGPTR( context
->SegDs
, DX_reg(context
) );
4005 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4006 if (!INT21_GetDriveAllocInfo(context
, 0))
4007 SET_AX( context
, 0xffff );
4010 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4011 if (!INT21_GetDriveAllocInfo(context
, DL_reg(context
)))
4012 SET_AX( context
, 0xffff );
4015 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4016 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4017 SET_AL( context
, 0 );
4020 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4022 BYTE drive
= INT21_MapDrive( 0 );
4023 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4025 if (INT21_FillDrivePB( drive
))
4027 SET_AL( context
, 0x00 ); /* success */
4028 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
] ) );
4029 context
->SegDs
= INT21_GetHeapSelector( context
);
4033 SET_AL( context
, 0xff ); /* invalid or network drive */
4038 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4039 SET_AL( context
, 0 );
4042 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4043 INT21_ReadRandomRecordFromFCB( context
);
4046 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4047 INT21_WriteRandomRecordToFCB( context
);
4050 case 0x23: /* GET FILE SIZE FOR FCB */
4051 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4052 INT_BARF( context
, 0x21 );
4055 case 0x25: /* SET INTERRUPT VECTOR */
4056 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context
));
4058 FARPROC16 ptr
= (FARPROC16
)MAKESEGPTR( context
->SegDs
, DX_reg(context
) );
4059 DOSVM_SetPMHandler16( AL_reg(context
), ptr
);
4063 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4064 INT_BARF( context
, 0x21 );
4067 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4068 INT21_RandomBlockReadFromFCB( context
);
4071 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4072 INT21_RandomBlockWriteToFCB( context
);
4075 case 0x29: /* PARSE FILENAME INTO FCB */
4076 INT21_ParseFileNameIntoFCB(context
);
4079 case 0x2a: /* GET SYSTEM DATE */
4080 TRACE( "GET SYSTEM DATE\n" );
4083 GetLocalTime( &systime
);
4084 SET_CX( context
, systime
.wYear
);
4085 SET_DH( context
, systime
.wMonth
);
4086 SET_DL( context
, systime
.wDay
);
4087 SET_AL( context
, systime
.wDayOfWeek
);
4091 case 0x2b: /* SET SYSTEM DATE */
4092 TRACE( "SET SYSTEM DATE\n" );
4094 WORD year
= CX_reg(context
);
4095 BYTE month
= DH_reg(context
);
4096 BYTE day
= DL_reg(context
);
4098 if (year
>= 1980 && year
<= 2099 &&
4099 month
>= 1 && month
<= 12 &&
4100 day
>= 1 && day
<= 31)
4102 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4104 SET_AL( context
, 0 ); /* Let's pretend we succeeded */
4108 SET_AL( context
, 0xff ); /* invalid date */
4109 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4115 case 0x2c: /* GET SYSTEM TIME */
4116 TRACE( "GET SYSTEM TIME\n" );
4119 GetLocalTime( &systime
);
4120 SET_CH( context
, systime
.wHour
);
4121 SET_CL( context
, systime
.wMinute
);
4122 SET_DH( context
, systime
.wSecond
);
4123 SET_DL( context
, systime
.wMilliseconds
/ 10 );
4127 case 0x2d: /* SET SYSTEM TIME */
4128 if( CH_reg(context
) >= 24 || CL_reg(context
) >= 60 || DH_reg(context
) >= 60 || DL_reg(context
) >= 100 ) {
4129 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4130 CH_reg(context
), CL_reg(context
),
4131 DH_reg(context
), DL_reg(context
) );
4132 SET_AL( context
, 0xFF );
4136 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4137 CH_reg(context
), CL_reg(context
),
4138 DH_reg(context
), DL_reg(context
) );
4139 SET_AL( context
, 0 ); /* Let's pretend we succeeded */
4143 case 0x2e: /* SET VERIFY FLAG */
4144 TRACE("SET VERIFY FLAG ignored\n");
4145 /* we cannot change the behaviour anyway, so just ignore it */
4148 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4149 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4151 TDB
*task
= GlobalLock16( GetCurrentTask() );
4152 context
->SegEs
= SELECTOROF( task
->dta
);
4153 SET_BX( context
, OFFSETOF( task
->dta
) );
4157 case 0x30: /* GET DOS VERSION */
4158 TRACE( "GET DOS VERSION - %s requested\n",
4159 (AL_reg(context
) == 0x00) ? "OEM number" : "version flag" );
4161 if (AL_reg(context
) == 0x00)
4162 SET_BH( context
, 0xff ); /* OEM number => undefined */
4164 SET_BH( context
, 0x08 ); /* version flag => DOS is in ROM */
4166 SET_AL( context
, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4167 SET_AH( context
, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4169 SET_BL( context
, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4170 SET_CX( context
, 0x3456 );
4173 case 0x31: /* TERMINATE AND STAY RESIDENT */
4174 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4177 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4179 BYTE drive
= INT21_MapDrive( DL_reg(context
) );
4180 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context
) );
4182 if (INT21_FillDrivePB( drive
))
4184 SET_AL( context
, 0x00 ); /* success */
4185 SET_BX( context
, offsetof( INT21_HEAP
, misc_dpb_list
[drive
] ) );
4186 context
->SegDs
= INT21_GetHeapSelector( context
);
4190 SET_AL( context
, 0xff ); /* invalid or network drive */
4195 case 0x33: /* MULTIPLEXED */
4196 switch (AL_reg(context
))
4198 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4199 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4200 SET_DL( context
, brk_flag
);
4203 case 0x01: /* SET EXTENDED BREAK STATE */
4204 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4205 brk_flag
= (DL_reg(context
) > 0) ? 1 : 0;
4208 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4209 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4210 /* ugly coding in order to stay reentrant */
4211 if (DL_reg(context
))
4213 SET_DL( context
, brk_flag
);
4218 SET_DL( context
, brk_flag
);
4223 case 0x05: /* GET BOOT DRIVE */
4224 TRACE("GET BOOT DRIVE\n");
4225 SET_DL( context
, 3 );
4226 /* c: is Wine's bootdrive (a: is 1)*/
4229 case 0x06: /* GET TRUE VERSION NUMBER */
4230 TRACE("GET TRUE VERSION NUMBER\n");
4231 SET_BL( context
, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4232 SET_BH( context
, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4233 SET_DL( context
, 0x00 ); /* revision */
4234 SET_DH( context
, 0x08 ); /* DOS is in ROM */
4238 INT_BARF( context
, 0x21 );
4243 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4244 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4245 context
->SegEs
= INT21_GetHeapSelector( context
);
4246 SET_BX( context
, offsetof(INT21_HEAP
, misc_indos
) );
4249 case 0x35: /* GET INTERRUPT VECTOR */
4250 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context
));
4252 FARPROC16 addr
= DOSVM_GetPMHandler16( AL_reg(context
) );
4253 context
->SegEs
= SELECTOROF(addr
);
4254 SET_BX( context
, OFFSETOF(addr
) );
4258 case 0x36: /* GET FREE DISK SPACE */
4259 TRACE("GET FREE DISK SPACE FOR DRIVE %s (limited to about 1GB)\n",
4260 INT21_DriveName( DL_reg(context
) ));
4261 if (!INT21_GetFreeDiskSpace(context
)) SET_AX( context
, 0xffff );
4264 case 0x37: /* SWITCHAR */
4266 switch (AL_reg(context
))
4268 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4269 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4270 SET_AL( context
, 0x00 ); /* success*/
4271 SET_DL( context
, '/' );
4273 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4274 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4276 SET_AL( context
, 0x00 ); /* success*/
4279 INT_BARF( context
, 0x21 );
4285 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4286 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4287 if (AL_reg(context
))
4289 WORD country
= AL_reg(context
);
4290 if (country
== 0xff)
4291 country
= BX_reg(context
);
4292 if (country
!= INT21_GetSystemCountryCode()) {
4293 FIXME( "Requested info on non-default country %04x\n", country
);
4298 if(AX_reg(context
) != 2 )
4300 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context
,
4303 SET_AX( context
, INT21_GetSystemCountryCode() );
4304 SET_BX( context
, INT21_GetSystemCountryCode() );
4305 RESET_CFLAG(context
);
4309 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4310 if (!INT21_CreateDirectory( context
))
4311 bSetDOSExtendedError
= TRUE
;
4313 RESET_CFLAG(context
);
4316 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4318 WCHAR dirW
[MAX_PATH
];
4319 char *dirA
= CTX_SEG_OFF_TO_LIN(context
,
4320 context
->SegDs
, context
->Edx
);
4322 TRACE( "REMOVE DIRECTORY %s\n", dirA
);
4324 MultiByteToWideChar(CP_OEMCP
, 0, dirA
, -1, dirW
, MAX_PATH
);
4326 if (!RemoveDirectoryW( dirW
))
4327 bSetDOSExtendedError
= TRUE
;
4329 RESET_CFLAG(context
);
4333 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4334 if (!INT21_SetCurrentDirectory( context
))
4335 bSetDOSExtendedError
= TRUE
;
4337 RESET_CFLAG(context
);
4340 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4341 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4342 OF_READWRITE
| OF_SHARE_COMPAT
, 0x12 ))
4343 bSetDOSExtendedError
= TRUE
;
4345 RESET_CFLAG(context
);
4348 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4349 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4350 AL_reg(context
), 0x01 ))
4351 bSetDOSExtendedError
= TRUE
;
4353 RESET_CFLAG(context
);
4356 case 0x3e: /* "CLOSE" - CLOSE FILE */
4357 TRACE( "CLOSE handle %d\n", BX_reg(context
) );
4358 if (_lclose16( BX_reg(context
) ) == HFILE_ERROR16
)
4359 bSetDOSExtendedError
= TRUE
;
4361 RESET_CFLAG(context
);
4364 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4365 TRACE( "READ from %d to %04X:%04X for %d bytes\n",
4372 WORD count
= CX_reg(context
);
4373 BYTE
*buffer
= CTX_SEG_OFF_TO_LIN( context
,
4377 /* Some programs pass a count larger than the allocated buffer */
4378 DWORD maxcount
= GetSelectorLimit16( context
->SegDs
) - DX_reg(context
) + 1;
4379 if (count
> maxcount
) count
= maxcount
;
4382 * FIXME: Reading from console (BX=1) in DOS mode
4383 * does not work as it is supposed to work.
4386 RESET_CFLAG(context
); /* set if error */
4387 if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context
)), buffer
, count
, &result
, NULL
))
4388 SET_AX( context
, (WORD
)result
);
4390 bSetDOSExtendedError
= TRUE
;
4394 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4395 TRACE( "WRITE from %04X:%04X to handle %d for %d byte\n",
4396 context
->SegDs
, DX_reg(context
),
4397 BX_reg(context
), CX_reg(context
) );
4399 char *ptr
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
4400 HFILE handle
= (HFILE
)DosFileHandleToWin32Handle(BX_reg(context
));
4401 LONG result
= _hwrite( handle
, ptr
, CX_reg(context
) );
4402 if (result
== HFILE_ERROR
)
4403 bSetDOSExtendedError
= TRUE
;
4406 SET_AX( context
, (WORD
)result
);
4407 RESET_CFLAG(context
);
4412 case 0x41: /* "UNLINK" - DELETE FILE */
4414 WCHAR fileW
[MAX_PATH
];
4415 char *fileA
= CTX_SEG_OFF_TO_LIN(context
,
4419 TRACE( "UNLINK %s\n", fileA
);
4420 MultiByteToWideChar(CP_OEMCP
, 0, fileA
, -1, fileW
, MAX_PATH
);
4422 if (!DeleteFileW( fileW
))
4423 bSetDOSExtendedError
= TRUE
;
4425 RESET_CFLAG(context
);
4429 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4430 TRACE( "LSEEK handle %d offset %d from %s\n",
4432 MAKELONG( DX_reg(context
), CX_reg(context
) ),
4433 (AL_reg(context
) == 0) ?
4434 "start of file" : ((AL_reg(context
) == 1) ?
4435 "current file position" : "end of file") );
4437 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
4438 LONG offset
= MAKELONG( DX_reg(context
), CX_reg(context
) );
4439 DWORD status
= SetFilePointer( handle
, offset
,
4440 NULL
, AL_reg(context
) );
4441 if (status
== INVALID_SET_FILE_POINTER
)
4442 bSetDOSExtendedError
= TRUE
;
4445 SET_AX( context
, LOWORD(status
) );
4446 SET_DX( context
, HIWORD(status
) );
4447 RESET_CFLAG(context
);
4452 case 0x43: /* FILE ATTRIBUTES */
4453 if (!INT21_FileAttributes( context
, AL_reg(context
), FALSE
))
4454 bSetDOSExtendedError
= TRUE
;
4456 RESET_CFLAG(context
);
4459 case 0x44: /* IOCTL */
4460 INT21_Ioctl( context
);
4463 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4464 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context
) );
4467 HFILE handle16
= HFILE_ERROR
;
4469 if (DuplicateHandle( GetCurrentProcess(),
4470 DosFileHandleToWin32Handle(BX_reg(context
)),
4471 GetCurrentProcess(),
4473 0, TRUE
, DUPLICATE_SAME_ACCESS
))
4474 handle16
= Win32HandleToDosFileHandle(handle32
);
4476 if (handle16
== HFILE_ERROR
)
4477 bSetDOSExtendedError
= TRUE
;
4480 SET_AX( context
, handle16
);
4481 RESET_CFLAG(context
);
4486 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4487 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4488 BX_reg(context
), CX_reg(context
) );
4489 if (!INT21_Dup2(BX_reg(context
), CX_reg(context
)))
4490 bSetDOSExtendedError
= TRUE
;
4492 RESET_CFLAG(context
);
4495 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4496 if (!INT21_GetCurrentDirectory( context
, FALSE
))
4497 bSetDOSExtendedError
= TRUE
;
4499 RESET_CFLAG(context
);
4502 case 0x48: /* ALLOCATE MEMORY */
4503 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context
) );
4505 DWORD bytes
= (DWORD
)BX_reg(context
) << 4;
4506 DWORD rv
= GlobalDOSAlloc16( bytes
);
4507 WORD selector
= LOWORD( rv
);
4511 SET_AX( context
, selector
);
4512 RESET_CFLAG(context
);
4517 SET_AX( context
, 0x0008 ); /* insufficient memory */
4518 SET_BX( context
, DOSMEM_Available() >> 4 );
4523 case 0x49: /* FREE MEMORY */
4524 TRACE( "FREE MEMORY segment %04X\n", context
->SegEs
);
4526 BOOL ok
= !GlobalDOSFree16( context
->SegEs
);
4528 /* If we don't reset ES_reg, we will fail in the relay code */
4529 if (ok
) context
->SegEs
= 0;
4532 TRACE("FREE MEMORY failed\n");
4534 SET_AX( context
, 0x0009 ); /* memory block address invalid */
4539 case 0x4a: /* RESIZE MEMORY BLOCK */
4540 TRACE( "RESIZE MEMORY segment %04X to %d paragraphs\n",
4541 context
->SegEs
, BX_reg(context
) );
4543 FIXME( "Resize memory block - unsupported under Win16\n" );
4548 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4550 char *program
= CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
);
4551 HINSTANCE16 instance
;
4553 TRACE( "EXEC %s\n", program
);
4555 RESET_CFLAG(context
);
4556 instance
= WinExec16( program
, SW_NORMAL
);
4559 SET_CFLAG( context
);
4560 SET_AX( context
, instance
);
4565 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
4566 TRACE( "EXIT with return code %d\n", AL_reg(context
) );
4567 DOSVM_Exit( AL_reg(context
) );
4570 case 0x4d: /* GET RETURN CODE */
4571 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
4572 SET_AX( context
, 0 );
4575 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
4576 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context
),
4577 (LPCSTR
)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Edx
));
4578 if (!INT21_FindFirst(context
)) break;
4581 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
4582 TRACE("FINDNEXT\n");
4583 if (!INT21_FindNext(context
))
4585 SetLastError( ERROR_NO_MORE_FILES
);
4586 SET_AX( context
, ERROR_NO_MORE_FILES
);
4589 else SET_AX( context
, 0 ); /* OK */
4592 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
4593 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
4594 DOSVM_psp
= BX_reg(context
);
4597 case 0x51: /* GET PSP ADDRESS */
4598 INT21_GetPSP( context
);
4601 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
4602 TRACE("Get List of Lists - not supported\n");
4604 SET_BX( context
, 0 );
4607 case 0x54: /* Get Verify Flag */
4608 TRACE("Get Verify Flag - Not Supported\n");
4609 SET_AL( context
, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
4612 case 0x56: /* "RENAME" - RENAME FILE */
4613 if (!INT21_RenameFile( context
))
4614 bSetDOSExtendedError
= TRUE
;
4616 RESET_CFLAG(context
);
4619 case 0x57: /* FILE DATE AND TIME */
4620 if (!INT21_FileDateTime( context
))
4621 bSetDOSExtendedError
= TRUE
;
4623 RESET_CFLAG(context
);
4626 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
4627 switch (AL_reg(context
))
4629 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
4630 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
4631 SET_AX( context
, 0 ); /* low memory first fit */
4634 case 0x01: /* SET ALLOCATION STRATEGY */
4635 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
4639 case 0x02: /* GET UMB LINK STATE */
4640 TRACE( "GET UMB LINK STATE\n" );
4641 SET_AL( context
, 0 ); /* UMBs not part of DOS memory chain */
4644 case 0x03: /* SET UMB LINK STATE */
4645 TRACE( "SET UMB LINK STATE to %d - ignored\n",
4650 INT_BARF( context
, 0x21 );
4654 case 0x59: /* GET EXTENDED ERROR INFO */
4655 INT21_GetExtendedError( context
);
4658 case 0x5a: /* CREATE TEMPORARY FILE */
4659 TRACE("CREATE TEMPORARY FILE\n");
4660 bSetDOSExtendedError
= !INT21_CreateTempFile(context
);
4663 case 0x5b: /* CREATE NEW FILE */
4664 if (!INT21_CreateFile( context
, context
->Edx
, FALSE
,
4665 OF_READWRITE
| OF_SHARE_COMPAT
, 0x10 ))
4666 bSetDOSExtendedError
= TRUE
;
4668 RESET_CFLAG(context
);
4671 case 0x5c: /* "FLOCK" - RECORD LOCKING */
4673 DWORD offset
= MAKELONG(DX_reg(context
), CX_reg(context
));
4674 DWORD length
= MAKELONG(DI_reg(context
), SI_reg(context
));
4675 HANDLE handle
= DosFileHandleToWin32Handle(BX_reg(context
));
4677 RESET_CFLAG(context
);
4678 switch (AL_reg(context
))
4680 case 0x00: /* LOCK */
4681 TRACE( "lock handle %d offset %d length %d\n",
4682 BX_reg(context
), offset
, length
);
4683 if (!LockFile( handle
, offset
, 0, length
, 0 ))
4684 bSetDOSExtendedError
= TRUE
;
4687 case 0x01: /* UNLOCK */
4688 TRACE( "unlock handle %d offset %d length %d\n",
4689 BX_reg(context
), offset
, length
);
4690 if (!UnlockFile( handle
, offset
, 0, length
, 0 ))
4691 bSetDOSExtendedError
= TRUE
;
4695 INT_BARF( context
, 0x21 );
4700 case 0x5d: /* NETWORK 5D */
4701 FIXME( "Network function 5D not implemented.\n" );
4702 SetLastError( ER_NoNetwork
);
4703 bSetDOSExtendedError
= TRUE
;
4706 case 0x5e: /* NETWORK 5E */
4707 bSetDOSExtendedError
= INT21_NetworkFunc( context
);
4710 case 0x5f: /* NETWORK 5F */
4711 /* FIXME: supporting this would need to 1:
4712 * - implement per drive current directory (as kernel32 doesn't)
4713 * - assign enabled/disabled flag on a per drive basis
4715 /* network software not installed */
4716 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context
));
4717 SetLastError( ER_NoNetwork
);
4718 bSetDOSExtendedError
= TRUE
;
4721 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
4723 WCHAR pathW
[MAX_PATH
], res
[MAX_PATH
];
4724 /* FIXME: likely to be broken */
4726 TRACE("TRUENAME %s\n",
4727 (LPCSTR
)CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
,context
->Esi
));
4728 MultiByteToWideChar(CP_OEMCP
, 0, CTX_SEG_OFF_TO_LIN(context
, context
->SegDs
, context
->Esi
), -1, pathW
, MAX_PATH
);
4729 if (!GetFullPathNameW( pathW
, 128, res
, NULL
))
4730 bSetDOSExtendedError
= TRUE
;
4733 SET_AX( context
, 0 );
4734 WideCharToMultiByte(CP_OEMCP
, 0, res
, -1,
4735 CTX_SEG_OFF_TO_LIN(context
, context
->SegEs
, context
->Edi
),
4741 case 0x61: /* UNUSED */
4742 SET_AL( context
, 0 );
4745 case 0x62: /* GET PSP ADDRESS */
4746 INT21_GetPSP( context
);
4749 case 0x63: /* MISC. LANGUAGE SUPPORT */
4750 switch (AL_reg(context
)) {
4751 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
4752 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
4753 context
->SegDs
= INT21_GetHeapSelector( context
);
4754 SET_SI( context
, offsetof(INT21_HEAP
, dbcs_table
) );
4755 SET_AL( context
, 0 ); /* success */
4760 case 0x64: /* OS/2 DOS BOX */
4761 INT_BARF( context
, 0x21 );
4765 case 0x65: /* EXTENDED COUNTRY INFORMATION */
4766 INT21_ExtendedCountryInformation( context
);
4769 case 0x66: /* GLOBAL CODE PAGE TABLE */
4770 switch (AL_reg(context
))
4773 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
4774 SET_BX( context
, GetOEMCP() );
4775 SET_DX( context
, GetOEMCP() );
4778 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
4779 BX_reg(context
), DX_reg(context
) );
4784 case 0x67: /* SET HANDLE COUNT */
4785 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context
) );
4786 if (SetHandleCount( BX_reg(context
) ) < BX_reg(context
) )
4787 bSetDOSExtendedError
= TRUE
;
4790 case 0x68: /* "FFLUSH" - COMMIT FILE */
4791 TRACE( "FFLUSH - handle %d\n", BX_reg(context
) );
4792 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context
)) ) &&
4793 GetLastError() != ERROR_ACCESS_DENIED
)
4794 bSetDOSExtendedError
= TRUE
;
4797 case 0x69: /* DISK SERIAL NUMBER */
4798 switch (AL_reg(context
))
4801 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
4802 INT21_DriveName(BL_reg(context
)));
4803 if (!INT21_GetDiskSerialNumber(context
)) bSetDOSExtendedError
= TRUE
;
4804 else SET_AX( context
, 0 );
4808 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
4809 INT21_DriveName(BL_reg(context
)));
4810 if (!INT21_SetDiskSerialNumber(context
)) bSetDOSExtendedError
= TRUE
;
4811 else SET_AX( context
, 1 );
4816 case 0x6a: /* COMMIT FILE */
4817 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context
) );
4818 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context
)) ) &&
4819 GetLastError() != ERROR_ACCESS_DENIED
)
4820 bSetDOSExtendedError
= TRUE
;
4823 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4824 SET_AL( context
, 0 );
4827 case 0x6c: /* EXTENDED OPEN/CREATE */
4828 if (!INT21_CreateFile( context
, context
->Esi
, TRUE
,
4829 BX_reg(context
), DL_reg(context
) ))
4830 bSetDOSExtendedError
= TRUE
;
4833 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
4834 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
4835 SET_CFLAG( context
);
4836 SET_AL( context
, 0 );
4839 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
4840 INT21_LongFilename( context
);
4843 case 0x73: /* MSDOS7 - FAT32 */
4844 RESET_CFLAG( context
);
4845 if (!INT21_Fat32( context
))
4846 bSetDOSExtendedError
= TRUE
;
4849 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
4850 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
4853 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
4854 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
4857 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
4858 /* we don't implement a DOS32 extender */
4859 TRACE( "DOS32 EXTENDER API - ignored\n" );
4863 INT_BARF( context
, 0x21 );
4866 } /* END OF SWITCH */
4868 /* Set general error condition. */
4869 if (bSetDOSExtendedError
)
4871 SET_AX( context
, GetLastError() );
4872 SET_CFLAG( context
);
4875 /* Print error code if carry flag is set. */
4876 if (context
->EFlags
& 0x0001)
4877 TRACE("failed, error %d\n", GetLastError() );
4879 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
4880 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
4881 AX_reg(context
), BX_reg(context
),
4882 CX_reg(context
), DX_reg(context
),
4883 SI_reg(context
), DI_reg(context
),
4884 (WORD
)context
->SegDs
, (WORD
)context
->SegEs
,