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