krnl386.exe16: Make a couple of functions static.
[wine.git] / dlls / krnl386.exe16 / int21.c
blob3a08749d41f64bc5c5ed6d8bc839d7733200d958
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 "config.h"
27 #include "wine/port.h"
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winreg.h"
41 #include "winternl.h"
42 #include "wine/winbase16.h"
43 #include "kernel16_private.h"
44 #include "dosexe.h"
45 #include "winerror.h"
46 #include "winuser.h"
47 #include "wine/unicode.h"
48 #include "wine/server.h"
49 #include "wine/debug.h"
50 #include "wine/exception.h"
52 BOOL WINAPI VerifyConsoleIoHandle(HANDLE);
54 * Note:
55 * - Most of the file related functions are wrong. NT's kernel32
56 * doesn't maintain a per drive current directory, while DOS does.
57 * We should in fact keep track in here of those per drive
58 * directories, and use this info while dealing with partial paths
59 * (drive defined, but only relative paths). This could even be
60 * created as an array of CDS (there should be an entry for that in
61 * the LOL)
65 * Forward declarations.
67 static BOOL INT21_RenameFile( CONTEXT *context );
69 WINE_DEFAULT_DEBUG_CHANNEL(int21);
72 #include "pshpack1.h"
75 * Extended Drive Parameter Block.
76 * This structure is compatible with standard DOS4+ DPB and
77 * extended DOS7 DPB.
79 typedef struct _INT21_DPB {
80 BYTE drive; /* 00 drive number (0=A, ...) */
81 BYTE unit; /* 01 unit number within device driver */
82 WORD sector_bytes; /* 02 bytes per sector */
83 BYTE cluster_sectors; /* 04 highest sector number within a cluster */
84 BYTE shift; /* 05 shift count to convert clusters into sectors */
85 WORD num_reserved; /* 06 reserved sectors at beginning of drive */
86 BYTE num_FAT; /* 08 number of FATs */
87 WORD num_root_entries; /* 09 number of root directory entries */
88 WORD first_data_sector; /* 0b number of first sector containing user data */
89 WORD num_clusters1; /* 0d highest cluster number (number of data clusters + 1) */
90 WORD sectors_per_FAT; /* 0f number of sectors per FAT */
91 WORD first_dir_sector; /* 11 sector number of first directory sector */
92 SEGPTR driver_header; /* 13 address of device driver header */
93 BYTE media_ID; /* 17 media ID byte */
94 BYTE access_flag; /* 18 0x00 if disk accessed, 0xff if not */
95 SEGPTR next; /* 19 pointer to next DPB */
96 WORD search_cluster1; /* 1d cluster at which to start search for free space */
97 WORD free_clusters_lo; /* 1f number of free clusters on drive or 0xffff if unknown */
98 WORD free_clusters_hi; /* 21 hiword of clusters_free */
99 WORD mirroring_flags; /* 23 active FAT/mirroring flags */
100 WORD info_sector; /* 25 sector number of file system info sector or 0xffff for none */
101 WORD spare_boot_sector; /* 27 sector number of backup boot sector or 0xffff for none */
102 DWORD first_cluster_sector; /* 29 sector number of the first cluster */
103 DWORD num_clusters2; /* 2d maximum cluster number */
104 DWORD fat_clusters; /* 31 number of clusters occupied by FAT */
105 DWORD root_cluster; /* 35 cluster number of start of root directory */
106 DWORD search_cluster2; /* 39 cluster at which to start searching for free space */
107 } INT21_DPB;
111 * Structure for DOS data that can be accessed directly from applications.
112 * Real and protected mode pointers will be returned to this structure so
113 * the structure must be correctly packed.
115 typedef struct _INT21_HEAP {
116 WORD uppercase_size; /* Size of the following table in bytes */
117 BYTE uppercase_table[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
119 WORD lowercase_size; /* Size of the following table in bytes */
120 BYTE lowercase_table[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
122 WORD collating_size; /* Size of the following table in bytes */
123 BYTE collating_table[256]; /* Values used to sort characters from 0x00 to 0xff. */
125 WORD filename_size; /* Size of the following filename data in bytes */
126 BYTE filename_reserved1; /* 0x01 for MS-DOS 3.30-6.00 */
127 BYTE filename_lowest; /* Lowest permissible character value for filename */
128 BYTE filename_highest; /* Highest permissible character value for filename */
129 BYTE filename_reserved2; /* 0x00 for MS-DOS 3.30-6.00 */
130 BYTE filename_exclude_first; /* First illegal character in permissible range */
131 BYTE filename_exclude_last; /* Last illegal character in permissible range */
132 BYTE filename_reserved3; /* 0x02 for MS-DOS 3.30-6.00 */
133 BYTE filename_illegal_size; /* Number of terminators in the following table */
134 BYTE filename_illegal_table[16]; /* Characters which terminate a filename */
136 WORD dbcs_size; /* Number of valid ranges in the following table */
137 BYTE dbcs_table[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
139 BYTE misc_indos; /* Interrupt 21 nesting flag */
140 WORD misc_selector; /* Protected mode selector for INT21_HEAP */
141 INT21_DPB misc_dpb_list[MAX_DOS_DRIVES]; /* Drive parameter blocks for all drives */
143 } INT21_HEAP;
146 struct FCB {
147 BYTE drive_number;
148 CHAR file_name[8];
149 CHAR file_extension[3];
150 WORD current_block_number;
151 WORD logical_record_size;
152 DWORD file_size;
153 WORD date_of_last_write;
154 WORD time_of_last_write;
155 BYTE file_number;
156 BYTE attributes;
157 WORD starting_cluster;
158 WORD sequence_number;
159 BYTE file_attributes;
160 BYTE unused;
161 BYTE record_within_current_block;
162 BYTE random_access_record_number[4];
166 struct XFCB {
167 BYTE xfcb_signature;
168 BYTE reserved[5];
169 BYTE xfcb_file_attribute;
170 BYTE fcb[37];
173 /* DTA layout for FindFirst/FindNext */
174 typedef struct
176 BYTE drive; /* 00 drive letter */
177 char mask[11]; /* 01 search template */
178 BYTE search_attr; /* 0c search attributes */
179 WORD count; /* 0d entry count within directory */
180 WORD cluster; /* 0f cluster of parent directory */
181 WCHAR *fullPath; /* 11 full path (was: reserved) */
182 BYTE fileattr; /* 15 file attributes */
183 WORD filetime; /* 16 file time */
184 WORD filedate; /* 18 file date */
185 DWORD filesize; /* 1a file size */
186 char filename[13]; /* 1e file name + extension */
187 } FINDFILE_DTA;
189 /* FCB layout for FindFirstFCB/FindNextFCB */
190 typedef struct
192 BYTE drive; /* 00 drive letter */
193 char filename[11]; /* 01 filename 8+3 format */
194 int count; /* 0c entry count (was: reserved) */
195 WCHAR *fullPath; /* 10 full path (was: reserved) */
196 } FINDFILE_FCB;
198 /* DOS directory entry for FindFirstFCB/FindNextFCB */
199 typedef struct
201 char filename[11]; /* 00 filename 8+3 format */
202 BYTE fileattr; /* 0b file attributes */
203 BYTE reserved[10]; /* 0c reserved */
204 WORD filetime; /* 16 file time */
205 WORD filedate; /* 18 file date */
206 WORD cluster; /* 1a file first cluster */
207 DWORD filesize; /* 1c file size */
208 } DOS_DIRENTRY_LAYOUT;
210 #include "poppack.h"
212 /* dos file attributes */
213 #define FA_NORMAL 0x00 /* Normal file, no attributes */
214 #define FA_RDONLY 0x01 /* Read only attribute */
215 #define FA_HIDDEN 0x02 /* Hidden file */
216 #define FA_SYSTEM 0x04 /* System file */
217 #define FA_LABEL 0x08 /* Volume label */
218 #define FA_DIRECTORY 0x10 /* Directory */
219 #define FA_ARCHIVE 0x20 /* Archive */
220 #define FA_UNUSED 0x40 /* Unused */
222 /* Error codes */
223 #define ER_NoNetwork 0x49
225 /* Error classes */
226 #define EC_OutOfResource 0x01
227 #define EC_Temporary 0x02
228 #define EC_AccessDenied 0x03
229 #define EC_InternalError 0x04
230 #define EC_HardwareFailure 0x05
231 #define EC_SystemFailure 0x06
232 #define EC_ProgramError 0x07
233 #define EC_NotFound 0x08
234 #define EC_MediaError 0x0b
235 #define EC_Exists 0x0c
236 #define EC_Unknown 0x0d
238 /* Suggested actions */
239 #define SA_Retry 0x01
240 #define SA_DelayedRetry 0x02
241 #define SA_Abort 0x04
242 #define SA_Ignore 0x06
243 #define SA_Ask4Retry 0x07
245 /* Error locus */
246 #define EL_Unknown 0x01
247 #define EL_Disk 0x02
248 #define EL_Network 0x03
249 #define EL_Serial 0x04
250 #define EL_Memory 0x05
252 /* BIOS Keyboard Scancodes */
253 #define KEY_LEFT 0x4B
254 #define KEY_RIGHT 0x4D
255 #define KEY_UP 0x48
256 #define KEY_DOWN 0x50
257 #define KEY_IC 0x52 /* insert char */
258 #define KEY_DC 0x53 /* delete char */
259 #define KEY_BACKSPACE 0x0E
260 #define KEY_HOME 0x47
261 #define KEY_END 0x4F
262 #define KEY_NPAGE 0x49
263 #define KEY_PPAGE 0x51
265 static int brk_flag;
267 struct magic_device
269 WCHAR name[10];
270 HANDLE handle;
271 LARGE_INTEGER index;
272 void (*ioctl_handler)(CONTEXT *);
275 static void INT21_IoctlScsiMgrHandler( CONTEXT * );
276 static void INT21_IoctlHPScanHandler( CONTEXT * );
278 static struct magic_device magic_devices[] =
280 { {'s','c','s','i','m','g','r','$',0}, NULL, { { 0, 0 } }, INT21_IoctlScsiMgrHandler },
281 { {'e','m','m','x','x','x','x','0',0}, NULL, { { 0, 0 } }, EMS_Ioctl_Handler },
282 { {'h','p','s','c','a','n',0}, NULL, { { 0, 0 } }, INT21_IoctlHPScanHandler },
285 #define NB_MAGIC_DEVICES (sizeof(magic_devices)/sizeof(magic_devices[0]))
288 /* Many calls translate a drive argument like this:
289 drive number (00h = default, 01h = A:, etc)
291 /******************************************************************
292 * INT21_DriveName
294 * Many calls translate a drive argument like this:
295 * drive number (00h = default, 01h = A:, etc)
297 static const char *INT21_DriveName(int drive)
299 if (drive > 0)
301 if (drive <= 26) return wine_dbg_sprintf("%c:", 'A' + drive - 1);
302 else return wine_dbg_sprintf( "<Bad drive: %d>", drive);
304 return "default";
307 /***********************************************************************
308 * INT21_GetCurrentDrive
310 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
311 * MAX_DOS_DRIVES on error.
313 static BYTE INT21_GetCurrentDrive(void)
315 WCHAR current_directory[MAX_PATH];
317 if (!GetCurrentDirectoryW( MAX_PATH, current_directory ) ||
318 current_directory[1] != ':')
320 TRACE( "Failed to get current drive.\n" );
321 return MAX_DOS_DRIVES;
324 return toupperW( current_directory[0] ) - 'A';
328 /***********************************************************************
329 * INT21_MapDrive
331 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
332 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
334 static BYTE INT21_MapDrive( BYTE drive )
336 if (drive)
338 WCHAR drivespec[] = {'A', ':', 0};
339 UINT drivetype;
341 drivespec[0] += drive - 1;
342 drivetype = GetDriveTypeW( drivespec );
344 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
345 return MAX_DOS_DRIVES;
347 return drive - 1;
350 return INT21_GetCurrentDrive();
354 /***********************************************************************
355 * INT21_SetCurrentDrive
357 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
359 static void INT21_SetCurrentDrive( BYTE drive )
361 WCHAR drivespec[] = {'A', ':', 0};
363 drivespec[0] += drive;
365 if (!SetCurrentDirectoryW( drivespec ))
366 TRACE( "Failed to set current drive.\n" );
370 /***********************************************************************
371 * INT21_GetSystemCountryCode
373 * Return DOS country code for default system locale.
375 static WORD INT21_GetSystemCountryCode( void )
377 return GetSystemDefaultLangID();
381 /***********************************************************************
382 * INT21_FillCountryInformation
384 * Fill 34-byte buffer with country information data using
385 * default system locale.
387 static void INT21_FillCountryInformation( BYTE *buffer )
389 /* 00 - WORD: date format
390 * 00 = mm/dd/yy
391 * 01 = dd/mm/yy
392 * 02 = yy/mm/dd
394 *(WORD*)(buffer + 0) = 0; /* FIXME: Get from locale */
396 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
397 buffer[2] = '$'; /* FIXME: Get from locale */
398 buffer[3] = 0;
400 /* 07 - BYTE[2]: ASCIIZ thousands separator */
401 buffer[7] = 0; /* FIXME: Get from locale */
402 buffer[8] = 0;
404 /* 09 - BYTE[2]: ASCIIZ decimal separator */
405 buffer[9] = '.'; /* FIXME: Get from locale */
406 buffer[10] = 0;
408 /* 11 - BYTE[2]: ASCIIZ date separator */
409 buffer[11] = '/'; /* FIXME: Get from locale */
410 buffer[12] = 0;
412 /* 13 - BYTE[2]: ASCIIZ time separator */
413 buffer[13] = ':'; /* FIXME: Get from locale */
414 buffer[14] = 0;
416 /* 15 - BYTE: Currency format
417 * bit 2 = set if currency symbol replaces decimal point
418 * bit 1 = number of spaces between value and currency symbol
419 * bit 0 = 0 if currency symbol precedes value
420 * 1 if currency symbol follows value
422 buffer[15] = 0; /* FIXME: Get from locale */
424 /* 16 - BYTE: Number of digits after decimal in currency */
425 buffer[16] = 0; /* FIXME: Get from locale */
427 /* 17 - BYTE: Time format
428 * bit 0 = 0 if 12-hour clock
429 * 1 if 24-hour clock
431 buffer[17] = 1; /* FIXME: Get from locale */
433 /* 18 - DWORD: Address of case map routine */
434 *(DWORD*)(buffer + 18) = 0; /* FIXME: ptr to case map routine */
436 /* 22 - BYTE[2]: ASCIIZ data-list separator */
437 buffer[22] = ','; /* FIXME: Get from locale */
438 buffer[23] = 0;
440 /* 24 - BYTE[10]: Reserved */
441 memset( buffer + 24, 0, 10 );
445 /***********************************************************************
446 * INT21_FillHeap
448 * Initialize DOS heap.
450 * Filename Terminator Table of w2k DE NTVDM:
451 * 16 00 01 00 FF 00 00 20-02 0E 2E 22 2F 5C 5B 5D ....... ..."/\[]
452 * 3A 7C 3C 3E 2B 3D 3B 2C-00 :|<>+=;,
454 static void INT21_FillHeap( INT21_HEAP *heap )
456 static const char terminators[] = ".\"/\\[]:|<>+=;,";
457 int i;
460 * Uppercase table.
462 heap->uppercase_size = 128;
463 for (i = 0; i < 128; i++)
464 heap->uppercase_table[i] = toupper( 128 + i );
467 * Lowercase table.
469 heap->lowercase_size = 256;
470 for (i = 0; i < 256; i++)
471 heap->lowercase_table[i] = tolower( i );
474 * Collating table.
476 heap->collating_size = 256;
477 for (i = 0; i < 256; i++)
478 heap->collating_table[i] = i;
481 * Filename table.
483 heap->filename_size = 8 + strlen(terminators);
484 heap->filename_illegal_size = strlen(terminators);
485 memcpy( heap->filename_illegal_table, terminators, heap->filename_illegal_size );
487 heap->filename_reserved1 = 0x01;
488 heap->filename_lowest = 0x00;
489 heap->filename_highest = 0xff;
490 heap->filename_reserved2 = 0x00;
491 heap->filename_exclude_first = 0x00;
492 heap->filename_exclude_last = 0x20;
493 heap->filename_reserved3 = 0x02;
496 * DBCS lead byte table. This table is empty.
498 heap->dbcs_size = 0;
499 memset( heap->dbcs_table, 0, sizeof(heap->dbcs_table) );
502 * Initialize InDos flag.
504 heap->misc_indos = 0;
507 * FIXME: Should drive parameter blocks (DPB) be
508 * initialized here and linked to DOS LOL?
513 /***********************************************************************
514 * INT21_GetHeapPointer
516 * Get pointer for DOS heap (INT21_HEAP).
517 * Creates and initializes heap on first call.
519 static INT21_HEAP *INT21_GetHeapPointer( void )
521 static INT21_HEAP *heap_pointer = NULL;
523 if (!heap_pointer)
525 WORD heap_selector;
527 heap_pointer = DOSVM_AllocDataUMB( sizeof(INT21_HEAP), &heap_selector );
528 heap_pointer->misc_selector = heap_selector;
529 INT21_FillHeap( heap_pointer );
532 return heap_pointer;
536 /***********************************************************************
537 * INT21_GetHeapSelector
539 * Get segment/selector for DOS heap (INT21_HEAP).
540 * Creates and initializes heap on first call.
542 static WORD INT21_GetHeapSelector( CONTEXT *context )
544 INT21_HEAP *heap = INT21_GetHeapPointer();
545 return heap->misc_selector;
549 /***********************************************************************
550 * INT21_FillDrivePB
552 * Fill DOS heap drive parameter block for the specified drive.
553 * Return TRUE if drive was valid and there were
554 * no errors while reading drive information.
556 static BOOL INT21_FillDrivePB( BYTE drive )
558 WCHAR drivespec[] = {'A', ':', 0};
559 INT21_HEAP *heap = INT21_GetHeapPointer();
560 INT21_DPB *dpb;
561 UINT drivetype;
562 DWORD cluster_sectors;
563 DWORD sector_bytes;
564 DWORD free_clusters;
565 DWORD total_clusters;
567 if (drive >= MAX_DOS_DRIVES)
568 return FALSE;
570 dpb = &heap->misc_dpb_list[drive];
571 drivespec[0] += drive;
572 drivetype = GetDriveTypeW( drivespec );
575 * FIXME: Does this check work correctly with floppy/cdrom drives?
577 if (drivetype == DRIVE_NO_ROOT_DIR || drivetype == DRIVE_UNKNOWN)
578 return FALSE;
581 * FIXME: Does this check work correctly with floppy/cdrom drives?
583 if (!GetDiskFreeSpaceW( drivespec, &cluster_sectors, &sector_bytes,
584 &free_clusters, &total_clusters ))
585 return FALSE;
588 * FIXME: Most of the values listed below are incorrect.
589 * All values should be validated.
592 dpb->drive = drive;
593 dpb->unit = 0;
594 dpb->sector_bytes = sector_bytes;
595 dpb->cluster_sectors = cluster_sectors - 1;
597 dpb->shift = 0;
598 while (cluster_sectors > 1)
600 cluster_sectors /= 2;
601 dpb->shift++;
604 dpb->num_reserved = 0;
605 dpb->num_FAT = 1;
606 dpb->num_root_entries = 2;
607 dpb->first_data_sector = 2;
608 dpb->num_clusters1 = total_clusters;
609 dpb->sectors_per_FAT = 1;
610 dpb->first_dir_sector = 1;
611 dpb->driver_header = 0;
612 dpb->media_ID = (drivetype == DRIVE_FIXED) ? 0xF8 : 0xF0;
613 dpb->access_flag = 0;
614 dpb->next = 0;
615 dpb->search_cluster1 = 0;
616 dpb->free_clusters_lo = LOWORD(free_clusters);
617 dpb->free_clusters_hi = HIWORD(free_clusters);
618 dpb->mirroring_flags = 0;
619 dpb->info_sector = 0xffff;
620 dpb->spare_boot_sector = 0xffff;
621 dpb->first_cluster_sector = 0;
622 dpb->num_clusters2 = total_clusters;
623 dpb->fat_clusters = 32;
624 dpb->root_cluster = 0;
625 dpb->search_cluster2 = 0;
627 return TRUE;
631 /***********************************************************************
632 * INT21_GetCurrentDirectory
634 * Handler for:
635 * - function 0x47
636 * - subfunction 0x47 of function 0x71
638 static BOOL INT21_GetCurrentDirectory( CONTEXT *context, BOOL islong )
640 char *buffer = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi);
641 BYTE drive = INT21_MapDrive( DL_reg(context) );
642 WCHAR pathW[MAX_PATH];
643 char pathA[MAX_PATH];
644 WCHAR *ptr = pathW;
646 TRACE( "drive %d\n", DL_reg(context) );
648 if (drive == MAX_DOS_DRIVES)
650 SetLastError(ERROR_INVALID_DRIVE);
651 return FALSE;
655 * Grab current directory.
658 if (!GetCurrentDirectoryW( MAX_PATH, pathW )) return FALSE;
660 if (toupperW(pathW[0]) - 'A' != drive || pathW[1] != ':')
662 /* cwd is not on the requested drive, get the environment string instead */
664 WCHAR env_var[4];
666 env_var[0] = '=';
667 env_var[1] = 'A' + drive;
668 env_var[2] = ':';
669 env_var[3] = 0;
670 if (!GetEnvironmentVariableW( env_var, pathW, MAX_PATH ))
672 /* return empty path */
673 buffer[0] = 0;
674 return TRUE;
679 * Convert into short format.
682 if (!islong)
684 DWORD result = GetShortPathNameW( pathW, pathW, MAX_PATH );
685 if (!result)
686 return FALSE;
687 if (result > MAX_PATH)
689 WARN( "Short path too long!\n" );
690 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
691 return FALSE;
696 * The returned pathname does not include
697 * the drive letter, colon or leading backslash.
700 if (ptr[0] == '\\')
703 * FIXME: We should probably just strip host part from name...
705 FIXME( "UNC names are not supported.\n" );
706 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
707 return FALSE;
709 else if (!ptr[0] || ptr[1] != ':' || ptr[2] != '\\')
711 WARN( "Path is neither UNC nor DOS path: %s\n",
712 wine_dbgstr_w(ptr) );
713 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
714 return FALSE;
716 else
718 /* Remove drive letter, colon and leading backslash. */
719 ptr += 3;
723 * Convert into OEM string.
726 if (!WideCharToMultiByte(CP_OEMCP, 0, ptr, -1, pathA,
727 MAX_PATH, NULL, NULL))
729 WARN( "Cannot convert path!\n" );
730 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
731 return FALSE;
735 * Success.
738 if (!islong)
740 /* Undocumented success code. */
741 SET_AX( context, 0x0100 );
743 /* Truncate buffer to 64 bytes. */
744 pathA[63] = 0;
747 TRACE( "%c:=%s\n", 'A' + drive, pathA );
749 strcpy( buffer, pathA );
750 return TRUE;
754 /***********************************************************************
755 * INT21_SetCurrentDirectory
757 * Handler for:
758 * - function 0x3b
759 * - subfunction 0x3b of function 0x71
761 static BOOL INT21_SetCurrentDirectory( CONTEXT *context )
763 WCHAR dirW[MAX_PATH];
764 WCHAR env_var[4];
765 DWORD attr;
766 char *dirA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
767 BYTE drive = INT21_GetCurrentDrive();
768 BOOL result;
770 TRACE( "SET CURRENT DIRECTORY %s\n", dirA );
772 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
773 if (!GetFullPathNameW( dirW, MAX_PATH, dirW, NULL )) return FALSE;
775 attr = GetFileAttributesW( dirW );
776 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
778 SetLastError( ERROR_PATH_NOT_FOUND );
779 return FALSE;
782 env_var[0] = '=';
783 env_var[1] = dirW[0];
784 env_var[2] = ':';
785 env_var[3] = 0;
786 result = SetEnvironmentVariableW( env_var, dirW );
788 /* only set current directory if on the current drive */
789 if (result && (toupperW(dirW[0]) - 'A' == drive)) result = SetCurrentDirectoryW( dirW );
791 return result;
794 /***********************************************************************
795 * INT21_CreateMagicDeviceHandle
797 * Create a dummy file handle for a "magic" device.
799 static HANDLE INT21_CreateMagicDeviceHandle( LPCWSTR name )
801 static const WCHAR prefixW[] = {'\\','?','?','\\','u','n','i','x'};
802 const char *dir = wine_get_server_dir();
803 int len;
804 HANDLE ret;
805 NTSTATUS status;
806 OBJECT_ATTRIBUTES attr;
807 UNICODE_STRING nameW;
808 IO_STATUS_BLOCK io;
810 len = MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, NULL, 0 );
811 nameW.Length = sizeof(prefixW) + (len + strlenW( name )) * sizeof(WCHAR);
812 nameW.MaximumLength = nameW.Length + sizeof(WCHAR);
813 if (!(nameW.Buffer = HeapAlloc( GetProcessHeap(), 0, nameW.MaximumLength )))
815 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
816 return 0;
818 memcpy( nameW.Buffer, prefixW, sizeof(prefixW) );
819 MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, nameW.Buffer + sizeof(prefixW)/sizeof(WCHAR), len );
820 len += sizeof(prefixW) / sizeof(WCHAR);
821 nameW.Buffer[len-1] = '/';
822 strcpyW( nameW.Buffer + len, name );
824 attr.Length = sizeof(attr);
825 attr.RootDirectory = 0;
826 attr.Attributes = 0;
827 attr.ObjectName = &nameW;
828 attr.SecurityDescriptor = NULL;
829 attr.SecurityQualityOfService = NULL;
831 status = NtCreateFile( &ret, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &attr, &io, NULL, 0,
832 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF,
833 FILE_SYNCHRONOUS_IO_ALERT, NULL, 0 );
834 if (status)
836 ret = 0;
837 SetLastError( RtlNtStatusToDosError(status) );
839 RtlFreeUnicodeString( &nameW );
840 return ret;
844 /***********************************************************************
845 * INT21_OpenMagicDevice
847 * Open a file handle for "magic" devices like EMMXXXX0.
849 static HANDLE INT21_OpenMagicDevice( LPCWSTR name, DWORD access )
851 unsigned int i;
852 const WCHAR *p;
853 HANDLE handle;
855 if (name[0] && (name[1] == ':')) name += 2;
856 if ((p = strrchrW( name, '/' ))) name = p + 1;
857 if ((p = strrchrW( name, '\\' ))) name = p + 1;
859 for (i = 0; i < NB_MAGIC_DEVICES; i++)
861 int len = strlenW( magic_devices[i].name );
862 if (!strncmpiW( magic_devices[i].name, name, len ) &&
863 (!name[len] || name[len] == '.' || name[len] == ':')) break;
865 if (i == NB_MAGIC_DEVICES) return 0;
867 if (!magic_devices[i].handle) /* need to open it */
869 IO_STATUS_BLOCK io;
870 FILE_INTERNAL_INFORMATION info;
872 if (!(handle = INT21_CreateMagicDeviceHandle( magic_devices[i].name ))) return 0;
874 NtQueryInformationFile( handle, &io, &info, sizeof(info), FileInternalInformation );
875 magic_devices[i].index = info.IndexNumber;
876 magic_devices[i].handle = handle;
878 if (!DuplicateHandle( GetCurrentProcess(), magic_devices[i].handle,
879 GetCurrentProcess(), &handle, access, FALSE, 0 )) handle = 0;
880 return handle;
884 /***********************************************************************
885 * INT21_CreateFile
887 * Handler for:
888 * - function 0x3c
889 * - function 0x3d
890 * - function 0x5b
891 * - function 0x6c
892 * - subfunction 0x6c of function 0x71
894 static BOOL INT21_CreateFile( CONTEXT *context,
895 DWORD pathSegOff,
896 BOOL returnStatus,
897 WORD dosAccessShare,
898 BYTE dosAction )
900 WORD dosStatus;
901 char *pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, pathSegOff);
902 WCHAR pathW[MAX_PATH];
903 DWORD winAccess;
904 DWORD winAttributes;
905 HANDLE winHandle;
906 DWORD winMode;
907 DWORD winSharing;
909 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
910 "create flags=%04x, file=%s.\n",
911 AH_reg(context), dosAction, dosAccessShare, CX_reg(context), pathA );
914 * Application tried to create/open a file whose name
915 * ends with a backslash. This is not allowed.
917 * FIXME: This needs to be validated, especially the return value.
919 if (pathA[strlen(pathA) - 1] == '/')
921 SetLastError( ERROR_FILE_NOT_FOUND );
922 return FALSE;
926 * Convert DOS action flags into Win32 creation disposition parameter.
928 switch(dosAction)
930 case 0x01:
931 winMode = OPEN_EXISTING;
932 break;
933 case 0x02:
934 winMode = TRUNCATE_EXISTING;
935 break;
936 case 0x10:
937 winMode = CREATE_NEW;
938 break;
939 case 0x11:
940 winMode = OPEN_ALWAYS;
941 break;
942 case 0x12:
943 winMode = CREATE_ALWAYS;
944 break;
945 default:
946 SetLastError( ERROR_INVALID_PARAMETER );
947 return FALSE;
951 * Convert DOS access/share flags into Win32 desired access parameter.
953 switch(dosAccessShare & 0x07)
955 case OF_READ:
956 winAccess = GENERIC_READ;
957 break;
958 case OF_WRITE:
959 winAccess = GENERIC_WRITE;
960 break;
961 case OF_READWRITE:
962 winAccess = GENERIC_READ | GENERIC_WRITE;
963 break;
964 case 0x04:
966 * Read-only, do not modify file's last-access time (DOS7).
968 * FIXME: How to prevent modification of last-access time?
970 winAccess = GENERIC_READ;
971 break;
972 default:
973 winAccess = 0;
977 * Convert DOS access/share flags into Win32 share mode parameter.
979 switch(dosAccessShare & 0x70)
981 case OF_SHARE_EXCLUSIVE:
982 winSharing = 0;
983 break;
984 case OF_SHARE_DENY_WRITE:
985 winSharing = FILE_SHARE_READ;
986 break;
987 case OF_SHARE_DENY_READ:
988 winSharing = FILE_SHARE_WRITE;
989 break;
990 case OF_SHARE_DENY_NONE:
991 case OF_SHARE_COMPAT:
992 default:
993 winSharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
997 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
998 * What to do with this bit?
999 * FIXME: Bits in the high byte of dosAccessShare are not supported.
1000 * See both function 0x6c and subfunction 0x6c of function 0x71 for
1001 * definition of these bits.
1005 * Convert DOS create attributes into Win32 flags and attributes parameter.
1007 if (winMode == OPEN_EXISTING || winMode == TRUNCATE_EXISTING)
1009 winAttributes = 0;
1011 else
1013 WORD dosAttributes = CX_reg(context);
1015 if (dosAttributes & FA_LABEL)
1018 * Application tried to create volume label entry.
1019 * This is difficult to support so we do not allow it.
1021 * FIXME: If volume does not already have a label,
1022 * this function is supposed to succeed.
1024 SetLastError( ERROR_ACCESS_DENIED );
1025 return TRUE;
1028 winAttributes = dosAttributes &
1029 (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
1030 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE);
1034 * Open the file.
1036 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
1038 if ((winHandle = INT21_OpenMagicDevice( pathW, winAccess )))
1040 dosStatus = 1;
1042 else
1044 winHandle = CreateFileW( pathW, winAccess, winSharing, NULL,
1045 winMode, winAttributes, 0 );
1046 /* DOS allows opening files on a CDROM R/W */
1047 if( winHandle == INVALID_HANDLE_VALUE &&
1048 (GetLastError() == ERROR_WRITE_PROTECT ||
1049 GetLastError() == ERROR_ACCESS_DENIED)) {
1050 winHandle = CreateFileW( pathW, winAccess & ~GENERIC_WRITE,
1051 winSharing, NULL, winMode, winAttributes, 0 );
1054 if (winHandle == INVALID_HANDLE_VALUE)
1055 return FALSE;
1058 * Determine DOS file status.
1060 * 1 = file opened
1061 * 2 = file created
1062 * 3 = file replaced
1064 switch(winMode)
1066 case OPEN_EXISTING:
1067 dosStatus = 1;
1068 break;
1069 case TRUNCATE_EXISTING:
1070 dosStatus = 3;
1071 break;
1072 case CREATE_NEW:
1073 dosStatus = 2;
1074 break;
1075 case OPEN_ALWAYS:
1076 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 1 : 2;
1077 break;
1078 case CREATE_ALWAYS:
1079 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 3 : 2;
1080 break;
1081 default:
1082 dosStatus = 0;
1087 * Return DOS file handle and DOS status.
1089 SET_AX( context, Win32HandleToDosFileHandle(winHandle) );
1091 if (returnStatus)
1092 SET_CX( context, dosStatus );
1094 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
1095 AX_reg(context), dosStatus );
1097 return TRUE;
1101 /***********************************************************************
1102 * INT21_GetCurrentDTA
1104 static BYTE *INT21_GetCurrentDTA( CONTEXT *context )
1106 TDB *pTask = GlobalLock16(GetCurrentTask());
1108 /* FIXME: This assumes DTA was set correctly! */
1109 return CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
1110 OFFSETOF(pTask->dta) );
1114 /***********************************************************************
1115 * INT21_OpenFileUsingFCB
1117 * Handler for function 0x0f.
1119 * PARAMS
1120 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
1122 * RETURNS (in AL)
1123 * 0x00: successful
1124 * 0xff: failed
1126 * NOTES
1127 * Opens a FCB file for read/write in compatibility mode. Upon calling
1128 * the FCB must have the drive_number, file_name, and file_extension
1129 * fields filled and all other bytes cleared.
1131 static void INT21_OpenFileUsingFCB( CONTEXT *context )
1133 struct FCB *fcb;
1134 struct XFCB *xfcb;
1135 char file_path[16];
1136 char *pos;
1137 HANDLE handle;
1138 HFILE16 hfile16;
1139 BY_HANDLE_FILE_INFORMATION info;
1140 BYTE AL_result;
1142 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1143 if (fcb->drive_number == 0xff) {
1144 xfcb = (struct XFCB *) fcb;
1145 fcb = (struct FCB *) xfcb->fcb;
1146 } /* if */
1148 AL_result = 0;
1149 file_path[0] = 'A' + INT21_MapDrive( fcb->drive_number );
1151 if (AL_result == 0) {
1152 file_path[1] = ':';
1153 pos = &file_path[2];
1154 memcpy(pos, fcb->file_name, 8);
1155 pos[8] = ' ';
1156 pos[9] = '\0';
1157 pos = strchr(pos, ' ');
1158 *pos = '.';
1159 pos++;
1160 memcpy(pos, fcb->file_extension, 3);
1161 pos[3] = ' ';
1162 pos[4] = '\0';
1163 pos = strchr(pos, ' ');
1164 *pos = '\0';
1166 handle = (HANDLE) _lopen(file_path, OF_READWRITE);
1167 if (handle == INVALID_HANDLE_VALUE) {
1168 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path);
1169 AL_result = 0xff; /* failed */
1170 } else {
1171 hfile16 = Win32HandleToDosFileHandle(handle);
1172 if (hfile16 == HFILE_ERROR16) {
1173 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle);
1174 CloseHandle(handle);
1175 AL_result = 0xff; /* failed */
1176 } else if (hfile16 > 255) {
1177 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16, file_path);
1178 _lclose16(hfile16);
1179 AL_result = 0xff; /* failed */
1180 } else {
1181 if (!GetFileInformationByHandle(handle, &info)) {
1182 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1183 hfile16, handle, file_path);
1184 _lclose16(hfile16);
1185 AL_result = 0xff; /* failed */
1186 } else {
1187 fcb->drive_number = file_path[0] - 'A' + 1;
1188 fcb->current_block_number = 0;
1189 fcb->logical_record_size = 128;
1190 fcb->file_size = info.nFileSizeLow;
1191 FileTimeToDosDateTime(&info.ftLastWriteTime,
1192 &fcb->date_of_last_write, &fcb->time_of_last_write);
1193 fcb->file_number = hfile16;
1194 fcb->attributes = 0xc2;
1195 fcb->starting_cluster = 0; /* don't know correct init value */
1196 fcb->sequence_number = 0; /* don't know correct init value */
1197 fcb->file_attributes = info.dwFileAttributes;
1198 /* The following fields are not initialized */
1199 /* by the native function: */
1200 /* unused */
1201 /* record_within_current_block */
1202 /* random_access_record_number */
1204 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1205 file_path, hfile16, handle);
1206 AL_result = 0x00; /* successful */
1207 } /* if */
1208 } /* if */
1209 } /* if */
1210 } /* if */
1211 SET_AL(context, AL_result);
1215 /***********************************************************************
1216 * INT21_CloseFileUsingFCB
1218 * Handler for function 0x10.
1220 * PARAMS
1221 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1223 * RETURNS (in AL)
1224 * 0x00: successful
1225 * 0xff: failed
1227 * NOTES
1228 * Closes a FCB file.
1230 static void INT21_CloseFileUsingFCB( CONTEXT *context )
1232 struct FCB *fcb;
1233 struct XFCB *xfcb;
1234 BYTE AL_result;
1236 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1237 if (fcb->drive_number == 0xff) {
1238 xfcb = (struct XFCB *) fcb;
1239 fcb = (struct FCB *) xfcb->fcb;
1240 } /* if */
1242 if (_lclose16((HFILE16) fcb->file_number) != 0) {
1243 TRACE("_lclose16(%d) failed\n", fcb->file_number);
1244 AL_result = 0xff; /* failed */
1245 } else {
1246 TRACE("successful closed file %d\n", fcb->file_number);
1247 AL_result = 0x00; /* successful */
1248 } /* if */
1249 SET_AL(context, AL_result);
1253 /***********************************************************************
1254 * INT21_SequentialReadFromFCB
1256 * Handler for function 0x14.
1258 * PARAMS
1259 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1261 * RETURNS (in AL)
1262 * 0: successful
1263 * 1: end of file, no data read
1264 * 2: segment wrap in DTA, no data read (not returned now)
1265 * 3: end of file, partial record read
1267 * NOTES
1268 * Reads a record with the size FCB->logical_record_size from the FCB
1269 * to the disk transfer area. The position of the record is specified
1270 * with FCB->current_block_number and FCB->record_within_current_block.
1271 * Then FCB->current_block_number and FCB->record_within_current_block
1272 * are updated to point to the next record. If a partial record is
1273 * read, it is filled with zeros up to the FCB->logical_record_size.
1275 static void INT21_SequentialReadFromFCB( CONTEXT *context )
1277 struct FCB *fcb;
1278 struct XFCB *xfcb;
1279 HANDLE handle;
1280 DWORD record_number;
1281 DWORD position;
1282 BYTE *disk_transfer_area;
1283 UINT bytes_read;
1284 BYTE AL_result;
1286 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1287 if (fcb->drive_number == 0xff) {
1288 xfcb = (struct XFCB *) fcb;
1289 fcb = (struct FCB *) xfcb->fcb;
1290 } /* if */
1292 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1293 if (handle == INVALID_HANDLE_VALUE) {
1294 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1295 fcb->file_number);
1296 AL_result = 0x01; /* end of file, no data read */
1297 } else {
1298 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1299 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1300 if (position != record_number * fcb->logical_record_size) {
1301 TRACE("seek(%d, %d, 0) failed with %u\n",
1302 fcb->file_number, record_number * fcb->logical_record_size, position);
1303 AL_result = 0x01; /* end of file, no data read */
1304 } else {
1305 disk_transfer_area = INT21_GetCurrentDTA(context);
1306 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1307 if (bytes_read != fcb->logical_record_size) {
1308 TRACE("_lread(%d, %p, %d) failed with %d\n",
1309 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1310 if (bytes_read == 0) {
1311 AL_result = 0x01; /* end of file, no data read */
1312 } else {
1313 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1314 AL_result = 0x03; /* end of file, partial record read */
1315 } /* if */
1316 } else {
1317 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1318 bytes_read, record_number, position, fcb->file_number, handle);
1319 AL_result = 0x00; /* successful */
1320 } /* if */
1321 } /* if */
1322 } /* if */
1323 if (AL_result == 0x00 || AL_result == 0x03) {
1324 if (fcb->record_within_current_block == 127) {
1325 fcb->record_within_current_block = 0;
1326 fcb->current_block_number++;
1327 } else {
1328 fcb->record_within_current_block++;
1329 } /* if */
1330 } /* if */
1331 SET_AL(context, AL_result);
1335 /***********************************************************************
1336 * INT21_SequentialWriteToFCB
1338 * Handler for function 0x15.
1340 * PARAMS
1341 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1343 * RETURNS (in AL)
1344 * 0: successful
1345 * 1: disk full
1346 * 2: segment wrap in DTA (not returned now)
1348 * NOTES
1349 * Writes a record with the size FCB->logical_record_size from the disk
1350 * transfer area to the FCB. The position of the record is specified
1351 * with FCB->current_block_number and FCB->record_within_current_block.
1352 * Then FCB->current_block_number and FCB->record_within_current_block
1353 * are updated to point to the next record.
1355 static void INT21_SequentialWriteToFCB( CONTEXT *context )
1357 struct FCB *fcb;
1358 struct XFCB *xfcb;
1359 HANDLE handle;
1360 DWORD record_number;
1361 DWORD position;
1362 BYTE *disk_transfer_area;
1363 UINT bytes_written;
1364 BYTE AL_result;
1366 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1367 if (fcb->drive_number == 0xff) {
1368 xfcb = (struct XFCB *) fcb;
1369 fcb = (struct FCB *) xfcb->fcb;
1370 } /* if */
1372 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1373 if (handle == INVALID_HANDLE_VALUE) {
1374 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1375 fcb->file_number);
1376 AL_result = 0x01; /* disk full */
1377 } else {
1378 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1379 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1380 if (position != record_number * fcb->logical_record_size) {
1381 TRACE("seek(%d, %d, 0) failed with %u\n",
1382 fcb->file_number, record_number * fcb->logical_record_size, position);
1383 AL_result = 0x01; /* disk full */
1384 } else {
1385 disk_transfer_area = INT21_GetCurrentDTA(context);
1386 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, fcb->logical_record_size);
1387 if (bytes_written != fcb->logical_record_size) {
1388 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1389 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1390 AL_result = 0x01; /* disk full */
1391 } else {
1392 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1393 bytes_written, record_number, position, fcb->file_number, handle);
1394 AL_result = 0x00; /* successful */
1395 } /* if */
1396 } /* if */
1397 } /* if */
1398 if (AL_result == 0x00) {
1399 if (fcb->record_within_current_block == 127) {
1400 fcb->record_within_current_block = 0;
1401 fcb->current_block_number++;
1402 } else {
1403 fcb->record_within_current_block++;
1404 } /* if */
1405 } /* if */
1406 SET_AL(context, AL_result);
1410 /***********************************************************************
1411 * INT21_ReadRandomRecordFromFCB
1413 * Handler for function 0x21.
1415 * PARAMS
1416 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1418 * RETURNS (in AL)
1419 * 0: successful
1420 * 1: end of file, no data read
1421 * 2: segment wrap in DTA, no data read (not returned now)
1422 * 3: end of file, partial record read
1424 * NOTES
1425 * Reads a record with the size FCB->logical_record_size from
1426 * the FCB to the disk transfer area. The position of the record
1427 * is specified with FCB->random_access_record_number. The
1428 * FCB->random_access_record_number is not updated. If a partial record
1429 * is read, it is filled with zeros up to the FCB->logical_record_size.
1431 static void INT21_ReadRandomRecordFromFCB( CONTEXT *context )
1433 struct FCB *fcb;
1434 struct XFCB *xfcb;
1435 HANDLE handle;
1436 DWORD record_number;
1437 DWORD position;
1438 BYTE *disk_transfer_area;
1439 UINT bytes_read;
1440 BYTE AL_result;
1442 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1443 if (fcb->drive_number == 0xff) {
1444 xfcb = (struct XFCB *) fcb;
1445 fcb = (struct FCB *) xfcb->fcb;
1446 } /* if */
1448 memcpy(&record_number, fcb->random_access_record_number, 4);
1449 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1450 if (handle == INVALID_HANDLE_VALUE) {
1451 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1452 fcb->file_number);
1453 AL_result = 0x01; /* end of file, no data read */
1454 } else {
1455 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1456 if (position != record_number * fcb->logical_record_size) {
1457 TRACE("seek(%d, %d, 0) failed with %u\n",
1458 fcb->file_number, record_number * fcb->logical_record_size, position);
1459 AL_result = 0x01; /* end of file, no data read */
1460 } else {
1461 disk_transfer_area = INT21_GetCurrentDTA(context);
1462 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1463 if (bytes_read != fcb->logical_record_size) {
1464 TRACE("_lread(%d, %p, %d) failed with %d\n",
1465 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1466 if (bytes_read == 0) {
1467 AL_result = 0x01; /* end of file, no data read */
1468 } else {
1469 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1470 AL_result = 0x03; /* end of file, partial record read */
1471 } /* if */
1472 } else {
1473 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1474 bytes_read, record_number, position, fcb->file_number, handle);
1475 AL_result = 0x00; /* successful */
1476 } /* if */
1477 } /* if */
1478 } /* if */
1479 fcb->current_block_number = record_number / 128;
1480 fcb->record_within_current_block = record_number % 128;
1481 SET_AL(context, AL_result);
1485 /***********************************************************************
1486 * INT21_WriteRandomRecordToFCB
1488 * Handler for function 0x22.
1490 * PARAMS
1491 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1493 * RETURNS (in AL)
1494 * 0: successful
1495 * 1: disk full
1496 * 2: segment wrap in DTA (not returned now)
1498 * NOTES
1499 * Writes a record with the size FCB->logical_record_size from
1500 * the disk transfer area to the FCB. The position of the record
1501 * is specified with FCB->random_access_record_number. The
1502 * FCB->random_access_record_number is not updated.
1504 static void INT21_WriteRandomRecordToFCB( CONTEXT *context )
1506 struct FCB *fcb;
1507 struct XFCB *xfcb;
1508 HANDLE handle;
1509 DWORD record_number;
1510 DWORD position;
1511 BYTE *disk_transfer_area;
1512 UINT bytes_written;
1513 BYTE AL_result;
1515 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1516 if (fcb->drive_number == 0xff) {
1517 xfcb = (struct XFCB *) fcb;
1518 fcb = (struct FCB *) xfcb->fcb;
1519 } /* if */
1521 memcpy(&record_number, fcb->random_access_record_number, 4);
1522 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1523 if (handle == INVALID_HANDLE_VALUE) {
1524 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1525 fcb->file_number);
1526 AL_result = 0x01; /* disk full */
1527 } else {
1528 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1529 if (position != record_number * fcb->logical_record_size) {
1530 TRACE("seek(%d, %d, 0) failed with %u\n",
1531 fcb->file_number, record_number * fcb->logical_record_size, position);
1532 AL_result = 0x01; /* disk full */
1533 } else {
1534 disk_transfer_area = INT21_GetCurrentDTA(context);
1535 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, fcb->logical_record_size);
1536 if (bytes_written != fcb->logical_record_size) {
1537 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1538 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1539 AL_result = 0x01; /* disk full */
1540 } else {
1541 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1542 bytes_written, record_number, position, fcb->file_number, handle);
1543 AL_result = 0x00; /* successful */
1544 } /* if */
1545 } /* if */
1546 } /* if */
1547 fcb->current_block_number = record_number / 128;
1548 fcb->record_within_current_block = record_number % 128;
1549 SET_AL(context, AL_result);
1553 /***********************************************************************
1554 * INT21_RandomBlockReadFromFCB
1556 * Handler for function 0x27.
1558 * PARAMS
1559 * CX [I/O] Number of records to read
1560 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1562 * RETURNS (in AL)
1563 * 0: successful
1564 * 1: end of file, no data read
1565 * 2: segment wrap in DTA, no data read (not returned now)
1566 * 3: end of file, partial record read
1568 * NOTES
1569 * Reads several records with the size FCB->logical_record_size from
1570 * the FCB to the disk transfer area. The number of records to be
1571 * read is specified in the CX register. The position of the first
1572 * record is specified with FCB->random_access_record_number. The
1573 * FCB->random_access_record_number, the FCB->current_block_number
1574 * and FCB->record_within_current_block are updated to point to the
1575 * next record after the records read. If a partial record is read,
1576 * it is filled with zeros up to the FCB->logical_record_size. The
1577 * CX register is set to the number of successfully read records.
1579 static void INT21_RandomBlockReadFromFCB( CONTEXT *context )
1581 struct FCB *fcb;
1582 struct XFCB *xfcb;
1583 HANDLE handle;
1584 DWORD record_number;
1585 DWORD position;
1586 BYTE *disk_transfer_area;
1587 UINT records_requested;
1588 UINT bytes_requested;
1589 UINT bytes_read;
1590 UINT records_read;
1591 BYTE AL_result;
1593 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1594 if (fcb->drive_number == 0xff) {
1595 xfcb = (struct XFCB *) fcb;
1596 fcb = (struct FCB *) xfcb->fcb;
1597 } /* if */
1599 memcpy(&record_number, fcb->random_access_record_number, 4);
1600 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1601 if (handle == INVALID_HANDLE_VALUE) {
1602 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1603 fcb->file_number);
1604 records_read = 0;
1605 AL_result = 0x01; /* end of file, no data read */
1606 } else {
1607 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1608 if (position != record_number * fcb->logical_record_size) {
1609 TRACE("seek(%d, %d, 0) failed with %u\n",
1610 fcb->file_number, record_number * fcb->logical_record_size, position);
1611 records_read = 0;
1612 AL_result = 0x01; /* end of file, no data read */
1613 } else {
1614 disk_transfer_area = INT21_GetCurrentDTA(context);
1615 records_requested = CX_reg(context);
1616 bytes_requested = records_requested * fcb->logical_record_size;
1617 bytes_read = _lread((HFILE) handle, disk_transfer_area, bytes_requested);
1618 if (bytes_read != bytes_requested) {
1619 TRACE("_lread(%d, %p, %d) failed with %d\n",
1620 fcb->file_number, disk_transfer_area, bytes_requested, bytes_read);
1621 records_read = bytes_read / fcb->logical_record_size;
1622 if (bytes_read % fcb->logical_record_size == 0) {
1623 AL_result = 0x01; /* end of file, no data read */
1624 } else {
1625 records_read++;
1626 memset(&disk_transfer_area[bytes_read], 0, records_read * fcb->logical_record_size - bytes_read);
1627 AL_result = 0x03; /* end of file, partial record read */
1628 } /* if */
1629 } else {
1630 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1631 bytes_read, record_number, position, fcb->file_number, handle);
1632 records_read = records_requested;
1633 AL_result = 0x00; /* successful */
1634 } /* if */
1635 } /* if */
1636 } /* if */
1637 record_number += records_read;
1638 memcpy(fcb->random_access_record_number, &record_number, 4);
1639 fcb->current_block_number = record_number / 128;
1640 fcb->record_within_current_block = record_number % 128;
1641 SET_CX(context, records_read);
1642 SET_AL(context, AL_result);
1646 /***********************************************************************
1647 * INT21_RandomBlockWriteToFCB
1649 * Handler for function 0x28.
1651 * PARAMS
1652 * CX [I/O] Number of records to write
1653 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1655 * RETURNS (in AL)
1656 * 0: successful
1657 * 1: disk full
1658 * 2: segment wrap in DTA (not returned now)
1660 * NOTES
1661 * Writes several records with the size FCB->logical_record_size from
1662 * the disk transfer area to the FCB. The number of records to be
1663 * written is specified in the CX register. The position of the first
1664 * record is specified with FCB->random_access_record_number. The
1665 * FCB->random_access_record_number, the FCB->current_block_number
1666 * and FCB->record_within_current_block are updated to point to the
1667 * next record after the records written. The CX register is set to
1668 * the number of successfully written records.
1670 static void INT21_RandomBlockWriteToFCB( CONTEXT *context )
1672 struct FCB *fcb;
1673 struct XFCB *xfcb;
1674 HANDLE handle;
1675 DWORD record_number;
1676 DWORD position;
1677 BYTE *disk_transfer_area;
1678 UINT records_requested;
1679 UINT bytes_requested;
1680 UINT bytes_written;
1681 UINT records_written;
1682 BYTE AL_result;
1684 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1685 if (fcb->drive_number == 0xff) {
1686 xfcb = (struct XFCB *) fcb;
1687 fcb = (struct FCB *) xfcb->fcb;
1688 } /* if */
1690 memcpy(&record_number, fcb->random_access_record_number, 4);
1691 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1692 if (handle == INVALID_HANDLE_VALUE) {
1693 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1694 fcb->file_number);
1695 records_written = 0;
1696 AL_result = 0x01; /* disk full */
1697 } else {
1698 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1699 if (position != record_number * fcb->logical_record_size) {
1700 TRACE("seek(%d, %d, 0) failed with %u\n",
1701 fcb->file_number, record_number * fcb->logical_record_size, position);
1702 records_written = 0;
1703 AL_result = 0x01; /* disk full */
1704 } else {
1705 disk_transfer_area = INT21_GetCurrentDTA(context);
1706 records_requested = CX_reg(context);
1707 bytes_requested = records_requested * fcb->logical_record_size;
1708 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, bytes_requested);
1709 if (bytes_written != bytes_requested) {
1710 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1711 fcb->file_number, disk_transfer_area, bytes_requested, bytes_written);
1712 records_written = bytes_written / fcb->logical_record_size;
1713 AL_result = 0x01; /* disk full */
1714 } else {
1715 TRACE("successful write %d bytes from record %d (position %u) of file %d (handle %p)\n",
1716 bytes_written, record_number, position, fcb->file_number, handle);
1717 records_written = records_requested;
1718 AL_result = 0x00; /* successful */
1719 } /* if */
1720 } /* if */
1721 } /* if */
1722 record_number += records_written;
1723 memcpy(fcb->random_access_record_number, &record_number, 4);
1724 fcb->current_block_number = record_number / 128;
1725 fcb->record_within_current_block = record_number % 128;
1726 SET_CX(context, records_written);
1727 SET_AL(context, AL_result);
1731 /***********************************************************************
1732 * INT21_CreateDirectory
1734 * Handler for:
1735 * - function 0x39
1736 * - subfunction 0x39 of function 0x71
1737 * - subfunction 0xff of function 0x43 (CL == 0x39)
1739 static BOOL INT21_CreateDirectory( CONTEXT *context )
1741 WCHAR dirW[MAX_PATH];
1742 char *dirA = CTX_SEG_OFF_TO_LIN(context,
1743 context->SegDs,
1744 context->Edx);
1746 TRACE( "CREATE DIRECTORY %s\n", dirA );
1748 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
1750 if (CreateDirectoryW(dirW, NULL))
1751 return TRUE;
1754 * FIXME: CreateDirectory's LastErrors will clash with the ones
1755 * used by DOS. AH=39 only returns 3 (path not found) and
1756 * 5 (access denied), while CreateDirectory return several
1757 * ones. Remap some of them. -Marcus
1759 switch (GetLastError())
1761 case ERROR_ALREADY_EXISTS:
1762 case ERROR_FILENAME_EXCED_RANGE:
1763 case ERROR_DISK_FULL:
1764 SetLastError(ERROR_ACCESS_DENIED);
1765 break;
1766 default:
1767 break;
1770 return FALSE;
1774 /***********************************************************************
1775 * INT21_ExtendedCountryInformation
1777 * Handler for function 0x65.
1779 static void INT21_ExtendedCountryInformation( CONTEXT *context )
1781 BYTE *dataptr = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
1782 BYTE buffsize = CX_reg (context);
1784 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1785 AL_reg(context) );
1788 * Check subfunctions that are passed country and code page.
1790 if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
1792 WORD country = DX_reg(context);
1793 WORD codepage = BX_reg(context);
1795 if (country != 0xffff && country != INT21_GetSystemCountryCode())
1796 FIXME( "Requested info on non-default country %04x\n", country );
1798 if (codepage != 0xffff && codepage != GetOEMCP())
1799 FIXME( "Requested info on non-default code page %04x\n", codepage );
1802 switch (AL_reg(context)) {
1803 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1804 INT_BARF( context, 0x21 );
1805 SET_CFLAG( context );
1806 break;
1808 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1809 TRACE( "Get general internationalization info\n" );
1810 dataptr[0] = 0x01; /* Info ID */
1811 *(WORD*)(dataptr+1) = 38; /* Size of the following info */
1812 *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
1813 *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
1814 /* FIXME: fill buffer partially up to buffsize bytes*/
1815 if (buffsize >= 0x29){
1816 INT21_FillCountryInformation( dataptr + 7 );
1817 SET_CX( context, 0x29 ); /* Size of returned info */
1818 }else{
1819 SET_CX( context, 0x07 ); /* Size of returned info */
1821 break;
1823 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1824 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1825 TRACE( "Get pointer to uppercase table\n" );
1826 dataptr[0] = AL_reg(context); /* Info ID */
1827 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1828 offsetof(INT21_HEAP, uppercase_size) );
1829 SET_CX( context, 5 ); /* Size of returned info */
1830 break;
1832 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1833 TRACE( "Get pointer to lowercase table\n" );
1834 dataptr[0] = 0x03; /* Info ID */
1835 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1836 offsetof(INT21_HEAP, lowercase_size) );
1837 SET_CX( context, 5 ); /* Size of returned info */
1838 break;
1840 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1841 TRACE("Get pointer to filename terminator table\n");
1842 dataptr[0] = 0x05; /* Info ID */
1843 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1844 offsetof(INT21_HEAP, filename_size) );
1845 SET_CX( context, 5 ); /* Size of returned info */
1846 break;
1848 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1849 TRACE("Get pointer to collating sequence table\n");
1850 dataptr[0] = 0x06; /* Info ID */
1851 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1852 offsetof(INT21_HEAP, collating_size) );
1853 SET_CX( context, 5 ); /* Size of returned info */
1854 break;
1856 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1857 TRACE("Get pointer to DBCS lead byte table\n");
1858 dataptr[0] = 0x07; /* Info ID */
1859 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1860 offsetof(INT21_HEAP, dbcs_size) );
1861 SET_CX( context, 5 ); /* Size of returned info */
1862 break;
1864 case 0x20: /* CAPITALIZE CHARACTER */
1865 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1866 TRACE("Convert char to uppercase\n");
1867 SET_DL( context, toupper(DL_reg(context)) );
1868 break;
1870 case 0x21: /* CAPITALIZE STRING */
1871 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1872 TRACE("Convert string to uppercase with length\n");
1874 char *ptr = CTX_SEG_OFF_TO_LIN( context, context->SegDs,
1875 context->Edx );
1876 WORD len = CX_reg(context);
1877 while (len--) { *ptr = toupper(*ptr); ptr++; }
1879 break;
1881 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1882 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1883 TRACE("Convert ASCIIZ string to uppercase\n");
1885 char *p = CTX_SEG_OFF_TO_LIN( context, context->SegDs, context->Edx );
1886 for ( ; *p; p++) *p = toupper(*p);
1888 break;
1890 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1891 INT_BARF( context, 0x21 );
1892 SET_CFLAG( context );
1893 break;
1895 default:
1896 INT_BARF( context, 0x21 );
1897 SET_CFLAG(context);
1898 break;
1903 /***********************************************************************
1904 * INT21_FileAttributes
1906 * Handler for:
1907 * - function 0x43
1908 * - subfunction 0x43 of function 0x71
1910 static BOOL INT21_FileAttributes( CONTEXT *context,
1911 BYTE subfunction,
1912 BOOL islong )
1914 WCHAR fileW[MAX_PATH];
1915 char *fileA = CTX_SEG_OFF_TO_LIN(context,
1916 context->SegDs,
1917 context->Edx);
1918 HANDLE handle;
1919 BOOL status;
1920 FILETIME filetime;
1921 DWORD result;
1922 WORD date, time;
1923 int len;
1925 switch (subfunction)
1927 case 0x00: /* GET FILE ATTRIBUTES */
1928 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA );
1929 len = MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1931 /* Winbench 96 Disk Test fails if we don't complain
1932 * about a filename that ends in \
1934 if (!len || (fileW[len-1] == '/') || (fileW[len-1] == '\\'))
1935 return FALSE;
1937 result = GetFileAttributesW( fileW );
1938 if (result == INVALID_FILE_ATTRIBUTES)
1939 return FALSE;
1940 else
1942 SET_CX( context, (WORD)result );
1943 if (!islong)
1944 SET_AX( context, (WORD)result ); /* DR DOS */
1946 break;
1948 case 0x01: /* SET FILE ATTRIBUTES */
1949 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
1950 CX_reg(context), fileA );
1951 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1953 if (!SetFileAttributesW( fileW, CX_reg(context) ))
1954 return FALSE;
1955 break;
1957 case 0x02: /* GET COMPRESSED FILE SIZE */
1958 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA );
1959 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1961 result = GetCompressedFileSizeW( fileW, NULL );
1962 if (result == INVALID_FILE_SIZE)
1963 return FALSE;
1964 else
1966 SET_AX( context, LOWORD(result) );
1967 SET_DX( context, HIWORD(result) );
1969 break;
1971 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
1972 if (!islong)
1973 INT_BARF( context, 0x21 );
1974 else
1976 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
1977 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1979 handle = CreateFileW( fileW, GENERIC_WRITE,
1980 FILE_SHARE_READ | FILE_SHARE_WRITE,
1981 NULL, OPEN_EXISTING, 0, 0 );
1982 if (handle == INVALID_HANDLE_VALUE)
1983 return FALSE;
1985 DosDateTimeToFileTime( DI_reg(context),
1986 CX_reg(context),
1987 &filetime );
1988 status = SetFileTime( handle, NULL, NULL, &filetime );
1990 CloseHandle( handle );
1991 return status;
1993 break;
1995 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
1996 if (!islong)
1997 INT_BARF( context, 0x21 );
1998 else
2000 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
2001 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2003 handle = CreateFileW( fileW, GENERIC_READ,
2004 FILE_SHARE_READ | FILE_SHARE_WRITE,
2005 NULL, OPEN_EXISTING, 0, 0 );
2006 if (handle == INVALID_HANDLE_VALUE)
2007 return FALSE;
2009 status = GetFileTime( handle, NULL, NULL, &filetime );
2010 if (status)
2012 FileTimeToDosDateTime( &filetime, &date, &time );
2013 SET_DI( context, date );
2014 SET_CX( context, time );
2017 CloseHandle( handle );
2018 return status;
2020 break;
2022 case 0x05: /* SET FILE LAST ACCESS DATE */
2023 if (!islong)
2024 INT_BARF( context, 0x21 );
2025 else
2027 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA );
2028 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2030 handle = CreateFileW( fileW, GENERIC_WRITE,
2031 FILE_SHARE_READ | FILE_SHARE_WRITE,
2032 NULL, OPEN_EXISTING, 0, 0 );
2033 if (handle == INVALID_HANDLE_VALUE)
2034 return FALSE;
2036 DosDateTimeToFileTime( DI_reg(context),
2038 &filetime );
2039 status = SetFileTime( handle, NULL, &filetime, NULL );
2041 CloseHandle( handle );
2042 return status;
2044 break;
2046 case 0x06: /* GET FILE LAST ACCESS DATE */
2047 if (!islong)
2048 INT_BARF( context, 0x21 );
2049 else
2051 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA );
2052 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2054 handle = CreateFileW( fileW, GENERIC_READ,
2055 FILE_SHARE_READ | FILE_SHARE_WRITE,
2056 NULL, OPEN_EXISTING, 0, 0 );
2057 if (handle == INVALID_HANDLE_VALUE)
2058 return FALSE;
2060 status = GetFileTime( handle, NULL, &filetime, NULL );
2061 if (status)
2063 FileTimeToDosDateTime( &filetime, &date, NULL );
2064 SET_DI( context, date );
2067 CloseHandle( handle );
2068 return status;
2070 break;
2072 case 0x07: /* SET FILE CREATION DATE AND TIME */
2073 if (!islong)
2074 INT_BARF( context, 0x21 );
2075 else
2077 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA );
2078 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2080 handle = CreateFileW( fileW, GENERIC_WRITE,
2081 FILE_SHARE_READ | FILE_SHARE_WRITE,
2082 NULL, OPEN_EXISTING, 0, 0 );
2083 if (handle == INVALID_HANDLE_VALUE)
2084 return FALSE;
2087 * FIXME: SI has number of 10-millisecond units past time in CX.
2089 DosDateTimeToFileTime( DI_reg(context),
2090 CX_reg(context),
2091 &filetime );
2092 status = SetFileTime( handle, &filetime, NULL, NULL );
2094 CloseHandle( handle );
2095 return status;
2097 break;
2099 case 0x08: /* GET FILE CREATION DATE AND TIME */
2100 if (!islong)
2101 INT_BARF( context, 0x21 );
2102 else
2104 TRACE( "GET FILE CREATION DATE AND TIME, file %s\n", fileA );
2105 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2107 handle = CreateFileW( fileW, GENERIC_READ,
2108 FILE_SHARE_READ | FILE_SHARE_WRITE,
2109 NULL, OPEN_EXISTING, 0, 0 );
2110 if (handle == INVALID_HANDLE_VALUE)
2111 return FALSE;
2113 status = GetFileTime( handle, &filetime, NULL, NULL );
2114 if (status)
2116 FileTimeToDosDateTime( &filetime, &date, &time );
2117 SET_DI( context, date );
2118 SET_CX( context, time );
2120 * FIXME: SI has number of 10-millisecond units past
2121 * time in CX.
2123 SET_SI( context, 0 );
2126 CloseHandle(handle);
2127 return status;
2129 break;
2131 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2132 if (islong || context->Ebp != 0x5053)
2133 INT_BARF( context, 0x21 );
2134 else
2136 switch(CL_reg(context))
2138 case 0x39:
2139 if (!INT21_CreateDirectory( context ))
2140 return FALSE;
2141 break;
2143 case 0x56:
2144 if (!INT21_RenameFile( context ))
2145 return FALSE;
2146 break;
2148 default:
2149 INT_BARF( context, 0x21 );
2152 break;
2154 default:
2155 INT_BARF( context, 0x21 );
2158 return TRUE;
2162 /***********************************************************************
2163 * INT21_FileDateTime
2165 * Handler for function 0x57.
2167 static BOOL INT21_FileDateTime( CONTEXT *context )
2169 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2170 FILETIME filetime;
2171 WORD date, time;
2173 switch (AL_reg(context)) {
2174 case 0x00: /* Get last-written stamp */
2175 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2176 BX_reg(context) );
2178 if (!GetFileTime( handle, NULL, NULL, &filetime ))
2179 return FALSE;
2180 FileTimeToDosDateTime( &filetime, &date, &time );
2181 SET_DX( context, date );
2182 SET_CX( context, time );
2183 break;
2186 case 0x01: /* Set last-written stamp */
2187 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2188 BX_reg(context) );
2190 DosDateTimeToFileTime( DX_reg(context),
2191 CX_reg(context),
2192 &filetime );
2193 if (!SetFileTime( handle, NULL, NULL, &filetime ))
2194 return FALSE;
2195 break;
2198 case 0x04: /* Get last access stamp, DOS 7 */
2199 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2200 BX_reg(context) );
2202 if (!GetFileTime( handle, NULL, &filetime, NULL ))
2203 return FALSE;
2204 FileTimeToDosDateTime( &filetime, &date, &time );
2205 SET_DX( context, date );
2206 SET_CX( context, time );
2207 break;
2210 case 0x05: /* Set last access stamp, DOS 7 */
2211 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2212 BX_reg(context) );
2214 DosDateTimeToFileTime( DX_reg(context),
2215 CX_reg(context),
2216 &filetime );
2217 if (!SetFileTime( handle, NULL, &filetime, NULL ))
2218 return FALSE;
2219 break;
2222 case 0x06: /* Get creation stamp, DOS 7 */
2223 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2224 BX_reg(context) );
2226 if (!GetFileTime( handle, &filetime, NULL, NULL ))
2227 return FALSE;
2228 FileTimeToDosDateTime( &filetime, &date, &time );
2229 SET_DX( context, date );
2230 SET_CX( context, time );
2232 * FIXME: SI has number of 10-millisecond units past time in CX.
2234 SET_SI( context, 0 );
2235 break;
2238 case 0x07: /* Set creation stamp, DOS 7 */
2239 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2240 BX_reg(context) );
2243 * FIXME: SI has number of 10-millisecond units past time in CX.
2245 DosDateTimeToFileTime( DX_reg(context),
2246 CX_reg(context),
2247 &filetime );
2248 if (!SetFileTime( handle, &filetime, NULL, NULL ))
2249 return FALSE;
2250 break;
2253 default:
2254 INT_BARF( context, 0x21 );
2255 break;
2258 return TRUE;
2262 /***********************************************************************
2263 * INT21_GetPSP
2265 * Handler for functions 0x51 and 0x62.
2267 static void INT21_GetPSP( CONTEXT *context )
2269 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
2271 SET_BX( context, LOWORD(GetCurrentPDB16()) );
2274 static inline void setword( BYTE *ptr, WORD w )
2276 ptr[0] = (BYTE)w;
2277 ptr[1] = (BYTE)(w >> 8);
2280 static void CreateBPB(int drive, BYTE *data, BOOL16 limited)
2281 /* limited == TRUE is used with INT 0x21/0x440d */
2283 /* FIXME: we're forcing some values without checking that those are valid */
2284 if (drive > 1)
2286 setword(data, 512);
2287 data[2] = 2;
2288 setword(&data[3], 0);
2289 data[5] = 2;
2290 setword(&data[6], 240);
2291 setword(&data[8], 64000);
2292 data[0x0a] = 0xf8;
2293 setword(&data[0x0b], 40);
2294 setword(&data[0x0d], 56);
2295 setword(&data[0x0f], 2);
2296 setword(&data[0x11], 0);
2297 if (!limited)
2299 setword(&data[0x1f], 800);
2300 data[0x21] = 5;
2301 setword(&data[0x22], 1);
2304 else
2305 { /* 1.44mb */
2306 setword(data, 512);
2307 data[2] = 2;
2308 setword(&data[3], 0);
2309 data[5] = 2;
2310 setword(&data[6], 240);
2311 setword(&data[8], 2880);
2312 data[0x0a] = 0xf8;
2313 setword(&data[0x0b], 6);
2314 setword(&data[0x0d], 18);
2315 setword(&data[0x0f], 2);
2316 setword(&data[0x11], 0);
2317 if (!limited)
2319 setword(&data[0x1f], 80);
2320 data[0x21] = 7;
2321 setword(&data[0x22], 2);
2326 static inline DWORD INT21_Ioctl_CylHeadSect2Lin(DWORD cyl, WORD head, WORD sec, WORD cyl_cnt,
2327 WORD head_cnt, WORD sec_cnt)
2329 DWORD res = (cyl * head_cnt*sec_cnt + head * sec_cnt + sec);
2330 return res;
2333 /***********************************************************************
2334 * INT21_Ioctl_Block
2336 * Handler for block device IOCTLs.
2338 static void INT21_Ioctl_Block( CONTEXT *context )
2340 BYTE *dataptr;
2341 BYTE drive = INT21_MapDrive( BL_reg(context) );
2342 WCHAR drivespec[] = {'A', ':', '\\', 0};
2343 UINT drivetype;
2345 drivespec[0] += drive;
2346 drivetype = GetDriveTypeW( drivespec );
2348 RESET_CFLAG(context);
2349 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
2351 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2352 AL_reg(context), 'A' + drive );
2353 SetLastError( ERROR_INVALID_DRIVE );
2354 SET_AX( context, ERROR_INVALID_DRIVE );
2355 SET_CFLAG( context );
2356 return;
2359 switch (AL_reg(context))
2361 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2362 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2363 INT_BARF( context, 0x21 );
2364 break;
2366 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2367 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2368 'A' + drive );
2370 if (drivetype == DRIVE_REMOVABLE)
2371 SET_AX( context, 0 ); /* removable */
2372 else
2373 SET_AX( context, 1 ); /* not removable */
2374 break;
2376 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2377 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2378 'A' + drive );
2380 if (drivetype == DRIVE_REMOTE)
2381 SET_DX( context, (1<<9) | (1<<12) ); /* remote + no direct IO */
2382 else if (drivetype == DRIVE_CDROM)
2383 /* CDROM should be set to remote. If it set the app will
2384 * call int2f to check if it cdrom or remote drive. */
2385 SET_DX( context, (1<<12) );
2386 else if (drivetype == DRIVE_FIXED)
2387 /* This should define if drive support 0x0d, 0x0f and 0x08
2388 * requests. The local fixed drive should do. */
2389 SET_DX( context, (1<<11) );
2390 else
2391 SET_DX( context, 0 ); /* FIXME: use driver attr here */
2392 break;
2394 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2395 /* Get pointer to IOCTL parameter block */
2396 dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2398 switch (CX_reg(context))
2400 case 0x0841: /* write logical device track */
2401 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2402 'A' + drive);
2404 WORD head = *(WORD *)(dataptr+1);
2405 WORD cyl = *(WORD *)(dataptr+3);
2406 WORD sect = *(WORD *)(dataptr+5);
2407 WORD nrsect = *(WORD *)(dataptr+7);
2408 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2409 WORD cyl_cnt, head_cnt, sec_cnt;
2411 /* FIXME: we're faking some values here */
2412 if (drive > 1)
2414 /* cyl_cnt = 0x300;
2415 head_cnt = 16;
2416 sec_cnt = 255; */
2417 SET_AX( context, ERROR_WRITE_FAULT );
2418 SET_CFLAG(context);
2419 break;
2421 else
2422 { /* floppy */
2423 cyl_cnt = 80;
2424 head_cnt = 2;
2425 sec_cnt = 18;
2428 if (!DOSVM_RawWrite(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2430 SET_AX( context, ERROR_WRITE_FAULT );
2431 SET_CFLAG(context);
2434 break;
2436 case 0x084a: /* lock logical volume */
2437 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2438 BH_reg(context), DX_reg(context), 'A' + drive );
2439 break;
2441 case 0x0860: /* get device parameters */
2442 /* FIXME: we're faking some values here */
2443 /* used by w4wgrp's winfile */
2444 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2445 dataptr[0] = 0x04;
2446 dataptr[6] = 0; /* media type */
2447 if (drive > 1)
2449 dataptr[1] = 0x05; /* fixed disk */
2450 setword(&dataptr[2], 0x01); /* non removable */
2451 setword(&dataptr[4], 0x300); /* # of cylinders */
2453 else
2455 dataptr[1] = 0x07; /* block dev, floppy */
2456 setword(&dataptr[2], 0x02); /* removable */
2457 setword(&dataptr[4], 80); /* # of cylinders */
2459 CreateBPB(drive, &dataptr[7], TRUE);
2460 RESET_CFLAG(context);
2461 break;
2463 case 0x0861: /* read logical device track */
2464 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2465 'A' + drive);
2467 WORD head = *(WORD *)(dataptr+1);
2468 WORD cyl = *(WORD *)(dataptr+3);
2469 WORD sect = *(WORD *)(dataptr+5);
2470 WORD nrsect = *(WORD *)(dataptr+7);
2471 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2472 WORD cyl_cnt, head_cnt, sec_cnt;
2474 /* FIXME: we're faking some values here */
2475 if (drive > 1)
2477 cyl_cnt = 0x300;
2478 head_cnt = 16;
2479 sec_cnt = 255;
2481 else
2482 { /* floppy */
2483 cyl_cnt = 80;
2484 head_cnt = 2;
2485 sec_cnt = 18;
2488 if (!DOSVM_RawRead(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2490 SET_AX( context, ERROR_READ_FAULT );
2491 SET_CFLAG(context);
2494 break;
2496 case 0x0866: /* get volume serial number */
2498 WCHAR label[12],fsname[9];
2499 DWORD serial;
2500 TRACE( "GENERIC IOCTL - Get media id - %c:\n",
2501 'A' + drive );
2503 GetVolumeInformationW(drivespec, label, 12, &serial, NULL, NULL, fsname, 9);
2504 *(WORD*)dataptr = 0;
2505 memcpy(dataptr+2,&serial,4);
2506 WideCharToMultiByte(CP_OEMCP, 0, label, 11, (LPSTR)dataptr + 6, 11, NULL, NULL);
2507 WideCharToMultiByte(CP_OEMCP, 0, fsname, 8, (LPSTR)dataptr + 17, 8, NULL, NULL);
2509 break;
2511 case 0x086a: /* unlock logical volume */
2512 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2513 'A' + drive );
2514 break;
2516 case 0x086f: /* get drive map information */
2517 memset(dataptr+1, '\0', dataptr[0]-1);
2518 dataptr[1] = dataptr[0];
2519 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
2520 dataptr[3] = 0xFF; /* no physical drive */
2521 break;
2523 case 0x0872:
2524 /* Trial and error implementation */
2525 SET_AX( context, drivetype == DRIVE_UNKNOWN ? 0x0f : 0x01 );
2526 SET_CFLAG(context); /* Seems to be set all the time */
2527 break;
2529 default:
2530 INT_BARF( context, 0x21 );
2532 break;
2534 case 0x0e: /* GET LOGICAL DRIVE MAP */
2535 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2536 'A' + drive );
2537 /* FIXME: this is not correct if drive has mappings */
2538 SET_AL( context, 0 ); /* drive has no mapping */
2539 break;
2541 case 0x0f: /* SET LOGICAL DRIVE MAP */
2542 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2543 INT21_DriveName( BL_reg(context)));
2544 /* FIXME: as of today, we don't support logical drive mapping... */
2545 SET_AL( context, 0 );
2546 break;
2548 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2549 default:
2550 INT_BARF( context, 0x21 );
2555 /***********************************************************************
2556 * INT21_IoctlScsiMgrHandler
2558 * IOCTL handler for the SCSIMGR device.
2560 static void INT21_IoctlScsiMgrHandler( CONTEXT *context )
2562 switch (AL_reg(context))
2564 case 0x00: /* GET DEVICE INFORMATION */
2565 SET_DX( context, 0xc0c0 );
2566 break;
2568 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2569 SET_DX( context, 0 );
2570 break;
2572 case 0x01: /* SET DEVICE INFORMATION */
2573 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2574 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2575 case 0x06: /* GET INPUT STATUS */
2576 case 0x07: /* GET OUTPUT STATUS */
2577 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2578 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2579 default:
2580 INT_BARF( context, 0x21 );
2581 break;
2586 /***********************************************************************
2587 * INT21_IoctlHPScanHandler
2589 * IOCTL handler for the HPSCAN device.
2591 static void INT21_IoctlHPScanHandler( CONTEXT *context )
2593 switch (AL_reg(context))
2595 case 0x00: /* GET DEVICE INFORMATION */
2596 SET_DX( context, 0xc0c0 );
2597 break;
2599 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2600 SET_DX( context, 0 );
2601 break;
2603 case 0x01: /* SET DEVICE INFORMATION */
2604 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2605 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2606 case 0x06: /* GET INPUT STATUS */
2607 case 0x07: /* GET OUTPUT STATUS */
2608 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2609 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2610 default:
2611 INT_BARF( context, 0x21 );
2612 break;
2617 /***********************************************************************
2618 * INT21_Ioctl_Char
2620 * Handler for character device IOCTLs.
2622 static void INT21_Ioctl_Char( CONTEXT *context )
2624 int status;
2625 BOOL IsConsoleIOHandle = FALSE;
2626 IO_STATUS_BLOCK io;
2627 FILE_INTERNAL_INFORMATION info;
2628 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2630 status = NtQueryInformationFile( handle, &io, &info, sizeof(info), FileInternalInformation );
2631 if (status)
2633 if( VerifyConsoleIoHandle( handle))
2634 IsConsoleIOHandle = TRUE;
2635 else {
2636 SET_AX( context, RtlNtStatusToDosError(status) );
2637 SET_CFLAG( context );
2638 return;
2640 } else {
2641 UINT i;
2642 for (i = 0; i < NB_MAGIC_DEVICES; i++)
2644 if (!magic_devices[i].handle) continue;
2645 if (magic_devices[i].index.QuadPart == info.IndexNumber.QuadPart)
2647 /* found it */
2648 magic_devices[i].ioctl_handler( context );
2649 return;
2654 /* no magic device found, do default handling */
2656 switch (AL_reg(context))
2658 case 0x00: /* GET DEVICE INFORMATION */
2659 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context) );
2660 if (IsConsoleIOHandle || GetFileType(handle) == FILE_TYPE_CHAR)
2663 * Returns attribute word in DX:
2664 * Bit 14 - Device driver can process IOCTL requests.
2665 * Bit 13 - Output until busy supported.
2666 * Bit 11 - Driver supports OPEN/CLOSE calls.
2667 * Bit 8 - Unknown.
2668 * Bit 7 - Set (indicates device).
2669 * Bit 6 - EOF on input.
2670 * Bit 5 - Raw (binary) mode.
2671 * Bit 4 - Device is special (uses int29).
2672 * Bit 3 - Clock device.
2673 * Bit 2 - NUL device.
2674 * Bit 1 - Console output device.
2675 * Bit 0 - Console input device.
2677 SET_DX( context, IsConsoleIOHandle ? 0x80c3 : 0x80c0 /* FIXME */ );
2679 else
2682 * Returns attribute word in DX:
2683 * Bit 15 - File is remote.
2684 * Bit 14 - Don't set file date/time on closing.
2685 * Bit 11 - Media not removable.
2686 * Bit 8 - Generate int24 if no disk space on write
2687 * or read past end of file
2688 * Bit 7 - Clear (indicates file).
2689 * Bit 6 - File has not been written.
2690 * Bit 5..0 - Drive number (0=A:,...)
2692 * FIXME: Should check if file is on remote or removable drive.
2693 * FIXME: Should use drive file is located on (and not current).
2695 SET_DX( context, 0x0140 + INT21_GetCurrentDrive() );
2697 break;
2699 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2700 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context) );
2702 * Returns attribute word in DX:
2703 * Bit 15 - Set if remote.
2704 * Bit 14 - Set if date/time not set on close.
2706 * FIXME: Should check if file is on remote drive.
2708 SET_DX( context, 0 );
2709 break;
2711 case 0x01: /* SET DEVICE INFORMATION */
2712 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2713 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2714 case 0x06: /* GET INPUT STATUS */
2715 case 0x07: /* GET OUTPUT STATUS */
2716 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2717 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2718 default:
2719 INT_BARF( context, 0x21 );
2720 break;
2725 /***********************************************************************
2726 * INT21_Ioctl
2728 * Handler for function 0x44.
2730 static void INT21_Ioctl( CONTEXT *context )
2732 switch (AL_reg(context))
2734 case 0x00:
2735 case 0x01:
2736 case 0x02:
2737 case 0x03:
2738 INT21_Ioctl_Char( context );
2739 break;
2741 case 0x04:
2742 case 0x05:
2743 INT21_Ioctl_Block( context );
2744 break;
2746 case 0x06:
2747 case 0x07:
2748 INT21_Ioctl_Char( context );
2749 break;
2751 case 0x08:
2752 case 0x09:
2753 INT21_Ioctl_Block( context );
2754 break;
2756 case 0x0a:
2757 INT21_Ioctl_Char( context );
2758 break;
2760 case 0x0b: /* SET SHARING RETRY COUNT */
2761 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2762 CX_reg(context), DX_reg(context) );
2763 if (!CX_reg(context))
2765 SET_AX( context, 1 );
2766 SET_CFLAG( context );
2768 else
2770 RESET_CFLAG( context );
2772 break;
2774 case 0x0c:
2775 INT21_Ioctl_Char( context );
2776 break;
2778 case 0x0d:
2779 case 0x0e:
2780 case 0x0f:
2781 INT21_Ioctl_Block( context );
2782 break;
2784 case 0x10:
2785 INT21_Ioctl_Char( context );
2786 break;
2788 case 0x11:
2789 INT21_Ioctl_Block( context );
2790 break;
2792 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2793 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2794 SET_CFLAG(context); /* Error / This is not DR DOS. */
2795 SET_AX( context, 0x0001 ); /* Invalid function */
2796 break;
2798 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2799 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2800 SET_CFLAG(context); /* Error / This is not DR DOS. */
2801 SET_AX( context, 0x0001 ); /* Invalid function */
2802 break;
2804 case 0xe0: /* Sun PC-NFS API */
2805 TRACE( "Sun PC-NFS API\n" );
2806 /* not installed */
2807 break;
2809 default:
2810 INT_BARF( context, 0x21 );
2815 /***********************************************************************
2816 * INT21_Fat32
2818 * Handler for function 0x73.
2820 static BOOL INT21_Fat32( CONTEXT *context )
2822 switch (AL_reg(context))
2824 case 0x02: /* FAT32 - GET EXTENDED DPB */
2826 BYTE drive = INT21_MapDrive( DL_reg(context) );
2827 WORD *ptr = CTX_SEG_OFF_TO_LIN(context,
2828 context->SegEs, context->Edi);
2829 INT21_DPB *target = (INT21_DPB*)(ptr + 1);
2830 INT21_DPB *source;
2832 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context) );
2834 if ( CX_reg(context) < sizeof(INT21_DPB) + 2 || *ptr < sizeof(INT21_DPB) )
2836 SetLastError( ERROR_BAD_LENGTH );
2837 return FALSE;
2840 if ( !INT21_FillDrivePB( drive ) )
2842 SetLastError( ERROR_INVALID_DRIVE );
2843 return FALSE;
2846 source = &INT21_GetHeapPointer()->misc_dpb_list[drive];
2848 *ptr = sizeof(INT21_DPB);
2849 *target = *source;
2851 if (LOWORD(context->Esi) != 0xF1A6)
2853 target->driver_header = 0;
2854 target->next = 0;
2856 else
2858 FIXME( "Caller requested driver and next DPB pointers!\n" );
2861 break;
2863 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2865 WCHAR dirW[MAX_PATH];
2866 char *dirA = CTX_SEG_OFF_TO_LIN( context,
2867 context->SegDs, context->Edx );
2868 BYTE *data = CTX_SEG_OFF_TO_LIN( context,
2869 context->SegEs, context->Edi );
2870 DWORD cluster_sectors;
2871 DWORD sector_bytes;
2872 DWORD free_clusters;
2873 DWORD total_clusters;
2875 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA );
2876 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
2878 if (CX_reg(context) < 44)
2880 SetLastError( ERROR_BAD_LENGTH );
2881 return FALSE;
2884 if (!GetDiskFreeSpaceW( dirW, &cluster_sectors, &sector_bytes,
2885 &free_clusters, &total_clusters ))
2886 return FALSE;
2888 *(WORD*) (data + 0) = 44; /* size of structure */
2889 *(WORD*) (data + 2) = 0; /* version */
2890 *(DWORD*)(data + 4) = cluster_sectors;
2891 *(DWORD*)(data + 8) = sector_bytes;
2892 *(DWORD*)(data + 12) = free_clusters;
2893 *(DWORD*)(data + 16) = total_clusters;
2896 * Below we have free/total sectors and
2897 * free/total allocation units without adjustment
2898 * for compression. We fake both using cluster information.
2900 *(DWORD*)(data + 20) = free_clusters * cluster_sectors;
2901 *(DWORD*)(data + 24) = total_clusters * cluster_sectors;
2902 *(DWORD*)(data + 28) = free_clusters;
2903 *(DWORD*)(data + 32) = total_clusters;
2906 * Between (data + 36) and (data + 43) there
2907 * are eight reserved bytes.
2910 break;
2912 default:
2913 INT_BARF( context, 0x21 );
2916 return TRUE;
2919 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA *dataA,
2920 const WIN32_FIND_DATAW *dataW)
2922 dataA->dwFileAttributes = dataW->dwFileAttributes;
2923 dataA->ftCreationTime = dataW->ftCreationTime;
2924 dataA->ftLastAccessTime = dataW->ftLastAccessTime;
2925 dataA->ftLastWriteTime = dataW->ftLastWriteTime;
2926 dataA->nFileSizeHigh = dataW->nFileSizeHigh;
2927 dataA->nFileSizeLow = dataW->nFileSizeLow;
2928 WideCharToMultiByte( CP_OEMCP, 0, dataW->cFileName, -1,
2929 dataA->cFileName, sizeof(dataA->cFileName), NULL, NULL );
2930 WideCharToMultiByte( CP_OEMCP, 0, dataW->cAlternateFileName, -1,
2931 dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName), NULL, NULL );
2934 /***********************************************************************
2935 * INT21_LongFilename
2937 * Handler for function 0x71.
2939 static void INT21_LongFilename( CONTEXT *context )
2941 BOOL bSetDOSExtendedError = FALSE;
2942 WCHAR pathW[MAX_PATH];
2943 char* pathA;
2945 switch (AL_reg(context))
2947 case 0x0d: /* RESET DRIVE */
2948 INT_BARF( context, 0x21 );
2949 break;
2951 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
2952 if (!INT21_CreateDirectory( context ))
2953 bSetDOSExtendedError = TRUE;
2954 break;
2956 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
2957 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2959 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA);
2960 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2961 if (!RemoveDirectoryW( pathW )) bSetDOSExtendedError = TRUE;
2962 break;
2964 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
2965 if (!INT21_SetCurrentDirectory( context ))
2966 bSetDOSExtendedError = TRUE;
2967 break;
2969 case 0x41: /* LONG FILENAME - DELETE FILE */
2970 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2972 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA );
2973 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2975 if (!DeleteFileW( pathW )) bSetDOSExtendedError = TRUE;
2976 break;
2978 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
2979 if (!INT21_FileAttributes( context, BL_reg(context), TRUE ))
2980 bSetDOSExtendedError = TRUE;
2981 break;
2983 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
2984 if (!INT21_GetCurrentDirectory( context, TRUE ))
2985 bSetDOSExtendedError = TRUE;
2986 break;
2988 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
2990 HANDLE handle;
2991 HGLOBAL16 h16;
2992 WIN32_FIND_DATAW dataW;
2993 WIN32_FIND_DATAA* dataA;
2995 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
2996 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA);
2998 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2999 handle = FindFirstFileW(pathW, &dataW);
3001 dataA = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
3002 if (handle != INVALID_HANDLE_VALUE &&
3003 (h16 = GlobalAlloc16(GMEM_MOVEABLE, sizeof(handle))))
3005 HANDLE* ptr = GlobalLock16( h16 );
3006 *ptr = handle;
3007 GlobalUnlock16( h16 );
3008 SET_AX( context, h16 );
3009 INT21_ConvertFindDataWtoA(dataA, &dataW);
3011 else
3013 if (handle != INVALID_HANDLE_VALUE) FindClose(handle);
3014 SET_AX( context, INVALID_HANDLE_VALUE16);
3015 bSetDOSExtendedError = TRUE;
3018 break;
3020 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
3022 HGLOBAL16 h16 = BX_reg(context);
3023 HANDLE* ptr;
3024 WIN32_FIND_DATAW dataW;
3025 WIN32_FIND_DATAA* dataA;
3027 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
3028 BX_reg(context));
3030 dataA = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
3032 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
3034 if (!FindNextFileW(*ptr, &dataW)) bSetDOSExtendedError = TRUE;
3035 else INT21_ConvertFindDataWtoA(dataA, &dataW);
3036 GlobalUnlock16( h16 );
3038 else
3040 SetLastError( ERROR_INVALID_HANDLE );
3041 bSetDOSExtendedError = TRUE;
3044 break;
3046 case 0x56: /* LONG FILENAME - RENAME FILE */
3047 if (!INT21_RenameFile(context))
3048 bSetDOSExtendedError = TRUE;
3049 break;
3051 case 0x60: /* LONG FILENAME - CONVERT PATH */
3053 WCHAR res[MAX_PATH];
3055 switch (CL_reg(context))
3057 case 0x00: /* "truename" - Canonicalize path */
3059 * FIXME: This is not 100% equal to 0x01 case,
3060 * if you fix this, fix int21 subfunction 0x60, too.
3063 case 0x01: /* Get short filename or path */
3064 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
3065 if (!GetShortPathNameW(pathW, res, 67))
3066 bSetDOSExtendedError = TRUE;
3067 else
3069 SET_AX( context, 0 );
3070 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
3071 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3072 67, NULL, NULL);
3074 break;
3076 case 0x02: /* Get canonical long filename or path */
3077 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
3078 if (!GetFullPathNameW(pathW, 128, res, NULL))
3079 bSetDOSExtendedError = TRUE;
3080 else
3082 SET_AX( context, 0 );
3083 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
3084 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3085 128, NULL, NULL);
3087 break;
3088 default:
3089 FIXME("Unimplemented long file name function:\n");
3090 INT_BARF( context, 0x21 );
3091 SET_CFLAG(context);
3092 SET_AL( context, 0 );
3093 break;
3096 break;
3098 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
3099 if (!INT21_CreateFile( context, context->Esi, TRUE,
3100 BX_reg(context), DL_reg(context) ))
3101 bSetDOSExtendedError = TRUE;
3102 break;
3104 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
3106 DWORD filename_len, flags;
3107 WCHAR dstW[8];
3109 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
3111 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA);
3112 SET_AX( context, 0 );
3113 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3114 if (!GetVolumeInformationW( pathW, NULL, 0, NULL, &filename_len,
3115 &flags, dstW, 8 ))
3117 INT_BARF( context, 0x21 );
3118 SET_CFLAG(context);
3119 break;
3121 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
3122 SET_CX( context, filename_len );
3123 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
3124 WideCharToMultiByte(CP_OEMCP, 0, dstW, -1,
3125 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3126 8, NULL, NULL);
3128 break;
3130 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
3132 HGLOBAL16 h16 = BX_reg(context);
3133 HANDLE* ptr;
3135 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
3136 BX_reg(context));
3137 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
3139 if (!FindClose( *ptr )) bSetDOSExtendedError = TRUE;
3140 GlobalUnlock16( h16 );
3141 GlobalFree16( h16 );
3143 else
3145 SetLastError( ERROR_INVALID_HANDLE );
3146 bSetDOSExtendedError = TRUE;
3149 break;
3151 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
3153 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
3154 BY_HANDLE_FILE_INFORMATION *info =
3155 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3157 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
3159 if (!GetFileInformationByHandle(handle, info))
3160 bSetDOSExtendedError = TRUE;
3162 break;
3164 case 0xa7: /* LONG FILENAME - CONVERT TIME */
3165 switch (BL_reg(context))
3167 case 0x00: /* FILE TIME TO DOS TIME */
3169 WORD date, time;
3170 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3171 context->SegDs,
3172 context->Esi);
3174 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
3176 FileTimeToDosDateTime( filetime, &date, &time );
3178 SET_DX( context, date );
3179 SET_CX( context, time );
3182 * FIXME: BH has number of 10-millisecond units
3183 * past time in CX.
3185 SET_BH( context, 0 );
3187 break;
3189 case 0x01: /* DOS TIME TO FILE TIME */
3191 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3192 context->SegEs,
3193 context->Edi);
3195 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
3198 * FIXME: BH has number of 10-millisecond units
3199 * past time in CX.
3201 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
3202 filetime );
3204 break;
3206 default:
3207 INT_BARF( context, 0x21 );
3208 break;
3210 break;
3212 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
3213 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3214 case 0xaa: /* LONG FILENAME - SUBST */
3215 default:
3216 FIXME("Unimplemented long file name function:\n");
3217 INT_BARF( context, 0x21 );
3218 SET_CFLAG(context);
3219 SET_AL( context, 0 );
3220 break;
3223 if (bSetDOSExtendedError)
3225 SET_AX( context, GetLastError() );
3226 SET_CFLAG( context );
3231 /***********************************************************************
3232 * INT21_RenameFile
3234 * Handler for:
3235 * - function 0x56
3236 * - subfunction 0x56 of function 0x71
3237 * - subfunction 0xff of function 0x43 (CL == 0x56)
3239 static BOOL INT21_RenameFile( CONTEXT *context )
3241 WCHAR fromW[MAX_PATH];
3242 WCHAR toW[MAX_PATH];
3243 char *fromA = CTX_SEG_OFF_TO_LIN(context,
3244 context->SegDs,context->Edx);
3245 char *toA = CTX_SEG_OFF_TO_LIN(context,
3246 context->SegEs,context->Edi);
3248 TRACE( "RENAME FILE %s to %s\n", fromA, toA );
3249 MultiByteToWideChar(CP_OEMCP, 0, fromA, -1, fromW, MAX_PATH);
3250 MultiByteToWideChar(CP_OEMCP, 0, toA, -1, toW, MAX_PATH);
3252 return MoveFileW( fromW, toW );
3256 /***********************************************************************
3257 * INT21_NetworkFunc
3259 * Handler for:
3260 * - function 0x5e
3262 static BOOL INT21_NetworkFunc (CONTEXT *context)
3264 switch (AL_reg(context))
3266 case 0x00: /* Get machine name. */
3268 WCHAR dstW[MAX_COMPUTERNAME_LENGTH + 1];
3269 DWORD s = sizeof(dstW) / sizeof(WCHAR);
3270 int len;
3272 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
3273 TRACE("getting machine name to %p\n", dst);
3274 if (!GetComputerNameW(dstW, &s) ||
3275 !WideCharToMultiByte(CP_OEMCP, 0, dstW, -1, dst, 16, NULL, NULL))
3277 WARN("failed!\n");
3278 SetLastError( ER_NoNetwork );
3279 return TRUE;
3281 for (len = strlen(dst); len < 15; len++) dst[len] = ' ';
3282 dst[15] = 0;
3283 SET_CH( context, 1 ); /* Valid */
3284 SET_CL( context, 1 ); /* NETbios number??? */
3285 TRACE("returning %s\n", debugstr_an(dst, 16));
3286 return FALSE;
3289 default:
3290 SetLastError( ER_NoNetwork );
3291 return TRUE;
3295 /******************************************************************
3296 * INT21_GetDiskSerialNumber
3299 static int INT21_GetDiskSerialNumber( CONTEXT *context )
3301 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3302 WCHAR path[] = {'A',':','\\',0}, label[11];
3303 DWORD serial;
3305 path[0] += INT21_MapDrive(BL_reg(context));
3306 if (!GetVolumeInformationW( path, label, 11, &serial, NULL, NULL, NULL, 0))
3308 SetLastError( ERROR_INVALID_DRIVE );
3309 return 0;
3312 *(WORD *)dataptr = 0;
3313 memcpy(dataptr + 2, &serial, sizeof(DWORD));
3314 WideCharToMultiByte(CP_OEMCP, 0, label, 11, (LPSTR)dataptr + 6, 11, NULL, NULL);
3315 memcpy(dataptr + 17, "FAT16 ", 8);
3316 return 1;
3320 /******************************************************************
3321 * INT21_SetDiskSerialNumber
3324 static BOOL INT21_SetDiskSerialNumber( CONTEXT *context )
3326 #if 0
3327 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3328 int drive = INT21_MapDrive(BL_reg(context));
3330 if (!is_valid_drive(drive))
3332 SetLastError( ERROR_INVALID_DRIVE );
3333 return FALSE;
3336 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
3337 return TRUE;
3338 #else
3339 FIXME("Setting drive serial number is no longer supported\n");
3340 SetLastError( ERROR_NOT_SUPPORTED );
3341 return FALSE;
3342 #endif
3346 /******************************************************************
3347 * INT21_GetFreeDiskSpace
3350 static BOOL INT21_GetFreeDiskSpace( CONTEXT *context )
3352 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
3353 WCHAR root[] = {'A',':','\\',0};
3354 const DWORD max_clusters = 0x3d83;
3355 const DWORD max_sectors_per_cluster = 0x7f;
3356 const DWORD max_bytes_per_sector = 0x200;
3358 root[0] += INT21_MapDrive(DL_reg(context));
3359 if (!GetDiskFreeSpaceW( root, &cluster_sectors, &sector_bytes,
3360 &free_clusters, &total_clusters ))
3361 return FALSE;
3363 /* Some old win31 apps (Lotus SmartSuite 5.1) crap out if there's too
3364 * much disk space, so Windows XP seems to apply the following limits:
3365 * cluster_sectors <= 0x7f
3366 * sector size <= 0x200
3367 * clusters <= 0x3D83
3368 * This means total reported space is limited to about 1GB.
3371 /* Make sure bytes-per-sector is in [, max] */
3372 while (sector_bytes > max_bytes_per_sector) {
3373 sector_bytes >>= 1;
3374 free_clusters <<= 1;
3375 total_clusters <<= 1;
3377 /* Then make sure sectors-per-cluster is in [max/2, max]. */
3378 while (cluster_sectors <= max_sectors_per_cluster/2) {
3379 cluster_sectors <<= 1;
3380 free_clusters >>= 1;
3381 total_clusters >>= 1;
3383 while (cluster_sectors > max_sectors_per_cluster) {
3384 cluster_sectors >>= 1;
3385 free_clusters <<= 1;
3386 total_clusters <<= 1;
3389 /* scale up sectors_per_cluster to exactly max_sectors_per_cluster.
3390 * We could skip this, but that would impose an artificially low
3391 * limit on reported disk space.
3392 * To avoid overflow, first apply a preliminary cap on sector count;
3393 * this will not affect the correctness of the final result,
3394 * because if the preliminary cap hits, the final one will, too.
3396 if (total_clusters > 4 * max_clusters)
3397 total_clusters = 4 * max_clusters;
3398 if (free_clusters > 4 * max_clusters)
3399 free_clusters = 4 * max_clusters;
3400 if (cluster_sectors < max_sectors_per_cluster) {
3401 free_clusters *= cluster_sectors;
3402 free_clusters /= max_sectors_per_cluster;
3403 total_clusters *= cluster_sectors;
3404 total_clusters /= max_sectors_per_cluster;
3405 cluster_sectors = max_sectors_per_cluster;
3408 /* Finally, apply real cluster count cap. */
3409 if (total_clusters > max_clusters)
3410 total_clusters = max_clusters;
3411 if (free_clusters > max_clusters)
3412 free_clusters = max_clusters;
3414 SET_AX( context, cluster_sectors );
3415 SET_BX( context, free_clusters );
3416 SET_CX( context, sector_bytes );
3417 SET_DX( context, total_clusters );
3418 return TRUE;
3421 /******************************************************************
3422 * INT21_GetDriveAllocInfo
3425 static BOOL INT21_GetDriveAllocInfo( CONTEXT *context, BYTE drive )
3427 INT21_DPB *dpb;
3429 drive = INT21_MapDrive( drive );
3430 if (!INT21_FillDrivePB( drive )) return FALSE;
3431 dpb = &(INT21_GetHeapPointer()->misc_dpb_list[drive]);
3432 SET_AL( context, dpb->cluster_sectors + 1 );
3433 SET_CX( context, dpb->sector_bytes );
3434 SET_DX( context, dpb->num_clusters1 );
3436 context->SegDs = INT21_GetHeapSelector( context );
3437 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive].media_ID ) );
3438 return TRUE;
3441 /***********************************************************************
3442 * INT21_GetExtendedError
3444 static void INT21_GetExtendedError( CONTEXT *context )
3446 BYTE class, action, locus;
3447 WORD error = GetLastError();
3449 switch(error)
3451 case ERROR_SUCCESS:
3452 class = action = locus = 0;
3453 break;
3454 case ERROR_DIR_NOT_EMPTY:
3455 class = EC_Exists;
3456 action = SA_Ignore;
3457 locus = EL_Disk;
3458 break;
3459 case ERROR_ACCESS_DENIED:
3460 class = EC_AccessDenied;
3461 action = SA_Abort;
3462 locus = EL_Disk;
3463 break;
3464 case ERROR_CANNOT_MAKE:
3465 class = EC_AccessDenied;
3466 action = SA_Abort;
3467 locus = EL_Unknown;
3468 break;
3469 case ERROR_DISK_FULL:
3470 case ERROR_HANDLE_DISK_FULL:
3471 class = EC_MediaError;
3472 action = SA_Abort;
3473 locus = EL_Disk;
3474 break;
3475 case ERROR_FILE_EXISTS:
3476 case ERROR_ALREADY_EXISTS:
3477 class = EC_Exists;
3478 action = SA_Abort;
3479 locus = EL_Disk;
3480 break;
3481 case ERROR_FILE_NOT_FOUND:
3482 case ERROR_PATH_NOT_FOUND:
3483 case ERROR_INVALID_NAME:
3484 class = EC_NotFound;
3485 action = SA_Abort;
3486 locus = EL_Disk;
3487 break;
3488 case ERROR_GEN_FAILURE:
3489 class = EC_SystemFailure;
3490 action = SA_Abort;
3491 locus = EL_Unknown;
3492 break;
3493 case ERROR_INVALID_DRIVE:
3494 class = EC_MediaError;
3495 action = SA_Abort;
3496 locus = EL_Disk;
3497 break;
3498 case ERROR_INVALID_HANDLE:
3499 class = EC_ProgramError;
3500 action = SA_Abort;
3501 locus = EL_Disk;
3502 break;
3503 case ERROR_LOCK_VIOLATION:
3504 class = EC_AccessDenied;
3505 action = SA_Abort;
3506 locus = EL_Disk;
3507 break;
3508 case ERROR_NO_MORE_FILES:
3509 class = EC_MediaError;
3510 action = SA_Abort;
3511 locus = EL_Disk;
3512 break;
3513 case ER_NoNetwork:
3514 class = EC_NotFound;
3515 action = SA_Abort;
3516 locus = EL_Network;
3517 break;
3518 case ERROR_NOT_ENOUGH_MEMORY:
3519 class = EC_OutOfResource;
3520 action = SA_Abort;
3521 locus = EL_Memory;
3522 break;
3523 case ERROR_SEEK:
3524 class = EC_NotFound;
3525 action = SA_Ignore;
3526 locus = EL_Disk;
3527 break;
3528 case ERROR_SHARING_VIOLATION:
3529 class = EC_Temporary;
3530 action = SA_Retry;
3531 locus = EL_Disk;
3532 break;
3533 case ERROR_TOO_MANY_OPEN_FILES:
3534 class = EC_ProgramError;
3535 action = SA_Abort;
3536 locus = EL_Disk;
3537 break;
3538 default:
3539 FIXME("Unknown error %d\n", error );
3540 class = EC_SystemFailure;
3541 action = SA_Abort;
3542 locus = EL_Unknown;
3543 break;
3545 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3546 error, class, action, locus );
3547 SET_AX( context, error );
3548 SET_BH( context, class );
3549 SET_BL( context, action );
3550 SET_CH( context, locus );
3553 static BOOL INT21_CreateTempFile( CONTEXT *context )
3555 static int counter = 0;
3556 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
3557 char *p = name + strlen(name);
3559 /* despite what Ralf Brown says, some programs seem to call without
3560 * ending backslash (DOS accepts that, so we accept it too) */
3561 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
3563 for (;;)
3565 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
3566 counter = (counter + 1) % 1000;
3568 SET_AX( context,
3569 Win32HandleToDosFileHandle(
3570 CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
3571 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
3572 CREATE_NEW, 0, 0 ) ) );
3573 if (AX_reg(context) != HFILE_ERROR16)
3575 TRACE("created %s\n", name );
3576 return TRUE;
3578 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
3582 /***********************************************************************
3583 * DOSFS_ToDosFCBFormat
3585 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3586 * expanding wild cards and converting to upper-case in the process.
3587 * File name can be terminated by '\0', '\\' or '/'.
3588 * Return FALSE if the name is not a valid DOS name.
3589 * 'buffer' must be at least 12 characters long.
3591 /* Chars we don't want to see in DOS file names */
3592 static BOOL INT21_ToDosFCBFormat( LPCWSTR name, LPWSTR buffer )
3594 static const WCHAR invalid_chars[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3595 LPCWSTR p = name;
3596 int i;
3598 /* Check for "." and ".." */
3599 if (*p == '.')
3601 p++;
3602 buffer[0] = '.';
3603 for(i = 1; i < 11; i++) buffer[i] = ' ';
3604 buffer[11] = 0;
3605 if (*p == '.')
3607 buffer[1] = '.';
3608 p++;
3610 return (!*p || (*p == '/') || (*p == '\\'));
3613 for (i = 0; i < 8; i++)
3615 switch(*p)
3617 case '\0':
3618 case '\\':
3619 case '/':
3620 case '.':
3621 buffer[i] = ' ';
3622 break;
3623 case '?':
3624 p++;
3625 /* fall through */
3626 case '*':
3627 buffer[i] = '?';
3628 break;
3629 default:
3630 if (strchrW( invalid_chars, *p )) return FALSE;
3631 buffer[i] = toupperW(*p);
3632 p++;
3633 break;
3637 if (*p == '*')
3639 /* Skip all chars after wildcard up to first dot */
3640 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
3642 else
3644 /* Check if name too long */
3645 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return FALSE;
3647 if (*p == '.') p++; /* Skip dot */
3649 for (i = 8; i < 11; i++)
3651 switch(*p)
3653 case '\0':
3654 case '\\':
3655 case '/':
3656 buffer[i] = ' ';
3657 break;
3658 case '.':
3659 return FALSE; /* Second extension not allowed */
3660 case '?':
3661 p++;
3662 /* fall through */
3663 case '*':
3664 buffer[i] = '?';
3665 break;
3666 default:
3667 if (strchrW( invalid_chars, *p )) return FALSE;
3668 buffer[i] = toupperW(*p);
3669 p++;
3670 break;
3673 buffer[11] = '\0';
3675 /* at most 3 character of the extension are processed
3676 * is something behind this ?
3678 while (*p == '*' || *p == ' ') p++; /* skip wildcards and spaces */
3679 return (!*p || (*p == '/') || (*p == '\\'));
3682 static HANDLE INT21_FindHandle;
3683 static const WCHAR *INT21_FindPath; /* will point to current dta->fullPath search */
3685 /******************************************************************
3686 * INT21_FindFirst
3688 static BOOL INT21_FindFirst( CONTEXT *context )
3690 WCHAR *p, *q;
3691 const char *path;
3692 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3693 WCHAR maskW[12], pathW[MAX_PATH];
3694 static const WCHAR wildcardW[] = {'*','.','*',0};
3696 path = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3697 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
3699 p = strrchrW( pathW, '\\');
3700 q = strrchrW( pathW, '/');
3701 if (q>p) p = q;
3702 if (!p)
3704 if (pathW[0] && pathW[1] == ':') p = pathW + 2;
3705 else p = pathW;
3707 else p++;
3709 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3710 * (doesn't matter as it is set below anyway)
3712 if (!INT21_ToDosFCBFormat( p, maskW ))
3714 SetLastError( ERROR_FILE_NOT_FOUND );
3715 SET_AX( context, ERROR_FILE_NOT_FOUND );
3716 SET_CFLAG(context);
3717 return FALSE;
3719 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
3721 dta->fullPath = HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW) + (p - pathW)*sizeof(WCHAR) );
3722 memcpy( dta->fullPath, pathW, (p - pathW) * sizeof(WCHAR) );
3723 memcpy( dta->fullPath + (p - pathW), wildcardW, sizeof(wildcardW) );
3724 /* we must have a fully qualified file name in dta->fullPath
3725 * (we could have a UNC path, but this would lead to some errors later on)
3727 dta->drive = toupperW(dta->fullPath[0]) - 'A';
3728 dta->count = 0;
3729 dta->search_attr = CL_reg(context);
3730 return TRUE;
3733 /******************************************************************
3734 * match_short
3736 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3738 static BOOL match_short(LPCWSTR shortW, LPCSTR maskA)
3740 WCHAR mask[11], file[12];
3741 int i;
3743 if (!INT21_ToDosFCBFormat( shortW, file )) return FALSE;
3744 MultiByteToWideChar(CP_OEMCP, 0, maskA, 11, mask, 11);
3745 for (i = 0; i < 11; i++)
3746 if (mask[i] != '?' && mask[i] != file[i]) return FALSE;
3747 return TRUE;
3750 static unsigned INT21_FindHelper(LPCWSTR fullPath, unsigned drive, unsigned count,
3751 LPCSTR mask, unsigned search_attr,
3752 WIN32_FIND_DATAW* entry)
3754 unsigned ncalls;
3756 if ((search_attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
3758 WCHAR path[] = {' ',':','\\',0};
3760 if (count) return 0;
3761 path[0] = drive + 'A';
3762 if (!GetVolumeInformationW(path, entry->cAlternateFileName, 13, NULL, NULL, NULL, NULL, 0)) return 0;
3763 if (!entry->cAlternateFileName[0]) return 0;
3764 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftCreationTime );
3765 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftLastAccessTime );
3766 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftLastWriteTime );
3767 entry->dwFileAttributes = FA_LABEL;
3768 entry->nFileSizeHigh = entry->nFileSizeLow = 0;
3769 TRACE("returning %s as label\n", debugstr_w(entry->cAlternateFileName));
3770 return 1;
3773 if (!INT21_FindHandle || INT21_FindPath != fullPath || count == 0)
3775 if (INT21_FindHandle) FindClose(INT21_FindHandle);
3776 INT21_FindHandle = FindFirstFileW(fullPath, entry);
3777 if (INT21_FindHandle == INVALID_HANDLE_VALUE)
3779 INT21_FindHandle = 0;
3780 return 0;
3782 INT21_FindPath = fullPath;
3783 /* we need to resync search */
3784 ncalls = count;
3786 else ncalls = 1;
3788 while (ncalls-- != 0)
3790 if (!FindNextFileW(INT21_FindHandle, entry))
3792 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3793 return 0;
3796 while (count < 0xffff)
3798 count++;
3799 /* Check the file attributes, and path */
3800 if (!(entry->dwFileAttributes & ~search_attr) &&
3801 match_short(entry->cAlternateFileName[0] ? entry->cAlternateFileName : entry->cFileName,
3802 mask))
3804 return count;
3806 if (!FindNextFileW(INT21_FindHandle, entry))
3808 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3809 return 0;
3812 WARN("Too many directory entries in %s\n", debugstr_w(fullPath) );
3813 return 0;
3816 /******************************************************************
3817 * INT21_FindNext
3819 static BOOL INT21_FindNext( CONTEXT *context )
3821 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3822 DWORD attr = dta->search_attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
3823 WIN32_FIND_DATAW entry;
3824 int n;
3826 if (!dta->fullPath) return FALSE;
3828 n = INT21_FindHelper(dta->fullPath, dta->drive, dta->count,
3829 dta->mask, attr, &entry);
3830 if (n)
3832 dta->fileattr = entry.dwFileAttributes;
3833 dta->filesize = entry.nFileSizeLow;
3834 FileTimeToDosDateTime( &entry.ftLastWriteTime, &dta->filedate, &dta->filetime );
3835 if (entry.cAlternateFileName[0])
3836 WideCharToMultiByte(CP_OEMCP, 0, entry.cAlternateFileName, -1,
3837 dta->filename, 13, NULL, NULL);
3838 else
3839 WideCharToMultiByte(CP_OEMCP, 0, entry.cFileName, -1, dta->filename, 13, NULL, NULL);
3841 if (!memchr(dta->mask,'?',11))
3843 /* wildcardless search, release resources in case no findnext will
3844 * be issued, and as a workaround in case file creation messes up
3845 * findnext, as sometimes happens with pkunzip
3847 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3848 INT21_FindPath = dta->fullPath = NULL;
3850 dta->count = n;
3851 return TRUE;
3853 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3854 INT21_FindPath = dta->fullPath = NULL;
3855 return FALSE;
3858 /* microsoft's programmers should be shot for using CP/M style int21
3859 calls in Windows for Workgroup's winfile.exe */
3861 /******************************************************************
3862 * INT21_FindFirstFCB
3865 static BOOL INT21_FindFirstFCB( CONTEXT *context )
3867 BYTE *fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3868 FINDFILE_FCB *pFCB;
3869 int drive;
3870 WCHAR p[] = {' ',':',};
3872 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
3873 else pFCB = (FINDFILE_FCB *)fcb;
3874 drive = INT21_MapDrive( pFCB->drive );
3875 if (drive == MAX_DOS_DRIVES) return FALSE;
3877 p[0] = 'A' + drive;
3878 pFCB->fullPath = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
3879 if (!pFCB->fullPath) return FALSE;
3880 GetLongPathNameW(p, pFCB->fullPath, MAX_PATH);
3881 pFCB->count = 0;
3882 return TRUE;
3885 /******************************************************************
3886 * INT21_FindNextFCB
3889 static BOOL INT21_FindNextFCB( CONTEXT *context )
3891 BYTE *fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3892 FINDFILE_FCB *pFCB;
3893 LPBYTE pResult = INT21_GetCurrentDTA(context);
3894 DOS_DIRENTRY_LAYOUT *ddl;
3895 WIN32_FIND_DATAW entry;
3896 BYTE attr;
3897 int n;
3898 WCHAR nameW[12];
3900 if (*fcb == 0xff) /* extended FCB ? */
3902 attr = fcb[6];
3903 pFCB = (FINDFILE_FCB *)(fcb + 7);
3905 else
3907 attr = 0;
3908 pFCB = (FINDFILE_FCB *)fcb;
3911 if (!pFCB->fullPath) return FALSE;
3912 n = INT21_FindHelper(pFCB->fullPath, INT21_MapDrive( pFCB->drive ),
3913 pFCB->count, pFCB->filename, attr, &entry);
3914 if (!n)
3916 HeapFree( GetProcessHeap(), 0, pFCB->fullPath );
3917 INT21_FindPath = pFCB->fullPath = NULL;
3918 return FALSE;
3920 pFCB->count += n;
3922 if (*fcb == 0xff)
3924 /* place extended FCB header before pResult if called with extended FCB */
3925 *pResult = 0xff;
3926 pResult += 6; /* leave reserved field behind */
3927 *pResult++ = entry.dwFileAttributes;
3929 *pResult++ = INT21_MapDrive( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
3930 ddl = (DOS_DIRENTRY_LAYOUT*)pResult;
3931 ddl->fileattr = entry.dwFileAttributes;
3932 ddl->cluster = 0; /* what else? */
3933 ddl->filesize = entry.nFileSizeLow;
3934 memset( ddl->reserved, 0, sizeof(ddl->reserved) );
3935 FileTimeToDosDateTime( &entry.ftLastWriteTime,
3936 &ddl->filedate, &ddl->filetime );
3938 /* Convert file name to FCB format */
3939 if (entry.cAlternateFileName[0])
3940 INT21_ToDosFCBFormat( entry.cAlternateFileName, nameW );
3941 else
3942 INT21_ToDosFCBFormat( entry.cFileName, nameW );
3943 WideCharToMultiByte(CP_OEMCP, 0, nameW, 11, ddl->filename, 11, NULL, NULL);
3944 return TRUE;
3948 /******************************************************************
3949 * INT21_ParseFileNameIntoFCB
3952 static void INT21_ParseFileNameIntoFCB( CONTEXT *context )
3954 char *filename =
3955 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
3956 char *fcb =
3957 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
3958 char *s;
3959 WCHAR *buffer;
3960 WCHAR fcbW[12];
3961 INT buffer_len, len;
3963 SET_AL( context, 0xff ); /* failed */
3965 TRACE("filename: '%s'\n", filename);
3967 s = filename;
3968 while (*s && (*s != ' ') && (*s != '\r') && (*s != '\n'))
3969 s++;
3970 len = filename - s;
3972 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
3973 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
3974 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
3975 buffer[len] = 0;
3976 INT21_ToDosFCBFormat(buffer, fcbW);
3977 HeapFree(GetProcessHeap(), 0, buffer);
3978 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
3979 *fcb = 0;
3980 TRACE("FCB: '%s'\n", fcb + 1);
3982 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
3984 /* point DS:SI to first unparsed character */
3985 SET_SI( context, context->Esi + (int)s - (int)filename );
3988 static BOOL INT21_Dup2(HFILE16 hFile1, HFILE16 hFile2)
3990 HFILE16 res = HFILE_ERROR16;
3991 HANDLE handle, new_handle;
3992 #define DOS_TABLE_SIZE 256
3993 DWORD map[DOS_TABLE_SIZE / 32];
3994 int i;
3996 handle = DosFileHandleToWin32Handle(hFile1);
3997 if (handle == INVALID_HANDLE_VALUE)
3998 return FALSE;
4000 _lclose16(hFile2);
4001 /* now loop to allocate the same one... */
4002 memset(map, 0, sizeof(map));
4003 for (i = 0; i < DOS_TABLE_SIZE; i++)
4005 if (!DuplicateHandle(GetCurrentProcess(), handle,
4006 GetCurrentProcess(), &new_handle,
4007 0, FALSE, DUPLICATE_SAME_ACCESS))
4009 res = HFILE_ERROR16;
4010 break;
4012 res = Win32HandleToDosFileHandle(new_handle);
4013 if (res == HFILE_ERROR16 || res == hFile2) break;
4014 map[res / 32] |= 1 << (res % 32);
4016 /* clean up the allocated slots */
4017 for (i = 0; i < DOS_TABLE_SIZE; i++)
4019 if (map[i / 32] & (1 << (i % 32)))
4020 _lclose16((HFILE16)i);
4022 return res == hFile2;
4026 /***********************************************************************
4027 * DOSVM_Int21Handler
4029 * Interrupt 0x21 handler.
4031 void WINAPI DOSVM_Int21Handler( CONTEXT *context )
4033 BOOL bSetDOSExtendedError = FALSE;
4035 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
4036 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
4037 AX_reg(context), BX_reg(context),
4038 CX_reg(context), DX_reg(context),
4039 SI_reg(context), DI_reg(context),
4040 (WORD)context->SegDs, (WORD)context->SegEs,
4041 context->EFlags );
4044 * Extended error is used by (at least) functions 0x2f to 0x62.
4045 * Function 0x59 returns extended error and error should not
4046 * be cleared before handling the function.
4048 if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59)
4049 SetLastError(0);
4051 RESET_CFLAG(context); /* Not sure if this is a good idea. */
4053 switch(AH_reg(context))
4055 case 0x00: /* TERMINATE PROGRAM */
4056 TRACE("TERMINATE PROGRAM\n");
4057 DOSVM_Exit( 0 );
4058 break;
4060 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
4061 TRACE("DIRECT CHARACTER INPUT WITH ECHO - not supported\n");
4062 break;
4064 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
4065 TRACE("Write Character to Standard Output\n");
4066 break;
4068 case 0x03: /* READ CHARACTER FROM STDAUX */
4069 case 0x04: /* WRITE CHARACTER TO STDAUX */
4070 case 0x05: /* WRITE CHARACTER TO PRINTER */
4071 INT_BARF( context, 0x21 );
4072 break;
4074 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
4075 if (DL_reg(context) == 0xff)
4077 TRACE("Direct Console Input\n");
4079 /* no character available */
4080 SET_AL( context, 0 );
4081 SET_ZFLAG( context );
4083 else
4085 TRACE("Direct Console Output\n");
4087 * At least DOS versions 2.1-7.0 return character
4088 * that was written in AL register.
4090 SET_AL( context, DL_reg(context) );
4092 break;
4094 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
4095 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO - not supported\n");
4096 SET_AL( context, 0 );
4097 break;
4099 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
4100 TRACE("CHARACTER INPUT WITHOUT ECHO - not supported\n");
4101 SET_AL( context, 0 );
4102 break;
4104 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
4105 TRACE("WRITE '$'-terminated string from %04X:%04X to stdout\n",
4106 context->SegDs, DX_reg(context) );
4108 LPSTR data = CTX_SEG_OFF_TO_LIN( context,
4109 context->SegDs, context->Edx );
4110 LPSTR p = data;
4111 DWORD w;
4113 * Do NOT use strchr() to calculate the string length,
4114 * as '\0' is valid string content, too!
4115 * Maybe we should check for non-'$' strings, but DOS doesn't.
4117 while (*p != '$') p++;
4119 WriteFile( DosFileHandleToWin32Handle(1), data, p - data, &w, NULL );
4120 SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
4122 break;
4124 case 0x0a: /* BUFFERED INPUT */
4125 TRACE( "BUFFERED INPUT - not supported\n" );
4126 break;
4128 case 0x0b: /* GET STDIN STATUS */
4129 TRACE( "GET STDIN STATUS - not supported\n" );
4130 SET_AL( context, 0 ); /* no character available */
4131 break;
4133 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
4135 BYTE al = AL_reg(context); /* Input function to execute after flush. */
4137 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
4139 /* FIXME: buffers are not flushed */
4142 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
4143 * int21 function identified by AL will be called.
4145 if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
4147 SET_AH( context, al );
4148 DOSVM_Int21Handler( context );
4151 break;
4153 case 0x0d: /* DISK BUFFER FLUSH */
4154 TRACE("DISK BUFFER FLUSH ignored\n");
4155 break;
4157 case 0x0e: /* SELECT DEFAULT DRIVE */
4158 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context) );
4159 INT21_SetCurrentDrive( DL_reg(context) );
4160 SET_AL( context, MAX_DOS_DRIVES );
4161 break;
4163 case 0x0f: /* OPEN FILE USING FCB */
4164 INT21_OpenFileUsingFCB( context );
4165 break;
4167 case 0x10: /* CLOSE FILE USING FCB */
4168 INT21_CloseFileUsingFCB( context );
4169 break;
4171 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
4172 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
4173 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
4174 if (!INT21_FindFirstFCB(context))
4176 SET_AL( context, 0xff );
4177 break;
4179 /* else fall through */
4181 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
4182 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
4183 break;
4185 case 0x13: /* DELETE FILE USING FCB */
4186 INT_BARF( context, 0x21 );
4187 break;
4189 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
4190 INT21_SequentialReadFromFCB( context );
4191 break;
4193 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
4194 INT21_SequentialWriteToFCB( context );
4195 break;
4197 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
4198 case 0x17: /* RENAME FILE USING FCB */
4199 INT_BARF( context, 0x21 );
4200 break;
4202 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4203 SET_AL( context, 0 );
4204 break;
4206 case 0x19: /* GET CURRENT DEFAULT DRIVE */
4207 SET_AL( context, INT21_GetCurrentDrive() );
4208 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context ) );
4209 break;
4211 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
4212 TRACE( "SET DISK TRANSFER AREA ADDRESS %04X:%04X\n",
4213 context->SegDs, DX_reg(context) );
4215 TDB *task = GlobalLock16( GetCurrentTask() );
4216 task->dta = MAKESEGPTR( context->SegDs, DX_reg(context) );
4218 break;
4220 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4221 if (!INT21_GetDriveAllocInfo(context, 0))
4222 SET_AX( context, 0xffff );
4223 break;
4225 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4226 if (!INT21_GetDriveAllocInfo(context, DL_reg(context)))
4227 SET_AX( context, 0xffff );
4228 break;
4230 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4231 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4232 SET_AL( context, 0 );
4233 break;
4235 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4237 BYTE drive = INT21_MapDrive( 0 );
4238 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4240 if (INT21_FillDrivePB( drive ))
4242 SET_AL( context, 0x00 ); /* success */
4243 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4244 context->SegDs = INT21_GetHeapSelector( context );
4246 else
4248 SET_AL( context, 0xff ); /* invalid or network drive */
4251 break;
4253 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4254 SET_AL( context, 0 );
4255 break;
4257 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4258 INT21_ReadRandomRecordFromFCB( context );
4259 break;
4261 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4262 INT21_WriteRandomRecordToFCB( context );
4263 break;
4265 case 0x23: /* GET FILE SIZE FOR FCB */
4266 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4267 INT_BARF( context, 0x21 );
4268 break;
4270 case 0x25: /* SET INTERRUPT VECTOR */
4271 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4273 FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
4274 DOSVM_SetPMHandler16( AL_reg(context), ptr );
4276 break;
4278 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4279 INT_BARF( context, 0x21 );
4280 break;
4282 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4283 INT21_RandomBlockReadFromFCB( context );
4284 break;
4286 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4287 INT21_RandomBlockWriteToFCB( context );
4288 break;
4290 case 0x29: /* PARSE FILENAME INTO FCB */
4291 INT21_ParseFileNameIntoFCB(context);
4292 break;
4294 case 0x2a: /* GET SYSTEM DATE */
4295 TRACE( "GET SYSTEM DATE\n" );
4297 SYSTEMTIME systime;
4298 GetLocalTime( &systime );
4299 SET_CX( context, systime.wYear );
4300 SET_DH( context, systime.wMonth );
4301 SET_DL( context, systime.wDay );
4302 SET_AL( context, systime.wDayOfWeek );
4304 break;
4306 case 0x2b: /* SET SYSTEM DATE */
4307 TRACE( "SET SYSTEM DATE\n" );
4309 WORD year = CX_reg(context);
4310 BYTE month = DH_reg(context);
4311 BYTE day = DL_reg(context);
4313 if (year >= 1980 && year <= 2099 &&
4314 month >= 1 && month <= 12 &&
4315 day >= 1 && day <= 31)
4317 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4318 day, month, year );
4319 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4321 else
4323 SET_AL( context, 0xff ); /* invalid date */
4324 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4325 day, month, year );
4328 break;
4330 case 0x2c: /* GET SYSTEM TIME */
4331 TRACE( "GET SYSTEM TIME\n" );
4333 SYSTEMTIME systime;
4334 GetLocalTime( &systime );
4335 SET_CH( context, systime.wHour );
4336 SET_CL( context, systime.wMinute );
4337 SET_DH( context, systime.wSecond );
4338 SET_DL( context, systime.wMilliseconds / 10 );
4340 break;
4342 case 0x2d: /* SET SYSTEM TIME */
4343 if( CH_reg(context) >= 24 || CL_reg(context) >= 60 || DH_reg(context) >= 60 || DL_reg(context) >= 100 ) {
4344 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4345 CH_reg(context), CL_reg(context),
4346 DH_reg(context), DL_reg(context) );
4347 SET_AL( context, 0xFF );
4349 else
4351 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4352 CH_reg(context), CL_reg(context),
4353 DH_reg(context), DL_reg(context) );
4354 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4356 break;
4358 case 0x2e: /* SET VERIFY FLAG */
4359 TRACE("SET VERIFY FLAG ignored\n");
4360 /* we cannot change the behaviour anyway, so just ignore it */
4361 break;
4363 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4364 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4366 TDB *task = GlobalLock16( GetCurrentTask() );
4367 context->SegEs = SELECTOROF( task->dta );
4368 SET_BX( context, OFFSETOF( task->dta ) );
4370 break;
4372 case 0x30: /* GET DOS VERSION */
4373 TRACE( "GET DOS VERSION - %s requested\n",
4374 (AL_reg(context) == 0x00) ? "OEM number" : "version flag" );
4376 if (AL_reg(context) == 0x00)
4377 SET_BH( context, 0xff ); /* OEM number => undefined */
4378 else
4379 SET_BH( context, 0x08 ); /* version flag => DOS is in ROM */
4381 SET_AL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4382 SET_AH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4384 SET_BL( context, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4385 SET_CX( context, 0x3456 );
4386 break;
4388 case 0x31: /* TERMINATE AND STAY RESIDENT */
4389 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4390 break;
4392 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4394 BYTE drive = INT21_MapDrive( DL_reg(context) );
4395 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context) );
4397 if (INT21_FillDrivePB( drive ))
4399 SET_AL( context, 0x00 ); /* success */
4400 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4401 context->SegDs = INT21_GetHeapSelector( context );
4403 else
4405 SET_AL( context, 0xff ); /* invalid or network drive */
4408 break;
4410 case 0x33: /* MULTIPLEXED */
4411 switch (AL_reg(context))
4413 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4414 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4415 SET_DL( context, brk_flag );
4416 break;
4418 case 0x01: /* SET EXTENDED BREAK STATE */
4419 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4420 brk_flag = (DL_reg(context) > 0) ? 1 : 0;
4421 break;
4423 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4424 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4425 /* ugly coding in order to stay reentrant */
4426 if (DL_reg(context))
4428 SET_DL( context, brk_flag );
4429 brk_flag = 1;
4431 else
4433 SET_DL( context, brk_flag );
4434 brk_flag = 0;
4436 break;
4438 case 0x05: /* GET BOOT DRIVE */
4439 TRACE("GET BOOT DRIVE\n");
4440 SET_DL( context, 3 );
4441 /* c: is Wine's bootdrive (a: is 1)*/
4442 break;
4444 case 0x06: /* GET TRUE VERSION NUMBER */
4445 TRACE("GET TRUE VERSION NUMBER\n");
4446 SET_BL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4447 SET_BH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4448 SET_DL( context, 0x00 ); /* revision */
4449 SET_DH( context, 0x08 ); /* DOS is in ROM */
4450 break;
4452 default:
4453 INT_BARF( context, 0x21 );
4454 break;
4456 break;
4458 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4459 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4460 context->SegEs = INT21_GetHeapSelector( context );
4461 SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
4462 break;
4464 case 0x35: /* GET INTERRUPT VECTOR */
4465 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4467 FARPROC16 addr = DOSVM_GetPMHandler16( AL_reg(context) );
4468 context->SegEs = SELECTOROF(addr);
4469 SET_BX( context, OFFSETOF(addr) );
4471 break;
4473 case 0x36: /* GET FREE DISK SPACE */
4474 TRACE("GET FREE DISK SPACE FOR DRIVE %s (limited to about 1GB)\n",
4475 INT21_DriveName( DL_reg(context) ));
4476 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
4477 break;
4479 case 0x37: /* SWITCHAR */
4481 switch (AL_reg(context))
4483 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4484 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4485 SET_AL( context, 0x00 ); /* success*/
4486 SET_DL( context, '/' );
4487 break;
4488 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4489 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4490 DL_reg( context ));
4491 SET_AL( context, 0x00 ); /* success*/
4492 break;
4493 default:
4494 INT_BARF( context, 0x21 );
4495 break;
4498 break;
4500 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4501 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4502 if (AL_reg(context))
4504 WORD country = AL_reg(context);
4505 if (country == 0xff)
4506 country = BX_reg(context);
4507 if (country != INT21_GetSystemCountryCode()) {
4508 FIXME( "Requested info on non-default country %04x\n", country );
4509 SET_AX(context, 2);
4510 SET_CFLAG(context);
4513 if(AX_reg(context) != 2 )
4515 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context,
4516 context->SegDs,
4517 context->Edx) );
4518 SET_AX( context, INT21_GetSystemCountryCode() );
4519 SET_BX( context, INT21_GetSystemCountryCode() );
4520 RESET_CFLAG(context);
4522 break;
4524 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4525 if (!INT21_CreateDirectory( context ))
4526 bSetDOSExtendedError = TRUE;
4527 else
4528 RESET_CFLAG(context);
4529 break;
4531 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4533 WCHAR dirW[MAX_PATH];
4534 char *dirA = CTX_SEG_OFF_TO_LIN(context,
4535 context->SegDs, context->Edx);
4537 TRACE( "REMOVE DIRECTORY %s\n", dirA );
4539 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
4541 if (!RemoveDirectoryW( dirW ))
4542 bSetDOSExtendedError = TRUE;
4543 else
4544 RESET_CFLAG(context);
4546 break;
4548 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4549 if (!INT21_SetCurrentDirectory( context ))
4550 bSetDOSExtendedError = TRUE;
4551 else
4552 RESET_CFLAG(context);
4553 break;
4555 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4556 if (!INT21_CreateFile( context, context->Edx, FALSE,
4557 OF_READWRITE | OF_SHARE_COMPAT, 0x12 ))
4558 bSetDOSExtendedError = TRUE;
4559 else
4560 RESET_CFLAG(context);
4561 break;
4563 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4564 if (!INT21_CreateFile( context, context->Edx, FALSE,
4565 AL_reg(context), 0x01 ))
4566 bSetDOSExtendedError = TRUE;
4567 else
4568 RESET_CFLAG(context);
4569 break;
4571 case 0x3e: /* "CLOSE" - CLOSE FILE */
4572 TRACE( "CLOSE handle %d\n", BX_reg(context) );
4573 if (_lclose16( BX_reg(context) ) == HFILE_ERROR16)
4574 bSetDOSExtendedError = TRUE;
4575 else
4576 RESET_CFLAG(context);
4577 break;
4579 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4580 TRACE( "READ from %d to %04X:%04X for %d bytes\n",
4581 BX_reg(context),
4582 context->SegDs,
4583 DX_reg(context),
4584 CX_reg(context) );
4586 DWORD result;
4587 WORD count = CX_reg(context);
4588 BYTE *buffer = CTX_SEG_OFF_TO_LIN( context,
4589 context->SegDs,
4590 context->Edx );
4592 /* Some programs pass a count larger than the allocated buffer */
4593 DWORD maxcount = GetSelectorLimit16( context->SegDs ) - DX_reg(context) + 1;
4594 if (count > maxcount) count = maxcount;
4597 * FIXME: Reading from console (BX=1) in DOS mode
4598 * does not work as it is supposed to work.
4601 RESET_CFLAG(context); /* set if error */
4602 if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context)), buffer, count, &result, NULL ))
4603 SET_AX( context, (WORD)result );
4604 else
4605 bSetDOSExtendedError = TRUE;
4607 break;
4609 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4610 TRACE( "WRITE from %04X:%04X to handle %d for %d byte\n",
4611 context->SegDs, DX_reg(context),
4612 BX_reg(context), CX_reg(context) );
4614 char *ptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4615 HFILE handle = (HFILE)DosFileHandleToWin32Handle(BX_reg(context));
4616 LONG result = _hwrite( handle, ptr, CX_reg(context) );
4617 if (result == HFILE_ERROR)
4618 bSetDOSExtendedError = TRUE;
4619 else
4621 SET_AX( context, (WORD)result );
4622 RESET_CFLAG(context);
4625 break;
4627 case 0x41: /* "UNLINK" - DELETE FILE */
4629 WCHAR fileW[MAX_PATH];
4630 char *fileA = CTX_SEG_OFF_TO_LIN(context,
4631 context->SegDs,
4632 context->Edx);
4634 TRACE( "UNLINK %s\n", fileA );
4635 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
4637 if (!DeleteFileW( fileW ))
4638 bSetDOSExtendedError = TRUE;
4639 else
4640 RESET_CFLAG(context);
4642 break;
4644 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4645 TRACE( "LSEEK handle %d offset %d from %s\n",
4646 BX_reg(context),
4647 MAKELONG( DX_reg(context), CX_reg(context) ),
4648 (AL_reg(context) == 0) ?
4649 "start of file" : ((AL_reg(context) == 1) ?
4650 "current file position" : "end of file") );
4652 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4653 LONG offset = MAKELONG( DX_reg(context), CX_reg(context) );
4654 DWORD status = SetFilePointer( handle, offset,
4655 NULL, AL_reg(context) );
4656 if (status == INVALID_SET_FILE_POINTER)
4657 bSetDOSExtendedError = TRUE;
4658 else
4660 SET_AX( context, LOWORD(status) );
4661 SET_DX( context, HIWORD(status) );
4662 RESET_CFLAG(context);
4665 break;
4667 case 0x43: /* FILE ATTRIBUTES */
4668 if (!INT21_FileAttributes( context, AL_reg(context), FALSE ))
4669 bSetDOSExtendedError = TRUE;
4670 else
4671 RESET_CFLAG(context);
4672 break;
4674 case 0x44: /* IOCTL */
4675 INT21_Ioctl( context );
4676 break;
4678 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4679 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context) );
4681 HANDLE handle32;
4682 HFILE handle16 = HFILE_ERROR;
4684 if (DuplicateHandle( GetCurrentProcess(),
4685 DosFileHandleToWin32Handle(BX_reg(context)),
4686 GetCurrentProcess(),
4687 &handle32,
4688 0, TRUE, DUPLICATE_SAME_ACCESS ))
4689 handle16 = Win32HandleToDosFileHandle(handle32);
4691 if (handle16 == HFILE_ERROR)
4692 bSetDOSExtendedError = TRUE;
4693 else
4695 SET_AX( context, handle16 );
4696 RESET_CFLAG(context);
4699 break;
4701 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4702 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4703 BX_reg(context), CX_reg(context) );
4704 if (!INT21_Dup2(BX_reg(context), CX_reg(context)))
4705 bSetDOSExtendedError = TRUE;
4706 else
4707 RESET_CFLAG(context);
4708 break;
4710 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4711 if (!INT21_GetCurrentDirectory( context, FALSE ))
4712 bSetDOSExtendedError = TRUE;
4713 else
4714 RESET_CFLAG(context);
4715 break;
4717 case 0x48: /* ALLOCATE MEMORY */
4718 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
4720 DWORD bytes = (DWORD)BX_reg(context) << 4;
4721 DWORD rv = GlobalDOSAlloc16( bytes );
4722 WORD selector = LOWORD( rv );
4724 if (selector)
4726 SET_AX( context, selector );
4727 RESET_CFLAG(context);
4729 else
4731 SET_CFLAG(context);
4732 SET_AX( context, 0x0008 ); /* insufficient memory */
4733 SET_BX( context, DOSMEM_Available() >> 4 );
4736 break;
4738 case 0x49: /* FREE MEMORY */
4739 TRACE( "FREE MEMORY segment %04X\n", context->SegEs );
4741 BOOL ok = !GlobalDOSFree16( context->SegEs );
4743 /* If we don't reset ES_reg, we will fail in the relay code */
4744 if (ok) context->SegEs = 0;
4745 else
4747 TRACE("FREE MEMORY failed\n");
4748 SET_CFLAG(context);
4749 SET_AX( context, 0x0009 ); /* memory block address invalid */
4752 break;
4754 case 0x4a: /* RESIZE MEMORY BLOCK */
4755 TRACE( "RESIZE MEMORY segment %04X to %d paragraphs\n",
4756 context->SegEs, BX_reg(context) );
4758 FIXME( "Resize memory block - unsupported under Win16\n" );
4759 SET_CFLAG(context);
4761 break;
4763 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4765 char *program = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4766 HINSTANCE16 instance;
4768 TRACE( "EXEC %s\n", program );
4770 RESET_CFLAG(context);
4771 instance = WinExec16( program, SW_NORMAL );
4772 if (instance < 32)
4774 SET_CFLAG( context );
4775 SET_AX( context, instance );
4778 break;
4780 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
4781 TRACE( "EXIT with return code %d\n", AL_reg(context) );
4782 DOSVM_Exit( AL_reg(context) );
4783 break;
4785 case 0x4d: /* GET RETURN CODE */
4786 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
4787 SET_AX( context, DOSVM_retval );
4788 DOSVM_retval = 0;
4789 break;
4791 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
4792 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
4793 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
4794 if (!INT21_FindFirst(context)) break;
4795 /* fall through */
4797 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
4798 TRACE("FINDNEXT\n");
4799 if (!INT21_FindNext(context))
4801 SetLastError( ERROR_NO_MORE_FILES );
4802 SET_AX( context, ERROR_NO_MORE_FILES );
4803 SET_CFLAG(context);
4805 else SET_AX( context, 0 ); /* OK */
4806 break;
4808 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
4809 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
4810 DOSVM_psp = BX_reg(context);
4811 break;
4813 case 0x51: /* GET PSP ADDRESS */
4814 INT21_GetPSP( context );
4815 break;
4817 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
4818 TRACE("Get List of Lists - not supported\n");
4819 context->SegEs = 0;
4820 SET_BX( context, 0 );
4821 break;
4823 case 0x54: /* Get Verify Flag */
4824 TRACE("Get Verify Flag - Not Supported\n");
4825 SET_AL( context, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
4826 break;
4828 case 0x56: /* "RENAME" - RENAME FILE */
4829 if (!INT21_RenameFile( context ))
4830 bSetDOSExtendedError = TRUE;
4831 else
4832 RESET_CFLAG(context);
4833 break;
4835 case 0x57: /* FILE DATE AND TIME */
4836 if (!INT21_FileDateTime( context ))
4837 bSetDOSExtendedError = TRUE;
4838 else
4839 RESET_CFLAG(context);
4840 break;
4842 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
4843 switch (AL_reg(context))
4845 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
4846 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
4847 SET_AX( context, 0 ); /* low memory first fit */
4848 break;
4850 case 0x01: /* SET ALLOCATION STRATEGY */
4851 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
4852 BL_reg(context) );
4853 break;
4855 case 0x02: /* GET UMB LINK STATE */
4856 TRACE( "GET UMB LINK STATE\n" );
4857 SET_AL( context, 0 ); /* UMBs not part of DOS memory chain */
4858 break;
4860 case 0x03: /* SET UMB LINK STATE */
4861 TRACE( "SET UMB LINK STATE to %d - ignored\n",
4862 BX_reg(context) );
4863 break;
4865 default:
4866 INT_BARF( context, 0x21 );
4868 break;
4870 case 0x59: /* GET EXTENDED ERROR INFO */
4871 INT21_GetExtendedError( context );
4872 break;
4874 case 0x5a: /* CREATE TEMPORARY FILE */
4875 TRACE("CREATE TEMPORARY FILE\n");
4876 bSetDOSExtendedError = !INT21_CreateTempFile(context);
4877 break;
4879 case 0x5b: /* CREATE NEW FILE */
4880 if (!INT21_CreateFile( context, context->Edx, FALSE,
4881 OF_READWRITE | OF_SHARE_COMPAT, 0x10 ))
4882 bSetDOSExtendedError = TRUE;
4883 else
4884 RESET_CFLAG(context);
4885 break;
4887 case 0x5c: /* "FLOCK" - RECORD LOCKING */
4889 DWORD offset = MAKELONG(DX_reg(context), CX_reg(context));
4890 DWORD length = MAKELONG(DI_reg(context), SI_reg(context));
4891 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4893 RESET_CFLAG(context);
4894 switch (AL_reg(context))
4896 case 0x00: /* LOCK */
4897 TRACE( "lock handle %d offset %d length %d\n",
4898 BX_reg(context), offset, length );
4899 if (!LockFile( handle, offset, 0, length, 0 ))
4900 bSetDOSExtendedError = TRUE;
4901 break;
4903 case 0x01: /* UNLOCK */
4904 TRACE( "unlock handle %d offset %d length %d\n",
4905 BX_reg(context), offset, length );
4906 if (!UnlockFile( handle, offset, 0, length, 0 ))
4907 bSetDOSExtendedError = TRUE;
4908 break;
4910 default:
4911 INT_BARF( context, 0x21 );
4914 break;
4916 case 0x5d: /* NETWORK 5D */
4917 FIXME( "Network function 5D not implemented.\n" );
4918 SetLastError( ER_NoNetwork );
4919 bSetDOSExtendedError = TRUE;
4920 break;
4922 case 0x5e: /* NETWORK 5E */
4923 bSetDOSExtendedError = INT21_NetworkFunc( context);
4924 break;
4926 case 0x5f: /* NETWORK 5F */
4927 /* FIXME: supporting this would need to 1:
4928 * - implement per drive current directory (as kernel32 doesn't)
4929 * - assign enabled/disabled flag on a per drive basis
4931 /* network software not installed */
4932 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
4933 SetLastError( ER_NoNetwork );
4934 bSetDOSExtendedError = TRUE;
4935 break;
4937 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
4939 WCHAR pathW[MAX_PATH], res[MAX_PATH];
4940 /* FIXME: likely to be broken */
4942 TRACE("TRUENAME %s\n",
4943 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
4944 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
4945 if (!GetFullPathNameW( pathW, 128, res, NULL ))
4946 bSetDOSExtendedError = TRUE;
4947 else
4949 SET_AX( context, 0 );
4950 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
4951 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
4952 128, NULL, NULL);
4955 break;
4957 case 0x61: /* UNUSED */
4958 SET_AL( context, 0 );
4959 break;
4961 case 0x62: /* GET PSP ADDRESS */
4962 INT21_GetPSP( context );
4963 break;
4965 case 0x63: /* MISC. LANGUAGE SUPPORT */
4966 switch (AL_reg(context)) {
4967 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
4968 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
4969 context->SegDs = INT21_GetHeapSelector( context );
4970 SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
4971 SET_AL( context, 0 ); /* success */
4972 break;
4974 break;
4976 case 0x64: /* OS/2 DOS BOX */
4977 INT_BARF( context, 0x21 );
4978 SET_CFLAG(context);
4979 break;
4981 case 0x65: /* EXTENDED COUNTRY INFORMATION */
4982 INT21_ExtendedCountryInformation( context );
4983 break;
4985 case 0x66: /* GLOBAL CODE PAGE TABLE */
4986 switch (AL_reg(context))
4988 case 0x01:
4989 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
4990 SET_BX( context, GetOEMCP() );
4991 SET_DX( context, GetOEMCP() );
4992 break;
4993 case 0x02:
4994 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
4995 BX_reg(context), DX_reg(context) );
4996 break;
4998 break;
5000 case 0x67: /* SET HANDLE COUNT */
5001 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
5002 if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
5003 bSetDOSExtendedError = TRUE;
5004 break;
5006 case 0x68: /* "FFLUSH" - COMMIT FILE */
5007 TRACE( "FFLUSH - handle %d\n", BX_reg(context) );
5008 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ) &&
5009 GetLastError() != ERROR_ACCESS_DENIED)
5010 bSetDOSExtendedError = TRUE;
5011 break;
5013 case 0x69: /* DISK SERIAL NUMBER */
5014 switch (AL_reg(context))
5016 case 0x00:
5017 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
5018 INT21_DriveName(BL_reg(context)));
5019 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
5020 else SET_AX( context, 0 );
5021 break;
5023 case 0x01:
5024 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
5025 INT21_DriveName(BL_reg(context)));
5026 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
5027 else SET_AX( context, 1 );
5028 break;
5030 break;
5032 case 0x6a: /* COMMIT FILE */
5033 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context) );
5034 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ) &&
5035 GetLastError() != ERROR_ACCESS_DENIED)
5036 bSetDOSExtendedError = TRUE;
5037 break;
5039 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
5040 SET_AL( context, 0 );
5041 break;
5043 case 0x6c: /* EXTENDED OPEN/CREATE */
5044 if (!INT21_CreateFile( context, context->Esi, TRUE,
5045 BX_reg(context), DL_reg(context) ))
5046 bSetDOSExtendedError = TRUE;
5047 break;
5049 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
5050 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
5051 SET_CFLAG( context );
5052 SET_AL( context, 0 );
5053 break;
5055 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
5056 INT21_LongFilename( context );
5057 break;
5059 case 0x73: /* MSDOS7 - FAT32 */
5060 RESET_CFLAG( context );
5061 if (!INT21_Fat32( context ))
5062 bSetDOSExtendedError = TRUE;
5063 break;
5065 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
5066 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
5067 break;
5069 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
5070 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
5071 break;
5073 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
5074 /* we don't implement a DOS32 extender */
5075 TRACE( "DOS32 EXTENDER API - ignored\n" );
5076 break;
5078 default:
5079 INT_BARF( context, 0x21 );
5080 break;
5082 } /* END OF SWITCH */
5084 /* Set general error condition. */
5085 if (bSetDOSExtendedError)
5087 SET_AX( context, GetLastError() );
5088 SET_CFLAG( context );
5091 /* Print error code if carry flag is set. */
5092 if (context->EFlags & 0x0001)
5093 TRACE("failed, error %d\n", GetLastError() );
5095 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
5096 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
5097 AX_reg(context), BX_reg(context),
5098 CX_reg(context), DX_reg(context),
5099 SI_reg(context), DI_reg(context),
5100 (WORD)context->SegDs, (WORD)context->SegEs,
5101 context->EFlags );