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