Add stubs for some new WinXP APIs.
[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 BOOL INT21_CreateHeap(void)
137 {
138     if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
139     {
140         WARN("Out of memory\n");
141         return FALSE;
142     }
143     heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
144     dpbsegptr = MAKESEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
145     strcpy(heap->biosdate, "01/01/80");
146     return TRUE;
147 }
148
149 static BYTE *GetCurrentDTA( CONTEXT86 *context )
150 {
151     TDB *pTask = TASK_GetCurrent();
152
153     /* FIXME: This assumes DTA was set correctly! */
154     return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
155                                                 (DWORD)OFFSETOF(pTask->dta) );
156 }
157
158
159 void CreateBPB(int drive, BYTE *data, BOOL16 limited)
160 /* limited == TRUE is used with INT 0x21/0x440d */
161 {
162         if (drive > 1) {
163                 setword(data, 512);
164                 data[2] = 2;
165                 setword(&data[3], 0);
166                 data[5] = 2;
167                 setword(&data[6], 240);
168                 setword(&data[8], 64000);
169                 data[0x0a] = 0xf8;
170                 setword(&data[0x0b], 40);
171                 setword(&data[0x0d], 56);
172                 setword(&data[0x0f], 2);
173                 setword(&data[0x11], 0);
174                 if (!limited) {
175                     setword(&data[0x1f], 800);
176                     data[0x21] = 5;
177                     setword(&data[0x22], 1);
178                 }
179         } else { /* 1.44mb */
180                 setword(data, 512);
181                 data[2] = 2;
182                 setword(&data[3], 0);
183                 data[5] = 2;
184                 setword(&data[6], 240);
185                 setword(&data[8], 2880);
186                 data[0x0a] = 0xf8;
187                 setword(&data[0x0b], 6);
188                 setword(&data[0x0d], 18);
189                 setword(&data[0x0f], 2);
190                 setword(&data[0x11], 0);
191                 if (!limited) {
192                     setword(&data[0x1f], 80);
193                     data[0x21] = 7;
194                     setword(&data[0x22], 2);
195                 }
196         }
197 }
198
199 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
200 {
201     DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
202     char root[] = "A:\\";
203
204     *root += DOS_GET_DRIVE( DL_reg(context) );
205     if (!GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
206                               &free_clusters, &total_clusters )) return 0;
207     SET_AX( context, cluster_sectors );
208     SET_BX( context, free_clusters );
209     SET_CX( context, sector_bytes );
210     SET_DX( context, total_clusters );
211     return 1;
212 }
213
214 static int INT21_GetDriveAllocInfo( CONTEXT86 *context )
215 {
216     if (!INT21_GetFreeDiskSpace( context )) return 0;
217     if (!heap && !INT21_CreateHeap()) return 0;
218     heap->mediaID = 0xf0;
219     context->SegDs = DosHeapHandle;
220     SET_BX( context, (int)&heap->mediaID - (int)heap );
221     return 1;
222 }
223
224 static int FillInDrivePB( int drive )
225 {
226         if(!DRIVE_IsValid(drive))
227         {
228             SetLastError( ERROR_INVALID_DRIVE );
229                         return 0;
230         }
231         else if (heap || INT21_CreateHeap())
232         {
233                 /* FIXME: I have no idea what a lot of this information should
234                  * say or whether it even really matters since we're not allowing
235                  * direct block access.  However, some programs seem to depend on
236                  * getting at least _something_ back from here.  The 'next' pointer
237                  * does worry me, though.  Should we have a complete table of
238                  * separate DPBs per drive?  Probably, but I'm lazy. :-)  -CH
239                  */
240                 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
241                 heap->dpb.sector_size = 512;
242                 heap->dpb.high_sector = 1;
243                 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
244                 heap->dpb.reserved = 0;
245                 heap->dpb.num_FAT = 1;
246                 heap->dpb.dir_entries = 2;
247                 heap->dpb.first_data = 2;
248                 heap->dpb.high_cluster = 64000;
249                 heap->dpb.sectors_in_FAT = 1;
250                 heap->dpb.start_dir = 1;
251                 heap->dpb.driver_head = 0;
252                 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
253                 heap->dpb.access_flag = 0;
254                 heap->dpb.next = 0;
255                 heap->dpb.free_search = 0;
256                 heap->dpb.free_clusters = 0xFFFF;    /* unknown */
257                                 return 1;
258                 }
259
260                 return 0;
261 }
262
263 static void GetDrivePB( CONTEXT86 *context, int drive )
264 {
265         if (FillInDrivePB( drive ))
266         {
267                 SET_AL( context, 0x00 );
268                 context->SegDs = SELECTOROF(dpbsegptr);
269                 SET_BX( context, OFFSETOF(dpbsegptr) );
270         }
271         else
272         {
273         SET_AX( context, 0x00ff );
274         }
275 }
276
277
278 static BOOL ioctlGenericBlkDevReq( CONTEXT86 *context )
279 {
280         BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
281         int drive = DOS_GET_DRIVE( BL_reg(context) );
282
283         if (!DRIVE_IsValid(drive))
284         {
285             SetLastError( ERROR_FILE_NOT_FOUND );
286             return TRUE;
287         }
288
289         if (CH_reg(context) != 0x08)
290         {
291             INT_BARF( context, 0x21 );
292             return FALSE;
293         }
294
295         switch (CL_reg(context))
296         {
297                 case 0x60: /* get device parameters */
298                            /* used by w4wgrp's winfile */
299                         memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
300                         dataptr[0] = 0x04;
301                         dataptr[6] = 0; /* media type */
302                         if (drive > 1)
303                         {
304                                 dataptr[1] = 0x05; /* fixed disk */
305                                 setword(&dataptr[2], 0x01); /* non removable */
306                                 setword(&dataptr[4], 0x300); /* # of cylinders */
307                         }
308                         else
309                         {
310                                 dataptr[1] = 0x07; /* block dev, floppy */
311                                 setword(&dataptr[2], 0x02); /* removable */
312                                 setword(&dataptr[4], 80); /* # of cylinders */
313                         }
314                         CreateBPB(drive, &dataptr[7], TRUE);
315                         RESET_CFLAG(context);
316                         break;
317
318                 case 0x66:/*  get disk serial number */
319                         {
320                                 char    label[12],fsname[9],path[4];
321                                 DWORD   serial;
322
323                                 strcpy(path,"x:\\");path[0]=drive+'A';
324                                 GetVolumeInformationA(
325                                         path,label,12,&serial,NULL,NULL,fsname,9
326                                 );
327                                 *(WORD*)dataptr         = 0;
328                                 memcpy(dataptr+2,&serial,4);
329                                 memcpy(dataptr+6,label  ,11);
330                                 memcpy(dataptr+17,fsname,8);
331                         }
332                         break;
333
334                 case 0x6f:
335                         memset(dataptr+1, '\0', dataptr[0]-1);
336                         dataptr[1] = dataptr[0];
337                         dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
338                         dataptr[3] = 0xFF; /* no physical drive */
339                         break;
340
341                 case 0x72:
342                         /* Trail on error implementation */
343                         SET_AX( context, GetDriveType16(BL_reg(context)) == DRIVE_UNKNOWN ? 0x0f : 0x01 );
344                         SET_CFLAG(context);     /* Seems to be set all the time */
345                         break;
346
347                 default:
348                         INT_BARF( context, 0x21 );
349         }
350         return FALSE;
351 }
352
353 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
354 {
355     char *filename =
356         CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
357     char *fcb =
358         CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
359     char *s;
360     WCHAR *buffer;
361     WCHAR fcbW[12];
362     INT buffer_len, len;
363
364     SET_AL( context, 0xff ); /* failed */
365
366     TRACE("filename: '%s'\n", filename);
367
368     s = filename;
369     len = 0;
370     while (*s)
371     {
372         if ((*s != ' ') && (*s != '\r') && (*s != '\n'))
373         {
374             s++;
375             len++;
376         }
377         else
378             break;
379     }
380
381     buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
382     buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
383     len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
384     buffer[len] = 0;
385     DOSFS_ToDosFCBFormat(buffer, fcbW);
386     HeapFree(GetProcessHeap(), 0, buffer);
387     WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
388     *fcb = 0;
389     TRACE("FCB: '%s'\n", fcb + 1);
390
391     SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
392
393     /* point DS:SI to first unparsed character */
394     SET_SI( context, context->Esi + (int)s - (int)filename );
395 }
396
397
398 /* Many calls translate a drive argument like this:
399    drive number (00h = default, 01h = A:, etc)
400    */
401 static char drivestring[]="default";
402
403 char *INT21_DriveName(int drive)
404 {
405
406     if(drive >0)
407       {
408         drivestring[0]= (unsigned char)drive + '@';
409         drivestring[1]=':';
410         drivestring[2]=0;
411       }
412     return drivestring;
413 }
414 static BOOL INT21_CreateFile( CONTEXT86 *context )
415 {
416     SET_AX( context, _lcreat16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
417                                                    context->Edx ), CX_reg(context) ) );
418     return (AX_reg(context) == (WORD)HFILE_ERROR16);
419 }
420
421 static HFILE16 _lcreat16_uniq( LPCSTR path, INT attr )
422 {
423     /* Mask off all flags not explicitly allowed by the doc */
424     attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
425     return Win32HandleToDosFileHandle( CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
426                                              FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
427                                              CREATE_NEW, attr, 0 ));
428 }
429
430 static void OpenExistingFile( CONTEXT86 *context )
431 {
432     SET_AX( context, _lopen16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
433                                AL_reg(context) ));
434     if (AX_reg(context) == (WORD)HFILE_ERROR16)
435     {
436         SET_AX( context, GetLastError() );
437         SET_CFLAG(context);
438     }
439 }
440
441 static BOOL INT21_ExtendedOpenCreateFile(CONTEXT86 *context )
442 {
443   BOOL bExtendedError = FALSE;
444   BYTE action = DL_reg(context);
445
446   /* Shuffle arguments to call OpenExistingFile */
447   SET_AL( context, BL_reg(context) );
448   SET_DX( context, SI_reg(context) );
449   /* BX,CX and DX should be preserved */
450   OpenExistingFile(context);
451
452   if ((context->EFlags & 0x0001) == 0) /* File exists */
453   {
454       UINT16    uReturnCX = 0;
455
456       /* Now decide what do do */
457
458       if ((action & 0x07) == 0)
459       {
460           _lclose16( AX_reg(context) );
461           SET_AX( context, 0x0050 );    /*File exists*/
462           SET_CFLAG(context);
463           WARN("extended open/create: failed because file exists \n");
464       }
465       else if ((action & 0x07) == 2)
466       {
467         /* Truncate it, but first check if opened for write */
468         if ((BL_reg(context) & 0x0007)== 0)
469         {
470             _lclose16( AX_reg(context) );
471             WARN("extended open/create: failed, trunc on ro file\n");
472             SET_AX( context, 0x000C );  /*Access code invalid*/
473             SET_CFLAG(context);
474         }
475         else
476         {
477                 TRACE("extended open/create: Closing before truncate\n");
478                 if (_lclose16( AX_reg(context) ))
479                 {
480                    WARN("extended open/create: close before trunc failed\n");
481                    SET_AX( context, 0x0019 );   /*Seek Error*/
482                    SET_CX( context, 0 );
483                    SET_CFLAG(context);
484                 }
485                 /* Shuffle arguments to call CreateFile */
486
487                 TRACE("extended open/create: Truncating\n");
488                 SET_AL( context, BL_reg(context) );
489                 /* CX is still the same */
490                 SET_DX( context, SI_reg(context) );
491                 bExtendedError = INT21_CreateFile(context);
492
493                 if (context->EFlags & 0x0001)   /*no file open, flags set */
494                 {
495                     WARN("extended open/create: trunc failed\n");
496                     return bExtendedError;
497                 }
498                 uReturnCX = 0x3;
499         }
500       }
501       else uReturnCX = 0x1;
502
503       SET_CX( context, uReturnCX );
504   }
505   else /* file does not exist */
506   {
507       RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
508       if ((action & 0xF0)== 0)
509       {
510         SET_CX( context, 0 );
511         SET_CFLAG(context);
512         WARN("extended open/create: failed, file dosen't exist\n");
513       }
514       else
515       {
516         /* Shuffle arguments to call CreateFile */
517         TRACE("extended open/create: Creating\n");
518         SET_AL( context, BL_reg(context) );
519         /* CX should still be the same */
520         SET_DX( context, SI_reg(context) );
521         bExtendedError = INT21_CreateFile(context);
522         if (context->EFlags & 0x0001)  /*no file open, flags set */
523         {
524             WARN("extended open/create: create failed\n");
525             return bExtendedError;
526         }
527         SET_CX( context, 2 );
528       }
529   }
530
531   return bExtendedError;
532 }
533
534
535 static BOOL INT21_ChangeDir( CONTEXT86 *context )
536 {
537     int drive;
538     char *dirname = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
539     WCHAR dirnameW[MAX_PATH];
540
541     TRACE("changedir %s\n", dirname);
542     if (dirname[0] && (dirname[1] == ':'))
543     {
544         drive = toupper(dirname[0]) - 'A';
545         dirname += 2;
546     }
547     else drive = DRIVE_GetCurrentDrive();
548     MultiByteToWideChar(CP_OEMCP, 0, dirname, -1, dirnameW, MAX_PATH);
549     return DRIVE_Chdir( drive, dirnameW );
550 }
551
552
553 static int INT21_FindFirst( CONTEXT86 *context )
554 {
555     char *p;
556     const char *path;
557     DOS_FULL_NAME full_name;
558     FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
559     WCHAR pathW[MAX_PATH];
560     WCHAR maskW[12];
561
562     path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
563     MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
564
565     dta->unixPath = NULL;
566     if (!DOSFS_GetFullName( pathW, FALSE, &full_name ))
567     {
568         SET_AX( context, GetLastError() );
569         SET_CFLAG(context);
570         return 0;
571     }
572     dta->unixPath = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.long_name)+1 );
573     strcpy( dta->unixPath, full_name.long_name );
574     p = strrchr( dta->unixPath, '/' );
575     *p = '\0';
576
577     MultiByteToWideChar(CP_OEMCP, 0, p + 1, -1, pathW, MAX_PATH);
578
579     /* Note: terminating NULL in dta->mask overwrites dta->search_attr
580      *       (doesn't matter as it is set below anyway)
581      */
582     if (!DOSFS_ToDosFCBFormat( pathW, maskW ))
583     {
584         HeapFree( GetProcessHeap(), 0, dta->unixPath );
585         dta->unixPath = NULL;
586         SetLastError( ERROR_FILE_NOT_FOUND );
587         SET_AX( context, ERROR_FILE_NOT_FOUND );
588         SET_CFLAG(context);
589         return 0;
590     }
591     WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
592     dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
593                                                : DRIVE_GetCurrentDrive();
594     dta->count = 0;
595     dta->search_attr = CL_reg(context);
596     return 1;
597 }
598
599
600 static int INT21_FindNext( CONTEXT86 *context )
601 {
602     FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
603     WIN32_FIND_DATAA entry;
604     int count;
605
606     if (!dta->unixPath) return 0;
607     if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
608                                   dta->search_attr, dta->count, &entry )))
609     {
610         HeapFree( GetProcessHeap(), 0, dta->unixPath );
611         dta->unixPath = NULL;
612         return 0;
613     }
614     if ((int)dta->count + count > 0xffff)
615     {
616         WARN("Too many directory entries in %s\n", dta->unixPath );
617         HeapFree( GetProcessHeap(), 0, dta->unixPath );
618         dta->unixPath = NULL;
619         return 0;
620     }
621     dta->count += count;
622     dta->fileattr = entry.dwFileAttributes;
623     dta->filesize = entry.nFileSizeLow;
624     FileTimeToDosDateTime( &entry.ftLastWriteTime,
625                            &dta->filedate, &dta->filetime );
626     strcpy( dta->filename, entry.cAlternateFileName );
627     if (!memchr(dta->mask,'?',11)) {
628         /* wildcardless search, release resources in case no findnext will
629          * be issued, and as a workaround in case file creation messes up
630          * findnext, as sometimes happens with pkunzip */
631         HeapFree( GetProcessHeap(), 0, dta->unixPath );
632         dta->unixPath = NULL;
633     }
634     return 1;
635 }
636
637
638 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
639 {
640     static int counter = 0;
641     char *name = CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx );
642     char *p = name + strlen(name);
643
644     /* despite what Ralf Brown says, some programs seem to call without
645      * ending backslash (DOS accepts that, so we accept it too) */
646     if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
647
648     for (;;)
649     {
650         sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
651         counter = (counter + 1) % 1000;
652
653         SET_AX( context, _lcreat16_uniq( name, 0 ) );
654         if (AX_reg(context) != HFILE_ERROR16)
655         {
656             TRACE("created %s\n", name );
657             return TRUE;
658         }
659         if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
660     }
661 }
662
663
664 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context )
665 {
666     int drive = DOS_GET_DRIVE( DL_reg(context) );
667     char *ptr = (char *)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Esi );
668
669     if (!DRIVE_IsValid(drive))
670     {
671         SetLastError( ERROR_INVALID_DRIVE );
672         return FALSE;
673     }
674     WideCharToMultiByte(CP_OEMCP, 0, DRIVE_GetDosCwd(drive), -1, ptr, 64, NULL, NULL);
675     ptr[63] = 0; /* ensure 0 termination */
676     SET_AX( context, 0x0100 );                       /* success return code */
677     return TRUE;
678 }
679
680
681 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
682 {
683     BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
684     int drive = DOS_GET_DRIVE( BL_reg(context) );
685
686     if (!DRIVE_IsValid(drive))
687     {
688         SetLastError( ERROR_INVALID_DRIVE );
689         return 0;
690     }
691
692     *(WORD *)dataptr = 0;
693     *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
694     memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
695     strncpy(dataptr + 0x11, "FAT16   ", 8);
696     return 1;
697 }
698
699
700 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
701 {
702     BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
703     int drive = DOS_GET_DRIVE( BL_reg(context) );
704
705     if (!DRIVE_IsValid(drive))
706     {
707         SetLastError( ERROR_INVALID_DRIVE );
708         return 0;
709     }
710
711     DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
712     return 1;
713 }
714
715
716 /* microsoft's programmers should be shot for using CP/M style int21
717    calls in Windows for Workgroup's winfile.exe */
718
719 static int INT21_FindFirstFCB( CONTEXT86 *context )
720 {
721     BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
722     FINDFILE_FCB *pFCB;
723     LPCSTR root, cwd;
724     int drive;
725
726     if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
727     else pFCB = (FINDFILE_FCB *)fcb;
728     drive = DOS_GET_DRIVE( pFCB->drive );
729     if (!DRIVE_IsValid( drive )) return 0;
730     root = DRIVE_GetRoot( drive );
731     cwd  = DRIVE_GetUnixCwd( drive );
732     pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
733                                 strlen(root)+strlen(cwd)+2 );
734     if (!pFCB->unixPath) return 0;
735     strcpy( pFCB->unixPath, root );
736     strcat( pFCB->unixPath, "/" );
737     strcat( pFCB->unixPath, cwd );
738     pFCB->count = 0;
739     return 1;
740 }
741
742
743 static int INT21_FindNextFCB( CONTEXT86 *context )
744 {
745     BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
746     FINDFILE_FCB *pFCB;
747     DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA(context);
748     WIN32_FIND_DATAA entry;
749     BYTE attr;
750     int count;
751
752     if (*fcb == 0xff) /* extended FCB ? */
753     {
754         attr = fcb[6];
755         pFCB = (FINDFILE_FCB *)(fcb + 7);
756     }
757     else
758     {
759         attr = 0;
760         pFCB = (FINDFILE_FCB *)fcb;
761     }
762
763     if (!pFCB->unixPath) return 0;
764     if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
765                                   DOS_GET_DRIVE( pFCB->drive ), attr,
766                                   pFCB->count, &entry )))
767     {
768         HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
769         pFCB->unixPath = NULL;
770         return 0;
771     }
772     pFCB->count += count;
773
774     if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
775         *(BYTE *)pResult = 0xff;
776         (BYTE *)pResult +=6; /* leave reserved field behind */
777         *(BYTE *)pResult = entry.dwFileAttributes;
778         ((BYTE *)pResult)++;
779     }
780     *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
781     ((BYTE *)pResult)++;
782     pResult->fileattr = entry.dwFileAttributes;
783     pResult->cluster  = 0;  /* what else? */
784     pResult->filesize = entry.nFileSizeLow;
785     memset( pResult->reserved, 0, sizeof(pResult->reserved) );
786     FileTimeToDosDateTime( &entry.ftLastWriteTime,
787                            &pResult->filedate, &pResult->filetime );
788
789     /* Convert file name to FCB format */
790
791     memset( pResult->filename, ' ', sizeof(pResult->filename) );
792     if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
793     else if (!strcmp( entry.cAlternateFileName, ".." ))
794         pResult->filename[0] = pResult->filename[1] = '.';
795     else
796     {
797         char *p = strrchr( entry.cAlternateFileName, '.' );
798         if (p && p[1] && (p != entry.cAlternateFileName))
799         {
800             memcpy( pResult->filename, entry.cAlternateFileName,
801                     min( (p - entry.cAlternateFileName), 8 ) );
802             memcpy( pResult->filename + 8, p + 1, min( strlen(p), 3 ) );
803         }
804         else
805             memcpy( pResult->filename, entry.cAlternateFileName,
806                     min( strlen(entry.cAlternateFileName), 8 ) );
807     }
808     return 1;
809 }
810
811
812 static BOOL
813 INT21_networkfunc (CONTEXT86 *context)
814 {
815      switch (AL_reg(context)) {
816      case 0x00: /* Get machine name. */
817      {
818           char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
819           TRACE("getting machine name to %p\n", dst);
820           if (gethostname (dst, 15))
821           {
822                WARN("failed!\n");
823                SetLastError( ER_NoNetwork );
824                return TRUE;
825           } else {
826                int len = strlen (dst);
827                while (len < 15)
828                     dst[len++] = ' ';
829                dst[15] = 0;
830                SET_CH( context, 1 ); /* Valid */
831                SET_CL( context, 1 ); /* NETbios number??? */
832                TRACE("returning %s\n", debugstr_an (dst, 16));
833                return FALSE;
834           }
835      }
836
837      default:
838           SetLastError( ER_NoNetwork );
839           return TRUE;
840      }
841 }
842
843
844 /***********************************************************************
845  *           INT_Int21Handler
846  */
847 void WINAPI INT_Int21Handler( CONTEXT86 *context )
848 {
849     BOOL        bSetDOSExtendedError = FALSE;
850
851     switch(AH_reg(context))
852     {
853     case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
854         TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
855               CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
856         if (!INT21_FindFirstFCB(context))
857         {
858             SET_AL( context, 0xff );
859             break;
860         }
861         /* else fall through */
862
863     case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
864         SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
865         break;
866
867     case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
868         SET_DL( context, 0 );
869         if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
870         break;
871
872     case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
873         if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
874         break;
875
876     case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
877         GetDrivePB(context, DRIVE_GetCurrentDrive());
878         break;
879
880     case 0x29: /* PARSE FILENAME INTO FCB */
881         INT21_ParseFileNameIntoFCB(context);
882         break;
883
884     case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
885         TRACE("GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
886               INT21_DriveName( DL_reg(context)));
887         GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
888         break;
889
890     case 0x36: /* GET FREE DISK SPACE */
891         TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
892               INT21_DriveName( DL_reg(context)));
893         if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
894         break;
895
896     case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
897         TRACE("CHDIR %s\n",
898               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
899         bSetDOSExtendedError = !INT21_ChangeDir(context);
900         break;
901
902     case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
903         TRACE("CREAT flag 0x%02x %s\n",CX_reg(context),
904               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
905         bSetDOSExtendedError = INT21_CreateFile( context );
906         break;
907
908     case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
909         TRACE("OPEN mode 0x%02x %s\n",AL_reg(context),
910               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
911         OpenExistingFile(context);
912         break;
913
914     case 0x44: /* IOCTL */
915         switch (AL_reg(context))
916         {
917         case 0x0d:
918             TRACE("IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
919                   INT21_DriveName( BL_reg(context)));
920             bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
921             break;
922
923         case 0x0F:   /* Set logical drive mapping */
924             {
925             int drive;
926             TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
927                   INT21_DriveName( BL_reg(context)));
928             drive = DOS_GET_DRIVE ( BL_reg(context) );
929             if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
930             {
931                 SET_CFLAG(context);
932                 SET_AX( context, 0x000F );  /* invalid drive */
933             }
934             break;
935             }
936         }
937         break;
938
939     case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
940         TRACE("CWD - GET CURRENT DIRECTORY for drive %s\n",
941               INT21_DriveName( DL_reg(context)));
942         bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
943         break;
944
945     case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
946         TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
947               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
948         if (!INT21_FindFirst(context)) break;
949         /* fall through */
950
951     case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
952         TRACE("FINDNEXT\n");
953         if (!INT21_FindNext(context))
954         {
955             SetLastError( ERROR_NO_MORE_FILES );
956             SET_AX( context, ERROR_NO_MORE_FILES );
957             SET_CFLAG(context);
958         }
959         else SET_AX( context, 0 );  /* OK */
960         break;
961
962     case 0x5a: /* CREATE TEMPORARY FILE */
963         TRACE("CREATE TEMPORARY FILE\n");
964         bSetDOSExtendedError = !INT21_CreateTempFile(context);
965         break;
966
967     case 0x5b: /* CREATE NEW FILE */
968         TRACE("CREATE NEW FILE 0x%02x for %s\n", CX_reg(context),
969               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
970         SET_AX( context,
971                _lcreat16_uniq( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
972                                CX_reg(context) ));
973         bSetDOSExtendedError = (AX_reg(context) != 0);
974         break;
975
976     case 0x5e:
977         bSetDOSExtendedError = INT21_networkfunc (context);
978         break;
979
980     case 0x5f: /* NETWORK */
981         switch (AL_reg(context))
982         {
983         case 0x07: /* ENABLE DRIVE */
984             TRACE("ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
985             if (!DRIVE_Enable( DL_reg(context) ))
986             {
987                 SetLastError( ERROR_INVALID_DRIVE );
988                 bSetDOSExtendedError = TRUE;
989             }
990             break;
991
992         case 0x08: /* DISABLE DRIVE */
993             TRACE("DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
994             if (!DRIVE_Disable( DL_reg(context) ))
995             {
996                 SetLastError( ERROR_INVALID_DRIVE );
997                 bSetDOSExtendedError = TRUE;
998             }
999             break;
1000
1001         default:
1002             /* network software not installed */
1003             TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
1004             SetLastError( ER_NoNetwork );
1005             bSetDOSExtendedError = TRUE;
1006             break;
1007         }
1008         break;
1009
1010     case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1011         TRACE("TRUENAME %s\n",
1012               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
1013         {
1014             if (!GetFullPathNameA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1015                                                         context->Esi), 128,
1016                                      CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1017                                                         context->Edi),NULL))
1018                 bSetDOSExtendedError = TRUE;
1019             else SET_AX( context, 0 );
1020         }
1021         break;
1022
1023     case 0x69: /* DISK SERIAL NUMBER */
1024         switch (AL_reg(context))
1025         {
1026         case 0x00:
1027             TRACE("GET DISK SERIAL NUMBER for drive %s\n",
1028                   INT21_DriveName(BL_reg(context)));
1029             if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1030             else SET_AX( context, 0 );
1031             break;
1032
1033         case 0x01:
1034             TRACE("SET DISK SERIAL NUMBER for drive %s\n",
1035                   INT21_DriveName(BL_reg(context)));
1036             if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1037             else SET_AX( context, 1 );
1038             break;
1039         }
1040         break;
1041
1042     case 0x6C: /* Extended Open/Create*/
1043         TRACE("EXTENDED OPEN/CREATE %s\n",
1044               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edi));
1045         bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1046         break;
1047
1048     case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
1049         switch(AL_reg(context))
1050         {
1051         case 0x47:  /* Get current directory */
1052             TRACE(" LONG FILENAME - GET CURRENT DIRECTORY for drive %s\n",
1053                   INT21_DriveName(DL_reg(context)));
1054             bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1055             break;
1056
1057         case 0x4e:  /* Find first file */
1058             TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
1059                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
1060             /* FIXME: use attributes in CX */
1061             SET_AX( context, FindFirstFile16(
1062                         CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1063                         (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1064                                                                context->Edi)));
1065             if (AX_reg(context) == INVALID_HANDLE_VALUE16)
1066                 bSetDOSExtendedError = TRUE;
1067             break;
1068         case 0x4f:  /* Find next file */
1069             TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
1070                   BX_reg(context));
1071             if (!FindNextFile16( BX_reg(context),
1072                     (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1073                                                              context->Edi)))
1074                 bSetDOSExtendedError = TRUE;
1075             break;
1076         case 0xa0:
1077             {
1078                 LPCSTR driveroot = (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,context->Edx);
1079                 LPSTR buffer = (LPSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegEs,context->Edi);
1080                 DWORD filename_len, flags;
1081
1082                 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", driveroot);
1083                 SET_AX( context, 0 );
1084                 if (!GetVolumeInformationA( driveroot, NULL, 0, NULL, &filename_len,
1085                                             &flags, buffer, 8 ))
1086                 {
1087                     INT_BARF( context, 0x21 );
1088                     SET_CFLAG(context);
1089                     break;
1090                 }
1091                 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
1092                 SET_CX( context, filename_len );
1093                 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
1094             }
1095             break;
1096         case 0xa1:  /* Find close */
1097             TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
1098                   BX_reg(context));
1099             bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
1100             break;
1101         case 0x60:
1102           switch(CL_reg(context))
1103           {
1104             case 0x01:  /* Get short filename or path */
1105               if (!GetShortPathNameA
1106                   ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1107                                        context->Esi),
1108                     CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1109                                        context->Edi), 67))
1110                 bSetDOSExtendedError = TRUE;
1111               else SET_AX( context, 0 );
1112               break;
1113             case 0x02:  /* Get canonical long filename or path */
1114               if (!GetFullPathNameA
1115                   ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1116                                        context->Esi), 128,
1117                     CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1118                                        context->Edi),NULL))
1119                 bSetDOSExtendedError = TRUE;
1120               else SET_AX( context, 0 );
1121               break;
1122             default:
1123               FIXME("Unimplemented long file name function:\n");
1124               INT_BARF( context, 0x21 );
1125               SET_CFLAG(context);
1126               SET_AL( context, 0 );
1127               break;
1128           }
1129           break;
1130         case 0x6c:  /* Create or open file */
1131             TRACE("LONG FILENAME - CREATE OR OPEN FILE %s\n",
1132                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Esi));
1133           /* translate Dos 7 action to Dos 6 action */
1134             bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1135             break;
1136
1137         case 0x3b:  /* Change directory */
1138             TRACE("LONG FILENAME - CHANGE DIRECTORY %s\n",
1139                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1140             if (!SetCurrentDirectoryA(CTX_SEG_OFF_TO_LIN(context,
1141                                                 context->SegDs,
1142                                                 context->Edx
1143                                         ))
1144             ) {
1145                 SET_CFLAG(context);
1146                 SET_AL( context, GetLastError() );
1147             }
1148             break;
1149
1150         default:
1151             FIXME("Unimplemented long file name function:\n");
1152             INT_BARF( context, 0x21 );
1153             SET_CFLAG(context);
1154             SET_AL( context, 0 );
1155             break;
1156         }
1157         break;
1158
1159
1160     case 0x73: /* MULTIPLEXED: Win95 OSR2/Win98 FAT32 calls */
1161         TRACE("windows95 function AX %04x\n",
1162                     AX_reg(context));
1163
1164         switch (AL_reg(context))
1165                 {
1166                 case 0x02:      /* Get Extended Drive Parameter Block for specific drive */
1167                                 /* ES:DI points to word with length of data (should be 0x3d) */
1168                         {
1169                                 WORD *buffer;
1170                                 struct EDPB *edpb;
1171                             DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
1172                             char root[] = "A:\\";
1173
1174                                 buffer = (WORD *)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
1175
1176                                 TRACE("Get Extended DPB: linear buffer address is %p\n", buffer);
1177
1178                                 /* validate passed-in buffer lengths */
1179                                 if ((*buffer != 0x3d) || (context->Ecx != 0x3f))
1180                                 {
1181                                         WARN("Get Extended DPB: buffer lengths incorrect\n");
1182                                         WARN("CX = %lx, buffer[0] = %x\n", context->Ecx, *buffer);
1183                                         SET_CFLAG(context);
1184                                         SET_AL( context, 0x18 );                /* bad buffer length */
1185                                 }
1186
1187                                 /* buffer checks out */
1188                                 buffer++;               /* skip over length word now */
1189                                 if (FillInDrivePB( DX_reg(context) ) )
1190                                 {
1191                                         edpb = (struct EDPB *)buffer;
1192
1193                                         /* copy down the old-style DPB portion first */
1194                                         memcpy(&edpb->dpb, &heap->dpb, sizeof(struct DPB));
1195
1196                                         /* now fill in the extended entries */
1197                                         edpb->edpb_flags = 0;
1198                                         edpb->next_edpb = 0;
1199                                         edpb->free_cluster = edpb->free_cluster2 = 0;
1200
1201                                         /* determine free disk space */
1202                                     *root += DOS_GET_DRIVE( DX_reg(context) );
1203                                     GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
1204                               &free_clusters, &total_clusters );
1205
1206                                         edpb->clusters_free = (free_clusters&0xffff);
1207
1208                                         edpb->clusters_free_hi = free_clusters >> 16;
1209                                         edpb->mirroring_flags = 0;
1210                                         edpb->info_sector = 0xffff;
1211                                         edpb->spare_boot_sector = 0xffff;
1212                                         edpb->first_cluster = 0;
1213                                         edpb->max_cluster = total_clusters;
1214                                         edpb->fat_clusters = 32;        /* made-up value */
1215                                         edpb->root_cluster = 0;
1216
1217                                         RESET_CFLAG(context);   /* clear carry */
1218                                         SET_AX( context, 0 );
1219                                 }
1220                                 else
1221                                 {
1222                                     SET_AX( context, 0x00ff );
1223                                     SET_CFLAG(context);
1224                                 }
1225                         }
1226                         break;
1227
1228                 case 0x03:      /* Get Extended free space on drive */
1229                 case 0x04:  /* Set DPB for formatting */
1230                 case 0x05:  /* extended absolute disk read/write */
1231                         FIXME("Unimplemented FAT32 int32 function %04x\n", AX_reg(context));
1232                         SET_CFLAG(context);
1233                         SET_AL( context, 0 );
1234                         break;
1235                 }
1236
1237                 break;
1238
1239     default:
1240         INT_BARF( context, 0x21 );
1241         break;
1242
1243     } /* END OF SWITCH */
1244
1245     if( bSetDOSExtendedError )          /* set general error condition */
1246     {
1247         SET_AX( context, GetLastError() );
1248         SET_CFLAG(context);
1249     }
1250 }