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