winegstreamer: Move the "flip" field to struct wg_parser_stream.
[wine/zf.git] / dlls / krnl386.exe16 / int21.c
blob24909eadddc0b1a7cff202186532088ba96ce923
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/server.h"
41 #include "wine/debug.h"
42 #include "wine/exception.h"
45 * Note:
46 * - Most of the file related functions are wrong. NT's kernel32
47 * doesn't maintain a per drive current directory, while DOS does.
48 * We should in fact keep track in here of those per drive
49 * directories, and use this info while dealing with partial paths
50 * (drive defined, but only relative paths). This could even be
51 * created as an array of CDS (there should be an entry for that in
52 * the LOL)
56 * Forward declarations.
58 static BOOL INT21_RenameFile( CONTEXT *context );
60 WINE_DEFAULT_DEBUG_CHANNEL(int21);
63 #include "pshpack1.h"
66 * Extended Drive Parameter Block.
67 * This structure is compatible with standard DOS4+ DPB and
68 * extended DOS7 DPB.
70 typedef struct _INT21_DPB {
71 BYTE drive; /* 00 drive number (0=A, ...) */
72 BYTE unit; /* 01 unit number within device driver */
73 WORD sector_bytes; /* 02 bytes per sector */
74 BYTE cluster_sectors; /* 04 highest sector number within a cluster */
75 BYTE shift; /* 05 shift count to convert clusters into sectors */
76 WORD num_reserved; /* 06 reserved sectors at beginning of drive */
77 BYTE num_FAT; /* 08 number of FATs */
78 WORD num_root_entries; /* 09 number of root directory entries */
79 WORD first_data_sector; /* 0b number of first sector containing user data */
80 WORD num_clusters1; /* 0d highest cluster number (number of data clusters + 1) */
81 WORD sectors_per_FAT; /* 0f number of sectors per FAT */
82 WORD first_dir_sector; /* 11 sector number of first directory sector */
83 SEGPTR driver_header; /* 13 address of device driver header */
84 BYTE media_ID; /* 17 media ID byte */
85 BYTE access_flag; /* 18 0x00 if disk accessed, 0xff if not */
86 SEGPTR next; /* 19 pointer to next DPB */
87 WORD search_cluster1; /* 1d cluster at which to start search for free space */
88 WORD free_clusters_lo; /* 1f number of free clusters on drive or 0xffff if unknown */
89 WORD free_clusters_hi; /* 21 hiword of clusters_free */
90 WORD mirroring_flags; /* 23 active FAT/mirroring flags */
91 WORD info_sector; /* 25 sector number of file system info sector or 0xffff for none */
92 WORD spare_boot_sector; /* 27 sector number of backup boot sector or 0xffff for none */
93 DWORD first_cluster_sector; /* 29 sector number of the first cluster */
94 DWORD num_clusters2; /* 2d maximum cluster number */
95 DWORD fat_clusters; /* 31 number of clusters occupied by FAT */
96 DWORD root_cluster; /* 35 cluster number of start of root directory */
97 DWORD search_cluster2; /* 39 cluster at which to start searching for free space */
98 } INT21_DPB;
102 * Structure for DOS data that can be accessed directly from applications.
103 * Real and protected mode pointers will be returned to this structure so
104 * the structure must be correctly packed.
106 typedef struct _INT21_HEAP {
107 WORD uppercase_size; /* Size of the following table in bytes */
108 BYTE uppercase_table[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
110 WORD lowercase_size; /* Size of the following table in bytes */
111 BYTE lowercase_table[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
113 WORD collating_size; /* Size of the following table in bytes */
114 BYTE collating_table[256]; /* Values used to sort characters from 0x00 to 0xff. */
116 WORD filename_size; /* Size of the following filename data in bytes */
117 BYTE filename_reserved1; /* 0x01 for MS-DOS 3.30-6.00 */
118 BYTE filename_lowest; /* Lowest permissible character value for filename */
119 BYTE filename_highest; /* Highest permissible character value for filename */
120 BYTE filename_reserved2; /* 0x00 for MS-DOS 3.30-6.00 */
121 BYTE filename_exclude_first; /* First illegal character in permissible range */
122 BYTE filename_exclude_last; /* Last illegal character in permissible range */
123 BYTE filename_reserved3; /* 0x02 for MS-DOS 3.30-6.00 */
124 BYTE filename_illegal_size; /* Number of terminators in the following table */
125 BYTE filename_illegal_table[16]; /* Characters which terminate a filename */
127 WORD dbcs_size; /* Number of valid ranges in the following table */
128 BYTE dbcs_table[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
130 BYTE misc_indos; /* Interrupt 21 nesting flag */
131 WORD misc_selector; /* Protected mode selector for INT21_HEAP */
132 INT21_DPB misc_dpb_list[MAX_DOS_DRIVES]; /* Drive parameter blocks for all drives */
134 } INT21_HEAP;
137 struct FCB {
138 BYTE drive_number;
139 CHAR file_name[8];
140 CHAR file_extension[3];
141 WORD current_block_number;
142 WORD logical_record_size;
143 DWORD file_size;
144 WORD date_of_last_write;
145 WORD time_of_last_write;
146 BYTE file_number;
147 BYTE attributes;
148 WORD starting_cluster;
149 WORD sequence_number;
150 BYTE file_attributes;
151 BYTE unused;
152 BYTE record_within_current_block;
153 BYTE random_access_record_number[4];
157 struct XFCB {
158 BYTE xfcb_signature;
159 BYTE reserved[5];
160 BYTE xfcb_file_attribute;
161 BYTE fcb[37];
164 /* DTA layout for FindFirst/FindNext */
165 typedef struct
167 BYTE drive; /* 00 drive letter */
168 char mask[11]; /* 01 search template */
169 BYTE search_attr; /* 0c search attributes */
170 WORD count; /* 0d entry count within directory */
171 WORD cluster; /* 0f cluster of parent directory */
172 WCHAR *fullPath; /* 11 full path (was: reserved) */
173 BYTE fileattr; /* 15 file attributes */
174 WORD filetime; /* 16 file time */
175 WORD filedate; /* 18 file date */
176 DWORD filesize; /* 1a file size */
177 char filename[13]; /* 1e file name + extension */
178 } FINDFILE_DTA;
180 /* FCB layout for FindFirstFCB/FindNextFCB */
181 typedef struct
183 BYTE drive; /* 00 drive letter */
184 char filename[11]; /* 01 filename 8+3 format */
185 int count; /* 0c entry count (was: reserved) */
186 WCHAR *fullPath; /* 10 full path (was: reserved) */
187 } FINDFILE_FCB;
189 /* DOS directory entry for FindFirstFCB/FindNextFCB */
190 typedef struct
192 char filename[11]; /* 00 filename 8+3 format */
193 BYTE fileattr; /* 0b file attributes */
194 BYTE reserved[10]; /* 0c reserved */
195 WORD filetime; /* 16 file time */
196 WORD filedate; /* 18 file date */
197 WORD cluster; /* 1a file first cluster */
198 DWORD filesize; /* 1c file size */
199 } DOS_DIRENTRY_LAYOUT;
201 #include "poppack.h"
203 /* dos file attributes */
204 #define FA_NORMAL 0x00 /* Normal file, no attributes */
205 #define FA_RDONLY 0x01 /* Read only attribute */
206 #define FA_HIDDEN 0x02 /* Hidden file */
207 #define FA_SYSTEM 0x04 /* System file */
208 #define FA_LABEL 0x08 /* Volume label */
209 #define FA_DIRECTORY 0x10 /* Directory */
210 #define FA_ARCHIVE 0x20 /* Archive */
211 #define FA_UNUSED 0x40 /* Unused */
213 /* Error codes */
214 #define ER_NoNetwork 0x49
216 /* Error classes */
217 #define EC_OutOfResource 0x01
218 #define EC_Temporary 0x02
219 #define EC_AccessDenied 0x03
220 #define EC_InternalError 0x04
221 #define EC_HardwareFailure 0x05
222 #define EC_SystemFailure 0x06
223 #define EC_ProgramError 0x07
224 #define EC_NotFound 0x08
225 #define EC_MediaError 0x0b
226 #define EC_Exists 0x0c
227 #define EC_Unknown 0x0d
229 /* Suggested actions */
230 #define SA_Retry 0x01
231 #define SA_DelayedRetry 0x02
232 #define SA_Abort 0x04
233 #define SA_Ignore 0x06
234 #define SA_Ask4Retry 0x07
236 /* Error locus */
237 #define EL_Unknown 0x01
238 #define EL_Disk 0x02
239 #define EL_Network 0x03
240 #define EL_Serial 0x04
241 #define EL_Memory 0x05
243 /* BIOS Keyboard Scancodes */
244 #define KEY_LEFT 0x4B
245 #define KEY_RIGHT 0x4D
246 #define KEY_UP 0x48
247 #define KEY_DOWN 0x50
248 #define KEY_IC 0x52 /* insert char */
249 #define KEY_DC 0x53 /* delete char */
250 #define KEY_BACKSPACE 0x0E
251 #define KEY_HOME 0x47
252 #define KEY_END 0x4F
253 #define KEY_NPAGE 0x49
254 #define KEY_PPAGE 0x51
256 static int brk_flag;
258 static BYTE drive_number( WCHAR letter )
260 if (letter >= 'A' && letter <= 'Z') return letter - 'A';
261 if (letter >= 'a' && letter <= 'z') return letter - 'a';
262 return MAX_DOS_DRIVES;
266 /* Many calls translate a drive argument like this:
267 drive number (00h = default, 01h = A:, etc)
269 /******************************************************************
270 * INT21_DriveName
272 * Many calls translate a drive argument like this:
273 * drive number (00h = default, 01h = A:, etc)
275 static const char *INT21_DriveName(int drive)
277 if (drive > 0)
279 if (drive <= 26) return wine_dbg_sprintf("%c:", 'A' + drive - 1);
280 else return wine_dbg_sprintf( "<Bad drive: %d>", drive);
282 return "default";
285 /***********************************************************************
286 * INT21_GetCurrentDrive
288 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
289 * MAX_DOS_DRIVES on error.
291 static BYTE INT21_GetCurrentDrive(void)
293 WCHAR current_directory[MAX_PATH];
295 if (!GetCurrentDirectoryW( MAX_PATH, current_directory ) ||
296 current_directory[1] != ':')
298 TRACE( "Failed to get current drive.\n" );
299 return MAX_DOS_DRIVES;
301 return drive_number( current_directory[0] );
305 /***********************************************************************
306 * INT21_MapDrive
308 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
309 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
311 static BYTE INT21_MapDrive( BYTE drive )
313 if (drive)
315 WCHAR drivespec[] = {'A', ':', 0};
316 UINT drivetype;
318 drivespec[0] += drive - 1;
319 drivetype = GetDriveTypeW( drivespec );
321 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
322 return MAX_DOS_DRIVES;
324 return drive - 1;
327 return INT21_GetCurrentDrive();
331 /***********************************************************************
332 * INT21_SetCurrentDrive
334 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
336 static void INT21_SetCurrentDrive( BYTE drive )
338 WCHAR drivespec[] = {'A', ':', 0};
340 drivespec[0] += drive;
342 if (!SetCurrentDirectoryW( drivespec ))
343 TRACE( "Failed to set current drive.\n" );
347 /***********************************************************************
348 * INT21_GetSystemCountryCode
350 * Return DOS country code for default system locale.
352 static WORD INT21_GetSystemCountryCode( void )
354 return GetSystemDefaultLangID();
358 /***********************************************************************
359 * INT21_FillCountryInformation
361 * Fill 34-byte buffer with country information data using
362 * default system locale.
364 static void INT21_FillCountryInformation( BYTE *buffer )
366 /* 00 - WORD: date format
367 * 00 = mm/dd/yy
368 * 01 = dd/mm/yy
369 * 02 = yy/mm/dd
371 *(WORD*)(buffer + 0) = 0; /* FIXME: Get from locale */
373 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
374 buffer[2] = '$'; /* FIXME: Get from locale */
375 buffer[3] = 0;
377 /* 07 - BYTE[2]: ASCIIZ thousands separator */
378 buffer[7] = 0; /* FIXME: Get from locale */
379 buffer[8] = 0;
381 /* 09 - BYTE[2]: ASCIIZ decimal separator */
382 buffer[9] = '.'; /* FIXME: Get from locale */
383 buffer[10] = 0;
385 /* 11 - BYTE[2]: ASCIIZ date separator */
386 buffer[11] = '/'; /* FIXME: Get from locale */
387 buffer[12] = 0;
389 /* 13 - BYTE[2]: ASCIIZ time separator */
390 buffer[13] = ':'; /* FIXME: Get from locale */
391 buffer[14] = 0;
393 /* 15 - BYTE: Currency format
394 * bit 2 = set if currency symbol replaces decimal point
395 * bit 1 = number of spaces between value and currency symbol
396 * bit 0 = 0 if currency symbol precedes value
397 * 1 if currency symbol follows value
399 buffer[15] = 0; /* FIXME: Get from locale */
401 /* 16 - BYTE: Number of digits after decimal in currency */
402 buffer[16] = 0; /* FIXME: Get from locale */
404 /* 17 - BYTE: Time format
405 * bit 0 = 0 if 12-hour clock
406 * 1 if 24-hour clock
408 buffer[17] = 1; /* FIXME: Get from locale */
410 /* 18 - DWORD: Address of case map routine */
411 *(DWORD*)(buffer + 18) = 0; /* FIXME: ptr to case map routine */
413 /* 22 - BYTE[2]: ASCIIZ data-list separator */
414 buffer[22] = ','; /* FIXME: Get from locale */
415 buffer[23] = 0;
417 /* 24 - BYTE[10]: Reserved */
418 memset( buffer + 24, 0, 10 );
422 /***********************************************************************
423 * INT21_FillHeap
425 * Initialize DOS heap.
427 * Filename Terminator Table of w2k DE NTVDM:
428 * 16 00 01 00 FF 00 00 20-02 0E 2E 22 2F 5C 5B 5D ....... ..."/\[]
429 * 3A 7C 3C 3E 2B 3D 3B 2C-00 :|<>+=;,
431 static void INT21_FillHeap( INT21_HEAP *heap )
433 static const char terminators[] = ".\"/\\[]:|<>+=;,";
434 int i;
437 * Uppercase table.
439 heap->uppercase_size = 128;
440 for (i = 0; i < 128; i++)
441 heap->uppercase_table[i] = toupper( 128 + i );
444 * Lowercase table.
446 heap->lowercase_size = 256;
447 for (i = 0; i < 256; i++)
448 heap->lowercase_table[i] = tolower( i );
451 * Collating table.
453 heap->collating_size = 256;
454 for (i = 0; i < 256; i++)
455 heap->collating_table[i] = i;
458 * Filename table.
460 heap->filename_size = 8 + strlen(terminators);
461 heap->filename_illegal_size = strlen(terminators);
462 memcpy( heap->filename_illegal_table, terminators, heap->filename_illegal_size );
464 heap->filename_reserved1 = 0x01;
465 heap->filename_lowest = 0x00;
466 heap->filename_highest = 0xff;
467 heap->filename_reserved2 = 0x00;
468 heap->filename_exclude_first = 0x00;
469 heap->filename_exclude_last = 0x20;
470 heap->filename_reserved3 = 0x02;
473 * DBCS lead byte table. This table is empty.
475 heap->dbcs_size = 0;
476 memset( heap->dbcs_table, 0, sizeof(heap->dbcs_table) );
479 * Initialize InDos flag.
481 heap->misc_indos = 0;
484 * FIXME: Should drive parameter blocks (DPB) be
485 * initialized here and linked to DOS LOL?
490 /***********************************************************************
491 * INT21_GetHeapPointer
493 * Get pointer for DOS heap (INT21_HEAP).
494 * Creates and initializes heap on first call.
496 static INT21_HEAP *INT21_GetHeapPointer( void )
498 static INT21_HEAP *heap_pointer = NULL;
500 if (!heap_pointer)
502 WORD heap_selector = GlobalAlloc16( GMEM_FIXED, sizeof(INT21_HEAP) );
503 heap_pointer = GlobalLock16( heap_selector );
504 heap_pointer->misc_selector = heap_selector;
505 INT21_FillHeap( heap_pointer );
508 return heap_pointer;
512 /***********************************************************************
513 * INT21_GetHeapSelector
515 * Get segment/selector for DOS heap (INT21_HEAP).
516 * Creates and initializes heap on first call.
518 static WORD INT21_GetHeapSelector( CONTEXT *context )
520 INT21_HEAP *heap = INT21_GetHeapPointer();
521 return heap->misc_selector;
525 /***********************************************************************
526 * INT21_FillDrivePB
528 * Fill DOS heap drive parameter block for the specified drive.
529 * Return TRUE if drive was valid and there were
530 * no errors while reading drive information.
532 static BOOL INT21_FillDrivePB( BYTE drive )
534 WCHAR drivespec[] = {'A', ':', 0};
535 INT21_HEAP *heap = INT21_GetHeapPointer();
536 INT21_DPB *dpb;
537 UINT drivetype;
538 DWORD cluster_sectors;
539 DWORD sector_bytes;
540 DWORD free_clusters;
541 DWORD total_clusters;
543 if (drive >= MAX_DOS_DRIVES)
544 return FALSE;
546 dpb = &heap->misc_dpb_list[drive];
547 drivespec[0] += drive;
548 drivetype = GetDriveTypeW( drivespec );
551 * FIXME: Does this check work correctly with floppy/cdrom drives?
553 if (drivetype == DRIVE_NO_ROOT_DIR || drivetype == DRIVE_UNKNOWN)
554 return FALSE;
557 * FIXME: Does this check work correctly with floppy/cdrom drives?
559 if (!GetDiskFreeSpaceW( drivespec, &cluster_sectors, &sector_bytes,
560 &free_clusters, &total_clusters ))
561 return FALSE;
564 * FIXME: Most of the values listed below are incorrect.
565 * All values should be validated.
568 dpb->drive = drive;
569 dpb->unit = 0;
570 dpb->sector_bytes = sector_bytes;
571 dpb->cluster_sectors = cluster_sectors - 1;
573 dpb->shift = 0;
574 while (cluster_sectors > 1)
576 cluster_sectors /= 2;
577 dpb->shift++;
580 dpb->num_reserved = 0;
581 dpb->num_FAT = 1;
582 dpb->num_root_entries = 2;
583 dpb->first_data_sector = 2;
584 dpb->num_clusters1 = total_clusters;
585 dpb->sectors_per_FAT = 1;
586 dpb->first_dir_sector = 1;
587 dpb->driver_header = 0;
588 dpb->media_ID = (drivetype == DRIVE_FIXED) ? 0xF8 : 0xF0;
589 dpb->access_flag = 0;
590 dpb->next = 0;
591 dpb->search_cluster1 = 0;
592 dpb->free_clusters_lo = LOWORD(free_clusters);
593 dpb->free_clusters_hi = HIWORD(free_clusters);
594 dpb->mirroring_flags = 0;
595 dpb->info_sector = 0xffff;
596 dpb->spare_boot_sector = 0xffff;
597 dpb->first_cluster_sector = 0;
598 dpb->num_clusters2 = total_clusters;
599 dpb->fat_clusters = 32;
600 dpb->root_cluster = 0;
601 dpb->search_cluster2 = 0;
603 return TRUE;
607 /***********************************************************************
608 * INT21_GetCurrentDirectory
610 * Handler for:
611 * - function 0x47
612 * - subfunction 0x47 of function 0x71
614 static BOOL INT21_GetCurrentDirectory( CONTEXT *context, BOOL islong )
616 char *buffer = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi);
617 BYTE drive = INT21_MapDrive( DL_reg(context) );
618 WCHAR pathW[MAX_PATH];
619 char pathA[MAX_PATH];
620 WCHAR *ptr = pathW;
622 TRACE( "drive %d\n", DL_reg(context) );
624 if (drive == MAX_DOS_DRIVES)
626 SetLastError(ERROR_INVALID_DRIVE);
627 return FALSE;
631 * Grab current directory.
634 if (!GetCurrentDirectoryW( MAX_PATH, pathW )) return FALSE;
636 if (drive_number( pathW[0] ) != drive || pathW[1] != ':')
638 /* cwd is not on the requested drive, get the environment string instead */
640 WCHAR env_var[4];
642 env_var[0] = '=';
643 env_var[1] = 'A' + drive;
644 env_var[2] = ':';
645 env_var[3] = 0;
646 if (!GetEnvironmentVariableW( env_var, pathW, MAX_PATH ))
648 /* return empty path */
649 buffer[0] = 0;
650 return TRUE;
655 * Convert into short format.
658 if (!islong)
660 DWORD result = GetShortPathNameW( pathW, pathW, MAX_PATH );
661 if (!result)
662 return FALSE;
663 if (result > MAX_PATH)
665 WARN( "Short path too long!\n" );
666 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
667 return FALSE;
672 * The returned pathname does not include
673 * the drive letter, colon or leading backslash.
676 if (ptr[0] == '\\')
679 * FIXME: We should probably just strip host part from name...
681 FIXME( "UNC names are not supported.\n" );
682 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
683 return FALSE;
685 else if (!ptr[0] || ptr[1] != ':' || ptr[2] != '\\')
687 WARN( "Path is neither UNC nor DOS path: %s\n",
688 wine_dbgstr_w(ptr) );
689 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
690 return FALSE;
692 else
694 /* Remove drive letter, colon and leading backslash. */
695 ptr += 3;
699 * Convert into OEM string.
702 if (!WideCharToMultiByte(CP_OEMCP, 0, ptr, -1, pathA,
703 MAX_PATH, NULL, NULL))
705 WARN( "Cannot convert path!\n" );
706 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
707 return FALSE;
711 * Success.
714 if (!islong)
716 /* Undocumented success code. */
717 SET_AX( context, 0x0100 );
719 /* Truncate buffer to 64 bytes. */
720 pathA[63] = 0;
723 TRACE( "%c:=%s\n", 'A' + drive, pathA );
725 strcpy( buffer, pathA );
726 return TRUE;
730 /***********************************************************************
731 * INT21_SetCurrentDirectory
733 * Handler for:
734 * - function 0x3b
735 * - subfunction 0x3b of function 0x71
737 static BOOL INT21_SetCurrentDirectory( CONTEXT *context )
739 WCHAR dirW[MAX_PATH];
740 WCHAR env_var[4];
741 DWORD attr;
742 char *dirA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
743 BYTE drive = INT21_GetCurrentDrive();
744 BOOL result;
746 TRACE( "SET CURRENT DIRECTORY %s\n", dirA );
748 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
749 if (!GetFullPathNameW( dirW, MAX_PATH, dirW, NULL )) return FALSE;
751 attr = GetFileAttributesW( dirW );
752 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
754 SetLastError( ERROR_PATH_NOT_FOUND );
755 return FALSE;
758 env_var[0] = '=';
759 env_var[1] = dirW[0];
760 env_var[2] = ':';
761 env_var[3] = 0;
762 result = SetEnvironmentVariableW( env_var, dirW );
764 /* only set current directory if on the current drive */
765 if (result && (drive_number( dirW[0] ) == drive)) result = SetCurrentDirectoryW( dirW );
767 return result;
770 /***********************************************************************
771 * INT21_CreateFile
773 * Handler for:
774 * - function 0x3c
775 * - function 0x3d
776 * - function 0x5b
777 * - function 0x6c
778 * - subfunction 0x6c of function 0x71
780 static BOOL INT21_CreateFile( CONTEXT *context,
781 DWORD pathSegOff,
782 BOOL returnStatus,
783 WORD dosAccessShare,
784 BYTE dosAction )
786 WORD dosStatus;
787 char *pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, pathSegOff);
788 WCHAR pathW[MAX_PATH];
789 DWORD winAccess;
790 DWORD winAttributes;
791 HANDLE winHandle;
792 DWORD winMode;
793 DWORD winSharing;
795 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
796 "create flags=%04x, file=%s.\n",
797 AH_reg(context), dosAction, dosAccessShare, CX_reg(context), pathA );
800 * Application tried to create/open a file whose name
801 * ends with a backslash. This is not allowed.
803 * FIXME: This needs to be validated, especially the return value.
805 if (pathA[strlen(pathA) - 1] == '/')
807 SetLastError( ERROR_FILE_NOT_FOUND );
808 return FALSE;
812 * Convert DOS action flags into Win32 creation disposition parameter.
814 switch(dosAction)
816 case 0x01:
817 winMode = OPEN_EXISTING;
818 break;
819 case 0x02:
820 winMode = TRUNCATE_EXISTING;
821 break;
822 case 0x10:
823 winMode = CREATE_NEW;
824 break;
825 case 0x11:
826 winMode = OPEN_ALWAYS;
827 break;
828 case 0x12:
829 winMode = CREATE_ALWAYS;
830 break;
831 default:
832 SetLastError( ERROR_INVALID_PARAMETER );
833 return FALSE;
837 * Convert DOS access/share flags into Win32 desired access parameter.
839 switch(dosAccessShare & 0x07)
841 case OF_READ:
842 winAccess = GENERIC_READ;
843 break;
844 case OF_WRITE:
845 winAccess = GENERIC_WRITE;
846 break;
847 case OF_READWRITE:
848 winAccess = GENERIC_READ | GENERIC_WRITE;
849 break;
850 case 0x04:
852 * Read-only, do not modify file's last-access time (DOS7).
854 * FIXME: How to prevent modification of last-access time?
856 winAccess = GENERIC_READ;
857 break;
858 default:
859 winAccess = 0;
863 * Convert DOS access/share flags into Win32 share mode parameter.
865 switch(dosAccessShare & 0x70)
867 case OF_SHARE_EXCLUSIVE:
868 winSharing = 0;
869 break;
870 case OF_SHARE_DENY_WRITE:
871 winSharing = FILE_SHARE_READ;
872 break;
873 case OF_SHARE_DENY_READ:
874 winSharing = FILE_SHARE_WRITE;
875 break;
876 case OF_SHARE_DENY_NONE:
877 case OF_SHARE_COMPAT:
878 default:
879 winSharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
883 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
884 * What to do with this bit?
885 * FIXME: Bits in the high byte of dosAccessShare are not supported.
886 * See both function 0x6c and subfunction 0x6c of function 0x71 for
887 * definition of these bits.
891 * Convert DOS create attributes into Win32 flags and attributes parameter.
893 if (winMode == OPEN_EXISTING || winMode == TRUNCATE_EXISTING)
895 winAttributes = 0;
897 else
899 WORD dosAttributes = CX_reg(context);
901 if (dosAttributes & FA_LABEL)
904 * Application tried to create volume label entry.
905 * This is difficult to support so we do not allow it.
907 * FIXME: If volume does not already have a label,
908 * this function is supposed to succeed.
910 SetLastError( ERROR_ACCESS_DENIED );
911 return TRUE;
914 winAttributes = dosAttributes &
915 (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
916 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE);
920 * Open the file.
922 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
924 winHandle = CreateFileW( pathW, winAccess, winSharing, NULL, winMode, winAttributes, 0 );
925 /* DOS allows opening files on a CDROM R/W */
926 if( winHandle == INVALID_HANDLE_VALUE &&
927 (GetLastError() == ERROR_WRITE_PROTECT ||
928 GetLastError() == ERROR_ACCESS_DENIED)) {
929 winHandle = CreateFileW( pathW, winAccess & ~GENERIC_WRITE,
930 winSharing, NULL, winMode, winAttributes, 0 );
933 if (winHandle == INVALID_HANDLE_VALUE)
934 return FALSE;
937 * Determine DOS file status.
939 * 1 = file opened
940 * 2 = file created
941 * 3 = file replaced
943 switch(winMode)
945 case OPEN_EXISTING:
946 dosStatus = 1;
947 break;
948 case TRUNCATE_EXISTING:
949 dosStatus = 3;
950 break;
951 case CREATE_NEW:
952 dosStatus = 2;
953 break;
954 case OPEN_ALWAYS:
955 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 1 : 2;
956 break;
957 case CREATE_ALWAYS:
958 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 3 : 2;
959 break;
960 default:
961 dosStatus = 0;
965 * Return DOS file handle and DOS status.
967 SET_AX( context, Win32HandleToDosFileHandle(winHandle) );
969 if (returnStatus)
970 SET_CX( context, dosStatus );
972 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
973 AX_reg(context), dosStatus );
975 return TRUE;
979 /***********************************************************************
980 * INT21_GetCurrentDTA
982 static BYTE *INT21_GetCurrentDTA( CONTEXT *context )
984 TDB *pTask = GlobalLock16(GetCurrentTask());
986 /* FIXME: This assumes DTA was set correctly! */
987 return CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
988 OFFSETOF(pTask->dta) );
992 /***********************************************************************
993 * INT21_OpenFileUsingFCB
995 * Handler for function 0x0f.
997 * PARAMS
998 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
1000 * RETURNS (in AL)
1001 * 0x00: successful
1002 * 0xff: failed
1004 * NOTES
1005 * Opens a FCB file for read/write in compatibility mode. Upon calling
1006 * the FCB must have the drive_number, file_name, and file_extension
1007 * fields filled and all other bytes cleared.
1009 static void INT21_OpenFileUsingFCB( CONTEXT *context )
1011 struct FCB *fcb;
1012 struct XFCB *xfcb;
1013 char file_path[16];
1014 char *pos;
1015 HANDLE handle;
1016 HFILE16 hfile16;
1017 BY_HANDLE_FILE_INFORMATION info;
1018 BYTE AL_result;
1020 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1021 if (fcb->drive_number == 0xff) {
1022 xfcb = (struct XFCB *) fcb;
1023 fcb = (struct FCB *) xfcb->fcb;
1024 } /* if */
1026 AL_result = 0;
1027 file_path[0] = 'A' + INT21_MapDrive( fcb->drive_number );
1029 if (AL_result == 0) {
1030 file_path[1] = ':';
1031 pos = &file_path[2];
1032 memcpy(pos, fcb->file_name, 8);
1033 pos[8] = ' ';
1034 pos[9] = '\0';
1035 pos = strchr(pos, ' ');
1036 *pos = '.';
1037 pos++;
1038 memcpy(pos, fcb->file_extension, 3);
1039 pos[3] = ' ';
1040 pos[4] = '\0';
1041 pos = strchr(pos, ' ');
1042 *pos = '\0';
1044 handle = (HANDLE) _lopen(file_path, OF_READWRITE);
1045 if (handle == INVALID_HANDLE_VALUE) {
1046 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path);
1047 AL_result = 0xff; /* failed */
1048 } else {
1049 hfile16 = Win32HandleToDosFileHandle(handle);
1050 if (hfile16 == HFILE_ERROR16) {
1051 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle);
1052 CloseHandle(handle);
1053 AL_result = 0xff; /* failed */
1054 } else if (hfile16 > 255) {
1055 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16, file_path);
1056 _lclose16(hfile16);
1057 AL_result = 0xff; /* failed */
1058 } else {
1059 if (!GetFileInformationByHandle(handle, &info)) {
1060 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1061 hfile16, handle, file_path);
1062 _lclose16(hfile16);
1063 AL_result = 0xff; /* failed */
1064 } else {
1065 fcb->drive_number = file_path[0] - 'A' + 1;
1066 fcb->current_block_number = 0;
1067 fcb->logical_record_size = 128;
1068 fcb->file_size = info.nFileSizeLow;
1069 FileTimeToDosDateTime(&info.ftLastWriteTime,
1070 &fcb->date_of_last_write, &fcb->time_of_last_write);
1071 fcb->file_number = hfile16;
1072 fcb->attributes = 0xc2;
1073 fcb->starting_cluster = 0; /* don't know correct init value */
1074 fcb->sequence_number = 0; /* don't know correct init value */
1075 fcb->file_attributes = info.dwFileAttributes;
1076 /* The following fields are not initialized */
1077 /* by the native function: */
1078 /* unused */
1079 /* record_within_current_block */
1080 /* random_access_record_number */
1082 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1083 file_path, hfile16, handle);
1084 AL_result = 0x00; /* successful */
1085 } /* if */
1086 } /* if */
1087 } /* if */
1088 } /* if */
1089 SET_AL(context, AL_result);
1093 /***********************************************************************
1094 * INT21_CloseFileUsingFCB
1096 * Handler for function 0x10.
1098 * PARAMS
1099 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1101 * RETURNS (in AL)
1102 * 0x00: successful
1103 * 0xff: failed
1105 * NOTES
1106 * Closes a FCB file.
1108 static void INT21_CloseFileUsingFCB( CONTEXT *context )
1110 struct FCB *fcb;
1111 struct XFCB *xfcb;
1112 BYTE AL_result;
1114 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1115 if (fcb->drive_number == 0xff) {
1116 xfcb = (struct XFCB *) fcb;
1117 fcb = (struct FCB *) xfcb->fcb;
1118 } /* if */
1120 if (_lclose16((HFILE16) fcb->file_number) != 0) {
1121 TRACE("_lclose16(%d) failed\n", fcb->file_number);
1122 AL_result = 0xff; /* failed */
1123 } else {
1124 TRACE("successful closed file %d\n", fcb->file_number);
1125 AL_result = 0x00; /* successful */
1126 } /* if */
1127 SET_AL(context, AL_result);
1131 /***********************************************************************
1132 * INT21_SequentialReadFromFCB
1134 * Handler for function 0x14.
1136 * PARAMS
1137 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1139 * RETURNS (in AL)
1140 * 0: successful
1141 * 1: end of file, no data read
1142 * 2: segment wrap in DTA, no data read (not returned now)
1143 * 3: end of file, partial record read
1145 * NOTES
1146 * Reads a record with the size FCB->logical_record_size from the FCB
1147 * to the disk transfer area. The position of the record is specified
1148 * with FCB->current_block_number and FCB->record_within_current_block.
1149 * Then FCB->current_block_number and FCB->record_within_current_block
1150 * are updated to point to the next record. If a partial record is
1151 * read, it is filled with zeros up to the FCB->logical_record_size.
1153 static void INT21_SequentialReadFromFCB( CONTEXT *context )
1155 struct FCB *fcb;
1156 struct XFCB *xfcb;
1157 HANDLE handle;
1158 DWORD record_number;
1159 DWORD position;
1160 BYTE *disk_transfer_area;
1161 UINT bytes_read;
1162 BYTE AL_result;
1164 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1165 if (fcb->drive_number == 0xff) {
1166 xfcb = (struct XFCB *) fcb;
1167 fcb = (struct FCB *) xfcb->fcb;
1168 } /* if */
1170 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1171 if (handle == INVALID_HANDLE_VALUE) {
1172 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1173 fcb->file_number);
1174 AL_result = 0x01; /* end of file, no data read */
1175 } else {
1176 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1177 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1178 if (position != record_number * fcb->logical_record_size) {
1179 TRACE("seek(%d, %d, 0) failed with %u\n",
1180 fcb->file_number, record_number * fcb->logical_record_size, position);
1181 AL_result = 0x01; /* end of file, no data read */
1182 } else {
1183 disk_transfer_area = INT21_GetCurrentDTA(context);
1184 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1185 if (bytes_read != fcb->logical_record_size) {
1186 TRACE("_lread(%d, %p, %d) failed with %d\n",
1187 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1188 if (bytes_read == 0) {
1189 AL_result = 0x01; /* end of file, no data read */
1190 } else {
1191 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1192 AL_result = 0x03; /* end of file, partial record read */
1193 } /* if */
1194 } else {
1195 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1196 bytes_read, record_number, position, fcb->file_number, handle);
1197 AL_result = 0x00; /* successful */
1198 } /* if */
1199 } /* if */
1200 } /* if */
1201 if (AL_result == 0x00 || AL_result == 0x03) {
1202 if (fcb->record_within_current_block == 127) {
1203 fcb->record_within_current_block = 0;
1204 fcb->current_block_number++;
1205 } else {
1206 fcb->record_within_current_block++;
1207 } /* if */
1208 } /* if */
1209 SET_AL(context, AL_result);
1213 /***********************************************************************
1214 * INT21_SequentialWriteToFCB
1216 * Handler for function 0x15.
1218 * PARAMS
1219 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1221 * RETURNS (in AL)
1222 * 0: successful
1223 * 1: disk full
1224 * 2: segment wrap in DTA (not returned now)
1226 * NOTES
1227 * Writes a record with the size FCB->logical_record_size from the disk
1228 * transfer area to the FCB. The position of the record is specified
1229 * with FCB->current_block_number and FCB->record_within_current_block.
1230 * Then FCB->current_block_number and FCB->record_within_current_block
1231 * are updated to point to the next record.
1233 static void INT21_SequentialWriteToFCB( CONTEXT *context )
1235 struct FCB *fcb;
1236 struct XFCB *xfcb;
1237 HANDLE handle;
1238 DWORD record_number;
1239 DWORD position;
1240 BYTE *disk_transfer_area;
1241 UINT bytes_written;
1242 BYTE AL_result;
1244 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1245 if (fcb->drive_number == 0xff) {
1246 xfcb = (struct XFCB *) fcb;
1247 fcb = (struct FCB *) xfcb->fcb;
1248 } /* if */
1250 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1251 if (handle == INVALID_HANDLE_VALUE) {
1252 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1253 fcb->file_number);
1254 AL_result = 0x01; /* disk full */
1255 } else {
1256 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1257 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1258 if (position != record_number * fcb->logical_record_size) {
1259 TRACE("seek(%d, %d, 0) failed with %u\n",
1260 fcb->file_number, record_number * fcb->logical_record_size, position);
1261 AL_result = 0x01; /* disk full */
1262 } else {
1263 disk_transfer_area = INT21_GetCurrentDTA(context);
1264 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, fcb->logical_record_size);
1265 if (bytes_written != fcb->logical_record_size) {
1266 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1267 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1268 AL_result = 0x01; /* disk full */
1269 } else {
1270 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1271 bytes_written, record_number, position, fcb->file_number, handle);
1272 AL_result = 0x00; /* successful */
1273 } /* if */
1274 } /* if */
1275 } /* if */
1276 if (AL_result == 0x00) {
1277 if (fcb->record_within_current_block == 127) {
1278 fcb->record_within_current_block = 0;
1279 fcb->current_block_number++;
1280 } else {
1281 fcb->record_within_current_block++;
1282 } /* if */
1283 } /* if */
1284 SET_AL(context, AL_result);
1288 /***********************************************************************
1289 * INT21_ReadRandomRecordFromFCB
1291 * Handler for function 0x21.
1293 * PARAMS
1294 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1296 * RETURNS (in AL)
1297 * 0: successful
1298 * 1: end of file, no data read
1299 * 2: segment wrap in DTA, no data read (not returned now)
1300 * 3: end of file, partial record read
1302 * NOTES
1303 * Reads a record with the size FCB->logical_record_size from
1304 * the FCB to the disk transfer area. The position of the record
1305 * is specified with FCB->random_access_record_number. The
1306 * FCB->random_access_record_number is not updated. If a partial record
1307 * is read, it is filled with zeros up to the FCB->logical_record_size.
1309 static void INT21_ReadRandomRecordFromFCB( CONTEXT *context )
1311 struct FCB *fcb;
1312 struct XFCB *xfcb;
1313 HANDLE handle;
1314 DWORD record_number;
1315 DWORD position;
1316 BYTE *disk_transfer_area;
1317 UINT bytes_read;
1318 BYTE AL_result;
1320 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1321 if (fcb->drive_number == 0xff) {
1322 xfcb = (struct XFCB *) fcb;
1323 fcb = (struct FCB *) xfcb->fcb;
1324 } /* if */
1326 memcpy(&record_number, fcb->random_access_record_number, 4);
1327 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1328 if (handle == INVALID_HANDLE_VALUE) {
1329 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1330 fcb->file_number);
1331 AL_result = 0x01; /* end of file, no data read */
1332 } else {
1333 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1334 if (position != record_number * fcb->logical_record_size) {
1335 TRACE("seek(%d, %d, 0) failed with %u\n",
1336 fcb->file_number, record_number * fcb->logical_record_size, position);
1337 AL_result = 0x01; /* end of file, no data read */
1338 } else {
1339 disk_transfer_area = INT21_GetCurrentDTA(context);
1340 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1341 if (bytes_read != fcb->logical_record_size) {
1342 TRACE("_lread(%d, %p, %d) failed with %d\n",
1343 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1344 if (bytes_read == 0) {
1345 AL_result = 0x01; /* end of file, no data read */
1346 } else {
1347 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1348 AL_result = 0x03; /* end of file, partial record read */
1349 } /* if */
1350 } else {
1351 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1352 bytes_read, record_number, position, fcb->file_number, handle);
1353 AL_result = 0x00; /* successful */
1354 } /* if */
1355 } /* if */
1356 } /* if */
1357 fcb->current_block_number = record_number / 128;
1358 fcb->record_within_current_block = record_number % 128;
1359 SET_AL(context, AL_result);
1363 /***********************************************************************
1364 * INT21_WriteRandomRecordToFCB
1366 * Handler for function 0x22.
1368 * PARAMS
1369 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1371 * RETURNS (in AL)
1372 * 0: successful
1373 * 1: disk full
1374 * 2: segment wrap in DTA (not returned now)
1376 * NOTES
1377 * Writes a record with the size FCB->logical_record_size from
1378 * the disk transfer area to the FCB. The position of the record
1379 * is specified with FCB->random_access_record_number. The
1380 * FCB->random_access_record_number is not updated.
1382 static void INT21_WriteRandomRecordToFCB( CONTEXT *context )
1384 struct FCB *fcb;
1385 struct XFCB *xfcb;
1386 HANDLE handle;
1387 DWORD record_number;
1388 DWORD position;
1389 BYTE *disk_transfer_area;
1390 UINT bytes_written;
1391 BYTE AL_result;
1393 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1394 if (fcb->drive_number == 0xff) {
1395 xfcb = (struct XFCB *) fcb;
1396 fcb = (struct FCB *) xfcb->fcb;
1397 } /* if */
1399 memcpy(&record_number, fcb->random_access_record_number, 4);
1400 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1401 if (handle == INVALID_HANDLE_VALUE) {
1402 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1403 fcb->file_number);
1404 AL_result = 0x01; /* disk full */
1405 } else {
1406 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1407 if (position != record_number * fcb->logical_record_size) {
1408 TRACE("seek(%d, %d, 0) failed with %u\n",
1409 fcb->file_number, record_number * fcb->logical_record_size, position);
1410 AL_result = 0x01; /* disk full */
1411 } else {
1412 disk_transfer_area = INT21_GetCurrentDTA(context);
1413 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, fcb->logical_record_size);
1414 if (bytes_written != fcb->logical_record_size) {
1415 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1416 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1417 AL_result = 0x01; /* disk full */
1418 } else {
1419 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1420 bytes_written, record_number, position, fcb->file_number, handle);
1421 AL_result = 0x00; /* successful */
1422 } /* if */
1423 } /* if */
1424 } /* if */
1425 fcb->current_block_number = record_number / 128;
1426 fcb->record_within_current_block = record_number % 128;
1427 SET_AL(context, AL_result);
1431 /***********************************************************************
1432 * INT21_RandomBlockReadFromFCB
1434 * Handler for function 0x27.
1436 * PARAMS
1437 * CX [I/O] Number of records to read
1438 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1440 * RETURNS (in AL)
1441 * 0: successful
1442 * 1: end of file, no data read
1443 * 2: segment wrap in DTA, no data read (not returned now)
1444 * 3: end of file, partial record read
1446 * NOTES
1447 * Reads several records with the size FCB->logical_record_size from
1448 * the FCB to the disk transfer area. The number of records to be
1449 * read is specified in the CX register. The position of the first
1450 * record is specified with FCB->random_access_record_number. The
1451 * FCB->random_access_record_number, the FCB->current_block_number
1452 * and FCB->record_within_current_block are updated to point to the
1453 * next record after the records read. If a partial record is read,
1454 * it is filled with zeros up to the FCB->logical_record_size. The
1455 * CX register is set to the number of successfully read records.
1457 static void INT21_RandomBlockReadFromFCB( CONTEXT *context )
1459 struct FCB *fcb;
1460 struct XFCB *xfcb;
1461 HANDLE handle;
1462 DWORD record_number;
1463 DWORD position;
1464 BYTE *disk_transfer_area;
1465 UINT records_requested;
1466 UINT bytes_requested;
1467 UINT bytes_read;
1468 UINT records_read;
1469 BYTE AL_result;
1471 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1472 if (fcb->drive_number == 0xff) {
1473 xfcb = (struct XFCB *) fcb;
1474 fcb = (struct FCB *) xfcb->fcb;
1475 } /* if */
1477 memcpy(&record_number, fcb->random_access_record_number, 4);
1478 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1479 if (handle == INVALID_HANDLE_VALUE) {
1480 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1481 fcb->file_number);
1482 records_read = 0;
1483 AL_result = 0x01; /* end of file, no data read */
1484 } else {
1485 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1486 if (position != record_number * fcb->logical_record_size) {
1487 TRACE("seek(%d, %d, 0) failed with %u\n",
1488 fcb->file_number, record_number * fcb->logical_record_size, position);
1489 records_read = 0;
1490 AL_result = 0x01; /* end of file, no data read */
1491 } else {
1492 disk_transfer_area = INT21_GetCurrentDTA(context);
1493 records_requested = CX_reg(context);
1494 bytes_requested = records_requested * fcb->logical_record_size;
1495 bytes_read = _lread((HFILE) handle, disk_transfer_area, bytes_requested);
1496 if (bytes_read != bytes_requested) {
1497 TRACE("_lread(%d, %p, %d) failed with %d\n",
1498 fcb->file_number, disk_transfer_area, bytes_requested, bytes_read);
1499 records_read = bytes_read / fcb->logical_record_size;
1500 if (bytes_read % fcb->logical_record_size == 0) {
1501 AL_result = 0x01; /* end of file, no data read */
1502 } else {
1503 records_read++;
1504 memset(&disk_transfer_area[bytes_read], 0, records_read * fcb->logical_record_size - bytes_read);
1505 AL_result = 0x03; /* end of file, partial record read */
1506 } /* if */
1507 } else {
1508 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1509 bytes_read, record_number, position, fcb->file_number, handle);
1510 records_read = records_requested;
1511 AL_result = 0x00; /* successful */
1512 } /* if */
1513 } /* if */
1514 } /* if */
1515 record_number += records_read;
1516 memcpy(fcb->random_access_record_number, &record_number, 4);
1517 fcb->current_block_number = record_number / 128;
1518 fcb->record_within_current_block = record_number % 128;
1519 SET_CX(context, records_read);
1520 SET_AL(context, AL_result);
1524 /***********************************************************************
1525 * INT21_RandomBlockWriteToFCB
1527 * Handler for function 0x28.
1529 * PARAMS
1530 * CX [I/O] Number of records to write
1531 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1533 * RETURNS (in AL)
1534 * 0: successful
1535 * 1: disk full
1536 * 2: segment wrap in DTA (not returned now)
1538 * NOTES
1539 * Writes several records with the size FCB->logical_record_size from
1540 * the disk transfer area to the FCB. The number of records to be
1541 * written is specified in the CX register. The position of the first
1542 * record is specified with FCB->random_access_record_number. The
1543 * FCB->random_access_record_number, the FCB->current_block_number
1544 * and FCB->record_within_current_block are updated to point to the
1545 * next record after the records written. The CX register is set to
1546 * the number of successfully written records.
1548 static void INT21_RandomBlockWriteToFCB( CONTEXT *context )
1550 struct FCB *fcb;
1551 struct XFCB *xfcb;
1552 HANDLE handle;
1553 DWORD record_number;
1554 DWORD position;
1555 BYTE *disk_transfer_area;
1556 UINT records_requested;
1557 UINT bytes_requested;
1558 UINT bytes_written;
1559 UINT records_written;
1560 BYTE AL_result;
1562 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1563 if (fcb->drive_number == 0xff) {
1564 xfcb = (struct XFCB *) fcb;
1565 fcb = (struct FCB *) xfcb->fcb;
1566 } /* if */
1568 memcpy(&record_number, fcb->random_access_record_number, 4);
1569 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1570 if (handle == INVALID_HANDLE_VALUE) {
1571 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1572 fcb->file_number);
1573 records_written = 0;
1574 AL_result = 0x01; /* disk full */
1575 } else {
1576 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1577 if (position != record_number * fcb->logical_record_size) {
1578 TRACE("seek(%d, %d, 0) failed with %u\n",
1579 fcb->file_number, record_number * fcb->logical_record_size, position);
1580 records_written = 0;
1581 AL_result = 0x01; /* disk full */
1582 } else {
1583 disk_transfer_area = INT21_GetCurrentDTA(context);
1584 records_requested = CX_reg(context);
1585 bytes_requested = records_requested * fcb->logical_record_size;
1586 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, bytes_requested);
1587 if (bytes_written != bytes_requested) {
1588 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1589 fcb->file_number, disk_transfer_area, bytes_requested, bytes_written);
1590 records_written = bytes_written / fcb->logical_record_size;
1591 AL_result = 0x01; /* disk full */
1592 } else {
1593 TRACE("successful write %d bytes from record %d (position %u) of file %d (handle %p)\n",
1594 bytes_written, record_number, position, fcb->file_number, handle);
1595 records_written = records_requested;
1596 AL_result = 0x00; /* successful */
1597 } /* if */
1598 } /* if */
1599 } /* if */
1600 record_number += records_written;
1601 memcpy(fcb->random_access_record_number, &record_number, 4);
1602 fcb->current_block_number = record_number / 128;
1603 fcb->record_within_current_block = record_number % 128;
1604 SET_CX(context, records_written);
1605 SET_AL(context, AL_result);
1609 /***********************************************************************
1610 * INT21_CreateDirectory
1612 * Handler for:
1613 * - function 0x39
1614 * - subfunction 0x39 of function 0x71
1615 * - subfunction 0xff of function 0x43 (CL == 0x39)
1617 static BOOL INT21_CreateDirectory( CONTEXT *context )
1619 WCHAR dirW[MAX_PATH];
1620 char *dirA = CTX_SEG_OFF_TO_LIN(context,
1621 context->SegDs,
1622 context->Edx);
1624 TRACE( "CREATE DIRECTORY %s\n", dirA );
1626 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
1628 if (CreateDirectoryW(dirW, NULL))
1629 return TRUE;
1632 * FIXME: CreateDirectory's LastErrors will clash with the ones
1633 * used by DOS. AH=39 only returns 3 (path not found) and
1634 * 5 (access denied), while CreateDirectory return several
1635 * ones. Remap some of them. -Marcus
1637 switch (GetLastError())
1639 case ERROR_ALREADY_EXISTS:
1640 case ERROR_FILENAME_EXCED_RANGE:
1641 case ERROR_DISK_FULL:
1642 SetLastError(ERROR_ACCESS_DENIED);
1643 break;
1644 default:
1645 break;
1648 return FALSE;
1652 /***********************************************************************
1653 * INT21_ExtendedCountryInformation
1655 * Handler for function 0x65.
1657 static void INT21_ExtendedCountryInformation( CONTEXT *context )
1659 BYTE *dataptr = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
1660 BYTE buffsize = CX_reg (context);
1662 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1663 AL_reg(context) );
1666 * Check subfunctions that are passed country and code page.
1668 if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
1670 WORD country = DX_reg(context);
1671 WORD codepage = BX_reg(context);
1673 if (country != 0xffff && country != INT21_GetSystemCountryCode())
1674 FIXME( "Requested info on non-default country %04x\n", country );
1676 if (codepage != 0xffff && codepage != GetOEMCP())
1677 FIXME( "Requested info on non-default code page %04x\n", codepage );
1680 switch (AL_reg(context)) {
1681 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1682 INT_BARF( context, 0x21 );
1683 SET_CFLAG( context );
1684 break;
1686 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1687 TRACE( "Get general internationalization info\n" );
1688 dataptr[0] = 0x01; /* Info ID */
1689 *(WORD*)(dataptr+1) = 38; /* Size of the following info */
1690 *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
1691 *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
1692 /* FIXME: fill buffer partially up to buffsize bytes*/
1693 if (buffsize >= 0x29){
1694 INT21_FillCountryInformation( dataptr + 7 );
1695 SET_CX( context, 0x29 ); /* Size of returned info */
1696 }else{
1697 SET_CX( context, 0x07 ); /* Size of returned info */
1699 break;
1701 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1702 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1703 TRACE( "Get pointer to uppercase table\n" );
1704 dataptr[0] = AL_reg(context); /* Info ID */
1705 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1706 offsetof(INT21_HEAP, uppercase_size) );
1707 SET_CX( context, 5 ); /* Size of returned info */
1708 break;
1710 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1711 TRACE( "Get pointer to lowercase table\n" );
1712 dataptr[0] = 0x03; /* Info ID */
1713 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1714 offsetof(INT21_HEAP, lowercase_size) );
1715 SET_CX( context, 5 ); /* Size of returned info */
1716 break;
1718 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1719 TRACE("Get pointer to filename terminator table\n");
1720 dataptr[0] = 0x05; /* Info ID */
1721 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1722 offsetof(INT21_HEAP, filename_size) );
1723 SET_CX( context, 5 ); /* Size of returned info */
1724 break;
1726 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1727 TRACE("Get pointer to collating sequence table\n");
1728 dataptr[0] = 0x06; /* Info ID */
1729 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1730 offsetof(INT21_HEAP, collating_size) );
1731 SET_CX( context, 5 ); /* Size of returned info */
1732 break;
1734 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1735 TRACE("Get pointer to DBCS lead byte table\n");
1736 dataptr[0] = 0x07; /* Info ID */
1737 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1738 offsetof(INT21_HEAP, dbcs_size) );
1739 SET_CX( context, 5 ); /* Size of returned info */
1740 break;
1742 case 0x20: /* CAPITALIZE CHARACTER */
1743 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1744 TRACE("Convert char to uppercase\n");
1745 SET_DL( context, toupper(DL_reg(context)) );
1746 break;
1748 case 0x21: /* CAPITALIZE STRING */
1749 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1750 TRACE("Convert string to uppercase with length\n");
1752 char *ptr = CTX_SEG_OFF_TO_LIN( context, context->SegDs,
1753 context->Edx );
1754 WORD len = CX_reg(context);
1755 while (len--) { *ptr = toupper(*ptr); ptr++; }
1757 break;
1759 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1760 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1761 TRACE("Convert ASCIIZ string to uppercase\n");
1763 char *p = CTX_SEG_OFF_TO_LIN( context, context->SegDs, context->Edx );
1764 for ( ; *p; p++) *p = toupper(*p);
1766 break;
1768 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1769 INT_BARF( context, 0x21 );
1770 SET_CFLAG( context );
1771 break;
1773 default:
1774 INT_BARF( context, 0x21 );
1775 SET_CFLAG(context);
1776 break;
1781 /***********************************************************************
1782 * INT21_FileAttributes
1784 * Handler for:
1785 * - function 0x43
1786 * - subfunction 0x43 of function 0x71
1788 static BOOL INT21_FileAttributes( CONTEXT *context,
1789 BYTE subfunction,
1790 BOOL islong )
1792 WCHAR fileW[MAX_PATH];
1793 char *fileA = CTX_SEG_OFF_TO_LIN(context,
1794 context->SegDs,
1795 context->Edx);
1796 HANDLE handle;
1797 BOOL status;
1798 FILETIME filetime;
1799 DWORD result;
1800 WORD date, time;
1801 int len;
1803 switch (subfunction)
1805 case 0x00: /* GET FILE ATTRIBUTES */
1806 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA );
1807 len = MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1809 /* Winbench 96 Disk Test fails if we don't complain
1810 * about a filename that ends in \
1812 if (!len || (fileW[len-1] == '/') || (fileW[len-1] == '\\'))
1813 return FALSE;
1815 result = GetFileAttributesW( fileW );
1816 if (result == INVALID_FILE_ATTRIBUTES)
1817 return FALSE;
1818 else
1820 SET_CX( context, (WORD)result );
1821 if (!islong)
1822 SET_AX( context, (WORD)result ); /* DR DOS */
1824 break;
1826 case 0x01: /* SET FILE ATTRIBUTES */
1827 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
1828 CX_reg(context), fileA );
1829 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1831 if (!SetFileAttributesW( fileW, CX_reg(context) ))
1832 return FALSE;
1833 break;
1835 case 0x02: /* GET COMPRESSED FILE SIZE */
1836 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA );
1837 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1839 result = GetCompressedFileSizeW( fileW, NULL );
1840 if (result == INVALID_FILE_SIZE)
1841 return FALSE;
1842 else
1844 SET_AX( context, LOWORD(result) );
1845 SET_DX( context, HIWORD(result) );
1847 break;
1849 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
1850 if (!islong)
1851 INT_BARF( context, 0x21 );
1852 else
1854 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
1855 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1857 handle = CreateFileW( fileW, GENERIC_WRITE,
1858 FILE_SHARE_READ | FILE_SHARE_WRITE,
1859 NULL, OPEN_EXISTING, 0, 0 );
1860 if (handle == INVALID_HANDLE_VALUE)
1861 return FALSE;
1863 DosDateTimeToFileTime( DI_reg(context),
1864 CX_reg(context),
1865 &filetime );
1866 status = SetFileTime( handle, NULL, NULL, &filetime );
1868 CloseHandle( handle );
1869 return status;
1871 break;
1873 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
1874 if (!islong)
1875 INT_BARF( context, 0x21 );
1876 else
1878 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
1879 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1881 handle = CreateFileW( fileW, GENERIC_READ,
1882 FILE_SHARE_READ | FILE_SHARE_WRITE,
1883 NULL, OPEN_EXISTING, 0, 0 );
1884 if (handle == INVALID_HANDLE_VALUE)
1885 return FALSE;
1887 status = GetFileTime( handle, NULL, NULL, &filetime );
1888 if (status)
1890 FileTimeToDosDateTime( &filetime, &date, &time );
1891 SET_DI( context, date );
1892 SET_CX( context, time );
1895 CloseHandle( handle );
1896 return status;
1898 break;
1900 case 0x05: /* SET FILE LAST ACCESS DATE */
1901 if (!islong)
1902 INT_BARF( context, 0x21 );
1903 else
1905 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA );
1906 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1908 handle = CreateFileW( fileW, GENERIC_WRITE,
1909 FILE_SHARE_READ | FILE_SHARE_WRITE,
1910 NULL, OPEN_EXISTING, 0, 0 );
1911 if (handle == INVALID_HANDLE_VALUE)
1912 return FALSE;
1914 DosDateTimeToFileTime( DI_reg(context),
1916 &filetime );
1917 status = SetFileTime( handle, NULL, &filetime, NULL );
1919 CloseHandle( handle );
1920 return status;
1922 break;
1924 case 0x06: /* GET FILE LAST ACCESS DATE */
1925 if (!islong)
1926 INT_BARF( context, 0x21 );
1927 else
1929 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA );
1930 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1932 handle = CreateFileW( fileW, GENERIC_READ,
1933 FILE_SHARE_READ | FILE_SHARE_WRITE,
1934 NULL, OPEN_EXISTING, 0, 0 );
1935 if (handle == INVALID_HANDLE_VALUE)
1936 return FALSE;
1938 status = GetFileTime( handle, NULL, &filetime, NULL );
1939 if (status)
1941 FileTimeToDosDateTime( &filetime, &date, NULL );
1942 SET_DI( context, date );
1945 CloseHandle( handle );
1946 return status;
1948 break;
1950 case 0x07: /* SET FILE CREATION DATE AND TIME */
1951 if (!islong)
1952 INT_BARF( context, 0x21 );
1953 else
1955 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA );
1956 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1958 handle = CreateFileW( fileW, GENERIC_WRITE,
1959 FILE_SHARE_READ | FILE_SHARE_WRITE,
1960 NULL, OPEN_EXISTING, 0, 0 );
1961 if (handle == INVALID_HANDLE_VALUE)
1962 return FALSE;
1965 * FIXME: SI has number of 10-millisecond units past time in CX.
1967 DosDateTimeToFileTime( DI_reg(context),
1968 CX_reg(context),
1969 &filetime );
1970 status = SetFileTime( handle, &filetime, NULL, NULL );
1972 CloseHandle( handle );
1973 return status;
1975 break;
1977 case 0x08: /* GET FILE CREATION DATE AND TIME */
1978 if (!islong)
1979 INT_BARF( context, 0x21 );
1980 else
1982 TRACE( "GET FILE CREATION DATE AND TIME, file %s\n", fileA );
1983 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1985 handle = CreateFileW( fileW, GENERIC_READ,
1986 FILE_SHARE_READ | FILE_SHARE_WRITE,
1987 NULL, OPEN_EXISTING, 0, 0 );
1988 if (handle == INVALID_HANDLE_VALUE)
1989 return FALSE;
1991 status = GetFileTime( handle, &filetime, NULL, NULL );
1992 if (status)
1994 FileTimeToDosDateTime( &filetime, &date, &time );
1995 SET_DI( context, date );
1996 SET_CX( context, time );
1998 * FIXME: SI has number of 10-millisecond units past
1999 * time in CX.
2001 SET_SI( context, 0 );
2004 CloseHandle(handle);
2005 return status;
2007 break;
2009 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2010 if (islong || context->Ebp != 0x5053)
2011 INT_BARF( context, 0x21 );
2012 else
2014 switch(CL_reg(context))
2016 case 0x39:
2017 if (!INT21_CreateDirectory( context ))
2018 return FALSE;
2019 break;
2021 case 0x56:
2022 if (!INT21_RenameFile( context ))
2023 return FALSE;
2024 break;
2026 default:
2027 INT_BARF( context, 0x21 );
2030 break;
2032 default:
2033 INT_BARF( context, 0x21 );
2036 return TRUE;
2040 /***********************************************************************
2041 * INT21_FileDateTime
2043 * Handler for function 0x57.
2045 static BOOL INT21_FileDateTime( CONTEXT *context )
2047 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2048 FILETIME filetime;
2049 WORD date, time;
2051 switch (AL_reg(context)) {
2052 case 0x00: /* Get last-written stamp */
2053 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2054 BX_reg(context) );
2056 if (!GetFileTime( handle, NULL, NULL, &filetime ))
2057 return FALSE;
2058 FileTimeToDosDateTime( &filetime, &date, &time );
2059 SET_DX( context, date );
2060 SET_CX( context, time );
2061 break;
2064 case 0x01: /* Set last-written stamp */
2065 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2066 BX_reg(context) );
2068 DosDateTimeToFileTime( DX_reg(context),
2069 CX_reg(context),
2070 &filetime );
2071 if (!SetFileTime( handle, NULL, NULL, &filetime ))
2072 return FALSE;
2073 break;
2076 case 0x04: /* Get last access stamp, DOS 7 */
2077 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2078 BX_reg(context) );
2080 if (!GetFileTime( handle, NULL, &filetime, NULL ))
2081 return FALSE;
2082 FileTimeToDosDateTime( &filetime, &date, &time );
2083 SET_DX( context, date );
2084 SET_CX( context, time );
2085 break;
2088 case 0x05: /* Set last access stamp, DOS 7 */
2089 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2090 BX_reg(context) );
2092 DosDateTimeToFileTime( DX_reg(context),
2093 CX_reg(context),
2094 &filetime );
2095 if (!SetFileTime( handle, NULL, &filetime, NULL ))
2096 return FALSE;
2097 break;
2100 case 0x06: /* Get creation stamp, DOS 7 */
2101 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2102 BX_reg(context) );
2104 if (!GetFileTime( handle, &filetime, NULL, NULL ))
2105 return FALSE;
2106 FileTimeToDosDateTime( &filetime, &date, &time );
2107 SET_DX( context, date );
2108 SET_CX( context, time );
2110 * FIXME: SI has number of 10-millisecond units past time in CX.
2112 SET_SI( context, 0 );
2113 break;
2116 case 0x07: /* Set creation stamp, DOS 7 */
2117 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2118 BX_reg(context) );
2121 * FIXME: SI has number of 10-millisecond units past time in CX.
2123 DosDateTimeToFileTime( DX_reg(context),
2124 CX_reg(context),
2125 &filetime );
2126 if (!SetFileTime( handle, &filetime, NULL, NULL ))
2127 return FALSE;
2128 break;
2131 default:
2132 INT_BARF( context, 0x21 );
2133 break;
2136 return TRUE;
2140 /***********************************************************************
2141 * INT21_GetPSP
2143 * Handler for functions 0x51 and 0x62.
2145 static void INT21_GetPSP( CONTEXT *context )
2147 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
2149 SET_BX( context, LOWORD(GetCurrentPDB16()) );
2152 static inline void setword( BYTE *ptr, WORD w )
2154 ptr[0] = (BYTE)w;
2155 ptr[1] = (BYTE)(w >> 8);
2158 static void CreateBPB(int drive, BYTE *data, BOOL16 limited)
2159 /* limited == TRUE is used with INT 0x21/0x440d */
2161 /* FIXME: we're forcing some values without checking that those are valid */
2162 if (drive > 1)
2164 setword(data, 512);
2165 data[2] = 2;
2166 setword(&data[3], 0);
2167 data[5] = 2;
2168 setword(&data[6], 240);
2169 setword(&data[8], 64000);
2170 data[0x0a] = 0xf8;
2171 setword(&data[0x0b], 40);
2172 setword(&data[0x0d], 56);
2173 setword(&data[0x0f], 2);
2174 setword(&data[0x11], 0);
2175 if (!limited)
2177 setword(&data[0x1f], 800);
2178 data[0x21] = 5;
2179 setword(&data[0x22], 1);
2182 else
2183 { /* 1.44mb */
2184 setword(data, 512);
2185 data[2] = 2;
2186 setword(&data[3], 0);
2187 data[5] = 2;
2188 setword(&data[6], 240);
2189 setword(&data[8], 2880);
2190 data[0x0a] = 0xf8;
2191 setword(&data[0x0b], 6);
2192 setword(&data[0x0d], 18);
2193 setword(&data[0x0f], 2);
2194 setword(&data[0x11], 0);
2195 if (!limited)
2197 setword(&data[0x1f], 80);
2198 data[0x21] = 7;
2199 setword(&data[0x22], 2);
2204 static inline DWORD INT21_Ioctl_CylHeadSect2Lin(DWORD cyl, WORD head, WORD sec, WORD cyl_cnt,
2205 WORD head_cnt, WORD sec_cnt)
2207 DWORD res = (cyl * head_cnt*sec_cnt + head * sec_cnt + sec);
2208 return res;
2211 /***********************************************************************
2212 * INT21_Ioctl_Block
2214 * Handler for block device IOCTLs.
2216 static void INT21_Ioctl_Block( CONTEXT *context )
2218 BYTE *dataptr;
2219 BYTE drive = INT21_MapDrive( BL_reg(context) );
2220 WCHAR drivespec[] = {'A', ':', '\\', 0};
2221 UINT drivetype;
2223 drivespec[0] += drive;
2224 drivetype = GetDriveTypeW( drivespec );
2226 RESET_CFLAG(context);
2227 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
2229 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2230 AL_reg(context), 'A' + drive );
2231 SetLastError( ERROR_INVALID_DRIVE );
2232 SET_AX( context, ERROR_INVALID_DRIVE );
2233 SET_CFLAG( context );
2234 return;
2237 switch (AL_reg(context))
2239 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2240 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2241 INT_BARF( context, 0x21 );
2242 break;
2244 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2245 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2246 'A' + drive );
2248 if (drivetype == DRIVE_REMOVABLE)
2249 SET_AX( context, 0 ); /* removable */
2250 else
2251 SET_AX( context, 1 ); /* not removable */
2252 break;
2254 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2255 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2256 'A' + drive );
2258 if (drivetype == DRIVE_REMOTE)
2259 SET_DX( context, (1<<9) | (1<<12) ); /* remote + no direct IO */
2260 else if (drivetype == DRIVE_CDROM)
2261 /* CDROM should be set to remote. If it set the app will
2262 * call int2f to check if it cdrom or remote drive. */
2263 SET_DX( context, (1<<12) );
2264 else if (drivetype == DRIVE_FIXED)
2265 /* This should define if drive support 0x0d, 0x0f and 0x08
2266 * requests. The local fixed drive should do. */
2267 SET_DX( context, (1<<11) );
2268 else
2269 SET_DX( context, 0 ); /* FIXME: use driver attr here */
2270 break;
2272 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2273 /* Get pointer to IOCTL parameter block */
2274 dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2276 switch (CX_reg(context))
2278 case 0x0841: /* write logical device track */
2279 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2280 'A' + drive);
2282 WORD head = *(WORD *)(dataptr+1);
2283 WORD cyl = *(WORD *)(dataptr+3);
2284 WORD sect = *(WORD *)(dataptr+5);
2285 WORD nrsect = *(WORD *)(dataptr+7);
2286 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2287 WORD cyl_cnt, head_cnt, sec_cnt;
2289 /* FIXME: we're faking some values here */
2290 if (drive > 1)
2292 /* cyl_cnt = 0x300;
2293 head_cnt = 16;
2294 sec_cnt = 255; */
2295 SET_AX( context, ERROR_WRITE_FAULT );
2296 SET_CFLAG(context);
2297 break;
2299 else
2300 { /* floppy */
2301 cyl_cnt = 80;
2302 head_cnt = 2;
2303 sec_cnt = 18;
2306 if (!DOSVM_RawWrite(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2308 SET_AX( context, ERROR_WRITE_FAULT );
2309 SET_CFLAG(context);
2312 break;
2314 case 0x084a: /* lock logical volume */
2315 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2316 BH_reg(context), DX_reg(context), 'A' + drive );
2317 break;
2319 case 0x0860: /* get device parameters */
2320 /* FIXME: we're faking some values here */
2321 /* used by w4wgrp's winfile */
2322 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2323 dataptr[0] = 0x04;
2324 dataptr[6] = 0; /* media type */
2325 if (drive > 1)
2327 dataptr[1] = 0x05; /* fixed disk */
2328 setword(&dataptr[2], 0x01); /* non removable */
2329 setword(&dataptr[4], 0x300); /* # of cylinders */
2331 else
2333 dataptr[1] = 0x07; /* block dev, floppy */
2334 setword(&dataptr[2], 0x02); /* removable */
2335 setword(&dataptr[4], 80); /* # of cylinders */
2337 CreateBPB(drive, &dataptr[7], TRUE);
2338 RESET_CFLAG(context);
2339 break;
2341 case 0x0861: /* read logical device track */
2342 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2343 'A' + drive);
2345 WORD head = *(WORD *)(dataptr+1);
2346 WORD cyl = *(WORD *)(dataptr+3);
2347 WORD sect = *(WORD *)(dataptr+5);
2348 WORD nrsect = *(WORD *)(dataptr+7);
2349 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2350 WORD cyl_cnt, head_cnt, sec_cnt;
2352 /* FIXME: we're faking some values here */
2353 if (drive > 1)
2355 cyl_cnt = 0x300;
2356 head_cnt = 16;
2357 sec_cnt = 255;
2359 else
2360 { /* floppy */
2361 cyl_cnt = 80;
2362 head_cnt = 2;
2363 sec_cnt = 18;
2366 if (!DOSVM_RawRead(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2368 SET_AX( context, ERROR_READ_FAULT );
2369 SET_CFLAG(context);
2372 break;
2374 case 0x0866: /* get volume serial number */
2376 WCHAR label[12],fsname[9];
2377 DWORD serial;
2378 TRACE( "GENERIC IOCTL - Get media id - %c:\n",
2379 'A' + drive );
2381 GetVolumeInformationW(drivespec, label, 12, &serial, NULL, NULL, fsname, 9);
2382 *(WORD*)dataptr = 0;
2383 memcpy(dataptr+2,&serial,4);
2384 WideCharToMultiByte(CP_OEMCP, 0, label, 11, (LPSTR)dataptr + 6, 11, NULL, NULL);
2385 WideCharToMultiByte(CP_OEMCP, 0, fsname, 8, (LPSTR)dataptr + 17, 8, NULL, NULL);
2387 break;
2389 case 0x086a: /* unlock logical volume */
2390 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2391 'A' + drive );
2392 break;
2394 case 0x086f: /* get drive map information */
2395 memset(dataptr+1, '\0', dataptr[0]-1);
2396 dataptr[1] = dataptr[0];
2397 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
2398 dataptr[3] = 0xFF; /* no physical drive */
2399 break;
2401 case 0x0872:
2402 /* Trial and error implementation */
2403 SET_AX( context, drivetype == DRIVE_UNKNOWN ? 0x0f : 0x01 );
2404 SET_CFLAG(context); /* Seems to be set all the time */
2405 break;
2407 default:
2408 INT_BARF( context, 0x21 );
2410 break;
2412 case 0x0e: /* GET LOGICAL DRIVE MAP */
2413 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2414 'A' + drive );
2415 /* FIXME: this is not correct if drive has mappings */
2416 SET_AL( context, 0 ); /* drive has no mapping */
2417 break;
2419 case 0x0f: /* SET LOGICAL DRIVE MAP */
2420 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2421 INT21_DriveName( BL_reg(context)));
2422 /* FIXME: as of today, we don't support logical drive mapping... */
2423 SET_AL( context, 0 );
2424 break;
2426 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2427 default:
2428 INT_BARF( context, 0x21 );
2433 /***********************************************************************
2434 * INT21_Ioctl_Char
2436 * Handler for character device IOCTLs.
2438 static void INT21_Ioctl_Char( CONTEXT *context )
2440 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2441 BOOL IsConsoleIOHandle = VerifyConsoleIoHandle( handle );
2443 switch (AL_reg(context))
2445 case 0x00: /* GET DEVICE INFORMATION */
2446 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context) );
2447 if (IsConsoleIOHandle || GetFileType(handle) == FILE_TYPE_CHAR)
2450 * Returns attribute word in DX:
2451 * Bit 14 - Device driver can process IOCTL requests.
2452 * Bit 13 - Output until busy supported.
2453 * Bit 11 - Driver supports OPEN/CLOSE calls.
2454 * Bit 8 - Unknown.
2455 * Bit 7 - Set (indicates device).
2456 * Bit 6 - EOF on input.
2457 * Bit 5 - Raw (binary) mode.
2458 * Bit 4 - Device is special (uses int29).
2459 * Bit 3 - Clock device.
2460 * Bit 2 - NUL device.
2461 * Bit 1 - Console output device.
2462 * Bit 0 - Console input device.
2464 SET_DX( context, IsConsoleIOHandle ? 0x80c3 : 0x80c0 /* FIXME */ );
2466 else
2469 * Returns attribute word in DX:
2470 * Bit 15 - File is remote.
2471 * Bit 14 - Don't set file date/time on closing.
2472 * Bit 11 - Media not removable.
2473 * Bit 8 - Generate int24 if no disk space on write
2474 * or read past end of file
2475 * Bit 7 - Clear (indicates file).
2476 * Bit 6 - File has not been written.
2477 * Bit 5..0 - Drive number (0=A:,...)
2479 * FIXME: Should check if file is on remote or removable drive.
2480 * FIXME: Should use drive file is located on (and not current).
2482 SET_DX( context, 0x0140 + INT21_GetCurrentDrive() );
2484 break;
2486 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2487 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context) );
2489 * Returns attribute word in DX:
2490 * Bit 15 - Set if remote.
2491 * Bit 14 - Set if date/time not set on close.
2493 * FIXME: Should check if file is on remote drive.
2495 SET_DX( context, 0 );
2496 break;
2498 case 0x01: /* SET DEVICE INFORMATION */
2499 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2500 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2501 case 0x06: /* GET INPUT STATUS */
2502 case 0x07: /* GET OUTPUT STATUS */
2503 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2504 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2505 default:
2506 INT_BARF( context, 0x21 );
2507 break;
2512 /***********************************************************************
2513 * INT21_Ioctl
2515 * Handler for function 0x44.
2517 static void INT21_Ioctl( CONTEXT *context )
2519 switch (AL_reg(context))
2521 case 0x00:
2522 case 0x01:
2523 case 0x02:
2524 case 0x03:
2525 INT21_Ioctl_Char( context );
2526 break;
2528 case 0x04:
2529 case 0x05:
2530 INT21_Ioctl_Block( context );
2531 break;
2533 case 0x06:
2534 case 0x07:
2535 INT21_Ioctl_Char( context );
2536 break;
2538 case 0x08:
2539 case 0x09:
2540 INT21_Ioctl_Block( context );
2541 break;
2543 case 0x0a:
2544 INT21_Ioctl_Char( context );
2545 break;
2547 case 0x0b: /* SET SHARING RETRY COUNT */
2548 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2549 CX_reg(context), DX_reg(context) );
2550 if (!CX_reg(context))
2552 SET_AX( context, 1 );
2553 SET_CFLAG( context );
2555 else
2557 RESET_CFLAG( context );
2559 break;
2561 case 0x0c:
2562 INT21_Ioctl_Char( context );
2563 break;
2565 case 0x0d:
2566 case 0x0e:
2567 case 0x0f:
2568 INT21_Ioctl_Block( context );
2569 break;
2571 case 0x10:
2572 INT21_Ioctl_Char( context );
2573 break;
2575 case 0x11:
2576 INT21_Ioctl_Block( context );
2577 break;
2579 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2580 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2581 SET_CFLAG(context); /* Error / This is not DR DOS. */
2582 SET_AX( context, 0x0001 ); /* Invalid function */
2583 break;
2585 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2586 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2587 SET_CFLAG(context); /* Error / This is not DR DOS. */
2588 SET_AX( context, 0x0001 ); /* Invalid function */
2589 break;
2591 case 0xe0: /* Sun PC-NFS API */
2592 TRACE( "Sun PC-NFS API\n" );
2593 /* not installed */
2594 break;
2596 default:
2597 INT_BARF( context, 0x21 );
2602 /***********************************************************************
2603 * INT21_Fat32
2605 * Handler for function 0x73.
2607 static BOOL INT21_Fat32( CONTEXT *context )
2609 switch (AL_reg(context))
2611 case 0x02: /* FAT32 - GET EXTENDED DPB */
2613 BYTE drive = INT21_MapDrive( DL_reg(context) );
2614 WORD *ptr = CTX_SEG_OFF_TO_LIN(context,
2615 context->SegEs, context->Edi);
2616 INT21_DPB *target = (INT21_DPB*)(ptr + 1);
2617 INT21_DPB *source;
2619 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context) );
2621 if ( CX_reg(context) < sizeof(INT21_DPB) + 2 || *ptr < sizeof(INT21_DPB) )
2623 SetLastError( ERROR_BAD_LENGTH );
2624 return FALSE;
2627 if ( !INT21_FillDrivePB( drive ) )
2629 SetLastError( ERROR_INVALID_DRIVE );
2630 return FALSE;
2633 source = &INT21_GetHeapPointer()->misc_dpb_list[drive];
2635 *ptr = sizeof(INT21_DPB);
2636 *target = *source;
2638 if (LOWORD(context->Esi) != 0xF1A6)
2640 target->driver_header = 0;
2641 target->next = 0;
2643 else
2645 FIXME( "Caller requested driver and next DPB pointers!\n" );
2648 break;
2650 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2652 WCHAR dirW[MAX_PATH];
2653 char *dirA = CTX_SEG_OFF_TO_LIN( context,
2654 context->SegDs, context->Edx );
2655 BYTE *data = CTX_SEG_OFF_TO_LIN( context,
2656 context->SegEs, context->Edi );
2657 DWORD cluster_sectors;
2658 DWORD sector_bytes;
2659 DWORD free_clusters;
2660 DWORD total_clusters;
2662 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA );
2663 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
2665 if (CX_reg(context) < 44)
2667 SetLastError( ERROR_BAD_LENGTH );
2668 return FALSE;
2671 if (!GetDiskFreeSpaceW( dirW, &cluster_sectors, &sector_bytes,
2672 &free_clusters, &total_clusters ))
2673 return FALSE;
2675 *(WORD*) (data + 0) = 44; /* size of structure */
2676 *(WORD*) (data + 2) = 0; /* version */
2677 *(DWORD*)(data + 4) = cluster_sectors;
2678 *(DWORD*)(data + 8) = sector_bytes;
2679 *(DWORD*)(data + 12) = free_clusters;
2680 *(DWORD*)(data + 16) = total_clusters;
2683 * Below we have free/total sectors and
2684 * free/total allocation units without adjustment
2685 * for compression. We fake both using cluster information.
2687 *(DWORD*)(data + 20) = free_clusters * cluster_sectors;
2688 *(DWORD*)(data + 24) = total_clusters * cluster_sectors;
2689 *(DWORD*)(data + 28) = free_clusters;
2690 *(DWORD*)(data + 32) = total_clusters;
2693 * Between (data + 36) and (data + 43) there
2694 * are eight reserved bytes.
2697 break;
2699 default:
2700 INT_BARF( context, 0x21 );
2703 return TRUE;
2706 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA *dataA,
2707 const WIN32_FIND_DATAW *dataW)
2709 dataA->dwFileAttributes = dataW->dwFileAttributes;
2710 dataA->ftCreationTime = dataW->ftCreationTime;
2711 dataA->ftLastAccessTime = dataW->ftLastAccessTime;
2712 dataA->ftLastWriteTime = dataW->ftLastWriteTime;
2713 dataA->nFileSizeHigh = dataW->nFileSizeHigh;
2714 dataA->nFileSizeLow = dataW->nFileSizeLow;
2715 WideCharToMultiByte( CP_OEMCP, 0, dataW->cFileName, -1,
2716 dataA->cFileName, sizeof(dataA->cFileName), NULL, NULL );
2717 WideCharToMultiByte( CP_OEMCP, 0, dataW->cAlternateFileName, -1,
2718 dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName), NULL, NULL );
2721 /***********************************************************************
2722 * INT21_LongFilename
2724 * Handler for function 0x71.
2726 static void INT21_LongFilename( CONTEXT *context )
2728 BOOL bSetDOSExtendedError = FALSE;
2729 WCHAR pathW[MAX_PATH];
2730 char* pathA;
2732 switch (AL_reg(context))
2734 case 0x0d: /* RESET DRIVE */
2735 INT_BARF( context, 0x21 );
2736 break;
2738 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
2739 if (!INT21_CreateDirectory( context ))
2740 bSetDOSExtendedError = TRUE;
2741 break;
2743 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
2744 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2746 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA);
2747 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2748 if (!RemoveDirectoryW( pathW )) bSetDOSExtendedError = TRUE;
2749 break;
2751 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
2752 if (!INT21_SetCurrentDirectory( context ))
2753 bSetDOSExtendedError = TRUE;
2754 break;
2756 case 0x41: /* LONG FILENAME - DELETE FILE */
2757 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2759 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA );
2760 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2762 if (!DeleteFileW( pathW )) bSetDOSExtendedError = TRUE;
2763 break;
2765 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
2766 if (!INT21_FileAttributes( context, BL_reg(context), TRUE ))
2767 bSetDOSExtendedError = TRUE;
2768 break;
2770 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
2771 if (!INT21_GetCurrentDirectory( context, TRUE ))
2772 bSetDOSExtendedError = TRUE;
2773 break;
2775 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
2777 HANDLE handle;
2778 HGLOBAL16 h16;
2779 WIN32_FIND_DATAW dataW;
2780 WIN32_FIND_DATAA* dataA;
2782 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
2783 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA);
2785 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2786 handle = FindFirstFileW(pathW, &dataW);
2788 dataA = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
2789 if (handle != INVALID_HANDLE_VALUE &&
2790 (h16 = GlobalAlloc16(GMEM_MOVEABLE, sizeof(handle))))
2792 HANDLE* ptr = GlobalLock16( h16 );
2793 *ptr = handle;
2794 GlobalUnlock16( h16 );
2795 SET_AX( context, h16 );
2796 INT21_ConvertFindDataWtoA(dataA, &dataW);
2798 else
2800 if (handle != INVALID_HANDLE_VALUE) FindClose(handle);
2801 SET_AX( context, INVALID_HANDLE_VALUE16);
2802 bSetDOSExtendedError = TRUE;
2805 break;
2807 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
2809 HGLOBAL16 h16 = BX_reg(context);
2810 HANDLE* ptr;
2811 WIN32_FIND_DATAW dataW;
2812 WIN32_FIND_DATAA* dataA;
2814 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
2815 BX_reg(context));
2817 dataA = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
2819 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
2821 if (!FindNextFileW(*ptr, &dataW)) bSetDOSExtendedError = TRUE;
2822 else INT21_ConvertFindDataWtoA(dataA, &dataW);
2823 GlobalUnlock16( h16 );
2825 else
2827 SetLastError( ERROR_INVALID_HANDLE );
2828 bSetDOSExtendedError = TRUE;
2831 break;
2833 case 0x56: /* LONG FILENAME - RENAME FILE */
2834 if (!INT21_RenameFile(context))
2835 bSetDOSExtendedError = TRUE;
2836 break;
2838 case 0x60: /* LONG FILENAME - CONVERT PATH */
2840 WCHAR res[MAX_PATH];
2842 switch (CL_reg(context))
2844 case 0x00: /* "truename" - Canonicalize path */
2846 * FIXME: This is not 100% equal to 0x01 case,
2847 * if you fix this, fix int21 subfunction 0x60, too.
2850 case 0x01: /* Get short filename or path */
2851 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
2852 if (!GetShortPathNameW(pathW, res, 67))
2853 bSetDOSExtendedError = TRUE;
2854 else
2856 SET_AX( context, 0 );
2857 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
2858 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
2859 67, NULL, NULL);
2861 break;
2863 case 0x02: /* Get canonical long filename or path */
2864 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
2865 if (!GetFullPathNameW(pathW, 128, res, NULL))
2866 bSetDOSExtendedError = TRUE;
2867 else
2869 SET_AX( context, 0 );
2870 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
2871 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
2872 128, NULL, NULL);
2874 break;
2875 default:
2876 FIXME("Unimplemented long file name function:\n");
2877 INT_BARF( context, 0x21 );
2878 SET_CFLAG(context);
2879 SET_AL( context, 0 );
2880 break;
2883 break;
2885 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
2886 if (!INT21_CreateFile( context, context->Esi, TRUE,
2887 BX_reg(context), DL_reg(context) ))
2888 bSetDOSExtendedError = TRUE;
2889 break;
2891 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
2893 DWORD filename_len, flags;
2894 WCHAR dstW[8];
2896 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
2898 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA);
2899 SET_AX( context, 0 );
2900 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2901 if (!GetVolumeInformationW( pathW, NULL, 0, NULL, &filename_len,
2902 &flags, dstW, 8 ))
2904 INT_BARF( context, 0x21 );
2905 SET_CFLAG(context);
2906 break;
2908 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
2909 SET_CX( context, filename_len );
2910 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
2911 WideCharToMultiByte(CP_OEMCP, 0, dstW, -1,
2912 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
2913 8, NULL, NULL);
2915 break;
2917 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
2919 HGLOBAL16 h16 = BX_reg(context);
2920 HANDLE* ptr;
2922 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
2923 BX_reg(context));
2924 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
2926 if (!FindClose( *ptr )) bSetDOSExtendedError = TRUE;
2927 GlobalUnlock16( h16 );
2928 GlobalFree16( h16 );
2930 else
2932 SetLastError( ERROR_INVALID_HANDLE );
2933 bSetDOSExtendedError = TRUE;
2936 break;
2938 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
2940 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2941 BY_HANDLE_FILE_INFORMATION *info =
2942 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2944 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
2946 if (!GetFileInformationByHandle(handle, info))
2947 bSetDOSExtendedError = TRUE;
2949 break;
2951 case 0xa7: /* LONG FILENAME - CONVERT TIME */
2952 switch (BL_reg(context))
2954 case 0x00: /* FILE TIME TO DOS TIME */
2956 WORD date, time;
2957 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
2958 context->SegDs,
2959 context->Esi);
2961 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
2963 FileTimeToDosDateTime( filetime, &date, &time );
2965 SET_DX( context, date );
2966 SET_CX( context, time );
2969 * FIXME: BH has number of 10-millisecond units
2970 * past time in CX.
2972 SET_BH( context, 0 );
2974 break;
2976 case 0x01: /* DOS TIME TO FILE TIME */
2978 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
2979 context->SegEs,
2980 context->Edi);
2982 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
2985 * FIXME: BH has number of 10-millisecond units
2986 * past time in CX.
2988 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
2989 filetime );
2991 break;
2993 default:
2994 INT_BARF( context, 0x21 );
2995 break;
2997 break;
2999 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
3000 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3001 case 0xaa: /* LONG FILENAME - SUBST */
3002 default:
3003 FIXME("Unimplemented long file name function:\n");
3004 INT_BARF( context, 0x21 );
3005 SET_CFLAG(context);
3006 SET_AL( context, 0 );
3007 break;
3010 if (bSetDOSExtendedError)
3012 SET_AX( context, GetLastError() );
3013 SET_CFLAG( context );
3018 /***********************************************************************
3019 * INT21_RenameFile
3021 * Handler for:
3022 * - function 0x56
3023 * - subfunction 0x56 of function 0x71
3024 * - subfunction 0xff of function 0x43 (CL == 0x56)
3026 static BOOL INT21_RenameFile( CONTEXT *context )
3028 WCHAR fromW[MAX_PATH];
3029 WCHAR toW[MAX_PATH];
3030 char *fromA = CTX_SEG_OFF_TO_LIN(context,
3031 context->SegDs,context->Edx);
3032 char *toA = CTX_SEG_OFF_TO_LIN(context,
3033 context->SegEs,context->Edi);
3035 TRACE( "RENAME FILE %s to %s\n", fromA, toA );
3036 MultiByteToWideChar(CP_OEMCP, 0, fromA, -1, fromW, MAX_PATH);
3037 MultiByteToWideChar(CP_OEMCP, 0, toA, -1, toW, MAX_PATH);
3039 return MoveFileW( fromW, toW );
3043 /***********************************************************************
3044 * INT21_NetworkFunc
3046 * Handler for:
3047 * - function 0x5e
3049 static BOOL INT21_NetworkFunc (CONTEXT *context)
3051 switch (AL_reg(context))
3053 case 0x00: /* Get machine name. */
3055 WCHAR dstW[MAX_COMPUTERNAME_LENGTH + 1];
3056 DWORD s = ARRAY_SIZE(dstW);
3057 int len;
3059 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
3060 TRACE("getting machine name to %p\n", dst);
3061 if (!GetComputerNameW(dstW, &s) ||
3062 !WideCharToMultiByte(CP_OEMCP, 0, dstW, -1, dst, 16, NULL, NULL))
3064 WARN("failed!\n");
3065 SetLastError( ER_NoNetwork );
3066 return TRUE;
3068 for (len = strlen(dst); len < 15; len++) dst[len] = ' ';
3069 dst[15] = 0;
3070 SET_CH( context, 1 ); /* Valid */
3071 SET_CL( context, 1 ); /* NETbios number??? */
3072 TRACE("returning %s\n", debugstr_an(dst, 16));
3073 return FALSE;
3076 default:
3077 SetLastError( ER_NoNetwork );
3078 return TRUE;
3082 /******************************************************************
3083 * INT21_GetDiskSerialNumber
3086 static int INT21_GetDiskSerialNumber( CONTEXT *context )
3088 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3089 WCHAR path[] = {'A',':','\\',0}, label[11];
3090 DWORD serial;
3092 path[0] += INT21_MapDrive(BL_reg(context));
3093 if (!GetVolumeInformationW( path, label, 11, &serial, NULL, NULL, NULL, 0))
3095 SetLastError( ERROR_INVALID_DRIVE );
3096 return 0;
3099 *(WORD *)dataptr = 0;
3100 memcpy(dataptr + 2, &serial, sizeof(DWORD));
3101 WideCharToMultiByte(CP_OEMCP, 0, label, 11, (LPSTR)dataptr + 6, 11, NULL, NULL);
3102 memcpy(dataptr + 17, "FAT16 ", 8);
3103 return 1;
3107 /******************************************************************
3108 * INT21_SetDiskSerialNumber
3111 static BOOL INT21_SetDiskSerialNumber( CONTEXT *context )
3113 #if 0
3114 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3115 int drive = INT21_MapDrive(BL_reg(context));
3117 if (!is_valid_drive(drive))
3119 SetLastError( ERROR_INVALID_DRIVE );
3120 return FALSE;
3123 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
3124 return TRUE;
3125 #else
3126 FIXME("Setting drive serial number is no longer supported\n");
3127 SetLastError( ERROR_NOT_SUPPORTED );
3128 return FALSE;
3129 #endif
3133 /******************************************************************
3134 * INT21_GetFreeDiskSpace
3137 static BOOL INT21_GetFreeDiskSpace( CONTEXT *context )
3139 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
3140 WCHAR root[] = {'A',':','\\',0};
3141 const DWORD max_clusters = 0x3d83;
3142 const DWORD max_sectors_per_cluster = 0x7f;
3143 const DWORD max_bytes_per_sector = 0x200;
3145 root[0] += INT21_MapDrive(DL_reg(context));
3146 if (!GetDiskFreeSpaceW( root, &cluster_sectors, &sector_bytes,
3147 &free_clusters, &total_clusters ))
3148 return FALSE;
3150 /* Some old win31 apps (Lotus SmartSuite 5.1) crap out if there's too
3151 * much disk space, so Windows XP seems to apply the following limits:
3152 * cluster_sectors <= 0x7f
3153 * sector size <= 0x200
3154 * clusters <= 0x3D83
3155 * This means total reported space is limited to about 1GB.
3158 /* Make sure bytes-per-sector is in [, max] */
3159 while (sector_bytes > max_bytes_per_sector) {
3160 sector_bytes >>= 1;
3161 free_clusters <<= 1;
3162 total_clusters <<= 1;
3164 /* Then make sure sectors-per-cluster is in [max/2, max]. */
3165 while (cluster_sectors <= max_sectors_per_cluster/2) {
3166 cluster_sectors <<= 1;
3167 free_clusters >>= 1;
3168 total_clusters >>= 1;
3170 while (cluster_sectors > max_sectors_per_cluster) {
3171 cluster_sectors >>= 1;
3172 free_clusters <<= 1;
3173 total_clusters <<= 1;
3176 /* scale up sectors_per_cluster to exactly max_sectors_per_cluster.
3177 * We could skip this, but that would impose an artificially low
3178 * limit on reported disk space.
3179 * To avoid overflow, first apply a preliminary cap on sector count;
3180 * this will not affect the correctness of the final result,
3181 * because if the preliminary cap hits, the final one will, too.
3183 if (total_clusters > 4 * max_clusters)
3184 total_clusters = 4 * max_clusters;
3185 if (free_clusters > 4 * max_clusters)
3186 free_clusters = 4 * max_clusters;
3187 if (cluster_sectors < max_sectors_per_cluster) {
3188 free_clusters *= cluster_sectors;
3189 free_clusters /= max_sectors_per_cluster;
3190 total_clusters *= cluster_sectors;
3191 total_clusters /= max_sectors_per_cluster;
3192 cluster_sectors = max_sectors_per_cluster;
3195 /* Finally, apply real cluster count cap. */
3196 if (total_clusters > max_clusters)
3197 total_clusters = max_clusters;
3198 if (free_clusters > max_clusters)
3199 free_clusters = max_clusters;
3201 SET_AX( context, cluster_sectors );
3202 SET_BX( context, free_clusters );
3203 SET_CX( context, sector_bytes );
3204 SET_DX( context, total_clusters );
3205 return TRUE;
3208 /******************************************************************
3209 * INT21_GetDriveAllocInfo
3212 static BOOL INT21_GetDriveAllocInfo( CONTEXT *context, BYTE drive )
3214 INT21_DPB *dpb;
3216 drive = INT21_MapDrive( drive );
3217 if (!INT21_FillDrivePB( drive )) return FALSE;
3218 dpb = &(INT21_GetHeapPointer()->misc_dpb_list[drive]);
3219 SET_AL( context, dpb->cluster_sectors + 1 );
3220 SET_CX( context, dpb->sector_bytes );
3221 SET_DX( context, dpb->num_clusters1 );
3223 context->SegDs = INT21_GetHeapSelector( context );
3224 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive].media_ID ) );
3225 return TRUE;
3228 /***********************************************************************
3229 * INT21_GetExtendedError
3231 static void INT21_GetExtendedError( CONTEXT *context )
3233 BYTE class, action, locus;
3234 WORD error = GetLastError();
3236 switch(error)
3238 case ERROR_SUCCESS:
3239 class = action = locus = 0;
3240 break;
3241 case ERROR_DIR_NOT_EMPTY:
3242 class = EC_Exists;
3243 action = SA_Ignore;
3244 locus = EL_Disk;
3245 break;
3246 case ERROR_ACCESS_DENIED:
3247 class = EC_AccessDenied;
3248 action = SA_Abort;
3249 locus = EL_Disk;
3250 break;
3251 case ERROR_CANNOT_MAKE:
3252 class = EC_AccessDenied;
3253 action = SA_Abort;
3254 locus = EL_Unknown;
3255 break;
3256 case ERROR_DISK_FULL:
3257 case ERROR_HANDLE_DISK_FULL:
3258 class = EC_MediaError;
3259 action = SA_Abort;
3260 locus = EL_Disk;
3261 break;
3262 case ERROR_FILE_EXISTS:
3263 case ERROR_ALREADY_EXISTS:
3264 class = EC_Exists;
3265 action = SA_Abort;
3266 locus = EL_Disk;
3267 break;
3268 case ERROR_FILE_NOT_FOUND:
3269 case ERROR_PATH_NOT_FOUND:
3270 case ERROR_INVALID_NAME:
3271 class = EC_NotFound;
3272 action = SA_Abort;
3273 locus = EL_Disk;
3274 break;
3275 case ERROR_GEN_FAILURE:
3276 class = EC_SystemFailure;
3277 action = SA_Abort;
3278 locus = EL_Unknown;
3279 break;
3280 case ERROR_INVALID_DRIVE:
3281 class = EC_MediaError;
3282 action = SA_Abort;
3283 locus = EL_Disk;
3284 break;
3285 case ERROR_INVALID_HANDLE:
3286 class = EC_ProgramError;
3287 action = SA_Abort;
3288 locus = EL_Disk;
3289 break;
3290 case ERROR_LOCK_VIOLATION:
3291 class = EC_AccessDenied;
3292 action = SA_Abort;
3293 locus = EL_Disk;
3294 break;
3295 case ERROR_NO_MORE_FILES:
3296 class = EC_MediaError;
3297 action = SA_Abort;
3298 locus = EL_Disk;
3299 break;
3300 case ER_NoNetwork:
3301 class = EC_NotFound;
3302 action = SA_Abort;
3303 locus = EL_Network;
3304 break;
3305 case ERROR_NOT_ENOUGH_MEMORY:
3306 class = EC_OutOfResource;
3307 action = SA_Abort;
3308 locus = EL_Memory;
3309 break;
3310 case ERROR_SEEK:
3311 class = EC_NotFound;
3312 action = SA_Ignore;
3313 locus = EL_Disk;
3314 break;
3315 case ERROR_SHARING_VIOLATION:
3316 class = EC_Temporary;
3317 action = SA_Retry;
3318 locus = EL_Disk;
3319 break;
3320 case ERROR_TOO_MANY_OPEN_FILES:
3321 class = EC_ProgramError;
3322 action = SA_Abort;
3323 locus = EL_Disk;
3324 break;
3325 default:
3326 FIXME("Unknown error %d\n", error );
3327 class = EC_SystemFailure;
3328 action = SA_Abort;
3329 locus = EL_Unknown;
3330 break;
3332 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3333 error, class, action, locus );
3334 SET_AX( context, error );
3335 SET_BH( context, class );
3336 SET_BL( context, action );
3337 SET_CH( context, locus );
3340 static BOOL INT21_CreateTempFile( CONTEXT *context )
3342 static int counter = 0;
3343 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
3344 char *p = name + strlen(name);
3346 /* despite what Ralf Brown says, some programs seem to call without
3347 * ending backslash (DOS accepts that, so we accept it too) */
3348 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
3350 for (;;)
3352 sprintf( p, "wine%04x.%03d", GetCurrentThreadId(), counter );
3353 counter = (counter + 1) % 1000;
3355 SET_AX( context,
3356 Win32HandleToDosFileHandle(
3357 CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
3358 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
3359 CREATE_NEW, 0, 0 ) ) );
3360 if (AX_reg(context) != HFILE_ERROR16)
3362 TRACE("created %s\n", name );
3363 return TRUE;
3365 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
3369 /***********************************************************************
3370 * DOSFS_ToDosFCBFormat
3372 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3373 * expanding wild cards and converting to upper-case in the process.
3374 * File name can be terminated by '\0', '\\' or '/'.
3375 * Return FALSE if the name is not a valid DOS name.
3376 * 'buffer' must be at least 12 characters long.
3378 /* Chars we don't want to see in DOS file names */
3379 static BOOL INT21_ToDosFCBFormat( LPCWSTR name, LPWSTR buffer )
3381 static const WCHAR invalid_chars[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3382 LPCWSTR p = name;
3383 int i;
3385 /* Check for "." and ".." */
3386 if (*p == '.')
3388 p++;
3389 buffer[0] = '.';
3390 for(i = 1; i < 11; i++) buffer[i] = ' ';
3391 buffer[11] = 0;
3392 if (*p == '.')
3394 buffer[1] = '.';
3395 p++;
3397 return (!*p || (*p == '/') || (*p == '\\'));
3400 for (i = 0; i < 8; i++)
3402 switch(*p)
3404 case '\0':
3405 case '\\':
3406 case '/':
3407 case '.':
3408 buffer[i] = ' ';
3409 break;
3410 case '?':
3411 p++;
3412 /* fall through */
3413 case '*':
3414 buffer[i] = '?';
3415 break;
3416 default:
3417 if (wcschr( invalid_chars, *p )) return FALSE;
3418 buffer[i] = *p++;
3419 break;
3423 if (*p == '*')
3425 /* Skip all chars after wildcard up to first dot */
3426 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
3428 else
3430 /* Check if name too long */
3431 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return FALSE;
3433 if (*p == '.') p++; /* Skip dot */
3435 for (i = 8; i < 11; i++)
3437 switch(*p)
3439 case '\0':
3440 case '\\':
3441 case '/':
3442 buffer[i] = ' ';
3443 break;
3444 case '.':
3445 return FALSE; /* Second extension not allowed */
3446 case '?':
3447 p++;
3448 /* fall through */
3449 case '*':
3450 buffer[i] = '?';
3451 break;
3452 default:
3453 if (wcschr( invalid_chars, *p )) return FALSE;
3454 buffer[i] = *p++;
3455 break;
3458 buffer[11] = '\0';
3459 wcsupr( buffer );
3461 /* at most 3 character of the extension are processed
3462 * is something behind this ?
3464 while (*p == '*' || *p == ' ') p++; /* skip wildcards and spaces */
3465 return (!*p || (*p == '/') || (*p == '\\'));
3468 static HANDLE INT21_FindHandle;
3469 static const WCHAR *INT21_FindPath; /* will point to current dta->fullPath search */
3471 /******************************************************************
3472 * INT21_FindFirst
3474 static BOOL INT21_FindFirst( CONTEXT *context )
3476 WCHAR *p, *q;
3477 const char *path;
3478 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3479 WCHAR maskW[12], pathW[MAX_PATH];
3480 static const WCHAR wildcardW[] = {'*','.','*',0};
3482 path = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3483 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
3485 p = wcsrchr( pathW, '\\');
3486 q = wcsrchr( pathW, '/');
3487 if (q>p) p = q;
3488 if (!p)
3490 if (pathW[0] && pathW[1] == ':') p = pathW + 2;
3491 else p = pathW;
3493 else p++;
3495 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3496 * (doesn't matter as it is set below anyway)
3498 if (!INT21_ToDosFCBFormat( p, maskW ))
3500 SetLastError( ERROR_FILE_NOT_FOUND );
3501 SET_AX( context, ERROR_FILE_NOT_FOUND );
3502 SET_CFLAG(context);
3503 return FALSE;
3505 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
3507 dta->fullPath = HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW) + (p - pathW)*sizeof(WCHAR) );
3508 memcpy( dta->fullPath, pathW, (p - pathW) * sizeof(WCHAR) );
3509 memcpy( dta->fullPath + (p - pathW), wildcardW, sizeof(wildcardW) );
3510 /* we must have a fully qualified file name in dta->fullPath
3511 * (we could have a UNC path, but this would lead to some errors later on)
3513 dta->drive = drive_number( dta->fullPath[0] );
3514 dta->count = 0;
3515 dta->search_attr = CL_reg(context);
3516 return TRUE;
3519 /******************************************************************
3520 * match_short
3522 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3524 static BOOL match_short(LPCWSTR shortW, LPCSTR maskA)
3526 WCHAR mask[11], file[12];
3527 int i;
3529 if (!INT21_ToDosFCBFormat( shortW, file )) return FALSE;
3530 MultiByteToWideChar(CP_OEMCP, 0, maskA, 11, mask, 11);
3531 for (i = 0; i < 11; i++)
3532 if (mask[i] != '?' && mask[i] != file[i]) return FALSE;
3533 return TRUE;
3536 static unsigned INT21_FindHelper(LPCWSTR fullPath, unsigned drive, unsigned count,
3537 LPCSTR mask, unsigned search_attr,
3538 WIN32_FIND_DATAW* entry)
3540 unsigned ncalls;
3542 if ((search_attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
3544 WCHAR path[] = {' ',':','\\',0};
3546 if (count) return 0;
3547 path[0] = drive + 'A';
3548 if (!GetVolumeInformationW(path, entry->cAlternateFileName, 13, NULL, NULL, NULL, NULL, 0)) return 0;
3549 if (!entry->cAlternateFileName[0]) return 0;
3550 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftCreationTime );
3551 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftLastAccessTime );
3552 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftLastWriteTime );
3553 entry->dwFileAttributes = FA_LABEL;
3554 entry->nFileSizeHigh = entry->nFileSizeLow = 0;
3555 TRACE("returning %s as label\n", debugstr_w(entry->cAlternateFileName));
3556 return 1;
3559 if (!INT21_FindHandle || INT21_FindPath != fullPath || count == 0)
3561 if (INT21_FindHandle) FindClose(INT21_FindHandle);
3562 INT21_FindHandle = FindFirstFileW(fullPath, entry);
3563 if (INT21_FindHandle == INVALID_HANDLE_VALUE)
3565 INT21_FindHandle = 0;
3566 return 0;
3568 INT21_FindPath = fullPath;
3569 /* we need to resync search */
3570 ncalls = count;
3572 else ncalls = 1;
3574 while (ncalls-- != 0)
3576 if (!FindNextFileW(INT21_FindHandle, entry))
3578 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3579 return 0;
3582 while (count < 0xffff)
3584 count++;
3585 /* Check the file attributes, and path */
3586 if (!(entry->dwFileAttributes & ~search_attr) &&
3587 match_short(entry->cAlternateFileName[0] ? entry->cAlternateFileName : entry->cFileName,
3588 mask))
3590 return count;
3592 if (!FindNextFileW(INT21_FindHandle, entry))
3594 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3595 return 0;
3598 WARN("Too many directory entries in %s\n", debugstr_w(fullPath) );
3599 return 0;
3602 /******************************************************************
3603 * INT21_FindNext
3605 static BOOL INT21_FindNext( CONTEXT *context )
3607 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3608 DWORD attr = dta->search_attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
3609 WIN32_FIND_DATAW entry;
3610 int n;
3612 if (!dta->fullPath) return FALSE;
3614 n = INT21_FindHelper(dta->fullPath, dta->drive, dta->count,
3615 dta->mask, attr, &entry);
3616 if (n)
3618 dta->fileattr = entry.dwFileAttributes;
3619 dta->filesize = entry.nFileSizeLow;
3620 FileTimeToDosDateTime( &entry.ftLastWriteTime, &dta->filedate, &dta->filetime );
3621 if (entry.cAlternateFileName[0])
3622 WideCharToMultiByte(CP_OEMCP, 0, entry.cAlternateFileName, -1,
3623 dta->filename, 13, NULL, NULL);
3624 else
3625 WideCharToMultiByte(CP_OEMCP, 0, entry.cFileName, -1, dta->filename, 13, NULL, NULL);
3627 if (!memchr(dta->mask,'?',11))
3629 /* wildcardless search, release resources in case no findnext will
3630 * be issued, and as a workaround in case file creation messes up
3631 * findnext, as sometimes happens with pkunzip
3633 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3634 INT21_FindPath = dta->fullPath = NULL;
3636 dta->count = n;
3637 return TRUE;
3639 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3640 INT21_FindPath = dta->fullPath = NULL;
3641 return FALSE;
3644 /* microsoft's programmers should be shot for using CP/M style int21
3645 calls in Windows for Workgroup's winfile.exe */
3647 /******************************************************************
3648 * INT21_FindFirstFCB
3651 static BOOL INT21_FindFirstFCB( CONTEXT *context )
3653 BYTE *fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3654 FINDFILE_FCB *pFCB;
3655 int drive;
3656 WCHAR p[] = {' ',':',};
3658 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
3659 else pFCB = (FINDFILE_FCB *)fcb;
3660 drive = INT21_MapDrive( pFCB->drive );
3661 if (drive == MAX_DOS_DRIVES) return FALSE;
3663 p[0] = 'A' + drive;
3664 pFCB->fullPath = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
3665 if (!pFCB->fullPath) return FALSE;
3666 GetLongPathNameW(p, pFCB->fullPath, MAX_PATH);
3667 pFCB->count = 0;
3668 return TRUE;
3671 /******************************************************************
3672 * INT21_FindNextFCB
3675 static BOOL INT21_FindNextFCB( CONTEXT *context )
3677 BYTE *fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3678 FINDFILE_FCB *pFCB;
3679 LPBYTE pResult = INT21_GetCurrentDTA(context);
3680 DOS_DIRENTRY_LAYOUT *ddl;
3681 WIN32_FIND_DATAW entry;
3682 BYTE attr;
3683 int n;
3684 WCHAR nameW[12];
3686 if (*fcb == 0xff) /* extended FCB ? */
3688 attr = fcb[6];
3689 pFCB = (FINDFILE_FCB *)(fcb + 7);
3691 else
3693 attr = 0;
3694 pFCB = (FINDFILE_FCB *)fcb;
3697 if (!pFCB->fullPath) return FALSE;
3698 n = INT21_FindHelper(pFCB->fullPath, INT21_MapDrive( pFCB->drive ),
3699 pFCB->count, pFCB->filename, attr, &entry);
3700 if (!n)
3702 HeapFree( GetProcessHeap(), 0, pFCB->fullPath );
3703 INT21_FindPath = pFCB->fullPath = NULL;
3704 return FALSE;
3706 pFCB->count += n;
3708 if (*fcb == 0xff)
3710 /* place extended FCB header before pResult if called with extended FCB */
3711 *pResult = 0xff;
3712 pResult += 6; /* leave reserved field behind */
3713 *pResult++ = entry.dwFileAttributes;
3715 *pResult++ = INT21_MapDrive( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
3716 ddl = (DOS_DIRENTRY_LAYOUT*)pResult;
3717 ddl->fileattr = entry.dwFileAttributes;
3718 ddl->cluster = 0; /* what else? */
3719 ddl->filesize = entry.nFileSizeLow;
3720 memset( ddl->reserved, 0, sizeof(ddl->reserved) );
3721 FileTimeToDosDateTime( &entry.ftLastWriteTime,
3722 &ddl->filedate, &ddl->filetime );
3724 /* Convert file name to FCB format */
3725 if (entry.cAlternateFileName[0])
3726 INT21_ToDosFCBFormat( entry.cAlternateFileName, nameW );
3727 else
3728 INT21_ToDosFCBFormat( entry.cFileName, nameW );
3729 WideCharToMultiByte(CP_OEMCP, 0, nameW, 11, ddl->filename, 11, NULL, NULL);
3730 return TRUE;
3734 /******************************************************************
3735 * INT21_ParseFileNameIntoFCB
3738 static void INT21_ParseFileNameIntoFCB( CONTEXT *context )
3740 char *filename =
3741 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
3742 char *fcb =
3743 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
3744 char *s;
3745 WCHAR *buffer;
3746 WCHAR fcbW[12];
3747 INT buffer_len, len;
3749 SET_AL( context, 0xff ); /* failed */
3751 TRACE("filename: '%s'\n", filename);
3753 s = filename;
3754 while (*s && (*s != ' ') && (*s != '\r') && (*s != '\n'))
3755 s++;
3756 len = filename - s;
3758 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
3759 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
3760 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
3761 buffer[len] = 0;
3762 INT21_ToDosFCBFormat(buffer, fcbW);
3763 HeapFree(GetProcessHeap(), 0, buffer);
3764 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
3765 *fcb = 0;
3766 TRACE("FCB: '%s'\n", fcb + 1);
3768 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
3770 /* point DS:SI to first unparsed character */
3771 SET_SI( context, context->Esi + (int)s - (int)filename );
3774 static BOOL INT21_Dup2(HFILE16 hFile1, HFILE16 hFile2)
3776 HFILE16 res = HFILE_ERROR16;
3777 HANDLE handle, new_handle;
3778 #define DOS_TABLE_SIZE 256
3779 DWORD map[DOS_TABLE_SIZE / 32];
3780 int i;
3782 handle = DosFileHandleToWin32Handle(hFile1);
3783 if (handle == INVALID_HANDLE_VALUE)
3784 return FALSE;
3786 _lclose16(hFile2);
3787 /* now loop to allocate the same one... */
3788 memset(map, 0, sizeof(map));
3789 for (i = 0; i < DOS_TABLE_SIZE; i++)
3791 if (!DuplicateHandle(GetCurrentProcess(), handle,
3792 GetCurrentProcess(), &new_handle,
3793 0, FALSE, DUPLICATE_SAME_ACCESS))
3795 res = HFILE_ERROR16;
3796 break;
3798 res = Win32HandleToDosFileHandle(new_handle);
3799 if (res == HFILE_ERROR16 || res == hFile2) break;
3800 map[res / 32] |= 1 << (res % 32);
3802 /* clean up the allocated slots */
3803 for (i = 0; i < DOS_TABLE_SIZE; i++)
3805 if (map[i / 32] & (1 << (i % 32)))
3806 _lclose16((HFILE16)i);
3808 return res == hFile2;
3812 /***********************************************************************
3813 * DOSVM_Int21Handler
3815 * Interrupt 0x21 handler.
3817 void WINAPI DOSVM_Int21Handler( CONTEXT *context )
3819 BOOL bSetDOSExtendedError = FALSE;
3821 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
3822 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
3823 AX_reg(context), BX_reg(context),
3824 CX_reg(context), DX_reg(context),
3825 SI_reg(context), DI_reg(context),
3826 (WORD)context->SegDs, (WORD)context->SegEs,
3827 context->EFlags );
3830 * Extended error is used by (at least) functions 0x2f to 0x62.
3831 * Function 0x59 returns extended error and error should not
3832 * be cleared before handling the function.
3834 if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59)
3835 SetLastError(0);
3837 RESET_CFLAG(context); /* Not sure if this is a good idea. */
3839 switch(AH_reg(context))
3841 case 0x00: /* TERMINATE PROGRAM */
3842 TRACE("TERMINATE PROGRAM\n");
3843 DOSVM_Exit( 0 );
3844 break;
3846 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
3847 TRACE("DIRECT CHARACTER INPUT WITH ECHO - not supported\n");
3848 break;
3850 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
3851 TRACE("Write Character to Standard Output\n");
3852 break;
3854 case 0x03: /* READ CHARACTER FROM STDAUX */
3855 case 0x04: /* WRITE CHARACTER TO STDAUX */
3856 case 0x05: /* WRITE CHARACTER TO PRINTER */
3857 INT_BARF( context, 0x21 );
3858 break;
3860 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
3861 if (DL_reg(context) == 0xff)
3863 TRACE("Direct Console Input\n");
3865 /* no character available */
3866 SET_AL( context, 0 );
3867 SET_ZFLAG( context );
3869 else
3871 TRACE("Direct Console Output\n");
3873 * At least DOS versions 2.1-7.0 return character
3874 * that was written in AL register.
3876 SET_AL( context, DL_reg(context) );
3878 break;
3880 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
3881 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO - not supported\n");
3882 SET_AL( context, 0 );
3883 break;
3885 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
3886 TRACE("CHARACTER INPUT WITHOUT ECHO - not supported\n");
3887 SET_AL( context, 0 );
3888 break;
3890 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
3891 TRACE("WRITE '$'-terminated string from %04X:%04X to stdout\n",
3892 context->SegDs, DX_reg(context) );
3894 LPSTR data = CTX_SEG_OFF_TO_LIN( context,
3895 context->SegDs, context->Edx );
3896 LPSTR p = data;
3897 DWORD w;
3899 * Do NOT use strchr() to calculate the string length,
3900 * as '\0' is valid string content, too!
3901 * Maybe we should check for non-'$' strings, but DOS doesn't.
3903 while (*p != '$') p++;
3905 WriteFile( DosFileHandleToWin32Handle(1), data, p - data, &w, NULL );
3906 SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
3908 break;
3910 case 0x0a: /* BUFFERED INPUT */
3911 TRACE( "BUFFERED INPUT - not supported\n" );
3912 break;
3914 case 0x0b: /* GET STDIN STATUS */
3915 TRACE( "GET STDIN STATUS - not supported\n" );
3916 SET_AL( context, 0 ); /* no character available */
3917 break;
3919 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
3921 BYTE al = AL_reg(context); /* Input function to execute after flush. */
3923 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
3925 /* FIXME: buffers are not flushed */
3928 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
3929 * int21 function identified by AL will be called.
3931 if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
3933 SET_AH( context, al );
3934 DOSVM_Int21Handler( context );
3937 break;
3939 case 0x0d: /* DISK BUFFER FLUSH */
3940 TRACE("DISK BUFFER FLUSH ignored\n");
3941 break;
3943 case 0x0e: /* SELECT DEFAULT DRIVE */
3944 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context) );
3945 INT21_SetCurrentDrive( DL_reg(context) );
3946 SET_AL( context, MAX_DOS_DRIVES );
3947 break;
3949 case 0x0f: /* OPEN FILE USING FCB */
3950 INT21_OpenFileUsingFCB( context );
3951 break;
3953 case 0x10: /* CLOSE FILE USING FCB */
3954 INT21_CloseFileUsingFCB( context );
3955 break;
3957 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
3958 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
3959 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
3960 if (!INT21_FindFirstFCB(context))
3962 SET_AL( context, 0xff );
3963 break;
3965 /* else fall through */
3967 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
3968 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
3969 break;
3971 case 0x13: /* DELETE FILE USING FCB */
3972 INT_BARF( context, 0x21 );
3973 break;
3975 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
3976 INT21_SequentialReadFromFCB( context );
3977 break;
3979 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
3980 INT21_SequentialWriteToFCB( context );
3981 break;
3983 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
3984 case 0x17: /* RENAME FILE USING FCB */
3985 INT_BARF( context, 0x21 );
3986 break;
3988 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
3989 SET_AL( context, 0 );
3990 break;
3992 case 0x19: /* GET CURRENT DEFAULT DRIVE */
3993 SET_AL( context, INT21_GetCurrentDrive() );
3994 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context ) );
3995 break;
3997 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
3998 TRACE( "SET DISK TRANSFER AREA ADDRESS %04X:%04X\n",
3999 context->SegDs, DX_reg(context) );
4001 TDB *task = GlobalLock16( GetCurrentTask() );
4002 task->dta = MAKESEGPTR( context->SegDs, DX_reg(context) );
4004 break;
4006 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4007 if (!INT21_GetDriveAllocInfo(context, 0))
4008 SET_AX( context, 0xffff );
4009 break;
4011 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4012 if (!INT21_GetDriveAllocInfo(context, DL_reg(context)))
4013 SET_AX( context, 0xffff );
4014 break;
4016 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4017 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4018 SET_AL( context, 0 );
4019 break;
4021 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4023 BYTE drive = INT21_MapDrive( 0 );
4024 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4026 if (INT21_FillDrivePB( drive ))
4028 SET_AL( context, 0x00 ); /* success */
4029 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4030 context->SegDs = INT21_GetHeapSelector( context );
4032 else
4034 SET_AL( context, 0xff ); /* invalid or network drive */
4037 break;
4039 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4040 SET_AL( context, 0 );
4041 break;
4043 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4044 INT21_ReadRandomRecordFromFCB( context );
4045 break;
4047 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4048 INT21_WriteRandomRecordToFCB( context );
4049 break;
4051 case 0x23: /* GET FILE SIZE FOR FCB */
4052 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4053 INT_BARF( context, 0x21 );
4054 break;
4056 case 0x25: /* SET INTERRUPT VECTOR */
4057 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4059 FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
4060 DOSVM_SetPMHandler16( AL_reg(context), ptr );
4062 break;
4064 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4065 INT_BARF( context, 0x21 );
4066 break;
4068 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4069 INT21_RandomBlockReadFromFCB( context );
4070 break;
4072 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4073 INT21_RandomBlockWriteToFCB( context );
4074 break;
4076 case 0x29: /* PARSE FILENAME INTO FCB */
4077 INT21_ParseFileNameIntoFCB(context);
4078 break;
4080 case 0x2a: /* GET SYSTEM DATE */
4081 TRACE( "GET SYSTEM DATE\n" );
4083 SYSTEMTIME systime;
4084 GetLocalTime( &systime );
4085 SET_CX( context, systime.wYear );
4086 SET_DH( context, systime.wMonth );
4087 SET_DL( context, systime.wDay );
4088 SET_AL( context, systime.wDayOfWeek );
4090 break;
4092 case 0x2b: /* SET SYSTEM DATE */
4093 TRACE( "SET SYSTEM DATE\n" );
4095 WORD year = CX_reg(context);
4096 BYTE month = DH_reg(context);
4097 BYTE day = DL_reg(context);
4099 if (year >= 1980 && year <= 2099 &&
4100 month >= 1 && month <= 12 &&
4101 day >= 1 && day <= 31)
4103 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4104 day, month, year );
4105 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4107 else
4109 SET_AL( context, 0xff ); /* invalid date */
4110 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4111 day, month, year );
4114 break;
4116 case 0x2c: /* GET SYSTEM TIME */
4117 TRACE( "GET SYSTEM TIME\n" );
4119 SYSTEMTIME systime;
4120 GetLocalTime( &systime );
4121 SET_CH( context, systime.wHour );
4122 SET_CL( context, systime.wMinute );
4123 SET_DH( context, systime.wSecond );
4124 SET_DL( context, systime.wMilliseconds / 10 );
4126 break;
4128 case 0x2d: /* SET SYSTEM TIME */
4129 if( CH_reg(context) >= 24 || CL_reg(context) >= 60 || DH_reg(context) >= 60 || DL_reg(context) >= 100 ) {
4130 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4131 CH_reg(context), CL_reg(context),
4132 DH_reg(context), DL_reg(context) );
4133 SET_AL( context, 0xFF );
4135 else
4137 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4138 CH_reg(context), CL_reg(context),
4139 DH_reg(context), DL_reg(context) );
4140 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4142 break;
4144 case 0x2e: /* SET VERIFY FLAG */
4145 TRACE("SET VERIFY FLAG ignored\n");
4146 /* we cannot change the behaviour anyway, so just ignore it */
4147 break;
4149 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4150 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4152 TDB *task = GlobalLock16( GetCurrentTask() );
4153 context->SegEs = SELECTOROF( task->dta );
4154 SET_BX( context, OFFSETOF( task->dta ) );
4156 break;
4158 case 0x30: /* GET DOS VERSION */
4159 TRACE( "GET DOS VERSION - %s requested\n",
4160 (AL_reg(context) == 0x00) ? "OEM number" : "version flag" );
4162 if (AL_reg(context) == 0x00)
4163 SET_BH( context, 0xff ); /* OEM number => undefined */
4164 else
4165 SET_BH( context, 0x08 ); /* version flag => DOS is in ROM */
4167 SET_AL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4168 SET_AH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4170 SET_BL( context, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4171 SET_CX( context, 0x3456 );
4172 break;
4174 case 0x31: /* TERMINATE AND STAY RESIDENT */
4175 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4176 break;
4178 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4180 BYTE drive = INT21_MapDrive( DL_reg(context) );
4181 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context) );
4183 if (INT21_FillDrivePB( drive ))
4185 SET_AL( context, 0x00 ); /* success */
4186 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4187 context->SegDs = INT21_GetHeapSelector( context );
4189 else
4191 SET_AL( context, 0xff ); /* invalid or network drive */
4194 break;
4196 case 0x33: /* MULTIPLEXED */
4197 switch (AL_reg(context))
4199 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4200 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4201 SET_DL( context, brk_flag );
4202 break;
4204 case 0x01: /* SET EXTENDED BREAK STATE */
4205 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4206 brk_flag = (DL_reg(context) > 0) ? 1 : 0;
4207 break;
4209 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4210 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4211 /* ugly coding in order to stay reentrant */
4212 if (DL_reg(context))
4214 SET_DL( context, brk_flag );
4215 brk_flag = 1;
4217 else
4219 SET_DL( context, brk_flag );
4220 brk_flag = 0;
4222 break;
4224 case 0x05: /* GET BOOT DRIVE */
4225 TRACE("GET BOOT DRIVE\n");
4226 SET_DL( context, 3 );
4227 /* c: is Wine's bootdrive (a: is 1)*/
4228 break;
4230 case 0x06: /* GET TRUE VERSION NUMBER */
4231 TRACE("GET TRUE VERSION NUMBER\n");
4232 SET_BL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4233 SET_BH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4234 SET_DL( context, 0x00 ); /* revision */
4235 SET_DH( context, 0x08 ); /* DOS is in ROM */
4236 break;
4238 default:
4239 INT_BARF( context, 0x21 );
4240 break;
4242 break;
4244 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4245 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4246 context->SegEs = INT21_GetHeapSelector( context );
4247 SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
4248 break;
4250 case 0x35: /* GET INTERRUPT VECTOR */
4251 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4253 FARPROC16 addr = DOSVM_GetPMHandler16( AL_reg(context) );
4254 context->SegEs = SELECTOROF(addr);
4255 SET_BX( context, OFFSETOF(addr) );
4257 break;
4259 case 0x36: /* GET FREE DISK SPACE */
4260 TRACE("GET FREE DISK SPACE FOR DRIVE %s (limited to about 1GB)\n",
4261 INT21_DriveName( DL_reg(context) ));
4262 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
4263 break;
4265 case 0x37: /* SWITCHAR */
4267 switch (AL_reg(context))
4269 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4270 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4271 SET_AL( context, 0x00 ); /* success*/
4272 SET_DL( context, '/' );
4273 break;
4274 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4275 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4276 DL_reg( context ));
4277 SET_AL( context, 0x00 ); /* success*/
4278 break;
4279 default:
4280 INT_BARF( context, 0x21 );
4281 break;
4284 break;
4286 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4287 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4288 if (AL_reg(context))
4290 WORD country = AL_reg(context);
4291 if (country == 0xff)
4292 country = BX_reg(context);
4293 if (country != INT21_GetSystemCountryCode()) {
4294 FIXME( "Requested info on non-default country %04x\n", country );
4295 SET_AX(context, 2);
4296 SET_CFLAG(context);
4299 if(AX_reg(context) != 2 )
4301 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context,
4302 context->SegDs,
4303 context->Edx) );
4304 SET_AX( context, INT21_GetSystemCountryCode() );
4305 SET_BX( context, INT21_GetSystemCountryCode() );
4306 RESET_CFLAG(context);
4308 break;
4310 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4311 if (!INT21_CreateDirectory( context ))
4312 bSetDOSExtendedError = TRUE;
4313 else
4314 RESET_CFLAG(context);
4315 break;
4317 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4319 WCHAR dirW[MAX_PATH];
4320 char *dirA = CTX_SEG_OFF_TO_LIN(context,
4321 context->SegDs, context->Edx);
4323 TRACE( "REMOVE DIRECTORY %s\n", dirA );
4325 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
4327 if (!RemoveDirectoryW( dirW ))
4328 bSetDOSExtendedError = TRUE;
4329 else
4330 RESET_CFLAG(context);
4332 break;
4334 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4335 if (!INT21_SetCurrentDirectory( context ))
4336 bSetDOSExtendedError = TRUE;
4337 else
4338 RESET_CFLAG(context);
4339 break;
4341 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4342 if (!INT21_CreateFile( context, context->Edx, FALSE,
4343 OF_READWRITE | OF_SHARE_COMPAT, 0x12 ))
4344 bSetDOSExtendedError = TRUE;
4345 else
4346 RESET_CFLAG(context);
4347 break;
4349 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4350 if (!INT21_CreateFile( context, context->Edx, FALSE,
4351 AL_reg(context), 0x01 ))
4352 bSetDOSExtendedError = TRUE;
4353 else
4354 RESET_CFLAG(context);
4355 break;
4357 case 0x3e: /* "CLOSE" - CLOSE FILE */
4358 TRACE( "CLOSE handle %d\n", BX_reg(context) );
4359 if (_lclose16( BX_reg(context) ) == HFILE_ERROR16)
4360 bSetDOSExtendedError = TRUE;
4361 else
4362 RESET_CFLAG(context);
4363 break;
4365 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4366 TRACE( "READ from %d to %04X:%04X for %d bytes\n",
4367 BX_reg(context),
4368 context->SegDs,
4369 DX_reg(context),
4370 CX_reg(context) );
4372 DWORD result;
4373 WORD count = CX_reg(context);
4374 BYTE *buffer = CTX_SEG_OFF_TO_LIN( context,
4375 context->SegDs,
4376 context->Edx );
4378 /* Some programs pass a count larger than the allocated buffer */
4379 DWORD maxcount = GetSelectorLimit16( context->SegDs ) - DX_reg(context) + 1;
4380 if (count > maxcount) count = maxcount;
4383 * FIXME: Reading from console (BX=1) in DOS mode
4384 * does not work as it is supposed to work.
4387 RESET_CFLAG(context); /* set if error */
4388 if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context)), buffer, count, &result, NULL ))
4389 SET_AX( context, (WORD)result );
4390 else
4391 bSetDOSExtendedError = TRUE;
4393 break;
4395 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4396 TRACE( "WRITE from %04X:%04X to handle %d for %d byte\n",
4397 context->SegDs, DX_reg(context),
4398 BX_reg(context), CX_reg(context) );
4400 char *ptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4401 HFILE handle = (HFILE)DosFileHandleToWin32Handle(BX_reg(context));
4402 LONG result = _hwrite( handle, ptr, CX_reg(context) );
4403 if (result == HFILE_ERROR)
4404 bSetDOSExtendedError = TRUE;
4405 else
4407 SET_AX( context, (WORD)result );
4408 RESET_CFLAG(context);
4411 break;
4413 case 0x41: /* "UNLINK" - DELETE FILE */
4415 WCHAR fileW[MAX_PATH];
4416 char *fileA = CTX_SEG_OFF_TO_LIN(context,
4417 context->SegDs,
4418 context->Edx);
4420 TRACE( "UNLINK %s\n", fileA );
4421 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
4423 if (!DeleteFileW( fileW ))
4424 bSetDOSExtendedError = TRUE;
4425 else
4426 RESET_CFLAG(context);
4428 break;
4430 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4431 TRACE( "LSEEK handle %d offset %d from %s\n",
4432 BX_reg(context),
4433 MAKELONG( DX_reg(context), CX_reg(context) ),
4434 (AL_reg(context) == 0) ?
4435 "start of file" : ((AL_reg(context) == 1) ?
4436 "current file position" : "end of file") );
4438 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4439 LONG offset = MAKELONG( DX_reg(context), CX_reg(context) );
4440 DWORD status = SetFilePointer( handle, offset,
4441 NULL, AL_reg(context) );
4442 if (status == INVALID_SET_FILE_POINTER)
4443 bSetDOSExtendedError = TRUE;
4444 else
4446 SET_AX( context, LOWORD(status) );
4447 SET_DX( context, HIWORD(status) );
4448 RESET_CFLAG(context);
4451 break;
4453 case 0x43: /* FILE ATTRIBUTES */
4454 if (!INT21_FileAttributes( context, AL_reg(context), FALSE ))
4455 bSetDOSExtendedError = TRUE;
4456 else
4457 RESET_CFLAG(context);
4458 break;
4460 case 0x44: /* IOCTL */
4461 INT21_Ioctl( context );
4462 break;
4464 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4465 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context) );
4467 HANDLE handle32;
4468 HFILE handle16 = HFILE_ERROR;
4470 if (DuplicateHandle( GetCurrentProcess(),
4471 DosFileHandleToWin32Handle(BX_reg(context)),
4472 GetCurrentProcess(),
4473 &handle32,
4474 0, TRUE, DUPLICATE_SAME_ACCESS ))
4475 handle16 = Win32HandleToDosFileHandle(handle32);
4477 if (handle16 == HFILE_ERROR)
4478 bSetDOSExtendedError = TRUE;
4479 else
4481 SET_AX( context, handle16 );
4482 RESET_CFLAG(context);
4485 break;
4487 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4488 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4489 BX_reg(context), CX_reg(context) );
4490 if (!INT21_Dup2(BX_reg(context), CX_reg(context)))
4491 bSetDOSExtendedError = TRUE;
4492 else
4493 RESET_CFLAG(context);
4494 break;
4496 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4497 if (!INT21_GetCurrentDirectory( context, FALSE ))
4498 bSetDOSExtendedError = TRUE;
4499 else
4500 RESET_CFLAG(context);
4501 break;
4503 case 0x48: /* ALLOCATE MEMORY */
4504 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
4506 DWORD bytes = (DWORD)BX_reg(context) << 4;
4507 DWORD rv = GlobalDOSAlloc16( bytes );
4508 WORD selector = LOWORD( rv );
4510 if (selector)
4512 SET_AX( context, selector );
4513 RESET_CFLAG(context);
4515 else
4517 SET_CFLAG(context);
4518 SET_AX( context, 0x0008 ); /* insufficient memory */
4519 SET_BX( context, DOSMEM_Available() >> 4 );
4522 break;
4524 case 0x49: /* FREE MEMORY */
4525 TRACE( "FREE MEMORY segment %04X\n", context->SegEs );
4527 BOOL ok = !GlobalDOSFree16( context->SegEs );
4529 /* If we don't reset ES_reg, we will fail in the relay code */
4530 if (ok) context->SegEs = 0;
4531 else
4533 TRACE("FREE MEMORY failed\n");
4534 SET_CFLAG(context);
4535 SET_AX( context, 0x0009 ); /* memory block address invalid */
4538 break;
4540 case 0x4a: /* RESIZE MEMORY BLOCK */
4541 TRACE( "RESIZE MEMORY segment %04X to %d paragraphs\n",
4542 context->SegEs, BX_reg(context) );
4544 FIXME( "Resize memory block - unsupported under Win16\n" );
4545 SET_CFLAG(context);
4547 break;
4549 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4551 char *program = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4552 HINSTANCE16 instance;
4554 TRACE( "EXEC %s\n", program );
4556 RESET_CFLAG(context);
4557 instance = WinExec16( program, SW_NORMAL );
4558 if (instance < 32)
4560 SET_CFLAG( context );
4561 SET_AX( context, instance );
4564 break;
4566 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
4567 TRACE( "EXIT with return code %d\n", AL_reg(context) );
4568 DOSVM_Exit( AL_reg(context) );
4569 break;
4571 case 0x4d: /* GET RETURN CODE */
4572 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
4573 SET_AX( context, 0 );
4574 break;
4576 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
4577 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
4578 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
4579 if (!INT21_FindFirst(context)) break;
4580 /* fall through */
4582 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
4583 TRACE("FINDNEXT\n");
4584 if (!INT21_FindNext(context))
4586 SetLastError( ERROR_NO_MORE_FILES );
4587 SET_AX( context, ERROR_NO_MORE_FILES );
4588 SET_CFLAG(context);
4590 else SET_AX( context, 0 ); /* OK */
4591 break;
4593 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
4594 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
4595 DOSVM_psp = BX_reg(context);
4596 break;
4598 case 0x51: /* GET PSP ADDRESS */
4599 INT21_GetPSP( context );
4600 break;
4602 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
4603 TRACE("Get List of Lists - not supported\n");
4604 context->SegEs = 0;
4605 SET_BX( context, 0 );
4606 break;
4608 case 0x54: /* Get Verify Flag */
4609 TRACE("Get Verify Flag - Not Supported\n");
4610 SET_AL( context, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
4611 break;
4613 case 0x56: /* "RENAME" - RENAME FILE */
4614 if (!INT21_RenameFile( context ))
4615 bSetDOSExtendedError = TRUE;
4616 else
4617 RESET_CFLAG(context);
4618 break;
4620 case 0x57: /* FILE DATE AND TIME */
4621 if (!INT21_FileDateTime( context ))
4622 bSetDOSExtendedError = TRUE;
4623 else
4624 RESET_CFLAG(context);
4625 break;
4627 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
4628 switch (AL_reg(context))
4630 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
4631 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
4632 SET_AX( context, 0 ); /* low memory first fit */
4633 break;
4635 case 0x01: /* SET ALLOCATION STRATEGY */
4636 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
4637 BL_reg(context) );
4638 break;
4640 case 0x02: /* GET UMB LINK STATE */
4641 TRACE( "GET UMB LINK STATE\n" );
4642 SET_AL( context, 0 ); /* UMBs not part of DOS memory chain */
4643 break;
4645 case 0x03: /* SET UMB LINK STATE */
4646 TRACE( "SET UMB LINK STATE to %d - ignored\n",
4647 BX_reg(context) );
4648 break;
4650 default:
4651 INT_BARF( context, 0x21 );
4653 break;
4655 case 0x59: /* GET EXTENDED ERROR INFO */
4656 INT21_GetExtendedError( context );
4657 break;
4659 case 0x5a: /* CREATE TEMPORARY FILE */
4660 TRACE("CREATE TEMPORARY FILE\n");
4661 bSetDOSExtendedError = !INT21_CreateTempFile(context);
4662 break;
4664 case 0x5b: /* CREATE NEW FILE */
4665 if (!INT21_CreateFile( context, context->Edx, FALSE,
4666 OF_READWRITE | OF_SHARE_COMPAT, 0x10 ))
4667 bSetDOSExtendedError = TRUE;
4668 else
4669 RESET_CFLAG(context);
4670 break;
4672 case 0x5c: /* "FLOCK" - RECORD LOCKING */
4674 DWORD offset = MAKELONG(DX_reg(context), CX_reg(context));
4675 DWORD length = MAKELONG(DI_reg(context), SI_reg(context));
4676 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4678 RESET_CFLAG(context);
4679 switch (AL_reg(context))
4681 case 0x00: /* LOCK */
4682 TRACE( "lock handle %d offset %d length %d\n",
4683 BX_reg(context), offset, length );
4684 if (!LockFile( handle, offset, 0, length, 0 ))
4685 bSetDOSExtendedError = TRUE;
4686 break;
4688 case 0x01: /* UNLOCK */
4689 TRACE( "unlock handle %d offset %d length %d\n",
4690 BX_reg(context), offset, length );
4691 if (!UnlockFile( handle, offset, 0, length, 0 ))
4692 bSetDOSExtendedError = TRUE;
4693 break;
4695 default:
4696 INT_BARF( context, 0x21 );
4699 break;
4701 case 0x5d: /* NETWORK 5D */
4702 FIXME( "Network function 5D not implemented.\n" );
4703 SetLastError( ER_NoNetwork );
4704 bSetDOSExtendedError = TRUE;
4705 break;
4707 case 0x5e: /* NETWORK 5E */
4708 bSetDOSExtendedError = INT21_NetworkFunc( context);
4709 break;
4711 case 0x5f: /* NETWORK 5F */
4712 /* FIXME: supporting this would need to 1:
4713 * - implement per drive current directory (as kernel32 doesn't)
4714 * - assign enabled/disabled flag on a per drive basis
4716 /* network software not installed */
4717 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
4718 SetLastError( ER_NoNetwork );
4719 bSetDOSExtendedError = TRUE;
4720 break;
4722 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
4724 WCHAR pathW[MAX_PATH], res[MAX_PATH];
4725 /* FIXME: likely to be broken */
4727 TRACE("TRUENAME %s\n",
4728 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
4729 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
4730 if (!GetFullPathNameW( pathW, 128, res, NULL ))
4731 bSetDOSExtendedError = TRUE;
4732 else
4734 SET_AX( context, 0 );
4735 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
4736 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
4737 128, NULL, NULL);
4740 break;
4742 case 0x61: /* UNUSED */
4743 SET_AL( context, 0 );
4744 break;
4746 case 0x62: /* GET PSP ADDRESS */
4747 INT21_GetPSP( context );
4748 break;
4750 case 0x63: /* MISC. LANGUAGE SUPPORT */
4751 switch (AL_reg(context)) {
4752 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
4753 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
4754 context->SegDs = INT21_GetHeapSelector( context );
4755 SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
4756 SET_AL( context, 0 ); /* success */
4757 break;
4759 break;
4761 case 0x64: /* OS/2 DOS BOX */
4762 INT_BARF( context, 0x21 );
4763 SET_CFLAG(context);
4764 break;
4766 case 0x65: /* EXTENDED COUNTRY INFORMATION */
4767 INT21_ExtendedCountryInformation( context );
4768 break;
4770 case 0x66: /* GLOBAL CODE PAGE TABLE */
4771 switch (AL_reg(context))
4773 case 0x01:
4774 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
4775 SET_BX( context, GetOEMCP() );
4776 SET_DX( context, GetOEMCP() );
4777 break;
4778 case 0x02:
4779 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
4780 BX_reg(context), DX_reg(context) );
4781 break;
4783 break;
4785 case 0x67: /* SET HANDLE COUNT */
4786 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
4787 if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
4788 bSetDOSExtendedError = TRUE;
4789 break;
4791 case 0x68: /* "FFLUSH" - COMMIT FILE */
4792 TRACE( "FFLUSH - handle %d\n", BX_reg(context) );
4793 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ) &&
4794 GetLastError() != ERROR_ACCESS_DENIED)
4795 bSetDOSExtendedError = TRUE;
4796 break;
4798 case 0x69: /* DISK SERIAL NUMBER */
4799 switch (AL_reg(context))
4801 case 0x00:
4802 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
4803 INT21_DriveName(BL_reg(context)));
4804 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
4805 else SET_AX( context, 0 );
4806 break;
4808 case 0x01:
4809 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
4810 INT21_DriveName(BL_reg(context)));
4811 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
4812 else SET_AX( context, 1 );
4813 break;
4815 break;
4817 case 0x6a: /* COMMIT FILE */
4818 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context) );
4819 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ) &&
4820 GetLastError() != ERROR_ACCESS_DENIED)
4821 bSetDOSExtendedError = TRUE;
4822 break;
4824 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4825 SET_AL( context, 0 );
4826 break;
4828 case 0x6c: /* EXTENDED OPEN/CREATE */
4829 if (!INT21_CreateFile( context, context->Esi, TRUE,
4830 BX_reg(context), DL_reg(context) ))
4831 bSetDOSExtendedError = TRUE;
4832 break;
4834 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
4835 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
4836 SET_CFLAG( context );
4837 SET_AL( context, 0 );
4838 break;
4840 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
4841 INT21_LongFilename( context );
4842 break;
4844 case 0x73: /* MSDOS7 - FAT32 */
4845 RESET_CFLAG( context );
4846 if (!INT21_Fat32( context ))
4847 bSetDOSExtendedError = TRUE;
4848 break;
4850 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
4851 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
4852 break;
4854 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
4855 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
4856 break;
4858 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
4859 /* we don't implement a DOS32 extender */
4860 TRACE( "DOS32 EXTENDER API - ignored\n" );
4861 break;
4863 default:
4864 INT_BARF( context, 0x21 );
4865 break;
4867 } /* END OF SWITCH */
4869 /* Set general error condition. */
4870 if (bSetDOSExtendedError)
4872 SET_AX( context, GetLastError() );
4873 SET_CFLAG( context );
4876 /* Print error code if carry flag is set. */
4877 if (context->EFlags & 0x0001)
4878 TRACE("failed, error %d\n", GetLastError() );
4880 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
4881 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
4882 AX_reg(context), BX_reg(context),
4883 CX_reg(context), DX_reg(context),
4884 SI_reg(context), DI_reg(context),
4885 (WORD)context->SegDs, (WORD)context->SegEs,
4886 context->EFlags );