Fix buffered input function. Add support for UMB subfunctions to
[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 "winerror.h"
36 #include "winuser.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
39
40 /*
41  * FIXME: Delete this reference when all int21 code has been moved to winedos.
42  */
43 extern void WINAPI INT_Int21Handler( CONTEXT86 *context );
44
45 WINE_DEFAULT_DEBUG_CHANNEL(int21);
46
47
48 #include "pshpack1.h"
49
50 /*
51  * Structure for DOS data that can be accessed directly from applications.
52  * Real and protected mode pointers will be returned to this structure so
53  * the structure must be correctly packed.
54  */
55 typedef struct _INT21_HEAP {
56     WORD uppercase_size;             /* Size of the following table in bytes */
57     BYTE uppercase_table[128];       /* Uppercase equivalents of chars from 0x80 to 0xff. */
58
59     WORD lowercase_size;             /* Size of the following table in bytes */
60     BYTE lowercase_table[256];       /* Lowercase equivalents of chars from 0x00 to 0xff. */
61
62     WORD collating_size;             /* Size of the following table in bytes */
63     BYTE collating_table[256];       /* Values used to sort characters from 0x00 to 0xff. */
64
65     WORD filename_size;              /* Size of the following filename data in bytes */
66     BYTE filename_reserved1;         /* 0x01 for MS-DOS 3.30-6.00 */
67     BYTE filename_lowest;            /* Lowest permissible character value for filename */
68     BYTE filename_highest;           /* Highest permissible character value for filename */
69     BYTE filename_reserved2;         /* 0x00 for MS-DOS 3.30-6.00 */
70     BYTE filename_exclude_first;     /* First illegal character in permissible range */
71     BYTE filename_exclude_last;      /* Last illegal character in permissible range */
72     BYTE filename_reserved3;         /* 0x02 for MS-DOS 3.30-6.00 */
73     BYTE filename_illegal_size;      /* Number of terminators in the following table */
74     BYTE filename_illegal_table[16]; /* Characters which terminate a filename */
75
76     WORD dbcs_size;                  /* Number of valid ranges in the following table */
77     BYTE dbcs_table[16];             /* Start/end bytes for N ranges and 00/00 as terminator */
78
79     BYTE misc_indos;                 /* Interrupt 21 nesting flag */
80 } INT21_HEAP;
81
82 #include "poppack.h"
83
84
85 /***********************************************************************
86  *           INT21_ReadChar
87  *
88  * Reads a character from the standard input.
89  * Extended keycodes will be returned as two separate characters.
90  */
91 static BOOL INT21_ReadChar( BYTE *input, BOOL peek )
92 {
93     static BYTE pending_scan = 0;
94
95     if (pending_scan)
96     {
97         if (input)
98             *input = pending_scan;
99         if (!peek)
100             pending_scan = 0;
101         return TRUE;
102     }
103     else
104     {
105         BYTE ascii;
106         BYTE scan;
107         if (!DOSVM_Int16ReadChar( &ascii, &scan, peek ))
108             return FALSE;
109
110         if (input)
111             *input = ascii;
112         if (!peek && !ascii)
113             pending_scan = scan;
114         return TRUE;
115     }
116 }
117
118
119 /***********************************************************************
120  *           INT21_GetSystemCountryCode
121  *
122  * Return DOS country code for default system locale.
123  */
124 static WORD INT21_GetSystemCountryCode()
125 {
126     /*
127      * FIXME: Determine country code. We should probably use
128      *        DOSCONF structure for that.
129      */
130     return GetSystemDefaultLangID();
131 }
132
133
134 /***********************************************************************
135  *           INT21_FillCountryInformation
136  *
137  * Fill 34-byte buffer with country information data using
138  * default system locale.
139  */
140 static void INT21_FillCountryInformation( BYTE *buffer )
141 {
142     /* 00 - WORD: date format
143      *          00 = mm/dd/yy
144      *          01 = dd/mm/yy
145      *          02 = yy/mm/dd
146      */
147     *(WORD*)(buffer + 0) = 0; /* FIXME: Get from locale */
148
149     /* 02 - BYTE[5]: ASCIIZ currency symbol string */
150     buffer[2] = '$'; /* FIXME: Get from locale */
151     buffer[3] = 0;
152
153     /* 07 - BYTE[2]: ASCIIZ thousands separator */
154     buffer[7] = 0; /* FIXME: Get from locale */
155     buffer[8] = 0;
156
157     /* 09 - BYTE[2]: ASCIIZ decimal separator */
158     buffer[9]  = '.'; /* FIXME: Get from locale */
159     buffer[10] = 0;
160
161     /* 11 - BYTE[2]: ASCIIZ date separator */
162     buffer[11] = '/'; /* FIXME: Get from locale */
163     buffer[12] = 0;
164
165     /* 13 - BYTE[2]: ASCIIZ time separator */
166     buffer[13] = ':'; /* FIXME: Get from locale */
167     buffer[14] = 0;
168
169     /* 15 - BYTE: Currency format
170      *          bit 2 = set if currency symbol replaces decimal point
171      *          bit 1 = number of spaces between value and currency symbol
172      *          bit 0 = 0 if currency symbol precedes value
173      *                  1 if currency symbol follows value
174      */
175     buffer[15] = 0; /* FIXME: Get from locale */
176
177     /* 16 - BYTE: Number of digits after decimal in currency */
178     buffer[16] = 0; /* FIXME: Get from locale */
179
180     /* 17 - BYTE: Time format
181      *          bit 0 = 0 if 12-hour clock
182      *                  1 if 24-hour clock
183      */
184     buffer[17] = 1; /* FIXME: Get from locale */
185
186     /* 18 - DWORD: Address of case map routine */
187     *(DWORD*)(buffer + 18) = 0; /* FIXME: ptr to case map routine */
188
189     /* 22 - BYTE[2]: ASCIIZ data-list separator */
190     buffer[22] = ','; /* FIXME: Get from locale */
191     buffer[23] = 0;
192
193     /* 24 - BYTE[10]: Reserved */
194     memset( buffer + 24, 0, 10 );
195 }
196
197
198 /***********************************************************************
199  *           INT21_FillHeap
200  *
201  * Initialize DOS heap.
202  */
203 static void INT21_FillHeap( INT21_HEAP *heap )
204 {
205     static const char terminators[] = "\"\\./[]:|<>+=;,";
206     int i;
207
208     /*
209      * Uppercase table.
210      */
211     heap->uppercase_size = 128;
212     for (i = 0; i < 128; i++) 
213         heap->uppercase_table[i] = toupper( 128 + i );
214
215     /*
216      * Lowercase table.
217      */
218     heap->lowercase_size = 256;
219     for (i = 0; i < 256; i++) 
220         heap->lowercase_table[i] = tolower( i );
221     
222     /*
223      * Collating table.
224      */
225     heap->collating_size = 256;
226     for (i = 0; i < 256; i++) 
227         heap->collating_table[i] = i;
228
229     /*
230      * Filename table.
231      */
232     heap->filename_size = 8 + strlen(terminators);
233     heap->filename_illegal_size = strlen(terminators);
234     strcpy( heap->filename_illegal_table, terminators );
235
236     heap->filename_reserved1 = 0x01;
237     heap->filename_lowest = 0;           /* FIXME: correct value? */
238     heap->filename_highest = 0xff;       /* FIXME: correct value? */
239     heap->filename_reserved2 = 0x00;    
240     heap->filename_exclude_first = 0x00; /* FIXME: correct value? */
241     heap->filename_exclude_last = 0x00;  /* FIXME: correct value? */
242     heap->filename_reserved3 = 0x02;
243
244     /*
245      * DBCS lead byte table. This table is empty.
246      */
247     heap->dbcs_size = 0;
248     memset( heap->dbcs_table, 0, sizeof(heap->dbcs_table) );
249
250     /*
251      * Initialize InDos flag.
252      */
253     heap->misc_indos = 0;
254 }
255
256
257 /***********************************************************************
258  *           INT21_GetHeapSelector
259  *
260  * Get segment/selector for DOS heap (INT21_HEAP).
261  * Creates and initializes heap on first call.
262  */
263 static WORD INT21_GetHeapSelector( CONTEXT86 *context )
264 {
265     static WORD heap_segment = 0;
266     static WORD heap_selector = 0;
267     static BOOL heap_initialized = FALSE;
268
269     if (!heap_initialized)
270     {
271         INT21_HEAP *ptr = DOSVM_AllocDataUMB( sizeof(INT21_HEAP), 
272                                               &heap_segment,
273                                               &heap_selector );
274         INT21_FillHeap( ptr );
275         heap_initialized = TRUE;
276     }
277
278     if (!ISV86(context) && DOSVM_IsWin16())
279         return heap_selector;
280     else
281         return heap_segment;
282 }
283
284
285 /***********************************************************************
286  *           INT21_BufferedInput
287  *
288  * Handler for function 0x0a.
289  *
290  * Reads a string of characters from standard input until
291  * enter key is pressed.
292  */
293 static void INT21_BufferedInput( CONTEXT86 *context )
294 {
295     BYTE *ptr = CTX_SEG_OFF_TO_LIN(context,
296                                    context->SegDs,
297                                    context->Edx);
298     BYTE capacity = ptr[0]; /* includes CR */
299     BYTE length = 0;        /* excludes CR */
300
301     TRACE( "BUFFERED INPUT (size=%d)\n", capacity );
302
303     /*
304      * Return immediately if capacity is zero.
305      *
306      * FIXME: What to return to application?
307      */
308     if (capacity == 0)
309         return;
310
311     /*
312      * FIXME: Some documents state that
313      *        ptr[1] holds number of chars from last input which 
314      *        may be recalled on entry, other documents do not mention
315      *        this at all.
316      */
317     if (ptr[1])
318         TRACE( "Handle old chars in buffer!\n" );
319
320     while(TRUE)
321     {
322         BYTE ascii;
323         BYTE scan;
324
325         DOSVM_Int16ReadChar( &ascii, &scan, FALSE );
326
327         if (ascii == '\r' || ascii == '\n')
328         {
329             /*
330              * FIXME: What should be echoed here?
331              */
332             DOSVM_PutChar( '\r' );
333             DOSVM_PutChar( '\n' );
334             ptr[1] = length;
335             ptr[2 + length] = '\r';
336             return;
337         }
338
339         /*
340          * FIXME: This function is supposed to support
341          *        DOS editing keys...
342          */
343
344         /*
345          * If the buffer becomes filled to within one byte of
346          * capacity, DOS rejects all further characters up to,
347          * but not including, the terminating carriage return.
348          */
349         if (ascii != 0 && length < capacity-1)
350         {
351             DOSVM_PutChar( ascii );
352             ptr[2 + length] = ascii;
353             length++;
354         }
355     }
356 }
357
358
359 /***********************************************************************
360  *           INT21_ExtendedCountryInformation
361  *
362  * Handler for function 0x65.
363  */
364 static void INT21_ExtendedCountryInformation( CONTEXT86 *context )
365 {
366     BYTE *dataptr = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
367
368     TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
369            AL_reg(context) );
370
371     /*
372      * Check subfunctions that are passed country and code page.
373      */
374     if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
375     {
376         WORD country = DX_reg(context);
377         WORD codepage = BX_reg(context);
378
379         if (country != 0xffff && country != INT21_GetSystemCountryCode())
380             FIXME( "Requested info on non-default country %04x\n", country );
381
382         if (codepage != 0xffff && codepage != GetOEMCP())
383             FIXME( "Requested info on non-default code page %04x\n", codepage );
384     }
385
386     switch (AL_reg(context)) {
387     case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
388         INT_BARF( context, 0x21 );
389         SET_CFLAG( context );
390         break;
391
392     case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
393         TRACE( "Get general internationalization info\n" );
394         dataptr[0] = 0x01; /* Info ID */
395         *(WORD*)(dataptr+1) = 38; /* Size of the following info */
396         *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
397         *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
398         INT21_FillCountryInformation( dataptr + 7 );
399         SET_CX( context, 41 ); /* Size of returned info */
400         break;
401         
402     case 0x02: /* GET POINTER TO UPPERCASE TABLE */
403     case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
404         TRACE( "Get pointer to uppercase table\n" );
405         dataptr[0] = AL_reg(context); /* Info ID */
406         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
407                                            offsetof(INT21_HEAP, uppercase_size) );
408         SET_CX( context, 5 ); /* Size of returned info */
409         break;
410
411     case 0x03: /* GET POINTER TO LOWERCASE TABLE */
412         TRACE( "Get pointer to lowercase table\n" );
413         dataptr[0] = 0x03; /* Info ID */
414         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
415                                            offsetof(INT21_HEAP, lowercase_size) );
416         SET_CX( context, 5 ); /* Size of returned info */
417         break;
418
419     case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
420         TRACE("Get pointer to filename terminator table\n");
421         dataptr[0] = 0x05; /* Info ID */
422         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
423                                            offsetof(INT21_HEAP, filename_size) );
424         SET_CX( context, 5 ); /* Size of returned info */
425         break;
426
427     case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
428         TRACE("Get pointer to collating sequence table\n");
429         dataptr[0] = 0x06; /* Info ID */
430         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
431                                            offsetof(INT21_HEAP, collating_size) );
432         SET_CX( context, 5 ); /* Size of returned info */
433         break;
434
435     case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
436         TRACE("Get pointer to DBCS lead byte table\n");
437         dataptr[0] = 0x07; /* Info ID */
438         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
439                                            offsetof(INT21_HEAP, dbcs_size) );
440         SET_CX( context, 5 ); /* Size of returned info */
441         break;
442
443     case 0x20: /* CAPITALIZE CHARACTER */
444     case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
445         TRACE("Convert char to uppercase\n");
446         SET_DL( context, toupper(DL_reg(context)) );
447         break;
448
449     case 0x21: /* CAPITALIZE STRING */
450     case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
451         TRACE("Convert string to uppercase with length\n");
452         {
453             char *ptr = (char *)CTX_SEG_OFF_TO_LIN( context,
454                                                     context->SegDs,
455                                                     context->Edx );
456             WORD len = CX_reg(context);
457             while (len--) { *ptr = toupper(*ptr); ptr++; }
458         }
459         break;
460
461     case 0x22: /* CAPITALIZE ASCIIZ STRING */
462     case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
463         TRACE("Convert ASCIIZ string to uppercase\n");
464         _strupr( (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx) );
465         break;
466
467     case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
468         INT_BARF( context, 0x21 );
469         SET_CFLAG( context );
470         break;
471
472     default:
473         INT_BARF( context, 0x21 );
474         SET_CFLAG(context);
475         break;
476     }
477 }
478
479
480 /***********************************************************************
481  *           INT21_FileDateTime
482  *
483  * Handler for function 0x57.
484  */
485 static BOOL INT21_FileDateTime( CONTEXT86 *context )
486 {
487     HANDLE   handle = DosFileHandleToWin32Handle(BX_reg(context));
488     FILETIME filetime;
489     WORD     date, time;
490
491     switch (AL_reg(context)) {
492     case 0x00:  /* Get last-written stamp */
493         TRACE( "GET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
494                BX_reg(context) );
495         {
496             if (!GetFileTime( handle, NULL, NULL, &filetime ))
497                 return FALSE;
498             FileTimeToDosDateTime( &filetime, &date, &time );
499             SET_DX( context, date );
500             SET_CX( context, time );
501             break;
502         }
503
504     case 0x01:  /* Set last-written stamp */
505         TRACE( "SET FILE LAST-WRITTEN DATE AND TIME, handle %d\n",
506                BX_reg(context) );
507         {
508             DosDateTimeToFileTime( DX_reg(context), 
509                                    CX_reg(context),
510                                    &filetime );
511             if (!SetFileTime( handle, NULL, NULL, &filetime ))
512                 return FALSE;
513             break;
514         }
515
516     case 0x04:  /* Get last access stamp, DOS 7 */
517         TRACE( "GET FILE LAST ACCESS DATE AND TIME, handle %d\n",
518                BX_reg(context) );
519         {
520             if (!GetFileTime( handle, NULL, &filetime, NULL ))
521                 return FALSE;
522             FileTimeToDosDateTime( &filetime, &date, &time );
523             SET_DX( context, date );
524             SET_CX( context, time );
525             break;
526         }
527
528     case 0x05:  /* Set last access stamp, DOS 7 */
529         TRACE( "SET FILE LAST ACCESS DATE AND TIME, handle %d\n",
530                BX_reg(context) );
531         {
532             DosDateTimeToFileTime( DX_reg(context), 
533                                    CX_reg(context),
534                                    &filetime );
535             if (!SetFileTime( handle, NULL, &filetime, NULL ))
536                 return FALSE;
537             break;
538         }
539
540     case 0x06:  /* Get creation stamp, DOS 7 */
541         TRACE( "GET FILE CREATION DATE AND TIME, handle %d\n",
542                BX_reg(context) );
543         {
544             if (!GetFileTime( handle, &filetime, NULL, NULL ))
545                 return FALSE;
546             FileTimeToDosDateTime( &filetime, &date, &time );
547             SET_DX( context, date );
548             SET_CX( context, time );
549             /*
550              * FIXME: SI has number of 10-millisecond units past time in CX.
551              */
552             SET_SI( context, 0 );
553             break;
554         }
555
556     case 0x07:  /* Set creation stamp, DOS 7 */
557         TRACE( "SET FILE CREATION DATE AND TIME, handle %d\n",
558                BX_reg(context) );
559         {
560             /*
561              * FIXME: SI has number of 10-millisecond units past time in CX.
562              */
563             DosDateTimeToFileTime( DX_reg(context), 
564                                    CX_reg(context),
565                                    &filetime );
566             if (!SetFileTime( handle, &filetime, NULL, NULL ))
567                 return FALSE;
568             break;
569         }
570
571     default:
572         INT_BARF( context, 0x21 );
573         break;
574     }
575
576     return TRUE;
577 }
578
579
580 /***********************************************************************
581  *           INT21_GetPSP
582  *
583  * Handler for functions 0x51 and 0x62.
584  */
585 static void INT21_GetPSP( CONTEXT86 *context )
586 {
587     TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
588
589     /*
590      * FIXME: should we return the original DOS PSP upon
591      *        Windows startup ? 
592      */
593     if (!ISV86(context) && DOSVM_IsWin16())
594         SET_BX( context, LOWORD(GetCurrentPDB16()) );
595     else
596         SET_BX( context, DOSVM_psp );
597 }
598
599
600 /***********************************************************************
601  *           INT21_Ioctl
602  *
603  * Handler for function 0x44.
604  */
605 static void INT21_Ioctl( CONTEXT86 *context )
606 {
607   static const WCHAR emmxxxx0W[] = {'E','M','M','X','X','X','X','0',0};
608   const DOS_DEVICE *dev = DOSFS_GetDeviceByHandle(
609       DosFileHandleToWin32Handle(BX_reg(context)) );
610
611   if (dev && !strcmpiW( dev->name, emmxxxx0W )) {
612     EMS_Ioctl_Handler(context);
613     return;
614   }
615
616   switch (AL_reg(context))
617   {
618   case 0x0b: /* SET SHARING RETRY COUNT */
619       TRACE("IOCTL - SET SHARING RETRY COUNT pause %d retries %d\n",
620            CX_reg(context), DX_reg(context));
621       if (!CX_reg(context))
622       {
623          SET_AX( context, 1 );
624          SET_CFLAG(context);
625          break;
626       }
627       DOSMEM_LOL()->sharing_retry_delay = CX_reg(context);
628       if (!DX_reg(context))
629          DOSMEM_LOL()->sharing_retry_count = DX_reg(context);
630       RESET_CFLAG(context);
631       break;
632   default:
633       INT_Int21Handler( context );
634   }
635 }
636
637
638 /***********************************************************************
639  *           INT21_GetExtendedError
640  */
641 static void INT21_GetExtendedError( CONTEXT86 *context )
642 {
643     BYTE class, action, locus;
644     WORD error = GetLastError();
645
646     switch(error)
647     {
648     case ERROR_SUCCESS:
649         class = action = locus = 0;
650         break;
651     case ERROR_DIR_NOT_EMPTY:
652         class  = EC_Exists;
653         action = SA_Ignore;
654         locus  = EL_Disk;
655         break;
656     case ERROR_ACCESS_DENIED:
657         class  = EC_AccessDenied;
658         action = SA_Abort;
659         locus  = EL_Disk;
660         break;
661     case ERROR_CANNOT_MAKE:
662         class  = EC_AccessDenied;
663         action = SA_Abort;
664         locus  = EL_Unknown;
665         break;
666     case ERROR_DISK_FULL:
667     case ERROR_HANDLE_DISK_FULL:
668         class  = EC_MediaError;
669         action = SA_Abort;
670         locus  = EL_Disk;
671         break;
672     case ERROR_FILE_EXISTS:
673     case ERROR_ALREADY_EXISTS:
674         class  = EC_Exists;
675         action = SA_Abort;
676         locus  = EL_Disk;
677         break;
678     case ERROR_FILE_NOT_FOUND:
679         class  = EC_NotFound;
680         action = SA_Abort;
681         locus  = EL_Disk;
682         break;
683     case ER_GeneralFailure:
684         class  = EC_SystemFailure;
685         action = SA_Abort;
686         locus  = EL_Unknown;
687         break;
688     case ERROR_INVALID_DRIVE:
689         class  = EC_MediaError;
690         action = SA_Abort;
691         locus  = EL_Disk;
692         break;
693     case ERROR_INVALID_HANDLE:
694         class  = EC_ProgramError;
695         action = SA_Abort;
696         locus  = EL_Disk;
697         break;
698     case ERROR_LOCK_VIOLATION:
699         class  = EC_AccessDenied;
700         action = SA_Abort;
701         locus  = EL_Disk;
702         break;
703     case ERROR_NO_MORE_FILES:
704         class  = EC_MediaError;
705         action = SA_Abort;
706         locus  = EL_Disk;
707         break;
708     case ER_NoNetwork:
709         class  = EC_NotFound;
710         action = SA_Abort;
711         locus  = EL_Network;
712         break;
713     case ERROR_NOT_ENOUGH_MEMORY:
714         class  = EC_OutOfResource;
715         action = SA_Abort;
716         locus  = EL_Memory;
717         break;
718     case ERROR_PATH_NOT_FOUND:
719         class  = EC_NotFound;
720         action = SA_Abort;
721         locus  = EL_Disk;
722         break;
723     case ERROR_SEEK:
724         class  = EC_NotFound;
725         action = SA_Ignore;
726         locus  = EL_Disk;
727         break;
728     case ERROR_SHARING_VIOLATION:
729         class  = EC_Temporary;
730         action = SA_Retry;
731         locus  = EL_Disk;
732         break;
733     case ERROR_TOO_MANY_OPEN_FILES:
734         class  = EC_ProgramError;
735         action = SA_Abort;
736         locus  = EL_Disk;
737         break;
738     default:
739         FIXME("Unknown error %d\n", error );
740         class  = EC_SystemFailure;
741         action = SA_Abort;
742         locus  = EL_Unknown;
743         break;
744     }
745     TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
746            error, class, action, locus );
747     SET_AX( context, error );
748     SET_BH( context, class );
749     SET_BL( context, action );
750     SET_CH( context, locus );
751 }
752
753
754 /***********************************************************************
755  *           DOSVM_Int21Handler
756  *
757  * Interrupt 0x21 handler.
758  */
759 void WINAPI DOSVM_Int21Handler( CONTEXT86 *context )
760 {
761     BOOL bSetDOSExtendedError = FALSE;
762
763     TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
764            "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
765            AX_reg(context), BX_reg(context), 
766            CX_reg(context), DX_reg(context),
767            SI_reg(context), DI_reg(context),
768            (WORD)context->SegDs, (WORD)context->SegEs,
769            context->EFlags );
770
771    /*
772     * Extended error is used by (at least) functions 0x2f to 0x62.
773     * Function 0x59 returns extended error and error should not
774     * be cleared before handling the function.
775     */
776     if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59) 
777         SetLastError(0);
778
779     RESET_CFLAG(context); /* Not sure if this is a good idea. */
780
781     switch(AH_reg(context))
782     {
783     case 0x00: /* TERMINATE PROGRAM */
784         TRACE("TERMINATE PROGRAM\n");
785         if (DOSVM_IsWin16())
786             ExitThread( 0 );
787         else
788             MZ_Exit( context, FALSE, 0 );
789         break;
790
791     case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
792         {
793             BYTE ascii;
794             TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
795             INT21_ReadChar( &ascii, FALSE );
796             SET_AL( context, ascii );
797             /*
798              * FIXME: What to echo when extended keycodes are read?
799              */
800             DOSVM_PutChar(AL_reg(context));
801         }
802         break;
803
804     case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
805         TRACE("Write Character to Standard Output\n");
806         DOSVM_PutChar(DL_reg(context));
807         break;
808
809     case 0x03: /* READ CHARACTER FROM STDAUX  */
810     case 0x04: /* WRITE CHARACTER TO STDAUX */
811     case 0x05: /* WRITE CHARACTER TO PRINTER */
812         INT_BARF( context, 0x21 );
813         break;
814
815     case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
816         if (DL_reg(context) == 0xff) 
817         {
818             TRACE("Direct Console Input\n");
819
820             if (INT21_ReadChar( NULL, TRUE ))
821             {
822                 BYTE ascii;
823                 INT21_ReadChar( &ascii, FALSE );
824                 SET_AL( context, ascii );
825                 RESET_ZFLAG( context );
826             }
827             else
828             {
829                 /* no character available */
830                 SET_AL( context, 0 );
831                 SET_ZFLAG( context );
832             }
833         } 
834         else 
835         {
836             TRACE("Direct Console Output\n");
837             DOSVM_PutChar(DL_reg(context));
838             /*
839              * At least DOS versions 2.1-7.0 return character 
840              * that was written in AL register.
841              */
842             SET_AL( context, DL_reg(context) );
843         }
844         break;
845
846     case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
847         {
848             BYTE ascii;
849             TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
850             INT21_ReadChar( &ascii, FALSE );
851             SET_AL( context, ascii );
852         }
853         break;
854
855     case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
856         {
857             BYTE ascii;
858             TRACE("CHARACTER INPUT WITHOUT ECHO\n");
859             INT21_ReadChar( &ascii, FALSE );
860             SET_AL( context, ascii );
861         }
862         break;
863
864     case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
865         INT_Int21Handler( context );
866         break;
867
868     case 0x0a: /* BUFFERED INPUT */
869         INT21_BufferedInput( context );
870         break;
871
872     case 0x0b: /* GET STDIN STATUS */
873         TRACE( "GET STDIN STATUS\n" );
874         {
875             if (INT21_ReadChar( NULL, TRUE ))
876                 SET_AL( context, 0xff ); /* character available */
877             else
878                 SET_AL( context, 0 ); /* no character available */
879         }
880         break;
881
882     case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
883         {
884             BYTE al = AL_reg(context); /* Input function to execute after flush. */
885
886             TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
887
888             /* FIXME: buffers are not flushed */
889
890             /*
891              * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
892              * int21 function identified by AL will be called.
893              */
894             if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
895             {
896                 SET_AH( context, al );
897                 DOSVM_Int21Handler( context );
898             }
899         }
900         break;
901
902     case 0x0d: /* DISK BUFFER FLUSH */
903         TRACE("DISK BUFFER FLUSH ignored\n");
904         break;
905
906     case 0x0e: /* SELECT DEFAULT DRIVE */
907         INT_Int21Handler( context );
908         break;
909
910     case 0x0f: /* OPEN FILE USING FCB */
911     case 0x10: /* CLOSE FILE USING FCB */
912         INT_BARF( context, 0x21 );
913         break;
914
915     case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
916     case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
917         INT_Int21Handler( context );
918         break;
919
920     case 0x13: /* DELETE FILE USING FCB */
921     case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
922     case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
923     case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
924     case 0x17: /* RENAME FILE USING FCB */
925         INT_BARF( context, 0x21 );
926         break;
927
928     case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
929         SET_AL( context, 0 );
930         break;
931
932     case 0x19: /* GET CURRENT DEFAULT DRIVE */
933     case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
934     case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */        
935     case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
936         INT_Int21Handler( context );
937         break;
938
939     case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
940     case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
941         SET_AL( context, 0 );
942         break;
943
944     case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
945         INT_Int21Handler( context );
946         break;
947
948     case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
949         SET_AL( context, 0 );
950         break;
951
952     case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
953     case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
954     case 0x23: /* GET FILE SIZE FOR FCB */
955     case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
956         INT_BARF( context, 0x21 );
957         break;
958
959     case 0x25: /* SET INTERRUPT VECTOR */
960         TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
961         {
962             FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
963             if (!ISV86(context) && DOSVM_IsWin16())
964                 DOSVM_SetPMHandler16(  AL_reg(context), ptr );
965             else
966                 DOSVM_SetRMHandler( AL_reg(context), ptr );
967         }
968         break;
969
970     case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
971     case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
972     case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
973         INT_BARF( context, 0x21 );
974         break;
975
976     case 0x29: /* PARSE FILENAME INTO FCB */
977         INT_Int21Handler( context );
978         break;
979
980     case 0x2a: /* GET SYSTEM DATE */
981         TRACE( "GET SYSTEM DATE\n" );
982         {
983             SYSTEMTIME systime;
984             GetLocalTime( &systime );
985             SET_CX( context, systime.wYear );
986             SET_DH( context, systime.wMonth );
987             SET_DL( context, systime.wDay );
988             SET_AL( context, systime.wDayOfWeek );
989         }
990         break;
991
992     case 0x2b: /* SET SYSTEM DATE */
993         FIXME("SetSystemDate(%02d/%02d/%04d): not allowed\n",
994               DL_reg(context), DH_reg(context), CX_reg(context) );
995         SET_AL( context, 0 );  /* Let's pretend we succeeded */
996         break;
997
998     case 0x2c: /* GET SYSTEM TIME */
999         TRACE( "GET SYSTEM TIME\n" );
1000         {
1001             SYSTEMTIME systime;
1002             GetLocalTime( &systime );
1003             SET_CH( context, systime.wHour );
1004             SET_CL( context, systime.wMinute );
1005             SET_DH( context, systime.wSecond );
1006             SET_DL( context, systime.wMilliseconds / 10 );
1007         }
1008         break;
1009
1010     case 0x2d: /* SET SYSTEM TIME */
1011         FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1012               CH_reg(context), CL_reg(context),
1013               DH_reg(context), DL_reg(context) );
1014         SET_AL( context, 0 );  /* Let's pretend we succeeded */
1015         break;
1016
1017     case 0x2e: /* SET VERIFY FLAG */
1018         TRACE("SET VERIFY FLAG ignored\n");
1019         /* we cannot change the behaviour anyway, so just ignore it */
1020         break;
1021
1022     case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */ 
1023         INT_Int21Handler( context );
1024         break;
1025
1026     case 0x30: /* GET DOS VERSION */
1027         TRACE( "GET DOS VERSION - %s requested\n",
1028                (AL_reg(context) == 0x00) ? "OEM number" : "version flag" );
1029
1030         SET_AL( context, HIBYTE(HIWORD(GetVersion16())) ); /* major version */
1031         SET_AH( context, LOBYTE(HIWORD(GetVersion16())) ); /* minor version */
1032
1033         if (AL_reg(context) == 0x00)
1034             SET_BH( context, 0xff ); /* OEM number => undefined */
1035         else
1036             SET_BH( context, 0x08 ); /* version flag => DOS is in ROM */
1037
1038         SET_BL( context, 0x12 );     /* 0x123456 is Wine's serial # */
1039         SET_CX( context, 0x3456 );
1040         break;
1041
1042     case 0x31: /* TERMINATE AND STAY RESIDENT */
1043         FIXME("TERMINATE AND STAY RESIDENT stub\n");
1044         break;
1045
1046     case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1047     case 0x33: /* MULTIPLEXED */
1048         INT_Int21Handler( context );
1049         break;
1050
1051     case 0x34: /* GET ADDRESS OF INDOS FLAG */
1052         TRACE( "GET ADDRESS OF INDOS FLAG\n" );
1053         context->SegEs = INT21_GetHeapSelector( context );
1054         SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
1055         break;
1056
1057     case 0x35: /* GET INTERRUPT VECTOR */
1058         TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
1059         {
1060             FARPROC16 addr;
1061             if (!ISV86(context) && DOSVM_IsWin16())
1062                 addr = DOSVM_GetPMHandler16( AL_reg(context) );
1063             else
1064                 addr = DOSVM_GetRMHandler( AL_reg(context) );
1065             context->SegEs = SELECTOROF(addr);
1066             SET_BX( context, OFFSETOF(addr) );
1067         }
1068         break;
1069
1070     case 0x36: /* GET FREE DISK SPACE */
1071     case 0x37: /* SWITCHAR */
1072         INT_Int21Handler( context );
1073         break;
1074
1075     case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1076         TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
1077         if (AL_reg(context)) 
1078         {
1079             WORD country = AL_reg(context);
1080             if (country == 0xff)
1081                 country = BX_reg(context);
1082             if (country != INT21_GetSystemCountryCode())
1083                 FIXME( "Requested info on non-default country %04x\n", country );
1084         }
1085         INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context, 
1086                                                          context->SegDs, 
1087                                                          context->Edx) );
1088         SET_AX( context, INT21_GetSystemCountryCode() );
1089         SET_BX( context, INT21_GetSystemCountryCode() );
1090         break;
1091
1092     case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1093     case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1094     case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1095     case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1096     case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1097     case 0x3e: /* "CLOSE" - CLOSE FILE */
1098     case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1099         INT_Int21Handler( context );
1100         break;
1101
1102     case 0x40:  /* "WRITE" - WRITE TO FILE OR DEVICE */
1103         TRACE( "WRITE from %04lX:%04X to handle %d for %d byte\n",
1104                context->SegDs, DX_reg(context),
1105                BX_reg(context), CX_reg(context) );
1106         {
1107             BYTE *ptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1108
1109             if (!DOSVM_IsWin16() && BX_reg(context) == 1)
1110             {
1111                 int i;
1112                 for(i=0; i<CX_reg(context); i++)
1113                     DOSVM_PutChar(ptr[i]);
1114                 SET_AX(context, CX_reg(context));
1115             }
1116             else
1117             {
1118                 HFILE handle = (HFILE)DosFileHandleToWin32Handle(BX_reg(context));
1119                 LONG result = _hwrite( handle, ptr, CX_reg(context) );
1120                 if (result == HFILE_ERROR)
1121                     bSetDOSExtendedError = TRUE;
1122                 else
1123                     SET_AX( context, (WORD)result );
1124             }
1125         }
1126         break;
1127
1128     case 0x41: /* "UNLINK" - DELETE FILE */
1129     case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1130     case 0x43: /* FILE ATTRIBUTES */
1131         INT_Int21Handler( context );
1132         break;
1133
1134     case 0x44: /* IOCTL */
1135         INT21_Ioctl( context );
1136         break;
1137
1138     case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1139         TRACE( "DUPLICATE FILE HANDLE %d\n", BX_reg(context) );
1140         {
1141             HANDLE handle32;
1142             HFILE  handle16 = HFILE_ERROR;
1143
1144             if (DuplicateHandle( GetCurrentProcess(),
1145                                  DosFileHandleToWin32Handle(BX_reg(context)),
1146                                  GetCurrentProcess(), 
1147                                  &handle32,
1148                                  0, TRUE, DUPLICATE_SAME_ACCESS ))
1149                 handle16 = Win32HandleToDosFileHandle(handle32);
1150
1151             if (handle16 == HFILE_ERROR)
1152                 bSetDOSExtendedError = TRUE;
1153             else
1154                 SET_AX( context, handle16 );
1155         }
1156         break;
1157
1158     case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1159     case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1160         INT_Int21Handler( context );
1161         break;
1162
1163     case 0x48: /* ALLOCATE MEMORY */
1164         TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
1165         {
1166             WORD  selector = 0;
1167             DWORD bytes = (DWORD)BX_reg(context) << 4;
1168
1169             if (!ISV86(context) && DOSVM_IsWin16())
1170             {
1171                 DWORD rv = GlobalDOSAlloc16( bytes );
1172                 selector = LOWORD( rv );
1173             }
1174             else
1175                 DOSMEM_GetBlock( bytes, &selector );
1176                
1177             if (selector)
1178                 SET_AX( context, selector );
1179             else
1180             {
1181                 SET_CFLAG(context);
1182                 SET_AX( context, 0x0008 ); /* insufficient memory */
1183                 SET_BX( context, DOSMEM_Available() >> 4 );
1184             }
1185         }
1186         break;
1187
1188     case 0x49: /* FREE MEMORY */
1189         TRACE( "FREE MEMORY segment %04lX\n", context->SegEs );
1190         {
1191             BOOL ok;
1192             
1193             if (!ISV86(context) && DOSVM_IsWin16())
1194             {
1195                 ok = !GlobalDOSFree16( context->SegEs );
1196
1197                 /* If we don't reset ES_reg, we will fail in the relay code */
1198                 if (ok)
1199                     context->SegEs = 0;
1200             }
1201             else
1202                 ok = DOSMEM_FreeBlock( (void*)((DWORD)context->SegEs << 4) );
1203
1204             if (!ok)
1205             {
1206                 TRACE("FREE MEMORY failed\n");
1207                 SET_CFLAG(context);
1208                 SET_AX( context, 0x0009 ); /* memory block address invalid */
1209             }
1210         }
1211         break;
1212
1213     case 0x4a: /* RESIZE MEMORY BLOCK */
1214         INT_Int21Handler( context );
1215         break;
1216
1217     case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1218         {
1219             BYTE *program = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1220             BYTE *paramblk = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx);
1221
1222             TRACE( "EXEC %s\n", program );
1223
1224             if (DOSVM_IsWin16())
1225             {
1226                 HINSTANCE16 instance = WinExec16( program, SW_NORMAL );
1227                 if (instance < 32)
1228                 {
1229                     SET_CFLAG( context );
1230                     SET_AX( context, instance );
1231                 }
1232             }
1233             else
1234             {
1235                 if (!MZ_Exec( context, program, AL_reg(context), paramblk))
1236                     bSetDOSExtendedError = TRUE;
1237             }
1238         }
1239         break;
1240
1241     case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1242         TRACE( "EXIT with return code %d\n", AL_reg(context) );
1243         if (DOSVM_IsWin16())
1244             ExitThread( AL_reg(context) );
1245         else
1246             MZ_Exit( context, FALSE, AL_reg(context) );
1247         break;
1248
1249     case 0x4d: /* GET RETURN CODE */
1250         TRACE("GET RETURN CODE (ERRORLEVEL)\n");
1251         SET_AX( context, DOSVM_retval );
1252         DOSVM_retval = 0;
1253         break;
1254
1255     case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1256     case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1257         INT_Int21Handler( context );
1258         break;
1259
1260     case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
1261         TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
1262         DOSVM_psp = BX_reg(context);
1263         break;
1264
1265     case 0x51: /* GET PSP ADDRESS */
1266         INT21_GetPSP( context );
1267         break;
1268
1269     case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1270         if (!ISV86(context) && DOSVM_IsWin16())
1271         {
1272             SEGPTR ptr = DOSMEM_LOL()->wine_pm_lol;
1273             context->SegEs = SELECTOROF(ptr);
1274             SET_BX( context, OFFSETOF(ptr) );
1275         }
1276         else
1277         {
1278             SEGPTR ptr = DOSMEM_LOL()->wine_rm_lol;
1279             context->SegEs = SELECTOROF(ptr);
1280             SET_BX( context, OFFSETOF(ptr) );
1281         }
1282         break;
1283
1284     case 0x54: /* Get Verify Flag */
1285         TRACE("Get Verify Flag - Not Supported\n");
1286         SET_AL( context, 0x00 );  /* pretend we can tell. 00h = off 01h = on */
1287         break;
1288
1289     case 0x56: /* "RENAME" - RENAME FILE */
1290         INT_Int21Handler( context );
1291         break;
1292
1293     case 0x57: /* FILE DATE AND TIME */
1294         if (!INT21_FileDateTime( context ))
1295             bSetDOSExtendedError = TRUE;
1296         break;
1297
1298     case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
1299         switch (AL_reg(context))
1300         {
1301         case 0x00: /* GET MEMORY ALLOCATION STRATEGY */
1302             TRACE( "GET MEMORY ALLOCATION STRATEGY\n" );
1303             SET_AX( context, 0 ); /* low memory first fit */
1304             break;
1305
1306         case 0x01: /* SET ALLOCATION STRATEGY */
1307             TRACE( "SET MEMORY ALLOCATION STRATEGY to %d - ignored\n",
1308                    BL_reg(context) );
1309             break;
1310
1311         case 0x02: /* GET UMB LINK STATE */
1312             TRACE( "GET UMB LINK STATE\n" );
1313             SET_AL( context, 0 ); /* UMBs not part of DOS memory chain */
1314             break;
1315
1316         case 0x03: /* SET UMB LINK STATE */
1317             TRACE( "SET UMB LINK STATE to %d - ignored\n",
1318                    BX_reg(context) );
1319             break;
1320
1321         default:
1322             INT_BARF( context, 0x21 );
1323         }
1324         break;
1325
1326     case 0x59: /* GET EXTENDED ERROR INFO */
1327         INT21_GetExtendedError( context );
1328         break;
1329
1330     case 0x5a: /* CREATE TEMPORARY FILE */
1331     case 0x5b: /* CREATE NEW FILE */ 
1332         INT_Int21Handler( context );
1333         break;
1334
1335     case 0x5c: /* "FLOCK" - RECORD LOCKING */
1336         {
1337             DWORD  offset = MAKELONG(DX_reg(context), CX_reg(context));
1338             DWORD  length = MAKELONG(DI_reg(context), SI_reg(context));
1339             HANDLE handle = DosFileHandleToWin32Handle(BX_reg(context));
1340
1341             switch (AL_reg(context))
1342             {
1343             case 0x00: /* LOCK */
1344                 TRACE( "lock handle %d offset %ld length %ld\n",
1345                        BX_reg(context), offset, length );
1346                 if (!LockFile( handle, offset, 0, length, 0 ))
1347                     bSetDOSExtendedError = TRUE;
1348                 break;
1349
1350             case 0x01: /* UNLOCK */
1351                 TRACE( "unlock handle %d offset %ld length %ld\n",
1352                        BX_reg(context), offset, length );
1353                 if (!UnlockFile( handle, offset, 0, length, 0 ))
1354                     bSetDOSExtendedError = TRUE;
1355                 break;
1356
1357             default:
1358                 INT_BARF( context, 0x21 );
1359             }
1360         }
1361         break;
1362
1363     case 0x5d: /* NETWORK 5D */
1364         FIXME( "Network function 5D not implemented.\n" );
1365         SetLastError( ER_NoNetwork );
1366         bSetDOSExtendedError = TRUE;
1367         break;
1368
1369     case 0x5e: /* NETWORK 5E */
1370     case 0x5f: /* NETWORK 5F */
1371     case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1372         INT_Int21Handler( context );
1373         break;
1374
1375     case 0x61: /* UNUSED */
1376         SET_AL( context, 0 );
1377         break;
1378
1379     case 0x62: /* GET PSP ADDRESS */
1380         INT21_GetPSP( context );
1381         break;
1382
1383     case 0x63: /* MISC. LANGUAGE SUPPORT */
1384         switch (AL_reg(context)) {
1385         case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
1386             TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
1387             context->SegDs = INT21_GetHeapSelector( context );
1388             SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
1389             SET_AL( context, 0 ); /* success */
1390             break;
1391         }
1392         break;
1393
1394     case 0x64: /* OS/2 DOS BOX */
1395         INT_BARF( context, 0x21 );
1396         SET_CFLAG(context);
1397         break;
1398
1399     case 0x65: /* EXTENDED COUNTRY INFORMATION */
1400         INT21_ExtendedCountryInformation( context );
1401         break;
1402
1403     case 0x66: /* GLOBAL CODE PAGE TABLE */
1404         switch (AL_reg(context))
1405         {
1406         case 0x01:
1407             TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
1408             SET_BX( context, GetOEMCP() );
1409             SET_DX( context, GetOEMCP() );
1410             break;
1411         case 0x02:
1412             FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
1413                    BX_reg(context), DX_reg(context) );
1414             break;
1415         }
1416         break;
1417
1418     case 0x67: /* SET HANDLE COUNT */
1419         TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
1420         if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
1421             bSetDOSExtendedError = TRUE;
1422         break;
1423
1424     case 0x68: /* "FFLUSH" - COMMIT FILE */
1425         TRACE( "FFLUSH - handle %d\n", BX_reg(context) );
1426         if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
1427             bSetDOSExtendedError = TRUE;
1428         break;
1429
1430     case 0x69: /* DISK SERIAL NUMBER */
1431         INT_Int21Handler( context );
1432         break;
1433
1434     case 0x6a: /* COMMIT FILE */
1435         TRACE( "COMMIT FILE - handle %d\n", BX_reg(context) );
1436         if (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ))
1437             bSetDOSExtendedError = TRUE;
1438         break;
1439
1440     case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
1441         SET_AL( context, 0 );
1442         break;
1443
1444     case 0x6c: /* EXTENDED OPEN/CREATE */
1445         INT_Int21Handler( context );
1446         break;
1447
1448     case 0x70: /* MSDOS 7 - GET/SET INTERNATIONALIZATION INFORMATION */
1449         FIXME( "MS-DOS 7 - GET/SET INTERNATIONALIZATION INFORMATION\n" );
1450         SET_CFLAG( context );
1451         SET_AL( context, 0 );
1452         break;
1453
1454     case 0x71: /* MSDOS 7 - LONG FILENAME FUNCTIONS */
1455         INT_Int21Handler( context );
1456         break;
1457
1458     case 0x73: /* MSDOS7 - FAT32 */
1459         INT_Int21Handler( context );
1460         break;
1461
1462     case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
1463         TRACE( "CONNECTION SERVICES - GET CONNECTION NUMBER - ignored\n" );
1464         break;
1465
1466     case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
1467         TRACE( "NOVELL NETWARE - RETURN SHELL VERSION - ignored\n" );
1468         break;
1469
1470     default:
1471         INT_BARF( context, 0x21 );
1472         break;
1473
1474     } /* END OF SWITCH */
1475
1476     /* Set general error condition. */
1477     if (bSetDOSExtendedError)
1478     {
1479         SET_AX( context, GetLastError() );
1480         SET_CFLAG( context );
1481     }
1482
1483     /* Print error code if carry flag is set. */
1484     if (context->EFlags & 0x0001)
1485         TRACE("failed, error %ld\n", GetLastError() );
1486
1487     TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
1488            "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1489            AX_reg(context), BX_reg(context), 
1490            CX_reg(context), DX_reg(context), 
1491            SI_reg(context), DI_reg(context),
1492            (WORD)context->SegDs, (WORD)context->SegEs,
1493            context->EFlags );
1494 }