mfplat/tests: Mark some tests as broken on Win 8 and 10 v1507.
[wine.git] / dlls / krnl386.exe16 / int21.c
blob50cc3fa7f2e3b70fa551a42b83e4b38203b5db42
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "wincon.h"
34 #include "winternl.h"
35 #include "wine/winbase16.h"
36 #include "kernel16_private.h"
37 #include "dosexe.h"
38 #include "winerror.h"
39 #include "winuser.h"
40 #include "wine/debug.h"
43 * Note:
44 * - Most of the file related functions are wrong. NT's kernel32
45 * doesn't maintain a per drive current directory, while DOS does.
46 * We should in fact keep track in here of those per drive
47 * directories, and use this info while dealing with partial paths
48 * (drive defined, but only relative paths). This could even be
49 * created as an array of CDS (there should be an entry for that in
50 * the LOL)
54 * Forward declarations.
56 static BOOL INT21_RenameFile( CONTEXT *context );
58 WINE_DEFAULT_DEBUG_CHANNEL(int21);
61 #include "pshpack1.h"
64 * Extended Drive Parameter Block.
65 * This structure is compatible with standard DOS4+ DPB and
66 * extended DOS7 DPB.
68 typedef struct _INT21_DPB {
69 BYTE drive; /* 00 drive number (0=A, ...) */
70 BYTE unit; /* 01 unit number within device driver */
71 WORD sector_bytes; /* 02 bytes per sector */
72 BYTE cluster_sectors; /* 04 highest sector number within a cluster */
73 BYTE shift; /* 05 shift count to convert clusters into sectors */
74 WORD num_reserved; /* 06 reserved sectors at beginning of drive */
75 BYTE num_FAT; /* 08 number of FATs */
76 WORD num_root_entries; /* 09 number of root directory entries */
77 WORD first_data_sector; /* 0b number of first sector containing user data */
78 WORD num_clusters1; /* 0d highest cluster number (number of data clusters + 1) */
79 WORD sectors_per_FAT; /* 0f number of sectors per FAT */
80 WORD first_dir_sector; /* 11 sector number of first directory sector */
81 SEGPTR driver_header; /* 13 address of device driver header */
82 BYTE media_ID; /* 17 media ID byte */
83 BYTE access_flag; /* 18 0x00 if disk accessed, 0xff if not */
84 SEGPTR next; /* 19 pointer to next DPB */
85 WORD search_cluster1; /* 1d cluster at which to start search for free space */
86 WORD free_clusters_lo; /* 1f number of free clusters on drive or 0xffff if unknown */
87 WORD free_clusters_hi; /* 21 hiword of clusters_free */
88 WORD mirroring_flags; /* 23 active FAT/mirroring flags */
89 WORD info_sector; /* 25 sector number of file system info sector or 0xffff for none */
90 WORD spare_boot_sector; /* 27 sector number of backup boot sector or 0xffff for none */
91 DWORD first_cluster_sector; /* 29 sector number of the first cluster */
92 DWORD num_clusters2; /* 2d maximum cluster number */
93 DWORD fat_clusters; /* 31 number of clusters occupied by FAT */
94 DWORD root_cluster; /* 35 cluster number of start of root directory */
95 DWORD search_cluster2; /* 39 cluster at which to start searching for free space */
96 } INT21_DPB;
100 * Structure for DOS data that can be accessed directly from applications.
101 * Real and protected mode pointers will be returned to this structure so
102 * the structure must be correctly packed.
104 typedef struct _INT21_HEAP {
105 WORD uppercase_size; /* Size of the following table in bytes */
106 BYTE uppercase_table[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
108 WORD lowercase_size; /* Size of the following table in bytes */
109 BYTE lowercase_table[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
111 WORD collating_size; /* Size of the following table in bytes */
112 BYTE collating_table[256]; /* Values used to sort characters from 0x00 to 0xff. */
114 WORD filename_size; /* Size of the following filename data in bytes */
115 BYTE filename_reserved1; /* 0x01 for MS-DOS 3.30-6.00 */
116 BYTE filename_lowest; /* Lowest permissible character value for filename */
117 BYTE filename_highest; /* Highest permissible character value for filename */
118 BYTE filename_reserved2; /* 0x00 for MS-DOS 3.30-6.00 */
119 BYTE filename_exclude_first; /* First illegal character in permissible range */
120 BYTE filename_exclude_last; /* Last illegal character in permissible range */
121 BYTE filename_reserved3; /* 0x02 for MS-DOS 3.30-6.00 */
122 BYTE filename_illegal_size; /* Number of terminators in the following table */
123 BYTE filename_illegal_table[16]; /* Characters which terminate a filename */
125 WORD dbcs_size; /* Number of valid ranges in the following table */
126 BYTE dbcs_table[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
128 BYTE misc_indos; /* Interrupt 21 nesting flag */
129 WORD misc_selector; /* Protected mode selector for INT21_HEAP */
130 INT21_DPB misc_dpb_list[MAX_DOS_DRIVES]; /* Drive parameter blocks for all drives */
132 } INT21_HEAP;
135 struct FCB {
136 BYTE drive_number;
137 CHAR file_name[8];
138 CHAR file_extension[3];
139 WORD current_block_number;
140 WORD logical_record_size;
141 DWORD file_size;
142 WORD date_of_last_write;
143 WORD time_of_last_write;
144 BYTE file_number;
145 BYTE attributes;
146 WORD starting_cluster;
147 WORD sequence_number;
148 BYTE file_attributes;
149 BYTE unused;
150 BYTE record_within_current_block;
151 BYTE random_access_record_number[4];
155 struct XFCB {
156 BYTE xfcb_signature;
157 BYTE reserved[5];
158 BYTE xfcb_file_attribute;
159 BYTE fcb[37];
162 /* DTA layout for FindFirst/FindNext */
163 typedef struct
165 BYTE drive; /* 00 drive letter */
166 char mask[11]; /* 01 search template */
167 BYTE search_attr; /* 0c search attributes */
168 WORD count; /* 0d entry count within directory */
169 WORD cluster; /* 0f cluster of parent directory */
170 WCHAR *fullPath; /* 11 full path (was: reserved) */
171 BYTE fileattr; /* 15 file attributes */
172 WORD filetime; /* 16 file time */
173 WORD filedate; /* 18 file date */
174 DWORD filesize; /* 1a file size */
175 char filename[13]; /* 1e file name + extension */
176 } FINDFILE_DTA;
178 /* FCB layout for FindFirstFCB/FindNextFCB */
179 typedef struct
181 BYTE drive; /* 00 drive letter */
182 char filename[11]; /* 01 filename 8+3 format */
183 int count; /* 0c entry count (was: reserved) */
184 WCHAR *fullPath; /* 10 full path (was: reserved) */
185 } FINDFILE_FCB;
187 /* DOS directory entry for FindFirstFCB/FindNextFCB */
188 typedef struct
190 char filename[11]; /* 00 filename 8+3 format */
191 BYTE fileattr; /* 0b file attributes */
192 BYTE reserved[10]; /* 0c reserved */
193 WORD filetime; /* 16 file time */
194 WORD filedate; /* 18 file date */
195 WORD cluster; /* 1a file first cluster */
196 DWORD filesize; /* 1c file size */
197 } DOS_DIRENTRY_LAYOUT;
199 #include "poppack.h"
201 /* dos file attributes */
202 #define FA_NORMAL 0x00 /* Normal file, no attributes */
203 #define FA_RDONLY 0x01 /* Read only attribute */
204 #define FA_HIDDEN 0x02 /* Hidden file */
205 #define FA_SYSTEM 0x04 /* System file */
206 #define FA_LABEL 0x08 /* Volume label */
207 #define FA_DIRECTORY 0x10 /* Directory */
208 #define FA_ARCHIVE 0x20 /* Archive */
209 #define FA_UNUSED 0x40 /* Unused */
211 /* Error codes */
212 #define ER_NoNetwork 0x49
214 /* Error classes */
215 #define EC_OutOfResource 0x01
216 #define EC_Temporary 0x02
217 #define EC_AccessDenied 0x03
218 #define EC_InternalError 0x04
219 #define EC_HardwareFailure 0x05
220 #define EC_SystemFailure 0x06
221 #define EC_ProgramError 0x07
222 #define EC_NotFound 0x08
223 #define EC_MediaError 0x0b
224 #define EC_Exists 0x0c
225 #define EC_Unknown 0x0d
227 /* Suggested actions */
228 #define SA_Retry 0x01
229 #define SA_DelayedRetry 0x02
230 #define SA_Abort 0x04
231 #define SA_Ignore 0x06
232 #define SA_Ask4Retry 0x07
234 /* Error locus */
235 #define EL_Unknown 0x01
236 #define EL_Disk 0x02
237 #define EL_Network 0x03
238 #define EL_Serial 0x04
239 #define EL_Memory 0x05
241 /* BIOS Keyboard Scancodes */
242 #define KEY_LEFT 0x4B
243 #define KEY_RIGHT 0x4D
244 #define KEY_UP 0x48
245 #define KEY_DOWN 0x50
246 #define KEY_IC 0x52 /* insert char */
247 #define KEY_DC 0x53 /* delete char */
248 #define KEY_BACKSPACE 0x0E
249 #define KEY_HOME 0x47
250 #define KEY_END 0x4F
251 #define KEY_NPAGE 0x49
252 #define KEY_PPAGE 0x51
254 static int brk_flag;
256 static BYTE drive_number( WCHAR letter )
258 if (letter >= 'A' && letter <= 'Z') return letter - 'A';
259 if (letter >= 'a' && letter <= 'z') return letter - 'a';
260 return MAX_DOS_DRIVES;
264 /* Many calls translate a drive argument like this:
265 drive number (00h = default, 01h = A:, etc)
267 /******************************************************************
268 * INT21_DriveName
270 * Many calls translate a drive argument like this:
271 * drive number (00h = default, 01h = A:, etc)
273 static const char *INT21_DriveName(int drive)
275 if (drive > 0)
277 if (drive <= 26) return wine_dbg_sprintf("%c:", 'A' + drive - 1);
278 else return wine_dbg_sprintf( "<Bad drive: %d>", drive);
280 return "default";
283 /***********************************************************************
284 * INT21_GetCurrentDrive
286 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
287 * MAX_DOS_DRIVES on error.
289 static BYTE INT21_GetCurrentDrive(void)
291 WCHAR current_directory[MAX_PATH];
293 if (!GetCurrentDirectoryW( MAX_PATH, current_directory ) ||
294 current_directory[1] != ':')
296 TRACE( "Failed to get current drive.\n" );
297 return MAX_DOS_DRIVES;
299 return drive_number( current_directory[0] );
303 /***********************************************************************
304 * INT21_MapDrive
306 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
307 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
309 static BYTE INT21_MapDrive( BYTE drive )
311 if (drive)
313 WCHAR drivespec[] = {'A', ':', 0};
314 UINT drivetype;
316 drivespec[0] += drive - 1;
317 drivetype = GetDriveTypeW( drivespec );
319 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
320 return MAX_DOS_DRIVES;
322 return drive - 1;
325 return INT21_GetCurrentDrive();
329 /***********************************************************************
330 * INT21_SetCurrentDrive
332 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
334 static void INT21_SetCurrentDrive( BYTE drive )
336 WCHAR drivespec[] = {'A', ':', 0};
338 drivespec[0] += drive;
340 if (!SetCurrentDirectoryW( drivespec ))
341 TRACE( "Failed to set current drive.\n" );
345 /***********************************************************************
346 * INT21_GetSystemCountryCode
348 * Return DOS country code for default system locale.
350 static WORD INT21_GetSystemCountryCode( void )
352 return GetSystemDefaultLangID();
356 /***********************************************************************
357 * INT21_FillCountryInformation
359 * Fill 34-byte buffer with country information data using
360 * default system locale.
362 static void INT21_FillCountryInformation( BYTE *buffer )
364 /* 00 - WORD: date format
365 * 00 = mm/dd/yy
366 * 01 = dd/mm/yy
367 * 02 = yy/mm/dd
369 *(WORD*)(buffer + 0) = 0; /* FIXME: Get from locale */
371 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
372 buffer[2] = '$'; /* FIXME: Get from locale */
373 buffer[3] = 0;
375 /* 07 - BYTE[2]: ASCIIZ thousands separator */
376 buffer[7] = 0; /* FIXME: Get from locale */
377 buffer[8] = 0;
379 /* 09 - BYTE[2]: ASCIIZ decimal separator */
380 buffer[9] = '.'; /* FIXME: Get from locale */
381 buffer[10] = 0;
383 /* 11 - BYTE[2]: ASCIIZ date separator */
384 buffer[11] = '/'; /* FIXME: Get from locale */
385 buffer[12] = 0;
387 /* 13 - BYTE[2]: ASCIIZ time separator */
388 buffer[13] = ':'; /* FIXME: Get from locale */
389 buffer[14] = 0;
391 /* 15 - BYTE: Currency format
392 * bit 2 = set if currency symbol replaces decimal point
393 * bit 1 = number of spaces between value and currency symbol
394 * bit 0 = 0 if currency symbol precedes value
395 * 1 if currency symbol follows value
397 buffer[15] = 0; /* FIXME: Get from locale */
399 /* 16 - BYTE: Number of digits after decimal in currency */
400 buffer[16] = 0; /* FIXME: Get from locale */
402 /* 17 - BYTE: Time format
403 * bit 0 = 0 if 12-hour clock
404 * 1 if 24-hour clock
406 buffer[17] = 1; /* FIXME: Get from locale */
408 /* 18 - DWORD: Address of case map routine */
409 *(DWORD*)(buffer + 18) = 0; /* FIXME: ptr to case map routine */
411 /* 22 - BYTE[2]: ASCIIZ data-list separator */
412 buffer[22] = ','; /* FIXME: Get from locale */
413 buffer[23] = 0;
415 /* 24 - BYTE[10]: Reserved */
416 memset( buffer + 24, 0, 10 );
420 /***********************************************************************
421 * INT21_FillHeap
423 * Initialize DOS heap.
425 * Filename Terminator Table of w2k DE NTVDM:
426 * 16 00 01 00 FF 00 00 20-02 0E 2E 22 2F 5C 5B 5D ....... ..."/\[]
427 * 3A 7C 3C 3E 2B 3D 3B 2C-00 :|<>+=;,
429 static void INT21_FillHeap( INT21_HEAP *heap )
431 static const char terminators[] = ".\"/\\[]:|<>+=;,";
432 int i;
435 * Uppercase table.
437 heap->uppercase_size = 128;
438 for (i = 0; i < 128; i++)
439 heap->uppercase_table[i] = toupper( 128 + i );
442 * Lowercase table.
444 heap->lowercase_size = 256;
445 for (i = 0; i < 256; i++)
446 heap->lowercase_table[i] = tolower( i );
449 * Collating table.
451 heap->collating_size = 256;
452 for (i = 0; i < 256; i++)
453 heap->collating_table[i] = i;
456 * Filename table.
458 heap->filename_size = 8 + strlen(terminators);
459 heap->filename_illegal_size = strlen(terminators);
460 memcpy( heap->filename_illegal_table, terminators, heap->filename_illegal_size );
462 heap->filename_reserved1 = 0x01;
463 heap->filename_lowest = 0x00;
464 heap->filename_highest = 0xff;
465 heap->filename_reserved2 = 0x00;
466 heap->filename_exclude_first = 0x00;
467 heap->filename_exclude_last = 0x20;
468 heap->filename_reserved3 = 0x02;
471 * DBCS lead byte table. This table is empty.
473 heap->dbcs_size = 0;
474 memset( heap->dbcs_table, 0, sizeof(heap->dbcs_table) );
477 * Initialize InDos flag.
479 heap->misc_indos = 0;
482 * FIXME: Should drive parameter blocks (DPB) be
483 * initialized here and linked to DOS LOL?
488 /***********************************************************************
489 * INT21_GetHeapPointer
491 * Get pointer for DOS heap (INT21_HEAP).
492 * Creates and initializes heap on first call.
494 static INT21_HEAP *INT21_GetHeapPointer( void )
496 static INT21_HEAP *heap_pointer = NULL;
498 if (!heap_pointer)
500 WORD heap_selector = GlobalAlloc16( GMEM_FIXED, sizeof(INT21_HEAP) );
501 heap_pointer = GlobalLock16( heap_selector );
502 heap_pointer->misc_selector = heap_selector;
503 INT21_FillHeap( heap_pointer );
506 return heap_pointer;
510 /***********************************************************************
511 * INT21_GetHeapSelector
513 * Get segment/selector for DOS heap (INT21_HEAP).
514 * Creates and initializes heap on first call.
516 static WORD INT21_GetHeapSelector( CONTEXT *context )
518 INT21_HEAP *heap = INT21_GetHeapPointer();
519 return heap->misc_selector;
523 /***********************************************************************
524 * INT21_FillDrivePB
526 * Fill DOS heap drive parameter block for the specified drive.
527 * Return TRUE if drive was valid and there were
528 * no errors while reading drive information.
530 static BOOL INT21_FillDrivePB( BYTE drive )
532 WCHAR drivespec[] = {'A', ':', 0};
533 INT21_HEAP *heap = INT21_GetHeapPointer();
534 INT21_DPB *dpb;
535 UINT drivetype;
536 DWORD cluster_sectors;
537 DWORD sector_bytes;
538 DWORD free_clusters;
539 DWORD total_clusters;
541 if (drive >= MAX_DOS_DRIVES)
542 return FALSE;
544 dpb = &heap->misc_dpb_list[drive];
545 drivespec[0] += drive;
546 drivetype = GetDriveTypeW( drivespec );
549 * FIXME: Does this check work correctly with floppy/cdrom drives?
551 if (drivetype == DRIVE_NO_ROOT_DIR || drivetype == DRIVE_UNKNOWN)
552 return FALSE;
555 * FIXME: Does this check work correctly with floppy/cdrom drives?
557 if (!GetDiskFreeSpaceW( drivespec, &cluster_sectors, &sector_bytes,
558 &free_clusters, &total_clusters ))
559 return FALSE;
562 * FIXME: Most of the values listed below are incorrect.
563 * All values should be validated.
566 dpb->drive = drive;
567 dpb->unit = 0;
568 dpb->sector_bytes = sector_bytes;
569 dpb->cluster_sectors = cluster_sectors - 1;
571 dpb->shift = 0;
572 while (cluster_sectors > 1)
574 cluster_sectors /= 2;
575 dpb->shift++;
578 dpb->num_reserved = 0;
579 dpb->num_FAT = 1;
580 dpb->num_root_entries = 2;
581 dpb->first_data_sector = 2;
582 dpb->num_clusters1 = total_clusters;
583 dpb->sectors_per_FAT = 1;
584 dpb->first_dir_sector = 1;
585 dpb->driver_header = 0;
586 dpb->media_ID = (drivetype == DRIVE_FIXED) ? 0xF8 : 0xF0;
587 dpb->access_flag = 0;
588 dpb->next = 0;
589 dpb->search_cluster1 = 0;
590 dpb->free_clusters_lo = LOWORD(free_clusters);
591 dpb->free_clusters_hi = HIWORD(free_clusters);
592 dpb->mirroring_flags = 0;
593 dpb->info_sector = 0xffff;
594 dpb->spare_boot_sector = 0xffff;
595 dpb->first_cluster_sector = 0;
596 dpb->num_clusters2 = total_clusters;
597 dpb->fat_clusters = 32;
598 dpb->root_cluster = 0;
599 dpb->search_cluster2 = 0;
601 return TRUE;
605 /***********************************************************************
606 * INT21_GetCurrentDirectory
608 * Handler for:
609 * - function 0x47
610 * - subfunction 0x47 of function 0x71
612 static BOOL INT21_GetCurrentDirectory( CONTEXT *context, BOOL islong )
614 char *buffer = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi);
615 BYTE drive = INT21_MapDrive( DL_reg(context) );
616 WCHAR pathW[MAX_PATH];
617 char pathA[MAX_PATH];
618 WCHAR *ptr = pathW;
620 TRACE( "drive %d\n", DL_reg(context) );
622 if (drive == MAX_DOS_DRIVES)
624 SetLastError(ERROR_INVALID_DRIVE);
625 return FALSE;
629 * Grab current directory.
632 if (!GetCurrentDirectoryW( MAX_PATH, pathW )) return FALSE;
634 if (drive_number( pathW[0] ) != drive || pathW[1] != ':')
636 /* cwd is not on the requested drive, get the environment string instead */
638 WCHAR env_var[4];
640 env_var[0] = '=';
641 env_var[1] = 'A' + drive;
642 env_var[2] = ':';
643 env_var[3] = 0;
644 if (!GetEnvironmentVariableW( env_var, pathW, MAX_PATH ))
646 /* return empty path */
647 buffer[0] = 0;
648 return TRUE;
653 * Convert into short format.
656 if (!islong)
658 DWORD result = GetShortPathNameW( pathW, pathW, MAX_PATH );
659 if (!result)
660 return FALSE;
661 if (result > MAX_PATH)
663 WARN( "Short path too long!\n" );
664 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
665 return FALSE;
670 * The returned pathname does not include
671 * the drive letter, colon or leading backslash.
674 if (ptr[0] == '\\')
677 * FIXME: We should probably just strip host part from name...
679 FIXME( "UNC names are not supported.\n" );
680 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
681 return FALSE;
683 else if (!ptr[0] || ptr[1] != ':' || ptr[2] != '\\')
685 WARN( "Path is neither UNC nor DOS path: %s\n",
686 wine_dbgstr_w(ptr) );
687 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
688 return FALSE;
690 else
692 /* Remove drive letter, colon and leading backslash. */
693 ptr += 3;
697 * Convert into OEM string.
700 if (!WideCharToMultiByte(CP_OEMCP, 0, ptr, -1, pathA,
701 MAX_PATH, NULL, NULL))
703 WARN( "Cannot convert path!\n" );
704 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
705 return FALSE;
709 * Success.
712 if (!islong)
714 /* Undocumented success code. */
715 SET_AX( context, 0x0100 );
717 /* Truncate buffer to 64 bytes. */
718 pathA[63] = 0;
721 TRACE( "%c:=%s\n", 'A' + drive, pathA );
723 strcpy( buffer, pathA );
724 return TRUE;
728 /***********************************************************************
729 * INT21_SetCurrentDirectory
731 * Handler for:
732 * - function 0x3b
733 * - subfunction 0x3b of function 0x71
735 static BOOL INT21_SetCurrentDirectory( CONTEXT *context )
737 WCHAR dirW[MAX_PATH];
738 WCHAR env_var[4];
739 DWORD attr;
740 char *dirA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
741 BYTE drive = INT21_GetCurrentDrive();
742 BOOL result;
744 TRACE( "SET CURRENT DIRECTORY %s\n", dirA );
746 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
747 if (!GetFullPathNameW( dirW, MAX_PATH, dirW, NULL )) return FALSE;
749 attr = GetFileAttributesW( dirW );
750 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
752 SetLastError( ERROR_PATH_NOT_FOUND );
753 return FALSE;
756 env_var[0] = '=';
757 env_var[1] = dirW[0];
758 env_var[2] = ':';
759 env_var[3] = 0;
760 result = SetEnvironmentVariableW( env_var, dirW );
762 /* only set current directory if on the current drive */
763 if (result && (drive_number( dirW[0] ) == drive)) result = SetCurrentDirectoryW( dirW );
765 return result;
768 /***********************************************************************
769 * INT21_CreateFile
771 * Handler for:
772 * - function 0x3c
773 * - function 0x3d
774 * - function 0x5b
775 * - function 0x6c
776 * - subfunction 0x6c of function 0x71
778 static BOOL INT21_CreateFile( CONTEXT *context,
779 DWORD pathSegOff,
780 BOOL returnStatus,
781 WORD dosAccessShare,
782 BYTE dosAction )
784 WORD dosStatus;
785 char *pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, pathSegOff);
786 WCHAR pathW[MAX_PATH];
787 DWORD winAccess;
788 DWORD winAttributes;
789 HANDLE winHandle;
790 DWORD winMode;
791 DWORD winSharing;
793 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
794 "create flags=%04x, file=%s.\n",
795 AH_reg(context), dosAction, dosAccessShare, CX_reg(context), pathA );
798 * Application tried to create/open a file whose name
799 * ends with a backslash. This is not allowed.
801 * FIXME: This needs to be validated, especially the return value.
803 if (pathA[strlen(pathA) - 1] == '/')
805 SetLastError( ERROR_FILE_NOT_FOUND );
806 return FALSE;
810 * Convert DOS action flags into Win32 creation disposition parameter.
812 switch(dosAction)
814 case 0x01:
815 winMode = OPEN_EXISTING;
816 break;
817 case 0x02:
818 winMode = TRUNCATE_EXISTING;
819 break;
820 case 0x10:
821 winMode = CREATE_NEW;
822 break;
823 case 0x11:
824 winMode = OPEN_ALWAYS;
825 break;
826 case 0x12:
827 winMode = CREATE_ALWAYS;
828 break;
829 default:
830 SetLastError( ERROR_INVALID_PARAMETER );
831 return FALSE;
835 * Convert DOS access/share flags into Win32 desired access parameter.
837 switch(dosAccessShare & 0x07)
839 case OF_READ:
840 winAccess = GENERIC_READ;
841 break;
842 case OF_WRITE:
843 winAccess = GENERIC_WRITE;
844 break;
845 case OF_READWRITE:
846 winAccess = GENERIC_READ | GENERIC_WRITE;
847 break;
848 case 0x04:
850 * Read-only, do not modify file's last-access time (DOS7).
852 * FIXME: How to prevent modification of last-access time?
854 winAccess = GENERIC_READ;
855 break;
856 default:
857 winAccess = 0;
861 * Convert DOS access/share flags into Win32 share mode parameter.
863 switch(dosAccessShare & 0x70)
865 case OF_SHARE_EXCLUSIVE:
866 winSharing = 0;
867 break;
868 case OF_SHARE_DENY_WRITE:
869 winSharing = FILE_SHARE_READ;
870 break;
871 case OF_SHARE_DENY_READ:
872 winSharing = FILE_SHARE_WRITE;
873 break;
874 case OF_SHARE_DENY_NONE:
875 case OF_SHARE_COMPAT:
876 default:
877 winSharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
881 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
882 * What to do with this bit?
883 * FIXME: Bits in the high byte of dosAccessShare are not supported.
884 * See both function 0x6c and subfunction 0x6c of function 0x71 for
885 * definition of these bits.
889 * Convert DOS create attributes into Win32 flags and attributes parameter.
891 if (winMode == OPEN_EXISTING || winMode == TRUNCATE_EXISTING)
893 winAttributes = 0;
895 else
897 WORD dosAttributes = CX_reg(context);
899 if (dosAttributes & FA_LABEL)
902 * Application tried to create volume label entry.
903 * This is difficult to support so we do not allow it.
905 * FIXME: If volume does not already have a label,
906 * this function is supposed to succeed.
908 SetLastError( ERROR_ACCESS_DENIED );
909 return TRUE;
912 winAttributes = dosAttributes &
913 (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
914 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE);
918 * Open the file.
920 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
922 winHandle = CreateFileW( pathW, winAccess, winSharing, NULL, winMode, winAttributes, 0 );
923 /* DOS allows opening files on a CDROM R/W */
924 if( winHandle == INVALID_HANDLE_VALUE &&
925 (GetLastError() == ERROR_WRITE_PROTECT ||
926 GetLastError() == ERROR_ACCESS_DENIED)) {
927 winHandle = CreateFileW( pathW, winAccess & ~GENERIC_WRITE,
928 winSharing, NULL, winMode, winAttributes, 0 );
931 if (winHandle == INVALID_HANDLE_VALUE)
932 return FALSE;
935 * Determine DOS file status.
937 * 1 = file opened
938 * 2 = file created
939 * 3 = file replaced
941 switch(winMode)
943 case OPEN_EXISTING:
944 dosStatus = 1;
945 break;
946 case TRUNCATE_EXISTING:
947 dosStatus = 3;
948 break;
949 case CREATE_NEW:
950 dosStatus = 2;
951 break;
952 case OPEN_ALWAYS:
953 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 1 : 2;
954 break;
955 case CREATE_ALWAYS:
956 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 3 : 2;
957 break;
958 default:
959 dosStatus = 0;
963 * Return DOS file handle and DOS status.
965 SET_AX( context, Win32HandleToDosFileHandle(winHandle) );
967 if (returnStatus)
968 SET_CX( context, dosStatus );
970 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
971 AX_reg(context), dosStatus );
973 return TRUE;
977 /***********************************************************************
978 * INT21_GetCurrentDTA
980 static BYTE *INT21_GetCurrentDTA( CONTEXT *context )
982 TDB *pTask = GlobalLock16(GetCurrentTask());
984 /* FIXME: This assumes DTA was set correctly! */
985 return CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
986 OFFSETOF(pTask->dta) );
990 /***********************************************************************
991 * INT21_OpenFileUsingFCB
993 * Handler for function 0x0f.
995 * PARAMS
996 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
998 * RETURNS (in AL)
999 * 0x00: successful
1000 * 0xff: failed
1002 * NOTES
1003 * Opens a FCB file for read/write in compatibility mode. Upon calling
1004 * the FCB must have the drive_number, file_name, and file_extension
1005 * fields filled and all other bytes cleared.
1007 static void INT21_OpenFileUsingFCB( CONTEXT *context )
1009 struct FCB *fcb;
1010 struct XFCB *xfcb;
1011 char file_path[16];
1012 char *pos;
1013 HANDLE handle;
1014 HFILE16 hfile16;
1015 BY_HANDLE_FILE_INFORMATION info;
1016 BYTE AL_result;
1018 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1019 if (fcb->drive_number == 0xff) {
1020 xfcb = (struct XFCB *) fcb;
1021 fcb = (struct FCB *) xfcb->fcb;
1022 } /* if */
1024 AL_result = 0;
1025 file_path[0] = 'A' + INT21_MapDrive( fcb->drive_number );
1027 if (AL_result == 0) {
1028 file_path[1] = ':';
1029 pos = &file_path[2];
1030 memcpy(pos, fcb->file_name, 8);
1031 pos[8] = ' ';
1032 pos[9] = '\0';
1033 pos = strchr(pos, ' ');
1034 *pos = '.';
1035 pos++;
1036 memcpy(pos, fcb->file_extension, 3);
1037 pos[3] = ' ';
1038 pos[4] = '\0';
1039 pos = strchr(pos, ' ');
1040 *pos = '\0';
1042 handle = (HANDLE) _lopen(file_path, OF_READWRITE);
1043 if (handle == INVALID_HANDLE_VALUE) {
1044 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path);
1045 AL_result = 0xff; /* failed */
1046 } else {
1047 hfile16 = Win32HandleToDosFileHandle(handle);
1048 if (hfile16 == HFILE_ERROR16) {
1049 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle);
1050 CloseHandle(handle);
1051 AL_result = 0xff; /* failed */
1052 } else if (hfile16 > 255) {
1053 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16, file_path);
1054 _lclose16(hfile16);
1055 AL_result = 0xff; /* failed */
1056 } else {
1057 if (!GetFileInformationByHandle(handle, &info)) {
1058 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1059 hfile16, handle, file_path);
1060 _lclose16(hfile16);
1061 AL_result = 0xff; /* failed */
1062 } else {
1063 fcb->drive_number = file_path[0] - 'A' + 1;
1064 fcb->current_block_number = 0;
1065 fcb->logical_record_size = 128;
1066 fcb->file_size = info.nFileSizeLow;
1067 FileTimeToDosDateTime(&info.ftLastWriteTime,
1068 &fcb->date_of_last_write, &fcb->time_of_last_write);
1069 fcb->file_number = hfile16;
1070 fcb->attributes = 0xc2;
1071 fcb->starting_cluster = 0; /* don't know correct init value */
1072 fcb->sequence_number = 0; /* don't know correct init value */
1073 fcb->file_attributes = info.dwFileAttributes;
1074 /* The following fields are not initialized */
1075 /* by the native function: */
1076 /* unused */
1077 /* record_within_current_block */
1078 /* random_access_record_number */
1080 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1081 file_path, hfile16, handle);
1082 AL_result = 0x00; /* successful */
1083 } /* if */
1084 } /* if */
1085 } /* if */
1086 } /* if */
1087 SET_AL(context, AL_result);
1091 /***********************************************************************
1092 * INT21_CloseFileUsingFCB
1094 * Handler for function 0x10.
1096 * PARAMS
1097 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1099 * RETURNS (in AL)
1100 * 0x00: successful
1101 * 0xff: failed
1103 * NOTES
1104 * Closes a FCB file.
1106 static void INT21_CloseFileUsingFCB( CONTEXT *context )
1108 struct FCB *fcb;
1109 struct XFCB *xfcb;
1110 BYTE AL_result;
1112 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1113 if (fcb->drive_number == 0xff) {
1114 xfcb = (struct XFCB *) fcb;
1115 fcb = (struct FCB *) xfcb->fcb;
1116 } /* if */
1118 if (_lclose16((HFILE16) fcb->file_number) != 0) {
1119 TRACE("_lclose16(%d) failed\n", fcb->file_number);
1120 AL_result = 0xff; /* failed */
1121 } else {
1122 TRACE("successful closed file %d\n", fcb->file_number);
1123 AL_result = 0x00; /* successful */
1124 } /* if */
1125 SET_AL(context, AL_result);
1129 /***********************************************************************
1130 * INT21_SequentialReadFromFCB
1132 * Handler for function 0x14.
1134 * PARAMS
1135 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1137 * RETURNS (in AL)
1138 * 0: successful
1139 * 1: end of file, no data read
1140 * 2: segment wrap in DTA, no data read (not returned now)
1141 * 3: end of file, partial record read
1143 * NOTES
1144 * Reads a record with the size FCB->logical_record_size from the FCB
1145 * to the disk transfer area. The position of the record is specified
1146 * with FCB->current_block_number and FCB->record_within_current_block.
1147 * Then FCB->current_block_number and FCB->record_within_current_block
1148 * are updated to point to the next record. If a partial record is
1149 * read, it is filled with zeros up to the FCB->logical_record_size.
1151 static void INT21_SequentialReadFromFCB( CONTEXT *context )
1153 struct FCB *fcb;
1154 struct XFCB *xfcb;
1155 HANDLE handle;
1156 DWORD record_number;
1157 DWORD position;
1158 BYTE *disk_transfer_area;
1159 UINT bytes_read;
1160 BYTE AL_result;
1162 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1163 if (fcb->drive_number == 0xff) {
1164 xfcb = (struct XFCB *) fcb;
1165 fcb = (struct FCB *) xfcb->fcb;
1166 } /* if */
1168 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1169 if (handle == INVALID_HANDLE_VALUE) {
1170 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1171 fcb->file_number);
1172 AL_result = 0x01; /* end of file, no data read */
1173 } else {
1174 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1175 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1176 if (position != record_number * fcb->logical_record_size) {
1177 TRACE("seek(%d, %ld, 0) failed with %lu\n",
1178 fcb->file_number, record_number * fcb->logical_record_size, position);
1179 AL_result = 0x01; /* end of file, no data read */
1180 } else {
1181 disk_transfer_area = INT21_GetCurrentDTA(context);
1182 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1183 if (bytes_read != fcb->logical_record_size) {
1184 TRACE("_lread(%d, %p, %d) failed with %d\n",
1185 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1186 if (bytes_read == 0) {
1187 AL_result = 0x01; /* end of file, no data read */
1188 } else {
1189 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1190 AL_result = 0x03; /* end of file, partial record read */
1191 } /* if */
1192 } else {
1193 TRACE("successful read %d bytes from record %ld (position %lu) of file %d (handle %p)\n",
1194 bytes_read, record_number, position, fcb->file_number, handle);
1195 AL_result = 0x00; /* successful */
1196 } /* if */
1197 } /* if */
1198 } /* if */
1199 if (AL_result == 0x00 || AL_result == 0x03) {
1200 if (fcb->record_within_current_block == 127) {
1201 fcb->record_within_current_block = 0;
1202 fcb->current_block_number++;
1203 } else {
1204 fcb->record_within_current_block++;
1205 } /* if */
1206 } /* if */
1207 SET_AL(context, AL_result);
1211 /***********************************************************************
1212 * INT21_SequentialWriteToFCB
1214 * Handler for function 0x15.
1216 * PARAMS
1217 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1219 * RETURNS (in AL)
1220 * 0: successful
1221 * 1: disk full
1222 * 2: segment wrap in DTA (not returned now)
1224 * NOTES
1225 * Writes a record with the size FCB->logical_record_size from the disk
1226 * transfer area to the FCB. The position of the record is specified
1227 * with FCB->current_block_number and FCB->record_within_current_block.
1228 * Then FCB->current_block_number and FCB->record_within_current_block
1229 * are updated to point to the next record.
1231 static void INT21_SequentialWriteToFCB( CONTEXT *context )
1233 struct FCB *fcb;
1234 struct XFCB *xfcb;
1235 HANDLE handle;
1236 DWORD record_number;
1237 DWORD position;
1238 BYTE *disk_transfer_area;
1239 UINT bytes_written;
1240 BYTE AL_result;
1242 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1243 if (fcb->drive_number == 0xff) {
1244 xfcb = (struct XFCB *) fcb;
1245 fcb = (struct FCB *) xfcb->fcb;
1246 } /* if */
1248 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1249 if (handle == INVALID_HANDLE_VALUE) {
1250 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1251 fcb->file_number);
1252 AL_result = 0x01; /* disk full */
1253 } else {
1254 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1255 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1256 if (position != record_number * fcb->logical_record_size) {
1257 TRACE("seek(%d, %ld, 0) failed with %lu\n",
1258 fcb->file_number, record_number * fcb->logical_record_size, position);
1259 AL_result = 0x01; /* disk full */
1260 } else {
1261 disk_transfer_area = INT21_GetCurrentDTA(context);
1262 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, fcb->logical_record_size);
1263 if (bytes_written != fcb->logical_record_size) {
1264 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1265 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1266 AL_result = 0x01; /* disk full */
1267 } else {
1268 TRACE("successful written %d bytes from record %ld (position %lu) of file %d (handle %p)\n",
1269 bytes_written, record_number, position, fcb->file_number, handle);
1270 AL_result = 0x00; /* successful */
1271 } /* if */
1272 } /* if */
1273 } /* if */
1274 if (AL_result == 0x00) {
1275 if (fcb->record_within_current_block == 127) {
1276 fcb->record_within_current_block = 0;
1277 fcb->current_block_number++;
1278 } else {
1279 fcb->record_within_current_block++;
1280 } /* if */
1281 } /* if */
1282 SET_AL(context, AL_result);
1286 /***********************************************************************
1287 * INT21_ReadRandomRecordFromFCB
1289 * Handler for function 0x21.
1291 * PARAMS
1292 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1294 * RETURNS (in AL)
1295 * 0: successful
1296 * 1: end of file, no data read
1297 * 2: segment wrap in DTA, no data read (not returned now)
1298 * 3: end of file, partial record read
1300 * NOTES
1301 * Reads a record with the size FCB->logical_record_size from
1302 * the FCB to the disk transfer area. The position of the record
1303 * is specified with FCB->random_access_record_number. The
1304 * FCB->random_access_record_number is not updated. If a partial record
1305 * is read, it is filled with zeros up to the FCB->logical_record_size.
1307 static void INT21_ReadRandomRecordFromFCB( CONTEXT *context )
1309 struct FCB *fcb;
1310 struct XFCB *xfcb;
1311 HANDLE handle;
1312 DWORD record_number;
1313 DWORD position;
1314 BYTE *disk_transfer_area;
1315 UINT bytes_read;
1316 BYTE AL_result;
1318 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1319 if (fcb->drive_number == 0xff) {
1320 xfcb = (struct XFCB *) fcb;
1321 fcb = (struct FCB *) xfcb->fcb;
1322 } /* if */
1324 memcpy(&record_number, fcb->random_access_record_number, 4);
1325 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1326 if (handle == INVALID_HANDLE_VALUE) {
1327 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1328 fcb->file_number);
1329 AL_result = 0x01; /* end of file, no data read */
1330 } else {
1331 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1332 if (position != record_number * fcb->logical_record_size) {
1333 TRACE("seek(%d, %ld, 0) failed with %lu\n",
1334 fcb->file_number, record_number * fcb->logical_record_size, position);
1335 AL_result = 0x01; /* end of file, no data read */
1336 } else {
1337 disk_transfer_area = INT21_GetCurrentDTA(context);
1338 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1339 if (bytes_read != fcb->logical_record_size) {
1340 TRACE("_lread(%d, %p, %d) failed with %d\n",
1341 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1342 if (bytes_read == 0) {
1343 AL_result = 0x01; /* end of file, no data read */
1344 } else {
1345 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1346 AL_result = 0x03; /* end of file, partial record read */
1347 } /* if */
1348 } else {
1349 TRACE("successful read %d bytes from record %ld (position %lu) of file %d (handle %p)\n",
1350 bytes_read, record_number, position, fcb->file_number, handle);
1351 AL_result = 0x00; /* successful */
1352 } /* if */
1353 } /* if */
1354 } /* if */
1355 fcb->current_block_number = record_number / 128;
1356 fcb->record_within_current_block = record_number % 128;
1357 SET_AL(context, AL_result);
1361 /***********************************************************************
1362 * INT21_WriteRandomRecordToFCB
1364 * Handler for function 0x22.
1366 * PARAMS
1367 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1369 * RETURNS (in AL)
1370 * 0: successful
1371 * 1: disk full
1372 * 2: segment wrap in DTA (not returned now)
1374 * NOTES
1375 * Writes a record with the size FCB->logical_record_size from
1376 * the disk transfer area to the FCB. The position of the record
1377 * is specified with FCB->random_access_record_number. The
1378 * FCB->random_access_record_number is not updated.
1380 static void INT21_WriteRandomRecordToFCB( CONTEXT *context )
1382 struct FCB *fcb;
1383 struct XFCB *xfcb;
1384 HANDLE handle;
1385 DWORD record_number;
1386 DWORD position;
1387 BYTE *disk_transfer_area;
1388 UINT bytes_written;
1389 BYTE AL_result;
1391 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1392 if (fcb->drive_number == 0xff) {
1393 xfcb = (struct XFCB *) fcb;
1394 fcb = (struct FCB *) xfcb->fcb;
1395 } /* if */
1397 memcpy(&record_number, fcb->random_access_record_number, 4);
1398 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1399 if (handle == INVALID_HANDLE_VALUE) {
1400 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1401 fcb->file_number);
1402 AL_result = 0x01; /* disk full */
1403 } else {
1404 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1405 if (position != record_number * fcb->logical_record_size) {
1406 TRACE("seek(%d, %ld, 0) failed with %lu\n",
1407 fcb->file_number, record_number * fcb->logical_record_size, position);
1408 AL_result = 0x01; /* disk full */
1409 } else {
1410 disk_transfer_area = INT21_GetCurrentDTA(context);
1411 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, fcb->logical_record_size);
1412 if (bytes_written != fcb->logical_record_size) {
1413 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1414 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1415 AL_result = 0x01; /* disk full */
1416 } else {
1417 TRACE("successful written %d bytes from record %ld (position %lu) of file %d (handle %p)\n",
1418 bytes_written, record_number, position, fcb->file_number, handle);
1419 AL_result = 0x00; /* successful */
1420 } /* if */
1421 } /* if */
1422 } /* if */
1423 fcb->current_block_number = record_number / 128;
1424 fcb->record_within_current_block = record_number % 128;
1425 SET_AL(context, AL_result);
1429 /***********************************************************************
1430 * INT21_RandomBlockReadFromFCB
1432 * Handler for function 0x27.
1434 * PARAMS
1435 * CX [I/O] Number of records to read
1436 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1438 * RETURNS (in AL)
1439 * 0: successful
1440 * 1: end of file, no data read
1441 * 2: segment wrap in DTA, no data read (not returned now)
1442 * 3: end of file, partial record read
1444 * NOTES
1445 * Reads several records with the size FCB->logical_record_size from
1446 * the FCB to the disk transfer area. The number of records to be
1447 * read is specified in the CX register. The position of the first
1448 * record is specified with FCB->random_access_record_number. The
1449 * FCB->random_access_record_number, the FCB->current_block_number
1450 * and FCB->record_within_current_block are updated to point to the
1451 * next record after the records read. If a partial record is read,
1452 * it is filled with zeros up to the FCB->logical_record_size. The
1453 * CX register is set to the number of successfully read records.
1455 static void INT21_RandomBlockReadFromFCB( CONTEXT *context )
1457 struct FCB *fcb;
1458 struct XFCB *xfcb;
1459 HANDLE handle;
1460 DWORD record_number;
1461 DWORD position;
1462 BYTE *disk_transfer_area;
1463 UINT records_requested;
1464 UINT bytes_requested;
1465 UINT bytes_read;
1466 UINT records_read;
1467 BYTE AL_result;
1469 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1470 if (fcb->drive_number == 0xff) {
1471 xfcb = (struct XFCB *) fcb;
1472 fcb = (struct FCB *) xfcb->fcb;
1473 } /* if */
1475 memcpy(&record_number, fcb->random_access_record_number, 4);
1476 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1477 if (handle == INVALID_HANDLE_VALUE) {
1478 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1479 fcb->file_number);
1480 records_read = 0;
1481 AL_result = 0x01; /* end of file, no data read */
1482 } else {
1483 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1484 if (position != record_number * fcb->logical_record_size) {
1485 TRACE("seek(%d, %ld, 0) failed with %lu\n",
1486 fcb->file_number, record_number * fcb->logical_record_size, position);
1487 records_read = 0;
1488 AL_result = 0x01; /* end of file, no data read */
1489 } else {
1490 disk_transfer_area = INT21_GetCurrentDTA(context);
1491 records_requested = CX_reg(context);
1492 bytes_requested = records_requested * fcb->logical_record_size;
1493 bytes_read = _lread((HFILE) handle, disk_transfer_area, bytes_requested);
1494 if (bytes_read != bytes_requested) {
1495 TRACE("_lread(%d, %p, %d) failed with %d\n",
1496 fcb->file_number, disk_transfer_area, bytes_requested, bytes_read);
1497 records_read = bytes_read / fcb->logical_record_size;
1498 if (bytes_read % fcb->logical_record_size == 0) {
1499 AL_result = 0x01; /* end of file, no data read */
1500 } else {
1501 records_read++;
1502 memset(&disk_transfer_area[bytes_read], 0, records_read * fcb->logical_record_size - bytes_read);
1503 AL_result = 0x03; /* end of file, partial record read */
1504 } /* if */
1505 } else {
1506 TRACE("successful read %d bytes from record %ld (position %lu) of file %d (handle %p)\n",
1507 bytes_read, record_number, position, fcb->file_number, handle);
1508 records_read = records_requested;
1509 AL_result = 0x00; /* successful */
1510 } /* if */
1511 } /* if */
1512 } /* if */
1513 record_number += records_read;
1514 memcpy(fcb->random_access_record_number, &record_number, 4);
1515 fcb->current_block_number = record_number / 128;
1516 fcb->record_within_current_block = record_number % 128;
1517 SET_CX(context, records_read);
1518 SET_AL(context, AL_result);
1522 /***********************************************************************
1523 * INT21_RandomBlockWriteToFCB
1525 * Handler for function 0x28.
1527 * PARAMS
1528 * CX [I/O] Number of records to write
1529 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1531 * RETURNS (in AL)
1532 * 0: successful
1533 * 1: disk full
1534 * 2: segment wrap in DTA (not returned now)
1536 * NOTES
1537 * Writes several records with the size FCB->logical_record_size from
1538 * the disk transfer area to the FCB. The number of records to be
1539 * written is specified in the CX register. The position of the first
1540 * record is specified with FCB->random_access_record_number. The
1541 * FCB->random_access_record_number, the FCB->current_block_number
1542 * and FCB->record_within_current_block are updated to point to the
1543 * next record after the records written. The CX register is set to
1544 * the number of successfully written records.
1546 static void INT21_RandomBlockWriteToFCB( CONTEXT *context )
1548 struct FCB *fcb;
1549 struct XFCB *xfcb;
1550 HANDLE handle;
1551 DWORD record_number;
1552 DWORD position;
1553 BYTE *disk_transfer_area;
1554 UINT records_requested;
1555 UINT bytes_requested;
1556 UINT bytes_written;
1557 UINT records_written;
1558 BYTE AL_result;
1560 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1561 if (fcb->drive_number == 0xff) {
1562 xfcb = (struct XFCB *) fcb;
1563 fcb = (struct FCB *) xfcb->fcb;
1564 } /* if */
1566 memcpy(&record_number, fcb->random_access_record_number, 4);
1567 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1568 if (handle == INVALID_HANDLE_VALUE) {
1569 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1570 fcb->file_number);
1571 records_written = 0;
1572 AL_result = 0x01; /* disk full */
1573 } else {
1574 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1575 if (position != record_number * fcb->logical_record_size) {
1576 TRACE("seek(%d, %ld, 0) failed with %lu\n",
1577 fcb->file_number, record_number * fcb->logical_record_size, position);
1578 records_written = 0;
1579 AL_result = 0x01; /* disk full */
1580 } else {
1581 disk_transfer_area = INT21_GetCurrentDTA(context);
1582 records_requested = CX_reg(context);
1583 bytes_requested = records_requested * fcb->logical_record_size;
1584 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, bytes_requested);
1585 if (bytes_written != bytes_requested) {
1586 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1587 fcb->file_number, disk_transfer_area, bytes_requested, bytes_written);
1588 records_written = bytes_written / fcb->logical_record_size;
1589 AL_result = 0x01; /* disk full */
1590 } else {
1591 TRACE("successful write %d bytes from record %ld (position %lu) of file %d (handle %p)\n",
1592 bytes_written, record_number, position, fcb->file_number, handle);
1593 records_written = records_requested;
1594 AL_result = 0x00; /* successful */
1595 } /* if */
1596 } /* if */
1597 } /* if */
1598 record_number += records_written;
1599 memcpy(fcb->random_access_record_number, &record_number, 4);
1600 fcb->current_block_number = record_number / 128;
1601 fcb->record_within_current_block = record_number % 128;
1602 SET_CX(context, records_written);
1603 SET_AL(context, AL_result);
1607 /***********************************************************************
1608 * INT21_CreateDirectory
1610 * Handler for:
1611 * - function 0x39
1612 * - subfunction 0x39 of function 0x71
1613 * - subfunction 0xff of function 0x43 (CL == 0x39)
1615 static BOOL INT21_CreateDirectory( CONTEXT *context )
1617 WCHAR dirW[MAX_PATH];
1618 char *dirA = CTX_SEG_OFF_TO_LIN(context,
1619 context->SegDs,
1620 context->Edx);
1622 TRACE( "CREATE DIRECTORY %s\n", dirA );
1624 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
1626 if (CreateDirectoryW(dirW, NULL))
1627 return TRUE;
1630 * FIXME: CreateDirectory's LastErrors will clash with the ones
1631 * used by DOS. AH=39 only returns 3 (path not found) and
1632 * 5 (access denied), while CreateDirectory return several
1633 * ones. Remap some of them. -Marcus
1635 switch (GetLastError())
1637 case ERROR_ALREADY_EXISTS:
1638 case ERROR_FILENAME_EXCED_RANGE:
1639 case ERROR_DISK_FULL:
1640 SetLastError(ERROR_ACCESS_DENIED);
1641 break;
1642 default:
1643 break;
1646 return FALSE;
1650 /***********************************************************************
1651 * INT21_ExtendedCountryInformation
1653 * Handler for function 0x65.
1655 static void INT21_ExtendedCountryInformation( CONTEXT *context )
1657 BYTE *dataptr = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
1658 BYTE buffsize = CX_reg (context);
1660 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1661 AL_reg(context) );
1664 * Check subfunctions that are passed country and code page.
1666 if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
1668 WORD country = DX_reg(context);
1669 WORD codepage = BX_reg(context);
1671 if (country != 0xffff && country != INT21_GetSystemCountryCode())
1672 FIXME( "Requested info on non-default country %04x\n", country );
1674 if (codepage != 0xffff && codepage != GetOEMCP())
1675 FIXME( "Requested info on non-default code page %04x\n", codepage );
1678 switch (AL_reg(context)) {
1679 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1680 INT_BARF( context, 0x21 );
1681 SET_CFLAG( context );
1682 break;
1684 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1685 TRACE( "Get general internationalization info\n" );
1686 dataptr[0] = 0x01; /* Info ID */
1687 *(WORD*)(dataptr+1) = 38; /* Size of the following info */
1688 *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
1689 *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
1690 /* FIXME: fill buffer partially up to buffsize bytes*/
1691 if (buffsize >= 0x29){
1692 INT21_FillCountryInformation( dataptr + 7 );
1693 SET_CX( context, 0x29 ); /* Size of returned info */
1694 }else{
1695 SET_CX( context, 0x07 ); /* Size of returned info */
1697 break;
1699 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1700 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1701 TRACE( "Get pointer to uppercase table\n" );
1702 dataptr[0] = AL_reg(context); /* Info ID */
1703 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1704 offsetof(INT21_HEAP, uppercase_size) );
1705 SET_CX( context, 5 ); /* Size of returned info */
1706 break;
1708 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1709 TRACE( "Get pointer to lowercase table\n" );
1710 dataptr[0] = 0x03; /* Info ID */
1711 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1712 offsetof(INT21_HEAP, lowercase_size) );
1713 SET_CX( context, 5 ); /* Size of returned info */
1714 break;
1716 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1717 TRACE("Get pointer to filename terminator table\n");
1718 dataptr[0] = 0x05; /* Info ID */
1719 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1720 offsetof(INT21_HEAP, filename_size) );
1721 SET_CX( context, 5 ); /* Size of returned info */
1722 break;
1724 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1725 TRACE("Get pointer to collating sequence table\n");
1726 dataptr[0] = 0x06; /* Info ID */
1727 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1728 offsetof(INT21_HEAP, collating_size) );
1729 SET_CX( context, 5 ); /* Size of returned info */
1730 break;
1732 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1733 TRACE("Get pointer to DBCS lead byte table\n");
1734 dataptr[0] = 0x07; /* Info ID */
1735 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1736 offsetof(INT21_HEAP, dbcs_size) );
1737 SET_CX( context, 5 ); /* Size of returned info */
1738 break;
1740 case 0x20: /* CAPITALIZE CHARACTER */
1741 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1742 TRACE("Convert char to uppercase\n");
1743 SET_DL( context, toupper(DL_reg(context)) );
1744 break;
1746 case 0x21: /* CAPITALIZE STRING */
1747 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1748 TRACE("Convert string to uppercase with length\n");
1750 char *ptr = CTX_SEG_OFF_TO_LIN( context, context->SegDs,
1751 context->Edx );
1752 WORD len = CX_reg(context);
1753 while (len--) { *ptr = toupper(*ptr); ptr++; }
1755 break;
1757 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1758 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1759 TRACE("Convert ASCIIZ string to uppercase\n");
1761 char *p = CTX_SEG_OFF_TO_LIN( context, context->SegDs, context->Edx );
1762 for ( ; *p; p++) *p = toupper(*p);
1764 break;
1766 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1767 INT_BARF( context, 0x21 );
1768 SET_CFLAG( context );
1769 break;
1771 default:
1772 INT_BARF( context, 0x21 );
1773 SET_CFLAG(context);
1774 break;
1779 /***********************************************************************
1780 * INT21_FileAttributes
1782 * Handler for:
1783 * - function 0x43
1784 * - subfunction 0x43 of function 0x71
1786 static BOOL INT21_FileAttributes( CONTEXT *context,
1787 BYTE subfunction,
1788 BOOL islong )
1790 WCHAR fileW[MAX_PATH];
1791 char *fileA = CTX_SEG_OFF_TO_LIN(context,
1792 context->SegDs,
1793 context->Edx);
1794 HANDLE handle;
1795 BOOL status;
1796 FILETIME filetime;
1797 DWORD result;
1798 WORD date, time;
1799 int len;
1801 switch (subfunction)
1803 case 0x00: /* GET FILE ATTRIBUTES */
1804 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA );
1805 len = MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1807 /* Winbench 96 Disk Test fails if we don't complain
1808 * about a filename that ends in \
1810 if (!len || (fileW[len-1] == '/') || (fileW[len-1] == '\\'))
1811 return FALSE;
1813 result = GetFileAttributesW( fileW );
1814 if (result == INVALID_FILE_ATTRIBUTES)
1815 return FALSE;
1816 else
1818 SET_CX( context, (WORD)result );
1819 if (!islong)
1820 SET_AX( context, (WORD)result ); /* DR DOS */
1822 break;
1824 case 0x01: /* SET FILE ATTRIBUTES */
1825 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
1826 CX_reg(context), fileA );
1827 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1829 if (!SetFileAttributesW( fileW, CX_reg(context) ))
1830 return FALSE;
1831 break;
1833 case 0x02: /* GET COMPRESSED FILE SIZE */
1834 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA );
1835 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1837 result = GetCompressedFileSizeW( fileW, NULL );
1838 if (result == INVALID_FILE_SIZE)
1839 return FALSE;
1840 else
1842 SET_AX( context, LOWORD(result) );
1843 SET_DX( context, HIWORD(result) );
1845 break;
1847 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
1848 if (!islong)
1849 INT_BARF( context, 0x21 );
1850 else
1852 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
1853 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1855 handle = CreateFileW( fileW, GENERIC_WRITE,
1856 FILE_SHARE_READ | FILE_SHARE_WRITE,
1857 NULL, OPEN_EXISTING, 0, 0 );
1858 if (handle == INVALID_HANDLE_VALUE)
1859 return FALSE;
1861 DosDateTimeToFileTime( DI_reg(context),
1862 CX_reg(context),
1863 &filetime );
1864 status = SetFileTime( handle, NULL, NULL, &filetime );
1866 CloseHandle( handle );
1867 return status;
1869 break;
1871 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
1872 if (!islong)
1873 INT_BARF( context, 0x21 );
1874 else
1876 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
1877 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1879 handle = CreateFileW( fileW, GENERIC_READ,
1880 FILE_SHARE_READ | FILE_SHARE_WRITE,
1881 NULL, OPEN_EXISTING, 0, 0 );
1882 if (handle == INVALID_HANDLE_VALUE)
1883 return FALSE;
1885 status = GetFileTime( handle, NULL, NULL, &filetime );
1886 if (status)
1888 FileTimeToDosDateTime( &filetime, &date, &time );
1889 SET_DI( context, date );
1890 SET_CX( context, time );
1893 CloseHandle( handle );
1894 return status;
1896 break;
1898 case 0x05: /* SET FILE LAST ACCESS DATE */
1899 if (!islong)
1900 INT_BARF( context, 0x21 );
1901 else
1903 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA );
1904 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1906 handle = CreateFileW( fileW, GENERIC_WRITE,
1907 FILE_SHARE_READ | FILE_SHARE_WRITE,
1908 NULL, OPEN_EXISTING, 0, 0 );
1909 if (handle == INVALID_HANDLE_VALUE)
1910 return FALSE;
1912 DosDateTimeToFileTime( DI_reg(context),
1914 &filetime );
1915 status = SetFileTime( handle, NULL, &filetime, NULL );
1917 CloseHandle( handle );
1918 return status;
1920 break;
1922 case 0x06: /* GET FILE LAST ACCESS DATE */
1923 if (!islong)
1924 INT_BARF( context, 0x21 );
1925 else
1927 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA );
1928 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1930 handle = CreateFileW( fileW, GENERIC_READ,
1931 FILE_SHARE_READ | FILE_SHARE_WRITE,
1932 NULL, OPEN_EXISTING, 0, 0 );
1933 if (handle == INVALID_HANDLE_VALUE)
1934 return FALSE;
1936 status = GetFileTime( handle, NULL, &filetime, NULL );
1937 if (status)
1939 FileTimeToDosDateTime( &filetime, &date, NULL );
1940 SET_DI( context, date );
1943 CloseHandle( handle );
1944 return status;
1946 break;
1948 case 0x07: /* SET FILE CREATION DATE AND TIME */
1949 if (!islong)
1950 INT_BARF( context, 0x21 );
1951 else
1953 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA );
1954 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1956 handle = CreateFileW( fileW, GENERIC_WRITE,
1957 FILE_SHARE_READ | FILE_SHARE_WRITE,
1958 NULL, OPEN_EXISTING, 0, 0 );
1959 if (handle == INVALID_HANDLE_VALUE)
1960 return FALSE;
1963 * FIXME: SI has number of 10-millisecond units past time in CX.
1965 DosDateTimeToFileTime( DI_reg(context),
1966 CX_reg(context),
1967 &filetime );
1968 status = SetFileTime( handle, &filetime, NULL, NULL );
1970 CloseHandle( handle );
1971 return status;
1973 break;
1975 case 0x08: /* GET FILE CREATION DATE AND TIME */
1976 if (!islong)
1977 INT_BARF( context, 0x21 );
1978 else
1980 TRACE( "GET FILE CREATION DATE AND TIME, file %s\n", fileA );
1981 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1983 handle = CreateFileW( fileW, GENERIC_READ,
1984 FILE_SHARE_READ | FILE_SHARE_WRITE,
1985 NULL, OPEN_EXISTING, 0, 0 );
1986 if (handle == INVALID_HANDLE_VALUE)
1987 return FALSE;
1989 status = GetFileTime( handle, &filetime, NULL, NULL );
1990 if (status)
1992 FileTimeToDosDateTime( &filetime, &date, &time );
1993 SET_DI( context, date );
1994 SET_CX( context, time );
1996 * FIXME: SI has number of 10-millisecond units past
1997 * time in CX.
1999 SET_SI( context, 0 );
2002 CloseHandle(handle);
2003 return status;
2005 break;
2007 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2008 if (islong || context->Ebp != 0x5053)
2009 INT_BARF( context, 0x21 );
2010 else
2012 switch(CL_reg(context))
2014 case 0x39:
2015 if (!INT21_CreateDirectory( context ))
2016 return FALSE;
2017 break;
2019 case 0x56:
2020 if (!INT21_RenameFile( context ))
2021 return FALSE;
2022 break;
2024 default:
2025 INT_BARF( context, 0x21 );
2028 break;
2030 default:
2031 INT_BARF( context, 0x21 );
2034 return TRUE;
2038 /***********************************************************************
2039 * INT21_FileDateTime
2041 * Handler for function 0x57.
2043 static BOOL INT21_FileDateTime( CONTEXT *context )
2045 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2046 FILETIME filetime;
2047 WORD date, time;
2049 switch (AL_reg(context)) {
2050 case 0x00: /* Get last-written stamp */
2051 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2052 BX_reg(context) );
2054 if (!GetFileTime( handle, NULL, NULL, &filetime ))
2055 return FALSE;
2056 FileTimeToDosDateTime( &filetime, &date, &time );
2057 SET_DX( context, date );
2058 SET_CX( context, time );
2059 break;
2062 case 0x01: /* Set last-written stamp */
2063 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2064 BX_reg(context) );
2066 DosDateTimeToFileTime( DX_reg(context),
2067 CX_reg(context),
2068 &filetime );
2069 if (!SetFileTime( handle, NULL, NULL, &filetime ))
2070 return FALSE;
2071 break;
2074 case 0x04: /* Get last access stamp, DOS 7 */
2075 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2076 BX_reg(context) );
2078 if (!GetFileTime( handle, NULL, &filetime, NULL ))
2079 return FALSE;
2080 FileTimeToDosDateTime( &filetime, &date, &time );
2081 SET_DX( context, date );
2082 SET_CX( context, time );
2083 break;
2086 case 0x05: /* Set last access stamp, DOS 7 */
2087 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2088 BX_reg(context) );
2090 DosDateTimeToFileTime( DX_reg(context),
2091 CX_reg(context),
2092 &filetime );
2093 if (!SetFileTime( handle, NULL, &filetime, NULL ))
2094 return FALSE;
2095 break;
2098 case 0x06: /* Get creation stamp, DOS 7 */
2099 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2100 BX_reg(context) );
2102 if (!GetFileTime( handle, &filetime, NULL, NULL ))
2103 return FALSE;
2104 FileTimeToDosDateTime( &filetime, &date, &time );
2105 SET_DX( context, date );
2106 SET_CX( context, time );
2108 * FIXME: SI has number of 10-millisecond units past time in CX.
2110 SET_SI( context, 0 );
2111 break;
2114 case 0x07: /* Set creation stamp, DOS 7 */
2115 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2116 BX_reg(context) );
2119 * FIXME: SI has number of 10-millisecond units past time in CX.
2121 DosDateTimeToFileTime( DX_reg(context),
2122 CX_reg(context),
2123 &filetime );
2124 if (!SetFileTime( handle, &filetime, NULL, NULL ))
2125 return FALSE;
2126 break;
2129 default:
2130 INT_BARF( context, 0x21 );
2131 break;
2134 return TRUE;
2138 /***********************************************************************
2139 * INT21_GetPSP
2141 * Handler for functions 0x51 and 0x62.
2143 static void INT21_GetPSP( CONTEXT *context )
2145 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
2147 SET_BX( context, LOWORD(GetCurrentPDB16()) );
2150 static inline void setword( BYTE *ptr, WORD w )
2152 ptr[0] = (BYTE)w;
2153 ptr[1] = (BYTE)(w >> 8);
2156 static void CreateBPB(int drive, BYTE *data, BOOL16 limited)
2157 /* limited == TRUE is used with INT 0x21/0x440d */
2159 /* FIXME: we're forcing some values without checking that those are valid */
2160 if (drive > 1)
2162 setword(data, 512);
2163 data[2] = 2;
2164 setword(&data[3], 0);
2165 data[5] = 2;
2166 setword(&data[6], 240);
2167 setword(&data[8], 64000);
2168 data[0x0a] = 0xf8;
2169 setword(&data[0x0b], 40);
2170 setword(&data[0x0d], 56);
2171 setword(&data[0x0f], 2);
2172 setword(&data[0x11], 0);
2173 if (!limited)
2175 setword(&data[0x1f], 800);
2176 data[0x21] = 5;
2177 setword(&data[0x22], 1);
2180 else
2181 { /* 1.44mb */
2182 setword(data, 512);
2183 data[2] = 2;
2184 setword(&data[3], 0);
2185 data[5] = 2;
2186 setword(&data[6], 240);
2187 setword(&data[8], 2880);
2188 data[0x0a] = 0xf8;
2189 setword(&data[0x0b], 6);
2190 setword(&data[0x0d], 18);
2191 setword(&data[0x0f], 2);
2192 setword(&data[0x11], 0);
2193 if (!limited)
2195 setword(&data[0x1f], 80);
2196 data[0x21] = 7;
2197 setword(&data[0x22], 2);
2202 static inline DWORD INT21_Ioctl_CylHeadSect2Lin(DWORD cyl, WORD head, WORD sec, WORD cyl_cnt,
2203 WORD head_cnt, WORD sec_cnt)
2205 DWORD res = (cyl * head_cnt*sec_cnt + head * sec_cnt + sec);
2206 return res;
2209 /***********************************************************************
2210 * INT21_Ioctl_Block
2212 * Handler for block device IOCTLs.
2214 static void INT21_Ioctl_Block( CONTEXT *context )
2216 BYTE *dataptr;
2217 BYTE drive = INT21_MapDrive( BL_reg(context) );
2218 WCHAR drivespec[] = {'A', ':', '\\', 0};
2219 UINT drivetype;
2221 drivespec[0] += drive;
2222 drivetype = GetDriveTypeW( drivespec );
2224 RESET_CFLAG(context);
2225 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
2227 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2228 AL_reg(context), 'A' + drive );
2229 SetLastError( ERROR_INVALID_DRIVE );
2230 SET_AX( context, ERROR_INVALID_DRIVE );
2231 SET_CFLAG( context );
2232 return;
2235 switch (AL_reg(context))
2237 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2238 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2239 INT_BARF( context, 0x21 );
2240 break;
2242 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2243 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2244 'A' + drive );
2246 if (drivetype == DRIVE_REMOVABLE)
2247 SET_AX( context, 0 ); /* removable */
2248 else
2249 SET_AX( context, 1 ); /* not removable */
2250 break;
2252 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2253 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2254 'A' + drive );
2256 if (drivetype == DRIVE_REMOTE)
2257 SET_DX( context, (1<<9) | (1<<12) ); /* remote + no direct IO */
2258 else if (drivetype == DRIVE_CDROM)
2259 /* CDROM should be set to remote. If it set the app will
2260 * call int2f to check if it cdrom or remote drive. */
2261 SET_DX( context, (1<<12) );
2262 else if (drivetype == DRIVE_FIXED)
2263 /* This should define if drive support 0x0d, 0x0f and 0x08
2264 * requests. The local fixed drive should do. */
2265 SET_DX( context, (1<<11) );
2266 else
2267 SET_DX( context, 0 ); /* FIXME: use driver attr here */
2268 break;
2270 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2271 /* Get pointer to IOCTL parameter block */
2272 dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2274 switch (CX_reg(context))
2276 case 0x0841: /* write logical device track */
2277 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2278 'A' + drive);
2280 WORD head = *(WORD *)(dataptr+1);
2281 WORD cyl = *(WORD *)(dataptr+3);
2282 WORD sect = *(WORD *)(dataptr+5);
2283 WORD nrsect = *(WORD *)(dataptr+7);
2284 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2285 WORD cyl_cnt, head_cnt, sec_cnt;
2287 /* FIXME: we're faking some values here */
2288 if (drive > 1)
2290 /* cyl_cnt = 0x300;
2291 head_cnt = 16;
2292 sec_cnt = 255; */
2293 SET_AX( context, ERROR_WRITE_FAULT );
2294 SET_CFLAG(context);
2295 break;
2297 else
2298 { /* floppy */
2299 cyl_cnt = 80;
2300 head_cnt = 2;
2301 sec_cnt = 18;
2304 if (!DOSVM_RawWrite(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2306 SET_AX( context, ERROR_WRITE_FAULT );
2307 SET_CFLAG(context);
2310 break;
2312 case 0x084a: /* lock logical volume */
2313 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2314 BH_reg(context), DX_reg(context), 'A' + drive );
2315 break;
2317 case 0x0860: /* get device parameters */
2318 /* FIXME: we're faking some values here */
2319 /* used by w4wgrp's winfile */
2320 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2321 dataptr[0] = 0x04;
2322 dataptr[6] = 0; /* media type */
2323 if (drive > 1)
2325 dataptr[1] = 0x05; /* fixed disk */
2326 setword(&dataptr[2], 0x01); /* non removable */
2327 setword(&dataptr[4], 0x300); /* # of cylinders */
2329 else
2331 dataptr[1] = 0x07; /* block dev, floppy */
2332 setword(&dataptr[2], 0x02); /* removable */
2333 setword(&dataptr[4], 80); /* # of cylinders */
2335 CreateBPB(drive, &dataptr[7], TRUE);
2336 RESET_CFLAG(context);
2337 break;
2339 case 0x0861: /* read logical device track */
2340 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2341 'A' + drive);
2343 WORD head = *(WORD *)(dataptr+1);
2344 WORD cyl = *(WORD *)(dataptr+3);
2345 WORD sect = *(WORD *)(dataptr+5);
2346 WORD nrsect = *(WORD *)(dataptr+7);
2347 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2348 WORD cyl_cnt, head_cnt, sec_cnt;
2350 /* FIXME: we're faking some values here */
2351 if (drive > 1)
2353 cyl_cnt = 0x300;
2354 head_cnt = 16;
2355 sec_cnt = 255;
2357 else
2358 { /* floppy */
2359 cyl_cnt = 80;
2360 head_cnt = 2;
2361 sec_cnt = 18;
2364 if (!DOSVM_RawRead(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2366 SET_AX( context, ERROR_READ_FAULT );
2367 SET_CFLAG(context);
2370 break;
2372 case 0x0866: /* get volume serial number */
2374 WCHAR label[12],fsname[9];
2375 DWORD serial;
2376 TRACE( "GENERIC IOCTL - Get media id - %c:\n",
2377 'A' + 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, (LPSTR)dataptr + 6, 11, NULL, NULL);
2383 WideCharToMultiByte(CP_OEMCP, 0, fsname, 8, (LPSTR)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 /* Trial and 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 */
2418 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2419 INT21_DriveName( BL_reg(context)));
2420 /* FIXME: as of today, we don't support logical drive mapping... */
2421 SET_AL( context, 0 );
2422 break;
2424 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2425 default:
2426 INT_BARF( context, 0x21 );
2431 /***********************************************************************
2432 * INT21_Ioctl_Char
2434 * Handler for character device IOCTLs.
2436 static void INT21_Ioctl_Char( CONTEXT *context )
2438 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2439 BOOL IsConsoleIOHandle = VerifyConsoleIoHandle( handle );
2441 switch (AL_reg(context))
2443 case 0x00: /* GET DEVICE INFORMATION */
2444 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context) );
2445 if (IsConsoleIOHandle || GetFileType(handle) == FILE_TYPE_CHAR)
2448 * Returns attribute word in DX:
2449 * Bit 14 - Device driver can process IOCTL requests.
2450 * Bit 13 - Output until busy supported.
2451 * Bit 11 - Driver supports OPEN/CLOSE calls.
2452 * Bit 8 - Unknown.
2453 * Bit 7 - Set (indicates device).
2454 * Bit 6 - EOF on input.
2455 * Bit 5 - Raw (binary) mode.
2456 * Bit 4 - Device is special (uses int29).
2457 * Bit 3 - Clock device.
2458 * Bit 2 - NUL device.
2459 * Bit 1 - Console output device.
2460 * Bit 0 - Console input device.
2462 SET_DX( context, IsConsoleIOHandle ? 0x80c3 : 0x80c0 /* FIXME */ );
2464 else
2467 * Returns attribute word in DX:
2468 * Bit 15 - File is remote.
2469 * Bit 14 - Don't set file date/time on closing.
2470 * Bit 11 - Media not removable.
2471 * Bit 8 - Generate int24 if no disk space on write
2472 * or read past end of file
2473 * Bit 7 - Clear (indicates file).
2474 * Bit 6 - File has not been written.
2475 * Bit 5..0 - Drive number (0=A:,...)
2477 * FIXME: Should check if file is on remote or removable drive.
2478 * FIXME: Should use drive file is located on (and not current).
2480 SET_DX( context, 0x0140 + INT21_GetCurrentDrive() );
2482 break;
2484 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2485 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context) );
2487 * Returns attribute word in DX:
2488 * Bit 15 - Set if remote.
2489 * Bit 14 - Set if date/time not set on close.
2491 * FIXME: Should check if file is on remote drive.
2493 SET_DX( context, 0 );
2494 break;
2496 case 0x01: /* SET DEVICE INFORMATION */
2497 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2498 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2499 case 0x06: /* GET INPUT STATUS */
2500 case 0x07: /* GET OUTPUT STATUS */
2501 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2502 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2503 default:
2504 INT_BARF( context, 0x21 );
2505 break;
2510 /***********************************************************************
2511 * INT21_Ioctl
2513 * Handler for function 0x44.
2515 static void INT21_Ioctl( CONTEXT *context )
2517 switch (AL_reg(context))
2519 case 0x00:
2520 case 0x01:
2521 case 0x02:
2522 case 0x03:
2523 INT21_Ioctl_Char( context );
2524 break;
2526 case 0x04:
2527 case 0x05:
2528 INT21_Ioctl_Block( context );
2529 break;
2531 case 0x06:
2532 case 0x07:
2533 INT21_Ioctl_Char( context );
2534 break;
2536 case 0x08:
2537 case 0x09:
2538 INT21_Ioctl_Block( context );
2539 break;
2541 case 0x0a:
2542 INT21_Ioctl_Char( context );
2543 break;
2545 case 0x0b: /* SET SHARING RETRY COUNT */
2546 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2547 CX_reg(context), DX_reg(context) );
2548 if (!CX_reg(context))
2550 SET_AX( context, 1 );
2551 SET_CFLAG( context );
2553 else
2555 RESET_CFLAG( context );
2557 break;
2559 case 0x0c:
2560 INT21_Ioctl_Char( context );
2561 break;
2563 case 0x0d:
2564 case 0x0e:
2565 case 0x0f:
2566 INT21_Ioctl_Block( context );
2567 break;
2569 case 0x10:
2570 INT21_Ioctl_Char( context );
2571 break;
2573 case 0x11:
2574 INT21_Ioctl_Block( context );
2575 break;
2577 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2578 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2579 SET_CFLAG(context); /* Error / This is not DR DOS. */
2580 SET_AX( context, 0x0001 ); /* Invalid function */
2581 break;
2583 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2584 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2585 SET_CFLAG(context); /* Error / This is not DR DOS. */
2586 SET_AX( context, 0x0001 ); /* Invalid function */
2587 break;
2589 case 0xe0: /* Sun PC-NFS API */
2590 TRACE( "Sun PC-NFS API\n" );
2591 /* not installed */
2592 break;
2594 default:
2595 INT_BARF( context, 0x21 );
2600 /***********************************************************************
2601 * INT21_Fat32
2603 * Handler for function 0x73.
2605 static BOOL INT21_Fat32( CONTEXT *context )
2607 switch (AL_reg(context))
2609 case 0x02: /* FAT32 - GET EXTENDED DPB */
2611 BYTE drive = INT21_MapDrive( DL_reg(context) );
2612 WORD *ptr = CTX_SEG_OFF_TO_LIN(context,
2613 context->SegEs, context->Edi);
2614 INT21_DPB *target = (INT21_DPB*)(ptr + 1);
2615 INT21_DPB *source;
2617 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context) );
2619 if ( CX_reg(context) < sizeof(INT21_DPB) + 2 || *ptr < sizeof(INT21_DPB) )
2621 SetLastError( ERROR_BAD_LENGTH );
2622 return FALSE;
2625 if ( !INT21_FillDrivePB( drive ) )
2627 SetLastError( ERROR_INVALID_DRIVE );
2628 return FALSE;
2631 source = &INT21_GetHeapPointer()->misc_dpb_list[drive];
2633 *ptr = sizeof(INT21_DPB);
2634 *target = *source;
2636 if (LOWORD(context->Esi) != 0xF1A6)
2638 target->driver_header = 0;
2639 target->next = 0;
2641 else
2643 FIXME( "Caller requested driver and next DPB pointers!\n" );
2646 break;
2648 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2650 WCHAR dirW[MAX_PATH];
2651 char *dirA = CTX_SEG_OFF_TO_LIN( context,
2652 context->SegDs, context->Edx );
2653 BYTE *data = CTX_SEG_OFF_TO_LIN( context,
2654 context->SegEs, context->Edi );
2655 DWORD cluster_sectors;
2656 DWORD sector_bytes;
2657 DWORD free_clusters;
2658 DWORD total_clusters;
2660 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA );
2661 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
2663 if (CX_reg(context) < 44)
2665 SetLastError( ERROR_BAD_LENGTH );
2666 return FALSE;
2669 if (!GetDiskFreeSpaceW( dirW, &cluster_sectors, &sector_bytes,
2670 &free_clusters, &total_clusters ))
2671 return FALSE;
2673 *(WORD*) (data + 0) = 44; /* size of structure */
2674 *(WORD*) (data + 2) = 0; /* version */
2675 *(DWORD*)(data + 4) = cluster_sectors;
2676 *(DWORD*)(data + 8) = sector_bytes;
2677 *(DWORD*)(data + 12) = free_clusters;
2678 *(DWORD*)(data + 16) = total_clusters;
2681 * Below we have free/total sectors and
2682 * free/total allocation units without adjustment
2683 * for compression. We fake both using cluster information.
2685 *(DWORD*)(data + 20) = free_clusters * cluster_sectors;
2686 *(DWORD*)(data + 24) = total_clusters * cluster_sectors;
2687 *(DWORD*)(data + 28) = free_clusters;
2688 *(DWORD*)(data + 32) = total_clusters;
2691 * Between (data + 36) and (data + 43) there
2692 * are eight reserved bytes.
2695 break;
2697 default:
2698 INT_BARF( context, 0x21 );
2701 return TRUE;
2704 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA *dataA,
2705 const WIN32_FIND_DATAW *dataW)
2707 dataA->dwFileAttributes = dataW->dwFileAttributes;
2708 dataA->ftCreationTime = dataW->ftCreationTime;
2709 dataA->ftLastAccessTime = dataW->ftLastAccessTime;
2710 dataA->ftLastWriteTime = dataW->ftLastWriteTime;
2711 dataA->nFileSizeHigh = dataW->nFileSizeHigh;
2712 dataA->nFileSizeLow = dataW->nFileSizeLow;
2713 WideCharToMultiByte( CP_OEMCP, 0, dataW->cFileName, -1,
2714 dataA->cFileName, sizeof(dataA->cFileName), NULL, NULL );
2715 WideCharToMultiByte( CP_OEMCP, 0, dataW->cAlternateFileName, -1,
2716 dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName), NULL, NULL );
2719 /***********************************************************************
2720 * INT21_LongFilename
2722 * Handler for function 0x71.
2724 static void INT21_LongFilename( CONTEXT *context )
2726 BOOL bSetDOSExtendedError = FALSE;
2727 WCHAR pathW[MAX_PATH];
2728 char* pathA;
2730 switch (AL_reg(context))
2732 case 0x0d: /* RESET DRIVE */
2733 INT_BARF( context, 0x21 );
2734 break;
2736 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
2737 if (!INT21_CreateDirectory( context ))
2738 bSetDOSExtendedError = TRUE;
2739 break;
2741 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
2742 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2744 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA);
2745 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2746 if (!RemoveDirectoryW( pathW )) bSetDOSExtendedError = TRUE;
2747 break;
2749 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
2750 if (!INT21_SetCurrentDirectory( context ))
2751 bSetDOSExtendedError = TRUE;
2752 break;
2754 case 0x41: /* LONG FILENAME - DELETE FILE */
2755 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2757 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA );
2758 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2760 if (!DeleteFileW( pathW )) bSetDOSExtendedError = TRUE;
2761 break;
2763 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
2764 if (!INT21_FileAttributes( context, BL_reg(context), TRUE ))
2765 bSetDOSExtendedError = TRUE;
2766 break;
2768 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
2769 if (!INT21_GetCurrentDirectory( context, TRUE ))
2770 bSetDOSExtendedError = TRUE;
2771 break;
2773 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
2775 HANDLE handle;
2776 HGLOBAL16 h16;
2777 WIN32_FIND_DATAW dataW;
2778 WIN32_FIND_DATAA* dataA;
2780 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
2781 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA);
2783 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2784 handle = FindFirstFileW(pathW, &dataW);
2786 dataA = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
2787 if (handle != INVALID_HANDLE_VALUE &&
2788 (h16 = GlobalAlloc16(GMEM_MOVEABLE, sizeof(handle))))
2790 HANDLE* ptr = GlobalLock16( h16 );
2791 *ptr = handle;
2792 GlobalUnlock16( h16 );
2793 SET_AX( context, h16 );
2794 INT21_ConvertFindDataWtoA(dataA, &dataW);
2796 else
2798 if (handle != INVALID_HANDLE_VALUE) FindClose(handle);
2799 SET_AX( context, INVALID_HANDLE_VALUE16);
2800 bSetDOSExtendedError = TRUE;
2803 break;
2805 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
2807 HGLOBAL16 h16 = BX_reg(context);
2808 HANDLE* ptr;
2809 WIN32_FIND_DATAW dataW;
2810 WIN32_FIND_DATAA* dataA;
2812 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
2813 BX_reg(context));
2815 dataA = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
2817 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
2819 if (!FindNextFileW(*ptr, &dataW)) bSetDOSExtendedError = TRUE;
2820 else INT21_ConvertFindDataWtoA(dataA, &dataW);
2821 GlobalUnlock16( h16 );
2823 else
2825 SetLastError( ERROR_INVALID_HANDLE );
2826 bSetDOSExtendedError = TRUE;
2829 break;
2831 case 0x56: /* LONG FILENAME - RENAME FILE */
2832 if (!INT21_RenameFile(context))
2833 bSetDOSExtendedError = TRUE;
2834 break;
2836 case 0x60: /* LONG FILENAME - CONVERT PATH */
2838 WCHAR res[MAX_PATH];
2840 switch (CL_reg(context))
2842 case 0x00: /* "truename" - Canonicalize path */
2844 * FIXME: This is not 100% equal to 0x01 case,
2845 * if you fix this, fix int21 subfunction 0x60, too.
2848 case 0x01: /* Get short filename or path */
2849 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
2850 if (!GetShortPathNameW(pathW, res, 67))
2851 bSetDOSExtendedError = TRUE;
2852 else
2854 SET_AX( context, 0 );
2855 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
2856 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
2857 67, NULL, NULL);
2859 break;
2861 case 0x02: /* Get canonical long filename or path */
2862 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
2863 if (!GetFullPathNameW(pathW, 128, res, NULL))
2864 bSetDOSExtendedError = TRUE;
2865 else
2867 SET_AX( context, 0 );
2868 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
2869 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
2870 128, NULL, NULL);
2872 break;
2873 default:
2874 FIXME("Unimplemented long file name function:\n");
2875 INT_BARF( context, 0x21 );
2876 SET_CFLAG(context);
2877 SET_AL( context, 0 );
2878 break;
2881 break;
2883 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
2884 if (!INT21_CreateFile( context, context->Esi, TRUE,
2885 BX_reg(context), DL_reg(context) ))
2886 bSetDOSExtendedError = TRUE;
2887 break;
2889 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
2891 DWORD filename_len, flags;
2892 WCHAR dstW[8];
2894 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
2896 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA);
2897 SET_AX( context, 0 );
2898 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2899 if (!GetVolumeInformationW( pathW, NULL, 0, NULL, &filename_len,
2900 &flags, dstW, 8 ))
2902 INT_BARF( context, 0x21 );
2903 SET_CFLAG(context);
2904 break;
2906 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
2907 SET_CX( context, filename_len );
2908 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
2909 WideCharToMultiByte(CP_OEMCP, 0, dstW, -1,
2910 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
2911 8, NULL, NULL);
2913 break;
2915 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
2917 HGLOBAL16 h16 = BX_reg(context);
2918 HANDLE* ptr;
2920 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
2921 BX_reg(context));
2922 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
2924 if (!FindClose( *ptr )) bSetDOSExtendedError = TRUE;
2925 GlobalUnlock16( h16 );
2926 GlobalFree16( h16 );
2928 else
2930 SetLastError( ERROR_INVALID_HANDLE );
2931 bSetDOSExtendedError = TRUE;
2934 break;
2936 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
2938 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2939 BY_HANDLE_FILE_INFORMATION *info =
2940 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2942 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
2944 if (!GetFileInformationByHandle(handle, info))
2945 bSetDOSExtendedError = TRUE;
2947 break;
2949 case 0xa7: /* LONG FILENAME - CONVERT TIME */
2950 switch (BL_reg(context))
2952 case 0x00: /* FILE TIME TO DOS TIME */
2954 WORD date, time;
2955 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
2956 context->SegDs,
2957 context->Esi);
2959 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
2961 FileTimeToDosDateTime( filetime, &date, &time );
2963 SET_DX( context, date );
2964 SET_CX( context, time );
2967 * FIXME: BH has number of 10-millisecond units
2968 * past time in CX.
2970 SET_BH( context, 0 );
2972 break;
2974 case 0x01: /* DOS TIME TO FILE TIME */
2976 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
2977 context->SegEs,
2978 context->Edi);
2980 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
2983 * FIXME: BH has number of 10-millisecond units
2984 * past time in CX.
2986 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
2987 filetime );
2989 break;
2991 default:
2992 INT_BARF( context, 0x21 );
2993 break;
2995 break;
2997 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
2998 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
2999 case 0xaa: /* LONG FILENAME - SUBST */
3000 default:
3001 FIXME("Unimplemented long file name function:\n");
3002 INT_BARF( context, 0x21 );
3003 SET_CFLAG(context);
3004 SET_AL( context, 0 );
3005 break;
3008 if (bSetDOSExtendedError)
3010 SET_AX( context, GetLastError() );
3011 SET_CFLAG( context );
3016 /***********************************************************************
3017 * INT21_RenameFile
3019 * Handler for:
3020 * - function 0x56
3021 * - subfunction 0x56 of function 0x71
3022 * - subfunction 0xff of function 0x43 (CL == 0x56)
3024 static BOOL INT21_RenameFile( CONTEXT *context )
3026 WCHAR fromW[MAX_PATH];
3027 WCHAR toW[MAX_PATH];
3028 char *fromA = CTX_SEG_OFF_TO_LIN(context,
3029 context->SegDs,context->Edx);
3030 char *toA = CTX_SEG_OFF_TO_LIN(context,
3031 context->SegEs,context->Edi);
3033 TRACE( "RENAME FILE %s to %s\n", fromA, toA );
3034 MultiByteToWideChar(CP_OEMCP, 0, fromA, -1, fromW, MAX_PATH);
3035 MultiByteToWideChar(CP_OEMCP, 0, toA, -1, toW, MAX_PATH);
3037 return MoveFileW( fromW, toW );
3041 /***********************************************************************
3042 * INT21_NetworkFunc
3044 * Handler for:
3045 * - function 0x5e
3047 static BOOL INT21_NetworkFunc (CONTEXT *context)
3049 switch (AL_reg(context))
3051 case 0x00: /* Get machine name. */
3053 WCHAR dstW[MAX_COMPUTERNAME_LENGTH + 1];
3054 DWORD s = ARRAY_SIZE(dstW);
3055 int len;
3057 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
3058 TRACE("getting machine name to %p\n", dst);
3059 if (!GetComputerNameW(dstW, &s) ||
3060 !WideCharToMultiByte(CP_OEMCP, 0, dstW, -1, dst, 16, NULL, NULL))
3062 WARN("failed!\n");
3063 SetLastError( ER_NoNetwork );
3064 return TRUE;
3066 for (len = strlen(dst); len < 15; len++) dst[len] = ' ';
3067 dst[15] = 0;
3068 SET_CH( context, 1 ); /* Valid */
3069 SET_CL( context, 1 ); /* NETbios number??? */
3070 TRACE("returning %s\n", debugstr_an(dst, 16));
3071 return FALSE;
3074 default:
3075 SetLastError( ER_NoNetwork );
3076 return TRUE;
3080 /******************************************************************
3081 * INT21_GetDiskSerialNumber
3084 static int INT21_GetDiskSerialNumber( CONTEXT *context )
3086 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3087 WCHAR path[] = {'A',':','\\',0}, label[11];
3088 DWORD serial;
3090 path[0] += INT21_MapDrive(BL_reg(context));
3091 if (!GetVolumeInformationW( path, label, 11, &serial, NULL, NULL, NULL, 0))
3093 SetLastError( ERROR_INVALID_DRIVE );
3094 return 0;
3097 *(WORD *)dataptr = 0;
3098 memcpy(dataptr + 2, &serial, sizeof(DWORD));
3099 WideCharToMultiByte(CP_OEMCP, 0, label, 11, (LPSTR)dataptr + 6, 11, NULL, NULL);
3100 memcpy(dataptr + 17, "FAT16 ", 8);
3101 return 1;
3105 /******************************************************************
3106 * INT21_SetDiskSerialNumber
3109 static BOOL INT21_SetDiskSerialNumber( CONTEXT *context )
3111 #if 0
3112 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3113 int drive = INT21_MapDrive(BL_reg(context));
3115 if (!is_valid_drive(drive))
3117 SetLastError( ERROR_INVALID_DRIVE );
3118 return FALSE;
3121 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
3122 return TRUE;
3123 #else
3124 FIXME("Setting drive serial number is no longer supported\n");
3125 SetLastError( ERROR_NOT_SUPPORTED );
3126 return FALSE;
3127 #endif
3131 /******************************************************************
3132 * INT21_GetFreeDiskSpace
3135 static BOOL INT21_GetFreeDiskSpace( CONTEXT *context )
3137 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
3138 WCHAR root[] = {'A',':','\\',0};
3139 const DWORD max_clusters = 0x3d83;
3140 const DWORD max_sectors_per_cluster = 0x7f;
3141 const DWORD max_bytes_per_sector = 0x200;
3143 root[0] += INT21_MapDrive(DL_reg(context));
3144 if (!GetDiskFreeSpaceW( root, &cluster_sectors, &sector_bytes,
3145 &free_clusters, &total_clusters ))
3146 return FALSE;
3148 /* Some old win31 apps (Lotus SmartSuite 5.1) crap out if there's too
3149 * much disk space, so Windows XP seems to apply the following limits:
3150 * cluster_sectors <= 0x7f
3151 * sector size <= 0x200
3152 * clusters <= 0x3D83
3153 * This means total reported space is limited to about 1GB.
3156 /* Make sure bytes-per-sector is in [, max] */
3157 while (sector_bytes > max_bytes_per_sector) {
3158 sector_bytes >>= 1;
3159 free_clusters <<= 1;
3160 total_clusters <<= 1;
3162 /* Then make sure sectors-per-cluster is in [max/2, max]. */
3163 while (cluster_sectors <= max_sectors_per_cluster/2) {
3164 cluster_sectors <<= 1;
3165 free_clusters >>= 1;
3166 total_clusters >>= 1;
3168 while (cluster_sectors > max_sectors_per_cluster) {
3169 cluster_sectors >>= 1;
3170 free_clusters <<= 1;
3171 total_clusters <<= 1;
3174 /* scale up sectors_per_cluster to exactly max_sectors_per_cluster.
3175 * We could skip this, but that would impose an artificially low
3176 * limit on reported disk space.
3177 * To avoid overflow, first apply a preliminary cap on sector count;
3178 * this will not affect the correctness of the final result,
3179 * because if the preliminary cap hits, the final one will, too.
3181 if (total_clusters > 4 * max_clusters)
3182 total_clusters = 4 * max_clusters;
3183 if (free_clusters > 4 * max_clusters)
3184 free_clusters = 4 * max_clusters;
3185 if (cluster_sectors < max_sectors_per_cluster) {
3186 free_clusters *= cluster_sectors;
3187 free_clusters /= max_sectors_per_cluster;
3188 total_clusters *= cluster_sectors;
3189 total_clusters /= max_sectors_per_cluster;
3190 cluster_sectors = max_sectors_per_cluster;
3193 /* Finally, apply real cluster count cap. */
3194 if (total_clusters > max_clusters)
3195 total_clusters = max_clusters;
3196 if (free_clusters > max_clusters)
3197 free_clusters = max_clusters;
3199 SET_AX( context, cluster_sectors );
3200 SET_BX( context, free_clusters );
3201 SET_CX( context, sector_bytes );
3202 SET_DX( context, total_clusters );
3203 return TRUE;
3206 /******************************************************************
3207 * INT21_GetDriveAllocInfo
3210 static BOOL INT21_GetDriveAllocInfo( CONTEXT *context, BYTE drive )
3212 INT21_DPB *dpb;
3214 drive = INT21_MapDrive( drive );
3215 if (!INT21_FillDrivePB( drive )) return FALSE;
3216 dpb = &(INT21_GetHeapPointer()->misc_dpb_list[drive]);
3217 SET_AL( context, dpb->cluster_sectors + 1 );
3218 SET_CX( context, dpb->sector_bytes );
3219 SET_DX( context, dpb->num_clusters1 );
3221 context->SegDs = INT21_GetHeapSelector( context );
3222 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive].media_ID ) );
3223 return TRUE;
3226 /***********************************************************************
3227 * INT21_GetExtendedError
3229 static void INT21_GetExtendedError( CONTEXT *context )
3231 BYTE class, action, locus;
3232 WORD error = GetLastError();
3234 switch(error)
3236 case ERROR_SUCCESS:
3237 class = action = locus = 0;
3238 break;
3239 case ERROR_DIR_NOT_EMPTY:
3240 class = EC_Exists;
3241 action = SA_Ignore;
3242 locus = EL_Disk;
3243 break;
3244 case ERROR_ACCESS_DENIED:
3245 class = EC_AccessDenied;
3246 action = SA_Abort;
3247 locus = EL_Disk;
3248 break;
3249 case ERROR_CANNOT_MAKE:
3250 class = EC_AccessDenied;
3251 action = SA_Abort;
3252 locus = EL_Unknown;
3253 break;
3254 case ERROR_DISK_FULL:
3255 case ERROR_HANDLE_DISK_FULL:
3256 class = EC_MediaError;
3257 action = SA_Abort;
3258 locus = EL_Disk;
3259 break;
3260 case ERROR_FILE_EXISTS:
3261 case ERROR_ALREADY_EXISTS:
3262 class = EC_Exists;
3263 action = SA_Abort;
3264 locus = EL_Disk;
3265 break;
3266 case ERROR_FILE_NOT_FOUND:
3267 case ERROR_PATH_NOT_FOUND:
3268 case ERROR_INVALID_NAME:
3269 class = EC_NotFound;
3270 action = SA_Abort;
3271 locus = EL_Disk;
3272 break;
3273 case ERROR_GEN_FAILURE:
3274 class = EC_SystemFailure;
3275 action = SA_Abort;
3276 locus = EL_Unknown;
3277 break;
3278 case ERROR_INVALID_DRIVE:
3279 class = EC_MediaError;
3280 action = SA_Abort;
3281 locus = EL_Disk;
3282 break;
3283 case ERROR_INVALID_HANDLE:
3284 class = EC_ProgramError;
3285 action = SA_Abort;
3286 locus = EL_Disk;
3287 break;
3288 case ERROR_LOCK_VIOLATION:
3289 class = EC_AccessDenied;
3290 action = SA_Abort;
3291 locus = EL_Disk;
3292 break;
3293 case ERROR_NO_MORE_FILES:
3294 class = EC_MediaError;
3295 action = SA_Abort;
3296 locus = EL_Disk;
3297 break;
3298 case ER_NoNetwork:
3299 class = EC_NotFound;
3300 action = SA_Abort;
3301 locus = EL_Network;
3302 break;
3303 case ERROR_NOT_ENOUGH_MEMORY:
3304 class = EC_OutOfResource;
3305 action = SA_Abort;
3306 locus = EL_Memory;
3307 break;
3308 case ERROR_SEEK:
3309 class = EC_NotFound;
3310 action = SA_Ignore;
3311 locus = EL_Disk;
3312 break;
3313 case ERROR_SHARING_VIOLATION:
3314 class = EC_Temporary;
3315 action = SA_Retry;
3316 locus = EL_Disk;
3317 break;
3318 case ERROR_TOO_MANY_OPEN_FILES:
3319 class = EC_ProgramError;
3320 action = SA_Abort;
3321 locus = EL_Disk;
3322 break;
3323 default:
3324 FIXME("Unknown error %d\n", error );
3325 class = EC_SystemFailure;
3326 action = SA_Abort;
3327 locus = EL_Unknown;
3328 break;
3330 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3331 error, class, action, locus );
3332 SET_AX( context, error );
3333 SET_BH( context, class );
3334 SET_BL( context, action );
3335 SET_CH( context, locus );
3338 static BOOL INT21_CreateTempFile( CONTEXT *context )
3340 static int counter = 0;
3341 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
3342 char *p = name + strlen(name);
3344 /* despite what Ralf Brown says, some programs seem to call without
3345 * ending backslash (DOS accepts that, so we accept it too) */
3346 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
3348 for (;;)
3350 sprintf( p, "wine%04lx.%03d", GetCurrentThreadId(), counter );
3351 counter = (counter + 1) % 1000;
3353 SET_AX( context,
3354 Win32HandleToDosFileHandle(
3355 CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
3356 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
3357 CREATE_NEW, 0, 0 ) ) );
3358 if (AX_reg(context) != HFILE_ERROR16)
3360 TRACE("created %s\n", name );
3361 return TRUE;
3363 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
3367 /***********************************************************************
3368 * DOSFS_ToDosFCBFormat
3370 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3371 * expanding wild cards and converting to upper-case in the process.
3372 * File name can be terminated by '\0', '\\' or '/'.
3373 * Return FALSE if the name is not a valid DOS name.
3374 * 'buffer' must be at least 12 characters long.
3376 /* Chars we don't want to see in DOS file names */
3377 static BOOL INT21_ToDosFCBFormat( LPCWSTR name, LPWSTR buffer )
3379 static const WCHAR invalid_chars[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3380 LPCWSTR p = name;
3381 int i;
3383 /* Check for "." and ".." */
3384 if (*p == '.')
3386 p++;
3387 buffer[0] = '.';
3388 for(i = 1; i < 11; i++) buffer[i] = ' ';
3389 buffer[11] = 0;
3390 if (*p == '.')
3392 buffer[1] = '.';
3393 p++;
3395 return (!*p || (*p == '/') || (*p == '\\'));
3398 for (i = 0; i < 8; i++)
3400 switch(*p)
3402 case '\0':
3403 case '\\':
3404 case '/':
3405 case '.':
3406 buffer[i] = ' ';
3407 break;
3408 case '?':
3409 p++;
3410 /* fall through */
3411 case '*':
3412 buffer[i] = '?';
3413 break;
3414 default:
3415 if (wcschr( invalid_chars, *p )) return FALSE;
3416 buffer[i] = *p++;
3417 break;
3421 if (*p == '*')
3423 /* Skip all chars after wildcard up to first dot */
3424 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
3426 else
3428 /* Check if name too long */
3429 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return FALSE;
3431 if (*p == '.') p++; /* Skip dot */
3433 for (i = 8; i < 11; i++)
3435 switch(*p)
3437 case '\0':
3438 case '\\':
3439 case '/':
3440 buffer[i] = ' ';
3441 break;
3442 case '.':
3443 return FALSE; /* Second extension not allowed */
3444 case '?':
3445 p++;
3446 /* fall through */
3447 case '*':
3448 buffer[i] = '?';
3449 break;
3450 default:
3451 if (wcschr( invalid_chars, *p )) return FALSE;
3452 buffer[i] = *p++;
3453 break;
3456 buffer[11] = '\0';
3457 wcsupr( buffer );
3459 /* at most 3 character of the extension are processed
3460 * is something behind this ?
3462 while (*p == '*' || *p == ' ') p++; /* skip wildcards and spaces */
3463 return (!*p || (*p == '/') || (*p == '\\'));
3466 static HANDLE INT21_FindHandle;
3467 static const WCHAR *INT21_FindPath; /* will point to current dta->fullPath search */
3469 /******************************************************************
3470 * INT21_FindFirst
3472 static BOOL INT21_FindFirst( CONTEXT *context )
3474 WCHAR *p, *q;
3475 const char *path;
3476 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3477 WCHAR maskW[12], pathW[MAX_PATH];
3478 static const WCHAR wildcardW[] = {'*','.','*',0};
3480 path = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3481 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
3483 p = wcsrchr( pathW, '\\');
3484 q = wcsrchr( pathW, '/');
3485 if (q>p) p = q;
3486 if (!p)
3488 if (pathW[0] && pathW[1] == ':') p = pathW + 2;
3489 else p = pathW;
3491 else p++;
3493 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3494 * (doesn't matter as it is set below anyway)
3496 if (!INT21_ToDosFCBFormat( p, maskW ))
3498 SetLastError( ERROR_FILE_NOT_FOUND );
3499 SET_AX( context, ERROR_FILE_NOT_FOUND );
3500 SET_CFLAG(context);
3501 return FALSE;
3503 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
3505 dta->fullPath = HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW) + (p - pathW)*sizeof(WCHAR) );
3506 memcpy( dta->fullPath, pathW, (p - pathW) * sizeof(WCHAR) );
3507 memcpy( dta->fullPath + (p - pathW), wildcardW, sizeof(wildcardW) );
3508 /* we must have a fully qualified file name in dta->fullPath
3509 * (we could have a UNC path, but this would lead to some errors later on)
3511 dta->drive = drive_number( dta->fullPath[0] );
3512 dta->count = 0;
3513 dta->search_attr = CL_reg(context);
3514 return TRUE;
3517 /******************************************************************
3518 * match_short
3520 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3522 static BOOL match_short(LPCWSTR shortW, LPCSTR maskA)
3524 WCHAR mask[11], file[12];
3525 int i;
3527 if (!INT21_ToDosFCBFormat( shortW, file )) return FALSE;
3528 MultiByteToWideChar(CP_OEMCP, 0, maskA, 11, mask, 11);
3529 for (i = 0; i < 11; i++)
3530 if (mask[i] != '?' && mask[i] != file[i]) return FALSE;
3531 return TRUE;
3534 static unsigned INT21_FindHelper(LPCWSTR fullPath, unsigned drive, unsigned count,
3535 LPCSTR mask, unsigned search_attr,
3536 WIN32_FIND_DATAW* entry)
3538 unsigned ncalls;
3540 if ((search_attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
3542 WCHAR path[] = {' ',':','\\',0};
3544 if (count) return 0;
3545 path[0] = drive + 'A';
3546 if (!GetVolumeInformationW(path, entry->cAlternateFileName, 13, NULL, NULL, NULL, NULL, 0)) return 0;
3547 if (!entry->cAlternateFileName[0]) return 0;
3548 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftCreationTime );
3549 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftLastAccessTime );
3550 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftLastWriteTime );
3551 entry->dwFileAttributes = FA_LABEL;
3552 entry->nFileSizeHigh = entry->nFileSizeLow = 0;
3553 TRACE("returning %s as label\n", debugstr_w(entry->cAlternateFileName));
3554 return 1;
3557 if (!INT21_FindHandle || INT21_FindPath != fullPath || count == 0)
3559 if (INT21_FindHandle) FindClose(INT21_FindHandle);
3560 INT21_FindHandle = FindFirstFileW(fullPath, entry);
3561 if (INT21_FindHandle == INVALID_HANDLE_VALUE)
3563 INT21_FindHandle = 0;
3564 return 0;
3566 INT21_FindPath = fullPath;
3567 /* we need to resync search */
3568 ncalls = count;
3570 else ncalls = 1;
3572 while (ncalls-- != 0)
3574 if (!FindNextFileW(INT21_FindHandle, entry))
3576 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3577 return 0;
3580 while (count < 0xffff)
3582 count++;
3583 /* Check the file attributes, and path */
3584 if (!(entry->dwFileAttributes & ~search_attr) &&
3585 match_short(entry->cAlternateFileName[0] ? entry->cAlternateFileName : entry->cFileName,
3586 mask))
3588 return count;
3590 if (!FindNextFileW(INT21_FindHandle, entry))
3592 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3593 return 0;
3596 WARN("Too many directory entries in %s\n", debugstr_w(fullPath) );
3597 return 0;
3600 /******************************************************************
3601 * INT21_FindNext
3603 static BOOL INT21_FindNext( CONTEXT *context )
3605 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3606 DWORD attr = dta->search_attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
3607 WIN32_FIND_DATAW entry;
3608 int n;
3610 if (!dta->fullPath) return FALSE;
3612 n = INT21_FindHelper(dta->fullPath, dta->drive, dta->count,
3613 dta->mask, attr, &entry);
3614 if (n)
3616 dta->fileattr = entry.dwFileAttributes;
3617 dta->filesize = entry.nFileSizeLow;
3618 FileTimeToDosDateTime( &entry.ftLastWriteTime, &dta->filedate, &dta->filetime );
3619 if (entry.cAlternateFileName[0])
3620 WideCharToMultiByte(CP_OEMCP, 0, entry.cAlternateFileName, -1,
3621 dta->filename, 13, NULL, NULL);
3622 else
3623 WideCharToMultiByte(CP_OEMCP, 0, entry.cFileName, -1, dta->filename, 13, NULL, NULL);
3625 if (!memchr(dta->mask,'?',11))
3627 /* wildcardless search, release resources in case no findnext will
3628 * be issued, and as a workaround in case file creation messes up
3629 * findnext, as sometimes happens with pkunzip
3631 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3632 INT21_FindPath = dta->fullPath = NULL;
3634 dta->count = n;
3635 return TRUE;
3637 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3638 INT21_FindPath = dta->fullPath = NULL;
3639 return FALSE;
3642 /* microsoft's programmers should be shot for using CP/M style int21
3643 calls in Windows for Workgroup's winfile.exe */
3645 /******************************************************************
3646 * INT21_FindFirstFCB
3649 static BOOL INT21_FindFirstFCB( CONTEXT *context )
3651 BYTE *fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3652 FINDFILE_FCB *pFCB;
3653 int drive;
3654 WCHAR p[] = {' ',':',};
3656 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
3657 else pFCB = (FINDFILE_FCB *)fcb;
3658 drive = INT21_MapDrive( pFCB->drive );
3659 if (drive == MAX_DOS_DRIVES) return FALSE;
3661 p[0] = 'A' + drive;
3662 pFCB->fullPath = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
3663 if (!pFCB->fullPath) return FALSE;
3664 GetLongPathNameW(p, pFCB->fullPath, MAX_PATH);
3665 pFCB->count = 0;
3666 return TRUE;
3669 /******************************************************************
3670 * INT21_FindNextFCB
3673 static BOOL INT21_FindNextFCB( CONTEXT *context )
3675 BYTE *fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3676 FINDFILE_FCB *pFCB;
3677 LPBYTE pResult = INT21_GetCurrentDTA(context);
3678 DOS_DIRENTRY_LAYOUT *ddl;
3679 WIN32_FIND_DATAW entry;
3680 BYTE attr;
3681 int n;
3682 WCHAR nameW[12];
3684 if (*fcb == 0xff) /* extended FCB ? */
3686 attr = fcb[6];
3687 pFCB = (FINDFILE_FCB *)(fcb + 7);
3689 else
3691 attr = 0;
3692 pFCB = (FINDFILE_FCB *)fcb;
3695 if (!pFCB->fullPath) return FALSE;
3696 n = INT21_FindHelper(pFCB->fullPath, INT21_MapDrive( pFCB->drive ),
3697 pFCB->count, pFCB->filename, attr, &entry);
3698 if (!n)
3700 HeapFree( GetProcessHeap(), 0, pFCB->fullPath );
3701 INT21_FindPath = pFCB->fullPath = NULL;
3702 return FALSE;
3704 pFCB->count += n;
3706 if (*fcb == 0xff)
3708 /* place extended FCB header before pResult if called with extended FCB */
3709 *pResult = 0xff;
3710 pResult += 6; /* leave reserved field behind */
3711 *pResult++ = entry.dwFileAttributes;
3713 *pResult++ = INT21_MapDrive( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
3714 ddl = (DOS_DIRENTRY_LAYOUT*)pResult;
3715 ddl->fileattr = entry.dwFileAttributes;
3716 ddl->cluster = 0; /* what else? */
3717 ddl->filesize = entry.nFileSizeLow;
3718 memset( ddl->reserved, 0, sizeof(ddl->reserved) );
3719 FileTimeToDosDateTime( &entry.ftLastWriteTime,
3720 &ddl->filedate, &ddl->filetime );
3722 /* Convert file name to FCB format */
3723 if (entry.cAlternateFileName[0])
3724 INT21_ToDosFCBFormat( entry.cAlternateFileName, nameW );
3725 else
3726 INT21_ToDosFCBFormat( entry.cFileName, nameW );
3727 WideCharToMultiByte(CP_OEMCP, 0, nameW, 11, ddl->filename, 11, NULL, NULL);
3728 return TRUE;
3732 /******************************************************************
3733 * INT21_ParseFileNameIntoFCB
3736 static void INT21_ParseFileNameIntoFCB( CONTEXT *context )
3738 char *filename =
3739 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
3740 char *fcb =
3741 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
3742 char *s;
3743 WCHAR *buffer;
3744 WCHAR fcbW[12];
3745 INT buffer_len, len;
3747 SET_AL( context, 0xff ); /* failed */
3749 TRACE("filename: '%s'\n", filename);
3751 s = filename;
3752 while (*s && (*s != ' ') && (*s != '\r') && (*s != '\n'))
3753 s++;
3754 len = filename - s;
3756 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
3757 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
3758 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
3759 buffer[len] = 0;
3760 INT21_ToDosFCBFormat(buffer, fcbW);
3761 HeapFree(GetProcessHeap(), 0, buffer);
3762 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
3763 *fcb = 0;
3764 TRACE("FCB: '%s'\n", fcb + 1);
3766 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
3768 /* point DS:SI to first unparsed character */
3769 SET_SI( context, context->Esi + (int)s - (int)filename );
3772 static BOOL INT21_Dup2(HFILE16 hFile1, HFILE16 hFile2)
3774 HFILE16 res = HFILE_ERROR16;
3775 HANDLE handle, new_handle;
3776 #define DOS_TABLE_SIZE 256
3777 DWORD map[DOS_TABLE_SIZE / 32];
3778 int i;
3780 handle = DosFileHandleToWin32Handle(hFile1);
3781 if (handle == INVALID_HANDLE_VALUE)
3782 return FALSE;
3784 _lclose16(hFile2);
3785 /* now loop to allocate the same one... */
3786 memset(map, 0, sizeof(map));
3787 for (i = 0; i < DOS_TABLE_SIZE; i++)
3789 if (!DuplicateHandle(GetCurrentProcess(), handle,
3790 GetCurrentProcess(), &new_handle,
3791 0, FALSE, DUPLICATE_SAME_ACCESS))
3793 res = HFILE_ERROR16;
3794 break;
3796 res = Win32HandleToDosFileHandle(new_handle);
3797 if (res == HFILE_ERROR16 || res == hFile2) break;
3798 map[res / 32] |= 1 << (res % 32);
3800 /* clean up the allocated slots */
3801 for (i = 0; i < DOS_TABLE_SIZE; i++)
3803 if (map[i / 32] & (1 << (i % 32)))
3804 _lclose16((HFILE16)i);
3806 return res == hFile2;
3810 /***********************************************************************
3811 * DOSVM_Int21Handler
3813 * Interrupt 0x21 handler.
3815 void WINAPI DOSVM_Int21Handler( CONTEXT *context )
3817 BOOL bSetDOSExtendedError = FALSE;
3819 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
3820 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
3821 AX_reg(context), BX_reg(context),
3822 CX_reg(context), DX_reg(context),
3823 SI_reg(context), DI_reg(context),
3824 (WORD)context->SegDs, (WORD)context->SegEs,
3825 context->EFlags );
3828 * Extended error is used by (at least) functions 0x2f to 0x62.
3829 * Function 0x59 returns extended error and error should not
3830 * be cleared before handling the function.
3832 if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59)
3833 SetLastError(0);
3835 RESET_CFLAG(context); /* Not sure if this is a good idea. */
3837 switch(AH_reg(context))
3839 case 0x00: /* TERMINATE PROGRAM */
3840 TRACE("TERMINATE PROGRAM\n");
3841 DOSVM_Exit( 0 );
3842 break;
3844 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
3845 TRACE("DIRECT CHARACTER INPUT WITH ECHO - not supported\n");
3846 break;
3848 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
3849 TRACE("Write Character to Standard Output\n");
3850 break;
3852 case 0x03: /* READ CHARACTER FROM STDAUX */
3853 case 0x04: /* WRITE CHARACTER TO STDAUX */
3854 case 0x05: /* WRITE CHARACTER TO PRINTER */
3855 INT_BARF( context, 0x21 );
3856 break;
3858 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
3859 if (DL_reg(context) == 0xff)
3861 TRACE("Direct Console Input\n");
3863 /* no character available */
3864 SET_AL( context, 0 );
3865 SET_ZFLAG( context );
3867 else
3869 TRACE("Direct Console Output\n");
3871 * At least DOS versions 2.1-7.0 return character
3872 * that was written in AL register.
3874 SET_AL( context, DL_reg(context) );
3876 break;
3878 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
3879 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO - not supported\n");
3880 SET_AL( context, 0 );
3881 break;
3883 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
3884 TRACE("CHARACTER INPUT WITHOUT ECHO - not supported\n");
3885 SET_AL( context, 0 );
3886 break;
3888 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
3889 TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
3890 context->SegDs, DX_reg(context) );
3892 LPSTR data = CTX_SEG_OFF_TO_LIN( context,
3893 context->SegDs, context->Edx );
3894 LPSTR p = data;
3895 DWORD w;
3897 * Do NOT use strchr() to calculate the string length,
3898 * as '\0' is valid string content, too!
3899 * Maybe we should check for non-'$' strings, but DOS doesn't.
3901 while (*p != '$') p++;
3903 WriteFile( DosFileHandleToWin32Handle(1), data, p - data, &w, NULL );
3904 SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
3906 break;
3908 case 0x0a: /* BUFFERED INPUT */
3909 TRACE( "BUFFERED INPUT - not supported\n" );
3910 break;
3912 case 0x0b: /* GET STDIN STATUS */
3913 TRACE( "GET STDIN STATUS - not supported\n" );
3914 SET_AL( context, 0 ); /* no character available */
3915 break;
3917 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
3919 BYTE al = AL_reg(context); /* Input function to execute after flush. */
3921 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
3923 /* FIXME: buffers are not flushed */
3926 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
3927 * int21 function identified by AL will be called.
3929 if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
3931 SET_AH( context, al );
3932 DOSVM_Int21Handler( context );
3935 break;
3937 case 0x0d: /* DISK BUFFER FLUSH */
3938 TRACE("DISK BUFFER FLUSH ignored\n");
3939 break;
3941 case 0x0e: /* SELECT DEFAULT DRIVE */
3942 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context) );
3943 INT21_SetCurrentDrive( DL_reg(context) );
3944 SET_AL( context, MAX_DOS_DRIVES );
3945 break;
3947 case 0x0f: /* OPEN FILE USING FCB */
3948 INT21_OpenFileUsingFCB( context );
3949 break;
3951 case 0x10: /* CLOSE FILE USING FCB */
3952 INT21_CloseFileUsingFCB( context );
3953 break;
3955 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
3956 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
3957 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
3958 if (!INT21_FindFirstFCB(context))
3960 SET_AL( context, 0xff );
3961 break;
3963 /* else fall through */
3965 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
3966 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
3967 break;
3969 case 0x13: /* DELETE FILE USING FCB */
3970 INT_BARF( context, 0x21 );
3971 break;
3973 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
3974 INT21_SequentialReadFromFCB( context );
3975 break;
3977 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
3978 INT21_SequentialWriteToFCB( context );
3979 break;
3981 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
3982 case 0x17: /* RENAME FILE USING FCB */
3983 INT_BARF( context, 0x21 );
3984 break;
3986 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
3987 SET_AL( context, 0 );
3988 break;
3990 case 0x19: /* GET CURRENT DEFAULT DRIVE */
3991 SET_AL( context, INT21_GetCurrentDrive() );
3992 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context ) );
3993 break;
3995 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
3996 TRACE( "SET DISK TRANSFER AREA ADDRESS %04lX:%04X\n",
3997 context->SegDs, DX_reg(context) );
3999 TDB *task = GlobalLock16( GetCurrentTask() );
4000 task->dta = MAKESEGPTR( context->SegDs, DX_reg(context) );
4002 break;
4004 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4005 if (!INT21_GetDriveAllocInfo(context, 0))
4006 SET_AX( context, 0xffff );
4007 break;
4009 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4010 if (!INT21_GetDriveAllocInfo(context, DL_reg(context)))
4011 SET_AX( context, 0xffff );
4012 break;
4014 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4015 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4016 SET_AL( context, 0 );
4017 break;
4019 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4021 BYTE drive = INT21_MapDrive( 0 );
4022 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4024 if (INT21_FillDrivePB( drive ))
4026 SET_AL( context, 0x00 ); /* success */
4027 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4028 context->SegDs = INT21_GetHeapSelector( context );
4030 else
4032 SET_AL( context, 0xff ); /* invalid or network drive */
4035 break;
4037 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4038 SET_AL( context, 0 );
4039 break;
4041 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4042 INT21_ReadRandomRecordFromFCB( context );
4043 break;
4045 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4046 INT21_WriteRandomRecordToFCB( context );
4047 break;
4049 case 0x23: /* GET FILE SIZE FOR FCB */
4050 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4051 INT_BARF( context, 0x21 );
4052 break;
4054 case 0x25: /* SET INTERRUPT VECTOR */
4055 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4057 FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
4058 DOSVM_SetPMHandler16( AL_reg(context), ptr );
4060 break;
4062 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4063 INT_BARF( context, 0x21 );
4064 break;
4066 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4067 INT21_RandomBlockReadFromFCB( context );
4068 break;
4070 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4071 INT21_RandomBlockWriteToFCB( context );
4072 break;
4074 case 0x29: /* PARSE FILENAME INTO FCB */
4075 INT21_ParseFileNameIntoFCB(context);
4076 break;
4078 case 0x2a: /* GET SYSTEM DATE */
4079 TRACE( "GET SYSTEM DATE\n" );
4081 SYSTEMTIME systime;
4082 GetLocalTime( &systime );
4083 SET_CX( context, systime.wYear );
4084 SET_DH( context, systime.wMonth );
4085 SET_DL( context, systime.wDay );
4086 SET_AL( context, systime.wDayOfWeek );
4088 break;
4090 case 0x2b: /* SET SYSTEM DATE */
4091 TRACE( "SET SYSTEM DATE\n" );
4093 WORD year = CX_reg(context);
4094 BYTE month = DH_reg(context);
4095 BYTE day = DL_reg(context);
4097 if (year >= 1980 && year <= 2099 &&
4098 month >= 1 && month <= 12 &&
4099 day >= 1 && day <= 31)
4101 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4102 day, month, year );
4103 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4105 else
4107 SET_AL( context, 0xff ); /* invalid date */
4108 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4109 day, month, year );
4112 break;
4114 case 0x2c: /* GET SYSTEM TIME */
4115 TRACE( "GET SYSTEM TIME\n" );
4117 SYSTEMTIME systime;
4118 GetLocalTime( &systime );
4119 SET_CH( context, systime.wHour );
4120 SET_CL( context, systime.wMinute );
4121 SET_DH( context, systime.wSecond );
4122 SET_DL( context, systime.wMilliseconds / 10 );
4124 break;
4126 case 0x2d: /* SET SYSTEM TIME */
4127 if( CH_reg(context) >= 24 || CL_reg(context) >= 60 || DH_reg(context) >= 60 || DL_reg(context) >= 100 ) {
4128 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4129 CH_reg(context), CL_reg(context),
4130 DH_reg(context), DL_reg(context) );
4131 SET_AL( context, 0xFF );
4133 else
4135 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4136 CH_reg(context), CL_reg(context),
4137 DH_reg(context), DL_reg(context) );
4138 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4140 break;
4142 case 0x2e: /* SET VERIFY FLAG */
4143 TRACE("SET VERIFY FLAG ignored\n");
4144 /* we cannot change the behaviour anyway, so just ignore it */
4145 break;
4147 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4148 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4150 TDB *task = GlobalLock16( GetCurrentTask() );
4151 context->SegEs = SELECTOROF( task->dta );
4152 SET_BX( context, OFFSETOF( task->dta ) );
4154 break;
4156 case 0x30: /* GET DOS VERSION */
4157 TRACE( "GET DOS VERSION - %s requested\n",
4158 (AL_reg(context) == 0x00) ? "OEM number" : "version flag" );
4160 if (AL_reg(context) == 0x00)
4161 SET_BH( context, 0xff ); /* OEM number => undefined */
4162 else
4163 SET_BH( context, 0x08 ); /* version flag => DOS is in ROM */
4165 SET_AL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4166 SET_AH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4168 SET_BL( context, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4169 SET_CX( context, 0x3456 );
4170 break;
4172 case 0x31: /* TERMINATE AND STAY RESIDENT */
4173 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4174 break;
4176 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4178 BYTE drive = INT21_MapDrive( DL_reg(context) );
4179 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context) );
4181 if (INT21_FillDrivePB( drive ))
4183 SET_AL( context, 0x00 ); /* success */
4184 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4185 context->SegDs = INT21_GetHeapSelector( context );
4187 else
4189 SET_AL( context, 0xff ); /* invalid or network drive */
4192 break;
4194 case 0x33: /* MULTIPLEXED */
4195 switch (AL_reg(context))
4197 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4198 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4199 SET_DL( context, brk_flag );
4200 break;
4202 case 0x01: /* SET EXTENDED BREAK STATE */
4203 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4204 brk_flag = (DL_reg(context) > 0) ? 1 : 0;
4205 break;
4207 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4208 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4209 /* ugly coding in order to stay reentrant */
4210 if (DL_reg(context))
4212 SET_DL( context, brk_flag );
4213 brk_flag = 1;
4215 else
4217 SET_DL( context, brk_flag );
4218 brk_flag = 0;
4220 break;
4222 case 0x05: /* GET BOOT DRIVE */
4223 TRACE("GET BOOT DRIVE\n");
4224 SET_DL( context, 3 );
4225 /* c: is Wine's bootdrive (a: is 1)*/
4226 break;
4228 case 0x06: /* GET TRUE VERSION NUMBER */
4229 TRACE("GET TRUE VERSION NUMBER\n");
4230 SET_BL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4231 SET_BH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4232 SET_DL( context, 0x00 ); /* revision */
4233 SET_DH( context, 0x08 ); /* DOS is in ROM */
4234 break;
4236 default:
4237 INT_BARF( context, 0x21 );
4238 break;
4240 break;
4242 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4243 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4244 context->SegEs = INT21_GetHeapSelector( context );
4245 SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
4246 break;
4248 case 0x35: /* GET INTERRUPT VECTOR */
4249 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4251 FARPROC16 addr = DOSVM_GetPMHandler16( AL_reg(context) );
4252 context->SegEs = SELECTOROF(addr);
4253 SET_BX( context, OFFSETOF(addr) );
4255 break;
4257 case 0x36: /* GET FREE DISK SPACE */
4258 TRACE("GET FREE DISK SPACE FOR DRIVE %s (limited to about 1GB)\n",
4259 INT21_DriveName( DL_reg(context) ));
4260 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
4261 break;
4263 case 0x37: /* SWITCHAR */
4265 switch (AL_reg(context))
4267 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4268 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4269 SET_AL( context, 0x00 ); /* success*/
4270 SET_DL( context, '/' );
4271 break;
4272 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4273 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4274 DL_reg( context ));
4275 SET_AL( context, 0x00 ); /* success*/
4276 break;
4277 default:
4278 INT_BARF( context, 0x21 );
4279 break;
4282 break;
4284 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4285 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4286 if (AL_reg(context))
4288 WORD country = AL_reg(context);
4289 if (country == 0xff)
4290 country = BX_reg(context);
4291 if (country != INT21_GetSystemCountryCode()) {
4292 FIXME( "Requested info on non-default country %04x\n", country );
4293 SET_AX(context, 2);
4294 SET_CFLAG(context);
4297 if(AX_reg(context) != 2 )
4299 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context,
4300 context->SegDs,
4301 context->Edx) );
4302 SET_AX( context, INT21_GetSystemCountryCode() );
4303 SET_BX( context, INT21_GetSystemCountryCode() );
4304 RESET_CFLAG(context);
4306 break;
4308 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4309 if (!INT21_CreateDirectory( context ))
4310 bSetDOSExtendedError = TRUE;
4311 else
4312 RESET_CFLAG(context);
4313 break;
4315 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4317 WCHAR dirW[MAX_PATH];
4318 char *dirA = CTX_SEG_OFF_TO_LIN(context,
4319 context->SegDs, context->Edx);
4321 TRACE( "REMOVE DIRECTORY %s\n", dirA );
4323 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
4325 if (!RemoveDirectoryW( dirW ))
4326 bSetDOSExtendedError = TRUE;
4327 else
4328 RESET_CFLAG(context);
4330 break;
4332 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4333 if (!INT21_SetCurrentDirectory( context ))
4334 bSetDOSExtendedError = TRUE;
4335 else
4336 RESET_CFLAG(context);
4337 break;
4339 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4340 if (!INT21_CreateFile( context, context->Edx, FALSE,
4341 OF_READWRITE | OF_SHARE_COMPAT, 0x12 ))
4342 bSetDOSExtendedError = TRUE;
4343 else
4344 RESET_CFLAG(context);
4345 break;
4347 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4348 if (!INT21_CreateFile( context, context->Edx, FALSE,
4349 AL_reg(context), 0x01 ))
4350 bSetDOSExtendedError = TRUE;
4351 else
4352 RESET_CFLAG(context);
4353 break;
4355 case 0x3e: /* "CLOSE" - CLOSE FILE */
4356 TRACE( "CLOSE handle %d\n", BX_reg(context) );
4357 if (_lclose16( BX_reg(context) ) == HFILE_ERROR16)
4358 bSetDOSExtendedError = TRUE;
4359 else
4360 RESET_CFLAG(context);
4361 break;
4363 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4364 TRACE( "READ from %d to %04lX:%04X for %d bytes\n",
4365 BX_reg(context),
4366 context->SegDs,
4367 DX_reg(context),
4368 CX_reg(context) );
4370 DWORD result;
4371 WORD count = CX_reg(context);
4372 BYTE *buffer = CTX_SEG_OFF_TO_LIN( context,
4373 context->SegDs,
4374 context->Edx );
4376 /* Some programs pass a count larger than the allocated buffer */
4377 DWORD maxcount = GetSelectorLimit16( context->SegDs ) - DX_reg(context) + 1;
4378 if (count > maxcount) count = maxcount;
4381 * FIXME: Reading from console (BX=1) in DOS mode
4382 * does not work as it is supposed to work.
4385 RESET_CFLAG(context); /* set if error */
4386 if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context)), buffer, count, &result, NULL ))
4387 SET_AX( context, (WORD)result );
4388 else
4389 bSetDOSExtendedError = TRUE;
4391 break;
4393 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4394 TRACE( "WRITE from %04lX:%04X to handle %d for %d byte\n",
4395 context->SegDs, DX_reg(context),
4396 BX_reg(context), CX_reg(context) );
4398 char *ptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4399 HFILE handle = (HFILE)DosFileHandleToWin32Handle(BX_reg(context));
4400 LONG result = _hwrite( handle, ptr, CX_reg(context) );
4401 if (result == HFILE_ERROR)
4402 bSetDOSExtendedError = TRUE;
4403 else
4405 SET_AX( context, (WORD)result );
4406 RESET_CFLAG(context);
4409 break;
4411 case 0x41: /* "UNLINK" - DELETE FILE */
4413 WCHAR fileW[MAX_PATH];
4414 char *fileA = CTX_SEG_OFF_TO_LIN(context,
4415 context->SegDs,
4416 context->Edx);
4418 TRACE( "UNLINK %s\n", fileA );
4419 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
4421 if (!DeleteFileW( fileW ))
4422 bSetDOSExtendedError = TRUE;
4423 else
4424 RESET_CFLAG(context);
4426 break;
4428 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4429 TRACE( "LSEEK handle %d offset %ld from %s\n",
4430 BX_reg(context),
4431 MAKELONG( DX_reg(context), CX_reg(context) ),
4432 (AL_reg(context) == 0) ?
4433 "start of file" : ((AL_reg(context) == 1) ?
4434 "current file position" : "end of file") );
4436 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4437 LONG offset = MAKELONG( DX_reg(context), CX_reg(context) );
4438 DWORD status = SetFilePointer( handle, offset,
4439 NULL, AL_reg(context) );
4440 if (status == INVALID_SET_FILE_POINTER)
4441 bSetDOSExtendedError = TRUE;
4442 else
4444 SET_AX( context, LOWORD(status) );
4445 SET_DX( context, HIWORD(status) );
4446 RESET_CFLAG(context);
4449 break;
4451 case 0x43: /* FILE ATTRIBUTES */
4452 if (!INT21_FileAttributes( context, AL_reg(context), FALSE ))
4453 bSetDOSExtendedError = TRUE;
4454 else
4455 RESET_CFLAG(context);
4456 break;
4458 case 0x44: /* IOCTL */
4459 INT21_Ioctl( context );
4460 break;
4462 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4463 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context) );
4465 HANDLE handle32;
4466 HFILE handle16 = HFILE_ERROR;
4468 if (DuplicateHandle( GetCurrentProcess(),
4469 DosFileHandleToWin32Handle(BX_reg(context)),
4470 GetCurrentProcess(),
4471 &handle32,
4472 0, TRUE, DUPLICATE_SAME_ACCESS ))
4473 handle16 = Win32HandleToDosFileHandle(handle32);
4475 if (handle16 == HFILE_ERROR)
4476 bSetDOSExtendedError = TRUE;
4477 else
4479 SET_AX( context, handle16 );
4480 RESET_CFLAG(context);
4483 break;
4485 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4486 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4487 BX_reg(context), CX_reg(context) );
4488 if (!INT21_Dup2(BX_reg(context), CX_reg(context)))
4489 bSetDOSExtendedError = TRUE;
4490 else
4491 RESET_CFLAG(context);
4492 break;
4494 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4495 if (!INT21_GetCurrentDirectory( context, FALSE ))
4496 bSetDOSExtendedError = TRUE;
4497 else
4498 RESET_CFLAG(context);
4499 break;
4501 case 0x48: /* ALLOCATE MEMORY */
4502 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
4504 DWORD bytes = (DWORD)BX_reg(context) << 4;
4505 DWORD rv = GlobalDOSAlloc16( bytes );
4506 WORD selector = LOWORD( rv );
4508 if (selector)
4510 SET_AX( context, selector );
4511 RESET_CFLAG(context);
4513 else
4515 SET_CFLAG(context);
4516 SET_AX( context, 0x0008 ); /* insufficient memory */
4517 SET_BX( context, DOSMEM_Available() >> 4 );
4520 break;
4522 case 0x49: /* FREE MEMORY */
4523 TRACE( "FREE MEMORY segment %04lX\n", context->SegEs );
4525 BOOL ok = !GlobalDOSFree16( context->SegEs );
4527 /* If we don't reset ES_reg, we will fail in the relay code */
4528 if (ok) context->SegEs = 0;
4529 else
4531 TRACE("FREE MEMORY failed\n");
4532 SET_CFLAG(context);
4533 SET_AX( context, 0x0009 ); /* memory block address invalid */
4536 break;
4538 case 0x4a: /* RESIZE MEMORY BLOCK */
4539 TRACE( "RESIZE MEMORY segment %04lX to %d paragraphs\n",
4540 context->SegEs, BX_reg(context) );
4542 FIXME( "Resize memory block - unsupported under Win16\n" );
4543 SET_CFLAG(context);
4545 break;
4547 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4549 char *program = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4550 HINSTANCE16 instance;
4552 TRACE( "EXEC %s\n", program );
4554 RESET_CFLAG(context);
4555 instance = WinExec16( program, SW_NORMAL );
4556 if (instance < 32)
4558 SET_CFLAG( context );
4559 SET_AX( context, instance );
4562 break;
4564 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
4565 TRACE( "EXIT with return code %d\n", AL_reg(context) );
4566 DOSVM_Exit( AL_reg(context) );
4567 break;
4569 case 0x4d: /* GET RETURN CODE */
4570 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
4571 SET_AX( context, 0 );
4572 break;
4574 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
4575 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
4576 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
4577 if (!INT21_FindFirst(context)) break;
4578 /* fall through */
4580 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
4581 TRACE("FINDNEXT\n");
4582 if (!INT21_FindNext(context))
4584 SetLastError( ERROR_NO_MORE_FILES );
4585 SET_AX( context, ERROR_NO_MORE_FILES );
4586 SET_CFLAG(context);
4588 else SET_AX( context, 0 ); /* OK */
4589 break;
4591 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
4592 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
4593 DOSVM_psp = BX_reg(context);
4594 break;
4596 case 0x51: /* GET PSP ADDRESS */
4597 INT21_GetPSP( context );
4598 break;
4600 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
4601 TRACE("Get List of Lists - not supported\n");
4602 context->SegEs = 0;
4603 SET_BX( context, 0 );
4604 break;
4606 case 0x54: /* Get Verify Flag */
4607 TRACE("Get Verify Flag - Not Supported\n");
4608 SET_AL( context, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
4609 break;
4611 case 0x56: /* "RENAME" - RENAME FILE */
4612 if (!INT21_RenameFile( context ))
4613 bSetDOSExtendedError = TRUE;
4614 else
4615 RESET_CFLAG(context);
4616 break;
4618 case 0x57: /* FILE DATE AND TIME */
4619 if (!INT21_FileDateTime( context ))
4620 bSetDOSExtendedError = TRUE;
4621 else
4622 RESET_CFLAG(context);
4623 break;
4625 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
4626 switch (AL_reg(context))
4628 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
4629 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
4630 SET_AX( context, 0 ); /* low memory first fit */
4631 break;
4633 case 0x01: /* SET ALLOCATION STRATEGY */
4634 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
4635 BL_reg(context) );
4636 break;
4638 case 0x02: /* GET UMB LINK STATE */
4639 TRACE( "GET UMB LINK STATE\n" );
4640 SET_AL( context, 0 ); /* UMBs not part of DOS memory chain */
4641 break;
4643 case 0x03: /* SET UMB LINK STATE */
4644 TRACE( "SET UMB LINK STATE to %d - ignored\n",
4645 BX_reg(context) );
4646 break;
4648 default:
4649 INT_BARF( context, 0x21 );
4651 break;
4653 case 0x59: /* GET EXTENDED ERROR INFO */
4654 INT21_GetExtendedError( context );
4655 break;
4657 case 0x5a: /* CREATE TEMPORARY FILE */
4658 TRACE("CREATE TEMPORARY FILE\n");
4659 bSetDOSExtendedError = !INT21_CreateTempFile(context);
4660 break;
4662 case 0x5b: /* CREATE NEW FILE */
4663 if (!INT21_CreateFile( context, context->Edx, FALSE,
4664 OF_READWRITE | OF_SHARE_COMPAT, 0x10 ))
4665 bSetDOSExtendedError = TRUE;
4666 else
4667 RESET_CFLAG(context);
4668 break;
4670 case 0x5c: /* "FLOCK" - RECORD LOCKING */
4672 DWORD offset = MAKELONG(DX_reg(context), CX_reg(context));
4673 DWORD length = MAKELONG(DI_reg(context), SI_reg(context));
4674 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4676 RESET_CFLAG(context);
4677 switch (AL_reg(context))
4679 case 0x00: /* LOCK */
4680 TRACE( "lock handle %d offset %ld length %ld\n",
4681 BX_reg(context), offset, length );
4682 if (!LockFile( handle, offset, 0, length, 0 ))
4683 bSetDOSExtendedError = TRUE;
4684 break;
4686 case 0x01: /* UNLOCK */
4687 TRACE( "unlock handle %d offset %ld length %ld\n",
4688 BX_reg(context), offset, length );
4689 if (!UnlockFile( handle, offset, 0, length, 0 ))
4690 bSetDOSExtendedError = TRUE;
4691 break;
4693 default:
4694 INT_BARF( context, 0x21 );
4697 break;
4699 case 0x5d: /* NETWORK 5D */
4700 FIXME( "Network function 5D not implemented.\n" );
4701 SetLastError( ER_NoNetwork );
4702 bSetDOSExtendedError = TRUE;
4703 break;
4705 case 0x5e: /* NETWORK 5E */
4706 bSetDOSExtendedError = INT21_NetworkFunc( context);
4707 break;
4709 case 0x5f: /* NETWORK 5F */
4710 /* FIXME: supporting this would need to 1:
4711 * - implement per drive current directory (as kernel32 doesn't)
4712 * - assign enabled/disabled flag on a per drive basis
4714 /* network software not installed */
4715 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
4716 SetLastError( ER_NoNetwork );
4717 bSetDOSExtendedError = TRUE;
4718 break;
4720 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
4722 WCHAR pathW[MAX_PATH], res[MAX_PATH];
4723 /* FIXME: likely to be broken */
4725 TRACE("TRUENAME %s\n",
4726 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
4727 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
4728 if (!GetFullPathNameW( pathW, 128, res, NULL ))
4729 bSetDOSExtendedError = TRUE;
4730 else
4732 SET_AX( context, 0 );
4733 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
4734 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
4735 128, NULL, NULL);
4738 break;
4740 case 0x61: /* UNUSED */
4741 SET_AL( context, 0 );
4742 break;
4744 case 0x62: /* GET PSP ADDRESS */
4745 INT21_GetPSP( context );
4746 break;
4748 case 0x63: /* MISC. LANGUAGE SUPPORT */
4749 switch (AL_reg(context)) {
4750 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
4751 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
4752 context->SegDs = INT21_GetHeapSelector( context );
4753 SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
4754 SET_AL( context, 0 ); /* success */
4755 break;
4757 break;
4759 case 0x64: /* OS/2 DOS BOX */
4760 INT_BARF( context, 0x21 );
4761 SET_CFLAG(context);
4762 break;
4764 case 0x65: /* EXTENDED COUNTRY INFORMATION */
4765 INT21_ExtendedCountryInformation( context );
4766 break;
4768 case 0x66: /* GLOBAL CODE PAGE TABLE */
4769 switch (AL_reg(context))
4771 case 0x01:
4772 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
4773 SET_BX( context, GetOEMCP() );
4774 SET_DX( context, GetOEMCP() );
4775 break;
4776 case 0x02:
4777 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
4778 BX_reg(context), DX_reg(context) );
4779 break;
4781 break;
4783 case 0x67: /* SET HANDLE COUNT */
4784 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
4785 if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
4786 bSetDOSExtendedError = TRUE;
4787 break;
4789 case 0x68: /* "FFLUSH" - COMMIT FILE */
4790 TRACE( "FFLUSH - handle %d\n", BX_reg(context) );
4791 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ) &&
4792 GetLastError() != ERROR_ACCESS_DENIED)
4793 bSetDOSExtendedError = TRUE;
4794 break;
4796 case 0x69: /* DISK SERIAL NUMBER */
4797 switch (AL_reg(context))
4799 case 0x00:
4800 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
4801 INT21_DriveName(BL_reg(context)));
4802 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
4803 else SET_AX( context, 0 );
4804 break;
4806 case 0x01:
4807 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
4808 INT21_DriveName(BL_reg(context)));
4809 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
4810 else SET_AX( context, 1 );
4811 break;
4813 break;
4815 case 0x6a: /* COMMIT FILE */
4816 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context) );
4817 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ) &&
4818 GetLastError() != ERROR_ACCESS_DENIED)
4819 bSetDOSExtendedError = TRUE;
4820 break;
4822 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4823 SET_AL( context, 0 );
4824 break;
4826 case 0x6c: /* EXTENDED OPEN/CREATE */
4827 if (!INT21_CreateFile( context, context->Esi, TRUE,
4828 BX_reg(context), DL_reg(context) ))
4829 bSetDOSExtendedError = TRUE;
4830 break;
4832 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
4833 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
4834 SET_CFLAG( context );
4835 SET_AL( context, 0 );
4836 break;
4838 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
4839 INT21_LongFilename( context );
4840 break;
4842 case 0x73: /* MSDOS7 - FAT32 */
4843 RESET_CFLAG( context );
4844 if (!INT21_Fat32( context ))
4845 bSetDOSExtendedError = TRUE;
4846 break;
4848 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
4849 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
4850 break;
4852 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
4853 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
4854 break;
4856 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
4857 /* we don't implement a DOS32 extender */
4858 TRACE( "DOS32 EXTENDER API - ignored\n" );
4859 break;
4861 default:
4862 INT_BARF( context, 0x21 );
4863 break;
4865 } /* END OF SWITCH */
4867 /* Set general error condition. */
4868 if (bSetDOSExtendedError)
4870 SET_AX( context, GetLastError() );
4871 SET_CFLAG( context );
4874 /* Print error code if carry flag is set. */
4875 if (context->EFlags & 0x0001)
4876 TRACE("failed, error %ld\n", GetLastError() );
4878 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
4879 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
4880 AX_reg(context), BX_reg(context),
4881 CX_reg(context), DX_reg(context),
4882 SI_reg(context), DI_reg(context),
4883 (WORD)context->SegDs, (WORD)context->SegEs,
4884 context->EFlags );