Added a few functions that are now implemented.
[wine/testsucceed.git] / dlls / winedos / int21.c
blob6df193ce80ead606572387b3885c0c51adfbe868
1 /*
2 * DOS interrupt 21h handler
4 * Copyright 1993, 1994 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1997 Andreas Mohr
7 * Copyright 1998 Uwe Bonnes
8 * Copyright 1998, 1999 Ove Kaaven
9 * Copyright 2003 Thomas Mertes
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "config.h"
27 #include "wine/port.h"
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <sys/stat.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winreg.h"
39 #include "winternl.h"
40 #include "wine/winbase16.h"
41 #include "dosexe.h"
42 #include "winerror.h"
43 #include "winuser.h"
44 #include "wine/unicode.h"
45 #include "wine/server.h"
46 #include "wine/debug.h"
47 #include "wine/exception.h"
50 * Note:
51 * - Most of the file related functions are wrong. NT's kernel32
52 * doesn't maintain a per drive current directory, while DOS does.
53 * We should in fact keep track in here of those per drive
54 * directories, and use this info while dealing with partial paths
55 * (drive defined, but only relative paths). This could even be
56 * created as an array of CDS (there should be an entry for that in
57 * the LOL)
61 * Forward declarations.
63 static BOOL INT21_RenameFile( CONTEXT86 *context );
65 WINE_DEFAULT_DEBUG_CHANNEL(int21);
68 #include "pshpack1.h"
71 * Extended Drive Parameter Block.
72 * This structure is compatible with standard DOS4+ DPB and
73 * extended DOS7 DPB.
75 typedef struct _INT21_DPB {
76 BYTE drive; /* 00 drive number (0=A, ...) */
77 BYTE unit; /* 01 unit number within device driver */
78 WORD sector_bytes; /* 02 bytes per sector */
79 BYTE cluster_sectors; /* 04 highest sector number within a cluster */
80 BYTE shift; /* 05 shift count to convert clusters into sectors */
81 WORD num_reserved; /* 06 reserved sectors at beginning of drive */
82 BYTE num_FAT; /* 08 number of FATs */
83 WORD num_root_entries; /* 09 number of root directory entries */
84 WORD first_data_sector; /* 0b number of first sector containing user data */
85 WORD num_clusters1; /* 0d highest cluster number (number of data clusters + 1) */
86 WORD sectors_per_FAT; /* 0f number of sectors per FAT */
87 WORD first_dir_sector; /* 11 sector number of first directory sector */
88 SEGPTR driver_header; /* 13 address of device driver header */
89 BYTE media_ID; /* 17 media ID byte */
90 BYTE access_flag; /* 18 0x00 if disk accessed, 0xff if not */
91 SEGPTR next; /* 19 pointer to next DPB */
92 WORD search_cluster1; /* 1d cluster at which to start search for free space */
93 WORD free_clusters_lo; /* 1f number of free clusters on drive or 0xffff if unknown */
94 WORD free_clusters_hi; /* 21 hiword of clusters_free */
95 WORD mirroring_flags; /* 23 active FAT/mirroring flags */
96 WORD info_sector; /* 25 sector number of file system info sector or 0xffff for none */
97 WORD spare_boot_sector; /* 27 sector number of backup boot sector or 0xffff for none */
98 DWORD first_cluster_sector; /* 29 sector number of the first cluster */
99 DWORD num_clusters2; /* 2d maximum cluster number */
100 DWORD fat_clusters; /* 31 number of clusters occupied by FAT */
101 DWORD root_cluster; /* 35 cluster number of start of root directory */
102 DWORD search_cluster2; /* 39 cluster at which to start searching for free space */
103 } INT21_DPB;
107 * Structure for DOS data that can be accessed directly from applications.
108 * Real and protected mode pointers will be returned to this structure so
109 * the structure must be correctly packed.
111 typedef struct _INT21_HEAP {
112 WORD uppercase_size; /* Size of the following table in bytes */
113 BYTE uppercase_table[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
115 WORD lowercase_size; /* Size of the following table in bytes */
116 BYTE lowercase_table[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
118 WORD collating_size; /* Size of the following table in bytes */
119 BYTE collating_table[256]; /* Values used to sort characters from 0x00 to 0xff. */
121 WORD filename_size; /* Size of the following filename data in bytes */
122 BYTE filename_reserved1; /* 0x01 for MS-DOS 3.30-6.00 */
123 BYTE filename_lowest; /* Lowest permissible character value for filename */
124 BYTE filename_highest; /* Highest permissible character value for filename */
125 BYTE filename_reserved2; /* 0x00 for MS-DOS 3.30-6.00 */
126 BYTE filename_exclude_first; /* First illegal character in permissible range */
127 BYTE filename_exclude_last; /* Last illegal character in permissible range */
128 BYTE filename_reserved3; /* 0x02 for MS-DOS 3.30-6.00 */
129 BYTE filename_illegal_size; /* Number of terminators in the following table */
130 BYTE filename_illegal_table[16]; /* Characters which terminate a filename */
132 WORD dbcs_size; /* Number of valid ranges in the following table */
133 BYTE dbcs_table[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
135 BYTE misc_indos; /* Interrupt 21 nesting flag */
136 WORD misc_segment; /* Real mode segment for INT21_HEAP */
137 WORD misc_selector; /* Protected mode selector for INT21_HEAP */
138 INT21_DPB misc_dpb_list[MAX_DOS_DRIVES]; /* Drive parameter blocks for all drives */
140 } INT21_HEAP;
143 struct FCB {
144 BYTE drive_number;
145 CHAR file_name[8];
146 CHAR file_extension[3];
147 WORD current_block_number;
148 WORD logical_record_size;
149 DWORD file_size;
150 WORD date_of_last_write;
151 WORD time_of_last_write;
152 BYTE file_number;
153 BYTE attributes;
154 WORD starting_cluster;
155 WORD sequence_number;
156 BYTE file_attributes;
157 BYTE unused;
158 BYTE record_within_current_block;
159 BYTE random_access_record_number[4];
163 struct XFCB {
164 BYTE xfcb_signature;
165 BYTE reserved[5];
166 BYTE xfcb_file_attribute;
167 BYTE fcb[37];
170 /* DTA layout for FindFirst/FindNext */
171 typedef struct
173 BYTE drive; /* 00 drive letter */
174 char mask[11]; /* 01 search template */
175 BYTE search_attr; /* 0c search attributes */
176 WORD count; /* 0d entry count within directory */
177 WORD cluster; /* 0f cluster of parent directory */
178 WCHAR *fullPath; /* 11 full path (was: reserved) */
179 BYTE fileattr; /* 15 file attributes */
180 WORD filetime; /* 16 file time */
181 WORD filedate; /* 18 file date */
182 DWORD filesize; /* 1a file size */
183 char filename[13]; /* 1e file name + extension */
184 } FINDFILE_DTA;
186 /* FCB layout for FindFirstFCB/FindNextFCB */
187 typedef struct
189 BYTE drive; /* 00 drive letter */
190 char filename[11]; /* 01 filename 8+3 format */
191 int count; /* 0c entry count (was: reserved) */
192 WCHAR *fullPath; /* 10 full path (was: reserved) */
193 } FINDFILE_FCB;
195 /* DOS directory entry for FindFirstFCB/FindNextFCB */
196 typedef struct
198 char filename[11]; /* 00 filename 8+3 format */
199 BYTE fileattr; /* 0b file attributes */
200 BYTE reserved[10]; /* 0c reserved */
201 WORD filetime; /* 16 file time */
202 WORD filedate; /* 18 file date */
203 WORD cluster; /* 1a file first cluster */
204 DWORD filesize; /* 1c file size */
205 } DOS_DIRENTRY_LAYOUT;
207 #include "poppack.h"
209 /* dos file attributes */
210 #define FA_NORMAL 0x00 /* Normal file, no attributes */
211 #define FA_RDONLY 0x01 /* Read only attribute */
212 #define FA_HIDDEN 0x02 /* Hidden file */
213 #define FA_SYSTEM 0x04 /* System file */
214 #define FA_LABEL 0x08 /* Volume label */
215 #define FA_DIRECTORY 0x10 /* Directory */
216 #define FA_ARCHIVE 0x20 /* Archive */
217 #define FA_UNUSED 0x40 /* Unused */
219 /* Error codes */
220 #define ER_NoNetwork 0x49
222 /* Error classes */
223 #define EC_OutOfResource 0x01
224 #define EC_Temporary 0x02
225 #define EC_AccessDenied 0x03
226 #define EC_InternalError 0x04
227 #define EC_HardwareFailure 0x05
228 #define EC_SystemFailure 0x06
229 #define EC_ProgramError 0x07
230 #define EC_NotFound 0x08
231 #define EC_MediaError 0x0b
232 #define EC_Exists 0x0c
233 #define EC_Unknown 0x0d
235 /* Suggested actions */
236 #define SA_Retry 0x01
237 #define SA_DelayedRetry 0x02
238 #define SA_Abort 0x04
239 #define SA_Ignore 0x06
240 #define SA_Ask4Retry 0x07
242 /* Error locus */
243 #define EL_Unknown 0x01
244 #define EL_Disk 0x02
245 #define EL_Network 0x03
246 #define EL_Serial 0x04
247 #define EL_Memory 0x05
249 /* BIOS Keyboard Scancodes */
250 #define KEY_LEFT 0x4B
251 #define KEY_RIGHT 0x4D
252 #define KEY_UP 0x48
253 #define KEY_DOWN 0x50
254 #define KEY_IC 0x52 /* insert char */
255 #define KEY_DC 0x53 /* delete char */
256 #define KEY_BACKSPACE 0x0E
257 #define KEY_HOME 0x47
258 #define KEY_END 0x4F
259 #define KEY_NPAGE 0x49
260 #define KEY_PPAGE 0x51
263 struct magic_device
265 WCHAR name[10];
266 HANDLE handle;
267 dev_t dev;
268 ino_t ino;
269 void (*ioctl_handler)(CONTEXT86 *);
272 static void INT21_IoctlScsiMgrHandler( CONTEXT86 * );
273 static void INT21_IoctlEMSHandler( CONTEXT86 * );
274 static void INT21_IoctlHPScanHandler( CONTEXT86 * );
276 static struct magic_device magic_devices[] =
278 { {'s','c','s','i','m','g','r','$',0}, NULL, 0, 0, INT21_IoctlScsiMgrHandler },
279 { {'e','m','m','x','x','x','x','0',0}, NULL, 0, 0, INT21_IoctlEMSHandler },
280 { {'h','p','s','c','a','n',0}, NULL, 0, 0, INT21_IoctlHPScanHandler },
283 #define NB_MAGIC_DEVICES (sizeof(magic_devices)/sizeof(magic_devices[0]))
286 /* Many calls translate a drive argument like this:
287 drive number (00h = default, 01h = A:, etc)
289 /******************************************************************
290 * INT21_DriveName
292 * Many calls translate a drive argument like this:
293 * drive number (00h = default, 01h = A:, etc)
295 static const char *INT21_DriveName(int drive)
297 if (drive > 0)
299 if (drive <= 26) return wine_dbg_sprintf("%c:", 'A' + drive - 1);
300 else return wine_dbg_sprintf( "<Bad drive: %d>", drive);
302 return "default";
305 /***********************************************************************
306 * INT21_GetCurrentDrive
308 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
309 * MAX_DOS_DRIVES on error.
311 static BYTE INT21_GetCurrentDrive(void)
313 WCHAR current_directory[MAX_PATH];
315 if (!GetCurrentDirectoryW( MAX_PATH, current_directory ) ||
316 current_directory[1] != ':')
318 TRACE( "Failed to get current drive.\n" );
319 return MAX_DOS_DRIVES;
322 return toupperW( current_directory[0] ) - 'A';
326 /***********************************************************************
327 * INT21_MapDrive
329 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
330 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
332 static BYTE INT21_MapDrive( BYTE drive )
334 if (drive)
336 WCHAR drivespec[3] = {'A', ':', 0};
337 UINT drivetype;
339 drivespec[0] += drive - 1;
340 drivetype = GetDriveTypeW( drivespec );
342 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
343 return MAX_DOS_DRIVES;
345 return drive - 1;
348 return INT21_GetCurrentDrive();
352 /***********************************************************************
353 * INT21_SetCurrentDrive
355 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
357 static void INT21_SetCurrentDrive( BYTE drive )
359 WCHAR drivespec[3] = {'A', ':', 0};
361 drivespec[0] += drive;
363 if (!SetCurrentDirectoryW( drivespec ))
364 TRACE( "Failed to set current drive.\n" );
368 /***********************************************************************
369 * INT21_ReadChar
371 * Reads a character from the standard input.
372 * Extended keycodes will be returned as two separate characters.
374 static BOOL INT21_ReadChar( BYTE *input, CONTEXT86 *waitctx )
376 static BYTE pending_scan = 0;
378 if (pending_scan)
380 if (input)
381 *input = pending_scan;
382 if (waitctx)
383 pending_scan = 0;
384 return TRUE;
386 else
388 BYTE ascii;
389 BYTE scan;
390 if (!DOSVM_Int16ReadChar( &ascii, &scan, waitctx ))
391 return FALSE;
393 if (input)
394 *input = ascii;
395 if (waitctx && !ascii)
396 pending_scan = scan;
397 return TRUE;
402 /***********************************************************************
403 * INT21_GetSystemCountryCode
405 * Return DOS country code for default system locale.
407 static WORD INT21_GetSystemCountryCode( void )
410 * FIXME: Determine country code. We should probably use
411 * DOSCONF structure for that.
413 return GetSystemDefaultLangID();
417 /***********************************************************************
418 * INT21_FillCountryInformation
420 * Fill 34-byte buffer with country information data using
421 * default system locale.
423 static void INT21_FillCountryInformation( BYTE *buffer )
425 /* 00 - WORD: date format
426 * 00 = mm/dd/yy
427 * 01 = dd/mm/yy
428 * 02 = yy/mm/dd
430 *(WORD*)(buffer + 0) = 0; /* FIXME: Get from locale */
432 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
433 buffer[2] = '$'; /* FIXME: Get from locale */
434 buffer[3] = 0;
436 /* 07 - BYTE[2]: ASCIIZ thousands separator */
437 buffer[7] = 0; /* FIXME: Get from locale */
438 buffer[8] = 0;
440 /* 09 - BYTE[2]: ASCIIZ decimal separator */
441 buffer[9] = '.'; /* FIXME: Get from locale */
442 buffer[10] = 0;
444 /* 11 - BYTE[2]: ASCIIZ date separator */
445 buffer[11] = '/'; /* FIXME: Get from locale */
446 buffer[12] = 0;
448 /* 13 - BYTE[2]: ASCIIZ time separator */
449 buffer[13] = ':'; /* FIXME: Get from locale */
450 buffer[14] = 0;
452 /* 15 - BYTE: Currency format
453 * bit 2 = set if currency symbol replaces decimal point
454 * bit 1 = number of spaces between value and currency symbol
455 * bit 0 = 0 if currency symbol precedes value
456 * 1 if currency symbol follows value
458 buffer[15] = 0; /* FIXME: Get from locale */
460 /* 16 - BYTE: Number of digits after decimal in currency */
461 buffer[16] = 0; /* FIXME: Get from locale */
463 /* 17 - BYTE: Time format
464 * bit 0 = 0 if 12-hour clock
465 * 1 if 24-hour clock
467 buffer[17] = 1; /* FIXME: Get from locale */
469 /* 18 - DWORD: Address of case map routine */
470 *(DWORD*)(buffer + 18) = 0; /* FIXME: ptr to case map routine */
472 /* 22 - BYTE[2]: ASCIIZ data-list separator */
473 buffer[22] = ','; /* FIXME: Get from locale */
474 buffer[23] = 0;
476 /* 24 - BYTE[10]: Reserved */
477 memset( buffer + 24, 0, 10 );
481 /***********************************************************************
482 * INT21_FillHeap
484 * Initialize DOS heap.
486 * Filename Terminator Table of w2k DE NTVDM:
487 * 16 00 01 00 FF 00 00 20-02 0E 2E 22 2F 5C 5B 5D ....... ..."/\[]
488 * 3A 7C 3C 3E 2B 3D 3B 2C-00 :|<>+=;,
490 static void INT21_FillHeap( INT21_HEAP *heap )
492 static const char terminators[] = ".\"/\\[]:|<>+=;,";
493 int i;
496 * Uppercase table.
498 heap->uppercase_size = 128;
499 for (i = 0; i < 128; i++)
500 heap->uppercase_table[i] = toupper( 128 + i );
503 * Lowercase table.
505 heap->lowercase_size = 256;
506 for (i = 0; i < 256; i++)
507 heap->lowercase_table[i] = tolower( i );
510 * Collating table.
512 heap->collating_size = 256;
513 for (i = 0; i < 256; i++)
514 heap->collating_table[i] = i;
517 * Filename table.
519 heap->filename_size = 8 + strlen(terminators);
520 heap->filename_illegal_size = strlen(terminators);
521 strcpy( heap->filename_illegal_table, terminators );
523 heap->filename_reserved1 = 0x01;
524 heap->filename_lowest = 0x00;
525 heap->filename_highest = 0xff;
526 heap->filename_reserved2 = 0x00;
527 heap->filename_exclude_first = 0x00;
528 heap->filename_exclude_last = 0x20;
529 heap->filename_reserved3 = 0x02;
532 * DBCS lead byte table. This table is empty.
534 heap->dbcs_size = 0;
535 memset( heap->dbcs_table, 0, sizeof(heap->dbcs_table) );
538 * Initialize InDos flag.
540 heap->misc_indos = 0;
543 * FIXME: Should drive parameter blocks (DPB) be
544 * initialized here and linked to DOS LOL?
549 /***********************************************************************
550 * INT21_GetHeapPointer
552 * Get pointer for DOS heap (INT21_HEAP).
553 * Creates and initializes heap on first call.
555 static INT21_HEAP *INT21_GetHeapPointer( void )
557 static INT21_HEAP *heap_pointer = NULL;
559 if (!heap_pointer)
561 WORD heap_segment;
562 WORD heap_selector;
564 heap_pointer = DOSVM_AllocDataUMB( sizeof(INT21_HEAP),
565 &heap_segment,
566 &heap_selector );
568 heap_pointer->misc_segment = heap_segment;
569 heap_pointer->misc_selector = heap_selector;
570 INT21_FillHeap( heap_pointer );
573 return heap_pointer;
577 /***********************************************************************
578 * INT21_GetHeapSelector
580 * Get segment/selector for DOS heap (INT21_HEAP).
581 * Creates and initializes heap on first call.
583 static WORD INT21_GetHeapSelector( CONTEXT86 *context )
585 INT21_HEAP *heap = INT21_GetHeapPointer();
587 if (!ISV86(context) && DOSVM_IsWin16())
588 return heap->misc_selector;
589 else
590 return heap->misc_segment;
594 /***********************************************************************
595 * INT21_FillDrivePB
597 * Fill DOS heap drive parameter block for the specified drive.
598 * Return TRUE if drive was valid and there were
599 * no errors while reading drive information.
601 static BOOL INT21_FillDrivePB( BYTE drive )
603 WCHAR drivespec[3] = {'A', ':', 0};
604 INT21_HEAP *heap = INT21_GetHeapPointer();
605 INT21_DPB *dpb;
606 UINT drivetype;
607 DWORD cluster_sectors;
608 DWORD sector_bytes;
609 DWORD free_clusters;
610 DWORD total_clusters;
612 if (drive >= MAX_DOS_DRIVES)
613 return FALSE;
615 dpb = &heap->misc_dpb_list[drive];
616 drivespec[0] += drive;
617 drivetype = GetDriveTypeW( drivespec );
620 * FIXME: Does this check work correctly with floppy/cdrom drives?
622 if (drivetype == DRIVE_NO_ROOT_DIR || drivetype == DRIVE_UNKNOWN)
623 return FALSE;
626 * FIXME: Does this check work correctly with floppy/cdrom drives?
628 if (!GetDiskFreeSpaceW( drivespec, &cluster_sectors, &sector_bytes,
629 &free_clusters, &total_clusters ))
630 return FALSE;
633 * FIXME: Most of the values listed below are incorrect.
634 * All values should be validated.
637 dpb->drive = drive;
638 dpb->unit = 0;
639 dpb->sector_bytes = sector_bytes;
640 dpb->cluster_sectors = cluster_sectors - 1;
642 dpb->shift = 0;
643 while (cluster_sectors > 1)
645 cluster_sectors /= 2;
646 dpb->shift++;
649 dpb->num_reserved = 0;
650 dpb->num_FAT = 1;
651 dpb->num_root_entries = 2;
652 dpb->first_data_sector = 2;
653 dpb->num_clusters1 = total_clusters;
654 dpb->sectors_per_FAT = 1;
655 dpb->first_dir_sector = 1;
656 dpb->driver_header = 0;
657 dpb->media_ID = (drivetype == DRIVE_FIXED) ? 0xF8 : 0xF0;
658 dpb->access_flag = 0;
659 dpb->next = 0;
660 dpb->search_cluster1 = 0;
661 dpb->free_clusters_lo = LOWORD(free_clusters);
662 dpb->free_clusters_hi = HIWORD(free_clusters);
663 dpb->mirroring_flags = 0;
664 dpb->info_sector = 0xffff;
665 dpb->spare_boot_sector = 0xffff;
666 dpb->first_cluster_sector = 0;
667 dpb->num_clusters2 = total_clusters;
668 dpb->fat_clusters = 32;
669 dpb->root_cluster = 0;
670 dpb->search_cluster2 = 0;
672 return TRUE;
676 /***********************************************************************
677 * INT21_GetCurrentDirectory
679 * Handler for:
680 * - function 0x47
681 * - subfunction 0x47 of function 0x71
683 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context, BOOL islong )
685 char *buffer = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi);
686 BYTE drive = INT21_MapDrive( DL_reg(context) );
687 WCHAR pathW[MAX_PATH];
688 char pathA[MAX_PATH];
689 WCHAR *ptr = pathW;
691 TRACE( "drive %d\n", DL_reg(context) );
693 if (drive == MAX_DOS_DRIVES)
695 SetLastError(ERROR_INVALID_DRIVE);
696 return FALSE;
700 * Grab current directory.
703 if (!GetCurrentDirectoryW( MAX_PATH, pathW )) return FALSE;
705 if (toupperW(pathW[0]) - 'A' != drive || pathW[1] != ':')
707 /* cwd is not on the requested drive, get the environment string instead */
709 WCHAR env_var[4];
711 env_var[0] = '=';
712 env_var[1] = 'A' + drive;
713 env_var[2] = ':';
714 env_var[3] = 0;
715 if (!GetEnvironmentVariableW( env_var, pathW, MAX_PATH ))
717 /* return empty path */
718 buffer[0] = 0;
719 return TRUE;
724 * Convert into short format.
727 if (!islong)
729 DWORD result = GetShortPathNameW( pathW, pathW, MAX_PATH );
730 if (!result)
731 return FALSE;
732 if (result > MAX_PATH)
734 WARN( "Short path too long!\n" );
735 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
736 return FALSE;
741 * The returned pathname does not include
742 * the drive letter, colon or leading backslash.
745 if (ptr[0] == '\\')
748 * FIXME: We should probably just strip host part from name...
750 FIXME( "UNC names are not supported.\n" );
751 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
752 return FALSE;
754 else if (!ptr[0] || ptr[1] != ':' || ptr[2] != '\\')
756 WARN( "Path is neither UNC nor DOS path: %s\n",
757 wine_dbgstr_w(ptr) );
758 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
759 return FALSE;
761 else
763 /* Remove drive letter, colon and leading backslash. */
764 ptr += 3;
768 * Convert into OEM string.
771 if (!WideCharToMultiByte(CP_OEMCP, 0, ptr, -1, pathA,
772 MAX_PATH, NULL, NULL))
774 WARN( "Cannot convert path!\n" );
775 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
776 return FALSE;
780 * Success.
783 if (!islong)
785 /* Undocumented success code. */
786 SET_AX( context, 0x0100 );
788 /* Truncate buffer to 64 bytes. */
789 pathA[63] = 0;
792 TRACE( "%c:=%s\n", 'A' + drive, pathA );
794 strcpy( buffer, pathA );
795 return TRUE;
799 /***********************************************************************
800 * INT21_SetCurrentDirectory
802 * Handler for:
803 * - function 0x3b
804 * - subfunction 0x3b of function 0x71
806 static BOOL INT21_SetCurrentDirectory( CONTEXT86 *context )
808 WCHAR dirW[MAX_PATH];
809 WCHAR env_var[4];
810 DWORD attr;
811 char *dirA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
812 BYTE drive = INT21_GetCurrentDrive();
813 BOOL result;
815 TRACE( "SET CURRENT DIRECTORY %s\n", dirA );
817 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
818 if (!GetFullPathNameW( dirW, MAX_PATH, dirW, NULL )) return FALSE;
820 attr = GetFileAttributesW( dirW );
821 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
823 SetLastError( ERROR_PATH_NOT_FOUND );
824 return FALSE;
827 env_var[0] = '=';
828 env_var[1] = dirW[0];
829 env_var[2] = ':';
830 env_var[3] = 0;
831 result = SetEnvironmentVariableW( env_var, dirW );
833 /* only set current directory if on the current drive */
834 if (result && (toupperW(dirW[0]) - 'A' == drive)) result = SetCurrentDirectoryW( dirW );
836 return result;
839 /***********************************************************************
840 * INT21_CreateMagicDeviceHandle
842 * Create a dummy file handle for a "magic" device.
844 static HANDLE INT21_CreateMagicDeviceHandle( LPCWSTR name )
846 const char *dir = wine_get_server_dir();
847 int len;
848 HANDLE ret;
849 NTSTATUS status;
850 OBJECT_ATTRIBUTES attr;
851 UNICODE_STRING nameW;
852 IO_STATUS_BLOCK io;
854 len = MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, NULL, 0 );
855 nameW.Length = (len + 1 + strlenW( name )) * sizeof(WCHAR);
856 nameW.MaximumLength = nameW.Length + sizeof(WCHAR);
857 if (!(nameW.Buffer = HeapAlloc( GetProcessHeap(), 0, nameW.Length )))
859 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
860 return 0;
862 MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, nameW.Buffer, len );
863 nameW.Buffer[len-1] = '/';
864 strcpyW( nameW.Buffer + len, name );
866 attr.Length = sizeof(attr);
867 attr.RootDirectory = 0;
868 attr.Attributes = 0;
869 attr.ObjectName = &nameW;
870 attr.SecurityDescriptor = NULL;
871 attr.SecurityQualityOfService = NULL;
873 status = NtCreateFile( &ret, GENERIC_READ|GENERIC_WRITE, &attr, &io, NULL, 0,
874 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF,
875 FILE_SYNCHRONOUS_IO_ALERT, NULL, 0 );
876 if (status)
878 ret = 0;
879 SetLastError( RtlNtStatusToDosError(status) );
881 RtlFreeUnicodeString( &nameW );
882 return ret;
886 /***********************************************************************
887 * INT21_OpenMagicDevice
889 * Open a file handle for "magic" devices like EMMXXXX0.
891 static HANDLE INT21_OpenMagicDevice( LPCWSTR name, DWORD access )
893 unsigned int i;
894 const WCHAR *p;
895 HANDLE handle;
897 if (name[0] && (name[1] == ':')) name += 2;
898 if ((p = strrchrW( name, '/' ))) name = p + 1;
899 if ((p = strrchrW( name, '\\' ))) name = p + 1;
901 for (i = 0; i < NB_MAGIC_DEVICES; i++)
903 int len = strlenW( magic_devices[i].name );
904 if (!strncmpiW( magic_devices[i].name, name, len ) &&
905 (!name[len] || name[len] == '.' || name[len] == ':')) break;
907 if (i == NB_MAGIC_DEVICES) return 0;
909 if (!magic_devices[i].handle) /* need to open it */
911 int fd;
912 struct stat st;
914 if (!(handle = INT21_CreateMagicDeviceHandle( magic_devices[i].name ))) return 0;
915 wine_server_handle_to_fd( handle, 0, &fd, NULL );
916 fstat( fd, &st );
917 wine_server_release_fd( handle, fd );
918 magic_devices[i].dev = st.st_dev;
919 magic_devices[i].ino = st.st_ino;
920 magic_devices[i].handle = handle;
922 if (!DuplicateHandle( GetCurrentProcess(), magic_devices[i].handle,
923 GetCurrentProcess(), &handle, access, FALSE, 0 )) handle = 0;
924 return handle;
928 /***********************************************************************
929 * INT21_CreateFile
931 * Handler for:
932 * - function 0x3c
933 * - function 0x3d
934 * - function 0x5b
935 * - function 0x6c
936 * - subfunction 0x6c of function 0x71
938 static BOOL INT21_CreateFile( CONTEXT86 *context,
939 DWORD pathSegOff,
940 BOOL returnStatus,
941 WORD dosAccessShare,
942 BYTE dosAction )
944 WORD dosStatus;
945 char *pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, pathSegOff);
946 WCHAR pathW[MAX_PATH];
947 DWORD winAccess;
948 DWORD winAttributes;
949 HANDLE winHandle;
950 DWORD winMode;
951 DWORD winSharing;
953 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
954 "create flags=%04x, file=%s.\n",
955 AH_reg(context), dosAction, dosAccessShare, CX_reg(context), pathA );
958 * Application tried to create/open a file whose name
959 * ends with a backslash. This is not allowed.
961 * FIXME: This needs to be validated, especially the return value.
963 if (pathA[strlen(pathA) - 1] == '/')
965 SetLastError( ERROR_FILE_NOT_FOUND );
966 return FALSE;
970 * Convert DOS action flags into Win32 creation disposition parameter.
972 switch(dosAction)
974 case 0x01:
975 winMode = OPEN_EXISTING;
976 break;
977 case 0x02:
978 winMode = TRUNCATE_EXISTING;
979 break;
980 case 0x10:
981 winMode = CREATE_NEW;
982 break;
983 case 0x11:
984 winMode = OPEN_ALWAYS;
985 break;
986 case 0x12:
987 winMode = CREATE_ALWAYS;
988 break;
989 default:
990 SetLastError( ERROR_INVALID_PARAMETER );
991 return FALSE;
995 * Convert DOS access/share flags into Win32 desired access parameter.
997 switch(dosAccessShare & 0x07)
999 case OF_READ:
1000 winAccess = GENERIC_READ;
1001 break;
1002 case OF_WRITE:
1003 winAccess = GENERIC_WRITE;
1004 break;
1005 case OF_READWRITE:
1006 winAccess = GENERIC_READ | GENERIC_WRITE;
1007 break;
1008 case 0x04:
1010 * Read-only, do not modify file's last-access time (DOS7).
1012 * FIXME: How to prevent modification of last-access time?
1014 winAccess = GENERIC_READ;
1015 break;
1016 default:
1017 winAccess = 0;
1021 * Convert DOS access/share flags into Win32 share mode parameter.
1023 switch(dosAccessShare & 0x70)
1025 case OF_SHARE_EXCLUSIVE:
1026 winSharing = 0;
1027 break;
1028 case OF_SHARE_DENY_WRITE:
1029 winSharing = FILE_SHARE_READ;
1030 break;
1031 case OF_SHARE_DENY_READ:
1032 winSharing = FILE_SHARE_WRITE;
1033 break;
1034 case OF_SHARE_DENY_NONE:
1035 case OF_SHARE_COMPAT:
1036 default:
1037 winSharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1041 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
1042 * What to do with this bit?
1043 * FIXME: Bits in the high byte of dosAccessShare are not supported.
1044 * See both function 0x6c and subfunction 0x6c of function 0x71 for
1045 * definition of these bits.
1049 * Convert DOS create attributes into Win32 flags and attributes parameter.
1051 if (winMode == OPEN_EXISTING || winMode == TRUNCATE_EXISTING)
1053 winAttributes = 0;
1055 else
1057 WORD dosAttributes = CX_reg(context);
1059 if (dosAttributes & FA_LABEL)
1062 * Application tried to create volume label entry.
1063 * This is difficult to support so we do not allow it.
1065 * FIXME: If volume does not already have a label,
1066 * this function is supposed to succeed.
1068 SetLastError( ERROR_ACCESS_DENIED );
1069 return TRUE;
1072 winAttributes = dosAttributes &
1073 (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
1074 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE);
1078 * Open the file.
1080 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
1082 if ((winHandle = INT21_OpenMagicDevice( pathW, winAccess )))
1084 dosStatus = 1;
1086 else
1088 winHandle = CreateFileW( pathW, winAccess, winSharing, NULL,
1089 winMode, winAttributes, 0 );
1091 if (winHandle == INVALID_HANDLE_VALUE)
1092 return FALSE;
1095 * Determine DOS file status.
1097 * 1 = file opened
1098 * 2 = file created
1099 * 3 = file replaced
1101 switch(winMode)
1103 case OPEN_EXISTING:
1104 dosStatus = 1;
1105 break;
1106 case TRUNCATE_EXISTING:
1107 dosStatus = 3;
1108 break;
1109 case CREATE_NEW:
1110 dosStatus = 2;
1111 break;
1112 case OPEN_ALWAYS:
1113 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 1 : 2;
1114 break;
1115 case CREATE_ALWAYS:
1116 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 3 : 2;
1117 break;
1118 default:
1119 dosStatus = 0;
1124 * Return DOS file handle and DOS status.
1126 SET_AX( context, Win32HandleToDosFileHandle(winHandle) );
1128 if (returnStatus)
1129 SET_CX( context, dosStatus );
1131 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
1132 AX_reg(context), dosStatus );
1134 return TRUE;
1138 /***********************************************************************
1139 * INT21_BufferedInput
1141 * Handler for function 0x0a and reading from console using
1142 * function 0x3f.
1144 * Reads a string of characters from standard input until
1145 * enter key is pressed. Returns either number of characters
1146 * read from console including terminating CR or
1147 * zero if capacity was zero.
1149 static WORD INT21_BufferedInput( CONTEXT86 *context, BYTE *ptr, WORD capacity )
1151 BYTE length = 0;
1154 * Return immediately if capacity is zero.
1156 if (capacity == 0)
1157 return 0;
1159 while(TRUE)
1161 BYTE ascii;
1162 BYTE scan;
1164 DOSVM_Int16ReadChar( &ascii, &scan, context );
1166 if (ascii == '\r' || ascii == '\n')
1168 ptr[length] = '\r';
1169 return length + 1;
1173 * DOS handles only backspace and KEY_LEFT
1174 * perhaps we should do more
1176 if (ascii == '\b' || scan == KEY_LEFT)
1178 if (length==0) continue;
1179 DOSVM_PutChar( '\b' );
1180 length--;
1181 continue;
1185 * If the buffer becomes filled to within one byte of
1186 * capacity, DOS rejects all further characters up to,
1187 * but not including, the terminating carriage return.
1189 if (ascii != 0 && length < capacity-1)
1191 DOSVM_PutChar( ascii );
1192 ptr[length] = ascii;
1193 length++;
1199 /***********************************************************************
1200 * INT21_GetCurrentDTA
1202 static BYTE *INT21_GetCurrentDTA( CONTEXT86 *context )
1204 TDB *pTask = GlobalLock16(GetCurrentTask());
1206 /* FIXME: This assumes DTA was set correctly! */
1207 return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
1208 (DWORD)OFFSETOF(pTask->dta) );
1212 /***********************************************************************
1213 * INT21_OpenFileUsingFCB
1215 * Handler for function 0x0f.
1217 * PARAMS
1218 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
1220 * RETURNS (in AL)
1221 * 0x00: successful
1222 * 0xff: failed
1224 * NOTES
1225 * Opens a FCB file for read/write in compatibility mode. Upon calling
1226 * the FCB must have the drive_number, file_name, and file_extension
1227 * fields filled and all other bytes cleared.
1229 static void INT21_OpenFileUsingFCB( CONTEXT86 *context )
1231 struct FCB *fcb;
1232 struct XFCB *xfcb;
1233 char file_path[16];
1234 char *pos;
1235 HANDLE handle;
1236 HFILE16 hfile16;
1237 BY_HANDLE_FILE_INFORMATION info;
1238 BYTE AL_result;
1240 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1241 if (fcb->drive_number == 0xff) {
1242 xfcb = (struct XFCB *) fcb;
1243 fcb = (struct FCB *) xfcb->fcb;
1244 } /* if */
1246 AL_result = 0;
1247 file_path[0] = 'A' + INT21_MapDrive( fcb->drive_number );
1249 if (AL_result == 0) {
1250 file_path[1] = ':';
1251 pos = &file_path[2];
1252 memcpy(pos, fcb->file_name, 8);
1253 pos[8] = ' ';
1254 pos[9] = '\0';
1255 pos = strchr(pos, ' ');
1256 *pos = '.';
1257 pos++;
1258 memcpy(pos, fcb->file_extension, 3);
1259 pos[3] = ' ';
1260 pos[4] = '\0';
1261 pos = strchr(pos, ' ');
1262 *pos = '\0';
1264 handle = (HANDLE) _lopen(file_path, OF_READWRITE);
1265 if (handle == INVALID_HANDLE_VALUE) {
1266 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path);
1267 AL_result = 0xff; /* failed */
1268 } else {
1269 hfile16 = Win32HandleToDosFileHandle(handle);
1270 if (hfile16 == HFILE_ERROR16) {
1271 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle);
1272 CloseHandle(handle);
1273 AL_result = 0xff; /* failed */
1274 } else if (hfile16 > 255) {
1275 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16, file_path);
1276 _lclose16(hfile16);
1277 AL_result = 0xff; /* failed */
1278 } else {
1279 if (!GetFileInformationByHandle(handle, &info)) {
1280 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1281 hfile16, handle, file_path);
1282 _lclose16(hfile16);
1283 AL_result = 0xff; /* failed */
1284 } else {
1285 fcb->drive_number = file_path[0] - 'A' + 1;
1286 fcb->current_block_number = 0;
1287 fcb->logical_record_size = 128;
1288 fcb->file_size = info.nFileSizeLow;
1289 FileTimeToDosDateTime(&info.ftLastWriteTime,
1290 &fcb->date_of_last_write, &fcb->time_of_last_write);
1291 fcb->file_number = hfile16;
1292 fcb->attributes = 0xc2;
1293 fcb->starting_cluster = 0; /* don't know correct init value */
1294 fcb->sequence_number = 0; /* don't know correct init value */
1295 fcb->file_attributes = info.dwFileAttributes;
1296 /* The following fields are not initialized */
1297 /* by the native function: */
1298 /* unused */
1299 /* record_within_current_block */
1300 /* random_access_record_number */
1302 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1303 file_path, hfile16, handle);
1304 AL_result = 0x00; /* successful */
1305 } /* if */
1306 } /* if */
1307 } /* if */
1308 } /* if */
1309 SET_AL(context, AL_result);
1313 /***********************************************************************
1314 * INT21_CloseFileUsingFCB
1316 * Handler for function 0x10.
1318 * PARAMS
1319 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1321 * RETURNS (in AL)
1322 * 0x00: successful
1323 * 0xff: failed
1325 * NOTES
1326 * Closes a FCB file.
1328 static void INT21_CloseFileUsingFCB( CONTEXT86 *context )
1330 struct FCB *fcb;
1331 struct XFCB *xfcb;
1332 BYTE AL_result;
1334 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1335 if (fcb->drive_number == 0xff) {
1336 xfcb = (struct XFCB *) fcb;
1337 fcb = (struct FCB *) xfcb->fcb;
1338 } /* if */
1340 if (_lclose16((HFILE16) fcb->file_number) != 0) {
1341 TRACE("_lclose16(%d) failed\n", fcb->file_number);
1342 AL_result = 0xff; /* failed */
1343 } else {
1344 TRACE("successful closed file %d\n", fcb->file_number);
1345 AL_result = 0x00; /* successful */
1346 } /* if */
1347 SET_AL(context, AL_result);
1351 /***********************************************************************
1352 * INT21_SequentialReadFromFCB
1354 * Handler for function 0x14.
1356 * PARAMS
1357 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1359 * RETURNS (in AL)
1360 * 0: successful
1361 * 1: end of file, no data read
1362 * 2: segment wrap in DTA, no data read (not returned now)
1363 * 3: end of file, partial record read
1365 * NOTES
1366 * Reads a record with the size FCB->logical_record_size from the FCB
1367 * to the disk transfer area. The position of the record is specified
1368 * with FCB->current_block_number and FCB->record_within_current_block.
1369 * Then FCB->current_block_number and FCB->record_within_current_block
1370 * are updated to point to the next record. If a partial record is
1371 * read, it is filled with zeros up to the FCB->logical_record_size.
1373 static void INT21_SequentialReadFromFCB( CONTEXT86 *context )
1375 struct FCB *fcb;
1376 struct XFCB *xfcb;
1377 HANDLE handle;
1378 DWORD record_number;
1379 long position;
1380 BYTE *disk_transfer_area;
1381 UINT bytes_read;
1382 BYTE AL_result;
1384 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1385 if (fcb->drive_number == 0xff) {
1386 xfcb = (struct XFCB *) fcb;
1387 fcb = (struct FCB *) xfcb->fcb;
1388 } /* if */
1390 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1391 if (handle == INVALID_HANDLE_VALUE) {
1392 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1393 fcb->file_number);
1394 AL_result = 0x01; /* end of file, no data read */
1395 } else {
1396 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1397 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1398 if (position != record_number * fcb->logical_record_size) {
1399 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1400 fcb->file_number, record_number * fcb->logical_record_size, position);
1401 AL_result = 0x01; /* end of file, no data read */
1402 } else {
1403 disk_transfer_area = INT21_GetCurrentDTA(context);
1404 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1405 if (bytes_read != fcb->logical_record_size) {
1406 TRACE("_lread(%d, %p, %d) failed with %d\n",
1407 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1408 if (bytes_read == 0) {
1409 AL_result = 0x01; /* end of file, no data read */
1410 } else {
1411 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1412 AL_result = 0x03; /* end of file, partial record read */
1413 } /* if */
1414 } else {
1415 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1416 bytes_read, record_number, position, fcb->file_number, handle);
1417 AL_result = 0x00; /* successful */
1418 } /* if */
1419 } /* if */
1420 } /* if */
1421 if (AL_result == 0x00 || AL_result == 0x03) {
1422 if (fcb->record_within_current_block == 127) {
1423 fcb->record_within_current_block = 0;
1424 fcb->current_block_number++;
1425 } else {
1426 fcb->record_within_current_block++;
1427 } /* if */
1428 } /* if */
1429 SET_AL(context, AL_result);
1433 /***********************************************************************
1434 * INT21_SequentialWriteToFCB
1436 * Handler for function 0x15.
1438 * PARAMS
1439 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1441 * RETURNS (in AL)
1442 * 0: successful
1443 * 1: disk full
1444 * 2: segment wrap in DTA (not returned now)
1446 * NOTES
1447 * Writes a record with the size FCB->logical_record_size from the disk
1448 * transfer area to the FCB. The position of the record is specified
1449 * with FCB->current_block_number and FCB->record_within_current_block.
1450 * Then FCB->current_block_number and FCB->record_within_current_block
1451 * are updated to point to the next record.
1453 static void INT21_SequentialWriteToFCB( CONTEXT86 *context )
1455 struct FCB *fcb;
1456 struct XFCB *xfcb;
1457 HANDLE handle;
1458 DWORD record_number;
1459 long position;
1460 BYTE *disk_transfer_area;
1461 UINT bytes_written;
1462 BYTE AL_result;
1464 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1465 if (fcb->drive_number == 0xff) {
1466 xfcb = (struct XFCB *) fcb;
1467 fcb = (struct FCB *) xfcb->fcb;
1468 } /* if */
1470 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1471 if (handle == INVALID_HANDLE_VALUE) {
1472 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1473 fcb->file_number);
1474 AL_result = 0x01; /* disk full */
1475 } else {
1476 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1477 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1478 if (position != record_number * fcb->logical_record_size) {
1479 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1480 fcb->file_number, record_number * fcb->logical_record_size, position);
1481 AL_result = 0x01; /* disk full */
1482 } else {
1483 disk_transfer_area = INT21_GetCurrentDTA(context);
1484 bytes_written = _lwrite((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1485 if (bytes_written != fcb->logical_record_size) {
1486 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1487 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1488 AL_result = 0x01; /* disk full */
1489 } else {
1490 TRACE("successful written %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1491 bytes_written, record_number, position, fcb->file_number, handle);
1492 AL_result = 0x00; /* successful */
1493 } /* if */
1494 } /* if */
1495 } /* if */
1496 if (AL_result == 0x00) {
1497 if (fcb->record_within_current_block == 127) {
1498 fcb->record_within_current_block = 0;
1499 fcb->current_block_number++;
1500 } else {
1501 fcb->record_within_current_block++;
1502 } /* if */
1503 } /* if */
1504 SET_AL(context, AL_result);
1508 /***********************************************************************
1509 * INT21_ReadRandomRecordFromFCB
1511 * Handler for function 0x21.
1513 * PARAMS
1514 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1516 * RETURNS (in AL)
1517 * 0: successful
1518 * 1: end of file, no data read
1519 * 2: segment wrap in DTA, no data read (not returned now)
1520 * 3: end of file, partial record read
1522 * NOTES
1523 * Reads a record with the size FCB->logical_record_size from
1524 * the FCB to the disk transfer area. The position of the record
1525 * is specified with FCB->random_access_record_number. The
1526 * FCB->random_access_record_number is not updated. If a partial record
1527 * is read, it is filled with zeros up to the FCB->logical_record_size.
1529 static void INT21_ReadRandomRecordFromFCB( CONTEXT86 *context )
1531 struct FCB *fcb;
1532 struct XFCB *xfcb;
1533 HANDLE handle;
1534 DWORD record_number;
1535 long position;
1536 BYTE *disk_transfer_area;
1537 UINT bytes_read;
1538 BYTE AL_result;
1540 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1541 if (fcb->drive_number == 0xff) {
1542 xfcb = (struct XFCB *) fcb;
1543 fcb = (struct FCB *) xfcb->fcb;
1544 } /* if */
1546 memcpy(&record_number, fcb->random_access_record_number, 4);
1547 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1548 if (handle == INVALID_HANDLE_VALUE) {
1549 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1550 fcb->file_number);
1551 AL_result = 0x01; /* end of file, no data read */
1552 } else {
1553 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1554 if (position != record_number * fcb->logical_record_size) {
1555 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1556 fcb->file_number, record_number * fcb->logical_record_size, position);
1557 AL_result = 0x01; /* end of file, no data read */
1558 } else {
1559 disk_transfer_area = INT21_GetCurrentDTA(context);
1560 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1561 if (bytes_read != fcb->logical_record_size) {
1562 TRACE("_lread(%d, %p, %d) failed with %d\n",
1563 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1564 if (bytes_read == 0) {
1565 AL_result = 0x01; /* end of file, no data read */
1566 } else {
1567 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1568 AL_result = 0x03; /* end of file, partial record read */
1569 } /* if */
1570 } else {
1571 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1572 bytes_read, record_number, position, fcb->file_number, handle);
1573 AL_result = 0x00; /* successful */
1574 } /* if */
1575 } /* if */
1576 } /* if */
1577 fcb->current_block_number = record_number / 128;
1578 fcb->record_within_current_block = record_number % 128;
1579 SET_AL(context, AL_result);
1583 /***********************************************************************
1584 * INT21_WriteRandomRecordToFCB
1586 * Handler for function 0x22.
1588 * PARAMS
1589 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1591 * RETURNS (in AL)
1592 * 0: successful
1593 * 1: disk full
1594 * 2: segment wrap in DTA (not returned now)
1596 * NOTES
1597 * Writes a record with the size FCB->logical_record_size from
1598 * the disk transfer area to the FCB. The position of the record
1599 * is specified with FCB->random_access_record_number. The
1600 * FCB->random_access_record_number is not updated.
1602 static void INT21_WriteRandomRecordToFCB( CONTEXT86 *context )
1604 struct FCB *fcb;
1605 struct XFCB *xfcb;
1606 HANDLE handle;
1607 DWORD record_number;
1608 long position;
1609 BYTE *disk_transfer_area;
1610 UINT bytes_written;
1611 BYTE AL_result;
1613 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1614 if (fcb->drive_number == 0xff) {
1615 xfcb = (struct XFCB *) fcb;
1616 fcb = (struct FCB *) xfcb->fcb;
1617 } /* if */
1619 memcpy(&record_number, fcb->random_access_record_number, 4);
1620 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1621 if (handle == INVALID_HANDLE_VALUE) {
1622 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1623 fcb->file_number);
1624 AL_result = 0x01; /* disk full */
1625 } else {
1626 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1627 if (position != record_number * fcb->logical_record_size) {
1628 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1629 fcb->file_number, record_number * fcb->logical_record_size, position);
1630 AL_result = 0x01; /* disk full */
1631 } else {
1632 disk_transfer_area = INT21_GetCurrentDTA(context);
1633 bytes_written = _lwrite((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1634 if (bytes_written != fcb->logical_record_size) {
1635 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1636 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1637 AL_result = 0x01; /* disk full */
1638 } else {
1639 TRACE("successful written %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1640 bytes_written, record_number, position, fcb->file_number, handle);
1641 AL_result = 0x00; /* successful */
1642 } /* if */
1643 } /* if */
1644 } /* if */
1645 fcb->current_block_number = record_number / 128;
1646 fcb->record_within_current_block = record_number % 128;
1647 SET_AL(context, AL_result);
1651 /***********************************************************************
1652 * INT21_RandomBlockReadFromFCB
1654 * Handler for function 0x27.
1656 * PARAMS
1657 * CX [I/O] Number of records to read
1658 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1660 * RETURNS (in AL)
1661 * 0: successful
1662 * 1: end of file, no data read
1663 * 2: segment wrap in DTA, no data read (not returned now)
1664 * 3: end of file, partial record read
1666 * NOTES
1667 * Reads several records with the size FCB->logical_record_size from
1668 * the FCB to the disk transfer area. The number of records to be
1669 * read is specified in the CX register. The position of the first
1670 * record is specified with FCB->random_access_record_number. The
1671 * FCB->random_access_record_number, the FCB->current_block_number
1672 * and FCB->record_within_current_block are updated to point to the
1673 * next record after the records read. If a partial record is read,
1674 * it is filled with zeros up to the FCB->logical_record_size. The
1675 * CX register is set to the number of successfully read records.
1677 static void INT21_RandomBlockReadFromFCB( CONTEXT86 *context )
1679 struct FCB *fcb;
1680 struct XFCB *xfcb;
1681 HANDLE handle;
1682 DWORD record_number;
1683 long position;
1684 BYTE *disk_transfer_area;
1685 UINT records_requested;
1686 UINT bytes_requested;
1687 UINT bytes_read;
1688 UINT records_read;
1689 BYTE AL_result;
1691 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1692 if (fcb->drive_number == 0xff) {
1693 xfcb = (struct XFCB *) fcb;
1694 fcb = (struct FCB *) xfcb->fcb;
1695 } /* if */
1697 memcpy(&record_number, fcb->random_access_record_number, 4);
1698 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1699 if (handle == INVALID_HANDLE_VALUE) {
1700 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1701 fcb->file_number);
1702 records_read = 0;
1703 AL_result = 0x01; /* end of file, no data read */
1704 } else {
1705 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1706 if (position != record_number * fcb->logical_record_size) {
1707 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1708 fcb->file_number, record_number * fcb->logical_record_size, position);
1709 records_read = 0;
1710 AL_result = 0x01; /* end of file, no data read */
1711 } else {
1712 disk_transfer_area = INT21_GetCurrentDTA(context);
1713 records_requested = CX_reg(context);
1714 bytes_requested = (UINT) records_requested * fcb->logical_record_size;
1715 bytes_read = _lread((HFILE) handle, disk_transfer_area, bytes_requested);
1716 if (bytes_read != bytes_requested) {
1717 TRACE("_lread(%d, %p, %d) failed with %d\n",
1718 fcb->file_number, disk_transfer_area, bytes_requested, bytes_read);
1719 records_read = bytes_read / fcb->logical_record_size;
1720 if (bytes_read % fcb->logical_record_size == 0) {
1721 AL_result = 0x01; /* end of file, no data read */
1722 } else {
1723 records_read++;
1724 memset(&disk_transfer_area[bytes_read], 0, records_read * fcb->logical_record_size - bytes_read);
1725 AL_result = 0x03; /* end of file, partial record read */
1726 } /* if */
1727 } else {
1728 TRACE("successful read %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1729 bytes_read, record_number, position, fcb->file_number, handle);
1730 records_read = records_requested;
1731 AL_result = 0x00; /* successful */
1732 } /* if */
1733 } /* if */
1734 } /* if */
1735 record_number += records_read;
1736 memcpy(fcb->random_access_record_number, &record_number, 4);
1737 fcb->current_block_number = record_number / 128;
1738 fcb->record_within_current_block = record_number % 128;
1739 SET_CX(context, records_read);
1740 SET_AL(context, AL_result);
1744 /***********************************************************************
1745 * INT21_RandomBlockWriteToFCB
1747 * Handler for function 0x28.
1749 * PARAMS
1750 * CX [I/O] Number of records to write
1751 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1753 * RETURNS (in AL)
1754 * 0: successful
1755 * 1: disk full
1756 * 2: segment wrap in DTA (not returned now)
1758 * NOTES
1759 * Writes several records with the size FCB->logical_record_size from
1760 * the disk transfer area to the FCB. The number of records to be
1761 * written is specified in the CX register. The position of the first
1762 * record is specified with FCB->random_access_record_number. The
1763 * FCB->random_access_record_number, the FCB->current_block_number
1764 * and FCB->record_within_current_block are updated to point to the
1765 * next record after the records written. The CX register is set to
1766 * the number of successfully written records.
1768 static void INT21_RandomBlockWriteToFCB( CONTEXT86 *context )
1770 struct FCB *fcb;
1771 struct XFCB *xfcb;
1772 HANDLE handle;
1773 DWORD record_number;
1774 long position;
1775 BYTE *disk_transfer_area;
1776 UINT records_requested;
1777 UINT bytes_requested;
1778 UINT bytes_written;
1779 UINT records_written;
1780 BYTE AL_result;
1782 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1783 if (fcb->drive_number == 0xff) {
1784 xfcb = (struct XFCB *) fcb;
1785 fcb = (struct FCB *) xfcb->fcb;
1786 } /* if */
1788 memcpy(&record_number, fcb->random_access_record_number, 4);
1789 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1790 if (handle == INVALID_HANDLE_VALUE) {
1791 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1792 fcb->file_number);
1793 records_written = 0;
1794 AL_result = 0x01; /* disk full */
1795 } else {
1796 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1797 if (position != record_number * fcb->logical_record_size) {
1798 TRACE("seek(%d, %ld, 0) failed with %ld\n",
1799 fcb->file_number, record_number * fcb->logical_record_size, position);
1800 records_written = 0;
1801 AL_result = 0x01; /* disk full */
1802 } else {
1803 disk_transfer_area = INT21_GetCurrentDTA(context);
1804 records_requested = CX_reg(context);
1805 bytes_requested = (UINT) records_requested * fcb->logical_record_size;
1806 bytes_written = _lwrite((HFILE) handle, disk_transfer_area, bytes_requested);
1807 if (bytes_written != bytes_requested) {
1808 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1809 fcb->file_number, disk_transfer_area, bytes_requested, bytes_written);
1810 records_written = bytes_written / fcb->logical_record_size;
1811 AL_result = 0x01; /* disk full */
1812 } else {
1813 TRACE("successful write %d bytes from record %ld (position %ld) of file %d (handle %p)\n",
1814 bytes_written, record_number, position, fcb->file_number, handle);
1815 records_written = records_requested;
1816 AL_result = 0x00; /* successful */
1817 } /* if */
1818 } /* if */
1819 } /* if */
1820 record_number += records_written;
1821 memcpy(fcb->random_access_record_number, &record_number, 4);
1822 fcb->current_block_number = record_number / 128;
1823 fcb->record_within_current_block = record_number % 128;
1824 SET_CX(context, records_written);
1825 SET_AL(context, AL_result);
1829 /***********************************************************************
1830 * INT21_CreateDirectory
1832 * Handler for:
1833 * - function 0x39
1834 * - subfunction 0x39 of function 0x71
1835 * - subfunction 0xff of function 0x43 (CL == 0x39)
1837 static BOOL INT21_CreateDirectory( CONTEXT86 *context )
1839 WCHAR dirW[MAX_PATH];
1840 char *dirA = CTX_SEG_OFF_TO_LIN(context,
1841 context->SegDs,
1842 context->Edx);
1844 TRACE( "CREATE DIRECTORY %s\n", dirA );
1846 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
1848 if (CreateDirectoryW(dirW, NULL))
1849 return TRUE;
1852 * FIXME: CreateDirectory's LastErrors will clash with the ones
1853 * used by DOS. AH=39 only returns 3 (path not found) and
1854 * 5 (access denied), while CreateDirectory return several
1855 * ones. Remap some of them. -Marcus
1857 switch (GetLastError())
1859 case ERROR_ALREADY_EXISTS:
1860 case ERROR_FILENAME_EXCED_RANGE:
1861 case ERROR_DISK_FULL:
1862 SetLastError(ERROR_ACCESS_DENIED);
1863 break;
1864 default:
1865 break;
1868 return FALSE;
1872 /***********************************************************************
1873 * INT21_ExtendedCountryInformation
1875 * Handler for function 0x65.
1877 static void INT21_ExtendedCountryInformation( CONTEXT86 *context )
1879 BYTE *dataptr = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
1880 BYTE buffsize = CX_reg (context);
1882 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1883 AL_reg(context) );
1886 * Check subfunctions that are passed country and code page.
1888 if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
1890 WORD country = DX_reg(context);
1891 WORD codepage = BX_reg(context);
1893 if (country != 0xffff && country != INT21_GetSystemCountryCode())
1894 FIXME( "Requested info on non-default country %04x\n", country );
1896 if (codepage != 0xffff && codepage != GetOEMCP())
1897 FIXME( "Requested info on non-default code page %04x\n", codepage );
1900 switch (AL_reg(context)) {
1901 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1902 INT_BARF( context, 0x21 );
1903 SET_CFLAG( context );
1904 break;
1906 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1907 TRACE( "Get general internationalization info\n" );
1908 dataptr[0] = 0x01; /* Info ID */
1909 *(WORD*)(dataptr+1) = 38; /* Size of the following info */
1910 *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
1911 *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
1912 /* FIXME: fill buffer partially up to buffsize bytes*/
1913 if (buffsize >= 0x29){
1914 INT21_FillCountryInformation( dataptr + 7 );
1915 SET_CX( context, 0x29 ); /* Size of returned info */
1916 }else{
1917 SET_CX( context, 0x07 ); /* Size of returned info */
1919 break;
1921 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1922 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1923 TRACE( "Get pointer to uppercase table\n" );
1924 dataptr[0] = AL_reg(context); /* Info ID */
1925 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1926 offsetof(INT21_HEAP, uppercase_size) );
1927 SET_CX( context, 5 ); /* Size of returned info */
1928 break;
1930 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1931 TRACE( "Get pointer to lowercase table\n" );
1932 dataptr[0] = 0x03; /* Info ID */
1933 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1934 offsetof(INT21_HEAP, lowercase_size) );
1935 SET_CX( context, 5 ); /* Size of returned info */
1936 break;
1938 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1939 TRACE("Get pointer to filename terminator table\n");
1940 dataptr[0] = 0x05; /* Info ID */
1941 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1942 offsetof(INT21_HEAP, filename_size) );
1943 SET_CX( context, 5 ); /* Size of returned info */
1944 break;
1946 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1947 TRACE("Get pointer to collating sequence table\n");
1948 dataptr[0] = 0x06; /* Info ID */
1949 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1950 offsetof(INT21_HEAP, collating_size) );
1951 SET_CX( context, 5 ); /* Size of returned info */
1952 break;
1954 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1955 TRACE("Get pointer to DBCS lead byte table\n");
1956 dataptr[0] = 0x07; /* Info ID */
1957 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1958 offsetof(INT21_HEAP, dbcs_size) );
1959 SET_CX( context, 5 ); /* Size of returned info */
1960 break;
1962 case 0x20: /* CAPITALIZE CHARACTER */
1963 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1964 TRACE("Convert char to uppercase\n");
1965 SET_DL( context, toupper(DL_reg(context)) );
1966 break;
1968 case 0x21: /* CAPITALIZE STRING */
1969 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1970 TRACE("Convert string to uppercase with length\n");
1972 char *ptr = (char *)CTX_SEG_OFF_TO_LIN( context,
1973 context->SegDs,
1974 context->Edx );
1975 WORD len = CX_reg(context);
1976 while (len--) { *ptr = toupper(*ptr); ptr++; }
1978 break;
1980 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1981 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1982 TRACE("Convert ASCIIZ string to uppercase\n");
1983 _strupr( (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx) );
1984 break;
1986 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1987 INT_BARF( context, 0x21 );
1988 SET_CFLAG( context );
1989 break;
1991 default:
1992 INT_BARF( context, 0x21 );
1993 SET_CFLAG(context);
1994 break;
1999 /***********************************************************************
2000 * INT21_FileAttributes
2002 * Handler for:
2003 * - function 0x43
2004 * - subfunction 0x43 of function 0x71
2006 static BOOL INT21_FileAttributes( CONTEXT86 *context,
2007 BYTE subfunction,
2008 BOOL islong )
2010 WCHAR fileW[MAX_PATH];
2011 char *fileA = CTX_SEG_OFF_TO_LIN(context,
2012 context->SegDs,
2013 context->Edx);
2014 HANDLE handle;
2015 BOOL status;
2016 FILETIME filetime;
2017 DWORD result;
2018 WORD date, time;
2020 switch (subfunction)
2022 case 0x00: /* GET FILE ATTRIBUTES */
2023 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA );
2024 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2026 result = GetFileAttributesW( fileW );
2027 if (result == INVALID_FILE_ATTRIBUTES)
2028 return FALSE;
2029 else
2031 SET_CX( context, (WORD)result );
2032 if (!islong)
2033 SET_AX( context, (WORD)result ); /* DR DOS */
2035 break;
2037 case 0x01: /* SET FILE ATTRIBUTES */
2038 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
2039 CX_reg(context), fileA );
2040 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2042 if (!SetFileAttributesW( fileW, CX_reg(context) ))
2043 return FALSE;
2044 break;
2046 case 0x02: /* GET COMPRESSED FILE SIZE */
2047 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA );
2048 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2050 result = GetCompressedFileSizeW( fileW, NULL );
2051 if (result == INVALID_FILE_SIZE)
2052 return FALSE;
2053 else
2055 SET_AX( context, LOWORD(result) );
2056 SET_DX( context, HIWORD(result) );
2058 break;
2060 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
2061 if (!islong)
2062 INT_BARF( context, 0x21 );
2063 else
2065 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
2066 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2068 handle = CreateFileW( fileW, GENERIC_WRITE,
2069 FILE_SHARE_READ | FILE_SHARE_WRITE,
2070 NULL, OPEN_EXISTING, 0, 0 );
2071 if (handle == INVALID_HANDLE_VALUE)
2072 return FALSE;
2074 DosDateTimeToFileTime( DI_reg(context),
2075 CX_reg(context),
2076 &filetime );
2077 status = SetFileTime( handle, NULL, NULL, &filetime );
2079 CloseHandle( handle );
2080 return status;
2082 break;
2084 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
2085 if (!islong)
2086 INT_BARF( context, 0x21 );
2087 else
2089 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
2090 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2092 handle = CreateFileW( fileW, GENERIC_READ,
2093 FILE_SHARE_READ | FILE_SHARE_WRITE,
2094 NULL, OPEN_EXISTING, 0, 0 );
2095 if (handle == INVALID_HANDLE_VALUE)
2096 return FALSE;
2098 status = GetFileTime( handle, NULL, NULL, &filetime );
2099 if (status)
2101 FileTimeToDosDateTime( &filetime, &date, &time );
2102 SET_DI( context, date );
2103 SET_CX( context, time );
2106 CloseHandle( handle );
2107 return status;
2109 break;
2111 case 0x05: /* SET FILE LAST ACCESS DATE */
2112 if (!islong)
2113 INT_BARF( context, 0x21 );
2114 else
2116 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA );
2117 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2119 handle = CreateFileW( fileW, GENERIC_WRITE,
2120 FILE_SHARE_READ | FILE_SHARE_WRITE,
2121 NULL, OPEN_EXISTING, 0, 0 );
2122 if (handle == INVALID_HANDLE_VALUE)
2123 return FALSE;
2125 DosDateTimeToFileTime( DI_reg(context),
2127 &filetime );
2128 status = SetFileTime( handle, NULL, &filetime, NULL );
2130 CloseHandle( handle );
2131 return status;
2133 break;
2135 case 0x06: /* GET FILE LAST ACCESS DATE */
2136 if (!islong)
2137 INT_BARF( context, 0x21 );
2138 else
2140 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA );
2141 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2143 handle = CreateFileW( fileW, GENERIC_READ,
2144 FILE_SHARE_READ | FILE_SHARE_WRITE,
2145 NULL, OPEN_EXISTING, 0, 0 );
2146 if (handle == INVALID_HANDLE_VALUE)
2147 return FALSE;
2149 status = GetFileTime( handle, NULL, &filetime, NULL );
2150 if (status)
2152 FileTimeToDosDateTime( &filetime, &date, NULL );
2153 SET_DI( context, date );
2156 CloseHandle( handle );
2157 return status;
2159 break;
2161 case 0x07: /* SET FILE CREATION DATE AND TIME */
2162 if (!islong)
2163 INT_BARF( context, 0x21 );
2164 else
2166 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA );
2168 handle = CreateFileW( fileW, GENERIC_WRITE,
2169 FILE_SHARE_READ | FILE_SHARE_WRITE,
2170 NULL, OPEN_EXISTING, 0, 0 );
2171 if (handle == INVALID_HANDLE_VALUE)
2172 return FALSE;
2175 * FIXME: SI has number of 10-millisecond units past time in CX.
2177 DosDateTimeToFileTime( DI_reg(context),
2178 CX_reg(context),
2179 &filetime );
2180 status = SetFileTime( handle, &filetime, NULL, NULL );
2182 CloseHandle( handle );
2183 return status;
2185 break;
2187 case 0x08: /* GET FILE CREATION DATE AND TIME */
2188 if (!islong)
2189 INT_BARF( context, 0x21 );
2190 else
2192 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2193 BX_reg(context) );
2195 handle = CreateFileW( fileW, GENERIC_READ,
2196 FILE_SHARE_READ | FILE_SHARE_WRITE,
2197 NULL, OPEN_EXISTING, 0, 0 );
2198 if (handle == INVALID_HANDLE_VALUE)
2199 return FALSE;
2201 status = GetFileTime( handle, &filetime, NULL, NULL );
2202 if (status)
2204 FileTimeToDosDateTime( &filetime, &date, &time );
2205 SET_DI( context, date );
2206 SET_CX( context, time );
2208 * FIXME: SI has number of 10-millisecond units past
2209 * time in CX.
2211 SET_SI( context, 0 );
2214 CloseHandle(handle);
2215 return status;
2217 break;
2219 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2220 if (islong || context->Ebp != 0x5053)
2221 INT_BARF( context, 0x21 );
2222 else
2224 switch(CL_reg(context))
2226 case 0x39:
2227 if (!INT21_CreateDirectory( context ))
2228 return FALSE;
2229 break;
2231 case 0x56:
2232 if (!INT21_RenameFile( context ))
2233 return FALSE;
2234 break;
2236 default:
2237 INT_BARF( context, 0x21 );
2240 break;
2242 default:
2243 INT_BARF( context, 0x21 );
2246 return TRUE;
2250 /***********************************************************************
2251 * INT21_FileDateTime
2253 * Handler for function 0x57.
2255 static BOOL INT21_FileDateTime( CONTEXT86 *context )
2257 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2258 FILETIME filetime;
2259 WORD date, time;
2261 switch (AL_reg(context)) {
2262 case 0x00: /* Get last-written stamp */
2263 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2264 BX_reg(context) );
2266 if (!GetFileTime( handle, NULL, NULL, &filetime ))
2267 return FALSE;
2268 FileTimeToDosDateTime( &filetime, &date, &time );
2269 SET_DX( context, date );
2270 SET_CX( context, time );
2271 break;
2274 case 0x01: /* Set last-written stamp */
2275 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2276 BX_reg(context) );
2278 DosDateTimeToFileTime( DX_reg(context),
2279 CX_reg(context),
2280 &filetime );
2281 if (!SetFileTime( handle, NULL, NULL, &filetime ))
2282 return FALSE;
2283 break;
2286 case 0x04: /* Get last access stamp, DOS 7 */
2287 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2288 BX_reg(context) );
2290 if (!GetFileTime( handle, NULL, &filetime, NULL ))
2291 return FALSE;
2292 FileTimeToDosDateTime( &filetime, &date, &time );
2293 SET_DX( context, date );
2294 SET_CX( context, time );
2295 break;
2298 case 0x05: /* Set last access stamp, DOS 7 */
2299 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2300 BX_reg(context) );
2302 DosDateTimeToFileTime( DX_reg(context),
2303 CX_reg(context),
2304 &filetime );
2305 if (!SetFileTime( handle, NULL, &filetime, NULL ))
2306 return FALSE;
2307 break;
2310 case 0x06: /* Get creation stamp, DOS 7 */
2311 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2312 BX_reg(context) );
2314 if (!GetFileTime( handle, &filetime, NULL, NULL ))
2315 return FALSE;
2316 FileTimeToDosDateTime( &filetime, &date, &time );
2317 SET_DX( context, date );
2318 SET_CX( context, time );
2320 * FIXME: SI has number of 10-millisecond units past time in CX.
2322 SET_SI( context, 0 );
2323 break;
2326 case 0x07: /* Set creation stamp, DOS 7 */
2327 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2328 BX_reg(context) );
2331 * FIXME: SI has number of 10-millisecond units past time in CX.
2333 DosDateTimeToFileTime( DX_reg(context),
2334 CX_reg(context),
2335 &filetime );
2336 if (!SetFileTime( handle, &filetime, NULL, NULL ))
2337 return FALSE;
2338 break;
2341 default:
2342 INT_BARF( context, 0x21 );
2343 break;
2346 return TRUE;
2350 /***********************************************************************
2351 * INT21_GetPSP
2353 * Handler for functions 0x51 and 0x62.
2355 static void INT21_GetPSP( CONTEXT86 *context )
2357 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
2360 * FIXME: should we return the original DOS PSP upon
2361 * Windows startup ?
2363 if (!ISV86(context) && DOSVM_IsWin16())
2364 SET_BX( context, LOWORD(GetCurrentPDB16()) );
2365 else
2366 SET_BX( context, DOSVM_psp );
2369 static inline void setword( BYTE *ptr, WORD w )
2371 ptr[0] = (BYTE)w;
2372 ptr[1] = (BYTE)(w >> 8);
2375 static void CreateBPB(int drive, BYTE *data, BOOL16 limited)
2376 /* limited == TRUE is used with INT 0x21/0x440d */
2378 /* FIXME: we're forcing some values without checking that those are valid */
2379 if (drive > 1)
2381 setword(data, 512);
2382 data[2] = 2;
2383 setword(&data[3], 0);
2384 data[5] = 2;
2385 setword(&data[6], 240);
2386 setword(&data[8], 64000);
2387 data[0x0a] = 0xf8;
2388 setword(&data[0x0b], 40);
2389 setword(&data[0x0d], 56);
2390 setword(&data[0x0f], 2);
2391 setword(&data[0x11], 0);
2392 if (!limited)
2394 setword(&data[0x1f], 800);
2395 data[0x21] = 5;
2396 setword(&data[0x22], 1);
2399 else
2400 { /* 1.44mb */
2401 setword(data, 512);
2402 data[2] = 2;
2403 setword(&data[3], 0);
2404 data[5] = 2;
2405 setword(&data[6], 240);
2406 setword(&data[8], 2880);
2407 data[0x0a] = 0xf8;
2408 setword(&data[0x0b], 6);
2409 setword(&data[0x0d], 18);
2410 setword(&data[0x0f], 2);
2411 setword(&data[0x11], 0);
2412 if (!limited)
2414 setword(&data[0x1f], 80);
2415 data[0x21] = 7;
2416 setword(&data[0x22], 2);
2421 inline DWORD INT21_Ioctl_CylHeadSect2Lin(DWORD cyl, WORD head, WORD sec, WORD cyl_cnt, WORD head_cnt, WORD sec_cnt)
2423 DWORD res = (cyl * head_cnt*sec_cnt + head * sec_cnt + sec);
2424 return res;
2427 /***********************************************************************
2428 * INT21_Ioctl_Block
2430 * Handler for block device IOCTLs.
2432 static void INT21_Ioctl_Block( CONTEXT86 *context )
2434 BYTE *dataptr;
2435 BYTE drive = INT21_MapDrive( BL_reg(context) );
2436 WCHAR drivespec[4] = {'A', ':', '\\', 0};
2437 UINT drivetype;
2439 drivespec[0] += drive;
2440 drivetype = GetDriveTypeW( drivespec );
2442 RESET_CFLAG(context);
2443 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
2445 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2446 AL_reg(context), 'A' + drive );
2447 SetLastError( ERROR_INVALID_DRIVE );
2448 SET_AX( context, ERROR_INVALID_DRIVE );
2449 SET_CFLAG( context );
2450 return;
2453 switch (AL_reg(context))
2455 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2456 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2457 INT_BARF( context, 0x21 );
2458 break;
2460 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2461 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2462 'A' + drive );
2464 if (drivetype == DRIVE_REMOVABLE)
2465 SET_AX( context, 0 ); /* removable */
2466 else
2467 SET_AX( context, 1 ); /* not removable */
2468 break;
2470 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2471 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2472 'A' + drive );
2474 if (drivetype == DRIVE_REMOTE)
2475 SET_DX( context, (1<<9) | (1<<12) ); /* remote + no direct IO */
2476 else
2477 SET_DX( context, 0 ); /* FIXME: use driver attr here */
2478 break;
2480 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2481 /* Get pointer to IOCTL parameter block */
2482 dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2484 switch (CX_reg(context))
2486 case 0x0841: /* write logical device track */
2487 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2488 'A' + drive);
2490 WORD head = *(WORD *)(dataptr+1);
2491 WORD cyl = *(WORD *)(dataptr+3);
2492 WORD sect = *(WORD *)(dataptr+5);
2493 WORD nrsect = *(WORD *)(dataptr+7);
2494 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2495 WORD cyl_cnt, head_cnt, sec_cnt;
2497 /* FIXME: we're faking some values here */
2498 if (drive > 1)
2500 /* cyl_cnt = 0x300;
2501 head_cnt = 16;
2502 sec_cnt = 255; */
2503 SET_AX( context, ERROR_WRITE_FAULT );
2504 SET_CFLAG(context);
2505 break;
2507 else
2508 { /* floppy */
2509 cyl_cnt = 80;
2510 head_cnt = 2;
2511 sec_cnt = 18;
2514 if (!DOSVM_RawWrite(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2516 SET_AX( context, ERROR_WRITE_FAULT );
2517 SET_CFLAG(context);
2520 break;
2522 case 0x084a: /* lock logical volume */
2523 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2524 BH_reg(context), DX_reg(context), 'A' + drive );
2525 break;
2527 case 0x0860: /* get device parameters */
2528 /* FIXME: we're faking some values here */
2529 /* used by w4wgrp's winfile */
2530 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2531 dataptr[0] = 0x04;
2532 dataptr[6] = 0; /* media type */
2533 if (drive > 1)
2535 dataptr[1] = 0x05; /* fixed disk */
2536 setword(&dataptr[2], 0x01); /* non removable */
2537 setword(&dataptr[4], 0x300); /* # of cylinders */
2539 else
2541 dataptr[1] = 0x07; /* block dev, floppy */
2542 setword(&dataptr[2], 0x02); /* removable */
2543 setword(&dataptr[4], 80); /* # of cylinders */
2545 CreateBPB(drive, &dataptr[7], TRUE);
2546 RESET_CFLAG(context);
2547 break;
2549 case 0x0861: /* read logical device track */
2550 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2551 'A' + drive);
2553 WORD head = *(WORD *)(dataptr+1);
2554 WORD cyl = *(WORD *)(dataptr+3);
2555 WORD sect = *(WORD *)(dataptr+5);
2556 WORD nrsect = *(WORD *)(dataptr+7);
2557 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2558 WORD cyl_cnt, head_cnt, sec_cnt;
2560 /* FIXME: we're faking some values here */
2561 if (drive > 1)
2563 cyl_cnt = 0x300;
2564 head_cnt = 16;
2565 sec_cnt = 255;
2567 else
2568 { /* floppy */
2569 cyl_cnt = 80;
2570 head_cnt = 2;
2571 sec_cnt = 18;
2574 if (!DOSVM_RawRead(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2576 SET_AX( context, ERROR_READ_FAULT );
2577 SET_CFLAG(context);
2580 break;
2582 case 0x0866: /* get volume serial number */
2584 WCHAR label[12],fsname[9];
2585 DWORD serial;
2587 drivespec[0] += drive;
2588 GetVolumeInformationW(drivespec, label, 12, &serial, NULL, NULL, fsname, 9);
2589 *(WORD*)dataptr = 0;
2590 memcpy(dataptr+2,&serial,4);
2591 WideCharToMultiByte(CP_OEMCP, 0, label, 11, dataptr + 6, 11, NULL, NULL);
2592 WideCharToMultiByte(CP_OEMCP, 0, fsname, 8, dataptr + 17, 8, NULL, NULL);
2594 break;
2596 case 0x086a: /* unlock logical volume */
2597 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2598 'A' + drive );
2599 break;
2601 case 0x086f: /* get drive map information */
2602 memset(dataptr+1, '\0', dataptr[0]-1);
2603 dataptr[1] = dataptr[0];
2604 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
2605 dataptr[3] = 0xFF; /* no physical drive */
2606 break;
2608 case 0x0872:
2609 /* Trial and error implementation */
2610 SET_AX( context, drivetype == DRIVE_UNKNOWN ? 0x0f : 0x01 );
2611 SET_CFLAG(context); /* Seems to be set all the time */
2612 break;
2614 default:
2615 INT_BARF( context, 0x21 );
2617 break;
2619 case 0x0e: /* GET LOGICAL DRIVE MAP */
2620 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2621 'A' + drive );
2622 /* FIXME: this is not correct if drive has mappings */
2623 SET_AL( context, 0 ); /* drive has no mapping */
2624 break;
2626 case 0x0f: /* SET LOGICAL DRIVE MAP */
2628 WCHAR dev[3], tgt[4];
2630 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2631 INT21_DriveName( BL_reg(context)));
2632 dev[0] = 'A' + drive; dev[1] = ':'; dev[2] = 0;
2633 tgt[0] = 'A' + drive + 1; tgt[1] = ':'; tgt[2] = '\\'; tgt[3] = 0;
2634 if (!DefineDosDeviceW(DDD_RAW_TARGET_PATH, dev, tgt))
2636 SET_CFLAG(context);
2637 SET_AX( context, 0x000F ); /* invalid drive */
2640 break;
2642 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2643 default:
2644 INT_BARF( context, 0x21 );
2649 /***********************************************************************
2650 * INT21_IoctlScsiMgrHandler
2652 * IOCTL handler for the SCSIMGR device.
2654 static void INT21_IoctlScsiMgrHandler( CONTEXT86 *context )
2656 switch (AL_reg(context))
2658 case 0x00: /* GET DEVICE INFORMATION */
2659 SET_DX( context, 0xc0c0 );
2660 break;
2662 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2663 DOSVM_ASPIHandler(context);
2664 break;
2666 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2667 SET_DX( context, 0 );
2668 break;
2670 case 0x01: /* SET DEVICE INFORMATION */
2671 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2672 case 0x06: /* GET INPUT STATUS */
2673 case 0x07: /* GET OUTPUT STATUS */
2674 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2675 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2676 default:
2677 INT_BARF( context, 0x21 );
2678 break;
2683 /***********************************************************************
2684 * INT21_IoctlEMSHandler
2686 * IOCTL handler for the EMXXXX0 device.
2688 static void INT21_IoctlEMSHandler( CONTEXT86 *context )
2690 EMS_Ioctl_Handler(context);
2694 /***********************************************************************
2695 * INT21_IoctlHPScanHandler
2697 * IOCTL handler for the HPSCAN device.
2699 static void INT21_IoctlHPScanHandler( CONTEXT86 *context )
2701 switch (AL_reg(context))
2703 case 0x00: /* GET DEVICE INFORMATION */
2704 SET_DX( context, 0xc0c0 );
2705 break;
2707 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2708 SET_DX( context, 0 );
2709 break;
2711 case 0x01: /* SET DEVICE INFORMATION */
2712 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2713 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2714 case 0x06: /* GET INPUT STATUS */
2715 case 0x07: /* GET OUTPUT STATUS */
2716 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2717 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2718 default:
2719 INT_BARF( context, 0x21 );
2720 break;
2725 /***********************************************************************
2726 * INT21_Ioctl_Char
2728 * Handler for character device IOCTLs.
2730 static void INT21_Ioctl_Char( CONTEXT86 *context )
2732 struct stat st;
2733 int status, i, fd;
2734 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2736 status = wine_server_handle_to_fd( handle, 0, &fd, NULL );
2737 if (status)
2739 SET_AX( context, RtlNtStatusToDosError(status) );
2740 SET_CFLAG( context );
2741 return;
2743 fstat( fd, &st );
2744 wine_server_release_fd( handle, fd );
2746 for (i = 0; i < NB_MAGIC_DEVICES; i++)
2748 if (!magic_devices[i].handle) continue;
2749 if (magic_devices[i].dev == st.st_dev && magic_devices[i].ino == st.st_ino)
2751 /* found it */
2752 magic_devices[i].ioctl_handler( context );
2753 return;
2757 /* no magic device found, do default handling */
2759 switch (AL_reg(context))
2761 case 0x00: /* GET DEVICE INFORMATION */
2762 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context) );
2763 if (S_ISCHR(st.st_mode))
2766 * Returns attribute word in DX:
2767 * Bit 14 - Device driver can process IOCTL requests.
2768 * Bit 13 - Output until busy supported.
2769 * Bit 11 - Driver supports OPEN/CLOSE calls.
2770 * Bit 8 - Unknown.
2771 * Bit 7 - Set (indicates device).
2772 * Bit 6 - EOF on input.
2773 * Bit 5 - Raw (binary) mode.
2774 * Bit 4 - Device is special (uses int29).
2775 * Bit 3 - Clock device.
2776 * Bit 2 - NUL device.
2777 * Bit 1 - Standard output.
2778 * Bit 0 - Standard input.
2780 SET_DX( context, 0x80c0 /* FIXME */ );
2782 else
2785 * Returns attribute word in DX:
2786 * Bit 15 - File is remote.
2787 * Bit 14 - Don't set file date/time on closing.
2788 * Bit 11 - Media not removable.
2789 * Bit 8 - Generate int24 if no disk space on write
2790 * or read past end of file
2791 * Bit 7 - Clear (indicates file).
2792 * Bit 6 - File has not been written.
2793 * Bit 5..0 - Drive number (0=A:,...)
2795 * FIXME: Should check if file is on remote or removable drive.
2796 * FIXME: Should use drive file is located on (and not current).
2798 SET_DX( context, 0x0140 + INT21_GetCurrentDrive() );
2800 break;
2802 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2803 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context) );
2805 * Returns attribute word in DX:
2806 * Bit 15 - Set if remote.
2807 * Bit 14 - Set if date/time not set on close.
2809 * FIXME: Should check if file is on remote drive.
2811 SET_DX( context, 0 );
2812 break;
2814 case 0x01: /* SET DEVICE INFORMATION */
2815 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2816 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2817 case 0x06: /* GET INPUT STATUS */
2818 case 0x07: /* GET OUTPUT STATUS */
2819 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2820 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2821 default:
2822 INT_BARF( context, 0x21 );
2823 break;
2828 /***********************************************************************
2829 * INT21_Ioctl
2831 * Handler for function 0x44.
2833 static void INT21_Ioctl( CONTEXT86 *context )
2835 switch (AL_reg(context))
2837 case 0x00:
2838 case 0x01:
2839 case 0x02:
2840 case 0x03:
2841 INT21_Ioctl_Char( context );
2842 break;
2844 case 0x04:
2845 case 0x05:
2846 INT21_Ioctl_Block( context );
2847 break;
2849 case 0x06:
2850 case 0x07:
2851 INT21_Ioctl_Char( context );
2852 break;
2854 case 0x08:
2855 case 0x09:
2856 INT21_Ioctl_Block( context );
2857 break;
2859 case 0x0a:
2860 INT21_Ioctl_Char( context );
2861 break;
2863 case 0x0b: /* SET SHARING RETRY COUNT */
2864 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2865 CX_reg(context), DX_reg(context) );
2866 if (!CX_reg(context))
2868 SET_AX( context, 1 );
2869 SET_CFLAG( context );
2871 else
2873 DOSDEV_SetSharingRetry( CX_reg(context), DX_reg(context) );
2874 RESET_CFLAG( context );
2876 break;
2878 case 0x0c:
2879 INT21_Ioctl_Char( context );
2880 break;
2882 case 0x0d:
2883 case 0x0e:
2884 case 0x0f:
2885 INT21_Ioctl_Block( context );
2886 break;
2888 case 0x10:
2889 INT21_Ioctl_Char( context );
2890 break;
2892 case 0x11:
2893 INT21_Ioctl_Block( context );
2894 break;
2896 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2897 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2898 SET_CFLAG(context); /* Error / This is not DR DOS. */
2899 SET_AX( context, 0x0001 ); /* Invalid function */
2900 break;
2902 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2903 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2904 SET_CFLAG(context); /* Error / This is not DR DOS. */
2905 SET_AX( context, 0x0001 ); /* Invalid function */
2906 break;
2908 case 0xe0: /* Sun PC-NFS API */
2909 TRACE( "Sun PC-NFS API\n" );
2910 /* not installed */
2911 break;
2913 default:
2914 INT_BARF( context, 0x21 );
2919 /***********************************************************************
2920 * INT21_Fat32
2922 * Handler for function 0x73.
2924 static BOOL INT21_Fat32( CONTEXT86 *context )
2926 switch (AL_reg(context))
2928 case 0x02: /* FAT32 - GET EXTENDED DPB */
2930 BYTE drive = INT21_MapDrive( DL_reg(context) );
2931 WORD *ptr = CTX_SEG_OFF_TO_LIN(context,
2932 context->SegEs, context->Edi);
2933 INT21_DPB *target = (INT21_DPB*)(ptr + 1);
2934 INT21_DPB *source;
2936 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context) );
2938 if ( CX_reg(context) < sizeof(INT21_DPB) + 2 || *ptr < sizeof(INT21_DPB) )
2940 SetLastError( ERROR_BAD_LENGTH );
2941 return FALSE;
2944 if ( !INT21_FillDrivePB( drive ) )
2946 SetLastError( ERROR_INVALID_DRIVE );
2947 return FALSE;
2950 source = &INT21_GetHeapPointer()->misc_dpb_list[drive];
2952 *ptr = sizeof(INT21_DPB);
2953 memcpy( target, source, sizeof(INT21_DPB));
2955 if (LOWORD(context->Esi) != 0xF1A6)
2957 target->driver_header = 0;
2958 target->next = 0;
2960 else
2962 FIXME( "Caller requested driver and next DPB pointers!\n" );
2965 break;
2967 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2969 WCHAR dirW[MAX_PATH];
2970 char *dirA = CTX_SEG_OFF_TO_LIN( context,
2971 context->SegDs, context->Edx );
2972 BYTE *data = CTX_SEG_OFF_TO_LIN( context,
2973 context->SegEs, context->Edi );
2974 DWORD cluster_sectors;
2975 DWORD sector_bytes;
2976 DWORD free_clusters;
2977 DWORD total_clusters;
2979 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA );
2980 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
2982 if (CX_reg(context) < 44)
2984 SetLastError( ERROR_BAD_LENGTH );
2985 return FALSE;
2988 if (!GetDiskFreeSpaceW( dirW, &cluster_sectors, &sector_bytes,
2989 &free_clusters, &total_clusters ))
2990 return FALSE;
2992 *(WORD*) (data + 0) = 44; /* size of structure */
2993 *(WORD*) (data + 2) = 0; /* version */
2994 *(DWORD*)(data + 4) = cluster_sectors;
2995 *(DWORD*)(data + 8) = sector_bytes;
2996 *(DWORD*)(data + 12) = free_clusters;
2997 *(DWORD*)(data + 16) = total_clusters;
3000 * Below we have free/total sectors and
3001 * free/total allocation units without adjustment
3002 * for compression. We fake both using cluster information.
3004 *(DWORD*)(data + 20) = free_clusters * cluster_sectors;
3005 *(DWORD*)(data + 24) = total_clusters * cluster_sectors;
3006 *(DWORD*)(data + 28) = free_clusters;
3007 *(DWORD*)(data + 32) = total_clusters;
3010 * Between (data + 36) and (data + 43) there
3011 * are eight reserved bytes.
3014 break;
3016 default:
3017 INT_BARF( context, 0x21 );
3020 return TRUE;
3023 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA *dataA,
3024 const WIN32_FIND_DATAW *dataW)
3026 dataA->dwFileAttributes = dataW->dwFileAttributes;
3027 dataA->ftCreationTime = dataW->ftCreationTime;
3028 dataA->ftLastAccessTime = dataW->ftLastAccessTime;
3029 dataA->ftLastWriteTime = dataW->ftLastWriteTime;
3030 dataA->nFileSizeHigh = dataW->nFileSizeHigh;
3031 dataA->nFileSizeLow = dataW->nFileSizeLow;
3032 WideCharToMultiByte( CP_OEMCP, 0, dataW->cFileName, -1,
3033 dataA->cFileName, sizeof(dataA->cFileName), NULL, NULL );
3034 WideCharToMultiByte( CP_OEMCP, 0, dataW->cAlternateFileName, -1,
3035 dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName), NULL, NULL );
3038 /***********************************************************************
3039 * INT21_LongFilename
3041 * Handler for function 0x71.
3043 static void INT21_LongFilename( CONTEXT86 *context )
3045 BOOL bSetDOSExtendedError = FALSE;
3046 WCHAR pathW[MAX_PATH];
3047 char* pathA;
3049 if (HIBYTE(HIWORD(GetVersion16())) < 0x07)
3051 TRACE( "LONG FILENAME - functions supported only under DOS7\n" );
3052 SET_CFLAG( context );
3053 SET_AL( context, 0 );
3054 return;
3057 switch (AL_reg(context))
3059 case 0x0d: /* RESET DRIVE */
3060 INT_BARF( context, 0x21 );
3061 break;
3063 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
3064 if (!INT21_CreateDirectory( context ))
3065 bSetDOSExtendedError = TRUE;
3066 break;
3068 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
3069 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3071 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA);
3072 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3073 if (!RemoveDirectoryW( pathW )) bSetDOSExtendedError = TRUE;
3074 break;
3076 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
3077 if (!INT21_SetCurrentDirectory( context ))
3078 bSetDOSExtendedError = TRUE;
3079 break;
3081 case 0x41: /* LONG FILENAME - DELETE FILE */
3082 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3084 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA );
3085 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3087 if (!DeleteFileW( pathW )) bSetDOSExtendedError = TRUE;
3088 break;
3090 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
3091 if (!INT21_FileAttributes( context, BL_reg(context), TRUE ))
3092 bSetDOSExtendedError = TRUE;
3093 break;
3095 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
3096 if (!INT21_GetCurrentDirectory( context, TRUE ))
3097 bSetDOSExtendedError = TRUE;
3098 break;
3100 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
3102 HANDLE handle;
3103 HGLOBAL16 h16;
3104 WIN32_FIND_DATAW dataW;
3105 WIN32_FIND_DATAA* dataA;
3107 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
3108 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA);
3110 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3111 handle = FindFirstFileW(pathW, &dataW);
3113 dataA = (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
3114 context->Edi);
3115 if (handle != INVALID_HANDLE_VALUE &&
3116 (h16 = GlobalAlloc16(GMEM_MOVEABLE, sizeof(handle))))
3118 HANDLE* ptr = GlobalLock16( h16 );
3119 *ptr = handle;
3120 GlobalUnlock16( h16 );
3121 SET_AX( context, h16 );
3122 INT21_ConvertFindDataWtoA(dataA, &dataW);
3124 else
3126 if (handle != INVALID_HANDLE_VALUE) FindClose(handle);
3127 SET_AX( context, INVALID_HANDLE_VALUE16);
3128 bSetDOSExtendedError = TRUE;
3131 break;
3133 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
3135 HGLOBAL16 h16 = BX_reg(context);
3136 HANDLE* ptr;
3137 WIN32_FIND_DATAW dataW;
3138 WIN32_FIND_DATAA* dataA;
3140 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
3141 BX_reg(context));
3143 dataA = (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
3144 context->Edi);
3146 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
3148 if (!FindNextFileW(*ptr, &dataW)) bSetDOSExtendedError = TRUE;
3149 else INT21_ConvertFindDataWtoA(dataA, &dataW);
3150 GlobalUnlock16( h16 );
3152 else
3154 SetLastError( ERROR_INVALID_HANDLE );
3155 bSetDOSExtendedError = TRUE;
3158 break;
3160 case 0x56: /* LONG FILENAME - RENAME FILE */
3161 if (!INT21_RenameFile(context))
3162 bSetDOSExtendedError = TRUE;
3163 break;
3165 case 0x60: /* LONG FILENAME - CONVERT PATH */
3167 WCHAR res[MAX_PATH];
3169 switch (CL_reg(context))
3171 case 0x00: /* "truename" - Canonicalize path */
3173 * FIXME: This is not 100% equal to 0x01 case,
3174 * if you fix this, fix int21 subfunction 0x60, too.
3177 case 0x01: /* Get short filename or path */
3178 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
3179 if (!GetShortPathNameW(pathW, res, 67))
3180 bSetDOSExtendedError = TRUE;
3181 else
3183 SET_AX( context, 0 );
3184 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
3185 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3186 67, NULL, NULL);
3188 break;
3190 case 0x02: /* Get canonical long filename or path */
3191 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
3192 if (!GetFullPathNameW(pathW, 128, res, NULL))
3193 bSetDOSExtendedError = TRUE;
3194 else
3196 SET_AX( context, 0 );
3197 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
3198 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3199 128, NULL, NULL);
3201 break;
3202 default:
3203 FIXME("Unimplemented long file name function:\n");
3204 INT_BARF( context, 0x21 );
3205 SET_CFLAG(context);
3206 SET_AL( context, 0 );
3207 break;
3210 break;
3212 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
3213 if (!INT21_CreateFile( context, context->Esi, TRUE,
3214 BX_reg(context), DL_reg(context) ))
3215 bSetDOSExtendedError = TRUE;
3216 break;
3218 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
3220 DWORD filename_len, flags;
3221 WCHAR dstW[8];
3223 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
3225 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA);
3226 SET_AX( context, 0 );
3227 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3228 if (!GetVolumeInformationW( pathW, NULL, 0, NULL, &filename_len,
3229 &flags, dstW, 8 ))
3231 INT_BARF( context, 0x21 );
3232 SET_CFLAG(context);
3233 break;
3235 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
3236 SET_CX( context, filename_len );
3237 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
3238 WideCharToMultiByte(CP_OEMCP, 0, dstW, -1,
3239 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3240 8, NULL, NULL);
3242 break;
3244 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
3246 HGLOBAL16 h16 = BX_reg(context);
3247 HANDLE* ptr;
3249 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
3250 BX_reg(context));
3251 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
3253 if (!FindClose( *ptr )) bSetDOSExtendedError = TRUE;
3254 GlobalUnlock16( h16 );
3255 GlobalFree16( h16 );
3257 else
3259 SetLastError( ERROR_INVALID_HANDLE );
3260 bSetDOSExtendedError = TRUE;
3263 break;
3265 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
3267 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
3268 BY_HANDLE_FILE_INFORMATION *info =
3269 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3271 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
3273 if (!GetFileInformationByHandle(handle, info))
3274 bSetDOSExtendedError = TRUE;
3276 break;
3278 case 0xa7: /* LONG FILENAME - CONVERT TIME */
3279 switch (BL_reg(context))
3281 case 0x00: /* FILE TIME TO DOS TIME */
3283 WORD date, time;
3284 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3285 context->SegDs,
3286 context->Esi);
3288 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
3290 FileTimeToDosDateTime( filetime, &date, &time );
3292 SET_DX( context, date );
3293 SET_CX( context, time );
3296 * FIXME: BH has number of 10-millisecond units
3297 * past time in CX.
3299 SET_BH( context, 0 );
3301 break;
3303 case 0x01: /* DOS TIME TO FILE TIME */
3305 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3306 context->SegEs,
3307 context->Edi);
3309 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
3312 * FIXME: BH has number of 10-millisecond units
3313 * past time in CX.
3315 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
3316 filetime );
3318 break;
3320 default:
3321 INT_BARF( context, 0x21 );
3322 break;
3324 break;
3326 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
3327 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3328 case 0xaa: /* LONG FILENAME - SUBST */
3329 default:
3330 FIXME("Unimplemented long file name function:\n");
3331 INT_BARF( context, 0x21 );
3332 SET_CFLAG(context);
3333 SET_AL( context, 0 );
3334 break;
3337 if (bSetDOSExtendedError)
3339 SET_AX( context, GetLastError() );
3340 SET_CFLAG( context );
3345 /***********************************************************************
3346 * INT21_RenameFile
3348 * Handler for:
3349 * - function 0x56
3350 * - subfunction 0x56 of function 0x71
3351 * - subfunction 0xff of function 0x43 (CL == 0x56)
3353 static BOOL INT21_RenameFile( CONTEXT86 *context )
3355 WCHAR fromW[MAX_PATH];
3356 WCHAR toW[MAX_PATH];
3357 char *fromA = CTX_SEG_OFF_TO_LIN(context,
3358 context->SegDs,context->Edx);
3359 char *toA = CTX_SEG_OFF_TO_LIN(context,
3360 context->SegEs,context->Edi);
3362 TRACE( "RENAME FILE %s to %s\n", fromA, toA );
3363 MultiByteToWideChar(CP_OEMCP, 0, fromA, -1, fromW, MAX_PATH);
3364 MultiByteToWideChar(CP_OEMCP, 0, toA, -1, toW, MAX_PATH);
3366 return MoveFileW( fromW, toW );
3370 /***********************************************************************
3371 * INT21_NetworkFunc
3373 * Handler for:
3374 * - function 0x5e
3376 static BOOL INT21_NetworkFunc (CONTEXT86 *context)
3378 switch (AL_reg(context))
3380 case 0x00: /* Get machine name. */
3382 WCHAR dstW[MAX_COMPUTERNAME_LENGTH + 1];
3383 DWORD s = sizeof(dstW) / sizeof(WCHAR);
3384 int len;
3386 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
3387 TRACE("getting machine name to %p\n", dst);
3388 if (!GetComputerNameW(dstW, &s) ||
3389 !WideCharToMultiByte(CP_OEMCP, 0, dstW, -1, dst, 16, NULL, NULL))
3391 WARN("failed!\n");
3392 SetLastError( ER_NoNetwork );
3393 return TRUE;
3395 for (len = strlen(dst); len < 15; len++) dst[len] = ' ';
3396 dst[15] = 0;
3397 SET_CH( context, 1 ); /* Valid */
3398 SET_CL( context, 1 ); /* NETbios number??? */
3399 TRACE("returning %s\n", debugstr_an(dst, 16));
3400 return FALSE;
3403 default:
3404 SetLastError( ER_NoNetwork );
3405 return TRUE;
3409 /******************************************************************
3410 * INT21_GetDiskSerialNumber
3413 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
3415 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3416 WCHAR path[] = {'A',':',0}, label[11];
3417 DWORD serial;
3419 path[0] += INT21_MapDrive(BL_reg(context));
3420 if (!GetVolumeInformationW( path, label, 11, &serial, NULL, NULL, NULL, 0))
3422 SetLastError( ERROR_INVALID_DRIVE );
3423 return 0;
3426 *(WORD *)dataptr = 0;
3427 memcpy(dataptr + 2, &serial, sizeof(DWORD));
3428 WideCharToMultiByte(CP_OEMCP, 0, label, 11, dataptr + 6, 11, NULL, NULL);
3429 strncpy(dataptr + 17, "FAT16 ", 8);
3430 return 1;
3434 /******************************************************************
3435 * INT21_SetDiskSerialNumber
3438 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
3440 #if 0
3441 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3442 int drive = INT21_MapDrive(BL_reg(context));
3444 if (!is_valid_drive(drive))
3446 SetLastError( ERROR_INVALID_DRIVE );
3447 return 0;
3450 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
3451 return 1;
3452 #else
3453 FIXME("Setting drive serial number is no longer supported\n");
3454 SetLastError( ERROR_NOT_SUPPORTED );
3455 return 0;
3456 #endif
3460 /******************************************************************
3461 * INT21_GetFreeDiskSpace
3464 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
3466 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
3467 WCHAR root[] = {'A',':','\\',0};
3469 root[0] += INT21_MapDrive(DL_reg(context));
3470 if (!GetDiskFreeSpaceW( root, &cluster_sectors, &sector_bytes,
3471 &free_clusters, &total_clusters )) return 0;
3472 /* make sure that the number of clusters fits in a 16 bits value */
3473 while( total_clusters & 0xffff0000) {
3474 cluster_sectors <<= 1;
3475 free_clusters >>= 1;
3476 total_clusters >>= 1;
3478 SET_AX( context, cluster_sectors );
3479 SET_BX( context, free_clusters );
3480 SET_CX( context, sector_bytes );
3481 SET_DX( context, total_clusters );
3482 return 1;
3485 /******************************************************************
3486 * INT21_GetDriveAllocInfo
3489 static int INT21_GetDriveAllocInfo( CONTEXT86 *context, BYTE drive )
3491 INT21_DPB *dpb;
3493 drive = INT21_MapDrive( drive );
3494 if (!INT21_FillDrivePB( drive )) return 0;
3495 dpb = &(INT21_GetHeapPointer()->misc_dpb_list[drive]);
3496 SET_AL( context, dpb->cluster_sectors + 1 );
3497 SET_CX( context, dpb->sector_bytes );
3498 SET_DX( context, dpb->num_clusters1 );
3500 context->SegDs = INT21_GetHeapSelector( context );
3501 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive].media_ID ) );
3502 return 1;
3505 /***********************************************************************
3506 * INT21_GetExtendedError
3508 static void INT21_GetExtendedError( CONTEXT86 *context )
3510 BYTE class, action, locus;
3511 WORD error = GetLastError();
3513 switch(error)
3515 case ERROR_SUCCESS:
3516 class = action = locus = 0;
3517 break;
3518 case ERROR_DIR_NOT_EMPTY:
3519 class = EC_Exists;
3520 action = SA_Ignore;
3521 locus = EL_Disk;
3522 break;
3523 case ERROR_ACCESS_DENIED:
3524 class = EC_AccessDenied;
3525 action = SA_Abort;
3526 locus = EL_Disk;
3527 break;
3528 case ERROR_CANNOT_MAKE:
3529 class = EC_AccessDenied;
3530 action = SA_Abort;
3531 locus = EL_Unknown;
3532 break;
3533 case ERROR_DISK_FULL:
3534 case ERROR_HANDLE_DISK_FULL:
3535 class = EC_MediaError;
3536 action = SA_Abort;
3537 locus = EL_Disk;
3538 break;
3539 case ERROR_FILE_EXISTS:
3540 case ERROR_ALREADY_EXISTS:
3541 class = EC_Exists;
3542 action = SA_Abort;
3543 locus = EL_Disk;
3544 break;
3545 case ERROR_FILE_NOT_FOUND:
3546 class = EC_NotFound;
3547 action = SA_Abort;
3548 locus = EL_Disk;
3549 break;
3550 case ERROR_GEN_FAILURE:
3551 class = EC_SystemFailure;
3552 action = SA_Abort;
3553 locus = EL_Unknown;
3554 break;
3555 case ERROR_INVALID_DRIVE:
3556 class = EC_MediaError;
3557 action = SA_Abort;
3558 locus = EL_Disk;
3559 break;
3560 case ERROR_INVALID_HANDLE:
3561 class = EC_ProgramError;
3562 action = SA_Abort;
3563 locus = EL_Disk;
3564 break;
3565 case ERROR_LOCK_VIOLATION:
3566 class = EC_AccessDenied;
3567 action = SA_Abort;
3568 locus = EL_Disk;
3569 break;
3570 case ERROR_NO_MORE_FILES:
3571 class = EC_MediaError;
3572 action = SA_Abort;
3573 locus = EL_Disk;
3574 break;
3575 case ER_NoNetwork:
3576 class = EC_NotFound;
3577 action = SA_Abort;
3578 locus = EL_Network;
3579 break;
3580 case ERROR_NOT_ENOUGH_MEMORY:
3581 class = EC_OutOfResource;
3582 action = SA_Abort;
3583 locus = EL_Memory;
3584 break;
3585 case ERROR_PATH_NOT_FOUND:
3586 class = EC_NotFound;
3587 action = SA_Abort;
3588 locus = EL_Disk;
3589 break;
3590 case ERROR_SEEK:
3591 class = EC_NotFound;
3592 action = SA_Ignore;
3593 locus = EL_Disk;
3594 break;
3595 case ERROR_SHARING_VIOLATION:
3596 class = EC_Temporary;
3597 action = SA_Retry;
3598 locus = EL_Disk;
3599 break;
3600 case ERROR_TOO_MANY_OPEN_FILES:
3601 class = EC_ProgramError;
3602 action = SA_Abort;
3603 locus = EL_Disk;
3604 break;
3605 default:
3606 FIXME("Unknown error %d\n", error );
3607 class = EC_SystemFailure;
3608 action = SA_Abort;
3609 locus = EL_Unknown;
3610 break;
3612 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3613 error, class, action, locus );
3614 SET_AX( context, error );
3615 SET_BH( context, class );
3616 SET_BL( context, action );
3617 SET_CH( context, locus );
3620 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
3622 static int counter = 0;
3623 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
3624 char *p = name + strlen(name);
3626 /* despite what Ralf Brown says, some programs seem to call without
3627 * ending backslash (DOS accepts that, so we accept it too) */
3628 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
3630 for (;;)
3632 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
3633 counter = (counter + 1) % 1000;
3635 SET_AX( context,
3636 Win32HandleToDosFileHandle(
3637 CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
3638 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
3639 CREATE_NEW, 0, 0 ) ) );
3640 if (AX_reg(context) != HFILE_ERROR16)
3642 TRACE("created %s\n", name );
3643 return TRUE;
3645 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
3649 /***********************************************************************
3650 * DOSFS_ToDosFCBFormat
3652 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3653 * expanding wild cards and converting to upper-case in the process.
3654 * File name can be terminated by '\0', '\\' or '/'.
3655 * Return FALSE if the name is not a valid DOS name.
3656 * 'buffer' must be at least 12 characters long.
3658 /* Chars we don't want to see in DOS file names */
3659 static BOOL INT21_ToDosFCBFormat( LPCWSTR name, LPWSTR buffer )
3661 static const WCHAR invalid_chars[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3662 LPCWSTR p = name;
3663 int i;
3665 /* Check for "." and ".." */
3666 if (*p == '.')
3668 p++;
3669 buffer[0] = '.';
3670 for(i = 1; i < 11; i++) buffer[i] = ' ';
3671 buffer[11] = 0;
3672 if (*p == '.')
3674 buffer[1] = '.';
3675 p++;
3677 return (!*p || (*p == '/') || (*p == '\\'));
3680 for (i = 0; i < 8; i++)
3682 switch(*p)
3684 case '\0':
3685 case '\\':
3686 case '/':
3687 case '.':
3688 buffer[i] = ' ';
3689 break;
3690 case '?':
3691 p++;
3692 /* fall through */
3693 case '*':
3694 buffer[i] = '?';
3695 break;
3696 default:
3697 if (strchrW( invalid_chars, *p )) return FALSE;
3698 buffer[i] = toupperW(*p);
3699 p++;
3700 break;
3704 if (*p == '*')
3706 /* Skip all chars after wildcard up to first dot */
3707 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
3709 else
3711 /* Check if name too long */
3712 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return FALSE;
3714 if (*p == '.') p++; /* Skip dot */
3716 for (i = 8; i < 11; i++)
3718 switch(*p)
3720 case '\0':
3721 case '\\':
3722 case '/':
3723 buffer[i] = ' ';
3724 break;
3725 case '.':
3726 return FALSE; /* Second extension not allowed */
3727 case '?':
3728 p++;
3729 /* fall through */
3730 case '*':
3731 buffer[i] = '?';
3732 break;
3733 default:
3734 if (strchrW( invalid_chars, *p )) return FALSE;
3735 buffer[i] = toupperW(*p);
3736 p++;
3737 break;
3740 buffer[11] = '\0';
3742 /* at most 3 character of the extension are processed
3743 * is something behind this ?
3745 while (*p == '*' || *p == ' ') p++; /* skip wildcards and spaces */
3746 return (!*p || (*p == '/') || (*p == '\\'));
3749 static HANDLE INT21_FindHandle;
3750 static const WCHAR *INT21_FindPath; /* will point to current dta->fullPath search */
3752 /******************************************************************
3753 * INT21_FindFirst
3755 static int INT21_FindFirst( CONTEXT86 *context )
3757 WCHAR *p, *q;
3758 const char *path;
3759 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3760 WCHAR maskW[12], pathW[MAX_PATH];
3761 static const WCHAR wildcardW[] = {'*','.','*',0};
3763 path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3764 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
3766 p = strrchrW( pathW, '\\');
3767 q = strrchrW( pathW, '/');
3768 if (q>p) p = q;
3769 if (!p)
3771 if (pathW[0] && pathW[1] == ':') p = pathW + 2;
3772 else p = pathW;
3774 else p++;
3776 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3777 * (doesn't matter as it is set below anyway)
3779 if (!INT21_ToDosFCBFormat( p, maskW ))
3781 SetLastError( ERROR_FILE_NOT_FOUND );
3782 SET_AX( context, ERROR_FILE_NOT_FOUND );
3783 SET_CFLAG(context);
3784 return 0;
3786 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
3788 dta->fullPath = HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW) + (p - pathW)*sizeof(WCHAR) );
3789 memcpy( dta->fullPath, pathW, (p - pathW) * sizeof(WCHAR) );
3790 memcpy( dta->fullPath + (p - pathW), wildcardW, sizeof(wildcardW) );
3791 /* we must have a fully qualified file name in dta->fullPath
3792 * (we could have a UNC path, but this would lead to some errors later on)
3794 dta->drive = toupperW(dta->fullPath[0]) - 'A';
3795 dta->count = 0;
3796 dta->search_attr = CL_reg(context);
3797 return 1;
3800 /******************************************************************
3801 * match_short
3803 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3805 static BOOL match_short(LPCWSTR shortW, LPCSTR maskA)
3807 WCHAR mask[11], file[12];
3808 int i;
3810 if (!INT21_ToDosFCBFormat( shortW, file )) return FALSE;
3811 MultiByteToWideChar(CP_OEMCP, 0, maskA, 11, mask, 11);
3812 for (i = 0; i < 11; i++)
3813 if (mask[i] != '?' && mask[i] != file[i]) return FALSE;
3814 return TRUE;
3817 static unsigned INT21_FindHelper(LPCWSTR fullPath, unsigned drive, unsigned count,
3818 LPCSTR mask, unsigned search_attr,
3819 WIN32_FIND_DATAW* entry)
3821 unsigned ncalls;
3823 if ((search_attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
3825 WCHAR path[] = {' ',':',0};
3827 if (count) return 0;
3828 path[0] = drive + 'A';
3829 if (!GetVolumeInformationW(path, entry->cAlternateFileName, 13, NULL, NULL, NULL, NULL, 0)) return 0;
3830 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftCreationTime );
3831 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftLastAccessTime );
3832 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftLastWriteTime );
3833 entry->dwFileAttributes = FA_LABEL;
3834 entry->nFileSizeHigh = entry->nFileSizeLow = 0;
3835 TRACE("returning %s as label\n", debugstr_w(entry->cAlternateFileName));
3836 return 1;
3839 if (!INT21_FindHandle || INT21_FindPath != fullPath || count == 0)
3841 if (INT21_FindHandle) FindClose(INT21_FindHandle);
3842 INT21_FindHandle = FindFirstFileW(fullPath, entry);
3843 if (INT21_FindHandle == INVALID_HANDLE_VALUE)
3845 INT21_FindHandle = 0;
3846 return 0;
3848 INT21_FindPath = fullPath;
3849 /* we need to resync search */
3850 ncalls = count;
3852 else ncalls = 1;
3854 while (ncalls-- != 0)
3856 if (!FindNextFileW(INT21_FindHandle, entry))
3858 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3859 return 0;
3862 while (count < 0xffff)
3864 count++;
3865 /* Check the file attributes, and path */
3866 if (!(entry->dwFileAttributes & ~search_attr) &&
3867 match_short(entry->cAlternateFileName[0] ? entry->cAlternateFileName : entry->cFileName,
3868 mask))
3870 return count;
3872 if (!FindNextFileW(INT21_FindHandle, entry))
3874 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3875 return 0;
3878 WARN("Too many directory entries in %s\n", debugstr_w(fullPath) );
3879 return 0;
3882 /******************************************************************
3883 * INT21_FindNext
3885 static int INT21_FindNext( CONTEXT86 *context )
3887 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3888 DWORD attr = dta->search_attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
3889 WIN32_FIND_DATAW entry;
3890 int n;
3892 if (!dta->fullPath) return 0;
3894 n = INT21_FindHelper(dta->fullPath, dta->drive, dta->count,
3895 dta->mask, attr, &entry);
3896 if (n)
3898 dta->fileattr = entry.dwFileAttributes;
3899 dta->filesize = entry.nFileSizeLow;
3900 FileTimeToDosDateTime( &entry.ftLastWriteTime, &dta->filedate, &dta->filetime );
3901 if (entry.cAlternateFileName[0])
3902 WideCharToMultiByte(CP_OEMCP, 0, entry.cAlternateFileName, -1,
3903 dta->filename, 13, NULL, NULL);
3904 else
3905 WideCharToMultiByte(CP_OEMCP, 0, entry.cFileName, -1, dta->filename, 13, NULL, NULL);
3907 if (!memchr(dta->mask,'?',11))
3909 /* wildcardless search, release resources in case no findnext will
3910 * be issued, and as a workaround in case file creation messes up
3911 * findnext, as sometimes happens with pkunzip
3913 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3914 INT21_FindPath = dta->fullPath = NULL;
3916 dta->count = n;
3917 return 1;
3919 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3920 INT21_FindPath = dta->fullPath = NULL;
3921 return 0;
3924 /* microsoft's programmers should be shot for using CP/M style int21
3925 calls in Windows for Workgroup's winfile.exe */
3927 /******************************************************************
3928 * INT21_FindFirstFCB
3931 static int INT21_FindFirstFCB( CONTEXT86 *context )
3933 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3934 FINDFILE_FCB *pFCB;
3935 int drive;
3936 WCHAR p[] = {' ',':',};
3938 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
3939 else pFCB = (FINDFILE_FCB *)fcb;
3940 drive = INT21_MapDrive( pFCB->drive );
3941 if (drive == MAX_DOS_DRIVES) return 0;
3943 p[0] = 'A' + drive;
3944 pFCB->fullPath = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
3945 if (!pFCB->fullPath) return 0;
3946 GetLongPathNameW(p, pFCB->fullPath, MAX_PATH);
3947 pFCB->count = 0;
3948 return 1;
3951 /******************************************************************
3952 * INT21_FindNextFCB
3955 static int INT21_FindNextFCB( CONTEXT86 *context )
3957 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3958 FINDFILE_FCB *pFCB;
3959 LPBYTE pResult = INT21_GetCurrentDTA(context);
3960 DOS_DIRENTRY_LAYOUT *ddl;
3961 WIN32_FIND_DATAW entry;
3962 BYTE attr;
3963 int n;
3964 WCHAR nameW[12];
3966 if (*fcb == 0xff) /* extended FCB ? */
3968 attr = fcb[6];
3969 pFCB = (FINDFILE_FCB *)(fcb + 7);
3971 else
3973 attr = 0;
3974 pFCB = (FINDFILE_FCB *)fcb;
3977 if (!pFCB->fullPath) return 0;
3978 n = INT21_FindHelper(pFCB->fullPath, INT21_MapDrive( pFCB->drive ),
3979 pFCB->count, pFCB->filename, attr, &entry);
3980 if (!n)
3982 HeapFree( GetProcessHeap(), 0, pFCB->fullPath );
3983 INT21_FindPath = pFCB->fullPath = NULL;
3984 return 0;
3986 pFCB->count += n;
3988 if (*fcb == 0xff)
3990 /* place extended FCB header before pResult if called with extended FCB */
3991 *pResult = 0xff;
3992 pResult += 6; /* leave reserved field behind */
3993 *pResult++ = entry.dwFileAttributes;
3995 *pResult++ = INT21_MapDrive( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
3996 ddl = (DOS_DIRENTRY_LAYOUT*)pResult;
3997 ddl->fileattr = entry.dwFileAttributes;
3998 ddl->cluster = 0; /* what else? */
3999 ddl->filesize = entry.nFileSizeLow;
4000 memset( ddl->reserved, 0, sizeof(ddl->reserved) );
4001 FileTimeToDosDateTime( &entry.ftLastWriteTime,
4002 &ddl->filedate, &ddl->filetime );
4004 /* Convert file name to FCB format */
4005 if (entry.cAlternateFileName[0])
4006 INT21_ToDosFCBFormat( entry.cAlternateFileName, nameW );
4007 else
4008 INT21_ToDosFCBFormat( entry.cFileName, nameW );
4009 WideCharToMultiByte(CP_OEMCP, 0, nameW, 11, ddl->filename, 11, NULL, NULL);
4010 return 1;
4014 /******************************************************************
4015 * INT21_ParseFileNameIntoFCB
4018 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
4020 char *filename =
4021 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
4022 char *fcb =
4023 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
4024 char *s;
4025 WCHAR *buffer;
4026 WCHAR fcbW[12];
4027 INT buffer_len, len;
4029 SET_AL( context, 0xff ); /* failed */
4031 TRACE("filename: '%s'\n", filename);
4033 s = filename;
4034 while (*s && (*s != ' ') && (*s != '\r') && (*s != '\n'))
4035 s++;
4036 len = filename - s;
4038 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
4039 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
4040 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
4041 buffer[len] = 0;
4042 INT21_ToDosFCBFormat(buffer, fcbW);
4043 HeapFree(GetProcessHeap(), 0, buffer);
4044 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
4045 *fcb = 0;
4046 TRACE("FCB: '%s'\n", fcb + 1);
4048 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
4050 /* point DS:SI to first unparsed character */
4051 SET_SI( context, context->Esi + (int)s - (int)filename );
4054 static BOOL INT21_Dup2(HFILE16 hFile1, HFILE16 hFile2)
4056 HFILE16 res = HFILE_ERROR16;
4057 HANDLE handle, new_handle;
4058 #define DOS_TABLE_SIZE 256
4059 DWORD map[DOS_TABLE_SIZE / 32];
4060 int i;
4062 handle = DosFileHandleToWin32Handle(hFile1);
4063 if (handle == INVALID_HANDLE_VALUE)
4064 return FALSE;
4066 _lclose16(hFile2);
4067 /* now loop to allocate the same one... */
4068 memset(map, 0, sizeof(map));
4069 for (i = 0; i < DOS_TABLE_SIZE; i++)
4071 if (!DuplicateHandle(GetCurrentProcess(), handle,
4072 GetCurrentProcess(), &new_handle,
4073 0, FALSE, DUPLICATE_SAME_ACCESS))
4075 res = HFILE_ERROR16;
4076 break;
4078 res = Win32HandleToDosFileHandle(new_handle);
4079 if (res == HFILE_ERROR16 || res == hFile2) break;
4080 map[res / 32] |= 1 << (res % 32);
4082 /* clean up the allocated slots */
4083 for (i = 0; i < DOS_TABLE_SIZE; i++)
4085 if (map[i / 32] & (1 << (i % 32)))
4086 _lclose16((HFILE16)i);
4088 return res == hFile2;
4092 /***********************************************************************
4093 * DOSVM_Int21Handler
4095 * Interrupt 0x21 handler.
4097 void WINAPI DOSVM_Int21Handler( CONTEXT86 *context )
4099 BOOL bSetDOSExtendedError = FALSE;
4101 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
4102 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
4103 AX_reg(context), BX_reg(context),
4104 CX_reg(context), DX_reg(context),
4105 SI_reg(context), DI_reg(context),
4106 (WORD)context->SegDs, (WORD)context->SegEs,
4107 context->EFlags );
4110 * Extended error is used by (at least) functions 0x2f to 0x62.
4111 * Function 0x59 returns extended error and error should not
4112 * be cleared before handling the function.
4114 if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59)
4115 SetLastError(0);
4117 RESET_CFLAG(context); /* Not sure if this is a good idea. */
4119 switch(AH_reg(context))
4121 case 0x00: /* TERMINATE PROGRAM */
4122 TRACE("TERMINATE PROGRAM\n");
4123 if (DOSVM_IsWin16())
4124 ExitThread( 0 );
4125 else if(ISV86(context))
4126 MZ_Exit( context, FALSE, 0 );
4127 else
4128 ERR( "Called from DOS protected mode\n" );
4129 break;
4131 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
4133 BYTE ascii;
4134 TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
4135 INT21_ReadChar( &ascii, context );
4136 SET_AL( context, ascii );
4138 * FIXME: What to echo when extended keycodes are read?
4140 DOSVM_PutChar(AL_reg(context));
4142 break;
4144 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
4145 TRACE("Write Character to Standard Output\n");
4146 DOSVM_PutChar(DL_reg(context));
4147 break;
4149 case 0x03: /* READ CHARACTER FROM STDAUX */
4150 case 0x04: /* WRITE CHARACTER TO STDAUX */
4151 case 0x05: /* WRITE CHARACTER TO PRINTER */
4152 INT_BARF( context, 0x21 );
4153 break;
4155 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
4156 if (DL_reg(context) == 0xff)
4158 TRACE("Direct Console Input\n");
4160 if (INT21_ReadChar( NULL, NULL ))
4162 BYTE ascii;
4163 INT21_ReadChar( &ascii, context );
4164 SET_AL( context, ascii );
4165 RESET_ZFLAG( context );
4167 else
4169 /* no character available */
4170 SET_AL( context, 0 );
4171 SET_ZFLAG( context );
4174 else
4176 TRACE("Direct Console Output\n");
4177 DOSVM_PutChar(DL_reg(context));
4179 * At least DOS versions 2.1-7.0 return character
4180 * that was written in AL register.
4182 SET_AL( context, DL_reg(context) );
4184 break;
4186 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
4188 BYTE ascii;
4189 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
4190 INT21_ReadChar( &ascii, context );
4191 SET_AL( context, ascii );
4193 break;
4195 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
4197 BYTE ascii;
4198 TRACE("CHARACTER INPUT WITHOUT ECHO\n");
4199 INT21_ReadChar( &ascii, context );
4200 SET_AL( context, ascii );
4202 break;
4204 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
4205 TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
4206 context->SegDs, DX_reg(context) );
4208 LPSTR data = CTX_SEG_OFF_TO_LIN( context,
4209 context->SegDs, context->Edx );
4210 LPSTR p = data;
4213 * Do NOT use strchr() to calculate the string length,
4214 * as '\0' is valid string content, too!
4215 * Maybe we should check for non-'$' strings, but DOS doesn't.
4217 while (*p != '$') p++;
4219 if (DOSVM_IsWin16())
4220 WriteFile( DosFileHandleToWin32Handle(1),
4221 data, p - data, 0, 0 );
4222 else
4223 for(; data != p; data++)
4224 DOSVM_PutChar( *data );
4226 SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
4228 break;
4230 case 0x0a: /* BUFFERED INPUT */
4232 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context,
4233 context->SegDs,
4234 context->Edx);
4235 WORD result;
4237 TRACE( "BUFFERED INPUT (size=%d)\n", ptr[0] );
4240 * FIXME: Some documents state that
4241 * ptr[1] holds number of chars from last input which
4242 * may be recalled on entry, other documents do not mention
4243 * this at all.
4245 if (ptr[1])
4246 TRACE( "Handle old chars in buffer!\n" );
4249 * ptr[0] - capacity (includes terminating CR)
4250 * ptr[1] - characters read (excludes terminating CR)
4252 result = INT21_BufferedInput( context, ptr + 2, ptr[0] );
4253 if (result > 0)
4254 ptr[1] = (BYTE)result - 1;
4255 else
4256 ptr[1] = 0;
4258 break;
4260 case 0x0b: /* GET STDIN STATUS */
4261 TRACE( "GET STDIN STATUS\n" );
4263 if (INT21_ReadChar( NULL, NULL ))
4264 SET_AL( context, 0xff ); /* character available */
4265 else
4266 SET_AL( context, 0 ); /* no character available */
4268 break;
4270 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
4272 BYTE al = AL_reg(context); /* Input function to execute after flush. */
4274 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
4276 /* FIXME: buffers are not flushed */
4279 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
4280 * int21 function identified by AL will be called.
4282 if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
4284 SET_AH( context, al );
4285 DOSVM_Int21Handler( context );
4288 break;
4290 case 0x0d: /* DISK BUFFER FLUSH */
4291 TRACE("DISK BUFFER FLUSH ignored\n");
4292 break;
4294 case 0x0e: /* SELECT DEFAULT DRIVE */
4295 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context) );
4296 INT21_SetCurrentDrive( DL_reg(context) );
4297 SET_AL( context, MAX_DOS_DRIVES );
4298 break;
4300 case 0x0f: /* OPEN FILE USING FCB */
4301 INT21_OpenFileUsingFCB( context );
4302 break;
4304 case 0x10: /* CLOSE FILE USING FCB */
4305 INT21_CloseFileUsingFCB( context );
4306 break;
4308 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
4309 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
4310 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
4311 if (!INT21_FindFirstFCB(context))
4313 SET_AL( context, 0xff );
4314 break;
4316 /* else fall through */
4318 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
4319 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
4320 break;
4322 case 0x13: /* DELETE FILE USING FCB */
4323 INT_BARF( context, 0x21 );
4324 break;
4326 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
4327 INT21_SequentialReadFromFCB( context );
4328 break;
4330 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
4331 INT21_SequentialWriteToFCB( context );
4332 break;
4334 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
4335 case 0x17: /* RENAME FILE USING FCB */
4336 INT_BARF( context, 0x21 );
4337 break;
4339 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4340 SET_AL( context, 0 );
4341 break;
4343 case 0x19: /* GET CURRENT DEFAULT DRIVE */
4344 SET_AL( context, INT21_GetCurrentDrive() );
4345 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context ) );
4346 break;
4348 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
4349 TRACE( "SET DISK TRANSFER AREA ADDRESS %04lX:%04X\n",
4350 context->SegDs, DX_reg(context) );
4352 TDB *task = GlobalLock16( GetCurrentTask() );
4353 task->dta = MAKESEGPTR( context->SegDs, DX_reg(context) );
4355 break;
4357 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4358 if (!INT21_GetDriveAllocInfo(context, 0))
4359 SET_AX( context, 0xffff );
4360 break;
4362 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4363 if (!INT21_GetDriveAllocInfo(context, DL_reg(context)))
4364 SET_AX( context, 0xffff );
4365 break;
4367 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4368 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4369 SET_AL( context, 0 );
4370 break;
4372 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4374 BYTE drive = INT21_MapDrive( 0 );
4375 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4377 if (INT21_FillDrivePB( drive ))
4379 SET_AL( context, 0x00 ); /* success */
4380 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4381 context->SegDs = INT21_GetHeapSelector( context );
4383 else
4385 SET_AL( context, 0xff ); /* invalid or network drive */
4388 break;
4390 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4391 SET_AL( context, 0 );
4392 break;
4394 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4395 INT21_ReadRandomRecordFromFCB( context );
4396 break;
4398 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4399 INT21_WriteRandomRecordToFCB( context );
4400 break;
4402 case 0x23: /* GET FILE SIZE FOR FCB */
4403 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4404 INT_BARF( context, 0x21 );
4405 break;
4407 case 0x25: /* SET INTERRUPT VECTOR */
4408 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4410 FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
4411 if (!ISV86(context) && DOSVM_IsWin16())
4412 DOSVM_SetPMHandler16( AL_reg(context), ptr );
4413 else
4414 DOSVM_SetRMHandler( AL_reg(context), ptr );
4416 break;
4418 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4419 INT_BARF( context, 0x21 );
4420 break;
4422 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4423 INT21_RandomBlockReadFromFCB( context );
4424 break;
4426 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4427 INT21_RandomBlockWriteToFCB( context );
4428 break;
4430 case 0x29: /* PARSE FILENAME INTO FCB */
4431 INT21_ParseFileNameIntoFCB(context);
4432 break;
4434 case 0x2a: /* GET SYSTEM DATE */
4435 TRACE( "GET SYSTEM DATE\n" );
4437 SYSTEMTIME systime;
4438 GetLocalTime( &systime );
4439 SET_CX( context, systime.wYear );
4440 SET_DH( context, systime.wMonth );
4441 SET_DL( context, systime.wDay );
4442 SET_AL( context, systime.wDayOfWeek );
4444 break;
4446 case 0x2b: /* SET SYSTEM DATE */
4447 TRACE( "SET SYSTEM DATE\n" );
4449 WORD year = CX_reg(context);
4450 BYTE month = DH_reg(context);
4451 BYTE day = DL_reg(context);
4453 if (year >= 1980 && year <= 2099 &&
4454 month >= 1 && month <= 12 &&
4455 day >= 1 && day <= 31)
4457 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4458 day, month, year );
4459 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4461 else
4463 SET_AL( context, 0xff ); /* invalid date */
4464 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4465 day, month, year );
4468 break;
4470 case 0x2c: /* GET SYSTEM TIME */
4471 TRACE( "GET SYSTEM TIME\n" );
4473 SYSTEMTIME systime;
4474 GetLocalTime( &systime );
4475 SET_CH( context, systime.wHour );
4476 SET_CL( context, systime.wMinute );
4477 SET_DH( context, systime.wSecond );
4478 SET_DL( context, systime.wMilliseconds / 10 );
4480 break;
4482 case 0x2d: /* SET SYSTEM TIME */
4483 if( CH_reg(context) >= 24 || CL_reg(context) >= 60 || DH_reg(context) >= 60 || DL_reg(context) >= 100 ) {
4484 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4485 CH_reg(context), CL_reg(context),
4486 DH_reg(context), DL_reg(context) );
4487 SET_AL( context, 0xFF );
4489 else
4491 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4492 CH_reg(context), CL_reg(context),
4493 DH_reg(context), DL_reg(context) );
4494 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4496 break;
4498 case 0x2e: /* SET VERIFY FLAG */
4499 TRACE("SET VERIFY FLAG ignored\n");
4500 /* we cannot change the behaviour anyway, so just ignore it */
4501 break;
4503 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4504 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4506 TDB *task = GlobalLock16( GetCurrentTask() );
4507 context->SegEs = SELECTOROF( task->dta );
4508 SET_BX( context, OFFSETOF( task->dta ) );
4510 break;
4512 case 0x30: /* GET DOS VERSION */
4513 TRACE( "GET DOS VERSION - %s requested\n",
4514 (AL_reg(context) == 0x00) ? "OEM number" : "version flag" );
4516 if (AL_reg(context) == 0x00)
4517 SET_BH( context, 0xff ); /* OEM number => undefined */
4518 else
4519 SET_BH( context, 0x08 ); /* version flag => DOS is in ROM */
4521 SET_AL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4522 SET_AH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4524 SET_BL( context, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4525 SET_CX( context, 0x3456 );
4526 break;
4528 case 0x31: /* TERMINATE AND STAY RESIDENT */
4529 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4530 break;
4532 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4534 BYTE drive = INT21_MapDrive( DL_reg(context) );
4535 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context) );
4537 if (INT21_FillDrivePB( drive ))
4539 SET_AL( context, 0x00 ); /* success */
4540 SET_DX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4541 context->SegDs = INT21_GetHeapSelector( context );
4543 else
4545 SET_AL( context, 0xff ); /* invalid or network drive */
4548 break;
4550 case 0x33: /* MULTIPLEXED */
4551 switch (AL_reg(context))
4553 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4554 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4555 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4556 break;
4558 case 0x01: /* SET EXTENDED BREAK STATE */
4559 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4560 DOSCONF_GetConfig()->brk_flag = (DL_reg(context) > 0) ? 1 : 0;
4561 break;
4563 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4564 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4565 /* ugly coding in order to stay reentrant */
4566 if (DL_reg(context))
4568 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4569 DOSCONF_GetConfig()->brk_flag = 1;
4571 else
4573 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4574 DOSCONF_GetConfig()->brk_flag = 0;
4576 break;
4578 case 0x05: /* GET BOOT DRIVE */
4579 TRACE("GET BOOT DRIVE\n");
4580 SET_DL( context, 3 );
4581 /* c: is Wine's bootdrive (a: is 1)*/
4582 break;
4584 case 0x06: /* GET TRUE VERSION NUMBER */
4585 TRACE("GET TRUE VERSION NUMBER\n");
4586 SET_BL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4587 SET_BH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4588 SET_DL( context, 0x00 ); /* revision */
4589 SET_DH( context, 0x08 ); /* DOS is in ROM */
4590 break;
4592 default:
4593 INT_BARF( context, 0x21 );
4594 break;
4596 break;
4598 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4599 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4600 context->SegEs = INT21_GetHeapSelector( context );
4601 SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
4602 break;
4604 case 0x35: /* GET INTERRUPT VECTOR */
4605 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4607 FARPROC16 addr;
4608 if (!ISV86(context) && DOSVM_IsWin16())
4609 addr = DOSVM_GetPMHandler16( AL_reg(context) );
4610 else
4611 addr = DOSVM_GetRMHandler( AL_reg(context) );
4612 context->SegEs = SELECTOROF(addr);
4613 SET_BX( context, OFFSETOF(addr) );
4615 break;
4617 case 0x36: /* GET FREE DISK SPACE */
4618 TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
4619 INT21_DriveName( DL_reg(context) ));
4620 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
4621 break;
4623 case 0x37: /* SWITCHAR */
4625 switch (AL_reg(context))
4627 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4628 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4629 SET_AL( context, 0x00 ); /* success*/
4630 SET_DL( context, '/' );
4631 break;
4632 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4633 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4634 DL_reg( context ));
4635 SET_AL( context, 0x00 ); /* success*/
4636 break;
4637 default:
4638 INT_BARF( context, 0x21 );
4639 break;
4642 break;
4644 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4645 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4646 if (AL_reg(context))
4648 WORD country = AL_reg(context);
4649 if (country == 0xff)
4650 country = BX_reg(context);
4651 if (country != INT21_GetSystemCountryCode()) {
4652 FIXME( "Requested info on non-default country %04x\n", country );
4653 SET_AX(context, 2);
4654 SET_CFLAG(context);
4657 if(AX_reg(context) != 2 )
4659 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context,
4660 context->SegDs,
4661 context->Edx) );
4662 SET_AX( context, INT21_GetSystemCountryCode() );
4663 SET_BX( context, INT21_GetSystemCountryCode() );
4664 RESET_CFLAG(context);
4666 break;
4668 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4669 if (!INT21_CreateDirectory( context ))
4670 bSetDOSExtendedError = TRUE;
4671 else
4672 RESET_CFLAG(context);
4673 break;
4675 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4677 WCHAR dirW[MAX_PATH];
4678 char *dirA = CTX_SEG_OFF_TO_LIN(context,
4679 context->SegDs, context->Edx);
4681 TRACE( "REMOVE DIRECTORY %s\n", dirA );
4683 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
4685 if (!RemoveDirectoryW( dirW ))
4686 bSetDOSExtendedError = TRUE;
4687 else
4688 RESET_CFLAG(context);
4690 break;
4692 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4693 if (!INT21_SetCurrentDirectory( context ))
4694 bSetDOSExtendedError = TRUE;
4695 else
4696 RESET_CFLAG(context);
4697 break;
4699 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4700 if (!INT21_CreateFile( context, context->Edx, FALSE,
4701 OF_READWRITE | OF_SHARE_COMPAT, 0x12 ))
4702 bSetDOSExtendedError = TRUE;
4703 else
4704 RESET_CFLAG(context);
4705 break;
4707 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4708 if (!INT21_CreateFile( context, context->Edx, FALSE,
4709 AL_reg(context), 0x01 ))
4710 bSetDOSExtendedError = TRUE;
4711 else
4712 RESET_CFLAG(context);
4713 break;
4715 case 0x3e: /* "CLOSE" - CLOSE FILE */
4716 TRACE( "CLOSE handle %d\n", BX_reg(context) );
4717 if (_lclose16( BX_reg(context) ) == HFILE_ERROR16)
4718 bSetDOSExtendedError = TRUE;
4719 else
4720 RESET_CFLAG(context);
4721 break;
4723 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4724 TRACE( "READ from %d to %04lX:%04X for %d bytes\n",
4725 BX_reg(context),
4726 context->SegDs,
4727 DX_reg(context),
4728 CX_reg(context) );
4730 DWORD result;
4731 WORD count = CX_reg(context);
4732 BYTE *buffer = CTX_SEG_OFF_TO_LIN( context,
4733 context->SegDs,
4734 context->Edx );
4736 /* Some programs pass a count larger than the allocated buffer */
4737 if (DOSVM_IsWin16())
4739 WORD maxcount = GetSelectorLimit16( context->SegDs )
4740 - DX_reg(context) + 1;
4741 if (count > maxcount)
4742 count = maxcount;
4746 * FIXME: Reading from console (BX=1) in DOS mode
4747 * does not work as it is supposed to work.
4750 RESET_CFLAG(context); /* set if error */
4751 if (!DOSVM_IsWin16() && BX_reg(context) == 0)
4753 result = INT21_BufferedInput( context, buffer, count );
4754 SET_AX( context, (WORD)result );
4756 else if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context)),
4757 buffer, count, &result, NULL ))
4758 SET_AX( context, (WORD)result );
4759 else
4760 bSetDOSExtendedError = TRUE;
4762 break;
4764 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4765 TRACE( "WRITE from %04lX:%04X to handle %d for %d byte\n",
4766 context->SegDs, DX_reg(context),
4767 BX_reg(context), CX_reg(context) );
4769 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4771 if (!DOSVM_IsWin16() &&
4772 (BX_reg(context) == 1 || BX_reg(context) == 2))
4774 int i;
4775 for(i=0; i<CX_reg(context); i++)
4776 DOSVM_PutChar(ptr[i]);
4777 SET_AX(context, CX_reg(context));
4778 RESET_CFLAG(context);
4780 else
4782 HFILE handle = (HFILE)DosFileHandleToWin32Handle(BX_reg(context));
4783 LONG result = _hwrite( handle, ptr, CX_reg(context) );
4784 if (result == HFILE_ERROR)
4785 bSetDOSExtendedError = TRUE;
4786 else
4788 SET_AX( context, (WORD)result );
4789 RESET_CFLAG(context);
4793 break;
4795 case 0x41: /* "UNLINK" - DELETE FILE */
4797 WCHAR fileW[MAX_PATH];
4798 char *fileA = CTX_SEG_OFF_TO_LIN(context,
4799 context->SegDs,
4800 context->Edx);
4802 TRACE( "UNLINK %s\n", fileA );
4803 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
4805 if (!DeleteFileW( fileW ))
4806 bSetDOSExtendedError = TRUE;
4807 else
4808 RESET_CFLAG(context);
4810 break;
4812 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4813 TRACE( "LSEEK handle %d offset %ld from %s\n",
4814 BX_reg(context),
4815 MAKELONG( DX_reg(context), CX_reg(context) ),
4816 (AL_reg(context) == 0) ?
4817 "start of file" : ((AL_reg(context) == 1) ?
4818 "current file position" : "end of file") );
4820 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4821 LONG offset = MAKELONG( DX_reg(context), CX_reg(context) );
4822 DWORD status = SetFilePointer( handle, offset,
4823 NULL, AL_reg(context) );
4824 if (status == INVALID_SET_FILE_POINTER)
4825 bSetDOSExtendedError = TRUE;
4826 else
4828 SET_AX( context, LOWORD(status) );
4829 SET_DX( context, HIWORD(status) );
4830 RESET_CFLAG(context);
4833 break;
4835 case 0x43: /* FILE ATTRIBUTES */
4836 if (!INT21_FileAttributes( context, AL_reg(context), FALSE ))
4837 bSetDOSExtendedError = TRUE;
4838 else
4839 RESET_CFLAG(context);
4840 break;
4842 case 0x44: /* IOCTL */
4843 INT21_Ioctl( context );
4844 break;
4846 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4847 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context) );
4849 HANDLE handle32;
4850 HFILE handle16 = HFILE_ERROR;
4852 if (DuplicateHandle( GetCurrentProcess(),
4853 DosFileHandleToWin32Handle(BX_reg(context)),
4854 GetCurrentProcess(),
4855 &handle32,
4856 0, TRUE, DUPLICATE_SAME_ACCESS ))
4857 handle16 = Win32HandleToDosFileHandle(handle32);
4859 if (handle16 == HFILE_ERROR)
4860 bSetDOSExtendedError = TRUE;
4861 else
4863 SET_AX( context, handle16 );
4864 RESET_CFLAG(context);
4867 break;
4869 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4870 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4871 BX_reg(context), CX_reg(context) );
4872 if (!INT21_Dup2(BX_reg(context), CX_reg(context)))
4873 bSetDOSExtendedError = TRUE;
4874 else
4875 RESET_CFLAG(context);
4876 break;
4878 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4879 if (!INT21_GetCurrentDirectory( context, FALSE ))
4880 bSetDOSExtendedError = TRUE;
4881 else
4882 RESET_CFLAG(context);
4883 break;
4885 case 0x48: /* ALLOCATE MEMORY */
4886 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
4888 WORD selector = 0;
4889 DWORD bytes = (DWORD)BX_reg(context) << 4;
4891 if (!ISV86(context) && DOSVM_IsWin16())
4893 DWORD rv = GlobalDOSAlloc16( bytes );
4894 selector = LOWORD( rv );
4896 else
4897 DOSMEM_GetBlock( bytes, &selector );
4899 if (selector)
4901 SET_AX( context, selector );
4902 RESET_CFLAG(context);
4904 else
4906 SET_CFLAG(context);
4907 SET_AX( context, 0x0008 ); /* insufficient memory */
4908 SET_BX( context, DOSMEM_Available() >> 4 );
4911 break;
4913 case 0x49: /* FREE MEMORY */
4914 TRACE( "FREE MEMORY segment %04lX\n", context->SegEs );
4916 BOOL ok;
4918 if (!ISV86(context) && DOSVM_IsWin16())
4920 ok = !GlobalDOSFree16( context->SegEs );
4922 /* If we don't reset ES_reg, we will fail in the relay code */
4923 if (ok)
4924 context->SegEs = 0;
4926 else
4927 ok = DOSMEM_FreeBlock( (void*)((DWORD)context->SegEs << 4) );
4929 if (!ok)
4931 TRACE("FREE MEMORY failed\n");
4932 SET_CFLAG(context);
4933 SET_AX( context, 0x0009 ); /* memory block address invalid */
4936 break;
4938 case 0x4a: /* RESIZE MEMORY BLOCK */
4939 TRACE( "RESIZE MEMORY segment %04lX to %d paragraphs\n",
4940 context->SegEs, BX_reg(context) );
4942 DWORD newsize = (DWORD)BX_reg(context) << 4;
4944 if (!ISV86(context) && DOSVM_IsWin16())
4946 FIXME( "Resize memory block - unsupported under Win16\n" );
4947 SET_CFLAG(context);
4949 else
4951 LPVOID address = (void*)((DWORD)context->SegEs << 4);
4952 UINT blocksize = DOSMEM_ResizeBlock( address, newsize, FALSE );
4954 RESET_CFLAG(context);
4955 if (blocksize == (UINT)-1)
4957 SET_CFLAG( context );
4958 SET_AX( context, 0x0009 ); /* illegal address */
4960 else if(blocksize != newsize)
4962 SET_CFLAG( context );
4963 SET_AX( context, 0x0008 ); /* insufficient memory */
4964 SET_BX( context, blocksize >> 4 ); /* new block size */
4968 break;
4970 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4972 BYTE *program = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4973 BYTE *paramblk = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx);
4975 TRACE( "EXEC %s\n", program );
4977 RESET_CFLAG(context);
4978 if (DOSVM_IsWin16())
4980 HINSTANCE16 instance = WinExec16( program, SW_NORMAL );
4981 if (instance < 32)
4983 SET_CFLAG( context );
4984 SET_AX( context, instance );
4987 else
4989 if (!MZ_Exec( context, program, AL_reg(context), paramblk))
4990 bSetDOSExtendedError = TRUE;
4993 break;
4995 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
4996 TRACE( "EXIT with return code %d\n", AL_reg(context) );
4997 if (DOSVM_IsWin16())
4998 ExitThread( AL_reg(context) );
4999 else if(ISV86(context))
5000 MZ_Exit( context, FALSE, AL_reg(context) );
5001 else
5004 * Exit from DPMI.
5006 DWORD rv = AL_reg(context);
5007 RaiseException( EXCEPTION_VM86_INTx, 0, 1, &rv );
5009 break;
5011 case 0x4d: /* GET RETURN CODE */
5012 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
5013 SET_AX( context, DOSVM_retval );
5014 DOSVM_retval = 0;
5015 break;
5017 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
5018 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
5019 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
5020 if (!INT21_FindFirst(context)) break;
5021 /* fall through */
5023 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
5024 TRACE("FINDNEXT\n");
5025 if (!INT21_FindNext(context))
5027 SetLastError( ERROR_NO_MORE_FILES );
5028 SET_AX( context, ERROR_NO_MORE_FILES );
5029 SET_CFLAG(context);
5031 else SET_AX( context, 0 ); /* OK */
5032 break;
5034 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
5035 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
5036 DOSVM_psp = BX_reg(context);
5037 break;
5039 case 0x51: /* GET PSP ADDRESS */
5040 INT21_GetPSP( context );
5041 break;
5043 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
5045 SEGPTR ptr = DOSDEV_GetLOL( ISV86(context) || !DOSVM_IsWin16() );
5046 context->SegEs = SELECTOROF(ptr);
5047 SET_BX( context, OFFSETOF(ptr) );
5049 break;
5051 case 0x54: /* Get Verify Flag */
5052 TRACE("Get Verify Flag - Not Supported\n");
5053 SET_AL( context, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
5054 break;
5056 case 0x56: /* "RENAME" - RENAME FILE */
5057 if (!INT21_RenameFile( context ))
5058 bSetDOSExtendedError = TRUE;
5059 else
5060 RESET_CFLAG(context);
5061 break;
5063 case 0x57: /* FILE DATE AND TIME */
5064 if (!INT21_FileDateTime( context ))
5065 bSetDOSExtendedError = TRUE;
5066 else
5067 RESET_CFLAG(context);
5068 break;
5070 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
5071 switch (AL_reg(context))
5073 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
5074 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
5075 SET_AX( context, 0 ); /* low memory first fit */
5076 break;
5078 case 0x01: /* SET ALLOCATION STRATEGY */
5079 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
5080 BL_reg(context) );
5081 break;
5083 case 0x02: /* GET UMB LINK STATE */
5084 TRACE( "GET UMB LINK STATE\n" );
5085 SET_AL( context, 0 ); /* UMBs not part of DOS memory chain */
5086 break;
5088 case 0x03: /* SET UMB LINK STATE */
5089 TRACE( "SET UMB LINK STATE to %d - ignored\n",
5090 BX_reg(context) );
5091 break;
5093 default:
5094 INT_BARF( context, 0x21 );
5096 break;
5098 case 0x59: /* GET EXTENDED ERROR INFO */
5099 INT21_GetExtendedError( context );
5100 break;
5102 case 0x5a: /* CREATE TEMPORARY FILE */
5103 TRACE("CREATE TEMPORARY FILE\n");
5104 bSetDOSExtendedError = !INT21_CreateTempFile(context);
5105 break;
5107 case 0x5b: /* CREATE NEW FILE */
5108 if (!INT21_CreateFile( context, context->Edx, FALSE,
5109 OF_READWRITE | OF_SHARE_COMPAT, 0x10 ))
5110 bSetDOSExtendedError = TRUE;
5111 else
5112 RESET_CFLAG(context);
5113 break;
5115 case 0x5c: /* "FLOCK" - RECORD LOCKING */
5117 DWORD offset = MAKELONG(DX_reg(context), CX_reg(context));
5118 DWORD length = MAKELONG(DI_reg(context), SI_reg(context));
5119 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
5121 RESET_CFLAG(context);
5122 switch (AL_reg(context))
5124 case 0x00: /* LOCK */
5125 TRACE( "lock handle %d offset %ld length %ld\n",
5126 BX_reg(context), offset, length );
5127 if (!LockFile( handle, offset, 0, length, 0 ))
5128 bSetDOSExtendedError = TRUE;
5129 break;
5131 case 0x01: /* UNLOCK */
5132 TRACE( "unlock handle %d offset %ld length %ld\n",
5133 BX_reg(context), offset, length );
5134 if (!UnlockFile( handle, offset, 0, length, 0 ))
5135 bSetDOSExtendedError = TRUE;
5136 break;
5138 default:
5139 INT_BARF( context, 0x21 );
5142 break;
5144 case 0x5d: /* NETWORK 5D */
5145 FIXME( "Network function 5D not implemented.\n" );
5146 SetLastError( ER_NoNetwork );
5147 bSetDOSExtendedError = TRUE;
5148 break;
5150 case 0x5e: /* NETWORK 5E */
5151 bSetDOSExtendedError = INT21_NetworkFunc( context);
5152 break;
5154 case 0x5f: /* NETWORK 5F */
5155 /* FIXME: supporting this would need to 1:
5156 * - implement per drive current directory (as kernel32 doesn't)
5157 * - assign enabled/disabled flag on a per drive basis
5159 /* network software not installed */
5160 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
5161 SetLastError( ER_NoNetwork );
5162 bSetDOSExtendedError = TRUE;
5163 break;
5165 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
5167 WCHAR pathW[MAX_PATH], res[MAX_PATH];
5168 /* FIXME: likely to be broken */
5170 TRACE("TRUENAME %s\n",
5171 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
5172 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
5173 if (!GetFullPathNameW( pathW, 128, res, NULL ))
5174 bSetDOSExtendedError = TRUE;
5175 else
5177 SET_AX( context, 0 );
5178 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
5179 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
5180 128, NULL, NULL);
5183 break;
5185 case 0x61: /* UNUSED */
5186 SET_AL( context, 0 );
5187 break;
5189 case 0x62: /* GET PSP ADDRESS */
5190 INT21_GetPSP( context );
5191 break;
5193 case 0x63: /* MISC. LANGUAGE SUPPORT */
5194 switch (AL_reg(context)) {
5195 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
5196 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
5197 context->SegDs = INT21_GetHeapSelector( context );
5198 SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
5199 SET_AL( context, 0 ); /* success */
5200 break;
5202 break;
5204 case 0x64: /* OS/2 DOS BOX */
5205 INT_BARF( context, 0x21 );
5206 SET_CFLAG(context);
5207 break;
5209 case 0x65: /* EXTENDED COUNTRY INFORMATION */
5210 INT21_ExtendedCountryInformation( context );
5211 break;
5213 case 0x66: /* GLOBAL CODE PAGE TABLE */
5214 switch (AL_reg(context))
5216 case 0x01:
5217 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
5218 SET_BX( context, GetOEMCP() );
5219 SET_DX( context, GetOEMCP() );
5220 break;
5221 case 0x02:
5222 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
5223 BX_reg(context), DX_reg(context) );
5224 break;
5226 break;
5228 case 0x67: /* SET HANDLE COUNT */
5229 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
5230 if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
5231 bSetDOSExtendedError = TRUE;
5232 break;
5234 case 0x68: /* "FFLUSH" - COMMIT FILE */
5235 TRACE( "FFLUSH - handle %d\n", BX_reg(context) );
5236 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
5237 bSetDOSExtendedError = TRUE;
5238 break;
5240 case 0x69: /* DISK SERIAL NUMBER */
5241 switch (AL_reg(context))
5243 case 0x00:
5244 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
5245 INT21_DriveName(BL_reg(context)));
5246 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
5247 else SET_AX( context, 0 );
5248 break;
5250 case 0x01:
5251 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
5252 INT21_DriveName(BL_reg(context)));
5253 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
5254 else SET_AX( context, 1 );
5255 break;
5257 break;
5259 case 0x6a: /* COMMIT FILE */
5260 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context) );
5261 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
5262 bSetDOSExtendedError = TRUE;
5263 break;
5265 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
5266 SET_AL( context, 0 );
5267 break;
5269 case 0x6c: /* EXTENDED OPEN/CREATE */
5270 if (!INT21_CreateFile( context, context->Esi, TRUE,
5271 BX_reg(context), DL_reg(context) ))
5272 bSetDOSExtendedError = TRUE;
5273 break;
5275 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
5276 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
5277 SET_CFLAG( context );
5278 SET_AL( context, 0 );
5279 break;
5281 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
5282 INT21_LongFilename( context );
5283 break;
5285 case 0x73: /* MSDOS7 - FAT32 */
5286 RESET_CFLAG( context );
5287 if (!INT21_Fat32( context ))
5288 bSetDOSExtendedError = TRUE;
5289 break;
5291 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
5292 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
5293 break;
5295 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
5296 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
5297 break;
5299 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
5300 /* we don't implement a DOS32 extender */
5301 TRACE( "DOS32 EXTENDER API - ignored\n" );
5302 break;
5304 default:
5305 INT_BARF( context, 0x21 );
5306 break;
5308 } /* END OF SWITCH */
5310 /* Set general error condition. */
5311 if (bSetDOSExtendedError)
5313 SET_AX( context, GetLastError() );
5314 SET_CFLAG( context );
5317 /* Print error code if carry flag is set. */
5318 if (context->EFlags & 0x0001)
5319 TRACE("failed, error %ld\n", GetLastError() );
5321 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
5322 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
5323 AX_reg(context), BX_reg(context),
5324 CX_reg(context), DX_reg(context),
5325 SI_reg(context), DI_reg(context),
5326 (WORD)context->SegDs, (WORD)context->SegEs,
5327 context->EFlags );