Fixed a race condition on RPC worker thread creation, and a typo.
[wine] / dlls / winedos / int21.c
1 /*
2  * DOS interrupt 21h handler
3  *
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  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winternl.h"
30 #include "wine/winbase16.h"
31 #include "dosexe.h"
32 #include "miscemu.h"
33 #include "msdos.h"
34 #include "file.h"
35 #include "task.h"
36 #include "winerror.h"
37 #include "winuser.h"
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
40
41 /*
42  * FIXME: Delete this reference when all int21 code has been moved to winedos.
43  */
44 extern void WINAPI INT_Int21Handler( CONTEXT86 *context );
45
46 /*
47  * Forward declarations.
48  */
49 static BOOL INT21_RenameFile( CONTEXT86 *context );
50
51 WINE_DEFAULT_DEBUG_CHANNEL(int21);
52
53
54 #include "pshpack1.h"
55
56 /*
57  * Structure for DOS data that can be accessed directly from applications.
58  * Real and protected mode pointers will be returned to this structure so
59  * the structure must be correctly packed.
60  */
61 typedef struct _INT21_HEAP {
62     WORD uppercase_size;             /* Size of the following table in bytes */
63     BYTE uppercase_table[128];       /* Uppercase equivalents of chars from 0x80 to 0xff. */
64
65     WORD lowercase_size;             /* Size of the following table in bytes */
66     BYTE lowercase_table[256];       /* Lowercase equivalents of chars from 0x00 to 0xff. */
67
68     WORD collating_size;             /* Size of the following table in bytes */
69     BYTE collating_table[256];       /* Values used to sort characters from 0x00 to 0xff. */
70
71     WORD filename_size;              /* Size of the following filename data in bytes */
72     BYTE filename_reserved1;         /* 0x01 for MS-DOS 3.30-6.00 */
73     BYTE filename_lowest;            /* Lowest permissible character value for filename */
74     BYTE filename_highest;           /* Highest permissible character value for filename */
75     BYTE filename_reserved2;         /* 0x00 for MS-DOS 3.30-6.00 */
76     BYTE filename_exclude_first;     /* First illegal character in permissible range */
77     BYTE filename_exclude_last;      /* Last illegal character in permissible range */
78     BYTE filename_reserved3;         /* 0x02 for MS-DOS 3.30-6.00 */
79     BYTE filename_illegal_size;      /* Number of terminators in the following table */
80     BYTE filename_illegal_table[16]; /* Characters which terminate a filename */
81
82     WORD dbcs_size;                  /* Number of valid ranges in the following table */
83     BYTE dbcs_table[16];             /* Start/end bytes for N ranges and 00/00 as terminator */
84
85     BYTE misc_indos;                 /* Interrupt 21 nesting flag */
86 } INT21_HEAP;
87
88 #include "poppack.h"
89
90
91 /***********************************************************************
92  *           INT21_ReadChar
93  *
94  * Reads a character from the standard input.
95  * Extended keycodes will be returned as two separate characters.
96  */
97 static BOOL INT21_ReadChar( BYTE *input, BOOL peek )
98 {
99     static BYTE pending_scan = 0;
100
101     if (pending_scan)
102     {
103         if (input)
104             *input = pending_scan;
105         if (!peek)
106             pending_scan = 0;
107         return TRUE;
108     }
109     else
110     {
111         BYTE ascii;
112         BYTE scan;
113         if (!DOSVM_Int16ReadChar( &ascii, &scan, peek ))
114             return FALSE;
115
116         if (input)
117             *input = ascii;
118         if (!peek && !ascii)
119             pending_scan = scan;
120         return TRUE;
121     }
122 }
123
124
125 /***********************************************************************
126  *           INT21_GetSystemCountryCode
127  *
128  * Return DOS country code for default system locale.
129  */
130 static WORD INT21_GetSystemCountryCode()
131 {
132     /*
133      * FIXME: Determine country code. We should probably use
134      *        DOSCONF structure for that.
135      */
136     return GetSystemDefaultLangID();
137 }
138
139
140 /***********************************************************************
141  *           INT21_FillCountryInformation
142  *
143  * Fill 34-byte buffer with country information data using
144  * default system locale.
145  */
146 static void INT21_FillCountryInformation( BYTE *buffer )
147 {
148     /* 00 - WORD: date format
149      *          00 = mm/dd/yy
150      *          01 = dd/mm/yy
151      *          02 = yy/mm/dd
152      */
153     *(WORD*)(buffer + 0) = 0; /* FIXME: Get from locale */
154
155     /* 02 - BYTE[5]: ASCIIZ currency symbol string */
156     buffer[2] = '$'; /* FIXME: Get from locale */
157     buffer[3] = 0;
158
159     /* 07 - BYTE[2]: ASCIIZ thousands separator */
160     buffer[7] = 0; /* FIXME: Get from locale */
161     buffer[8] = 0;
162
163     /* 09 - BYTE[2]: ASCIIZ decimal separator */
164     buffer[9]  = '.'; /* FIXME: Get from locale */
165     buffer[10] = 0;
166
167     /* 11 - BYTE[2]: ASCIIZ date separator */
168     buffer[11] = '/'; /* FIXME: Get from locale */
169     buffer[12] = 0;
170
171     /* 13 - BYTE[2]: ASCIIZ time separator */
172     buffer[13] = ':'; /* FIXME: Get from locale */
173     buffer[14] = 0;
174
175     /* 15 - BYTE: Currency format
176      *          bit 2 = set if currency symbol replaces decimal point
177      *          bit 1 = number of spaces between value and currency symbol
178      *          bit 0 = 0 if currency symbol precedes value
179      *                  1 if currency symbol follows value
180      */
181     buffer[15] = 0; /* FIXME: Get from locale */
182
183     /* 16 - BYTE: Number of digits after decimal in currency */
184     buffer[16] = 0; /* FIXME: Get from locale */
185
186     /* 17 - BYTE: Time format
187      *          bit 0 = 0 if 12-hour clock
188      *                  1 if 24-hour clock
189      */
190     buffer[17] = 1; /* FIXME: Get from locale */
191
192     /* 18 - DWORD: Address of case map routine */
193     *(DWORD*)(buffer + 18) = 0; /* FIXME: ptr to case map routine */
194
195     /* 22 - BYTE[2]: ASCIIZ data-list separator */
196     buffer[22] = ','; /* FIXME: Get from locale */
197     buffer[23] = 0;
198
199     /* 24 - BYTE[10]: Reserved */
200     memset( buffer + 24, 0, 10 );
201 }
202
203
204 /***********************************************************************
205  *           INT21_FillHeap
206  *
207  * Initialize DOS heap.
208  */
209 static void INT21_FillHeap( INT21_HEAP *heap )
210 {
211     static const char terminators[] = "\"\\./[]:|<>+=;,";
212     int i;
213
214     /*
215      * Uppercase table.
216      */
217     heap->uppercase_size = 128;
218     for (i = 0; i < 128; i++) 
219         heap->uppercase_table[i] = toupper( 128 + i );
220
221     /*
222      * Lowercase table.
223      */
224     heap->lowercase_size = 256;
225     for (i = 0; i < 256; i++) 
226         heap->lowercase_table[i] = tolower( i );
227     
228     /*
229      * Collating table.
230      */
231     heap->collating_size = 256;
232     for (i = 0; i < 256; i++) 
233         heap->collating_table[i] = i;
234
235     /*
236      * Filename table.
237      */
238     heap->filename_size = 8 + strlen(terminators);
239     heap->filename_illegal_size = strlen(terminators);
240     strcpy( heap->filename_illegal_table, terminators );
241
242     heap->filename_reserved1 = 0x01;
243     heap->filename_lowest = 0;           /* FIXME: correct value? */
244     heap->filename_highest = 0xff;       /* FIXME: correct value? */
245     heap->filename_reserved2 = 0x00;    
246     heap->filename_exclude_first = 0x00; /* FIXME: correct value? */
247     heap->filename_exclude_last = 0x00;  /* FIXME: correct value? */
248     heap->filename_reserved3 = 0x02;
249
250     /*
251      * DBCS lead byte table. This table is empty.
252      */
253     heap->dbcs_size = 0;
254     memset( heap->dbcs_table, 0, sizeof(heap->dbcs_table) );
255
256     /*
257      * Initialize InDos flag.
258      */
259     heap->misc_indos = 0;
260 }
261
262
263 /***********************************************************************
264  *           INT21_GetHeapSelector
265  *
266  * Get segment/selector for DOS heap (INT21_HEAP).
267  * Creates and initializes heap on first call.
268  */
269 static WORD INT21_GetHeapSelector( CONTEXT86 *context )
270 {
271     static WORD heap_segment = 0;
272     static WORD heap_selector = 0;
273     static BOOL heap_initialized = FALSE;
274
275     if (!heap_initialized)
276     {
277         INT21_HEAP *ptr = DOSVM_AllocDataUMB( sizeof(INT21_HEAP), 
278                                               &heap_segment,
279                                               &heap_selector );
280         INT21_FillHeap( ptr );
281         heap_initialized = TRUE;
282     }
283
284     if (!ISV86(context) && DOSVM_IsWin16())
285         return heap_selector;
286     else
287         return heap_segment;
288 }
289
290
291 /***********************************************************************
292  *           INT21_BufferedInput
293  *
294  * Handler for function 0x0a.
295  *
296  * Reads a string of characters from standard input until
297  * enter key is pressed.
298  */
299 static void INT21_BufferedInput( CONTEXT86 *context )
300 {
301     BYTE *ptr = CTX_SEG_OFF_TO_LIN(context,
302                                    context->SegDs,
303                                    context->Edx);
304     BYTE capacity = ptr[0]; /* includes CR */
305     BYTE length = 0;        /* excludes CR */
306
307     TRACE( "BUFFERED INPUT (size=%d)\n", capacity );
308
309     /*
310      * Return immediately if capacity is zero.
311      *
312      * FIXME: What to return to application?
313      */
314     if (capacity == 0)
315         return;
316
317     /*
318      * FIXME: Some documents state that
319      *        ptr[1] holds number of chars from last input which 
320      *        may be recalled on entry, other documents do not mention
321      *        this at all.
322      */
323     if (ptr[1])
324         TRACE( "Handle old chars in buffer!\n" );
325
326     while(TRUE)
327     {
328         BYTE ascii;
329         BYTE scan;
330
331         DOSVM_Int16ReadChar( &ascii, &scan, FALSE );
332
333         if (ascii == '\r' || ascii == '\n')
334         {
335             /*
336              * FIXME: What should be echoed here?
337              */
338             DOSVM_PutChar( '\r' );
339             DOSVM_PutChar( '\n' );
340             ptr[1] = length;
341             ptr[2 + length] = '\r';
342             return;
343         }
344
345         /*
346          * FIXME: This function is supposed to support
347          *        DOS editing keys...
348          */
349
350         /*
351          * If the buffer becomes filled to within one byte of
352          * capacity, DOS rejects all further characters up to,
353          * but not including, the terminating carriage return.
354          */
355         if (ascii != 0 && length < capacity-1)
356         {
357             DOSVM_PutChar( ascii );
358             ptr[2 + length] = ascii;
359             length++;
360         }
361     }
362 }
363
364
365 /***********************************************************************
366  *           INT21_CreateDirectory
367  *
368  * Handler for:
369  * - function 0x39
370  * - subfunction 0x39 of function 0x71
371  * - subfunction 0xff of function 0x43 (CL == 0x39)
372  */
373 static BOOL INT21_CreateDirectory( CONTEXT86 *context )
374 {
375     WCHAR dirW[MAX_PATH];
376     char *dirA = CTX_SEG_OFF_TO_LIN(context,
377                                     context->SegDs, 
378                                     context->Edx);
379
380     TRACE( "CREATE DIRECTORY %s\n", dirA );
381
382     MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
383
384     if (CreateDirectoryW(dirW, NULL))
385         return TRUE;
386
387     /*
388      * FIXME: CreateDirectory's LastErrors will clash with the ones
389      *        used by DOS. AH=39 only returns 3 (path not found) and 
390      *        5 (access denied), while CreateDirectory return several
391      *        ones. Remap some of them. -Marcus
392      */
393     switch (GetLastError()) 
394     {
395     case ERROR_ALREADY_EXISTS:
396     case ERROR_FILENAME_EXCED_RANGE:
397     case ERROR_DISK_FULL:
398         SetLastError(ERROR_ACCESS_DENIED);
399         break;
400     default: 
401         break;
402     }
403
404     return FALSE;
405 }
406
407
408 /***********************************************************************
409  *           INT21_ExtendedCountryInformation
410  *
411  * Handler for function 0x65.
412  */
413 static void INT21_ExtendedCountryInformation( CONTEXT86 *context )
414 {
415     BYTE *dataptr = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
416
417     TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
418            AL_reg(context) );
419
420     /*
421      * Check subfunctions that are passed country and code page.
422      */
423     if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
424     {
425         WORD country = DX_reg(context);
426         WORD codepage = BX_reg(context);
427
428         if (country != 0xffff && country != INT21_GetSystemCountryCode())
429             FIXME( "Requested info on non-default country %04x\n", country );
430
431         if (codepage != 0xffff && codepage != GetOEMCP())
432             FIXME( "Requested info on non-default code page %04x\n", codepage );
433     }
434
435     switch (AL_reg(context)) {
436     case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
437         INT_BARF( context, 0x21 );
438         SET_CFLAG( context );
439         break;
440
441     case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
442         TRACE( "Get general internationalization info\n" );
443         dataptr[0] = 0x01; /* Info ID */
444         *(WORD*)(dataptr+1) = 38; /* Size of the following info */
445         *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
446         *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
447         INT21_FillCountryInformation( dataptr + 7 );
448         SET_CX( context, 41 ); /* Size of returned info */
449         break;
450         
451     case 0x02: /* GET POINTER TO UPPERCASE TABLE */
452     case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
453         TRACE( "Get pointer to uppercase table\n" );
454         dataptr[0] = AL_reg(context); /* Info ID */
455         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
456                                            offsetof(INT21_HEAP, uppercase_size) );
457         SET_CX( context, 5 ); /* Size of returned info */
458         break;
459
460     case 0x03: /* GET POINTER TO LOWERCASE TABLE */
461         TRACE( "Get pointer to lowercase table\n" );
462         dataptr[0] = 0x03; /* Info ID */
463         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
464                                            offsetof(INT21_HEAP, lowercase_size) );
465         SET_CX( context, 5 ); /* Size of returned info */
466         break;
467
468     case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
469         TRACE("Get pointer to filename terminator table\n");
470         dataptr[0] = 0x05; /* Info ID */
471         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
472                                            offsetof(INT21_HEAP, filename_size) );
473         SET_CX( context, 5 ); /* Size of returned info */
474         break;
475
476     case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
477         TRACE("Get pointer to collating sequence table\n");
478         dataptr[0] = 0x06; /* Info ID */
479         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
480                                            offsetof(INT21_HEAP, collating_size) );
481         SET_CX( context, 5 ); /* Size of returned info */
482         break;
483
484     case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
485         TRACE("Get pointer to DBCS lead byte table\n");
486         dataptr[0] = 0x07; /* Info ID */
487         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
488                                            offsetof(INT21_HEAP, dbcs_size) );
489         SET_CX( context, 5 ); /* Size of returned info */
490         break;
491
492     case 0x20: /* CAPITALIZE CHARACTER */
493     case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
494         TRACE("Convert char to uppercase\n");
495         SET_DL( context, toupper(DL_reg(context)) );
496         break;
497
498     case 0x21: /* CAPITALIZE STRING */
499     case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
500         TRACE("Convert string to uppercase with length\n");
501         {
502             char *ptr = (char *)CTX_SEG_OFF_TO_LIN( context,
503                                                     context->SegDs,
504                                                     context->Edx );
505             WORD len = CX_reg(context);
506             while (len--) { *ptr = toupper(*ptr); ptr++; }
507         }
508         break;
509
510     case 0x22: /* CAPITALIZE ASCIIZ STRING */
511     case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
512         TRACE("Convert ASCIIZ string to uppercase\n");
513         _strupr( (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx) );
514         break;
515
516     case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
517         INT_BARF( context, 0x21 );
518         SET_CFLAG( context );
519         break;
520
521     default:
522         INT_BARF( context, 0x21 );
523         SET_CFLAG(context);
524         break;
525     }
526 }
527
528
529 /***********************************************************************
530  *           INT21_FileAttributes
531  *
532  * Handler for:
533  * - function 0x43
534  * - subfunction 0x43 of function 0x71
535  */
536 static BOOL INT21_FileAttributes( CONTEXT86 *context, 
537                                   BYTE       subfunction,
538                                   BOOL       islong )
539 {
540     WCHAR fileW[MAX_PATH];
541     char *fileA = CTX_SEG_OFF_TO_LIN(context, 
542                                      context->SegDs, 
543                                      context->Edx);
544     HANDLE   handle;
545     BOOL     status;
546     FILETIME filetime;
547     DWORD    result;
548     WORD     date, time;
549
550     switch (subfunction)
551     {
552     case 0x00: /* GET FILE ATTRIBUTES */
553         TRACE( "GET FILE ATTRIBUTES for %s\n", fileA );
554         MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
555
556         result = GetFileAttributesW( fileW );
557         if (result == -1)
558             return FALSE;
559         else
560         {
561             SET_CX( context, (WORD)result );
562             if (!islong)
563                 SET_AX( context, (WORD)result ); /* DR DOS */
564         }
565         break;
566
567     case 0x01: /* SET FILE ATTRIBUTES */
568         TRACE( "SET FILE ATTRIBUTES 0x%02x for %s\n", 
569                CX_reg(context), fileA );
570         MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
571
572         if (!SetFileAttributesW( fileW, CX_reg(context) ))
573             return FALSE;
574         break;
575
576     case 0x02: /* GET COMPRESSED FILE SIZE */
577         TRACE( "GET COMPRESSED FILE SIZE for %s\n", fileA );
578         MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
579
580         result = GetCompressedFileSizeW( fileW, NULL );
581         if (result == INVALID_FILE_SIZE)
582             return FALSE;
583         else
584         {
585             SET_AX( context, LOWORD(result) );
586             SET_DX( context, HIWORD(result) );
587         }
588         break;
589
590     case 0x03: /* SET FILE LAST-WRITTEN DATE AND TIME */
591         if (!islong)
592             INT_BARF( context, 0x21 );
593         else
594         {
595             TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
596             MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
597
598             handle = CreateFileW( fileW, GENERIC_WRITE, 
599                                   FILE_SHARE_READ | FILE_SHARE_WRITE, 
600                                   NULL, OPEN_EXISTING, 0, 0 );
601             if (handle == INVALID_HANDLE_VALUE)
602                 return FALSE;
603
604             DosDateTimeToFileTime( DI_reg(context), 
605                                    CX_reg(context),
606                                    &filetime );
607             status = SetFileTime( handle, NULL, NULL, &filetime );
608
609             CloseHandle( handle );
610             return status;
611         }
612         break;
613
614     case 0x04: /* GET FILE LAST-WRITTEN DATE AND TIME */
615         if (!islong)
616             INT_BARF( context, 0x21 );
617         else
618         {
619             TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, file %s\n", fileA );
620             MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
621
622             handle = CreateFileW( fileW, GENERIC_READ, 
623                                   FILE_SHARE_READ | FILE_SHARE_WRITE, 
624                                   NULL, OPEN_EXISTING, 0, 0 );
625             if (handle == INVALID_HANDLE_VALUE)
626                 return FALSE;
627
628             status = GetFileTime( handle, NULL, NULL, &filetime );
629             if (status)
630             {
631                 FileTimeToDosDateTime( &filetime, &date, &time );
632                 SET_DI( context, date );
633                 SET_CX( context, time );
634             }
635
636             CloseHandle( handle );
637             return status;
638         }
639         break;
640
641     case 0x05: /* SET FILE LAST ACCESS DATE */
642         if (!islong)
643             INT_BARF( context, 0x21 );
644         else
645         {
646             TRACE( "SET FILE LAST ACCESS DATE, file %s\n", fileA );
647             MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
648
649             handle = CreateFileW( fileW, GENERIC_WRITE, 
650                                   FILE_SHARE_READ | FILE_SHARE_WRITE, 
651                                   NULL, OPEN_EXISTING, 0, 0 );
652             if (handle == INVALID_HANDLE_VALUE)
653                 return FALSE;
654
655             DosDateTimeToFileTime( DI_reg(context), 
656                                    0,
657                                    &filetime );
658             status = SetFileTime( handle, NULL, &filetime, NULL );
659
660             CloseHandle( handle );
661             return status;
662         }
663         break;
664
665     case 0x06: /* GET FILE LAST ACCESS DATE */
666         if (!islong)
667             INT_BARF( context, 0x21 );
668         else
669         {
670             TRACE( "GET FILE LAST ACCESS DATE, file %s\n", fileA );
671             MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
672
673             handle = CreateFileW( fileW, GENERIC_READ, 
674                                   FILE_SHARE_READ | FILE_SHARE_WRITE, 
675                                   NULL, OPEN_EXISTING, 0, 0 );
676             if (handle == INVALID_HANDLE_VALUE)
677                 return FALSE;
678
679             status = GetFileTime( handle, NULL, &filetime, NULL );
680             if (status)
681             {
682                 FileTimeToDosDateTime( &filetime, &date, NULL );
683                 SET_DI( context, date );
684             }
685
686             CloseHandle( handle );
687             return status;
688         }
689         break;
690
691     case 0x07: /* SET FILE CREATION DATE AND TIME */
692         if (!islong)
693             INT_BARF( context, 0x21 );
694         else
695         {
696             TRACE( "SET FILE CREATION DATE AND TIME, file %s\n", fileA );
697
698             handle = CreateFileW( fileW, GENERIC_WRITE,
699                                   FILE_SHARE_READ | FILE_SHARE_WRITE, 
700                                   NULL, OPEN_EXISTING, 0, 0 );
701             if (handle == INVALID_HANDLE_VALUE)
702                 return FALSE;
703             
704             /*
705              * FIXME: SI has number of 10-millisecond units past time in CX.
706              */
707             DosDateTimeToFileTime( DI_reg(context),
708                                    CX_reg(context),
709                                    &filetime );
710             status = SetFileTime( handle, &filetime, NULL, NULL );
711
712             CloseHandle( handle );
713             return status;
714         }
715         break;
716
717     case 0x08: /* GET FILE CREATION DATE AND TIME */
718         if (!islong)
719             INT_BARF( context, 0x21 );
720         else
721         {
722             TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
723                    BX_reg(context) );
724
725             handle = CreateFileW( fileW, GENERIC_READ, 
726                                   FILE_SHARE_READ | FILE_SHARE_WRITE, 
727                                   NULL, OPEN_EXISTING, 0, 0 );
728             if (handle == INVALID_HANDLE_VALUE)
729                 return FALSE;
730
731             status = GetFileTime( handle, &filetime, NULL, NULL );
732             if (status)
733             {            
734                 FileTimeToDosDateTime( &filetime, &date, &time );
735                 SET_DI( context, date );
736                 SET_CX( context, time );
737                 /*
738                  * FIXME: SI has number of 10-millisecond units past 
739                  *        time in CX.
740                  */
741                 SET_SI( context, 0 );
742             }
743
744             CloseHandle(handle);
745             return status;
746         }
747         break;
748
749     case 0xff: /* EXTENDED-LENGTH FILENAME OPERATIONS */
750         if (islong || context->Ebp != 0x5053)
751             INT_BARF( context, 0x21 );
752         else
753         {
754             switch(CL_reg(context))
755             {
756             case 0x39:
757                 if (!INT21_CreateDirectory( context ))
758                     return FALSE;
759                 break;
760
761             case 0x56:
762                 if (!INT21_RenameFile( context ))
763                     return FALSE;
764                 break;
765
766             default:
767                 INT_BARF( context, 0x21 );
768             }
769         }
770         break;
771
772     default:
773         INT_BARF( context, 0x21 );
774     }
775
776     return TRUE;
777 }
778
779
780 /***********************************************************************
781  *           INT21_FileDateTime
782  *
783  * Handler for function 0x57.
784  */
785 static BOOL INT21_FileDateTime( CONTEXT86 *context )
786 {
787     HANDLE   handle = DosFileHandleToWin32Handle(BX_reg(context));
788     FILETIME filetime;
789     WORD     date, time;
790
791     switch (AL_reg(context)) {
792     case 0x00:  /* Get last-written stamp */
793         TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
794                BX_reg(context) );
795         {
796             if (!GetFileTime( handle, NULL, NULL, &filetime ))
797                 return FALSE;
798             FileTimeToDosDateTime( &filetime, &date, &time );
799             SET_DX( context, date );
800             SET_CX( context, time );
801             break;
802         }
803
804     case 0x01:  /* Set last-written stamp */
805         TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
806                BX_reg(context) );
807         {
808             DosDateTimeToFileTime( DX_reg(context), 
809                                    CX_reg(context),
810                                    &filetime );
811             if (!SetFileTime( handle, NULL, NULL, &filetime ))
812                 return FALSE;
813             break;
814         }
815
816     case 0x04:  /* Get last access stamp, DOS 7 */
817         TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
818                BX_reg(context) );
819         {
820             if (!GetFileTime( handle, NULL, &filetime, NULL ))
821                 return FALSE;
822             FileTimeToDosDateTime( &filetime, &date, &time );
823             SET_DX( context, date );
824             SET_CX( context, time );
825             break;
826         }
827
828     case 0x05:  /* Set last access stamp, DOS 7 */
829         TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
830                BX_reg(context) );
831         {
832             DosDateTimeToFileTime( DX_reg(context), 
833                                    CX_reg(context),
834                                    &filetime );
835             if (!SetFileTime( handle, NULL, &filetime, NULL ))
836                 return FALSE;
837             break;
838         }
839
840     case 0x06:  /* Get creation stamp, DOS 7 */
841         TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
842                BX_reg(context) );
843         {
844             if (!GetFileTime( handle, &filetime, NULL, NULL ))
845                 return FALSE;
846             FileTimeToDosDateTime( &filetime, &date, &time );
847             SET_DX( context, date );
848             SET_CX( context, time );
849             /*
850              * FIXME: SI has number of 10-millisecond units past time in CX.
851              */
852             SET_SI( context, 0 );
853             break;
854         }
855
856     case 0x07:  /* Set creation stamp, DOS 7 */
857         TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
858                BX_reg(context) );
859         {
860             /*
861              * FIXME: SI has number of 10-millisecond units past time in CX.
862              */
863             DosDateTimeToFileTime( DX_reg(context), 
864                                    CX_reg(context),
865                                    &filetime );
866             if (!SetFileTime( handle, &filetime, NULL, NULL ))
867                 return FALSE;
868             break;
869         }
870
871     default:
872         INT_BARF( context, 0x21 );
873         break;
874     }
875
876     return TRUE;
877 }
878
879
880 /***********************************************************************
881  *           INT21_GetPSP
882  *
883  * Handler for functions 0x51 and 0x62.
884  */
885 static void INT21_GetPSP( CONTEXT86 *context )
886 {
887     TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
888
889     /*
890      * FIXME: should we return the original DOS PSP upon
891      *        Windows startup ? 
892      */
893     if (!ISV86(context) && DOSVM_IsWin16())
894         SET_BX( context, LOWORD(GetCurrentPDB16()) );
895     else
896         SET_BX( context, DOSVM_psp );
897 }
898
899
900 /***********************************************************************
901  *           INT21_Ioctl_Block
902  *
903  * Handler for block device IOCTLs.
904  */
905 static void INT21_Ioctl_Block( CONTEXT86 *context )
906 {
907     INT_Int21Handler( context );
908 }
909
910
911 /***********************************************************************
912  *           INT21_Ioctl_Char
913  *
914  * Handler for character device IOCTLs.
915  */
916 static void INT21_Ioctl_Char( CONTEXT86 *context )
917 {
918     static const WCHAR emmxxxx0W[] = {'E','M','M','X','X','X','X','0',0};
919     static const WCHAR scsimgrW[] = {'S','C','S','I','M','G','R','$',0};
920
921     HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
922     const DOS_DEVICE *dev = DOSFS_GetDeviceByHandle(handle);
923
924     if (dev && !strcmpiW( dev->name, emmxxxx0W )) 
925     {
926         EMS_Ioctl_Handler(context);
927         return;
928     }
929
930     if (dev && !strcmpiW( dev->name, scsimgrW ) && AL_reg(context) == 2)
931     {
932         DOSVM_ASPIHandler(context);
933         return;
934     }
935
936     INT_Int21Handler( context );
937 }
938
939
940 /***********************************************************************
941  *           INT21_Ioctl
942  *
943  * Handler for function 0x44.
944  */
945 static void INT21_Ioctl( CONTEXT86 *context )
946 {
947     switch (AL_reg(context))
948     {
949     case 0x00:
950     case 0x01:
951     case 0x02:
952     case 0x03:
953         INT21_Ioctl_Char( context );
954         break;
955
956     case 0x04:
957     case 0x05:
958         INT21_Ioctl_Block( context );
959         break;
960
961     case 0x06:
962     case 0x07:
963         INT21_Ioctl_Char( context );
964         break;
965
966     case 0x08:
967     case 0x09:
968         INT21_Ioctl_Block( context );
969         break;
970
971     case 0x0a:
972         INT21_Ioctl_Char( context );
973         break;
974
975     case 0x0b: /* SET SHARING RETRY COUNT */
976         TRACE( "SET SHARING RETRY COUNT: Pause %d, retries %d.\n",
977                CX_reg(context), DX_reg(context) );
978         if (!CX_reg(context))
979         {
980             SET_AX( context, 1 );
981             SET_CFLAG( context );
982         }
983         else
984         {
985             DOSMEM_LOL()->sharing_retry_delay = CX_reg(context);
986             if (DX_reg(context))
987                 DOSMEM_LOL()->sharing_retry_count = DX_reg(context);
988             RESET_CFLAG( context );
989         }
990         break;
991
992     case 0x0c:
993         INT21_Ioctl_Char( context );
994         break;
995
996     case 0x0d:
997     case 0x0e:
998     case 0x0f:
999         INT21_Ioctl_Block( context );
1000         break;
1001
1002     case 0x10:
1003         INT21_Ioctl_Char( context );
1004         break;
1005
1006     case 0x11:
1007         INT21_Ioctl_Block( context );
1008         break;
1009
1010     case 0x12: /*  DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION) */
1011         TRACE( "DR DOS - DETERMINE DOS TYPE (OBSOLETE FUNCTION)\n" );
1012         SET_CFLAG(context);        /* Error / This is not DR DOS. */
1013         SET_AX( context, 0x0001 ); /* Invalid function */
1014         break;
1015
1016     case 0x52: /* DR DOS - DETERMINE DOS TYPE */
1017         TRACE( "DR DOS - DETERMINE DOS TYPE\n" );
1018         SET_CFLAG(context);        /* Error / This is not DR DOS. */
1019         SET_AX( context, 0x0001 ); /* Invalid function */
1020         break;
1021
1022     case 0xe0:  /* Sun PC-NFS API */
1023         TRACE( "Sun PC-NFS API\n" );
1024         /* not installed */
1025         break;
1026
1027     default:
1028         INT_BARF( context, 0x21 );
1029     }
1030 }
1031
1032
1033 /***********************************************************************
1034  *           INT21_LongFilename
1035  *
1036  * Handler for function 0x71.
1037  */
1038 static void INT21_LongFilename( CONTEXT86 *context )
1039 {
1040     BOOL bSetDOSExtendedError = FALSE;
1041
1042     if (HIBYTE(HIWORD(GetVersion16())) < 0x07)
1043     {
1044         TRACE( "LONG FILENAME - functions supported only under DOS7\n" );
1045         SET_CFLAG( context );
1046         SET_AL( context, 0 );
1047         return;
1048     }
1049
1050     switch (AL_reg(context))
1051     {
1052     case 0x0d: /* RESET DRIVE */
1053         INT_Int21Handler( context );
1054         break;
1055
1056     case 0x39: /* LONG FILENAME - MAKE DIRECTORY */
1057         if (!INT21_CreateDirectory( context ))
1058             bSetDOSExtendedError = TRUE;
1059         break;
1060
1061     case 0x3a: /* LONG FILENAME - REMOVE DIRECTORY */
1062         {
1063             WCHAR dirW[MAX_PATH];
1064             char *dirA = CTX_SEG_OFF_TO_LIN(context,
1065                                             context->SegDs, context->Edx);
1066
1067             TRACE( "LONG FILENAME - REMOVE DIRECTORY %s\n", dirA );
1068             MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
1069
1070             if (!RemoveDirectoryW( dirW ))
1071                 bSetDOSExtendedError = TRUE;
1072         }
1073         break;
1074
1075     case 0x3b: /* LONG FILENAME - CHANGE DIRECTORY */
1076         INT_Int21Handler( context );
1077         break;
1078
1079     case 0x41: /* LONG FILENAME - DELETE FILE */
1080         {
1081             WCHAR fileW[MAX_PATH];
1082             char *fileA = CTX_SEG_OFF_TO_LIN(context, 
1083                                              context->SegDs, context->Edx);
1084
1085             TRACE( "LONG FILENAME - DELETE FILE %s\n", fileA );
1086             MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1087
1088             if (!DeleteFileW( fileW ))
1089                 bSetDOSExtendedError = TRUE;
1090         }
1091         break;
1092
1093     case 0x43: /* LONG FILENAME - EXTENDED GET/SET FILE ATTRIBUTES */
1094         if (!INT21_FileAttributes( context, BL_reg(context), TRUE ))
1095             bSetDOSExtendedError = TRUE;
1096         break;
1097
1098     case 0x47: /* LONG FILENAME - GET CURRENT DIRECTORY */
1099     case 0x4e: /* LONG FILENAME - FIND FIRST MATCHING FILE */
1100     case 0x4f: /* LONG FILENAME - FIND NEXT MATCHING FILE */
1101         INT_Int21Handler( context );
1102         break;
1103
1104     case 0x56: /* LONG FILENAME - RENAME FILE */
1105         if (!INT21_RenameFile(context))
1106             bSetDOSExtendedError = TRUE;
1107         break;
1108
1109     case 0x60: /* LONG FILENAME - CONVERT PATH */
1110     case 0x6c: /* LONG FILENAME - CREATE OR OPEN FILE */
1111     case 0xa0: /* LONG FILENAME - GET VOLUME INFORMATION */
1112     case 0xa1: /* LONG FILENAME - "FindClose" - TERMINATE DIRECTORY SEARCH */
1113     case 0xa6: /* LONG FILENAME - GET FILE INFO BY HANDLE */
1114     case 0xa7: /* LONG FILENAME - CONVERT TIME */
1115     case 0xa8: /* LONG FILENAME - GENERATE SHORT FILENAME */
1116     case 0xa9: /* LONG FILENAME - SERVER CREATE OR OPEN FILE */
1117     case 0xaa: /* LONG FILENAME - SUBST */
1118         INT_Int21Handler( context );
1119         break;
1120
1121     default:
1122         INT_BARF( context, 0x21 );
1123     }
1124
1125     if (bSetDOSExtendedError)
1126     {
1127         SET_AX( context, GetLastError() );
1128         SET_CFLAG( context );
1129     }
1130 }
1131
1132
1133 /***********************************************************************
1134  *           INT21_RenameFile
1135  *
1136  * Handler for:
1137  * - function 0x56
1138  * - subfunction 0x56 of function 0x71
1139  * - subfunction 0xff of function 0x43 (CL == 0x56)
1140  */
1141 static BOOL INT21_RenameFile( CONTEXT86 *context )
1142 {
1143     WCHAR fromW[MAX_PATH];
1144     WCHAR toW[MAX_PATH];
1145     char *fromA = CTX_SEG_OFF_TO_LIN(context, 
1146                                      context->SegDs,context->Edx);
1147     char *toA = CTX_SEG_OFF_TO_LIN(context, 
1148                                    context->SegEs,context->Edi);
1149
1150     TRACE( "RENAME FILE %s to %s\n", fromA, toA );
1151     MultiByteToWideChar(CP_OEMCP, 0, fromA, -1, fromW, MAX_PATH);
1152     MultiByteToWideChar(CP_OEMCP, 0, toA, -1, toW, MAX_PATH);
1153
1154     return MoveFileW( fromW, toW );
1155 }
1156
1157
1158 /***********************************************************************
1159  *           INT21_GetExtendedError
1160  */
1161 static void INT21_GetExtendedError( CONTEXT86 *context )
1162 {
1163     BYTE class, action, locus;
1164     WORD error = GetLastError();
1165
1166     switch(error)
1167     {
1168     case ERROR_SUCCESS:
1169         class = action = locus = 0;
1170         break;
1171     case ERROR_DIR_NOT_EMPTY:
1172         class  = EC_Exists;
1173         action = SA_Ignore;
1174         locus  = EL_Disk;
1175         break;
1176     case ERROR_ACCESS_DENIED:
1177         class  = EC_AccessDenied;
1178         action = SA_Abort;
1179         locus  = EL_Disk;
1180         break;
1181     case ERROR_CANNOT_MAKE:
1182         class  = EC_AccessDenied;
1183         action = SA_Abort;
1184         locus  = EL_Unknown;
1185         break;
1186     case ERROR_DISK_FULL:
1187     case ERROR_HANDLE_DISK_FULL:
1188         class  = EC_MediaError;
1189         action = SA_Abort;
1190         locus  = EL_Disk;
1191         break;
1192     case ERROR_FILE_EXISTS:
1193     case ERROR_ALREADY_EXISTS:
1194         class  = EC_Exists;
1195         action = SA_Abort;
1196         locus  = EL_Disk;
1197         break;
1198     case ERROR_FILE_NOT_FOUND:
1199         class  = EC_NotFound;
1200         action = SA_Abort;
1201         locus  = EL_Disk;
1202         break;
1203     case ER_GeneralFailure:
1204         class  = EC_SystemFailure;
1205         action = SA_Abort;
1206         locus  = EL_Unknown;
1207         break;
1208     case ERROR_INVALID_DRIVE:
1209         class  = EC_MediaError;
1210         action = SA_Abort;
1211         locus  = EL_Disk;
1212         break;
1213     case ERROR_INVALID_HANDLE:
1214         class  = EC_ProgramError;
1215         action = SA_Abort;
1216         locus  = EL_Disk;
1217         break;
1218     case ERROR_LOCK_VIOLATION:
1219         class  = EC_AccessDenied;
1220         action = SA_Abort;
1221         locus  = EL_Disk;
1222         break;
1223     case ERROR_NO_MORE_FILES:
1224         class  = EC_MediaError;
1225         action = SA_Abort;
1226         locus  = EL_Disk;
1227         break;
1228     case ER_NoNetwork:
1229         class  = EC_NotFound;
1230         action = SA_Abort;
1231         locus  = EL_Network;
1232         break;
1233     case ERROR_NOT_ENOUGH_MEMORY:
1234         class  = EC_OutOfResource;
1235         action = SA_Abort;
1236         locus  = EL_Memory;
1237         break;
1238     case ERROR_PATH_NOT_FOUND:
1239         class  = EC_NotFound;
1240         action = SA_Abort;
1241         locus  = EL_Disk;
1242         break;
1243     case ERROR_SEEK:
1244         class  = EC_NotFound;
1245         action = SA_Ignore;
1246         locus  = EL_Disk;
1247         break;
1248     case ERROR_SHARING_VIOLATION:
1249         class  = EC_Temporary;
1250         action = SA_Retry;
1251         locus  = EL_Disk;
1252         break;
1253     case ERROR_TOO_MANY_OPEN_FILES:
1254         class  = EC_ProgramError;
1255         action = SA_Abort;
1256         locus  = EL_Disk;
1257         break;
1258     default:
1259         FIXME("Unknown error %d\n", error );
1260         class  = EC_SystemFailure;
1261         action = SA_Abort;
1262         locus  = EL_Unknown;
1263         break;
1264     }
1265     TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
1266            error, class, action, locus );
1267     SET_AX( context, error );
1268     SET_BH( context, class );
1269     SET_BL( context, action );
1270     SET_CH( context, locus );
1271 }
1272
1273
1274 /***********************************************************************
1275  *           DOSVM_Int21Handler
1276  *
1277  * Interrupt 0x21 handler.
1278  */
1279 void WINAPI DOSVM_Int21Handler( CONTEXT86 *context )
1280 {
1281     BOOL bSetDOSExtendedError = FALSE;
1282
1283     TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
1284            "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1285            AX_reg(context), BX_reg(context), 
1286            CX_reg(context), DX_reg(context),
1287            SI_reg(context), DI_reg(context),
1288            (WORD)context->SegDs, (WORD)context->SegEs,
1289            context->EFlags );
1290
1291    /*
1292     * Extended error is used by (at least) functions 0x2f to 0x62.
1293     * Function 0x59 returns extended error and error should not
1294     * be cleared before handling the function.
1295     */
1296     if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59) 
1297         SetLastError(0);
1298
1299     RESET_CFLAG(context); /* Not sure if this is a good idea. */
1300
1301     switch(AH_reg(context))
1302     {
1303     case 0x00: /* TERMINATE PROGRAM */
1304         TRACE("TERMINATE PROGRAM\n");
1305         if (DOSVM_IsWin16())
1306             ExitThread( 0 );
1307         else
1308             MZ_Exit( context, FALSE, 0 );
1309         break;
1310
1311     case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
1312         {
1313             BYTE ascii;
1314             TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
1315             INT21_ReadChar( &ascii, FALSE );
1316             SET_AL( context, ascii );
1317             /*
1318              * FIXME: What to echo when extended keycodes are read?
1319              */
1320             DOSVM_PutChar(AL_reg(context));
1321         }
1322         break;
1323
1324     case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
1325         TRACE("Write Character to Standard Output\n");
1326         DOSVM_PutChar(DL_reg(context));
1327         break;
1328
1329     case 0x03: /* READ CHARACTER FROM STDAUX  */
1330     case 0x04: /* WRITE CHARACTER TO STDAUX */
1331     case 0x05: /* WRITE CHARACTER TO PRINTER */
1332         INT_BARF( context, 0x21 );
1333         break;
1334
1335     case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
1336         if (DL_reg(context) == 0xff) 
1337         {
1338             TRACE("Direct Console Input\n");
1339
1340             if (INT21_ReadChar( NULL, TRUE ))
1341             {
1342                 BYTE ascii;
1343                 INT21_ReadChar( &ascii, FALSE );
1344                 SET_AL( context, ascii );
1345                 RESET_ZFLAG( context );
1346             }
1347             else
1348             {
1349                 /* no character available */
1350                 SET_AL( context, 0 );
1351                 SET_ZFLAG( context );
1352             }
1353         } 
1354         else 
1355         {
1356             TRACE("Direct Console Output\n");
1357             DOSVM_PutChar(DL_reg(context));
1358             /*
1359              * At least DOS versions 2.1-7.0 return character 
1360              * that was written in AL register.
1361              */
1362             SET_AL( context, DL_reg(context) );
1363         }
1364         break;
1365
1366     case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
1367         {
1368             BYTE ascii;
1369             TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
1370             INT21_ReadChar( &ascii, FALSE );
1371             SET_AL( context, ascii );
1372         }
1373         break;
1374
1375     case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
1376         {
1377             BYTE ascii;
1378             TRACE("CHARACTER INPUT WITHOUT ECHO\n");
1379             INT21_ReadChar( &ascii, FALSE );
1380             SET_AL( context, ascii );
1381         }
1382         break;
1383
1384     case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
1385         TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
1386               context->SegDs, DX_reg(context) );
1387         {
1388             LPSTR data = CTX_SEG_OFF_TO_LIN( context, 
1389                                              context->SegDs, context->Edx );
1390             LPSTR p = data;
1391
1392             /*
1393              * Do NOT use strchr() to calculate the string length,
1394              * as '\0' is valid string content, too!
1395              * Maybe we should check for non-'$' strings, but DOS doesn't.
1396              */
1397             while (*p != '$') p++;
1398
1399             if (DOSVM_IsWin16())
1400                 WriteFile( DosFileHandleToWin32Handle(1), 
1401                            data, p - data, 0, 0 );
1402             else
1403                 for(; data != p; data++)
1404                     DOSVM_PutChar( *data );
1405
1406             SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
1407         }
1408         break;
1409
1410     case 0x0a: /* BUFFERED INPUT */
1411         INT21_BufferedInput( context );
1412         break;
1413
1414     case 0x0b: /* GET STDIN STATUS */
1415         TRACE( "GET STDIN STATUS\n" );
1416         {
1417             if (INT21_ReadChar( NULL, TRUE ))
1418                 SET_AL( context, 0xff ); /* character available */
1419             else
1420                 SET_AL( context, 0 ); /* no character available */
1421         }
1422         break;
1423
1424     case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
1425         {
1426             BYTE al = AL_reg(context); /* Input function to execute after flush. */
1427
1428             TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
1429
1430             /* FIXME: buffers are not flushed */
1431
1432             /*
1433              * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
1434              * int21 function identified by AL will be called.
1435              */
1436             if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
1437             {
1438                 SET_AH( context, al );
1439                 DOSVM_Int21Handler( context );
1440             }
1441         }
1442         break;
1443
1444     case 0x0d: /* DISK BUFFER FLUSH */
1445         TRACE("DISK BUFFER FLUSH ignored\n");
1446         break;
1447
1448     case 0x0e: /* SELECT DEFAULT DRIVE */
1449         INT_Int21Handler( context );
1450         break;
1451
1452     case 0x0f: /* OPEN FILE USING FCB */
1453     case 0x10: /* CLOSE FILE USING FCB */
1454         INT_BARF( context, 0x21 );
1455         break;
1456
1457     case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1458     case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1459         INT_Int21Handler( context );
1460         break;
1461
1462     case 0x13: /* DELETE FILE USING FCB */
1463     case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
1464     case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
1465     case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
1466     case 0x17: /* RENAME FILE USING FCB */
1467         INT_BARF( context, 0x21 );
1468         break;
1469
1470     case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
1471         SET_AL( context, 0 );
1472         break;
1473
1474     case 0x19: /* GET CURRENT DEFAULT DRIVE */
1475         INT_Int21Handler( context );
1476         break;
1477
1478     case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1479         TRACE( "SET DISK TRANSFER AREA ADDRESS %04lX:%04X\n",
1480                context->SegDs, DX_reg(context) );
1481         {
1482             TDB *task = GlobalLock16( GetCurrentTask() );
1483             task->dta = MAKESEGPTR( context->SegDs, DX_reg(context) );
1484         }
1485         break;
1486
1487     case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1488     case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1489         INT_Int21Handler( context );
1490         break;
1491
1492     case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
1493     case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
1494         SET_AL( context, 0 );
1495         break;
1496
1497     case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1498         INT_Int21Handler( context );
1499         break;
1500
1501     case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
1502         SET_AL( context, 0 );
1503         break;
1504
1505     case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
1506     case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
1507     case 0x23: /* GET FILE SIZE FOR FCB */
1508     case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
1509         INT_BARF( context, 0x21 );
1510         break;
1511
1512     case 0x25: /* SET INTERRUPT VECTOR */
1513         TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
1514         {
1515             FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
1516             if (!ISV86(context) && DOSVM_IsWin16())
1517                 DOSVM_SetPMHandler16(  AL_reg(context), ptr );
1518             else
1519                 DOSVM_SetRMHandler( AL_reg(context), ptr );
1520         }
1521         break;
1522
1523     case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
1524     case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
1525     case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
1526         INT_BARF( context, 0x21 );
1527         break;
1528
1529     case 0x29: /* PARSE FILENAME INTO FCB */
1530         INT_Int21Handler( context );
1531         break;
1532
1533     case 0x2a: /* GET SYSTEM DATE */
1534         TRACE( "GET SYSTEM DATE\n" );
1535         {
1536             SYSTEMTIME systime;
1537             GetLocalTime( &systime );
1538             SET_CX( context, systime.wYear );
1539             SET_DH( context, systime.wMonth );
1540             SET_DL( context, systime.wDay );
1541             SET_AL( context, systime.wDayOfWeek );
1542         }
1543         break;
1544
1545     case 0x2b: /* SET SYSTEM DATE */
1546         FIXME("SetSystemDate(%02d/%02d/%04d): not allowed\n",
1547               DL_reg(context), DH_reg(context), CX_reg(context) );
1548         SET_AL( context, 0 );  /* Let's pretend we succeeded */
1549         break;
1550
1551     case 0x2c: /* GET SYSTEM TIME */
1552         TRACE( "GET SYSTEM TIME\n" );
1553         {
1554             SYSTEMTIME systime;
1555             GetLocalTime( &systime );
1556             SET_CH( context, systime.wHour );
1557             SET_CL( context, systime.wMinute );
1558             SET_DH( context, systime.wSecond );
1559             SET_DL( context, systime.wMilliseconds / 10 );
1560         }
1561         break;
1562
1563     case 0x2d: /* SET SYSTEM TIME */
1564         FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1565               CH_reg(context), CL_reg(context),
1566               DH_reg(context), DL_reg(context) );
1567         SET_AL( context, 0 );  /* Let's pretend we succeeded */
1568         break;
1569
1570     case 0x2e: /* SET VERIFY FLAG */
1571         TRACE("SET VERIFY FLAG ignored\n");
1572         /* we cannot change the behaviour anyway, so just ignore it */
1573         break;
1574
1575     case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1576         TRACE( "GET DISK TRANSFER AREA ADDRESS\n" );
1577         {
1578             TDB *task = GlobalLock16( GetCurrentTask() );
1579             context->SegEs = SELECTOROF( task->dta );
1580             SET_BX( context, OFFSETOF( task->dta ) );
1581         }
1582         break;
1583
1584     case 0x30: /* GET DOS VERSION */
1585         TRACE( "GET DOS VERSION - %s requested\n",
1586                (AL_reg(context) == 0x00) ? "OEM number" : "version flag" );
1587
1588         if (AL_reg(context) == 0x00)
1589             SET_BH( context, 0xff ); /* OEM number => undefined */
1590         else
1591             SET_BH( context, 0x08 ); /* version flag => DOS is in ROM */
1592
1593         SET_AL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
1594         SET_AH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
1595
1596         SET_BL( context, 0x12 );     /* 0x123456 is 24-bit Wine's serial # */
1597         SET_CX( context, 0x3456 );
1598         break;
1599
1600     case 0x31: /* TERMINATE AND STAY RESIDENT */
1601         FIXME("TERMINATE AND STAY RESIDENT stub\n");
1602         break;
1603
1604     case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1605     case 0x33: /* MULTIPLEXED */
1606         INT_Int21Handler( context );
1607         break;
1608
1609     case 0x34: /* GET ADDRESS OF INDOS FLAG */
1610         TRACE( "GET ADDRESS OF INDOS FLAG\n" );
1611         context->SegEs = INT21_GetHeapSelector( context );
1612         SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
1613         break;
1614
1615     case 0x35: /* GET INTERRUPT VECTOR */
1616         TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
1617         {
1618             FARPROC16 addr;
1619             if (!ISV86(context) && DOSVM_IsWin16())
1620                 addr = DOSVM_GetPMHandler16( AL_reg(context) );
1621             else
1622                 addr = DOSVM_GetRMHandler( AL_reg(context) );
1623             context->SegEs = SELECTOROF(addr);
1624             SET_BX( context, OFFSETOF(addr) );
1625         }
1626         break;
1627
1628     case 0x36: /* GET FREE DISK SPACE */
1629         INT_Int21Handler( context );
1630         break;
1631
1632     case 0x37: /* SWITCHAR */
1633         {
1634             switch (AL_reg(context))
1635             {
1636             case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
1637                 TRACE( "SWITCHAR - GET SWITCH CHARACTER\n" );
1638                 SET_AL( context, 0x00 ); /* success*/
1639                 SET_DL( context, '/' );
1640                 break;
1641             case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
1642                 FIXME( "SWITCHAR - SET SWITCH CHARACTER: %c\n",
1643                        DL_reg( context ));
1644                 SET_AL( context, 0x00 ); /* success*/
1645                 break;
1646             default:
1647                 INT_BARF( context, 0x21 );
1648                 break;
1649             }
1650         }
1651         break;
1652
1653     case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1654         TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
1655         if (AL_reg(context)) 
1656         {
1657             WORD country = AL_reg(context);
1658             if (country == 0xff)
1659                 country = BX_reg(context);
1660             if (country != INT21_GetSystemCountryCode())
1661                 FIXME( "Requested info on non-default country %04x\n", country );
1662         }
1663         INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context, 
1664                                                          context->SegDs, 
1665                                                          context->Edx) );
1666         SET_AX( context, INT21_GetSystemCountryCode() );
1667         SET_BX( context, INT21_GetSystemCountryCode() );
1668         break;
1669
1670     case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1671         if (!INT21_CreateDirectory( context ))
1672             bSetDOSExtendedError = TRUE;
1673         break;
1674
1675     case 0x3a: /* "RMDIR" - REMOVE DIRECTORY */
1676         {
1677             WCHAR dirW[MAX_PATH];
1678             char *dirA = CTX_SEG_OFF_TO_LIN(context,
1679                                             context->SegDs, context->Edx);
1680
1681             TRACE( "REMOVE DIRECTORY %s\n", dirA );
1682
1683             MultiByteToWideChar(CP_OEMCP, 0, dirA, -1, dirW, MAX_PATH);
1684
1685             if (!RemoveDirectoryW( dirW ))
1686                 bSetDOSExtendedError = TRUE;
1687         }
1688         break;
1689
1690     case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1691     case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1692     case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1693         INT_Int21Handler( context );
1694         break;
1695
1696     case 0x3e: /* "CLOSE" - CLOSE FILE */
1697         TRACE( "CLOSE handle %d\n", BX_reg(context) );
1698         if (_lclose16( BX_reg(context) ) == HFILE_ERROR16)
1699             bSetDOSExtendedError = TRUE;
1700         break;
1701
1702     case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1703         TRACE( "READ from %d to %04lX:%04X for %d bytes\n",
1704                BX_reg(context),
1705                context->SegDs,
1706                DX_reg(context),
1707                CX_reg(context) );
1708         {
1709             DWORD result;
1710             WORD  count  = CX_reg(context);
1711             BYTE *buffer = CTX_SEG_OFF_TO_LIN( context, 
1712                                                context->SegDs,
1713                                                context->Edx );
1714
1715             /* Some programs pass a count larger than the allocated buffer */
1716             if (DOSVM_IsWin16())
1717             {
1718                 WORD maxcount = GetSelectorLimit16( context->SegDs )
1719                     - DX_reg(context) + 1;
1720                 if (count > maxcount)
1721                     count = maxcount;
1722             }
1723
1724             /*
1725              * FIXME: Reading from console (BX=1) in DOS mode
1726              *        does not work as it is supposed to work.
1727              */
1728
1729             if (ReadFile( DosFileHandleToWin32Handle(BX_reg(context)),
1730                           buffer, count, &result, NULL ))
1731                 SET_AX( context, (WORD)result );
1732             else
1733                 bSetDOSExtendedError = TRUE;
1734         }
1735         break;
1736
1737     case 0x40:  /* "WRITE" - WRITE TO FILE OR DEVICE */
1738         TRACE( "WRITE from %04lX:%04X to handle %d for %d byte\n",
1739                context->SegDs, DX_reg(context),
1740                BX_reg(context), CX_reg(context) );
1741         {
1742             BYTE *ptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1743
1744             if (!DOSVM_IsWin16() && BX_reg(context) == 1)
1745             {
1746                 int i;
1747                 for(i=0; i<CX_reg(context); i++)
1748                     DOSVM_PutChar(ptr[i]);
1749                 SET_AX(context, CX_reg(context));
1750             }
1751             else
1752             {
1753                 HFILE handle = (HFILE)DosFileHandleToWin32Handle(BX_reg(context));
1754                 LONG result = _hwrite( handle, ptr, CX_reg(context) );
1755                 if (result == HFILE_ERROR)
1756                     bSetDOSExtendedError = TRUE;
1757                 else
1758                     SET_AX( context, (WORD)result );
1759             }
1760         }
1761         break;
1762
1763     case 0x41: /* "UNLINK" - DELETE FILE */
1764         {
1765             WCHAR fileW[MAX_PATH];
1766             char *fileA = CTX_SEG_OFF_TO_LIN(context, 
1767                                              context->SegDs, 
1768                                              context->Edx);
1769
1770             TRACE( "UNLINK %s\n", fileA );
1771             MultiByteToWideChar(CP_OEMCP, 0, fileA, -1, fileW, MAX_PATH);
1772
1773             if (!DeleteFileW( fileW ))
1774                 bSetDOSExtendedError = TRUE;
1775         }
1776         break;
1777
1778     case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1779         TRACE( "LSEEK handle %d offset %ld from %s\n",
1780                BX_reg(context), 
1781                MAKELONG( DX_reg(context), CX_reg(context) ),
1782                (AL_reg(context) == 0) ? 
1783                "start of file" : ((AL_reg(context) == 1) ? 
1784                                   "current file position" : "end of file") );
1785         {
1786             HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
1787             LONG   offset = MAKELONG( DX_reg(context), CX_reg(context) );
1788             DWORD  status = SetFilePointer( handle, offset, 
1789                                             NULL, AL_reg(context) );
1790             if (status == INVALID_SET_FILE_POINTER)
1791                 bSetDOSExtendedError = TRUE;
1792             else
1793             {
1794                 SET_AX( context, LOWORD(status) );
1795                 SET_DX( context, HIWORD(status) );
1796             }
1797         }
1798         break;
1799
1800     case 0x43: /* FILE ATTRIBUTES */
1801         if (!INT21_FileAttributes( context, AL_reg(context), FALSE ))
1802             bSetDOSExtendedError = TRUE;
1803         break;
1804
1805     case 0x44: /* IOCTL */
1806         INT21_Ioctl( context );
1807         break;
1808
1809     case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1810         TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context) );
1811         {
1812             HANDLE handle32;
1813             HFILE  handle16 = HFILE_ERROR;
1814
1815             if (DuplicateHandle( GetCurrentProcess(),
1816                                  DosFileHandleToWin32Handle(BX_reg(context)),
1817                                  GetCurrentProcess(), 
1818                                  &handle32,
1819                                  0, TRUE, DUPLICATE_SAME_ACCESS ))
1820                 handle16 = Win32HandleToDosFileHandle(handle32);
1821
1822             if (handle16 == HFILE_ERROR)
1823                 bSetDOSExtendedError = TRUE;
1824             else
1825                 SET_AX( context, handle16 );
1826         }
1827         break;
1828
1829     case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1830         TRACE( "FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
1831                BX_reg(context), CX_reg(context) );
1832         if (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR16)
1833             bSetDOSExtendedError = TRUE;
1834         break;
1835
1836     case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1837         INT_Int21Handler( context );
1838         break;
1839
1840     case 0x48: /* ALLOCATE MEMORY */
1841         TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
1842         {
1843             WORD  selector = 0;
1844             DWORD bytes = (DWORD)BX_reg(context) << 4;
1845
1846             if (!ISV86(context) && DOSVM_IsWin16())
1847             {
1848                 DWORD rv = GlobalDOSAlloc16( bytes );
1849                 selector = LOWORD( rv );
1850             }
1851             else
1852                 DOSMEM_GetBlock( bytes, &selector );
1853                
1854             if (selector)
1855                 SET_AX( context, selector );
1856             else
1857             {
1858                 SET_CFLAG(context);
1859                 SET_AX( context, 0x0008 ); /* insufficient memory */
1860                 SET_BX( context, DOSMEM_Available() >> 4 );
1861             }
1862         }
1863         break;
1864
1865     case 0x49: /* FREE MEMORY */
1866         TRACE( "FREE MEMORY segment %04lX\n", context->SegEs );
1867         {
1868             BOOL ok;
1869             
1870             if (!ISV86(context) && DOSVM_IsWin16())
1871             {
1872                 ok = !GlobalDOSFree16( context->SegEs );
1873
1874                 /* If we don't reset ES_reg, we will fail in the relay code */
1875                 if (ok)
1876                     context->SegEs = 0;
1877             }
1878             else
1879                 ok = DOSMEM_FreeBlock( (void*)((DWORD)context->SegEs << 4) );
1880
1881             if (!ok)
1882             {
1883                 TRACE("FREE MEMORY failed\n");
1884                 SET_CFLAG(context);
1885                 SET_AX( context, 0x0009 ); /* memory block address invalid */
1886             }
1887         }
1888         break;
1889
1890     case 0x4a: /* RESIZE MEMORY BLOCK */
1891         TRACE( "RESIZE MEMORY segment %04lX to %d paragraphs\n", 
1892                context->SegEs, BX_reg(context) );
1893         {
1894             DWORD newsize = (DWORD)BX_reg(context) << 4;
1895             
1896             if (!ISV86(context) && DOSVM_IsWin16())
1897             {
1898                 FIXME( "Resize memory block - unsupported under Win16\n" );
1899             }
1900             else
1901             {
1902                 LPVOID address = (void*)((DWORD)context->SegEs << 4);
1903                 UINT blocksize = DOSMEM_ResizeBlock( address, newsize, FALSE );
1904
1905                 if (blocksize == (UINT)-1)
1906                 {
1907                     SET_CFLAG( context );
1908                     SET_AX( context, 0x0009 ); /* illegal address */
1909                 }
1910                 else if(blocksize != newsize)
1911                 {
1912                     SET_CFLAG( context );
1913                     SET_AX( context, 0x0008 );    /* insufficient memory */
1914                     SET_BX( context, blocksize >> 4 ); /* new block size */
1915                 }
1916             }
1917         }
1918         break;
1919
1920     case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1921         {
1922             BYTE *program = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1923             BYTE *paramblk = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx);
1924
1925             TRACE( "EXEC %s\n", program );
1926
1927             if (DOSVM_IsWin16())
1928             {
1929                 HINSTANCE16 instance = WinExec16( program, SW_NORMAL );
1930                 if (instance < 32)
1931                 {
1932                     SET_CFLAG( context );
1933                     SET_AX( context, instance );
1934                 }
1935             }
1936             else
1937             {
1938                 if (!MZ_Exec( context, program, AL_reg(context), paramblk))
1939                     bSetDOSExtendedError = TRUE;
1940             }
1941         }
1942         break;
1943
1944     case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1945         TRACE( "EXIT with return code %d\n", AL_reg(context) );
1946         if (DOSVM_IsWin16())
1947             ExitThread( AL_reg(context) );
1948         else
1949             MZ_Exit( context, FALSE, AL_reg(context) );
1950         break;
1951
1952     case 0x4d: /* GET RETURN CODE */
1953         TRACE("GET RETURN CODE (ERRORLEVEL)\n");
1954         SET_AX( context, DOSVM_retval );
1955         DOSVM_retval = 0;
1956         break;
1957
1958     case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1959     case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1960         INT_Int21Handler( context );
1961         break;
1962
1963     case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
1964         TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
1965         DOSVM_psp = BX_reg(context);
1966         break;
1967
1968     case 0x51: /* GET PSP ADDRESS */
1969         INT21_GetPSP( context );
1970         break;
1971
1972     case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1973         if (!ISV86(context) && DOSVM_IsWin16())
1974         {
1975             SEGPTR ptr = DOSMEM_LOL()->wine_pm_lol;
1976             context->SegEs = SELECTOROF(ptr);
1977             SET_BX( context, OFFSETOF(ptr) );
1978         }
1979         else
1980         {
1981             SEGPTR ptr = DOSMEM_LOL()->wine_rm_lol;
1982             context->SegEs = SELECTOROF(ptr);
1983             SET_BX( context, OFFSETOF(ptr) );
1984         }
1985         break;
1986
1987     case 0x54: /* Get Verify Flag */
1988         TRACE("Get Verify Flag - Not Supported\n");
1989         SET_AL( context, 0x00 );  /* pretend we can tell. 00h = off 01h = on */
1990         break;
1991
1992     case 0x56: /* "RENAME" - RENAME FILE */
1993         if (!INT21_RenameFile( context ))
1994             bSetDOSExtendedError = TRUE;
1995         break;
1996
1997     case 0x57: /* FILE DATE AND TIME */
1998         if (!INT21_FileDateTime( context ))
1999             bSetDOSExtendedError = TRUE;
2000         break;
2001
2002     case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
2003         switch (AL_reg(context))
2004         {
2005         case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
2006             TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
2007             SET_AX( context, 0 ); /* low memory first fit */
2008             break;
2009
2010         case 0x01: /* SET ALLOCATION STRATEGY */
2011             TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
2012                    BL_reg(context) );
2013             break;
2014
2015         case 0x02: /* GET UMB LINK STATE */
2016             TRACE( "GET UMB LINK STATE\n" );
2017             SET_AL( context, 0 ); /* UMBs not part of DOS memory chain */
2018             break;
2019
2020         case 0x03: /* SET UMB LINK STATE */
2021             TRACE( "SET UMB LINK STATE to %d - ignored\n",
2022                    BX_reg(context) );
2023             break;
2024
2025         default:
2026             INT_BARF( context, 0x21 );
2027         }
2028         break;
2029
2030     case 0x59: /* GET EXTENDED ERROR INFO */
2031         INT21_GetExtendedError( context );
2032         break;
2033
2034     case 0x5a: /* CREATE TEMPORARY FILE */
2035     case 0x5b: /* CREATE NEW FILE */ 
2036         INT_Int21Handler( context );
2037         break;
2038
2039     case 0x5c: /* "FLOCK" - RECORD LOCKING */
2040         {
2041             DWORD  offset = MAKELONG(DX_reg(context), CX_reg(context));
2042             DWORD  length = MAKELONG(DI_reg(context), SI_reg(context));
2043             HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
2044
2045             switch (AL_reg(context))
2046             {
2047             case 0x00: /* LOCK */
2048                 TRACE( "lock handle %d offset %ld length %ld\n",
2049                        BX_reg(context), offset, length );
2050                 if (!LockFile( handle, offset, 0, length, 0 ))
2051                     bSetDOSExtendedError = TRUE;
2052                 break;
2053
2054             case 0x01: /* UNLOCK */
2055                 TRACE( "unlock handle %d offset %ld length %ld\n",
2056                        BX_reg(context), offset, length );
2057                 if (!UnlockFile( handle, offset, 0, length, 0 ))
2058                     bSetDOSExtendedError = TRUE;
2059                 break;
2060
2061             default:
2062                 INT_BARF( context, 0x21 );
2063             }
2064         }
2065         break;
2066
2067     case 0x5d: /* NETWORK 5D */
2068         FIXME( "Network function 5D not implemented.\n" );
2069         SetLastError( ER_NoNetwork );
2070         bSetDOSExtendedError = TRUE;
2071         break;
2072
2073     case 0x5e: /* NETWORK 5E */
2074     case 0x5f: /* NETWORK 5F */
2075     case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
2076         INT_Int21Handler( context );
2077         break;
2078
2079     case 0x61: /* UNUSED */
2080         SET_AL( context, 0 );
2081         break;
2082
2083     case 0x62: /* GET PSP ADDRESS */
2084         INT21_GetPSP( context );
2085         break;
2086
2087     case 0x63: /* MISC. LANGUAGE SUPPORT */
2088         switch (AL_reg(context)) {
2089         case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
2090             TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
2091             context->SegDs = INT21_GetHeapSelector( context );
2092             SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
2093             SET_AL( context, 0 ); /* success */
2094             break;
2095         }
2096         break;
2097
2098     case 0x64: /* OS/2 DOS BOX */
2099         INT_BARF( context, 0x21 );
2100         SET_CFLAG(context);
2101         break;
2102
2103     case 0x65: /* EXTENDED COUNTRY INFORMATION */
2104         INT21_ExtendedCountryInformation( context );
2105         break;
2106
2107     case 0x66: /* GLOBAL CODE PAGE TABLE */
2108         switch (AL_reg(context))
2109         {
2110         case 0x01:
2111             TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
2112             SET_BX( context, GetOEMCP() );
2113             SET_DX( context, GetOEMCP() );
2114             break;
2115         case 0x02:
2116             FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
2117                    BX_reg(context), DX_reg(context) );
2118             break;
2119         }
2120         break;
2121
2122     case 0x67: /* SET HANDLE COUNT */
2123         TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
2124         if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
2125             bSetDOSExtendedError = TRUE;
2126         break;
2127
2128     case 0x68: /* "FFLUSH" - COMMIT FILE */
2129         TRACE( "FFLUSH - handle %d\n", BX_reg(context) );
2130         if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
2131             bSetDOSExtendedError = TRUE;
2132         break;
2133
2134     case 0x69: /* DISK SERIAL NUMBER */
2135         INT_Int21Handler( context );
2136         break;
2137
2138     case 0x6a: /* COMMIT FILE */
2139         TRACE( "COMMIT FILE - handle %d\n", BX_reg(context) );
2140         if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
2141             bSetDOSExtendedError = TRUE;
2142         break;
2143
2144     case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
2145         SET_AL( context, 0 );
2146         break;
2147
2148     case 0x6c: /* EXTENDED OPEN/CREATE */
2149         INT_Int21Handler( context );
2150         break;
2151
2152     case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
2153         FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
2154         SET_CFLAG( context );
2155         SET_AL( context, 0 );
2156         break;
2157
2158     case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
2159         INT21_LongFilename( context );
2160         break;
2161
2162     case 0x73: /* MSDOS7 - FAT32 */
2163         INT_Int21Handler( context );
2164         break;
2165
2166     case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
2167         TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
2168         break;
2169
2170     case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
2171         TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
2172         break;
2173
2174     default:
2175         INT_BARF( context, 0x21 );
2176         break;
2177
2178     } /* END OF SWITCH */
2179
2180     /* Set general error condition. */
2181     if (bSetDOSExtendedError)
2182     {
2183         SET_AX( context, GetLastError() );
2184         SET_CFLAG( context );
2185     }
2186
2187     /* Print error code if carry flag is set. */
2188     if (context->EFlags & 0x0001)
2189         TRACE("failed, error %ld\n", GetLastError() );
2190
2191     TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
2192            "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
2193            AX_reg(context), BX_reg(context), 
2194            CX_reg(context), DX_reg(context), 
2195            SI_reg(context), DI_reg(context),
2196            (WORD)context->SegDs, (WORD)context->SegEs,
2197            context->EFlags );
2198 }