hid: Rewrite HidP_MaxUsageListLength using enum_value_caps.
[wine.git] / dlls / krnl386.exe16 / int21.c
blob5129fbd8eb6685dbb33367dff33b2475033afd01
1 /*
2 * DOS interrupt 21h handler
4 * Copyright 1993, 1994 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1997 Andreas Mohr
7 * Copyright 1998 Uwe Bonnes
8 * Copyright 1998, 1999 Ove Kaaven
9 * Copyright 2003 Thomas Mertes
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "wincon.h"
34 #include "winternl.h"
35 #include "wine/winbase16.h"
36 #include "kernel16_private.h"
37 #include "dosexe.h"
38 #include "winerror.h"
39 #include "winuser.h"
40 #include "wine/debug.h"
41 #include "wine/exception.h"
44 * Note:
45 * - Most of the file related functions are wrong. NT's kernel32
46 * doesn't maintain a per drive current directory, while DOS does.
47 * We should in fact keep track in here of those per drive
48 * directories, and use this info while dealing with partial paths
49 * (drive defined, but only relative paths). This could even be
50 * created as an array of CDS (there should be an entry for that in
51 * the LOL)
55 * Forward declarations.
57 static BOOL INT21_RenameFile( CONTEXT *context );
59 WINE_DEFAULT_DEBUG_CHANNEL(int21);
62 #include "pshpack1.h"
65 * Extended Drive Parameter Block.
66 * This structure is compatible with standard DOS4+ DPB and
67 * extended DOS7 DPB.
69 typedef struct _INT21_DPB {
70 BYTE drive; /* 00 drive number (0=A, ...) */
71 BYTE unit; /* 01 unit number within device driver */
72 WORD sector_bytes; /* 02 bytes per sector */
73 BYTE cluster_sectors; /* 04 highest sector number within a cluster */
74 BYTE shift; /* 05 shift count to convert clusters into sectors */
75 WORD num_reserved; /* 06 reserved sectors at beginning of drive */
76 BYTE num_FAT; /* 08 number of FATs */
77 WORD num_root_entries; /* 09 number of root directory entries */
78 WORD first_data_sector; /* 0b number of first sector containing user data */
79 WORD num_clusters1; /* 0d highest cluster number (number of data clusters + 1) */
80 WORD sectors_per_FAT; /* 0f number of sectors per FAT */
81 WORD first_dir_sector; /* 11 sector number of first directory sector */
82 SEGPTR driver_header; /* 13 address of device driver header */
83 BYTE media_ID; /* 17 media ID byte */
84 BYTE access_flag; /* 18 0x00 if disk accessed, 0xff if not */
85 SEGPTR next; /* 19 pointer to next DPB */
86 WORD search_cluster1; /* 1d cluster at which to start search for free space */
87 WORD free_clusters_lo; /* 1f number of free clusters on drive or 0xffff if unknown */
88 WORD free_clusters_hi; /* 21 hiword of clusters_free */
89 WORD mirroring_flags; /* 23 active FAT/mirroring flags */
90 WORD info_sector; /* 25 sector number of file system info sector or 0xffff for none */
91 WORD spare_boot_sector; /* 27 sector number of backup boot sector or 0xffff for none */
92 DWORD first_cluster_sector; /* 29 sector number of the first cluster */
93 DWORD num_clusters2; /* 2d maximum cluster number */
94 DWORD fat_clusters; /* 31 number of clusters occupied by FAT */
95 DWORD root_cluster; /* 35 cluster number of start of root directory */
96 DWORD search_cluster2; /* 39 cluster at which to start searching for free space */
97 } INT21_DPB;
101 * Structure for DOS data that can be accessed directly from applications.
102 * Real and protected mode pointers will be returned to this structure so
103 * the structure must be correctly packed.
105 typedef struct _INT21_HEAP {
106 WORD uppercase_size; /* Size of the following table in bytes */
107 BYTE uppercase_table[128]; /* Uppercase equivalents of chars from 0x80 to 0xff. */
109 WORD lowercase_size; /* Size of the following table in bytes */
110 BYTE lowercase_table[256]; /* Lowercase equivalents of chars from 0x00 to 0xff. */
112 WORD collating_size; /* Size of the following table in bytes */
113 BYTE collating_table[256]; /* Values used to sort characters from 0x00 to 0xff. */
115 WORD filename_size; /* Size of the following filename data in bytes */
116 BYTE filename_reserved1; /* 0x01 for MS-DOS 3.30-6.00 */
117 BYTE filename_lowest; /* Lowest permissible character value for filename */
118 BYTE filename_highest; /* Highest permissible character value for filename */
119 BYTE filename_reserved2; /* 0x00 for MS-DOS 3.30-6.00 */
120 BYTE filename_exclude_first; /* First illegal character in permissible range */
121 BYTE filename_exclude_last; /* Last illegal character in permissible range */
122 BYTE filename_reserved3; /* 0x02 for MS-DOS 3.30-6.00 */
123 BYTE filename_illegal_size; /* Number of terminators in the following table */
124 BYTE filename_illegal_table[16]; /* Characters which terminate a filename */
126 WORD dbcs_size; /* Number of valid ranges in the following table */
127 BYTE dbcs_table[16]; /* Start/end bytes for N ranges and 00/00 as terminator */
129 BYTE misc_indos; /* Interrupt 21 nesting flag */
130 WORD misc_selector; /* Protected mode selector for INT21_HEAP */
131 INT21_DPB misc_dpb_list[MAX_DOS_DRIVES]; /* Drive parameter blocks for all drives */
133 } INT21_HEAP;
136 struct FCB {
137 BYTE drive_number;
138 CHAR file_name[8];
139 CHAR file_extension[3];
140 WORD current_block_number;
141 WORD logical_record_size;
142 DWORD file_size;
143 WORD date_of_last_write;
144 WORD time_of_last_write;
145 BYTE file_number;
146 BYTE attributes;
147 WORD starting_cluster;
148 WORD sequence_number;
149 BYTE file_attributes;
150 BYTE unused;
151 BYTE record_within_current_block;
152 BYTE random_access_record_number[4];
156 struct XFCB {
157 BYTE xfcb_signature;
158 BYTE reserved[5];
159 BYTE xfcb_file_attribute;
160 BYTE fcb[37];
163 /* DTA layout for FindFirst/FindNext */
164 typedef struct
166 BYTE drive; /* 00 drive letter */
167 char mask[11]; /* 01 search template */
168 BYTE search_attr; /* 0c search attributes */
169 WORD count; /* 0d entry count within directory */
170 WORD cluster; /* 0f cluster of parent directory */
171 WCHAR *fullPath; /* 11 full path (was: reserved) */
172 BYTE fileattr; /* 15 file attributes */
173 WORD filetime; /* 16 file time */
174 WORD filedate; /* 18 file date */
175 DWORD filesize; /* 1a file size */
176 char filename[13]; /* 1e file name + extension */
177 } FINDFILE_DTA;
179 /* FCB layout for FindFirstFCB/FindNextFCB */
180 typedef struct
182 BYTE drive; /* 00 drive letter */
183 char filename[11]; /* 01 filename 8+3 format */
184 int count; /* 0c entry count (was: reserved) */
185 WCHAR *fullPath; /* 10 full path (was: reserved) */
186 } FINDFILE_FCB;
188 /* DOS directory entry for FindFirstFCB/FindNextFCB */
189 typedef struct
191 char filename[11]; /* 00 filename 8+3 format */
192 BYTE fileattr; /* 0b file attributes */
193 BYTE reserved[10]; /* 0c reserved */
194 WORD filetime; /* 16 file time */
195 WORD filedate; /* 18 file date */
196 WORD cluster; /* 1a file first cluster */
197 DWORD filesize; /* 1c file size */
198 } DOS_DIRENTRY_LAYOUT;
200 #include "poppack.h"
202 /* dos file attributes */
203 #define FA_NORMAL 0x00 /* Normal file, no attributes */
204 #define FA_RDONLY 0x01 /* Read only attribute */
205 #define FA_HIDDEN 0x02 /* Hidden file */
206 #define FA_SYSTEM 0x04 /* System file */
207 #define FA_LABEL 0x08 /* Volume label */
208 #define FA_DIRECTORY 0x10 /* Directory */
209 #define FA_ARCHIVE 0x20 /* Archive */
210 #define FA_UNUSED 0x40 /* Unused */
212 /* Error codes */
213 #define ER_NoNetwork 0x49
215 /* Error classes */
216 #define EC_OutOfResource 0x01
217 #define EC_Temporary 0x02
218 #define EC_AccessDenied 0x03
219 #define EC_InternalError 0x04
220 #define EC_HardwareFailure 0x05
221 #define EC_SystemFailure 0x06
222 #define EC_ProgramError 0x07
223 #define EC_NotFound 0x08
224 #define EC_MediaError 0x0b
225 #define EC_Exists 0x0c
226 #define EC_Unknown 0x0d
228 /* Suggested actions */
229 #define SA_Retry 0x01
230 #define SA_DelayedRetry 0x02
231 #define SA_Abort 0x04
232 #define SA_Ignore 0x06
233 #define SA_Ask4Retry 0x07
235 /* Error locus */
236 #define EL_Unknown 0x01
237 #define EL_Disk 0x02
238 #define EL_Network 0x03
239 #define EL_Serial 0x04
240 #define EL_Memory 0x05
242 /* BIOS Keyboard Scancodes */
243 #define KEY_LEFT 0x4B
244 #define KEY_RIGHT 0x4D
245 #define KEY_UP 0x48
246 #define KEY_DOWN 0x50
247 #define KEY_IC 0x52 /* insert char */
248 #define KEY_DC 0x53 /* delete char */
249 #define KEY_BACKSPACE 0x0E
250 #define KEY_HOME 0x47
251 #define KEY_END 0x4F
252 #define KEY_NPAGE 0x49
253 #define KEY_PPAGE 0x51
255 static int brk_flag;
257 static BYTE drive_number( WCHAR letter )
259 if (letter >= 'A' && letter <= 'Z') return letter - 'A';
260 if (letter >= 'a' && letter <= 'z') return letter - 'a';
261 return MAX_DOS_DRIVES;
265 /* Many calls translate a drive argument like this:
266 drive number (00h = default, 01h = A:, etc)
268 /******************************************************************
269 * INT21_DriveName
271 * Many calls translate a drive argument like this:
272 * drive number (00h = default, 01h = A:, etc)
274 static const char *INT21_DriveName(int drive)
276 if (drive > 0)
278 if (drive <= 26) return wine_dbg_sprintf("%c:", 'A' + drive - 1);
279 else return wine_dbg_sprintf( "<Bad drive: %d>", drive);
281 return "default";
284 /***********************************************************************
285 * INT21_GetCurrentDrive
287 * Return current drive using scheme (0=A:, 1=B:, 2=C:, ...) or
288 * MAX_DOS_DRIVES on error.
290 static BYTE INT21_GetCurrentDrive(void)
292 WCHAR current_directory[MAX_PATH];
294 if (!GetCurrentDirectoryW( MAX_PATH, current_directory ) ||
295 current_directory[1] != ':')
297 TRACE( "Failed to get current drive.\n" );
298 return MAX_DOS_DRIVES;
300 return drive_number( current_directory[0] );
304 /***********************************************************************
305 * INT21_MapDrive
307 * Convert drive number from scheme (0=default, 1=A:, 2=B:, ...) into
308 * scheme (0=A:, 1=B:, 2=C:, ...) or MAX_DOS_DRIVES on error.
310 static BYTE INT21_MapDrive( BYTE drive )
312 if (drive)
314 WCHAR drivespec[] = {'A', ':', 0};
315 UINT drivetype;
317 drivespec[0] += drive - 1;
318 drivetype = GetDriveTypeW( drivespec );
320 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
321 return MAX_DOS_DRIVES;
323 return drive - 1;
326 return INT21_GetCurrentDrive();
330 /***********************************************************************
331 * INT21_SetCurrentDrive
333 * Set current drive. Uses scheme (0=A:, 1=B:, 2=C:, ...).
335 static void INT21_SetCurrentDrive( BYTE drive )
337 WCHAR drivespec[] = {'A', ':', 0};
339 drivespec[0] += drive;
341 if (!SetCurrentDirectoryW( drivespec ))
342 TRACE( "Failed to set current drive.\n" );
346 /***********************************************************************
347 * INT21_GetSystemCountryCode
349 * Return DOS country code for default system locale.
351 static WORD INT21_GetSystemCountryCode( void )
353 return GetSystemDefaultLangID();
357 /***********************************************************************
358 * INT21_FillCountryInformation
360 * Fill 34-byte buffer with country information data using
361 * default system locale.
363 static void INT21_FillCountryInformation( BYTE *buffer )
365 /* 00 - WORD: date format
366 * 00 = mm/dd/yy
367 * 01 = dd/mm/yy
368 * 02 = yy/mm/dd
370 *(WORD*)(buffer + 0) = 0; /* FIXME: Get from locale */
372 /* 02 - BYTE[5]: ASCIIZ currency symbol string */
373 buffer[2] = '$'; /* FIXME: Get from locale */
374 buffer[3] = 0;
376 /* 07 - BYTE[2]: ASCIIZ thousands separator */
377 buffer[7] = 0; /* FIXME: Get from locale */
378 buffer[8] = 0;
380 /* 09 - BYTE[2]: ASCIIZ decimal separator */
381 buffer[9] = '.'; /* FIXME: Get from locale */
382 buffer[10] = 0;
384 /* 11 - BYTE[2]: ASCIIZ date separator */
385 buffer[11] = '/'; /* FIXME: Get from locale */
386 buffer[12] = 0;
388 /* 13 - BYTE[2]: ASCIIZ time separator */
389 buffer[13] = ':'; /* FIXME: Get from locale */
390 buffer[14] = 0;
392 /* 15 - BYTE: Currency format
393 * bit 2 = set if currency symbol replaces decimal point
394 * bit 1 = number of spaces between value and currency symbol
395 * bit 0 = 0 if currency symbol precedes value
396 * 1 if currency symbol follows value
398 buffer[15] = 0; /* FIXME: Get from locale */
400 /* 16 - BYTE: Number of digits after decimal in currency */
401 buffer[16] = 0; /* FIXME: Get from locale */
403 /* 17 - BYTE: Time format
404 * bit 0 = 0 if 12-hour clock
405 * 1 if 24-hour clock
407 buffer[17] = 1; /* FIXME: Get from locale */
409 /* 18 - DWORD: Address of case map routine */
410 *(DWORD*)(buffer + 18) = 0; /* FIXME: ptr to case map routine */
412 /* 22 - BYTE[2]: ASCIIZ data-list separator */
413 buffer[22] = ','; /* FIXME: Get from locale */
414 buffer[23] = 0;
416 /* 24 - BYTE[10]: Reserved */
417 memset( buffer + 24, 0, 10 );
421 /***********************************************************************
422 * INT21_FillHeap
424 * Initialize DOS heap.
426 * Filename Terminator Table of w2k DE NTVDM:
427 * 16 00 01 00 FF 00 00 20-02 0E 2E 22 2F 5C 5B 5D ....... ..."/\[]
428 * 3A 7C 3C 3E 2B 3D 3B 2C-00 :|<>+=;,
430 static void INT21_FillHeap( INT21_HEAP *heap )
432 static const char terminators[] = ".\"/\\[]:|<>+=;,";
433 int i;
436 * Uppercase table.
438 heap->uppercase_size = 128;
439 for (i = 0; i < 128; i++)
440 heap->uppercase_table[i] = toupper( 128 + i );
443 * Lowercase table.
445 heap->lowercase_size = 256;
446 for (i = 0; i < 256; i++)
447 heap->lowercase_table[i] = tolower( i );
450 * Collating table.
452 heap->collating_size = 256;
453 for (i = 0; i < 256; i++)
454 heap->collating_table[i] = i;
457 * Filename table.
459 heap->filename_size = 8 + strlen(terminators);
460 heap->filename_illegal_size = strlen(terminators);
461 memcpy( heap->filename_illegal_table, terminators, heap->filename_illegal_size );
463 heap->filename_reserved1 = 0x01;
464 heap->filename_lowest = 0x00;
465 heap->filename_highest = 0xff;
466 heap->filename_reserved2 = 0x00;
467 heap->filename_exclude_first = 0x00;
468 heap->filename_exclude_last = 0x20;
469 heap->filename_reserved3 = 0x02;
472 * DBCS lead byte table. This table is empty.
474 heap->dbcs_size = 0;
475 memset( heap->dbcs_table, 0, sizeof(heap->dbcs_table) );
478 * Initialize InDos flag.
480 heap->misc_indos = 0;
483 * FIXME: Should drive parameter blocks (DPB) be
484 * initialized here and linked to DOS LOL?
489 /***********************************************************************
490 * INT21_GetHeapPointer
492 * Get pointer for DOS heap (INT21_HEAP).
493 * Creates and initializes heap on first call.
495 static INT21_HEAP *INT21_GetHeapPointer( void )
497 static INT21_HEAP *heap_pointer = NULL;
499 if (!heap_pointer)
501 WORD heap_selector = GlobalAlloc16( GMEM_FIXED, sizeof(INT21_HEAP) );
502 heap_pointer = GlobalLock16( heap_selector );
503 heap_pointer->misc_selector = heap_selector;
504 INT21_FillHeap( heap_pointer );
507 return heap_pointer;
511 /***********************************************************************
512 * INT21_GetHeapSelector
514 * Get segment/selector for DOS heap (INT21_HEAP).
515 * Creates and initializes heap on first call.
517 static WORD INT21_GetHeapSelector( CONTEXT *context )
519 INT21_HEAP *heap = INT21_GetHeapPointer();
520 return heap->misc_selector;
524 /***********************************************************************
525 * INT21_FillDrivePB
527 * Fill DOS heap drive parameter block for the specified drive.
528 * Return TRUE if drive was valid and there were
529 * no errors while reading drive information.
531 static BOOL INT21_FillDrivePB( BYTE drive )
533 WCHAR drivespec[] = {'A', ':', 0};
534 INT21_HEAP *heap = INT21_GetHeapPointer();
535 INT21_DPB *dpb;
536 UINT drivetype;
537 DWORD cluster_sectors;
538 DWORD sector_bytes;
539 DWORD free_clusters;
540 DWORD total_clusters;
542 if (drive >= MAX_DOS_DRIVES)
543 return FALSE;
545 dpb = &heap->misc_dpb_list[drive];
546 drivespec[0] += drive;
547 drivetype = GetDriveTypeW( drivespec );
550 * FIXME: Does this check work correctly with floppy/cdrom drives?
552 if (drivetype == DRIVE_NO_ROOT_DIR || drivetype == DRIVE_UNKNOWN)
553 return FALSE;
556 * FIXME: Does this check work correctly with floppy/cdrom drives?
558 if (!GetDiskFreeSpaceW( drivespec, &cluster_sectors, &sector_bytes,
559 &free_clusters, &total_clusters ))
560 return FALSE;
563 * FIXME: Most of the values listed below are incorrect.
564 * All values should be validated.
567 dpb->drive = drive;
568 dpb->unit = 0;
569 dpb->sector_bytes = sector_bytes;
570 dpb->cluster_sectors = cluster_sectors - 1;
572 dpb->shift = 0;
573 while (cluster_sectors > 1)
575 cluster_sectors /= 2;
576 dpb->shift++;
579 dpb->num_reserved = 0;
580 dpb->num_FAT = 1;
581 dpb->num_root_entries = 2;
582 dpb->first_data_sector = 2;
583 dpb->num_clusters1 = total_clusters;
584 dpb->sectors_per_FAT = 1;
585 dpb->first_dir_sector = 1;
586 dpb->driver_header = 0;
587 dpb->media_ID = (drivetype == DRIVE_FIXED) ? 0xF8 : 0xF0;
588 dpb->access_flag = 0;
589 dpb->next = 0;
590 dpb->search_cluster1 = 0;
591 dpb->free_clusters_lo = LOWORD(free_clusters);
592 dpb->free_clusters_hi = HIWORD(free_clusters);
593 dpb->mirroring_flags = 0;
594 dpb->info_sector = 0xffff;
595 dpb->spare_boot_sector = 0xffff;
596 dpb->first_cluster_sector = 0;
597 dpb->num_clusters2 = total_clusters;
598 dpb->fat_clusters = 32;
599 dpb->root_cluster = 0;
600 dpb->search_cluster2 = 0;
602 return TRUE;
606 /***********************************************************************
607 * INT21_GetCurrentDirectory
609 * Handler for:
610 * - function 0x47
611 * - subfunction 0x47 of function 0x71
613 static BOOL INT21_GetCurrentDirectory( CONTEXT *context, BOOL islong )
615 char *buffer = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi);
616 BYTE drive = INT21_MapDrive( DL_reg(context) );
617 WCHAR pathW[MAX_PATH];
618 char pathA[MAX_PATH];
619 WCHAR *ptr = pathW;
621 TRACE( "drive %d\n", DL_reg(context) );
623 if (drive == MAX_DOS_DRIVES)
625 SetLastError(ERROR_INVALID_DRIVE);
626 return FALSE;
630 * Grab current directory.
633 if (!GetCurrentDirectoryW( MAX_PATH, pathW )) return FALSE;
635 if (drive_number( pathW[0] ) != drive || pathW[1] != ':')
637 /* cwd is not on the requested drive, get the environment string instead */
639 WCHAR env_var[4];
641 env_var[0] = '=';
642 env_var[1] = 'A' + drive;
643 env_var[2] = ':';
644 env_var[3] = 0;
645 if (!GetEnvironmentVariableW( env_var, pathW, MAX_PATH ))
647 /* return empty path */
648 buffer[0] = 0;
649 return TRUE;
654 * Convert into short format.
657 if (!islong)
659 DWORD result = GetShortPathNameW( pathW, pathW, MAX_PATH );
660 if (!result)
661 return FALSE;
662 if (result > MAX_PATH)
664 WARN( "Short path too long!\n" );
665 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
666 return FALSE;
671 * The returned pathname does not include
672 * the drive letter, colon or leading backslash.
675 if (ptr[0] == '\\')
678 * FIXME: We should probably just strip host part from name...
680 FIXME( "UNC names are not supported.\n" );
681 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
682 return FALSE;
684 else if (!ptr[0] || ptr[1] != ':' || ptr[2] != '\\')
686 WARN( "Path is neither UNC nor DOS path: %s\n",
687 wine_dbgstr_w(ptr) );
688 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
689 return FALSE;
691 else
693 /* Remove drive letter, colon and leading backslash. */
694 ptr += 3;
698 * Convert into OEM string.
701 if (!WideCharToMultiByte(CP_OEMCP, 0, ptr, -1, pathA,
702 MAX_PATH, NULL, NULL))
704 WARN( "Cannot convert path!\n" );
705 SetLastError(ERROR_NETWORK_BUSY); /* Internal Wine error. */
706 return FALSE;
710 * Success.
713 if (!islong)
715 /* Undocumented success code. */
716 SET_AX( context, 0x0100 );
718 /* Truncate buffer to 64 bytes. */
719 pathA[63] = 0;
722 TRACE( "%c:=%s\n", 'A' + drive, pathA );
724 strcpy( buffer, pathA );
725 return TRUE;
729 /***********************************************************************
730 * INT21_SetCurrentDirectory
732 * Handler for:
733 * - function 0x3b
734 * - subfunction 0x3b of function 0x71
736 static BOOL INT21_SetCurrentDirectory( CONTEXT *context )
738 WCHAR dirW[MAX_PATH];
739 WCHAR env_var[4];
740 DWORD attr;
741 char *dirA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
742 BYTE drive = INT21_GetCurrentDrive();
743 BOOL result;
745 TRACE( "SET CURRENT DIRECTORY %s\n", dirA );
747 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
748 if (!GetFullPathNameW( dirW, MAX_PATH, dirW, NULL )) return FALSE;
750 attr = GetFileAttributesW( dirW );
751 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
753 SetLastError( ERROR_PATH_NOT_FOUND );
754 return FALSE;
757 env_var[0] = '=';
758 env_var[1] = dirW[0];
759 env_var[2] = ':';
760 env_var[3] = 0;
761 result = SetEnvironmentVariableW( env_var, dirW );
763 /* only set current directory if on the current drive */
764 if (result && (drive_number( dirW[0] ) == drive)) result = SetCurrentDirectoryW( dirW );
766 return result;
769 /***********************************************************************
770 * INT21_CreateFile
772 * Handler for:
773 * - function 0x3c
774 * - function 0x3d
775 * - function 0x5b
776 * - function 0x6c
777 * - subfunction 0x6c of function 0x71
779 static BOOL INT21_CreateFile( CONTEXT *context,
780 DWORD pathSegOff,
781 BOOL returnStatus,
782 WORD dosAccessShare,
783 BYTE dosAction )
785 WORD dosStatus;
786 char *pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, pathSegOff);
787 WCHAR pathW[MAX_PATH];
788 DWORD winAccess;
789 DWORD winAttributes;
790 HANDLE winHandle;
791 DWORD winMode;
792 DWORD winSharing;
794 TRACE( "CreateFile called: function=%02x, action=%02x, access/share=%04x, "
795 "create flags=%04x, file=%s.\n",
796 AH_reg(context), dosAction, dosAccessShare, CX_reg(context), pathA );
799 * Application tried to create/open a file whose name
800 * ends with a backslash. This is not allowed.
802 * FIXME: This needs to be validated, especially the return value.
804 if (pathA[strlen(pathA) - 1] == '/')
806 SetLastError( ERROR_FILE_NOT_FOUND );
807 return FALSE;
811 * Convert DOS action flags into Win32 creation disposition parameter.
813 switch(dosAction)
815 case 0x01:
816 winMode = OPEN_EXISTING;
817 break;
818 case 0x02:
819 winMode = TRUNCATE_EXISTING;
820 break;
821 case 0x10:
822 winMode = CREATE_NEW;
823 break;
824 case 0x11:
825 winMode = OPEN_ALWAYS;
826 break;
827 case 0x12:
828 winMode = CREATE_ALWAYS;
829 break;
830 default:
831 SetLastError( ERROR_INVALID_PARAMETER );
832 return FALSE;
836 * Convert DOS access/share flags into Win32 desired access parameter.
838 switch(dosAccessShare & 0x07)
840 case OF_READ:
841 winAccess = GENERIC_READ;
842 break;
843 case OF_WRITE:
844 winAccess = GENERIC_WRITE;
845 break;
846 case OF_READWRITE:
847 winAccess = GENERIC_READ | GENERIC_WRITE;
848 break;
849 case 0x04:
851 * Read-only, do not modify file's last-access time (DOS7).
853 * FIXME: How to prevent modification of last-access time?
855 winAccess = GENERIC_READ;
856 break;
857 default:
858 winAccess = 0;
862 * Convert DOS access/share flags into Win32 share mode parameter.
864 switch(dosAccessShare & 0x70)
866 case OF_SHARE_EXCLUSIVE:
867 winSharing = 0;
868 break;
869 case OF_SHARE_DENY_WRITE:
870 winSharing = FILE_SHARE_READ;
871 break;
872 case OF_SHARE_DENY_READ:
873 winSharing = FILE_SHARE_WRITE;
874 break;
875 case OF_SHARE_DENY_NONE:
876 case OF_SHARE_COMPAT:
877 default:
878 winSharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
882 * FIXME: Bit (dosAccessShare & 0x80) represents inheritance.
883 * What to do with this bit?
884 * FIXME: Bits in the high byte of dosAccessShare are not supported.
885 * See both function 0x6c and subfunction 0x6c of function 0x71 for
886 * definition of these bits.
890 * Convert DOS create attributes into Win32 flags and attributes parameter.
892 if (winMode == OPEN_EXISTING || winMode == TRUNCATE_EXISTING)
894 winAttributes = 0;
896 else
898 WORD dosAttributes = CX_reg(context);
900 if (dosAttributes & FA_LABEL)
903 * Application tried to create volume label entry.
904 * This is difficult to support so we do not allow it.
906 * FIXME: If volume does not already have a label,
907 * this function is supposed to succeed.
909 SetLastError( ERROR_ACCESS_DENIED );
910 return TRUE;
913 winAttributes = dosAttributes &
914 (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
915 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE);
919 * Open the file.
921 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
923 winHandle = CreateFileW( pathW, winAccess, winSharing, NULL, winMode, winAttributes, 0 );
924 /* DOS allows opening files on a CDROM R/W */
925 if( winHandle == INVALID_HANDLE_VALUE &&
926 (GetLastError() == ERROR_WRITE_PROTECT ||
927 GetLastError() == ERROR_ACCESS_DENIED)) {
928 winHandle = CreateFileW( pathW, winAccess & ~GENERIC_WRITE,
929 winSharing, NULL, winMode, winAttributes, 0 );
932 if (winHandle == INVALID_HANDLE_VALUE)
933 return FALSE;
936 * Determine DOS file status.
938 * 1 = file opened
939 * 2 = file created
940 * 3 = file replaced
942 switch(winMode)
944 case OPEN_EXISTING:
945 dosStatus = 1;
946 break;
947 case TRUNCATE_EXISTING:
948 dosStatus = 3;
949 break;
950 case CREATE_NEW:
951 dosStatus = 2;
952 break;
953 case OPEN_ALWAYS:
954 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 1 : 2;
955 break;
956 case CREATE_ALWAYS:
957 dosStatus = (GetLastError() == ERROR_ALREADY_EXISTS) ? 3 : 2;
958 break;
959 default:
960 dosStatus = 0;
964 * Return DOS file handle and DOS status.
966 SET_AX( context, Win32HandleToDosFileHandle(winHandle) );
968 if (returnStatus)
969 SET_CX( context, dosStatus );
971 TRACE( "CreateFile finished: handle=%d, status=%d.\n",
972 AX_reg(context), dosStatus );
974 return TRUE;
978 /***********************************************************************
979 * INT21_GetCurrentDTA
981 static BYTE *INT21_GetCurrentDTA( CONTEXT *context )
983 TDB *pTask = GlobalLock16(GetCurrentTask());
985 /* FIXME: This assumes DTA was set correctly! */
986 return CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
987 OFFSETOF(pTask->dta) );
991 /***********************************************************************
992 * INT21_OpenFileUsingFCB
994 * Handler for function 0x0f.
996 * PARAMS
997 * DX:DX [I/O] File control block (FCB or XFCB) of unopened file
999 * RETURNS (in AL)
1000 * 0x00: successful
1001 * 0xff: failed
1003 * NOTES
1004 * Opens a FCB file for read/write in compatibility mode. Upon calling
1005 * the FCB must have the drive_number, file_name, and file_extension
1006 * fields filled and all other bytes cleared.
1008 static void INT21_OpenFileUsingFCB( CONTEXT *context )
1010 struct FCB *fcb;
1011 struct XFCB *xfcb;
1012 char file_path[16];
1013 char *pos;
1014 HANDLE handle;
1015 HFILE16 hfile16;
1016 BY_HANDLE_FILE_INFORMATION info;
1017 BYTE AL_result;
1019 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1020 if (fcb->drive_number == 0xff) {
1021 xfcb = (struct XFCB *) fcb;
1022 fcb = (struct FCB *) xfcb->fcb;
1023 } /* if */
1025 AL_result = 0;
1026 file_path[0] = 'A' + INT21_MapDrive( fcb->drive_number );
1028 if (AL_result == 0) {
1029 file_path[1] = ':';
1030 pos = &file_path[2];
1031 memcpy(pos, fcb->file_name, 8);
1032 pos[8] = ' ';
1033 pos[9] = '\0';
1034 pos = strchr(pos, ' ');
1035 *pos = '.';
1036 pos++;
1037 memcpy(pos, fcb->file_extension, 3);
1038 pos[3] = ' ';
1039 pos[4] = '\0';
1040 pos = strchr(pos, ' ');
1041 *pos = '\0';
1043 handle = (HANDLE) _lopen(file_path, OF_READWRITE);
1044 if (handle == INVALID_HANDLE_VALUE) {
1045 TRACE("_lopen(\"%s\") failed: INVALID_HANDLE_VALUE\n", file_path);
1046 AL_result = 0xff; /* failed */
1047 } else {
1048 hfile16 = Win32HandleToDosFileHandle(handle);
1049 if (hfile16 == HFILE_ERROR16) {
1050 TRACE("Win32HandleToDosFileHandle(%p) failed: HFILE_ERROR\n", handle);
1051 CloseHandle(handle);
1052 AL_result = 0xff; /* failed */
1053 } else if (hfile16 > 255) {
1054 TRACE("hfile16 (=%d) larger than 255 for \"%s\"\n", hfile16, file_path);
1055 _lclose16(hfile16);
1056 AL_result = 0xff; /* failed */
1057 } else {
1058 if (!GetFileInformationByHandle(handle, &info)) {
1059 TRACE("GetFileInformationByHandle(%d, %p) for \"%s\" failed\n",
1060 hfile16, handle, file_path);
1061 _lclose16(hfile16);
1062 AL_result = 0xff; /* failed */
1063 } else {
1064 fcb->drive_number = file_path[0] - 'A' + 1;
1065 fcb->current_block_number = 0;
1066 fcb->logical_record_size = 128;
1067 fcb->file_size = info.nFileSizeLow;
1068 FileTimeToDosDateTime(&info.ftLastWriteTime,
1069 &fcb->date_of_last_write, &fcb->time_of_last_write);
1070 fcb->file_number = hfile16;
1071 fcb->attributes = 0xc2;
1072 fcb->starting_cluster = 0; /* don't know correct init value */
1073 fcb->sequence_number = 0; /* don't know correct init value */
1074 fcb->file_attributes = info.dwFileAttributes;
1075 /* The following fields are not initialized */
1076 /* by the native function: */
1077 /* unused */
1078 /* record_within_current_block */
1079 /* random_access_record_number */
1081 TRACE("successful opened file \"%s\" as %d (handle %p)\n",
1082 file_path, hfile16, handle);
1083 AL_result = 0x00; /* successful */
1084 } /* if */
1085 } /* if */
1086 } /* if */
1087 } /* if */
1088 SET_AL(context, AL_result);
1092 /***********************************************************************
1093 * INT21_CloseFileUsingFCB
1095 * Handler for function 0x10.
1097 * PARAMS
1098 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1100 * RETURNS (in AL)
1101 * 0x00: successful
1102 * 0xff: failed
1104 * NOTES
1105 * Closes a FCB file.
1107 static void INT21_CloseFileUsingFCB( CONTEXT *context )
1109 struct FCB *fcb;
1110 struct XFCB *xfcb;
1111 BYTE AL_result;
1113 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1114 if (fcb->drive_number == 0xff) {
1115 xfcb = (struct XFCB *) fcb;
1116 fcb = (struct FCB *) xfcb->fcb;
1117 } /* if */
1119 if (_lclose16((HFILE16) fcb->file_number) != 0) {
1120 TRACE("_lclose16(%d) failed\n", fcb->file_number);
1121 AL_result = 0xff; /* failed */
1122 } else {
1123 TRACE("successful closed file %d\n", fcb->file_number);
1124 AL_result = 0x00; /* successful */
1125 } /* if */
1126 SET_AL(context, AL_result);
1130 /***********************************************************************
1131 * INT21_SequentialReadFromFCB
1133 * Handler for function 0x14.
1135 * PARAMS
1136 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1138 * RETURNS (in AL)
1139 * 0: successful
1140 * 1: end of file, no data read
1141 * 2: segment wrap in DTA, no data read (not returned now)
1142 * 3: end of file, partial record read
1144 * NOTES
1145 * Reads a record with the size FCB->logical_record_size from the FCB
1146 * to the disk transfer area. The position of the record is specified
1147 * with FCB->current_block_number and FCB->record_within_current_block.
1148 * Then FCB->current_block_number and FCB->record_within_current_block
1149 * are updated to point to the next record. If a partial record is
1150 * read, it is filled with zeros up to the FCB->logical_record_size.
1152 static void INT21_SequentialReadFromFCB( CONTEXT *context )
1154 struct FCB *fcb;
1155 struct XFCB *xfcb;
1156 HANDLE handle;
1157 DWORD record_number;
1158 DWORD position;
1159 BYTE *disk_transfer_area;
1160 UINT bytes_read;
1161 BYTE AL_result;
1163 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1164 if (fcb->drive_number == 0xff) {
1165 xfcb = (struct XFCB *) fcb;
1166 fcb = (struct FCB *) xfcb->fcb;
1167 } /* if */
1169 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1170 if (handle == INVALID_HANDLE_VALUE) {
1171 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1172 fcb->file_number);
1173 AL_result = 0x01; /* end of file, no data read */
1174 } else {
1175 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1176 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1177 if (position != record_number * fcb->logical_record_size) {
1178 TRACE("seek(%d, %d, 0) failed with %u\n",
1179 fcb->file_number, record_number * fcb->logical_record_size, position);
1180 AL_result = 0x01; /* end of file, no data read */
1181 } else {
1182 disk_transfer_area = INT21_GetCurrentDTA(context);
1183 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1184 if (bytes_read != fcb->logical_record_size) {
1185 TRACE("_lread(%d, %p, %d) failed with %d\n",
1186 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1187 if (bytes_read == 0) {
1188 AL_result = 0x01; /* end of file, no data read */
1189 } else {
1190 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1191 AL_result = 0x03; /* end of file, partial record read */
1192 } /* if */
1193 } else {
1194 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1195 bytes_read, record_number, position, fcb->file_number, handle);
1196 AL_result = 0x00; /* successful */
1197 } /* if */
1198 } /* if */
1199 } /* if */
1200 if (AL_result == 0x00 || AL_result == 0x03) {
1201 if (fcb->record_within_current_block == 127) {
1202 fcb->record_within_current_block = 0;
1203 fcb->current_block_number++;
1204 } else {
1205 fcb->record_within_current_block++;
1206 } /* if */
1207 } /* if */
1208 SET_AL(context, AL_result);
1212 /***********************************************************************
1213 * INT21_SequentialWriteToFCB
1215 * Handler for function 0x15.
1217 * PARAMS
1218 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1220 * RETURNS (in AL)
1221 * 0: successful
1222 * 1: disk full
1223 * 2: segment wrap in DTA (not returned now)
1225 * NOTES
1226 * Writes a record with the size FCB->logical_record_size from the disk
1227 * transfer area to the FCB. The position of the record is specified
1228 * with FCB->current_block_number and FCB->record_within_current_block.
1229 * Then FCB->current_block_number and FCB->record_within_current_block
1230 * are updated to point to the next record.
1232 static void INT21_SequentialWriteToFCB( CONTEXT *context )
1234 struct FCB *fcb;
1235 struct XFCB *xfcb;
1236 HANDLE handle;
1237 DWORD record_number;
1238 DWORD position;
1239 BYTE *disk_transfer_area;
1240 UINT bytes_written;
1241 BYTE AL_result;
1243 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1244 if (fcb->drive_number == 0xff) {
1245 xfcb = (struct XFCB *) fcb;
1246 fcb = (struct FCB *) xfcb->fcb;
1247 } /* if */
1249 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1250 if (handle == INVALID_HANDLE_VALUE) {
1251 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1252 fcb->file_number);
1253 AL_result = 0x01; /* disk full */
1254 } else {
1255 record_number = 128 * fcb->current_block_number + fcb->record_within_current_block;
1256 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1257 if (position != record_number * fcb->logical_record_size) {
1258 TRACE("seek(%d, %d, 0) failed with %u\n",
1259 fcb->file_number, record_number * fcb->logical_record_size, position);
1260 AL_result = 0x01; /* disk full */
1261 } else {
1262 disk_transfer_area = INT21_GetCurrentDTA(context);
1263 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, fcb->logical_record_size);
1264 if (bytes_written != fcb->logical_record_size) {
1265 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1266 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1267 AL_result = 0x01; /* disk full */
1268 } else {
1269 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1270 bytes_written, record_number, position, fcb->file_number, handle);
1271 AL_result = 0x00; /* successful */
1272 } /* if */
1273 } /* if */
1274 } /* if */
1275 if (AL_result == 0x00) {
1276 if (fcb->record_within_current_block == 127) {
1277 fcb->record_within_current_block = 0;
1278 fcb->current_block_number++;
1279 } else {
1280 fcb->record_within_current_block++;
1281 } /* if */
1282 } /* if */
1283 SET_AL(context, AL_result);
1287 /***********************************************************************
1288 * INT21_ReadRandomRecordFromFCB
1290 * Handler for function 0x21.
1292 * PARAMS
1293 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1295 * RETURNS (in AL)
1296 * 0: successful
1297 * 1: end of file, no data read
1298 * 2: segment wrap in DTA, no data read (not returned now)
1299 * 3: end of file, partial record read
1301 * NOTES
1302 * Reads a record with the size FCB->logical_record_size from
1303 * the FCB to the disk transfer area. The position of the record
1304 * is specified with FCB->random_access_record_number. The
1305 * FCB->random_access_record_number is not updated. If a partial record
1306 * is read, it is filled with zeros up to the FCB->logical_record_size.
1308 static void INT21_ReadRandomRecordFromFCB( CONTEXT *context )
1310 struct FCB *fcb;
1311 struct XFCB *xfcb;
1312 HANDLE handle;
1313 DWORD record_number;
1314 DWORD position;
1315 BYTE *disk_transfer_area;
1316 UINT bytes_read;
1317 BYTE AL_result;
1319 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1320 if (fcb->drive_number == 0xff) {
1321 xfcb = (struct XFCB *) fcb;
1322 fcb = (struct FCB *) xfcb->fcb;
1323 } /* if */
1325 memcpy(&record_number, fcb->random_access_record_number, 4);
1326 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1327 if (handle == INVALID_HANDLE_VALUE) {
1328 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1329 fcb->file_number);
1330 AL_result = 0x01; /* end of file, no data read */
1331 } else {
1332 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1333 if (position != record_number * fcb->logical_record_size) {
1334 TRACE("seek(%d, %d, 0) failed with %u\n",
1335 fcb->file_number, record_number * fcb->logical_record_size, position);
1336 AL_result = 0x01; /* end of file, no data read */
1337 } else {
1338 disk_transfer_area = INT21_GetCurrentDTA(context);
1339 bytes_read = _lread((HFILE) handle, disk_transfer_area, fcb->logical_record_size);
1340 if (bytes_read != fcb->logical_record_size) {
1341 TRACE("_lread(%d, %p, %d) failed with %d\n",
1342 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_read);
1343 if (bytes_read == 0) {
1344 AL_result = 0x01; /* end of file, no data read */
1345 } else {
1346 memset(&disk_transfer_area[bytes_read], 0, fcb->logical_record_size - bytes_read);
1347 AL_result = 0x03; /* end of file, partial record read */
1348 } /* if */
1349 } else {
1350 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1351 bytes_read, record_number, position, fcb->file_number, handle);
1352 AL_result = 0x00; /* successful */
1353 } /* if */
1354 } /* if */
1355 } /* if */
1356 fcb->current_block_number = record_number / 128;
1357 fcb->record_within_current_block = record_number % 128;
1358 SET_AL(context, AL_result);
1362 /***********************************************************************
1363 * INT21_WriteRandomRecordToFCB
1365 * Handler for function 0x22.
1367 * PARAMS
1368 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1370 * RETURNS (in AL)
1371 * 0: successful
1372 * 1: disk full
1373 * 2: segment wrap in DTA (not returned now)
1375 * NOTES
1376 * Writes a record with the size FCB->logical_record_size from
1377 * the disk transfer area to the FCB. The position of the record
1378 * is specified with FCB->random_access_record_number. The
1379 * FCB->random_access_record_number is not updated.
1381 static void INT21_WriteRandomRecordToFCB( CONTEXT *context )
1383 struct FCB *fcb;
1384 struct XFCB *xfcb;
1385 HANDLE handle;
1386 DWORD record_number;
1387 DWORD position;
1388 BYTE *disk_transfer_area;
1389 UINT bytes_written;
1390 BYTE AL_result;
1392 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1393 if (fcb->drive_number == 0xff) {
1394 xfcb = (struct XFCB *) fcb;
1395 fcb = (struct FCB *) xfcb->fcb;
1396 } /* if */
1398 memcpy(&record_number, fcb->random_access_record_number, 4);
1399 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1400 if (handle == INVALID_HANDLE_VALUE) {
1401 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1402 fcb->file_number);
1403 AL_result = 0x01; /* disk full */
1404 } else {
1405 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1406 if (position != record_number * fcb->logical_record_size) {
1407 TRACE("seek(%d, %d, 0) failed with %u\n",
1408 fcb->file_number, record_number * fcb->logical_record_size, position);
1409 AL_result = 0x01; /* disk full */
1410 } else {
1411 disk_transfer_area = INT21_GetCurrentDTA(context);
1412 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, fcb->logical_record_size);
1413 if (bytes_written != fcb->logical_record_size) {
1414 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1415 fcb->file_number, disk_transfer_area, fcb->logical_record_size, bytes_written);
1416 AL_result = 0x01; /* disk full */
1417 } else {
1418 TRACE("successful written %d bytes from record %d (position %u) of file %d (handle %p)\n",
1419 bytes_written, record_number, position, fcb->file_number, handle);
1420 AL_result = 0x00; /* successful */
1421 } /* if */
1422 } /* if */
1423 } /* if */
1424 fcb->current_block_number = record_number / 128;
1425 fcb->record_within_current_block = record_number % 128;
1426 SET_AL(context, AL_result);
1430 /***********************************************************************
1431 * INT21_RandomBlockReadFromFCB
1433 * Handler for function 0x27.
1435 * PARAMS
1436 * CX [I/O] Number of records to read
1437 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1439 * RETURNS (in AL)
1440 * 0: successful
1441 * 1: end of file, no data read
1442 * 2: segment wrap in DTA, no data read (not returned now)
1443 * 3: end of file, partial record read
1445 * NOTES
1446 * Reads several records with the size FCB->logical_record_size from
1447 * the FCB to the disk transfer area. The number of records to be
1448 * read is specified in the CX register. The position of the first
1449 * record is specified with FCB->random_access_record_number. The
1450 * FCB->random_access_record_number, the FCB->current_block_number
1451 * and FCB->record_within_current_block are updated to point to the
1452 * next record after the records read. If a partial record is read,
1453 * it is filled with zeros up to the FCB->logical_record_size. The
1454 * CX register is set to the number of successfully read records.
1456 static void INT21_RandomBlockReadFromFCB( CONTEXT *context )
1458 struct FCB *fcb;
1459 struct XFCB *xfcb;
1460 HANDLE handle;
1461 DWORD record_number;
1462 DWORD position;
1463 BYTE *disk_transfer_area;
1464 UINT records_requested;
1465 UINT bytes_requested;
1466 UINT bytes_read;
1467 UINT records_read;
1468 BYTE AL_result;
1470 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1471 if (fcb->drive_number == 0xff) {
1472 xfcb = (struct XFCB *) fcb;
1473 fcb = (struct FCB *) xfcb->fcb;
1474 } /* if */
1476 memcpy(&record_number, fcb->random_access_record_number, 4);
1477 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1478 if (handle == INVALID_HANDLE_VALUE) {
1479 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1480 fcb->file_number);
1481 records_read = 0;
1482 AL_result = 0x01; /* end of file, no data read */
1483 } else {
1484 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1485 if (position != record_number * fcb->logical_record_size) {
1486 TRACE("seek(%d, %d, 0) failed with %u\n",
1487 fcb->file_number, record_number * fcb->logical_record_size, position);
1488 records_read = 0;
1489 AL_result = 0x01; /* end of file, no data read */
1490 } else {
1491 disk_transfer_area = INT21_GetCurrentDTA(context);
1492 records_requested = CX_reg(context);
1493 bytes_requested = records_requested * fcb->logical_record_size;
1494 bytes_read = _lread((HFILE) handle, disk_transfer_area, bytes_requested);
1495 if (bytes_read != bytes_requested) {
1496 TRACE("_lread(%d, %p, %d) failed with %d\n",
1497 fcb->file_number, disk_transfer_area, bytes_requested, bytes_read);
1498 records_read = bytes_read / fcb->logical_record_size;
1499 if (bytes_read % fcb->logical_record_size == 0) {
1500 AL_result = 0x01; /* end of file, no data read */
1501 } else {
1502 records_read++;
1503 memset(&disk_transfer_area[bytes_read], 0, records_read * fcb->logical_record_size - bytes_read);
1504 AL_result = 0x03; /* end of file, partial record read */
1505 } /* if */
1506 } else {
1507 TRACE("successful read %d bytes from record %d (position %u) of file %d (handle %p)\n",
1508 bytes_read, record_number, position, fcb->file_number, handle);
1509 records_read = records_requested;
1510 AL_result = 0x00; /* successful */
1511 } /* if */
1512 } /* if */
1513 } /* if */
1514 record_number += records_read;
1515 memcpy(fcb->random_access_record_number, &record_number, 4);
1516 fcb->current_block_number = record_number / 128;
1517 fcb->record_within_current_block = record_number % 128;
1518 SET_CX(context, records_read);
1519 SET_AL(context, AL_result);
1523 /***********************************************************************
1524 * INT21_RandomBlockWriteToFCB
1526 * Handler for function 0x28.
1528 * PARAMS
1529 * CX [I/O] Number of records to write
1530 * DX:DX [I/O] File control block (FCB or XFCB) of open file
1532 * RETURNS (in AL)
1533 * 0: successful
1534 * 1: disk full
1535 * 2: segment wrap in DTA (not returned now)
1537 * NOTES
1538 * Writes several records with the size FCB->logical_record_size from
1539 * the disk transfer area to the FCB. The number of records to be
1540 * written is specified in the CX register. The position of the first
1541 * record is specified with FCB->random_access_record_number. The
1542 * FCB->random_access_record_number, the FCB->current_block_number
1543 * and FCB->record_within_current_block are updated to point to the
1544 * next record after the records written. The CX register is set to
1545 * the number of successfully written records.
1547 static void INT21_RandomBlockWriteToFCB( CONTEXT *context )
1549 struct FCB *fcb;
1550 struct XFCB *xfcb;
1551 HANDLE handle;
1552 DWORD record_number;
1553 DWORD position;
1554 BYTE *disk_transfer_area;
1555 UINT records_requested;
1556 UINT bytes_requested;
1557 UINT bytes_written;
1558 UINT records_written;
1559 BYTE AL_result;
1561 fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1562 if (fcb->drive_number == 0xff) {
1563 xfcb = (struct XFCB *) fcb;
1564 fcb = (struct FCB *) xfcb->fcb;
1565 } /* if */
1567 memcpy(&record_number, fcb->random_access_record_number, 4);
1568 handle = DosFileHandleToWin32Handle((HFILE16) fcb->file_number);
1569 if (handle == INVALID_HANDLE_VALUE) {
1570 TRACE("DosFileHandleToWin32Handle(%d) failed: INVALID_HANDLE_VALUE\n",
1571 fcb->file_number);
1572 records_written = 0;
1573 AL_result = 0x01; /* disk full */
1574 } else {
1575 position = SetFilePointer(handle, record_number * fcb->logical_record_size, NULL, 0);
1576 if (position != record_number * fcb->logical_record_size) {
1577 TRACE("seek(%d, %d, 0) failed with %u\n",
1578 fcb->file_number, record_number * fcb->logical_record_size, position);
1579 records_written = 0;
1580 AL_result = 0x01; /* disk full */
1581 } else {
1582 disk_transfer_area = INT21_GetCurrentDTA(context);
1583 records_requested = CX_reg(context);
1584 bytes_requested = records_requested * fcb->logical_record_size;
1585 bytes_written = _lwrite((HFILE) handle, (LPCSTR)disk_transfer_area, bytes_requested);
1586 if (bytes_written != bytes_requested) {
1587 TRACE("_lwrite(%d, %p, %d) failed with %d\n",
1588 fcb->file_number, disk_transfer_area, bytes_requested, bytes_written);
1589 records_written = bytes_written / fcb->logical_record_size;
1590 AL_result = 0x01; /* disk full */
1591 } else {
1592 TRACE("successful write %d bytes from record %d (position %u) of file %d (handle %p)\n",
1593 bytes_written, record_number, position, fcb->file_number, handle);
1594 records_written = records_requested;
1595 AL_result = 0x00; /* successful */
1596 } /* if */
1597 } /* if */
1598 } /* if */
1599 record_number += records_written;
1600 memcpy(fcb->random_access_record_number, &record_number, 4);
1601 fcb->current_block_number = record_number / 128;
1602 fcb->record_within_current_block = record_number % 128;
1603 SET_CX(context, records_written);
1604 SET_AL(context, AL_result);
1608 /***********************************************************************
1609 * INT21_CreateDirectory
1611 * Handler for:
1612 * - function 0x39
1613 * - subfunction 0x39 of function 0x71
1614 * - subfunction 0xff of function 0x43 (CL == 0x39)
1616 static BOOL INT21_CreateDirectory( CONTEXT *context )
1618 WCHAR dirW[MAX_PATH];
1619 char *dirA = CTX_SEG_OFF_TO_LIN(context,
1620 context->SegDs,
1621 context->Edx);
1623 TRACE( "CREATE DIRECTORY %s\n", dirA );
1625 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
1627 if (CreateDirectoryW(dirW, NULL))
1628 return TRUE;
1631 * FIXME: CreateDirectory's LastErrors will clash with the ones
1632 * used by DOS. AH=39 only returns 3 (path not found) and
1633 * 5 (access denied), while CreateDirectory return several
1634 * ones. Remap some of them. -Marcus
1636 switch (GetLastError())
1638 case ERROR_ALREADY_EXISTS:
1639 case ERROR_FILENAME_EXCED_RANGE:
1640 case ERROR_DISK_FULL:
1641 SetLastError(ERROR_ACCESS_DENIED);
1642 break;
1643 default:
1644 break;
1647 return FALSE;
1651 /***********************************************************************
1652 * INT21_ExtendedCountryInformation
1654 * Handler for function 0x65.
1656 static void INT21_ExtendedCountryInformation( CONTEXT *context )
1658 BYTE *dataptr = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
1659 BYTE buffsize = CX_reg (context);
1661 TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
1662 AL_reg(context) );
1665 * Check subfunctions that are passed country and code page.
1667 if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
1669 WORD country = DX_reg(context);
1670 WORD codepage = BX_reg(context);
1672 if (country != 0xffff && country != INT21_GetSystemCountryCode())
1673 FIXME( "Requested info on non-default country %04x\n", country );
1675 if (codepage != 0xffff && codepage != GetOEMCP())
1676 FIXME( "Requested info on non-default code page %04x\n", codepage );
1679 switch (AL_reg(context)) {
1680 case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
1681 INT_BARF( context, 0x21 );
1682 SET_CFLAG( context );
1683 break;
1685 case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
1686 TRACE( "Get general internationalization info\n" );
1687 dataptr[0] = 0x01; /* Info ID */
1688 *(WORD*)(dataptr+1) = 38; /* Size of the following info */
1689 *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
1690 *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
1691 /* FIXME: fill buffer partially up to buffsize bytes*/
1692 if (buffsize >= 0x29){
1693 INT21_FillCountryInformation( dataptr + 7 );
1694 SET_CX( context, 0x29 ); /* Size of returned info */
1695 }else{
1696 SET_CX( context, 0x07 ); /* Size of returned info */
1698 break;
1700 case 0x02: /* GET POINTER TO UPPERCASE TABLE */
1701 case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
1702 TRACE( "Get pointer to uppercase table\n" );
1703 dataptr[0] = AL_reg(context); /* Info ID */
1704 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1705 offsetof(INT21_HEAP, uppercase_size) );
1706 SET_CX( context, 5 ); /* Size of returned info */
1707 break;
1709 case 0x03: /* GET POINTER TO LOWERCASE TABLE */
1710 TRACE( "Get pointer to lowercase table\n" );
1711 dataptr[0] = 0x03; /* Info ID */
1712 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1713 offsetof(INT21_HEAP, lowercase_size) );
1714 SET_CX( context, 5 ); /* Size of returned info */
1715 break;
1717 case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
1718 TRACE("Get pointer to filename terminator table\n");
1719 dataptr[0] = 0x05; /* Info ID */
1720 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1721 offsetof(INT21_HEAP, filename_size) );
1722 SET_CX( context, 5 ); /* Size of returned info */
1723 break;
1725 case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
1726 TRACE("Get pointer to collating sequence table\n");
1727 dataptr[0] = 0x06; /* Info ID */
1728 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1729 offsetof(INT21_HEAP, collating_size) );
1730 SET_CX( context, 5 ); /* Size of returned info */
1731 break;
1733 case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
1734 TRACE("Get pointer to DBCS lead byte table\n");
1735 dataptr[0] = 0x07; /* Info ID */
1736 *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
1737 offsetof(INT21_HEAP, dbcs_size) );
1738 SET_CX( context, 5 ); /* Size of returned info */
1739 break;
1741 case 0x20: /* CAPITALIZE CHARACTER */
1742 case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
1743 TRACE("Convert char to uppercase\n");
1744 SET_DL( context, toupper(DL_reg(context)) );
1745 break;
1747 case 0x21: /* CAPITALIZE STRING */
1748 case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
1749 TRACE("Convert string to uppercase with length\n");
1751 char *ptr = CTX_SEG_OFF_TO_LIN( context, context->SegDs,
1752 context->Edx );
1753 WORD len = CX_reg(context);
1754 while (len--) { *ptr = toupper(*ptr); ptr++; }
1756 break;
1758 case 0x22: /* CAPITALIZE ASCIIZ STRING */
1759 case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
1760 TRACE("Convert ASCIIZ string to uppercase\n");
1762 char *p = CTX_SEG_OFF_TO_LIN( context, context->SegDs, context->Edx );
1763 for ( ; *p; p++) *p = toupper(*p);
1765 break;
1767 case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
1768 INT_BARF( context, 0x21 );
1769 SET_CFLAG( context );
1770 break;
1772 default:
1773 INT_BARF( context, 0x21 );
1774 SET_CFLAG(context);
1775 break;
1780 /***********************************************************************
1781 * INT21_FileAttributes
1783 * Handler for:
1784 * - function 0x43
1785 * - subfunction 0x43 of function 0x71
1787 static BOOL INT21_FileAttributes( CONTEXT *context,
1788 BYTE subfunction,
1789 BOOL islong )
1791 WCHAR fileW[MAX_PATH];
1792 char *fileA = CTX_SEG_OFF_TO_LIN(context,
1793 context->SegDs,
1794 context->Edx);
1795 HANDLE handle;
1796 BOOL status;
1797 FILETIME filetime;
1798 DWORD result;
1799 WORD date, time;
1800 int len;
1802 switch (subfunction)
1804 case 0x00: /* GET FILE ATTRIBUTES */
1805 TRACE( "GET FILE ATTRIBUTES for %s\n", fileA );
1806 len = MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1808 /* Winbench 96 Disk Test fails if we don't complain
1809 * about a filename that ends in \
1811 if (!len || (fileW[len-1] == '/') || (fileW[len-1] == '\\'))
1812 return FALSE;
1814 result = GetFileAttributesW( fileW );
1815 if (result == INVALID_FILE_ATTRIBUTES)
1816 return FALSE;
1817 else
1819 SET_CX( context, (WORD)result );
1820 if (!islong)
1821 SET_AX( context, (WORD)result ); /* DR DOS */
1823 break;
1825 case 0x01: /* SET FILE ATTRIBUTES */
1826 TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n",
1827 CX_reg(context), fileA );
1828 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1830 if (!SetFileAttributesW( fileW, CX_reg(context) ))
1831 return FALSE;
1832 break;
1834 case 0x02: /* GET COMPRESSED FILE SIZE */
1835 TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA );
1836 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1838 result = GetCompressedFileSizeW( fileW, NULL );
1839 if (result == INVALID_FILE_SIZE)
1840 return FALSE;
1841 else
1843 SET_AX( context, LOWORD(result) );
1844 SET_DX( context, HIWORD(result) );
1846 break;
1848 case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
1849 if (!islong)
1850 INT_BARF( context, 0x21 );
1851 else
1853 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
1854 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1856 handle = CreateFileW( fileW, GENERIC_WRITE,
1857 FILE_SHARE_READ | FILE_SHARE_WRITE,
1858 NULL, OPEN_EXISTING, 0, 0 );
1859 if (handle == INVALID_HANDLE_VALUE)
1860 return FALSE;
1862 DosDateTimeToFileTime( DI_reg(context),
1863 CX_reg(context),
1864 &filetime );
1865 status = SetFileTime( handle, NULL, NULL, &filetime );
1867 CloseHandle( handle );
1868 return status;
1870 break;
1872 case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
1873 if (!islong)
1874 INT_BARF( context, 0x21 );
1875 else
1877 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
1878 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1880 handle = CreateFileW( fileW, GENERIC_READ,
1881 FILE_SHARE_READ | FILE_SHARE_WRITE,
1882 NULL, OPEN_EXISTING, 0, 0 );
1883 if (handle == INVALID_HANDLE_VALUE)
1884 return FALSE;
1886 status = GetFileTime( handle, NULL, NULL, &filetime );
1887 if (status)
1889 FileTimeToDosDateTime( &filetime, &date, &time );
1890 SET_DI( context, date );
1891 SET_CX( context, time );
1894 CloseHandle( handle );
1895 return status;
1897 break;
1899 case 0x05: /* SET FILE LAST ACCESS DATE */
1900 if (!islong)
1901 INT_BARF( context, 0x21 );
1902 else
1904 TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA );
1905 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1907 handle = CreateFileW( fileW, GENERIC_WRITE,
1908 FILE_SHARE_READ | FILE_SHARE_WRITE,
1909 NULL, OPEN_EXISTING, 0, 0 );
1910 if (handle == INVALID_HANDLE_VALUE)
1911 return FALSE;
1913 DosDateTimeToFileTime( DI_reg(context),
1915 &filetime );
1916 status = SetFileTime( handle, NULL, &filetime, NULL );
1918 CloseHandle( handle );
1919 return status;
1921 break;
1923 case 0x06: /* GET FILE LAST ACCESS DATE */
1924 if (!islong)
1925 INT_BARF( context, 0x21 );
1926 else
1928 TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA );
1929 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1931 handle = CreateFileW( fileW, GENERIC_READ,
1932 FILE_SHARE_READ | FILE_SHARE_WRITE,
1933 NULL, OPEN_EXISTING, 0, 0 );
1934 if (handle == INVALID_HANDLE_VALUE)
1935 return FALSE;
1937 status = GetFileTime( handle, NULL, &filetime, NULL );
1938 if (status)
1940 FileTimeToDosDateTime( &filetime, &date, NULL );
1941 SET_DI( context, date );
1944 CloseHandle( handle );
1945 return status;
1947 break;
1949 case 0x07: /* SET FILE CREATION DATE AND TIME */
1950 if (!islong)
1951 INT_BARF( context, 0x21 );
1952 else
1954 TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA );
1955 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1957 handle = CreateFileW( fileW, GENERIC_WRITE,
1958 FILE_SHARE_READ | FILE_SHARE_WRITE,
1959 NULL, OPEN_EXISTING, 0, 0 );
1960 if (handle == INVALID_HANDLE_VALUE)
1961 return FALSE;
1964 * FIXME: SI has number of 10-millisecond units past time in CX.
1966 DosDateTimeToFileTime( DI_reg(context),
1967 CX_reg(context),
1968 &filetime );
1969 status = SetFileTime( handle, &filetime, NULL, NULL );
1971 CloseHandle( handle );
1972 return status;
1974 break;
1976 case 0x08: /* GET FILE CREATION DATE AND TIME */
1977 if (!islong)
1978 INT_BARF( context, 0x21 );
1979 else
1981 TRACE( "GET FILE CREATION DATE AND TIME, file %s\n", fileA );
1982 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1984 handle = CreateFileW( fileW, GENERIC_READ,
1985 FILE_SHARE_READ | FILE_SHARE_WRITE,
1986 NULL, OPEN_EXISTING, 0, 0 );
1987 if (handle == INVALID_HANDLE_VALUE)
1988 return FALSE;
1990 status = GetFileTime( handle, &filetime, NULL, NULL );
1991 if (status)
1993 FileTimeToDosDateTime( &filetime, &date, &time );
1994 SET_DI( context, date );
1995 SET_CX( context, time );
1997 * FIXME: SI has number of 10-millisecond units past
1998 * time in CX.
2000 SET_SI( context, 0 );
2003 CloseHandle(handle);
2004 return status;
2006 break;
2008 case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
2009 if (islong || context->Ebp != 0x5053)
2010 INT_BARF( context, 0x21 );
2011 else
2013 switch(CL_reg(context))
2015 case 0x39:
2016 if (!INT21_CreateDirectory( context ))
2017 return FALSE;
2018 break;
2020 case 0x56:
2021 if (!INT21_RenameFile( context ))
2022 return FALSE;
2023 break;
2025 default:
2026 INT_BARF( context, 0x21 );
2029 break;
2031 default:
2032 INT_BARF( context, 0x21 );
2035 return TRUE;
2039 /***********************************************************************
2040 * INT21_FileDateTime
2042 * Handler for function 0x57.
2044 static BOOL INT21_FileDateTime( CONTEXT *context )
2046 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2047 FILETIME filetime;
2048 WORD date, time;
2050 switch (AL_reg(context)) {
2051 case 0x00: /* Get last-written stamp */
2052 TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2053 BX_reg(context) );
2055 if (!GetFileTime( handle, NULL, NULL, &filetime ))
2056 return FALSE;
2057 FileTimeToDosDateTime( &filetime, &date, &time );
2058 SET_DX( context, date );
2059 SET_CX( context, time );
2060 break;
2063 case 0x01: /* Set last-written stamp */
2064 TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
2065 BX_reg(context) );
2067 DosDateTimeToFileTime( DX_reg(context),
2068 CX_reg(context),
2069 &filetime );
2070 if (!SetFileTime( handle, NULL, NULL, &filetime ))
2071 return FALSE;
2072 break;
2075 case 0x04: /* Get last access stamp, DOS 7 */
2076 TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2077 BX_reg(context) );
2079 if (!GetFileTime( handle, NULL, &filetime, NULL ))
2080 return FALSE;
2081 FileTimeToDosDateTime( &filetime, &date, &time );
2082 SET_DX( context, date );
2083 SET_CX( context, time );
2084 break;
2087 case 0x05: /* Set last access stamp, DOS 7 */
2088 TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
2089 BX_reg(context) );
2091 DosDateTimeToFileTime( DX_reg(context),
2092 CX_reg(context),
2093 &filetime );
2094 if (!SetFileTime( handle, NULL, &filetime, NULL ))
2095 return FALSE;
2096 break;
2099 case 0x06: /* Get creation stamp, DOS 7 */
2100 TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
2101 BX_reg(context) );
2103 if (!GetFileTime( handle, &filetime, NULL, NULL ))
2104 return FALSE;
2105 FileTimeToDosDateTime( &filetime, &date, &time );
2106 SET_DX( context, date );
2107 SET_CX( context, time );
2109 * FIXME: SI has number of 10-millisecond units past time in CX.
2111 SET_SI( context, 0 );
2112 break;
2115 case 0x07: /* Set creation stamp, DOS 7 */
2116 TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
2117 BX_reg(context) );
2120 * FIXME: SI has number of 10-millisecond units past time in CX.
2122 DosDateTimeToFileTime( DX_reg(context),
2123 CX_reg(context),
2124 &filetime );
2125 if (!SetFileTime( handle, &filetime, NULL, NULL ))
2126 return FALSE;
2127 break;
2130 default:
2131 INT_BARF( context, 0x21 );
2132 break;
2135 return TRUE;
2139 /***********************************************************************
2140 * INT21_GetPSP
2142 * Handler for functions 0x51 and 0x62.
2144 static void INT21_GetPSP( CONTEXT *context )
2146 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
2148 SET_BX( context, LOWORD(GetCurrentPDB16()) );
2151 static inline void setword( BYTE *ptr, WORD w )
2153 ptr[0] = (BYTE)w;
2154 ptr[1] = (BYTE)(w >> 8);
2157 static void CreateBPB(int drive, BYTE *data, BOOL16 limited)
2158 /* limited == TRUE is used with INT 0x21/0x440d */
2160 /* FIXME: we're forcing some values without checking that those are valid */
2161 if (drive > 1)
2163 setword(data, 512);
2164 data[2] = 2;
2165 setword(&data[3], 0);
2166 data[5] = 2;
2167 setword(&data[6], 240);
2168 setword(&data[8], 64000);
2169 data[0x0a] = 0xf8;
2170 setword(&data[0x0b], 40);
2171 setword(&data[0x0d], 56);
2172 setword(&data[0x0f], 2);
2173 setword(&data[0x11], 0);
2174 if (!limited)
2176 setword(&data[0x1f], 800);
2177 data[0x21] = 5;
2178 setword(&data[0x22], 1);
2181 else
2182 { /* 1.44mb */
2183 setword(data, 512);
2184 data[2] = 2;
2185 setword(&data[3], 0);
2186 data[5] = 2;
2187 setword(&data[6], 240);
2188 setword(&data[8], 2880);
2189 data[0x0a] = 0xf8;
2190 setword(&data[0x0b], 6);
2191 setword(&data[0x0d], 18);
2192 setword(&data[0x0f], 2);
2193 setword(&data[0x11], 0);
2194 if (!limited)
2196 setword(&data[0x1f], 80);
2197 data[0x21] = 7;
2198 setword(&data[0x22], 2);
2203 static inline DWORD INT21_Ioctl_CylHeadSect2Lin(DWORD cyl, WORD head, WORD sec, WORD cyl_cnt,
2204 WORD head_cnt, WORD sec_cnt)
2206 DWORD res = (cyl * head_cnt*sec_cnt + head * sec_cnt + sec);
2207 return res;
2210 /***********************************************************************
2211 * INT21_Ioctl_Block
2213 * Handler for block device IOCTLs.
2215 static void INT21_Ioctl_Block( CONTEXT *context )
2217 BYTE *dataptr;
2218 BYTE drive = INT21_MapDrive( BL_reg(context) );
2219 WCHAR drivespec[] = {'A', ':', '\\', 0};
2220 UINT drivetype;
2222 drivespec[0] += drive;
2223 drivetype = GetDriveTypeW( drivespec );
2225 RESET_CFLAG(context);
2226 if (drivetype == DRIVE_UNKNOWN || drivetype == DRIVE_NO_ROOT_DIR)
2228 TRACE( "IOCTL - SUBFUNCTION %d - INVALID DRIVE %c:\n",
2229 AL_reg(context), 'A' + drive );
2230 SetLastError( ERROR_INVALID_DRIVE );
2231 SET_AX( context, ERROR_INVALID_DRIVE );
2232 SET_CFLAG( context );
2233 return;
2236 switch (AL_reg(context))
2238 case 0x04: /* READ FROM BLOCK DEVICE CONTROL CHANNEL */
2239 case 0x05: /* WRITE TO BLOCK DEVICE CONTROL CHANNEL */
2240 INT_BARF( context, 0x21 );
2241 break;
2243 case 0x08: /* CHECK IF BLOCK DEVICE REMOVABLE */
2244 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOVABLE - %c:\n",
2245 'A' + drive );
2247 if (drivetype == DRIVE_REMOVABLE)
2248 SET_AX( context, 0 ); /* removable */
2249 else
2250 SET_AX( context, 1 ); /* not removable */
2251 break;
2253 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
2254 TRACE( "IOCTL - CHECK IF BLOCK DEVICE REMOTE - %c:\n",
2255 'A' + drive );
2257 if (drivetype == DRIVE_REMOTE)
2258 SET_DX( context, (1<<9) | (1<<12) ); /* remote + no direct IO */
2259 else if (drivetype == DRIVE_CDROM)
2260 /* CDROM should be set to remote. If it set the app will
2261 * call int2f to check if it cdrom or remote drive. */
2262 SET_DX( context, (1<<12) );
2263 else if (drivetype == DRIVE_FIXED)
2264 /* This should define if drive support 0x0d, 0x0f and 0x08
2265 * requests. The local fixed drive should do. */
2266 SET_DX( context, (1<<11) );
2267 else
2268 SET_DX( context, 0 ); /* FIXME: use driver attr here */
2269 break;
2271 case 0x0d: /* GENERIC BLOCK DEVICE REQUEST */
2272 /* Get pointer to IOCTL parameter block */
2273 dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2275 switch (CX_reg(context))
2277 case 0x0841: /* write logical device track */
2278 TRACE( "GENERIC IOCTL - Write logical device track - %c:\n",
2279 'A' + drive);
2281 WORD head = *(WORD *)(dataptr+1);
2282 WORD cyl = *(WORD *)(dataptr+3);
2283 WORD sect = *(WORD *)(dataptr+5);
2284 WORD nrsect = *(WORD *)(dataptr+7);
2285 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2286 WORD cyl_cnt, head_cnt, sec_cnt;
2288 /* FIXME: we're faking some values here */
2289 if (drive > 1)
2291 /* cyl_cnt = 0x300;
2292 head_cnt = 16;
2293 sec_cnt = 255; */
2294 SET_AX( context, ERROR_WRITE_FAULT );
2295 SET_CFLAG(context);
2296 break;
2298 else
2299 { /* floppy */
2300 cyl_cnt = 80;
2301 head_cnt = 2;
2302 sec_cnt = 18;
2305 if (!DOSVM_RawWrite(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2307 SET_AX( context, ERROR_WRITE_FAULT );
2308 SET_CFLAG(context);
2311 break;
2313 case 0x084a: /* lock logical volume */
2314 TRACE( "GENERIC IOCTL - Lock logical volume, level %d mode %d - %c:\n",
2315 BH_reg(context), DX_reg(context), 'A' + drive );
2316 break;
2318 case 0x0860: /* get device parameters */
2319 /* FIXME: we're faking some values here */
2320 /* used by w4wgrp's winfile */
2321 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
2322 dataptr[0] = 0x04;
2323 dataptr[6] = 0; /* media type */
2324 if (drive > 1)
2326 dataptr[1] = 0x05; /* fixed disk */
2327 setword(&dataptr[2], 0x01); /* non removable */
2328 setword(&dataptr[4], 0x300); /* # of cylinders */
2330 else
2332 dataptr[1] = 0x07; /* block dev, floppy */
2333 setword(&dataptr[2], 0x02); /* removable */
2334 setword(&dataptr[4], 80); /* # of cylinders */
2336 CreateBPB(drive, &dataptr[7], TRUE);
2337 RESET_CFLAG(context);
2338 break;
2340 case 0x0861: /* read logical device track */
2341 TRACE( "GENERIC IOCTL - Read logical device track - %c:\n",
2342 'A' + drive);
2344 WORD head = *(WORD *)(dataptr+1);
2345 WORD cyl = *(WORD *)(dataptr+3);
2346 WORD sect = *(WORD *)(dataptr+5);
2347 WORD nrsect = *(WORD *)(dataptr+7);
2348 BYTE *data = CTX_SEG_OFF_TO_LIN(context, *(WORD *)(dataptr+11), *(WORD *)(dataptr+9));
2349 WORD cyl_cnt, head_cnt, sec_cnt;
2351 /* FIXME: we're faking some values here */
2352 if (drive > 1)
2354 cyl_cnt = 0x300;
2355 head_cnt = 16;
2356 sec_cnt = 255;
2358 else
2359 { /* floppy */
2360 cyl_cnt = 80;
2361 head_cnt = 2;
2362 sec_cnt = 18;
2365 if (!DOSVM_RawRead(drive, INT21_Ioctl_CylHeadSect2Lin(cyl, head, sect, cyl_cnt, head_cnt, sec_cnt), nrsect, data, FALSE))
2367 SET_AX( context, ERROR_READ_FAULT );
2368 SET_CFLAG(context);
2371 break;
2373 case 0x0866: /* get volume serial number */
2375 WCHAR label[12],fsname[9];
2376 DWORD serial;
2377 TRACE( "GENERIC IOCTL - Get media id - %c:\n",
2378 'A' + drive );
2380 GetVolumeInformationW(drivespec, label, 12, &serial, NULL, NULL, fsname, 9);
2381 *(WORD*)dataptr = 0;
2382 memcpy(dataptr+2,&serial,4);
2383 WideCharToMultiByte(CP_OEMCP, 0, label, 11, (LPSTR)dataptr + 6, 11, NULL, NULL);
2384 WideCharToMultiByte(CP_OEMCP, 0, fsname, 8, (LPSTR)dataptr + 17, 8, NULL, NULL);
2386 break;
2388 case 0x086a: /* unlock logical volume */
2389 TRACE( "GENERIC IOCTL - Logical volume unlocked - %c:\n",
2390 'A' + drive );
2391 break;
2393 case 0x086f: /* get drive map information */
2394 memset(dataptr+1, '\0', dataptr[0]-1);
2395 dataptr[1] = dataptr[0];
2396 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
2397 dataptr[3] = 0xFF; /* no physical drive */
2398 break;
2400 case 0x0872:
2401 /* Trial and error implementation */
2402 SET_AX( context, drivetype == DRIVE_UNKNOWN ? 0x0f : 0x01 );
2403 SET_CFLAG(context); /* Seems to be set all the time */
2404 break;
2406 default:
2407 INT_BARF( context, 0x21 );
2409 break;
2411 case 0x0e: /* GET LOGICAL DRIVE MAP */
2412 TRACE( "IOCTL - GET LOGICAL DRIVE MAP - %c:\n",
2413 'A' + drive );
2414 /* FIXME: this is not correct if drive has mappings */
2415 SET_AL( context, 0 ); /* drive has no mapping */
2416 break;
2418 case 0x0f: /* SET LOGICAL DRIVE MAP */
2419 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
2420 INT21_DriveName( BL_reg(context)));
2421 /* FIXME: as of today, we don't support logical drive mapping... */
2422 SET_AL( context, 0 );
2423 break;
2425 case 0x11: /* QUERY GENERIC IOCTL CAPABILITY */
2426 default:
2427 INT_BARF( context, 0x21 );
2432 /***********************************************************************
2433 * INT21_Ioctl_Char
2435 * Handler for character device IOCTLs.
2437 static void INT21_Ioctl_Char( CONTEXT *context )
2439 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2440 BOOL IsConsoleIOHandle = VerifyConsoleIoHandle( handle );
2442 switch (AL_reg(context))
2444 case 0x00: /* GET DEVICE INFORMATION */
2445 TRACE( "IOCTL - GET DEVICE INFORMATION - %d\n", BX_reg(context) );
2446 if (IsConsoleIOHandle || GetFileType(handle) == FILE_TYPE_CHAR)
2449 * Returns attribute word in DX:
2450 * Bit 14 - Device driver can process IOCTL requests.
2451 * Bit 13 - Output until busy supported.
2452 * Bit 11 - Driver supports OPEN/CLOSE calls.
2453 * Bit 8 - Unknown.
2454 * Bit 7 - Set (indicates device).
2455 * Bit 6 - EOF on input.
2456 * Bit 5 - Raw (binary) mode.
2457 * Bit 4 - Device is special (uses int29).
2458 * Bit 3 - Clock device.
2459 * Bit 2 - NUL device.
2460 * Bit 1 - Console output device.
2461 * Bit 0 - Console input device.
2463 SET_DX( context, IsConsoleIOHandle ? 0x80c3 : 0x80c0 /* FIXME */ );
2465 else
2468 * Returns attribute word in DX:
2469 * Bit 15 - File is remote.
2470 * Bit 14 - Don't set file date/time on closing.
2471 * Bit 11 - Media not removable.
2472 * Bit 8 - Generate int24 if no disk space on write
2473 * or read past end of file
2474 * Bit 7 - Clear (indicates file).
2475 * Bit 6 - File has not been written.
2476 * Bit 5..0 - Drive number (0=A:,...)
2478 * FIXME: Should check if file is on remote or removable drive.
2479 * FIXME: Should use drive file is located on (and not current).
2481 SET_DX( context, 0x0140 + INT21_GetCurrentDrive() );
2483 break;
2485 case 0x0a: /* CHECK IF HANDLE IS REMOTE */
2486 TRACE( "IOCTL - CHECK IF HANDLE IS REMOTE - %d\n", BX_reg(context) );
2488 * Returns attribute word in DX:
2489 * Bit 15 - Set if remote.
2490 * Bit 14 - Set if date/time not set on close.
2492 * FIXME: Should check if file is on remote drive.
2494 SET_DX( context, 0 );
2495 break;
2497 case 0x01: /* SET DEVICE INFORMATION */
2498 case 0x02: /* READ FROM CHARACTER DEVICE CONTROL CHANNEL */
2499 case 0x03: /* WRITE TO CHARACTER DEVICE CONTROL CHANNEL */
2500 case 0x06: /* GET INPUT STATUS */
2501 case 0x07: /* GET OUTPUT STATUS */
2502 case 0x0c: /* GENERIC CHARACTER DEVICE REQUEST */
2503 case 0x10: /* QUERY GENERIC IOCTL CAPABILITY */
2504 default:
2505 INT_BARF( context, 0x21 );
2506 break;
2511 /***********************************************************************
2512 * INT21_Ioctl
2514 * Handler for function 0x44.
2516 static void INT21_Ioctl( CONTEXT *context )
2518 switch (AL_reg(context))
2520 case 0x00:
2521 case 0x01:
2522 case 0x02:
2523 case 0x03:
2524 INT21_Ioctl_Char( context );
2525 break;
2527 case 0x04:
2528 case 0x05:
2529 INT21_Ioctl_Block( context );
2530 break;
2532 case 0x06:
2533 case 0x07:
2534 INT21_Ioctl_Char( context );
2535 break;
2537 case 0x08:
2538 case 0x09:
2539 INT21_Ioctl_Block( context );
2540 break;
2542 case 0x0a:
2543 INT21_Ioctl_Char( context );
2544 break;
2546 case 0x0b: /* SET SHARING RETRY COUNT */
2547 TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
2548 CX_reg(context), DX_reg(context) );
2549 if (!CX_reg(context))
2551 SET_AX( context, 1 );
2552 SET_CFLAG( context );
2554 else
2556 RESET_CFLAG( context );
2558 break;
2560 case 0x0c:
2561 INT21_Ioctl_Char( context );
2562 break;
2564 case 0x0d:
2565 case 0x0e:
2566 case 0x0f:
2567 INT21_Ioctl_Block( context );
2568 break;
2570 case 0x10:
2571 INT21_Ioctl_Char( context );
2572 break;
2574 case 0x11:
2575 INT21_Ioctl_Block( context );
2576 break;
2578 case 0x12: /* DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
2579 TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
2580 SET_CFLAG(context); /* Error / This is not DR DOS. */
2581 SET_AX( context, 0x0001 ); /* Invalid function */
2582 break;
2584 case 0x52: /* DR DOS - DETERMINE DOS TYPE */
2585 TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
2586 SET_CFLAG(context); /* Error / This is not DR DOS. */
2587 SET_AX( context, 0x0001 ); /* Invalid function */
2588 break;
2590 case 0xe0: /* Sun PC-NFS API */
2591 TRACE( "Sun PC-NFS API\n" );
2592 /* not installed */
2593 break;
2595 default:
2596 INT_BARF( context, 0x21 );
2601 /***********************************************************************
2602 * INT21_Fat32
2604 * Handler for function 0x73.
2606 static BOOL INT21_Fat32( CONTEXT *context )
2608 switch (AL_reg(context))
2610 case 0x02: /* FAT32 - GET EXTENDED DPB */
2612 BYTE drive = INT21_MapDrive( DL_reg(context) );
2613 WORD *ptr = CTX_SEG_OFF_TO_LIN(context,
2614 context->SegEs, context->Edi);
2615 INT21_DPB *target = (INT21_DPB*)(ptr + 1);
2616 INT21_DPB *source;
2618 TRACE( "FAT32 - GET EXTENDED DPB %d\n", DL_reg(context) );
2620 if ( CX_reg(context) < sizeof(INT21_DPB) + 2 || *ptr < sizeof(INT21_DPB) )
2622 SetLastError( ERROR_BAD_LENGTH );
2623 return FALSE;
2626 if ( !INT21_FillDrivePB( drive ) )
2628 SetLastError( ERROR_INVALID_DRIVE );
2629 return FALSE;
2632 source = &INT21_GetHeapPointer()->misc_dpb_list[drive];
2634 *ptr = sizeof(INT21_DPB);
2635 *target = *source;
2637 if (LOWORD(context->Esi) != 0xF1A6)
2639 target->driver_header = 0;
2640 target->next = 0;
2642 else
2644 FIXME( "Caller requested driver and next DPB pointers!\n" );
2647 break;
2649 case 0x03: /* FAT32 - GET EXTENDED FREE SPACE ON DRIVE */
2651 WCHAR dirW[MAX_PATH];
2652 char *dirA = CTX_SEG_OFF_TO_LIN( context,
2653 context->SegDs, context->Edx );
2654 BYTE *data = CTX_SEG_OFF_TO_LIN( context,
2655 context->SegEs, context->Edi );
2656 DWORD cluster_sectors;
2657 DWORD sector_bytes;
2658 DWORD free_clusters;
2659 DWORD total_clusters;
2661 TRACE( "FAT32 - GET EXTENDED FREE SPACE ON DRIVE %s\n", dirA );
2662 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
2664 if (CX_reg(context) < 44)
2666 SetLastError( ERROR_BAD_LENGTH );
2667 return FALSE;
2670 if (!GetDiskFreeSpaceW( dirW, &cluster_sectors, &sector_bytes,
2671 &free_clusters, &total_clusters ))
2672 return FALSE;
2674 *(WORD*) (data + 0) = 44; /* size of structure */
2675 *(WORD*) (data + 2) = 0; /* version */
2676 *(DWORD*)(data + 4) = cluster_sectors;
2677 *(DWORD*)(data + 8) = sector_bytes;
2678 *(DWORD*)(data + 12) = free_clusters;
2679 *(DWORD*)(data + 16) = total_clusters;
2682 * Below we have free/total sectors and
2683 * free/total allocation units without adjustment
2684 * for compression. We fake both using cluster information.
2686 *(DWORD*)(data + 20) = free_clusters * cluster_sectors;
2687 *(DWORD*)(data + 24) = total_clusters * cluster_sectors;
2688 *(DWORD*)(data + 28) = free_clusters;
2689 *(DWORD*)(data + 32) = total_clusters;
2692 * Between (data + 36) and (data + 43) there
2693 * are eight reserved bytes.
2696 break;
2698 default:
2699 INT_BARF( context, 0x21 );
2702 return TRUE;
2705 static void INT21_ConvertFindDataWtoA(WIN32_FIND_DATAA *dataA,
2706 const WIN32_FIND_DATAW *dataW)
2708 dataA->dwFileAttributes = dataW->dwFileAttributes;
2709 dataA->ftCreationTime = dataW->ftCreationTime;
2710 dataA->ftLastAccessTime = dataW->ftLastAccessTime;
2711 dataA->ftLastWriteTime = dataW->ftLastWriteTime;
2712 dataA->nFileSizeHigh = dataW->nFileSizeHigh;
2713 dataA->nFileSizeLow = dataW->nFileSizeLow;
2714 WideCharToMultiByte( CP_OEMCP, 0, dataW->cFileName, -1,
2715 dataA->cFileName, sizeof(dataA->cFileName), NULL, NULL );
2716 WideCharToMultiByte( CP_OEMCP, 0, dataW->cAlternateFileName, -1,
2717 dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName), NULL, NULL );
2720 /***********************************************************************
2721 * INT21_LongFilename
2723 * Handler for function 0x71.
2725 static void INT21_LongFilename( CONTEXT *context )
2727 BOOL bSetDOSExtendedError = FALSE;
2728 WCHAR pathW[MAX_PATH];
2729 char* pathA;
2731 switch (AL_reg(context))
2733 case 0x0d: /* RESET DRIVE */
2734 INT_BARF( context, 0x21 );
2735 break;
2737 case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
2738 if (!INT21_CreateDirectory( context ))
2739 bSetDOSExtendedError = TRUE;
2740 break;
2742 case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
2743 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2745 TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", pathA);
2746 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2747 if (!RemoveDirectoryW( pathW )) bSetDOSExtendedError = TRUE;
2748 break;
2750 case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
2751 if (!INT21_SetCurrentDirectory( context ))
2752 bSetDOSExtendedError = TRUE;
2753 break;
2755 case 0x41: /* LONG FILENAME - DELETE FILE */
2756 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2758 TRACE( "LONG FILENAME - DELETE FILE %s\n", pathA );
2759 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2761 if (!DeleteFileW( pathW )) bSetDOSExtendedError = TRUE;
2762 break;
2764 case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
2765 if (!INT21_FileAttributes( context, BL_reg(context), TRUE ))
2766 bSetDOSExtendedError = TRUE;
2767 break;
2769 case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
2770 if (!INT21_GetCurrentDirectory( context, TRUE ))
2771 bSetDOSExtendedError = TRUE;
2772 break;
2774 case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
2776 HANDLE handle;
2777 HGLOBAL16 h16;
2778 WIN32_FIND_DATAW dataW;
2779 WIN32_FIND_DATAA* dataA;
2781 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
2782 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n", pathA);
2784 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2785 handle = FindFirstFileW(pathW, &dataW);
2787 dataA = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
2788 if (handle != INVALID_HANDLE_VALUE &&
2789 (h16 = GlobalAlloc16(GMEM_MOVEABLE, sizeof(handle))))
2791 HANDLE* ptr = GlobalLock16( h16 );
2792 *ptr = handle;
2793 GlobalUnlock16( h16 );
2794 SET_AX( context, h16 );
2795 INT21_ConvertFindDataWtoA(dataA, &dataW);
2797 else
2799 if (handle != INVALID_HANDLE_VALUE) FindClose(handle);
2800 SET_AX( context, INVALID_HANDLE_VALUE16);
2801 bSetDOSExtendedError = TRUE;
2804 break;
2806 case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
2808 HGLOBAL16 h16 = BX_reg(context);
2809 HANDLE* ptr;
2810 WIN32_FIND_DATAW dataW;
2811 WIN32_FIND_DATAA* dataA;
2813 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
2814 BX_reg(context));
2816 dataA = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
2818 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
2820 if (!FindNextFileW(*ptr, &dataW)) bSetDOSExtendedError = TRUE;
2821 else INT21_ConvertFindDataWtoA(dataA, &dataW);
2822 GlobalUnlock16( h16 );
2824 else
2826 SetLastError( ERROR_INVALID_HANDLE );
2827 bSetDOSExtendedError = TRUE;
2830 break;
2832 case 0x56: /* LONG FILENAME - RENAME FILE */
2833 if (!INT21_RenameFile(context))
2834 bSetDOSExtendedError = TRUE;
2835 break;
2837 case 0x60: /* LONG FILENAME - CONVERT PATH */
2839 WCHAR res[MAX_PATH];
2841 switch (CL_reg(context))
2843 case 0x00: /* "truename" - Canonicalize path */
2845 * FIXME: This is not 100% equal to 0x01 case,
2846 * if you fix this, fix int21 subfunction 0x60, too.
2849 case 0x01: /* Get short filename or path */
2850 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
2851 if (!GetShortPathNameW(pathW, res, 67))
2852 bSetDOSExtendedError = TRUE;
2853 else
2855 SET_AX( context, 0 );
2856 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
2857 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
2858 67, NULL, NULL);
2860 break;
2862 case 0x02: /* Get canonical long filename or path */
2863 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
2864 if (!GetFullPathNameW(pathW, 128, res, NULL))
2865 bSetDOSExtendedError = TRUE;
2866 else
2868 SET_AX( context, 0 );
2869 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
2870 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
2871 128, NULL, NULL);
2873 break;
2874 default:
2875 FIXME("Unimplemented long file name function:\n");
2876 INT_BARF( context, 0x21 );
2877 SET_CFLAG(context);
2878 SET_AL( context, 0 );
2879 break;
2882 break;
2884 case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
2885 if (!INT21_CreateFile( context, context->Esi, TRUE,
2886 BX_reg(context), DL_reg(context) ))
2887 bSetDOSExtendedError = TRUE;
2888 break;
2890 case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
2892 DWORD filename_len, flags;
2893 WCHAR dstW[8];
2895 pathA = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
2897 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", pathA);
2898 SET_AX( context, 0 );
2899 MultiByteToWideChar(CP_OEMCP, 0, pathA, -1, pathW, MAX_PATH);
2900 if (!GetVolumeInformationW( pathW, NULL, 0, NULL, &filename_len,
2901 &flags, dstW, 8 ))
2903 INT_BARF( context, 0x21 );
2904 SET_CFLAG(context);
2905 break;
2907 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
2908 SET_CX( context, filename_len );
2909 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
2910 WideCharToMultiByte(CP_OEMCP, 0, dstW, -1,
2911 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
2912 8, NULL, NULL);
2914 break;
2916 case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
2918 HGLOBAL16 h16 = BX_reg(context);
2919 HANDLE* ptr;
2921 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
2922 BX_reg(context));
2923 if (h16 != INVALID_HANDLE_VALUE16 && (ptr = GlobalLock16( h16 )))
2925 if (!FindClose( *ptr )) bSetDOSExtendedError = TRUE;
2926 GlobalUnlock16( h16 );
2927 GlobalFree16( h16 );
2929 else
2931 SetLastError( ERROR_INVALID_HANDLE );
2932 bSetDOSExtendedError = TRUE;
2935 break;
2937 case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
2939 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2940 BY_HANDLE_FILE_INFORMATION *info =
2941 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2943 TRACE( "LONG FILENAME - GET FILE INFO BY HANDLE\n" );
2945 if (!GetFileInformationByHandle(handle, info))
2946 bSetDOSExtendedError = TRUE;
2948 break;
2950 case 0xa7: /* LONG FILENAME - CONVERT TIME */
2951 switch (BL_reg(context))
2953 case 0x00: /* FILE TIME TO DOS TIME */
2955 WORD date, time;
2956 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
2957 context->SegDs,
2958 context->Esi);
2960 TRACE( "LONG FILENAME - FILE TIME TO DOS TIME\n" );
2962 FileTimeToDosDateTime( filetime, &date, &time );
2964 SET_DX( context, date );
2965 SET_CX( context, time );
2968 * FIXME: BH has number of 10-millisecond units
2969 * past time in CX.
2971 SET_BH( context, 0 );
2973 break;
2975 case 0x01: /* DOS TIME TO FILE TIME */
2977 FILETIME *filetime = CTX_SEG_OFF_TO_LIN(context,
2978 context->SegEs,
2979 context->Edi);
2981 TRACE( "LONG FILENAME - DOS TIME TO FILE TIME\n" );
2984 * FIXME: BH has number of 10-millisecond units
2985 * past time in CX.
2987 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
2988 filetime );
2990 break;
2992 default:
2993 INT_BARF( context, 0x21 );
2994 break;
2996 break;
2998 case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
2999 case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
3000 case 0xaa: /* LONG FILENAME - SUBST */
3001 default:
3002 FIXME("Unimplemented long file name function:\n");
3003 INT_BARF( context, 0x21 );
3004 SET_CFLAG(context);
3005 SET_AL( context, 0 );
3006 break;
3009 if (bSetDOSExtendedError)
3011 SET_AX( context, GetLastError() );
3012 SET_CFLAG( context );
3017 /***********************************************************************
3018 * INT21_RenameFile
3020 * Handler for:
3021 * - function 0x56
3022 * - subfunction 0x56 of function 0x71
3023 * - subfunction 0xff of function 0x43 (CL == 0x56)
3025 static BOOL INT21_RenameFile( CONTEXT *context )
3027 WCHAR fromW[MAX_PATH];
3028 WCHAR toW[MAX_PATH];
3029 char *fromA = CTX_SEG_OFF_TO_LIN(context,
3030 context->SegDs,context->Edx);
3031 char *toA = CTX_SEG_OFF_TO_LIN(context,
3032 context->SegEs,context->Edi);
3034 TRACE( "RENAME FILE %s to %s\n", fromA, toA );
3035 MultiByteToWideChar(CP_OEMCP, 0, fromA, -1, fromW, MAX_PATH);
3036 MultiByteToWideChar(CP_OEMCP, 0, toA, -1, toW, MAX_PATH);
3038 return MoveFileW( fromW, toW );
3042 /***********************************************************************
3043 * INT21_NetworkFunc
3045 * Handler for:
3046 * - function 0x5e
3048 static BOOL INT21_NetworkFunc (CONTEXT *context)
3050 switch (AL_reg(context))
3052 case 0x00: /* Get machine name. */
3054 WCHAR dstW[MAX_COMPUTERNAME_LENGTH + 1];
3055 DWORD s = ARRAY_SIZE(dstW);
3056 int len;
3058 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
3059 TRACE("getting machine name to %p\n", dst);
3060 if (!GetComputerNameW(dstW, &s) ||
3061 !WideCharToMultiByte(CP_OEMCP, 0, dstW, -1, dst, 16, NULL, NULL))
3063 WARN("failed!\n");
3064 SetLastError( ER_NoNetwork );
3065 return TRUE;
3067 for (len = strlen(dst); len < 15; len++) dst[len] = ' ';
3068 dst[15] = 0;
3069 SET_CH( context, 1 ); /* Valid */
3070 SET_CL( context, 1 ); /* NETbios number??? */
3071 TRACE("returning %s\n", debugstr_an(dst, 16));
3072 return FALSE;
3075 default:
3076 SetLastError( ER_NoNetwork );
3077 return TRUE;
3081 /******************************************************************
3082 * INT21_GetDiskSerialNumber
3085 static int INT21_GetDiskSerialNumber( CONTEXT *context )
3087 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3088 WCHAR path[] = {'A',':','\\',0}, label[11];
3089 DWORD serial;
3091 path[0] += INT21_MapDrive(BL_reg(context));
3092 if (!GetVolumeInformationW( path, label, 11, &serial, NULL, NULL, NULL, 0))
3094 SetLastError( ERROR_INVALID_DRIVE );
3095 return 0;
3098 *(WORD *)dataptr = 0;
3099 memcpy(dataptr + 2, &serial, sizeof(DWORD));
3100 WideCharToMultiByte(CP_OEMCP, 0, label, 11, (LPSTR)dataptr + 6, 11, NULL, NULL);
3101 memcpy(dataptr + 17, "FAT16 ", 8);
3102 return 1;
3106 /******************************************************************
3107 * INT21_SetDiskSerialNumber
3110 static BOOL INT21_SetDiskSerialNumber( CONTEXT *context )
3112 #if 0
3113 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3114 int drive = INT21_MapDrive(BL_reg(context));
3116 if (!is_valid_drive(drive))
3118 SetLastError( ERROR_INVALID_DRIVE );
3119 return FALSE;
3122 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
3123 return TRUE;
3124 #else
3125 FIXME("Setting drive serial number is no longer supported\n");
3126 SetLastError( ERROR_NOT_SUPPORTED );
3127 return FALSE;
3128 #endif
3132 /******************************************************************
3133 * INT21_GetFreeDiskSpace
3136 static BOOL INT21_GetFreeDiskSpace( CONTEXT *context )
3138 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
3139 WCHAR root[] = {'A',':','\\',0};
3140 const DWORD max_clusters = 0x3d83;
3141 const DWORD max_sectors_per_cluster = 0x7f;
3142 const DWORD max_bytes_per_sector = 0x200;
3144 root[0] += INT21_MapDrive(DL_reg(context));
3145 if (!GetDiskFreeSpaceW( root, &cluster_sectors, &sector_bytes,
3146 &free_clusters, &total_clusters ))
3147 return FALSE;
3149 /* Some old win31 apps (Lotus SmartSuite 5.1) crap out if there's too
3150 * much disk space, so Windows XP seems to apply the following limits:
3151 * cluster_sectors <= 0x7f
3152 * sector size <= 0x200
3153 * clusters <= 0x3D83
3154 * This means total reported space is limited to about 1GB.
3157 /* Make sure bytes-per-sector is in [, max] */
3158 while (sector_bytes > max_bytes_per_sector) {
3159 sector_bytes >>= 1;
3160 free_clusters <<= 1;
3161 total_clusters <<= 1;
3163 /* Then make sure sectors-per-cluster is in [max/2, max]. */
3164 while (cluster_sectors <= max_sectors_per_cluster/2) {
3165 cluster_sectors <<= 1;
3166 free_clusters >>= 1;
3167 total_clusters >>= 1;
3169 while (cluster_sectors > max_sectors_per_cluster) {
3170 cluster_sectors >>= 1;
3171 free_clusters <<= 1;
3172 total_clusters <<= 1;
3175 /* scale up sectors_per_cluster to exactly max_sectors_per_cluster.
3176 * We could skip this, but that would impose an artificially low
3177 * limit on reported disk space.
3178 * To avoid overflow, first apply a preliminary cap on sector count;
3179 * this will not affect the correctness of the final result,
3180 * because if the preliminary cap hits, the final one will, too.
3182 if (total_clusters > 4 * max_clusters)
3183 total_clusters = 4 * max_clusters;
3184 if (free_clusters > 4 * max_clusters)
3185 free_clusters = 4 * max_clusters;
3186 if (cluster_sectors < max_sectors_per_cluster) {
3187 free_clusters *= cluster_sectors;
3188 free_clusters /= max_sectors_per_cluster;
3189 total_clusters *= cluster_sectors;
3190 total_clusters /= max_sectors_per_cluster;
3191 cluster_sectors = max_sectors_per_cluster;
3194 /* Finally, apply real cluster count cap. */
3195 if (total_clusters > max_clusters)
3196 total_clusters = max_clusters;
3197 if (free_clusters > max_clusters)
3198 free_clusters = max_clusters;
3200 SET_AX( context, cluster_sectors );
3201 SET_BX( context, free_clusters );
3202 SET_CX( context, sector_bytes );
3203 SET_DX( context, total_clusters );
3204 return TRUE;
3207 /******************************************************************
3208 * INT21_GetDriveAllocInfo
3211 static BOOL INT21_GetDriveAllocInfo( CONTEXT *context, BYTE drive )
3213 INT21_DPB *dpb;
3215 drive = INT21_MapDrive( drive );
3216 if (!INT21_FillDrivePB( drive )) return FALSE;
3217 dpb = &(INT21_GetHeapPointer()->misc_dpb_list[drive]);
3218 SET_AL( context, dpb->cluster_sectors + 1 );
3219 SET_CX( context, dpb->sector_bytes );
3220 SET_DX( context, dpb->num_clusters1 );
3222 context->SegDs = INT21_GetHeapSelector( context );
3223 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive].media_ID ) );
3224 return TRUE;
3227 /***********************************************************************
3228 * INT21_GetExtendedError
3230 static void INT21_GetExtendedError( CONTEXT *context )
3232 BYTE class, action, locus;
3233 WORD error = GetLastError();
3235 switch(error)
3237 case ERROR_SUCCESS:
3238 class = action = locus = 0;
3239 break;
3240 case ERROR_DIR_NOT_EMPTY:
3241 class = EC_Exists;
3242 action = SA_Ignore;
3243 locus = EL_Disk;
3244 break;
3245 case ERROR_ACCESS_DENIED:
3246 class = EC_AccessDenied;
3247 action = SA_Abort;
3248 locus = EL_Disk;
3249 break;
3250 case ERROR_CANNOT_MAKE:
3251 class = EC_AccessDenied;
3252 action = SA_Abort;
3253 locus = EL_Unknown;
3254 break;
3255 case ERROR_DISK_FULL:
3256 case ERROR_HANDLE_DISK_FULL:
3257 class = EC_MediaError;
3258 action = SA_Abort;
3259 locus = EL_Disk;
3260 break;
3261 case ERROR_FILE_EXISTS:
3262 case ERROR_ALREADY_EXISTS:
3263 class = EC_Exists;
3264 action = SA_Abort;
3265 locus = EL_Disk;
3266 break;
3267 case ERROR_FILE_NOT_FOUND:
3268 case ERROR_PATH_NOT_FOUND:
3269 case ERROR_INVALID_NAME:
3270 class = EC_NotFound;
3271 action = SA_Abort;
3272 locus = EL_Disk;
3273 break;
3274 case ERROR_GEN_FAILURE:
3275 class = EC_SystemFailure;
3276 action = SA_Abort;
3277 locus = EL_Unknown;
3278 break;
3279 case ERROR_INVALID_DRIVE:
3280 class = EC_MediaError;
3281 action = SA_Abort;
3282 locus = EL_Disk;
3283 break;
3284 case ERROR_INVALID_HANDLE:
3285 class = EC_ProgramError;
3286 action = SA_Abort;
3287 locus = EL_Disk;
3288 break;
3289 case ERROR_LOCK_VIOLATION:
3290 class = EC_AccessDenied;
3291 action = SA_Abort;
3292 locus = EL_Disk;
3293 break;
3294 case ERROR_NO_MORE_FILES:
3295 class = EC_MediaError;
3296 action = SA_Abort;
3297 locus = EL_Disk;
3298 break;
3299 case ER_NoNetwork:
3300 class = EC_NotFound;
3301 action = SA_Abort;
3302 locus = EL_Network;
3303 break;
3304 case ERROR_NOT_ENOUGH_MEMORY:
3305 class = EC_OutOfResource;
3306 action = SA_Abort;
3307 locus = EL_Memory;
3308 break;
3309 case ERROR_SEEK:
3310 class = EC_NotFound;
3311 action = SA_Ignore;
3312 locus = EL_Disk;
3313 break;
3314 case ERROR_SHARING_VIOLATION:
3315 class = EC_Temporary;
3316 action = SA_Retry;
3317 locus = EL_Disk;
3318 break;
3319 case ERROR_TOO_MANY_OPEN_FILES:
3320 class = EC_ProgramError;
3321 action = SA_Abort;
3322 locus = EL_Disk;
3323 break;
3324 default:
3325 FIXME("Unknown error %d\n", error );
3326 class = EC_SystemFailure;
3327 action = SA_Abort;
3328 locus = EL_Unknown;
3329 break;
3331 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
3332 error, class, action, locus );
3333 SET_AX( context, error );
3334 SET_BH( context, class );
3335 SET_BL( context, action );
3336 SET_CH( context, locus );
3339 static BOOL INT21_CreateTempFile( CONTEXT *context )
3341 static int counter = 0;
3342 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
3343 char *p = name + strlen(name);
3345 /* despite what Ralf Brown says, some programs seem to call without
3346 * ending backslash (DOS accepts that, so we accept it too) */
3347 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
3349 for (;;)
3351 sprintf( p, "wine%04x.%03d", GetCurrentThreadId(), counter );
3352 counter = (counter + 1) % 1000;
3354 SET_AX( context,
3355 Win32HandleToDosFileHandle(
3356 CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
3357 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
3358 CREATE_NEW, 0, 0 ) ) );
3359 if (AX_reg(context) != HFILE_ERROR16)
3361 TRACE("created %s\n", name );
3362 return TRUE;
3364 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
3368 /***********************************************************************
3369 * DOSFS_ToDosFCBFormat
3371 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
3372 * expanding wild cards and converting to upper-case in the process.
3373 * File name can be terminated by '\0', '\\' or '/'.
3374 * Return FALSE if the name is not a valid DOS name.
3375 * 'buffer' must be at least 12 characters long.
3377 /* Chars we don't want to see in DOS file names */
3378 static BOOL INT21_ToDosFCBFormat( LPCWSTR name, LPWSTR buffer )
3380 static const WCHAR invalid_chars[] = {'*','?','<','>','|','\\','"','+','=',',',';','[',']',' ','\345',0};
3381 LPCWSTR p = name;
3382 int i;
3384 /* Check for "." and ".." */
3385 if (*p == '.')
3387 p++;
3388 buffer[0] = '.';
3389 for(i = 1; i < 11; i++) buffer[i] = ' ';
3390 buffer[11] = 0;
3391 if (*p == '.')
3393 buffer[1] = '.';
3394 p++;
3396 return (!*p || (*p == '/') || (*p == '\\'));
3399 for (i = 0; i < 8; i++)
3401 switch(*p)
3403 case '\0':
3404 case '\\':
3405 case '/':
3406 case '.':
3407 buffer[i] = ' ';
3408 break;
3409 case '?':
3410 p++;
3411 /* fall through */
3412 case '*':
3413 buffer[i] = '?';
3414 break;
3415 default:
3416 if (wcschr( invalid_chars, *p )) return FALSE;
3417 buffer[i] = *p++;
3418 break;
3422 if (*p == '*')
3424 /* Skip all chars after wildcard up to first dot */
3425 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
3427 else
3429 /* Check if name too long */
3430 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return FALSE;
3432 if (*p == '.') p++; /* Skip dot */
3434 for (i = 8; i < 11; i++)
3436 switch(*p)
3438 case '\0':
3439 case '\\':
3440 case '/':
3441 buffer[i] = ' ';
3442 break;
3443 case '.':
3444 return FALSE; /* Second extension not allowed */
3445 case '?':
3446 p++;
3447 /* fall through */
3448 case '*':
3449 buffer[i] = '?';
3450 break;
3451 default:
3452 if (wcschr( invalid_chars, *p )) return FALSE;
3453 buffer[i] = *p++;
3454 break;
3457 buffer[11] = '\0';
3458 wcsupr( buffer );
3460 /* at most 3 character of the extension are processed
3461 * is something behind this ?
3463 while (*p == '*' || *p == ' ') p++; /* skip wildcards and spaces */
3464 return (!*p || (*p == '/') || (*p == '\\'));
3467 static HANDLE INT21_FindHandle;
3468 static const WCHAR *INT21_FindPath; /* will point to current dta->fullPath search */
3470 /******************************************************************
3471 * INT21_FindFirst
3473 static BOOL INT21_FindFirst( CONTEXT *context )
3475 WCHAR *p, *q;
3476 const char *path;
3477 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3478 WCHAR maskW[12], pathW[MAX_PATH];
3479 static const WCHAR wildcardW[] = {'*','.','*',0};
3481 path = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3482 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
3484 p = wcsrchr( pathW, '\\');
3485 q = wcsrchr( pathW, '/');
3486 if (q>p) p = q;
3487 if (!p)
3489 if (pathW[0] && pathW[1] == ':') p = pathW + 2;
3490 else p = pathW;
3492 else p++;
3494 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
3495 * (doesn't matter as it is set below anyway)
3497 if (!INT21_ToDosFCBFormat( p, maskW ))
3499 SetLastError( ERROR_FILE_NOT_FOUND );
3500 SET_AX( context, ERROR_FILE_NOT_FOUND );
3501 SET_CFLAG(context);
3502 return FALSE;
3504 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
3506 dta->fullPath = HeapAlloc( GetProcessHeap(), 0, sizeof(wildcardW) + (p - pathW)*sizeof(WCHAR) );
3507 memcpy( dta->fullPath, pathW, (p - pathW) * sizeof(WCHAR) );
3508 memcpy( dta->fullPath + (p - pathW), wildcardW, sizeof(wildcardW) );
3509 /* we must have a fully qualified file name in dta->fullPath
3510 * (we could have a UNC path, but this would lead to some errors later on)
3512 dta->drive = drive_number( dta->fullPath[0] );
3513 dta->count = 0;
3514 dta->search_attr = CL_reg(context);
3515 return TRUE;
3518 /******************************************************************
3519 * match_short
3521 * Check is a short path name (DTA unicode) matches a mask (FCB ansi)
3523 static BOOL match_short(LPCWSTR shortW, LPCSTR maskA)
3525 WCHAR mask[11], file[12];
3526 int i;
3528 if (!INT21_ToDosFCBFormat( shortW, file )) return FALSE;
3529 MultiByteToWideChar(CP_OEMCP, 0, maskA, 11, mask, 11);
3530 for (i = 0; i < 11; i++)
3531 if (mask[i] != '?' && mask[i] != file[i]) return FALSE;
3532 return TRUE;
3535 static unsigned INT21_FindHelper(LPCWSTR fullPath, unsigned drive, unsigned count,
3536 LPCSTR mask, unsigned search_attr,
3537 WIN32_FIND_DATAW* entry)
3539 unsigned ncalls;
3541 if ((search_attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
3543 WCHAR path[] = {' ',':','\\',0};
3545 if (count) return 0;
3546 path[0] = drive + 'A';
3547 if (!GetVolumeInformationW(path, entry->cAlternateFileName, 13, NULL, NULL, NULL, NULL, 0)) return 0;
3548 if (!entry->cAlternateFileName[0]) return 0;
3549 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftCreationTime );
3550 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftLastAccessTime );
3551 RtlSecondsSince1970ToTime( 0, (LARGE_INTEGER *)&entry->ftLastWriteTime );
3552 entry->dwFileAttributes = FA_LABEL;
3553 entry->nFileSizeHigh = entry->nFileSizeLow = 0;
3554 TRACE("returning %s as label\n", debugstr_w(entry->cAlternateFileName));
3555 return 1;
3558 if (!INT21_FindHandle || INT21_FindPath != fullPath || count == 0)
3560 if (INT21_FindHandle) FindClose(INT21_FindHandle);
3561 INT21_FindHandle = FindFirstFileW(fullPath, entry);
3562 if (INT21_FindHandle == INVALID_HANDLE_VALUE)
3564 INT21_FindHandle = 0;
3565 return 0;
3567 INT21_FindPath = fullPath;
3568 /* we need to resync search */
3569 ncalls = count;
3571 else ncalls = 1;
3573 while (ncalls-- != 0)
3575 if (!FindNextFileW(INT21_FindHandle, entry))
3577 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3578 return 0;
3581 while (count < 0xffff)
3583 count++;
3584 /* Check the file attributes, and path */
3585 if (!(entry->dwFileAttributes & ~search_attr) &&
3586 match_short(entry->cAlternateFileName[0] ? entry->cAlternateFileName : entry->cFileName,
3587 mask))
3589 return count;
3591 if (!FindNextFileW(INT21_FindHandle, entry))
3593 FindClose(INT21_FindHandle); INT21_FindHandle = 0;
3594 return 0;
3597 WARN("Too many directory entries in %s\n", debugstr_w(fullPath) );
3598 return 0;
3601 /******************************************************************
3602 * INT21_FindNext
3604 static BOOL INT21_FindNext( CONTEXT *context )
3606 FINDFILE_DTA *dta = (FINDFILE_DTA *)INT21_GetCurrentDTA(context);
3607 DWORD attr = dta->search_attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
3608 WIN32_FIND_DATAW entry;
3609 int n;
3611 if (!dta->fullPath) return FALSE;
3613 n = INT21_FindHelper(dta->fullPath, dta->drive, dta->count,
3614 dta->mask, attr, &entry);
3615 if (n)
3617 dta->fileattr = entry.dwFileAttributes;
3618 dta->filesize = entry.nFileSizeLow;
3619 FileTimeToDosDateTime( &entry.ftLastWriteTime, &dta->filedate, &dta->filetime );
3620 if (entry.cAlternateFileName[0])
3621 WideCharToMultiByte(CP_OEMCP, 0, entry.cAlternateFileName, -1,
3622 dta->filename, 13, NULL, NULL);
3623 else
3624 WideCharToMultiByte(CP_OEMCP, 0, entry.cFileName, -1, dta->filename, 13, NULL, NULL);
3626 if (!memchr(dta->mask,'?',11))
3628 /* wildcardless search, release resources in case no findnext will
3629 * be issued, and as a workaround in case file creation messes up
3630 * findnext, as sometimes happens with pkunzip
3632 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3633 INT21_FindPath = dta->fullPath = NULL;
3635 dta->count = n;
3636 return TRUE;
3638 HeapFree( GetProcessHeap(), 0, dta->fullPath );
3639 INT21_FindPath = dta->fullPath = NULL;
3640 return FALSE;
3643 /* microsoft's programmers should be shot for using CP/M style int21
3644 calls in Windows for Workgroup's winfile.exe */
3646 /******************************************************************
3647 * INT21_FindFirstFCB
3650 static BOOL INT21_FindFirstFCB( CONTEXT *context )
3652 BYTE *fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3653 FINDFILE_FCB *pFCB;
3654 int drive;
3655 WCHAR p[] = {' ',':',};
3657 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
3658 else pFCB = (FINDFILE_FCB *)fcb;
3659 drive = INT21_MapDrive( pFCB->drive );
3660 if (drive == MAX_DOS_DRIVES) return FALSE;
3662 p[0] = 'A' + drive;
3663 pFCB->fullPath = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
3664 if (!pFCB->fullPath) return FALSE;
3665 GetLongPathNameW(p, pFCB->fullPath, MAX_PATH);
3666 pFCB->count = 0;
3667 return TRUE;
3670 /******************************************************************
3671 * INT21_FindNextFCB
3674 static BOOL INT21_FindNextFCB( CONTEXT *context )
3676 BYTE *fcb = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
3677 FINDFILE_FCB *pFCB;
3678 LPBYTE pResult = INT21_GetCurrentDTA(context);
3679 DOS_DIRENTRY_LAYOUT *ddl;
3680 WIN32_FIND_DATAW entry;
3681 BYTE attr;
3682 int n;
3683 WCHAR nameW[12];
3685 if (*fcb == 0xff) /* extended FCB ? */
3687 attr = fcb[6];
3688 pFCB = (FINDFILE_FCB *)(fcb + 7);
3690 else
3692 attr = 0;
3693 pFCB = (FINDFILE_FCB *)fcb;
3696 if (!pFCB->fullPath) return FALSE;
3697 n = INT21_FindHelper(pFCB->fullPath, INT21_MapDrive( pFCB->drive ),
3698 pFCB->count, pFCB->filename, attr, &entry);
3699 if (!n)
3701 HeapFree( GetProcessHeap(), 0, pFCB->fullPath );
3702 INT21_FindPath = pFCB->fullPath = NULL;
3703 return FALSE;
3705 pFCB->count += n;
3707 if (*fcb == 0xff)
3709 /* place extended FCB header before pResult if called with extended FCB */
3710 *pResult = 0xff;
3711 pResult += 6; /* leave reserved field behind */
3712 *pResult++ = entry.dwFileAttributes;
3714 *pResult++ = INT21_MapDrive( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
3715 ddl = (DOS_DIRENTRY_LAYOUT*)pResult;
3716 ddl->fileattr = entry.dwFileAttributes;
3717 ddl->cluster = 0; /* what else? */
3718 ddl->filesize = entry.nFileSizeLow;
3719 memset( ddl->reserved, 0, sizeof(ddl->reserved) );
3720 FileTimeToDosDateTime( &entry.ftLastWriteTime,
3721 &ddl->filedate, &ddl->filetime );
3723 /* Convert file name to FCB format */
3724 if (entry.cAlternateFileName[0])
3725 INT21_ToDosFCBFormat( entry.cAlternateFileName, nameW );
3726 else
3727 INT21_ToDosFCBFormat( entry.cFileName, nameW );
3728 WideCharToMultiByte(CP_OEMCP, 0, nameW, 11, ddl->filename, 11, NULL, NULL);
3729 return TRUE;
3733 /******************************************************************
3734 * INT21_ParseFileNameIntoFCB
3737 static void INT21_ParseFileNameIntoFCB( CONTEXT *context )
3739 char *filename =
3740 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
3741 char *fcb =
3742 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
3743 char *s;
3744 WCHAR *buffer;
3745 WCHAR fcbW[12];
3746 INT buffer_len, len;
3748 SET_AL( context, 0xff ); /* failed */
3750 TRACE("filename: '%s'\n", filename);
3752 s = filename;
3753 while (*s && (*s != ' ') && (*s != '\r') && (*s != '\n'))
3754 s++;
3755 len = filename - s;
3757 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
3758 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
3759 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
3760 buffer[len] = 0;
3761 INT21_ToDosFCBFormat(buffer, fcbW);
3762 HeapFree(GetProcessHeap(), 0, buffer);
3763 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
3764 *fcb = 0;
3765 TRACE("FCB: '%s'\n", fcb + 1);
3767 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
3769 /* point DS:SI to first unparsed character */
3770 SET_SI( context, context->Esi + (int)s - (int)filename );
3773 static BOOL INT21_Dup2(HFILE16 hFile1, HFILE16 hFile2)
3775 HFILE16 res = HFILE_ERROR16;
3776 HANDLE handle, new_handle;
3777 #define DOS_TABLE_SIZE 256
3778 DWORD map[DOS_TABLE_SIZE / 32];
3779 int i;
3781 handle = DosFileHandleToWin32Handle(hFile1);
3782 if (handle == INVALID_HANDLE_VALUE)
3783 return FALSE;
3785 _lclose16(hFile2);
3786 /* now loop to allocate the same one... */
3787 memset(map, 0, sizeof(map));
3788 for (i = 0; i < DOS_TABLE_SIZE; i++)
3790 if (!DuplicateHandle(GetCurrentProcess(), handle,
3791 GetCurrentProcess(), &new_handle,
3792 0, FALSE, DUPLICATE_SAME_ACCESS))
3794 res = HFILE_ERROR16;
3795 break;
3797 res = Win32HandleToDosFileHandle(new_handle);
3798 if (res == HFILE_ERROR16 || res == hFile2) break;
3799 map[res / 32] |= 1 << (res % 32);
3801 /* clean up the allocated slots */
3802 for (i = 0; i < DOS_TABLE_SIZE; i++)
3804 if (map[i / 32] & (1 << (i % 32)))
3805 _lclose16((HFILE16)i);
3807 return res == hFile2;
3811 /***********************************************************************
3812 * DOSVM_Int21Handler
3814 * Interrupt 0x21 handler.
3816 void WINAPI DOSVM_Int21Handler( CONTEXT *context )
3818 BOOL bSetDOSExtendedError = FALSE;
3820 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
3821 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
3822 AX_reg(context), BX_reg(context),
3823 CX_reg(context), DX_reg(context),
3824 SI_reg(context), DI_reg(context),
3825 (WORD)context->SegDs, (WORD)context->SegEs,
3826 context->EFlags );
3829 * Extended error is used by (at least) functions 0x2f to 0x62.
3830 * Function 0x59 returns extended error and error should not
3831 * be cleared before handling the function.
3833 if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59)
3834 SetLastError(0);
3836 RESET_CFLAG(context); /* Not sure if this is a good idea. */
3838 switch(AH_reg(context))
3840 case 0x00: /* TERMINATE PROGRAM */
3841 TRACE("TERMINATE PROGRAM\n");
3842 DOSVM_Exit( 0 );
3843 break;
3845 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
3846 TRACE("DIRECT CHARACTER INPUT WITH ECHO - not supported\n");
3847 break;
3849 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
3850 TRACE("Write Character to Standard Output\n");
3851 break;
3853 case 0x03: /* READ CHARACTER FROM STDAUX */
3854 case 0x04: /* WRITE CHARACTER TO STDAUX */
3855 case 0x05: /* WRITE CHARACTER TO PRINTER */
3856 INT_BARF( context, 0x21 );
3857 break;
3859 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
3860 if (DL_reg(context) == 0xff)
3862 TRACE("Direct Console Input\n");
3864 /* no character available */
3865 SET_AL( context, 0 );
3866 SET_ZFLAG( context );
3868 else
3870 TRACE("Direct Console Output\n");
3872 * At least DOS versions 2.1-7.0 return character
3873 * that was written in AL register.
3875 SET_AL( context, DL_reg(context) );
3877 break;
3879 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
3880 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO - not supported\n");
3881 SET_AL( context, 0 );
3882 break;
3884 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
3885 TRACE("CHARACTER INPUT WITHOUT ECHO - not supported\n");
3886 SET_AL( context, 0 );
3887 break;
3889 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
3890 TRACE("WRITE '$'-terminated string from %04X:%04X to stdout\n",
3891 context->SegDs, DX_reg(context) );
3893 LPSTR data = CTX_SEG_OFF_TO_LIN( context,
3894 context->SegDs, context->Edx );
3895 LPSTR p = data;
3896 DWORD w;
3898 * Do NOT use strchr() to calculate the string length,
3899 * as '\0' is valid string content, too!
3900 * Maybe we should check for non-'$' strings, but DOS doesn't.
3902 while (*p != '$') p++;
3904 WriteFile( DosFileHandleToWin32Handle(1), data, p - data, &w, NULL );
3905 SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
3907 break;
3909 case 0x0a: /* BUFFERED INPUT */
3910 TRACE( "BUFFERED INPUT - not supported\n" );
3911 break;
3913 case 0x0b: /* GET STDIN STATUS */
3914 TRACE( "GET STDIN STATUS - not supported\n" );
3915 SET_AL( context, 0 ); /* no character available */
3916 break;
3918 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
3920 BYTE al = AL_reg(context); /* Input function to execute after flush. */
3922 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
3924 /* FIXME: buffers are not flushed */
3927 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
3928 * int21 function identified by AL will be called.
3930 if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
3932 SET_AH( context, al );
3933 DOSVM_Int21Handler( context );
3936 break;
3938 case 0x0d: /* DISK BUFFER FLUSH */
3939 TRACE("DISK BUFFER FLUSH ignored\n");
3940 break;
3942 case 0x0e: /* SELECT DEFAULT DRIVE */
3943 TRACE( "SELECT DEFAULT DRIVE - %c:\n", 'A' + DL_reg(context) );
3944 INT21_SetCurrentDrive( DL_reg(context) );
3945 SET_AL( context, MAX_DOS_DRIVES );
3946 break;
3948 case 0x0f: /* OPEN FILE USING FCB */
3949 INT21_OpenFileUsingFCB( context );
3950 break;
3952 case 0x10: /* CLOSE FILE USING FCB */
3953 INT21_CloseFileUsingFCB( context );
3954 break;
3956 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
3957 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
3958 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
3959 if (!INT21_FindFirstFCB(context))
3961 SET_AL( context, 0xff );
3962 break;
3964 /* else fall through */
3966 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
3967 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
3968 break;
3970 case 0x13: /* DELETE FILE USING FCB */
3971 INT_BARF( context, 0x21 );
3972 break;
3974 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
3975 INT21_SequentialReadFromFCB( context );
3976 break;
3978 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
3979 INT21_SequentialWriteToFCB( context );
3980 break;
3982 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
3983 case 0x17: /* RENAME FILE USING FCB */
3984 INT_BARF( context, 0x21 );
3985 break;
3987 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
3988 SET_AL( context, 0 );
3989 break;
3991 case 0x19: /* GET CURRENT DEFAULT DRIVE */
3992 SET_AL( context, INT21_GetCurrentDrive() );
3993 TRACE( "GET CURRENT DRIVE -> %c:\n", 'A' + AL_reg( context ) );
3994 break;
3996 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
3997 TRACE( "SET DISK TRANSFER AREA ADDRESS %04X:%04X\n",
3998 context->SegDs, DX_reg(context) );
4000 TDB *task = GlobalLock16( GetCurrentTask() );
4001 task->dta = MAKESEGPTR( context->SegDs, DX_reg(context) );
4003 break;
4005 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
4006 if (!INT21_GetDriveAllocInfo(context, 0))
4007 SET_AX( context, 0xffff );
4008 break;
4010 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
4011 if (!INT21_GetDriveAllocInfo(context, DL_reg(context)))
4012 SET_AX( context, 0xffff );
4013 break;
4015 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4016 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4017 SET_AL( context, 0 );
4018 break;
4020 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
4022 BYTE drive = INT21_MapDrive( 0 );
4023 TRACE( "GET DPB FOR DEFAULT DRIVE\n" );
4025 if (INT21_FillDrivePB( drive ))
4027 SET_AL( context, 0x00 ); /* success */
4028 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4029 context->SegDs = INT21_GetHeapSelector( context );
4031 else
4033 SET_AL( context, 0xff ); /* invalid or network drive */
4036 break;
4038 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4039 SET_AL( context, 0 );
4040 break;
4042 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
4043 INT21_ReadRandomRecordFromFCB( context );
4044 break;
4046 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
4047 INT21_WriteRandomRecordToFCB( context );
4048 break;
4050 case 0x23: /* GET FILE SIZE FOR FCB */
4051 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
4052 INT_BARF( context, 0x21 );
4053 break;
4055 case 0x25: /* SET INTERRUPT VECTOR */
4056 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4058 FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
4059 DOSVM_SetPMHandler16( AL_reg(context), ptr );
4061 break;
4063 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
4064 INT_BARF( context, 0x21 );
4065 break;
4067 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
4068 INT21_RandomBlockReadFromFCB( context );
4069 break;
4071 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
4072 INT21_RandomBlockWriteToFCB( context );
4073 break;
4075 case 0x29: /* PARSE FILENAME INTO FCB */
4076 INT21_ParseFileNameIntoFCB(context);
4077 break;
4079 case 0x2a: /* GET SYSTEM DATE */
4080 TRACE( "GET SYSTEM DATE\n" );
4082 SYSTEMTIME systime;
4083 GetLocalTime( &systime );
4084 SET_CX( context, systime.wYear );
4085 SET_DH( context, systime.wMonth );
4086 SET_DL( context, systime.wDay );
4087 SET_AL( context, systime.wDayOfWeek );
4089 break;
4091 case 0x2b: /* SET SYSTEM DATE */
4092 TRACE( "SET SYSTEM DATE\n" );
4094 WORD year = CX_reg(context);
4095 BYTE month = DH_reg(context);
4096 BYTE day = DL_reg(context);
4098 if (year >= 1980 && year <= 2099 &&
4099 month >= 1 && month <= 12 &&
4100 day >= 1 && day <= 31)
4102 FIXME( "SetSystemDate(%02d/%02d/%04d): not allowed\n",
4103 day, month, year );
4104 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4106 else
4108 SET_AL( context, 0xff ); /* invalid date */
4109 TRACE( "SetSystemDate(%02d/%02d/%04d): invalid date\n",
4110 day, month, year );
4113 break;
4115 case 0x2c: /* GET SYSTEM TIME */
4116 TRACE( "GET SYSTEM TIME\n" );
4118 SYSTEMTIME systime;
4119 GetLocalTime( &systime );
4120 SET_CH( context, systime.wHour );
4121 SET_CL( context, systime.wMinute );
4122 SET_DH( context, systime.wSecond );
4123 SET_DL( context, systime.wMilliseconds / 10 );
4125 break;
4127 case 0x2d: /* SET SYSTEM TIME */
4128 if( CH_reg(context) >= 24 || CL_reg(context) >= 60 || DH_reg(context) >= 60 || DL_reg(context) >= 100 ) {
4129 TRACE("SetSystemTime(%02d:%02d:%02d.%02d): wrong time\n",
4130 CH_reg(context), CL_reg(context),
4131 DH_reg(context), DL_reg(context) );
4132 SET_AL( context, 0xFF );
4134 else
4136 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
4137 CH_reg(context), CL_reg(context),
4138 DH_reg(context), DL_reg(context) );
4139 SET_AL( context, 0 ); /* Let's pretend we succeeded */
4141 break;
4143 case 0x2e: /* SET VERIFY FLAG */
4144 TRACE("SET VERIFY FLAG ignored\n");
4145 /* we cannot change the behaviour anyway, so just ignore it */
4146 break;
4148 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
4149 TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
4151 TDB *task = GlobalLock16( GetCurrentTask() );
4152 context->SegEs = SELECTOROF( task->dta );
4153 SET_BX( context, OFFSETOF( task->dta ) );
4155 break;
4157 case 0x30: /* GET DOS VERSION */
4158 TRACE( "GET DOS VERSION - %s requested\n",
4159 (AL_reg(context) == 0x00) ? "OEM number" : "version flag" );
4161 if (AL_reg(context) == 0x00)
4162 SET_BH( context, 0xff ); /* OEM number => undefined */
4163 else
4164 SET_BH( context, 0x08 ); /* version flag => DOS is in ROM */
4166 SET_AL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
4167 SET_AH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
4169 SET_BL( context, 0x12 ); /* 0x123456 is 24-bit Wine's serial # */
4170 SET_CX( context, 0x3456 );
4171 break;
4173 case 0x31: /* TERMINATE AND STAY RESIDENT */
4174 FIXME("TERMINATE AND STAY RESIDENT stub\n");
4175 break;
4177 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
4179 BYTE drive = INT21_MapDrive( DL_reg(context) );
4180 TRACE( "GET DPB FOR SPECIFIC DRIVE %d\n", DL_reg(context) );
4182 if (INT21_FillDrivePB( drive ))
4184 SET_AL( context, 0x00 ); /* success */
4185 SET_BX( context, offsetof( INT21_HEAP, misc_dpb_list[drive] ) );
4186 context->SegDs = INT21_GetHeapSelector( context );
4188 else
4190 SET_AL( context, 0xff ); /* invalid or network drive */
4193 break;
4195 case 0x33: /* MULTIPLEXED */
4196 switch (AL_reg(context))
4198 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
4199 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
4200 SET_DL( context, brk_flag );
4201 break;
4203 case 0x01: /* SET EXTENDED BREAK STATE */
4204 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
4205 brk_flag = (DL_reg(context) > 0) ? 1 : 0;
4206 break;
4208 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
4209 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
4210 /* ugly coding in order to stay reentrant */
4211 if (DL_reg(context))
4213 SET_DL( context, brk_flag );
4214 brk_flag = 1;
4216 else
4218 SET_DL( context, brk_flag );
4219 brk_flag = 0;
4221 break;
4223 case 0x05: /* GET BOOT DRIVE */
4224 TRACE("GET BOOT DRIVE\n");
4225 SET_DL( context, 3 );
4226 /* c: is Wine's bootdrive (a: is 1)*/
4227 break;
4229 case 0x06: /* GET TRUE VERSION NUMBER */
4230 TRACE("GET TRUE VERSION NUMBER\n");
4231 SET_BL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major */
4232 SET_BH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor */
4233 SET_DL( context, 0x00 ); /* revision */
4234 SET_DH( context, 0x08 ); /* DOS is in ROM */
4235 break;
4237 default:
4238 INT_BARF( context, 0x21 );
4239 break;
4241 break;
4243 case 0x34: /* GET ADDRESS OF INDOS FLAG */
4244 TRACE( "GET ADDRESS OF INDOS FLAG\n" );
4245 context->SegEs = INT21_GetHeapSelector( context );
4246 SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
4247 break;
4249 case 0x35: /* GET INTERRUPT VECTOR */
4250 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
4252 FARPROC16 addr = DOSVM_GetPMHandler16( AL_reg(context) );
4253 context->SegEs = SELECTOROF(addr);
4254 SET_BX( context, OFFSETOF(addr) );
4256 break;
4258 case 0x36: /* GET FREE DISK SPACE */
4259 TRACE("GET FREE DISK SPACE FOR DRIVE %s (limited to about 1GB)\n",
4260 INT21_DriveName( DL_reg(context) ));
4261 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
4262 break;
4264 case 0x37: /* SWITCHAR */
4266 switch (AL_reg(context))
4268 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
4269 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
4270 SET_AL( context, 0x00 ); /* success*/
4271 SET_DL( context, '/' );
4272 break;
4273 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
4274 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
4275 DL_reg( context ));
4276 SET_AL( context, 0x00 ); /* success*/
4277 break;
4278 default:
4279 INT_BARF( context, 0x21 );
4280 break;
4283 break;
4285 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
4286 TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
4287 if (AL_reg(context))
4289 WORD country = AL_reg(context);
4290 if (country == 0xff)
4291 country = BX_reg(context);
4292 if (country != INT21_GetSystemCountryCode()) {
4293 FIXME( "Requested info on non-default country %04x\n", country );
4294 SET_AX(context, 2);
4295 SET_CFLAG(context);
4298 if(AX_reg(context) != 2 )
4300 INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context,
4301 context->SegDs,
4302 context->Edx) );
4303 SET_AX( context, INT21_GetSystemCountryCode() );
4304 SET_BX( context, INT21_GetSystemCountryCode() );
4305 RESET_CFLAG(context);
4307 break;
4309 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
4310 if (!INT21_CreateDirectory( context ))
4311 bSetDOSExtendedError = TRUE;
4312 else
4313 RESET_CFLAG(context);
4314 break;
4316 case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
4318 WCHAR dirW[MAX_PATH];
4319 char *dirA = CTX_SEG_OFF_TO_LIN(context,
4320 context->SegDs, context->Edx);
4322 TRACE( "REMOVE DIRECTORY %s\n", dirA );
4324 MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
4326 if (!RemoveDirectoryW( dirW ))
4327 bSetDOSExtendedError = TRUE;
4328 else
4329 RESET_CFLAG(context);
4331 break;
4333 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
4334 if (!INT21_SetCurrentDirectory( context ))
4335 bSetDOSExtendedError = TRUE;
4336 else
4337 RESET_CFLAG(context);
4338 break;
4340 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
4341 if (!INT21_CreateFile( context, context->Edx, FALSE,
4342 OF_READWRITE | OF_SHARE_COMPAT, 0x12 ))
4343 bSetDOSExtendedError = TRUE;
4344 else
4345 RESET_CFLAG(context);
4346 break;
4348 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
4349 if (!INT21_CreateFile( context, context->Edx, FALSE,
4350 AL_reg(context), 0x01 ))
4351 bSetDOSExtendedError = TRUE;
4352 else
4353 RESET_CFLAG(context);
4354 break;
4356 case 0x3e: /* "CLOSE" - CLOSE FILE */
4357 TRACE( "CLOSE handle %d\n", BX_reg(context) );
4358 if (_lclose16( BX_reg(context) ) == HFILE_ERROR16)
4359 bSetDOSExtendedError = TRUE;
4360 else
4361 RESET_CFLAG(context);
4362 break;
4364 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
4365 TRACE( "READ from %d to %04X:%04X for %d bytes\n",
4366 BX_reg(context),
4367 context->SegDs,
4368 DX_reg(context),
4369 CX_reg(context) );
4371 DWORD result;
4372 WORD count = CX_reg(context);
4373 BYTE *buffer = CTX_SEG_OFF_TO_LIN( context,
4374 context->SegDs,
4375 context->Edx );
4377 /* Some programs pass a count larger than the allocated buffer */
4378 DWORD maxcount = GetSelectorLimit16( context->SegDs ) - DX_reg(context) + 1;
4379 if (count > maxcount) count = maxcount;
4382 * FIXME: Reading from console (BX=1) in DOS mode
4383 * does not work as it is supposed to work.
4386 RESET_CFLAG(context); /* set if error */
4387 if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context)), buffer, count, &result, NULL ))
4388 SET_AX( context, (WORD)result );
4389 else
4390 bSetDOSExtendedError = TRUE;
4392 break;
4394 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
4395 TRACE( "WRITE from %04X:%04X to handle %d for %d byte\n",
4396 context->SegDs, DX_reg(context),
4397 BX_reg(context), CX_reg(context) );
4399 char *ptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4400 HFILE handle = (HFILE)DosFileHandleToWin32Handle(BX_reg(context));
4401 LONG result = _hwrite( handle, ptr, CX_reg(context) );
4402 if (result == HFILE_ERROR)
4403 bSetDOSExtendedError = TRUE;
4404 else
4406 SET_AX( context, (WORD)result );
4407 RESET_CFLAG(context);
4410 break;
4412 case 0x41: /* "UNLINK" - DELETE FILE */
4414 WCHAR fileW[MAX_PATH];
4415 char *fileA = CTX_SEG_OFF_TO_LIN(context,
4416 context->SegDs,
4417 context->Edx);
4419 TRACE( "UNLINK %s\n", fileA );
4420 MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
4422 if (!DeleteFileW( fileW ))
4423 bSetDOSExtendedError = TRUE;
4424 else
4425 RESET_CFLAG(context);
4427 break;
4429 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
4430 TRACE( "LSEEK handle %d offset %d from %s\n",
4431 BX_reg(context),
4432 MAKELONG( DX_reg(context), CX_reg(context) ),
4433 (AL_reg(context) == 0) ?
4434 "start of file" : ((AL_reg(context) == 1) ?
4435 "current file position" : "end of file") );
4437 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4438 LONG offset = MAKELONG( DX_reg(context), CX_reg(context) );
4439 DWORD status = SetFilePointer( handle, offset,
4440 NULL, AL_reg(context) );
4441 if (status == INVALID_SET_FILE_POINTER)
4442 bSetDOSExtendedError = TRUE;
4443 else
4445 SET_AX( context, LOWORD(status) );
4446 SET_DX( context, HIWORD(status) );
4447 RESET_CFLAG(context);
4450 break;
4452 case 0x43: /* FILE ATTRIBUTES */
4453 if (!INT21_FileAttributes( context, AL_reg(context), FALSE ))
4454 bSetDOSExtendedError = TRUE;
4455 else
4456 RESET_CFLAG(context);
4457 break;
4459 case 0x44: /* IOCTL */
4460 INT21_Ioctl( context );
4461 break;
4463 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
4464 TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context) );
4466 HANDLE handle32;
4467 HFILE handle16 = HFILE_ERROR;
4469 if (DuplicateHandle( GetCurrentProcess(),
4470 DosFileHandleToWin32Handle(BX_reg(context)),
4471 GetCurrentProcess(),
4472 &handle32,
4473 0, TRUE, DUPLICATE_SAME_ACCESS ))
4474 handle16 = Win32HandleToDosFileHandle(handle32);
4476 if (handle16 == HFILE_ERROR)
4477 bSetDOSExtendedError = TRUE;
4478 else
4480 SET_AX( context, handle16 );
4481 RESET_CFLAG(context);
4484 break;
4486 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
4487 TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
4488 BX_reg(context), CX_reg(context) );
4489 if (!INT21_Dup2(BX_reg(context), CX_reg(context)))
4490 bSetDOSExtendedError = TRUE;
4491 else
4492 RESET_CFLAG(context);
4493 break;
4495 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
4496 if (!INT21_GetCurrentDirectory( context, FALSE ))
4497 bSetDOSExtendedError = TRUE;
4498 else
4499 RESET_CFLAG(context);
4500 break;
4502 case 0x48: /* ALLOCATE MEMORY */
4503 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
4505 DWORD bytes = (DWORD)BX_reg(context) << 4;
4506 DWORD rv = GlobalDOSAlloc16( bytes );
4507 WORD selector = LOWORD( rv );
4509 if (selector)
4511 SET_AX( context, selector );
4512 RESET_CFLAG(context);
4514 else
4516 SET_CFLAG(context);
4517 SET_AX( context, 0x0008 ); /* insufficient memory */
4518 SET_BX( context, DOSMEM_Available() >> 4 );
4521 break;
4523 case 0x49: /* FREE MEMORY */
4524 TRACE( "FREE MEMORY segment %04X\n", context->SegEs );
4526 BOOL ok = !GlobalDOSFree16( context->SegEs );
4528 /* If we don't reset ES_reg, we will fail in the relay code */
4529 if (ok) context->SegEs = 0;
4530 else
4532 TRACE("FREE MEMORY failed\n");
4533 SET_CFLAG(context);
4534 SET_AX( context, 0x0009 ); /* memory block address invalid */
4537 break;
4539 case 0x4a: /* RESIZE MEMORY BLOCK */
4540 TRACE( "RESIZE MEMORY segment %04X to %d paragraphs\n",
4541 context->SegEs, BX_reg(context) );
4543 FIXME( "Resize memory block - unsupported under Win16\n" );
4544 SET_CFLAG(context);
4546 break;
4548 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
4550 char *program = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
4551 HINSTANCE16 instance;
4553 TRACE( "EXEC %s\n", program );
4555 RESET_CFLAG(context);
4556 instance = WinExec16( program, SW_NORMAL );
4557 if (instance < 32)
4559 SET_CFLAG( context );
4560 SET_AX( context, instance );
4563 break;
4565 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
4566 TRACE( "EXIT with return code %d\n", AL_reg(context) );
4567 DOSVM_Exit( AL_reg(context) );
4568 break;
4570 case 0x4d: /* GET RETURN CODE */
4571 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
4572 SET_AX( context, 0 );
4573 break;
4575 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
4576 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
4577 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
4578 if (!INT21_FindFirst(context)) break;
4579 /* fall through */
4581 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
4582 TRACE("FINDNEXT\n");
4583 if (!INT21_FindNext(context))
4585 SetLastError( ERROR_NO_MORE_FILES );
4586 SET_AX( context, ERROR_NO_MORE_FILES );
4587 SET_CFLAG(context);
4589 else SET_AX( context, 0 ); /* OK */
4590 break;
4592 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
4593 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
4594 DOSVM_psp = BX_reg(context);
4595 break;
4597 case 0x51: /* GET PSP ADDRESS */
4598 INT21_GetPSP( context );
4599 break;
4601 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
4602 TRACE("Get List of Lists - not supported\n");
4603 context->SegEs = 0;
4604 SET_BX( context, 0 );
4605 break;
4607 case 0x54: /* Get Verify Flag */
4608 TRACE("Get Verify Flag - Not Supported\n");
4609 SET_AL( context, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
4610 break;
4612 case 0x56: /* "RENAME" - RENAME FILE */
4613 if (!INT21_RenameFile( context ))
4614 bSetDOSExtendedError = TRUE;
4615 else
4616 RESET_CFLAG(context);
4617 break;
4619 case 0x57: /* FILE DATE AND TIME */
4620 if (!INT21_FileDateTime( context ))
4621 bSetDOSExtendedError = TRUE;
4622 else
4623 RESET_CFLAG(context);
4624 break;
4626 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
4627 switch (AL_reg(context))
4629 case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
4630 TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
4631 SET_AX( context, 0 ); /* low memory first fit */
4632 break;
4634 case 0x01: /* SET ALLOCATION STRATEGY */
4635 TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
4636 BL_reg(context) );
4637 break;
4639 case 0x02: /* GET UMB LINK STATE */
4640 TRACE( "GET UMB LINK STATE\n" );
4641 SET_AL( context, 0 ); /* UMBs not part of DOS memory chain */
4642 break;
4644 case 0x03: /* SET UMB LINK STATE */
4645 TRACE( "SET UMB LINK STATE to %d - ignored\n",
4646 BX_reg(context) );
4647 break;
4649 default:
4650 INT_BARF( context, 0x21 );
4652 break;
4654 case 0x59: /* GET EXTENDED ERROR INFO */
4655 INT21_GetExtendedError( context );
4656 break;
4658 case 0x5a: /* CREATE TEMPORARY FILE */
4659 TRACE("CREATE TEMPORARY FILE\n");
4660 bSetDOSExtendedError = !INT21_CreateTempFile(context);
4661 break;
4663 case 0x5b: /* CREATE NEW FILE */
4664 if (!INT21_CreateFile( context, context->Edx, FALSE,
4665 OF_READWRITE | OF_SHARE_COMPAT, 0x10 ))
4666 bSetDOSExtendedError = TRUE;
4667 else
4668 RESET_CFLAG(context);
4669 break;
4671 case 0x5c: /* "FLOCK" - RECORD LOCKING */
4673 DWORD offset = MAKELONG(DX_reg(context), CX_reg(context));
4674 DWORD length = MAKELONG(DI_reg(context), SI_reg(context));
4675 HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
4677 RESET_CFLAG(context);
4678 switch (AL_reg(context))
4680 case 0x00: /* LOCK */
4681 TRACE( "lock handle %d offset %d length %d\n",
4682 BX_reg(context), offset, length );
4683 if (!LockFile( handle, offset, 0, length, 0 ))
4684 bSetDOSExtendedError = TRUE;
4685 break;
4687 case 0x01: /* UNLOCK */
4688 TRACE( "unlock handle %d offset %d length %d\n",
4689 BX_reg(context), offset, length );
4690 if (!UnlockFile( handle, offset, 0, length, 0 ))
4691 bSetDOSExtendedError = TRUE;
4692 break;
4694 default:
4695 INT_BARF( context, 0x21 );
4698 break;
4700 case 0x5d: /* NETWORK 5D */
4701 FIXME( "Network function 5D not implemented.\n" );
4702 SetLastError( ER_NoNetwork );
4703 bSetDOSExtendedError = TRUE;
4704 break;
4706 case 0x5e: /* NETWORK 5E */
4707 bSetDOSExtendedError = INT21_NetworkFunc( context);
4708 break;
4710 case 0x5f: /* NETWORK 5F */
4711 /* FIXME: supporting this would need to 1:
4712 * - implement per drive current directory (as kernel32 doesn't)
4713 * - assign enabled/disabled flag on a per drive basis
4715 /* network software not installed */
4716 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
4717 SetLastError( ER_NoNetwork );
4718 bSetDOSExtendedError = TRUE;
4719 break;
4721 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
4723 WCHAR pathW[MAX_PATH], res[MAX_PATH];
4724 /* FIXME: likely to be broken */
4726 TRACE("TRUENAME %s\n",
4727 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
4728 MultiByteToWideChar(CP_OEMCP, 0, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi), -1, pathW, MAX_PATH);
4729 if (!GetFullPathNameW( pathW, 128, res, NULL ))
4730 bSetDOSExtendedError = TRUE;
4731 else
4733 SET_AX( context, 0 );
4734 WideCharToMultiByte(CP_OEMCP, 0, res, -1,
4735 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi),
4736 128, NULL, NULL);
4739 break;
4741 case 0x61: /* UNUSED */
4742 SET_AL( context, 0 );
4743 break;
4745 case 0x62: /* GET PSP ADDRESS */
4746 INT21_GetPSP( context );
4747 break;
4749 case 0x63: /* MISC. LANGUAGE SUPPORT */
4750 switch (AL_reg(context)) {
4751 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
4752 TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
4753 context->SegDs = INT21_GetHeapSelector( context );
4754 SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
4755 SET_AL( context, 0 ); /* success */
4756 break;
4758 break;
4760 case 0x64: /* OS/2 DOS BOX */
4761 INT_BARF( context, 0x21 );
4762 SET_CFLAG(context);
4763 break;
4765 case 0x65: /* EXTENDED COUNTRY INFORMATION */
4766 INT21_ExtendedCountryInformation( context );
4767 break;
4769 case 0x66: /* GLOBAL CODE PAGE TABLE */
4770 switch (AL_reg(context))
4772 case 0x01:
4773 TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
4774 SET_BX( context, GetOEMCP() );
4775 SET_DX( context, GetOEMCP() );
4776 break;
4777 case 0x02:
4778 FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
4779 BX_reg(context), DX_reg(context) );
4780 break;
4782 break;
4784 case 0x67: /* SET HANDLE COUNT */
4785 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
4786 if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
4787 bSetDOSExtendedError = TRUE;
4788 break;
4790 case 0x68: /* "FFLUSH" - COMMIT FILE */
4791 TRACE( "FFLUSH - handle %d\n", BX_reg(context) );
4792 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ) &&
4793 GetLastError() != ERROR_ACCESS_DENIED)
4794 bSetDOSExtendedError = TRUE;
4795 break;
4797 case 0x69: /* DISK SERIAL NUMBER */
4798 switch (AL_reg(context))
4800 case 0x00:
4801 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
4802 INT21_DriveName(BL_reg(context)));
4803 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
4804 else SET_AX( context, 0 );
4805 break;
4807 case 0x01:
4808 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
4809 INT21_DriveName(BL_reg(context)));
4810 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
4811 else SET_AX( context, 1 );
4812 break;
4814 break;
4816 case 0x6a: /* COMMIT FILE */
4817 TRACE( "COMMIT FILE - handle %d\n", BX_reg(context) );
4818 if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ) &&
4819 GetLastError() != ERROR_ACCESS_DENIED)
4820 bSetDOSExtendedError = TRUE;
4821 break;
4823 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
4824 SET_AL( context, 0 );
4825 break;
4827 case 0x6c: /* EXTENDED OPEN/CREATE */
4828 if (!INT21_CreateFile( context, context->Esi, TRUE,
4829 BX_reg(context), DL_reg(context) ))
4830 bSetDOSExtendedError = TRUE;
4831 break;
4833 case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
4834 FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
4835 SET_CFLAG( context );
4836 SET_AL( context, 0 );
4837 break;
4839 case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
4840 INT21_LongFilename( context );
4841 break;
4843 case 0x73: /* MSDOS7 - FAT32 */
4844 RESET_CFLAG( context );
4845 if (!INT21_Fat32( context ))
4846 bSetDOSExtendedError = TRUE;
4847 break;
4849 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
4850 TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
4851 break;
4853 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
4854 TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
4855 break;
4857 case 0xff: /* DOS32 EXTENDER (DOS/4GW) - API */
4858 /* we don't implement a DOS32 extender */
4859 TRACE( "DOS32 EXTENDER API - ignored\n" );
4860 break;
4862 default:
4863 INT_BARF( context, 0x21 );
4864 break;
4866 } /* END OF SWITCH */
4868 /* Set general error condition. */
4869 if (bSetDOSExtendedError)
4871 SET_AX( context, GetLastError() );
4872 SET_CFLAG( context );
4875 /* Print error code if carry flag is set. */
4876 if (context->EFlags & 0x0001)
4877 TRACE("failed, error %d\n", GetLastError() );
4879 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
4880 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08x\n",
4881 AX_reg(context), BX_reg(context),
4882 CX_reg(context), DX_reg(context),
4883 SI_reg(context), DI_reg(context),
4884 (WORD)context->SegDs, (WORD)context->SegEs,
4885 context->EFlags );