Clean up int21 handling. Move error handling to winedos.
[wine] / msdos / int21.c
1 /*
2  * DOS interrupt 21h handler
3  *
4  * Copyright 1993, 1994 Erik Bos
5  * Copyright 1996 Alexandre Julliard
6  * Copyright 1997 Andreas Mohr
7  * Copyright 1998 Uwe Bonnes
8  * Copyright 1998, 1999 Ove Kaaven
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <time.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #ifdef HAVE_SYS_FILE_H
34 # include <sys/file.h>
35 #endif
36 #include <string.h>
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
39 #endif
40 #include <sys/types.h>
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
44 #ifdef HAVE_UTIME_H
45 # include <utime.h>
46 #endif
47 #include <ctype.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winternl.h"
51 #include "wingdi.h"
52 #include "winuser.h" /* SW_NORMAL */
53 #include "wine/winbase16.h"
54 #include "winerror.h"
55 #include "drive.h"
56 #include "file.h"
57 #include "callback.h"
58 #include "msdos.h"
59 #include "miscemu.h"
60 #include "task.h"
61 #include "wine/unicode.h"
62 #include "wine/debug.h"
63
64 WINE_DEFAULT_DEBUG_CHANNEL(int21);
65 #if defined(__svr4__) || defined(_SCO_DS)
66 /* SVR4 DOESNT do locking the same way must implement properly */
67 #define LOCK_EX 0
68 #define LOCK_SH  1
69 #define LOCK_NB  8
70 #endif
71
72
73 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
74
75 /* Define the drive parameter block, as used by int21/1F
76  * and int21/32.  This table can be accessed through the
77  * global 'dpb' pointer, which points into the local dos
78  * heap.
79  */
80 struct DPB
81 {
82     BYTE drive_num;         /* 0=A, etc. */
83     BYTE unit_num;          /* Drive's unit number (?) */
84     WORD sector_size;       /* Sector size in bytes */
85     BYTE high_sector;       /* Highest sector in a cluster */
86     BYTE shift;             /* Shift count (?) */
87     WORD reserved;          /* Number of reserved sectors at start */
88     BYTE num_FAT;           /* Number of FATs */
89     WORD dir_entries;       /* Number of root dir entries */
90     WORD first_data;        /* First data sector */
91     WORD high_cluster;      /* Highest cluster number */
92     WORD sectors_in_FAT;    /* Number of sectors per FAT */
93     WORD start_dir;         /* Starting sector of first dir */
94     DWORD driver_head;      /* Address of device driver header (?) */
95     BYTE media_ID;          /* Media ID */
96     BYTE access_flag;       /* Prev. accessed flag (0=yes,0xFF=no) */
97     DWORD next;             /* Pointer to next DPB in list */
98     WORD free_search;       /* Free cluster search start */
99     WORD free_clusters;     /* Number of free clusters (0xFFFF=unknown) */
100 };
101
102 struct EDPB                     /* FAT32 extended Drive Parameter Block */
103 {                               /* from Ralf Brown's Interrupt List */
104         struct DPB dpb;         /* first 24 bytes = original DPB */
105
106         BYTE edpb_flags;        /* undocumented/unknown flags */
107         DWORD next_edpb;        /* pointer to next EDPB */
108         WORD free_cluster;      /* cluster to start search for free space on write, typically
109                                    the last cluster allocated */
110         WORD clusters_free;     /* number of free clusters on drive or FFFF = unknown */
111         WORD clusters_free_hi;  /* hiword of clusters_free */
112         WORD mirroring_flags;   /* mirroring flags: bit 7 set = do not mirror active FAT */
113                                 /* bits 0-3 = 0-based number of the active FAT */
114         WORD info_sector;       /* sector number of file system info sector, or FFFF for none */
115         WORD spare_boot_sector; /* sector number of backup boot sector, or FFFF for none */
116         DWORD first_cluster;    /* sector number of the first cluster */
117         DWORD max_cluster;      /* sector number of the last cluster */
118         DWORD fat_clusters;     /* number of clusters occupied by FAT */
119         DWORD root_cluster;     /* cluster number of start of root directory */
120         DWORD free_cluster2;    /* same as free_cluster: cluster at which to start
121                                    search for free space when writing */
122
123 };
124
125 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 static void INT21_GetSystemDate( CONTEXT86 *context )
472 {
473     SYSTEMTIME systime;
474     GetLocalTime( &systime );
475     SET_CX( context, systime.wYear );
476     SET_DX( context, (systime.wMonth << 8) | systime.wDay );
477     SET_AX( context, systime.wDayOfWeek );
478 }
479
480 static void INT21_GetSystemTime( CONTEXT86 *context )
481 {
482     SYSTEMTIME systime;
483     GetLocalTime( &systime );
484     SET_CX( context, (systime.wHour << 8) | systime.wMinute );
485     SET_DX( context, (systime.wSecond << 8) | (systime.wMilliseconds / 10) );
486 }
487
488 /* Many calls translate a drive argument like this:
489    drive number (00h = default, 01h = A:, etc)
490    */
491 static char drivestring[]="default";
492
493 char *INT21_DriveName(int drive)
494 {
495
496     if(drive >0)
497       {
498         drivestring[0]= (unsigned char)drive + '@';
499         drivestring[1]=':';
500         drivestring[2]=0;
501       }
502     return drivestring;
503 }
504 static BOOL INT21_CreateFile( CONTEXT86 *context )
505 {
506     SET_AX( context, _lcreat16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
507                                                    context->Edx ), CX_reg(context) ) );
508     return (AX_reg(context) == (WORD)HFILE_ERROR16);
509 }
510
511 static HFILE16 _lcreat16_uniq( LPCSTR path, INT attr )
512 {
513     /* Mask off all flags not explicitly allowed by the doc */
514     attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
515     return Win32HandleToDosFileHandle( CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
516                                              FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
517                                              CREATE_NEW, attr, 0 ));
518 }
519
520 static void OpenExistingFile( CONTEXT86 *context )
521 {
522     SET_AX( context, _lopen16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
523                                AL_reg(context) ));
524     if (AX_reg(context) == (WORD)HFILE_ERROR16)
525     {
526         SET_AX( context, GetLastError() );
527         SET_CFLAG(context);
528     }
529 }
530
531 static BOOL INT21_ExtendedOpenCreateFile(CONTEXT86 *context )
532 {
533   BOOL bExtendedError = FALSE;
534   BYTE action = DL_reg(context);
535
536   /* Shuffle arguments to call OpenExistingFile */
537   SET_AL( context, BL_reg(context) );
538   SET_DX( context, SI_reg(context) );
539   /* BX,CX and DX should be preserved */
540   OpenExistingFile(context);
541
542   if ((context->EFlags & 0x0001) == 0) /* File exists */
543   {
544       UINT16    uReturnCX = 0;
545
546       /* Now decide what do do */
547
548       if ((action & 0x07) == 0)
549       {
550           _lclose16( AX_reg(context) );
551           SET_AX( context, 0x0050 );    /*File exists*/
552           SET_CFLAG(context);
553           WARN("extended open/create: failed because file exists \n");
554       }
555       else if ((action & 0x07) == 2)
556       {
557         /* Truncate it, but first check if opened for write */
558         if ((BL_reg(context) & 0x0007)== 0)
559         {
560             _lclose16( AX_reg(context) );
561             WARN("extended open/create: failed, trunc on ro file\n");
562             SET_AX( context, 0x000C );  /*Access code invalid*/
563             SET_CFLAG(context);
564         }
565         else
566         {
567                 TRACE("extended open/create: Closing before truncate\n");
568                 if (_lclose16( AX_reg(context) ))
569                 {
570                    WARN("extended open/create: close before trunc failed\n");
571                    SET_AX( context, 0x0019 );   /*Seek Error*/
572                    SET_CX( context, 0 );
573                    SET_CFLAG(context);
574                 }
575                 /* Shuffle arguments to call CreateFile */
576
577                 TRACE("extended open/create: Truncating\n");
578                 SET_AL( context, BL_reg(context) );
579                 /* CX is still the same */
580                 SET_DX( context, SI_reg(context) );
581                 bExtendedError = INT21_CreateFile(context);
582
583                 if (context->EFlags & 0x0001)   /*no file open, flags set */
584                 {
585                     WARN("extended open/create: trunc failed\n");
586                     return bExtendedError;
587                 }
588                 uReturnCX = 0x3;
589         }
590       }
591       else uReturnCX = 0x1;
592
593       SET_CX( context, uReturnCX );
594   }
595   else /* file does not exist */
596   {
597       RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
598       if ((action & 0xF0)== 0)
599       {
600         SET_CX( context, 0 );
601         SET_CFLAG(context);
602         WARN("extended open/create: failed, file dosen't exist\n");
603       }
604       else
605       {
606         /* Shuffle arguments to call CreateFile */
607         TRACE("extended open/create: Creating\n");
608         SET_AL( context, BL_reg(context) );
609         /* CX should still be the same */
610         SET_DX( context, SI_reg(context) );
611         bExtendedError = INT21_CreateFile(context);
612         if (context->EFlags & 0x0001)  /*no file open, flags set */
613         {
614             WARN("extended open/create: create failed\n");
615             return bExtendedError;
616         }
617         SET_CX( context, 2 );
618       }
619   }
620
621   return bExtendedError;
622 }
623
624
625 static BOOL INT21_ChangeDir( CONTEXT86 *context )
626 {
627     int drive;
628     char *dirname = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
629     WCHAR dirnameW[MAX_PATH];
630
631     TRACE("changedir %s\n", dirname);
632     if (dirname[0] && (dirname[1] == ':'))
633     {
634         drive = toupper(dirname[0]) - 'A';
635         dirname += 2;
636     }
637     else drive = DRIVE_GetCurrentDrive();
638     MultiByteToWideChar(CP_OEMCP, 0, dirname, -1, dirnameW, MAX_PATH);
639     return DRIVE_Chdir( drive, dirnameW );
640 }
641
642
643 static int INT21_FindFirst( CONTEXT86 *context )
644 {
645     char *p;
646     const char *path;
647     DOS_FULL_NAME full_name;
648     FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
649     WCHAR pathW[MAX_PATH];
650     WCHAR maskW[12];
651
652     path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
653     MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
654
655     dta->unixPath = NULL;
656     if (!DOSFS_GetFullName( pathW, FALSE, &full_name ))
657     {
658         SET_AX( context, GetLastError() );
659         SET_CFLAG(context);
660         return 0;
661     }
662     dta->unixPath = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.long_name)+1 );
663     strcpy( dta->unixPath, full_name.long_name );
664     p = strrchr( dta->unixPath, '/' );
665     *p = '\0';
666
667     MultiByteToWideChar(CP_OEMCP, 0, p + 1, -1, pathW, MAX_PATH);
668
669     /* Note: terminating NULL in dta->mask overwrites dta->search_attr
670      *       (doesn't matter as it is set below anyway)
671      */
672     if (!DOSFS_ToDosFCBFormat( pathW, maskW ))
673     {
674         HeapFree( GetProcessHeap(), 0, dta->unixPath );
675         dta->unixPath = NULL;
676         SetLastError( ERROR_FILE_NOT_FOUND );
677         SET_AX( context, ERROR_FILE_NOT_FOUND );
678         SET_CFLAG(context);
679         return 0;
680     }
681     WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
682     dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
683                                                : DRIVE_GetCurrentDrive();
684     dta->count = 0;
685     dta->search_attr = CL_reg(context);
686     return 1;
687 }
688
689
690 static int INT21_FindNext( CONTEXT86 *context )
691 {
692     FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
693     WIN32_FIND_DATAA entry;
694     int count;
695
696     if (!dta->unixPath) return 0;
697     if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
698                                   dta->search_attr, dta->count, &entry )))
699     {
700         HeapFree( GetProcessHeap(), 0, dta->unixPath );
701         dta->unixPath = NULL;
702         return 0;
703     }
704     if ((int)dta->count + count > 0xffff)
705     {
706         WARN("Too many directory entries in %s\n", dta->unixPath );
707         HeapFree( GetProcessHeap(), 0, dta->unixPath );
708         dta->unixPath = NULL;
709         return 0;
710     }
711     dta->count += count;
712     dta->fileattr = entry.dwFileAttributes;
713     dta->filesize = entry.nFileSizeLow;
714     FileTimeToDosDateTime( &entry.ftLastWriteTime,
715                            &dta->filedate, &dta->filetime );
716     strcpy( dta->filename, entry.cAlternateFileName );
717     if (!memchr(dta->mask,'?',11)) {
718         /* wildcardless search, release resources in case no findnext will
719          * be issued, and as a workaround in case file creation messes up
720          * findnext, as sometimes happens with pkunzip */
721         HeapFree( GetProcessHeap(), 0, dta->unixPath );
722         dta->unixPath = NULL;
723     }
724     return 1;
725 }
726
727
728 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
729 {
730     static int counter = 0;
731     char *name = CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx );
732     char *p = name + strlen(name);
733
734     /* despite what Ralf Brown says, some programs seem to call without
735      * ending backslash (DOS accepts that, so we accept it too) */
736     if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
737
738     for (;;)
739     {
740         sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
741         counter = (counter + 1) % 1000;
742
743         if ((SET_AX( context, _lcreat16_uniq( name, 0 ))) != (WORD)HFILE_ERROR16)
744         {
745             TRACE("created %s\n", name );
746             return TRUE;
747         }
748         if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
749     }
750 }
751
752
753 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context )
754 {
755     int drive = DOS_GET_DRIVE( DL_reg(context) );
756     char *ptr = (char *)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Esi );
757
758     if (!DRIVE_IsValid(drive))
759     {
760         SetLastError( ERROR_INVALID_DRIVE );
761         return FALSE;
762     }
763     WideCharToMultiByte(CP_OEMCP, 0, DRIVE_GetDosCwd(drive), -1, ptr, 64, NULL, NULL);
764     ptr[63] = 0; /* ensure 0 termination */
765     SET_AX( context, 0x0100 );                       /* success return code */
766     return TRUE;
767 }
768
769
770 static void INT21_GetDBCSLeadTable( CONTEXT86 *context )
771 {
772     if (heap || INT21_CreateHeap())
773     { /* return an empty table just as DOS 4.0+ does */
774         context->SegDs = DosHeapHandle;
775         SET_SI( context, (int)&heap->DummyDBCSLeadTable - (int)heap );
776     }
777     else
778     {
779         SET_AX( context, 0x1 ); /* error */
780         SET_CFLAG(context);
781     }
782 }
783
784
785 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
786 {
787     BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
788     int drive = DOS_GET_DRIVE( BL_reg(context) );
789
790     if (!DRIVE_IsValid(drive))
791     {
792         SetLastError( ERROR_INVALID_DRIVE );
793         return 0;
794     }
795
796     *(WORD *)dataptr = 0;
797     *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
798     memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
799     strncpy(dataptr + 0x11, "FAT16   ", 8);
800     return 1;
801 }
802
803
804 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
805 {
806     BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
807     int drive = DOS_GET_DRIVE( BL_reg(context) );
808
809     if (!DRIVE_IsValid(drive))
810     {
811         SetLastError( ERROR_INVALID_DRIVE );
812         return 0;
813     }
814
815     DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
816     return 1;
817 }
818
819
820 /* microsoft's programmers should be shot for using CP/M style int21
821    calls in Windows for Workgroup's winfile.exe */
822
823 static int INT21_FindFirstFCB( CONTEXT86 *context )
824 {
825     BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
826     FINDFILE_FCB *pFCB;
827     LPCSTR root, cwd;
828     int drive;
829
830     if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
831     else pFCB = (FINDFILE_FCB *)fcb;
832     drive = DOS_GET_DRIVE( pFCB->drive );
833     if (!DRIVE_IsValid( drive )) return 0;
834     root = DRIVE_GetRoot( drive );
835     cwd  = DRIVE_GetUnixCwd( drive );
836     pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
837                                 strlen(root)+strlen(cwd)+2 );
838     if (!pFCB->unixPath) return 0;
839     strcpy( pFCB->unixPath, root );
840     strcat( pFCB->unixPath, "/" );
841     strcat( pFCB->unixPath, cwd );
842     pFCB->count = 0;
843     return 1;
844 }
845
846
847 static int INT21_FindNextFCB( CONTEXT86 *context )
848 {
849     BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
850     FINDFILE_FCB *pFCB;
851     DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA(context);
852     WIN32_FIND_DATAA entry;
853     BYTE attr;
854     int count;
855
856     if (*fcb == 0xff) /* extended FCB ? */
857     {
858         attr = fcb[6];
859         pFCB = (FINDFILE_FCB *)(fcb + 7);
860     }
861     else
862     {
863         attr = 0;
864         pFCB = (FINDFILE_FCB *)fcb;
865     }
866
867     if (!pFCB->unixPath) return 0;
868     if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
869                                   DOS_GET_DRIVE( pFCB->drive ), attr,
870                                   pFCB->count, &entry )))
871     {
872         HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
873         pFCB->unixPath = NULL;
874         return 0;
875     }
876     pFCB->count += count;
877
878     if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
879         *(BYTE *)pResult = 0xff;
880         (BYTE *)pResult +=6; /* leave reserved field behind */
881         *(BYTE *)pResult = entry.dwFileAttributes;
882         ((BYTE *)pResult)++;
883     }
884     *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
885     ((BYTE *)pResult)++;
886     pResult->fileattr = entry.dwFileAttributes;
887     pResult->cluster  = 0;  /* what else? */
888     pResult->filesize = entry.nFileSizeLow;
889     memset( pResult->reserved, 0, sizeof(pResult->reserved) );
890     FileTimeToDosDateTime( &entry.ftLastWriteTime,
891                            &pResult->filedate, &pResult->filetime );
892
893     /* Convert file name to FCB format */
894
895     memset( pResult->filename, ' ', sizeof(pResult->filename) );
896     if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
897     else if (!strcmp( entry.cAlternateFileName, ".." ))
898         pResult->filename[0] = pResult->filename[1] = '.';
899     else
900     {
901         char *p = strrchr( entry.cAlternateFileName, '.' );
902         if (p && p[1] && (p != entry.cAlternateFileName))
903         {
904             memcpy( pResult->filename, entry.cAlternateFileName,
905                     min( (p - entry.cAlternateFileName), 8 ) );
906             memcpy( pResult->filename + 8, p + 1, min( strlen(p), 3 ) );
907         }
908         else
909             memcpy( pResult->filename, entry.cAlternateFileName,
910                     min( strlen(entry.cAlternateFileName), 8 ) );
911     }
912     return 1;
913 }
914
915
916 static void DeleteFileFCB( CONTEXT86 *context )
917 {
918     FIXME("(%p): stub\n", context);
919 }
920
921 static void RenameFileFCB( CONTEXT86 *context )
922 {
923     FIXME("(%p): stub\n", context);
924 }
925
926
927
928 static void fLock( CONTEXT86 * context )
929 {
930
931     switch ( AX_reg(context) & 0xff )
932     {
933         case 0x00: /* LOCK */
934           TRACE("lock handle %d offset %ld length %ld\n",
935                 BX_reg(context),
936                 MAKELONG(DX_reg(context),CX_reg(context)),
937                 MAKELONG(DI_reg(context),SI_reg(context))) ;
938           if (!LockFile(DosFileHandleToWin32Handle(BX_reg(context)),
939                         MAKELONG(DX_reg(context),CX_reg(context)), 0,
940                         MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
941             SET_AX( context, GetLastError() );
942             SET_CFLAG(context);
943           }
944           break;
945
946         case 0x01: /* UNLOCK */
947           TRACE("unlock handle %d offset %ld length %ld\n",
948                 BX_reg(context),
949                 MAKELONG(DX_reg(context),CX_reg(context)),
950                 MAKELONG(DI_reg(context),SI_reg(context))) ;
951           if (!UnlockFile(DosFileHandleToWin32Handle(BX_reg(context)),
952                           MAKELONG(DX_reg(context),CX_reg(context)), 0,
953                           MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
954             SET_AX( context, GetLastError() );
955             SET_CFLAG(context);
956           }
957           return;
958         default:
959           SET_AX( context, 0x0001 );
960           SET_CFLAG(context);
961           return;
962      }
963 }
964
965 static BOOL
966 INT21_networkfunc (CONTEXT86 *context)
967 {
968      switch (AL_reg(context)) {
969      case 0x00: /* Get machine name. */
970      {
971           char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
972           TRACE("getting machine name to %p\n", dst);
973           if (gethostname (dst, 15))
974           {
975                WARN("failed!\n");
976                SetLastError( ER_NoNetwork );
977                return TRUE;
978           } else {
979                int len = strlen (dst);
980                while (len < 15)
981                     dst[len++] = ' ';
982                dst[15] = 0;
983                SET_CH( context, 1 ); /* Valid */
984                SET_CL( context, 1 ); /* NETbios number??? */
985                TRACE("returning %s\n", debugstr_an (dst, 16));
986                return FALSE;
987           }
988      }
989
990      default:
991           SetLastError( ER_NoNetwork );
992           return TRUE;
993      }
994 }
995
996
997 static void ASPI_DOS_HandleInt( CONTEXT86 *context )
998 {
999     if (!Dosvm.ASPIHandler && !DPMI_LoadDosSystem())
1000     {
1001         ERR("could not setup ASPI handler\n");
1002         return;
1003     }
1004     Dosvm.ASPIHandler( context );
1005 }
1006
1007
1008 /***********************************************************************
1009  *           INT_Int21Handler
1010  */
1011 void WINAPI INT_Int21Handler( CONTEXT86 *context )
1012 {
1013     BOOL        bSetDOSExtendedError = FALSE;
1014
1015     switch(AH_reg(context))
1016     {
1017     case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
1018         TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
1019               context->SegDs,DX_reg(context) );
1020         {
1021             LPSTR data = CTX_SEG_OFF_TO_LIN(context,context->SegDs,context->Edx);
1022             LPSTR p = data;
1023             /* do NOT use strchr() to calculate the string length,
1024             as '\0' is valid string content, too !
1025             Maybe we should check for non-'$' strings, but DOS doesn't. */
1026             while (*p != '$') p++;
1027             _hwrite16( 1, data, (int)p - (int)data);
1028             SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
1029         }
1030         break;
1031
1032     case 0x0a: /* BUFFERED INPUT */
1033       {
1034         char *buffer = ((char *)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1035                                                    context->Edx ));
1036         int res;
1037
1038         TRACE("BUFFERED INPUT (size=%d)\n",buffer[0]);
1039         if (buffer[1])
1040           TRACE("Handle old chars in buffer!\n");
1041         res=_lread16( 0, buffer+2,buffer[0]);
1042         buffer[1]=res;
1043         if(buffer[res+1] == '\n')
1044           buffer[res+1] = '\r';
1045         break;
1046       }
1047
1048     case 0x5c: /* "FLOCK" - RECORD LOCKING */
1049         fLock(context);
1050         break;
1051
1052     case 0x0e: /* SELECT DEFAULT DRIVE */
1053         TRACE("SELECT DEFAULT DRIVE %d\n", DL_reg(context));
1054         DRIVE_SetCurrentDrive( DL_reg(context) );
1055         SET_AL( context, MAX_DOS_DRIVES );
1056         break;
1057
1058     case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1059         TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
1060               CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1061         if (!INT21_FindFirstFCB(context))
1062         {
1063             SET_AL( context, 0xff );
1064             break;
1065         }
1066         /* else fall through */
1067
1068     case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1069         SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
1070         break;
1071
1072     case 0x13: /* DELETE FILE USING FCB */
1073         DeleteFileFCB(context);
1074         break;
1075
1076     case 0x17: /* RENAME FILE USING FCB */
1077         RenameFileFCB(context);
1078         break;
1079
1080     case 0x19: /* GET CURRENT DEFAULT DRIVE */
1081         SET_AL( context, DRIVE_GetCurrentDrive() );
1082         break;
1083
1084     case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1085         {
1086             TDB *pTask = TASK_GetCurrent();
1087             pTask->dta = MAKESEGPTR(context->SegDs,DX_reg(context));
1088             TRACE("Set DTA: %08lx\n", pTask->dta);
1089         }
1090         break;
1091
1092     case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1093         SET_DL( context, 0 );
1094         if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
1095         break;
1096
1097     case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1098         if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
1099         break;
1100
1101     case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1102         GetDrivePB(context, DRIVE_GetCurrentDrive());
1103         break;
1104
1105     case 0x29: /* PARSE FILENAME INTO FCB */
1106         INT21_ParseFileNameIntoFCB(context);
1107         break;
1108
1109     case 0x2a: /* GET SYSTEM DATE */
1110         INT21_GetSystemDate(context);
1111         break;
1112
1113     case 0x2c: /* GET SYSTEM TIME */
1114         INT21_GetSystemTime(context);
1115         break;
1116
1117     case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1118         TRACE("GET DISK TRANSFER AREA ADDRESS\n");
1119         {
1120             TDB *pTask = TASK_GetCurrent();
1121             context->SegEs = SELECTOROF( pTask->dta );
1122             SET_BX( context, OFFSETOF( pTask->dta ) );
1123         }
1124         break;
1125
1126     case 0x30: /* GET DOS VERSION */
1127         TRACE("GET DOS VERSION %s requested\n",
1128               (AL_reg(context) == 0x00)?"OEM number":"version flag");
1129         SET_AX( context, (HIWORD(GetVersion16()) >> 8) | (HIWORD(GetVersion16()) << 8) );
1130 #if 0
1131         SET_AH( context, 0x7 );
1132         SET_AL( context, 0xA );
1133 #endif
1134
1135         SET_BX( context, 0x00FF );     /* 0x123456 is Wine's serial # */
1136         SET_CX( context, 0x0000 );
1137         break;
1138
1139     case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1140         TRACE("GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
1141               INT21_DriveName( DL_reg(context)));
1142         GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
1143         break;
1144
1145     case 0x33: /* MULTIPLEXED */
1146         switch (AL_reg(context))
1147         {
1148               case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1149                 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
1150                 INT21_ReadConfigSys();
1151                 SET_DL( context, DOSCONF_config.brk_flag );
1152                 break;
1153
1154               case 0x01: /* SET EXTENDED BREAK STATE */
1155                 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
1156                 INT21_ReadConfigSys();
1157                 DOSCONF_config.brk_flag = (DL_reg(context) > 0);
1158                 break;
1159
1160               case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1161                 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
1162                 INT21_ReadConfigSys();
1163                 /* ugly coding in order to stay reentrant */
1164                 if (DL_reg(context))
1165                 {
1166                     SET_DL( context, DOSCONF_config.brk_flag );
1167                     DOSCONF_config.brk_flag = 1;
1168                 }
1169                 else
1170                 {
1171                     SET_DL( context, DOSCONF_config.brk_flag );
1172                     DOSCONF_config.brk_flag = 0;
1173                 }
1174                 break;
1175
1176               case 0x05: /* GET BOOT DRIVE */
1177                 TRACE("GET BOOT DRIVE\n");
1178                 SET_DL( context, 3 );
1179                 /* c: is Wine's bootdrive (a: is 1)*/
1180                 break;
1181
1182               case 0x06: /* GET TRUE VERSION NUMBER */
1183                 TRACE("GET TRUE VERSION NUMBER\n");
1184                 SET_BX( context, (HIWORD(GetVersion16() >> 8)) | (HIWORD(GetVersion16() << 8)) );
1185                 SET_DX( context, 0x00 );
1186                 break;
1187
1188               default:
1189                 INT_BARF( context, 0x21 );
1190                 break;
1191         }
1192         break;
1193
1194     case 0x34: /* GET ADDRESS OF INDOS FLAG */
1195         TRACE("GET ADDRESS OF INDOS FLAG\n");
1196         if (!heap) INT21_CreateHeap();
1197         context->SegEs = DosHeapHandle;
1198         SET_BX( context, (int)&heap->InDosFlag - (int)heap );
1199         break;
1200
1201     case 0x36: /* GET FREE DISK SPACE */
1202         TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
1203               INT21_DriveName( DL_reg(context)));
1204         if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
1205         break;
1206
1207     case 0x37:
1208       {
1209         unsigned char switchchar='/';
1210         switch (AL_reg(context))
1211         {
1212         case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
1213           TRACE("SWITCHAR - GET SWITCH CHARACTER\n");
1214           SET_AL( context, 0x00 ); /* success*/
1215           SET_DL( context, switchchar );
1216           break;
1217         case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
1218           TRACE("SWITCHAR - SET SWITCH CHARACTER\n");
1219           switchchar = DL_reg(context);
1220           SET_AL( context, 0x00 ); /* success*/
1221           break;
1222         default: /*"AVAILDEV" - SPECIFY \DEV\ PREFIX USE*/
1223           INT_BARF( context, 0x21 );
1224           break;
1225         }
1226         break;
1227       }
1228
1229     case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1230         TRACE("MKDIR %s\n",
1231               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1232         bSetDOSExtendedError = (!CreateDirectory16( CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1233                                                            context->Edx ), NULL));
1234         /* FIXME: CreateDirectory's LastErrors will clash with the ones
1235          * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1236          * denied), while CreateDirectory return several ones. remap some of
1237          * them. -Marcus
1238          */
1239         if (bSetDOSExtendedError) {
1240                 switch (GetLastError()) {
1241                 case ERROR_ALREADY_EXISTS:
1242                 case ERROR_FILENAME_EXCED_RANGE:
1243                 case ERROR_DISK_FULL:
1244                         SetLastError(ERROR_ACCESS_DENIED);
1245                         break;
1246                 default: break;
1247                 }
1248         }
1249         break;
1250
1251     case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1252         TRACE("RMDIR %s\n",
1253               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1254         bSetDOSExtendedError = (!RemoveDirectory16( CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1255                                                                  context->Edx )));
1256         break;
1257
1258     case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1259         TRACE("CHDIR %s\n",
1260               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1261         bSetDOSExtendedError = !INT21_ChangeDir(context);
1262         break;
1263
1264     case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1265         TRACE("CREAT flag 0x%02x %s\n",CX_reg(context),
1266               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1267         bSetDOSExtendedError = INT21_CreateFile( context );
1268         break;
1269
1270     case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1271         TRACE("OPEN mode 0x%02x %s\n",AL_reg(context),
1272               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1273         OpenExistingFile(context);
1274         break;
1275
1276     case 0x3e: /* "CLOSE" - CLOSE FILE */
1277         TRACE("CLOSE handle %d\n",BX_reg(context));
1278         bSetDOSExtendedError = ((SET_AX( context, _lclose16( BX_reg(context) )) != 0) );
1279         break;
1280
1281     case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1282         TRACE("READ from %d to %04lX:%04X for %d byte\n",BX_reg(context),
1283               context->SegDs,DX_reg(context),CX_reg(context) );
1284         {
1285             LONG result;
1286             if (ISV86(context))
1287                 result = _hread16( BX_reg(context),
1288                                    CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1289                                                                context->Edx ),
1290                                    CX_reg(context) );
1291             else
1292                 result = WIN16_hread( BX_reg(context),
1293                                       MAKESEGPTR( context->SegDs, context->Edx ),
1294                                       CX_reg(context) );
1295             if (result == -1) bSetDOSExtendedError = TRUE;
1296             else SET_AX( context, (WORD)result );
1297         }
1298         break;
1299
1300     case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1301         TRACE("WRITE from %04lX:%04X to handle %d for %d byte\n",
1302               context->SegDs,DX_reg(context),BX_reg(context),CX_reg(context) );
1303         {
1304             LONG result = _hwrite16( BX_reg(context),
1305                                      CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1306                                                          context->Edx ),
1307                                      CX_reg(context) );
1308             if (result == -1) bSetDOSExtendedError = TRUE;
1309             else SET_AX( context, (WORD)result );
1310         }
1311         break;
1312
1313     case 0x41: /* "UNLINK" - DELETE FILE */
1314         TRACE("UNLINK %s\n",
1315               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1316         bSetDOSExtendedError = (!DeleteFileA( CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1317                                                              context->Edx )));
1318         break;
1319
1320     case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1321         TRACE("LSEEK handle %d offset %ld from %s\n",
1322               BX_reg(context), MAKELONG(DX_reg(context),CX_reg(context)),
1323               (AL_reg(context)==0)?"start of file":(AL_reg(context)==1)?
1324               "current file position":"end of file");
1325         {
1326             LONG status = _llseek16( BX_reg(context),
1327                                      MAKELONG(DX_reg(context),CX_reg(context)),
1328                                      AL_reg(context) );
1329             if (status == -1) bSetDOSExtendedError = TRUE;
1330             else
1331             {
1332                 SET_AX( context, LOWORD(status) );
1333                 SET_DX( context, HIWORD(status) );
1334             }
1335         }
1336         break;
1337
1338     case 0x43: /* FILE ATTRIBUTES */
1339         switch (AL_reg(context))
1340         {
1341         case 0x00:
1342             TRACE("GET FILE ATTRIBUTES for %s\n",
1343                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1344             SET_AX( context, GetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1345                                                                     context->Edx)));
1346             if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1347             else SET_CX( context, AX_reg(context) );
1348             break;
1349
1350         case 0x01:
1351             TRACE("SET FILE ATTRIBUTES 0x%02x for %s\n", CX_reg(context),
1352                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1353             bSetDOSExtendedError =
1354                 (!SetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1355                                                            context->Edx),
1356                                                            CX_reg(context) ));
1357             break;
1358         case 0x02:
1359             FIXME("GET COMPRESSED FILE SIZE for %s stub\n",
1360                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1361         }
1362         break;
1363
1364     case 0x44: /* IOCTL */
1365         switch (AL_reg(context))
1366         {
1367         case 0x00:
1368             ioctlGetDeviceInfo(context);
1369             break;
1370
1371         case 0x01:
1372             break;
1373         case 0x02:{
1374             const DOS_DEVICE *dev;
1375             static const WCHAR scsimgrW[] = {'S','C','S','I','M','G','R','$',0};
1376             if ((dev = DOSFS_GetDeviceByHandle( DosFileHandleToWin32Handle(BX_reg(context)) )) &&
1377                 !strcmpiW( dev->name, scsimgrW ))
1378             {
1379                 ASPI_DOS_HandleInt(context);
1380             }
1381             break;
1382        }
1383         case 0x05:{     /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1384             /*BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);*/
1385             int drive = DOS_GET_DRIVE(BL_reg(context));
1386
1387             FIXME("program tried to write to block device control channel of drive %d:\n",drive);
1388             /* for (i=0;i<CX_reg(context);i++)
1389                 fprintf(stdnimp,"%02x ",dataptr[i]);
1390             fprintf(stdnimp,"\n");*/
1391             SET_AX( context, context->Ecx );
1392             break;
1393         }
1394         case 0x08:   /* Check if drive is removable. */
1395             TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOVABLE for drive %s\n",
1396                  INT21_DriveName( BL_reg(context)));
1397             switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1398             {
1399             case DRIVE_UNKNOWN:
1400                 SetLastError( ERROR_INVALID_DRIVE );
1401                 SET_AX( context, ERROR_INVALID_DRIVE );
1402                 SET_CFLAG(context);
1403                 break;
1404             case DRIVE_REMOVABLE:
1405                 SET_AX( context, 0 );      /* removable */
1406                 break;
1407             default:
1408                 SET_AX( context, 1 );   /* not removable */
1409                 break;
1410             }
1411             break;
1412
1413         case 0x09:   /* CHECK IF BLOCK DEVICE REMOTE */
1414             TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOTE for drive %s\n",
1415                  INT21_DriveName( BL_reg(context)));
1416             switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1417             {
1418             case DRIVE_UNKNOWN:
1419                 SetLastError( ERROR_INVALID_DRIVE );
1420                 SET_AX( context, ERROR_INVALID_DRIVE );
1421                 SET_CFLAG(context);
1422                 break;
1423             case DRIVE_REMOTE:
1424                 SET_DX( context, (1<<9) | (1<<12) );  /* remote */
1425                 break;
1426             default:
1427                 SET_DX( context, 0 );  /* FIXME: use driver attr here */
1428                 break;
1429             }
1430             break;
1431
1432         case 0x0a: /* check if handle (BX) is remote */
1433             TRACE("IOCTL - CHECK IF HANDLE %d IS REMOTE\n",BX_reg(context));
1434             /* returns DX, bit 15 set if remote, bit 14 set if date/time
1435              * not set on close
1436              */
1437             SET_DX( context, 0 );
1438             break;
1439
1440         case 0x0d:
1441             TRACE("IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
1442                   INT21_DriveName( BL_reg(context)));
1443             bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1444             break;
1445
1446         case 0x0e: /* get logical drive mapping */
1447             TRACE("IOCTL - GET LOGICAL DRIVE MAP for drive %s\n",
1448                   INT21_DriveName( BL_reg(context)));
1449             SET_AL( context, 0 ); /* drive has no mapping - FIXME: may be wrong*/
1450             break;
1451
1452         case 0x0F:   /* Set logical drive mapping */
1453             {
1454             int drive;
1455             TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
1456                   INT21_DriveName( BL_reg(context)));
1457             drive = DOS_GET_DRIVE ( BL_reg(context) );
1458             if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
1459             {
1460                 SET_CFLAG(context);
1461                 SET_AX( context, 0x000F );  /* invalid drive */
1462             }
1463             break;
1464             }
1465
1466         case 0xe0:  /* Sun PC-NFS API */
1467             /* not installed */
1468             break;
1469
1470         case 0x52:  /* DR-DOS version */
1471             /* This is not DR-DOS */
1472
1473             TRACE("GET DR-DOS VERSION requested\n");
1474
1475             SET_AX( context, 0x0001 ); /* Invalid function */
1476             SET_CFLAG(context);       /* Error */
1477             SetLastError( ERROR_INVALID_FUNCTION );
1478             break;
1479
1480         default:
1481             INT_BARF( context, 0x21 );
1482             break;
1483         }
1484         break;
1485
1486     case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1487         {
1488             HANDLE handle;
1489             TRACE("DUP - DUPLICATE FILE HANDLE %d\n",BX_reg(context));
1490             if ((bSetDOSExtendedError = !DuplicateHandle( GetCurrentProcess(),
1491                                                           DosFileHandleToWin32Handle(BX_reg(context)),
1492                                                           GetCurrentProcess(), &handle,
1493                                                           0, TRUE, DUPLICATE_SAME_ACCESS )))
1494                 SET_AX( context, HFILE_ERROR16 );
1495             else
1496                 SET_AX( context, Win32HandleToDosFileHandle(handle) );
1497             break;
1498         }
1499
1500     case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1501         TRACE("FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
1502               BX_reg(context),CX_reg(context));
1503         bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR16);
1504         break;
1505
1506     case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1507         TRACE("CWD - GET CURRENT DIRECTORY for drive %s\n",
1508               INT21_DriveName( DL_reg(context)));
1509         bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1510         break;
1511
1512     case 0x48: /* ALLOCATE MEMORY */
1513         TRACE("ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context));
1514         {
1515             LPVOID *mem;
1516             if (ISV86(context))
1517               {
1518                 mem= DOSMEM_GetBlock((DWORD)BX_reg(context)<<4,NULL);
1519             if (mem)
1520                 SET_AX( context, DOSMEM_MapLinearToDos(mem)>>4 );
1521               }
1522             else
1523               {
1524                 mem = (LPVOID)GlobalDOSAlloc16(BX_reg(context)<<4);
1525                 if (mem)
1526                   SET_AX( context, (DWORD)mem&0xffff );
1527               }
1528             if (!mem)
1529               {
1530                 SET_CFLAG(context);
1531                 SET_AX( context, 0x0008 ); /* insufficient memory */
1532                 SET_BX( context, DOSMEM_Available()>>4 );
1533             }
1534         }
1535         break;
1536
1537     case 0x49: /* FREE MEMORY */
1538         TRACE("FREE MEMORY segment %04lX\n", context->SegEs);
1539         {
1540           BOOL ret;
1541           if (ISV86(context))
1542             ret= DOSMEM_FreeBlock(DOSMEM_MapDosToLinear(context->SegEs<<4));
1543           else
1544             {
1545               ret = !GlobalDOSFree16(context->SegEs);
1546               /* If we don't reset ES_reg, we will fail in the relay code */
1547               context->SegEs=ret;
1548             }
1549           if (!ret)
1550             {
1551               TRACE("FREE MEMORY failed\n");
1552             SET_CFLAG(context);
1553             SET_AX( context, 0x0009 ); /* memory block address invalid */
1554         }
1555         }
1556         break;
1557
1558     case 0x4a: /* RESIZE MEMORY BLOCK */
1559         TRACE("RESIZE MEMORY segment %04lX to %d paragraphs\n", context->SegEs, BX_reg(context));
1560         if (!ISV86(context))
1561           FIXME("RESIZE MEMORY probably insufficient implementation. Expect crash soon\n");
1562         {
1563             LPVOID *mem = DOSMEM_ResizeBlock(DOSMEM_MapDosToLinear(context->SegEs<<4),
1564                                              BX_reg(context)<<4,NULL);
1565             if (mem)
1566                 SET_AX( context, DOSMEM_MapLinearToDos(mem)>>4 );
1567             else {
1568                 SET_CFLAG(context);
1569                 SET_AX( context, 0x0008 ); /* insufficient memory */
1570                 SET_BX( context, DOSMEM_Available()>>4 ); /* not quite right */
1571             }
1572         }
1573         break;
1574
1575     case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1576         TRACE("EXEC %s\n", (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx ));
1577         SET_AX( context, WinExec16( CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx ),
1578                                     SW_NORMAL ));
1579         if (AX_reg(context) < 32) SET_CFLAG(context);
1580         break;
1581
1582     case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1583         TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
1584               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1585         if (!INT21_FindFirst(context)) break;
1586         /* fall through */
1587
1588     case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1589         TRACE("FINDNEXT\n");
1590         if (!INT21_FindNext(context))
1591         {
1592             SetLastError( ERROR_NO_MORE_FILES );
1593             SET_AX( context, ERROR_NO_MORE_FILES );
1594             SET_CFLAG(context);
1595         }
1596         else SET_AX( context, 0 );  /* OK */
1597         break;
1598     case 0x51: /* GET PSP ADDRESS */
1599         TRACE("GET CURRENT PROCESS ID (GET PSP ADDRESS)\n");
1600         /* FIXME: should we return the original DOS PSP upon */
1601         /*        Windows startup ? */
1602         SET_BX( context, GetCurrentPDB16() );
1603         break;
1604     case 0x62: /* GET PSP ADDRESS */
1605         /* FIXME: should we return the original DOS PSP upon */
1606         /*        Windows startup ? */
1607         SET_BX( context, GetCurrentPDB16() );
1608         break;
1609
1610     case 0x56: /* "RENAME" - RENAME FILE */
1611         TRACE("RENAME %s to %s\n",
1612               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1613               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi));
1614         bSetDOSExtendedError =
1615                 (!MoveFileA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1616                                CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi)));
1617         break;
1618
1619     case 0x57: /* FILE DATE AND TIME */
1620         switch (AL_reg(context))
1621         {
1622         case 0x00:  /* Get */
1623             {
1624                 FILETIME filetime;
1625                 TRACE("GET FILE DATE AND TIME for handle %d\n",
1626                       BX_reg(context));
1627                 if (!GetFileTime( DosFileHandleToWin32Handle(BX_reg(context)), NULL, NULL, &filetime ))
1628                      bSetDOSExtendedError = TRUE;
1629                 else
1630                 {
1631                     WORD date, time;
1632                     FileTimeToDosDateTime( &filetime, &date, &time );
1633                     SET_DX( context, date );
1634                     SET_CX( context, time );
1635                 }
1636             }
1637             break;
1638
1639         case 0x01:  /* Set */
1640             {
1641                 FILETIME filetime;
1642                 TRACE("SET FILE DATE AND TIME for handle %d\n",
1643                       BX_reg(context));
1644                 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
1645                                        &filetime );
1646                 bSetDOSExtendedError =
1647                         (!SetFileTime( DosFileHandleToWin32Handle(BX_reg(context)),
1648                                       NULL, NULL, &filetime ));
1649             }
1650             break;
1651         }
1652         break;
1653
1654     case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1655         TRACE("GET OR SET MEMORY/UMB ALLOCATION STRATEGY subfunction %d\n",
1656               AL_reg(context));
1657         switch (AL_reg(context))
1658         {
1659         case 0x00:
1660             SET_AX( context, 1 );
1661             break;
1662         case 0x02:
1663             SET_AX( context, 0 );
1664             break;
1665         case 0x01:
1666         case 0x03:
1667             break;
1668         }
1669         RESET_CFLAG(context);
1670         break;
1671
1672     case 0x5a: /* CREATE TEMPORARY FILE */
1673         TRACE("CREATE TEMPORARY FILE\n");
1674         bSetDOSExtendedError = !INT21_CreateTempFile(context);
1675         break;
1676
1677     case 0x5b: /* CREATE NEW FILE */
1678         TRACE("CREATE NEW FILE 0x%02x for %s\n", CX_reg(context),
1679               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
1680         bSetDOSExtendedError = ((SET_AX( context,
1681                _lcreat16_uniq( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1682                                CX_reg(context) ))) == (WORD)HFILE_ERROR16);
1683         break;
1684
1685     case 0x5d: /* NETWORK */
1686         FIXME("Function 0x%04x not implemented.\n", AX_reg (context));
1687         /* Fix the following while you're at it.  */
1688         SetLastError( ER_NoNetwork );
1689         bSetDOSExtendedError = TRUE;
1690         break;
1691
1692     case 0x5e:
1693         bSetDOSExtendedError = INT21_networkfunc (context);
1694         break;
1695
1696     case 0x5f: /* NETWORK */
1697         switch (AL_reg(context))
1698         {
1699         case 0x07: /* ENABLE DRIVE */
1700             TRACE("ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1701             if (!DRIVE_Enable( DL_reg(context) ))
1702             {
1703                 SetLastError( ERROR_INVALID_DRIVE );
1704                 bSetDOSExtendedError = TRUE;
1705             }
1706             break;
1707
1708         case 0x08: /* DISABLE DRIVE */
1709             TRACE("DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1710             if (!DRIVE_Disable( DL_reg(context) ))
1711             {
1712                 SetLastError( ERROR_INVALID_DRIVE );
1713                 bSetDOSExtendedError = TRUE;
1714             }
1715             break;
1716
1717         default:
1718             /* network software not installed */
1719             TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
1720             SetLastError( ER_NoNetwork );
1721             bSetDOSExtendedError = TRUE;
1722             break;
1723         }
1724         break;
1725
1726     case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1727         TRACE("TRUENAME %s\n",
1728               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
1729         {
1730             if (!GetFullPathNameA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1731                                                         context->Esi), 128,
1732                                      CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1733                                                         context->Edi),NULL))
1734                 bSetDOSExtendedError = TRUE;
1735             else SET_AX( context, 0 );
1736         }
1737         break;
1738
1739     case 0x61: /* UNUSED */
1740     case 0x63: /* misc. language support */
1741         switch (AL_reg(context)) {
1742         case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
1743             INT21_GetDBCSLeadTable(context);
1744             break;
1745         }
1746         break;
1747     case 0x64: /* OS/2 DOS BOX */
1748         INT_BARF( context, 0x21 );
1749         SET_CFLAG(context);
1750         break;
1751
1752     case 0x65:{/* GET EXTENDED COUNTRY INFORMATION */
1753         BYTE    *dataptr=CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi);
1754         TRACE("GET EXTENDED COUNTRY INFORMATION code page %d country %d\n",
1755               BX_reg(context), DX_reg(context));
1756         switch (AL_reg(context)) {
1757         case 0x01:
1758             TRACE("\tget general internationalization info\n");
1759             dataptr[0] = 0x1;
1760             *(WORD*)(dataptr+1) = 41;
1761             *(WORD*)(dataptr+3) = GetSystemDefaultLangID();
1762             *(WORD*)(dataptr+5) = CodePage;
1763             *(DWORD*)(dataptr+0x19) = 0; /* FIXME: ptr to case map routine */
1764             break;
1765         case 0x06:
1766             TRACE("\tget pointer to collating sequence table\n");
1767             dataptr[0] = 0x06;
1768             *(DWORD*)(dataptr+1) = MAKELONG(DOSMEM_CollateTable & 0xFFFF,DOSMEM_AllocSelector(DOSMEM_CollateTable>>16));
1769             SET_CX( context, 258 );/*FIXME: size of table?*/
1770             break;
1771         case 0x20:
1772             TRACE("\tConvert char to uppercase\n");
1773             SET_DL( context, toupper(DL_reg(context)) );
1774             break;
1775         case 0x21:
1776             TRACE("\tconvert string to uppercase with length\n");
1777             {
1778                 char *ptr = (char *)CTX_SEG_OFF_TO_LIN(context,context->SegDs,context->Edx);
1779                 WORD len = CX_reg(context);
1780                 while (len--) { *ptr = toupper(*ptr); ptr++; }
1781             }
1782             break;
1783         case 0x22:
1784             TRACE("\tConvert ASCIIZ string to uppercase\n");
1785             _strupr( (LPSTR)CTX_SEG_OFF_TO_LIN(context,context->SegDs,context->Edx) );
1786             break;
1787         default:
1788             TRACE("\tunimplemented function %d\n",AL_reg(context));
1789             INT_BARF( context, 0x21 );
1790             SET_CFLAG(context);
1791             break;
1792         }
1793         break;
1794     }
1795     case 0x66: /* GLOBAL CODE PAGE TABLE */
1796         switch (AL_reg(context))
1797         {
1798         case 0x01:
1799             TRACE("GET GLOBAL CODE PAGE TABLE\n");
1800             SET_BX( context, CodePage );
1801             SET_DX( context, CodePage );
1802             RESET_CFLAG(context);
1803             break;
1804         case 0x02:
1805             TRACE("SET GLOBAL CODE PAGE TABLE active page %d system page %d\n",
1806                   BX_reg(context),DX_reg(context));
1807             CodePage = BX_reg(context);
1808             RESET_CFLAG(context);
1809             break;
1810         }
1811         break;
1812
1813     case 0x67: /* SET HANDLE COUNT */
1814         TRACE("SET HANDLE COUNT to %d\n",BX_reg(context) );
1815         SetHandleCount16( BX_reg(context) );
1816         if (GetLastError()) bSetDOSExtendedError = TRUE;
1817         break;
1818
1819     case 0x68: /* "FFLUSH" - COMMIT FILE */
1820     case 0x6a: /* COMMIT FILE */
1821         TRACE("FFLUSH/COMMIT handle %d\n",BX_reg(context));
1822         bSetDOSExtendedError = (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ));
1823         break;
1824
1825     case 0x69: /* DISK SERIAL NUMBER */
1826         switch (AL_reg(context))
1827         {
1828         case 0x00:
1829             TRACE("GET DISK SERIAL NUMBER for drive %s\n",
1830                   INT21_DriveName(BL_reg(context)));
1831             if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1832             else SET_AX( context, 0 );
1833             break;
1834
1835         case 0x01:
1836             TRACE("SET DISK SERIAL NUMBER for drive %s\n",
1837                   INT21_DriveName(BL_reg(context)));
1838             if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1839             else SET_AX( context, 1 );
1840             break;
1841         }
1842         break;
1843
1844     case 0x6C: /* Extended Open/Create*/
1845         TRACE("EXTENDED OPEN/CREATE %s\n",
1846               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edi));
1847         bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1848         break;
1849
1850     case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
1851         if ((GetVersion()&0xC0000004)!=0xC0000004) {
1852             /* not supported on anything but Win95 */
1853             TRACE("LONG FILENAME functions supported only by win95\n");
1854             SET_CFLAG(context);
1855             SET_AL( context, 0 );
1856         } else
1857         switch(AL_reg(context))
1858         {
1859         case 0x39:  /* Create directory */
1860             TRACE("LONG FILENAME - MAKE DIRECTORY %s\n",
1861                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,context->Edx));
1862             bSetDOSExtendedError = (!CreateDirectoryA(
1863                                         CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1864                                                   context->Edx ), NULL));
1865             /* FIXME: CreateDirectory's LastErrors will clash with the ones
1866              * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1867              * denied), while CreateDirectory return several ones. remap some of
1868              * them. -Marcus
1869              */
1870             if (bSetDOSExtendedError) {
1871                     switch (GetLastError()) {
1872                     case ERROR_ALREADY_EXISTS:
1873                     case ERROR_FILENAME_EXCED_RANGE:
1874                     case ERROR_DISK_FULL:
1875                             SetLastError(ERROR_ACCESS_DENIED);
1876                             break;
1877                     default: break;
1878                     }
1879             }
1880             break;
1881         case 0x3a:  /* Remove directory */
1882             TRACE("LONG FILENAME - REMOVE DIRECTORY %s\n",
1883                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,context->Edx));
1884             bSetDOSExtendedError = (!RemoveDirectoryA(
1885                                         CTX_SEG_OFF_TO_LIN(context,  context->SegDs,
1886                                                         context->Edx )));
1887             break;
1888         case 0x43:  /* Get/Set file attributes */
1889           TRACE("LONG FILENAME -EXTENDED GET/SET FILE ATTRIBUTES %s\n",
1890                 (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,context->Edx));
1891         switch (BL_reg(context))
1892         {
1893         case 0x00: /* Get file attributes */
1894             TRACE("\tretrieve attributes\n");
1895             SET_CX( context, GetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1896                                                                     context->Edx)));
1897             if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1898             break;
1899         case 0x01:
1900             TRACE("\tset attributes 0x%04x\n",CX_reg(context));
1901             bSetDOSExtendedError = (!SetFileAttributesA(
1902                                         CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1903                                                            context->Edx),
1904                                         CX_reg(context)  ) );
1905             break;
1906         default:
1907           FIXME("Unimplemented long file name function:\n");
1908           INT_BARF( context, 0x21 );
1909           SET_CFLAG(context);
1910           SET_AL( context, 0 );
1911           break;
1912         }
1913         break;
1914         case 0x47:  /* Get current directory */
1915             TRACE(" LONG FILENAME - GET CURRENT DIRECTORY for drive %s\n",
1916                   INT21_DriveName(DL_reg(context)));
1917             bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1918             break;
1919
1920         case 0x4e:  /* Find first file */
1921             TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
1922                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
1923             /* FIXME: use attributes in CX */
1924             if ((SET_AX( context, FindFirstFile16(
1925                    CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1926                    (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1927                                                           context->Edi))))
1928                 == INVALID_HANDLE_VALUE16)
1929                 bSetDOSExtendedError = TRUE;
1930             break;
1931         case 0x4f:  /* Find next file */
1932             TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
1933                   BX_reg(context));
1934             if (!FindNextFile16( BX_reg(context),
1935                     (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1936                                                              context->Edi)))
1937                 bSetDOSExtendedError = TRUE;
1938             break;
1939         case 0xa0:
1940             {
1941                 LPCSTR driveroot = (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,context->Edx);
1942                 LPSTR buffer = (LPSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegEs,context->Edi);
1943                 DWORD filename_len, flags;
1944
1945                 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", driveroot);
1946                 SET_AX( context, 0 );
1947                 if (!GetVolumeInformationA( driveroot, NULL, 0, NULL, &filename_len,
1948                                             &flags, buffer, 8 ))
1949                 {
1950                     INT_BARF( context, 0x21 );
1951                     SET_CFLAG(context);
1952                     break;
1953                 }
1954                 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
1955                 SET_CX( context, filename_len );
1956                 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
1957             }
1958             break;
1959         case 0xa1:  /* Find close */
1960             TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
1961                   BX_reg(context));
1962             bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
1963             break;
1964         case 0x60:
1965           switch(CL_reg(context))
1966           {
1967             case 0x01:  /* Get short filename or path */
1968               if (!GetShortPathNameA
1969                   ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1970                                        context->Esi),
1971                     CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1972                                        context->Edi), 67))
1973                 bSetDOSExtendedError = TRUE;
1974               else SET_AX( context, 0 );
1975               break;
1976             case 0x02:  /* Get canonical long filename or path */
1977               if (!GetFullPathNameA
1978                   ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1979                                        context->Esi), 128,
1980                     CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1981                                        context->Edi),NULL))
1982                 bSetDOSExtendedError = TRUE;
1983               else SET_AX( context, 0 );
1984               break;
1985             default:
1986               FIXME("Unimplemented long file name function:\n");
1987               INT_BARF( context, 0x21 );
1988               SET_CFLAG(context);
1989               SET_AL( context, 0 );
1990               break;
1991           }
1992           break;
1993         case 0x6c:  /* Create or open file */
1994             TRACE("LONG FILENAME - CREATE OR OPEN FILE %s\n",
1995                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Esi));
1996           /* translate Dos 7 action to Dos 6 action */
1997             bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1998             break;
1999
2000         case 0x3b:  /* Change directory */
2001             TRACE("LONG FILENAME - CHANGE DIRECTORY %s\n",
2002                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
2003             if (!SetCurrentDirectoryA(CTX_SEG_OFF_TO_LIN(context,
2004                                                 context->SegDs,
2005                                                 context->Edx
2006                                         ))
2007             ) {
2008                 SET_CFLAG(context);
2009                 SET_AL( context, GetLastError() );
2010             }
2011             break;
2012         case 0x41:  /* Delete file */
2013             TRACE("LONG FILENAME - DELETE FILE %s\n",
2014                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
2015             if (!DeleteFileA(CTX_SEG_OFF_TO_LIN(context,
2016                                         context->SegDs,
2017                                         context->Edx)
2018             )) {
2019                 SET_CFLAG(context);
2020                 SET_AL( context, GetLastError() );
2021             }
2022             break;
2023         case 0x56:  /* Move (rename) file */
2024             {
2025                 LPCSTR fn1 = (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx);
2026                 LPCSTR fn2 = (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegEs, context->Edi);
2027                 TRACE("LONG FILENAME - RENAME FILE %s to %s\n", fn1, fn2);
2028                 if (!MoveFileA(fn1, fn2))
2029                 {
2030                     SET_CFLAG(context);
2031                     SET_AL( context, GetLastError() );
2032                 }
2033             }
2034             break;
2035         default:
2036             FIXME("Unimplemented long file name function:\n");
2037             INT_BARF( context, 0x21 );
2038             SET_CFLAG(context);
2039             SET_AL( context, 0 );
2040             break;
2041         }
2042         break;
2043
2044     case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
2045     case 0x72: /* MS-DOS 7 (Windows95) - ??? */
2046         TRACE("windows95 function AX %04x\n",
2047                     AX_reg(context));
2048         WARN("        returning unimplemented\n");
2049         SET_CFLAG(context);
2050         SET_AL( context, 0 );
2051         break;
2052
2053     case 0x73: /* MULTIPLEXED: Win95 OSR2/Win98 FAT32 calls */
2054         TRACE("windows95 function AX %04x\n",
2055                     AX_reg(context));
2056
2057         switch (AL_reg(context))
2058                 {
2059                 case 0x02:      /* Get Extended Drive Parameter Block for specific drive */
2060                                 /* ES:DI points to word with length of data (should be 0x3d) */
2061                         {
2062                                 WORD *buffer;
2063                                 struct EDPB *edpb;
2064                             DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
2065                             char root[] = "A:\\";
2066
2067                                 buffer = (WORD *)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
2068
2069                                 TRACE("Get Extended DPB: linear buffer address is %p\n", buffer);
2070
2071                                 /* validate passed-in buffer lengths */
2072                                 if ((*buffer != 0x3d) || (context->Ecx != 0x3f))
2073                                 {
2074                                         WARN("Get Extended DPB: buffer lengths incorrect\n");
2075                                         WARN("CX = %lx, buffer[0] = %x\n", context->Ecx, *buffer);
2076                                         SET_CFLAG(context);
2077                                         SET_AL( context, 0x18 );                /* bad buffer length */
2078                                 }
2079
2080                                 /* buffer checks out */
2081                                 buffer++;               /* skip over length word now */
2082                                 if (FillInDrivePB( DX_reg(context) ) )
2083                                 {
2084                                         edpb = (struct EDPB *)buffer;
2085
2086                                         /* copy down the old-style DPB portion first */
2087                                         memcpy(&edpb->dpb, &heap->dpb, sizeof(struct DPB));
2088
2089                                         /* now fill in the extended entries */
2090                                         edpb->edpb_flags = 0;
2091                                         edpb->next_edpb = 0;
2092                                         edpb->free_cluster = edpb->free_cluster2 = 0;
2093
2094                                         /* determine free disk space */
2095                                     *root += DOS_GET_DRIVE( DX_reg(context) );
2096                                     GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
2097                               &free_clusters, &total_clusters );
2098
2099                                         edpb->clusters_free = (free_clusters&0xffff);
2100
2101                                         edpb->clusters_free_hi = free_clusters >> 16;
2102                                         edpb->mirroring_flags = 0;
2103                                         edpb->info_sector = 0xffff;
2104                                         edpb->spare_boot_sector = 0xffff;
2105                                         edpb->first_cluster = 0;
2106                                         edpb->max_cluster = total_clusters;
2107                                         edpb->fat_clusters = 32;        /* made-up value */
2108                                         edpb->root_cluster = 0;
2109
2110                                         RESET_CFLAG(context);   /* clear carry */
2111                                         SET_AX( context, 0 );
2112                                 }
2113                                 else
2114                                 {
2115                                     SET_AX( context, 0x00ff );
2116                                     SET_CFLAG(context);
2117                                 }
2118                         }
2119                         break;
2120
2121                 case 0x03:      /* Get Extended free space on drive */
2122                 case 0x04:  /* Set DPB for formatting */
2123                 case 0x05:  /* extended absolute disk read/write */
2124                         FIXME("Unimplemented FAT32 int32 function %04x\n", AX_reg(context));
2125                         SET_CFLAG(context);
2126                         SET_AL( context, 0 );
2127                         break;
2128                 }
2129
2130                 break;
2131
2132     case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
2133     case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
2134         break;
2135
2136     default:
2137         INT_BARF( context, 0x21 );
2138         break;
2139
2140     } /* END OF SWITCH */
2141
2142     if( bSetDOSExtendedError )          /* set general error condition */
2143     {
2144         SET_AX( context, GetLastError() );
2145         SET_CFLAG(context);
2146     }
2147 }