No longer directly accessing debuggee memory.
[wine] / msdos / int21.c
1 /*
2  * DOS interrupt 21h handler
3  */
4
5 #include "config.h"
6
7 #include <time.h>
8 #include <fcntl.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #ifdef HAVE_SYS_FILE_H
12 # include <sys/file.h>
13 #endif
14 #include <string.h>
15 #include <sys/stat.h>
16 #include <sys/time.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #include <utime.h>
20 #include <ctype.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24 #include "winuser.h" /* SW_NORMAL */
25 #include "wine/winbase16.h"
26 #include "winerror.h"
27 #include "drive.h"
28 #include "file.h"
29 #include "heap.h"
30 #include "msdos.h"
31 #include "ldt.h"
32 #include "task.h"
33 #include "options.h"
34 #include "miscemu.h"
35 #include "dosexe.h" /* for the MZ_SUPPORTED define */
36 #include "module.h"
37 #include "debugtools.h"
38 #include "console.h"
39
40 DEFAULT_DEBUG_CHANNEL(int21)
41 #if defined(__svr4__) || defined(_SCO_DS)
42 /* SVR4 DOESNT do locking the same way must implement properly */
43 #define LOCK_EX 0
44 #define LOCK_SH  1
45 #define LOCK_NB  8
46 #endif
47
48
49 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
50
51 /* Define the drive parameter block, as used by int21/1F
52  * and int21/32.  This table can be accessed through the
53  * global 'dpb' pointer, which points into the local dos
54  * heap.
55  */
56 struct DPB
57 {
58     BYTE drive_num;         /* 0=A, etc. */
59     BYTE unit_num;          /* Drive's unit number (?) */
60     WORD sector_size;       /* Sector size in bytes */
61     BYTE high_sector;       /* Highest sector in a cluster */
62     BYTE shift;             /* Shift count (?) */
63     WORD reserved;          /* Number of reserved sectors at start */
64     BYTE num_FAT;           /* Number of FATs */
65     WORD dir_entries;       /* Number of root dir entries */
66     WORD first_data;        /* First data sector */
67     WORD high_cluster;      /* Highest cluster number */
68     WORD sectors_in_FAT;    /* Number of sectors per FAT */
69     WORD start_dir;         /* Starting sector of first dir */
70     DWORD driver_head;      /* Address of device driver header (?) */
71     BYTE media_ID;          /* Media ID */
72     BYTE access_flag;       /* Prev. accessed flag (0=yes,0xFF=no) */
73     DWORD next;             /* Pointer to next DPB in list */
74     WORD free_search;       /* Free cluster search start */
75     WORD free_clusters;     /* Number of free clusters (0xFFFF=unknown) */
76 };
77
78 WORD CodePage = 437;
79 DWORD dpbsegptr;
80
81 struct DosHeap {
82         BYTE InDosFlag;
83         BYTE mediaID;
84         BYTE biosdate[8];
85         struct DPB dpb;
86         BYTE DummyDBCSLeadTable[6];
87 };
88 static struct DosHeap *heap;
89 static WORD DosHeapHandle;
90
91 extern char TempDirectory[];
92
93 static BOOL INT21_CreateHeap(void)
94 {
95     if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
96     {
97         WARN("Out of memory\n");
98         return FALSE;
99     }
100     heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
101     dpbsegptr = PTR_SEG_OFF_TO_SEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
102     heap->InDosFlag = 0;
103     strcpy(heap->biosdate, "01/01/80");
104     memset(heap->DummyDBCSLeadTable, 0, 6);
105     return TRUE;
106 }
107
108 static BYTE *GetCurrentDTA( CONTEXT86 *context )
109 {
110     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
111
112     /* FIXME: This assumes DTA was set correctly! */
113     return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta), 
114                                                 (DWORD)OFFSETOF(pTask->dta) );
115 }
116
117
118 void CreateBPB(int drive, BYTE *data, BOOL16 limited)
119 /* limited == TRUE is used with INT 0x21/0x440d */
120 {
121         if (drive > 1) {
122                 setword(data, 512);
123                 data[2] = 2;
124                 setword(&data[3], 0);
125                 data[5] = 2;
126                 setword(&data[6], 240);
127                 setword(&data[8], 64000);
128                 data[0x0a] = 0xf8;
129                 setword(&data[0x0b], 40);
130                 setword(&data[0x0d], 56);
131                 setword(&data[0x0f], 2);
132                 setword(&data[0x11], 0);
133                 if (!limited) {
134                     setword(&data[0x1f], 800);
135                     data[0x21] = 5;
136                     setword(&data[0x22], 1);
137                 }
138         } else { /* 1.44mb */
139                 setword(data, 512);
140                 data[2] = 2;
141                 setword(&data[3], 0);
142                 data[5] = 2;
143                 setword(&data[6], 240);
144                 setword(&data[8], 2880);
145                 data[0x0a] = 0xf8;
146                 setword(&data[0x0b], 6);
147                 setword(&data[0x0d], 18);
148                 setword(&data[0x0f], 2);
149                 setword(&data[0x11], 0);
150                 if (!limited) {
151                     setword(&data[0x1f], 80);
152                     data[0x21] = 7;
153                     setword(&data[0x22], 2);
154                 }
155         }       
156 }
157
158 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
159 {
160     DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
161     char root[] = "A:\\";
162
163     *root += DOS_GET_DRIVE( DL_reg(context) );
164     if (!GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
165                               &free_clusters, &total_clusters )) return 0;
166     AX_reg(context) = cluster_sectors;
167     BX_reg(context) = free_clusters;
168     CX_reg(context) = sector_bytes;
169     DX_reg(context) = total_clusters;
170     return 1;
171 }
172
173 static int INT21_GetDriveAllocInfo( CONTEXT86 *context )
174 {
175     if (!INT21_GetFreeDiskSpace( context )) return 0;
176     if (!heap && !INT21_CreateHeap()) return 0;
177     heap->mediaID = 0xf0;
178     DS_reg(context) = DosHeapHandle;
179     BX_reg(context) = (int)&heap->mediaID - (int)heap;
180     return 1;
181 }
182
183 static void GetDrivePB( CONTEXT86 *context, int drive )
184 {
185         if(!DRIVE_IsValid(drive))
186         {
187             SetLastError( ERROR_INVALID_DRIVE );
188             AX_reg(context) = 0x00ff;
189         }
190         else if (heap || INT21_CreateHeap())
191         {
192                 FIXME("GetDrivePB not fully implemented.\n");
193
194                 /* FIXME: I have no idea what a lot of this information should
195                  * say or whether it even really matters since we're not allowing
196                  * direct block access.  However, some programs seem to depend on
197                  * getting at least _something_ back from here.  The 'next' pointer
198                  * does worry me, though.  Should we have a complete table of
199                  * separate DPBs per drive?  Probably, but I'm lazy. :-)  -CH
200                  */
201                 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
202                 heap->dpb.sector_size = 512;
203                 heap->dpb.high_sector = 1;
204                 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
205                 heap->dpb.reserved = 0;
206                 heap->dpb.num_FAT = 1;
207                 heap->dpb.dir_entries = 2;
208                 heap->dpb.first_data = 2;
209                 heap->dpb.high_cluster = 64000;
210                 heap->dpb.sectors_in_FAT = 1;
211                 heap->dpb.start_dir = 1;
212                 heap->dpb.driver_head = 0;
213                 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
214                 heap->dpb.access_flag = 0;
215                 heap->dpb.next = 0;
216                 heap->dpb.free_search = 0;
217                 heap->dpb.free_clusters = 0xFFFF;    /* unknown */
218
219                 AL_reg(context) = 0x00;
220                 DS_reg(context) = SELECTOROF(dpbsegptr);
221                 BX_reg(context) = OFFSETOF(dpbsegptr);
222         }
223 }
224
225
226 static void ioctlGetDeviceInfo( CONTEXT86 *context )
227 {
228     int curr_drive;
229     const DOS_DEVICE *dev;
230
231     TRACE("(%d)\n", BX_reg(context));
232     
233     RESET_CFLAG(context);
234
235     /* DOS device ? */
236     if ((dev = DOSFS_GetDeviceByHandle( FILE_GetHandle(BX_reg(context)) )))
237     {
238         DX_reg(context) = dev->flags;
239         return;
240     }
241
242     /* it seems to be a file */
243     curr_drive = DRIVE_GetCurrentDrive();
244     DX_reg(context) = 0x0140 + curr_drive + ((curr_drive > 1) ? 0x0800 : 0); 
245     /* no floppy */
246     /* bits 0-5 are current drive
247      * bit 6 - file has NOT been written..FIXME: correct?
248      * bit 8 - generate int24 if no diskspace on write/ read past end of file
249      * bit 11 - media not removable
250      * bit 14 - don't set file date/time on closing
251      * bit 15 - file is remote
252      */
253 }
254
255 static BOOL ioctlGenericBlkDevReq( CONTEXT86 *context )
256 {
257         BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
258         int drive = DOS_GET_DRIVE( BL_reg(context) );
259
260         if (!DRIVE_IsValid(drive))
261         {
262             SetLastError( ERROR_FILE_NOT_FOUND );
263             return TRUE;
264         }
265
266         if (CH_reg(context) != 0x08)
267         {
268             INT_BARF( context, 0x21 );
269             return FALSE;
270         }
271
272         switch (CL_reg(context)) 
273         {
274                 case 0x4a: /* lock logical volume */
275                         TRACE("lock logical volume (%d) level %d mode %d\n",drive,BH_reg(context),DX_reg(context));
276                         break;
277
278                 case 0x60: /* get device parameters */
279                            /* used by w4wgrp's winfile */
280                         memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
281                         dataptr[0] = 0x04;
282                         dataptr[6] = 0; /* media type */
283                         if (drive > 1) 
284                         {
285                                 dataptr[1] = 0x05; /* fixed disk */
286                                 setword(&dataptr[2], 0x01); /* non removable */
287                                 setword(&dataptr[4], 0x300); /* # of cylinders */
288                         }
289                         else
290                         {
291                                 dataptr[1] = 0x07; /* block dev, floppy */
292                                 setword(&dataptr[2], 0x02); /* removable */
293                                 setword(&dataptr[4], 80); /* # of cylinders */
294                         }
295                         CreateBPB(drive, &dataptr[7], TRUE);
296                         RESET_CFLAG(context);
297                         break;
298
299                 case 0x41: /* write logical device track */
300                 case 0x61: /* read logical device track */
301                         {
302                                 BYTE drive = BL_reg(context) ?
303                                                 BL_reg(context) : DRIVE_GetCurrentDrive(); 
304                                 WORD head   = *(WORD *)dataptr+1;
305                                 WORD cyl    = *(WORD *)dataptr+3;
306                                 WORD sect   = *(WORD *)dataptr+5;
307                                 WORD nrsect = *(WORD *)dataptr+7;
308                                 BYTE *data  =  (BYTE *)dataptr+9;
309                                 int (*raw_func)(BYTE, DWORD, DWORD, BYTE *, BOOL);
310
311                                 raw_func = (CL_reg(context) == 0x41) ?
312                                                                 DRIVE_RawWrite : DRIVE_RawRead;
313
314                                 if (raw_func(drive, head*cyl*sect, nrsect, data, FALSE))
315                                         RESET_CFLAG(context);
316                                 else
317                                 {
318                                         AX_reg(context) = 0x1e; /* read fault */
319                                         SET_CFLAG(context);
320                                 }
321                         }
322                         break;
323                 case 0x66:/*  get disk serial number */
324                         {       
325                                 char    label[12],fsname[9],path[4];
326                                 DWORD   serial;
327
328                                 strcpy(path,"x:\\");path[0]=drive+'A';
329                                 GetVolumeInformationA(
330                                         path,label,12,&serial,NULL,NULL,fsname,9
331                                 );
332                                 *(WORD*)dataptr         = 0;
333                                 memcpy(dataptr+2,&serial,4);
334                                 memcpy(dataptr+6,label  ,11);
335                                 memcpy(dataptr+17,fsname,8);
336                         }
337                         break;
338
339                 case 0x6a:
340                         TRACE("logical volume %d unlocked.\n",drive);
341                         break;
342
343                 case 0x6f:
344                         memset(dataptr+1, '\0', dataptr[0]-1);
345                         dataptr[1] = dataptr[0]; 
346                         dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
347                         dataptr[3] = 0xFF; /* no physical drive */
348                         break;
349
350                 case 0x72:
351                         /* Trail on error implementation */
352                         AX_reg(context) = GetDriveType16(BL_reg(context)) == DRIVE_CANNOTDETERMINE ? 0x0f : 0x01;
353                         SET_CFLAG(context);     /* Seems to be set all the time */
354                         break;
355
356                 default:
357                         INT_BARF( context, 0x21 );
358         }
359         return FALSE;
360 }
361
362 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
363 {
364     char *filename =
365         CTX_SEG_OFF_TO_LIN(context, DS_reg(context), ESI_reg(context) );
366     char *fcb =
367         CTX_SEG_OFF_TO_LIN(context, ES_reg(context), EDI_reg(context) );
368     char *buffer, *s, *d;
369
370     AL_reg(context) = 0xff; /* failed */
371
372     TRACE("filename: '%s'\n", filename);
373
374     buffer = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + 1);
375
376     s = filename;
377     d = buffer;
378     while (*s)
379     {
380         if ((*s != ' ') && (*s != '\r') && (*s != '\n'))
381             *d++ = *s++;
382         else
383             break;
384     }
385
386     *d = '\0';
387     DOSFS_ToDosFCBFormat(buffer, fcb + 1);
388     *fcb = 0;
389     TRACE("FCB: '%s'\n", ((CHAR *)fcb + 1));
390
391     HeapFree( GetProcessHeap(), 0, buffer);
392
393     AL_reg(context) =
394         ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0;
395
396     /* point DS:SI to first unparsed character */
397     SI_reg(context) += (int)s - (int)filename;
398 }
399
400 static void INT21_GetSystemDate( CONTEXT86 *context )
401 {
402     SYSTEMTIME systime;
403     GetLocalTime( &systime );
404     CX_reg(context) = systime.wYear;
405     DX_reg(context) = (systime.wMonth << 8) | systime.wDay;
406     AX_reg(context) = systime.wDayOfWeek;
407 }
408
409 static void INT21_GetSystemTime( CONTEXT86 *context )
410 {
411     SYSTEMTIME systime;
412     GetLocalTime( &systime );
413     CX_reg(context) = (systime.wHour << 8) | systime.wMinute;
414     DX_reg(context) = (systime.wSecond << 8) | (systime.wMilliseconds / 10);
415 }
416
417 /* Many calls translate a drive argument like this:
418    drive number (00h = default, 01h = A:, etc)
419    */  
420 static char drivestring[]="default";
421
422 char *INT21_DriveName(int drive)
423 {
424
425     if(drive >0)
426       {
427         drivestring[0]= (unsigned char)drive + '@';
428         drivestring[1]=':';
429         drivestring[2]=0;
430       }
431     return drivestring;
432 }
433 static BOOL INT21_CreateFile( CONTEXT86 *context )
434 {
435     AX_reg(context) = _lcreat16( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
436                                           EDX_reg(context) ), CX_reg(context) );
437     return (AX_reg(context) == (WORD)HFILE_ERROR16);
438 }
439
440 static HFILE16 _lcreat16_uniq( LPCSTR path, INT attr )
441 {
442     /* Mask off all flags not explicitly allowed by the doc */
443     attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
444     return FILE_AllocDosHandle( CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
445                                              FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
446                                              CREATE_NEW, attr, -1 ));
447 }
448
449 static void OpenExistingFile( CONTEXT86 *context )
450 {
451     AX_reg(context) = _lopen16( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
452                                AL_reg(context) );
453     if (AX_reg(context) == (WORD)HFILE_ERROR16)
454     {
455         AX_reg(context) = GetLastError();
456         SET_CFLAG(context);
457     }
458 }
459
460 static BOOL INT21_ExtendedOpenCreateFile(CONTEXT86 *context )
461 {
462   BOOL bExtendedError = FALSE;
463   BYTE action = DL_reg(context);
464
465   /* Shuffle arguments to call OpenExistingFile */
466   AL_reg(context) = BL_reg(context);
467   DX_reg(context) = SI_reg(context);
468   /* BX,CX and DX should be preserved */
469   OpenExistingFile(context);
470
471   if ((EFL_reg(context) & 0x0001) == 0) /* File exists */
472   {
473       UINT16    uReturnCX = 0;
474
475       /* Now decide what do do */
476
477       if ((action & 0x07) == 0)
478       {
479           _lclose16( AX_reg(context) );
480           AX_reg(context) = 0x0050;     /*File exists*/
481           SET_CFLAG(context);
482           WARN("extended open/create: failed because file exists \n");
483       }
484       else if ((action & 0x07) == 2) 
485       {
486         /* Truncate it, but first check if opened for write */
487         if ((BL_reg(context) & 0x0007)== 0) 
488         {
489             _lclose16( AX_reg(context) );
490             WARN("extended open/create: failed, trunc on ro file\n");
491             AX_reg(context) = 0x000C;   /*Access code invalid*/
492             SET_CFLAG(context);
493         }
494         else
495         {
496                 TRACE("extended open/create: Closing before truncate\n");
497                 if (_lclose16( AX_reg(context) ))
498                 {
499                    WARN("extended open/create: close before trunc failed\n");
500                    AX_reg(context) = 0x0019;    /*Seek Error*/
501                    CX_reg(context) = 0;
502                    SET_CFLAG(context);
503                 }
504                 /* Shuffle arguments to call CreateFile */
505
506                 TRACE("extended open/create: Truncating\n");
507                 AL_reg(context) = BL_reg(context);
508                 /* CX is still the same */
509                 DX_reg(context) = SI_reg(context);
510                 bExtendedError = INT21_CreateFile(context);
511
512                 if (EFL_reg(context) & 0x0001)  /*no file open, flags set */
513                 {
514                     WARN("extended open/create: trunc failed\n");
515                     return bExtendedError;
516                 }
517                 uReturnCX = 0x3;
518         }
519       } 
520       else uReturnCX = 0x1;
521
522       CX_reg(context) = uReturnCX;
523   }
524   else /* file does not exist */
525   {
526       RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
527       if ((action & 0xF0)== 0)
528       {
529         CX_reg(context) = 0;
530         SET_CFLAG(context);
531         WARN("extended open/create: failed, file dosen't exist\n");
532       }
533       else
534       {
535         /* Shuffle arguments to call CreateFile */
536         TRACE("extended open/create: Creating\n");
537         AL_reg(context) = BL_reg(context);
538         /* CX should still be the same */
539         DX_reg(context) = SI_reg(context);
540         bExtendedError = INT21_CreateFile(context);
541         if (EFL_reg(context) & 0x0001)  /*no file open, flags set */
542         {
543             WARN("extended open/create: create failed\n");
544             return bExtendedError;
545         }
546         CX_reg(context) = 2;
547       }
548   }
549
550   return bExtendedError;
551 }
552
553
554 static BOOL INT21_ChangeDir( CONTEXT86 *context )
555 {
556     int drive;
557     char *dirname = CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context));
558
559     TRACE("changedir %s\n", dirname);
560     if (dirname[0] && (dirname[1] == ':'))
561     {
562         drive = toupper(dirname[0]) - 'A';
563         dirname += 2;
564     }
565     else drive = DRIVE_GetCurrentDrive();
566     return DRIVE_Chdir( drive, dirname );
567 }
568
569
570 static int INT21_FindFirst( CONTEXT86 *context )
571 {
572     char *p;
573     const char *path;
574     DOS_FULL_NAME full_name;
575     FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
576
577     path = (const char *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
578     dta->unixPath = NULL;
579     if (!DOSFS_GetFullName( path, FALSE, &full_name ))
580     {
581         AX_reg(context) = GetLastError();
582         SET_CFLAG(context);
583         return 0;
584     }
585     dta->unixPath = HEAP_strdupA( GetProcessHeap(), 0, full_name.long_name );
586     p = strrchr( dta->unixPath, '/' );
587     *p = '\0';
588
589     /* Note: terminating NULL in dta->mask overwrites dta->search_attr
590      *       (doesn't matter as it is set below anyway)
591      */
592     if (!DOSFS_ToDosFCBFormat( p + 1, dta->mask ))
593     {
594         HeapFree( GetProcessHeap(), 0, dta->unixPath );
595         dta->unixPath = NULL;
596         SetLastError( ERROR_FILE_NOT_FOUND );
597         AX_reg(context) = ERROR_FILE_NOT_FOUND;
598         SET_CFLAG(context);
599         return 0;
600     }
601     dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
602                                                : DRIVE_GetCurrentDrive();
603     dta->count = 0;
604     dta->search_attr = CL_reg(context);
605     return 1;
606 }
607
608
609 static int INT21_FindNext( CONTEXT86 *context )
610 {
611     FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
612     WIN32_FIND_DATAA entry;
613     int count;
614
615     if (!dta->unixPath) return 0;
616     if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
617                                   dta->search_attr, dta->count, &entry )))
618     {
619         HeapFree( GetProcessHeap(), 0, dta->unixPath );
620         dta->unixPath = NULL;
621         return 0;
622     }
623     if ((int)dta->count + count > 0xffff)
624     {
625         WARN("Too many directory entries in %s\n", dta->unixPath );
626         HeapFree( GetProcessHeap(), 0, dta->unixPath );
627         dta->unixPath = NULL;
628         return 0;
629     }
630     dta->count += count;
631     dta->fileattr = entry.dwFileAttributes;
632     dta->filesize = entry.nFileSizeLow;
633     FileTimeToDosDateTime( &entry.ftLastWriteTime,
634                            &dta->filedate, &dta->filetime );
635     strcpy( dta->filename, entry.cAlternateFileName );
636     if (!memchr(dta->mask,'?',11)) {
637         /* wildcardless search, release resources in case no findnext will
638          * be issued, and as a workaround in case file creation messes up
639          * findnext, as sometimes happens with pkunzip */
640         HeapFree( GetProcessHeap(), 0, dta->unixPath );
641         dta->unixPath = NULL;
642     }
643     return 1;
644 }
645
646
647 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
648 {
649     static int counter = 0;
650     char *name = CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context) );
651     char *p = name + strlen(name);
652
653     /* despite what Ralf Brown says, some programs seem to call without 
654      * ending backslash (DOS accepts that, so we accept it too) */
655     if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
656
657     for (;;)
658     {
659         sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
660         counter = (counter + 1) % 1000;
661
662         if ((AX_reg(context) = _lcreat16_uniq( name, 0 )) != (WORD)HFILE_ERROR16)
663         {
664             TRACE("created %s\n", name );
665             return TRUE;
666         }
667         if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
668     }
669 }
670
671
672 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context ) 
673 {
674     int drive = DOS_GET_DRIVE( DL_reg(context) );
675     char *ptr = (char *)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), ESI_reg(context) );
676
677     if (!DRIVE_IsValid(drive))
678     {
679         SetLastError( ERROR_INVALID_DRIVE );
680         return FALSE;
681     }
682     lstrcpynA( ptr, DRIVE_GetDosCwd(drive), 64 );
683     AX_reg(context) = 0x0100;                        /* success return code */
684     return TRUE;
685 }
686
687
688 static void INT21_GetDBCSLeadTable( CONTEXT86 *context )
689 {
690     if (heap || INT21_CreateHeap())
691     { /* return an empty table just as DOS 4.0+ does */
692         DS_reg(context) = DosHeapHandle;
693         SI_reg(context) = (int)&heap->DummyDBCSLeadTable - (int)heap;
694     }
695     else
696     {
697         AX_reg(context) = 0x1; /* error */
698         SET_CFLAG(context);
699     }
700 }
701
702
703 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
704 {
705     BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
706     int drive = DOS_GET_DRIVE( BL_reg(context) );
707         
708     if (!DRIVE_IsValid(drive))
709     {
710         SetLastError( ERROR_INVALID_DRIVE );
711         return 0;
712     }
713     
714     *(WORD *)dataptr = 0;
715     *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
716     memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
717     strncpy(dataptr + 0x11, "FAT16   ", 8);
718     return 1;
719 }
720
721
722 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
723 {
724     BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
725     int drive = DOS_GET_DRIVE( BL_reg(context) );
726
727     if (!DRIVE_IsValid(drive))
728     {
729         SetLastError( ERROR_INVALID_DRIVE );
730         return 0;
731     }
732
733     DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
734     return 1;
735 }
736
737
738 /* microsoft's programmers should be shot for using CP/M style int21
739    calls in Windows for Workgroup's winfile.exe */
740
741 static int INT21_FindFirstFCB( CONTEXT86 *context )
742 {
743     BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
744     FINDFILE_FCB *pFCB;
745     LPCSTR root, cwd;
746     int drive;
747
748     if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
749     else pFCB = (FINDFILE_FCB *)fcb;
750     drive = DOS_GET_DRIVE( pFCB->drive );
751     if (!DRIVE_IsValid( drive )) return 0;
752     root = DRIVE_GetRoot( drive );
753     cwd  = DRIVE_GetUnixCwd( drive );
754     pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
755                                 strlen(root)+strlen(cwd)+2 );
756     if (!pFCB->unixPath) return 0;
757     strcpy( pFCB->unixPath, root );
758     strcat( pFCB->unixPath, "/" );
759     strcat( pFCB->unixPath, cwd );
760     pFCB->count = 0;
761     return 1;
762 }
763
764
765 static int INT21_FindNextFCB( CONTEXT86 *context )
766 {
767     BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
768     FINDFILE_FCB *pFCB;
769     DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA(context);
770     WIN32_FIND_DATAA entry;
771     BYTE attr;
772     int count;
773
774     if (*fcb == 0xff) /* extended FCB ? */
775     {
776         attr = fcb[6];
777         pFCB = (FINDFILE_FCB *)(fcb + 7);
778     }
779     else
780     {
781         attr = 0;
782         pFCB = (FINDFILE_FCB *)fcb;
783     }
784
785     if (!pFCB->unixPath) return 0;
786     if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
787                                   DOS_GET_DRIVE( pFCB->drive ), attr,
788                                   pFCB->count, &entry )))
789     {
790         HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
791         pFCB->unixPath = NULL;
792         return 0;
793     }
794     pFCB->count += count;
795
796     if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
797         *(BYTE *)pResult = 0xff;
798         (BYTE *)pResult +=6; /* leave reserved field behind */
799         *(BYTE *)pResult = entry.dwFileAttributes;
800         ((BYTE *)pResult)++;
801     }
802     *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
803     ((BYTE *)pResult)++;
804     pResult->fileattr = entry.dwFileAttributes;
805     pResult->cluster  = 0;  /* what else? */
806     pResult->filesize = entry.nFileSizeLow;
807     memset( pResult->reserved, 0, sizeof(pResult->reserved) );
808     FileTimeToDosDateTime( &entry.ftLastWriteTime,
809                            &pResult->filedate, &pResult->filetime );
810
811     /* Convert file name to FCB format */
812
813     memset( pResult->filename, ' ', sizeof(pResult->filename) );
814     if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
815     else if (!strcmp( entry.cAlternateFileName, ".." ))
816         pResult->filename[0] = pResult->filename[1] = '.';
817     else
818     {
819         char *p = strrchr( entry.cAlternateFileName, '.' );
820         if (p && p[1] && (p != entry.cAlternateFileName))
821         {
822             memcpy( pResult->filename, entry.cAlternateFileName,
823                     MIN( (p - entry.cAlternateFileName), 8 ) );
824             memcpy( pResult->filename + 8, p + 1, MIN( strlen(p), 3 ) );
825         }
826         else
827             memcpy( pResult->filename, entry.cAlternateFileName,
828                     MIN( strlen(entry.cAlternateFileName), 8 ) );
829     }
830     return 1;
831 }
832
833
834 static void DeleteFileFCB( CONTEXT86 *context )
835 {
836     FIXME("(%p): stub\n", context);
837 }
838
839 static void RenameFileFCB( CONTEXT86 *context )
840 {
841     FIXME("(%p): stub\n", context);
842 }
843
844
845
846 static void fLock( CONTEXT86 * context )
847 {
848
849     switch ( AX_reg(context) & 0xff )
850     {
851         case 0x00: /* LOCK */
852           TRACE("lock handle %d offset %ld length %ld\n",
853                 BX_reg(context),
854                 MAKELONG(DX_reg(context),CX_reg(context)),
855                 MAKELONG(DI_reg(context),SI_reg(context))) ;
856           if (!LockFile(FILE_GetHandle(BX_reg(context)),
857                         MAKELONG(DX_reg(context),CX_reg(context)), 0,
858                         MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
859             AX_reg(context) = GetLastError();
860             SET_CFLAG(context);
861           }
862           break;
863
864         case 0x01: /* UNLOCK */
865           TRACE("unlock handle %d offset %ld length %ld\n",
866                 BX_reg(context),
867                 MAKELONG(DX_reg(context),CX_reg(context)),
868                 MAKELONG(DI_reg(context),SI_reg(context))) ;
869           if (!UnlockFile(FILE_GetHandle(BX_reg(context)),
870                           MAKELONG(DX_reg(context),CX_reg(context)), 0,
871                           MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
872             AX_reg(context) = GetLastError();
873             SET_CFLAG(context);
874           }
875           return;
876         default:
877           AX_reg(context) = 0x0001;
878           SET_CFLAG(context);
879           return;
880      }
881
882
883 static BOOL
884 INT21_networkfunc (CONTEXT86 *context)
885 {
886      switch (AL_reg(context)) {
887      case 0x00: /* Get machine name. */
888      {
889           char *dst = CTX_SEG_OFF_TO_LIN (context,DS_reg(context),EDX_reg(context));
890           TRACE("getting machine name to %p\n", dst);
891           if (gethostname (dst, 15))
892           {
893                WARN("failed!\n");
894                SetLastError( ER_NoNetwork );
895                return TRUE;
896           } else {
897                int len = strlen (dst);
898                while (len < 15)
899                     dst[len++] = ' ';
900                dst[15] = 0;
901                CH_reg(context) = 1; /* Valid */
902                CL_reg(context) = 1; /* NETbios number??? */
903                TRACE("returning %s\n", debugstr_an (dst, 16));
904                return FALSE;
905           }
906      }
907
908      default:
909           SetLastError( ER_NoNetwork );
910           return TRUE;
911      }
912 }
913
914 static void INT21_SetCurrentPSP(WORD psp)
915 {
916 #ifdef MZ_SUPPORTED
917     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
918     NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
919     
920     GlobalUnlock16( GetCurrentTask() );
921     if (pModule->lpDosTask)
922         pModule->lpDosTask->psp_seg = psp;
923     else
924 #endif
925         ERR("Cannot change PSP for non-DOS task!\n");
926 }
927     
928 static WORD INT21_GetCurrentPSP()
929 {
930 #ifdef MZ_SUPPORTED
931     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
932     NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
933         
934     GlobalUnlock16( GetCurrentTask() );
935     if (pModule->lpDosTask)
936         return pModule->lpDosTask->psp_seg;
937     else
938 #endif
939         return GetCurrentPDB16();
940 }
941
942
943 /***********************************************************************
944  *           INT21_GetExtendedError
945  */
946 static void INT21_GetExtendedError( CONTEXT86 *context )
947 {
948     BYTE class, action, locus;
949     WORD error = GetLastError();
950
951     switch(error)
952     {
953     case ERROR_SUCCESS:
954         class = action = locus = 0;
955         break;
956     case ERROR_DIR_NOT_EMPTY:
957         class  = EC_Exists;
958         action = SA_Ignore;
959         locus  = EL_Disk;
960         break;
961     case ERROR_ACCESS_DENIED:
962         class  = EC_AccessDenied;
963         action = SA_Abort;
964         locus  = EL_Disk;
965         break;
966     case ERROR_CANNOT_MAKE:
967         class  = EC_AccessDenied;
968         action = SA_Abort;
969         locus  = EL_Unknown;
970         break;
971     case ERROR_DISK_FULL:
972     case ERROR_HANDLE_DISK_FULL:
973         class  = EC_MediaError;
974         action = SA_Abort;
975         locus  = EL_Disk;
976         break;
977     case ERROR_FILE_EXISTS:
978     case ERROR_ALREADY_EXISTS:
979         class  = EC_Exists;
980         action = SA_Abort;
981         locus  = EL_Disk;
982         break;
983     case ERROR_FILE_NOT_FOUND:
984         class  = EC_NotFound;
985         action = SA_Abort;
986         locus  = EL_Disk;
987         break;
988     case ER_GeneralFailure:
989         class  = EC_SystemFailure;
990         action = SA_Abort;
991         locus  = EL_Unknown;
992         break;
993     case ERROR_INVALID_DRIVE:
994         class  = EC_MediaError;
995         action = SA_Abort;
996         locus  = EL_Disk;
997         break;
998     case ERROR_INVALID_HANDLE:
999         class  = EC_ProgramError;
1000         action = SA_Abort;
1001         locus  = EL_Disk;
1002         break;
1003     case ERROR_LOCK_VIOLATION:
1004         class  = EC_AccessDenied;
1005         action = SA_Abort;
1006         locus  = EL_Disk;
1007         break;
1008     case ERROR_NO_MORE_FILES:
1009         class  = EC_MediaError;
1010         action = SA_Abort;
1011         locus  = EL_Disk;
1012         break;
1013     case ER_NoNetwork:
1014         class  = EC_NotFound;
1015         action = SA_Abort;
1016         locus  = EL_Network;
1017         break;
1018     case ERROR_NOT_ENOUGH_MEMORY:
1019         class  = EC_OutOfResource;
1020         action = SA_Abort;
1021         locus  = EL_Memory;
1022         break;
1023     case ERROR_PATH_NOT_FOUND:
1024         class  = EC_NotFound;
1025         action = SA_Abort;
1026         locus  = EL_Disk;
1027         break;
1028     case ERROR_SEEK:
1029         class  = EC_NotFound;
1030         action = SA_Ignore;
1031         locus  = EL_Disk;
1032         break;
1033     case ERROR_SHARING_VIOLATION:
1034         class  = EC_Temporary;
1035         action = SA_Retry;
1036         locus  = EL_Disk;
1037         break;
1038     case ERROR_TOO_MANY_OPEN_FILES:
1039         class  = EC_ProgramError;
1040         action = SA_Abort;
1041         locus  = EL_Disk;
1042         break;
1043     default:
1044         FIXME("Unknown error %d\n", error );
1045         class  = EC_SystemFailure;
1046         action = SA_Abort;
1047         locus  = EL_Unknown;
1048         break;
1049     }
1050     TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
1051            error, class, action, locus );
1052     AX_reg(context) = error;
1053     BH_reg(context) = class;
1054     BL_reg(context) = action;
1055     CH_reg(context) = locus;
1056 }
1057
1058 /***********************************************************************
1059  *           DOS3Call  (KERNEL.102)
1060  */
1061 void WINAPI DOS3Call( CONTEXT86 *context )
1062 {
1063     BOOL        bSetDOSExtendedError = FALSE;
1064
1065
1066     TRACE("AX=%04x BX=%04x CX=%04x DX=%04x "
1067           "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1068           AX_reg(context), BX_reg(context), CX_reg(context), DX_reg(context),
1069           SI_reg(context), DI_reg(context),
1070           (WORD)DS_reg(context), (WORD)ES_reg(context),
1071           EFL_reg(context) );
1072
1073
1074     if (AH_reg(context) == 0x59)  /* Get extended error info */
1075     {
1076         INT21_GetExtendedError( context );
1077         return;
1078     }
1079
1080     if (AH_reg(context) == 0x0C)  /* Flush buffer and read standard input */
1081     {
1082         TRACE("FLUSH BUFFER AND READ STANDARD INPUT\n");
1083         /* no flush here yet */
1084         AH_reg(context) = AL_reg(context);
1085     }
1086
1087     if (AH_reg(context)>=0x2f) {
1088         /* extended error is used by (at least) functions 0x2f to 0x62 */
1089         SetLastError(0);
1090     }
1091     RESET_CFLAG(context);  /* Not sure if this is a good idea */
1092
1093     switch(AH_reg(context)) 
1094     {
1095     case 0x03: /* READ CHARACTER FROM STDAUX  */
1096     case 0x04: /* WRITE CHARACTER TO STDAUX */
1097     case 0x05: /* WRITE CHARACTER TO PRINTER */
1098     case 0x0f: /* OPEN FILE USING FCB */
1099     case 0x10: /* CLOSE FILE USING FCB */
1100     case 0x14: /* SEQUENTIAL READ FROM FCB FILE */              
1101     case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
1102     case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
1103     case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
1104     case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
1105     case 0x23: /* GET FILE SIZE FOR FCB */
1106     case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
1107     case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
1108     case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
1109     case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
1110         INT_BARF( context, 0x21 );
1111         break;
1112
1113     case 0x00: /* TERMINATE PROGRAM */
1114         TRACE("TERMINATE PROGRAM\n");
1115         ExitProcess( 0 );
1116         break;
1117
1118     case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
1119         TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
1120         AL_reg(context) = CONSOLE_GetCharacter();
1121         /* FIXME: no echo */
1122         break;
1123
1124     case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
1125         TRACE("Write Character to Standard Output\n");
1126         CONSOLE_Write(DL_reg(context), 0, 0, 0);
1127         break;
1128
1129     case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
1130         if (DL_reg(context) == 0xff) {
1131             static char scan = 0;
1132             TRACE("Direct Console Input\n");
1133             if (scan) {
1134                 /* return pending scancode */
1135                 AL_reg(context) = scan;
1136                 EFL_reg(context) &= ~0x40; /* clear ZF */
1137                 scan = 0;
1138             } else {
1139                 char ascii;
1140                 if (CONSOLE_CheckForKeystroke(&scan,&ascii)) {
1141                     CONSOLE_GetKeystroke(&scan,&ascii);
1142                     /* return ASCII code */
1143                     AL_reg(context) = ascii;
1144                     EFL_reg(context) &= ~0x40; /* clear ZF */
1145                     /* return scan code on next call only if ascii==0 */
1146                     if (ascii) scan = 0;
1147                 } else {
1148                     /* nothing pending, clear everything */
1149                     AL_reg(context) = 0;
1150                     EFL_reg(context) |= 0x40; /* set ZF */
1151                     scan = 0; /* just in case */
1152                 }
1153             }
1154         } else {
1155             TRACE("Direct Console Output\n");
1156             CONSOLE_Write(DL_reg(context), 0, 0, 0);
1157         }
1158         break;
1159
1160     case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
1161         TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
1162         AL_reg(context) = CONSOLE_GetCharacter();
1163         break;
1164
1165     case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
1166         TRACE("CHARACTER INPUT WITHOUT ECHO\n");
1167         AL_reg(context) = CONSOLE_GetCharacter();
1168         break;
1169
1170     case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
1171         TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
1172               DS_reg(context),DX_reg(context) );
1173         {
1174             LPSTR data = CTX_SEG_OFF_TO_LIN(context,DS_reg(context),EDX_reg(context));
1175             LPSTR p = data;
1176             /* do NOT use strchr() to calculate the string length,
1177             as '\0' is valid string content, too !
1178             Maybe we should check for non-'$' strings, but DOS doesn't. */
1179             while (*p != '$') p++;
1180             _hwrite16( 1, data, (int)p - (int)data);
1181             AL_reg(context) = '$'; /* yes, '$' (0x24) gets returned in AL */
1182         }
1183         break;
1184
1185     case 0x0a: /* BUFFERED INPUT */
1186       {
1187         char *buffer = ((char *)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), 
1188                                                    EDX_reg(context) ));
1189         int res;
1190         
1191         TRACE("BUFFERED INPUT (size=%d)\n",buffer[0]);
1192         if (buffer[1])
1193           TRACE("Handle old chars in buffer!\n");
1194         res=_lread16( 0, buffer+2,buffer[0]);
1195         buffer[1]=res;
1196         if(buffer[res+1] == '\n')
1197           buffer[res+1] = '\r';
1198         break;
1199       }
1200
1201     case 0x0b: {/* GET STDIN STATUS */
1202                 char x1,x2;
1203
1204                 if (CONSOLE_CheckForKeystroke(&x1,&x2))
1205                     AL_reg(context) = 0xff; 
1206                 else
1207                     AL_reg(context) = 0; 
1208                 break;
1209         }
1210     case 0x2e: /* SET VERIFY FLAG */
1211         TRACE("SET VERIFY FLAG ignored\n");
1212         /* we cannot change the behaviour anyway, so just ignore it */
1213         break;
1214
1215     case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
1216     case 0x1d:
1217     case 0x1e:
1218     case 0x20:
1219     case 0x6b: /* NULL FUNCTION */
1220         AL_reg(context) = 0;
1221         break;
1222         
1223     case 0x5c: /* "FLOCK" - RECORD LOCKING */
1224         fLock(context);
1225         break;
1226
1227     case 0x0d: /* DISK BUFFER FLUSH */
1228         TRACE("DISK BUFFER FLUSH ignored\n");
1229         RESET_CFLAG(context); /* dos 6+ only */
1230         break;
1231
1232     case 0x0e: /* SELECT DEFAULT DRIVE */
1233         TRACE("SELECT DEFAULT DRIVE %d\n", DL_reg(context));
1234         DRIVE_SetCurrentDrive( DL_reg(context) );
1235         AL_reg(context) = MAX_DOS_DRIVES;
1236         break;
1237
1238     case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1239         TRACE("FIND FIRST MATCHING FILE USING FCB %p\n", 
1240               CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1241         if (!INT21_FindFirstFCB(context))
1242         {
1243             AL_reg(context) = 0xff;
1244             break;
1245         }
1246         /* else fall through */
1247
1248     case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1249         AL_reg(context) = INT21_FindNextFCB(context) ? 0x00 : 0xff;
1250         break;
1251
1252     case 0x13: /* DELETE FILE USING FCB */
1253         DeleteFileFCB(context);
1254         break;
1255             
1256     case 0x17: /* RENAME FILE USING FCB */
1257         RenameFileFCB(context);
1258         break;
1259
1260     case 0x19: /* GET CURRENT DEFAULT DRIVE */
1261         AL_reg(context) = DRIVE_GetCurrentDrive();
1262         break;
1263
1264     case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1265         {
1266             TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1267             pTask->dta = PTR_SEG_OFF_TO_SEGPTR(DS_reg(context),DX_reg(context));
1268             TRACE("Set DTA: %08lx\n", pTask->dta);
1269         }
1270         break;
1271
1272     case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1273         DL_reg(context) = 0;
1274         if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1275         break;
1276         
1277     case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1278         if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1279         break;
1280
1281     case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1282         GetDrivePB(context, DRIVE_GetCurrentDrive());
1283         break;
1284                 
1285     case 0x25: /* SET INTERRUPT VECTOR */
1286         INT_CtxSetHandler( context, AL_reg(context),
1287                         (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1288                                                           DX_reg(context)));
1289         break;
1290
1291     case 0x29: /* PARSE FILENAME INTO FCB */
1292         INT21_ParseFileNameIntoFCB(context);
1293         break;
1294
1295     case 0x2a: /* GET SYSTEM DATE */
1296         INT21_GetSystemDate(context);
1297         break;
1298
1299     case 0x2b: /* SET SYSTEM DATE */
1300         FIXME("SetSystemDate(%02d/%02d/%04d): not allowed\n",
1301               DL_reg(context), DH_reg(context), CX_reg(context) );
1302         AL_reg(context) = 0;  /* Let's pretend we succeeded */
1303         break;
1304
1305     case 0x2c: /* GET SYSTEM TIME */
1306         INT21_GetSystemTime(context);
1307         break;
1308
1309     case 0x2d: /* SET SYSTEM TIME */
1310         FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1311               CH_reg(context), CL_reg(context),
1312               DH_reg(context), DL_reg(context) );
1313         AL_reg(context) = 0;  /* Let's pretend we succeeded */
1314         break;
1315
1316     case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1317         TRACE("GET DISK TRANSFER AREA ADDRESS\n");
1318         {
1319             TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1320             ES_reg(context) = SELECTOROF( pTask->dta );
1321             BX_reg(context) = OFFSETOF( pTask->dta );
1322         }
1323         break;
1324             
1325     case 0x30: /* GET DOS VERSION */
1326         TRACE("GET DOS VERSION %s requested\n",
1327               (AL_reg(context) == 0x00)?"OEM number":"version flag");
1328         AX_reg(context) = (HIWORD(GetVersion16()) >> 8) |
1329                           (HIWORD(GetVersion16()) << 8);
1330 #if 0
1331         AH_reg(context) = 0x7;
1332         AL_reg(context) = 0xA;
1333 #endif
1334
1335         BX_reg(context) = 0x00FF;     /* 0x123456 is Wine's serial # */
1336         CX_reg(context) = 0x0000;
1337         break;
1338
1339     case 0x31: /* TERMINATE AND STAY RESIDENT */
1340         FIXME("TERMINATE AND STAY RESIDENT stub\n");
1341         break;
1342
1343     case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1344         TRACE("GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
1345               INT21_DriveName( DL_reg(context)));
1346         GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
1347         break;
1348
1349     case 0x33: /* MULTIPLEXED */
1350         switch (AL_reg(context))
1351         {
1352               case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1353                 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
1354                 DL_reg(context) = DOSCONF_config.brk_flag;
1355                 break;
1356
1357               case 0x01: /* SET EXTENDED BREAK STATE */
1358                 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
1359                 DOSCONF_config.brk_flag = (DL_reg(context) > 0);
1360                 break;          
1361                 
1362               case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1363                 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
1364                 /* ugly coding in order to stay reentrant */
1365                 if (DL_reg(context))
1366                 {
1367                     DL_reg(context) = DOSCONF_config.brk_flag;
1368                     DOSCONF_config.brk_flag = 1;
1369                 }
1370                 else
1371                 {
1372                     DL_reg(context) = DOSCONF_config.brk_flag;
1373                     DOSCONF_config.brk_flag = 0;
1374                 }
1375                 break;
1376
1377               case 0x05: /* GET BOOT DRIVE */
1378                 TRACE("GET BOOT DRIVE\n");
1379                 DL_reg(context) = 3;
1380                 /* c: is Wine's bootdrive (a: is 1)*/
1381                 break;
1382                                 
1383               case 0x06: /* GET TRUE VERSION NUMBER */
1384                 TRACE("GET TRUE VERSION NUMBER\n");
1385                 BX_reg(context) = (HIWORD(GetVersion16() >> 8)) |
1386                                   (HIWORD(GetVersion16() << 8));
1387                 DX_reg(context) = 0x00;
1388                 break;
1389
1390               default:
1391                 INT_BARF( context, 0x21 );
1392                 break;                  
1393         }
1394         break;  
1395             
1396     case 0x34: /* GET ADDRESS OF INDOS FLAG */
1397         TRACE("GET ADDRESS OF INDOS FLAG\n");
1398         if (!heap) INT21_CreateHeap();
1399         ES_reg(context) = DosHeapHandle;
1400         BX_reg(context) = (int)&heap->InDosFlag - (int)heap;
1401         break;
1402
1403     case 0x35: /* GET INTERRUPT VECTOR */
1404         TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
1405         {
1406             FARPROC16 addr = INT_CtxGetHandler( context, AL_reg(context) );
1407             ES_reg(context) = SELECTOROF(addr);
1408             BX_reg(context) = OFFSETOF(addr);
1409         }
1410         break;
1411
1412     case 0x36: /* GET FREE DISK SPACE */
1413         TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
1414               INT21_DriveName( DL_reg(context)));
1415         if (!INT21_GetFreeDiskSpace(context)) AX_reg(context) = 0xffff;
1416         break;
1417
1418     case 0x37: 
1419       {
1420         unsigned char switchchar='/';
1421         switch (AL_reg(context))
1422         {
1423         case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
1424           TRACE("SWITCHAR - GET SWITCH CHARACTER\n");
1425           AL_reg(context) = 0x00; /* success*/
1426           DL_reg(context) = switchchar;
1427           break;
1428         case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
1429           TRACE("SWITCHAR - SET SWITCH CHARACTER\n");
1430           switchchar = DL_reg(context);
1431           AL_reg(context) = 0x00; /* success*/
1432           break;
1433         default: /*"AVAILDEV" - SPECIFY \DEV\ PREFIX USE*/
1434           INT_BARF( context, 0x21 );
1435           break;
1436         }
1437         break;
1438       }
1439
1440     case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1441         TRACE("GET COUNTRY-SPECIFIC INFORMATION for country 0x%02x\n",
1442               AL_reg(context));
1443         AX_reg(context) = 0x02; /* no country support available */
1444         SET_CFLAG(context);
1445         break;
1446
1447     case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1448         TRACE("MKDIR %s\n",
1449               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1450         bSetDOSExtendedError = (!CreateDirectory16( CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),
1451                                                            EDX_reg(context) ), NULL));
1452         /* FIXME: CreateDirectory's LastErrors will clash with the ones
1453          * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1454          * denied), while CreateDirectory return several ones. remap some of
1455          * them. -Marcus
1456          */
1457         if (bSetDOSExtendedError) {
1458                 switch (GetLastError()) {
1459                 case ERROR_ALREADY_EXISTS:
1460                 case ERROR_FILENAME_EXCED_RANGE:
1461                 case ERROR_DISK_FULL:
1462                         SetLastError(ERROR_ACCESS_DENIED);
1463                         break;
1464                 default: break;
1465                 }
1466         }
1467         break;
1468         
1469     case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1470         TRACE("RMDIR %s\n",
1471               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1472         bSetDOSExtendedError = (!RemoveDirectory16( CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),
1473                                                                  EDX_reg(context) )));
1474         break;
1475
1476     case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1477         TRACE("CHDIR %s\n",
1478               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1479         bSetDOSExtendedError = !INT21_ChangeDir(context);
1480         break;
1481         
1482     case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1483         TRACE("CREAT flag 0x%02x %s\n",CX_reg(context),
1484               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1485         bSetDOSExtendedError = INT21_CreateFile( context );
1486         break;
1487
1488     case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1489         TRACE("OPEN mode 0x%02x %s\n",AL_reg(context),
1490               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1491         OpenExistingFile(context);
1492         break;
1493
1494     case 0x3e: /* "CLOSE" - CLOSE FILE */
1495         TRACE("CLOSE handle %d\n",BX_reg(context));
1496         bSetDOSExtendedError = ((AX_reg(context) = _lclose16( BX_reg(context) )) != 0);
1497         break;
1498
1499     case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1500         TRACE("READ from %d to %04lX:%04X for %d byte\n",BX_reg(context),
1501               DS_reg(context),DX_reg(context),CX_reg(context) );
1502         {
1503             LONG result;
1504             if (ISV86(context))
1505                 result = _hread16( BX_reg(context),
1506                                    CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1507                                                                EDX_reg(context) ),
1508                                    CX_reg(context) );
1509             else
1510                 result = WIN16_hread( BX_reg(context),
1511                                       PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1512                                                              EDX_reg(context) ),
1513                                       CX_reg(context) );
1514             if (result == -1) bSetDOSExtendedError = TRUE;
1515             else AX_reg(context) = (WORD)result;
1516         }
1517         break;
1518
1519     case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1520         TRACE("WRITE from %04lX:%04X to handle %d for %d byte\n",
1521               DS_reg(context),DX_reg(context),BX_reg(context),CX_reg(context) );
1522         {
1523             LONG result = _hwrite16( BX_reg(context),
1524                                      CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),
1525                                                          EDX_reg(context) ),
1526                                      CX_reg(context) );
1527             if (result == -1) bSetDOSExtendedError = TRUE;
1528             else AX_reg(context) = (WORD)result;
1529         }
1530         break;
1531
1532     case 0x41: /* "UNLINK" - DELETE FILE */
1533         TRACE("UNLINK%s\n",
1534               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1535         bSetDOSExtendedError = (!DeleteFileA( CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),
1536                                                              EDX_reg(context) )));
1537         break;
1538
1539     case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1540         TRACE("LSEEK handle %d offset %ld from %s\n",
1541               BX_reg(context), MAKELONG(DX_reg(context),CX_reg(context)),
1542               (AL_reg(context)==0)?"start of file":(AL_reg(context)==1)?
1543               "current file position":"end of file");
1544         {
1545             LONG status = _llseek16( BX_reg(context),
1546                                      MAKELONG(DX_reg(context),CX_reg(context)),
1547                                      AL_reg(context) );
1548             if (status == -1) bSetDOSExtendedError = TRUE;
1549             else
1550             {
1551                 AX_reg(context) = LOWORD(status);
1552                 DX_reg(context) = HIWORD(status);
1553             }
1554         }
1555         break;
1556
1557     case 0x43: /* FILE ATTRIBUTES */
1558         switch (AL_reg(context))
1559         {
1560         case 0x00:
1561             TRACE("GET FILE ATTRIBUTES for %s\n", 
1562                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1563             AX_reg(context) = (WORD)GetFileAttributesA(
1564                                           CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1565                                                              EDX_reg(context)));
1566             if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1567             else CX_reg(context) = AX_reg(context);
1568             break;
1569
1570         case 0x01:
1571             TRACE("SET FILE ATTRIBUTES 0x%02x for %s\n", CX_reg(context),
1572                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1573             bSetDOSExtendedError = 
1574                 (!SetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, DS_reg(context), 
1575                                                            EDX_reg(context)),
1576                                                            CX_reg(context) ));
1577             break;
1578         case 0x02:
1579             TRACE("GET COMPRESSED FILE SIZE for %s stub\n",
1580                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1581         }
1582         break;
1583         
1584     case 0x44: /* IOCTL */
1585         switch (AL_reg(context))
1586         {
1587         case 0x00:
1588             ioctlGetDeviceInfo(context);
1589             break;
1590
1591         case 0x01:
1592             break;
1593         case 0x02:{
1594             const DOS_DEVICE *dev;
1595             if ((dev = DOSFS_GetDeviceByHandle( FILE_GetHandle(BX_reg(context)) )) &&
1596                 !strcasecmp( dev->name, "SCSIMGR$" ))
1597             {
1598                 ASPI_DOS_HandleInt(context);
1599             }
1600             break;
1601        }
1602         case 0x05:{     /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1603             /*BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context));*/
1604             int drive = DOS_GET_DRIVE(BL_reg(context));
1605
1606             FIXME("program tried to write to block device control channel of drive %d:\n",drive);
1607             /* for (i=0;i<CX_reg(context);i++)
1608                 fprintf(stdnimp,"%02x ",dataptr[i]);
1609             fprintf(stdnimp,"\n");*/
1610             AX_reg(context)=CX_reg(context);
1611             break;
1612         }
1613         case 0x08:   /* Check if drive is removable. */
1614             TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOVABLE for drive %s\n",
1615                  INT21_DriveName( BL_reg(context)));
1616             switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1617             {
1618             case DRIVE_CANNOTDETERMINE:
1619                 SetLastError( ERROR_INVALID_DRIVE );
1620                 AX_reg(context) = ERROR_INVALID_DRIVE;
1621                 SET_CFLAG(context);
1622                 break;
1623             case DRIVE_REMOVABLE:
1624                 AX_reg(context) = 0;      /* removable */
1625                 break;
1626             default:
1627                 AX_reg(context) = 1;   /* not removable */
1628                 break;
1629             }
1630             break;
1631
1632         case 0x09:   /* CHECK IF BLOCK DEVICE REMOTE */
1633             TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOTE for drive %s\n",
1634                  INT21_DriveName( BL_reg(context))); 
1635             switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1636             {
1637             case DRIVE_CANNOTDETERMINE:
1638                 SetLastError( ERROR_INVALID_DRIVE );
1639                 AX_reg(context) = ERROR_INVALID_DRIVE;
1640                 SET_CFLAG(context);
1641                 break;
1642             case DRIVE_REMOTE:
1643                 DX_reg(context) = (1<<9) | (1<<12);  /* remote */
1644                 break;
1645             default:
1646                 DX_reg(context) = 0;  /* FIXME: use driver attr here */
1647                 break;
1648             }
1649             break;
1650
1651         case 0x0a: /* check if handle (BX) is remote */
1652             TRACE("IOCTL - CHECK IF HANDLE %d IS REMOTE\n",BX_reg(context));
1653             /* returns DX, bit 15 set if remote, bit 14 set if date/time
1654              * not set on close
1655              */
1656             DX_reg(context) = 0;
1657             break;
1658
1659         case 0x0b:   /* SET SHARING RETRY COUNT */
1660             TRACE("IOCTL - SET SHARING RETRY COUNT pause %d retries %d\n",
1661                  CX_reg(context), DX_reg(context));
1662             if (!CX_reg(context))
1663             { 
1664                 AX_reg(context) = 1;
1665                 SET_CFLAG(context);
1666                 break;
1667             }
1668             DOSMEM_LOL()->sharing_retry_delay = CX_reg(context);
1669             if (!DX_reg(context))
1670                 DOSMEM_LOL()->sharing_retry_count = DX_reg(context);
1671             RESET_CFLAG(context);
1672             break;
1673
1674         case 0x0d:
1675             TRACE("IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
1676                   INT21_DriveName( BL_reg(context)));
1677             bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1678             break;
1679
1680         case 0x0e: /* get logical drive mapping */
1681             TRACE("IOCTL - GET LOGICAL DRIVE MAP for drive %s\n",
1682                   INT21_DriveName( BL_reg(context)));
1683             AL_reg(context) = 0; /* drive has no mapping - FIXME: may be wrong*/
1684             break;
1685
1686         case 0x0F:   /* Set logical drive mapping */
1687             {
1688             int drive;
1689             TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
1690                   INT21_DriveName( BL_reg(context))); 
1691             drive = DOS_GET_DRIVE ( BL_reg(context) );
1692             if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
1693             {
1694                 SET_CFLAG(context);
1695                 AX_reg(context) = 0x000F;  /* invalid drive */
1696             }
1697             break;
1698             }
1699
1700         case 0xe0:  /* Sun PC-NFS API */
1701             /* not installed */
1702             break;
1703                 
1704         default:
1705             INT_BARF( context, 0x21 );
1706             break;
1707         }
1708         break;
1709
1710     case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1711         {
1712             HANDLE handle;
1713             TRACE("DUP - DUPLICATE FILE HANDLE %d\n",BX_reg(context));
1714             if ((bSetDOSExtendedError = !DuplicateHandle( GetCurrentProcess(),
1715                                                           FILE_GetHandle(BX_reg(context)),
1716                                                           GetCurrentProcess(), &handle,
1717                                                           0, TRUE, DUPLICATE_SAME_ACCESS )))
1718                 AX_reg(context) = HFILE_ERROR16;
1719             else
1720                 AX_reg(context) = FILE_AllocDosHandle(handle);
1721             break;
1722         }
1723
1724     case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1725         TRACE("FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
1726               BX_reg(context),CX_reg(context));
1727         bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR16);
1728         break;
1729
1730     case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1731         TRACE("CWD - GET CURRENT DIRECTORY for drive %s\n",
1732               INT21_DriveName( DL_reg(context)));
1733         bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1734         break;
1735
1736     case 0x48: /* ALLOCATE MEMORY */
1737         TRACE("ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context));
1738         {
1739             LPVOID *mem; 
1740             if (ISV86(context))
1741               {
1742                 mem= DOSMEM_GetBlock(0,(DWORD)BX_reg(context)<<4,NULL);
1743             if (mem)
1744                 AX_reg(context) = DOSMEM_MapLinearToDos(mem)>>4;
1745               }
1746             else
1747               {
1748                 mem = (LPVOID)GlobalDOSAlloc16(BX_reg(context)<<4);
1749                 if (mem)
1750                   AX_reg(context) = (DWORD)mem&0xffff;
1751               }
1752             if (!mem)
1753               {
1754                 SET_CFLAG(context);
1755                 AX_reg(context) = 0x0008; /* insufficient memory */
1756                 BX_reg(context) = DOSMEM_Available(0)>>4;
1757             }
1758         }
1759         break;
1760
1761     case 0x49: /* FREE MEMORY */
1762         TRACE("FREE MEMORY segment %04lX\n", ES_reg(context));
1763         {
1764           BOOL ret;
1765           if (ISV86(context))
1766             ret= DOSMEM_FreeBlock(0,DOSMEM_MapDosToLinear(ES_reg(context)<<4));
1767           else
1768             {
1769               ret = !GlobalDOSFree16(ES_reg(context));
1770               /* If we don't reset ES_reg, we will fail in the relay code */
1771               ES_reg(context)=ret;
1772             }
1773           if (!ret)
1774             {
1775               TRACE("FREE MEMORY failed\n");
1776             SET_CFLAG(context);
1777             AX_reg(context) = 0x0009; /* memory block address invalid */
1778         }
1779         }
1780         break;
1781
1782     case 0x4a: /* RESIZE MEMORY BLOCK */
1783         TRACE("RESIZE MEMORY segment %04lX to %d paragraphs\n", ES_reg(context), BX_reg(context));
1784         if (!ISV86(context))
1785           FIXME("RESIZE MEMORY probably insufficient implementation. Expect crash soon\n");
1786         {
1787             LPVOID *mem = DOSMEM_ResizeBlock(0,DOSMEM_MapDosToLinear(ES_reg(context)<<4),
1788                                                BX_reg(context)<<4,NULL);
1789             if (mem)
1790                 AX_reg(context) = DOSMEM_MapLinearToDos(mem)>>4;
1791             else {
1792                 SET_CFLAG(context);
1793                 AX_reg(context) = 0x0008; /* insufficient memory */
1794                 BX_reg(context) = DOSMEM_Available(0)>>4; /* not quite right */
1795             }
1796         }
1797         break;
1798
1799     case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1800         TRACE("EXEC %s\n",
1801               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),EDX_reg(context) ));
1802         AX_reg(context) = WinExec16( CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),
1803                                                          EDX_reg(context) ),
1804                                      SW_NORMAL );
1805         if (AX_reg(context) < 32) SET_CFLAG(context);
1806         break;          
1807         
1808     case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1809         TRACE("EXIT with return code %d\n",AL_reg(context));
1810         ExitProcess( AL_reg(context) );
1811         break;
1812
1813     case 0x4d: /* GET RETURN CODE */
1814         TRACE("GET RETURN CODE (ERRORLEVEL)\n");
1815         AX_reg(context) = 0; /* normal exit */
1816         break;
1817
1818     case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1819         TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
1820               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1821         if (!INT21_FindFirst(context)) break;
1822         /* fall through */
1823
1824     case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1825         TRACE("FINDNEXT\n");
1826         if (!INT21_FindNext(context))
1827         {
1828             SetLastError( ERROR_NO_MORE_FILES );
1829             AX_reg(context) = ERROR_NO_MORE_FILES;
1830             SET_CFLAG(context);
1831         }
1832         else AX_reg(context) = 0;  /* OK */
1833         break;
1834     case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
1835         TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
1836         INT21_SetCurrentPSP(BX_reg(context));
1837         break;
1838     case 0x51: /* GET PSP ADDRESS */
1839         TRACE("GET CURRENT PROCESS ID (GET PSP ADDRESS)\n");
1840         /* FIXME: should we return the original DOS PSP upon */
1841         /*        Windows startup ? */
1842         BX_reg(context) = INT21_GetCurrentPSP();
1843         break;
1844     case 0x62: /* GET PSP ADDRESS */
1845         TRACE("GET CURRENT PSP ADDRESS\n");
1846         /* FIXME: should we return the original DOS PSP upon */
1847         /*        Windows startup ? */
1848         BX_reg(context) = INT21_GetCurrentPSP();
1849         break;
1850
1851     case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1852         TRACE("SYSVARS - GET LIST OF LISTS\n");
1853         {
1854             ES_reg(context) = ISV86(context) ? HIWORD(DOS_LOLSeg) : LOWORD(DOS_LOLSeg);
1855             BX_reg(context) = FIELD_OFFSET(DOS_LISTOFLISTS, ptr_first_DPB);
1856         }
1857         break;
1858
1859     case 0x54: /* Get Verify Flag */
1860         TRACE("Get Verify Flag - Not Supported\n");
1861         AL_reg(context) = 0x00;  /* pretend we can tell. 00h = off 01h = on */
1862         break;
1863
1864     case 0x56: /* "RENAME" - RENAME FILE */
1865         TRACE("RENAME %s to %s\n",
1866               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
1867               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, ES_reg(context),EDI_reg(context)));
1868         bSetDOSExtendedError = 
1869                 (!MoveFileA( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
1870                                CTX_SEG_OFF_TO_LIN(context, ES_reg(context),EDI_reg(context))));
1871         break;
1872
1873     case 0x57: /* FILE DATE AND TIME */
1874         switch (AL_reg(context))
1875         {
1876         case 0x00:  /* Get */
1877             {
1878                 FILETIME filetime;
1879                 TRACE("GET FILE DATE AND TIME for handle %d\n",
1880                       BX_reg(context));
1881                 if (!GetFileTime( FILE_GetHandle(BX_reg(context)), NULL, NULL, &filetime ))
1882                      bSetDOSExtendedError = TRUE;
1883                 else FileTimeToDosDateTime( &filetime, &DX_reg(context),
1884                                             &CX_reg(context) );
1885             }
1886             break;
1887
1888         case 0x01:  /* Set */
1889             {
1890                 FILETIME filetime;
1891                 TRACE("SET FILE DATE AND TIME for handle %d\n",
1892                       BX_reg(context));
1893                 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
1894                                        &filetime );
1895                 bSetDOSExtendedError = 
1896                         (!SetFileTime( FILE_GetHandle(BX_reg(context)),
1897                                       NULL, NULL, &filetime ));
1898             }
1899             break;
1900         }
1901         break;
1902
1903     case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1904         TRACE("GET OR SET MEMORY/UMB ALLOCATION STRATEGY subfunction %d\n",
1905               AL_reg(context));
1906         switch (AL_reg(context))
1907         {
1908         case 0x00:
1909             AX_reg(context) = 1;
1910             break;
1911         case 0x02:
1912             AX_reg(context) = 0;
1913             break;
1914         case 0x01:
1915         case 0x03:
1916             break;
1917         }
1918         RESET_CFLAG(context);
1919         break;
1920
1921     case 0x5a: /* CREATE TEMPORARY FILE */
1922         TRACE("CREATE TEMPORARY FILE\n");
1923         bSetDOSExtendedError = !INT21_CreateTempFile(context);
1924         break;
1925
1926     case 0x5b: /* CREATE NEW FILE */
1927         TRACE("CREATE NEW FILE 0x%02x for %s\n", CX_reg(context),
1928               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1929         bSetDOSExtendedError = ((AX_reg(context) = 
1930                _lcreat16_uniq( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
1931                                CX_reg(context) )) == (WORD)HFILE_ERROR16);
1932         break;
1933
1934     case 0x5d: /* NETWORK */
1935         FIXME("Function 0x%04x not implemented.\n", AX_reg (context));
1936         /* Fix the following while you're at it.  */
1937         SetLastError( ER_NoNetwork );
1938         bSetDOSExtendedError = TRUE;
1939         break;
1940
1941     case 0x5e:
1942         bSetDOSExtendedError = INT21_networkfunc (context);
1943         break;
1944
1945     case 0x5f: /* NETWORK */
1946         switch (AL_reg(context))
1947         {
1948         case 0x07: /* ENABLE DRIVE */
1949             TRACE("ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1950             if (!DRIVE_Enable( DL_reg(context) ))
1951             {
1952                 SetLastError( ERROR_INVALID_DRIVE );
1953                 bSetDOSExtendedError = TRUE;
1954             }
1955             break;
1956
1957         case 0x08: /* DISABLE DRIVE */
1958             TRACE("DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1959             if (!DRIVE_Disable( DL_reg(context) ))
1960             {
1961                 SetLastError( ERROR_INVALID_DRIVE );
1962                 bSetDOSExtendedError = TRUE;
1963             } 
1964             break;
1965
1966         default:
1967             /* network software not installed */
1968             TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
1969             SetLastError( ER_NoNetwork );
1970             bSetDOSExtendedError = TRUE;
1971             break;
1972         }
1973         break;
1974
1975     case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1976         TRACE("TRUENAME %s\n",
1977               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),ESI_reg(context)));
1978         {
1979             if (!GetFullPathNameA( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1980                                                         ESI_reg(context)), 128,
1981                                      CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
1982                                                         EDI_reg(context)),NULL))
1983                 bSetDOSExtendedError = TRUE;
1984             else AX_reg(context) = 0;
1985         }
1986         break;
1987
1988     case 0x61: /* UNUSED */
1989     case 0x63: /* misc. language support */
1990         switch (AL_reg(context)) {
1991         case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
1992             INT21_GetDBCSLeadTable(context);
1993             break;
1994         }
1995         break;
1996     case 0x64: /* OS/2 DOS BOX */
1997         INT_BARF( context, 0x21 );
1998         SET_CFLAG(context);
1999         break;
2000
2001     case 0x65:{/* GET EXTENDED COUNTRY INFORMATION */
2002         extern WORD WINE_LanguageId;
2003         BYTE    *dataptr=CTX_SEG_OFF_TO_LIN(context, ES_reg(context),EDI_reg(context));
2004         TRACE("GET EXTENDED COUNTRY INFORMATION code page %d country %d\n",
2005               BX_reg(context), DX_reg(context));
2006         switch (AL_reg(context)) {
2007         case 0x01:
2008             TRACE("\tget general internationalization info\n");
2009             dataptr[0] = 0x1;
2010             *(WORD*)(dataptr+1) = 41;
2011             *(WORD*)(dataptr+3) = WINE_LanguageId;
2012             *(WORD*)(dataptr+5) = CodePage;
2013             *(DWORD*)(dataptr+0x19) = 0; /* FIXME: ptr to case map routine */
2014             break;
2015         case 0x06:
2016             TRACE("\tget pointer to collating sequence table\n");
2017             dataptr[0] = 0x06;
2018             *(DWORD*)(dataptr+1) = MAKELONG(DOSMEM_CollateTable & 0xFFFF,DOSMEM_AllocSelector(DOSMEM_CollateTable>>16));
2019             CX_reg(context)         = 258;/*FIXME: size of table?*/
2020             break;
2021         case 0x20:
2022             TRACE("\tConvert char to uppercase\n");
2023             DL_reg(context) = toupper(DL_reg(context));
2024             break;
2025         case 0x21:
2026             TRACE("\tconvert string to uppercase with length\n");
2027             CharUpperBuffA( (LPSTR)CTX_SEG_OFF_TO_LIN(context,DS_reg(context),EDX_reg(context)),
2028                             CX_reg(context) );
2029             break;
2030         case 0x22:            
2031             TRACE("\tConvert ASCIIZ string to uppercase\n");
2032             CharUpperA( (LPSTR)CTX_SEG_OFF_TO_LIN(context,DS_reg(context),EDX_reg(context)) );
2033             break;    
2034         default:
2035             TRACE("\tunimplemented function %d\n",AL_reg(context));
2036             INT_BARF( context, 0x21 );
2037             SET_CFLAG(context);
2038             break;
2039         }
2040         break;
2041     }
2042     case 0x66: /* GLOBAL CODE PAGE TABLE */
2043         switch (AL_reg(context))
2044         {
2045         case 0x01:
2046             TRACE("GET GLOBAL CODE PAGE TABLE\n");
2047             DX_reg(context) = BX_reg(context) = CodePage;
2048             RESET_CFLAG(context);
2049             break;                      
2050         case 0x02: 
2051             TRACE("SET GLOBAL CODE PAGE TABLE active page %d system page %d\n",
2052                   BX_reg(context),DX_reg(context));
2053             CodePage = BX_reg(context);
2054             RESET_CFLAG(context);
2055             break;
2056         }
2057         break;
2058
2059     case 0x67: /* SET HANDLE COUNT */
2060         TRACE("SET HANDLE COUNT to %d\n",BX_reg(context) );
2061         SetHandleCount16( BX_reg(context) );
2062         if (GetLastError()) bSetDOSExtendedError = TRUE;
2063         break;
2064
2065     case 0x68: /* "FFLUSH" - COMMIT FILE */
2066     case 0x6a: /* COMMIT FILE */
2067         TRACE("FFLUSH/COMMIT handle %d\n",BX_reg(context));
2068         bSetDOSExtendedError = (!FlushFileBuffers( FILE_GetHandle(BX_reg(context)) ));
2069         break;          
2070         
2071     case 0x69: /* DISK SERIAL NUMBER */
2072         switch (AL_reg(context))
2073         {
2074         case 0x00:
2075             TRACE("GET DISK SERIAL NUMBER for drive %s\n",
2076                   INT21_DriveName(BL_reg(context)));
2077             if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
2078             else AX_reg(context) = 0;
2079             break;
2080
2081         case 0x01:
2082             TRACE("SET DISK SERIAL NUMBER for drive %s\n",
2083                   INT21_DriveName(BL_reg(context)));
2084             if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
2085             else AX_reg(context) = 1;
2086             break;
2087         }
2088         break;
2089     
2090     case 0x6C: /* Extended Open/Create*/
2091         TRACE("EXTENDED OPEN/CREATE %s\n",
2092               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDI_reg(context)));
2093         bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
2094         break;
2095         
2096     case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
2097         if ((GetVersion()&0xC0000004)!=0xC0000004) {
2098             /* not supported on anything but Win95 */
2099             TRACE("LONG FILENAME functions supported only by win95\n");
2100             SET_CFLAG(context);
2101             AL_reg(context) = 0;
2102         } else
2103         switch(AL_reg(context))
2104         {
2105         case 0x39:  /* Create directory */
2106             TRACE("LONG FILENAME - MAKE DIRECTORY %s\n",
2107                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),EDX_reg(context)));
2108             bSetDOSExtendedError = (!CreateDirectoryA( 
2109                                         CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),
2110                                                   EDX_reg(context) ), NULL));
2111             /* FIXME: CreateDirectory's LastErrors will clash with the ones
2112              * used by dos. AH=39 only returns 3 (path not found) and 5 (access
2113              * denied), while CreateDirectory return several ones. remap some of
2114              * them. -Marcus
2115              */
2116             if (bSetDOSExtendedError) {
2117                     switch (GetLastError()) {
2118                     case ERROR_ALREADY_EXISTS:
2119                     case ERROR_FILENAME_EXCED_RANGE:
2120                     case ERROR_DISK_FULL:
2121                             SetLastError(ERROR_ACCESS_DENIED);
2122                             break;
2123                     default: break;
2124                     }
2125             }
2126             break;
2127         case 0x3a:  /* Remove directory */
2128             TRACE("LONG FILENAME - REMOVE DIRECTORY %s\n",
2129                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),EDX_reg(context)));
2130             bSetDOSExtendedError = (!RemoveDirectoryA( 
2131                                         CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),
2132                                                         EDX_reg(context) )));
2133             break;
2134         case 0x43:  /* Get/Set file attributes */
2135           TRACE("LONG FILENAME -EXTENDED GET/SET FILE ATTRIBUTES %s\n",
2136                 (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),EDX_reg(context)));
2137         switch (BL_reg(context))
2138         {
2139         case 0x00: /* Get file attributes */
2140             TRACE("\tretrieve attributes\n");
2141             CX_reg(context) = (WORD)GetFileAttributesA(
2142                                           CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2143                                                              EDX_reg(context)));
2144             if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
2145             break;
2146         case 0x01:
2147             TRACE("\tset attributes 0x%04x\n",CX_reg(context));
2148             bSetDOSExtendedError = (!SetFileAttributesA( 
2149                                         CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2150                                                            EDX_reg(context)),
2151                                         CX_reg(context)  ) );
2152             break;
2153         default:
2154           FIXME("Unimplemented long file name function:\n");
2155           INT_BARF( context, 0x21 );
2156           SET_CFLAG(context);
2157           AL_reg(context) = 0;
2158           break;
2159         }
2160         break;
2161         case 0x47:  /* Get current directory */
2162             TRACE(" LONG FILENAME - GET CURRENT DIRECTORY for drive %s\n",
2163                   INT21_DriveName(DL_reg(context)));
2164             bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
2165             break;
2166
2167         case 0x4e:  /* Find first file */
2168             TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
2169                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)));
2170             /* FIXME: use attributes in CX */
2171             if ((AX_reg(context) = FindFirstFile16(
2172                    CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
2173                    (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2174                                                             EDI_reg(context))))
2175                 == INVALID_HANDLE_VALUE16)
2176                 bSetDOSExtendedError = TRUE;
2177             break;
2178         case 0x4f:  /* Find next file */
2179             TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
2180                   BX_reg(context));
2181             if (!FindNextFile16( BX_reg(context),
2182                     (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2183                                                              EDI_reg(context))))
2184                 bSetDOSExtendedError = TRUE;
2185             break;
2186         case 0xa1:  /* Find close */
2187             TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
2188                   BX_reg(context));
2189             bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
2190             break;
2191         case 0xa0:
2192             TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive %s stub\n",
2193                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),EDX_reg(context)));
2194             break;
2195         case 0x60:  
2196           switch(CL_reg(context))
2197           {
2198             case 0x01:  /*Get short filename or path */
2199               if (!GetShortPathNameA
2200                   ( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2201                                        ESI_reg(context)),
2202                     CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2203                                        EDI_reg(context)), 67))
2204                 bSetDOSExtendedError = TRUE;
2205               else AX_reg(context) = 0;
2206               break;
2207             case 0x02:  /*Get canonical long filename or path */
2208               if (!GetFullPathNameA
2209                   ( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2210                                        ESI_reg(context)), 128,
2211                     CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2212                                        EDI_reg(context)),NULL))
2213                 bSetDOSExtendedError = TRUE;
2214               else AX_reg(context) = 0;
2215               break;
2216             default:
2217               FIXME("Unimplemented long file name function:\n");
2218               INT_BARF( context, 0x21 );
2219               SET_CFLAG(context);
2220               AL_reg(context) = 0;
2221               break;
2222           }
2223             break;
2224         case 0x6c:  /* Create or open file */
2225             TRACE("LONG FILENAME - CREATE OR OPEN FILE %s\n",
2226                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), ESI_reg(context))); 
2227           /* translate Dos 7 action to Dos 6 action */
2228             bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
2229             break;
2230
2231         case 0x3b:  /* Change directory */
2232             TRACE("LONG FILENAME - CHANGE DIRECTORY %s\n",
2233                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context))); 
2234             if (!SetCurrentDirectoryA(CTX_SEG_OFF_TO_LIN(context, 
2235                                                 DS_reg(context),
2236                                                 EDX_reg(context)
2237                                         ))
2238             ) {
2239                 SET_CFLAG(context);
2240                 AL_reg(context) = GetLastError();
2241             }
2242             break;
2243         case 0x41:  /* Delete file */
2244             TRACE("LONG FILENAME - DELETE FILE %s\n",
2245                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context))); 
2246             if (!DeleteFileA(CTX_SEG_OFF_TO_LIN(context, 
2247                                         DS_reg(context),
2248                                         EDX_reg(context))
2249             )) {
2250                 SET_CFLAG(context);
2251                 AL_reg(context) = GetLastError();
2252             }
2253             break;
2254         case 0x56:  /* Move (rename) file */
2255             TRACE("LONG FILENAME - RENAME FILE %s to %s stub\n",
2256                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)),
2257                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  ES_reg(context), EDI_reg(context))); 
2258         default:
2259             FIXME("Unimplemented long file name function:\n");
2260             INT_BARF( context, 0x21 );
2261             SET_CFLAG(context);
2262             AL_reg(context) = 0;
2263             break;
2264         }
2265         break;
2266
2267     case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
2268     case 0x72: /* MS-DOS 7 (Windows95) - ??? */
2269     case 0x73: /* MS-DOS 7 (Windows95) - DRIVE LOCKING ??? */
2270         TRACE("windows95 function AX %04x\n",
2271                     AX_reg(context));
2272         WARN("        returning unimplemented\n");
2273         SET_CFLAG(context);
2274         AL_reg(context) = 0;
2275         break;
2276
2277     case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
2278     case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
2279         break;
2280
2281     default:
2282         INT_BARF( context, 0x21 );
2283         break;
2284
2285     } /* END OF SWITCH */
2286
2287     if( bSetDOSExtendedError )          /* set general error condition */
2288     {   
2289         AX_reg(context) = GetLastError();
2290         SET_CFLAG(context);
2291     }
2292
2293     if ((EFL_reg(context) & 0x0001))
2294         TRACE("failed, error 0x%04lx\n", GetLastError() );
2295  
2296     TRACE("returning: AX=%04x BX=%04x CX=%04x DX=%04x "
2297                  "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
2298                  AX_reg(context), BX_reg(context), CX_reg(context),
2299                  DX_reg(context), SI_reg(context), DI_reg(context),
2300                  (WORD)DS_reg(context), (WORD)ES_reg(context),
2301                  EFL_reg(context));
2302 }
2303
2304 FARPROC16 WINAPI GetSetKernelDOSProc16(FARPROC16 DosProc)
2305 {
2306         FIXME("(DosProc=0x%08x): stub\n", (UINT)DosProc);
2307         return NULL;
2308 }
2309