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