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