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