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