Updated.
[wine/multimedia.git] / dlls / winedos / int21.c
blobdd0d016d5b5791322f465152ea84cd403272e591
1 /*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "config.h"
27 #include "wine/port.h"
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winreg.h"
38 #include "winternl.h"
39 #include "wine/winbase16.h"
40 #include "dosexe.h"
41 #include "file.h"
42 #include "winerror.h"
43 #include "winuser.h"
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
46 #include "wine/exception.h"
49 * Note:
50 * - Most of the file related functions are wrong. NT's kernel32
51 * doesn't maintain a per drive current directory, while DOS does.
52 * We should in fact keep track in here of those per driver
53 * directories, and use this infro while dealing with partial paths
54 * (drive defined, but only relative paths). This could even be
55 * created as an array of CDS (there should be an entry for that in
56 * the LOL)
60 * Forward declarations.
62 static BOOL INT21_RenameFile( CONTEXT86 *context );
64 WINE_DEFAULT_DEBUG_CHANNEL(int21);
67 #include "pshpack1.h"
70 * Extended Drive Parameter Block.
71 * This structure is compatible with standard DOS4+ DPB and
72 * extended DOS7 DPB.
74 typedef struct _INT21_DPB {
75 BYTE drive; /* 00 drive number (0=A, ...) */
76 BYTE unit; /* 01 unit number within device driver */
77 WORD sector_bytes; /* 02 bytes per sector */
78 BYTE cluster_sectors; /* 04 highest sector number within a cluster */
79 BYTE shift; /* 05 shift count to convert clusters into sectors */
80 WORD num_reserved; /* 06 reserved sectors at beginning of drive */
81 BYTE num_FAT; /* 08 number of FATs */
82 WORD num_root_entries; /* 09 number of root directory entries */
83 WORD first_data_sector; /* 0b number of first sector containing user data */
84 WORD num_clusters1; /* 0d highest cluster number (number of data clusters + 1) */
85 WORD sectors_per_FAT; /* 0f number of sectors per FAT */
86 WORD first_dir_sector; /* 11 sector number of first directory sector */
87 SEGPTR driver_header; /* 13 address of device driver header */
88 BYTE media_ID; /* 17 media ID byte */
89 BYTE access_flag; /* 18 0x00 if disk accessed, 0xff if not */
90 SEGPTR next; /* 19 pointer to next DPB */
91 WORD search_cluster1; /* 1d cluster at which to start search for free space */
92 WORD free_clusters_lo; /* 1f number of free clusters on drive or 0xffff if unknown */
93 WORD free_clusters_hi; /* 21 hiword of clusters_free */
94 WORD mirroring_flags; /* 23 active FAT/mirroring flags */
95 WORD info_sector; /* 25 sector number of file system info sector or 0xffff for none */
96 WORD spare_boot_sector; /* 27 sector number of backup boot sector or 0xffff for none */
97 DWORD first_cluster_sector; /* 29 sector number of the first cluster */
98 DWORD num_clusters2; /* 2d maximum cluster number */
99 DWORD fat_clusters; /* 31 number of clusters occupied by FAT */
100 DWORD root_cluster; /* 35 cluster number of start of root directory */
101 DWORD search_cluster2; /* 39 cluster at which to start searching for free space */
102 } INT21_DPB;
106 * Structure for DOS data that can be accessed directly from applications.
107 * Real and protected mode pointers will be returned to this structure so
108 * the structure must be correctly packed.
110 typedef struct _INT21_HEAP {
111 WORD uppercase_size; /* Size of the following table in bytes */
112 BYTE uppercase_table[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
114 WORD lowercase_size; /* Size of the following table in bytes */
115 BYTE lowercase_table[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
117 WORD collating_size; /* Size of the following table in bytes */
118 BYTE collating_table[256]; /* Values used to sort characters from 0x00 to 0xff. */
120 WORD filename_size; /* Size of the following filename data in bytes */
121 BYTE filename_reserved1; /* 0x01 for MS-DOS 3.30-6.00 */
122 BYTE filename_lowest; /* Lowest permissible character value for filename */
123 BYTE filename_highest; /* Highest permissible character value for filename */
124 BYTE filename_reserved2; /* 0x00 for MS-DOS 3.30-6.00 */
125 BYTE filename_exclude_first; /* First illegal character in permissible range */
126 BYTE filename_exclude_last; /* Last illegal character in permissible range */
127 BYTE filename_reserved3; /* 0x02 for MS-DOS 3.30-6.00 */
128 BYTE filename_illegal_size; /* Number of terminators in the following table */
129 BYTE filename_illegal_table[16]; /* Characters which terminate a filename */
131 WORD dbcs_size; /* Number of valid ranges in the following table */
132 BYTE dbcs_table[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
134 BYTE misc_indos; /* Interrupt 21 nesting flag */
135 WORD misc_segment; /* Real mode segment for INT21_HEAP */
136 WORD misc_selector; /* Protected mode selector for INT21_HEAP */
137 INT21_DPB misc_dpb_list[MAX_DOS_DRIVES]; /* Drive parameter blocks for all drives */
139 } INT21_HEAP;
142 struct FCB {
143 BYTE drive_number;
144 CHAR file_name[8];
145 CHAR file_extension[3];
146 WORD current_block_number;
147 WORD logical_record_size;
148 DWORD file_size;
149 WORD date_of_last_write;
150 WORD time_of_last_write;
151 BYTE file_number;
152 BYTE attributes;
153 WORD starting_cluster;
154 WORD sequence_number;
155 BYTE file_attributes;
156 BYTE unused;
157 BYTE record_within_current_block;
158 BYTE random_access_record_number[4];
162 struct XFCB {
163 BYTE xfcb_signature;
164 BYTE reserved[5];
165 BYTE xfcb_file_attribute;
166 BYTE fcb[37];
169 /* DTA layout for FindFirst/FindNext */
170 typedef struct
172 BYTE drive; /* 00 drive letter */
173 char mask[11]; /* 01 search template */
174 BYTE search_attr; /* 0c search attributes */
175 WORD count; /* 0d entry count within directory */
176 WORD cluster; /* 0f cluster of parent directory */
177 WCHAR *fullPath; /* 11 full path (was: reserved) */
178 BYTE fileattr; /* 15 file attributes */
179 WORD filetime; /* 16 file time */
180 WORD filedate; /* 18 file date */
181 DWORD filesize; /* 1a file size */
182 char filename[13]; /* 1e file name + extension */
183 } FINDFILE_DTA;
185 /* FCB layout for FindFirstFCB/FindNextFCB */
186 typedef struct
188 BYTE drive; /* 00 drive letter */
189 char filename[11]; /* 01 filename 8+3 format */
190 int count; /* 0c entry count (was: reserved) */
191 WCHAR *fullPath; /* 10 full path (was: reserved) */
192 } FINDFILE_FCB;
194 /* DOS directory entry for FindFirstFCB/FindNextFCB */
195 typedef struct
197 char filename[11]; /* 00 filename 8+3 format */
198 BYTE fileattr; /* 0b file attributes */
199 BYTE reserved[10]; /* 0c reserved */
200 WORD filetime; /* 16 file time */
201 WORD filedate; /* 18 file date */
202 WORD cluster; /* 1a file first cluster */
203 DWORD filesize; /* 1c file size */
204 } DOS_DIRENTRY_LAYOUT;
206 #include "poppack.h"
208 /* dos file attributes */
209 #define FA_NORMAL 0x00 /* Normal file, no attributes */
210 #define FA_RDONLY 0x01 /* Read only attribute */
211 #define FA_HIDDEN 0x02 /* Hidden file */
212 #define FA_SYSTEM 0x04 /* System file */
213 #define FA_LABEL 0x08 /* Volume label */
214 #define FA_DIRECTORY 0x10 /* Directory */
215 #define FA_ARCHIVE 0x20 /* Archive */
216 #define FA_UNUSED 0x40 /* Unused */
218 /* Error codes */
219 #define ER_NoNetwork 0x49
221 /* Error classes */
222 #define EC_OutOfResource 0x01
223 #define EC_Temporary 0x02
224 #define EC_AccessDenied 0x03
225 #define EC_InternalError 0x04
226 #define EC_HardwareFailure 0x05
227 #define EC_SystemFailure 0x06
228 #define EC_ProgramError 0x07
229 #define EC_NotFound 0x08
230 #define EC_MediaError 0x0b
231 #define EC_Exists 0x0c
232 #define EC_Unknown 0x0d
234 /* Suggested actions */
235 #define SA_Retry 0x01
236 #define SA_DelayedRetry 0x02
237 #define SA_Abort 0x04
238 #define SA_Ignore 0x06
239 #define SA_Ask4Retry 0x07
241 /* Error locus */
242 #define EL_Unknown 0x01
243 #define EL_Disk 0x02
244 #define EL_Network 0x03
245 #define EL_Serial 0x04
246 #define EL_Memory 0x05
249 /* Many calls translate a drive argument like this:
250 drive number (00h = default, 01h = A:, etc)
252 /******************************************************************
253 * INT21_DriveName
255 * Many calls translate a drive argument like this:
256 * drive number (00h = default, 01h = A:, etc)
258 static const char *INT21_DriveName(int drive)
260 if (drive > 0)
262 if (drive <= 26) return wine_dbg_sprintf("%c:", 'A' + drive - 1);
263 else return wine_dbg_sprintf( "<Bad drive: %d>", drive);
265 return "default";
268 /***********************************************************************
269 * INT21_GetCurrentDrive
271 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
272 * MAX_DOS_DRIVES on error.
274 static BYTE INT21_GetCurrentDrive(void)
276 WCHAR current_directory[MAX_PATH];
278 if (!GetCurrentDirectoryW( MAX_PATH, current_directory ) ||
279 current_directory[1] != ':')
281 TRACE( "Failed to get current drive.\n" );
282 return MAX_DOS_DRIVES;
285 return toupperW( current_directory[0] ) - 'A';
289 /***********************************************************************
290 * INT21_MapDrive
292 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
293 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
295 static BYTE INT21_MapDrive( BYTE drive )
297 if (drive)
299 WCHAR drivespec[3] = {'A', ':', 0};
300 UINT drivetype;
302 drivespec[0] += drive - 1;
303 drivetype = GetDriveTypeW( drivespec );
305 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
306 return MAX_DOS_DRIVES;
308 return drive - 1;
311 return INT21_GetCurrentDrive();
315 /***********************************************************************
316 * INT21_SetCurrentDrive
318 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
320 static void INT21_SetCurrentDrive( BYTE drive )
322 WCHAR drivespec[3] = {'A', ':', 0};
324 drivespec[0] += drive;
326 if (!SetCurrentDirectoryW( drivespec ))
327 TRACE( "Failed to set current drive.\n" );
331 /***********************************************************************
332 * INT21_ReadChar
334 * Reads a character from the standard input.
335 * Extended keycodes will be returned as two separate characters.
337 static BOOL INT21_ReadChar( BYTE *input, CONTEXT86 *waitctx )
339 static BYTE pending_scan = 0;
341 if (pending_scan)
343 if (input)
344 *input = pending_scan;
345 if (waitctx)
346 pending_scan = 0;
347 return TRUE;
349 else
351 BYTE ascii;
352 BYTE scan;
353 if (!DOSVM_Int16ReadChar( &ascii, &scan, waitctx ))
354 return FALSE;
356 if (input)
357 *input = ascii;
358 if (waitctx && !ascii)
359 pending_scan = scan;
360 return TRUE;
365 /***********************************************************************
366 * INT21_GetSystemCountryCode
368 * Return DOS country code for default system locale.
370 static WORD INT21_GetSystemCountryCode( void )
373 * FIXME: Determine country code. We should probably use
374 * DOSCONF structure for that.
376 return GetSystemDefaultLangID();
380 /***********************************************************************
381 * INT21_FillCountryInformation
383 * Fill 34-byte buffer with country information data using
384 * default system locale.
386 static void INT21_FillCountryInformation( BYTE *buffer )
388 /* 00 - WORD: date format
389 * 00 = mm/dd/yy
390 * 01 = dd/mm/yy
391 * 02 = yy/mm/dd
393 *(WORD*)(buffer + 0) = 0; /* FIXME: Get from locale */
395 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
396 buffer[2] = '$'; /* FIXME: Get from locale */
397 buffer[3] = 0;
399 /* 07 - BYTE[2]: ASCIIZ thousands separator */
400 buffer[7] = 0; /* FIXME: Get from locale */
401 buffer[8] = 0;
403 /* 09 - BYTE[2]: ASCIIZ decimal separator */
404 buffer[9] = '.'; /* FIXME: Get from locale */
405 buffer[10] = 0;
407 /* 11 - BYTE[2]: ASCIIZ date separator */
408 buffer[11] = '/'; /* FIXME: Get from locale */
409 buffer[12] = 0;
411 /* 13 - BYTE[2]: ASCIIZ time separator */
412 buffer[13] = ':'; /* FIXME: Get from locale */
413 buffer[14] = 0;
415 /* 15 - BYTE: Currency format
416 * bit 2 = set if currency symbol replaces decimal point
417 * bit 1 = number of spaces between value and currency symbol
418 * bit 0 = 0 if currency symbol precedes value
419 * 1 if currency symbol follows value
421 buffer[15] = 0; /* FIXME: Get from locale */
423 /* 16 - BYTE: Number of digits after decimal in currency */
424 buffer[16] = 0; /* FIXME: Get from locale */
426 /* 17 - BYTE: Time format
427 * bit 0 = 0 if 12-hour clock
428 * 1 if 24-hour clock
430 buffer[17] = 1; /* FIXME: Get from locale */
432 /* 18 - DWORD: Address of case map routine */
433 *(DWORD*)(buffer + 18) = 0; /* FIXME: ptr to case map routine */
435 /* 22 - BYTE[2]: ASCIIZ data-list separator */
436 buffer[22] = ','; /* FIXME: Get from locale */
437 buffer[23] = 0;
439 /* 24 - BYTE[10]: Reserved */
440 memset( buffer + 24, 0, 10 );
444 /***********************************************************************
445 * INT21_FillHeap
447 * Initialize DOS heap.
449 static void INT21_FillHeap( INT21_HEAP *heap )
451 static const char terminators[] = "\"\\./[]:|<>+=;,";
452 int i;
455 * Uppercase table.
457 heap->uppercase_size = 128;
458 for (i = 0; i < 128; i++)
459 heap->uppercase_table[i] = toupper( 128 + i );
462 * Lowercase table.
464 heap->lowercase_size = 256;
465 for (i = 0; i < 256; i++)
466 heap->lowercase_table[i] = tolower( i );
469 * Collating table.
471 heap->collating_size = 256;
472 for (i = 0; i < 256; i++)
473 heap->collating_table[i] = i;
476 * Filename table.
478 heap->filename_size = 8 + strlen(terminators);
479 heap->filename_illegal_size = strlen(terminators);
480 strcpy( heap->filename_illegal_table, terminators );
482 heap->filename_reserved1 = 0x01;
483 heap->filename_lowest = 0; /* FIXME: correct value? */
484 heap->filename_highest = 0xff; /* FIXME: correct value? */
485 heap->filename_reserved2 = 0x00;
486 heap->filename_exclude_first = 0x00; /* FIXME: correct value? */
487 heap->filename_exclude_last = 0x00; /* FIXME: correct value? */
488 heap->filename_reserved3 = 0x02;
491 * DBCS lead byte table. This table is empty.
493 heap->dbcs_size = 0;
494 memset( heap->dbcs_table, 0, sizeof(heap->dbcs_table) );
497 * Initialize InDos flag.
499 heap->misc_indos = 0;
502 * FIXME: Should drive parameter blocks (DPB) be
503 * initialized here and linked to DOS LOL?
508 /***********************************************************************
509 * INT21_GetHeapPointer
511 * Get pointer for DOS heap (INT21_HEAP).
512 * Creates and initializes heap on first call.
514 static INT21_HEAP *INT21_GetHeapPointer( void )
516 static INT21_HEAP *heap_pointer = NULL;
518 if (!heap_pointer)
520 WORD heap_segment;
521 WORD heap_selector;
523 heap_pointer = DOSVM_AllocDataUMB( sizeof(INT21_HEAP),
524 &heap_segment,
525 &heap_selector );
527 heap_pointer->misc_segment = heap_segment;
528 heap_pointer->misc_selector = heap_selector;
529 INT21_FillHeap( heap_pointer );
532 return heap_pointer;
536 /***********************************************************************
537 * INT21_GetHeapSelector
539 * Get segment/selector for DOS heap (INT21_HEAP).
540 * Creates and initializes heap on first call.
542 static WORD INT21_GetHeapSelector( CONTEXT86 *context )
544 INT21_HEAP *heap = INT21_GetHeapPointer();
546 if (!ISV86(context) && DOSVM_IsWin16())
547 return heap->misc_selector;
548 else
549 return heap->misc_segment;
553 /***********************************************************************
554 * INT21_FillDrivePB
556 * Fill DOS heap drive parameter block for the specified drive.
557 * Return TRUE if drive was valid and there were
558 * no errors while reading drive information.
560 static BOOL INT21_FillDrivePB( BYTE drive )
562 WCHAR drivespec[3] = {'A', ':', 0};
563 INT21_HEAP *heap = INT21_GetHeapPointer();
564 INT21_DPB *dpb;
565 UINT drivetype;
566 DWORD cluster_sectors;
567 DWORD sector_bytes;
568 DWORD free_clusters;
569 DWORD total_clusters;
571 if (drive >= MAX_DOS_DRIVES)
572 return FALSE;
574 dpb = &heap->misc_dpb_list[drive];
575 drivespec[0] += drive;
576 drivetype = GetDriveTypeW( drivespec );
579 * FIXME: Does this check work correctly with floppy/cdrom drives?
581 if (drivetype == DRIVE_NO_ROOT_DIR || drivetype == DRIVE_UNKNOWN)
582 return FALSE;
585 * FIXME: Does this check work correctly with floppy/cdrom drives?
587 if (!GetDiskFreeSpaceW( drivespec, &cluster_sectors, &sector_bytes,
588 &free_clusters, &total_clusters ))
589 return FALSE;
592 * FIXME: Most of the values listed below are incorrect.
593 * All values should be validated.
596 dpb->drive = drive;
597 dpb->unit = 0;
598 dpb->sector_bytes = sector_bytes;
599 dpb->cluster_sectors = cluster_sectors - 1;
601 dpb->shift = 0;
602 while (cluster_sectors > 1)
604 cluster_sectors /= 2;
605 dpb->shift++;
608 dpb->num_reserved = 0;
609 dpb->num_FAT = 1;
610 dpb->num_root_entries = 2;
611 dpb->first_data_sector = 2;
612 dpb->num_clusters1 = total_clusters;
613 dpb->sectors_per_FAT = 1;
614 dpb->first_dir_sector = 1;
615 dpb->driver_header = 0;
616 dpb->media_ID = (drivetype == DRIVE_FIXED) ? 0xF8 : 0xF0;
617 dpb->access_flag = 0;
618 dpb->next = 0;
619 dpb->search_cluster1 = 0;
620 dpb->free_clusters_lo = LOWORD(free_clusters);
621 dpb->free_clusters_hi = HIWORD(free_clusters);
622 dpb->mirroring_flags = 0;
623 dpb->info_sector = 0xffff;
624 dpb->spare_boot_sector = 0xffff;
625 dpb->first_cluster_sector = 0;
626 dpb->num_clusters2 = total_clusters;
627 dpb->fat_clusters = 32;
628 dpb->root_cluster = 0;
629 dpb->search_cluster2 = 0;
631 return TRUE;
635 /***********************************************************************
636 * INT21_GetCurrentDirectory
638 * Handler for:
639 * - function 0x47
640 * - subfunction 0x47 of function 0x71
642 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context, BOOL islong )
644 char *buffer = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi);
645 BYTE new_drive = INT21_MapDrive( DL_reg(context) );
646 BYTE old_drive = INT21_GetCurrentDrive();
647 WCHAR pathW[MAX_PATH];
648 char pathA[MAX_PATH];
649 WCHAR *ptr = pathW;
651 TRACE( "drive %d\n", DL_reg(context) );
653 if (new_drive == MAX_DOS_DRIVES)
655 SetLastError(ERROR_INVALID_DRIVE);
656 return FALSE;
660 * Grab current directory.
663 INT21_SetCurrentDrive( new_drive );
664 if (!GetCurrentDirectoryW( MAX_PATH, pathW ))
666 INT21_SetCurrentDrive( old_drive );
667 return FALSE;
669 INT21_SetCurrentDrive( old_drive );
672 * Convert into short format.
675 if (!islong)
677 DWORD result = GetShortPathNameW( pathW, pathW, MAX_PATH );
678 if (!result)
679 return FALSE;
680 if (result > MAX_PATH)
682 WARN( "Short path too long!\n" );
683 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
684 return FALSE;
689 * The returned pathname does not include
690 * the drive letter, colon or leading backslash.
693 if (ptr[0] == '\\')
696 * FIXME: We should probably just strip host part from name...
698 FIXME( "UNC names are not supported.\n" );
699 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
700 return FALSE;
702 else if (!ptr[0] || ptr[1] != ':' || ptr[2] != '\\')
704 WARN( "Path is neither UNC nor DOS path: %s\n",
705 wine_dbgstr_w(ptr) );
706 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
707 return FALSE;
709 else
711 /* Remove drive letter, colon and leading backslash. */
712 ptr += 3;
716 * Convert into OEM string.
719 if (!WideCharToMultiByte(CP_OEMCP, 0, ptr, -1, pathA,
720 MAX_PATH, NULL, NULL))
722 WARN( "Cannot convert path!\n" );
723 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
724 return FALSE;
728 * Success.
731 if (!islong)
733 /* Undocumented success code. */
734 SET_AX( context, 0x0100 );
736 /* Truncate buffer to 64 bytes. */
737 pathA[63] = 0;
740 TRACE( "%c:=%s\n", 'A' + new_drive, pathA );
742 strcpy( buffer, pathA );
743 return TRUE;
747 /***********************************************************************
748 * INT21_SetCurrentDirectory
750 * Handler for:
751 * - function 0x3b
752 * - subfunction 0x3b of function 0x71
754 static BOOL INT21_SetCurrentDirectory( CONTEXT86 *context )
756 WCHAR dirW[MAX_PATH];
757 char *dirA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
758 BYTE drive = INT21_GetCurrentDrive();
759 BOOL result;
761 TRACE( "SET CURRENT DIRECTORY %s\n", dirA );
763 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
764 result = SetCurrentDirectoryW( dirW );
766 /* This function must not change current drive. */
767 INT21_SetCurrentDrive( drive );
769 return result;
773 /***********************************************************************
774 * INT21_CreateFile
776 * Handler for:
777 * - function 0x3c
778 * - function 0x3d
779 * - function 0x5b
780 * - function 0x6c
781 * - subfunction 0x6c of function 0x71
783 static BOOL INT21_CreateFile( CONTEXT86 *context,
784 DWORD pathSegOff,
785 BOOL returnStatus,
786 WORD dosAccessShare,
787 BYTE dosAction )
789 WORD dosStatus;
790 char *pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, pathSegOff);
791 WCHAR pathW[MAX_PATH];
792 DWORD winAccess;
793 DWORD winAttributes;
794 HANDLE winHandle;
795 DWORD winMode;
796 DWORD winSharing;
798 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
799 "create flags=%04x, file=%s.\n",
800 AH_reg(context), dosAction, dosAccessShare, CX_reg(context), pathA );
803 * Application tried to create/open a file whose name
804 * ends with a backslash. This is not allowed.
806 * FIXME: This needs to be validated, especially the return value.
808 if (pathA[strlen(pathA) - 1] == '/')
810 SetLastError( ERROR_FILE_NOT_FOUND );
811 return FALSE;
815 * Convert DOS action flags into Win32 creation disposition parameter.
817 switch(dosAction)
819 case 0x01:
820 winMode = OPEN_EXISTING;
821 break;
822 case 0x02:
823 winMode = TRUNCATE_EXISTING;
824 break;
825 case 0x10:
826 winMode = CREATE_NEW;
827 break;
828 case 0x11:
829 winMode = OPEN_ALWAYS;
830 break;
831 case 0x12:
832 winMode = CREATE_ALWAYS;
833 break;
834 default:
835 SetLastError( ERROR_INVALID_PARAMETER );
836 return FALSE;
840 * Convert DOS access/share flags into Win32 desired access parameter.
842 switch(dosAccessShare & 0x07)
844 case OF_READ:
845 winAccess = GENERIC_READ;
846 break;
847 case OF_WRITE:
848 winAccess = GENERIC_WRITE;
849 break;
850 case OF_READWRITE:
851 winAccess = GENERIC_READ | GENERIC_WRITE;
852 break;
853 case 0x04:
855 * Read-only, do not modify file's last-access time (DOS7).
857 * FIXME: How to prevent modification of last-access time?
859 winAccess = GENERIC_READ;
860 break;
861 default:
862 winAccess = 0;
866 * Convert DOS access/share flags into Win32 share mode parameter.
868 switch(dosAccessShare & 0x70)
870 case OF_SHARE_EXCLUSIVE:
871 winSharing = 0;
872 break;
873 case OF_SHARE_DENY_WRITE:
874 winSharing = FILE_SHARE_READ;
875 break;
876 case OF_SHARE_DENY_READ:
877 winSharing = FILE_SHARE_WRITE;
878 break;
879 case OF_SHARE_DENY_NONE:
880 case OF_SHARE_COMPAT:
881 default:
882 winSharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
886 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
887 * What to do with this bit?
888 * FIXME: Bits in the high byte of dosAccessShare are not supported.
889 * See both function 0x6c and subfunction 0x6c of function 0x71 for
890 * definition of these bits.
894 * Convert DOS create attributes into Win32 flags and attributes parameter.
896 if (winMode == OPEN_EXISTING || winMode == TRUNCATE_EXISTING)
898 winAttributes = 0;
900 else
902 WORD dosAttributes = CX_reg(context);
904 if (dosAttributes & FILE_ATTRIBUTE_LABEL)
907 * Application tried to create volume label entry.
908 * This is difficult to support so we do not allow it.
910 * FIXME: If volume does not already have a label,
911 * this function is supposed to succeed.
913 SetLastError( ERROR_ACCESS_DENIED );
914 return TRUE;
917 winAttributes = dosAttributes &
918 (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
919 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE);
923 * Open the file.
925 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
927 winHandle = CreateFileW( pathW, winAccess, winSharing, NULL,
928 winMode, winAttributes, 0 );
930 if (winHandle == INVALID_HANDLE_VALUE)
931 return FALSE;
934 * Determine DOS file status.
936 * 1 = file opened
937 * 2 = file created
938 * 3 = file replaced
940 switch(winMode)
942 case OPEN_EXISTING:
943 dosStatus = 1;
944 break;
945 case TRUNCATE_EXISTING:
946 dosStatus = 3;
947 break;
948 case CREATE_NEW:
949 dosStatus = 2;
950 break;
951 case OPEN_ALWAYS:
952 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 1 : 2;
953 break;
954 case CREATE_ALWAYS:
955 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 3 : 2;
956 break;
957 default:
958 dosStatus = 0;
962 * Return DOS file handle and DOS status.
964 SET_AX( context, Win32HandleToDosFileHandle(winHandle) );
966 if (returnStatus)
967 SET_CX( context, dosStatus );
969 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
970 AX_reg(context), dosStatus );
972 return TRUE;
976 /***********************************************************************
977 * INT21_BufferedInput
979 * Handler for function 0x0a and reading from console using
980 * function 0x3f.
982 * Reads a string of characters from standard input until
983 * enter key is pressed. Returns either number of characters
984 * read from console including terminating CR or
985 * zero if capacity was zero.
987 static WORD INT21_BufferedInput( CONTEXT86 *context, BYTE *ptr, WORD capacity )
989 BYTE length = 0;
992 * Return immediately if capacity is zero.
994 if (capacity == 0)
995 return 0;
997 while(TRUE)
999 BYTE ascii;
1000 BYTE scan;
1002 DOSVM_Int16ReadChar( &ascii, &scan, context );
1004 if (ascii == '\r' || ascii == '\n')
1007 * FIXME: What should be echoed here?
1009 DOSVM_PutChar( '\r' );
1010 DOSVM_PutChar( '\n' );
1011 ptr[length] = '\r';
1012 return length + 1;
1016 * FIXME: This function is supposed to support
1017 * DOS editing keys...
1021 * If the buffer becomes filled to within one byte of
1022 * capacity, DOS rejects all further characters up to,
1023 * but not including, the terminating carriage return.
1025 if (ascii != 0 && length < capacity-1)
1027 DOSVM_PutChar( ascii );
1028 ptr[length] = ascii;
1029 length++;
1035 /***********************************************************************
1036 * INT21_GetCurrentDTA
1038 static BYTE *INT21_GetCurrentDTA( CONTEXT86 *context )
1040 TDB *pTask = GlobalLock16(GetCurrentTask());
1042 /* FIXME: This assumes DTA was set correctly! */
1043 return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
1044 (DWORD)OFFSETOF(pTask->dta) );
1048 /***********************************************************************
1049 * INT21_OpenFileUsingFCB
1051 * Handler for function 0x0f.
1053 * PARAMS
1054 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
1056 * RETURNS (in AL)
1057 * 0x00: successful
1058 * 0xff: failed
1060 * NOTES
1061 * Opens a FCB file for read/write in compatibility mode. Upon calling
1062 * the FCB must have the drive_number, file_name, and file_extension
1063 * fields filled and all other bytes cleared.
1065 static void INT21_OpenFileUsingFCB( CONTEXT86 *context )
1067 struct FCB *fcb;
1068 struct XFCB *xfcb;
1069 char file_path[16];
1070 char *pos;
1071 HANDLE handle;
1072 HFILE16 hfile16;
1073 BY_HANDLE_FILE_INFORMATION info;
1074 BYTE AL_result;
1076 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1077 if (fcb->drive_number == 0xff) {
1078 xfcb = (struct XFCB *) fcb;
1079 fcb = (struct FCB *) xfcb->fcb;
1080 } /* if */
1082 AL_result = 0;
1083 file_path[0] = 'A' + INT21_MapDrive( fcb->drive_number );
1085 if (AL_result == 0) {
1086 file_path[1] = ':';
1087 pos = &file_path[2];
1088 memcpy(pos, fcb->file_name, 8);
1089 pos[8] = ' ';
1090 pos[9] = '\0';
1091 pos = strchr(pos, ' ');
1092 *pos = '.';
1093 pos++;
1094 memcpy(pos, fcb->file_extension, 3);
1095 pos[3] = ' ';
1096 pos[4] = '\0';
1097 pos = strchr(pos, ' ');
1098 *pos = '\0';
1100 handle = (HANDLE) _lopen(file_path, OF_READWRITE);
1101 if (handle == INVALID_HANDLE_VALUE) {
1102 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path);
1103 AL_result = 0xff; /* failed */
1104 } else {
1105 hfile16 = Win32HandleToDosFileHandle(handle);
1106 if (hfile16 == HFILE_ERROR16) {
1107 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle);
1108 CloseHandle(handle);
1109 AL_result = 0xff; /* failed */
1110 } else if (hfile16 > 255) {
1111 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16, file_path);
1112 _lclose16(hfile16);
1113 AL_result = 0xff; /* failed */
1114 } else {
1115 if (!GetFileInformationByHandle(handle, &info)) {
1116 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1117 hfile16, handle, file_path);
1118 _lclose16(hfile16);
1119 AL_result = 0xff; /* failed */
1120 } else {
1121 fcb->drive_number = file_path[0] - 'A' + 1;
1122 fcb->current_block_number = 0;
1123 fcb->logical_record_size = 128;
1124 fcb->file_size = info.nFileSizeLow;
1125 FileTimeToDosDateTime(&info.ftLastWriteTime,
1126 &fcb->date_of_last_write, &fcb->time_of_last_write);
1127 fcb->file_number = hfile16;
1128 fcb->attributes = 0xc2;
1129 fcb->starting_cluster = 0; /* don't know correct init value */
1130 fcb->sequence_number = 0; /* don't know correct init value */
1131 fcb->file_attributes = info.dwFileAttributes;
1132 /* The following fields are not initialized */
1133 /* by the native function: */
1134 /* unused */
1135 /* record_within_current_block */
1136 /* random_access_record_number */
1138 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1139 file_path, hfile16, handle);
1140 AL_result = 0x00; /* successful */
1141 } /* if */
1142 } /* if */
1143 } /* if */
1144 } /* if */
1145 SET_AL(context, AL_result);
1149 /***********************************************************************
1150 * INT21_CloseFileUsingFCB
1152 * Handler for function 0x10.
1154 * PARAMS
1155 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1157 * RETURNS (in AL)
1158 * 0x00: successful
1159 * 0xff: failed
1161 * NOTES
1162 * Closes a FCB file.
1164 static void INT21_CloseFileUsingFCB( CONTEXT86 *context )
1166 struct FCB *fcb;
1167 struct XFCB *xfcb;
1168 BYTE AL_result;
1170 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1171 if (fcb->drive_number == 0xff) {
1172 xfcb = (struct XFCB *) fcb;
1173 fcb = (struct FCB *) xfcb->fcb;
1174 } /* if */
1176 if (_lclose16((HFILE16) fcb->file_number) != 0) {
1177 TRACE("_lclose16(%d) failed\n", fcb->file_number);
1178 AL_result = 0xff; /* failed */
1179 } else {
1180 TRACE("successful closed file %d\n", fcb->file_number);
1181 AL_result = 0x00; /* successful */
1182 } /* if */
1183 SET_AL(context, AL_result);
1187 /***********************************************************************
1188 * INT21_SequentialReadFromFCB
1190 * Handler for function 0x14.
1192 * PARAMS
1193 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1195 * RETURNS (in AL)
1196 * 0: successful
1197 * 1: end of file, no data read
1198 * 2: segment wrap in DTA, no data read (not returned now)
1199 * 3: end of file, partial record read
1201 * NOTES
1202 * Reads a record with the size FCB->logical_record_size from the FCB
1203 * to the disk transfer area. The position of the record is specified
1204 * with FCB->current_block_number and FCB->record_within_current_block.
1205 * Then FCB->current_block_number and FCB->record_within_current_block
1206 * are updated to point to the next record. If a partial record is
1207 * read, it is filled with zeros up to the FCB->logical_record_size.
1209 static void INT21_SequentialReadFromFCB( CONTEXT86 *context )
1211 struct FCB *fcb;
1212 struct XFCB *xfcb;
1213 HANDLE handle;
1214 DWORD record_number;
1215 long position;
1216 BYTE *disk_transfer_area;
1217 UINT bytes_read;
1218 BYTE AL_result;
1220 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1221 if (fcb->drive_number == 0xff) {
1222 xfcb = (struct XFCB *) fcb;
1223 fcb = (struct FCB *) xfcb->fcb;
1224 } /* if */
1226 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1227 if (handle == INVALID_HANDLE_VALUE) {
1228 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1229 fcb->file_number);
1230 AL_result = 0x01; /* end of file, no data read */
1231 } else {
1232 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1233 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1234 if (position != record_number * fcb->logical_record_size) {
1235 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1236 fcb->file_number, record_number * fcb->logical_record_size, position);
1237 AL_result = 0x01; /* end of file, no data read */
1238 } else {
1239 disk_transfer_area = INT21_GetCurrentDTA(context);
1240 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1241 if (bytes_read != fcb->logical_record_size) {
1242 TRACE("_lread(%d, %p, %d) failed with %d\n",
1243 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1244 if (bytes_read == 0) {
1245 AL_result = 0x01; /* end of file, no data read */
1246 } else {
1247 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1248 AL_result = 0x03; /* end of file, partial record read */
1249 } /* if */
1250 } else {
1251 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1252 bytes_read, record_number, position, fcb->file_number, handle);
1253 AL_result = 0x00; /* successful */
1254 } /* if */
1255 } /* if */
1256 } /* if */
1257 if (AL_result == 0x00 || AL_result == 0x03) {
1258 if (fcb->record_within_current_block == 127) {
1259 fcb->record_within_current_block = 0;
1260 fcb->current_block_number++;
1261 } else {
1262 fcb->record_within_current_block++;
1263 } /* if */
1264 } /* if */
1265 SET_AL(context, AL_result);
1269 /***********************************************************************
1270 * INT21_SequentialWriteToFCB
1272 * Handler for function 0x15.
1274 * PARAMS
1275 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1277 * RETURNS (in AL)
1278 * 0: successful
1279 * 1: disk full
1280 * 2: segment wrap in DTA (not returned now)
1282 * NOTES
1283 * Writes a record with the size FCB->logical_record_size from the disk
1284 * transfer area to the FCB. The position of the record is specified
1285 * with FCB->current_block_number and FCB->record_within_current_block.
1286 * Then FCB->current_block_number and FCB->record_within_current_block
1287 * are updated to point to the next record.
1289 static void INT21_SequentialWriteToFCB( CONTEXT86 *context )
1291 struct FCB *fcb;
1292 struct XFCB *xfcb;
1293 HANDLE handle;
1294 DWORD record_number;
1295 long position;
1296 BYTE *disk_transfer_area;
1297 UINT bytes_written;
1298 BYTE AL_result;
1300 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1301 if (fcb->drive_number == 0xff) {
1302 xfcb = (struct XFCB *) fcb;
1303 fcb = (struct FCB *) xfcb->fcb;
1304 } /* if */
1306 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1307 if (handle == INVALID_HANDLE_VALUE) {
1308 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1309 fcb->file_number);
1310 AL_result = 0x01; /* disk full */
1311 } else {
1312 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1313 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1314 if (position != record_number * fcb->logical_record_size) {
1315 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1316 fcb->file_number, record_number * fcb->logical_record_size, position);
1317 AL_result = 0x01; /* disk full */
1318 } else {
1319 disk_transfer_area = INT21_GetCurrentDTA(context);
1320 bytes_written = _lwrite((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1321 if (bytes_written != fcb->logical_record_size) {
1322 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1323 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1324 AL_result = 0x01; /* disk full */
1325 } else {
1326 TRACE("successful written %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1327 bytes_written, record_number, position, fcb->file_number, handle);
1328 AL_result = 0x00; /* successful */
1329 } /* if */
1330 } /* if */
1331 } /* if */
1332 if (AL_result == 0x00) {
1333 if (fcb->record_within_current_block == 127) {
1334 fcb->record_within_current_block = 0;
1335 fcb->current_block_number++;
1336 } else {
1337 fcb->record_within_current_block++;
1338 } /* if */
1339 } /* if */
1340 SET_AL(context, AL_result);
1344 /***********************************************************************
1345 * INT21_ReadRandomRecordFromFCB
1347 * Handler for function 0x21.
1349 * PARAMS
1350 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1352 * RETURNS (in AL)
1353 * 0: successful
1354 * 1: end of file, no data read
1355 * 2: segment wrap in DTA, no data read (not returned now)
1356 * 3: end of file, partial record read
1358 * NOTES
1359 * Reads a record with the size FCB->logical_record_size from
1360 * the FCB to the disk transfer area. The position of the record
1361 * is specified with FCB->random_access_record_number. The
1362 * FCB->random_access_record_number is not updated. If a partial record
1363 * is read, it is filled with zeros up to the FCB->logical_record_size.
1365 static void INT21_ReadRandomRecordFromFCB( CONTEXT86 *context )
1367 struct FCB *fcb;
1368 struct XFCB *xfcb;
1369 HANDLE handle;
1370 DWORD record_number;
1371 long position;
1372 BYTE *disk_transfer_area;
1373 UINT bytes_read;
1374 BYTE AL_result;
1376 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1377 if (fcb->drive_number == 0xff) {
1378 xfcb = (struct XFCB *) fcb;
1379 fcb = (struct FCB *) xfcb->fcb;
1380 } /* if */
1382 memcpy(&record_number, fcb->random_access_record_number, 4);
1383 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1384 if (handle == INVALID_HANDLE_VALUE) {
1385 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1386 fcb->file_number);
1387 AL_result = 0x01; /* end of file, no data read */
1388 } else {
1389 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1390 if (position != record_number * fcb->logical_record_size) {
1391 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1392 fcb->file_number, record_number * fcb->logical_record_size, position);
1393 AL_result = 0x01; /* end of file, no data read */
1394 } else {
1395 disk_transfer_area = INT21_GetCurrentDTA(context);
1396 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1397 if (bytes_read != fcb->logical_record_size) {
1398 TRACE("_lread(%d, %p, %d) failed with %d\n",
1399 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1400 if (bytes_read == 0) {
1401 AL_result = 0x01; /* end of file, no data read */
1402 } else {
1403 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1404 AL_result = 0x03; /* end of file, partial record read */
1405 } /* if */
1406 } else {
1407 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1408 bytes_read, record_number, position, fcb->file_number, handle);
1409 AL_result = 0x00; /* successful */
1410 } /* if */
1411 } /* if */
1412 } /* if */
1413 fcb->current_block_number = record_number / 128;
1414 fcb->record_within_current_block = record_number % 128;
1415 SET_AL(context, AL_result);
1419 /***********************************************************************
1420 * INT21_WriteRandomRecordToFCB
1422 * Handler for function 0x22.
1424 * PARAMS
1425 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1427 * RETURNS (in AL)
1428 * 0: successful
1429 * 1: disk full
1430 * 2: segment wrap in DTA (not returned now)
1432 * NOTES
1433 * Writes a record with the size FCB->logical_record_size from
1434 * the disk transfer area to the FCB. The position of the record
1435 * is specified with FCB->random_access_record_number. The
1436 * FCB->random_access_record_number is not updated.
1438 static void INT21_WriteRandomRecordToFCB( CONTEXT86 *context )
1440 struct FCB *fcb;
1441 struct XFCB *xfcb;
1442 HANDLE handle;
1443 DWORD record_number;
1444 long position;
1445 BYTE *disk_transfer_area;
1446 UINT bytes_written;
1447 BYTE AL_result;
1449 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1450 if (fcb->drive_number == 0xff) {
1451 xfcb = (struct XFCB *) fcb;
1452 fcb = (struct FCB *) xfcb->fcb;
1453 } /* if */
1455 memcpy(&record_number, fcb->random_access_record_number, 4);
1456 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1457 if (handle == INVALID_HANDLE_VALUE) {
1458 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1459 fcb->file_number);
1460 AL_result = 0x01; /* disk full */
1461 } else {
1462 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1463 if (position != record_number * fcb->logical_record_size) {
1464 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1465 fcb->file_number, record_number * fcb->logical_record_size, position);
1466 AL_result = 0x01; /* disk full */
1467 } else {
1468 disk_transfer_area = INT21_GetCurrentDTA(context);
1469 bytes_written = _lwrite((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1470 if (bytes_written != fcb->logical_record_size) {
1471 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1472 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1473 AL_result = 0x01; /* disk full */
1474 } else {
1475 TRACE("successful written %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1476 bytes_written, record_number, position, fcb->file_number, handle);
1477 AL_result = 0x00; /* successful */
1478 } /* if */
1479 } /* if */
1480 } /* if */
1481 fcb->current_block_number = record_number / 128;
1482 fcb->record_within_current_block = record_number % 128;
1483 SET_AL(context, AL_result);
1487 /***********************************************************************
1488 * INT21_RandomBlockReadFromFCB
1490 * Handler for function 0x27.
1492 * PARAMS
1493 * CX [I/O] Number of records to read
1494 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1496 * RETURNS (in AL)
1497 * 0: successful
1498 * 1: end of file, no data read
1499 * 2: segment wrap in DTA, no data read (not returned now)
1500 * 3: end of file, partial record read
1502 * NOTES
1503 * Reads several records with the size FCB->logical_record_size from
1504 * the FCB to the disk transfer area. The number of records to be
1505 * read is specified in the CX register. The position of the first
1506 * record is specified with FCB->random_access_record_number. The
1507 * FCB->random_access_record_number, the FCB->current_block_number
1508 * and FCB->record_within_current_block are updated to point to the
1509 * next record after the records read. If a partial record is read,
1510 * it is filled with zeros up to the FCB->logical_record_size. The
1511 * CX register is set to the number of successfully read records.
1513 static void INT21_RandomBlockReadFromFCB( CONTEXT86 *context )
1515 struct FCB *fcb;
1516 struct XFCB *xfcb;
1517 HANDLE handle;
1518 DWORD record_number;
1519 long position;
1520 BYTE *disk_transfer_area;
1521 UINT records_requested;
1522 UINT bytes_requested;
1523 UINT bytes_read;
1524 UINT records_read;
1525 BYTE AL_result;
1527 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1528 if (fcb->drive_number == 0xff) {
1529 xfcb = (struct XFCB *) fcb;
1530 fcb = (struct FCB *) xfcb->fcb;
1531 } /* if */
1533 memcpy(&record_number, fcb->random_access_record_number, 4);
1534 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1535 if (handle == INVALID_HANDLE_VALUE) {
1536 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1537 fcb->file_number);
1538 records_read = 0;
1539 AL_result = 0x01; /* end of file, no data read */
1540 } else {
1541 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1542 if (position != record_number * fcb->logical_record_size) {
1543 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1544 fcb->file_number, record_number * fcb->logical_record_size, position);
1545 records_read = 0;
1546 AL_result = 0x01; /* end of file, no data read */
1547 } else {
1548 disk_transfer_area = INT21_GetCurrentDTA(context);
1549 records_requested = CX_reg(context);
1550 bytes_requested = (UINT) records_requested * fcb->logical_record_size;
1551 bytes_read = _lread((HFILE) handle, disk_transfer_area, bytes_requested);
1552 if (bytes_read != bytes_requested) {
1553 TRACE("_lread(%d, %p, %d) failed with %d\n",
1554 fcb->file_number, disk_transfer_area, bytes_requested, bytes_read);
1555 records_read = bytes_read / fcb->logical_record_size;
1556 if (bytes_read % fcb->logical_record_size == 0) {
1557 AL_result = 0x01; /* end of file, no data read */
1558 } else {
1559 records_read++;
1560 memset(&disk_transfer_area[bytes_read], 0, records_read * fcb->logical_record_size - bytes_read);
1561 AL_result = 0x03; /* end of file, partial record read */
1562 } /* if */
1563 } else {
1564 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1565 bytes_read, record_number, position, fcb->file_number, handle);
1566 records_read = records_requested;
1567 AL_result = 0x00; /* successful */
1568 } /* if */
1569 } /* if */
1570 } /* if */
1571 record_number += records_read;
1572 memcpy(fcb->random_access_record_number, &record_number, 4);
1573 fcb->current_block_number = record_number / 128;
1574 fcb->record_within_current_block = record_number % 128;
1575 SET_CX(context, records_read);
1576 SET_AL(context, AL_result);
1580 /***********************************************************************
1581 * INT21_RandomBlockWriteToFCB
1583 * Handler for function 0x28.
1585 * PARAMS
1586 * CX [I/O] Number of records to write
1587 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1589 * RETURNS (in AL)
1590 * 0: successful
1591 * 1: disk full
1592 * 2: segment wrap in DTA (not returned now)
1594 * NOTES
1595 * Writes several records with the size FCB->logical_record_size from
1596 * the disk transfer area to the FCB. The number of records to be
1597 * written is specified in the CX register. The position of the first
1598 * record is specified with FCB->random_access_record_number. The
1599 * FCB->random_access_record_number, the FCB->current_block_number
1600 * and FCB->record_within_current_block are updated to point to the
1601 * next record after the records written. The CX register is set to
1602 * the number of successfully written records.
1604 static void INT21_RandomBlockWriteToFCB( CONTEXT86 *context )
1606 struct FCB *fcb;
1607 struct XFCB *xfcb;
1608 HANDLE handle;
1609 DWORD record_number;
1610 long position;
1611 BYTE *disk_transfer_area;
1612 UINT records_requested;
1613 UINT bytes_requested;
1614 UINT bytes_written;
1615 UINT records_written;
1616 BYTE AL_result;
1618 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1619 if (fcb->drive_number == 0xff) {
1620 xfcb = (struct XFCB *) fcb;
1621 fcb = (struct FCB *) xfcb->fcb;
1622 } /* if */
1624 memcpy(&record_number, fcb->random_access_record_number, 4);
1625 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1626 if (handle == INVALID_HANDLE_VALUE) {
1627 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1628 fcb->file_number);
1629 records_written = 0;
1630 AL_result = 0x01; /* disk full */
1631 } else {
1632 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1633 if (position != record_number * fcb->logical_record_size) {
1634 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1635 fcb->file_number, record_number * fcb->logical_record_size, position);
1636 records_written = 0;
1637 AL_result = 0x01; /* disk full */
1638 } else {
1639 disk_transfer_area = INT21_GetCurrentDTA(context);
1640 records_requested = CX_reg(context);
1641 bytes_requested = (UINT) records_requested * fcb->logical_record_size;
1642 bytes_written = _lwrite((HFILE) handle, disk_transfer_area, bytes_requested);
1643 if (bytes_written != bytes_requested) {
1644 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1645 fcb->file_number, disk_transfer_area, bytes_requested, bytes_written);
1646 records_written = bytes_written / fcb->logical_record_size;
1647 AL_result = 0x01; /* disk full */
1648 } else {
1649 TRACE("successful write %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1650 bytes_written, record_number, position, fcb->file_number, handle);
1651 records_written = records_requested;
1652 AL_result = 0x00; /* successful */
1653 } /* if */
1654 } /* if */
1655 } /* if */
1656 record_number += records_written;
1657 memcpy(fcb->random_access_record_number, &record_number, 4);
1658 fcb->current_block_number = record_number / 128;
1659 fcb->record_within_current_block = record_number % 128;
1660 SET_CX(context, records_written);
1661 SET_AL(context, AL_result);
1665 /***********************************************************************
1666 * INT21_CreateDirectory
1668 * Handler for:
1669 * - function 0x39
1670 * - subfunction 0x39 of function 0x71
1671 * - subfunction 0xff of function 0x43 (CL == 0x39)
1673 static BOOL INT21_CreateDirectory( CONTEXT86 *context )
1675 WCHAR dirW[MAX_PATH];
1676 char *dirA = CTX_SEG_OFF_TO_LIN(context,
1677 context->SegDs,
1678 context->Edx);
1680 TRACE( "CREATE DIRECTORY %s\n", dirA );
1682 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
1684 if (CreateDirectoryW(dirW, NULL))
1685 return TRUE;
1688 * FIXME: CreateDirectory's LastErrors will clash with the ones
1689 * used by DOS. AH=39 only returns 3 (path not found) and
1690 * 5 (access denied), while CreateDirectory return several
1691 * ones. Remap some of them. -Marcus
1693 switch (GetLastError())
1695 case ERROR_ALREADY_EXISTS:
1696 case ERROR_FILENAME_EXCED_RANGE:
1697 case ERROR_DISK_FULL:
1698 SetLastError(ERROR_ACCESS_DENIED);
1699 break;
1700 default:
1701 break;
1704 return FALSE;
1708 /***********************************************************************
1709 * INT21_ExtendedCountryInformation
1711 * Handler for function 0x65.
1713 static void INT21_ExtendedCountryInformation( CONTEXT86 *context )
1715 BYTE *dataptr = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
1717 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1718 AL_reg(context) );
1721 * Check subfunctions that are passed country and code page.
1723 if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
1725 WORD country = DX_reg(context);
1726 WORD codepage = BX_reg(context);
1728 if (country != 0xffff && country != INT21_GetSystemCountryCode())
1729 FIXME( "Requested info on non-default country %04x\n", country );
1731 if (codepage != 0xffff && codepage != GetOEMCP())
1732 FIXME( "Requested info on non-default code page %04x\n", codepage );
1735 switch (AL_reg(context)) {
1736 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1737 INT_BARF( context, 0x21 );
1738 SET_CFLAG( context );
1739 break;
1741 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1742 TRACE( "Get general internationalization info\n" );
1743 dataptr[0] = 0x01; /* Info ID */
1744 *(WORD*)(dataptr+1) = 38; /* Size of the following info */
1745 *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
1746 *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
1747 INT21_FillCountryInformation( dataptr + 7 );
1748 SET_CX( context, 41 ); /* Size of returned info */
1749 break;
1751 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1752 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1753 TRACE( "Get pointer to uppercase table\n" );
1754 dataptr[0] = AL_reg(context); /* Info ID */
1755 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1756 offsetof(INT21_HEAP, uppercase_size) );
1757 SET_CX( context, 5 ); /* Size of returned info */
1758 break;
1760 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1761 TRACE( "Get pointer to lowercase table\n" );
1762 dataptr[0] = 0x03; /* Info ID */
1763 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1764 offsetof(INT21_HEAP, lowercase_size) );
1765 SET_CX( context, 5 ); /* Size of returned info */
1766 break;
1768 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1769 TRACE("Get pointer to filename terminator table\n");
1770 dataptr[0] = 0x05; /* Info ID */
1771 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1772 offsetof(INT21_HEAP, filename_size) );
1773 SET_CX( context, 5 ); /* Size of returned info */
1774 break;
1776 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1777 TRACE("Get pointer to collating sequence table\n");
1778 dataptr[0] = 0x06; /* Info ID */
1779 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1780 offsetof(INT21_HEAP, collating_size) );
1781 SET_CX( context, 5 ); /* Size of returned info */
1782 break;
1784 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1785 TRACE("Get pointer to DBCS lead byte table\n");
1786 dataptr[0] = 0x07; /* Info ID */
1787 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1788 offsetof(INT21_HEAP, dbcs_size) );
1789 SET_CX( context, 5 ); /* Size of returned info */
1790 break;
1792 case 0x20: /* CAPITALIZE CHARACTER */
1793 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1794 TRACE("Convert char to uppercase\n");
1795 SET_DL( context, toupper(DL_reg(context)) );
1796 break;
1798 case 0x21: /* CAPITALIZE STRING */
1799 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1800 TRACE("Convert string to uppercase with length\n");
1802 char *ptr = (char *)CTX_SEG_OFF_TO_LIN( context,
1803 context->SegDs,
1804 context->Edx );
1805 WORD len = CX_reg(context);
1806 while (len--) { *ptr = toupper(*ptr); ptr++; }
1808 break;
1810 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1811 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1812 TRACE("Convert ASCIIZ string to uppercase\n");
1813 _strupr( (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx) );
1814 break;
1816 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1817 INT_BARF( context, 0x21 );
1818 SET_CFLAG( context );
1819 break;
1821 default:
1822 INT_BARF( context, 0x21 );
1823 SET_CFLAG(context);
1824 break;
1829 /***********************************************************************
1830 * INT21_FileAttributes
1832 * Handler for:
1833 * - function 0x43
1834 * - subfunction 0x43 of function 0x71
1836 static BOOL INT21_FileAttributes( CONTEXT86 *context,
1837 BYTE subfunction,
1838 BOOL islong )
1840 WCHAR fileW[MAX_PATH];
1841 char *fileA = CTX_SEG_OFF_TO_LIN(context,
1842 context->SegDs,
1843 context->Edx);
1844 HANDLE handle;
1845 BOOL status;
1846 FILETIME filetime;
1847 DWORD result;
1848 WORD date, time;
1850 switch (subfunction)
1852 case 0x00: /* GET FILE ATTRIBUTES */
1853 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA );
1854 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1856 result = GetFileAttributesW( fileW );
1857 if (result == INVALID_FILE_ATTRIBUTES)
1858 return FALSE;
1859 else
1861 SET_CX( context, (WORD)result );
1862 if (!islong)
1863 SET_AX( context, (WORD)result ); /* DR DOS */
1865 break;
1867 case 0x01: /* SET FILE ATTRIBUTES */
1868 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
1869 CX_reg(context), fileA );
1870 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1872 if (!SetFileAttributesW( fileW, CX_reg(context) ))
1873 return FALSE;
1874 break;
1876 case 0x02: /* GET COMPRESSED FILE SIZE */
1877 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA );
1878 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1880 result = GetCompressedFileSizeW( fileW, NULL );
1881 if (result == INVALID_FILE_SIZE)
1882 return FALSE;
1883 else
1885 SET_AX( context, LOWORD(result) );
1886 SET_DX( context, HIWORD(result) );
1888 break;
1890 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
1891 if (!islong)
1892 INT_BARF( context, 0x21 );
1893 else
1895 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
1896 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1898 handle = CreateFileW( fileW, GENERIC_WRITE,
1899 FILE_SHARE_READ | FILE_SHARE_WRITE,
1900 NULL, OPEN_EXISTING, 0, 0 );
1901 if (handle == INVALID_HANDLE_VALUE)
1902 return FALSE;
1904 DosDateTimeToFileTime( DI_reg(context),
1905 CX_reg(context),
1906 &filetime );
1907 status = SetFileTime( handle, NULL, NULL, &filetime );
1909 CloseHandle( handle );
1910 return status;
1912 break;
1914 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
1915 if (!islong)
1916 INT_BARF( context, 0x21 );
1917 else
1919 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
1920 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1922 handle = CreateFileW( fileW, GENERIC_READ,
1923 FILE_SHARE_READ | FILE_SHARE_WRITE,
1924 NULL, OPEN_EXISTING, 0, 0 );
1925 if (handle == INVALID_HANDLE_VALUE)
1926 return FALSE;
1928 status = GetFileTime( handle, NULL, NULL, &filetime );
1929 if (status)
1931 FileTimeToDosDateTime( &filetime, &date, &time );
1932 SET_DI( context, date );
1933 SET_CX( context, time );
1936 CloseHandle( handle );
1937 return status;
1939 break;
1941 case 0x05: /* SET FILE LAST ACCESS DATE */
1942 if (!islong)
1943 INT_BARF( context, 0x21 );
1944 else
1946 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA );
1947 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1949 handle = CreateFileW( fileW, GENERIC_WRITE,
1950 FILE_SHARE_READ | FILE_SHARE_WRITE,
1951 NULL, OPEN_EXISTING, 0, 0 );
1952 if (handle == INVALID_HANDLE_VALUE)
1953 return FALSE;
1955 DosDateTimeToFileTime( DI_reg(context),
1957 &filetime );
1958 status = SetFileTime( handle, NULL, &filetime, NULL );
1960 CloseHandle( handle );
1961 return status;
1963 break;
1965 case 0x06: /* GET FILE LAST ACCESS DATE */
1966 if (!islong)
1967 INT_BARF( context, 0x21 );
1968 else
1970 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA );
1971 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1973 handle = CreateFileW( fileW, GENERIC_READ,
1974 FILE_SHARE_READ | FILE_SHARE_WRITE,
1975 NULL, OPEN_EXISTING, 0, 0 );
1976 if (handle == INVALID_HANDLE_VALUE)
1977 return FALSE;
1979 status = GetFileTime( handle, NULL, &filetime, NULL );
1980 if (status)
1982 FileTimeToDosDateTime( &filetime, &date, NULL );
1983 SET_DI( context, date );
1986 CloseHandle( handle );
1987 return status;
1989 break;
1991 case 0x07: /* SET FILE CREATION DATE AND TIME */
1992 if (!islong)
1993 INT_BARF( context, 0x21 );
1994 else
1996 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA );
1998 handle = CreateFileW( fileW, GENERIC_WRITE,
1999 FILE_SHARE_READ | FILE_SHARE_WRITE,
2000 NULL, OPEN_EXISTING, 0, 0 );
2001 if (handle == INVALID_HANDLE_VALUE)
2002 return FALSE;
2005 * FIXME: SI has number of 10-millisecond units past time in CX.
2007 DosDateTimeToFileTime( DI_reg(context),
2008 CX_reg(context),
2009 &filetime );
2010 status = SetFileTime( handle, &filetime, NULL, NULL );
2012 CloseHandle( handle );
2013 return status;
2015 break;
2017 case 0x08: /* GET FILE CREATION DATE AND TIME */
2018 if (!islong)
2019 INT_BARF( context, 0x21 );
2020 else
2022 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2023 BX_reg(context) );
2025 handle = CreateFileW( fileW, GENERIC_READ,
2026 FILE_SHARE_READ | FILE_SHARE_WRITE,
2027 NULL, OPEN_EXISTING, 0, 0 );
2028 if (handle == INVALID_HANDLE_VALUE)
2029 return FALSE;
2031 status = GetFileTime( handle, &filetime, NULL, NULL );
2032 if (status)
2034 FileTimeToDosDateTime( &filetime, &date, &time );
2035 SET_DI( context, date );
2036 SET_CX( context, time );
2038 * FIXME: SI has number of 10-millisecond units past
2039 * time in CX.
2041 SET_SI( context, 0 );
2044 CloseHandle(handle);
2045 return status;
2047 break;
2049 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2050 if (islong || context->Ebp != 0x5053)
2051 INT_BARF( context, 0x21 );
2052 else
2054 switch(CL_reg(context))
2056 case 0x39:
2057 if (!INT21_CreateDirectory( context ))
2058 return FALSE;
2059 break;
2061 case 0x56:
2062 if (!INT21_RenameFile( context ))
2063 return FALSE;
2064 break;
2066 default:
2067 INT_BARF( context, 0x21 );
2070 break;
2072 default:
2073 INT_BARF( context, 0x21 );
2076 return TRUE;
2080 /***********************************************************************
2081 * INT21_FileDateTime
2083 * Handler for function 0x57.
2085 static BOOL INT21_FileDateTime( CONTEXT86 *context )
2087 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2088 FILETIME filetime;
2089 WORD date, time;
2091 switch (AL_reg(context)) {
2092 case 0x00: /* Get last-written stamp */
2093 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2094 BX_reg(context) );
2096 if (!GetFileTime( handle, NULL, NULL, &filetime ))
2097 return FALSE;
2098 FileTimeToDosDateTime( &filetime, &date, &time );
2099 SET_DX( context, date );
2100 SET_CX( context, time );
2101 break;
2104 case 0x01: /* Set last-written stamp */
2105 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2106 BX_reg(context) );
2108 DosDateTimeToFileTime( DX_reg(context),
2109 CX_reg(context),
2110 &filetime );
2111 if (!SetFileTime( handle, NULL, NULL, &filetime ))
2112 return FALSE;
2113 break;
2116 case 0x04: /* Get last access stamp, DOS 7 */
2117 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2118 BX_reg(context) );
2120 if (!GetFileTime( handle, NULL, &filetime, NULL ))
2121 return FALSE;
2122 FileTimeToDosDateTime( &filetime, &date, &time );
2123 SET_DX( context, date );
2124 SET_CX( context, time );
2125 break;
2128 case 0x05: /* Set last access stamp, DOS 7 */
2129 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2130 BX_reg(context) );
2132 DosDateTimeToFileTime( DX_reg(context),
2133 CX_reg(context),
2134 &filetime );
2135 if (!SetFileTime( handle, NULL, &filetime, NULL ))
2136 return FALSE;
2137 break;
2140 case 0x06: /* Get creation stamp, DOS 7 */
2141 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2142 BX_reg(context) );
2144 if (!GetFileTime( handle, &filetime, NULL, NULL ))
2145 return FALSE;
2146 FileTimeToDosDateTime( &filetime, &date, &time );
2147 SET_DX( context, date );
2148 SET_CX( context, time );
2150 * FIXME: SI has number of 10-millisecond units past time in CX.
2152 SET_SI( context, 0 );
2153 break;
2156 case 0x07: /* Set creation stamp, DOS 7 */
2157 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2158 BX_reg(context) );
2161 * FIXME: SI has number of 10-millisecond units past time in CX.
2163 DosDateTimeToFileTime( DX_reg(context),
2164 CX_reg(context),
2165 &filetime );
2166 if (!SetFileTime( handle, &filetime, NULL, NULL ))
2167 return FALSE;
2168 break;
2171 default:
2172 INT_BARF( context, 0x21 );
2173 break;
2176 return TRUE;
2180 /***********************************************************************
2181 * INT21_GetPSP
2183 * Handler for functions 0x51 and 0x62.
2185 static void INT21_GetPSP( CONTEXT86 *context )
2187 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
2190 * FIXME: should we return the original DOS PSP upon
2191 * Windows startup ?
2193 if (!ISV86(context) && DOSVM_IsWin16())
2194 SET_BX( context, LOWORD(GetCurrentPDB16()) );
2195 else
2196 SET_BX( context, DOSVM_psp );
2199 static inline void setword( BYTE *ptr, WORD w )
2201 ptr[0] = (BYTE)w;
2202 ptr[1] = (BYTE)(w >> 8);
2205 static void CreateBPB(int drive, BYTE *data, BOOL16 limited)
2206 /* limited == TRUE is used with INT 0x21/0x440d */
2208 /* FIXME: we're forcing some values without checking that those are valid */
2209 if (drive > 1)
2211 setword(data, 512);
2212 data[2] = 2;
2213 setword(&data[3], 0);
2214 data[5] = 2;
2215 setword(&data[6], 240);
2216 setword(&data[8], 64000);
2217 data[0x0a] = 0xf8;
2218 setword(&data[0x0b], 40);
2219 setword(&data[0x0d], 56);
2220 setword(&data[0x0f], 2);
2221 setword(&data[0x11], 0);
2222 if (!limited)
2224 setword(&data[0x1f], 800);
2225 data[0x21] = 5;
2226 setword(&data[0x22], 1);
2229 else
2230 { /* 1.44mb */
2231 setword(data, 512);
2232 data[2] = 2;
2233 setword(&data[3], 0);
2234 data[5] = 2;
2235 setword(&data[6], 240);
2236 setword(&data[8], 2880);
2237 data[0x0a] = 0xf8;
2238 setword(&data[0x0b], 6);
2239 setword(&data[0x0d], 18);
2240 setword(&data[0x0f], 2);
2241 setword(&data[0x11], 0);
2242 if (!limited)
2244 setword(&data[0x1f], 80);
2245 data[0x21] = 7;
2246 setword(&data[0x22], 2);
2251 /***********************************************************************
2252 * INT21_Ioctl_Block
2254 * Handler for block device IOCTLs.
2256 static void INT21_Ioctl_Block( CONTEXT86 *context )
2258 BYTE *dataptr;
2259 BYTE drive = INT21_MapDrive( BL_reg(context) );
2260 WCHAR drivespec[4] = {'A', ':', '\\', 0};
2261 UINT drivetype;
2263 drivespec[0] += drive;
2264 drivetype = GetDriveTypeW( drivespec );
2266 RESET_CFLAG(context);
2267 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
2269 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2270 AL_reg(context), 'A' + drive );
2271 SetLastError( ERROR_INVALID_DRIVE );
2272 SET_AX( context, ERROR_INVALID_DRIVE );
2273 SET_CFLAG( context );
2274 return;
2277 switch (AL_reg(context))
2279 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2280 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2281 INT_BARF( context, 0x21 );
2282 break;
2284 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2285 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2286 'A' + drive );
2288 if (drivetype == DRIVE_REMOVABLE)
2289 SET_AX( context, 0 ); /* removable */
2290 else
2291 SET_AX( context, 1 ); /* not removable */
2292 break;
2294 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2295 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2296 'A' + drive );
2298 if (drivetype == DRIVE_REMOTE)
2299 SET_DX( context, (1<<9) | (1<<12) ); /* remote + no direct IO */
2300 else
2301 SET_DX( context, 0 ); /* FIXME: use driver attr here */
2302 break;
2304 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2305 /* Get pointer to IOCTL parameter block. */
2306 dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2308 switch (CX_reg(context))
2310 case 0x0841: /* write logical device track */
2311 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2312 'A' + drive);
2314 WORD head = *(WORD *)dataptr+1;
2315 WORD cyl = *(WORD *)dataptr+3;
2316 WORD sect = *(WORD *)dataptr+5;
2317 WORD nrsect = *(WORD *)dataptr+7;
2318 BYTE *data = (BYTE *)dataptr+9; /* FIXME: is this correct? */
2320 if (!DOSVM_RawWrite(drive, head*cyl*sect, nrsect, data, FALSE))
2322 SET_AX( context, ERROR_WRITE_FAULT );
2323 SET_CFLAG(context);
2326 break;
2328 case 0x084a: /* lock logical volume */
2329 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2330 BH_reg(context), DX_reg(context), 'A' + drive );
2331 break;
2333 case 0x0860: /* get device parameters */
2334 /* FIXME: we're faking some values here */
2335 /* used by w4wgrp's winfile */
2336 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2337 dataptr[0] = 0x04;
2338 dataptr[6] = 0; /* media type */
2339 if (drive > 1)
2341 dataptr[1] = 0x05; /* fixed disk */
2342 setword(&dataptr[2], 0x01); /* non removable */
2343 setword(&dataptr[4], 0x300); /* # of cylinders */
2345 else
2347 dataptr[1] = 0x07; /* block dev, floppy */
2348 setword(&dataptr[2], 0x02); /* removable */
2349 setword(&dataptr[4], 80); /* # of cylinders */
2351 CreateBPB(drive, &dataptr[7], TRUE);
2352 RESET_CFLAG(context);
2353 break;
2355 case 0x0861: /* read logical device track */
2356 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2357 'A' + drive);
2359 WORD head = *(WORD *)dataptr+1;
2360 WORD cyl = *(WORD *)dataptr+3;
2361 WORD sect = *(WORD *)dataptr+5;
2362 WORD nrsect = *(WORD *)dataptr+7;
2363 BYTE *data = (BYTE *)dataptr+9; /* FIXME: is this correct? */
2365 if (!DOSVM_RawRead(drive, head*cyl*sect, nrsect, data, FALSE))
2367 SET_AX( context, ERROR_READ_FAULT );
2368 SET_CFLAG(context);
2371 break;
2373 case 0x0866: /* get volume serial number */
2375 WCHAR label[12],fsname[9];
2376 DWORD serial;
2378 drivespec[0] += drive;
2379 GetVolumeInformationW(drivespec, label, 12, &serial, NULL, NULL, fsname, 9);
2380 *(WORD*)dataptr = 0;
2381 memcpy(dataptr+2,&serial,4);
2382 WideCharToMultiByte(CP_OEMCP, 0, label, 11, dataptr + 6, 11, NULL, NULL);
2383 WideCharToMultiByte(CP_OEMCP, 0, fsname, 8, dataptr + 17, 8, NULL, NULL);
2385 break;
2387 case 0x086a: /* unlock logical volume */
2388 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2389 'A' + drive );
2390 break;
2392 case 0x086f: /* get drive map information */
2393 memset(dataptr+1, '\0', dataptr[0]-1);
2394 dataptr[1] = dataptr[0];
2395 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
2396 dataptr[3] = 0xFF; /* no physical drive */
2397 break;
2399 case 0x0872:
2400 /* Trail on error implementation */
2401 SET_AX( context, drivetype == DRIVE_UNKNOWN ? 0x0f : 0x01 );
2402 SET_CFLAG(context); /* Seems to be set all the time */
2403 break;
2405 default:
2406 INT_BARF( context, 0x21 );
2408 break;
2410 case 0x0e: /* GET LOGICAL DRIVE MAP */
2411 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2412 'A' + drive );
2413 /* FIXME: this is not correct if drive has mappings */
2414 SET_AL( context, 0 ); /* drive has no mapping */
2415 break;
2417 case 0x0f: /* SET LOGICAL DRIVE MAP */
2419 WCHAR dev[3], tgt[4];
2421 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2422 INT21_DriveName( BL_reg(context)));
2423 dev[0] = 'A' + drive; dev[1] = ':'; dev[2] = 0;
2424 tgt[0] = 'A' + drive + 1; dev[1] = ':'; dev[2] = '\\'; dev[3] = 0;
2425 if (!DefineDosDeviceW(DDD_RAW_TARGET_PATH, dev, tgt))
2427 SET_CFLAG(context);
2428 SET_AX( context, 0x000F ); /* invalid drive */
2431 break;
2433 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2434 default:
2435 INT_BARF( context, 0x21 );
2440 /***********************************************************************
2441 * INT21_Ioctl_Char
2443 * Handler for character device IOCTLs.
2445 static void INT21_Ioctl_Char( CONTEXT86 *context )
2447 static const WCHAR emmxxxx0W[] = {'E','M','M','X','X','X','X','0',0};
2448 static const WCHAR scsimgrW[] = {'S','C','S','I','M','G','R','$',0};
2450 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2451 const DOS_DEVICE *dev = DOSFS_GetDeviceByHandle(handle);
2453 if (dev && !strcmpiW( dev->name, emmxxxx0W ))
2455 EMS_Ioctl_Handler(context);
2456 return;
2459 if (dev && !strcmpiW( dev->name, scsimgrW ) && AL_reg(context) == 2)
2461 DOSVM_ASPIHandler(context);
2462 return;
2465 switch (AL_reg(context))
2467 case 0x00: /* GET DEVICE INFORMATION */
2468 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context) );
2469 if (dev)
2472 * Returns attribute word in DX:
2473 * Bit 14 - Device driver can process IOCTL requests.
2474 * Bit 13 - Output until busy supported.
2475 * Bit 11 - Driver supports OPEN/CLOSE calls.
2476 * Bit 8 - Unknown.
2477 * Bit 7 - Set (indicates device).
2478 * Bit 6 - EOF on input.
2479 * Bit 5 - Raw (binary) mode.
2480 * Bit 4 - Device is special (uses int29).
2481 * Bit 3 - Clock device.
2482 * Bit 2 - NUL device.
2483 * Bit 1 - Standard output.
2484 * Bit 0 - Standard input.
2486 SET_DX( context, dev->flags );
2488 else
2491 * Returns attribute word in DX:
2492 * Bit 15 - File is remote.
2493 * Bit 14 - Don't set file date/time on closing.
2494 * Bit 11 - Media not removable.
2495 * Bit 8 - Generate int24 if no disk space on write
2496 * or read past end of file
2497 * Bit 7 - Clear (indicates file).
2498 * Bit 6 - File has not been written.
2499 * Bit 5..0 - Drive number (0=A:,...)
2501 * FIXME: Should check if file is on remote or removable drive.
2502 * FIXME: Should use drive file is located on (and not current).
2504 SET_DX( context, 0x0140 + INT21_GetCurrentDrive() );
2506 break;
2508 case 0x01: /* SET DEVICE INFORMATION */
2509 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2510 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2511 case 0x06: /* GET INPUT STATUS */
2512 case 0x07: /* GET OUTPUT STATUS */
2513 INT_BARF( context, 0x21 );
2514 break;
2516 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2517 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context) );
2519 * Returns attribute word in DX:
2520 * Bit 15 - Set if remote.
2521 * Bit 14 - Set if date/time not set on close.
2523 * FIXME: Should check if file is on remote drive.
2525 SET_DX( context, 0 );
2526 break;
2528 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2529 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2530 default:
2531 INT_BARF( context, 0x21 );
2536 /***********************************************************************
2537 * INT21_Ioctl
2539 * Handler for function 0x44.
2541 static void INT21_Ioctl( CONTEXT86 *context )
2543 switch (AL_reg(context))
2545 case 0x00:
2546 case 0x01:
2547 case 0x02:
2548 case 0x03:
2549 INT21_Ioctl_Char( context );
2550 break;
2552 case 0x04:
2553 case 0x05:
2554 INT21_Ioctl_Block( context );
2555 break;
2557 case 0x06:
2558 case 0x07:
2559 INT21_Ioctl_Char( context );
2560 break;
2562 case 0x08:
2563 case 0x09:
2564 INT21_Ioctl_Block( context );
2565 break;
2567 case 0x0a:
2568 INT21_Ioctl_Char( context );
2569 break;
2571 case 0x0b: /* SET SHARING RETRY COUNT */
2572 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2573 CX_reg(context), DX_reg(context) );
2574 if (!CX_reg(context))
2576 SET_AX( context, 1 );
2577 SET_CFLAG( context );
2579 else
2581 DOSDEV_SetSharingRetry( CX_reg(context), DX_reg(context) );
2582 RESET_CFLAG( context );
2584 break;
2586 case 0x0c:
2587 INT21_Ioctl_Char( context );
2588 break;
2590 case 0x0d:
2591 case 0x0e:
2592 case 0x0f:
2593 INT21_Ioctl_Block( context );
2594 break;
2596 case 0x10:
2597 INT21_Ioctl_Char( context );
2598 break;
2600 case 0x11:
2601 INT21_Ioctl_Block( context );
2602 break;
2604 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2605 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2606 SET_CFLAG(context); /* Error / This is not DR DOS. */
2607 SET_AX( context, 0x0001 ); /* Invalid function */
2608 break;
2610 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2611 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2612 SET_CFLAG(context); /* Error / This is not DR DOS. */
2613 SET_AX( context, 0x0001 ); /* Invalid function */
2614 break;
2616 case 0xe0: /* Sun PC-NFS API */
2617 TRACE( "Sun PC-NFS API\n" );
2618 /* not installed */
2619 break;
2621 default:
2622 INT_BARF( context, 0x21 );
2627 /***********************************************************************
2628 * INT21_Fat32
2630 * Handler for function 0x73.
2632 static BOOL INT21_Fat32( CONTEXT86 *context )
2634 switch (AL_reg(context))
2636 case 0x02: /* FAT32 - GET EXTENDED DPB */
2638 BYTE drive = INT21_MapDrive( DL_reg(context) );
2639 WORD *ptr = CTX_SEG_OFF_TO_LIN(context,
2640 context->SegEs, context->Edi);
2641 INT21_DPB *target = (INT21_DPB*)(ptr + 1);
2642 INT21_DPB *source;
2644 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context) );
2646 if ( CX_reg(context) < sizeof(INT21_DPB) + 2 || *ptr < sizeof(INT21_DPB) )
2648 SetLastError( ERROR_BAD_LENGTH );
2649 return FALSE;
2652 if ( !INT21_FillDrivePB( drive ) )
2654 SetLastError( ERROR_INVALID_DRIVE );
2655 return FALSE;
2658 source = &INT21_GetHeapPointer()->misc_dpb_list[drive];
2660 *ptr = sizeof(INT21_DPB);
2661 memcpy( target, source, sizeof(INT21_DPB));
2663 if (LOWORD(context->Esi) != 0xF1A6)
2665 target->driver_header = 0;
2666 target->next = 0;
2668 else
2670 FIXME( "Caller requested driver and next DPB pointers!\n" );
2673 break;
2675 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2677 WCHAR dirW[MAX_PATH];
2678 char *dirA = CTX_SEG_OFF_TO_LIN( context,
2679 context->SegDs, context->Edx );
2680 BYTE *data = CTX_SEG_OFF_TO_LIN( context,
2681 context->SegEs, context->Edi );
2682 DWORD cluster_sectors;
2683 DWORD sector_bytes;
2684 DWORD free_clusters;
2685 DWORD total_clusters;
2687 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA );
2688 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
2690 if (CX_reg(context) < 44)
2692 SetLastError( ERROR_BAD_LENGTH );
2693 return FALSE;
2696 if (!GetDiskFreeSpaceW( dirW, &cluster_sectors, &sector_bytes,
2697 &free_clusters, &total_clusters ))
2698 return FALSE;
2700 *(WORD*) (data + 0) = 44; /* size of structure */
2701 *(WORD*) (data + 2) = 0; /* version */
2702 *(DWORD*)(data + 4) = cluster_sectors;
2703 *(DWORD*)(data + 8) = sector_bytes;
2704 *(DWORD*)(data + 12) = free_clusters;
2705 *(DWORD*)(data + 16) = total_clusters;
2708 * Below we have free/total sectors and
2709 * free/total allocation units without adjustment
2710 * for compression. We fake both using cluster information.
2712 *(DWORD*)(data + 20) = free_clusters * cluster_sectors;
2713 *(DWORD*)(data + 24) = total_clusters * cluster_sectors;
2714 *(DWORD*)(data + 28) = free_clusters;
2715 *(DWORD*)(data + 32) = total_clusters;
2718 * Between (data + 36) and (data + 43) there
2719 * are eight reserved bytes.
2722 break;
2724 default:
2725 INT_BARF( context, 0x21 );
2728 return TRUE;
2731 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA *dataA,
2732 const WIN32_FIND_DATAW *dataW)
2734 dataA->dwFileAttributes = dataW->dwFileAttributes;
2735 dataA->ftCreationTime = dataW->ftCreationTime;
2736 dataA->ftLastAccessTime = dataW->ftLastAccessTime;
2737 dataA->ftLastWriteTime = dataW->ftLastWriteTime;
2738 dataA->nFileSizeHigh = dataW->nFileSizeHigh;
2739 dataA->nFileSizeLow = dataW->nFileSizeLow;
2740 WideCharToMultiByte( CP_OEMCP, 0, dataW->cFileName, -1,
2741 dataA->cFileName, sizeof(dataA->cFileName), NULL, NULL );
2742 WideCharToMultiByte( CP_OEMCP, 0, dataW->cAlternateFileName, -1,
2743 dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName), NULL, NULL );
2746 /***********************************************************************
2747 * INT21_LongFilename
2749 * Handler for function 0x71.
2751 static void INT21_LongFilename( CONTEXT86 *context )
2753 BOOL bSetDOSExtendedError = FALSE;
2754 WCHAR pathW[MAX_PATH];
2755 char* pathA;
2757 if (HIBYTE(HIWORD(GetVersion16())) < 0x07)
2759 TRACE( "LONG FILENAME - functions supported only under DOS7\n" );
2760 SET_CFLAG( context );
2761 SET_AL( context, 0 );
2762 return;
2765 switch (AL_reg(context))
2767 case 0x0d: /* RESET DRIVE */
2768 INT_BARF( context, 0x21 );
2769 break;
2771 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
2772 if (!INT21_CreateDirectory( context ))
2773 bSetDOSExtendedError = TRUE;
2774 break;
2776 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
2777 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2779 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA);
2780 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2781 if (!RemoveDirectoryW( pathW )) bSetDOSExtendedError = TRUE;
2782 break;
2784 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
2785 if (!INT21_SetCurrentDirectory( context ))
2786 bSetDOSExtendedError = TRUE;
2787 break;
2789 case 0x41: /* LONG FILENAME - DELETE FILE */
2790 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2792 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA );
2793 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2795 if (!DeleteFileW( pathW )) bSetDOSExtendedError = TRUE;
2796 break;
2798 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
2799 if (!INT21_FileAttributes( context, BL_reg(context), TRUE ))
2800 bSetDOSExtendedError = TRUE;
2801 break;
2803 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
2804 if (!INT21_GetCurrentDirectory( context, TRUE ))
2805 bSetDOSExtendedError = TRUE;
2806 break;
2808 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
2810 HANDLE handle;
2811 HGLOBAL16 h16;
2812 WIN32_FIND_DATAW dataW;
2813 WIN32_FIND_DATAA* dataA;
2815 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
2816 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA);
2818 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2819 handle = FindFirstFileW(pathW, &dataW);
2821 dataA = (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
2822 context->Edi);
2823 if (handle != INVALID_HANDLE_VALUE &&
2824 (h16 = GlobalAlloc16(GMEM_MOVEABLE, sizeof(handle))))
2826 HANDLE* ptr = GlobalLock16( h16 );
2827 *ptr = handle;
2828 GlobalUnlock16( h16 );
2829 SET_AX( context, h16 );
2830 INT21_ConvertFindDataWtoA(dataA, &dataW);
2832 else
2834 if (handle != INVALID_HANDLE_VALUE) FindClose(handle);
2835 SET_AX( context, INVALID_HANDLE_VALUE16);
2836 bSetDOSExtendedError = TRUE;
2839 break;
2841 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
2843 HGLOBAL16 h16 = BX_reg(context);
2844 HANDLE* ptr;
2845 WIN32_FIND_DATAW dataW;
2846 WIN32_FIND_DATAA* dataA;
2848 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
2849 BX_reg(context));
2851 dataA = (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
2852 context->Edi);
2854 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
2856 if (!FindNextFileW(*ptr, &dataW)) bSetDOSExtendedError = TRUE;
2857 else INT21_ConvertFindDataWtoA(dataA, &dataW);
2858 GlobalUnlock16( h16 );
2860 else
2862 SetLastError( ERROR_INVALID_HANDLE );
2863 bSetDOSExtendedError = TRUE;
2866 break;
2868 case 0x56: /* LONG FILENAME - RENAME FILE */
2869 if (!INT21_RenameFile(context))
2870 bSetDOSExtendedError = TRUE;
2871 break;
2873 case 0x60: /* LONG FILENAME - CONVERT PATH */
2875 WCHAR res[MAX_PATH];
2877 switch (CL_reg(context))
2879 case 0x01: /* Get short filename or path */
2880 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
2881 if (!GetShortPathNameW(pathW, res, 67))
2882 bSetDOSExtendedError = TRUE;
2883 else
2885 SET_AX( context, 0 );
2886 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
2887 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
2888 67, NULL, NULL);
2890 break;
2892 case 0x02: /* Get canonical long filename or path */
2893 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
2894 if (!GetFullPathNameW(pathW, 128, res, NULL))
2895 bSetDOSExtendedError = TRUE;
2896 else
2898 SET_AX( context, 0 );
2899 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
2900 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
2901 128, NULL, NULL);
2903 break;
2904 default:
2905 FIXME("Unimplemented long file name function:\n");
2906 INT_BARF( context, 0x21 );
2907 SET_CFLAG(context);
2908 SET_AL( context, 0 );
2909 break;
2912 break;
2914 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
2915 if (!INT21_CreateFile( context, context->Esi, TRUE,
2916 BX_reg(context), DL_reg(context) ))
2917 bSetDOSExtendedError = TRUE;
2918 break;
2920 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
2922 DWORD filename_len, flags;
2923 WCHAR dstW[8];
2925 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
2927 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA);
2928 SET_AX( context, 0 );
2929 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2930 if (!GetVolumeInformationW( pathW, NULL, 0, NULL, &filename_len,
2931 &flags, dstW, 8 ))
2933 INT_BARF( context, 0x21 );
2934 SET_CFLAG(context);
2935 break;
2937 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
2938 SET_CX( context, filename_len );
2939 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
2940 WideCharToMultiByte(CP_OEMCP, 0, dstW, -1,
2941 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
2942 8, NULL, NULL);
2944 break;
2946 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
2948 HGLOBAL16 h16 = BX_reg(context);
2949 HANDLE* ptr;
2951 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
2952 BX_reg(context));
2953 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
2955 if (!FindClose( *ptr )) bSetDOSExtendedError = TRUE;
2956 GlobalUnlock16( h16 );
2957 GlobalFree16( h16 );
2959 else
2961 SetLastError( ERROR_INVALID_HANDLE );
2962 bSetDOSExtendedError = TRUE;
2965 break;
2967 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
2969 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2970 BY_HANDLE_FILE_INFORMATION *info =
2971 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2973 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
2975 if (!GetFileInformationByHandle(handle, info))
2976 bSetDOSExtendedError = TRUE;
2978 break;
2980 case 0xa7: /* LONG FILENAME - CONVERT TIME */
2981 switch (BL_reg(context))
2983 case 0x00: /* FILE TIME TO DOS TIME */
2985 WORD date, time;
2986 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
2987 context->SegDs,
2988 context->Esi);
2990 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
2992 FileTimeToDosDateTime( filetime, &date, &time );
2994 SET_DX( context, date );
2995 SET_CX( context, time );
2998 * FIXME: BH has number of 10-millisecond units
2999 * past time in CX.
3001 SET_BH( context, 0 );
3003 break;
3005 case 0x01: /* DOS TIME TO FILE TIME */
3007 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3008 context->SegEs,
3009 context->Edi);
3011 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
3014 * FIXME: BH has number of 10-millisecond units
3015 * past time in CX.
3017 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
3018 filetime );
3020 break;
3022 default:
3023 INT_BARF( context, 0x21 );
3024 break;
3026 break;
3028 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
3029 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3030 case 0xaa: /* LONG FILENAME - SUBST */
3031 default:
3032 FIXME("Unimplemented long file name function:\n");
3033 INT_BARF( context, 0x21 );
3034 SET_CFLAG(context);
3035 SET_AL( context, 0 );
3036 break;
3039 if (bSetDOSExtendedError)
3041 SET_AX( context, GetLastError() );
3042 SET_CFLAG( context );
3047 /***********************************************************************
3048 * INT21_RenameFile
3050 * Handler for:
3051 * - function 0x56
3052 * - subfunction 0x56 of function 0x71
3053 * - subfunction 0xff of function 0x43 (CL == 0x56)
3055 static BOOL INT21_RenameFile( CONTEXT86 *context )
3057 WCHAR fromW[MAX_PATH];
3058 WCHAR toW[MAX_PATH];
3059 char *fromA = CTX_SEG_OFF_TO_LIN(context,
3060 context->SegDs,context->Edx);
3061 char *toA = CTX_SEG_OFF_TO_LIN(context,
3062 context->SegEs,context->Edi);
3064 TRACE( "RENAME FILE %s to %s\n", fromA, toA );
3065 MultiByteToWideChar(CP_OEMCP, 0, fromA, -1, fromW, MAX_PATH);
3066 MultiByteToWideChar(CP_OEMCP, 0, toA, -1, toW, MAX_PATH);
3068 return MoveFileW( fromW, toW );
3072 /***********************************************************************
3073 * INT21_NetworkFunc
3075 * Handler for:
3076 * - function 0x5e
3078 static BOOL INT21_NetworkFunc (CONTEXT86 *context)
3080 switch (AL_reg(context))
3082 case 0x00: /* Get machine name. */
3084 WCHAR dstW[MAX_COMPUTERNAME_LENGTH + 1];
3085 DWORD s = sizeof(dstW) / sizeof(WCHAR);
3086 int len;
3088 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
3089 TRACE("getting machine name to %p\n", dst);
3090 if (!GetComputerNameW(dstW, &s) ||
3091 !WideCharToMultiByte(CP_OEMCP, 0, dstW, -1, dst, 16, NULL, NULL))
3093 WARN("failed!\n");
3094 SetLastError( ER_NoNetwork );
3095 return TRUE;
3097 for (len = strlen(dst); len < 15; len++) dst[len] = ' ';
3098 dst[15] = 0;
3099 SET_CH( context, 1 ); /* Valid */
3100 SET_CL( context, 1 ); /* NETbios number??? */
3101 TRACE("returning %s\n", debugstr_an(dst, 16));
3102 return FALSE;
3105 default:
3106 SetLastError( ER_NoNetwork );
3107 return TRUE;
3111 /******************************************************************
3112 * INT21_GetDiskSerialNumber
3115 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
3117 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3118 WCHAR path[] = {'A',':',0}, label[11];
3119 DWORD serial;
3121 path[0] += INT21_MapDrive(BL_reg(context));
3122 if (!GetVolumeInformationW( path, label, 11, &serial, NULL, NULL, NULL, 0))
3124 SetLastError( ERROR_INVALID_DRIVE );
3125 return 0;
3128 *(WORD *)dataptr = 0;
3129 memcpy(dataptr + 2, &serial, sizeof(DWORD));
3130 WideCharToMultiByte(CP_OEMCP, 0, label, 11, dataptr + 6, 11, NULL, NULL);
3131 strncpy(dataptr + 17, "FAT16 ", 8);
3132 return 1;
3136 /******************************************************************
3137 * INT21_SetDiskSerialNumber
3140 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
3142 #if 0
3143 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3144 int drive = INT21_MapDrive(BL_reg(context));
3146 if (!is_valid_drive(drive))
3148 SetLastError( ERROR_INVALID_DRIVE );
3149 return 0;
3152 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
3153 return 1;
3154 #else
3155 FIXME("Setting drive serial number is no longer supported\n");
3156 SetLastError( ERROR_NOT_SUPPORTED );
3157 return 0;
3158 #endif
3162 /******************************************************************
3163 * INT21_GetFreeDiskSpace
3166 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
3168 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
3169 WCHAR root[] = {'A',':','\\',0};
3171 root[0] += INT21_MapDrive(DL_reg(context));
3172 if (!GetDiskFreeSpaceW( root, &cluster_sectors, &sector_bytes,
3173 &free_clusters, &total_clusters )) return 0;
3174 SET_AX( context, cluster_sectors );
3175 SET_BX( context, free_clusters );
3176 SET_CX( context, sector_bytes );
3177 SET_DX( context, total_clusters );
3178 return 1;
3181 /******************************************************************
3182 * INT21_GetDriveAllocInfo
3185 static int INT21_GetDriveAllocInfo( CONTEXT86 *context, int drive )
3187 INT21_DPB *dpb;
3189 if (!INT21_FillDrivePB( drive )) return 0;
3190 dpb = &(INT21_GetHeapPointer()->misc_dpb_list[drive]);
3191 SET_AL( context, dpb->cluster_sectors + 1 );
3192 SET_CX( context, dpb->sector_bytes );
3193 SET_DX( context, dpb->num_clusters1 );
3195 context->SegDs = INT21_GetHeapSelector( context );
3196 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive].media_ID ) );
3197 return 1;
3200 /***********************************************************************
3201 * INT21_GetExtendedError
3203 static void INT21_GetExtendedError( CONTEXT86 *context )
3205 BYTE class, action, locus;
3206 WORD error = GetLastError();
3208 switch(error)
3210 case ERROR_SUCCESS:
3211 class = action = locus = 0;
3212 break;
3213 case ERROR_DIR_NOT_EMPTY:
3214 class = EC_Exists;
3215 action = SA_Ignore;
3216 locus = EL_Disk;
3217 break;
3218 case ERROR_ACCESS_DENIED:
3219 class = EC_AccessDenied;
3220 action = SA_Abort;
3221 locus = EL_Disk;
3222 break;
3223 case ERROR_CANNOT_MAKE:
3224 class = EC_AccessDenied;
3225 action = SA_Abort;
3226 locus = EL_Unknown;
3227 break;
3228 case ERROR_DISK_FULL:
3229 case ERROR_HANDLE_DISK_FULL:
3230 class = EC_MediaError;
3231 action = SA_Abort;
3232 locus = EL_Disk;
3233 break;
3234 case ERROR_FILE_EXISTS:
3235 case ERROR_ALREADY_EXISTS:
3236 class = EC_Exists;
3237 action = SA_Abort;
3238 locus = EL_Disk;
3239 break;
3240 case ERROR_FILE_NOT_FOUND:
3241 class = EC_NotFound;
3242 action = SA_Abort;
3243 locus = EL_Disk;
3244 break;
3245 case ERROR_GEN_FAILURE:
3246 class = EC_SystemFailure;
3247 action = SA_Abort;
3248 locus = EL_Unknown;
3249 break;
3250 case ERROR_INVALID_DRIVE:
3251 class = EC_MediaError;
3252 action = SA_Abort;
3253 locus = EL_Disk;
3254 break;
3255 case ERROR_INVALID_HANDLE:
3256 class = EC_ProgramError;
3257 action = SA_Abort;
3258 locus = EL_Disk;
3259 break;
3260 case ERROR_LOCK_VIOLATION:
3261 class = EC_AccessDenied;
3262 action = SA_Abort;
3263 locus = EL_Disk;
3264 break;
3265 case ERROR_NO_MORE_FILES:
3266 class = EC_MediaError;
3267 action = SA_Abort;
3268 locus = EL_Disk;
3269 break;
3270 case ER_NoNetwork:
3271 class = EC_NotFound;
3272 action = SA_Abort;
3273 locus = EL_Network;
3274 break;
3275 case ERROR_NOT_ENOUGH_MEMORY:
3276 class = EC_OutOfResource;
3277 action = SA_Abort;
3278 locus = EL_Memory;
3279 break;
3280 case ERROR_PATH_NOT_FOUND:
3281 class = EC_NotFound;
3282 action = SA_Abort;
3283 locus = EL_Disk;
3284 break;
3285 case ERROR_SEEK:
3286 class = EC_NotFound;
3287 action = SA_Ignore;
3288 locus = EL_Disk;
3289 break;
3290 case ERROR_SHARING_VIOLATION:
3291 class = EC_Temporary;
3292 action = SA_Retry;
3293 locus = EL_Disk;
3294 break;
3295 case ERROR_TOO_MANY_OPEN_FILES:
3296 class = EC_ProgramError;
3297 action = SA_Abort;
3298 locus = EL_Disk;
3299 break;
3300 default:
3301 FIXME("Unknown error %d\n", error );
3302 class = EC_SystemFailure;
3303 action = SA_Abort;
3304 locus = EL_Unknown;
3305 break;
3307 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3308 error, class, action, locus );
3309 SET_AX( context, error );
3310 SET_BH( context, class );
3311 SET_BL( context, action );
3312 SET_CH( context, locus );
3315 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
3317 static int counter = 0;
3318 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
3319 char *p = name + strlen(name);
3321 /* despite what Ralf Brown says, some programs seem to call without
3322 * ending backslash (DOS accepts that, so we accept it too) */
3323 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
3325 for (;;)
3327 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
3328 counter = (counter + 1) % 1000;
3330 SET_AX( context,
3331 Win32HandleToDosFileHandle(
3332 CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
3333 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
3334 CREATE_NEW, 0, 0 ) ) );
3335 if (AX_reg(context) != HFILE_ERROR16)
3337 TRACE("created %s\n", name );
3338 return TRUE;
3340 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
3344 /***********************************************************************
3345 * DOSFS_ToDosFCBFormat
3347 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3348 * expanding wild cards and converting to upper-case in the process.
3349 * File name can be terminated by '\0', '\\' or '/'.
3350 * Return FALSE if the name is not a valid DOS name.
3351 * 'buffer' must be at least 12 characters long.
3353 /* Chars we don't want to see in DOS file names */
3354 #define INVALID_DOS_CHARS "*?<>|\"+=,;[] \345"
3355 static BOOL INT21_ToDosFCBFormat( LPCWSTR name, LPWSTR buffer )
3357 static const char invalid_chars[] = INVALID_DOS_CHARS;
3358 LPCWSTR p = name;
3359 int i;
3361 /* Check for "." and ".." */
3362 if (*p == '.')
3364 p++;
3365 buffer[0] = '.';
3366 for(i = 1; i < 11; i++) buffer[i] = ' ';
3367 buffer[11] = 0;
3368 if (*p == '.')
3370 buffer[1] = '.';
3371 p++;
3373 return (!*p || (*p == '/') || (*p == '\\'));
3376 for (i = 0; i < 8; i++)
3378 switch(*p)
3380 case '\0':
3381 case '\\':
3382 case '/':
3383 case '.':
3384 buffer[i] = ' ';
3385 break;
3386 case '?':
3387 p++;
3388 /* fall through */
3389 case '*':
3390 buffer[i] = '?';
3391 break;
3392 default:
3393 if (*p < 256 && strchr( invalid_chars, (char)*p )) return FALSE;
3394 buffer[i] = toupperW(*p);
3395 p++;
3396 break;
3400 if (*p == '*')
3402 /* Skip all chars after wildcard up to first dot */
3403 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
3405 else
3407 /* Check if name too long */
3408 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return FALSE;
3410 if (*p == '.') p++; /* Skip dot */
3412 for (i = 8; i < 11; i++)
3414 switch(*p)
3416 case '\0':
3417 case '\\':
3418 case '/':
3419 buffer[i] = ' ';
3420 break;
3421 case '.':
3422 return FALSE; /* Second extension not allowed */
3423 case '?':
3424 p++;
3425 /* fall through */
3426 case '*':
3427 buffer[i] = '?';
3428 break;
3429 default:
3430 if (*p < 256 && strchr( invalid_chars, (char)*p )) return FALSE;
3431 buffer[i] = toupperW(*p);
3432 p++;
3433 break;
3436 buffer[11] = '\0';
3438 /* at most 3 character of the extension are processed
3439 * is something behind this ?
3441 while (*p == '*' || *p == ' ') p++; /* skip wildcards and spaces */
3442 return IS_END_OF_NAME(*p);
3445 static HANDLE INT21_FindHandle;
3446 static const WCHAR *INT21_FindPath; /* will point to current dta->fullPath search */
3448 /******************************************************************
3449 * INT21_FindFirst
3451 static int INT21_FindFirst( CONTEXT86 *context )
3453 WCHAR *p;
3454 const char *path;
3455 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3456 WCHAR maskW[12], pathW[MAX_PATH];
3457 static const WCHAR wildcardW[] = {'*','.','*',0};
3459 path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3460 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
3462 p = strrchrW( pathW, '\\');
3463 if (!p)
3465 if (pathW[0] && pathW[1] == ':') p = pathW + 2;
3466 else p = pathW;
3468 else p++;
3470 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3471 * (doesn't matter as it is set below anyway)
3473 if (!INT21_ToDosFCBFormat( p, maskW ))
3475 SetLastError( ERROR_FILE_NOT_FOUND );
3476 SET_AX( context, ERROR_FILE_NOT_FOUND );
3477 SET_CFLAG(context);
3478 return 0;
3480 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
3482 dta->fullPath = HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW) + (p - pathW)*sizeof(WCHAR) );
3483 memcpy( dta->fullPath, pathW, (p - pathW) * sizeof(WCHAR) );
3484 memcpy( dta->fullPath + (p - pathW), wildcardW, sizeof(wildcardW) );
3485 /* we must have a fully qualified file name in dta->fullPath
3486 * (we could have a UNC path, but this would lead to some errors later on)
3488 dta->drive = toupperW(dta->fullPath[0]) - 'A';
3489 dta->count = 0;
3490 dta->search_attr = CL_reg(context);
3491 return 1;
3494 /******************************************************************
3495 * match_short
3497 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3499 static BOOL match_short(LPCWSTR shortW, LPCSTR maskA)
3501 WCHAR mask[11], file[12];
3502 int i;
3504 if (!INT21_ToDosFCBFormat( shortW, file )) return FALSE;
3505 MultiByteToWideChar(CP_OEMCP, 0, maskA, 11, mask, 11);
3506 for (i = 0; i < 11; i++)
3507 if (mask[i] != '?' && mask[i] != file[i]) return FALSE;
3508 return TRUE;
3511 static unsigned INT21_FindHelper(LPCWSTR fullPath, unsigned drive, unsigned count,
3512 LPCSTR mask, unsigned search_attr,
3513 WIN32_FIND_DATAW* entry)
3515 unsigned ncalls;
3517 if ((search_attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
3519 WCHAR path[] = {' ',':',0};
3521 if (count) return 0;
3522 path[0] = drive + 'A';
3523 if (!GetVolumeInformationW(path, entry->cAlternateFileName, 13, NULL, NULL, NULL, NULL, 0)) return 0;
3524 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftCreationTime );
3525 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftLastAccessTime );
3526 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftLastWriteTime );
3527 entry->dwFileAttributes = FILE_ATTRIBUTE_LABEL;
3528 entry->nFileSizeHigh = entry->nFileSizeLow = 0;
3529 TRACE("returning %s as label\n", debugstr_w(entry->cAlternateFileName));
3530 return 1;
3533 if (!INT21_FindHandle || INT21_FindPath != fullPath || count == 0)
3535 if (INT21_FindHandle) FindClose(INT21_FindHandle);
3536 INT21_FindHandle = FindFirstFileW(fullPath, entry);
3537 if (INT21_FindHandle == INVALID_HANDLE_VALUE)
3539 INT21_FindHandle = 0;
3540 return 0;
3542 INT21_FindPath = fullPath;
3543 /* we need to resync search */
3544 ncalls = count;
3546 else ncalls = 1;
3548 while (ncalls-- != 0)
3550 if (!FindNextFileW(INT21_FindHandle, entry))
3552 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3553 return 0;
3556 while (count < 0xffff)
3558 count++;
3559 /* Check the file attributes, and path */
3560 if (!(entry->dwFileAttributes & ~search_attr) &&
3561 match_short(entry->cAlternateFileName, mask))
3563 return count;
3565 if (!FindNextFileW(INT21_FindHandle, entry))
3567 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3568 return 0;
3571 WARN("Too many directory entries in %s\n", debugstr_w(fullPath) );
3572 return 0;
3575 /******************************************************************
3576 * INT21_FindNext
3578 static int INT21_FindNext( CONTEXT86 *context )
3580 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3581 DWORD attr = dta->search_attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
3582 WIN32_FIND_DATAW entry;
3583 int n;
3585 if (!dta->fullPath) return 0;
3587 n = INT21_FindHelper(dta->fullPath, dta->drive, dta->count,
3588 dta->mask, attr, &entry);
3589 if (n)
3591 dta->fileattr = entry.dwFileAttributes;
3592 dta->filesize = entry.nFileSizeLow;
3593 FileTimeToDosDateTime( &entry.ftLastWriteTime, &dta->filedate, &dta->filetime );
3594 WideCharToMultiByte(CP_OEMCP, 0, entry.cAlternateFileName, -1,
3595 dta->filename, 13, NULL, NULL);
3596 if (!memchr(dta->mask,'?',11))
3598 /* wildcardless search, release resources in case no findnext will
3599 * be issued, and as a workaround in case file creation messes up
3600 * findnext, as sometimes happens with pkunzip
3602 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3603 INT21_FindPath = dta->fullPath = NULL;
3605 dta->count = n;
3606 return 1;
3608 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3609 INT21_FindPath = dta->fullPath = NULL;
3610 return 0;
3613 /* microsoft's programmers should be shot for using CP/M style int21
3614 calls in Windows for Workgroup's winfile.exe */
3616 /******************************************************************
3617 * INT21_FindFirstFCB
3620 static int INT21_FindFirstFCB( CONTEXT86 *context )
3622 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3623 FINDFILE_FCB *pFCB;
3624 int drive;
3625 WCHAR p[] = {' ',':',};
3627 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
3628 else pFCB = (FINDFILE_FCB *)fcb;
3629 drive = INT21_MapDrive( pFCB->drive );
3630 if (drive == MAX_DOS_DRIVES) return 0;
3632 p[0] = 'A' + drive;
3633 pFCB->fullPath = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
3634 if (!pFCB->fullPath) return 0;
3635 GetLongPathNameW(p, pFCB->fullPath, MAX_PATH);
3636 pFCB->count = 0;
3637 return 1;
3640 /******************************************************************
3641 * INT21_FindNextFCB
3644 static int INT21_FindNextFCB( CONTEXT86 *context )
3646 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3647 FINDFILE_FCB *pFCB;
3648 DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)INT21_GetCurrentDTA(context);
3649 WIN32_FIND_DATAW entry;
3650 BYTE attr;
3651 int n;
3652 WCHAR nameW[12];
3654 if (*fcb == 0xff) /* extended FCB ? */
3656 attr = fcb[6];
3657 pFCB = (FINDFILE_FCB *)(fcb + 7);
3659 else
3661 attr = 0;
3662 pFCB = (FINDFILE_FCB *)fcb;
3665 if (!pFCB->fullPath) return 0;
3666 n = INT21_FindHelper(pFCB->fullPath, INT21_MapDrive( pFCB->drive ),
3667 pFCB->count, pFCB->filename, attr, &entry);
3668 if (!n)
3670 HeapFree( GetProcessHeap(), 0, pFCB->fullPath );
3671 INT21_FindPath = pFCB->fullPath = NULL;
3672 return 0;
3674 pFCB->count += n;
3676 if (*fcb == 0xff)
3678 /* place extended FCB header before pResult if called with extended FCB */
3679 *(BYTE *)pResult = 0xff;
3680 (BYTE *)pResult +=6; /* leave reserved field behind */
3681 *(BYTE *)pResult = entry.dwFileAttributes;
3682 ((BYTE *)pResult)++;
3684 *(BYTE *)pResult = INT21_MapDrive( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
3685 ((BYTE *)pResult)++;
3686 pResult->fileattr = entry.dwFileAttributes;
3687 pResult->cluster = 0; /* what else? */
3688 pResult->filesize = entry.nFileSizeLow;
3689 memset( pResult->reserved, 0, sizeof(pResult->reserved) );
3690 FileTimeToDosDateTime( &entry.ftLastWriteTime,
3691 &pResult->filedate, &pResult->filetime );
3693 /* Convert file name to FCB format */
3694 INT21_ToDosFCBFormat( entry.cAlternateFileName, nameW );
3695 WideCharToMultiByte(CP_OEMCP, 0, nameW, 11, pResult->filename, 11, NULL, NULL);
3696 return 1;
3700 /******************************************************************
3701 * INT21_ParseFileNameIntoFCB
3704 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
3706 char *filename =
3707 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
3708 char *fcb =
3709 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
3710 char *s;
3711 WCHAR *buffer;
3712 WCHAR fcbW[12];
3713 INT buffer_len, len;
3715 SET_AL( context, 0xff ); /* failed */
3717 TRACE("filename: '%s'\n", filename);
3719 s = filename;
3720 while (*s && (*s != ' ') && (*s != '\r') && (*s != '\n'))
3721 s++;
3722 len = filename - s;
3724 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
3725 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
3726 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
3727 buffer[len] = 0;
3728 INT21_ToDosFCBFormat(buffer, fcbW);
3729 HeapFree(GetProcessHeap(), 0, buffer);
3730 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
3731 *fcb = 0;
3732 TRACE("FCB: '%s'\n", fcb + 1);
3734 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
3736 /* point DS:SI to first unparsed character */
3737 SET_SI( context, context->Esi + (int)s - (int)filename );
3740 /***********************************************************************
3741 * DOSVM_Int21Handler
3743 * Interrupt 0x21 handler.
3745 void WINAPI DOSVM_Int21Handler( CONTEXT86 *context )
3747 BOOL bSetDOSExtendedError = FALSE;
3749 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
3750 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
3751 AX_reg(context), BX_reg(context),
3752 CX_reg(context), DX_reg(context),
3753 SI_reg(context), DI_reg(context),
3754 (WORD)context->SegDs, (WORD)context->SegEs,
3755 context->EFlags );
3758 * Extended error is used by (at least) functions 0x2f to 0x62.
3759 * Function 0x59 returns extended error and error should not
3760 * be cleared before handling the function.
3762 if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59)
3763 SetLastError(0);
3765 RESET_CFLAG(context); /* Not sure if this is a good idea. */
3767 switch(AH_reg(context))
3769 case 0x00: /* TERMINATE PROGRAM */
3770 TRACE("TERMINATE PROGRAM\n");
3771 if (DOSVM_IsWin16())
3772 ExitThread( 0 );
3773 else if(ISV86(context))
3774 MZ_Exit( context, FALSE, 0 );
3775 else
3776 ERR( "Called from DOS protected mode\n" );
3777 break;
3779 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
3781 BYTE ascii;
3782 TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
3783 INT21_ReadChar( &ascii, context );
3784 SET_AL( context, ascii );
3786 * FIXME: What to echo when extended keycodes are read?
3788 DOSVM_PutChar(AL_reg(context));
3790 break;
3792 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
3793 TRACE("Write Character to Standard Output\n");
3794 DOSVM_PutChar(DL_reg(context));
3795 break;
3797 case 0x03: /* READ CHARACTER FROM STDAUX */
3798 case 0x04: /* WRITE CHARACTER TO STDAUX */
3799 case 0x05: /* WRITE CHARACTER TO PRINTER */
3800 INT_BARF( context, 0x21 );
3801 break;
3803 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
3804 if (DL_reg(context) == 0xff)
3806 TRACE("Direct Console Input\n");
3808 if (INT21_ReadChar( NULL, NULL ))
3810 BYTE ascii;
3811 INT21_ReadChar( &ascii, context );
3812 SET_AL( context, ascii );
3813 RESET_ZFLAG( context );
3815 else
3817 /* no character available */
3818 SET_AL( context, 0 );
3819 SET_ZFLAG( context );
3822 else
3824 TRACE("Direct Console Output\n");
3825 DOSVM_PutChar(DL_reg(context));
3827 * At least DOS versions 2.1-7.0 return character
3828 * that was written in AL register.
3830 SET_AL( context, DL_reg(context) );
3832 break;
3834 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
3836 BYTE ascii;
3837 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
3838 INT21_ReadChar( &ascii, context );
3839 SET_AL( context, ascii );
3841 break;
3843 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
3845 BYTE ascii;
3846 TRACE("CHARACTER INPUT WITHOUT ECHO\n");
3847 INT21_ReadChar( &ascii, context );
3848 SET_AL( context, ascii );
3850 break;
3852 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
3853 TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
3854 context->SegDs, DX_reg(context) );
3856 LPSTR data = CTX_SEG_OFF_TO_LIN( context,
3857 context->SegDs, context->Edx );
3858 LPSTR p = data;
3861 * Do NOT use strchr() to calculate the string length,
3862 * as '\0' is valid string content, too!
3863 * Maybe we should check for non-'$' strings, but DOS doesn't.
3865 while (*p != '$') p++;
3867 if (DOSVM_IsWin16())
3868 WriteFile( DosFileHandleToWin32Handle(1),
3869 data, p - data, 0, 0 );
3870 else
3871 for(; data != p; data++)
3872 DOSVM_PutChar( *data );
3874 SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
3876 break;
3878 case 0x0a: /* BUFFERED INPUT */
3880 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context,
3881 context->SegDs,
3882 context->Edx);
3883 WORD result;
3885 TRACE( "BUFFERED INPUT (size=%d)\n", ptr[0] );
3888 * FIXME: Some documents state that
3889 * ptr[1] holds number of chars from last input which
3890 * may be recalled on entry, other documents do not mention
3891 * this at all.
3893 if (ptr[1])
3894 TRACE( "Handle old chars in buffer!\n" );
3897 * ptr[0] - capacity (includes terminating CR)
3898 * ptr[1] - characters read (excludes terminating CR)
3900 result = INT21_BufferedInput( context, ptr + 2, ptr[0] );
3901 if (result > 0)
3902 ptr[1] = (BYTE)result - 1;
3903 else
3904 ptr[1] = 0;
3906 break;
3908 case 0x0b: /* GET STDIN STATUS */
3909 TRACE( "GET STDIN STATUS\n" );
3911 if (INT21_ReadChar( NULL, NULL ))
3912 SET_AL( context, 0xff ); /* character available */
3913 else
3914 SET_AL( context, 0 ); /* no character available */
3916 break;
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 );
3936 break;
3938 case 0x0d: /* DISK BUFFER FLUSH */
3939 TRACE("DISK BUFFER FLUSH ignored\n");
3940 break;
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 );
3946 break;
3948 case 0x0f: /* OPEN FILE USING FCB */
3949 INT21_OpenFileUsingFCB( context );
3950 break;
3952 case 0x10: /* CLOSE FILE USING FCB */
3953 INT21_CloseFileUsingFCB( context );
3954 break;
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 );
3962 break;
3964 /* else fall through */
3966 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
3967 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
3968 break;
3970 case 0x13: /* DELETE FILE USING FCB */
3971 INT_BARF( context, 0x21 );
3972 break;
3974 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
3975 INT21_SequentialReadFromFCB( context );
3976 break;
3978 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
3979 INT21_SequentialWriteToFCB( context );
3980 break;
3982 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
3983 case 0x17: /* RENAME FILE USING FCB */
3984 INT_BARF( context, 0x21 );
3985 break;
3987 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
3988 SET_AL( context, 0 );
3989 break;
3991 case 0x19: /* GET CURRENT DEFAULT DRIVE */
3992 SET_AL( context, INT21_GetCurrentDrive() );
3993 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context ) );
3994 break;
3996 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
3997 TRACE( "SET DISK TRANSFER AREA ADDRESS %04lX:%04X\n",
3998 context->SegDs, DX_reg(context) );
4000 TDB *task = GlobalLock16( GetCurrentTask() );
4001 task->dta = MAKESEGPTR( context->SegDs, DX_reg(context) );
4003 break;
4005 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4006 if (!INT21_GetDriveAllocInfo(context, 0))
4007 SET_AX( context, 0xffff );
4008 break;
4010 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4011 if (!INT21_GetDriveAllocInfo(context, DL_reg(context)))
4012 SET_AX( context, 0xffff );
4013 break;
4015 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4016 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4017 SET_AL( context, 0 );
4018 break;
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 );
4031 else
4033 SET_AL( context, 0xff ); /* invalid or network drive */
4036 break;
4038 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4039 SET_AL( context, 0 );
4040 break;
4042 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4043 INT21_ReadRandomRecordFromFCB( context );
4044 break;
4046 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4047 INT21_WriteRandomRecordToFCB( context );
4048 break;
4050 case 0x23: /* GET FILE SIZE FOR FCB */
4051 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4052 INT_BARF( context, 0x21 );
4053 break;
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 if (!ISV86(context) && DOSVM_IsWin16())
4060 DOSVM_SetPMHandler16( AL_reg(context), ptr );
4061 else
4062 DOSVM_SetRMHandler( AL_reg(context), ptr );
4064 break;
4066 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4067 INT_BARF( context, 0x21 );
4068 break;
4070 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4071 INT21_RandomBlockReadFromFCB( context );
4072 break;
4074 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4075 INT21_RandomBlockWriteToFCB( context );
4076 break;
4078 case 0x29: /* PARSE FILENAME INTO FCB */
4079 INT21_ParseFileNameIntoFCB(context);
4080 break;
4082 case 0x2a: /* GET SYSTEM DATE */
4083 TRACE( "GET SYSTEM DATE\n" );
4085 SYSTEMTIME systime;
4086 GetLocalTime( &systime );
4087 SET_CX( context, systime.wYear );
4088 SET_DH( context, systime.wMonth );
4089 SET_DL( context, systime.wDay );
4090 SET_AL( context, systime.wDayOfWeek );
4092 break;
4094 case 0x2b: /* SET SYSTEM DATE */
4095 TRACE( "SET SYSTEM DATE\n" );
4097 WORD year = CX_reg(context);
4098 BYTE month = DH_reg(context);
4099 BYTE day = DL_reg(context);
4101 if (year >= 1980 && year <= 2099 &&
4102 month >= 1 && month <= 12 &&
4103 day >= 1 && day <= 31)
4105 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4106 day, month, year );
4107 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4109 else
4111 SET_AL( context, 0xff ); /* invalid date */
4112 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4113 day, month, year );
4116 break;
4118 case 0x2c: /* GET SYSTEM TIME */
4119 TRACE( "GET SYSTEM TIME\n" );
4121 SYSTEMTIME systime;
4122 GetLocalTime( &systime );
4123 SET_CH( context, systime.wHour );
4124 SET_CL( context, systime.wMinute );
4125 SET_DH( context, systime.wSecond );
4126 SET_DL( context, systime.wMilliseconds / 10 );
4128 break;
4130 case 0x2d: /* SET SYSTEM TIME */
4131 if( CH_reg(context) >= 24 || CL_reg(context) >= 60 || DH_reg(context) >= 60 || DL_reg(context) >= 100 ) {
4132 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4133 CH_reg(context), CL_reg(context),
4134 DH_reg(context), DL_reg(context) );
4135 SET_AL( context, 0xFF );
4137 else
4139 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4140 CH_reg(context), CL_reg(context),
4141 DH_reg(context), DL_reg(context) );
4142 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4144 break;
4146 case 0x2e: /* SET VERIFY FLAG */
4147 TRACE("SET VERIFY FLAG ignored\n");
4148 /* we cannot change the behaviour anyway, so just ignore it */
4149 break;
4151 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4152 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4154 TDB *task = GlobalLock16( GetCurrentTask() );
4155 context->SegEs = SELECTOROF( task->dta );
4156 SET_BX( context, OFFSETOF( task->dta ) );
4158 break;
4160 case 0x30: /* GET DOS VERSION */
4161 TRACE( "GET DOS VERSION - %s requested\n",
4162 (AL_reg(context) == 0x00) ? "OEM number" : "version flag" );
4164 if (AL_reg(context) == 0x00)
4165 SET_BH( context, 0xff ); /* OEM number => undefined */
4166 else
4167 SET_BH( context, 0x08 ); /* version flag => DOS is in ROM */
4169 SET_AL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4170 SET_AH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4172 SET_BL( context, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4173 SET_CX( context, 0x3456 );
4174 break;
4176 case 0x31: /* TERMINATE AND STAY RESIDENT */
4177 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4178 break;
4180 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4182 BYTE drive = INT21_MapDrive( DL_reg(context) );
4183 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context) );
4185 if (INT21_FillDrivePB( drive ))
4187 SET_AL( context, 0x00 ); /* success */
4188 SET_DX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4189 context->SegDs = INT21_GetHeapSelector( context );
4191 else
4193 SET_AL( context, 0xff ); /* invalid or network drive */
4196 break;
4198 case 0x33: /* MULTIPLEXED */
4199 switch (AL_reg(context))
4201 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4202 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4203 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4204 break;
4206 case 0x01: /* SET EXTENDED BREAK STATE */
4207 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4208 DOSCONF_GetConfig()->brk_flag = (DL_reg(context) > 0) ? 1 : 0;
4209 break;
4211 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4212 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4213 /* ugly coding in order to stay reentrant */
4214 if (DL_reg(context))
4216 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4217 DOSCONF_GetConfig()->brk_flag = 1;
4219 else
4221 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4222 DOSCONF_GetConfig()->brk_flag = 0;
4224 break;
4226 case 0x05: /* GET BOOT DRIVE */
4227 TRACE("GET BOOT DRIVE\n");
4228 SET_DL( context, 3 );
4229 /* c: is Wine's bootdrive (a: is 1)*/
4230 break;
4232 case 0x06: /* GET TRUE VERSION NUMBER */
4233 TRACE("GET TRUE VERSION NUMBER\n");
4234 SET_BL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4235 SET_BH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4236 SET_DL( context, 0x00 ); /* revision */
4237 SET_DH( context, 0x08 ); /* DOS is in ROM */
4238 break;
4240 default:
4241 INT_BARF( context, 0x21 );
4242 break;
4244 break;
4246 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4247 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4248 context->SegEs = INT21_GetHeapSelector( context );
4249 SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
4250 break;
4252 case 0x35: /* GET INTERRUPT VECTOR */
4253 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4255 FARPROC16 addr;
4256 if (!ISV86(context) && DOSVM_IsWin16())
4257 addr = DOSVM_GetPMHandler16( AL_reg(context) );
4258 else
4259 addr = DOSVM_GetRMHandler( AL_reg(context) );
4260 context->SegEs = SELECTOROF(addr);
4261 SET_BX( context, OFFSETOF(addr) );
4263 break;
4265 case 0x36: /* GET FREE DISK SPACE */
4266 TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
4267 INT21_DriveName( DL_reg(context) ));
4268 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
4269 break;
4271 case 0x37: /* SWITCHAR */
4273 switch (AL_reg(context))
4275 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4276 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4277 SET_AL( context, 0x00 ); /* success*/
4278 SET_DL( context, '/' );
4279 break;
4280 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4281 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4282 DL_reg( context ));
4283 SET_AL( context, 0x00 ); /* success*/
4284 break;
4285 default:
4286 INT_BARF( context, 0x21 );
4287 break;
4290 break;
4292 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4293 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4294 if (AL_reg(context))
4296 WORD country = AL_reg(context);
4297 if (country == 0xff)
4298 country = BX_reg(context);
4299 if (country != INT21_GetSystemCountryCode()) {
4300 FIXME( "Requested info on non-default country %04x\n", country );
4301 SET_AX(context, 2);
4302 SET_CFLAG(context);
4305 if(AX_reg(context) != 2 )
4307 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context,
4308 context->SegDs,
4309 context->Edx) );
4310 SET_AX( context, INT21_GetSystemCountryCode() );
4311 SET_BX( context, INT21_GetSystemCountryCode() );
4312 RESET_CFLAG(context);
4314 break;
4316 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4317 if (!INT21_CreateDirectory( context ))
4318 bSetDOSExtendedError = TRUE;
4319 else
4320 RESET_CFLAG(context);
4321 break;
4323 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4325 WCHAR dirW[MAX_PATH];
4326 char *dirA = CTX_SEG_OFF_TO_LIN(context,
4327 context->SegDs, context->Edx);
4329 TRACE( "REMOVE DIRECTORY %s\n", dirA );
4331 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
4333 if (!RemoveDirectoryW( dirW ))
4334 bSetDOSExtendedError = TRUE;
4335 else
4336 RESET_CFLAG(context);
4338 break;
4340 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4341 if (!INT21_SetCurrentDirectory( context ))
4342 bSetDOSExtendedError = TRUE;
4343 else
4344 RESET_CFLAG(context);
4345 break;
4347 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4348 if (!INT21_CreateFile( context, context->Edx, FALSE,
4349 OF_READWRITE | OF_SHARE_COMPAT, 0x12 ))
4350 bSetDOSExtendedError = TRUE;
4351 else
4352 RESET_CFLAG(context);
4353 break;
4355 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4356 if (!INT21_CreateFile( context, context->Edx, FALSE,
4357 AL_reg(context), 0x01 ))
4358 bSetDOSExtendedError = TRUE;
4359 else
4360 RESET_CFLAG(context);
4361 break;
4363 case 0x3e: /* "CLOSE" - CLOSE FILE */
4364 TRACE( "CLOSE handle %d\n", BX_reg(context) );
4365 if (_lclose16( BX_reg(context) ) == HFILE_ERROR16)
4366 bSetDOSExtendedError = TRUE;
4367 else
4368 RESET_CFLAG(context);
4369 break;
4371 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4372 TRACE( "READ from %d to %04lX:%04X for %d bytes\n",
4373 BX_reg(context),
4374 context->SegDs,
4375 DX_reg(context),
4376 CX_reg(context) );
4378 DWORD result;
4379 WORD count = CX_reg(context);
4380 BYTE *buffer = CTX_SEG_OFF_TO_LIN( context,
4381 context->SegDs,
4382 context->Edx );
4384 /* Some programs pass a count larger than the allocated buffer */
4385 if (DOSVM_IsWin16())
4387 WORD maxcount = GetSelectorLimit16( context->SegDs )
4388 - DX_reg(context) + 1;
4389 if (count > maxcount)
4390 count = maxcount;
4394 * FIXME: Reading from console (BX=1) in DOS mode
4395 * does not work as it is supposed to work.
4398 RESET_CFLAG(context); /* set if error */
4399 if (!DOSVM_IsWin16() && BX_reg(context) == 0)
4401 result = INT21_BufferedInput( context, buffer, count );
4402 SET_AX( context, (WORD)result );
4404 else if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context)),
4405 buffer, count, &result, NULL ))
4406 SET_AX( context, (WORD)result );
4407 else
4408 bSetDOSExtendedError = TRUE;
4410 break;
4412 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4413 TRACE( "WRITE from %04lX:%04X to handle %d for %d byte\n",
4414 context->SegDs, DX_reg(context),
4415 BX_reg(context), CX_reg(context) );
4417 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4419 if (!DOSVM_IsWin16() &&
4420 (BX_reg(context) == 1 || BX_reg(context) == 2))
4422 int i;
4423 for(i=0; i<CX_reg(context); i++)
4424 DOSVM_PutChar(ptr[i]);
4425 SET_AX(context, CX_reg(context));
4426 RESET_CFLAG(context);
4428 else
4430 HFILE handle = (HFILE)DosFileHandleToWin32Handle(BX_reg(context));
4431 LONG result = _hwrite( handle, ptr, CX_reg(context) );
4432 if (result == HFILE_ERROR)
4433 bSetDOSExtendedError = TRUE;
4434 else
4436 SET_AX( context, (WORD)result );
4437 RESET_CFLAG(context);
4441 break;
4443 case 0x41: /* "UNLINK" - DELETE FILE */
4445 WCHAR fileW[MAX_PATH];
4446 char *fileA = CTX_SEG_OFF_TO_LIN(context,
4447 context->SegDs,
4448 context->Edx);
4450 TRACE( "UNLINK %s\n", fileA );
4451 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
4453 if (!DeleteFileW( fileW ))
4454 bSetDOSExtendedError = TRUE;
4455 else
4456 RESET_CFLAG(context);
4458 break;
4460 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4461 TRACE( "LSEEK handle %d offset %ld from %s\n",
4462 BX_reg(context),
4463 MAKELONG( DX_reg(context), CX_reg(context) ),
4464 (AL_reg(context) == 0) ?
4465 "start of file" : ((AL_reg(context) == 1) ?
4466 "current file position" : "end of file") );
4468 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4469 LONG offset = MAKELONG( DX_reg(context), CX_reg(context) );
4470 DWORD status = SetFilePointer( handle, offset,
4471 NULL, AL_reg(context) );
4472 if (status == INVALID_SET_FILE_POINTER)
4473 bSetDOSExtendedError = TRUE;
4474 else
4476 SET_AX( context, LOWORD(status) );
4477 SET_DX( context, HIWORD(status) );
4478 RESET_CFLAG(context);
4481 break;
4483 case 0x43: /* FILE ATTRIBUTES */
4484 if (!INT21_FileAttributes( context, AL_reg(context), FALSE ))
4485 bSetDOSExtendedError = TRUE;
4486 else
4487 RESET_CFLAG(context);
4488 break;
4490 case 0x44: /* IOCTL */
4491 INT21_Ioctl( context );
4492 break;
4494 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4495 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context) );
4497 HANDLE handle32;
4498 HFILE handle16 = HFILE_ERROR;
4500 if (DuplicateHandle( GetCurrentProcess(),
4501 DosFileHandleToWin32Handle(BX_reg(context)),
4502 GetCurrentProcess(),
4503 &handle32,
4504 0, TRUE, DUPLICATE_SAME_ACCESS ))
4505 handle16 = Win32HandleToDosFileHandle(handle32);
4507 if (handle16 == HFILE_ERROR)
4508 bSetDOSExtendedError = TRUE;
4509 else
4511 SET_AX( context, handle16 );
4512 RESET_CFLAG(context);
4515 break;
4517 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4518 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4519 BX_reg(context), CX_reg(context) );
4521 if (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR16)
4522 bSetDOSExtendedError = TRUE;
4523 else
4524 RESET_CFLAG(context);
4525 break;
4527 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4528 if (!INT21_GetCurrentDirectory( context, FALSE ))
4529 bSetDOSExtendedError = TRUE;
4530 else
4531 RESET_CFLAG(context);
4532 break;
4534 case 0x48: /* ALLOCATE MEMORY */
4535 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
4537 WORD selector = 0;
4538 DWORD bytes = (DWORD)BX_reg(context) << 4;
4540 if (!ISV86(context) && DOSVM_IsWin16())
4542 DWORD rv = GlobalDOSAlloc16( bytes );
4543 selector = LOWORD( rv );
4545 else
4546 DOSMEM_GetBlock( bytes, &selector );
4548 if (selector)
4550 SET_AX( context, selector );
4551 RESET_CFLAG(context);
4553 else
4555 SET_CFLAG(context);
4556 SET_AX( context, 0x0008 ); /* insufficient memory */
4557 SET_BX( context, DOSMEM_Available() >> 4 );
4560 break;
4562 case 0x49: /* FREE MEMORY */
4563 TRACE( "FREE MEMORY segment %04lX\n", context->SegEs );
4565 BOOL ok;
4567 if (!ISV86(context) && DOSVM_IsWin16())
4569 ok = !GlobalDOSFree16( context->SegEs );
4571 /* If we don't reset ES_reg, we will fail in the relay code */
4572 if (ok)
4573 context->SegEs = 0;
4575 else
4576 ok = DOSMEM_FreeBlock( (void*)((DWORD)context->SegEs << 4) );
4578 if (!ok)
4580 TRACE("FREE MEMORY failed\n");
4581 SET_CFLAG(context);
4582 SET_AX( context, 0x0009 ); /* memory block address invalid */
4585 break;
4587 case 0x4a: /* RESIZE MEMORY BLOCK */
4588 TRACE( "RESIZE MEMORY segment %04lX to %d paragraphs\n",
4589 context->SegEs, BX_reg(context) );
4591 DWORD newsize = (DWORD)BX_reg(context) << 4;
4593 if (!ISV86(context) && DOSVM_IsWin16())
4595 FIXME( "Resize memory block - unsupported under Win16\n" );
4596 SET_CFLAG(context);
4598 else
4600 LPVOID address = (void*)((DWORD)context->SegEs << 4);
4601 UINT blocksize = DOSMEM_ResizeBlock( address, newsize, FALSE );
4603 RESET_CFLAG(context);
4604 if (blocksize == (UINT)-1)
4606 SET_CFLAG( context );
4607 SET_AX( context, 0x0009 ); /* illegal address */
4609 else if(blocksize != newsize)
4611 SET_CFLAG( context );
4612 SET_AX( context, 0x0008 ); /* insufficient memory */
4613 SET_BX( context, blocksize >> 4 ); /* new block size */
4617 break;
4619 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4621 BYTE *program = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4622 BYTE *paramblk = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx);
4624 TRACE( "EXEC %s\n", program );
4626 RESET_CFLAG(context);
4627 if (DOSVM_IsWin16())
4629 HINSTANCE16 instance = WinExec16( program, SW_NORMAL );
4630 if (instance < 32)
4632 SET_CFLAG( context );
4633 SET_AX( context, instance );
4636 else
4638 if (!MZ_Exec( context, program, AL_reg(context), paramblk))
4639 bSetDOSExtendedError = TRUE;
4642 break;
4644 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
4645 TRACE( "EXIT with return code %d\n", AL_reg(context) );
4646 if (DOSVM_IsWin16())
4647 ExitThread( AL_reg(context) );
4648 else if(ISV86(context))
4649 MZ_Exit( context, FALSE, AL_reg(context) );
4650 else
4653 * Exit from DPMI.
4655 DWORD rv = AL_reg(context);
4656 RaiseException( EXCEPTION_VM86_INTx, 0, 1, &rv );
4658 break;
4660 case 0x4d: /* GET RETURN CODE */
4661 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
4662 SET_AX( context, DOSVM_retval );
4663 DOSVM_retval = 0;
4664 break;
4666 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
4667 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
4668 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
4669 if (!INT21_FindFirst(context)) break;
4670 /* fall through */
4672 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
4673 TRACE("FINDNEXT\n");
4674 if (!INT21_FindNext(context))
4676 SetLastError( ERROR_NO_MORE_FILES );
4677 SET_AX( context, ERROR_NO_MORE_FILES );
4678 SET_CFLAG(context);
4680 else SET_AX( context, 0 ); /* OK */
4681 break;
4683 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
4684 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
4685 DOSVM_psp = BX_reg(context);
4686 break;
4688 case 0x51: /* GET PSP ADDRESS */
4689 INT21_GetPSP( context );
4690 break;
4692 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
4694 SEGPTR ptr = DOSDEV_GetLOL( ISV86(context) || !DOSVM_IsWin16() );
4695 context->SegEs = SELECTOROF(ptr);
4696 SET_BX( context, OFFSETOF(ptr) );
4698 break;
4700 case 0x54: /* Get Verify Flag */
4701 TRACE("Get Verify Flag - Not Supported\n");
4702 SET_AL( context, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
4703 break;
4705 case 0x56: /* "RENAME" - RENAME FILE */
4706 if (!INT21_RenameFile( context ))
4707 bSetDOSExtendedError = TRUE;
4708 else
4709 RESET_CFLAG(context);
4710 break;
4712 case 0x57: /* FILE DATE AND TIME */
4713 if (!INT21_FileDateTime( context ))
4714 bSetDOSExtendedError = TRUE;
4715 else
4716 RESET_CFLAG(context);
4717 break;
4719 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
4720 switch (AL_reg(context))
4722 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
4723 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
4724 SET_AX( context, 0 ); /* low memory first fit */
4725 break;
4727 case 0x01: /* SET ALLOCATION STRATEGY */
4728 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
4729 BL_reg(context) );
4730 break;
4732 case 0x02: /* GET UMB LINK STATE */
4733 TRACE( "GET UMB LINK STATE\n" );
4734 SET_AL( context, 0 ); /* UMBs not part of DOS memory chain */
4735 break;
4737 case 0x03: /* SET UMB LINK STATE */
4738 TRACE( "SET UMB LINK STATE to %d - ignored\n",
4739 BX_reg(context) );
4740 break;
4742 default:
4743 INT_BARF( context, 0x21 );
4745 break;
4747 case 0x59: /* GET EXTENDED ERROR INFO */
4748 INT21_GetExtendedError( context );
4749 break;
4751 case 0x5a: /* CREATE TEMPORARY FILE */
4752 TRACE("CREATE TEMPORARY FILE\n");
4753 bSetDOSExtendedError = !INT21_CreateTempFile(context);
4754 break;
4756 case 0x5b: /* CREATE NEW FILE */
4757 if (!INT21_CreateFile( context, context->Edx, FALSE,
4758 OF_READWRITE | OF_SHARE_COMPAT, 0x10 ))
4759 bSetDOSExtendedError = TRUE;
4760 else
4761 RESET_CFLAG(context);
4762 break;
4764 case 0x5c: /* "FLOCK" - RECORD LOCKING */
4766 DWORD offset = MAKELONG(DX_reg(context), CX_reg(context));
4767 DWORD length = MAKELONG(DI_reg(context), SI_reg(context));
4768 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4770 RESET_CFLAG(context);
4771 switch (AL_reg(context))
4773 case 0x00: /* LOCK */
4774 TRACE( "lock handle %d offset %ld length %ld\n",
4775 BX_reg(context), offset, length );
4776 if (!LockFile( handle, offset, 0, length, 0 ))
4777 bSetDOSExtendedError = TRUE;
4778 break;
4780 case 0x01: /* UNLOCK */
4781 TRACE( "unlock handle %d offset %ld length %ld\n",
4782 BX_reg(context), offset, length );
4783 if (!UnlockFile( handle, offset, 0, length, 0 ))
4784 bSetDOSExtendedError = TRUE;
4785 break;
4787 default:
4788 INT_BARF( context, 0x21 );
4791 break;
4793 case 0x5d: /* NETWORK 5D */
4794 FIXME( "Network function 5D not implemented.\n" );
4795 SetLastError( ER_NoNetwork );
4796 bSetDOSExtendedError = TRUE;
4797 break;
4799 case 0x5e: /* NETWORK 5E */
4800 bSetDOSExtendedError = INT21_NetworkFunc( context);
4801 break;
4803 case 0x5f: /* NETWORK 5F */
4804 /* FIXME: supporting this would need to 1:
4805 * - implement per drive current directory (as kernel32 doesn't)
4806 * - assign enabled/disabled flag on a per drive basis
4808 /* network software not installed */
4809 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
4810 SetLastError( ER_NoNetwork );
4811 bSetDOSExtendedError = TRUE;
4812 break;
4814 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
4816 WCHAR pathW[MAX_PATH], res[MAX_PATH];
4817 /* FIXME: likely to be broken */
4819 TRACE("TRUENAME %s\n",
4820 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
4821 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
4822 if (!GetFullPathNameW( pathW, 128, res, NULL ))
4823 bSetDOSExtendedError = TRUE;
4824 else
4826 SET_AX( context, 0 );
4827 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
4828 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
4829 128, NULL, NULL);
4832 break;
4834 case 0x61: /* UNUSED */
4835 SET_AL( context, 0 );
4836 break;
4838 case 0x62: /* GET PSP ADDRESS */
4839 INT21_GetPSP( context );
4840 break;
4842 case 0x63: /* MISC. LANGUAGE SUPPORT */
4843 switch (AL_reg(context)) {
4844 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
4845 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
4846 context->SegDs = INT21_GetHeapSelector( context );
4847 SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
4848 SET_AL( context, 0 ); /* success */
4849 break;
4851 break;
4853 case 0x64: /* OS/2 DOS BOX */
4854 INT_BARF( context, 0x21 );
4855 SET_CFLAG(context);
4856 break;
4858 case 0x65: /* EXTENDED COUNTRY INFORMATION */
4859 INT21_ExtendedCountryInformation( context );
4860 break;
4862 case 0x66: /* GLOBAL CODE PAGE TABLE */
4863 switch (AL_reg(context))
4865 case 0x01:
4866 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
4867 SET_BX( context, GetOEMCP() );
4868 SET_DX( context, GetOEMCP() );
4869 break;
4870 case 0x02:
4871 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
4872 BX_reg(context), DX_reg(context) );
4873 break;
4875 break;
4877 case 0x67: /* SET HANDLE COUNT */
4878 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
4879 if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
4880 bSetDOSExtendedError = TRUE;
4881 break;
4883 case 0x68: /* "FFLUSH" - COMMIT FILE */
4884 TRACE( "FFLUSH - handle %d\n", BX_reg(context) );
4885 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
4886 bSetDOSExtendedError = TRUE;
4887 break;
4889 case 0x69: /* DISK SERIAL NUMBER */
4890 switch (AL_reg(context))
4892 case 0x00:
4893 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
4894 INT21_DriveName(BL_reg(context)));
4895 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
4896 else SET_AX( context, 0 );
4897 break;
4899 case 0x01:
4900 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
4901 INT21_DriveName(BL_reg(context)));
4902 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
4903 else SET_AX( context, 1 );
4904 break;
4906 break;
4908 case 0x6a: /* COMMIT FILE */
4909 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context) );
4910 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
4911 bSetDOSExtendedError = TRUE;
4912 break;
4914 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4915 SET_AL( context, 0 );
4916 break;
4918 case 0x6c: /* EXTENDED OPEN/CREATE */
4919 if (!INT21_CreateFile( context, context->Esi, TRUE,
4920 BX_reg(context), DL_reg(context) ))
4921 bSetDOSExtendedError = TRUE;
4922 break;
4924 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
4925 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
4926 SET_CFLAG( context );
4927 SET_AL( context, 0 );
4928 break;
4930 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
4931 INT21_LongFilename( context );
4932 break;
4934 case 0x73: /* MSDOS7 - FAT32 */
4935 RESET_CFLAG( context );
4936 if (!INT21_Fat32( context ))
4937 bSetDOSExtendedError = TRUE;
4938 break;
4940 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
4941 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
4942 break;
4944 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
4945 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
4946 break;
4948 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
4949 /* we don't implement a DOS32 extender */
4950 TRACE( "DOS32 EXTENDER API - ignored\n" );
4951 break;
4953 default:
4954 INT_BARF( context, 0x21 );
4955 break;
4957 } /* END OF SWITCH */
4959 /* Set general error condition. */
4960 if (bSetDOSExtendedError)
4962 SET_AX( context, GetLastError() );
4963 SET_CFLAG( context );
4966 /* Print error code if carry flag is set. */
4967 if (context->EFlags & 0x0001)
4968 TRACE("failed, error %ld\n", GetLastError() );
4970 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
4971 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
4972 AX_reg(context), BX_reg(context),
4973 CX_reg(context), DX_reg(context),
4974 SI_reg(context), DI_reg(context),
4975 (WORD)context->SegDs, (WORD)context->SegEs,
4976 context->EFlags );