setupapi: When freeing an interface, delete it from the registry if its device is...
[wine/multimedia.git] / dlls / winedos / int21.c
blob7e080e0d033b4d301452fdacfbc360d0f7fbedf6
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 "dosexe.h"
44 #include "winerror.h"
45 #include "winuser.h"
46 #include "wine/unicode.h"
47 #include "wine/server.h"
48 #include "wine/debug.h"
49 #include "wine/exception.h"
51 BOOL WINAPI VerifyConsoleIoHandle(HANDLE);
53 * Note:
54 * - Most of the file related functions are wrong. NT's kernel32
55 * doesn't maintain a per drive current directory, while DOS does.
56 * We should in fact keep track in here of those per drive
57 * directories, and use this info while dealing with partial paths
58 * (drive defined, but only relative paths). This could even be
59 * created as an array of CDS (there should be an entry for that in
60 * the LOL)
64 * Forward declarations.
66 static BOOL INT21_RenameFile( CONTEXT86 *context );
68 WINE_DEFAULT_DEBUG_CHANNEL(int21);
71 #include "pshpack1.h"
74 * Extended Drive Parameter Block.
75 * This structure is compatible with standard DOS4+ DPB and
76 * extended DOS7 DPB.
78 typedef struct _INT21_DPB {
79 BYTE drive; /* 00 drive number (0=A, ...) */
80 BYTE unit; /* 01 unit number within device driver */
81 WORD sector_bytes; /* 02 bytes per sector */
82 BYTE cluster_sectors; /* 04 highest sector number within a cluster */
83 BYTE shift; /* 05 shift count to convert clusters into sectors */
84 WORD num_reserved; /* 06 reserved sectors at beginning of drive */
85 BYTE num_FAT; /* 08 number of FATs */
86 WORD num_root_entries; /* 09 number of root directory entries */
87 WORD first_data_sector; /* 0b number of first sector containing user data */
88 WORD num_clusters1; /* 0d highest cluster number (number of data clusters + 1) */
89 WORD sectors_per_FAT; /* 0f number of sectors per FAT */
90 WORD first_dir_sector; /* 11 sector number of first directory sector */
91 SEGPTR driver_header; /* 13 address of device driver header */
92 BYTE media_ID; /* 17 media ID byte */
93 BYTE access_flag; /* 18 0x00 if disk accessed, 0xff if not */
94 SEGPTR next; /* 19 pointer to next DPB */
95 WORD search_cluster1; /* 1d cluster at which to start search for free space */
96 WORD free_clusters_lo; /* 1f number of free clusters on drive or 0xffff if unknown */
97 WORD free_clusters_hi; /* 21 hiword of clusters_free */
98 WORD mirroring_flags; /* 23 active FAT/mirroring flags */
99 WORD info_sector; /* 25 sector number of file system info sector or 0xffff for none */
100 WORD spare_boot_sector; /* 27 sector number of backup boot sector or 0xffff for none */
101 DWORD first_cluster_sector; /* 29 sector number of the first cluster */
102 DWORD num_clusters2; /* 2d maximum cluster number */
103 DWORD fat_clusters; /* 31 number of clusters occupied by FAT */
104 DWORD root_cluster; /* 35 cluster number of start of root directory */
105 DWORD search_cluster2; /* 39 cluster at which to start searching for free space */
106 } INT21_DPB;
110 * Structure for DOS data that can be accessed directly from applications.
111 * Real and protected mode pointers will be returned to this structure so
112 * the structure must be correctly packed.
114 typedef struct _INT21_HEAP {
115 WORD uppercase_size; /* Size of the following table in bytes */
116 BYTE uppercase_table[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
118 WORD lowercase_size; /* Size of the following table in bytes */
119 BYTE lowercase_table[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
121 WORD collating_size; /* Size of the following table in bytes */
122 BYTE collating_table[256]; /* Values used to sort characters from 0x00 to 0xff. */
124 WORD filename_size; /* Size of the following filename data in bytes */
125 BYTE filename_reserved1; /* 0x01 for MS-DOS 3.30-6.00 */
126 BYTE filename_lowest; /* Lowest permissible character value for filename */
127 BYTE filename_highest; /* Highest permissible character value for filename */
128 BYTE filename_reserved2; /* 0x00 for MS-DOS 3.30-6.00 */
129 BYTE filename_exclude_first; /* First illegal character in permissible range */
130 BYTE filename_exclude_last; /* Last illegal character in permissible range */
131 BYTE filename_reserved3; /* 0x02 for MS-DOS 3.30-6.00 */
132 BYTE filename_illegal_size; /* Number of terminators in the following table */
133 BYTE filename_illegal_table[16]; /* Characters which terminate a filename */
135 WORD dbcs_size; /* Number of valid ranges in the following table */
136 BYTE dbcs_table[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
138 BYTE misc_indos; /* Interrupt 21 nesting flag */
139 WORD misc_segment; /* Real mode segment for INT21_HEAP */
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
266 struct magic_device
268 WCHAR name[10];
269 HANDLE handle;
270 LARGE_INTEGER index;
271 void (*ioctl_handler)(CONTEXT86 *);
274 static void INT21_IoctlScsiMgrHandler( CONTEXT86 * );
275 static void INT21_IoctlEMSHandler( CONTEXT86 * );
276 static void INT21_IoctlHPScanHandler( CONTEXT86 * );
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 } }, INT21_IoctlEMSHandler },
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[3] = {'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[3] = {'A', ':', 0};
363 drivespec[0] += drive;
365 if (!SetCurrentDirectoryW( drivespec ))
366 TRACE( "Failed to set current drive.\n" );
370 /***********************************************************************
371 * INT21_ReadChar
373 * Reads a character from the standard input.
374 * Extended keycodes will be returned as two separate characters.
376 static BOOL INT21_ReadChar( BYTE *input, CONTEXT86 *waitctx )
378 static BYTE pending_scan = 0;
380 if (pending_scan)
382 if (input)
383 *input = pending_scan;
384 if (waitctx)
385 pending_scan = 0;
386 return TRUE;
388 else
390 BYTE ascii;
391 BYTE scan;
392 if (!DOSVM_Int16ReadChar( &ascii, &scan, waitctx ))
393 return FALSE;
395 if (input)
396 *input = ascii;
397 if (waitctx && !ascii)
398 pending_scan = scan;
399 return TRUE;
404 /***********************************************************************
405 * INT21_GetSystemCountryCode
407 * Return DOS country code for default system locale.
409 static WORD INT21_GetSystemCountryCode( void )
412 * FIXME: Determine country code. We should probably use
413 * DOSCONF structure for that.
415 return GetSystemDefaultLangID();
419 /***********************************************************************
420 * INT21_FillCountryInformation
422 * Fill 34-byte buffer with country information data using
423 * default system locale.
425 static void INT21_FillCountryInformation( BYTE *buffer )
427 /* 00 - WORD: date format
428 * 00 = mm/dd/yy
429 * 01 = dd/mm/yy
430 * 02 = yy/mm/dd
432 *(WORD*)(buffer + 0) = 0; /* FIXME: Get from locale */
434 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
435 buffer[2] = '$'; /* FIXME: Get from locale */
436 buffer[3] = 0;
438 /* 07 - BYTE[2]: ASCIIZ thousands separator */
439 buffer[7] = 0; /* FIXME: Get from locale */
440 buffer[8] = 0;
442 /* 09 - BYTE[2]: ASCIIZ decimal separator */
443 buffer[9] = '.'; /* FIXME: Get from locale */
444 buffer[10] = 0;
446 /* 11 - BYTE[2]: ASCIIZ date separator */
447 buffer[11] = '/'; /* FIXME: Get from locale */
448 buffer[12] = 0;
450 /* 13 - BYTE[2]: ASCIIZ time separator */
451 buffer[13] = ':'; /* FIXME: Get from locale */
452 buffer[14] = 0;
454 /* 15 - BYTE: Currency format
455 * bit 2 = set if currency symbol replaces decimal point
456 * bit 1 = number of spaces between value and currency symbol
457 * bit 0 = 0 if currency symbol precedes value
458 * 1 if currency symbol follows value
460 buffer[15] = 0; /* FIXME: Get from locale */
462 /* 16 - BYTE: Number of digits after decimal in currency */
463 buffer[16] = 0; /* FIXME: Get from locale */
465 /* 17 - BYTE: Time format
466 * bit 0 = 0 if 12-hour clock
467 * 1 if 24-hour clock
469 buffer[17] = 1; /* FIXME: Get from locale */
471 /* 18 - DWORD: Address of case map routine */
472 *(DWORD*)(buffer + 18) = 0; /* FIXME: ptr to case map routine */
474 /* 22 - BYTE[2]: ASCIIZ data-list separator */
475 buffer[22] = ','; /* FIXME: Get from locale */
476 buffer[23] = 0;
478 /* 24 - BYTE[10]: Reserved */
479 memset( buffer + 24, 0, 10 );
483 /***********************************************************************
484 * INT21_FillHeap
486 * Initialize DOS heap.
488 * Filename Terminator Table of w2k DE NTVDM:
489 * 16 00 01 00 FF 00 00 20-02 0E 2E 22 2F 5C 5B 5D ....... ..."/\[]
490 * 3A 7C 3C 3E 2B 3D 3B 2C-00 :|<>+=;,
492 static void INT21_FillHeap( INT21_HEAP *heap )
494 static const char terminators[] = ".\"/\\[]:|<>+=;,";
495 int i;
498 * Uppercase table.
500 heap->uppercase_size = 128;
501 for (i = 0; i < 128; i++)
502 heap->uppercase_table[i] = toupper( 128 + i );
505 * Lowercase table.
507 heap->lowercase_size = 256;
508 for (i = 0; i < 256; i++)
509 heap->lowercase_table[i] = tolower( i );
512 * Collating table.
514 heap->collating_size = 256;
515 for (i = 0; i < 256; i++)
516 heap->collating_table[i] = i;
519 * Filename table.
521 heap->filename_size = 8 + strlen(terminators);
522 heap->filename_illegal_size = strlen(terminators);
523 memcpy( heap->filename_illegal_table, terminators, heap->filename_illegal_size );
525 heap->filename_reserved1 = 0x01;
526 heap->filename_lowest = 0x00;
527 heap->filename_highest = 0xff;
528 heap->filename_reserved2 = 0x00;
529 heap->filename_exclude_first = 0x00;
530 heap->filename_exclude_last = 0x20;
531 heap->filename_reserved3 = 0x02;
534 * DBCS lead byte table. This table is empty.
536 heap->dbcs_size = 0;
537 memset( heap->dbcs_table, 0, sizeof(heap->dbcs_table) );
540 * Initialize InDos flag.
542 heap->misc_indos = 0;
545 * FIXME: Should drive parameter blocks (DPB) be
546 * initialized here and linked to DOS LOL?
551 /***********************************************************************
552 * INT21_GetHeapPointer
554 * Get pointer for DOS heap (INT21_HEAP).
555 * Creates and initializes heap on first call.
557 static INT21_HEAP *INT21_GetHeapPointer( void )
559 static INT21_HEAP *heap_pointer = NULL;
561 if (!heap_pointer)
563 WORD heap_segment;
564 WORD heap_selector;
566 heap_pointer = DOSVM_AllocDataUMB( sizeof(INT21_HEAP),
567 &heap_segment,
568 &heap_selector );
570 heap_pointer->misc_segment = heap_segment;
571 heap_pointer->misc_selector = heap_selector;
572 INT21_FillHeap( heap_pointer );
575 return heap_pointer;
579 /***********************************************************************
580 * INT21_GetHeapSelector
582 * Get segment/selector for DOS heap (INT21_HEAP).
583 * Creates and initializes heap on first call.
585 static WORD INT21_GetHeapSelector( CONTEXT86 *context )
587 INT21_HEAP *heap = INT21_GetHeapPointer();
589 if (!ISV86(context) && DOSVM_IsWin16())
590 return heap->misc_selector;
591 else
592 return heap->misc_segment;
596 /***********************************************************************
597 * INT21_FillDrivePB
599 * Fill DOS heap drive parameter block for the specified drive.
600 * Return TRUE if drive was valid and there were
601 * no errors while reading drive information.
603 static BOOL INT21_FillDrivePB( BYTE drive )
605 WCHAR drivespec[3] = {'A', ':', 0};
606 INT21_HEAP *heap = INT21_GetHeapPointer();
607 INT21_DPB *dpb;
608 UINT drivetype;
609 DWORD cluster_sectors;
610 DWORD sector_bytes;
611 DWORD free_clusters;
612 DWORD total_clusters;
614 if (drive >= MAX_DOS_DRIVES)
615 return FALSE;
617 dpb = &heap->misc_dpb_list[drive];
618 drivespec[0] += drive;
619 drivetype = GetDriveTypeW( drivespec );
622 * FIXME: Does this check work correctly with floppy/cdrom drives?
624 if (drivetype == DRIVE_NO_ROOT_DIR || drivetype == DRIVE_UNKNOWN)
625 return FALSE;
628 * FIXME: Does this check work correctly with floppy/cdrom drives?
630 if (!GetDiskFreeSpaceW( drivespec, &cluster_sectors, &sector_bytes,
631 &free_clusters, &total_clusters ))
632 return FALSE;
635 * FIXME: Most of the values listed below are incorrect.
636 * All values should be validated.
639 dpb->drive = drive;
640 dpb->unit = 0;
641 dpb->sector_bytes = sector_bytes;
642 dpb->cluster_sectors = cluster_sectors - 1;
644 dpb->shift = 0;
645 while (cluster_sectors > 1)
647 cluster_sectors /= 2;
648 dpb->shift++;
651 dpb->num_reserved = 0;
652 dpb->num_FAT = 1;
653 dpb->num_root_entries = 2;
654 dpb->first_data_sector = 2;
655 dpb->num_clusters1 = total_clusters;
656 dpb->sectors_per_FAT = 1;
657 dpb->first_dir_sector = 1;
658 dpb->driver_header = 0;
659 dpb->media_ID = (drivetype == DRIVE_FIXED) ? 0xF8 : 0xF0;
660 dpb->access_flag = 0;
661 dpb->next = 0;
662 dpb->search_cluster1 = 0;
663 dpb->free_clusters_lo = LOWORD(free_clusters);
664 dpb->free_clusters_hi = HIWORD(free_clusters);
665 dpb->mirroring_flags = 0;
666 dpb->info_sector = 0xffff;
667 dpb->spare_boot_sector = 0xffff;
668 dpb->first_cluster_sector = 0;
669 dpb->num_clusters2 = total_clusters;
670 dpb->fat_clusters = 32;
671 dpb->root_cluster = 0;
672 dpb->search_cluster2 = 0;
674 return TRUE;
678 /***********************************************************************
679 * INT21_GetCurrentDirectory
681 * Handler for:
682 * - function 0x47
683 * - subfunction 0x47 of function 0x71
685 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context, BOOL islong )
687 char *buffer = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi);
688 BYTE drive = INT21_MapDrive( DL_reg(context) );
689 WCHAR pathW[MAX_PATH];
690 char pathA[MAX_PATH];
691 WCHAR *ptr = pathW;
693 TRACE( "drive %d\n", DL_reg(context) );
695 if (drive == MAX_DOS_DRIVES)
697 SetLastError(ERROR_INVALID_DRIVE);
698 return FALSE;
702 * Grab current directory.
705 if (!GetCurrentDirectoryW( MAX_PATH, pathW )) return FALSE;
707 if (toupperW(pathW[0]) - 'A' != drive || pathW[1] != ':')
709 /* cwd is not on the requested drive, get the environment string instead */
711 WCHAR env_var[4];
713 env_var[0] = '=';
714 env_var[1] = 'A' + drive;
715 env_var[2] = ':';
716 env_var[3] = 0;
717 if (!GetEnvironmentVariableW( env_var, pathW, MAX_PATH ))
719 /* return empty path */
720 buffer[0] = 0;
721 return TRUE;
726 * Convert into short format.
729 if (!islong)
731 DWORD result = GetShortPathNameW( pathW, pathW, MAX_PATH );
732 if (!result)
733 return FALSE;
734 if (result > MAX_PATH)
736 WARN( "Short path too long!\n" );
737 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
738 return FALSE;
743 * The returned pathname does not include
744 * the drive letter, colon or leading backslash.
747 if (ptr[0] == '\\')
750 * FIXME: We should probably just strip host part from name...
752 FIXME( "UNC names are not supported.\n" );
753 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
754 return FALSE;
756 else if (!ptr[0] || ptr[1] != ':' || ptr[2] != '\\')
758 WARN( "Path is neither UNC nor DOS path: %s\n",
759 wine_dbgstr_w(ptr) );
760 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
761 return FALSE;
763 else
765 /* Remove drive letter, colon and leading backslash. */
766 ptr += 3;
770 * Convert into OEM string.
773 if (!WideCharToMultiByte(CP_OEMCP, 0, ptr, -1, pathA,
774 MAX_PATH, NULL, NULL))
776 WARN( "Cannot convert path!\n" );
777 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
778 return FALSE;
782 * Success.
785 if (!islong)
787 /* Undocumented success code. */
788 SET_AX( context, 0x0100 );
790 /* Truncate buffer to 64 bytes. */
791 pathA[63] = 0;
794 TRACE( "%c:=%s\n", 'A' + drive, pathA );
796 strcpy( buffer, pathA );
797 return TRUE;
801 /***********************************************************************
802 * INT21_SetCurrentDirectory
804 * Handler for:
805 * - function 0x3b
806 * - subfunction 0x3b of function 0x71
808 static BOOL INT21_SetCurrentDirectory( CONTEXT86 *context )
810 WCHAR dirW[MAX_PATH];
811 WCHAR env_var[4];
812 DWORD attr;
813 char *dirA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
814 BYTE drive = INT21_GetCurrentDrive();
815 BOOL result;
817 TRACE( "SET CURRENT DIRECTORY %s\n", dirA );
819 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
820 if (!GetFullPathNameW( dirW, MAX_PATH, dirW, NULL )) return FALSE;
822 attr = GetFileAttributesW( dirW );
823 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
825 SetLastError( ERROR_PATH_NOT_FOUND );
826 return FALSE;
829 env_var[0] = '=';
830 env_var[1] = dirW[0];
831 env_var[2] = ':';
832 env_var[3] = 0;
833 result = SetEnvironmentVariableW( env_var, dirW );
835 /* only set current directory if on the current drive */
836 if (result && (toupperW(dirW[0]) - 'A' == drive)) result = SetCurrentDirectoryW( dirW );
838 return result;
841 /***********************************************************************
842 * INT21_CreateMagicDeviceHandle
844 * Create a dummy file handle for a "magic" device.
846 static HANDLE INT21_CreateMagicDeviceHandle( LPCWSTR name )
848 static const WCHAR prefixW[] = {'\\','?','?','\\','u','n','i','x'};
849 const char *dir = wine_get_server_dir();
850 int len;
851 HANDLE ret;
852 NTSTATUS status;
853 OBJECT_ATTRIBUTES attr;
854 UNICODE_STRING nameW;
855 IO_STATUS_BLOCK io;
857 len = MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, NULL, 0 );
858 nameW.Length = sizeof(prefixW) + (len + strlenW( name )) * sizeof(WCHAR);
859 nameW.MaximumLength = nameW.Length + sizeof(WCHAR);
860 if (!(nameW.Buffer = HeapAlloc( GetProcessHeap(), 0, nameW.MaximumLength )))
862 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
863 return 0;
865 memcpy( nameW.Buffer, prefixW, sizeof(prefixW) );
866 MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, nameW.Buffer + sizeof(prefixW)/sizeof(WCHAR), len );
867 len += sizeof(prefixW) / sizeof(WCHAR);
868 nameW.Buffer[len-1] = '/';
869 strcpyW( nameW.Buffer + len, name );
871 attr.Length = sizeof(attr);
872 attr.RootDirectory = 0;
873 attr.Attributes = 0;
874 attr.ObjectName = &nameW;
875 attr.SecurityDescriptor = NULL;
876 attr.SecurityQualityOfService = NULL;
878 status = NtCreateFile( &ret, GENERIC_READ|GENERIC_WRITE, &attr, &io, NULL, 0,
879 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF,
880 FILE_SYNCHRONOUS_IO_ALERT, NULL, 0 );
881 if (status)
883 ret = 0;
884 SetLastError( RtlNtStatusToDosError(status) );
886 RtlFreeUnicodeString( &nameW );
887 return ret;
891 /***********************************************************************
892 * INT21_OpenMagicDevice
894 * Open a file handle for "magic" devices like EMMXXXX0.
896 static HANDLE INT21_OpenMagicDevice( LPCWSTR name, DWORD access )
898 unsigned int i;
899 const WCHAR *p;
900 HANDLE handle;
902 if (name[0] && (name[1] == ':')) name += 2;
903 if ((p = strrchrW( name, '/' ))) name = p + 1;
904 if ((p = strrchrW( name, '\\' ))) name = p + 1;
906 for (i = 0; i < NB_MAGIC_DEVICES; i++)
908 int len = strlenW( magic_devices[i].name );
909 if (!strncmpiW( magic_devices[i].name, name, len ) &&
910 (!name[len] || name[len] == '.' || name[len] == ':')) break;
912 if (i == NB_MAGIC_DEVICES) return 0;
914 if (!magic_devices[i].handle) /* need to open it */
916 IO_STATUS_BLOCK io;
917 FILE_INTERNAL_INFORMATION info;
919 if (!(handle = INT21_CreateMagicDeviceHandle( magic_devices[i].name ))) return 0;
921 NtQueryInformationFile( handle, &io, &info, sizeof(info), FileInternalInformation );
922 magic_devices[i].index = info.IndexNumber;
923 magic_devices[i].handle = handle;
925 if (!DuplicateHandle( GetCurrentProcess(), magic_devices[i].handle,
926 GetCurrentProcess(), &handle, access, FALSE, 0 )) handle = 0;
927 return handle;
931 /***********************************************************************
932 * INT21_CreateFile
934 * Handler for:
935 * - function 0x3c
936 * - function 0x3d
937 * - function 0x5b
938 * - function 0x6c
939 * - subfunction 0x6c of function 0x71
941 static BOOL INT21_CreateFile( CONTEXT86 *context,
942 DWORD pathSegOff,
943 BOOL returnStatus,
944 WORD dosAccessShare,
945 BYTE dosAction )
947 WORD dosStatus;
948 char *pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, pathSegOff);
949 WCHAR pathW[MAX_PATH];
950 DWORD winAccess;
951 DWORD winAttributes;
952 HANDLE winHandle;
953 DWORD winMode;
954 DWORD winSharing;
956 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
957 "create flags=%04x, file=%s.\n",
958 AH_reg(context), dosAction, dosAccessShare, CX_reg(context), pathA );
961 * Application tried to create/open a file whose name
962 * ends with a backslash. This is not allowed.
964 * FIXME: This needs to be validated, especially the return value.
966 if (pathA[strlen(pathA) - 1] == '/')
968 SetLastError( ERROR_FILE_NOT_FOUND );
969 return FALSE;
973 * Convert DOS action flags into Win32 creation disposition parameter.
975 switch(dosAction)
977 case 0x01:
978 winMode = OPEN_EXISTING;
979 break;
980 case 0x02:
981 winMode = TRUNCATE_EXISTING;
982 break;
983 case 0x10:
984 winMode = CREATE_NEW;
985 break;
986 case 0x11:
987 winMode = OPEN_ALWAYS;
988 break;
989 case 0x12:
990 winMode = CREATE_ALWAYS;
991 break;
992 default:
993 SetLastError( ERROR_INVALID_PARAMETER );
994 return FALSE;
998 * Convert DOS access/share flags into Win32 desired access parameter.
1000 switch(dosAccessShare & 0x07)
1002 case OF_READ:
1003 winAccess = GENERIC_READ;
1004 break;
1005 case OF_WRITE:
1006 winAccess = GENERIC_WRITE;
1007 break;
1008 case OF_READWRITE:
1009 winAccess = GENERIC_READ | GENERIC_WRITE;
1010 break;
1011 case 0x04:
1013 * Read-only, do not modify file's last-access time (DOS7).
1015 * FIXME: How to prevent modification of last-access time?
1017 winAccess = GENERIC_READ;
1018 break;
1019 default:
1020 winAccess = 0;
1024 * Convert DOS access/share flags into Win32 share mode parameter.
1026 switch(dosAccessShare & 0x70)
1028 case OF_SHARE_EXCLUSIVE:
1029 winSharing = 0;
1030 break;
1031 case OF_SHARE_DENY_WRITE:
1032 winSharing = FILE_SHARE_READ;
1033 break;
1034 case OF_SHARE_DENY_READ:
1035 winSharing = FILE_SHARE_WRITE;
1036 break;
1037 case OF_SHARE_DENY_NONE:
1038 case OF_SHARE_COMPAT:
1039 default:
1040 winSharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1044 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
1045 * What to do with this bit?
1046 * FIXME: Bits in the high byte of dosAccessShare are not supported.
1047 * See both function 0x6c and subfunction 0x6c of function 0x71 for
1048 * definition of these bits.
1052 * Convert DOS create attributes into Win32 flags and attributes parameter.
1054 if (winMode == OPEN_EXISTING || winMode == TRUNCATE_EXISTING)
1056 winAttributes = 0;
1058 else
1060 WORD dosAttributes = CX_reg(context);
1062 if (dosAttributes & FA_LABEL)
1065 * Application tried to create volume label entry.
1066 * This is difficult to support so we do not allow it.
1068 * FIXME: If volume does not already have a label,
1069 * this function is supposed to succeed.
1071 SetLastError( ERROR_ACCESS_DENIED );
1072 return TRUE;
1075 winAttributes = dosAttributes &
1076 (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
1077 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE);
1081 * Open the file.
1083 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
1085 if ((winHandle = INT21_OpenMagicDevice( pathW, winAccess )))
1087 dosStatus = 1;
1089 else
1091 winHandle = CreateFileW( pathW, winAccess, winSharing, NULL,
1092 winMode, winAttributes, 0 );
1093 /* DOS allows to open files on a CDROM R/W */
1094 if( winHandle == INVALID_HANDLE_VALUE &&
1095 GetLastError()== ERROR_WRITE_PROTECT) {
1096 winHandle = CreateFileW( pathW, winAccess & ~GENERIC_WRITE,
1097 winSharing, NULL, winMode, winAttributes, 0 );
1100 if (winHandle == INVALID_HANDLE_VALUE)
1101 return FALSE;
1104 * Determine DOS file status.
1106 * 1 = file opened
1107 * 2 = file created
1108 * 3 = file replaced
1110 switch(winMode)
1112 case OPEN_EXISTING:
1113 dosStatus = 1;
1114 break;
1115 case TRUNCATE_EXISTING:
1116 dosStatus = 3;
1117 break;
1118 case CREATE_NEW:
1119 dosStatus = 2;
1120 break;
1121 case OPEN_ALWAYS:
1122 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 1 : 2;
1123 break;
1124 case CREATE_ALWAYS:
1125 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 3 : 2;
1126 break;
1127 default:
1128 dosStatus = 0;
1133 * Return DOS file handle and DOS status.
1135 SET_AX( context, Win32HandleToDosFileHandle(winHandle) );
1137 if (returnStatus)
1138 SET_CX( context, dosStatus );
1140 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
1141 AX_reg(context), dosStatus );
1143 return TRUE;
1147 /***********************************************************************
1148 * INT21_BufferedInput
1150 * Handler for function 0x0a and reading from console using
1151 * function 0x3f.
1153 * Reads a string of characters from standard input until
1154 * enter key is pressed. Returns either number of characters
1155 * read from console including terminating CR or
1156 * zero if capacity was zero.
1158 static WORD INT21_BufferedInput( CONTEXT86 *context, BYTE *ptr, WORD capacity )
1160 BYTE length = 0;
1163 * Return immediately if capacity is zero.
1165 if (capacity == 0)
1166 return 0;
1168 while(TRUE)
1170 BYTE ascii;
1171 BYTE scan;
1173 DOSVM_Int16ReadChar( &ascii, &scan, context );
1175 if (ascii == '\r' || ascii == '\n')
1177 ptr[length] = '\r';
1178 return length + 1;
1182 * DOS handles only backspace and KEY_LEFT
1183 * perhaps we should do more
1185 if (ascii == '\b' || scan == KEY_LEFT)
1187 if (length==0) continue;
1188 DOSVM_PutChar( '\b' );
1189 length--;
1190 continue;
1194 * If the buffer becomes filled to within one byte of
1195 * capacity, DOS rejects all further characters up to,
1196 * but not including, the terminating carriage return.
1198 if (ascii != 0 && length < capacity-1)
1200 DOSVM_PutChar( ascii );
1201 ptr[length] = ascii;
1202 length++;
1208 /***********************************************************************
1209 * INT21_GetCurrentDTA
1211 static BYTE *INT21_GetCurrentDTA( CONTEXT86 *context )
1213 TDB *pTask = GlobalLock16(GetCurrentTask());
1215 /* FIXME: This assumes DTA was set correctly! */
1216 return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
1217 (DWORD)OFFSETOF(pTask->dta) );
1221 /***********************************************************************
1222 * INT21_OpenFileUsingFCB
1224 * Handler for function 0x0f.
1226 * PARAMS
1227 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
1229 * RETURNS (in AL)
1230 * 0x00: successful
1231 * 0xff: failed
1233 * NOTES
1234 * Opens a FCB file for read/write in compatibility mode. Upon calling
1235 * the FCB must have the drive_number, file_name, and file_extension
1236 * fields filled and all other bytes cleared.
1238 static void INT21_OpenFileUsingFCB( CONTEXT86 *context )
1240 struct FCB *fcb;
1241 struct XFCB *xfcb;
1242 char file_path[16];
1243 char *pos;
1244 HANDLE handle;
1245 HFILE16 hfile16;
1246 BY_HANDLE_FILE_INFORMATION info;
1247 BYTE AL_result;
1249 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1250 if (fcb->drive_number == 0xff) {
1251 xfcb = (struct XFCB *) fcb;
1252 fcb = (struct FCB *) xfcb->fcb;
1253 } /* if */
1255 AL_result = 0;
1256 file_path[0] = 'A' + INT21_MapDrive( fcb->drive_number );
1258 if (AL_result == 0) {
1259 file_path[1] = ':';
1260 pos = &file_path[2];
1261 memcpy(pos, fcb->file_name, 8);
1262 pos[8] = ' ';
1263 pos[9] = '\0';
1264 pos = strchr(pos, ' ');
1265 *pos = '.';
1266 pos++;
1267 memcpy(pos, fcb->file_extension, 3);
1268 pos[3] = ' ';
1269 pos[4] = '\0';
1270 pos = strchr(pos, ' ');
1271 *pos = '\0';
1273 handle = (HANDLE) _lopen(file_path, OF_READWRITE);
1274 if (handle == INVALID_HANDLE_VALUE) {
1275 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path);
1276 AL_result = 0xff; /* failed */
1277 } else {
1278 hfile16 = Win32HandleToDosFileHandle(handle);
1279 if (hfile16 == HFILE_ERROR16) {
1280 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle);
1281 CloseHandle(handle);
1282 AL_result = 0xff; /* failed */
1283 } else if (hfile16 > 255) {
1284 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16, file_path);
1285 _lclose16(hfile16);
1286 AL_result = 0xff; /* failed */
1287 } else {
1288 if (!GetFileInformationByHandle(handle, &info)) {
1289 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1290 hfile16, handle, file_path);
1291 _lclose16(hfile16);
1292 AL_result = 0xff; /* failed */
1293 } else {
1294 fcb->drive_number = file_path[0] - 'A' + 1;
1295 fcb->current_block_number = 0;
1296 fcb->logical_record_size = 128;
1297 fcb->file_size = info.nFileSizeLow;
1298 FileTimeToDosDateTime(&info.ftLastWriteTime,
1299 &fcb->date_of_last_write, &fcb->time_of_last_write);
1300 fcb->file_number = hfile16;
1301 fcb->attributes = 0xc2;
1302 fcb->starting_cluster = 0; /* don't know correct init value */
1303 fcb->sequence_number = 0; /* don't know correct init value */
1304 fcb->file_attributes = info.dwFileAttributes;
1305 /* The following fields are not initialized */
1306 /* by the native function: */
1307 /* unused */
1308 /* record_within_current_block */
1309 /* random_access_record_number */
1311 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1312 file_path, hfile16, handle);
1313 AL_result = 0x00; /* successful */
1314 } /* if */
1315 } /* if */
1316 } /* if */
1317 } /* if */
1318 SET_AL(context, AL_result);
1322 /***********************************************************************
1323 * INT21_CloseFileUsingFCB
1325 * Handler for function 0x10.
1327 * PARAMS
1328 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1330 * RETURNS (in AL)
1331 * 0x00: successful
1332 * 0xff: failed
1334 * NOTES
1335 * Closes a FCB file.
1337 static void INT21_CloseFileUsingFCB( CONTEXT86 *context )
1339 struct FCB *fcb;
1340 struct XFCB *xfcb;
1341 BYTE AL_result;
1343 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1344 if (fcb->drive_number == 0xff) {
1345 xfcb = (struct XFCB *) fcb;
1346 fcb = (struct FCB *) xfcb->fcb;
1347 } /* if */
1349 if (_lclose16((HFILE16) fcb->file_number) != 0) {
1350 TRACE("_lclose16(%d) failed\n", fcb->file_number);
1351 AL_result = 0xff; /* failed */
1352 } else {
1353 TRACE("successful closed file %d\n", fcb->file_number);
1354 AL_result = 0x00; /* successful */
1355 } /* if */
1356 SET_AL(context, AL_result);
1360 /***********************************************************************
1361 * INT21_SequentialReadFromFCB
1363 * Handler for function 0x14.
1365 * PARAMS
1366 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1368 * RETURNS (in AL)
1369 * 0: successful
1370 * 1: end of file, no data read
1371 * 2: segment wrap in DTA, no data read (not returned now)
1372 * 3: end of file, partial record read
1374 * NOTES
1375 * Reads a record with the size FCB->logical_record_size from the FCB
1376 * to the disk transfer area. The position of the record is specified
1377 * with FCB->current_block_number and FCB->record_within_current_block.
1378 * Then FCB->current_block_number and FCB->record_within_current_block
1379 * are updated to point to the next record. If a partial record is
1380 * read, it is filled with zeros up to the FCB->logical_record_size.
1382 static void INT21_SequentialReadFromFCB( CONTEXT86 *context )
1384 struct FCB *fcb;
1385 struct XFCB *xfcb;
1386 HANDLE handle;
1387 DWORD record_number;
1388 long position;
1389 BYTE *disk_transfer_area;
1390 UINT bytes_read;
1391 BYTE AL_result;
1393 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1394 if (fcb->drive_number == 0xff) {
1395 xfcb = (struct XFCB *) fcb;
1396 fcb = (struct FCB *) xfcb->fcb;
1397 } /* if */
1399 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1400 if (handle == INVALID_HANDLE_VALUE) {
1401 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1402 fcb->file_number);
1403 AL_result = 0x01; /* end of file, no data read */
1404 } else {
1405 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1406 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1407 if (position != record_number * fcb->logical_record_size) {
1408 TRACE("seek(%d, %d, 0) failed with %ld\n",
1409 fcb->file_number, record_number * fcb->logical_record_size, position);
1410 AL_result = 0x01; /* end of file, no data read */
1411 } else {
1412 disk_transfer_area = INT21_GetCurrentDTA(context);
1413 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1414 if (bytes_read != fcb->logical_record_size) {
1415 TRACE("_lread(%d, %p, %d) failed with %d\n",
1416 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1417 if (bytes_read == 0) {
1418 AL_result = 0x01; /* end of file, no data read */
1419 } else {
1420 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1421 AL_result = 0x03; /* end of file, partial record read */
1422 } /* if */
1423 } else {
1424 TRACE("successful read %d bytes from record %d (position %ld) of file %d (handle %p)\n",
1425 bytes_read, record_number, position, fcb->file_number, handle);
1426 AL_result = 0x00; /* successful */
1427 } /* if */
1428 } /* if */
1429 } /* if */
1430 if (AL_result == 0x00 || AL_result == 0x03) {
1431 if (fcb->record_within_current_block == 127) {
1432 fcb->record_within_current_block = 0;
1433 fcb->current_block_number++;
1434 } else {
1435 fcb->record_within_current_block++;
1436 } /* if */
1437 } /* if */
1438 SET_AL(context, AL_result);
1442 /***********************************************************************
1443 * INT21_SequentialWriteToFCB
1445 * Handler for function 0x15.
1447 * PARAMS
1448 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1450 * RETURNS (in AL)
1451 * 0: successful
1452 * 1: disk full
1453 * 2: segment wrap in DTA (not returned now)
1455 * NOTES
1456 * Writes a record with the size FCB->logical_record_size from the disk
1457 * transfer area to the FCB. The position of the record is specified
1458 * with FCB->current_block_number and FCB->record_within_current_block.
1459 * Then FCB->current_block_number and FCB->record_within_current_block
1460 * are updated to point to the next record.
1462 static void INT21_SequentialWriteToFCB( CONTEXT86 *context )
1464 struct FCB *fcb;
1465 struct XFCB *xfcb;
1466 HANDLE handle;
1467 DWORD record_number;
1468 long position;
1469 BYTE *disk_transfer_area;
1470 UINT bytes_written;
1471 BYTE AL_result;
1473 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1474 if (fcb->drive_number == 0xff) {
1475 xfcb = (struct XFCB *) fcb;
1476 fcb = (struct FCB *) xfcb->fcb;
1477 } /* if */
1479 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1480 if (handle == INVALID_HANDLE_VALUE) {
1481 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1482 fcb->file_number);
1483 AL_result = 0x01; /* disk full */
1484 } else {
1485 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1486 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1487 if (position != record_number * fcb->logical_record_size) {
1488 TRACE("seek(%d, %d, 0) failed with %ld\n",
1489 fcb->file_number, record_number * fcb->logical_record_size, position);
1490 AL_result = 0x01; /* disk full */
1491 } else {
1492 disk_transfer_area = INT21_GetCurrentDTA(context);
1493 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, fcb->logical_record_size);
1494 if (bytes_written != fcb->logical_record_size) {
1495 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1496 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1497 AL_result = 0x01; /* disk full */
1498 } else {
1499 TRACE("successful written %d bytes from record %d (position %ld) of file %d (handle %p)\n",
1500 bytes_written, record_number, position, fcb->file_number, handle);
1501 AL_result = 0x00; /* successful */
1502 } /* if */
1503 } /* if */
1504 } /* if */
1505 if (AL_result == 0x00) {
1506 if (fcb->record_within_current_block == 127) {
1507 fcb->record_within_current_block = 0;
1508 fcb->current_block_number++;
1509 } else {
1510 fcb->record_within_current_block++;
1511 } /* if */
1512 } /* if */
1513 SET_AL(context, AL_result);
1517 /***********************************************************************
1518 * INT21_ReadRandomRecordFromFCB
1520 * Handler for function 0x21.
1522 * PARAMS
1523 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1525 * RETURNS (in AL)
1526 * 0: successful
1527 * 1: end of file, no data read
1528 * 2: segment wrap in DTA, no data read (not returned now)
1529 * 3: end of file, partial record read
1531 * NOTES
1532 * Reads a record with the size FCB->logical_record_size from
1533 * the FCB to the disk transfer area. The position of the record
1534 * is specified with FCB->random_access_record_number. The
1535 * FCB->random_access_record_number is not updated. If a partial record
1536 * is read, it is filled with zeros up to the FCB->logical_record_size.
1538 static void INT21_ReadRandomRecordFromFCB( CONTEXT86 *context )
1540 struct FCB *fcb;
1541 struct XFCB *xfcb;
1542 HANDLE handle;
1543 DWORD record_number;
1544 long position;
1545 BYTE *disk_transfer_area;
1546 UINT bytes_read;
1547 BYTE AL_result;
1549 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1550 if (fcb->drive_number == 0xff) {
1551 xfcb = (struct XFCB *) fcb;
1552 fcb = (struct FCB *) xfcb->fcb;
1553 } /* if */
1555 memcpy(&record_number, fcb->random_access_record_number, 4);
1556 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1557 if (handle == INVALID_HANDLE_VALUE) {
1558 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1559 fcb->file_number);
1560 AL_result = 0x01; /* end of file, no data read */
1561 } else {
1562 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1563 if (position != record_number * fcb->logical_record_size) {
1564 TRACE("seek(%d, %d, 0) failed with %ld\n",
1565 fcb->file_number, record_number * fcb->logical_record_size, position);
1566 AL_result = 0x01; /* end of file, no data read */
1567 } else {
1568 disk_transfer_area = INT21_GetCurrentDTA(context);
1569 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1570 if (bytes_read != fcb->logical_record_size) {
1571 TRACE("_lread(%d, %p, %d) failed with %d\n",
1572 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1573 if (bytes_read == 0) {
1574 AL_result = 0x01; /* end of file, no data read */
1575 } else {
1576 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1577 AL_result = 0x03; /* end of file, partial record read */
1578 } /* if */
1579 } else {
1580 TRACE("successful read %d bytes from record %d (position %ld) of file %d (handle %p)\n",
1581 bytes_read, record_number, position, fcb->file_number, handle);
1582 AL_result = 0x00; /* successful */
1583 } /* if */
1584 } /* if */
1585 } /* if */
1586 fcb->current_block_number = record_number / 128;
1587 fcb->record_within_current_block = record_number % 128;
1588 SET_AL(context, AL_result);
1592 /***********************************************************************
1593 * INT21_WriteRandomRecordToFCB
1595 * Handler for function 0x22.
1597 * PARAMS
1598 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1600 * RETURNS (in AL)
1601 * 0: successful
1602 * 1: disk full
1603 * 2: segment wrap in DTA (not returned now)
1605 * NOTES
1606 * Writes a record with the size FCB->logical_record_size from
1607 * the disk transfer area to the FCB. The position of the record
1608 * is specified with FCB->random_access_record_number. The
1609 * FCB->random_access_record_number is not updated.
1611 static void INT21_WriteRandomRecordToFCB( CONTEXT86 *context )
1613 struct FCB *fcb;
1614 struct XFCB *xfcb;
1615 HANDLE handle;
1616 DWORD record_number;
1617 long position;
1618 BYTE *disk_transfer_area;
1619 UINT bytes_written;
1620 BYTE AL_result;
1622 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1623 if (fcb->drive_number == 0xff) {
1624 xfcb = (struct XFCB *) fcb;
1625 fcb = (struct FCB *) xfcb->fcb;
1626 } /* if */
1628 memcpy(&record_number, fcb->random_access_record_number, 4);
1629 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1630 if (handle == INVALID_HANDLE_VALUE) {
1631 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1632 fcb->file_number);
1633 AL_result = 0x01; /* disk full */
1634 } else {
1635 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1636 if (position != record_number * fcb->logical_record_size) {
1637 TRACE("seek(%d, %d, 0) failed with %ld\n",
1638 fcb->file_number, record_number * fcb->logical_record_size, position);
1639 AL_result = 0x01; /* disk full */
1640 } else {
1641 disk_transfer_area = INT21_GetCurrentDTA(context);
1642 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, fcb->logical_record_size);
1643 if (bytes_written != fcb->logical_record_size) {
1644 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1645 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1646 AL_result = 0x01; /* disk full */
1647 } else {
1648 TRACE("successful written %d bytes from record %d (position %ld) of file %d (handle %p)\n",
1649 bytes_written, record_number, position, fcb->file_number, handle);
1650 AL_result = 0x00; /* successful */
1651 } /* if */
1652 } /* if */
1653 } /* if */
1654 fcb->current_block_number = record_number / 128;
1655 fcb->record_within_current_block = record_number % 128;
1656 SET_AL(context, AL_result);
1660 /***********************************************************************
1661 * INT21_RandomBlockReadFromFCB
1663 * Handler for function 0x27.
1665 * PARAMS
1666 * CX [I/O] Number of records to read
1667 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1669 * RETURNS (in AL)
1670 * 0: successful
1671 * 1: end of file, no data read
1672 * 2: segment wrap in DTA, no data read (not returned now)
1673 * 3: end of file, partial record read
1675 * NOTES
1676 * Reads several records with the size FCB->logical_record_size from
1677 * the FCB to the disk transfer area. The number of records to be
1678 * read is specified in the CX register. The position of the first
1679 * record is specified with FCB->random_access_record_number. The
1680 * FCB->random_access_record_number, the FCB->current_block_number
1681 * and FCB->record_within_current_block are updated to point to the
1682 * next record after the records read. If a partial record is read,
1683 * it is filled with zeros up to the FCB->logical_record_size. The
1684 * CX register is set to the number of successfully read records.
1686 static void INT21_RandomBlockReadFromFCB( CONTEXT86 *context )
1688 struct FCB *fcb;
1689 struct XFCB *xfcb;
1690 HANDLE handle;
1691 DWORD record_number;
1692 long position;
1693 BYTE *disk_transfer_area;
1694 UINT records_requested;
1695 UINT bytes_requested;
1696 UINT bytes_read;
1697 UINT records_read;
1698 BYTE AL_result;
1700 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1701 if (fcb->drive_number == 0xff) {
1702 xfcb = (struct XFCB *) fcb;
1703 fcb = (struct FCB *) xfcb->fcb;
1704 } /* if */
1706 memcpy(&record_number, fcb->random_access_record_number, 4);
1707 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1708 if (handle == INVALID_HANDLE_VALUE) {
1709 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1710 fcb->file_number);
1711 records_read = 0;
1712 AL_result = 0x01; /* end of file, no data read */
1713 } else {
1714 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1715 if (position != record_number * fcb->logical_record_size) {
1716 TRACE("seek(%d, %d, 0) failed with %ld\n",
1717 fcb->file_number, record_number * fcb->logical_record_size, position);
1718 records_read = 0;
1719 AL_result = 0x01; /* end of file, no data read */
1720 } else {
1721 disk_transfer_area = INT21_GetCurrentDTA(context);
1722 records_requested = CX_reg(context);
1723 bytes_requested = (UINT) records_requested * fcb->logical_record_size;
1724 bytes_read = _lread((HFILE) handle, disk_transfer_area, bytes_requested);
1725 if (bytes_read != bytes_requested) {
1726 TRACE("_lread(%d, %p, %d) failed with %d\n",
1727 fcb->file_number, disk_transfer_area, bytes_requested, bytes_read);
1728 records_read = bytes_read / fcb->logical_record_size;
1729 if (bytes_read % fcb->logical_record_size == 0) {
1730 AL_result = 0x01; /* end of file, no data read */
1731 } else {
1732 records_read++;
1733 memset(&disk_transfer_area[bytes_read], 0, records_read * fcb->logical_record_size - bytes_read);
1734 AL_result = 0x03; /* end of file, partial record read */
1735 } /* if */
1736 } else {
1737 TRACE("successful read %d bytes from record %d (position %ld) of file %d (handle %p)\n",
1738 bytes_read, record_number, position, fcb->file_number, handle);
1739 records_read = records_requested;
1740 AL_result = 0x00; /* successful */
1741 } /* if */
1742 } /* if */
1743 } /* if */
1744 record_number += records_read;
1745 memcpy(fcb->random_access_record_number, &record_number, 4);
1746 fcb->current_block_number = record_number / 128;
1747 fcb->record_within_current_block = record_number % 128;
1748 SET_CX(context, records_read);
1749 SET_AL(context, AL_result);
1753 /***********************************************************************
1754 * INT21_RandomBlockWriteToFCB
1756 * Handler for function 0x28.
1758 * PARAMS
1759 * CX [I/O] Number of records to write
1760 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1762 * RETURNS (in AL)
1763 * 0: successful
1764 * 1: disk full
1765 * 2: segment wrap in DTA (not returned now)
1767 * NOTES
1768 * Writes several records with the size FCB->logical_record_size from
1769 * the disk transfer area to the FCB. The number of records to be
1770 * written is specified in the CX register. The position of the first
1771 * record is specified with FCB->random_access_record_number. The
1772 * FCB->random_access_record_number, the FCB->current_block_number
1773 * and FCB->record_within_current_block are updated to point to the
1774 * next record after the records written. The CX register is set to
1775 * the number of successfully written records.
1777 static void INT21_RandomBlockWriteToFCB( CONTEXT86 *context )
1779 struct FCB *fcb;
1780 struct XFCB *xfcb;
1781 HANDLE handle;
1782 DWORD record_number;
1783 long position;
1784 BYTE *disk_transfer_area;
1785 UINT records_requested;
1786 UINT bytes_requested;
1787 UINT bytes_written;
1788 UINT records_written;
1789 BYTE AL_result;
1791 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1792 if (fcb->drive_number == 0xff) {
1793 xfcb = (struct XFCB *) fcb;
1794 fcb = (struct FCB *) xfcb->fcb;
1795 } /* if */
1797 memcpy(&record_number, fcb->random_access_record_number, 4);
1798 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1799 if (handle == INVALID_HANDLE_VALUE) {
1800 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1801 fcb->file_number);
1802 records_written = 0;
1803 AL_result = 0x01; /* disk full */
1804 } else {
1805 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1806 if (position != record_number * fcb->logical_record_size) {
1807 TRACE("seek(%d, %d, 0) failed with %ld\n",
1808 fcb->file_number, record_number * fcb->logical_record_size, position);
1809 records_written = 0;
1810 AL_result = 0x01; /* disk full */
1811 } else {
1812 disk_transfer_area = INT21_GetCurrentDTA(context);
1813 records_requested = CX_reg(context);
1814 bytes_requested = (UINT) records_requested * fcb->logical_record_size;
1815 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, bytes_requested);
1816 if (bytes_written != bytes_requested) {
1817 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1818 fcb->file_number, disk_transfer_area, bytes_requested, bytes_written);
1819 records_written = bytes_written / fcb->logical_record_size;
1820 AL_result = 0x01; /* disk full */
1821 } else {
1822 TRACE("successful write %d bytes from record %d (position %ld) of file %d (handle %p)\n",
1823 bytes_written, record_number, position, fcb->file_number, handle);
1824 records_written = records_requested;
1825 AL_result = 0x00; /* successful */
1826 } /* if */
1827 } /* if */
1828 } /* if */
1829 record_number += records_written;
1830 memcpy(fcb->random_access_record_number, &record_number, 4);
1831 fcb->current_block_number = record_number / 128;
1832 fcb->record_within_current_block = record_number % 128;
1833 SET_CX(context, records_written);
1834 SET_AL(context, AL_result);
1838 /***********************************************************************
1839 * INT21_CreateDirectory
1841 * Handler for:
1842 * - function 0x39
1843 * - subfunction 0x39 of function 0x71
1844 * - subfunction 0xff of function 0x43 (CL == 0x39)
1846 static BOOL INT21_CreateDirectory( CONTEXT86 *context )
1848 WCHAR dirW[MAX_PATH];
1849 char *dirA = CTX_SEG_OFF_TO_LIN(context,
1850 context->SegDs,
1851 context->Edx);
1853 TRACE( "CREATE DIRECTORY %s\n", dirA );
1855 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
1857 if (CreateDirectoryW(dirW, NULL))
1858 return TRUE;
1861 * FIXME: CreateDirectory's LastErrors will clash with the ones
1862 * used by DOS. AH=39 only returns 3 (path not found) and
1863 * 5 (access denied), while CreateDirectory return several
1864 * ones. Remap some of them. -Marcus
1866 switch (GetLastError())
1868 case ERROR_ALREADY_EXISTS:
1869 case ERROR_FILENAME_EXCED_RANGE:
1870 case ERROR_DISK_FULL:
1871 SetLastError(ERROR_ACCESS_DENIED);
1872 break;
1873 default:
1874 break;
1877 return FALSE;
1881 /***********************************************************************
1882 * INT21_ExtendedCountryInformation
1884 * Handler for function 0x65.
1886 static void INT21_ExtendedCountryInformation( CONTEXT86 *context )
1888 BYTE *dataptr = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
1889 BYTE buffsize = CX_reg (context);
1891 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1892 AL_reg(context) );
1895 * Check subfunctions that are passed country and code page.
1897 if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
1899 WORD country = DX_reg(context);
1900 WORD codepage = BX_reg(context);
1902 if (country != 0xffff && country != INT21_GetSystemCountryCode())
1903 FIXME( "Requested info on non-default country %04x\n", country );
1905 if (codepage != 0xffff && codepage != GetOEMCP())
1906 FIXME( "Requested info on non-default code page %04x\n", codepage );
1909 switch (AL_reg(context)) {
1910 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1911 INT_BARF( context, 0x21 );
1912 SET_CFLAG( context );
1913 break;
1915 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1916 TRACE( "Get general internationalization info\n" );
1917 dataptr[0] = 0x01; /* Info ID */
1918 *(WORD*)(dataptr+1) = 38; /* Size of the following info */
1919 *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
1920 *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
1921 /* FIXME: fill buffer partially up to buffsize bytes*/
1922 if (buffsize >= 0x29){
1923 INT21_FillCountryInformation( dataptr + 7 );
1924 SET_CX( context, 0x29 ); /* Size of returned info */
1925 }else{
1926 SET_CX( context, 0x07 ); /* Size of returned info */
1928 break;
1930 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1931 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1932 TRACE( "Get pointer to uppercase table\n" );
1933 dataptr[0] = AL_reg(context); /* Info ID */
1934 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1935 offsetof(INT21_HEAP, uppercase_size) );
1936 SET_CX( context, 5 ); /* Size of returned info */
1937 break;
1939 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1940 TRACE( "Get pointer to lowercase table\n" );
1941 dataptr[0] = 0x03; /* Info ID */
1942 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1943 offsetof(INT21_HEAP, lowercase_size) );
1944 SET_CX( context, 5 ); /* Size of returned info */
1945 break;
1947 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1948 TRACE("Get pointer to filename terminator table\n");
1949 dataptr[0] = 0x05; /* Info ID */
1950 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1951 offsetof(INT21_HEAP, filename_size) );
1952 SET_CX( context, 5 ); /* Size of returned info */
1953 break;
1955 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1956 TRACE("Get pointer to collating sequence table\n");
1957 dataptr[0] = 0x06; /* Info ID */
1958 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1959 offsetof(INT21_HEAP, collating_size) );
1960 SET_CX( context, 5 ); /* Size of returned info */
1961 break;
1963 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1964 TRACE("Get pointer to DBCS lead byte table\n");
1965 dataptr[0] = 0x07; /* Info ID */
1966 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1967 offsetof(INT21_HEAP, dbcs_size) );
1968 SET_CX( context, 5 ); /* Size of returned info */
1969 break;
1971 case 0x20: /* CAPITALIZE CHARACTER */
1972 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1973 TRACE("Convert char to uppercase\n");
1974 SET_DL( context, toupper(DL_reg(context)) );
1975 break;
1977 case 0x21: /* CAPITALIZE STRING */
1978 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1979 TRACE("Convert string to uppercase with length\n");
1981 char *ptr = (char *)CTX_SEG_OFF_TO_LIN( context,
1982 context->SegDs,
1983 context->Edx );
1984 WORD len = CX_reg(context);
1985 while (len--) { *ptr = toupper(*ptr); ptr++; }
1987 break;
1989 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1990 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1991 TRACE("Convert ASCIIZ string to uppercase\n");
1993 char *p = CTX_SEG_OFF_TO_LIN( context, context->SegDs, context->Edx );
1994 for ( ; *p; p++) *p = toupper(*p);
1996 break;
1998 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1999 INT_BARF( context, 0x21 );
2000 SET_CFLAG( context );
2001 break;
2003 default:
2004 INT_BARF( context, 0x21 );
2005 SET_CFLAG(context);
2006 break;
2011 /***********************************************************************
2012 * INT21_FileAttributes
2014 * Handler for:
2015 * - function 0x43
2016 * - subfunction 0x43 of function 0x71
2018 static BOOL INT21_FileAttributes( CONTEXT86 *context,
2019 BYTE subfunction,
2020 BOOL islong )
2022 WCHAR fileW[MAX_PATH];
2023 char *fileA = CTX_SEG_OFF_TO_LIN(context,
2024 context->SegDs,
2025 context->Edx);
2026 HANDLE handle;
2027 BOOL status;
2028 FILETIME filetime;
2029 DWORD result;
2030 WORD date, time;
2032 switch (subfunction)
2034 case 0x00: /* GET FILE ATTRIBUTES */
2035 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA );
2036 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2038 result = GetFileAttributesW( fileW );
2039 if (result == INVALID_FILE_ATTRIBUTES)
2040 return FALSE;
2041 else
2043 SET_CX( context, (WORD)result );
2044 if (!islong)
2045 SET_AX( context, (WORD)result ); /* DR DOS */
2047 break;
2049 case 0x01: /* SET FILE ATTRIBUTES */
2050 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
2051 CX_reg(context), fileA );
2052 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2054 if (!SetFileAttributesW( fileW, CX_reg(context) ))
2055 return FALSE;
2056 break;
2058 case 0x02: /* GET COMPRESSED FILE SIZE */
2059 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA );
2060 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2062 result = GetCompressedFileSizeW( fileW, NULL );
2063 if (result == INVALID_FILE_SIZE)
2064 return FALSE;
2065 else
2067 SET_AX( context, LOWORD(result) );
2068 SET_DX( context, HIWORD(result) );
2070 break;
2072 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
2073 if (!islong)
2074 INT_BARF( context, 0x21 );
2075 else
2077 TRACE( "SET FILE LAST-WRITTEN 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;
2086 DosDateTimeToFileTime( DI_reg(context),
2087 CX_reg(context),
2088 &filetime );
2089 status = SetFileTime( handle, NULL, NULL, &filetime );
2091 CloseHandle( handle );
2092 return status;
2094 break;
2096 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
2097 if (!islong)
2098 INT_BARF( context, 0x21 );
2099 else
2101 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
2102 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2104 handle = CreateFileW( fileW, GENERIC_READ,
2105 FILE_SHARE_READ | FILE_SHARE_WRITE,
2106 NULL, OPEN_EXISTING, 0, 0 );
2107 if (handle == INVALID_HANDLE_VALUE)
2108 return FALSE;
2110 status = GetFileTime( handle, NULL, NULL, &filetime );
2111 if (status)
2113 FileTimeToDosDateTime( &filetime, &date, &time );
2114 SET_DI( context, date );
2115 SET_CX( context, time );
2118 CloseHandle( handle );
2119 return status;
2121 break;
2123 case 0x05: /* SET FILE LAST ACCESS DATE */
2124 if (!islong)
2125 INT_BARF( context, 0x21 );
2126 else
2128 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA );
2129 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2131 handle = CreateFileW( fileW, GENERIC_WRITE,
2132 FILE_SHARE_READ | FILE_SHARE_WRITE,
2133 NULL, OPEN_EXISTING, 0, 0 );
2134 if (handle == INVALID_HANDLE_VALUE)
2135 return FALSE;
2137 DosDateTimeToFileTime( DI_reg(context),
2139 &filetime );
2140 status = SetFileTime( handle, NULL, &filetime, NULL );
2142 CloseHandle( handle );
2143 return status;
2145 break;
2147 case 0x06: /* GET FILE LAST ACCESS DATE */
2148 if (!islong)
2149 INT_BARF( context, 0x21 );
2150 else
2152 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA );
2153 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2155 handle = CreateFileW( fileW, GENERIC_READ,
2156 FILE_SHARE_READ | FILE_SHARE_WRITE,
2157 NULL, OPEN_EXISTING, 0, 0 );
2158 if (handle == INVALID_HANDLE_VALUE)
2159 return FALSE;
2161 status = GetFileTime( handle, NULL, &filetime, NULL );
2162 if (status)
2164 FileTimeToDosDateTime( &filetime, &date, NULL );
2165 SET_DI( context, date );
2168 CloseHandle( handle );
2169 return status;
2171 break;
2173 case 0x07: /* SET FILE CREATION DATE AND TIME */
2174 if (!islong)
2175 INT_BARF( context, 0x21 );
2176 else
2178 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA );
2180 handle = CreateFileW( fileW, GENERIC_WRITE,
2181 FILE_SHARE_READ | FILE_SHARE_WRITE,
2182 NULL, OPEN_EXISTING, 0, 0 );
2183 if (handle == INVALID_HANDLE_VALUE)
2184 return FALSE;
2187 * FIXME: SI has number of 10-millisecond units past time in CX.
2189 DosDateTimeToFileTime( DI_reg(context),
2190 CX_reg(context),
2191 &filetime );
2192 status = SetFileTime( handle, &filetime, NULL, NULL );
2194 CloseHandle( handle );
2195 return status;
2197 break;
2199 case 0x08: /* GET FILE CREATION DATE AND TIME */
2200 if (!islong)
2201 INT_BARF( context, 0x21 );
2202 else
2204 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2205 BX_reg(context) );
2207 handle = CreateFileW( fileW, GENERIC_READ,
2208 FILE_SHARE_READ | FILE_SHARE_WRITE,
2209 NULL, OPEN_EXISTING, 0, 0 );
2210 if (handle == INVALID_HANDLE_VALUE)
2211 return FALSE;
2213 status = GetFileTime( handle, &filetime, NULL, NULL );
2214 if (status)
2216 FileTimeToDosDateTime( &filetime, &date, &time );
2217 SET_DI( context, date );
2218 SET_CX( context, time );
2220 * FIXME: SI has number of 10-millisecond units past
2221 * time in CX.
2223 SET_SI( context, 0 );
2226 CloseHandle(handle);
2227 return status;
2229 break;
2231 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2232 if (islong || context->Ebp != 0x5053)
2233 INT_BARF( context, 0x21 );
2234 else
2236 switch(CL_reg(context))
2238 case 0x39:
2239 if (!INT21_CreateDirectory( context ))
2240 return FALSE;
2241 break;
2243 case 0x56:
2244 if (!INT21_RenameFile( context ))
2245 return FALSE;
2246 break;
2248 default:
2249 INT_BARF( context, 0x21 );
2252 break;
2254 default:
2255 INT_BARF( context, 0x21 );
2258 return TRUE;
2262 /***********************************************************************
2263 * INT21_FileDateTime
2265 * Handler for function 0x57.
2267 static BOOL INT21_FileDateTime( CONTEXT86 *context )
2269 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2270 FILETIME filetime;
2271 WORD date, time;
2273 switch (AL_reg(context)) {
2274 case 0x00: /* Get last-written stamp */
2275 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2276 BX_reg(context) );
2278 if (!GetFileTime( handle, NULL, NULL, &filetime ))
2279 return FALSE;
2280 FileTimeToDosDateTime( &filetime, &date, &time );
2281 SET_DX( context, date );
2282 SET_CX( context, time );
2283 break;
2286 case 0x01: /* Set last-written stamp */
2287 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2288 BX_reg(context) );
2290 DosDateTimeToFileTime( DX_reg(context),
2291 CX_reg(context),
2292 &filetime );
2293 if (!SetFileTime( handle, NULL, NULL, &filetime ))
2294 return FALSE;
2295 break;
2298 case 0x04: /* Get last access stamp, DOS 7 */
2299 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2300 BX_reg(context) );
2302 if (!GetFileTime( handle, NULL, &filetime, NULL ))
2303 return FALSE;
2304 FileTimeToDosDateTime( &filetime, &date, &time );
2305 SET_DX( context, date );
2306 SET_CX( context, time );
2307 break;
2310 case 0x05: /* Set last access stamp, DOS 7 */
2311 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2312 BX_reg(context) );
2314 DosDateTimeToFileTime( DX_reg(context),
2315 CX_reg(context),
2316 &filetime );
2317 if (!SetFileTime( handle, NULL, &filetime, NULL ))
2318 return FALSE;
2319 break;
2322 case 0x06: /* Get creation stamp, DOS 7 */
2323 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2324 BX_reg(context) );
2326 if (!GetFileTime( handle, &filetime, NULL, NULL ))
2327 return FALSE;
2328 FileTimeToDosDateTime( &filetime, &date, &time );
2329 SET_DX( context, date );
2330 SET_CX( context, time );
2332 * FIXME: SI has number of 10-millisecond units past time in CX.
2334 SET_SI( context, 0 );
2335 break;
2338 case 0x07: /* Set creation stamp, DOS 7 */
2339 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2340 BX_reg(context) );
2343 * FIXME: SI has number of 10-millisecond units past time in CX.
2345 DosDateTimeToFileTime( DX_reg(context),
2346 CX_reg(context),
2347 &filetime );
2348 if (!SetFileTime( handle, &filetime, NULL, NULL ))
2349 return FALSE;
2350 break;
2353 default:
2354 INT_BARF( context, 0x21 );
2355 break;
2358 return TRUE;
2362 /***********************************************************************
2363 * INT21_GetPSP
2365 * Handler for functions 0x51 and 0x62.
2367 static void INT21_GetPSP( CONTEXT86 *context )
2369 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
2372 * FIXME: should we return the original DOS PSP upon
2373 * Windows startup ?
2375 if (!ISV86(context) && DOSVM_IsWin16())
2376 SET_BX( context, LOWORD(GetCurrentPDB16()) );
2377 else
2378 SET_BX( context, DOSVM_psp );
2381 static inline void setword( BYTE *ptr, WORD w )
2383 ptr[0] = (BYTE)w;
2384 ptr[1] = (BYTE)(w >> 8);
2387 static void CreateBPB(int drive, BYTE *data, BOOL16 limited)
2388 /* limited == TRUE is used with INT 0x21/0x440d */
2390 /* FIXME: we're forcing some values without checking that those are valid */
2391 if (drive > 1)
2393 setword(data, 512);
2394 data[2] = 2;
2395 setword(&data[3], 0);
2396 data[5] = 2;
2397 setword(&data[6], 240);
2398 setword(&data[8], 64000);
2399 data[0x0a] = 0xf8;
2400 setword(&data[0x0b], 40);
2401 setword(&data[0x0d], 56);
2402 setword(&data[0x0f], 2);
2403 setword(&data[0x11], 0);
2404 if (!limited)
2406 setword(&data[0x1f], 800);
2407 data[0x21] = 5;
2408 setword(&data[0x22], 1);
2411 else
2412 { /* 1.44mb */
2413 setword(data, 512);
2414 data[2] = 2;
2415 setword(&data[3], 0);
2416 data[5] = 2;
2417 setword(&data[6], 240);
2418 setword(&data[8], 2880);
2419 data[0x0a] = 0xf8;
2420 setword(&data[0x0b], 6);
2421 setword(&data[0x0d], 18);
2422 setword(&data[0x0f], 2);
2423 setword(&data[0x11], 0);
2424 if (!limited)
2426 setword(&data[0x1f], 80);
2427 data[0x21] = 7;
2428 setword(&data[0x22], 2);
2433 static inline DWORD INT21_Ioctl_CylHeadSect2Lin(DWORD cyl, WORD head, WORD sec, WORD cyl_cnt,
2434 WORD head_cnt, WORD sec_cnt)
2436 DWORD res = (cyl * head_cnt*sec_cnt + head * sec_cnt + sec);
2437 return res;
2440 /***********************************************************************
2441 * INT21_Ioctl_Block
2443 * Handler for block device IOCTLs.
2445 static void INT21_Ioctl_Block( CONTEXT86 *context )
2447 BYTE *dataptr;
2448 BYTE drive = INT21_MapDrive( BL_reg(context) );
2449 WCHAR drivespec[4] = {'A', ':', '\\', 0};
2450 UINT drivetype;
2452 drivespec[0] += drive;
2453 drivetype = GetDriveTypeW( drivespec );
2455 RESET_CFLAG(context);
2456 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
2458 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2459 AL_reg(context), 'A' + drive );
2460 SetLastError( ERROR_INVALID_DRIVE );
2461 SET_AX( context, ERROR_INVALID_DRIVE );
2462 SET_CFLAG( context );
2463 return;
2466 switch (AL_reg(context))
2468 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2469 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2470 INT_BARF( context, 0x21 );
2471 break;
2473 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2474 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2475 'A' + drive );
2477 if (drivetype == DRIVE_REMOVABLE)
2478 SET_AX( context, 0 ); /* removable */
2479 else
2480 SET_AX( context, 1 ); /* not removable */
2481 break;
2483 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2484 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2485 'A' + drive );
2487 if (drivetype == DRIVE_REMOTE)
2488 SET_DX( context, (1<<9) | (1<<12) ); /* remote + no direct IO */
2489 else
2490 SET_DX( context, 0 ); /* FIXME: use driver attr here */
2491 break;
2493 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2494 /* Get pointer to IOCTL parameter block */
2495 dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2497 switch (CX_reg(context))
2499 case 0x0841: /* write logical device track */
2500 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2501 'A' + drive);
2503 WORD head = *(WORD *)(dataptr+1);
2504 WORD cyl = *(WORD *)(dataptr+3);
2505 WORD sect = *(WORD *)(dataptr+5);
2506 WORD nrsect = *(WORD *)(dataptr+7);
2507 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2508 WORD cyl_cnt, head_cnt, sec_cnt;
2510 /* FIXME: we're faking some values here */
2511 if (drive > 1)
2513 /* cyl_cnt = 0x300;
2514 head_cnt = 16;
2515 sec_cnt = 255; */
2516 SET_AX( context, ERROR_WRITE_FAULT );
2517 SET_CFLAG(context);
2518 break;
2520 else
2521 { /* floppy */
2522 cyl_cnt = 80;
2523 head_cnt = 2;
2524 sec_cnt = 18;
2527 if (!DOSVM_RawWrite(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2529 SET_AX( context, ERROR_WRITE_FAULT );
2530 SET_CFLAG(context);
2533 break;
2535 case 0x084a: /* lock logical volume */
2536 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2537 BH_reg(context), DX_reg(context), 'A' + drive );
2538 break;
2540 case 0x0860: /* get device parameters */
2541 /* FIXME: we're faking some values here */
2542 /* used by w4wgrp's winfile */
2543 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2544 dataptr[0] = 0x04;
2545 dataptr[6] = 0; /* media type */
2546 if (drive > 1)
2548 dataptr[1] = 0x05; /* fixed disk */
2549 setword(&dataptr[2], 0x01); /* non removable */
2550 setword(&dataptr[4], 0x300); /* # of cylinders */
2552 else
2554 dataptr[1] = 0x07; /* block dev, floppy */
2555 setword(&dataptr[2], 0x02); /* removable */
2556 setword(&dataptr[4], 80); /* # of cylinders */
2558 CreateBPB(drive, &dataptr[7], TRUE);
2559 RESET_CFLAG(context);
2560 break;
2562 case 0x0861: /* read logical device track */
2563 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2564 'A' + drive);
2566 WORD head = *(WORD *)(dataptr+1);
2567 WORD cyl = *(WORD *)(dataptr+3);
2568 WORD sect = *(WORD *)(dataptr+5);
2569 WORD nrsect = *(WORD *)(dataptr+7);
2570 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2571 WORD cyl_cnt, head_cnt, sec_cnt;
2573 /* FIXME: we're faking some values here */
2574 if (drive > 1)
2576 cyl_cnt = 0x300;
2577 head_cnt = 16;
2578 sec_cnt = 255;
2580 else
2581 { /* floppy */
2582 cyl_cnt = 80;
2583 head_cnt = 2;
2584 sec_cnt = 18;
2587 if (!DOSVM_RawRead(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2589 SET_AX( context, ERROR_READ_FAULT );
2590 SET_CFLAG(context);
2593 break;
2595 case 0x0866: /* get volume serial number */
2597 WCHAR label[12],fsname[9];
2598 DWORD serial;
2600 drivespec[0] += drive;
2601 GetVolumeInformationW(drivespec, label, 12, &serial, NULL, NULL, fsname, 9);
2602 *(WORD*)dataptr = 0;
2603 memcpy(dataptr+2,&serial,4);
2604 WideCharToMultiByte(CP_OEMCP, 0, label, 11, (LPSTR)dataptr + 6, 11, NULL, NULL);
2605 WideCharToMultiByte(CP_OEMCP, 0, fsname, 8, (LPSTR)dataptr + 17, 8, NULL, NULL);
2607 break;
2609 case 0x086a: /* unlock logical volume */
2610 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2611 'A' + drive );
2612 break;
2614 case 0x086f: /* get drive map information */
2615 memset(dataptr+1, '\0', dataptr[0]-1);
2616 dataptr[1] = dataptr[0];
2617 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
2618 dataptr[3] = 0xFF; /* no physical drive */
2619 break;
2621 case 0x0872:
2622 /* Trial and error implementation */
2623 SET_AX( context, drivetype == DRIVE_UNKNOWN ? 0x0f : 0x01 );
2624 SET_CFLAG(context); /* Seems to be set all the time */
2625 break;
2627 default:
2628 INT_BARF( context, 0x21 );
2630 break;
2632 case 0x0e: /* GET LOGICAL DRIVE MAP */
2633 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2634 'A' + drive );
2635 /* FIXME: this is not correct if drive has mappings */
2636 SET_AL( context, 0 ); /* drive has no mapping */
2637 break;
2639 case 0x0f: /* SET LOGICAL DRIVE MAP */
2640 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2641 INT21_DriveName( BL_reg(context)));
2642 /* FIXME: as of today, we don't support logical drive mapping... */
2643 SET_AL( context, 0 );
2644 break;
2646 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2647 default:
2648 INT_BARF( context, 0x21 );
2653 /***********************************************************************
2654 * INT21_IoctlScsiMgrHandler
2656 * IOCTL handler for the SCSIMGR device.
2658 static void INT21_IoctlScsiMgrHandler( CONTEXT86 *context )
2660 switch (AL_reg(context))
2662 case 0x00: /* GET DEVICE INFORMATION */
2663 SET_DX( context, 0xc0c0 );
2664 break;
2666 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2667 DOSVM_ASPIHandler(context);
2668 break;
2670 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2671 SET_DX( context, 0 );
2672 break;
2674 case 0x01: /* SET DEVICE INFORMATION */
2675 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2676 case 0x06: /* GET INPUT STATUS */
2677 case 0x07: /* GET OUTPUT STATUS */
2678 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2679 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2680 default:
2681 INT_BARF( context, 0x21 );
2682 break;
2687 /***********************************************************************
2688 * INT21_IoctlEMSHandler
2690 * IOCTL handler for the EMXXXX0 device.
2692 static void INT21_IoctlEMSHandler( CONTEXT86 *context )
2694 EMS_Ioctl_Handler(context);
2698 /***********************************************************************
2699 * INT21_IoctlHPScanHandler
2701 * IOCTL handler for the HPSCAN device.
2703 static void INT21_IoctlHPScanHandler( CONTEXT86 *context )
2705 switch (AL_reg(context))
2707 case 0x00: /* GET DEVICE INFORMATION */
2708 SET_DX( context, 0xc0c0 );
2709 break;
2711 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2712 SET_DX( context, 0 );
2713 break;
2715 case 0x01: /* SET DEVICE INFORMATION */
2716 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2717 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2718 case 0x06: /* GET INPUT STATUS */
2719 case 0x07: /* GET OUTPUT STATUS */
2720 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2721 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2722 default:
2723 INT_BARF( context, 0x21 );
2724 break;
2729 /***********************************************************************
2730 * INT21_Ioctl_Char
2732 * Handler for character device IOCTLs.
2734 static void INT21_Ioctl_Char( CONTEXT86 *context )
2736 int status, i;
2737 int IsConsoleIOHandle = 0;
2738 IO_STATUS_BLOCK io;
2739 FILE_INTERNAL_INFORMATION info;
2740 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2742 status = NtQueryInformationFile( handle, &io, &info, sizeof(info), FileInternalInformation );
2743 if (status)
2745 if( VerifyConsoleIoHandle( handle))
2746 IsConsoleIOHandle = 1;
2747 else {
2748 SET_AX( context, RtlNtStatusToDosError(status) );
2749 SET_CFLAG( context );
2750 return;
2752 } else {
2753 for (i = 0; i < NB_MAGIC_DEVICES; i++)
2755 if (!magic_devices[i].handle) continue;
2756 if (magic_devices[i].index.QuadPart == info.IndexNumber.QuadPart)
2758 /* found it */
2759 magic_devices[i].ioctl_handler( context );
2760 return;
2765 /* no magic device found, do default handling */
2767 switch (AL_reg(context))
2769 case 0x00: /* GET DEVICE INFORMATION */
2770 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context) );
2771 if (IsConsoleIOHandle || GetFileType(handle) == FILE_TYPE_CHAR)
2774 * Returns attribute word in DX:
2775 * Bit 14 - Device driver can process IOCTL requests.
2776 * Bit 13 - Output until busy supported.
2777 * Bit 11 - Driver supports OPEN/CLOSE calls.
2778 * Bit 8 - Unknown.
2779 * Bit 7 - Set (indicates device).
2780 * Bit 6 - EOF on input.
2781 * Bit 5 - Raw (binary) mode.
2782 * Bit 4 - Device is special (uses int29).
2783 * Bit 3 - Clock device.
2784 * Bit 2 - NUL device.
2785 * Bit 1 - Console output device.
2786 * Bit 0 - Console input device.
2788 SET_DX( context, IsConsoleIOHandle ? 0x80c3 : 0x80c0 /* FIXME */ );
2790 else
2793 * Returns attribute word in DX:
2794 * Bit 15 - File is remote.
2795 * Bit 14 - Don't set file date/time on closing.
2796 * Bit 11 - Media not removable.
2797 * Bit 8 - Generate int24 if no disk space on write
2798 * or read past end of file
2799 * Bit 7 - Clear (indicates file).
2800 * Bit 6 - File has not been written.
2801 * Bit 5..0 - Drive number (0=A:,...)
2803 * FIXME: Should check if file is on remote or removable drive.
2804 * FIXME: Should use drive file is located on (and not current).
2806 SET_DX( context, 0x0140 + INT21_GetCurrentDrive() );
2808 break;
2810 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2811 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context) );
2813 * Returns attribute word in DX:
2814 * Bit 15 - Set if remote.
2815 * Bit 14 - Set if date/time not set on close.
2817 * FIXME: Should check if file is on remote drive.
2819 SET_DX( context, 0 );
2820 break;
2822 case 0x01: /* SET DEVICE INFORMATION */
2823 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2824 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2825 case 0x06: /* GET INPUT STATUS */
2826 case 0x07: /* GET OUTPUT STATUS */
2827 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2828 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2829 default:
2830 INT_BARF( context, 0x21 );
2831 break;
2836 /***********************************************************************
2837 * INT21_Ioctl
2839 * Handler for function 0x44.
2841 static void INT21_Ioctl( CONTEXT86 *context )
2843 switch (AL_reg(context))
2845 case 0x00:
2846 case 0x01:
2847 case 0x02:
2848 case 0x03:
2849 INT21_Ioctl_Char( context );
2850 break;
2852 case 0x04:
2853 case 0x05:
2854 INT21_Ioctl_Block( context );
2855 break;
2857 case 0x06:
2858 case 0x07:
2859 INT21_Ioctl_Char( context );
2860 break;
2862 case 0x08:
2863 case 0x09:
2864 INT21_Ioctl_Block( context );
2865 break;
2867 case 0x0a:
2868 INT21_Ioctl_Char( context );
2869 break;
2871 case 0x0b: /* SET SHARING RETRY COUNT */
2872 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2873 CX_reg(context), DX_reg(context) );
2874 if (!CX_reg(context))
2876 SET_AX( context, 1 );
2877 SET_CFLAG( context );
2879 else
2881 DOSDEV_SetSharingRetry( CX_reg(context), DX_reg(context) );
2882 RESET_CFLAG( context );
2884 break;
2886 case 0x0c:
2887 INT21_Ioctl_Char( context );
2888 break;
2890 case 0x0d:
2891 case 0x0e:
2892 case 0x0f:
2893 INT21_Ioctl_Block( context );
2894 break;
2896 case 0x10:
2897 INT21_Ioctl_Char( context );
2898 break;
2900 case 0x11:
2901 INT21_Ioctl_Block( context );
2902 break;
2904 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2905 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2906 SET_CFLAG(context); /* Error / This is not DR DOS. */
2907 SET_AX( context, 0x0001 ); /* Invalid function */
2908 break;
2910 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2911 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2912 SET_CFLAG(context); /* Error / This is not DR DOS. */
2913 SET_AX( context, 0x0001 ); /* Invalid function */
2914 break;
2916 case 0xe0: /* Sun PC-NFS API */
2917 TRACE( "Sun PC-NFS API\n" );
2918 /* not installed */
2919 break;
2921 default:
2922 INT_BARF( context, 0x21 );
2927 /***********************************************************************
2928 * INT21_Fat32
2930 * Handler for function 0x73.
2932 static BOOL INT21_Fat32( CONTEXT86 *context )
2934 switch (AL_reg(context))
2936 case 0x02: /* FAT32 - GET EXTENDED DPB */
2938 BYTE drive = INT21_MapDrive( DL_reg(context) );
2939 WORD *ptr = CTX_SEG_OFF_TO_LIN(context,
2940 context->SegEs, context->Edi);
2941 INT21_DPB *target = (INT21_DPB*)(ptr + 1);
2942 INT21_DPB *source;
2944 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context) );
2946 if ( CX_reg(context) < sizeof(INT21_DPB) + 2 || *ptr < sizeof(INT21_DPB) )
2948 SetLastError( ERROR_BAD_LENGTH );
2949 return FALSE;
2952 if ( !INT21_FillDrivePB( drive ) )
2954 SetLastError( ERROR_INVALID_DRIVE );
2955 return FALSE;
2958 source = &INT21_GetHeapPointer()->misc_dpb_list[drive];
2960 *ptr = sizeof(INT21_DPB);
2961 memcpy( target, source, sizeof(INT21_DPB));
2963 if (LOWORD(context->Esi) != 0xF1A6)
2965 target->driver_header = 0;
2966 target->next = 0;
2968 else
2970 FIXME( "Caller requested driver and next DPB pointers!\n" );
2973 break;
2975 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2977 WCHAR dirW[MAX_PATH];
2978 char *dirA = CTX_SEG_OFF_TO_LIN( context,
2979 context->SegDs, context->Edx );
2980 BYTE *data = CTX_SEG_OFF_TO_LIN( context,
2981 context->SegEs, context->Edi );
2982 DWORD cluster_sectors;
2983 DWORD sector_bytes;
2984 DWORD free_clusters;
2985 DWORD total_clusters;
2987 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA );
2988 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
2990 if (CX_reg(context) < 44)
2992 SetLastError( ERROR_BAD_LENGTH );
2993 return FALSE;
2996 if (!GetDiskFreeSpaceW( dirW, &cluster_sectors, &sector_bytes,
2997 &free_clusters, &total_clusters ))
2998 return FALSE;
3000 *(WORD*) (data + 0) = 44; /* size of structure */
3001 *(WORD*) (data + 2) = 0; /* version */
3002 *(DWORD*)(data + 4) = cluster_sectors;
3003 *(DWORD*)(data + 8) = sector_bytes;
3004 *(DWORD*)(data + 12) = free_clusters;
3005 *(DWORD*)(data + 16) = total_clusters;
3008 * Below we have free/total sectors and
3009 * free/total allocation units without adjustment
3010 * for compression. We fake both using cluster information.
3012 *(DWORD*)(data + 20) = free_clusters * cluster_sectors;
3013 *(DWORD*)(data + 24) = total_clusters * cluster_sectors;
3014 *(DWORD*)(data + 28) = free_clusters;
3015 *(DWORD*)(data + 32) = total_clusters;
3018 * Between (data + 36) and (data + 43) there
3019 * are eight reserved bytes.
3022 break;
3024 default:
3025 INT_BARF( context, 0x21 );
3028 return TRUE;
3031 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA *dataA,
3032 const WIN32_FIND_DATAW *dataW)
3034 dataA->dwFileAttributes = dataW->dwFileAttributes;
3035 dataA->ftCreationTime = dataW->ftCreationTime;
3036 dataA->ftLastAccessTime = dataW->ftLastAccessTime;
3037 dataA->ftLastWriteTime = dataW->ftLastWriteTime;
3038 dataA->nFileSizeHigh = dataW->nFileSizeHigh;
3039 dataA->nFileSizeLow = dataW->nFileSizeLow;
3040 WideCharToMultiByte( CP_OEMCP, 0, dataW->cFileName, -1,
3041 dataA->cFileName, sizeof(dataA->cFileName), NULL, NULL );
3042 WideCharToMultiByte( CP_OEMCP, 0, dataW->cAlternateFileName, -1,
3043 dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName), NULL, NULL );
3046 /***********************************************************************
3047 * INT21_LongFilename
3049 * Handler for function 0x71.
3051 static void INT21_LongFilename( CONTEXT86 *context )
3053 BOOL bSetDOSExtendedError = FALSE;
3054 WCHAR pathW[MAX_PATH];
3055 char* pathA;
3057 if (HIBYTE(HIWORD(GetVersion16())) < 0x07)
3059 TRACE( "LONG FILENAME - functions supported only under DOS7\n" );
3060 SET_CFLAG( context );
3061 SET_AL( context, 0 );
3062 return;
3065 switch (AL_reg(context))
3067 case 0x0d: /* RESET DRIVE */
3068 INT_BARF( context, 0x21 );
3069 break;
3071 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
3072 if (!INT21_CreateDirectory( context ))
3073 bSetDOSExtendedError = TRUE;
3074 break;
3076 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
3077 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3079 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA);
3080 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3081 if (!RemoveDirectoryW( pathW )) bSetDOSExtendedError = TRUE;
3082 break;
3084 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
3085 if (!INT21_SetCurrentDirectory( context ))
3086 bSetDOSExtendedError = TRUE;
3087 break;
3089 case 0x41: /* LONG FILENAME - DELETE FILE */
3090 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3092 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA );
3093 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3095 if (!DeleteFileW( pathW )) bSetDOSExtendedError = TRUE;
3096 break;
3098 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
3099 if (!INT21_FileAttributes( context, BL_reg(context), TRUE ))
3100 bSetDOSExtendedError = TRUE;
3101 break;
3103 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
3104 if (!INT21_GetCurrentDirectory( context, TRUE ))
3105 bSetDOSExtendedError = TRUE;
3106 break;
3108 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
3110 HANDLE handle;
3111 HGLOBAL16 h16;
3112 WIN32_FIND_DATAW dataW;
3113 WIN32_FIND_DATAA* dataA;
3115 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
3116 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA);
3118 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3119 handle = FindFirstFileW(pathW, &dataW);
3121 dataA = (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
3122 context->Edi);
3123 if (handle != INVALID_HANDLE_VALUE &&
3124 (h16 = GlobalAlloc16(GMEM_MOVEABLE, sizeof(handle))))
3126 HANDLE* ptr = GlobalLock16( h16 );
3127 *ptr = handle;
3128 GlobalUnlock16( h16 );
3129 SET_AX( context, h16 );
3130 INT21_ConvertFindDataWtoA(dataA, &dataW);
3132 else
3134 if (handle != INVALID_HANDLE_VALUE) FindClose(handle);
3135 SET_AX( context, INVALID_HANDLE_VALUE16);
3136 bSetDOSExtendedError = TRUE;
3139 break;
3141 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
3143 HGLOBAL16 h16 = BX_reg(context);
3144 HANDLE* ptr;
3145 WIN32_FIND_DATAW dataW;
3146 WIN32_FIND_DATAA* dataA;
3148 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
3149 BX_reg(context));
3151 dataA = (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
3152 context->Edi);
3154 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
3156 if (!FindNextFileW(*ptr, &dataW)) bSetDOSExtendedError = TRUE;
3157 else INT21_ConvertFindDataWtoA(dataA, &dataW);
3158 GlobalUnlock16( h16 );
3160 else
3162 SetLastError( ERROR_INVALID_HANDLE );
3163 bSetDOSExtendedError = TRUE;
3166 break;
3168 case 0x56: /* LONG FILENAME - RENAME FILE */
3169 if (!INT21_RenameFile(context))
3170 bSetDOSExtendedError = TRUE;
3171 break;
3173 case 0x60: /* LONG FILENAME - CONVERT PATH */
3175 WCHAR res[MAX_PATH];
3177 switch (CL_reg(context))
3179 case 0x00: /* "truename" - Canonicalize path */
3181 * FIXME: This is not 100% equal to 0x01 case,
3182 * if you fix this, fix int21 subfunction 0x60, too.
3185 case 0x01: /* Get short filename or path */
3186 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
3187 if (!GetShortPathNameW(pathW, res, 67))
3188 bSetDOSExtendedError = TRUE;
3189 else
3191 SET_AX( context, 0 );
3192 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
3193 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3194 67, NULL, NULL);
3196 break;
3198 case 0x02: /* Get canonical long filename or path */
3199 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
3200 if (!GetFullPathNameW(pathW, 128, res, NULL))
3201 bSetDOSExtendedError = TRUE;
3202 else
3204 SET_AX( context, 0 );
3205 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
3206 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3207 128, NULL, NULL);
3209 break;
3210 default:
3211 FIXME("Unimplemented long file name function:\n");
3212 INT_BARF( context, 0x21 );
3213 SET_CFLAG(context);
3214 SET_AL( context, 0 );
3215 break;
3218 break;
3220 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
3221 if (!INT21_CreateFile( context, context->Esi, TRUE,
3222 BX_reg(context), DL_reg(context) ))
3223 bSetDOSExtendedError = TRUE;
3224 break;
3226 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
3228 DWORD filename_len, flags;
3229 WCHAR dstW[8];
3231 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
3233 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA);
3234 SET_AX( context, 0 );
3235 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3236 if (!GetVolumeInformationW( pathW, NULL, 0, NULL, &filename_len,
3237 &flags, dstW, 8 ))
3239 INT_BARF( context, 0x21 );
3240 SET_CFLAG(context);
3241 break;
3243 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
3244 SET_CX( context, filename_len );
3245 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
3246 WideCharToMultiByte(CP_OEMCP, 0, dstW, -1,
3247 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3248 8, NULL, NULL);
3250 break;
3252 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
3254 HGLOBAL16 h16 = BX_reg(context);
3255 HANDLE* ptr;
3257 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
3258 BX_reg(context));
3259 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
3261 if (!FindClose( *ptr )) bSetDOSExtendedError = TRUE;
3262 GlobalUnlock16( h16 );
3263 GlobalFree16( h16 );
3265 else
3267 SetLastError( ERROR_INVALID_HANDLE );
3268 bSetDOSExtendedError = TRUE;
3271 break;
3273 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
3275 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
3276 BY_HANDLE_FILE_INFORMATION *info =
3277 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3279 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
3281 if (!GetFileInformationByHandle(handle, info))
3282 bSetDOSExtendedError = TRUE;
3284 break;
3286 case 0xa7: /* LONG FILENAME - CONVERT TIME */
3287 switch (BL_reg(context))
3289 case 0x00: /* FILE TIME TO DOS TIME */
3291 WORD date, time;
3292 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3293 context->SegDs,
3294 context->Esi);
3296 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
3298 FileTimeToDosDateTime( filetime, &date, &time );
3300 SET_DX( context, date );
3301 SET_CX( context, time );
3304 * FIXME: BH has number of 10-millisecond units
3305 * past time in CX.
3307 SET_BH( context, 0 );
3309 break;
3311 case 0x01: /* DOS TIME TO FILE TIME */
3313 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3314 context->SegEs,
3315 context->Edi);
3317 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
3320 * FIXME: BH has number of 10-millisecond units
3321 * past time in CX.
3323 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
3324 filetime );
3326 break;
3328 default:
3329 INT_BARF( context, 0x21 );
3330 break;
3332 break;
3334 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
3335 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3336 case 0xaa: /* LONG FILENAME - SUBST */
3337 default:
3338 FIXME("Unimplemented long file name function:\n");
3339 INT_BARF( context, 0x21 );
3340 SET_CFLAG(context);
3341 SET_AL( context, 0 );
3342 break;
3345 if (bSetDOSExtendedError)
3347 SET_AX( context, GetLastError() );
3348 SET_CFLAG( context );
3353 /***********************************************************************
3354 * INT21_RenameFile
3356 * Handler for:
3357 * - function 0x56
3358 * - subfunction 0x56 of function 0x71
3359 * - subfunction 0xff of function 0x43 (CL == 0x56)
3361 static BOOL INT21_RenameFile( CONTEXT86 *context )
3363 WCHAR fromW[MAX_PATH];
3364 WCHAR toW[MAX_PATH];
3365 char *fromA = CTX_SEG_OFF_TO_LIN(context,
3366 context->SegDs,context->Edx);
3367 char *toA = CTX_SEG_OFF_TO_LIN(context,
3368 context->SegEs,context->Edi);
3370 TRACE( "RENAME FILE %s to %s\n", fromA, toA );
3371 MultiByteToWideChar(CP_OEMCP, 0, fromA, -1, fromW, MAX_PATH);
3372 MultiByteToWideChar(CP_OEMCP, 0, toA, -1, toW, MAX_PATH);
3374 return MoveFileW( fromW, toW );
3378 /***********************************************************************
3379 * INT21_NetworkFunc
3381 * Handler for:
3382 * - function 0x5e
3384 static BOOL INT21_NetworkFunc (CONTEXT86 *context)
3386 switch (AL_reg(context))
3388 case 0x00: /* Get machine name. */
3390 WCHAR dstW[MAX_COMPUTERNAME_LENGTH + 1];
3391 DWORD s = sizeof(dstW) / sizeof(WCHAR);
3392 int len;
3394 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
3395 TRACE("getting machine name to %p\n", dst);
3396 if (!GetComputerNameW(dstW, &s) ||
3397 !WideCharToMultiByte(CP_OEMCP, 0, dstW, -1, dst, 16, NULL, NULL))
3399 WARN("failed!\n");
3400 SetLastError( ER_NoNetwork );
3401 return TRUE;
3403 for (len = strlen(dst); len < 15; len++) dst[len] = ' ';
3404 dst[15] = 0;
3405 SET_CH( context, 1 ); /* Valid */
3406 SET_CL( context, 1 ); /* NETbios number??? */
3407 TRACE("returning %s\n", debugstr_an(dst, 16));
3408 return FALSE;
3411 default:
3412 SetLastError( ER_NoNetwork );
3413 return TRUE;
3417 /******************************************************************
3418 * INT21_GetDiskSerialNumber
3421 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
3423 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3424 WCHAR path[] = {'A',':',0}, label[11];
3425 DWORD serial;
3427 path[0] += INT21_MapDrive(BL_reg(context));
3428 if (!GetVolumeInformationW( path, label, 11, &serial, NULL, NULL, NULL, 0))
3430 SetLastError( ERROR_INVALID_DRIVE );
3431 return 0;
3434 *(WORD *)dataptr = 0;
3435 memcpy(dataptr + 2, &serial, sizeof(DWORD));
3436 WideCharToMultiByte(CP_OEMCP, 0, label, 11, (LPSTR)dataptr + 6, 11, NULL, NULL);
3437 memcpy(dataptr + 17, "FAT16 ", 8);
3438 return 1;
3442 /******************************************************************
3443 * INT21_SetDiskSerialNumber
3446 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
3448 #if 0
3449 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3450 int drive = INT21_MapDrive(BL_reg(context));
3452 if (!is_valid_drive(drive))
3454 SetLastError( ERROR_INVALID_DRIVE );
3455 return 0;
3458 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
3459 return 1;
3460 #else
3461 FIXME("Setting drive serial number is no longer supported\n");
3462 SetLastError( ERROR_NOT_SUPPORTED );
3463 return 0;
3464 #endif
3468 /******************************************************************
3469 * INT21_GetFreeDiskSpace
3472 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
3474 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
3475 WCHAR root[] = {'A',':','\\',0};
3476 const DWORD max_clusters = 0x3d83;
3477 const DWORD max_sectors_per_cluster = 0x7f;
3478 const DWORD max_bytes_per_sector = 0x200;
3480 root[0] += INT21_MapDrive(DL_reg(context));
3481 if (!GetDiskFreeSpaceW( root, &cluster_sectors, &sector_bytes,
3482 &free_clusters, &total_clusters )) return 0;
3484 /* Some old win31 apps (Lotus SmartSuite 5.1) crap out if there's too
3485 * much disk space, so Windows XP seems to apply the following limits:
3486 * cluster_sectors <= 0x7f
3487 * sector size <= 0x200
3488 * clusters <= 0x3D83
3489 * This means total reported space is limited to about 1GB.
3492 /* Make sure bytes-per-sector is in [, max] */
3493 while (sector_bytes > max_bytes_per_sector) {
3494 sector_bytes >>= 1;
3495 free_clusters <<= 1;
3496 total_clusters <<= 1;
3498 /* Then make sure sectors-per-cluster is in [max/2, max]. */
3499 while (cluster_sectors <= max_sectors_per_cluster/2) {
3500 cluster_sectors <<= 1;
3501 free_clusters >>= 1;
3502 total_clusters >>= 1;
3504 while (cluster_sectors > max_sectors_per_cluster) {
3505 cluster_sectors >>= 1;
3506 free_clusters <<= 1;
3507 total_clusters <<= 1;
3510 /* scale up sectors_per_cluster to exactly max_sectors_per_cluster.
3511 * We could skip this, but that would impose an artificially low
3512 * limit on reported disk space.
3513 * To avoid overflow, first apply a preliminary cap on sector count;
3514 * this will not affect the correctness of the final result,
3515 * because if the preliminary cap hits, the final one will, too.
3517 if (total_clusters > 4 * max_clusters)
3518 total_clusters = 4 * max_clusters;
3519 if (free_clusters > 4 * max_clusters)
3520 free_clusters = 4 * max_clusters;
3521 if (cluster_sectors < max_sectors_per_cluster) {
3522 free_clusters *= cluster_sectors;
3523 free_clusters /= max_sectors_per_cluster;
3524 total_clusters *= cluster_sectors;
3525 total_clusters /= max_sectors_per_cluster;
3526 cluster_sectors = max_sectors_per_cluster;
3529 /* Finally, apply real cluster count cap. */
3530 if (total_clusters > max_clusters)
3531 total_clusters = max_clusters;
3532 if (free_clusters > max_clusters)
3533 free_clusters = max_clusters;
3535 SET_AX( context, cluster_sectors );
3536 SET_BX( context, free_clusters );
3537 SET_CX( context, sector_bytes );
3538 SET_DX( context, total_clusters );
3539 return 1;
3542 /******************************************************************
3543 * INT21_GetDriveAllocInfo
3546 static int INT21_GetDriveAllocInfo( CONTEXT86 *context, BYTE drive )
3548 INT21_DPB *dpb;
3550 drive = INT21_MapDrive( drive );
3551 if (!INT21_FillDrivePB( drive )) return 0;
3552 dpb = &(INT21_GetHeapPointer()->misc_dpb_list[drive]);
3553 SET_AL( context, dpb->cluster_sectors + 1 );
3554 SET_CX( context, dpb->sector_bytes );
3555 SET_DX( context, dpb->num_clusters1 );
3557 context->SegDs = INT21_GetHeapSelector( context );
3558 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive].media_ID ) );
3559 return 1;
3562 /***********************************************************************
3563 * INT21_GetExtendedError
3565 static void INT21_GetExtendedError( CONTEXT86 *context )
3567 BYTE class, action, locus;
3568 WORD error = GetLastError();
3570 switch(error)
3572 case ERROR_SUCCESS:
3573 class = action = locus = 0;
3574 break;
3575 case ERROR_DIR_NOT_EMPTY:
3576 class = EC_Exists;
3577 action = SA_Ignore;
3578 locus = EL_Disk;
3579 break;
3580 case ERROR_ACCESS_DENIED:
3581 class = EC_AccessDenied;
3582 action = SA_Abort;
3583 locus = EL_Disk;
3584 break;
3585 case ERROR_CANNOT_MAKE:
3586 class = EC_AccessDenied;
3587 action = SA_Abort;
3588 locus = EL_Unknown;
3589 break;
3590 case ERROR_DISK_FULL:
3591 case ERROR_HANDLE_DISK_FULL:
3592 class = EC_MediaError;
3593 action = SA_Abort;
3594 locus = EL_Disk;
3595 break;
3596 case ERROR_FILE_EXISTS:
3597 case ERROR_ALREADY_EXISTS:
3598 class = EC_Exists;
3599 action = SA_Abort;
3600 locus = EL_Disk;
3601 break;
3602 case ERROR_FILE_NOT_FOUND:
3603 class = EC_NotFound;
3604 action = SA_Abort;
3605 locus = EL_Disk;
3606 break;
3607 case ERROR_GEN_FAILURE:
3608 class = EC_SystemFailure;
3609 action = SA_Abort;
3610 locus = EL_Unknown;
3611 break;
3612 case ERROR_INVALID_DRIVE:
3613 class = EC_MediaError;
3614 action = SA_Abort;
3615 locus = EL_Disk;
3616 break;
3617 case ERROR_INVALID_HANDLE:
3618 class = EC_ProgramError;
3619 action = SA_Abort;
3620 locus = EL_Disk;
3621 break;
3622 case ERROR_LOCK_VIOLATION:
3623 class = EC_AccessDenied;
3624 action = SA_Abort;
3625 locus = EL_Disk;
3626 break;
3627 case ERROR_NO_MORE_FILES:
3628 class = EC_MediaError;
3629 action = SA_Abort;
3630 locus = EL_Disk;
3631 break;
3632 case ER_NoNetwork:
3633 class = EC_NotFound;
3634 action = SA_Abort;
3635 locus = EL_Network;
3636 break;
3637 case ERROR_NOT_ENOUGH_MEMORY:
3638 class = EC_OutOfResource;
3639 action = SA_Abort;
3640 locus = EL_Memory;
3641 break;
3642 case ERROR_PATH_NOT_FOUND:
3643 class = EC_NotFound;
3644 action = SA_Abort;
3645 locus = EL_Disk;
3646 break;
3647 case ERROR_SEEK:
3648 class = EC_NotFound;
3649 action = SA_Ignore;
3650 locus = EL_Disk;
3651 break;
3652 case ERROR_SHARING_VIOLATION:
3653 class = EC_Temporary;
3654 action = SA_Retry;
3655 locus = EL_Disk;
3656 break;
3657 case ERROR_TOO_MANY_OPEN_FILES:
3658 class = EC_ProgramError;
3659 action = SA_Abort;
3660 locus = EL_Disk;
3661 break;
3662 default:
3663 FIXME("Unknown error %d\n", error );
3664 class = EC_SystemFailure;
3665 action = SA_Abort;
3666 locus = EL_Unknown;
3667 break;
3669 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3670 error, class, action, locus );
3671 SET_AX( context, error );
3672 SET_BH( context, class );
3673 SET_BL( context, action );
3674 SET_CH( context, locus );
3677 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
3679 static int counter = 0;
3680 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
3681 char *p = name + strlen(name);
3683 /* despite what Ralf Brown says, some programs seem to call without
3684 * ending backslash (DOS accepts that, so we accept it too) */
3685 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
3687 for (;;)
3689 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
3690 counter = (counter + 1) % 1000;
3692 SET_AX( context,
3693 Win32HandleToDosFileHandle(
3694 CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
3695 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
3696 CREATE_NEW, 0, 0 ) ) );
3697 if (AX_reg(context) != HFILE_ERROR16)
3699 TRACE("created %s\n", name );
3700 return TRUE;
3702 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
3706 /***********************************************************************
3707 * DOSFS_ToDosFCBFormat
3709 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3710 * expanding wild cards and converting to upper-case in the process.
3711 * File name can be terminated by '\0', '\\' or '/'.
3712 * Return FALSE if the name is not a valid DOS name.
3713 * 'buffer' must be at least 12 characters long.
3715 /* Chars we don't want to see in DOS file names */
3716 static BOOL INT21_ToDosFCBFormat( LPCWSTR name, LPWSTR buffer )
3718 static const WCHAR invalid_chars[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3719 LPCWSTR p = name;
3720 int i;
3722 /* Check for "." and ".." */
3723 if (*p == '.')
3725 p++;
3726 buffer[0] = '.';
3727 for(i = 1; i < 11; i++) buffer[i] = ' ';
3728 buffer[11] = 0;
3729 if (*p == '.')
3731 buffer[1] = '.';
3732 p++;
3734 return (!*p || (*p == '/') || (*p == '\\'));
3737 for (i = 0; i < 8; i++)
3739 switch(*p)
3741 case '\0':
3742 case '\\':
3743 case '/':
3744 case '.':
3745 buffer[i] = ' ';
3746 break;
3747 case '?':
3748 p++;
3749 /* fall through */
3750 case '*':
3751 buffer[i] = '?';
3752 break;
3753 default:
3754 if (strchrW( invalid_chars, *p )) return FALSE;
3755 buffer[i] = toupperW(*p);
3756 p++;
3757 break;
3761 if (*p == '*')
3763 /* Skip all chars after wildcard up to first dot */
3764 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
3766 else
3768 /* Check if name too long */
3769 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return FALSE;
3771 if (*p == '.') p++; /* Skip dot */
3773 for (i = 8; i < 11; i++)
3775 switch(*p)
3777 case '\0':
3778 case '\\':
3779 case '/':
3780 buffer[i] = ' ';
3781 break;
3782 case '.':
3783 return FALSE; /* Second extension not allowed */
3784 case '?':
3785 p++;
3786 /* fall through */
3787 case '*':
3788 buffer[i] = '?';
3789 break;
3790 default:
3791 if (strchrW( invalid_chars, *p )) return FALSE;
3792 buffer[i] = toupperW(*p);
3793 p++;
3794 break;
3797 buffer[11] = '\0';
3799 /* at most 3 character of the extension are processed
3800 * is something behind this ?
3802 while (*p == '*' || *p == ' ') p++; /* skip wildcards and spaces */
3803 return (!*p || (*p == '/') || (*p == '\\'));
3806 static HANDLE INT21_FindHandle;
3807 static const WCHAR *INT21_FindPath; /* will point to current dta->fullPath search */
3809 /******************************************************************
3810 * INT21_FindFirst
3812 static int INT21_FindFirst( CONTEXT86 *context )
3814 WCHAR *p, *q;
3815 const char *path;
3816 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3817 WCHAR maskW[12], pathW[MAX_PATH];
3818 static const WCHAR wildcardW[] = {'*','.','*',0};
3820 path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3821 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
3823 p = strrchrW( pathW, '\\');
3824 q = strrchrW( pathW, '/');
3825 if (q>p) p = q;
3826 if (!p)
3828 if (pathW[0] && pathW[1] == ':') p = pathW + 2;
3829 else p = pathW;
3831 else p++;
3833 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3834 * (doesn't matter as it is set below anyway)
3836 if (!INT21_ToDosFCBFormat( p, maskW ))
3838 SetLastError( ERROR_FILE_NOT_FOUND );
3839 SET_AX( context, ERROR_FILE_NOT_FOUND );
3840 SET_CFLAG(context);
3841 return 0;
3843 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
3845 dta->fullPath = HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW) + (p - pathW)*sizeof(WCHAR) );
3846 memcpy( dta->fullPath, pathW, (p - pathW) * sizeof(WCHAR) );
3847 memcpy( dta->fullPath + (p - pathW), wildcardW, sizeof(wildcardW) );
3848 /* we must have a fully qualified file name in dta->fullPath
3849 * (we could have a UNC path, but this would lead to some errors later on)
3851 dta->drive = toupperW(dta->fullPath[0]) - 'A';
3852 dta->count = 0;
3853 dta->search_attr = CL_reg(context);
3854 return 1;
3857 /******************************************************************
3858 * match_short
3860 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3862 static BOOL match_short(LPCWSTR shortW, LPCSTR maskA)
3864 WCHAR mask[11], file[12];
3865 int i;
3867 if (!INT21_ToDosFCBFormat( shortW, file )) return FALSE;
3868 MultiByteToWideChar(CP_OEMCP, 0, maskA, 11, mask, 11);
3869 for (i = 0; i < 11; i++)
3870 if (mask[i] != '?' && mask[i] != file[i]) return FALSE;
3871 return TRUE;
3874 static unsigned INT21_FindHelper(LPCWSTR fullPath, unsigned drive, unsigned count,
3875 LPCSTR mask, unsigned search_attr,
3876 WIN32_FIND_DATAW* entry)
3878 unsigned ncalls;
3880 if ((search_attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
3882 WCHAR path[] = {' ',':',0};
3884 if (count) return 0;
3885 path[0] = drive + 'A';
3886 if (!GetVolumeInformationW(path, entry->cAlternateFileName, 13, NULL, NULL, NULL, NULL, 0)) return 0;
3887 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftCreationTime );
3888 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftLastAccessTime );
3889 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftLastWriteTime );
3890 entry->dwFileAttributes = FA_LABEL;
3891 entry->nFileSizeHigh = entry->nFileSizeLow = 0;
3892 TRACE("returning %s as label\n", debugstr_w(entry->cAlternateFileName));
3893 return 1;
3896 if (!INT21_FindHandle || INT21_FindPath != fullPath || count == 0)
3898 if (INT21_FindHandle) FindClose(INT21_FindHandle);
3899 INT21_FindHandle = FindFirstFileW(fullPath, entry);
3900 if (INT21_FindHandle == INVALID_HANDLE_VALUE)
3902 INT21_FindHandle = 0;
3903 return 0;
3905 INT21_FindPath = fullPath;
3906 /* we need to resync search */
3907 ncalls = count;
3909 else ncalls = 1;
3911 while (ncalls-- != 0)
3913 if (!FindNextFileW(INT21_FindHandle, entry))
3915 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3916 return 0;
3919 while (count < 0xffff)
3921 count++;
3922 /* Check the file attributes, and path */
3923 if (!(entry->dwFileAttributes & ~search_attr) &&
3924 match_short(entry->cAlternateFileName[0] ? entry->cAlternateFileName : entry->cFileName,
3925 mask))
3927 return count;
3929 if (!FindNextFileW(INT21_FindHandle, entry))
3931 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3932 return 0;
3935 WARN("Too many directory entries in %s\n", debugstr_w(fullPath) );
3936 return 0;
3939 /******************************************************************
3940 * INT21_FindNext
3942 static int INT21_FindNext( CONTEXT86 *context )
3944 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3945 DWORD attr = dta->search_attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
3946 WIN32_FIND_DATAW entry;
3947 int n;
3949 if (!dta->fullPath) return 0;
3951 n = INT21_FindHelper(dta->fullPath, dta->drive, dta->count,
3952 dta->mask, attr, &entry);
3953 if (n)
3955 dta->fileattr = entry.dwFileAttributes;
3956 dta->filesize = entry.nFileSizeLow;
3957 FileTimeToDosDateTime( &entry.ftLastWriteTime, &dta->filedate, &dta->filetime );
3958 if (entry.cAlternateFileName[0])
3959 WideCharToMultiByte(CP_OEMCP, 0, entry.cAlternateFileName, -1,
3960 dta->filename, 13, NULL, NULL);
3961 else
3962 WideCharToMultiByte(CP_OEMCP, 0, entry.cFileName, -1, dta->filename, 13, NULL, NULL);
3964 if (!memchr(dta->mask,'?',11))
3966 /* wildcardless search, release resources in case no findnext will
3967 * be issued, and as a workaround in case file creation messes up
3968 * findnext, as sometimes happens with pkunzip
3970 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3971 INT21_FindPath = dta->fullPath = NULL;
3973 dta->count = n;
3974 return 1;
3976 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3977 INT21_FindPath = dta->fullPath = NULL;
3978 return 0;
3981 /* microsoft's programmers should be shot for using CP/M style int21
3982 calls in Windows for Workgroup's winfile.exe */
3984 /******************************************************************
3985 * INT21_FindFirstFCB
3988 static int INT21_FindFirstFCB( CONTEXT86 *context )
3990 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3991 FINDFILE_FCB *pFCB;
3992 int drive;
3993 WCHAR p[] = {' ',':',};
3995 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
3996 else pFCB = (FINDFILE_FCB *)fcb;
3997 drive = INT21_MapDrive( pFCB->drive );
3998 if (drive == MAX_DOS_DRIVES) return 0;
4000 p[0] = 'A' + drive;
4001 pFCB->fullPath = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
4002 if (!pFCB->fullPath) return 0;
4003 GetLongPathNameW(p, pFCB->fullPath, MAX_PATH);
4004 pFCB->count = 0;
4005 return 1;
4008 /******************************************************************
4009 * INT21_FindNextFCB
4012 static int INT21_FindNextFCB( CONTEXT86 *context )
4014 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4015 FINDFILE_FCB *pFCB;
4016 LPBYTE pResult = INT21_GetCurrentDTA(context);
4017 DOS_DIRENTRY_LAYOUT *ddl;
4018 WIN32_FIND_DATAW entry;
4019 BYTE attr;
4020 int n;
4021 WCHAR nameW[12];
4023 if (*fcb == 0xff) /* extended FCB ? */
4025 attr = fcb[6];
4026 pFCB = (FINDFILE_FCB *)(fcb + 7);
4028 else
4030 attr = 0;
4031 pFCB = (FINDFILE_FCB *)fcb;
4034 if (!pFCB->fullPath) return 0;
4035 n = INT21_FindHelper(pFCB->fullPath, INT21_MapDrive( pFCB->drive ),
4036 pFCB->count, pFCB->filename, attr, &entry);
4037 if (!n)
4039 HeapFree( GetProcessHeap(), 0, pFCB->fullPath );
4040 INT21_FindPath = pFCB->fullPath = NULL;
4041 return 0;
4043 pFCB->count += n;
4045 if (*fcb == 0xff)
4047 /* place extended FCB header before pResult if called with extended FCB */
4048 *pResult = 0xff;
4049 pResult += 6; /* leave reserved field behind */
4050 *pResult++ = entry.dwFileAttributes;
4052 *pResult++ = INT21_MapDrive( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
4053 ddl = (DOS_DIRENTRY_LAYOUT*)pResult;
4054 ddl->fileattr = entry.dwFileAttributes;
4055 ddl->cluster = 0; /* what else? */
4056 ddl->filesize = entry.nFileSizeLow;
4057 memset( ddl->reserved, 0, sizeof(ddl->reserved) );
4058 FileTimeToDosDateTime( &entry.ftLastWriteTime,
4059 &ddl->filedate, &ddl->filetime );
4061 /* Convert file name to FCB format */
4062 if (entry.cAlternateFileName[0])
4063 INT21_ToDosFCBFormat( entry.cAlternateFileName, nameW );
4064 else
4065 INT21_ToDosFCBFormat( entry.cFileName, nameW );
4066 WideCharToMultiByte(CP_OEMCP, 0, nameW, 11, ddl->filename, 11, NULL, NULL);
4067 return 1;
4071 /******************************************************************
4072 * INT21_ParseFileNameIntoFCB
4075 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
4077 char *filename =
4078 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
4079 char *fcb =
4080 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
4081 char *s;
4082 WCHAR *buffer;
4083 WCHAR fcbW[12];
4084 INT buffer_len, len;
4086 SET_AL( context, 0xff ); /* failed */
4088 TRACE("filename: '%s'\n", filename);
4090 s = filename;
4091 while (*s && (*s != ' ') && (*s != '\r') && (*s != '\n'))
4092 s++;
4093 len = filename - s;
4095 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
4096 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
4097 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
4098 buffer[len] = 0;
4099 INT21_ToDosFCBFormat(buffer, fcbW);
4100 HeapFree(GetProcessHeap(), 0, buffer);
4101 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
4102 *fcb = 0;
4103 TRACE("FCB: '%s'\n", fcb + 1);
4105 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
4107 /* point DS:SI to first unparsed character */
4108 SET_SI( context, context->Esi + (int)s - (int)filename );
4111 static BOOL INT21_Dup2(HFILE16 hFile1, HFILE16 hFile2)
4113 HFILE16 res = HFILE_ERROR16;
4114 HANDLE handle, new_handle;
4115 #define DOS_TABLE_SIZE 256
4116 DWORD map[DOS_TABLE_SIZE / 32];
4117 int i;
4119 handle = DosFileHandleToWin32Handle(hFile1);
4120 if (handle == INVALID_HANDLE_VALUE)
4121 return FALSE;
4123 _lclose16(hFile2);
4124 /* now loop to allocate the same one... */
4125 memset(map, 0, sizeof(map));
4126 for (i = 0; i < DOS_TABLE_SIZE; i++)
4128 if (!DuplicateHandle(GetCurrentProcess(), handle,
4129 GetCurrentProcess(), &new_handle,
4130 0, FALSE, DUPLICATE_SAME_ACCESS))
4132 res = HFILE_ERROR16;
4133 break;
4135 res = Win32HandleToDosFileHandle(new_handle);
4136 if (res == HFILE_ERROR16 || res == hFile2) break;
4137 map[res / 32] |= 1 << (res % 32);
4139 /* clean up the allocated slots */
4140 for (i = 0; i < DOS_TABLE_SIZE; i++)
4142 if (map[i / 32] & (1 << (i % 32)))
4143 _lclose16((HFILE16)i);
4145 return res == hFile2;
4149 /***********************************************************************
4150 * DOSVM_Int21Handler
4152 * Interrupt 0x21 handler.
4154 void WINAPI DOSVM_Int21Handler( CONTEXT86 *context )
4156 BOOL bSetDOSExtendedError = FALSE;
4158 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
4159 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
4160 AX_reg(context), BX_reg(context),
4161 CX_reg(context), DX_reg(context),
4162 SI_reg(context), DI_reg(context),
4163 (WORD)context->SegDs, (WORD)context->SegEs,
4164 context->EFlags );
4167 * Extended error is used by (at least) functions 0x2f to 0x62.
4168 * Function 0x59 returns extended error and error should not
4169 * be cleared before handling the function.
4171 if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59)
4172 SetLastError(0);
4174 RESET_CFLAG(context); /* Not sure if this is a good idea. */
4176 switch(AH_reg(context))
4178 case 0x00: /* TERMINATE PROGRAM */
4179 TRACE("TERMINATE PROGRAM\n");
4180 if (DOSVM_IsWin16())
4181 ExitThread( 0 );
4182 else if(ISV86(context))
4183 MZ_Exit( context, FALSE, 0 );
4184 else
4185 ERR( "Called from DOS protected mode\n" );
4186 break;
4188 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
4190 BYTE ascii;
4191 TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
4192 INT21_ReadChar( &ascii, context );
4193 SET_AL( context, ascii );
4195 * FIXME: What to echo when extended keycodes are read?
4197 DOSVM_PutChar(AL_reg(context));
4199 break;
4201 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
4202 TRACE("Write Character to Standard Output\n");
4203 DOSVM_PutChar(DL_reg(context));
4204 break;
4206 case 0x03: /* READ CHARACTER FROM STDAUX */
4207 case 0x04: /* WRITE CHARACTER TO STDAUX */
4208 case 0x05: /* WRITE CHARACTER TO PRINTER */
4209 INT_BARF( context, 0x21 );
4210 break;
4212 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
4213 if (DL_reg(context) == 0xff)
4215 TRACE("Direct Console Input\n");
4217 if (INT21_ReadChar( NULL, NULL ))
4219 BYTE ascii;
4220 INT21_ReadChar( &ascii, context );
4221 SET_AL( context, ascii );
4222 RESET_ZFLAG( context );
4224 else
4226 /* no character available */
4227 SET_AL( context, 0 );
4228 SET_ZFLAG( context );
4231 else
4233 TRACE("Direct Console Output\n");
4234 DOSVM_PutChar(DL_reg(context));
4236 * At least DOS versions 2.1-7.0 return character
4237 * that was written in AL register.
4239 SET_AL( context, DL_reg(context) );
4241 break;
4243 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
4245 BYTE ascii;
4246 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
4247 INT21_ReadChar( &ascii, context );
4248 SET_AL( context, ascii );
4250 break;
4252 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
4254 BYTE ascii;
4255 TRACE("CHARACTER INPUT WITHOUT ECHO\n");
4256 INT21_ReadChar( &ascii, context );
4257 SET_AL( context, ascii );
4259 break;
4261 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
4262 TRACE("WRITE '$'-terminated string from %04X:%04X to stdout\n",
4263 context->SegDs, DX_reg(context) );
4265 LPSTR data = CTX_SEG_OFF_TO_LIN( context,
4266 context->SegDs, context->Edx );
4267 LPSTR p = data;
4268 DWORD w;
4270 * Do NOT use strchr() to calculate the string length,
4271 * as '\0' is valid string content, too!
4272 * Maybe we should check for non-'$' strings, but DOS doesn't.
4274 while (*p != '$') p++;
4276 if (DOSVM_IsWin16())
4277 WriteFile( DosFileHandleToWin32Handle(1),
4278 data, p - data, &w, NULL );
4279 else
4280 for(; data != p; data++)
4281 DOSVM_PutChar( *data );
4283 SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
4285 break;
4287 case 0x0a: /* BUFFERED INPUT */
4289 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context,
4290 context->SegDs,
4291 context->Edx);
4292 WORD result;
4294 TRACE( "BUFFERED INPUT (size=%d)\n", ptr[0] );
4297 * FIXME: Some documents state that
4298 * ptr[1] holds number of chars from last input which
4299 * may be recalled on entry, other documents do not mention
4300 * this at all.
4302 if (ptr[1])
4303 TRACE( "Handle old chars in buffer!\n" );
4306 * ptr[0] - capacity (includes terminating CR)
4307 * ptr[1] - characters read (excludes terminating CR)
4309 result = INT21_BufferedInput( context, ptr + 2, ptr[0] );
4310 if (result > 0)
4311 ptr[1] = (BYTE)result - 1;
4312 else
4313 ptr[1] = 0;
4315 break;
4317 case 0x0b: /* GET STDIN STATUS */
4318 TRACE( "GET STDIN STATUS\n" );
4320 if (INT21_ReadChar( NULL, NULL ))
4321 SET_AL( context, 0xff ); /* character available */
4322 else
4323 SET_AL( context, 0 ); /* no character available */
4325 break;
4327 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
4329 BYTE al = AL_reg(context); /* Input function to execute after flush. */
4331 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
4333 /* FIXME: buffers are not flushed */
4336 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
4337 * int21 function identified by AL will be called.
4339 if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
4341 SET_AH( context, al );
4342 DOSVM_Int21Handler( context );
4345 break;
4347 case 0x0d: /* DISK BUFFER FLUSH */
4348 TRACE("DISK BUFFER FLUSH ignored\n");
4349 break;
4351 case 0x0e: /* SELECT DEFAULT DRIVE */
4352 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context) );
4353 INT21_SetCurrentDrive( DL_reg(context) );
4354 SET_AL( context, MAX_DOS_DRIVES );
4355 break;
4357 case 0x0f: /* OPEN FILE USING FCB */
4358 INT21_OpenFileUsingFCB( context );
4359 break;
4361 case 0x10: /* CLOSE FILE USING FCB */
4362 INT21_CloseFileUsingFCB( context );
4363 break;
4365 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
4366 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
4367 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
4368 if (!INT21_FindFirstFCB(context))
4370 SET_AL( context, 0xff );
4371 break;
4373 /* else fall through */
4375 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
4376 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
4377 break;
4379 case 0x13: /* DELETE FILE USING FCB */
4380 INT_BARF( context, 0x21 );
4381 break;
4383 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
4384 INT21_SequentialReadFromFCB( context );
4385 break;
4387 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
4388 INT21_SequentialWriteToFCB( context );
4389 break;
4391 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
4392 case 0x17: /* RENAME FILE USING FCB */
4393 INT_BARF( context, 0x21 );
4394 break;
4396 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4397 SET_AL( context, 0 );
4398 break;
4400 case 0x19: /* GET CURRENT DEFAULT DRIVE */
4401 SET_AL( context, INT21_GetCurrentDrive() );
4402 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context ) );
4403 break;
4405 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
4406 TRACE( "SET DISK TRANSFER AREA ADDRESS %04X:%04X\n",
4407 context->SegDs, DX_reg(context) );
4409 TDB *task = GlobalLock16( GetCurrentTask() );
4410 task->dta = MAKESEGPTR( context->SegDs, DX_reg(context) );
4412 break;
4414 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4415 if (!INT21_GetDriveAllocInfo(context, 0))
4416 SET_AX( context, 0xffff );
4417 break;
4419 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4420 if (!INT21_GetDriveAllocInfo(context, DL_reg(context)))
4421 SET_AX( context, 0xffff );
4422 break;
4424 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4425 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4426 SET_AL( context, 0 );
4427 break;
4429 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4431 BYTE drive = INT21_MapDrive( 0 );
4432 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4434 if (INT21_FillDrivePB( drive ))
4436 SET_AL( context, 0x00 ); /* success */
4437 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4438 context->SegDs = INT21_GetHeapSelector( context );
4440 else
4442 SET_AL( context, 0xff ); /* invalid or network drive */
4445 break;
4447 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4448 SET_AL( context, 0 );
4449 break;
4451 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4452 INT21_ReadRandomRecordFromFCB( context );
4453 break;
4455 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4456 INT21_WriteRandomRecordToFCB( context );
4457 break;
4459 case 0x23: /* GET FILE SIZE FOR FCB */
4460 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4461 INT_BARF( context, 0x21 );
4462 break;
4464 case 0x25: /* SET INTERRUPT VECTOR */
4465 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4467 FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
4468 if (!ISV86(context) && DOSVM_IsWin16())
4469 DOSVM_SetPMHandler16( AL_reg(context), ptr );
4470 else
4471 DOSVM_SetRMHandler( AL_reg(context), ptr );
4473 break;
4475 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4476 INT_BARF( context, 0x21 );
4477 break;
4479 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4480 INT21_RandomBlockReadFromFCB( context );
4481 break;
4483 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4484 INT21_RandomBlockWriteToFCB( context );
4485 break;
4487 case 0x29: /* PARSE FILENAME INTO FCB */
4488 INT21_ParseFileNameIntoFCB(context);
4489 break;
4491 case 0x2a: /* GET SYSTEM DATE */
4492 TRACE( "GET SYSTEM DATE\n" );
4494 SYSTEMTIME systime;
4495 GetLocalTime( &systime );
4496 SET_CX( context, systime.wYear );
4497 SET_DH( context, systime.wMonth );
4498 SET_DL( context, systime.wDay );
4499 SET_AL( context, systime.wDayOfWeek );
4501 break;
4503 case 0x2b: /* SET SYSTEM DATE */
4504 TRACE( "SET SYSTEM DATE\n" );
4506 WORD year = CX_reg(context);
4507 BYTE month = DH_reg(context);
4508 BYTE day = DL_reg(context);
4510 if (year >= 1980 && year <= 2099 &&
4511 month >= 1 && month <= 12 &&
4512 day >= 1 && day <= 31)
4514 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4515 day, month, year );
4516 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4518 else
4520 SET_AL( context, 0xff ); /* invalid date */
4521 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4522 day, month, year );
4525 break;
4527 case 0x2c: /* GET SYSTEM TIME */
4528 TRACE( "GET SYSTEM TIME\n" );
4530 SYSTEMTIME systime;
4531 GetLocalTime( &systime );
4532 SET_CH( context, systime.wHour );
4533 SET_CL( context, systime.wMinute );
4534 SET_DH( context, systime.wSecond );
4535 SET_DL( context, systime.wMilliseconds / 10 );
4537 break;
4539 case 0x2d: /* SET SYSTEM TIME */
4540 if( CH_reg(context) >= 24 || CL_reg(context) >= 60 || DH_reg(context) >= 60 || DL_reg(context) >= 100 ) {
4541 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4542 CH_reg(context), CL_reg(context),
4543 DH_reg(context), DL_reg(context) );
4544 SET_AL( context, 0xFF );
4546 else
4548 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4549 CH_reg(context), CL_reg(context),
4550 DH_reg(context), DL_reg(context) );
4551 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4553 break;
4555 case 0x2e: /* SET VERIFY FLAG */
4556 TRACE("SET VERIFY FLAG ignored\n");
4557 /* we cannot change the behaviour anyway, so just ignore it */
4558 break;
4560 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4561 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4563 TDB *task = GlobalLock16( GetCurrentTask() );
4564 context->SegEs = SELECTOROF( task->dta );
4565 SET_BX( context, OFFSETOF( task->dta ) );
4567 break;
4569 case 0x30: /* GET DOS VERSION */
4570 TRACE( "GET DOS VERSION - %s requested\n",
4571 (AL_reg(context) == 0x00) ? "OEM number" : "version flag" );
4573 if (AL_reg(context) == 0x00)
4574 SET_BH( context, 0xff ); /* OEM number => undefined */
4575 else
4576 SET_BH( context, 0x08 ); /* version flag => DOS is in ROM */
4578 SET_AL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4579 SET_AH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4581 SET_BL( context, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4582 SET_CX( context, 0x3456 );
4583 break;
4585 case 0x31: /* TERMINATE AND STAY RESIDENT */
4586 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4587 break;
4589 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4591 BYTE drive = INT21_MapDrive( DL_reg(context) );
4592 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context) );
4594 if (INT21_FillDrivePB( drive ))
4596 SET_AL( context, 0x00 ); /* success */
4597 SET_DX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4598 context->SegDs = INT21_GetHeapSelector( context );
4600 else
4602 SET_AL( context, 0xff ); /* invalid or network drive */
4605 break;
4607 case 0x33: /* MULTIPLEXED */
4608 switch (AL_reg(context))
4610 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4611 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4612 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4613 break;
4615 case 0x01: /* SET EXTENDED BREAK STATE */
4616 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4617 DOSCONF_GetConfig()->brk_flag = (DL_reg(context) > 0) ? 1 : 0;
4618 break;
4620 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4621 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4622 /* ugly coding in order to stay reentrant */
4623 if (DL_reg(context))
4625 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4626 DOSCONF_GetConfig()->brk_flag = 1;
4628 else
4630 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4631 DOSCONF_GetConfig()->brk_flag = 0;
4633 break;
4635 case 0x05: /* GET BOOT DRIVE */
4636 TRACE("GET BOOT DRIVE\n");
4637 SET_DL( context, 3 );
4638 /* c: is Wine's bootdrive (a: is 1)*/
4639 break;
4641 case 0x06: /* GET TRUE VERSION NUMBER */
4642 TRACE("GET TRUE VERSION NUMBER\n");
4643 SET_BL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4644 SET_BH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4645 SET_DL( context, 0x00 ); /* revision */
4646 SET_DH( context, 0x08 ); /* DOS is in ROM */
4647 break;
4649 default:
4650 INT_BARF( context, 0x21 );
4651 break;
4653 break;
4655 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4656 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4657 context->SegEs = INT21_GetHeapSelector( context );
4658 SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
4659 break;
4661 case 0x35: /* GET INTERRUPT VECTOR */
4662 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4664 FARPROC16 addr;
4665 if (!ISV86(context) && DOSVM_IsWin16())
4666 addr = DOSVM_GetPMHandler16( AL_reg(context) );
4667 else
4668 addr = DOSVM_GetRMHandler( AL_reg(context) );
4669 context->SegEs = SELECTOROF(addr);
4670 SET_BX( context, OFFSETOF(addr) );
4672 break;
4674 case 0x36: /* GET FREE DISK SPACE */
4675 TRACE("GET FREE DISK SPACE FOR DRIVE %s (limited to about 1GB)\n",
4676 INT21_DriveName( DL_reg(context) ));
4677 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
4678 break;
4680 case 0x37: /* SWITCHAR */
4682 switch (AL_reg(context))
4684 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4685 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4686 SET_AL( context, 0x00 ); /* success*/
4687 SET_DL( context, '/' );
4688 break;
4689 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4690 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4691 DL_reg( context ));
4692 SET_AL( context, 0x00 ); /* success*/
4693 break;
4694 default:
4695 INT_BARF( context, 0x21 );
4696 break;
4699 break;
4701 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4702 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4703 if (AL_reg(context))
4705 WORD country = AL_reg(context);
4706 if (country == 0xff)
4707 country = BX_reg(context);
4708 if (country != INT21_GetSystemCountryCode()) {
4709 FIXME( "Requested info on non-default country %04x\n", country );
4710 SET_AX(context, 2);
4711 SET_CFLAG(context);
4714 if(AX_reg(context) != 2 )
4716 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context,
4717 context->SegDs,
4718 context->Edx) );
4719 SET_AX( context, INT21_GetSystemCountryCode() );
4720 SET_BX( context, INT21_GetSystemCountryCode() );
4721 RESET_CFLAG(context);
4723 break;
4725 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4726 if (!INT21_CreateDirectory( context ))
4727 bSetDOSExtendedError = TRUE;
4728 else
4729 RESET_CFLAG(context);
4730 break;
4732 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4734 WCHAR dirW[MAX_PATH];
4735 char *dirA = CTX_SEG_OFF_TO_LIN(context,
4736 context->SegDs, context->Edx);
4738 TRACE( "REMOVE DIRECTORY %s\n", dirA );
4740 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
4742 if (!RemoveDirectoryW( dirW ))
4743 bSetDOSExtendedError = TRUE;
4744 else
4745 RESET_CFLAG(context);
4747 break;
4749 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4750 if (!INT21_SetCurrentDirectory( context ))
4751 bSetDOSExtendedError = TRUE;
4752 else
4753 RESET_CFLAG(context);
4754 break;
4756 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4757 if (!INT21_CreateFile( context, context->Edx, FALSE,
4758 OF_READWRITE | OF_SHARE_COMPAT, 0x12 ))
4759 bSetDOSExtendedError = TRUE;
4760 else
4761 RESET_CFLAG(context);
4762 break;
4764 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4765 if (!INT21_CreateFile( context, context->Edx, FALSE,
4766 AL_reg(context), 0x01 ))
4767 bSetDOSExtendedError = TRUE;
4768 else
4769 RESET_CFLAG(context);
4770 break;
4772 case 0x3e: /* "CLOSE" - CLOSE FILE */
4773 TRACE( "CLOSE handle %d\n", BX_reg(context) );
4774 if (_lclose16( BX_reg(context) ) == HFILE_ERROR16)
4775 bSetDOSExtendedError = TRUE;
4776 else
4777 RESET_CFLAG(context);
4778 break;
4780 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4781 TRACE( "READ from %d to %04X:%04X for %d bytes\n",
4782 BX_reg(context),
4783 context->SegDs,
4784 DX_reg(context),
4785 CX_reg(context) );
4787 DWORD result;
4788 WORD count = CX_reg(context);
4789 BYTE *buffer = CTX_SEG_OFF_TO_LIN( context,
4790 context->SegDs,
4791 context->Edx );
4793 /* Some programs pass a count larger than the allocated buffer */
4794 if (DOSVM_IsWin16())
4796 DWORD maxcount = GetSelectorLimit16( context->SegDs )
4797 - DX_reg(context) + 1;
4798 if (count > maxcount)
4799 count = maxcount;
4803 * FIXME: Reading from console (BX=1) in DOS mode
4804 * does not work as it is supposed to work.
4807 RESET_CFLAG(context); /* set if error */
4808 if (!DOSVM_IsWin16() && BX_reg(context) == 0)
4810 result = INT21_BufferedInput( context, buffer, count );
4811 SET_AX( context, (WORD)result );
4813 else if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context)),
4814 buffer, count, &result, NULL ))
4815 SET_AX( context, (WORD)result );
4816 else
4817 bSetDOSExtendedError = TRUE;
4819 break;
4821 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4822 TRACE( "WRITE from %04X:%04X to handle %d for %d byte\n",
4823 context->SegDs, DX_reg(context),
4824 BX_reg(context), CX_reg(context) );
4826 char *ptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4828 if (!DOSVM_IsWin16() &&
4829 (BX_reg(context) == 1 || BX_reg(context) == 2))
4831 int i;
4832 for(i=0; i<CX_reg(context); i++)
4833 DOSVM_PutChar(ptr[i]);
4834 SET_AX(context, CX_reg(context));
4835 RESET_CFLAG(context);
4837 else
4839 HFILE handle = (HFILE)DosFileHandleToWin32Handle(BX_reg(context));
4840 LONG result = _hwrite( handle, ptr, CX_reg(context) );
4841 if (result == HFILE_ERROR)
4842 bSetDOSExtendedError = TRUE;
4843 else
4845 SET_AX( context, (WORD)result );
4846 RESET_CFLAG(context);
4850 break;
4852 case 0x41: /* "UNLINK" - DELETE FILE */
4854 WCHAR fileW[MAX_PATH];
4855 char *fileA = CTX_SEG_OFF_TO_LIN(context,
4856 context->SegDs,
4857 context->Edx);
4859 TRACE( "UNLINK %s\n", fileA );
4860 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
4862 if (!DeleteFileW( fileW ))
4863 bSetDOSExtendedError = TRUE;
4864 else
4865 RESET_CFLAG(context);
4867 break;
4869 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4870 TRACE( "LSEEK handle %d offset %d from %s\n",
4871 BX_reg(context),
4872 MAKELONG( DX_reg(context), CX_reg(context) ),
4873 (AL_reg(context) == 0) ?
4874 "start of file" : ((AL_reg(context) == 1) ?
4875 "current file position" : "end of file") );
4877 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4878 LONG offset = MAKELONG( DX_reg(context), CX_reg(context) );
4879 DWORD status = SetFilePointer( handle, offset,
4880 NULL, AL_reg(context) );
4881 if (status == INVALID_SET_FILE_POINTER)
4882 bSetDOSExtendedError = TRUE;
4883 else
4885 SET_AX( context, LOWORD(status) );
4886 SET_DX( context, HIWORD(status) );
4887 RESET_CFLAG(context);
4890 break;
4892 case 0x43: /* FILE ATTRIBUTES */
4893 if (!INT21_FileAttributes( context, AL_reg(context), FALSE ))
4894 bSetDOSExtendedError = TRUE;
4895 else
4896 RESET_CFLAG(context);
4897 break;
4899 case 0x44: /* IOCTL */
4900 INT21_Ioctl( context );
4901 break;
4903 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4904 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context) );
4906 HANDLE handle32;
4907 HFILE handle16 = HFILE_ERROR;
4909 if (DuplicateHandle( GetCurrentProcess(),
4910 DosFileHandleToWin32Handle(BX_reg(context)),
4911 GetCurrentProcess(),
4912 &handle32,
4913 0, TRUE, DUPLICATE_SAME_ACCESS ))
4914 handle16 = Win32HandleToDosFileHandle(handle32);
4916 if (handle16 == HFILE_ERROR)
4917 bSetDOSExtendedError = TRUE;
4918 else
4920 SET_AX( context, handle16 );
4921 RESET_CFLAG(context);
4924 break;
4926 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4927 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4928 BX_reg(context), CX_reg(context) );
4929 if (!INT21_Dup2(BX_reg(context), CX_reg(context)))
4930 bSetDOSExtendedError = TRUE;
4931 else
4932 RESET_CFLAG(context);
4933 break;
4935 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4936 if (!INT21_GetCurrentDirectory( context, FALSE ))
4937 bSetDOSExtendedError = TRUE;
4938 else
4939 RESET_CFLAG(context);
4940 break;
4942 case 0x48: /* ALLOCATE MEMORY */
4943 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
4945 WORD selector = 0;
4946 DWORD bytes = (DWORD)BX_reg(context) << 4;
4948 if (!ISV86(context) && DOSVM_IsWin16())
4950 DWORD rv = GlobalDOSAlloc16( bytes );
4951 selector = LOWORD( rv );
4953 else
4954 DOSMEM_AllocBlock( bytes, &selector );
4956 if (selector)
4958 SET_AX( context, selector );
4959 RESET_CFLAG(context);
4961 else
4963 SET_CFLAG(context);
4964 SET_AX( context, 0x0008 ); /* insufficient memory */
4965 SET_BX( context, DOSMEM_Available() >> 4 );
4968 break;
4970 case 0x49: /* FREE MEMORY */
4971 TRACE( "FREE MEMORY segment %04X\n", context->SegEs );
4973 BOOL ok;
4975 if (!ISV86(context) && DOSVM_IsWin16())
4977 ok = !GlobalDOSFree16( context->SegEs );
4979 /* If we don't reset ES_reg, we will fail in the relay code */
4980 if (ok)
4981 context->SegEs = 0;
4983 else
4984 ok = DOSMEM_FreeBlock( PTR_REAL_TO_LIN(context->SegEs, 0) );
4986 if (!ok)
4988 TRACE("FREE MEMORY failed\n");
4989 SET_CFLAG(context);
4990 SET_AX( context, 0x0009 ); /* memory block address invalid */
4993 break;
4995 case 0x4a: /* RESIZE MEMORY BLOCK */
4996 TRACE( "RESIZE MEMORY segment %04X to %d paragraphs\n",
4997 context->SegEs, BX_reg(context) );
4999 DWORD newsize = (DWORD)BX_reg(context) << 4;
5001 if (!ISV86(context) && DOSVM_IsWin16())
5003 FIXME( "Resize memory block - unsupported under Win16\n" );
5004 SET_CFLAG(context);
5006 else
5008 LPVOID address = (void*)((DWORD)context->SegEs << 4);
5009 UINT blocksize = DOSMEM_ResizeBlock( address, newsize, FALSE );
5011 RESET_CFLAG(context);
5012 if (blocksize == (UINT)-1)
5014 SET_CFLAG( context );
5015 SET_AX( context, 0x0009 ); /* illegal address */
5017 else if(blocksize != newsize)
5019 SET_CFLAG( context );
5020 SET_AX( context, 0x0008 ); /* insufficient memory */
5021 SET_BX( context, blocksize >> 4 ); /* new block size */
5025 break;
5027 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
5029 char *program = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
5030 BYTE *paramblk = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx);
5032 TRACE( "EXEC %s\n", program );
5034 RESET_CFLAG(context);
5035 if (DOSVM_IsWin16())
5037 HINSTANCE16 instance = WinExec16( program, SW_NORMAL );
5038 if (instance < 32)
5040 SET_CFLAG( context );
5041 SET_AX( context, instance );
5044 else
5046 if (!MZ_Exec( context, program, AL_reg(context), paramblk))
5047 bSetDOSExtendedError = TRUE;
5050 break;
5052 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
5053 TRACE( "EXIT with return code %d\n", AL_reg(context) );
5054 if (DOSVM_IsWin16())
5055 ExitThread( AL_reg(context) );
5056 else if(ISV86(context))
5057 MZ_Exit( context, FALSE, AL_reg(context) );
5058 else
5061 * Exit from DPMI.
5063 ULONG_PTR rv = AL_reg(context);
5064 RaiseException( EXCEPTION_VM86_INTx, 0, 1, &rv );
5066 break;
5068 case 0x4d: /* GET RETURN CODE */
5069 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
5070 SET_AX( context, DOSVM_retval );
5071 DOSVM_retval = 0;
5072 break;
5074 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
5075 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
5076 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
5077 if (!INT21_FindFirst(context)) break;
5078 /* fall through */
5080 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
5081 TRACE("FINDNEXT\n");
5082 if (!INT21_FindNext(context))
5084 SetLastError( ERROR_NO_MORE_FILES );
5085 SET_AX( context, ERROR_NO_MORE_FILES );
5086 SET_CFLAG(context);
5088 else SET_AX( context, 0 ); /* OK */
5089 break;
5091 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
5092 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
5093 DOSVM_psp = BX_reg(context);
5094 break;
5096 case 0x51: /* GET PSP ADDRESS */
5097 INT21_GetPSP( context );
5098 break;
5100 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
5102 SEGPTR ptr = DOSDEV_GetLOL( ISV86(context) || !DOSVM_IsWin16() );
5103 context->SegEs = SELECTOROF(ptr);
5104 SET_BX( context, OFFSETOF(ptr) );
5106 break;
5108 case 0x54: /* Get Verify Flag */
5109 TRACE("Get Verify Flag - Not Supported\n");
5110 SET_AL( context, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
5111 break;
5113 case 0x56: /* "RENAME" - RENAME FILE */
5114 if (!INT21_RenameFile( context ))
5115 bSetDOSExtendedError = TRUE;
5116 else
5117 RESET_CFLAG(context);
5118 break;
5120 case 0x57: /* FILE DATE AND TIME */
5121 if (!INT21_FileDateTime( context ))
5122 bSetDOSExtendedError = TRUE;
5123 else
5124 RESET_CFLAG(context);
5125 break;
5127 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
5128 switch (AL_reg(context))
5130 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
5131 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
5132 SET_AX( context, 0 ); /* low memory first fit */
5133 break;
5135 case 0x01: /* SET ALLOCATION STRATEGY */
5136 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
5137 BL_reg(context) );
5138 break;
5140 case 0x02: /* GET UMB LINK STATE */
5141 TRACE( "GET UMB LINK STATE\n" );
5142 SET_AL( context, 0 ); /* UMBs not part of DOS memory chain */
5143 break;
5145 case 0x03: /* SET UMB LINK STATE */
5146 TRACE( "SET UMB LINK STATE to %d - ignored\n",
5147 BX_reg(context) );
5148 break;
5150 default:
5151 INT_BARF( context, 0x21 );
5153 break;
5155 case 0x59: /* GET EXTENDED ERROR INFO */
5156 INT21_GetExtendedError( context );
5157 break;
5159 case 0x5a: /* CREATE TEMPORARY FILE */
5160 TRACE("CREATE TEMPORARY FILE\n");
5161 bSetDOSExtendedError = !INT21_CreateTempFile(context);
5162 break;
5164 case 0x5b: /* CREATE NEW FILE */
5165 if (!INT21_CreateFile( context, context->Edx, FALSE,
5166 OF_READWRITE | OF_SHARE_COMPAT, 0x10 ))
5167 bSetDOSExtendedError = TRUE;
5168 else
5169 RESET_CFLAG(context);
5170 break;
5172 case 0x5c: /* "FLOCK" - RECORD LOCKING */
5174 DWORD offset = MAKELONG(DX_reg(context), CX_reg(context));
5175 DWORD length = MAKELONG(DI_reg(context), SI_reg(context));
5176 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
5178 RESET_CFLAG(context);
5179 switch (AL_reg(context))
5181 case 0x00: /* LOCK */
5182 TRACE( "lock handle %d offset %d length %d\n",
5183 BX_reg(context), offset, length );
5184 if (!LockFile( handle, offset, 0, length, 0 ))
5185 bSetDOSExtendedError = TRUE;
5186 break;
5188 case 0x01: /* UNLOCK */
5189 TRACE( "unlock handle %d offset %d length %d\n",
5190 BX_reg(context), offset, length );
5191 if (!UnlockFile( handle, offset, 0, length, 0 ))
5192 bSetDOSExtendedError = TRUE;
5193 break;
5195 default:
5196 INT_BARF( context, 0x21 );
5199 break;
5201 case 0x5d: /* NETWORK 5D */
5202 FIXME( "Network function 5D not implemented.\n" );
5203 SetLastError( ER_NoNetwork );
5204 bSetDOSExtendedError = TRUE;
5205 break;
5207 case 0x5e: /* NETWORK 5E */
5208 bSetDOSExtendedError = INT21_NetworkFunc( context);
5209 break;
5211 case 0x5f: /* NETWORK 5F */
5212 /* FIXME: supporting this would need to 1:
5213 * - implement per drive current directory (as kernel32 doesn't)
5214 * - assign enabled/disabled flag on a per drive basis
5216 /* network software not installed */
5217 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
5218 SetLastError( ER_NoNetwork );
5219 bSetDOSExtendedError = TRUE;
5220 break;
5222 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
5224 WCHAR pathW[MAX_PATH], res[MAX_PATH];
5225 /* FIXME: likely to be broken */
5227 TRACE("TRUENAME %s\n",
5228 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
5229 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
5230 if (!GetFullPathNameW( pathW, 128, res, NULL ))
5231 bSetDOSExtendedError = TRUE;
5232 else
5234 SET_AX( context, 0 );
5235 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
5236 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
5237 128, NULL, NULL);
5240 break;
5242 case 0x61: /* UNUSED */
5243 SET_AL( context, 0 );
5244 break;
5246 case 0x62: /* GET PSP ADDRESS */
5247 INT21_GetPSP( context );
5248 break;
5250 case 0x63: /* MISC. LANGUAGE SUPPORT */
5251 switch (AL_reg(context)) {
5252 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
5253 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
5254 context->SegDs = INT21_GetHeapSelector( context );
5255 SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
5256 SET_AL( context, 0 ); /* success */
5257 break;
5259 break;
5261 case 0x64: /* OS/2 DOS BOX */
5262 INT_BARF( context, 0x21 );
5263 SET_CFLAG(context);
5264 break;
5266 case 0x65: /* EXTENDED COUNTRY INFORMATION */
5267 INT21_ExtendedCountryInformation( context );
5268 break;
5270 case 0x66: /* GLOBAL CODE PAGE TABLE */
5271 switch (AL_reg(context))
5273 case 0x01:
5274 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
5275 SET_BX( context, GetOEMCP() );
5276 SET_DX( context, GetOEMCP() );
5277 break;
5278 case 0x02:
5279 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
5280 BX_reg(context), DX_reg(context) );
5281 break;
5283 break;
5285 case 0x67: /* SET HANDLE COUNT */
5286 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
5287 if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
5288 bSetDOSExtendedError = TRUE;
5289 break;
5291 case 0x68: /* "FFLUSH" - COMMIT FILE */
5292 TRACE( "FFLUSH - handle %d\n", BX_reg(context) );
5293 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
5294 bSetDOSExtendedError = TRUE;
5295 break;
5297 case 0x69: /* DISK SERIAL NUMBER */
5298 switch (AL_reg(context))
5300 case 0x00:
5301 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
5302 INT21_DriveName(BL_reg(context)));
5303 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
5304 else SET_AX( context, 0 );
5305 break;
5307 case 0x01:
5308 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
5309 INT21_DriveName(BL_reg(context)));
5310 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
5311 else SET_AX( context, 1 );
5312 break;
5314 break;
5316 case 0x6a: /* COMMIT FILE */
5317 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context) );
5318 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
5319 bSetDOSExtendedError = TRUE;
5320 break;
5322 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
5323 SET_AL( context, 0 );
5324 break;
5326 case 0x6c: /* EXTENDED OPEN/CREATE */
5327 if (!INT21_CreateFile( context, context->Esi, TRUE,
5328 BX_reg(context), DL_reg(context) ))
5329 bSetDOSExtendedError = TRUE;
5330 break;
5332 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
5333 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
5334 SET_CFLAG( context );
5335 SET_AL( context, 0 );
5336 break;
5338 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
5339 INT21_LongFilename( context );
5340 break;
5342 case 0x73: /* MSDOS7 - FAT32 */
5343 RESET_CFLAG( context );
5344 if (!INT21_Fat32( context ))
5345 bSetDOSExtendedError = TRUE;
5346 break;
5348 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
5349 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
5350 break;
5352 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
5353 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
5354 break;
5356 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
5357 /* we don't implement a DOS32 extender */
5358 TRACE( "DOS32 EXTENDER API - ignored\n" );
5359 break;
5361 default:
5362 INT_BARF( context, 0x21 );
5363 break;
5365 } /* END OF SWITCH */
5367 /* Set general error condition. */
5368 if (bSetDOSExtendedError)
5370 SET_AX( context, GetLastError() );
5371 SET_CFLAG( context );
5374 /* Print error code if carry flag is set. */
5375 if (context->EFlags & 0x0001)
5376 TRACE("failed, error %d\n", GetLastError() );
5378 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
5379 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
5380 AX_reg(context), BX_reg(context),
5381 CX_reg(context), DX_reg(context),
5382 SI_reg(context), DI_reg(context),
5383 (WORD)context->SegDs, (WORD)context->SegEs,
5384 context->EFlags );