Fix filename terminator table, correct exclude_last value.
[wine/wine-gecko.git] / dlls / winedos / int21.c
blob3c8488709e3362ae41b7c311dd161384350580ea
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 driver
54 * directories, and use this infro 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 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 );
1881 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1882 AL_reg(context) );
1885 * Check subfunctions that are passed country and code page.
1887 if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
1889 WORD country = DX_reg(context);
1890 WORD codepage = BX_reg(context);
1892 if (country != 0xffff && country != INT21_GetSystemCountryCode())
1893 FIXME( "Requested info on non-default country %04x\n", country );
1895 if (codepage != 0xffff && codepage != GetOEMCP())
1896 FIXME( "Requested info on non-default code page %04x\n", codepage );
1899 switch (AL_reg(context)) {
1900 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1901 INT_BARF( context, 0x21 );
1902 SET_CFLAG( context );
1903 break;
1905 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1906 TRACE( "Get general internationalization info\n" );
1907 dataptr[0] = 0x01; /* Info ID */
1908 *(WORD*)(dataptr+1) = 38; /* Size of the following info */
1909 *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
1910 *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
1911 INT21_FillCountryInformation( dataptr + 7 );
1912 SET_CX( context, 41 ); /* Size of returned info */
1913 break;
1915 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1916 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1917 TRACE( "Get pointer to uppercase table\n" );
1918 dataptr[0] = AL_reg(context); /* Info ID */
1919 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1920 offsetof(INT21_HEAP, uppercase_size) );
1921 SET_CX( context, 5 ); /* Size of returned info */
1922 break;
1924 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1925 TRACE( "Get pointer to lowercase table\n" );
1926 dataptr[0] = 0x03; /* Info ID */
1927 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1928 offsetof(INT21_HEAP, lowercase_size) );
1929 SET_CX( context, 5 ); /* Size of returned info */
1930 break;
1932 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1933 TRACE("Get pointer to filename terminator table\n");
1934 dataptr[0] = 0x05; /* Info ID */
1935 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1936 offsetof(INT21_HEAP, filename_size) );
1937 SET_CX( context, 5 ); /* Size of returned info */
1938 break;
1940 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1941 TRACE("Get pointer to collating sequence table\n");
1942 dataptr[0] = 0x06; /* Info ID */
1943 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1944 offsetof(INT21_HEAP, collating_size) );
1945 SET_CX( context, 5 ); /* Size of returned info */
1946 break;
1948 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1949 TRACE("Get pointer to DBCS lead byte table\n");
1950 dataptr[0] = 0x07; /* Info ID */
1951 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1952 offsetof(INT21_HEAP, dbcs_size) );
1953 SET_CX( context, 5 ); /* Size of returned info */
1954 break;
1956 case 0x20: /* CAPITALIZE CHARACTER */
1957 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1958 TRACE("Convert char to uppercase\n");
1959 SET_DL( context, toupper(DL_reg(context)) );
1960 break;
1962 case 0x21: /* CAPITALIZE STRING */
1963 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1964 TRACE("Convert string to uppercase with length\n");
1966 char *ptr = (char *)CTX_SEG_OFF_TO_LIN( context,
1967 context->SegDs,
1968 context->Edx );
1969 WORD len = CX_reg(context);
1970 while (len--) { *ptr = toupper(*ptr); ptr++; }
1972 break;
1974 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1975 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1976 TRACE("Convert ASCIIZ string to uppercase\n");
1977 _strupr( (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx) );
1978 break;
1980 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1981 INT_BARF( context, 0x21 );
1982 SET_CFLAG( context );
1983 break;
1985 default:
1986 INT_BARF( context, 0x21 );
1987 SET_CFLAG(context);
1988 break;
1993 /***********************************************************************
1994 * INT21_FileAttributes
1996 * Handler for:
1997 * - function 0x43
1998 * - subfunction 0x43 of function 0x71
2000 static BOOL INT21_FileAttributes( CONTEXT86 *context,
2001 BYTE subfunction,
2002 BOOL islong )
2004 WCHAR fileW[MAX_PATH];
2005 char *fileA = CTX_SEG_OFF_TO_LIN(context,
2006 context->SegDs,
2007 context->Edx);
2008 HANDLE handle;
2009 BOOL status;
2010 FILETIME filetime;
2011 DWORD result;
2012 WORD date, time;
2014 switch (subfunction)
2016 case 0x00: /* GET FILE ATTRIBUTES */
2017 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA );
2018 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2020 result = GetFileAttributesW( fileW );
2021 if (result == INVALID_FILE_ATTRIBUTES)
2022 return FALSE;
2023 else
2025 SET_CX( context, (WORD)result );
2026 if (!islong)
2027 SET_AX( context, (WORD)result ); /* DR DOS */
2029 break;
2031 case 0x01: /* SET FILE ATTRIBUTES */
2032 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
2033 CX_reg(context), fileA );
2034 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2036 if (!SetFileAttributesW( fileW, CX_reg(context) ))
2037 return FALSE;
2038 break;
2040 case 0x02: /* GET COMPRESSED FILE SIZE */
2041 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA );
2042 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2044 result = GetCompressedFileSizeW( fileW, NULL );
2045 if (result == INVALID_FILE_SIZE)
2046 return FALSE;
2047 else
2049 SET_AX( context, LOWORD(result) );
2050 SET_DX( context, HIWORD(result) );
2052 break;
2054 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
2055 if (!islong)
2056 INT_BARF( context, 0x21 );
2057 else
2059 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
2060 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2062 handle = CreateFileW( fileW, GENERIC_WRITE,
2063 FILE_SHARE_READ | FILE_SHARE_WRITE,
2064 NULL, OPEN_EXISTING, 0, 0 );
2065 if (handle == INVALID_HANDLE_VALUE)
2066 return FALSE;
2068 DosDateTimeToFileTime( DI_reg(context),
2069 CX_reg(context),
2070 &filetime );
2071 status = SetFileTime( handle, NULL, NULL, &filetime );
2073 CloseHandle( handle );
2074 return status;
2076 break;
2078 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
2079 if (!islong)
2080 INT_BARF( context, 0x21 );
2081 else
2083 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
2084 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2086 handle = CreateFileW( fileW, GENERIC_READ,
2087 FILE_SHARE_READ | FILE_SHARE_WRITE,
2088 NULL, OPEN_EXISTING, 0, 0 );
2089 if (handle == INVALID_HANDLE_VALUE)
2090 return FALSE;
2092 status = GetFileTime( handle, NULL, NULL, &filetime );
2093 if (status)
2095 FileTimeToDosDateTime( &filetime, &date, &time );
2096 SET_DI( context, date );
2097 SET_CX( context, time );
2100 CloseHandle( handle );
2101 return status;
2103 break;
2105 case 0x05: /* SET FILE LAST ACCESS DATE */
2106 if (!islong)
2107 INT_BARF( context, 0x21 );
2108 else
2110 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA );
2111 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2113 handle = CreateFileW( fileW, GENERIC_WRITE,
2114 FILE_SHARE_READ | FILE_SHARE_WRITE,
2115 NULL, OPEN_EXISTING, 0, 0 );
2116 if (handle == INVALID_HANDLE_VALUE)
2117 return FALSE;
2119 DosDateTimeToFileTime( DI_reg(context),
2121 &filetime );
2122 status = SetFileTime( handle, NULL, &filetime, NULL );
2124 CloseHandle( handle );
2125 return status;
2127 break;
2129 case 0x06: /* GET FILE LAST ACCESS DATE */
2130 if (!islong)
2131 INT_BARF( context, 0x21 );
2132 else
2134 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA );
2135 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
2137 handle = CreateFileW( fileW, GENERIC_READ,
2138 FILE_SHARE_READ | FILE_SHARE_WRITE,
2139 NULL, OPEN_EXISTING, 0, 0 );
2140 if (handle == INVALID_HANDLE_VALUE)
2141 return FALSE;
2143 status = GetFileTime( handle, NULL, &filetime, NULL );
2144 if (status)
2146 FileTimeToDosDateTime( &filetime, &date, NULL );
2147 SET_DI( context, date );
2150 CloseHandle( handle );
2151 return status;
2153 break;
2155 case 0x07: /* SET FILE CREATION DATE AND TIME */
2156 if (!islong)
2157 INT_BARF( context, 0x21 );
2158 else
2160 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA );
2162 handle = CreateFileW( fileW, GENERIC_WRITE,
2163 FILE_SHARE_READ | FILE_SHARE_WRITE,
2164 NULL, OPEN_EXISTING, 0, 0 );
2165 if (handle == INVALID_HANDLE_VALUE)
2166 return FALSE;
2169 * FIXME: SI has number of 10-millisecond units past time in CX.
2171 DosDateTimeToFileTime( DI_reg(context),
2172 CX_reg(context),
2173 &filetime );
2174 status = SetFileTime( handle, &filetime, NULL, NULL );
2176 CloseHandle( handle );
2177 return status;
2179 break;
2181 case 0x08: /* GET FILE CREATION DATE AND TIME */
2182 if (!islong)
2183 INT_BARF( context, 0x21 );
2184 else
2186 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2187 BX_reg(context) );
2189 handle = CreateFileW( fileW, GENERIC_READ,
2190 FILE_SHARE_READ | FILE_SHARE_WRITE,
2191 NULL, OPEN_EXISTING, 0, 0 );
2192 if (handle == INVALID_HANDLE_VALUE)
2193 return FALSE;
2195 status = GetFileTime( handle, &filetime, NULL, NULL );
2196 if (status)
2198 FileTimeToDosDateTime( &filetime, &date, &time );
2199 SET_DI( context, date );
2200 SET_CX( context, time );
2202 * FIXME: SI has number of 10-millisecond units past
2203 * time in CX.
2205 SET_SI( context, 0 );
2208 CloseHandle(handle);
2209 return status;
2211 break;
2213 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2214 if (islong || context->Ebp != 0x5053)
2215 INT_BARF( context, 0x21 );
2216 else
2218 switch(CL_reg(context))
2220 case 0x39:
2221 if (!INT21_CreateDirectory( context ))
2222 return FALSE;
2223 break;
2225 case 0x56:
2226 if (!INT21_RenameFile( context ))
2227 return FALSE;
2228 break;
2230 default:
2231 INT_BARF( context, 0x21 );
2234 break;
2236 default:
2237 INT_BARF( context, 0x21 );
2240 return TRUE;
2244 /***********************************************************************
2245 * INT21_FileDateTime
2247 * Handler for function 0x57.
2249 static BOOL INT21_FileDateTime( CONTEXT86 *context )
2251 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2252 FILETIME filetime;
2253 WORD date, time;
2255 switch (AL_reg(context)) {
2256 case 0x00: /* Get last-written stamp */
2257 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2258 BX_reg(context) );
2260 if (!GetFileTime( handle, NULL, NULL, &filetime ))
2261 return FALSE;
2262 FileTimeToDosDateTime( &filetime, &date, &time );
2263 SET_DX( context, date );
2264 SET_CX( context, time );
2265 break;
2268 case 0x01: /* Set last-written stamp */
2269 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2270 BX_reg(context) );
2272 DosDateTimeToFileTime( DX_reg(context),
2273 CX_reg(context),
2274 &filetime );
2275 if (!SetFileTime( handle, NULL, NULL, &filetime ))
2276 return FALSE;
2277 break;
2280 case 0x04: /* Get last access stamp, DOS 7 */
2281 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2282 BX_reg(context) );
2284 if (!GetFileTime( handle, NULL, &filetime, NULL ))
2285 return FALSE;
2286 FileTimeToDosDateTime( &filetime, &date, &time );
2287 SET_DX( context, date );
2288 SET_CX( context, time );
2289 break;
2292 case 0x05: /* Set last access stamp, DOS 7 */
2293 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2294 BX_reg(context) );
2296 DosDateTimeToFileTime( DX_reg(context),
2297 CX_reg(context),
2298 &filetime );
2299 if (!SetFileTime( handle, NULL, &filetime, NULL ))
2300 return FALSE;
2301 break;
2304 case 0x06: /* Get creation stamp, DOS 7 */
2305 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2306 BX_reg(context) );
2308 if (!GetFileTime( handle, &filetime, NULL, NULL ))
2309 return FALSE;
2310 FileTimeToDosDateTime( &filetime, &date, &time );
2311 SET_DX( context, date );
2312 SET_CX( context, time );
2314 * FIXME: SI has number of 10-millisecond units past time in CX.
2316 SET_SI( context, 0 );
2317 break;
2320 case 0x07: /* Set creation stamp, DOS 7 */
2321 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2322 BX_reg(context) );
2325 * FIXME: SI has number of 10-millisecond units past time in CX.
2327 DosDateTimeToFileTime( DX_reg(context),
2328 CX_reg(context),
2329 &filetime );
2330 if (!SetFileTime( handle, &filetime, NULL, NULL ))
2331 return FALSE;
2332 break;
2335 default:
2336 INT_BARF( context, 0x21 );
2337 break;
2340 return TRUE;
2344 /***********************************************************************
2345 * INT21_GetPSP
2347 * Handler for functions 0x51 and 0x62.
2349 static void INT21_GetPSP( CONTEXT86 *context )
2351 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
2354 * FIXME: should we return the original DOS PSP upon
2355 * Windows startup ?
2357 if (!ISV86(context) && DOSVM_IsWin16())
2358 SET_BX( context, LOWORD(GetCurrentPDB16()) );
2359 else
2360 SET_BX( context, DOSVM_psp );
2363 static inline void setword( BYTE *ptr, WORD w )
2365 ptr[0] = (BYTE)w;
2366 ptr[1] = (BYTE)(w >> 8);
2369 static void CreateBPB(int drive, BYTE *data, BOOL16 limited)
2370 /* limited == TRUE is used with INT 0x21/0x440d */
2372 /* FIXME: we're forcing some values without checking that those are valid */
2373 if (drive > 1)
2375 setword(data, 512);
2376 data[2] = 2;
2377 setword(&data[3], 0);
2378 data[5] = 2;
2379 setword(&data[6], 240);
2380 setword(&data[8], 64000);
2381 data[0x0a] = 0xf8;
2382 setword(&data[0x0b], 40);
2383 setword(&data[0x0d], 56);
2384 setword(&data[0x0f], 2);
2385 setword(&data[0x11], 0);
2386 if (!limited)
2388 setword(&data[0x1f], 800);
2389 data[0x21] = 5;
2390 setword(&data[0x22], 1);
2393 else
2394 { /* 1.44mb */
2395 setword(data, 512);
2396 data[2] = 2;
2397 setword(&data[3], 0);
2398 data[5] = 2;
2399 setword(&data[6], 240);
2400 setword(&data[8], 2880);
2401 data[0x0a] = 0xf8;
2402 setword(&data[0x0b], 6);
2403 setword(&data[0x0d], 18);
2404 setword(&data[0x0f], 2);
2405 setword(&data[0x11], 0);
2406 if (!limited)
2408 setword(&data[0x1f], 80);
2409 data[0x21] = 7;
2410 setword(&data[0x22], 2);
2415 inline DWORD INT21_Ioctl_CylHeadSect2Lin(DWORD cyl, WORD head, WORD sec, WORD cyl_cnt, WORD head_cnt, WORD sec_cnt)
2417 DWORD res = (cyl * head_cnt*sec_cnt + head * sec_cnt + sec);
2418 return res;
2421 /***********************************************************************
2422 * INT21_Ioctl_Block
2424 * Handler for block device IOCTLs.
2426 static void INT21_Ioctl_Block( CONTEXT86 *context )
2428 BYTE *dataptr;
2429 BYTE drive = INT21_MapDrive( BL_reg(context) );
2430 WCHAR drivespec[4] = {'A', ':', '\\', 0};
2431 UINT drivetype;
2433 drivespec[0] += drive;
2434 drivetype = GetDriveTypeW( drivespec );
2436 RESET_CFLAG(context);
2437 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
2439 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2440 AL_reg(context), 'A' + drive );
2441 SetLastError( ERROR_INVALID_DRIVE );
2442 SET_AX( context, ERROR_INVALID_DRIVE );
2443 SET_CFLAG( context );
2444 return;
2447 switch (AL_reg(context))
2449 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2450 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2451 INT_BARF( context, 0x21 );
2452 break;
2454 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2455 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2456 'A' + drive );
2458 if (drivetype == DRIVE_REMOVABLE)
2459 SET_AX( context, 0 ); /* removable */
2460 else
2461 SET_AX( context, 1 ); /* not removable */
2462 break;
2464 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2465 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2466 'A' + drive );
2468 if (drivetype == DRIVE_REMOTE)
2469 SET_DX( context, (1<<9) | (1<<12) ); /* remote + no direct IO */
2470 else
2471 SET_DX( context, 0 ); /* FIXME: use driver attr here */
2472 break;
2474 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2475 /* Get pointer to IOCTL parameter block */
2476 dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2478 switch (CX_reg(context))
2480 case 0x0841: /* write logical device track */
2481 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2482 'A' + drive);
2484 WORD head = *(WORD *)(dataptr+1);
2485 WORD cyl = *(WORD *)(dataptr+3);
2486 WORD sect = *(WORD *)(dataptr+5);
2487 WORD nrsect = *(WORD *)(dataptr+7);
2488 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2489 WORD cyl_cnt, head_cnt, sec_cnt;
2491 /* FIXME: we're faking some values here */
2492 if (drive > 1)
2494 /* cyl_cnt = 0x300;
2495 head_cnt = 16;
2496 sec_cnt = 255; */
2497 SET_AX( context, ERROR_WRITE_FAULT );
2498 SET_CFLAG(context);
2499 break;
2501 else
2502 { /* floppy */
2503 cyl_cnt = 80;
2504 head_cnt = 2;
2505 sec_cnt = 18;
2508 if (!DOSVM_RawWrite(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2510 SET_AX( context, ERROR_WRITE_FAULT );
2511 SET_CFLAG(context);
2514 break;
2516 case 0x084a: /* lock logical volume */
2517 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2518 BH_reg(context), DX_reg(context), 'A' + drive );
2519 break;
2521 case 0x0860: /* get device parameters */
2522 /* FIXME: we're faking some values here */
2523 /* used by w4wgrp's winfile */
2524 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2525 dataptr[0] = 0x04;
2526 dataptr[6] = 0; /* media type */
2527 if (drive > 1)
2529 dataptr[1] = 0x05; /* fixed disk */
2530 setword(&dataptr[2], 0x01); /* non removable */
2531 setword(&dataptr[4], 0x300); /* # of cylinders */
2533 else
2535 dataptr[1] = 0x07; /* block dev, floppy */
2536 setword(&dataptr[2], 0x02); /* removable */
2537 setword(&dataptr[4], 80); /* # of cylinders */
2539 CreateBPB(drive, &dataptr[7], TRUE);
2540 RESET_CFLAG(context);
2541 break;
2543 case 0x0861: /* read logical device track */
2544 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2545 'A' + drive);
2547 WORD head = *(WORD *)(dataptr+1);
2548 WORD cyl = *(WORD *)(dataptr+3);
2549 WORD sect = *(WORD *)(dataptr+5);
2550 WORD nrsect = *(WORD *)(dataptr+7);
2551 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2552 WORD cyl_cnt, head_cnt, sec_cnt;
2554 /* FIXME: we're faking some values here */
2555 if (drive > 1)
2557 cyl_cnt = 0x300;
2558 head_cnt = 16;
2559 sec_cnt = 255;
2561 else
2562 { /* floppy */
2563 cyl_cnt = 80;
2564 head_cnt = 2;
2565 sec_cnt = 18;
2568 if (!DOSVM_RawRead(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2570 SET_AX( context, ERROR_READ_FAULT );
2571 SET_CFLAG(context);
2574 break;
2576 case 0x0866: /* get volume serial number */
2578 WCHAR label[12],fsname[9];
2579 DWORD serial;
2581 drivespec[0] += drive;
2582 GetVolumeInformationW(drivespec, label, 12, &serial, NULL, NULL, fsname, 9);
2583 *(WORD*)dataptr = 0;
2584 memcpy(dataptr+2,&serial,4);
2585 WideCharToMultiByte(CP_OEMCP, 0, label, 11, dataptr + 6, 11, NULL, NULL);
2586 WideCharToMultiByte(CP_OEMCP, 0, fsname, 8, dataptr + 17, 8, NULL, NULL);
2588 break;
2590 case 0x086a: /* unlock logical volume */
2591 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2592 'A' + drive );
2593 break;
2595 case 0x086f: /* get drive map information */
2596 memset(dataptr+1, '\0', dataptr[0]-1);
2597 dataptr[1] = dataptr[0];
2598 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
2599 dataptr[3] = 0xFF; /* no physical drive */
2600 break;
2602 case 0x0872:
2603 /* Trial and error implementation */
2604 SET_AX( context, drivetype == DRIVE_UNKNOWN ? 0x0f : 0x01 );
2605 SET_CFLAG(context); /* Seems to be set all the time */
2606 break;
2608 default:
2609 INT_BARF( context, 0x21 );
2611 break;
2613 case 0x0e: /* GET LOGICAL DRIVE MAP */
2614 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2615 'A' + drive );
2616 /* FIXME: this is not correct if drive has mappings */
2617 SET_AL( context, 0 ); /* drive has no mapping */
2618 break;
2620 case 0x0f: /* SET LOGICAL DRIVE MAP */
2622 WCHAR dev[3], tgt[4];
2624 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2625 INT21_DriveName( BL_reg(context)));
2626 dev[0] = 'A' + drive; dev[1] = ':'; dev[2] = 0;
2627 tgt[0] = 'A' + drive + 1; dev[1] = ':'; dev[2] = '\\'; dev[3] = 0;
2628 if (!DefineDosDeviceW(DDD_RAW_TARGET_PATH, dev, tgt))
2630 SET_CFLAG(context);
2631 SET_AX( context, 0x000F ); /* invalid drive */
2634 break;
2636 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2637 default:
2638 INT_BARF( context, 0x21 );
2643 /***********************************************************************
2644 * INT21_IoctlScsiMgrHandler
2646 * IOCTL handler for the SCSIMGR device.
2648 static void INT21_IoctlScsiMgrHandler( CONTEXT86 *context )
2650 switch (AL_reg(context))
2652 case 0x00: /* GET DEVICE INFORMATION */
2653 SET_DX( context, 0xc0c0 );
2654 break;
2656 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2657 DOSVM_ASPIHandler(context);
2658 break;
2660 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2661 SET_DX( context, 0 );
2662 break;
2664 case 0x01: /* SET DEVICE INFORMATION */
2665 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2666 case 0x06: /* GET INPUT STATUS */
2667 case 0x07: /* GET OUTPUT STATUS */
2668 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2669 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2670 default:
2671 INT_BARF( context, 0x21 );
2672 break;
2677 /***********************************************************************
2678 * INT21_IoctlEMSHandler
2680 * IOCTL handler for the EMXXXX0 device.
2682 static void INT21_IoctlEMSHandler( CONTEXT86 *context )
2684 EMS_Ioctl_Handler(context);
2688 /***********************************************************************
2689 * INT21_IoctlHPScanHandler
2691 * IOCTL handler for the HPSCAN device.
2693 static void INT21_IoctlHPScanHandler( CONTEXT86 *context )
2695 switch (AL_reg(context))
2697 case 0x00: /* GET DEVICE INFORMATION */
2698 SET_DX( context, 0xc0c0 );
2699 break;
2701 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2702 SET_DX( context, 0 );
2703 break;
2705 case 0x01: /* SET DEVICE INFORMATION */
2706 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2707 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2708 case 0x06: /* GET INPUT STATUS */
2709 case 0x07: /* GET OUTPUT STATUS */
2710 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2711 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2712 default:
2713 INT_BARF( context, 0x21 );
2714 break;
2719 /***********************************************************************
2720 * INT21_Ioctl_Char
2722 * Handler for character device IOCTLs.
2724 static void INT21_Ioctl_Char( CONTEXT86 *context )
2726 struct stat st;
2727 int status, i, fd;
2728 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2730 status = wine_server_handle_to_fd( handle, 0, &fd, NULL );
2731 if (status)
2733 SET_AX( context, RtlNtStatusToDosError(status) );
2734 SET_CFLAG( context );
2735 return;
2737 fstat( fd, &st );
2738 wine_server_release_fd( handle, fd );
2740 for (i = 0; i < NB_MAGIC_DEVICES; i++)
2742 if (!magic_devices[i].handle) continue;
2743 if (magic_devices[i].dev == st.st_dev && magic_devices[i].ino == st.st_ino)
2745 /* found it */
2746 magic_devices[i].ioctl_handler( context );
2747 return;
2751 /* no magic device found, do default handling */
2753 switch (AL_reg(context))
2755 case 0x00: /* GET DEVICE INFORMATION */
2756 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context) );
2757 if (S_ISCHR(st.st_mode))
2760 * Returns attribute word in DX:
2761 * Bit 14 - Device driver can process IOCTL requests.
2762 * Bit 13 - Output until busy supported.
2763 * Bit 11 - Driver supports OPEN/CLOSE calls.
2764 * Bit 8 - Unknown.
2765 * Bit 7 - Set (indicates device).
2766 * Bit 6 - EOF on input.
2767 * Bit 5 - Raw (binary) mode.
2768 * Bit 4 - Device is special (uses int29).
2769 * Bit 3 - Clock device.
2770 * Bit 2 - NUL device.
2771 * Bit 1 - Standard output.
2772 * Bit 0 - Standard input.
2774 SET_DX( context, 0x80c0 /* FIXME */ );
2776 else
2779 * Returns attribute word in DX:
2780 * Bit 15 - File is remote.
2781 * Bit 14 - Don't set file date/time on closing.
2782 * Bit 11 - Media not removable.
2783 * Bit 8 - Generate int24 if no disk space on write
2784 * or read past end of file
2785 * Bit 7 - Clear (indicates file).
2786 * Bit 6 - File has not been written.
2787 * Bit 5..0 - Drive number (0=A:,...)
2789 * FIXME: Should check if file is on remote or removable drive.
2790 * FIXME: Should use drive file is located on (and not current).
2792 SET_DX( context, 0x0140 + INT21_GetCurrentDrive() );
2794 break;
2796 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2797 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context) );
2799 * Returns attribute word in DX:
2800 * Bit 15 - Set if remote.
2801 * Bit 14 - Set if date/time not set on close.
2803 * FIXME: Should check if file is on remote drive.
2805 SET_DX( context, 0 );
2806 break;
2808 case 0x01: /* SET DEVICE INFORMATION */
2809 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2810 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2811 case 0x06: /* GET INPUT STATUS */
2812 case 0x07: /* GET OUTPUT STATUS */
2813 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2814 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2815 default:
2816 INT_BARF( context, 0x21 );
2817 break;
2822 /***********************************************************************
2823 * INT21_Ioctl
2825 * Handler for function 0x44.
2827 static void INT21_Ioctl( CONTEXT86 *context )
2829 switch (AL_reg(context))
2831 case 0x00:
2832 case 0x01:
2833 case 0x02:
2834 case 0x03:
2835 INT21_Ioctl_Char( context );
2836 break;
2838 case 0x04:
2839 case 0x05:
2840 INT21_Ioctl_Block( context );
2841 break;
2843 case 0x06:
2844 case 0x07:
2845 INT21_Ioctl_Char( context );
2846 break;
2848 case 0x08:
2849 case 0x09:
2850 INT21_Ioctl_Block( context );
2851 break;
2853 case 0x0a:
2854 INT21_Ioctl_Char( context );
2855 break;
2857 case 0x0b: /* SET SHARING RETRY COUNT */
2858 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2859 CX_reg(context), DX_reg(context) );
2860 if (!CX_reg(context))
2862 SET_AX( context, 1 );
2863 SET_CFLAG( context );
2865 else
2867 DOSDEV_SetSharingRetry( CX_reg(context), DX_reg(context) );
2868 RESET_CFLAG( context );
2870 break;
2872 case 0x0c:
2873 INT21_Ioctl_Char( context );
2874 break;
2876 case 0x0d:
2877 case 0x0e:
2878 case 0x0f:
2879 INT21_Ioctl_Block( context );
2880 break;
2882 case 0x10:
2883 INT21_Ioctl_Char( context );
2884 break;
2886 case 0x11:
2887 INT21_Ioctl_Block( context );
2888 break;
2890 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2891 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2892 SET_CFLAG(context); /* Error / This is not DR DOS. */
2893 SET_AX( context, 0x0001 ); /* Invalid function */
2894 break;
2896 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2897 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2898 SET_CFLAG(context); /* Error / This is not DR DOS. */
2899 SET_AX( context, 0x0001 ); /* Invalid function */
2900 break;
2902 case 0xe0: /* Sun PC-NFS API */
2903 TRACE( "Sun PC-NFS API\n" );
2904 /* not installed */
2905 break;
2907 default:
2908 INT_BARF( context, 0x21 );
2913 /***********************************************************************
2914 * INT21_Fat32
2916 * Handler for function 0x73.
2918 static BOOL INT21_Fat32( CONTEXT86 *context )
2920 switch (AL_reg(context))
2922 case 0x02: /* FAT32 - GET EXTENDED DPB */
2924 BYTE drive = INT21_MapDrive( DL_reg(context) );
2925 WORD *ptr = CTX_SEG_OFF_TO_LIN(context,
2926 context->SegEs, context->Edi);
2927 INT21_DPB *target = (INT21_DPB*)(ptr + 1);
2928 INT21_DPB *source;
2930 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context) );
2932 if ( CX_reg(context) < sizeof(INT21_DPB) + 2 || *ptr < sizeof(INT21_DPB) )
2934 SetLastError( ERROR_BAD_LENGTH );
2935 return FALSE;
2938 if ( !INT21_FillDrivePB( drive ) )
2940 SetLastError( ERROR_INVALID_DRIVE );
2941 return FALSE;
2944 source = &INT21_GetHeapPointer()->misc_dpb_list[drive];
2946 *ptr = sizeof(INT21_DPB);
2947 memcpy( target, source, sizeof(INT21_DPB));
2949 if (LOWORD(context->Esi) != 0xF1A6)
2951 target->driver_header = 0;
2952 target->next = 0;
2954 else
2956 FIXME( "Caller requested driver and next DPB pointers!\n" );
2959 break;
2961 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2963 WCHAR dirW[MAX_PATH];
2964 char *dirA = CTX_SEG_OFF_TO_LIN( context,
2965 context->SegDs, context->Edx );
2966 BYTE *data = CTX_SEG_OFF_TO_LIN( context,
2967 context->SegEs, context->Edi );
2968 DWORD cluster_sectors;
2969 DWORD sector_bytes;
2970 DWORD free_clusters;
2971 DWORD total_clusters;
2973 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA );
2974 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
2976 if (CX_reg(context) < 44)
2978 SetLastError( ERROR_BAD_LENGTH );
2979 return FALSE;
2982 if (!GetDiskFreeSpaceW( dirW, &cluster_sectors, &sector_bytes,
2983 &free_clusters, &total_clusters ))
2984 return FALSE;
2986 *(WORD*) (data + 0) = 44; /* size of structure */
2987 *(WORD*) (data + 2) = 0; /* version */
2988 *(DWORD*)(data + 4) = cluster_sectors;
2989 *(DWORD*)(data + 8) = sector_bytes;
2990 *(DWORD*)(data + 12) = free_clusters;
2991 *(DWORD*)(data + 16) = total_clusters;
2994 * Below we have free/total sectors and
2995 * free/total allocation units without adjustment
2996 * for compression. We fake both using cluster information.
2998 *(DWORD*)(data + 20) = free_clusters * cluster_sectors;
2999 *(DWORD*)(data + 24) = total_clusters * cluster_sectors;
3000 *(DWORD*)(data + 28) = free_clusters;
3001 *(DWORD*)(data + 32) = total_clusters;
3004 * Between (data + 36) and (data + 43) there
3005 * are eight reserved bytes.
3008 break;
3010 default:
3011 INT_BARF( context, 0x21 );
3014 return TRUE;
3017 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA *dataA,
3018 const WIN32_FIND_DATAW *dataW)
3020 dataA->dwFileAttributes = dataW->dwFileAttributes;
3021 dataA->ftCreationTime = dataW->ftCreationTime;
3022 dataA->ftLastAccessTime = dataW->ftLastAccessTime;
3023 dataA->ftLastWriteTime = dataW->ftLastWriteTime;
3024 dataA->nFileSizeHigh = dataW->nFileSizeHigh;
3025 dataA->nFileSizeLow = dataW->nFileSizeLow;
3026 WideCharToMultiByte( CP_OEMCP, 0, dataW->cFileName, -1,
3027 dataA->cFileName, sizeof(dataA->cFileName), NULL, NULL );
3028 WideCharToMultiByte( CP_OEMCP, 0, dataW->cAlternateFileName, -1,
3029 dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName), NULL, NULL );
3032 /***********************************************************************
3033 * INT21_LongFilename
3035 * Handler for function 0x71.
3037 static void INT21_LongFilename( CONTEXT86 *context )
3039 BOOL bSetDOSExtendedError = FALSE;
3040 WCHAR pathW[MAX_PATH];
3041 char* pathA;
3043 if (HIBYTE(HIWORD(GetVersion16())) < 0x07)
3045 TRACE( "LONG FILENAME - functions supported only under DOS7\n" );
3046 SET_CFLAG( context );
3047 SET_AL( context, 0 );
3048 return;
3051 switch (AL_reg(context))
3053 case 0x0d: /* RESET DRIVE */
3054 INT_BARF( context, 0x21 );
3055 break;
3057 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
3058 if (!INT21_CreateDirectory( context ))
3059 bSetDOSExtendedError = TRUE;
3060 break;
3062 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
3063 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3065 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA);
3066 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3067 if (!RemoveDirectoryW( pathW )) bSetDOSExtendedError = TRUE;
3068 break;
3070 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
3071 if (!INT21_SetCurrentDirectory( context ))
3072 bSetDOSExtendedError = TRUE;
3073 break;
3075 case 0x41: /* LONG FILENAME - DELETE FILE */
3076 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3078 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA );
3079 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3081 if (!DeleteFileW( pathW )) bSetDOSExtendedError = TRUE;
3082 break;
3084 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
3085 if (!INT21_FileAttributes( context, BL_reg(context), TRUE ))
3086 bSetDOSExtendedError = TRUE;
3087 break;
3089 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
3090 if (!INT21_GetCurrentDirectory( context, TRUE ))
3091 bSetDOSExtendedError = TRUE;
3092 break;
3094 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
3096 HANDLE handle;
3097 HGLOBAL16 h16;
3098 WIN32_FIND_DATAW dataW;
3099 WIN32_FIND_DATAA* dataA;
3101 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
3102 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA);
3104 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3105 handle = FindFirstFileW(pathW, &dataW);
3107 dataA = (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
3108 context->Edi);
3109 if (handle != INVALID_HANDLE_VALUE &&
3110 (h16 = GlobalAlloc16(GMEM_MOVEABLE, sizeof(handle))))
3112 HANDLE* ptr = GlobalLock16( h16 );
3113 *ptr = handle;
3114 GlobalUnlock16( h16 );
3115 SET_AX( context, h16 );
3116 INT21_ConvertFindDataWtoA(dataA, &dataW);
3118 else
3120 if (handle != INVALID_HANDLE_VALUE) FindClose(handle);
3121 SET_AX( context, INVALID_HANDLE_VALUE16);
3122 bSetDOSExtendedError = TRUE;
3125 break;
3127 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
3129 HGLOBAL16 h16 = BX_reg(context);
3130 HANDLE* ptr;
3131 WIN32_FIND_DATAW dataW;
3132 WIN32_FIND_DATAA* dataA;
3134 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
3135 BX_reg(context));
3137 dataA = (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
3138 context->Edi);
3140 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
3142 if (!FindNextFileW(*ptr, &dataW)) bSetDOSExtendedError = TRUE;
3143 else INT21_ConvertFindDataWtoA(dataA, &dataW);
3144 GlobalUnlock16( h16 );
3146 else
3148 SetLastError( ERROR_INVALID_HANDLE );
3149 bSetDOSExtendedError = TRUE;
3152 break;
3154 case 0x56: /* LONG FILENAME - RENAME FILE */
3155 if (!INT21_RenameFile(context))
3156 bSetDOSExtendedError = TRUE;
3157 break;
3159 case 0x60: /* LONG FILENAME - CONVERT PATH */
3161 WCHAR res[MAX_PATH];
3163 switch (CL_reg(context))
3165 case 0x00: /* "truename" - Canonicalize path */
3167 * FIXME: This is not 100% equal to 0x01 case,
3168 * if you fix this, fix int21 subfunction 0x60, too.
3171 case 0x01: /* Get short filename or path */
3172 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
3173 if (!GetShortPathNameW(pathW, res, 67))
3174 bSetDOSExtendedError = TRUE;
3175 else
3177 SET_AX( context, 0 );
3178 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
3179 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3180 67, NULL, NULL);
3182 break;
3184 case 0x02: /* Get canonical long filename or path */
3185 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
3186 if (!GetFullPathNameW(pathW, 128, res, NULL))
3187 bSetDOSExtendedError = TRUE;
3188 else
3190 SET_AX( context, 0 );
3191 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
3192 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3193 128, NULL, NULL);
3195 break;
3196 default:
3197 FIXME("Unimplemented long file name function:\n");
3198 INT_BARF( context, 0x21 );
3199 SET_CFLAG(context);
3200 SET_AL( context, 0 );
3201 break;
3204 break;
3206 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
3207 if (!INT21_CreateFile( context, context->Esi, TRUE,
3208 BX_reg(context), DL_reg(context) ))
3209 bSetDOSExtendedError = TRUE;
3210 break;
3212 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
3214 DWORD filename_len, flags;
3215 WCHAR dstW[8];
3217 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
3219 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA);
3220 SET_AX( context, 0 );
3221 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
3222 if (!GetVolumeInformationW( pathW, NULL, 0, NULL, &filename_len,
3223 &flags, dstW, 8 ))
3225 INT_BARF( context, 0x21 );
3226 SET_CFLAG(context);
3227 break;
3229 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
3230 SET_CX( context, filename_len );
3231 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
3232 WideCharToMultiByte(CP_OEMCP, 0, dstW, -1,
3233 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
3234 8, NULL, NULL);
3236 break;
3238 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
3240 HGLOBAL16 h16 = BX_reg(context);
3241 HANDLE* ptr;
3243 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
3244 BX_reg(context));
3245 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
3247 if (!FindClose( *ptr )) bSetDOSExtendedError = TRUE;
3248 GlobalUnlock16( h16 );
3249 GlobalFree16( h16 );
3251 else
3253 SetLastError( ERROR_INVALID_HANDLE );
3254 bSetDOSExtendedError = TRUE;
3257 break;
3259 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
3261 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
3262 BY_HANDLE_FILE_INFORMATION *info =
3263 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3265 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
3267 if (!GetFileInformationByHandle(handle, info))
3268 bSetDOSExtendedError = TRUE;
3270 break;
3272 case 0xa7: /* LONG FILENAME - CONVERT TIME */
3273 switch (BL_reg(context))
3275 case 0x00: /* FILE TIME TO DOS TIME */
3277 WORD date, time;
3278 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3279 context->SegDs,
3280 context->Esi);
3282 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
3284 FileTimeToDosDateTime( filetime, &date, &time );
3286 SET_DX( context, date );
3287 SET_CX( context, time );
3290 * FIXME: BH has number of 10-millisecond units
3291 * past time in CX.
3293 SET_BH( context, 0 );
3295 break;
3297 case 0x01: /* DOS TIME TO FILE TIME */
3299 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
3300 context->SegEs,
3301 context->Edi);
3303 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
3306 * FIXME: BH has number of 10-millisecond units
3307 * past time in CX.
3309 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
3310 filetime );
3312 break;
3314 default:
3315 INT_BARF( context, 0x21 );
3316 break;
3318 break;
3320 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
3321 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3322 case 0xaa: /* LONG FILENAME - SUBST */
3323 default:
3324 FIXME("Unimplemented long file name function:\n");
3325 INT_BARF( context, 0x21 );
3326 SET_CFLAG(context);
3327 SET_AL( context, 0 );
3328 break;
3331 if (bSetDOSExtendedError)
3333 SET_AX( context, GetLastError() );
3334 SET_CFLAG( context );
3339 /***********************************************************************
3340 * INT21_RenameFile
3342 * Handler for:
3343 * - function 0x56
3344 * - subfunction 0x56 of function 0x71
3345 * - subfunction 0xff of function 0x43 (CL == 0x56)
3347 static BOOL INT21_RenameFile( CONTEXT86 *context )
3349 WCHAR fromW[MAX_PATH];
3350 WCHAR toW[MAX_PATH];
3351 char *fromA = CTX_SEG_OFF_TO_LIN(context,
3352 context->SegDs,context->Edx);
3353 char *toA = CTX_SEG_OFF_TO_LIN(context,
3354 context->SegEs,context->Edi);
3356 TRACE( "RENAME FILE %s to %s\n", fromA, toA );
3357 MultiByteToWideChar(CP_OEMCP, 0, fromA, -1, fromW, MAX_PATH);
3358 MultiByteToWideChar(CP_OEMCP, 0, toA, -1, toW, MAX_PATH);
3360 return MoveFileW( fromW, toW );
3364 /***********************************************************************
3365 * INT21_NetworkFunc
3367 * Handler for:
3368 * - function 0x5e
3370 static BOOL INT21_NetworkFunc (CONTEXT86 *context)
3372 switch (AL_reg(context))
3374 case 0x00: /* Get machine name. */
3376 WCHAR dstW[MAX_COMPUTERNAME_LENGTH + 1];
3377 DWORD s = sizeof(dstW) / sizeof(WCHAR);
3378 int len;
3380 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
3381 TRACE("getting machine name to %p\n", dst);
3382 if (!GetComputerNameW(dstW, &s) ||
3383 !WideCharToMultiByte(CP_OEMCP, 0, dstW, -1, dst, 16, NULL, NULL))
3385 WARN("failed!\n");
3386 SetLastError( ER_NoNetwork );
3387 return TRUE;
3389 for (len = strlen(dst); len < 15; len++) dst[len] = ' ';
3390 dst[15] = 0;
3391 SET_CH( context, 1 ); /* Valid */
3392 SET_CL( context, 1 ); /* NETbios number??? */
3393 TRACE("returning %s\n", debugstr_an(dst, 16));
3394 return FALSE;
3397 default:
3398 SetLastError( ER_NoNetwork );
3399 return TRUE;
3403 /******************************************************************
3404 * INT21_GetDiskSerialNumber
3407 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
3409 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3410 WCHAR path[] = {'A',':',0}, label[11];
3411 DWORD serial;
3413 path[0] += INT21_MapDrive(BL_reg(context));
3414 if (!GetVolumeInformationW( path, label, 11, &serial, NULL, NULL, NULL, 0))
3416 SetLastError( ERROR_INVALID_DRIVE );
3417 return 0;
3420 *(WORD *)dataptr = 0;
3421 memcpy(dataptr + 2, &serial, sizeof(DWORD));
3422 WideCharToMultiByte(CP_OEMCP, 0, label, 11, dataptr + 6, 11, NULL, NULL);
3423 strncpy(dataptr + 17, "FAT16 ", 8);
3424 return 1;
3428 /******************************************************************
3429 * INT21_SetDiskSerialNumber
3432 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
3434 #if 0
3435 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3436 int drive = INT21_MapDrive(BL_reg(context));
3438 if (!is_valid_drive(drive))
3440 SetLastError( ERROR_INVALID_DRIVE );
3441 return 0;
3444 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
3445 return 1;
3446 #else
3447 FIXME("Setting drive serial number is no longer supported\n");
3448 SetLastError( ERROR_NOT_SUPPORTED );
3449 return 0;
3450 #endif
3454 /******************************************************************
3455 * INT21_GetFreeDiskSpace
3458 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
3460 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
3461 WCHAR root[] = {'A',':','\\',0};
3463 root[0] += INT21_MapDrive(DL_reg(context));
3464 if (!GetDiskFreeSpaceW( root, &cluster_sectors, &sector_bytes,
3465 &free_clusters, &total_clusters )) return 0;
3466 /* make sure that the number of clusters fits in a 16 bits value */
3467 while( total_clusters & 0xffff0000) {
3468 cluster_sectors <<= 1;
3469 free_clusters >>= 1;
3470 total_clusters >>= 1;
3472 SET_AX( context, cluster_sectors );
3473 SET_BX( context, free_clusters );
3474 SET_CX( context, sector_bytes );
3475 SET_DX( context, total_clusters );
3476 return 1;
3479 /******************************************************************
3480 * INT21_GetDriveAllocInfo
3483 static int INT21_GetDriveAllocInfo( CONTEXT86 *context, BYTE drive )
3485 INT21_DPB *dpb;
3487 drive = INT21_MapDrive( drive );
3488 if (!INT21_FillDrivePB( drive )) return 0;
3489 dpb = &(INT21_GetHeapPointer()->misc_dpb_list[drive]);
3490 SET_AL( context, dpb->cluster_sectors + 1 );
3491 SET_CX( context, dpb->sector_bytes );
3492 SET_DX( context, dpb->num_clusters1 );
3494 context->SegDs = INT21_GetHeapSelector( context );
3495 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive].media_ID ) );
3496 return 1;
3499 /***********************************************************************
3500 * INT21_GetExtendedError
3502 static void INT21_GetExtendedError( CONTEXT86 *context )
3504 BYTE class, action, locus;
3505 WORD error = GetLastError();
3507 switch(error)
3509 case ERROR_SUCCESS:
3510 class = action = locus = 0;
3511 break;
3512 case ERROR_DIR_NOT_EMPTY:
3513 class = EC_Exists;
3514 action = SA_Ignore;
3515 locus = EL_Disk;
3516 break;
3517 case ERROR_ACCESS_DENIED:
3518 class = EC_AccessDenied;
3519 action = SA_Abort;
3520 locus = EL_Disk;
3521 break;
3522 case ERROR_CANNOT_MAKE:
3523 class = EC_AccessDenied;
3524 action = SA_Abort;
3525 locus = EL_Unknown;
3526 break;
3527 case ERROR_DISK_FULL:
3528 case ERROR_HANDLE_DISK_FULL:
3529 class = EC_MediaError;
3530 action = SA_Abort;
3531 locus = EL_Disk;
3532 break;
3533 case ERROR_FILE_EXISTS:
3534 case ERROR_ALREADY_EXISTS:
3535 class = EC_Exists;
3536 action = SA_Abort;
3537 locus = EL_Disk;
3538 break;
3539 case ERROR_FILE_NOT_FOUND:
3540 class = EC_NotFound;
3541 action = SA_Abort;
3542 locus = EL_Disk;
3543 break;
3544 case ERROR_GEN_FAILURE:
3545 class = EC_SystemFailure;
3546 action = SA_Abort;
3547 locus = EL_Unknown;
3548 break;
3549 case ERROR_INVALID_DRIVE:
3550 class = EC_MediaError;
3551 action = SA_Abort;
3552 locus = EL_Disk;
3553 break;
3554 case ERROR_INVALID_HANDLE:
3555 class = EC_ProgramError;
3556 action = SA_Abort;
3557 locus = EL_Disk;
3558 break;
3559 case ERROR_LOCK_VIOLATION:
3560 class = EC_AccessDenied;
3561 action = SA_Abort;
3562 locus = EL_Disk;
3563 break;
3564 case ERROR_NO_MORE_FILES:
3565 class = EC_MediaError;
3566 action = SA_Abort;
3567 locus = EL_Disk;
3568 break;
3569 case ER_NoNetwork:
3570 class = EC_NotFound;
3571 action = SA_Abort;
3572 locus = EL_Network;
3573 break;
3574 case ERROR_NOT_ENOUGH_MEMORY:
3575 class = EC_OutOfResource;
3576 action = SA_Abort;
3577 locus = EL_Memory;
3578 break;
3579 case ERROR_PATH_NOT_FOUND:
3580 class = EC_NotFound;
3581 action = SA_Abort;
3582 locus = EL_Disk;
3583 break;
3584 case ERROR_SEEK:
3585 class = EC_NotFound;
3586 action = SA_Ignore;
3587 locus = EL_Disk;
3588 break;
3589 case ERROR_SHARING_VIOLATION:
3590 class = EC_Temporary;
3591 action = SA_Retry;
3592 locus = EL_Disk;
3593 break;
3594 case ERROR_TOO_MANY_OPEN_FILES:
3595 class = EC_ProgramError;
3596 action = SA_Abort;
3597 locus = EL_Disk;
3598 break;
3599 default:
3600 FIXME("Unknown error %d\n", error );
3601 class = EC_SystemFailure;
3602 action = SA_Abort;
3603 locus = EL_Unknown;
3604 break;
3606 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3607 error, class, action, locus );
3608 SET_AX( context, error );
3609 SET_BH( context, class );
3610 SET_BL( context, action );
3611 SET_CH( context, locus );
3614 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
3616 static int counter = 0;
3617 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
3618 char *p = name + strlen(name);
3620 /* despite what Ralf Brown says, some programs seem to call without
3621 * ending backslash (DOS accepts that, so we accept it too) */
3622 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
3624 for (;;)
3626 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
3627 counter = (counter + 1) % 1000;
3629 SET_AX( context,
3630 Win32HandleToDosFileHandle(
3631 CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
3632 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
3633 CREATE_NEW, 0, 0 ) ) );
3634 if (AX_reg(context) != HFILE_ERROR16)
3636 TRACE("created %s\n", name );
3637 return TRUE;
3639 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
3643 /***********************************************************************
3644 * DOSFS_ToDosFCBFormat
3646 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3647 * expanding wild cards and converting to upper-case in the process.
3648 * File name can be terminated by '\0', '\\' or '/'.
3649 * Return FALSE if the name is not a valid DOS name.
3650 * 'buffer' must be at least 12 characters long.
3652 /* Chars we don't want to see in DOS file names */
3653 static BOOL INT21_ToDosFCBFormat( LPCWSTR name, LPWSTR buffer )
3655 static const WCHAR invalid_chars[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3656 LPCWSTR p = name;
3657 int i;
3659 /* Check for "." and ".." */
3660 if (*p == '.')
3662 p++;
3663 buffer[0] = '.';
3664 for(i = 1; i < 11; i++) buffer[i] = ' ';
3665 buffer[11] = 0;
3666 if (*p == '.')
3668 buffer[1] = '.';
3669 p++;
3671 return (!*p || (*p == '/') || (*p == '\\'));
3674 for (i = 0; i < 8; i++)
3676 switch(*p)
3678 case '\0':
3679 case '\\':
3680 case '/':
3681 case '.':
3682 buffer[i] = ' ';
3683 break;
3684 case '?':
3685 p++;
3686 /* fall through */
3687 case '*':
3688 buffer[i] = '?';
3689 break;
3690 default:
3691 if (strchrW( invalid_chars, *p )) return FALSE;
3692 buffer[i] = toupperW(*p);
3693 p++;
3694 break;
3698 if (*p == '*')
3700 /* Skip all chars after wildcard up to first dot */
3701 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
3703 else
3705 /* Check if name too long */
3706 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return FALSE;
3708 if (*p == '.') p++; /* Skip dot */
3710 for (i = 8; i < 11; i++)
3712 switch(*p)
3714 case '\0':
3715 case '\\':
3716 case '/':
3717 buffer[i] = ' ';
3718 break;
3719 case '.':
3720 return FALSE; /* Second extension not allowed */
3721 case '?':
3722 p++;
3723 /* fall through */
3724 case '*':
3725 buffer[i] = '?';
3726 break;
3727 default:
3728 if (strchrW( invalid_chars, *p )) return FALSE;
3729 buffer[i] = toupperW(*p);
3730 p++;
3731 break;
3734 buffer[11] = '\0';
3736 /* at most 3 character of the extension are processed
3737 * is something behind this ?
3739 while (*p == '*' || *p == ' ') p++; /* skip wildcards and spaces */
3740 return (!*p || (*p == '/') || (*p == '\\'));
3743 static HANDLE INT21_FindHandle;
3744 static const WCHAR *INT21_FindPath; /* will point to current dta->fullPath search */
3746 /******************************************************************
3747 * INT21_FindFirst
3749 static int INT21_FindFirst( CONTEXT86 *context )
3751 WCHAR *p, *q;
3752 const char *path;
3753 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3754 WCHAR maskW[12], pathW[MAX_PATH];
3755 static const WCHAR wildcardW[] = {'*','.','*',0};
3757 path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3758 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
3760 p = strrchrW( pathW, '\\');
3761 q = strrchrW( pathW, '/');
3762 if (q>p) p = q;
3763 if (!p)
3765 if (pathW[0] && pathW[1] == ':') p = pathW + 2;
3766 else p = pathW;
3768 else p++;
3770 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3771 * (doesn't matter as it is set below anyway)
3773 if (!INT21_ToDosFCBFormat( p, maskW ))
3775 SetLastError( ERROR_FILE_NOT_FOUND );
3776 SET_AX( context, ERROR_FILE_NOT_FOUND );
3777 SET_CFLAG(context);
3778 return 0;
3780 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
3782 dta->fullPath = HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW) + (p - pathW)*sizeof(WCHAR) );
3783 memcpy( dta->fullPath, pathW, (p - pathW) * sizeof(WCHAR) );
3784 memcpy( dta->fullPath + (p - pathW), wildcardW, sizeof(wildcardW) );
3785 /* we must have a fully qualified file name in dta->fullPath
3786 * (we could have a UNC path, but this would lead to some errors later on)
3788 dta->drive = toupperW(dta->fullPath[0]) - 'A';
3789 dta->count = 0;
3790 dta->search_attr = CL_reg(context);
3791 return 1;
3794 /******************************************************************
3795 * match_short
3797 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3799 static BOOL match_short(LPCWSTR shortW, LPCSTR maskA)
3801 WCHAR mask[11], file[12];
3802 int i;
3804 if (!INT21_ToDosFCBFormat( shortW, file )) return FALSE;
3805 MultiByteToWideChar(CP_OEMCP, 0, maskA, 11, mask, 11);
3806 for (i = 0; i < 11; i++)
3807 if (mask[i] != '?' && mask[i] != file[i]) return FALSE;
3808 return TRUE;
3811 static unsigned INT21_FindHelper(LPCWSTR fullPath, unsigned drive, unsigned count,
3812 LPCSTR mask, unsigned search_attr,
3813 WIN32_FIND_DATAW* entry)
3815 unsigned ncalls;
3817 if ((search_attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
3819 WCHAR path[] = {' ',':',0};
3821 if (count) return 0;
3822 path[0] = drive + 'A';
3823 if (!GetVolumeInformationW(path, entry->cAlternateFileName, 13, NULL, NULL, NULL, NULL, 0)) return 0;
3824 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftCreationTime );
3825 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftLastAccessTime );
3826 RtlSecondsSince1970ToTime( (time_t)0, (LARGE_INTEGER *)&entry->ftLastWriteTime );
3827 entry->dwFileAttributes = FA_LABEL;
3828 entry->nFileSizeHigh = entry->nFileSizeLow = 0;
3829 TRACE("returning %s as label\n", debugstr_w(entry->cAlternateFileName));
3830 return 1;
3833 if (!INT21_FindHandle || INT21_FindPath != fullPath || count == 0)
3835 if (INT21_FindHandle) FindClose(INT21_FindHandle);
3836 INT21_FindHandle = FindFirstFileW(fullPath, entry);
3837 if (INT21_FindHandle == INVALID_HANDLE_VALUE)
3839 INT21_FindHandle = 0;
3840 return 0;
3842 INT21_FindPath = fullPath;
3843 /* we need to resync search */
3844 ncalls = count;
3846 else ncalls = 1;
3848 while (ncalls-- != 0)
3850 if (!FindNextFileW(INT21_FindHandle, entry))
3852 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3853 return 0;
3856 while (count < 0xffff)
3858 count++;
3859 /* Check the file attributes, and path */
3860 if (!(entry->dwFileAttributes & ~search_attr) &&
3861 match_short(entry->cAlternateFileName[0] ? entry->cAlternateFileName : entry->cFileName,
3862 mask))
3864 return count;
3866 if (!FindNextFileW(INT21_FindHandle, entry))
3868 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3869 return 0;
3872 WARN("Too many directory entries in %s\n", debugstr_w(fullPath) );
3873 return 0;
3876 /******************************************************************
3877 * INT21_FindNext
3879 static int INT21_FindNext( CONTEXT86 *context )
3881 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3882 DWORD attr = dta->search_attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
3883 WIN32_FIND_DATAW entry;
3884 int n;
3886 if (!dta->fullPath) return 0;
3888 n = INT21_FindHelper(dta->fullPath, dta->drive, dta->count,
3889 dta->mask, attr, &entry);
3890 if (n)
3892 dta->fileattr = entry.dwFileAttributes;
3893 dta->filesize = entry.nFileSizeLow;
3894 FileTimeToDosDateTime( &entry.ftLastWriteTime, &dta->filedate, &dta->filetime );
3895 if (entry.cAlternateFileName[0])
3896 WideCharToMultiByte(CP_OEMCP, 0, entry.cAlternateFileName, -1,
3897 dta->filename, 13, NULL, NULL);
3898 else
3899 WideCharToMultiByte(CP_OEMCP, 0, entry.cFileName, -1, dta->filename, 13, NULL, NULL);
3901 if (!memchr(dta->mask,'?',11))
3903 /* wildcardless search, release resources in case no findnext will
3904 * be issued, and as a workaround in case file creation messes up
3905 * findnext, as sometimes happens with pkunzip
3907 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3908 INT21_FindPath = dta->fullPath = NULL;
3910 dta->count = n;
3911 return 1;
3913 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3914 INT21_FindPath = dta->fullPath = NULL;
3915 return 0;
3918 /* microsoft's programmers should be shot for using CP/M style int21
3919 calls in Windows for Workgroup's winfile.exe */
3921 /******************************************************************
3922 * INT21_FindFirstFCB
3925 static int INT21_FindFirstFCB( CONTEXT86 *context )
3927 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3928 FINDFILE_FCB *pFCB;
3929 int drive;
3930 WCHAR p[] = {' ',':',};
3932 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
3933 else pFCB = (FINDFILE_FCB *)fcb;
3934 drive = INT21_MapDrive( pFCB->drive );
3935 if (drive == MAX_DOS_DRIVES) return 0;
3937 p[0] = 'A' + drive;
3938 pFCB->fullPath = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
3939 if (!pFCB->fullPath) return 0;
3940 GetLongPathNameW(p, pFCB->fullPath, MAX_PATH);
3941 pFCB->count = 0;
3942 return 1;
3945 /******************************************************************
3946 * INT21_FindNextFCB
3949 static int INT21_FindNextFCB( CONTEXT86 *context )
3951 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3952 FINDFILE_FCB *pFCB;
3953 LPBYTE pResult = INT21_GetCurrentDTA(context);
3954 DOS_DIRENTRY_LAYOUT *ddl;
3955 WIN32_FIND_DATAW entry;
3956 BYTE attr;
3957 int n;
3958 WCHAR nameW[12];
3960 if (*fcb == 0xff) /* extended FCB ? */
3962 attr = fcb[6];
3963 pFCB = (FINDFILE_FCB *)(fcb + 7);
3965 else
3967 attr = 0;
3968 pFCB = (FINDFILE_FCB *)fcb;
3971 if (!pFCB->fullPath) return 0;
3972 n = INT21_FindHelper(pFCB->fullPath, INT21_MapDrive( pFCB->drive ),
3973 pFCB->count, pFCB->filename, attr, &entry);
3974 if (!n)
3976 HeapFree( GetProcessHeap(), 0, pFCB->fullPath );
3977 INT21_FindPath = pFCB->fullPath = NULL;
3978 return 0;
3980 pFCB->count += n;
3982 if (*fcb == 0xff)
3984 /* place extended FCB header before pResult if called with extended FCB */
3985 *pResult = 0xff;
3986 pResult += 6; /* leave reserved field behind */
3987 *pResult++ = entry.dwFileAttributes;
3989 *pResult++ = INT21_MapDrive( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
3990 ddl = (DOS_DIRENTRY_LAYOUT*)pResult;
3991 ddl->fileattr = entry.dwFileAttributes;
3992 ddl->cluster = 0; /* what else? */
3993 ddl->filesize = entry.nFileSizeLow;
3994 memset( ddl->reserved, 0, sizeof(ddl->reserved) );
3995 FileTimeToDosDateTime( &entry.ftLastWriteTime,
3996 &ddl->filedate, &ddl->filetime );
3998 /* Convert file name to FCB format */
3999 if (entry.cAlternateFileName[0])
4000 INT21_ToDosFCBFormat( entry.cAlternateFileName, nameW );
4001 else
4002 INT21_ToDosFCBFormat( entry.cFileName, nameW );
4003 WideCharToMultiByte(CP_OEMCP, 0, nameW, 11, ddl->filename, 11, NULL, NULL);
4004 return 1;
4008 /******************************************************************
4009 * INT21_ParseFileNameIntoFCB
4012 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
4014 char *filename =
4015 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
4016 char *fcb =
4017 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
4018 char *s;
4019 WCHAR *buffer;
4020 WCHAR fcbW[12];
4021 INT buffer_len, len;
4023 SET_AL( context, 0xff ); /* failed */
4025 TRACE("filename: '%s'\n", filename);
4027 s = filename;
4028 while (*s && (*s != ' ') && (*s != '\r') && (*s != '\n'))
4029 s++;
4030 len = filename - s;
4032 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
4033 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
4034 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
4035 buffer[len] = 0;
4036 INT21_ToDosFCBFormat(buffer, fcbW);
4037 HeapFree(GetProcessHeap(), 0, buffer);
4038 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
4039 *fcb = 0;
4040 TRACE("FCB: '%s'\n", fcb + 1);
4042 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
4044 /* point DS:SI to first unparsed character */
4045 SET_SI( context, context->Esi + (int)s - (int)filename );
4048 static BOOL INT21_Dup2(HFILE16 hFile1, HFILE16 hFile2)
4050 HFILE16 res = HFILE_ERROR16;
4051 HANDLE handle, new_handle;
4052 #define DOS_TABLE_SIZE 256
4053 DWORD map[DOS_TABLE_SIZE / 32];
4054 int i;
4056 handle = DosFileHandleToWin32Handle(hFile1);
4057 if (handle == INVALID_HANDLE_VALUE)
4058 return FALSE;
4060 _lclose16(hFile2);
4061 /* now loop to allocate the same one... */
4062 memset(map, 0, sizeof(map));
4063 for (i = 0; i < DOS_TABLE_SIZE; i++)
4065 if (!DuplicateHandle(GetCurrentProcess(), handle,
4066 GetCurrentProcess(), &new_handle,
4067 0, FALSE, DUPLICATE_SAME_ACCESS))
4069 res = HFILE_ERROR16;
4070 break;
4072 res = Win32HandleToDosFileHandle(new_handle);
4073 if (res == HFILE_ERROR16 || res == hFile2) break;
4074 map[res / 32] |= 1 << (res % 32);
4076 /* clean up the allocated slots */
4077 for (i = 0; i < DOS_TABLE_SIZE; i++)
4079 if (map[i / 32] & (1 << (i % 32)))
4080 _lclose16((HFILE16)i);
4082 return res == hFile2;
4086 /***********************************************************************
4087 * DOSVM_Int21Handler
4089 * Interrupt 0x21 handler.
4091 void WINAPI DOSVM_Int21Handler( CONTEXT86 *context )
4093 BOOL bSetDOSExtendedError = FALSE;
4095 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
4096 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
4097 AX_reg(context), BX_reg(context),
4098 CX_reg(context), DX_reg(context),
4099 SI_reg(context), DI_reg(context),
4100 (WORD)context->SegDs, (WORD)context->SegEs,
4101 context->EFlags );
4104 * Extended error is used by (at least) functions 0x2f to 0x62.
4105 * Function 0x59 returns extended error and error should not
4106 * be cleared before handling the function.
4108 if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59)
4109 SetLastError(0);
4111 RESET_CFLAG(context); /* Not sure if this is a good idea. */
4113 switch(AH_reg(context))
4115 case 0x00: /* TERMINATE PROGRAM */
4116 TRACE("TERMINATE PROGRAM\n");
4117 if (DOSVM_IsWin16())
4118 ExitThread( 0 );
4119 else if(ISV86(context))
4120 MZ_Exit( context, FALSE, 0 );
4121 else
4122 ERR( "Called from DOS protected mode\n" );
4123 break;
4125 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
4127 BYTE ascii;
4128 TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
4129 INT21_ReadChar( &ascii, context );
4130 SET_AL( context, ascii );
4132 * FIXME: What to echo when extended keycodes are read?
4134 DOSVM_PutChar(AL_reg(context));
4136 break;
4138 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
4139 TRACE("Write Character to Standard Output\n");
4140 DOSVM_PutChar(DL_reg(context));
4141 break;
4143 case 0x03: /* READ CHARACTER FROM STDAUX */
4144 case 0x04: /* WRITE CHARACTER TO STDAUX */
4145 case 0x05: /* WRITE CHARACTER TO PRINTER */
4146 INT_BARF( context, 0x21 );
4147 break;
4149 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
4150 if (DL_reg(context) == 0xff)
4152 TRACE("Direct Console Input\n");
4154 if (INT21_ReadChar( NULL, NULL ))
4156 BYTE ascii;
4157 INT21_ReadChar( &ascii, context );
4158 SET_AL( context, ascii );
4159 RESET_ZFLAG( context );
4161 else
4163 /* no character available */
4164 SET_AL( context, 0 );
4165 SET_ZFLAG( context );
4168 else
4170 TRACE("Direct Console Output\n");
4171 DOSVM_PutChar(DL_reg(context));
4173 * At least DOS versions 2.1-7.0 return character
4174 * that was written in AL register.
4176 SET_AL( context, DL_reg(context) );
4178 break;
4180 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
4182 BYTE ascii;
4183 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
4184 INT21_ReadChar( &ascii, context );
4185 SET_AL( context, ascii );
4187 break;
4189 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
4191 BYTE ascii;
4192 TRACE("CHARACTER INPUT WITHOUT ECHO\n");
4193 INT21_ReadChar( &ascii, context );
4194 SET_AL( context, ascii );
4196 break;
4198 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
4199 TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
4200 context->SegDs, DX_reg(context) );
4202 LPSTR data = CTX_SEG_OFF_TO_LIN( context,
4203 context->SegDs, context->Edx );
4204 LPSTR p = data;
4207 * Do NOT use strchr() to calculate the string length,
4208 * as '\0' is valid string content, too!
4209 * Maybe we should check for non-'$' strings, but DOS doesn't.
4211 while (*p != '$') p++;
4213 if (DOSVM_IsWin16())
4214 WriteFile( DosFileHandleToWin32Handle(1),
4215 data, p - data, 0, 0 );
4216 else
4217 for(; data != p; data++)
4218 DOSVM_PutChar( *data );
4220 SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
4222 break;
4224 case 0x0a: /* BUFFERED INPUT */
4226 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context,
4227 context->SegDs,
4228 context->Edx);
4229 WORD result;
4231 TRACE( "BUFFERED INPUT (size=%d)\n", ptr[0] );
4234 * FIXME: Some documents state that
4235 * ptr[1] holds number of chars from last input which
4236 * may be recalled on entry, other documents do not mention
4237 * this at all.
4239 if (ptr[1])
4240 TRACE( "Handle old chars in buffer!\n" );
4243 * ptr[0] - capacity (includes terminating CR)
4244 * ptr[1] - characters read (excludes terminating CR)
4246 result = INT21_BufferedInput( context, ptr + 2, ptr[0] );
4247 if (result > 0)
4248 ptr[1] = (BYTE)result - 1;
4249 else
4250 ptr[1] = 0;
4252 break;
4254 case 0x0b: /* GET STDIN STATUS */
4255 TRACE( "GET STDIN STATUS\n" );
4257 if (INT21_ReadChar( NULL, NULL ))
4258 SET_AL( context, 0xff ); /* character available */
4259 else
4260 SET_AL( context, 0 ); /* no character available */
4262 break;
4264 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
4266 BYTE al = AL_reg(context); /* Input function to execute after flush. */
4268 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
4270 /* FIXME: buffers are not flushed */
4273 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
4274 * int21 function identified by AL will be called.
4276 if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
4278 SET_AH( context, al );
4279 DOSVM_Int21Handler( context );
4282 break;
4284 case 0x0d: /* DISK BUFFER FLUSH */
4285 TRACE("DISK BUFFER FLUSH ignored\n");
4286 break;
4288 case 0x0e: /* SELECT DEFAULT DRIVE */
4289 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context) );
4290 INT21_SetCurrentDrive( DL_reg(context) );
4291 SET_AL( context, MAX_DOS_DRIVES );
4292 break;
4294 case 0x0f: /* OPEN FILE USING FCB */
4295 INT21_OpenFileUsingFCB( context );
4296 break;
4298 case 0x10: /* CLOSE FILE USING FCB */
4299 INT21_CloseFileUsingFCB( context );
4300 break;
4302 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
4303 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
4304 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
4305 if (!INT21_FindFirstFCB(context))
4307 SET_AL( context, 0xff );
4308 break;
4310 /* else fall through */
4312 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
4313 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
4314 break;
4316 case 0x13: /* DELETE FILE USING FCB */
4317 INT_BARF( context, 0x21 );
4318 break;
4320 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
4321 INT21_SequentialReadFromFCB( context );
4322 break;
4324 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
4325 INT21_SequentialWriteToFCB( context );
4326 break;
4328 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
4329 case 0x17: /* RENAME FILE USING FCB */
4330 INT_BARF( context, 0x21 );
4331 break;
4333 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4334 SET_AL( context, 0 );
4335 break;
4337 case 0x19: /* GET CURRENT DEFAULT DRIVE */
4338 SET_AL( context, INT21_GetCurrentDrive() );
4339 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context ) );
4340 break;
4342 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
4343 TRACE( "SET DISK TRANSFER AREA ADDRESS %04lX:%04X\n",
4344 context->SegDs, DX_reg(context) );
4346 TDB *task = GlobalLock16( GetCurrentTask() );
4347 task->dta = MAKESEGPTR( context->SegDs, DX_reg(context) );
4349 break;
4351 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4352 if (!INT21_GetDriveAllocInfo(context, 0))
4353 SET_AX( context, 0xffff );
4354 break;
4356 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4357 if (!INT21_GetDriveAllocInfo(context, DL_reg(context)))
4358 SET_AX( context, 0xffff );
4359 break;
4361 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4362 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4363 SET_AL( context, 0 );
4364 break;
4366 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4368 BYTE drive = INT21_MapDrive( 0 );
4369 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4371 if (INT21_FillDrivePB( drive ))
4373 SET_AL( context, 0x00 ); /* success */
4374 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4375 context->SegDs = INT21_GetHeapSelector( context );
4377 else
4379 SET_AL( context, 0xff ); /* invalid or network drive */
4382 break;
4384 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4385 SET_AL( context, 0 );
4386 break;
4388 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4389 INT21_ReadRandomRecordFromFCB( context );
4390 break;
4392 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4393 INT21_WriteRandomRecordToFCB( context );
4394 break;
4396 case 0x23: /* GET FILE SIZE FOR FCB */
4397 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4398 INT_BARF( context, 0x21 );
4399 break;
4401 case 0x25: /* SET INTERRUPT VECTOR */
4402 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4404 FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
4405 if (!ISV86(context) && DOSVM_IsWin16())
4406 DOSVM_SetPMHandler16( AL_reg(context), ptr );
4407 else
4408 DOSVM_SetRMHandler( AL_reg(context), ptr );
4410 break;
4412 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4413 INT_BARF( context, 0x21 );
4414 break;
4416 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4417 INT21_RandomBlockReadFromFCB( context );
4418 break;
4420 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4421 INT21_RandomBlockWriteToFCB( context );
4422 break;
4424 case 0x29: /* PARSE FILENAME INTO FCB */
4425 INT21_ParseFileNameIntoFCB(context);
4426 break;
4428 case 0x2a: /* GET SYSTEM DATE */
4429 TRACE( "GET SYSTEM DATE\n" );
4431 SYSTEMTIME systime;
4432 GetLocalTime( &systime );
4433 SET_CX( context, systime.wYear );
4434 SET_DH( context, systime.wMonth );
4435 SET_DL( context, systime.wDay );
4436 SET_AL( context, systime.wDayOfWeek );
4438 break;
4440 case 0x2b: /* SET SYSTEM DATE */
4441 TRACE( "SET SYSTEM DATE\n" );
4443 WORD year = CX_reg(context);
4444 BYTE month = DH_reg(context);
4445 BYTE day = DL_reg(context);
4447 if (year >= 1980 && year <= 2099 &&
4448 month >= 1 && month <= 12 &&
4449 day >= 1 && day <= 31)
4451 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4452 day, month, year );
4453 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4455 else
4457 SET_AL( context, 0xff ); /* invalid date */
4458 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4459 day, month, year );
4462 break;
4464 case 0x2c: /* GET SYSTEM TIME */
4465 TRACE( "GET SYSTEM TIME\n" );
4467 SYSTEMTIME systime;
4468 GetLocalTime( &systime );
4469 SET_CH( context, systime.wHour );
4470 SET_CL( context, systime.wMinute );
4471 SET_DH( context, systime.wSecond );
4472 SET_DL( context, systime.wMilliseconds / 10 );
4474 break;
4476 case 0x2d: /* SET SYSTEM TIME */
4477 if( CH_reg(context) >= 24 || CL_reg(context) >= 60 || DH_reg(context) >= 60 || DL_reg(context) >= 100 ) {
4478 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4479 CH_reg(context), CL_reg(context),
4480 DH_reg(context), DL_reg(context) );
4481 SET_AL( context, 0xFF );
4483 else
4485 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4486 CH_reg(context), CL_reg(context),
4487 DH_reg(context), DL_reg(context) );
4488 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4490 break;
4492 case 0x2e: /* SET VERIFY FLAG */
4493 TRACE("SET VERIFY FLAG ignored\n");
4494 /* we cannot change the behaviour anyway, so just ignore it */
4495 break;
4497 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4498 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4500 TDB *task = GlobalLock16( GetCurrentTask() );
4501 context->SegEs = SELECTOROF( task->dta );
4502 SET_BX( context, OFFSETOF( task->dta ) );
4504 break;
4506 case 0x30: /* GET DOS VERSION */
4507 TRACE( "GET DOS VERSION - %s requested\n",
4508 (AL_reg(context) == 0x00) ? "OEM number" : "version flag" );
4510 if (AL_reg(context) == 0x00)
4511 SET_BH( context, 0xff ); /* OEM number => undefined */
4512 else
4513 SET_BH( context, 0x08 ); /* version flag => DOS is in ROM */
4515 SET_AL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4516 SET_AH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4518 SET_BL( context, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4519 SET_CX( context, 0x3456 );
4520 break;
4522 case 0x31: /* TERMINATE AND STAY RESIDENT */
4523 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4524 break;
4526 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4528 BYTE drive = INT21_MapDrive( DL_reg(context) );
4529 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context) );
4531 if (INT21_FillDrivePB( drive ))
4533 SET_AL( context, 0x00 ); /* success */
4534 SET_DX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4535 context->SegDs = INT21_GetHeapSelector( context );
4537 else
4539 SET_AL( context, 0xff ); /* invalid or network drive */
4542 break;
4544 case 0x33: /* MULTIPLEXED */
4545 switch (AL_reg(context))
4547 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4548 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4549 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4550 break;
4552 case 0x01: /* SET EXTENDED BREAK STATE */
4553 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4554 DOSCONF_GetConfig()->brk_flag = (DL_reg(context) > 0) ? 1 : 0;
4555 break;
4557 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4558 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4559 /* ugly coding in order to stay reentrant */
4560 if (DL_reg(context))
4562 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4563 DOSCONF_GetConfig()->brk_flag = 1;
4565 else
4567 SET_DL( context, DOSCONF_GetConfig()->brk_flag );
4568 DOSCONF_GetConfig()->brk_flag = 0;
4570 break;
4572 case 0x05: /* GET BOOT DRIVE */
4573 TRACE("GET BOOT DRIVE\n");
4574 SET_DL( context, 3 );
4575 /* c: is Wine's bootdrive (a: is 1)*/
4576 break;
4578 case 0x06: /* GET TRUE VERSION NUMBER */
4579 TRACE("GET TRUE VERSION NUMBER\n");
4580 SET_BL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4581 SET_BH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4582 SET_DL( context, 0x00 ); /* revision */
4583 SET_DH( context, 0x08 ); /* DOS is in ROM */
4584 break;
4586 default:
4587 INT_BARF( context, 0x21 );
4588 break;
4590 break;
4592 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4593 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4594 context->SegEs = INT21_GetHeapSelector( context );
4595 SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
4596 break;
4598 case 0x35: /* GET INTERRUPT VECTOR */
4599 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4601 FARPROC16 addr;
4602 if (!ISV86(context) && DOSVM_IsWin16())
4603 addr = DOSVM_GetPMHandler16( AL_reg(context) );
4604 else
4605 addr = DOSVM_GetRMHandler( AL_reg(context) );
4606 context->SegEs = SELECTOROF(addr);
4607 SET_BX( context, OFFSETOF(addr) );
4609 break;
4611 case 0x36: /* GET FREE DISK SPACE */
4612 TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
4613 INT21_DriveName( DL_reg(context) ));
4614 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
4615 break;
4617 case 0x37: /* SWITCHAR */
4619 switch (AL_reg(context))
4621 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4622 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4623 SET_AL( context, 0x00 ); /* success*/
4624 SET_DL( context, '/' );
4625 break;
4626 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4627 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4628 DL_reg( context ));
4629 SET_AL( context, 0x00 ); /* success*/
4630 break;
4631 default:
4632 INT_BARF( context, 0x21 );
4633 break;
4636 break;
4638 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4639 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4640 if (AL_reg(context))
4642 WORD country = AL_reg(context);
4643 if (country == 0xff)
4644 country = BX_reg(context);
4645 if (country != INT21_GetSystemCountryCode()) {
4646 FIXME( "Requested info on non-default country %04x\n", country );
4647 SET_AX(context, 2);
4648 SET_CFLAG(context);
4651 if(AX_reg(context) != 2 )
4653 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context,
4654 context->SegDs,
4655 context->Edx) );
4656 SET_AX( context, INT21_GetSystemCountryCode() );
4657 SET_BX( context, INT21_GetSystemCountryCode() );
4658 RESET_CFLAG(context);
4660 break;
4662 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4663 if (!INT21_CreateDirectory( context ))
4664 bSetDOSExtendedError = TRUE;
4665 else
4666 RESET_CFLAG(context);
4667 break;
4669 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4671 WCHAR dirW[MAX_PATH];
4672 char *dirA = CTX_SEG_OFF_TO_LIN(context,
4673 context->SegDs, context->Edx);
4675 TRACE( "REMOVE DIRECTORY %s\n", dirA );
4677 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
4679 if (!RemoveDirectoryW( dirW ))
4680 bSetDOSExtendedError = TRUE;
4681 else
4682 RESET_CFLAG(context);
4684 break;
4686 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4687 if (!INT21_SetCurrentDirectory( context ))
4688 bSetDOSExtendedError = TRUE;
4689 else
4690 RESET_CFLAG(context);
4691 break;
4693 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4694 if (!INT21_CreateFile( context, context->Edx, FALSE,
4695 OF_READWRITE | OF_SHARE_COMPAT, 0x12 ))
4696 bSetDOSExtendedError = TRUE;
4697 else
4698 RESET_CFLAG(context);
4699 break;
4701 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4702 if (!INT21_CreateFile( context, context->Edx, FALSE,
4703 AL_reg(context), 0x01 ))
4704 bSetDOSExtendedError = TRUE;
4705 else
4706 RESET_CFLAG(context);
4707 break;
4709 case 0x3e: /* "CLOSE" - CLOSE FILE */
4710 TRACE( "CLOSE handle %d\n", BX_reg(context) );
4711 if (_lclose16( BX_reg(context) ) == HFILE_ERROR16)
4712 bSetDOSExtendedError = TRUE;
4713 else
4714 RESET_CFLAG(context);
4715 break;
4717 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4718 TRACE( "READ from %d to %04lX:%04X for %d bytes\n",
4719 BX_reg(context),
4720 context->SegDs,
4721 DX_reg(context),
4722 CX_reg(context) );
4724 DWORD result;
4725 WORD count = CX_reg(context);
4726 BYTE *buffer = CTX_SEG_OFF_TO_LIN( context,
4727 context->SegDs,
4728 context->Edx );
4730 /* Some programs pass a count larger than the allocated buffer */
4731 if (DOSVM_IsWin16())
4733 WORD maxcount = GetSelectorLimit16( context->SegDs )
4734 - DX_reg(context) + 1;
4735 if (count > maxcount)
4736 count = maxcount;
4740 * FIXME: Reading from console (BX=1) in DOS mode
4741 * does not work as it is supposed to work.
4744 RESET_CFLAG(context); /* set if error */
4745 if (!DOSVM_IsWin16() && BX_reg(context) == 0)
4747 result = INT21_BufferedInput( context, buffer, count );
4748 SET_AX( context, (WORD)result );
4750 else if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context)),
4751 buffer, count, &result, NULL ))
4752 SET_AX( context, (WORD)result );
4753 else
4754 bSetDOSExtendedError = TRUE;
4756 break;
4758 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4759 TRACE( "WRITE from %04lX:%04X to handle %d for %d byte\n",
4760 context->SegDs, DX_reg(context),
4761 BX_reg(context), CX_reg(context) );
4763 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4765 if (!DOSVM_IsWin16() &&
4766 (BX_reg(context) == 1 || BX_reg(context) == 2))
4768 int i;
4769 for(i=0; i<CX_reg(context); i++)
4770 DOSVM_PutChar(ptr[i]);
4771 SET_AX(context, CX_reg(context));
4772 RESET_CFLAG(context);
4774 else
4776 HFILE handle = (HFILE)DosFileHandleToWin32Handle(BX_reg(context));
4777 LONG result = _hwrite( handle, ptr, CX_reg(context) );
4778 if (result == HFILE_ERROR)
4779 bSetDOSExtendedError = TRUE;
4780 else
4782 SET_AX( context, (WORD)result );
4783 RESET_CFLAG(context);
4787 break;
4789 case 0x41: /* "UNLINK" - DELETE FILE */
4791 WCHAR fileW[MAX_PATH];
4792 char *fileA = CTX_SEG_OFF_TO_LIN(context,
4793 context->SegDs,
4794 context->Edx);
4796 TRACE( "UNLINK %s\n", fileA );
4797 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
4799 if (!DeleteFileW( fileW ))
4800 bSetDOSExtendedError = TRUE;
4801 else
4802 RESET_CFLAG(context);
4804 break;
4806 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4807 TRACE( "LSEEK handle %d offset %ld from %s\n",
4808 BX_reg(context),
4809 MAKELONG( DX_reg(context), CX_reg(context) ),
4810 (AL_reg(context) == 0) ?
4811 "start of file" : ((AL_reg(context) == 1) ?
4812 "current file position" : "end of file") );
4814 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4815 LONG offset = MAKELONG( DX_reg(context), CX_reg(context) );
4816 DWORD status = SetFilePointer( handle, offset,
4817 NULL, AL_reg(context) );
4818 if (status == INVALID_SET_FILE_POINTER)
4819 bSetDOSExtendedError = TRUE;
4820 else
4822 SET_AX( context, LOWORD(status) );
4823 SET_DX( context, HIWORD(status) );
4824 RESET_CFLAG(context);
4827 break;
4829 case 0x43: /* FILE ATTRIBUTES */
4830 if (!INT21_FileAttributes( context, AL_reg(context), FALSE ))
4831 bSetDOSExtendedError = TRUE;
4832 else
4833 RESET_CFLAG(context);
4834 break;
4836 case 0x44: /* IOCTL */
4837 INT21_Ioctl( context );
4838 break;
4840 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4841 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context) );
4843 HANDLE handle32;
4844 HFILE handle16 = HFILE_ERROR;
4846 if (DuplicateHandle( GetCurrentProcess(),
4847 DosFileHandleToWin32Handle(BX_reg(context)),
4848 GetCurrentProcess(),
4849 &handle32,
4850 0, TRUE, DUPLICATE_SAME_ACCESS ))
4851 handle16 = Win32HandleToDosFileHandle(handle32);
4853 if (handle16 == HFILE_ERROR)
4854 bSetDOSExtendedError = TRUE;
4855 else
4857 SET_AX( context, handle16 );
4858 RESET_CFLAG(context);
4861 break;
4863 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4864 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4865 BX_reg(context), CX_reg(context) );
4866 if (!INT21_Dup2(BX_reg(context), CX_reg(context)))
4867 bSetDOSExtendedError = TRUE;
4868 else
4869 RESET_CFLAG(context);
4870 break;
4872 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4873 if (!INT21_GetCurrentDirectory( context, FALSE ))
4874 bSetDOSExtendedError = TRUE;
4875 else
4876 RESET_CFLAG(context);
4877 break;
4879 case 0x48: /* ALLOCATE MEMORY */
4880 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
4882 WORD selector = 0;
4883 DWORD bytes = (DWORD)BX_reg(context) << 4;
4885 if (!ISV86(context) && DOSVM_IsWin16())
4887 DWORD rv = GlobalDOSAlloc16( bytes );
4888 selector = LOWORD( rv );
4890 else
4891 DOSMEM_GetBlock( bytes, &selector );
4893 if (selector)
4895 SET_AX( context, selector );
4896 RESET_CFLAG(context);
4898 else
4900 SET_CFLAG(context);
4901 SET_AX( context, 0x0008 ); /* insufficient memory */
4902 SET_BX( context, DOSMEM_Available() >> 4 );
4905 break;
4907 case 0x49: /* FREE MEMORY */
4908 TRACE( "FREE MEMORY segment %04lX\n", context->SegEs );
4910 BOOL ok;
4912 if (!ISV86(context) && DOSVM_IsWin16())
4914 ok = !GlobalDOSFree16( context->SegEs );
4916 /* If we don't reset ES_reg, we will fail in the relay code */
4917 if (ok)
4918 context->SegEs = 0;
4920 else
4921 ok = DOSMEM_FreeBlock( (void*)((DWORD)context->SegEs << 4) );
4923 if (!ok)
4925 TRACE("FREE MEMORY failed\n");
4926 SET_CFLAG(context);
4927 SET_AX( context, 0x0009 ); /* memory block address invalid */
4930 break;
4932 case 0x4a: /* RESIZE MEMORY BLOCK */
4933 TRACE( "RESIZE MEMORY segment %04lX to %d paragraphs\n",
4934 context->SegEs, BX_reg(context) );
4936 DWORD newsize = (DWORD)BX_reg(context) << 4;
4938 if (!ISV86(context) && DOSVM_IsWin16())
4940 FIXME( "Resize memory block - unsupported under Win16\n" );
4941 SET_CFLAG(context);
4943 else
4945 LPVOID address = (void*)((DWORD)context->SegEs << 4);
4946 UINT blocksize = DOSMEM_ResizeBlock( address, newsize, FALSE );
4948 RESET_CFLAG(context);
4949 if (blocksize == (UINT)-1)
4951 SET_CFLAG( context );
4952 SET_AX( context, 0x0009 ); /* illegal address */
4954 else if(blocksize != newsize)
4956 SET_CFLAG( context );
4957 SET_AX( context, 0x0008 ); /* insufficient memory */
4958 SET_BX( context, blocksize >> 4 ); /* new block size */
4962 break;
4964 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4966 BYTE *program = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4967 BYTE *paramblk = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx);
4969 TRACE( "EXEC %s\n", program );
4971 RESET_CFLAG(context);
4972 if (DOSVM_IsWin16())
4974 HINSTANCE16 instance = WinExec16( program, SW_NORMAL );
4975 if (instance < 32)
4977 SET_CFLAG( context );
4978 SET_AX( context, instance );
4981 else
4983 if (!MZ_Exec( context, program, AL_reg(context), paramblk))
4984 bSetDOSExtendedError = TRUE;
4987 break;
4989 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
4990 TRACE( "EXIT with return code %d\n", AL_reg(context) );
4991 if (DOSVM_IsWin16())
4992 ExitThread( AL_reg(context) );
4993 else if(ISV86(context))
4994 MZ_Exit( context, FALSE, AL_reg(context) );
4995 else
4998 * Exit from DPMI.
5000 DWORD rv = AL_reg(context);
5001 RaiseException( EXCEPTION_VM86_INTx, 0, 1, &rv );
5003 break;
5005 case 0x4d: /* GET RETURN CODE */
5006 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
5007 SET_AX( context, DOSVM_retval );
5008 DOSVM_retval = 0;
5009 break;
5011 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
5012 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
5013 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
5014 if (!INT21_FindFirst(context)) break;
5015 /* fall through */
5017 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
5018 TRACE("FINDNEXT\n");
5019 if (!INT21_FindNext(context))
5021 SetLastError( ERROR_NO_MORE_FILES );
5022 SET_AX( context, ERROR_NO_MORE_FILES );
5023 SET_CFLAG(context);
5025 else SET_AX( context, 0 ); /* OK */
5026 break;
5028 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
5029 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
5030 DOSVM_psp = BX_reg(context);
5031 break;
5033 case 0x51: /* GET PSP ADDRESS */
5034 INT21_GetPSP( context );
5035 break;
5037 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
5039 SEGPTR ptr = DOSDEV_GetLOL( ISV86(context) || !DOSVM_IsWin16() );
5040 context->SegEs = SELECTOROF(ptr);
5041 SET_BX( context, OFFSETOF(ptr) );
5043 break;
5045 case 0x54: /* Get Verify Flag */
5046 TRACE("Get Verify Flag - Not Supported\n");
5047 SET_AL( context, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
5048 break;
5050 case 0x56: /* "RENAME" - RENAME FILE */
5051 if (!INT21_RenameFile( context ))
5052 bSetDOSExtendedError = TRUE;
5053 else
5054 RESET_CFLAG(context);
5055 break;
5057 case 0x57: /* FILE DATE AND TIME */
5058 if (!INT21_FileDateTime( context ))
5059 bSetDOSExtendedError = TRUE;
5060 else
5061 RESET_CFLAG(context);
5062 break;
5064 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
5065 switch (AL_reg(context))
5067 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
5068 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
5069 SET_AX( context, 0 ); /* low memory first fit */
5070 break;
5072 case 0x01: /* SET ALLOCATION STRATEGY */
5073 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
5074 BL_reg(context) );
5075 break;
5077 case 0x02: /* GET UMB LINK STATE */
5078 TRACE( "GET UMB LINK STATE\n" );
5079 SET_AL( context, 0 ); /* UMBs not part of DOS memory chain */
5080 break;
5082 case 0x03: /* SET UMB LINK STATE */
5083 TRACE( "SET UMB LINK STATE to %d - ignored\n",
5084 BX_reg(context) );
5085 break;
5087 default:
5088 INT_BARF( context, 0x21 );
5090 break;
5092 case 0x59: /* GET EXTENDED ERROR INFO */
5093 INT21_GetExtendedError( context );
5094 break;
5096 case 0x5a: /* CREATE TEMPORARY FILE */
5097 TRACE("CREATE TEMPORARY FILE\n");
5098 bSetDOSExtendedError = !INT21_CreateTempFile(context);
5099 break;
5101 case 0x5b: /* CREATE NEW FILE */
5102 if (!INT21_CreateFile( context, context->Edx, FALSE,
5103 OF_READWRITE | OF_SHARE_COMPAT, 0x10 ))
5104 bSetDOSExtendedError = TRUE;
5105 else
5106 RESET_CFLAG(context);
5107 break;
5109 case 0x5c: /* "FLOCK" - RECORD LOCKING */
5111 DWORD offset = MAKELONG(DX_reg(context), CX_reg(context));
5112 DWORD length = MAKELONG(DI_reg(context), SI_reg(context));
5113 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
5115 RESET_CFLAG(context);
5116 switch (AL_reg(context))
5118 case 0x00: /* LOCK */
5119 TRACE( "lock handle %d offset %ld length %ld\n",
5120 BX_reg(context), offset, length );
5121 if (!LockFile( handle, offset, 0, length, 0 ))
5122 bSetDOSExtendedError = TRUE;
5123 break;
5125 case 0x01: /* UNLOCK */
5126 TRACE( "unlock handle %d offset %ld length %ld\n",
5127 BX_reg(context), offset, length );
5128 if (!UnlockFile( handle, offset, 0, length, 0 ))
5129 bSetDOSExtendedError = TRUE;
5130 break;
5132 default:
5133 INT_BARF( context, 0x21 );
5136 break;
5138 case 0x5d: /* NETWORK 5D */
5139 FIXME( "Network function 5D not implemented.\n" );
5140 SetLastError( ER_NoNetwork );
5141 bSetDOSExtendedError = TRUE;
5142 break;
5144 case 0x5e: /* NETWORK 5E */
5145 bSetDOSExtendedError = INT21_NetworkFunc( context);
5146 break;
5148 case 0x5f: /* NETWORK 5F */
5149 /* FIXME: supporting this would need to 1:
5150 * - implement per drive current directory (as kernel32 doesn't)
5151 * - assign enabled/disabled flag on a per drive basis
5153 /* network software not installed */
5154 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
5155 SetLastError( ER_NoNetwork );
5156 bSetDOSExtendedError = TRUE;
5157 break;
5159 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
5161 WCHAR pathW[MAX_PATH], res[MAX_PATH];
5162 /* FIXME: likely to be broken */
5164 TRACE("TRUENAME %s\n",
5165 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
5166 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
5167 if (!GetFullPathNameW( pathW, 128, res, NULL ))
5168 bSetDOSExtendedError = TRUE;
5169 else
5171 SET_AX( context, 0 );
5172 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
5173 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
5174 128, NULL, NULL);
5177 break;
5179 case 0x61: /* UNUSED */
5180 SET_AL( context, 0 );
5181 break;
5183 case 0x62: /* GET PSP ADDRESS */
5184 INT21_GetPSP( context );
5185 break;
5187 case 0x63: /* MISC. LANGUAGE SUPPORT */
5188 switch (AL_reg(context)) {
5189 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
5190 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
5191 context->SegDs = INT21_GetHeapSelector( context );
5192 SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
5193 SET_AL( context, 0 ); /* success */
5194 break;
5196 break;
5198 case 0x64: /* OS/2 DOS BOX */
5199 INT_BARF( context, 0x21 );
5200 SET_CFLAG(context);
5201 break;
5203 case 0x65: /* EXTENDED COUNTRY INFORMATION */
5204 INT21_ExtendedCountryInformation( context );
5205 break;
5207 case 0x66: /* GLOBAL CODE PAGE TABLE */
5208 switch (AL_reg(context))
5210 case 0x01:
5211 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
5212 SET_BX( context, GetOEMCP() );
5213 SET_DX( context, GetOEMCP() );
5214 break;
5215 case 0x02:
5216 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
5217 BX_reg(context), DX_reg(context) );
5218 break;
5220 break;
5222 case 0x67: /* SET HANDLE COUNT */
5223 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
5224 if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
5225 bSetDOSExtendedError = TRUE;
5226 break;
5228 case 0x68: /* "FFLUSH" - COMMIT FILE */
5229 TRACE( "FFLUSH - handle %d\n", BX_reg(context) );
5230 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
5231 bSetDOSExtendedError = TRUE;
5232 break;
5234 case 0x69: /* DISK SERIAL NUMBER */
5235 switch (AL_reg(context))
5237 case 0x00:
5238 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
5239 INT21_DriveName(BL_reg(context)));
5240 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
5241 else SET_AX( context, 0 );
5242 break;
5244 case 0x01:
5245 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
5246 INT21_DriveName(BL_reg(context)));
5247 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
5248 else SET_AX( context, 1 );
5249 break;
5251 break;
5253 case 0x6a: /* COMMIT FILE */
5254 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context) );
5255 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
5256 bSetDOSExtendedError = TRUE;
5257 break;
5259 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
5260 SET_AL( context, 0 );
5261 break;
5263 case 0x6c: /* EXTENDED OPEN/CREATE */
5264 if (!INT21_CreateFile( context, context->Esi, TRUE,
5265 BX_reg(context), DL_reg(context) ))
5266 bSetDOSExtendedError = TRUE;
5267 break;
5269 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
5270 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
5271 SET_CFLAG( context );
5272 SET_AL( context, 0 );
5273 break;
5275 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
5276 INT21_LongFilename( context );
5277 break;
5279 case 0x73: /* MSDOS7 - FAT32 */
5280 RESET_CFLAG( context );
5281 if (!INT21_Fat32( context ))
5282 bSetDOSExtendedError = TRUE;
5283 break;
5285 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
5286 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
5287 break;
5289 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
5290 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
5291 break;
5293 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
5294 /* we don't implement a DOS32 extender */
5295 TRACE( "DOS32 EXTENDER API - ignored\n" );
5296 break;
5298 default:
5299 INT_BARF( context, 0x21 );
5300 break;
5302 } /* END OF SWITCH */
5304 /* Set general error condition. */
5305 if (bSetDOSExtendedError)
5307 SET_AX( context, GetLastError() );
5308 SET_CFLAG( context );
5311 /* Print error code if carry flag is set. */
5312 if (context->EFlags & 0x0001)
5313 TRACE("failed, error %ld\n", GetLastError() );
5315 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
5316 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
5317 AX_reg(context), BX_reg(context),
5318 CX_reg(context), DX_reg(context),
5319 SI_reg(context), DI_reg(context),
5320 (WORD)context->SegDs, (WORD)context->SegEs,
5321 context->EFlags );