Do not clear FD_WINE_CONNECTED on shutdown(). FD_WINE_CONNECTED
[wine] / msdos / int21.c
1 /*
2  * DOS interrupt 21h handler
3  *
4  * Copyright 1993, 1994 Erik Bos
5  * Copyright 1996 Alexandre Julliard
6  * Copyright 1997 Andreas Mohr
7  * Copyright 1998 Uwe Bonnes
8  * Copyright 1998, 1999 Ove Kaaven
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <time.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #ifdef HAVE_SYS_FILE_H
33 # include <sys/file.h>
34 #endif
35 #include <string.h>
36 #ifdef HAVE_SYS_TIME_H
37 # include <sys/time.h>
38 #endif
39 #include <sys/types.h>
40 #ifdef HAVE_UNISTD_H
41 # include <unistd.h>
42 #endif
43 #ifdef HAVE_UTIME_H
44 # include <utime.h>
45 #endif
46 #include <ctype.h>
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winternl.h"
50 #include "wingdi.h"
51 #include "winuser.h" /* SW_NORMAL */
52 #include "wine/winbase16.h"
53 #include "winerror.h"
54 #include "drive.h"
55 #include "file.h"
56 #include "callback.h"
57 #include "msdos.h"
58 #include "miscemu.h"
59 #include "task.h"
60 #include "wine/unicode.h"
61 #include "wine/debug.h"
62
63 WINE_DEFAULT_DEBUG_CHANNEL(int21);
64 #if defined(__svr4__) || defined(_SCO_DS)
65 /* SVR4 DOESNT do locking the same way must implement properly */
66 #define LOCK_EX 0
67 #define LOCK_SH  1
68 #define LOCK_NB  8
69 #endif
70
71
72 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
73
74 /* Define the drive parameter block, as used by int21/1F
75  * and int21/32.  This table can be accessed through the
76  * global 'dpb' pointer, which points into the local dos
77  * heap.
78  */
79 struct DPB
80 {
81     BYTE drive_num;         /* 0=A, etc. */
82     BYTE unit_num;          /* Drive's unit number (?) */
83     WORD sector_size;       /* Sector size in bytes */
84     BYTE high_sector;       /* Highest sector in a cluster */
85     BYTE shift;             /* Shift count (?) */
86     WORD reserved;          /* Number of reserved sectors at start */
87     BYTE num_FAT;           /* Number of FATs */
88     WORD dir_entries;       /* Number of root dir entries */
89     WORD first_data;        /* First data sector */
90     WORD high_cluster;      /* Highest cluster number */
91     WORD sectors_in_FAT;    /* Number of sectors per FAT */
92     WORD start_dir;         /* Starting sector of first dir */
93     DWORD driver_head;      /* Address of device driver header (?) */
94     BYTE media_ID;          /* Media ID */
95     BYTE access_flag;       /* Prev. accessed flag (0=yes,0xFF=no) */
96     DWORD next;             /* Pointer to next DPB in list */
97     WORD free_search;       /* Free cluster search start */
98     WORD free_clusters;     /* Number of free clusters (0xFFFF=unknown) */
99 };
100
101 struct EDPB                     /* FAT32 extended Drive Parameter Block */
102 {                               /* from Ralf Brown's Interrupt List */
103         struct DPB dpb;         /* first 24 bytes = original DPB */
104
105         BYTE edpb_flags;        /* undocumented/unknown flags */
106         DWORD next_edpb;        /* pointer to next EDPB */
107         WORD free_cluster;      /* cluster to start search for free space on write, typically
108                                    the last cluster allocated */
109         WORD clusters_free;     /* number of free clusters on drive or FFFF = unknown */
110         WORD clusters_free_hi;  /* hiword of clusters_free */
111         WORD mirroring_flags;   /* mirroring flags: bit 7 set = do not mirror active FAT */
112                                 /* bits 0-3 = 0-based number of the active FAT */
113         WORD info_sector;       /* sector number of file system info sector, or FFFF for none */
114         WORD spare_boot_sector; /* sector number of backup boot sector, or FFFF for none */
115         DWORD first_cluster;    /* sector number of the first cluster */
116         DWORD max_cluster;      /* sector number of the last cluster */
117         DWORD fat_clusters;     /* number of clusters occupied by FAT */
118         DWORD root_cluster;     /* cluster number of start of root directory */
119         DWORD free_cluster2;    /* same as free_cluster: cluster at which to start
120                                    search for free space when writing */
121
122 };
123
124 DWORD dpbsegptr;
125
126 struct DosHeap {
127         BYTE mediaID;
128         BYTE biosdate[8];
129         struct DPB dpb;
130 };
131 static struct DosHeap *heap;
132 static WORD DosHeapHandle;
133
134 extern char TempDirectory[];
135
136 static void INT21_ReadConfigSys(void)
137 {
138     static int done;
139     if (!done) DOSCONF_ReadConfig();
140     done = 1;
141 }
142
143 static BOOL INT21_CreateHeap(void)
144 {
145     if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
146     {
147         WARN("Out of memory\n");
148         return FALSE;
149     }
150     heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
151     dpbsegptr = MAKESEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
152     strcpy(heap->biosdate, "01/01/80");
153     return TRUE;
154 }
155
156 static BYTE *GetCurrentDTA( CONTEXT86 *context )
157 {
158     TDB *pTask = TASK_GetCurrent();
159
160     /* FIXME: This assumes DTA was set correctly! */
161     return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
162                                                 (DWORD)OFFSETOF(pTask->dta) );
163 }
164
165
166 void CreateBPB(int drive, BYTE *data, BOOL16 limited)
167 /* limited == TRUE is used with INT 0x21/0x440d */
168 {
169         if (drive > 1) {
170                 setword(data, 512);
171                 data[2] = 2;
172                 setword(&data[3], 0);
173                 data[5] = 2;
174                 setword(&data[6], 240);
175                 setword(&data[8], 64000);
176                 data[0x0a] = 0xf8;
177                 setword(&data[0x0b], 40);
178                 setword(&data[0x0d], 56);
179                 setword(&data[0x0f], 2);
180                 setword(&data[0x11], 0);
181                 if (!limited) {
182                     setword(&data[0x1f], 800);
183                     data[0x21] = 5;
184                     setword(&data[0x22], 1);
185                 }
186         } else { /* 1.44mb */
187                 setword(data, 512);
188                 data[2] = 2;
189                 setword(&data[3], 0);
190                 data[5] = 2;
191                 setword(&data[6], 240);
192                 setword(&data[8], 2880);
193                 data[0x0a] = 0xf8;
194                 setword(&data[0x0b], 6);
195                 setword(&data[0x0d], 18);
196                 setword(&data[0x0f], 2);
197                 setword(&data[0x11], 0);
198                 if (!limited) {
199                     setword(&data[0x1f], 80);
200                     data[0x21] = 7;
201                     setword(&data[0x22], 2);
202                 }
203         }
204 }
205
206 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
207 {
208     DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
209     char root[] = "A:\\";
210
211     *root += DOS_GET_DRIVE( DL_reg(context) );
212     if (!GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
213                               &free_clusters, &total_clusters )) return 0;
214     SET_AX( context, cluster_sectors );
215     SET_BX( context, free_clusters );
216     SET_CX( context, sector_bytes );
217     SET_DX( context, total_clusters );
218     return 1;
219 }
220
221 static int INT21_GetDriveAllocInfo( CONTEXT86 *context )
222 {
223     if (!INT21_GetFreeDiskSpace( context )) return 0;
224     if (!heap && !INT21_CreateHeap()) return 0;
225     heap->mediaID = 0xf0;
226     context->SegDs = DosHeapHandle;
227     SET_BX( context, (int)&heap->mediaID - (int)heap );
228     return 1;
229 }
230
231 static int FillInDrivePB( int drive )
232 {
233         if(!DRIVE_IsValid(drive))
234         {
235             SetLastError( ERROR_INVALID_DRIVE );
236                         return 0;
237         }
238         else if (heap || INT21_CreateHeap())
239         {
240                 /* FIXME: I have no idea what a lot of this information should
241                  * say or whether it even really matters since we're not allowing
242                  * direct block access.  However, some programs seem to depend on
243                  * getting at least _something_ back from here.  The 'next' pointer
244                  * does worry me, though.  Should we have a complete table of
245                  * separate DPBs per drive?  Probably, but I'm lazy. :-)  -CH
246                  */
247                 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
248                 heap->dpb.sector_size = 512;
249                 heap->dpb.high_sector = 1;
250                 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
251                 heap->dpb.reserved = 0;
252                 heap->dpb.num_FAT = 1;
253                 heap->dpb.dir_entries = 2;
254                 heap->dpb.first_data = 2;
255                 heap->dpb.high_cluster = 64000;
256                 heap->dpb.sectors_in_FAT = 1;
257                 heap->dpb.start_dir = 1;
258                 heap->dpb.driver_head = 0;
259                 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
260                 heap->dpb.access_flag = 0;
261                 heap->dpb.next = 0;
262                 heap->dpb.free_search = 0;
263                 heap->dpb.free_clusters = 0xFFFF;    /* unknown */
264                                 return 1;
265                 }
266
267                 return 0;
268 }
269
270 static void GetDrivePB( CONTEXT86 *context, int drive )
271 {
272         if (FillInDrivePB( drive ))
273         {
274                 SET_AL( context, 0x00 );
275                 context->SegDs = SELECTOROF(dpbsegptr);
276                 SET_BX( context, OFFSETOF(dpbsegptr) );
277         }
278         else
279         {
280         SET_AX( context, 0x00ff );
281         }
282 }
283
284
285 static void ioctlGetDeviceInfo( CONTEXT86 *context )
286 {
287     int curr_drive;
288     const DOS_DEVICE *dev;
289
290     TRACE("(%d)\n", BX_reg(context));
291
292     RESET_CFLAG(context);
293
294     /* DOS device ? */
295     if ((dev = DOSFS_GetDeviceByHandle( DosFileHandleToWin32Handle(BX_reg(context)) )))
296     {
297         SET_DX( context, dev->flags );
298         return;
299     }
300
301     /* it seems to be a file */
302     curr_drive = DRIVE_GetCurrentDrive();
303     SET_DX( context, 0x0140 + curr_drive + ((curr_drive > 1) ? 0x0800 : 0) );
304     /* no floppy */
305     /* bits 0-5 are current drive
306      * bit 6 - file has NOT been written..FIXME: correct?
307      * bit 8 - generate int24 if no diskspace on write/ read past end of file
308      * bit 11 - media not removable
309      * bit 14 - don't set file date/time on closing
310      * bit 15 - file is remote
311      */
312 }
313
314 static BOOL ioctlGenericBlkDevReq( CONTEXT86 *context )
315 {
316         BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
317         int drive = DOS_GET_DRIVE( BL_reg(context) );
318
319         if (!DRIVE_IsValid(drive))
320         {
321             SetLastError( ERROR_FILE_NOT_FOUND );
322             return TRUE;
323         }
324
325         if (CH_reg(context) != 0x08)
326         {
327             INT_BARF( context, 0x21 );
328             return FALSE;
329         }
330
331         switch (CL_reg(context))
332         {
333                 case 0x4a: /* lock logical volume */
334                         TRACE("lock logical volume (%d) level %d mode %d\n",drive,BH_reg(context),DX_reg(context));
335                         break;
336
337                 case 0x60: /* get device parameters */
338                            /* used by w4wgrp's winfile */
339                         memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
340                         dataptr[0] = 0x04;
341                         dataptr[6] = 0; /* media type */
342                         if (drive > 1)
343                         {
344                                 dataptr[1] = 0x05; /* fixed disk */
345                                 setword(&dataptr[2], 0x01); /* non removable */
346                                 setword(&dataptr[4], 0x300); /* # of cylinders */
347                         }
348                         else
349                         {
350                                 dataptr[1] = 0x07; /* block dev, floppy */
351                                 setword(&dataptr[2], 0x02); /* removable */
352                                 setword(&dataptr[4], 80); /* # of cylinders */
353                         }
354                         CreateBPB(drive, &dataptr[7], TRUE);
355                         RESET_CFLAG(context);
356                         break;
357
358                 case 0x41: /* write logical device track */
359                 case 0x61: /* read logical device track */
360                         {
361                                 BYTE drive = BL_reg(context) ?
362                                                 BL_reg(context) : DRIVE_GetCurrentDrive();
363                                 WORD head   = *(WORD *)dataptr+1;
364                                 WORD cyl    = *(WORD *)dataptr+3;
365                                 WORD sect   = *(WORD *)dataptr+5;
366                                 WORD nrsect = *(WORD *)dataptr+7;
367                                 BYTE *data  =  (BYTE *)dataptr+9;
368                                 int (*raw_func)(BYTE, DWORD, DWORD, BYTE *, BOOL);
369
370                                 raw_func = (CL_reg(context) == 0x41) ?
371                                                                 DRIVE_RawWrite : DRIVE_RawRead;
372
373                                 if (raw_func(drive, head*cyl*sect, nrsect, data, FALSE))
374                                         RESET_CFLAG(context);
375                                 else
376                                 {
377                                         SET_AX( context, 0x1e ); /* read fault */
378                                         SET_CFLAG(context);
379                                 }
380                         }
381                         break;
382                 case 0x66:/*  get disk serial number */
383                         {
384                                 char    label[12],fsname[9],path[4];
385                                 DWORD   serial;
386
387                                 strcpy(path,"x:\\");path[0]=drive+'A';
388                                 GetVolumeInformationA(
389                                         path,label,12,&serial,NULL,NULL,fsname,9
390                                 );
391                                 *(WORD*)dataptr         = 0;
392                                 memcpy(dataptr+2,&serial,4);
393                                 memcpy(dataptr+6,label  ,11);
394                                 memcpy(dataptr+17,fsname,8);
395                         }
396                         break;
397
398                 case 0x6a:
399                         TRACE("logical volume %d unlocked.\n",drive);
400                         break;
401
402                 case 0x6f:
403                         memset(dataptr+1, '\0', dataptr[0]-1);
404                         dataptr[1] = dataptr[0];
405                         dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
406                         dataptr[3] = 0xFF; /* no physical drive */
407                         break;
408
409                 case 0x72:
410                         /* Trail on error implementation */
411                         SET_AX( context, GetDriveType16(BL_reg(context)) == DRIVE_UNKNOWN ? 0x0f : 0x01 );
412                         SET_CFLAG(context);     /* Seems to be set all the time */
413                         break;
414
415                 default:
416                         INT_BARF( context, 0x21 );
417         }
418         return FALSE;
419 }
420
421 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
422 {
423     char *filename =
424         CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
425     char *fcb =
426         CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
427     char *s;
428     WCHAR *buffer;
429     WCHAR fcbW[12];
430     INT buffer_len, len;
431
432     SET_AL( context, 0xff ); /* failed */
433
434     TRACE("filename: '%s'\n", filename);
435
436     s = filename;
437     len = 0;
438     while (*s)
439     {
440         if ((*s != ' ') && (*s != '\r') && (*s != '\n'))
441         {
442             s++;
443             len++;
444         }
445         else
446             break;
447     }
448
449     buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
450     buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
451     len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
452     buffer[len] = 0;
453     DOSFS_ToDosFCBFormat(buffer, fcbW);
454     HeapFree(GetProcessHeap(), 0, buffer);
455     WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
456     *fcb = 0;
457     TRACE("FCB: '%s'\n", fcb + 1);
458
459     SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
460
461     /* point DS:SI to first unparsed character */
462     SET_SI( context, context->Esi + (int)s - (int)filename );
463 }
464
465
466 /* Many calls translate a drive argument like this:
467    drive number (00h = default, 01h = A:, etc)
468    */
469 static char drivestring[]="default";
470
471 char *INT21_DriveName(int drive)
472 {
473
474     if(drive >0)
475       {
476         drivestring[0]= (unsigned char)drive + '@';
477         drivestring[1]=':';
478         drivestring[2]=0;
479       }
480     return drivestring;
481 }
482 static BOOL INT21_CreateFile( CONTEXT86 *context )
483 {
484     SET_AX( context, _lcreat16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
485                                                    context->Edx ), CX_reg(context) ) );
486     return (AX_reg(context) == (WORD)HFILE_ERROR16);
487 }
488
489 static HFILE16 _lcreat16_uniq( LPCSTR path, INT attr )
490 {
491     /* Mask off all flags not explicitly allowed by the doc */
492     attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
493     return Win32HandleToDosFileHandle( CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
494                                              FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
495                                              CREATE_NEW, attr, 0 ));
496 }
497
498 static void OpenExistingFile( CONTEXT86 *context )
499 {
500     SET_AX( context, _lopen16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
501                                AL_reg(context) ));
502     if (AX_reg(context) == (WORD)HFILE_ERROR16)
503     {
504         SET_AX( context, GetLastError() );
505         SET_CFLAG(context);
506     }
507 }
508
509 static BOOL INT21_ExtendedOpenCreateFile(CONTEXT86 *context )
510 {
511   BOOL bExtendedError = FALSE;
512   BYTE action = DL_reg(context);
513
514   /* Shuffle arguments to call OpenExistingFile */
515   SET_AL( context, BL_reg(context) );
516   SET_DX( context, SI_reg(context) );
517   /* BX,CX and DX should be preserved */
518   OpenExistingFile(context);
519
520   if ((context->EFlags & 0x0001) == 0) /* File exists */
521   {
522       UINT16    uReturnCX = 0;
523
524       /* Now decide what do do */
525
526       if ((action & 0x07) == 0)
527       {
528           _lclose16( AX_reg(context) );
529           SET_AX( context, 0x0050 );    /*File exists*/
530           SET_CFLAG(context);
531           WARN("extended open/create: failed because file exists \n");
532       }
533       else if ((action & 0x07) == 2)
534       {
535         /* Truncate it, but first check if opened for write */
536         if ((BL_reg(context) & 0x0007)== 0)
537         {
538             _lclose16( AX_reg(context) );
539             WARN("extended open/create: failed, trunc on ro file\n");
540             SET_AX( context, 0x000C );  /*Access code invalid*/
541             SET_CFLAG(context);
542         }
543         else
544         {
545                 TRACE("extended open/create: Closing before truncate\n");
546                 if (_lclose16( AX_reg(context) ))
547                 {
548                    WARN("extended open/create: close before trunc failed\n");
549                    SET_AX( context, 0x0019 );   /*Seek Error*/
550                    SET_CX( context, 0 );
551                    SET_CFLAG(context);
552                 }
553                 /* Shuffle arguments to call CreateFile */
554
555                 TRACE("extended open/create: Truncating\n");
556                 SET_AL( context, BL_reg(context) );
557                 /* CX is still the same */
558                 SET_DX( context, SI_reg(context) );
559                 bExtendedError = INT21_CreateFile(context);
560
561                 if (context->EFlags & 0x0001)   /*no file open, flags set */
562                 {
563                     WARN("extended open/create: trunc failed\n");
564                     return bExtendedError;
565                 }
566                 uReturnCX = 0x3;
567         }
568       }
569       else uReturnCX = 0x1;
570
571       SET_CX( context, uReturnCX );
572   }
573   else /* file does not exist */
574   {
575       RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
576       if ((action & 0xF0)== 0)
577       {
578         SET_CX( context, 0 );
579         SET_CFLAG(context);
580         WARN("extended open/create: failed, file dosen't exist\n");
581       }
582       else
583       {
584         /* Shuffle arguments to call CreateFile */
585         TRACE("extended open/create: Creating\n");
586         SET_AL( context, BL_reg(context) );
587         /* CX should still be the same */
588         SET_DX( context, SI_reg(context) );
589         bExtendedError = INT21_CreateFile(context);
590         if (context->EFlags & 0x0001)  /*no file open, flags set */
591         {
592             WARN("extended open/create: create failed\n");
593             return bExtendedError;
594         }
595         SET_CX( context, 2 );
596       }
597   }
598
599   return bExtendedError;
600 }
601
602
603 static BOOL INT21_ChangeDir( CONTEXT86 *context )
604 {
605     int drive;
606     char *dirname = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
607     WCHAR dirnameW[MAX_PATH];
608
609     TRACE("changedir %s\n", dirname);
610     if (dirname[0] && (dirname[1] == ':'))
611     {
612         drive = toupper(dirname[0]) - 'A';
613         dirname += 2;
614     }
615     else drive = DRIVE_GetCurrentDrive();
616     MultiByteToWideChar(CP_OEMCP, 0, dirname, -1, dirnameW, MAX_PATH);
617     return DRIVE_Chdir( drive, dirnameW );
618 }
619
620
621 static int INT21_FindFirst( CONTEXT86 *context )
622 {
623     char *p;
624     const char *path;
625     DOS_FULL_NAME full_name;
626     FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
627     WCHAR pathW[MAX_PATH];
628     WCHAR maskW[12];
629
630     path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
631     MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
632
633     dta->unixPath = NULL;
634     if (!DOSFS_GetFullName( pathW, FALSE, &full_name ))
635     {
636         SET_AX( context, GetLastError() );
637         SET_CFLAG(context);
638         return 0;
639     }
640     dta->unixPath = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.long_name)+1 );
641     strcpy( dta->unixPath, full_name.long_name );
642     p = strrchr( dta->unixPath, '/' );
643     *p = '\0';
644
645     MultiByteToWideChar(CP_OEMCP, 0, p + 1, -1, pathW, MAX_PATH);
646
647     /* Note: terminating NULL in dta->mask overwrites dta->search_attr
648      *       (doesn't matter as it is set below anyway)
649      */
650     if (!DOSFS_ToDosFCBFormat( pathW, maskW ))
651     {
652         HeapFree( GetProcessHeap(), 0, dta->unixPath );
653         dta->unixPath = NULL;
654         SetLastError( ERROR_FILE_NOT_FOUND );
655         SET_AX( context, ERROR_FILE_NOT_FOUND );
656         SET_CFLAG(context);
657         return 0;
658     }
659     WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
660     dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
661                                                : DRIVE_GetCurrentDrive();
662     dta->count = 0;
663     dta->search_attr = CL_reg(context);
664     return 1;
665 }
666
667
668 static int INT21_FindNext( CONTEXT86 *context )
669 {
670     FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
671     WIN32_FIND_DATAA entry;
672     int count;
673
674     if (!dta->unixPath) return 0;
675     if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
676                                   dta->search_attr, dta->count, &entry )))
677     {
678         HeapFree( GetProcessHeap(), 0, dta->unixPath );
679         dta->unixPath = NULL;
680         return 0;
681     }
682     if ((int)dta->count + count > 0xffff)
683     {
684         WARN("Too many directory entries in %s\n", dta->unixPath );
685         HeapFree( GetProcessHeap(), 0, dta->unixPath );
686         dta->unixPath = NULL;
687         return 0;
688     }
689     dta->count += count;
690     dta->fileattr = entry.dwFileAttributes;
691     dta->filesize = entry.nFileSizeLow;
692     FileTimeToDosDateTime( &entry.ftLastWriteTime,
693                            &dta->filedate, &dta->filetime );
694     strcpy( dta->filename, entry.cAlternateFileName );
695     if (!memchr(dta->mask,'?',11)) {
696         /* wildcardless search, release resources in case no findnext will
697          * be issued, and as a workaround in case file creation messes up
698          * findnext, as sometimes happens with pkunzip */
699         HeapFree( GetProcessHeap(), 0, dta->unixPath );
700         dta->unixPath = NULL;
701     }
702     return 1;
703 }
704
705
706 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
707 {
708     static int counter = 0;
709     char *name = CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx );
710     char *p = name + strlen(name);
711
712     /* despite what Ralf Brown says, some programs seem to call without
713      * ending backslash (DOS accepts that, so we accept it too) */
714     if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
715
716     for (;;)
717     {
718         sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
719         counter = (counter + 1) % 1000;
720
721         if ((SET_AX( context, _lcreat16_uniq( name, 0 ))) != (WORD)HFILE_ERROR16)
722         {
723             TRACE("created %s\n", name );
724             return TRUE;
725         }
726         if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
727     }
728 }
729
730
731 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context )
732 {
733     int drive = DOS_GET_DRIVE( DL_reg(context) );
734     char *ptr = (char *)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Esi );
735
736     if (!DRIVE_IsValid(drive))
737     {
738         SetLastError( ERROR_INVALID_DRIVE );
739         return FALSE;
740     }
741     WideCharToMultiByte(CP_OEMCP, 0, DRIVE_GetDosCwd(drive), -1, ptr, 64, NULL, NULL);
742     ptr[63] = 0; /* ensure 0 termination */
743     SET_AX( context, 0x0100 );                       /* success return code */
744     return TRUE;
745 }
746
747
748 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
749 {
750     BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
751     int drive = DOS_GET_DRIVE( BL_reg(context) );
752
753     if (!DRIVE_IsValid(drive))
754     {
755         SetLastError( ERROR_INVALID_DRIVE );
756         return 0;
757     }
758
759     *(WORD *)dataptr = 0;
760     *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
761     memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
762     strncpy(dataptr + 0x11, "FAT16   ", 8);
763     return 1;
764 }
765
766
767 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
768 {
769     BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
770     int drive = DOS_GET_DRIVE( BL_reg(context) );
771
772     if (!DRIVE_IsValid(drive))
773     {
774         SetLastError( ERROR_INVALID_DRIVE );
775         return 0;
776     }
777
778     DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
779     return 1;
780 }
781
782
783 /* microsoft's programmers should be shot for using CP/M style int21
784    calls in Windows for Workgroup's winfile.exe */
785
786 static int INT21_FindFirstFCB( CONTEXT86 *context )
787 {
788     BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
789     FINDFILE_FCB *pFCB;
790     LPCSTR root, cwd;
791     int drive;
792
793     if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
794     else pFCB = (FINDFILE_FCB *)fcb;
795     drive = DOS_GET_DRIVE( pFCB->drive );
796     if (!DRIVE_IsValid( drive )) return 0;
797     root = DRIVE_GetRoot( drive );
798     cwd  = DRIVE_GetUnixCwd( drive );
799     pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
800                                 strlen(root)+strlen(cwd)+2 );
801     if (!pFCB->unixPath) return 0;
802     strcpy( pFCB->unixPath, root );
803     strcat( pFCB->unixPath, "/" );
804     strcat( pFCB->unixPath, cwd );
805     pFCB->count = 0;
806     return 1;
807 }
808
809
810 static int INT21_FindNextFCB( CONTEXT86 *context )
811 {
812     BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
813     FINDFILE_FCB *pFCB;
814     DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA(context);
815     WIN32_FIND_DATAA entry;
816     BYTE attr;
817     int count;
818
819     if (*fcb == 0xff) /* extended FCB ? */
820     {
821         attr = fcb[6];
822         pFCB = (FINDFILE_FCB *)(fcb + 7);
823     }
824     else
825     {
826         attr = 0;
827         pFCB = (FINDFILE_FCB *)fcb;
828     }
829
830     if (!pFCB->unixPath) return 0;
831     if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
832                                   DOS_GET_DRIVE( pFCB->drive ), attr,
833                                   pFCB->count, &entry )))
834     {
835         HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
836         pFCB->unixPath = NULL;
837         return 0;
838     }
839     pFCB->count += count;
840
841     if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
842         *(BYTE *)pResult = 0xff;
843         (BYTE *)pResult +=6; /* leave reserved field behind */
844         *(BYTE *)pResult = entry.dwFileAttributes;
845         ((BYTE *)pResult)++;
846     }
847     *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
848     ((BYTE *)pResult)++;
849     pResult->fileattr = entry.dwFileAttributes;
850     pResult->cluster  = 0;  /* what else? */
851     pResult->filesize = entry.nFileSizeLow;
852     memset( pResult->reserved, 0, sizeof(pResult->reserved) );
853     FileTimeToDosDateTime( &entry.ftLastWriteTime,
854                            &pResult->filedate, &pResult->filetime );
855
856     /* Convert file name to FCB format */
857
858     memset( pResult->filename, ' ', sizeof(pResult->filename) );
859     if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
860     else if (!strcmp( entry.cAlternateFileName, ".." ))
861         pResult->filename[0] = pResult->filename[1] = '.';
862     else
863     {
864         char *p = strrchr( entry.cAlternateFileName, '.' );
865         if (p && p[1] && (p != entry.cAlternateFileName))
866         {
867             memcpy( pResult->filename, entry.cAlternateFileName,
868                     min( (p - entry.cAlternateFileName), 8 ) );
869             memcpy( pResult->filename + 8, p + 1, min( strlen(p), 3 ) );
870         }
871         else
872             memcpy( pResult->filename, entry.cAlternateFileName,
873                     min( strlen(entry.cAlternateFileName), 8 ) );
874     }
875     return 1;
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             SET_AX( 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             SET_AX( context, GetLastError() );
906             SET_CFLAG(context);
907           }
908           return;
909         default:
910           SET_AX( 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                SET_CH( context, 1 ); /* Valid */
935                SET_CL( 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
948 static void ASPI_DOS_HandleInt( CONTEXT86 *context )
949 {
950     if (!Dosvm.ASPIHandler && !DPMI_LoadDosSystem())
951     {
952         ERR("could not setup ASPI handler\n");
953         return;
954     }
955     Dosvm.ASPIHandler( context );
956 }
957
958
959 /***********************************************************************
960  *           INT_Int21Handler
961  */
962 void WINAPI INT_Int21Handler( CONTEXT86 *context )
963 {
964     BOOL        bSetDOSExtendedError = FALSE;
965
966     switch(AH_reg(context))
967     {
968     case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
969         TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
970               context->SegDs,DX_reg(context) );
971         {
972             LPSTR data = CTX_SEG_OFF_TO_LIN(context,context->SegDs,context->Edx);
973             LPSTR p = data;
974             /* do NOT use strchr() to calculate the string length,
975             as '\0' is valid string content, too !
976             Maybe we should check for non-'$' strings, but DOS doesn't. */
977             while (*p != '$') p++;
978             _hwrite16( 1, data, (int)p - (int)data);
979             SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
980         }
981         break;
982
983     case 0x0a: /* BUFFERED INPUT */
984       {
985         char *buffer = ((char *)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
986                                                    context->Edx ));
987         int res;
988
989         TRACE("BUFFERED INPUT (size=%d)\n",buffer[0]);
990         if (buffer[1])
991           TRACE("Handle old chars in buffer!\n");
992         res=_lread16( 0, buffer+2,buffer[0]);
993         buffer[1]=res;
994         if(buffer[res+1] == '\n')
995           buffer[res+1] = '\r';
996         break;
997       }
998
999     case 0x5c: /* "FLOCK" - RECORD LOCKING */
1000         fLock(context);
1001         break;
1002
1003     case 0x0e: /* SELECT DEFAULT DRIVE */
1004         TRACE("SELECT DEFAULT DRIVE %d\n", DL_reg(context));
1005         DRIVE_SetCurrentDrive( DL_reg(context) );
1006         SET_AL( context, MAX_DOS_DRIVES );
1007         break;
1008
1009     case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1010         TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
1011               CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1012         if (!INT21_FindFirstFCB(context))
1013         {
1014             SET_AL( context, 0xff );
1015             break;
1016         }
1017         /* else fall through */
1018
1019     case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1020         SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
1021         break;
1022
1023     case 0x19: /* GET CURRENT DEFAULT DRIVE */
1024         SET_AL( context, DRIVE_GetCurrentDrive() );
1025         break;
1026
1027     case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1028         {
1029             TDB *pTask = TASK_GetCurrent();
1030             pTask->dta = MAKESEGPTR(context->SegDs,DX_reg(context));
1031             TRACE("Set DTA: %08lx\n", pTask->dta);
1032         }
1033         break;
1034
1035     case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1036         SET_DL( context, 0 );
1037         if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
1038         break;
1039
1040     case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1041         if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
1042         break;
1043
1044     case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1045         GetDrivePB(context, DRIVE_GetCurrentDrive());
1046         break;
1047
1048     case 0x29: /* PARSE FILENAME INTO FCB */
1049         INT21_ParseFileNameIntoFCB(context);
1050         break;
1051
1052     case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1053         TRACE("GET DISK TRANSFER AREA ADDRESS\n");
1054         {
1055             TDB *pTask = TASK_GetCurrent();
1056             context->SegEs = SELECTOROF( pTask->dta );
1057             SET_BX( context, OFFSETOF( pTask->dta ) );
1058         }
1059         break;
1060
1061     case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1062         TRACE("GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
1063               INT21_DriveName( DL_reg(context)));
1064         GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
1065         break;
1066
1067     case 0x33: /* MULTIPLEXED */
1068         switch (AL_reg(context))
1069         {
1070               case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1071                 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
1072                 INT21_ReadConfigSys();
1073                 SET_DL( context, DOSCONF_config.brk_flag );
1074                 break;
1075
1076               case 0x01: /* SET EXTENDED BREAK STATE */
1077                 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
1078                 INT21_ReadConfigSys();
1079                 DOSCONF_config.brk_flag = (DL_reg(context) > 0);
1080                 break;
1081
1082               case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1083                 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
1084                 INT21_ReadConfigSys();
1085                 /* ugly coding in order to stay reentrant */
1086                 if (DL_reg(context))
1087                 {
1088                     SET_DL( context, DOSCONF_config.brk_flag );
1089                     DOSCONF_config.brk_flag = 1;
1090                 }
1091                 else
1092                 {
1093                     SET_DL( context, DOSCONF_config.brk_flag );
1094                     DOSCONF_config.brk_flag = 0;
1095                 }
1096                 break;
1097
1098               case 0x05: /* GET BOOT DRIVE */
1099                 TRACE("GET BOOT DRIVE\n");
1100                 SET_DL( context, 3 );
1101                 /* c: is Wine's bootdrive (a: is 1)*/
1102                 break;
1103
1104               case 0x06: /* GET TRUE VERSION NUMBER */
1105                 TRACE("GET TRUE VERSION NUMBER\n");
1106                 SET_BX( context, (HIWORD(GetVersion16() >> 8)) | (HIWORD(GetVersion16() << 8)) );
1107                 SET_DX( context, 0x00 );
1108                 break;
1109
1110               default:
1111                 INT_BARF( context, 0x21 );
1112                 break;
1113         }
1114         break;
1115
1116     case 0x36: /* GET FREE DISK SPACE */
1117         TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
1118               INT21_DriveName( DL_reg(context)));
1119         if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
1120         break;
1121
1122     case 0x37:
1123       {
1124         unsigned char switchchar='/';
1125         switch (AL_reg(context))
1126         {
1127         case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
1128           TRACE("SWITCHAR - GET SWITCH CHARACTER\n");
1129           SET_AL( context, 0x00 ); /* success*/
1130           SET_DL( context, switchchar );
1131           break;
1132         case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
1133           TRACE("SWITCHAR - SET SWITCH CHARACTER\n");
1134           switchchar = DL_reg(context);
1135           SET_AL( context, 0x00 ); /* success*/
1136           break;
1137         default: /*"AVAILDEV" - SPECIFY \DEV\ PREFIX USE*/
1138           INT_BARF( context, 0x21 );
1139           break;
1140         }
1141         break;
1142       }
1143
1144     case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1145         TRACE("MKDIR %s\n",
1146               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1147         bSetDOSExtendedError = (!CreateDirectory16( CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1148                                                            context->Edx ), NULL));
1149         /* FIXME: CreateDirectory's LastErrors will clash with the ones
1150          * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1151          * denied), while CreateDirectory return several ones. remap some of
1152          * them. -Marcus
1153          */
1154         if (bSetDOSExtendedError) {
1155                 switch (GetLastError()) {
1156                 case ERROR_ALREADY_EXISTS:
1157                 case ERROR_FILENAME_EXCED_RANGE:
1158                 case ERROR_DISK_FULL:
1159                         SetLastError(ERROR_ACCESS_DENIED);
1160                         break;
1161                 default: break;
1162                 }
1163         }
1164         break;
1165
1166     case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1167         TRACE("RMDIR %s\n",
1168               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1169         bSetDOSExtendedError = (!RemoveDirectory16( CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1170                                                                  context->Edx )));
1171         break;
1172
1173     case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1174         TRACE("CHDIR %s\n",
1175               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1176         bSetDOSExtendedError = !INT21_ChangeDir(context);
1177         break;
1178
1179     case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1180         TRACE("CREAT flag 0x%02x %s\n",CX_reg(context),
1181               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1182         bSetDOSExtendedError = INT21_CreateFile( context );
1183         break;
1184
1185     case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1186         TRACE("OPEN mode 0x%02x %s\n",AL_reg(context),
1187               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1188         OpenExistingFile(context);
1189         break;
1190
1191     case 0x3e: /* "CLOSE" - CLOSE FILE */
1192         TRACE("CLOSE handle %d\n",BX_reg(context));
1193         SET_AX( context, _lclose16( BX_reg(context) ));
1194         bSetDOSExtendedError = (AX_reg(context) != 0);
1195         break;
1196
1197     case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1198         TRACE("READ from %d to %04lX:%04X for %d byte\n",BX_reg(context),
1199               context->SegDs,DX_reg(context),CX_reg(context) );
1200         {
1201             LONG result;
1202             if (ISV86(context))
1203                 result = _hread16( BX_reg(context),
1204                                    CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1205                                                                context->Edx ),
1206                                    CX_reg(context) );
1207             else
1208                 result = WIN16_hread( BX_reg(context),
1209                                       MAKESEGPTR( context->SegDs, context->Edx ),
1210                                       CX_reg(context) );
1211             if (result == -1) bSetDOSExtendedError = TRUE;
1212             else SET_AX( context, (WORD)result );
1213         }
1214         break;
1215
1216     case 0x41: /* "UNLINK" - DELETE FILE */
1217         TRACE("UNLINK %s\n",
1218               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1219         bSetDOSExtendedError = (!DeleteFileA( CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1220                                                              context->Edx )));
1221         break;
1222
1223     case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1224         TRACE("LSEEK handle %d offset %ld from %s\n",
1225               BX_reg(context), MAKELONG(DX_reg(context),CX_reg(context)),
1226               (AL_reg(context)==0)?"start of file":(AL_reg(context)==1)?
1227               "current file position":"end of file");
1228         {
1229             LONG status = _llseek16( BX_reg(context),
1230                                      MAKELONG(DX_reg(context),CX_reg(context)),
1231                                      AL_reg(context) );
1232             if (status == -1) bSetDOSExtendedError = TRUE;
1233             else
1234             {
1235                 SET_AX( context, LOWORD(status) );
1236                 SET_DX( context, HIWORD(status) );
1237             }
1238         }
1239         break;
1240
1241     case 0x43: /* FILE ATTRIBUTES */
1242         switch (AL_reg(context))
1243         {
1244         case 0x00:
1245             TRACE("GET FILE ATTRIBUTES for %s\n",
1246                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1247             SET_AX( context, GetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1248                                                                     context->Edx)));
1249             if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1250             else SET_CX( context, AX_reg(context) );
1251             break;
1252
1253         case 0x01:
1254             TRACE("SET FILE ATTRIBUTES 0x%02x for %s\n", CX_reg(context),
1255                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1256             bSetDOSExtendedError =
1257                 (!SetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1258                                                            context->Edx),
1259                                                            CX_reg(context) ));
1260             break;
1261         case 0x02:
1262             FIXME("GET COMPRESSED FILE SIZE for %s stub\n",
1263                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1264         }
1265         break;
1266
1267     case 0x44: /* IOCTL */
1268         switch (AL_reg(context))
1269         {
1270         case 0x00:
1271             ioctlGetDeviceInfo(context);
1272             break;
1273
1274         case 0x01:
1275             break;
1276         case 0x02:{
1277             const DOS_DEVICE *dev;
1278             static const WCHAR scsimgrW[] = {'S','C','S','I','M','G','R','$',0};
1279             if ((dev = DOSFS_GetDeviceByHandle( DosFileHandleToWin32Handle(BX_reg(context)) )) &&
1280                 !strcmpiW( dev->name, scsimgrW ))
1281             {
1282                 ASPI_DOS_HandleInt(context);
1283             }
1284             break;
1285        }
1286         case 0x05:{     /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1287             /*BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);*/
1288             int drive = DOS_GET_DRIVE(BL_reg(context));
1289
1290             FIXME("program tried to write to block device control channel of drive %d:\n",drive);
1291             /* for (i=0;i<CX_reg(context);i++)
1292                 fprintf(stdnimp,"%02x ",dataptr[i]);
1293             fprintf(stdnimp,"\n");*/
1294             SET_AX( context, context->Ecx );
1295             break;
1296         }
1297         case 0x08:   /* Check if drive is removable. */
1298             TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOVABLE for drive %s\n",
1299                  INT21_DriveName( BL_reg(context)));
1300             switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1301             {
1302             case DRIVE_UNKNOWN:
1303                 SetLastError( ERROR_INVALID_DRIVE );
1304                 SET_AX( context, ERROR_INVALID_DRIVE );
1305                 SET_CFLAG(context);
1306                 break;
1307             case DRIVE_REMOVABLE:
1308                 SET_AX( context, 0 );      /* removable */
1309                 break;
1310             default:
1311                 SET_AX( context, 1 );   /* not removable */
1312                 break;
1313             }
1314             break;
1315
1316         case 0x09:   /* CHECK IF BLOCK DEVICE REMOTE */
1317             TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOTE for drive %s\n",
1318                  INT21_DriveName( BL_reg(context)));
1319             switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1320             {
1321             case DRIVE_UNKNOWN:
1322                 SetLastError( ERROR_INVALID_DRIVE );
1323                 SET_AX( context, ERROR_INVALID_DRIVE );
1324                 SET_CFLAG(context);
1325                 break;
1326             case DRIVE_REMOTE:
1327                 SET_DX( context, (1<<9) | (1<<12) );  /* remote */
1328                 break;
1329             default:
1330                 SET_DX( context, 0 );  /* FIXME: use driver attr here */
1331                 break;
1332             }
1333             break;
1334
1335         case 0x0a: /* check if handle (BX) is remote */
1336             TRACE("IOCTL - CHECK IF HANDLE %d IS REMOTE\n",BX_reg(context));
1337             /* returns DX, bit 15 set if remote, bit 14 set if date/time
1338              * not set on close
1339              */
1340             SET_DX( context, 0 );
1341             break;
1342
1343         case 0x0d:
1344             TRACE("IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
1345                   INT21_DriveName( BL_reg(context)));
1346             bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1347             break;
1348
1349         case 0x0e: /* get logical drive mapping */
1350             TRACE("IOCTL - GET LOGICAL DRIVE MAP for drive %s\n",
1351                   INT21_DriveName( BL_reg(context)));
1352             SET_AL( context, 0 ); /* drive has no mapping - FIXME: may be wrong*/
1353             break;
1354
1355         case 0x0F:   /* Set logical drive mapping */
1356             {
1357             int drive;
1358             TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
1359                   INT21_DriveName( BL_reg(context)));
1360             drive = DOS_GET_DRIVE ( BL_reg(context) );
1361             if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
1362             {
1363                 SET_CFLAG(context);
1364                 SET_AX( context, 0x000F );  /* invalid drive */
1365             }
1366             break;
1367             }
1368
1369         case 0xe0:  /* Sun PC-NFS API */
1370             /* not installed */
1371             break;
1372
1373         case 0x52:  /* DR-DOS version */
1374             /* This is not DR-DOS */
1375
1376             TRACE("GET DR-DOS VERSION requested\n");
1377
1378             SET_AX( context, 0x0001 ); /* Invalid function */
1379             SET_CFLAG(context);       /* Error */
1380             SetLastError( ERROR_INVALID_FUNCTION );
1381             break;
1382
1383         default:
1384             INT_BARF( context, 0x21 );
1385             break;
1386         }
1387         break;
1388
1389     case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1390         TRACE("FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
1391               BX_reg(context),CX_reg(context));
1392         bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR16);
1393         break;
1394
1395     case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1396         TRACE("CWD - GET CURRENT DIRECTORY for drive %s\n",
1397               INT21_DriveName( DL_reg(context)));
1398         bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1399         break;
1400
1401     case 0x4a: /* RESIZE MEMORY BLOCK */
1402         TRACE("RESIZE MEMORY segment %04lX to %d paragraphs\n", context->SegEs, BX_reg(context));
1403         if (!ISV86(context))
1404           FIXME("RESIZE MEMORY probably insufficient implementation. Expect crash soon\n");
1405         {
1406             LPVOID *mem = DOSMEM_ResizeBlock(DOSMEM_MapDosToLinear(context->SegEs<<4),
1407                                              BX_reg(context)<<4,NULL);
1408             if (mem)
1409                 SET_AX( context, DOSMEM_MapLinearToDos(mem)>>4 );
1410             else {
1411                 SET_CFLAG(context);
1412                 SET_AX( context, 0x0008 ); /* insufficient memory */
1413                 SET_BX( context, DOSMEM_Available()>>4 ); /* not quite right */
1414             }
1415         }
1416         break;
1417
1418     case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1419         TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
1420               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1421         if (!INT21_FindFirst(context)) break;
1422         /* fall through */
1423
1424     case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1425         TRACE("FINDNEXT\n");
1426         if (!INT21_FindNext(context))
1427         {
1428             SetLastError( ERROR_NO_MORE_FILES );
1429             SET_AX( context, ERROR_NO_MORE_FILES );
1430             SET_CFLAG(context);
1431         }
1432         else SET_AX( context, 0 );  /* OK */
1433         break;
1434
1435     case 0x56: /* "RENAME" - RENAME FILE */
1436         TRACE("RENAME %s to %s\n",
1437               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1438               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi));
1439         bSetDOSExtendedError =
1440                 (!MoveFileA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1441                                CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi)));
1442         break;
1443
1444     case 0x5a: /* CREATE TEMPORARY FILE */
1445         TRACE("CREATE TEMPORARY FILE\n");
1446         bSetDOSExtendedError = !INT21_CreateTempFile(context);
1447         break;
1448
1449     case 0x5b: /* CREATE NEW FILE */
1450         TRACE("CREATE NEW FILE 0x%02x for %s\n", CX_reg(context),
1451               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1452         SET_AX( context,
1453                _lcreat16_uniq( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1454                                CX_reg(context) ));
1455         bSetDOSExtendedError = (AX_reg(context) != 0);
1456         break;
1457
1458     case 0x5e:
1459         bSetDOSExtendedError = INT21_networkfunc (context);
1460         break;
1461
1462     case 0x5f: /* NETWORK */
1463         switch (AL_reg(context))
1464         {
1465         case 0x07: /* ENABLE DRIVE */
1466             TRACE("ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1467             if (!DRIVE_Enable( DL_reg(context) ))
1468             {
1469                 SetLastError( ERROR_INVALID_DRIVE );
1470                 bSetDOSExtendedError = TRUE;
1471             }
1472             break;
1473
1474         case 0x08: /* DISABLE DRIVE */
1475             TRACE("DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1476             if (!DRIVE_Disable( DL_reg(context) ))
1477             {
1478                 SetLastError( ERROR_INVALID_DRIVE );
1479                 bSetDOSExtendedError = TRUE;
1480             }
1481             break;
1482
1483         default:
1484             /* network software not installed */
1485             TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
1486             SetLastError( ER_NoNetwork );
1487             bSetDOSExtendedError = TRUE;
1488             break;
1489         }
1490         break;
1491
1492     case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1493         TRACE("TRUENAME %s\n",
1494               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
1495         {
1496             if (!GetFullPathNameA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1497                                                         context->Esi), 128,
1498                                      CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1499                                                         context->Edi),NULL))
1500                 bSetDOSExtendedError = TRUE;
1501             else SET_AX( context, 0 );
1502         }
1503         break;
1504
1505     case 0x69: /* DISK SERIAL NUMBER */
1506         switch (AL_reg(context))
1507         {
1508         case 0x00:
1509             TRACE("GET DISK SERIAL NUMBER for drive %s\n",
1510                   INT21_DriveName(BL_reg(context)));
1511             if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1512             else SET_AX( context, 0 );
1513             break;
1514
1515         case 0x01:
1516             TRACE("SET DISK SERIAL NUMBER for drive %s\n",
1517                   INT21_DriveName(BL_reg(context)));
1518             if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1519             else SET_AX( context, 1 );
1520             break;
1521         }
1522         break;
1523
1524     case 0x6C: /* Extended Open/Create*/
1525         TRACE("EXTENDED OPEN/CREATE %s\n",
1526               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edi));
1527         bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1528         break;
1529
1530     case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
1531         if ((GetVersion()&0xC0000004)!=0xC0000004) {
1532             /* not supported on anything but Win95 */
1533             TRACE("LONG FILENAME functions supported only by win95\n");
1534             SET_CFLAG(context);
1535             SET_AL( context, 0 );
1536         } else
1537         switch(AL_reg(context))
1538         {
1539         case 0x39:  /* Create directory */
1540             TRACE("LONG FILENAME - MAKE DIRECTORY %s\n",
1541                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,context->Edx));
1542             bSetDOSExtendedError = (!CreateDirectoryA(
1543                                         CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1544                                                   context->Edx ), NULL));
1545             /* FIXME: CreateDirectory's LastErrors will clash with the ones
1546              * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1547              * denied), while CreateDirectory return several ones. remap some of
1548              * them. -Marcus
1549              */
1550             if (bSetDOSExtendedError) {
1551                     switch (GetLastError()) {
1552                     case ERROR_ALREADY_EXISTS:
1553                     case ERROR_FILENAME_EXCED_RANGE:
1554                     case ERROR_DISK_FULL:
1555                             SetLastError(ERROR_ACCESS_DENIED);
1556                             break;
1557                     default: break;
1558                     }
1559             }
1560             break;
1561         case 0x3a:  /* Remove directory */
1562             TRACE("LONG FILENAME - REMOVE DIRECTORY %s\n",
1563                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,context->Edx));
1564             bSetDOSExtendedError = (!RemoveDirectoryA(
1565                                         CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1566                                                         context->Edx )));
1567             break;
1568         case 0x43:  /* Get/Set file attributes */
1569           TRACE("LONG FILENAME -EXTENDED GET/SET FILE ATTRIBUTES %s\n",
1570                 (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,context->Edx));
1571         switch (BL_reg(context))
1572         {
1573         case 0x00: /* Get file attributes */
1574             TRACE("\tretrieve attributes\n");
1575             SET_CX( context, GetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1576                                                                     context->Edx)));
1577             if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1578             break;
1579         case 0x01:
1580             TRACE("\tset attributes 0x%04x\n",CX_reg(context));
1581             bSetDOSExtendedError = (!SetFileAttributesA(
1582                                         CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1583                                                            context->Edx),
1584                                         CX_reg(context)  ) );
1585             break;
1586         default:
1587           FIXME("Unimplemented long file name function:\n");
1588           INT_BARF( context, 0x21 );
1589           SET_CFLAG(context);
1590           SET_AL( context, 0 );
1591           break;
1592         }
1593         break;
1594         case 0x47:  /* Get current directory */
1595             TRACE(" LONG FILENAME - GET CURRENT DIRECTORY for drive %s\n",
1596                   INT21_DriveName(DL_reg(context)));
1597             bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1598             break;
1599
1600         case 0x4e:  /* Find first file */
1601             TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
1602                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
1603             /* FIXME: use attributes in CX */
1604             if ((SET_AX( context, FindFirstFile16(
1605                    CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1606                    (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1607                                                           context->Edi))))
1608                 == INVALID_HANDLE_VALUE16)
1609                 bSetDOSExtendedError = TRUE;
1610             break;
1611         case 0x4f:  /* Find next file */
1612             TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
1613                   BX_reg(context));
1614             if (!FindNextFile16( BX_reg(context),
1615                     (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1616                                                              context->Edi)))
1617                 bSetDOSExtendedError = TRUE;
1618             break;
1619         case 0xa0:
1620             {
1621                 LPCSTR driveroot = (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,context->Edx);
1622                 LPSTR buffer = (LPSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegEs,context->Edi);
1623                 DWORD filename_len, flags;
1624
1625                 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", driveroot);
1626                 SET_AX( context, 0 );
1627                 if (!GetVolumeInformationA( driveroot, NULL, 0, NULL, &filename_len,
1628                                             &flags, buffer, 8 ))
1629                 {
1630                     INT_BARF( context, 0x21 );
1631                     SET_CFLAG(context);
1632                     break;
1633                 }
1634                 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
1635                 SET_CX( context, filename_len );
1636                 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
1637             }
1638             break;
1639         case 0xa1:  /* Find close */
1640             TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
1641                   BX_reg(context));
1642             bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
1643             break;
1644         case 0x60:
1645           switch(CL_reg(context))
1646           {
1647             case 0x01:  /* Get short filename or path */
1648               if (!GetShortPathNameA
1649                   ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1650                                        context->Esi),
1651                     CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1652                                        context->Edi), 67))
1653                 bSetDOSExtendedError = TRUE;
1654               else SET_AX( context, 0 );
1655               break;
1656             case 0x02:  /* Get canonical long filename or path */
1657               if (!GetFullPathNameA
1658                   ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1659                                        context->Esi), 128,
1660                     CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1661                                        context->Edi),NULL))
1662                 bSetDOSExtendedError = TRUE;
1663               else SET_AX( context, 0 );
1664               break;
1665             default:
1666               FIXME("Unimplemented long file name function:\n");
1667               INT_BARF( context, 0x21 );
1668               SET_CFLAG(context);
1669               SET_AL( context, 0 );
1670               break;
1671           }
1672           break;
1673         case 0x6c:  /* Create or open file */
1674             TRACE("LONG FILENAME - CREATE OR OPEN FILE %s\n",
1675                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Esi));
1676           /* translate Dos 7 action to Dos 6 action */
1677             bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1678             break;
1679
1680         case 0x3b:  /* Change directory */
1681             TRACE("LONG FILENAME - CHANGE DIRECTORY %s\n",
1682                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1683             if (!SetCurrentDirectoryA(CTX_SEG_OFF_TO_LIN(context,
1684                                                 context->SegDs,
1685                                                 context->Edx
1686                                         ))
1687             ) {
1688                 SET_CFLAG(context);
1689                 SET_AL( context, GetLastError() );
1690             }
1691             break;
1692         case 0x41:  /* Delete file */
1693             TRACE("LONG FILENAME - DELETE FILE %s\n",
1694                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1695             if (!DeleteFileA(CTX_SEG_OFF_TO_LIN(context,
1696                                         context->SegDs,
1697                                         context->Edx)
1698             )) {
1699                 SET_CFLAG(context);
1700                 SET_AL( context, GetLastError() );
1701             }
1702             break;
1703         case 0x56:  /* Move (rename) file */
1704             {
1705                 LPCSTR fn1 = (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx);
1706                 LPCSTR fn2 = (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegEs, context->Edi);
1707                 TRACE("LONG FILENAME - RENAME FILE %s to %s\n", fn1, fn2);
1708                 if (!MoveFileA(fn1, fn2))
1709                 {
1710                     SET_CFLAG(context);
1711                     SET_AL( context, GetLastError() );
1712                 }
1713             }
1714             break;
1715         default:
1716             FIXME("Unimplemented long file name function:\n");
1717             INT_BARF( context, 0x21 );
1718             SET_CFLAG(context);
1719             SET_AL( context, 0 );
1720             break;
1721         }
1722         break;
1723
1724
1725     case 0x73: /* MULTIPLEXED: Win95 OSR2/Win98 FAT32 calls */
1726         TRACE("windows95 function AX %04x\n",
1727                     AX_reg(context));
1728
1729         switch (AL_reg(context))
1730                 {
1731                 case 0x02:      /* Get Extended Drive Parameter Block for specific drive */
1732                                 /* ES:DI points to word with length of data (should be 0x3d) */
1733                         {
1734                                 WORD *buffer;
1735                                 struct EDPB *edpb;
1736                             DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
1737                             char root[] = "A:\\";
1738
1739                                 buffer = (WORD *)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
1740
1741                                 TRACE("Get Extended DPB: linear buffer address is %p\n", buffer);
1742
1743                                 /* validate passed-in buffer lengths */
1744                                 if ((*buffer != 0x3d) || (context->Ecx != 0x3f))
1745                                 {
1746                                         WARN("Get Extended DPB: buffer lengths incorrect\n");
1747                                         WARN("CX = %lx, buffer[0] = %x\n", context->Ecx, *buffer);
1748                                         SET_CFLAG(context);
1749                                         SET_AL( context, 0x18 );                /* bad buffer length */
1750                                 }
1751
1752                                 /* buffer checks out */
1753                                 buffer++;               /* skip over length word now */
1754                                 if (FillInDrivePB( DX_reg(context) ) )
1755                                 {
1756                                         edpb = (struct EDPB *)buffer;
1757
1758                                         /* copy down the old-style DPB portion first */
1759                                         memcpy(&edpb->dpb, &heap->dpb, sizeof(struct DPB));
1760
1761                                         /* now fill in the extended entries */
1762                                         edpb->edpb_flags = 0;
1763                                         edpb->next_edpb = 0;
1764                                         edpb->free_cluster = edpb->free_cluster2 = 0;
1765
1766                                         /* determine free disk space */
1767                                     *root += DOS_GET_DRIVE( DX_reg(context) );
1768                                     GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
1769                               &free_clusters, &total_clusters );
1770
1771                                         edpb->clusters_free = (free_clusters&0xffff);
1772
1773                                         edpb->clusters_free_hi = free_clusters >> 16;
1774                                         edpb->mirroring_flags = 0;
1775                                         edpb->info_sector = 0xffff;
1776                                         edpb->spare_boot_sector = 0xffff;
1777                                         edpb->first_cluster = 0;
1778                                         edpb->max_cluster = total_clusters;
1779                                         edpb->fat_clusters = 32;        /* made-up value */
1780                                         edpb->root_cluster = 0;
1781
1782                                         RESET_CFLAG(context);   /* clear carry */
1783                                         SET_AX( context, 0 );
1784                                 }
1785                                 else
1786                                 {
1787                                     SET_AX( context, 0x00ff );
1788                                     SET_CFLAG(context);
1789                                 }
1790                         }
1791                         break;
1792
1793                 case 0x03:      /* Get Extended free space on drive */
1794                 case 0x04:  /* Set DPB for formatting */
1795                 case 0x05:  /* extended absolute disk read/write */
1796                         FIXME("Unimplemented FAT32 int32 function %04x\n", AX_reg(context));
1797                         SET_CFLAG(context);
1798                         SET_AL( context, 0 );
1799                         break;
1800                 }
1801
1802                 break;
1803
1804     default:
1805         INT_BARF( context, 0x21 );
1806         break;
1807
1808     } /* END OF SWITCH */
1809
1810     if( bSetDOSExtendedError )          /* set general error condition */
1811     {
1812         SET_AX( context, GetLastError() );
1813         SET_CFLAG(context);
1814     }
1815 }