Move int21 country information handling to winedos.
[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 DeleteFileFCB( CONTEXT86 *context )
881 {
882     FIXME("(%p): stub\n", context);
883 }
884
885 static void RenameFileFCB( CONTEXT86 *context )
886 {
887     FIXME("(%p): stub\n", context);
888 }
889
890
891
892 static void fLock( CONTEXT86 * context )
893 {
894
895     switch ( AX_reg(context) & 0xff )
896     {
897         case 0x00: /* LOCK */
898           TRACE("lock handle %d offset %ld length %ld\n",
899                 BX_reg(context),
900                 MAKELONG(DX_reg(context),CX_reg(context)),
901                 MAKELONG(DI_reg(context),SI_reg(context))) ;
902           if (!LockFile(DosFileHandleToWin32Handle(BX_reg(context)),
903                         MAKELONG(DX_reg(context),CX_reg(context)), 0,
904                         MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
905             SET_AX( context, GetLastError() );
906             SET_CFLAG(context);
907           }
908           break;
909
910         case 0x01: /* UNLOCK */
911           TRACE("unlock handle %d offset %ld length %ld\n",
912                 BX_reg(context),
913                 MAKELONG(DX_reg(context),CX_reg(context)),
914                 MAKELONG(DI_reg(context),SI_reg(context))) ;
915           if (!UnlockFile(DosFileHandleToWin32Handle(BX_reg(context)),
916                           MAKELONG(DX_reg(context),CX_reg(context)), 0,
917                           MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
918             SET_AX( context, GetLastError() );
919             SET_CFLAG(context);
920           }
921           return;
922         default:
923           SET_AX( context, 0x0001 );
924           SET_CFLAG(context);
925           return;
926      }
927 }
928
929 static BOOL
930 INT21_networkfunc (CONTEXT86 *context)
931 {
932      switch (AL_reg(context)) {
933      case 0x00: /* Get machine name. */
934      {
935           char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
936           TRACE("getting machine name to %p\n", dst);
937           if (gethostname (dst, 15))
938           {
939                WARN("failed!\n");
940                SetLastError( ER_NoNetwork );
941                return TRUE;
942           } else {
943                int len = strlen (dst);
944                while (len < 15)
945                     dst[len++] = ' ';
946                dst[15] = 0;
947                SET_CH( context, 1 ); /* Valid */
948                SET_CL( context, 1 ); /* NETbios number??? */
949                TRACE("returning %s\n", debugstr_an (dst, 16));
950                return FALSE;
951           }
952      }
953
954      default:
955           SetLastError( ER_NoNetwork );
956           return TRUE;
957      }
958 }
959
960
961 static void ASPI_DOS_HandleInt( CONTEXT86 *context )
962 {
963     if (!Dosvm.ASPIHandler && !DPMI_LoadDosSystem())
964     {
965         ERR("could not setup ASPI handler\n");
966         return;
967     }
968     Dosvm.ASPIHandler( context );
969 }
970
971
972 /***********************************************************************
973  *           INT_Int21Handler
974  */
975 void WINAPI INT_Int21Handler( CONTEXT86 *context )
976 {
977     BOOL        bSetDOSExtendedError = FALSE;
978
979     switch(AH_reg(context))
980     {
981     case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
982         TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
983               context->SegDs,DX_reg(context) );
984         {
985             LPSTR data = CTX_SEG_OFF_TO_LIN(context,context->SegDs,context->Edx);
986             LPSTR p = data;
987             /* do NOT use strchr() to calculate the string length,
988             as '\0' is valid string content, too !
989             Maybe we should check for non-'$' strings, but DOS doesn't. */
990             while (*p != '$') p++;
991             _hwrite16( 1, data, (int)p - (int)data);
992             SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
993         }
994         break;
995
996     case 0x0a: /* BUFFERED INPUT */
997       {
998         char *buffer = ((char *)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
999                                                    context->Edx ));
1000         int res;
1001
1002         TRACE("BUFFERED INPUT (size=%d)\n",buffer[0]);
1003         if (buffer[1])
1004           TRACE("Handle old chars in buffer!\n");
1005         res=_lread16( 0, buffer+2,buffer[0]);
1006         buffer[1]=res;
1007         if(buffer[res+1] == '\n')
1008           buffer[res+1] = '\r';
1009         break;
1010       }
1011
1012     case 0x5c: /* "FLOCK" - RECORD LOCKING */
1013         fLock(context);
1014         break;
1015
1016     case 0x0e: /* SELECT DEFAULT DRIVE */
1017         TRACE("SELECT DEFAULT DRIVE %d\n", DL_reg(context));
1018         DRIVE_SetCurrentDrive( DL_reg(context) );
1019         SET_AL( context, MAX_DOS_DRIVES );
1020         break;
1021
1022     case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1023         TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
1024               CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1025         if (!INT21_FindFirstFCB(context))
1026         {
1027             SET_AL( context, 0xff );
1028             break;
1029         }
1030         /* else fall through */
1031
1032     case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1033         SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
1034         break;
1035
1036     case 0x13: /* DELETE FILE USING FCB */
1037         DeleteFileFCB(context);
1038         break;
1039
1040     case 0x17: /* RENAME FILE USING FCB */
1041         RenameFileFCB(context);
1042         break;
1043
1044     case 0x19: /* GET CURRENT DEFAULT DRIVE */
1045         SET_AL( context, DRIVE_GetCurrentDrive() );
1046         break;
1047
1048     case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1049         {
1050             TDB *pTask = TASK_GetCurrent();
1051             pTask->dta = MAKESEGPTR(context->SegDs,DX_reg(context));
1052             TRACE("Set DTA: %08lx\n", pTask->dta);
1053         }
1054         break;
1055
1056     case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1057         SET_DL( context, 0 );
1058         if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
1059         break;
1060
1061     case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1062         if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
1063         break;
1064
1065     case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1066         GetDrivePB(context, DRIVE_GetCurrentDrive());
1067         break;
1068
1069     case 0x29: /* PARSE FILENAME INTO FCB */
1070         INT21_ParseFileNameIntoFCB(context);
1071         break;
1072
1073     case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1074         TRACE("GET DISK TRANSFER AREA ADDRESS\n");
1075         {
1076             TDB *pTask = TASK_GetCurrent();
1077             context->SegEs = SELECTOROF( pTask->dta );
1078             SET_BX( context, OFFSETOF( pTask->dta ) );
1079         }
1080         break;
1081
1082     case 0x30: /* GET DOS VERSION */
1083         TRACE("GET DOS VERSION %s requested\n",
1084               (AL_reg(context) == 0x00)?"OEM number":"version flag");
1085         SET_AX( context, (HIWORD(GetVersion16()) >> 8) | (HIWORD(GetVersion16()) << 8) );
1086 #if 0
1087         SET_AH( context, 0x7 );
1088         SET_AL( context, 0xA );
1089 #endif
1090
1091         SET_BX( context, 0x00FF );     /* 0x123456 is Wine's serial # */
1092         SET_CX( context, 0x0000 );
1093         break;
1094
1095     case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1096         TRACE("GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
1097               INT21_DriveName( DL_reg(context)));
1098         GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
1099         break;
1100
1101     case 0x33: /* MULTIPLEXED */
1102         switch (AL_reg(context))
1103         {
1104               case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1105                 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
1106                 INT21_ReadConfigSys();
1107                 SET_DL( context, DOSCONF_config.brk_flag );
1108                 break;
1109
1110               case 0x01: /* SET EXTENDED BREAK STATE */
1111                 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
1112                 INT21_ReadConfigSys();
1113                 DOSCONF_config.brk_flag = (DL_reg(context) > 0);
1114                 break;
1115
1116               case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1117                 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
1118                 INT21_ReadConfigSys();
1119                 /* ugly coding in order to stay reentrant */
1120                 if (DL_reg(context))
1121                 {
1122                     SET_DL( context, DOSCONF_config.brk_flag );
1123                     DOSCONF_config.brk_flag = 1;
1124                 }
1125                 else
1126                 {
1127                     SET_DL( context, DOSCONF_config.brk_flag );
1128                     DOSCONF_config.brk_flag = 0;
1129                 }
1130                 break;
1131
1132               case 0x05: /* GET BOOT DRIVE */
1133                 TRACE("GET BOOT DRIVE\n");
1134                 SET_DL( context, 3 );
1135                 /* c: is Wine's bootdrive (a: is 1)*/
1136                 break;
1137
1138               case 0x06: /* GET TRUE VERSION NUMBER */
1139                 TRACE("GET TRUE VERSION NUMBER\n");
1140                 SET_BX( context, (HIWORD(GetVersion16() >> 8)) | (HIWORD(GetVersion16() << 8)) );
1141                 SET_DX( context, 0x00 );
1142                 break;
1143
1144               default:
1145                 INT_BARF( context, 0x21 );
1146                 break;
1147         }
1148         break;
1149
1150     case 0x36: /* GET FREE DISK SPACE */
1151         TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
1152               INT21_DriveName( DL_reg(context)));
1153         if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
1154         break;
1155
1156     case 0x37:
1157       {
1158         unsigned char switchchar='/';
1159         switch (AL_reg(context))
1160         {
1161         case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
1162           TRACE("SWITCHAR - GET SWITCH CHARACTER\n");
1163           SET_AL( context, 0x00 ); /* success*/
1164           SET_DL( context, switchchar );
1165           break;
1166         case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
1167           TRACE("SWITCHAR - SET SWITCH CHARACTER\n");
1168           switchchar = DL_reg(context);
1169           SET_AL( context, 0x00 ); /* success*/
1170           break;
1171         default: /*"AVAILDEV" - SPECIFY \DEV\ PREFIX USE*/
1172           INT_BARF( context, 0x21 );
1173           break;
1174         }
1175         break;
1176       }
1177
1178     case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1179         TRACE("MKDIR %s\n",
1180               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1181         bSetDOSExtendedError = (!CreateDirectory16( CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1182                                                            context->Edx ), NULL));
1183         /* FIXME: CreateDirectory's LastErrors will clash with the ones
1184          * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1185          * denied), while CreateDirectory return several ones. remap some of
1186          * them. -Marcus
1187          */
1188         if (bSetDOSExtendedError) {
1189                 switch (GetLastError()) {
1190                 case ERROR_ALREADY_EXISTS:
1191                 case ERROR_FILENAME_EXCED_RANGE:
1192                 case ERROR_DISK_FULL:
1193                         SetLastError(ERROR_ACCESS_DENIED);
1194                         break;
1195                 default: break;
1196                 }
1197         }
1198         break;
1199
1200     case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1201         TRACE("RMDIR %s\n",
1202               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1203         bSetDOSExtendedError = (!RemoveDirectory16( CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1204                                                                  context->Edx )));
1205         break;
1206
1207     case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1208         TRACE("CHDIR %s\n",
1209               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1210         bSetDOSExtendedError = !INT21_ChangeDir(context);
1211         break;
1212
1213     case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1214         TRACE("CREAT flag 0x%02x %s\n",CX_reg(context),
1215               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1216         bSetDOSExtendedError = INT21_CreateFile( context );
1217         break;
1218
1219     case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1220         TRACE("OPEN mode 0x%02x %s\n",AL_reg(context),
1221               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1222         OpenExistingFile(context);
1223         break;
1224
1225     case 0x3e: /* "CLOSE" - CLOSE FILE */
1226         TRACE("CLOSE handle %d\n",BX_reg(context));
1227         SET_AX( context, _lclose16( BX_reg(context) ));
1228         bSetDOSExtendedError = (AX_reg(context) != 0);
1229         break;
1230
1231     case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1232         TRACE("READ from %d to %04lX:%04X for %d byte\n",BX_reg(context),
1233               context->SegDs,DX_reg(context),CX_reg(context) );
1234         {
1235             LONG result;
1236             if (ISV86(context))
1237                 result = _hread16( BX_reg(context),
1238                                    CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1239                                                                context->Edx ),
1240                                    CX_reg(context) );
1241             else
1242                 result = WIN16_hread( BX_reg(context),
1243                                       MAKESEGPTR( context->SegDs, context->Edx ),
1244                                       CX_reg(context) );
1245             if (result == -1) bSetDOSExtendedError = TRUE;
1246             else SET_AX( context, (WORD)result );
1247         }
1248         break;
1249
1250     case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1251         TRACE("WRITE from %04lX:%04X to handle %d for %d byte\n",
1252               context->SegDs,DX_reg(context),BX_reg(context),CX_reg(context) );
1253         {
1254             LONG result = _hwrite16( BX_reg(context),
1255                                      CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1256                                                          context->Edx ),
1257                                      CX_reg(context) );
1258             if (result == -1) bSetDOSExtendedError = TRUE;
1259             else SET_AX( context, (WORD)result );
1260         }
1261         break;
1262
1263     case 0x41: /* "UNLINK" - DELETE FILE */
1264         TRACE("UNLINK %s\n",
1265               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1266         bSetDOSExtendedError = (!DeleteFileA( CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1267                                                              context->Edx )));
1268         break;
1269
1270     case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1271         TRACE("LSEEK handle %d offset %ld from %s\n",
1272               BX_reg(context), MAKELONG(DX_reg(context),CX_reg(context)),
1273               (AL_reg(context)==0)?"start of file":(AL_reg(context)==1)?
1274               "current file position":"end of file");
1275         {
1276             LONG status = _llseek16( BX_reg(context),
1277                                      MAKELONG(DX_reg(context),CX_reg(context)),
1278                                      AL_reg(context) );
1279             if (status == -1) bSetDOSExtendedError = TRUE;
1280             else
1281             {
1282                 SET_AX( context, LOWORD(status) );
1283                 SET_DX( context, HIWORD(status) );
1284             }
1285         }
1286         break;
1287
1288     case 0x43: /* FILE ATTRIBUTES */
1289         switch (AL_reg(context))
1290         {
1291         case 0x00:
1292             TRACE("GET FILE ATTRIBUTES for %s\n",
1293                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1294             SET_AX( context, GetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1295                                                                     context->Edx)));
1296             if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1297             else SET_CX( context, AX_reg(context) );
1298             break;
1299
1300         case 0x01:
1301             TRACE("SET FILE ATTRIBUTES 0x%02x for %s\n", CX_reg(context),
1302                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1303             bSetDOSExtendedError =
1304                 (!SetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1305                                                            context->Edx),
1306                                                            CX_reg(context) ));
1307             break;
1308         case 0x02:
1309             FIXME("GET COMPRESSED FILE SIZE for %s stub\n",
1310                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1311         }
1312         break;
1313
1314     case 0x44: /* IOCTL */
1315         switch (AL_reg(context))
1316         {
1317         case 0x00:
1318             ioctlGetDeviceInfo(context);
1319             break;
1320
1321         case 0x01:
1322             break;
1323         case 0x02:{
1324             const DOS_DEVICE *dev;
1325             static const WCHAR scsimgrW[] = {'S','C','S','I','M','G','R','$',0};
1326             if ((dev = DOSFS_GetDeviceByHandle( DosFileHandleToWin32Handle(BX_reg(context)) )) &&
1327                 !strcmpiW( dev->name, scsimgrW ))
1328             {
1329                 ASPI_DOS_HandleInt(context);
1330             }
1331             break;
1332        }
1333         case 0x05:{     /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1334             /*BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);*/
1335             int drive = DOS_GET_DRIVE(BL_reg(context));
1336
1337             FIXME("program tried to write to block device control channel of drive %d:\n",drive);
1338             /* for (i=0;i<CX_reg(context);i++)
1339                 fprintf(stdnimp,"%02x ",dataptr[i]);
1340             fprintf(stdnimp,"\n");*/
1341             SET_AX( context, context->Ecx );
1342             break;
1343         }
1344         case 0x08:   /* Check if drive is removable. */
1345             TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOVABLE for drive %s\n",
1346                  INT21_DriveName( BL_reg(context)));
1347             switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1348             {
1349             case DRIVE_UNKNOWN:
1350                 SetLastError( ERROR_INVALID_DRIVE );
1351                 SET_AX( context, ERROR_INVALID_DRIVE );
1352                 SET_CFLAG(context);
1353                 break;
1354             case DRIVE_REMOVABLE:
1355                 SET_AX( context, 0 );      /* removable */
1356                 break;
1357             default:
1358                 SET_AX( context, 1 );   /* not removable */
1359                 break;
1360             }
1361             break;
1362
1363         case 0x09:   /* CHECK IF BLOCK DEVICE REMOTE */
1364             TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOTE for drive %s\n",
1365                  INT21_DriveName( BL_reg(context)));
1366             switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1367             {
1368             case DRIVE_UNKNOWN:
1369                 SetLastError( ERROR_INVALID_DRIVE );
1370                 SET_AX( context, ERROR_INVALID_DRIVE );
1371                 SET_CFLAG(context);
1372                 break;
1373             case DRIVE_REMOTE:
1374                 SET_DX( context, (1<<9) | (1<<12) );  /* remote */
1375                 break;
1376             default:
1377                 SET_DX( context, 0 );  /* FIXME: use driver attr here */
1378                 break;
1379             }
1380             break;
1381
1382         case 0x0a: /* check if handle (BX) is remote */
1383             TRACE("IOCTL - CHECK IF HANDLE %d IS REMOTE\n",BX_reg(context));
1384             /* returns DX, bit 15 set if remote, bit 14 set if date/time
1385              * not set on close
1386              */
1387             SET_DX( context, 0 );
1388             break;
1389
1390         case 0x0d:
1391             TRACE("IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
1392                   INT21_DriveName( BL_reg(context)));
1393             bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1394             break;
1395
1396         case 0x0e: /* get logical drive mapping */
1397             TRACE("IOCTL - GET LOGICAL DRIVE MAP for drive %s\n",
1398                   INT21_DriveName( BL_reg(context)));
1399             SET_AL( context, 0 ); /* drive has no mapping - FIXME: may be wrong*/
1400             break;
1401
1402         case 0x0F:   /* Set logical drive mapping */
1403             {
1404             int drive;
1405             TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
1406                   INT21_DriveName( BL_reg(context)));
1407             drive = DOS_GET_DRIVE ( BL_reg(context) );
1408             if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
1409             {
1410                 SET_CFLAG(context);
1411                 SET_AX( context, 0x000F );  /* invalid drive */
1412             }
1413             break;
1414             }
1415
1416         case 0xe0:  /* Sun PC-NFS API */
1417             /* not installed */
1418             break;
1419
1420         case 0x52:  /* DR-DOS version */
1421             /* This is not DR-DOS */
1422
1423             TRACE("GET DR-DOS VERSION requested\n");
1424
1425             SET_AX( context, 0x0001 ); /* Invalid function */
1426             SET_CFLAG(context);       /* Error */
1427             SetLastError( ERROR_INVALID_FUNCTION );
1428             break;
1429
1430         default:
1431             INT_BARF( context, 0x21 );
1432             break;
1433         }
1434         break;
1435
1436     case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1437         {
1438             HANDLE handle;
1439             TRACE("DUP - DUPLICATE FILE HANDLE %d\n",BX_reg(context));
1440             if ((bSetDOSExtendedError = !DuplicateHandle( GetCurrentProcess(),
1441                                                           DosFileHandleToWin32Handle(BX_reg(context)),
1442                                                           GetCurrentProcess(), &handle,
1443                                                           0, TRUE, DUPLICATE_SAME_ACCESS )))
1444                 SET_AX( context, HFILE_ERROR16 );
1445             else
1446                 SET_AX( context, Win32HandleToDosFileHandle(handle) );
1447             break;
1448         }
1449
1450     case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1451         TRACE("FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
1452               BX_reg(context),CX_reg(context));
1453         bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR16);
1454         break;
1455
1456     case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1457         TRACE("CWD - GET CURRENT DIRECTORY for drive %s\n",
1458               INT21_DriveName( DL_reg(context)));
1459         bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1460         break;
1461
1462     case 0x4a: /* RESIZE MEMORY BLOCK */
1463         TRACE("RESIZE MEMORY segment %04lX to %d paragraphs\n", context->SegEs, BX_reg(context));
1464         if (!ISV86(context))
1465           FIXME("RESIZE MEMORY probably insufficient implementation. Expect crash soon\n");
1466         {
1467             LPVOID *mem = DOSMEM_ResizeBlock(DOSMEM_MapDosToLinear(context->SegEs<<4),
1468                                              BX_reg(context)<<4,NULL);
1469             if (mem)
1470                 SET_AX( context, DOSMEM_MapLinearToDos(mem)>>4 );
1471             else {
1472                 SET_CFLAG(context);
1473                 SET_AX( context, 0x0008 ); /* insufficient memory */
1474                 SET_BX( context, DOSMEM_Available()>>4 ); /* not quite right */
1475             }
1476         }
1477         break;
1478
1479     case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1480         TRACE("EXEC %s\n", (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx ));
1481         SET_AX( context, WinExec16( CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx ),
1482                                     SW_NORMAL ));
1483         if (AX_reg(context) < 32) SET_CFLAG(context);
1484         break;
1485
1486     case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1487         TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
1488               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1489         if (!INT21_FindFirst(context)) break;
1490         /* fall through */
1491
1492     case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1493         TRACE("FINDNEXT\n");
1494         if (!INT21_FindNext(context))
1495         {
1496             SetLastError( ERROR_NO_MORE_FILES );
1497             SET_AX( context, ERROR_NO_MORE_FILES );
1498             SET_CFLAG(context);
1499         }
1500         else SET_AX( context, 0 );  /* OK */
1501         break;
1502
1503     case 0x56: /* "RENAME" - RENAME FILE */
1504         TRACE("RENAME %s to %s\n",
1505               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1506               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi));
1507         bSetDOSExtendedError =
1508                 (!MoveFileA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1509                                CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi)));
1510         break;
1511
1512     case 0x57: /* FILE DATE AND TIME */
1513         switch (AL_reg(context))
1514         {
1515         case 0x00:  /* Get */
1516             {
1517                 FILETIME filetime;
1518                 TRACE("GET FILE DATE AND TIME for handle %d\n",
1519                       BX_reg(context));
1520                 if (!GetFileTime( DosFileHandleToWin32Handle(BX_reg(context)), NULL, NULL, &filetime ))
1521                      bSetDOSExtendedError = TRUE;
1522                 else
1523                 {
1524                     WORD date, time;
1525                     FileTimeToDosDateTime( &filetime, &date, &time );
1526                     SET_DX( context, date );
1527                     SET_CX( context, time );
1528                 }
1529             }
1530             break;
1531
1532         case 0x01:  /* Set */
1533             {
1534                 FILETIME filetime;
1535                 TRACE("SET FILE DATE AND TIME for handle %d\n",
1536                       BX_reg(context));
1537                 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
1538                                        &filetime );
1539                 bSetDOSExtendedError =
1540                         (!SetFileTime( DosFileHandleToWin32Handle(BX_reg(context)),
1541                                       NULL, NULL, &filetime ));
1542             }
1543             break;
1544         }
1545         break;
1546
1547     case 0x5a: /* CREATE TEMPORARY FILE */
1548         TRACE("CREATE TEMPORARY FILE\n");
1549         bSetDOSExtendedError = !INT21_CreateTempFile(context);
1550         break;
1551
1552     case 0x5b: /* CREATE NEW FILE */
1553         TRACE("CREATE NEW FILE 0x%02x for %s\n", CX_reg(context),
1554               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1555         SET_AX( context,
1556                _lcreat16_uniq( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1557                                CX_reg(context) ));
1558         bSetDOSExtendedError = (AX_reg(context) != 0);
1559         break;
1560
1561     case 0x5d: /* NETWORK */
1562         FIXME("Function 0x%04x not implemented.\n", AX_reg (context));
1563         /* Fix the following while you're at it.  */
1564         SetLastError( ER_NoNetwork );
1565         bSetDOSExtendedError = TRUE;
1566         break;
1567
1568     case 0x5e:
1569         bSetDOSExtendedError = INT21_networkfunc (context);
1570         break;
1571
1572     case 0x5f: /* NETWORK */
1573         switch (AL_reg(context))
1574         {
1575         case 0x07: /* ENABLE DRIVE */
1576             TRACE("ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1577             if (!DRIVE_Enable( DL_reg(context) ))
1578             {
1579                 SetLastError( ERROR_INVALID_DRIVE );
1580                 bSetDOSExtendedError = TRUE;
1581             }
1582             break;
1583
1584         case 0x08: /* DISABLE DRIVE */
1585             TRACE("DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1586             if (!DRIVE_Disable( DL_reg(context) ))
1587             {
1588                 SetLastError( ERROR_INVALID_DRIVE );
1589                 bSetDOSExtendedError = TRUE;
1590             }
1591             break;
1592
1593         default:
1594             /* network software not installed */
1595             TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
1596             SetLastError( ER_NoNetwork );
1597             bSetDOSExtendedError = TRUE;
1598             break;
1599         }
1600         break;
1601
1602     case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1603         TRACE("TRUENAME %s\n",
1604               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
1605         {
1606             if (!GetFullPathNameA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1607                                                         context->Esi), 128,
1608                                      CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1609                                                         context->Edi),NULL))
1610                 bSetDOSExtendedError = TRUE;
1611             else SET_AX( context, 0 );
1612         }
1613         break;
1614
1615     case 0x68: /* "FFLUSH" - COMMIT FILE */
1616     case 0x6a: /* COMMIT FILE */
1617         TRACE("FFLUSH/COMMIT handle %d\n",BX_reg(context));
1618         bSetDOSExtendedError = (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ));
1619         break;
1620
1621     case 0x69: /* DISK SERIAL NUMBER */
1622         switch (AL_reg(context))
1623         {
1624         case 0x00:
1625             TRACE("GET DISK SERIAL NUMBER for drive %s\n",
1626                   INT21_DriveName(BL_reg(context)));
1627             if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1628             else SET_AX( context, 0 );
1629             break;
1630
1631         case 0x01:
1632             TRACE("SET DISK SERIAL NUMBER for drive %s\n",
1633                   INT21_DriveName(BL_reg(context)));
1634             if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1635             else SET_AX( context, 1 );
1636             break;
1637         }
1638         break;
1639
1640     case 0x6C: /* Extended Open/Create*/
1641         TRACE("EXTENDED OPEN/CREATE %s\n",
1642               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edi));
1643         bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1644         break;
1645
1646     case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
1647         if ((GetVersion()&0xC0000004)!=0xC0000004) {
1648             /* not supported on anything but Win95 */
1649             TRACE("LONG FILENAME functions supported only by win95\n");
1650             SET_CFLAG(context);
1651             SET_AL( context, 0 );
1652         } else
1653         switch(AL_reg(context))
1654         {
1655         case 0x39:  /* Create directory */
1656             TRACE("LONG FILENAME - MAKE DIRECTORY %s\n",
1657                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,context->Edx));
1658             bSetDOSExtendedError = (!CreateDirectoryA(
1659                                         CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1660                                                   context->Edx ), NULL));
1661             /* FIXME: CreateDirectory's LastErrors will clash with the ones
1662              * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1663              * denied), while CreateDirectory return several ones. remap some of
1664              * them. -Marcus
1665              */
1666             if (bSetDOSExtendedError) {
1667                     switch (GetLastError()) {
1668                     case ERROR_ALREADY_EXISTS:
1669                     case ERROR_FILENAME_EXCED_RANGE:
1670                     case ERROR_DISK_FULL:
1671                             SetLastError(ERROR_ACCESS_DENIED);
1672                             break;
1673                     default: break;
1674                     }
1675             }
1676             break;
1677         case 0x3a:  /* Remove directory */
1678             TRACE("LONG FILENAME - REMOVE DIRECTORY %s\n",
1679                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,context->Edx));
1680             bSetDOSExtendedError = (!RemoveDirectoryA(
1681                                         CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1682                                                         context->Edx )));
1683             break;
1684         case 0x43:  /* Get/Set file attributes */
1685           TRACE("LONG FILENAME -EXTENDED GET/SET FILE ATTRIBUTES %s\n",
1686                 (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,context->Edx));
1687         switch (BL_reg(context))
1688         {
1689         case 0x00: /* Get file attributes */
1690             TRACE("\tretrieve attributes\n");
1691             SET_CX( context, GetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1692                                                                     context->Edx)));
1693             if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1694             break;
1695         case 0x01:
1696             TRACE("\tset attributes 0x%04x\n",CX_reg(context));
1697             bSetDOSExtendedError = (!SetFileAttributesA(
1698                                         CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1699                                                            context->Edx),
1700                                         CX_reg(context)  ) );
1701             break;
1702         default:
1703           FIXME("Unimplemented long file name function:\n");
1704           INT_BARF( context, 0x21 );
1705           SET_CFLAG(context);
1706           SET_AL( context, 0 );
1707           break;
1708         }
1709         break;
1710         case 0x47:  /* Get current directory */
1711             TRACE(" LONG FILENAME - GET CURRENT DIRECTORY for drive %s\n",
1712                   INT21_DriveName(DL_reg(context)));
1713             bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1714             break;
1715
1716         case 0x4e:  /* Find first file */
1717             TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
1718                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
1719             /* FIXME: use attributes in CX */
1720             if ((SET_AX( context, FindFirstFile16(
1721                    CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1722                    (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1723                                                           context->Edi))))
1724                 == INVALID_HANDLE_VALUE16)
1725                 bSetDOSExtendedError = TRUE;
1726             break;
1727         case 0x4f:  /* Find next file */
1728             TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
1729                   BX_reg(context));
1730             if (!FindNextFile16( BX_reg(context),
1731                     (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1732                                                              context->Edi)))
1733                 bSetDOSExtendedError = TRUE;
1734             break;
1735         case 0xa0:
1736             {
1737                 LPCSTR driveroot = (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,context->Edx);
1738                 LPSTR buffer = (LPSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegEs,context->Edi);
1739                 DWORD filename_len, flags;
1740
1741                 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", driveroot);
1742                 SET_AX( context, 0 );
1743                 if (!GetVolumeInformationA( driveroot, NULL, 0, NULL, &filename_len,
1744                                             &flags, buffer, 8 ))
1745                 {
1746                     INT_BARF( context, 0x21 );
1747                     SET_CFLAG(context);
1748                     break;
1749                 }
1750                 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
1751                 SET_CX( context, filename_len );
1752                 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
1753             }
1754             break;
1755         case 0xa1:  /* Find close */
1756             TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
1757                   BX_reg(context));
1758             bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
1759             break;
1760         case 0x60:
1761           switch(CL_reg(context))
1762           {
1763             case 0x01:  /* Get short filename or path */
1764               if (!GetShortPathNameA
1765                   ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1766                                        context->Esi),
1767                     CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1768                                        context->Edi), 67))
1769                 bSetDOSExtendedError = TRUE;
1770               else SET_AX( context, 0 );
1771               break;
1772             case 0x02:  /* Get canonical long filename or path */
1773               if (!GetFullPathNameA
1774                   ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1775                                        context->Esi), 128,
1776                     CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1777                                        context->Edi),NULL))
1778                 bSetDOSExtendedError = TRUE;
1779               else SET_AX( context, 0 );
1780               break;
1781             default:
1782               FIXME("Unimplemented long file name function:\n");
1783               INT_BARF( context, 0x21 );
1784               SET_CFLAG(context);
1785               SET_AL( context, 0 );
1786               break;
1787           }
1788           break;
1789         case 0x6c:  /* Create or open file */
1790             TRACE("LONG FILENAME - CREATE OR OPEN FILE %s\n",
1791                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Esi));
1792           /* translate Dos 7 action to Dos 6 action */
1793             bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1794             break;
1795
1796         case 0x3b:  /* Change directory */
1797             TRACE("LONG FILENAME - CHANGE DIRECTORY %s\n",
1798                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1799             if (!SetCurrentDirectoryA(CTX_SEG_OFF_TO_LIN(context,
1800                                                 context->SegDs,
1801                                                 context->Edx
1802                                         ))
1803             ) {
1804                 SET_CFLAG(context);
1805                 SET_AL( context, GetLastError() );
1806             }
1807             break;
1808         case 0x41:  /* Delete file */
1809             TRACE("LONG FILENAME - DELETE FILE %s\n",
1810                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1811             if (!DeleteFileA(CTX_SEG_OFF_TO_LIN(context,
1812                                         context->SegDs,
1813                                         context->Edx)
1814             )) {
1815                 SET_CFLAG(context);
1816                 SET_AL( context, GetLastError() );
1817             }
1818             break;
1819         case 0x56:  /* Move (rename) file */
1820             {
1821                 LPCSTR fn1 = (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx);
1822                 LPCSTR fn2 = (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegEs, context->Edi);
1823                 TRACE("LONG FILENAME - RENAME FILE %s to %s\n", fn1, fn2);
1824                 if (!MoveFileA(fn1, fn2))
1825                 {
1826                     SET_CFLAG(context);
1827                     SET_AL( context, GetLastError() );
1828                 }
1829             }
1830             break;
1831         default:
1832             FIXME("Unimplemented long file name function:\n");
1833             INT_BARF( context, 0x21 );
1834             SET_CFLAG(context);
1835             SET_AL( context, 0 );
1836             break;
1837         }
1838         break;
1839
1840     case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
1841     case 0x72: /* MS-DOS 7 (Windows95) - ??? */
1842         TRACE("windows95 function AX %04x\n",
1843                     AX_reg(context));
1844         WARN("        returning unimplemented\n");
1845         SET_CFLAG(context);
1846         SET_AL( context, 0 );
1847         break;
1848
1849     case 0x73: /* MULTIPLEXED: Win95 OSR2/Win98 FAT32 calls */
1850         TRACE("windows95 function AX %04x\n",
1851                     AX_reg(context));
1852
1853         switch (AL_reg(context))
1854                 {
1855                 case 0x02:      /* Get Extended Drive Parameter Block for specific drive */
1856                                 /* ES:DI points to word with length of data (should be 0x3d) */
1857                         {
1858                                 WORD *buffer;
1859                                 struct EDPB *edpb;
1860                             DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
1861                             char root[] = "A:\\";
1862
1863                                 buffer = (WORD *)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
1864
1865                                 TRACE("Get Extended DPB: linear buffer address is %p\n", buffer);
1866
1867                                 /* validate passed-in buffer lengths */
1868                                 if ((*buffer != 0x3d) || (context->Ecx != 0x3f))
1869                                 {
1870                                         WARN("Get Extended DPB: buffer lengths incorrect\n");
1871                                         WARN("CX = %lx, buffer[0] = %x\n", context->Ecx, *buffer);
1872                                         SET_CFLAG(context);
1873                                         SET_AL( context, 0x18 );                /* bad buffer length */
1874                                 }
1875
1876                                 /* buffer checks out */
1877                                 buffer++;               /* skip over length word now */
1878                                 if (FillInDrivePB( DX_reg(context) ) )
1879                                 {
1880                                         edpb = (struct EDPB *)buffer;
1881
1882                                         /* copy down the old-style DPB portion first */
1883                                         memcpy(&edpb->dpb, &heap->dpb, sizeof(struct DPB));
1884
1885                                         /* now fill in the extended entries */
1886                                         edpb->edpb_flags = 0;
1887                                         edpb->next_edpb = 0;
1888                                         edpb->free_cluster = edpb->free_cluster2 = 0;
1889
1890                                         /* determine free disk space */
1891                                     *root += DOS_GET_DRIVE( DX_reg(context) );
1892                                     GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
1893                               &free_clusters, &total_clusters );
1894
1895                                         edpb->clusters_free = (free_clusters&0xffff);
1896
1897                                         edpb->clusters_free_hi = free_clusters >> 16;
1898                                         edpb->mirroring_flags = 0;
1899                                         edpb->info_sector = 0xffff;
1900                                         edpb->spare_boot_sector = 0xffff;
1901                                         edpb->first_cluster = 0;
1902                                         edpb->max_cluster = total_clusters;
1903                                         edpb->fat_clusters = 32;        /* made-up value */
1904                                         edpb->root_cluster = 0;
1905
1906                                         RESET_CFLAG(context);   /* clear carry */
1907                                         SET_AX( context, 0 );
1908                                 }
1909                                 else
1910                                 {
1911                                     SET_AX( context, 0x00ff );
1912                                     SET_CFLAG(context);
1913                                 }
1914                         }
1915                         break;
1916
1917                 case 0x03:      /* Get Extended free space on drive */
1918                 case 0x04:  /* Set DPB for formatting */
1919                 case 0x05:  /* extended absolute disk read/write */
1920                         FIXME("Unimplemented FAT32 int32 function %04x\n", AX_reg(context));
1921                         SET_CFLAG(context);
1922                         SET_AL( context, 0 );
1923                         break;
1924                 }
1925
1926                 break;
1927
1928     case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
1929     case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
1930         break;
1931
1932     default:
1933         INT_BARF( context, 0x21 );
1934         break;
1935
1936     } /* END OF SWITCH */
1937
1938     if( bSetDOSExtendedError )          /* set general error condition */
1939     {
1940         SET_AX( context, GetLastError() );
1941         SET_CFLAG(context);
1942     }
1943 }