Converted to the new debug interface, using script written by Patrik
[wine] / msdos / int21.c
1 /*
2  * DOS interrupt 21h handler
3  */
4
5 #include <time.h>
6 #include <fcntl.h>
7 #include <errno.h>
8 #include <stdlib.h>
9 #ifdef HAVE_SYS_FILE_H
10 # include <sys/file.h>
11 #endif
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <sys/time.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 #include <utime.h>
18 #include <ctype.h>
19 #include "windef.h"
20 #include "winbase.h"
21 #include "winuser.h" /* SW_NORMAL */
22 #include "wine/winbase16.h"
23 #include "winerror.h"
24 #include "drive.h"
25 #include "file.h"
26 #include "heap.h"
27 #include "msdos.h"
28 #include "ldt.h"
29 #include "task.h"
30 #include "options.h"
31 #include "miscemu.h"
32 #include "dosexe.h" /* for the MZ_SUPPORTED define */
33 #include "module.h"
34 #include "debugtools.h"
35 #include "console.h"
36
37 DEFAULT_DEBUG_CHANNEL(int21)
38 #if defined(__svr4__) || defined(_SCO_DS)
39 /* SVR4 DOESNT do locking the same way must implement properly */
40 #define LOCK_EX 0
41 #define LOCK_SH  1
42 #define LOCK_NB  8
43 #endif
44
45
46 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
47
48 /* Define the drive parameter block, as used by int21/1F
49  * and int21/32.  This table can be accessed through the
50  * global 'dpb' pointer, which points into the local dos
51  * heap.
52  */
53 struct DPB
54 {
55     BYTE drive_num;         /* 0=A, etc. */
56     BYTE unit_num;          /* Drive's unit number (?) */
57     WORD sector_size;       /* Sector size in bytes */
58     BYTE high_sector;       /* Highest sector in a cluster */
59     BYTE shift;             /* Shift count (?) */
60     WORD reserved;          /* Number of reserved sectors at start */
61     BYTE num_FAT;           /* Number of FATs */
62     WORD dir_entries;       /* Number of root dir entries */
63     WORD first_data;        /* First data sector */
64     WORD high_cluster;      /* Highest cluster number */
65     WORD sectors_in_FAT;    /* Number of sectors per FAT */
66     WORD start_dir;         /* Starting sector of first dir */
67     DWORD driver_head;      /* Address of device driver header (?) */
68     BYTE media_ID;          /* Media ID */
69     BYTE access_flag;       /* Prev. accessed flag (0=yes,0xFF=no) */
70     DWORD next;             /* Pointer to next DPB in list */
71     WORD free_search;       /* Free cluster search start */
72     WORD free_clusters;     /* Number of free clusters (0xFFFF=unknown) */
73 };
74
75 WORD CodePage = 437;
76 DWORD dpbsegptr;
77
78 struct DosHeap {
79         BYTE InDosFlag;
80         BYTE mediaID;
81         BYTE biosdate[8];
82         struct DPB dpb;
83         BYTE DummyDBCSLeadTable[6];
84 };
85 static struct DosHeap *heap;
86 static WORD DosHeapHandle;
87
88 extern char TempDirectory[];
89
90 static BOOL INT21_CreateHeap(void)
91 {
92     if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
93     {
94         WARN("Out of memory\n");
95         return FALSE;
96     }
97     heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
98     dpbsegptr = PTR_SEG_OFF_TO_SEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
99     heap->InDosFlag = 0;
100     strcpy(heap->biosdate, "01/01/80");
101     memset(heap->DummyDBCSLeadTable, 0, 6);
102     return TRUE;
103 }
104
105 static BYTE *GetCurrentDTA( CONTEXT86 *context )
106 {
107     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
108
109     /* FIXME: This assumes DTA was set correctly! */
110     return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta), 
111                                                 (DWORD)OFFSETOF(pTask->dta) );
112 }
113
114
115 void CreateBPB(int drive, BYTE *data, BOOL16 limited)
116 /* limited == TRUE is used with INT 0x21/0x440d */
117 {
118         if (drive > 1) {
119                 setword(data, 512);
120                 data[2] = 2;
121                 setword(&data[3], 0);
122                 data[5] = 2;
123                 setword(&data[6], 240);
124                 setword(&data[8], 64000);
125                 data[0x0a] = 0xf8;
126                 setword(&data[0x0b], 40);
127                 setword(&data[0x0d], 56);
128                 setword(&data[0x0f], 2);
129                 setword(&data[0x11], 0);
130                 if (!limited) {
131                     setword(&data[0x1f], 800);
132                     data[0x21] = 5;
133                     setword(&data[0x22], 1);
134                 }
135         } else { /* 1.44mb */
136                 setword(data, 512);
137                 data[2] = 2;
138                 setword(&data[3], 0);
139                 data[5] = 2;
140                 setword(&data[6], 240);
141                 setword(&data[8], 2880);
142                 data[0x0a] = 0xf8;
143                 setword(&data[0x0b], 6);
144                 setword(&data[0x0d], 18);
145                 setword(&data[0x0f], 2);
146                 setword(&data[0x11], 0);
147                 if (!limited) {
148                     setword(&data[0x1f], 80);
149                     data[0x21] = 7;
150                     setword(&data[0x22], 2);
151                 }
152         }       
153 }
154
155 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
156 {
157     DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
158     char root[] = "A:\\";
159
160     *root += DOS_GET_DRIVE( DL_reg(context) );
161     if (!GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
162                               &free_clusters, &total_clusters )) return 0;
163     AX_reg(context) = cluster_sectors;
164     BX_reg(context) = free_clusters;
165     CX_reg(context) = sector_bytes;
166     DX_reg(context) = total_clusters;
167     return 1;
168 }
169
170 static int INT21_GetDriveAllocInfo( CONTEXT86 *context )
171 {
172     if (!INT21_GetFreeDiskSpace( context )) return 0;
173     if (!heap && !INT21_CreateHeap()) return 0;
174     heap->mediaID = 0xf0;
175     DS_reg(context) = DosHeapHandle;
176     BX_reg(context) = (int)&heap->mediaID - (int)heap;
177     return 1;
178 }
179
180 static void GetDrivePB( CONTEXT86 *context, int drive )
181 {
182         if(!DRIVE_IsValid(drive))
183         {
184             SetLastError( ERROR_INVALID_DRIVE );
185             AX_reg(context) = 0x00ff;
186         }
187         else if (heap || INT21_CreateHeap())
188         {
189                 FIXME("GetDrivePB not fully implemented.\n");
190
191                 /* FIXME: I have no idea what a lot of this information should
192                  * say or whether it even really matters since we're not allowing
193                  * direct block access.  However, some programs seem to depend on
194                  * getting at least _something_ back from here.  The 'next' pointer
195                  * does worry me, though.  Should we have a complete table of
196                  * separate DPBs per drive?  Probably, but I'm lazy. :-)  -CH
197                  */
198                 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
199                 heap->dpb.sector_size = 512;
200                 heap->dpb.high_sector = 1;
201                 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
202                 heap->dpb.reserved = 0;
203                 heap->dpb.num_FAT = 1;
204                 heap->dpb.dir_entries = 2;
205                 heap->dpb.first_data = 2;
206                 heap->dpb.high_cluster = 64000;
207                 heap->dpb.sectors_in_FAT = 1;
208                 heap->dpb.start_dir = 1;
209                 heap->dpb.driver_head = 0;
210                 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
211                 heap->dpb.access_flag = 0;
212                 heap->dpb.next = 0;
213                 heap->dpb.free_search = 0;
214                 heap->dpb.free_clusters = 0xFFFF;    /* unknown */
215
216                 AL_reg(context) = 0x00;
217                 DS_reg(context) = SELECTOROF(dpbsegptr);
218                 BX_reg(context) = OFFSETOF(dpbsegptr);
219         }
220 }
221
222
223 static void ioctlGetDeviceInfo( CONTEXT86 *context )
224 {
225     int curr_drive;
226     const DOS_DEVICE *dev;
227
228     TRACE("(%d)\n", BX_reg(context));
229     
230     RESET_CFLAG(context);
231
232     /* DOS device ? */
233     if ((dev = DOSFS_GetDeviceByHandle( FILE_GetHandle(BX_reg(context)) )))
234     {
235         DX_reg(context) = dev->flags;
236         return;
237     }
238
239     /* it seems to be a file */
240     curr_drive = DRIVE_GetCurrentDrive();
241     DX_reg(context) = 0x0140 + curr_drive + ((curr_drive > 1) ? 0x0800 : 0); 
242     /* no floppy */
243     /* bits 0-5 are current drive
244      * bit 6 - file has NOT been written..FIXME: correct?
245      * bit 8 - generate int24 if no diskspace on write/ read past end of file
246      * bit 11 - media not removable
247      * bit 14 - don't set file date/time on closing
248      * bit 15 - file is remote
249      */
250 }
251
252 static BOOL ioctlGenericBlkDevReq( CONTEXT86 *context )
253 {
254         BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
255         int drive = DOS_GET_DRIVE( BL_reg(context) );
256
257         if (!DRIVE_IsValid(drive))
258         {
259             SetLastError( ERROR_FILE_NOT_FOUND );
260             return TRUE;
261         }
262
263         if (CH_reg(context) != 0x08)
264         {
265             INT_BARF( context, 0x21 );
266             return FALSE;
267         }
268
269         switch (CL_reg(context)) 
270         {
271                 case 0x4a: /* lock logical volume */
272                         TRACE("lock logical volume (%d) level %d mode %d\n",drive,BH_reg(context),DX_reg(context));
273                         break;
274
275                 case 0x60: /* get device parameters */
276                            /* used by w4wgrp's winfile */
277                         memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
278                         dataptr[0] = 0x04;
279                         dataptr[6] = 0; /* media type */
280                         if (drive > 1) 
281                         {
282                                 dataptr[1] = 0x05; /* fixed disk */
283                                 setword(&dataptr[2], 0x01); /* non removable */
284                                 setword(&dataptr[4], 0x300); /* # of cylinders */
285                         }
286                         else
287                         {
288                                 dataptr[1] = 0x07; /* block dev, floppy */
289                                 setword(&dataptr[2], 0x02); /* removable */
290                                 setword(&dataptr[4], 80); /* # of cylinders */
291                         }
292                         CreateBPB(drive, &dataptr[7], TRUE);
293                         RESET_CFLAG(context);
294                         break;
295
296                 case 0x41: /* write logical device track */
297                 case 0x61: /* read logical device track */
298                         {
299                                 BYTE drive = BL_reg(context) ?
300                                                 BL_reg(context) : DRIVE_GetCurrentDrive(); 
301                                 WORD head   = *(WORD *)dataptr+1;
302                                 WORD cyl    = *(WORD *)dataptr+3;
303                                 WORD sect   = *(WORD *)dataptr+5;
304                                 WORD nrsect = *(WORD *)dataptr+7;
305                                 BYTE *data  =  (BYTE *)dataptr+9;
306                                 int (*raw_func)(BYTE, DWORD, DWORD, BYTE *, BOOL);
307
308                                 raw_func = (CL_reg(context) == 0x41) ?
309                                                                 DRIVE_RawWrite : DRIVE_RawRead;
310
311                                 if (raw_func(drive, head*cyl*sect, nrsect, data, FALSE))
312                                         RESET_CFLAG(context);
313                                 else
314                                 {
315                                         AX_reg(context) = 0x1e; /* read fault */
316                                         SET_CFLAG(context);
317                                 }
318                         }
319                         break;
320                 case 0x66:/*  get disk serial number */
321                         {       
322                                 char    label[12],fsname[9],path[4];
323                                 DWORD   serial;
324
325                                 strcpy(path,"x:\\");path[0]=drive+'A';
326                                 GetVolumeInformationA(
327                                         path,label,12,&serial,NULL,NULL,fsname,9
328                                 );
329                                 *(WORD*)dataptr         = 0;
330                                 memcpy(dataptr+2,&serial,4);
331                                 memcpy(dataptr+6,label  ,11);
332                                 memcpy(dataptr+17,fsname,8);
333                         }
334                         break;
335
336                 case 0x6a:
337                         TRACE("logical volume %d unlocked.\n",drive);
338                         break;
339
340                 case 0x6f:
341                         memset(dataptr+1, '\0', dataptr[0]-1);
342                         dataptr[1] = dataptr[0]; 
343                         dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
344                         dataptr[3] = 0xFF; /* no physical drive */
345                         break;
346
347                 case 0x72:
348                         /* Trail on error implementation */
349                         AX_reg(context) = GetDriveType16(BL_reg(context)) == DRIVE_CANNOTDETERMINE ? 0x0f : 0x01;
350                         SET_CFLAG(context);     /* Seems to be set all the time */
351                         break;
352
353                 default:
354                         INT_BARF( context, 0x21 );
355         }
356         return FALSE;
357 }
358
359 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
360 {
361     char *filename =
362         CTX_SEG_OFF_TO_LIN(context, DS_reg(context), ESI_reg(context) );
363     char *fcb =
364         CTX_SEG_OFF_TO_LIN(context, ES_reg(context), EDI_reg(context) );
365     char *buffer, *s, *d;
366
367     AL_reg(context) = 0xff; /* failed */
368
369     TRACE("filename: '%s'\n", filename);
370
371     buffer = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + 1);
372
373     s = filename;
374     d = buffer;
375     while (*s)
376     {
377         if ((*s != ' ') && (*s != '\r') && (*s != '\n'))
378             *d++ = *s++;
379         else
380             break;
381     }
382
383     *d = '\0';
384     DOSFS_ToDosFCBFormat(buffer, fcb + 1);
385     *fcb = 0;
386     TRACE("FCB: '%s'\n", ((CHAR *)fcb + 1));
387
388     HeapFree( GetProcessHeap(), 0, buffer);
389
390     AL_reg(context) =
391         ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0;
392
393     /* point DS:SI to first unparsed character */
394     SI_reg(context) += (int)s - (int)filename;
395 }
396
397 static void INT21_GetSystemDate( CONTEXT86 *context )
398 {
399     SYSTEMTIME systime;
400     GetLocalTime( &systime );
401     CX_reg(context) = systime.wYear;
402     DX_reg(context) = (systime.wMonth << 8) | systime.wDay;
403     AX_reg(context) = systime.wDayOfWeek;
404 }
405
406 static void INT21_GetSystemTime( CONTEXT86 *context )
407 {
408     SYSTEMTIME systime;
409     GetLocalTime( &systime );
410     CX_reg(context) = (systime.wHour << 8) | systime.wMinute;
411     DX_reg(context) = (systime.wSecond << 8) | (systime.wMilliseconds / 10);
412 }
413
414 /* Many calls translate a drive argument like this:
415    drive number (00h = default, 01h = A:, etc)
416    */  
417 static char drivestring[]="default";
418
419 char *INT21_DriveName(int drive)
420 {
421
422     if(drive >0)
423       {
424         drivestring[0]= (unsigned char)drive + '@';
425         drivestring[1]=':';
426         drivestring[2]=0;
427       }
428     return drivestring;
429 }
430 static BOOL INT21_CreateFile( CONTEXT86 *context )
431 {
432     AX_reg(context) = _lcreat16( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
433                                           EDX_reg(context) ), CX_reg(context) );
434     return (AX_reg(context) == (WORD)HFILE_ERROR16);
435 }
436
437 static HFILE16 _lcreat16_uniq( LPCSTR path, INT attr )
438 {
439     /* Mask off all flags not explicitly allowed by the doc */
440     attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
441     return FILE_AllocDosHandle( CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
442                                              FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
443                                              CREATE_NEW, attr, -1 ));
444 }
445
446 static void OpenExistingFile( CONTEXT86 *context )
447 {
448     AX_reg(context) = _lopen16( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
449                                AL_reg(context) );
450     if (AX_reg(context) == (WORD)HFILE_ERROR16)
451     {
452         AX_reg(context) = GetLastError();
453         SET_CFLAG(context);
454     }
455 }
456
457 static BOOL INT21_ExtendedOpenCreateFile(CONTEXT86 *context )
458 {
459   BOOL bExtendedError = FALSE;
460   BYTE action = DL_reg(context);
461
462   /* Shuffle arguments to call OpenExistingFile */
463   AL_reg(context) = BL_reg(context);
464   DX_reg(context) = SI_reg(context);
465   /* BX,CX and DX should be preserved */
466   OpenExistingFile(context);
467
468   if ((EFL_reg(context) & 0x0001) == 0) /* File exists */
469   {
470       UINT16    uReturnCX = 0;
471
472       /* Now decide what do do */
473
474       if ((action & 0x07) == 0)
475       {
476           _lclose16( AX_reg(context) );
477           AX_reg(context) = 0x0050;     /*File exists*/
478           SET_CFLAG(context);
479           WARN("extended open/create: failed because file exists \n");
480       }
481       else if ((action & 0x07) == 2) 
482       {
483         /* Truncate it, but first check if opened for write */
484         if ((BL_reg(context) & 0x0007)== 0) 
485         {
486             _lclose16( AX_reg(context) );
487             WARN("extended open/create: failed, trunc on ro file\n");
488             AX_reg(context) = 0x000C;   /*Access code invalid*/
489             SET_CFLAG(context);
490         }
491         else
492         {
493                 TRACE("extended open/create: Closing before truncate\n");
494                 if (_lclose16( AX_reg(context) ))
495                 {
496                    WARN("extended open/create: close before trunc failed\n");
497                    AX_reg(context) = 0x0019;    /*Seek Error*/
498                    CX_reg(context) = 0;
499                    SET_CFLAG(context);
500                 }
501                 /* Shuffle arguments to call CreateFile */
502
503                 TRACE("extended open/create: Truncating\n");
504                 AL_reg(context) = BL_reg(context);
505                 /* CX is still the same */
506                 DX_reg(context) = SI_reg(context);
507                 bExtendedError = INT21_CreateFile(context);
508
509                 if (EFL_reg(context) & 0x0001)  /*no file open, flags set */
510                 {
511                     WARN("extended open/create: trunc failed\n");
512                     return bExtendedError;
513                 }
514                 uReturnCX = 0x3;
515         }
516       } 
517       else uReturnCX = 0x1;
518
519       CX_reg(context) = uReturnCX;
520   }
521   else /* file does not exist */
522   {
523       RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
524       if ((action & 0xF0)== 0)
525       {
526         CX_reg(context) = 0;
527         SET_CFLAG(context);
528         WARN("extended open/create: failed, file dosen't exist\n");
529       }
530       else
531       {
532         /* Shuffle arguments to call CreateFile */
533         TRACE("extended open/create: Creating\n");
534         AL_reg(context) = BL_reg(context);
535         /* CX should still be the same */
536         DX_reg(context) = SI_reg(context);
537         bExtendedError = INT21_CreateFile(context);
538         if (EFL_reg(context) & 0x0001)  /*no file open, flags set */
539         {
540             WARN("extended open/create: create failed\n");
541             return bExtendedError;
542         }
543         CX_reg(context) = 2;
544       }
545   }
546
547   return bExtendedError;
548 }
549
550
551 static BOOL INT21_ChangeDir( CONTEXT86 *context )
552 {
553     int drive;
554     char *dirname = CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context));
555
556     TRACE("changedir %s\n", dirname);
557     if (dirname[0] && (dirname[1] == ':'))
558     {
559         drive = toupper(dirname[0]) - 'A';
560         dirname += 2;
561     }
562     else drive = DRIVE_GetCurrentDrive();
563     return DRIVE_Chdir( drive, dirname );
564 }
565
566
567 static int INT21_FindFirst( CONTEXT86 *context )
568 {
569     char *p;
570     const char *path;
571     DOS_FULL_NAME full_name;
572     FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
573
574     path = (const char *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
575     dta->unixPath = NULL;
576     if (!DOSFS_GetFullName( path, FALSE, &full_name ))
577     {
578         AX_reg(context) = GetLastError();
579         SET_CFLAG(context);
580         return 0;
581     }
582     dta->unixPath = HEAP_strdupA( GetProcessHeap(), 0, full_name.long_name );
583     p = strrchr( dta->unixPath, '/' );
584     *p = '\0';
585
586     /* Note: terminating NULL in dta->mask overwrites dta->search_attr
587      *       (doesn't matter as it is set below anyway)
588      */
589     if (!DOSFS_ToDosFCBFormat( p + 1, dta->mask ))
590     {
591         HeapFree( GetProcessHeap(), 0, dta->unixPath );
592         dta->unixPath = NULL;
593         SetLastError( ERROR_FILE_NOT_FOUND );
594         AX_reg(context) = ERROR_FILE_NOT_FOUND;
595         SET_CFLAG(context);
596         return 0;
597     }
598     dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
599                                                : DRIVE_GetCurrentDrive();
600     dta->count = 0;
601     dta->search_attr = CL_reg(context);
602     return 1;
603 }
604
605
606 static int INT21_FindNext( CONTEXT86 *context )
607 {
608     FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
609     WIN32_FIND_DATAA entry;
610     int count;
611
612     if (!dta->unixPath) return 0;
613     if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
614                                   dta->search_attr, dta->count, &entry )))
615     {
616         HeapFree( GetProcessHeap(), 0, dta->unixPath );
617         dta->unixPath = NULL;
618         return 0;
619     }
620     if ((int)dta->count + count > 0xffff)
621     {
622         WARN("Too many directory entries in %s\n", dta->unixPath );
623         HeapFree( GetProcessHeap(), 0, dta->unixPath );
624         dta->unixPath = NULL;
625         return 0;
626     }
627     dta->count += count;
628     dta->fileattr = entry.dwFileAttributes;
629     dta->filesize = entry.nFileSizeLow;
630     FileTimeToDosDateTime( &entry.ftLastWriteTime,
631                            &dta->filedate, &dta->filetime );
632     strcpy( dta->filename, entry.cAlternateFileName );
633     if (!memchr(dta->mask,'?',11)) {
634         /* wildcardless search, release resources in case no findnext will
635          * be issued, and as a workaround in case file creation messes up
636          * findnext, as sometimes happens with pkunzip */
637         HeapFree( GetProcessHeap(), 0, dta->unixPath );
638         dta->unixPath = NULL;
639     }
640     return 1;
641 }
642
643
644 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
645 {
646     static int counter = 0;
647     char *name = CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context) );
648     char *p = name + strlen(name);
649
650     /* despite what Ralf Brown says, some programs seem to call without 
651      * ending backslash (DOS accepts that, so we accept it too) */
652     if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
653
654     for (;;)
655     {
656         sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
657         counter = (counter + 1) % 1000;
658
659         if ((AX_reg(context) = _lcreat16_uniq( name, 0 )) != (WORD)HFILE_ERROR16)
660         {
661             TRACE("created %s\n", name );
662             return TRUE;
663         }
664         if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
665     }
666 }
667
668
669 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context ) 
670 {
671     int drive = DOS_GET_DRIVE( DL_reg(context) );
672     char *ptr = (char *)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), ESI_reg(context) );
673
674     if (!DRIVE_IsValid(drive))
675     {
676         SetLastError( ERROR_INVALID_DRIVE );
677         return FALSE;
678     }
679     lstrcpynA( ptr, DRIVE_GetDosCwd(drive), 64 );
680     AX_reg(context) = 0x0100;                        /* success return code */
681     return TRUE;
682 }
683
684
685 static void INT21_GetDBCSLeadTable( CONTEXT86 *context )
686 {
687     if (heap || INT21_CreateHeap())
688     { /* return an empty table just as DOS 4.0+ does */
689         DS_reg(context) = DosHeapHandle;
690         SI_reg(context) = (int)&heap->DummyDBCSLeadTable - (int)heap;
691     }
692     else
693     {
694         AX_reg(context) = 0x1; /* error */
695         SET_CFLAG(context);
696     }
697 }
698
699
700 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
701 {
702     BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
703     int drive = DOS_GET_DRIVE( BL_reg(context) );
704         
705     if (!DRIVE_IsValid(drive))
706     {
707         SetLastError( ERROR_INVALID_DRIVE );
708         return 0;
709     }
710     
711     *(WORD *)dataptr = 0;
712     *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
713     memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
714     strncpy(dataptr + 0x11, "FAT16   ", 8);
715     return 1;
716 }
717
718
719 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
720 {
721     BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
722     int drive = DOS_GET_DRIVE( BL_reg(context) );
723
724     if (!DRIVE_IsValid(drive))
725     {
726         SetLastError( ERROR_INVALID_DRIVE );
727         return 0;
728     }
729
730     DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
731     return 1;
732 }
733
734
735 /* microsoft's programmers should be shot for using CP/M style int21
736    calls in Windows for Workgroup's winfile.exe */
737
738 static int INT21_FindFirstFCB( CONTEXT86 *context )
739 {
740     BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
741     FINDFILE_FCB *pFCB;
742     LPCSTR root, cwd;
743     int drive;
744
745     if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
746     else pFCB = (FINDFILE_FCB *)fcb;
747     drive = DOS_GET_DRIVE( pFCB->drive );
748     if (!DRIVE_IsValid( drive )) return 0;
749     root = DRIVE_GetRoot( drive );
750     cwd  = DRIVE_GetUnixCwd( drive );
751     pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
752                                 strlen(root)+strlen(cwd)+2 );
753     if (!pFCB->unixPath) return 0;
754     strcpy( pFCB->unixPath, root );
755     strcat( pFCB->unixPath, "/" );
756     strcat( pFCB->unixPath, cwd );
757     pFCB->count = 0;
758     return 1;
759 }
760
761
762 static int INT21_FindNextFCB( CONTEXT86 *context )
763 {
764     BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
765     FINDFILE_FCB *pFCB;
766     DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA(context);
767     WIN32_FIND_DATAA entry;
768     BYTE attr;
769     int count;
770
771     if (*fcb == 0xff) /* extended FCB ? */
772     {
773         attr = fcb[6];
774         pFCB = (FINDFILE_FCB *)(fcb + 7);
775     }
776     else
777     {
778         attr = 0;
779         pFCB = (FINDFILE_FCB *)fcb;
780     }
781
782     if (!pFCB->unixPath) return 0;
783     if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
784                                   DOS_GET_DRIVE( pFCB->drive ), attr,
785                                   pFCB->count, &entry )))
786     {
787         HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
788         pFCB->unixPath = NULL;
789         return 0;
790     }
791     pFCB->count += count;
792
793     if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
794         *(BYTE *)pResult = 0xff;
795         (BYTE *)pResult +=6; /* leave reserved field behind */
796         *(BYTE *)pResult = entry.dwFileAttributes;
797         ((BYTE *)pResult)++;
798     }
799     *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
800     ((BYTE *)pResult)++;
801     pResult->fileattr = entry.dwFileAttributes;
802     pResult->cluster  = 0;  /* what else? */
803     pResult->filesize = entry.nFileSizeLow;
804     memset( pResult->reserved, 0, sizeof(pResult->reserved) );
805     FileTimeToDosDateTime( &entry.ftLastWriteTime,
806                            &pResult->filedate, &pResult->filetime );
807
808     /* Convert file name to FCB format */
809
810     memset( pResult->filename, ' ', sizeof(pResult->filename) );
811     if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
812     else if (!strcmp( entry.cAlternateFileName, ".." ))
813         pResult->filename[0] = pResult->filename[1] = '.';
814     else
815     {
816         char *p = strrchr( entry.cAlternateFileName, '.' );
817         if (p && p[1] && (p != entry.cAlternateFileName))
818         {
819             memcpy( pResult->filename, entry.cAlternateFileName,
820                     MIN( (p - entry.cAlternateFileName), 8 ) );
821             memcpy( pResult->filename + 8, p + 1, MIN( strlen(p), 3 ) );
822         }
823         else
824             memcpy( pResult->filename, entry.cAlternateFileName,
825                     MIN( strlen(entry.cAlternateFileName), 8 ) );
826     }
827     return 1;
828 }
829
830
831 static void DeleteFileFCB( CONTEXT86 *context )
832 {
833     FIXME("(%p): stub\n", context);
834 }
835
836 static void RenameFileFCB( CONTEXT86 *context )
837 {
838     FIXME("(%p): stub\n", context);
839 }
840
841
842
843 static void fLock( CONTEXT86 * context )
844 {
845
846     switch ( AX_reg(context) & 0xff )
847     {
848         case 0x00: /* LOCK */
849           TRACE("lock handle %d offset %ld length %ld\n",
850                 BX_reg(context),
851                 MAKELONG(DX_reg(context),CX_reg(context)),
852                 MAKELONG(DI_reg(context),SI_reg(context))) ;
853           if (!LockFile(FILE_GetHandle(BX_reg(context)),
854                         MAKELONG(DX_reg(context),CX_reg(context)), 0,
855                         MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
856             AX_reg(context) = GetLastError();
857             SET_CFLAG(context);
858           }
859           break;
860
861         case 0x01: /* UNLOCK */
862           TRACE("unlock handle %d offset %ld length %ld\n",
863                 BX_reg(context),
864                 MAKELONG(DX_reg(context),CX_reg(context)),
865                 MAKELONG(DI_reg(context),SI_reg(context))) ;
866           if (!UnlockFile(FILE_GetHandle(BX_reg(context)),
867                           MAKELONG(DX_reg(context),CX_reg(context)), 0,
868                           MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
869             AX_reg(context) = GetLastError();
870             SET_CFLAG(context);
871           }
872           return;
873         default:
874           AX_reg(context) = 0x0001;
875           SET_CFLAG(context);
876           return;
877      }
878
879
880 static BOOL
881 INT21_networkfunc (CONTEXT86 *context)
882 {
883      switch (AL_reg(context)) {
884      case 0x00: /* Get machine name. */
885      {
886           char *dst = CTX_SEG_OFF_TO_LIN (context,DS_reg(context),EDX_reg(context));
887           TRACE("getting machine name to %p\n", dst);
888           if (gethostname (dst, 15))
889           {
890                WARN("failed!\n");
891                SetLastError( ER_NoNetwork );
892                return TRUE;
893           } else {
894                int len = strlen (dst);
895                while (len < 15)
896                     dst[len++] = ' ';
897                dst[15] = 0;
898                CH_reg(context) = 1; /* Valid */
899                CL_reg(context) = 1; /* NETbios number??? */
900                TRACE("returning %s\n", debugstr_an (dst, 16));
901                return FALSE;
902           }
903      }
904
905      default:
906           SetLastError( ER_NoNetwork );
907           return TRUE;
908      }
909 }
910
911 static void INT21_SetCurrentPSP(WORD psp)
912 {
913 #ifdef MZ_SUPPORTED
914     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
915     NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
916     
917     GlobalUnlock16( GetCurrentTask() );
918     if (pModule->lpDosTask)
919         pModule->lpDosTask->psp_seg = psp;
920     else
921 #endif
922         ERR("Cannot change PSP for non-DOS task!\n");
923 }
924     
925 static WORD INT21_GetCurrentPSP()
926 {
927 #ifdef MZ_SUPPORTED
928     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
929     NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
930         
931     GlobalUnlock16( GetCurrentTask() );
932     if (pModule->lpDosTask)
933         return pModule->lpDosTask->psp_seg;
934     else
935 #endif
936         return GetCurrentPDB16();
937 }
938
939
940 /***********************************************************************
941  *           INT21_GetExtendedError
942  */
943 static void INT21_GetExtendedError( CONTEXT86 *context )
944 {
945     BYTE class, action, locus;
946     WORD error = GetLastError();
947
948     switch(error)
949     {
950     case ERROR_SUCCESS:
951         class = action = locus = 0;
952         break;
953     case ERROR_DIR_NOT_EMPTY:
954         class  = EC_Exists;
955         action = SA_Ignore;
956         locus  = EL_Disk;
957         break;
958     case ERROR_ACCESS_DENIED:
959         class  = EC_AccessDenied;
960         action = SA_Abort;
961         locus  = EL_Disk;
962         break;
963     case ERROR_CANNOT_MAKE:
964         class  = EC_AccessDenied;
965         action = SA_Abort;
966         locus  = EL_Unknown;
967         break;
968     case ERROR_DISK_FULL:
969     case ERROR_HANDLE_DISK_FULL:
970         class  = EC_MediaError;
971         action = SA_Abort;
972         locus  = EL_Disk;
973         break;
974     case ERROR_FILE_EXISTS:
975     case ERROR_ALREADY_EXISTS:
976         class  = EC_Exists;
977         action = SA_Abort;
978         locus  = EL_Disk;
979         break;
980     case ERROR_FILE_NOT_FOUND:
981         class  = EC_NotFound;
982         action = SA_Abort;
983         locus  = EL_Disk;
984         break;
985     case ER_GeneralFailure:
986         class  = EC_SystemFailure;
987         action = SA_Abort;
988         locus  = EL_Unknown;
989         break;
990     case ERROR_INVALID_DRIVE:
991         class  = EC_MediaError;
992         action = SA_Abort;
993         locus  = EL_Disk;
994         break;
995     case ERROR_INVALID_HANDLE:
996         class  = EC_ProgramError;
997         action = SA_Abort;
998         locus  = EL_Disk;
999         break;
1000     case ERROR_LOCK_VIOLATION:
1001         class  = EC_AccessDenied;
1002         action = SA_Abort;
1003         locus  = EL_Disk;
1004         break;
1005     case ERROR_NO_MORE_FILES:
1006         class  = EC_MediaError;
1007         action = SA_Abort;
1008         locus  = EL_Disk;
1009         break;
1010     case ER_NoNetwork:
1011         class  = EC_NotFound;
1012         action = SA_Abort;
1013         locus  = EL_Network;
1014         break;
1015     case ERROR_NOT_ENOUGH_MEMORY:
1016         class  = EC_OutOfResource;
1017         action = SA_Abort;
1018         locus  = EL_Memory;
1019         break;
1020     case ERROR_PATH_NOT_FOUND:
1021         class  = EC_NotFound;
1022         action = SA_Abort;
1023         locus  = EL_Disk;
1024         break;
1025     case ERROR_SEEK:
1026         class  = EC_NotFound;
1027         action = SA_Ignore;
1028         locus  = EL_Disk;
1029         break;
1030     case ERROR_SHARING_VIOLATION:
1031         class  = EC_Temporary;
1032         action = SA_Retry;
1033         locus  = EL_Disk;
1034         break;
1035     case ERROR_TOO_MANY_OPEN_FILES:
1036         class  = EC_ProgramError;
1037         action = SA_Abort;
1038         locus  = EL_Disk;
1039         break;
1040     default:
1041         FIXME("Unknown error %d\n", error );
1042         class  = EC_SystemFailure;
1043         action = SA_Abort;
1044         locus  = EL_Unknown;
1045         break;
1046     }
1047     TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
1048            error, class, action, locus );
1049     AX_reg(context) = error;
1050     BH_reg(context) = class;
1051     BL_reg(context) = action;
1052     CH_reg(context) = locus;
1053 }
1054
1055 /***********************************************************************
1056  *           DOS3Call  (KERNEL.102)
1057  */
1058 void WINAPI DOS3Call( CONTEXT86 *context )
1059 {
1060     BOOL        bSetDOSExtendedError = FALSE;
1061
1062
1063     TRACE("AX=%04x BX=%04x CX=%04x DX=%04x "
1064           "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1065           AX_reg(context), BX_reg(context), CX_reg(context), DX_reg(context),
1066           SI_reg(context), DI_reg(context),
1067           (WORD)DS_reg(context), (WORD)ES_reg(context),
1068           EFL_reg(context) );
1069
1070
1071     if (AH_reg(context) == 0x59)  /* Get extended error info */
1072     {
1073         INT21_GetExtendedError( context );
1074         return;
1075     }
1076
1077     if (AH_reg(context) == 0x0C)  /* Flush buffer and read standard input */
1078     {
1079         TRACE("FLUSH BUFFER AND READ STANDARD INPUT\n");
1080         /* no flush here yet */
1081         AH_reg(context) = AL_reg(context);
1082     }
1083
1084     if (AH_reg(context)>=0x2f) {
1085         /* extended error is used by (at least) functions 0x2f to 0x62 */
1086         SetLastError(0);
1087     }
1088     RESET_CFLAG(context);  /* Not sure if this is a good idea */
1089
1090     switch(AH_reg(context)) 
1091     {
1092     case 0x03: /* READ CHARACTER FROM STDAUX  */
1093     case 0x04: /* WRITE CHARACTER TO STDAUX */
1094     case 0x05: /* WRITE CHARACTER TO PRINTER */
1095     case 0x0f: /* OPEN FILE USING FCB */
1096     case 0x10: /* CLOSE FILE USING FCB */
1097     case 0x14: /* SEQUENTIAL READ FROM FCB FILE */              
1098     case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
1099     case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
1100     case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
1101     case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
1102     case 0x23: /* GET FILE SIZE FOR FCB */
1103     case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
1104     case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
1105     case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
1106     case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
1107         INT_BARF( context, 0x21 );
1108         break;
1109
1110     case 0x00: /* TERMINATE PROGRAM */
1111         TRACE("TERMINATE PROGRAM\n");
1112         ExitProcess( 0 );
1113         break;
1114
1115     case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
1116         TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
1117         AL_reg(context) = CONSOLE_GetCharacter();
1118         /* FIXME: no echo */
1119         break;
1120
1121     case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
1122         TRACE("Write Character to Standard Output\n");
1123         CONSOLE_Write(DL_reg(context), 0, 0, 0);
1124         break;
1125
1126     case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
1127         if (DL_reg(context) == 0xff) {
1128             static char scan = 0;
1129             TRACE("Direct Console Input\n");
1130             if (scan) {
1131                 /* return pending scancode */
1132                 AL_reg(context) = scan;
1133                 FL_reg(context) &= ~0x40; /* clear ZF */
1134                 scan = 0;
1135             } else {
1136                 char ascii;
1137                 if (CONSOLE_CheckForKeystroke(&scan,&ascii)) {
1138                     CONSOLE_GetKeystroke(&scan,&ascii);
1139                     /* return ASCII code */
1140                     AL_reg(context) = ascii;
1141                     FL_reg(context) &= ~0x40; /* clear ZF */
1142                     /* return scan code on next call only if ascii==0 */
1143                     if (ascii) scan = 0;
1144                 } else {
1145                     /* nothing pending, clear everything */
1146                     AL_reg(context) = 0;
1147                     FL_reg(context) |= 0x40; /* set ZF */
1148                     scan = 0; /* just in case */
1149                 }
1150             }
1151         } else {
1152             TRACE("Direct Console Output\n");
1153             CONSOLE_Write(DL_reg(context), 0, 0, 0);
1154         }
1155         break;
1156
1157     case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
1158         TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
1159         AL_reg(context) = CONSOLE_GetCharacter();
1160         break;
1161
1162     case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
1163         TRACE("CHARACTER INPUT WITHOUT ECHO\n");
1164         AL_reg(context) = CONSOLE_GetCharacter();
1165         break;
1166
1167     case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
1168         TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
1169               DS_reg(context),DX_reg(context) );
1170         {
1171             LPSTR data = CTX_SEG_OFF_TO_LIN(context,DS_reg(context),EDX_reg(context));
1172             LPSTR p = data;
1173             /* do NOT use strchr() to calculate the string length,
1174             as '\0' is valid string content, too !
1175             Maybe we should check for non-'$' strings, but DOS doesn't. */
1176             while (*p != '$') p++;
1177             _hwrite16( 1, data, (int)p - (int)data);
1178             AL_reg(context) = '$'; /* yes, '$' (0x24) gets returned in AL */
1179         }
1180         break;
1181
1182     case 0x0a: /* BUFFERED INPUT */
1183       {
1184         char *buffer = ((char *)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), 
1185                                                    EDX_reg(context) ));
1186         int res;
1187         
1188         TRACE("BUFFERED INPUT (size=%d)\n",buffer[0]);
1189         if (buffer[1])
1190           TRACE("Handle old chars in buffer!\n");
1191         res=_lread16( 0, buffer+2,buffer[0]);
1192         buffer[1]=res;
1193         if(buffer[res+1] == '\n')
1194           buffer[res+1] = '\r';
1195         break;
1196       }
1197
1198     case 0x0b: {/* GET STDIN STATUS */
1199                 char x1,x2;
1200
1201                 if (CONSOLE_CheckForKeystroke(&x1,&x2))
1202                     AL_reg(context) = 0xff; 
1203                 else
1204                     AL_reg(context) = 0; 
1205                 break;
1206         }
1207     case 0x2e: /* SET VERIFY FLAG */
1208         TRACE("SET VERIFY FLAG ignored\n");
1209         /* we cannot change the behaviour anyway, so just ignore it */
1210         break;
1211
1212     case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
1213     case 0x1d:
1214     case 0x1e:
1215     case 0x20:
1216     case 0x6b: /* NULL FUNCTION */
1217         AL_reg(context) = 0;
1218         break;
1219         
1220     case 0x5c: /* "FLOCK" - RECORD LOCKING */
1221         fLock(context);
1222         break;
1223
1224     case 0x0d: /* DISK BUFFER FLUSH */
1225         TRACE("DISK BUFFER FLUSH ignored\n");
1226         RESET_CFLAG(context); /* dos 6+ only */
1227         break;
1228
1229     case 0x0e: /* SELECT DEFAULT DRIVE */
1230         TRACE("SELECT DEFAULT DRIVE %d\n", DL_reg(context));
1231         DRIVE_SetCurrentDrive( DL_reg(context) );
1232         AL_reg(context) = MAX_DOS_DRIVES;
1233         break;
1234
1235     case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1236         TRACE("FIND FIRST MATCHING FILE USING FCB %p\n", 
1237               CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1238         if (!INT21_FindFirstFCB(context))
1239         {
1240             AL_reg(context) = 0xff;
1241             break;
1242         }
1243         /* else fall through */
1244
1245     case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1246         AL_reg(context) = INT21_FindNextFCB(context) ? 0x00 : 0xff;
1247         break;
1248
1249     case 0x13: /* DELETE FILE USING FCB */
1250         DeleteFileFCB(context);
1251         break;
1252             
1253     case 0x17: /* RENAME FILE USING FCB */
1254         RenameFileFCB(context);
1255         break;
1256
1257     case 0x19: /* GET CURRENT DEFAULT DRIVE */
1258         AL_reg(context) = DRIVE_GetCurrentDrive();
1259         break;
1260
1261     case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1262         {
1263             TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1264             pTask->dta = PTR_SEG_OFF_TO_SEGPTR(DS_reg(context),DX_reg(context));
1265             TRACE("Set DTA: %08lx\n", pTask->dta);
1266         }
1267         break;
1268
1269     case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1270         DL_reg(context) = 0;
1271         if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1272         break;
1273         
1274     case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1275         if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1276         break;
1277
1278     case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1279         GetDrivePB(context, DRIVE_GetCurrentDrive());
1280         break;
1281                 
1282     case 0x25: /* SET INTERRUPT VECTOR */
1283         INT_CtxSetHandler( context, AL_reg(context),
1284                         (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1285                                                           DX_reg(context)));
1286         break;
1287
1288     case 0x29: /* PARSE FILENAME INTO FCB */
1289         INT21_ParseFileNameIntoFCB(context);
1290         break;
1291
1292     case 0x2a: /* GET SYSTEM DATE */
1293         INT21_GetSystemDate(context);
1294         break;
1295
1296     case 0x2b: /* SET SYSTEM DATE */
1297         FIXME("SetSystemDate(%02d/%02d/%04d): not allowed\n",
1298               DL_reg(context), DH_reg(context), CX_reg(context) );
1299         AL_reg(context) = 0;  /* Let's pretend we succeeded */
1300         break;
1301
1302     case 0x2c: /* GET SYSTEM TIME */
1303         INT21_GetSystemTime(context);
1304         break;
1305
1306     case 0x2d: /* SET SYSTEM TIME */
1307         FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1308               CH_reg(context), CL_reg(context),
1309               DH_reg(context), DL_reg(context) );
1310         AL_reg(context) = 0;  /* Let's pretend we succeeded */
1311         break;
1312
1313     case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1314         TRACE("GET DISK TRANSFER AREA ADDRESS\n");
1315         {
1316             TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1317             ES_reg(context) = SELECTOROF( pTask->dta );
1318             BX_reg(context) = OFFSETOF( pTask->dta );
1319         }
1320         break;
1321             
1322     case 0x30: /* GET DOS VERSION */
1323         TRACE("GET DOS VERSION %s requested\n",
1324               (AL_reg(context) == 0x00)?"OEM number":"version flag");
1325         AX_reg(context) = (HIWORD(GetVersion16()) >> 8) |
1326                           (HIWORD(GetVersion16()) << 8);
1327 #if 0
1328         AH_reg(context) = 0x7;
1329         AL_reg(context) = 0xA;
1330 #endif
1331
1332         BX_reg(context) = 0x00FF;     /* 0x123456 is Wine's serial # */
1333         CX_reg(context) = 0x0000;
1334         break;
1335
1336     case 0x31: /* TERMINATE AND STAY RESIDENT */
1337         FIXME("TERMINATE AND STAY RESIDENT stub\n");
1338         break;
1339
1340     case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1341         TRACE("GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
1342               INT21_DriveName( DL_reg(context)));
1343         GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
1344         break;
1345
1346     case 0x33: /* MULTIPLEXED */
1347         switch (AL_reg(context))
1348         {
1349               case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1350                 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
1351                 DL_reg(context) = DOSCONF_config.brk_flag;
1352                 break;
1353
1354               case 0x01: /* SET EXTENDED BREAK STATE */
1355                 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
1356                 DOSCONF_config.brk_flag = (DL_reg(context) > 0);
1357                 break;          
1358                 
1359               case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1360                 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
1361                 /* ugly coding in order to stay reentrant */
1362                 if (DL_reg(context))
1363                 {
1364                     DL_reg(context) = DOSCONF_config.brk_flag;
1365                     DOSCONF_config.brk_flag = 1;
1366                 }
1367                 else
1368                 {
1369                     DL_reg(context) = DOSCONF_config.brk_flag;
1370                     DOSCONF_config.brk_flag = 0;
1371                 }
1372                 break;
1373
1374               case 0x05: /* GET BOOT DRIVE */
1375                 TRACE("GET BOOT DRIVE\n");
1376                 DL_reg(context) = 3;
1377                 /* c: is Wine's bootdrive (a: is 1)*/
1378                 break;
1379                                 
1380               case 0x06: /* GET TRUE VERSION NUMBER */
1381                 TRACE("GET TRUE VERSION NUMBER\n");
1382                 BX_reg(context) = (HIWORD(GetVersion16() >> 8)) |
1383                                   (HIWORD(GetVersion16() << 8));
1384                 DX_reg(context) = 0x00;
1385                 break;
1386
1387               default:
1388                 INT_BARF( context, 0x21 );
1389                 break;                  
1390         }
1391         break;  
1392             
1393     case 0x34: /* GET ADDRESS OF INDOS FLAG */
1394         TRACE("GET ADDRESS OF INDOS FLAG\n");
1395         if (!heap) INT21_CreateHeap();
1396         ES_reg(context) = DosHeapHandle;
1397         BX_reg(context) = (int)&heap->InDosFlag - (int)heap;
1398         break;
1399
1400     case 0x35: /* GET INTERRUPT VECTOR */
1401         TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
1402         {
1403             FARPROC16 addr = INT_CtxGetHandler( context, AL_reg(context) );
1404             ES_reg(context) = SELECTOROF(addr);
1405             BX_reg(context) = OFFSETOF(addr);
1406         }
1407         break;
1408
1409     case 0x36: /* GET FREE DISK SPACE */
1410         TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
1411               INT21_DriveName( DL_reg(context)));
1412         if (!INT21_GetFreeDiskSpace(context)) AX_reg(context) = 0xffff;
1413         break;
1414
1415     case 0x37: 
1416       {
1417         unsigned char switchchar='/';
1418         switch (AL_reg(context))
1419         {
1420         case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
1421           TRACE("SWITCHAR - GET SWITCH CHARACTER\n");
1422           AL_reg(context) = 0x00; /* success*/
1423           DL_reg(context) = switchchar;
1424           break;
1425         case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
1426           TRACE("SWITCHAR - SET SWITCH CHARACTER\n");
1427           switchchar = DL_reg(context);
1428           AL_reg(context) = 0x00; /* success*/
1429           break;
1430         default: /*"AVAILDEV" - SPECIFY \DEV\ PREFIX USE*/
1431           INT_BARF( context, 0x21 );
1432           break;
1433         }
1434         break;
1435       }
1436
1437     case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1438         TRACE("GET COUNTRY-SPECIFIC INFORMATION for country 0x%02x\n",
1439               AL_reg(context));
1440         AX_reg(context) = 0x02; /* no country support available */
1441         SET_CFLAG(context);
1442         break;
1443
1444     case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1445         TRACE("MKDIR %s\n",
1446               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1447         bSetDOSExtendedError = (!CreateDirectory16( CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),
1448                                                            EDX_reg(context) ), NULL));
1449         /* FIXME: CreateDirectory's LastErrors will clash with the ones
1450          * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1451          * denied), while CreateDirectory return several ones. remap some of
1452          * them. -Marcus
1453          */
1454         if (bSetDOSExtendedError) {
1455                 switch (GetLastError()) {
1456                 case ERROR_ALREADY_EXISTS:
1457                 case ERROR_FILENAME_EXCED_RANGE:
1458                 case ERROR_DISK_FULL:
1459                         SetLastError(ERROR_ACCESS_DENIED);
1460                         break;
1461                 default: break;
1462                 }
1463         }
1464         break;
1465         
1466     case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1467         TRACE("RMDIR %s\n",
1468               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1469         bSetDOSExtendedError = (!RemoveDirectory16( CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),
1470                                                                  EDX_reg(context) )));
1471         break;
1472
1473     case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1474         TRACE("CHDIR %s\n",
1475               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1476         bSetDOSExtendedError = !INT21_ChangeDir(context);
1477         break;
1478         
1479     case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1480         TRACE("CREAT flag 0x%02x %s\n",CX_reg(context),
1481               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1482         bSetDOSExtendedError = INT21_CreateFile( context );
1483         break;
1484
1485     case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1486         TRACE("OPEN mode 0x%02x %s\n",AL_reg(context),
1487               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1488         OpenExistingFile(context);
1489         break;
1490
1491     case 0x3e: /* "CLOSE" - CLOSE FILE */
1492         TRACE("CLOSE handle %d\n",BX_reg(context));
1493         bSetDOSExtendedError = ((AX_reg(context) = _lclose16( BX_reg(context) )) != 0);
1494         break;
1495
1496     case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1497         TRACE("READ from %d to %04lX:%04X for %d byte\n",BX_reg(context),
1498               DS_reg(context),DX_reg(context),CX_reg(context) );
1499         {
1500             LONG result;
1501             if (ISV86(context))
1502                 result = _hread16( BX_reg(context),
1503                                    CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1504                                                                EDX_reg(context) ),
1505                                    CX_reg(context) );
1506             else
1507                 result = WIN16_hread( BX_reg(context),
1508                                       PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1509                                                              EDX_reg(context) ),
1510                                       CX_reg(context) );
1511             if (result == -1) bSetDOSExtendedError = TRUE;
1512             else AX_reg(context) = (WORD)result;
1513         }
1514         break;
1515
1516     case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1517         TRACE("WRITE from %04lX:%04X to handle %d for %d byte\n",
1518               DS_reg(context),DX_reg(context),BX_reg(context),CX_reg(context) );
1519         {
1520             LONG result = _hwrite16( BX_reg(context),
1521                                      CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),
1522                                                          EDX_reg(context) ),
1523                                      CX_reg(context) );
1524             if (result == -1) bSetDOSExtendedError = TRUE;
1525             else AX_reg(context) = (WORD)result;
1526         }
1527         break;
1528
1529     case 0x41: /* "UNLINK" - DELETE FILE */
1530         TRACE("UNLINK%s\n",
1531               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1532         bSetDOSExtendedError = (!DeleteFileA( CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),
1533                                                              EDX_reg(context) )));
1534         break;
1535
1536     case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1537         TRACE("LSEEK handle %d offset %ld from %s\n",
1538               BX_reg(context), MAKELONG(DX_reg(context),CX_reg(context)),
1539               (AL_reg(context)==0)?"start of file":(AL_reg(context)==1)?
1540               "current file position":"end of file");
1541         {
1542             LONG status = _llseek16( BX_reg(context),
1543                                      MAKELONG(DX_reg(context),CX_reg(context)),
1544                                      AL_reg(context) );
1545             if (status == -1) bSetDOSExtendedError = TRUE;
1546             else
1547             {
1548                 AX_reg(context) = LOWORD(status);
1549                 DX_reg(context) = HIWORD(status);
1550             }
1551         }
1552         break;
1553
1554     case 0x43: /* FILE ATTRIBUTES */
1555         switch (AL_reg(context))
1556         {
1557         case 0x00:
1558             TRACE("GET FILE ATTRIBUTES for %s\n", 
1559                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1560             AX_reg(context) = (WORD)GetFileAttributesA(
1561                                           CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1562                                                              EDX_reg(context)));
1563             if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1564             else CX_reg(context) = AX_reg(context);
1565             break;
1566
1567         case 0x01:
1568             TRACE("SET FILE ATTRIBUTES 0x%02x for %s\n", CX_reg(context),
1569                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1570             bSetDOSExtendedError = 
1571                 (!SetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, DS_reg(context), 
1572                                                            EDX_reg(context)),
1573                                                            CX_reg(context) ));
1574             break;
1575         case 0x02:
1576             TRACE("GET COMPRESSED FILE SIZE for %s stub\n",
1577                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1578         }
1579         break;
1580         
1581     case 0x44: /* IOCTL */
1582         switch (AL_reg(context))
1583         {
1584         case 0x00:
1585             ioctlGetDeviceInfo(context);
1586             break;
1587
1588         case 0x01:
1589             break;
1590         case 0x02:{
1591             const DOS_DEVICE *dev;
1592             if ((dev = DOSFS_GetDeviceByHandle( FILE_GetHandle(BX_reg(context)) )) &&
1593                 !strcasecmp( dev->name, "SCSIMGR$" ))
1594             {
1595                 ASPI_DOS_HandleInt(context);
1596             }
1597             break;
1598        }
1599         case 0x05:{     /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1600             /*BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context));*/
1601             int drive = DOS_GET_DRIVE(BL_reg(context));
1602
1603             FIXME("program tried to write to block device control channel of drive %d:\n",drive);
1604             /* for (i=0;i<CX_reg(context);i++)
1605                 fprintf(stdnimp,"%02x ",dataptr[i]);
1606             fprintf(stdnimp,"\n");*/
1607             AX_reg(context)=CX_reg(context);
1608             break;
1609         }
1610         case 0x08:   /* Check if drive is removable. */
1611             TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOVABLE for drive %s\n",
1612                  INT21_DriveName( BL_reg(context)));
1613             switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1614             {
1615             case DRIVE_CANNOTDETERMINE:
1616                 SetLastError( ERROR_INVALID_DRIVE );
1617                 AX_reg(context) = ERROR_INVALID_DRIVE;
1618                 SET_CFLAG(context);
1619                 break;
1620             case DRIVE_REMOVABLE:
1621                 AX_reg(context) = 0;      /* removable */
1622                 break;
1623             default:
1624                 AX_reg(context) = 1;   /* not removable */
1625                 break;
1626             }
1627             break;
1628
1629         case 0x09:   /* CHECK IF BLOCK DEVICE REMOTE */
1630             TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOTE for drive %s\n",
1631                  INT21_DriveName( BL_reg(context))); 
1632             switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1633             {
1634             case DRIVE_CANNOTDETERMINE:
1635                 SetLastError( ERROR_INVALID_DRIVE );
1636                 AX_reg(context) = ERROR_INVALID_DRIVE;
1637                 SET_CFLAG(context);
1638                 break;
1639             case DRIVE_REMOTE:
1640                 DX_reg(context) = (1<<9) | (1<<12);  /* remote */
1641                 break;
1642             default:
1643                 DX_reg(context) = 0;  /* FIXME: use driver attr here */
1644                 break;
1645             }
1646             break;
1647
1648         case 0x0a: /* check if handle (BX) is remote */
1649             TRACE("IOCTL - CHECK IF HANDLE %d IS REMOTE\n",BX_reg(context));
1650             /* returns DX, bit 15 set if remote, bit 14 set if date/time
1651              * not set on close
1652              */
1653             DX_reg(context) = 0;
1654             break;
1655
1656         case 0x0b:   /* SET SHARING RETRY COUNT */
1657             TRACE("IOCTL - SET SHARING RETRY COUNT pause %d retries %d\n",
1658                  CX_reg(context), DX_reg(context));
1659             if (!CX_reg(context))
1660             { 
1661                 AX_reg(context) = 1;
1662                 SET_CFLAG(context);
1663                 break;
1664             }
1665             DOSMEM_LOL()->sharing_retry_delay = CX_reg(context);
1666             if (!DX_reg(context))
1667                 DOSMEM_LOL()->sharing_retry_count = DX_reg(context);
1668             RESET_CFLAG(context);
1669             break;
1670
1671         case 0x0d:
1672             TRACE("IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
1673                   INT21_DriveName( BL_reg(context)));
1674             bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1675             break;
1676
1677         case 0x0e: /* get logical drive mapping */
1678             TRACE("IOCTL - GET LOGICAL DRIVE MAP for drive %s\n",
1679                   INT21_DriveName( BL_reg(context)));
1680             AL_reg(context) = 0; /* drive has no mapping - FIXME: may be wrong*/
1681             break;
1682
1683         case 0x0F:   /* Set logical drive mapping */
1684             {
1685             int drive;
1686             TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
1687                   INT21_DriveName( BL_reg(context))); 
1688             drive = DOS_GET_DRIVE ( BL_reg(context) );
1689             if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
1690             {
1691                 SET_CFLAG(context);
1692                 AX_reg(context) = 0x000F;  /* invalid drive */
1693             }
1694             break;
1695             }
1696
1697         case 0xe0:  /* Sun PC-NFS API */
1698             /* not installed */
1699             break;
1700                 
1701         default:
1702             INT_BARF( context, 0x21 );
1703             break;
1704         }
1705         break;
1706
1707     case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1708         {
1709             HANDLE handle;
1710             TRACE("DUP - DUPLICATE FILE HANDLE %d\n",BX_reg(context));
1711             if ((bSetDOSExtendedError = !DuplicateHandle( GetCurrentProcess(),
1712                                                           FILE_GetHandle(BX_reg(context)),
1713                                                           GetCurrentProcess(), &handle,
1714                                                           0, TRUE, DUPLICATE_SAME_ACCESS )))
1715                 AX_reg(context) = HFILE_ERROR16;
1716             else
1717                 AX_reg(context) = FILE_AllocDosHandle(handle);
1718             break;
1719         }
1720
1721     case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1722         TRACE("FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
1723               BX_reg(context),CX_reg(context));
1724         bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR16);
1725         break;
1726
1727     case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1728         TRACE("CWD - GET CURRENT DIRECTORY for drive %s\n",
1729               INT21_DriveName( DL_reg(context)));
1730         bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1731         break;
1732
1733     case 0x48: /* ALLOCATE MEMORY */
1734         TRACE("ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context));
1735         {
1736             LPVOID *mem; 
1737             if (ISV86(context))
1738               {
1739                 mem= DOSMEM_GetBlock(0,(DWORD)BX_reg(context)<<4,NULL);
1740             if (mem)
1741                 AX_reg(context) = DOSMEM_MapLinearToDos(mem)>>4;
1742               }
1743             else
1744               {
1745                 mem = (LPVOID)GlobalDOSAlloc16(BX_reg(context)<<4);
1746                 if (mem)
1747                   AX_reg(context) = (DWORD)mem&0xffff;
1748               }
1749             if (!mem)
1750               {
1751                 SET_CFLAG(context);
1752                 AX_reg(context) = 0x0008; /* insufficient memory */
1753                 BX_reg(context) = DOSMEM_Available(0)>>4;
1754             }
1755         }
1756         break;
1757
1758     case 0x49: /* FREE MEMORY */
1759         TRACE("FREE MEMORY segment %04lX\n", ES_reg(context));
1760         {
1761           BOOL ret;
1762           if (ISV86(context))
1763             ret= DOSMEM_FreeBlock(0,DOSMEM_MapDosToLinear(ES_reg(context)<<4));
1764           else
1765             {
1766               ret = !GlobalDOSFree16(ES_reg(context));
1767               /* If we don't reset ES_reg, we will fail in the relay code */
1768               ES_reg(context)=ret;
1769             }
1770           if (!ret)
1771             {
1772               TRACE("FREE MEMORY failed\n");
1773             SET_CFLAG(context);
1774             AX_reg(context) = 0x0009; /* memory block address invalid */
1775         }
1776         }
1777         break;
1778
1779     case 0x4a: /* RESIZE MEMORY BLOCK */
1780         TRACE("RESIZE MEMORY segment %04lX to %d paragraphs\n", ES_reg(context), BX_reg(context));
1781         if (!ISV86(context))
1782           FIXME("RESIZE MEMORY probably insufficient implementation. Expect crash soon\n");
1783         {
1784             LPVOID *mem = DOSMEM_ResizeBlock(0,DOSMEM_MapDosToLinear(ES_reg(context)<<4),
1785                                                BX_reg(context)<<4,NULL);
1786             if (mem)
1787                 AX_reg(context) = DOSMEM_MapLinearToDos(mem)>>4;
1788             else {
1789                 SET_CFLAG(context);
1790                 AX_reg(context) = 0x0008; /* insufficient memory */
1791                 BX_reg(context) = DOSMEM_Available(0)>>4; /* not quite right */
1792             }
1793         }
1794         break;
1795
1796     case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1797         TRACE("EXEC %s\n",
1798               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),EDX_reg(context) ));
1799         AX_reg(context) = WinExec16( CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),
1800                                                          EDX_reg(context) ),
1801                                      SW_NORMAL );
1802         if (AX_reg(context) < 32) SET_CFLAG(context);
1803         break;          
1804         
1805     case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1806         TRACE("EXIT with return code %d\n",AL_reg(context));
1807         ExitProcess( AL_reg(context) );
1808         break;
1809
1810     case 0x4d: /* GET RETURN CODE */
1811         TRACE("GET RETURN CODE (ERRORLEVEL)\n");
1812         AX_reg(context) = 0; /* normal exit */
1813         break;
1814
1815     case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1816         TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
1817               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1818         if (!INT21_FindFirst(context)) break;
1819         /* fall through */
1820
1821     case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1822         TRACE("FINDNEXT\n");
1823         if (!INT21_FindNext(context))
1824         {
1825             SetLastError( ERROR_NO_MORE_FILES );
1826             AX_reg(context) = ERROR_NO_MORE_FILES;
1827             SET_CFLAG(context);
1828         }
1829         else AX_reg(context) = 0;  /* OK */
1830         break;
1831     case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
1832         TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
1833         INT21_SetCurrentPSP(BX_reg(context));
1834         break;
1835     case 0x51: /* GET PSP ADDRESS */
1836         TRACE("GET CURRENT PROCESS ID (GET PSP ADDRESS)\n");
1837         /* FIXME: should we return the original DOS PSP upon */
1838         /*        Windows startup ? */
1839         BX_reg(context) = INT21_GetCurrentPSP();
1840         break;
1841     case 0x62: /* GET PSP ADDRESS */
1842         TRACE("GET CURRENT PSP ADDRESS\n");
1843         /* FIXME: should we return the original DOS PSP upon */
1844         /*        Windows startup ? */
1845         BX_reg(context) = INT21_GetCurrentPSP();
1846         break;
1847
1848     case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1849         TRACE("SYSVARS - GET LIST OF LISTS\n");
1850         {
1851             ES_reg(context) = ISV86(context) ? HIWORD(DOS_LOLSeg) : LOWORD(DOS_LOLSeg);
1852             BX_reg(context) = FIELD_OFFSET(DOS_LISTOFLISTS, ptr_first_DPB);
1853         }
1854         break;
1855
1856     case 0x54: /* Get Verify Flag */
1857         TRACE("Get Verify Flag - Not Supported\n");
1858         AL_reg(context) = 0x00;  /* pretend we can tell. 00h = off 01h = on */
1859         break;
1860
1861     case 0x56: /* "RENAME" - RENAME FILE */
1862         TRACE("RENAME %s to %s\n",
1863               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
1864               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, ES_reg(context),EDI_reg(context)));
1865         bSetDOSExtendedError = 
1866                 (!MoveFileA( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
1867                                CTX_SEG_OFF_TO_LIN(context, ES_reg(context),EDI_reg(context))));
1868         break;
1869
1870     case 0x57: /* FILE DATE AND TIME */
1871         switch (AL_reg(context))
1872         {
1873         case 0x00:  /* Get */
1874             {
1875                 FILETIME filetime;
1876                 TRACE("GET FILE DATE AND TIME for handle %d\n",
1877                       BX_reg(context));
1878                 if (!GetFileTime( FILE_GetHandle(BX_reg(context)), NULL, NULL, &filetime ))
1879                      bSetDOSExtendedError = TRUE;
1880                 else FileTimeToDosDateTime( &filetime, &DX_reg(context),
1881                                             &CX_reg(context) );
1882             }
1883             break;
1884
1885         case 0x01:  /* Set */
1886             {
1887                 FILETIME filetime;
1888                 TRACE("SET FILE DATE AND TIME for handle %d\n",
1889                       BX_reg(context));
1890                 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
1891                                        &filetime );
1892                 bSetDOSExtendedError = 
1893                         (!SetFileTime( FILE_GetHandle(BX_reg(context)),
1894                                       NULL, NULL, &filetime ));
1895             }
1896             break;
1897         }
1898         break;
1899
1900     case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1901         TRACE("GET OR SET MEMORY/UMB ALLOCATION STRATEGY subfunction %d\n",
1902               AL_reg(context));
1903         switch (AL_reg(context))
1904         {
1905         case 0x00:
1906             AX_reg(context) = 1;
1907             break;
1908         case 0x02:
1909             AX_reg(context) = 0;
1910             break;
1911         case 0x01:
1912         case 0x03:
1913             break;
1914         }
1915         RESET_CFLAG(context);
1916         break;
1917
1918     case 0x5a: /* CREATE TEMPORARY FILE */
1919         TRACE("CREATE TEMPORARY FILE\n");
1920         bSetDOSExtendedError = !INT21_CreateTempFile(context);
1921         break;
1922
1923     case 0x5b: /* CREATE NEW FILE */
1924         TRACE("CREATE NEW FILE 0x%02x for %s\n", CX_reg(context),
1925               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)));
1926         bSetDOSExtendedError = ((AX_reg(context) = 
1927                _lcreat16_uniq( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
1928                                CX_reg(context) )) == (WORD)HFILE_ERROR16);
1929         break;
1930
1931     case 0x5d: /* NETWORK */
1932         FIXME("Function 0x%04x not implemented.\n", AX_reg (context));
1933         /* Fix the following while you're at it.  */
1934         SetLastError( ER_NoNetwork );
1935         bSetDOSExtendedError = TRUE;
1936         break;
1937
1938     case 0x5e:
1939         bSetDOSExtendedError = INT21_networkfunc (context);
1940         break;
1941
1942     case 0x5f: /* NETWORK */
1943         switch (AL_reg(context))
1944         {
1945         case 0x07: /* ENABLE DRIVE */
1946             TRACE("ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1947             if (!DRIVE_Enable( DL_reg(context) ))
1948             {
1949                 SetLastError( ERROR_INVALID_DRIVE );
1950                 bSetDOSExtendedError = TRUE;
1951             }
1952             break;
1953
1954         case 0x08: /* DISABLE DRIVE */
1955             TRACE("DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1956             if (!DRIVE_Disable( DL_reg(context) ))
1957             {
1958                 SetLastError( ERROR_INVALID_DRIVE );
1959                 bSetDOSExtendedError = TRUE;
1960             } 
1961             break;
1962
1963         default:
1964             /* network software not installed */
1965             TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
1966             SetLastError( ER_NoNetwork );
1967             bSetDOSExtendedError = TRUE;
1968             break;
1969         }
1970         break;
1971
1972     case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1973         TRACE("TRUENAME %s\n",
1974               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),ESI_reg(context)));
1975         {
1976             if (!GetFullPathNameA( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1977                                                         ESI_reg(context)), 128,
1978                                      CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
1979                                                         EDI_reg(context)),NULL))
1980                 bSetDOSExtendedError = TRUE;
1981             else AX_reg(context) = 0;
1982         }
1983         break;
1984
1985     case 0x61: /* UNUSED */
1986     case 0x63: /* misc. language support */
1987         switch (AL_reg(context)) {
1988         case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
1989             INT21_GetDBCSLeadTable(context);
1990             break;
1991         }
1992         break;
1993     case 0x64: /* OS/2 DOS BOX */
1994         INT_BARF( context, 0x21 );
1995         SET_CFLAG(context);
1996         break;
1997
1998     case 0x65:{/* GET EXTENDED COUNTRY INFORMATION */
1999         extern WORD WINE_LanguageId;
2000         BYTE    *dataptr=CTX_SEG_OFF_TO_LIN(context, ES_reg(context),EDI_reg(context));
2001         TRACE("GET EXTENDED COUNTRY INFORMATION code page %d country %d\n",
2002               BX_reg(context), DX_reg(context));
2003         switch (AL_reg(context)) {
2004         case 0x01:
2005             TRACE("\tget general internationalization info\n");
2006             dataptr[0] = 0x1;
2007             *(WORD*)(dataptr+1) = 41;
2008             *(WORD*)(dataptr+3) = WINE_LanguageId;
2009             *(WORD*)(dataptr+5) = CodePage;
2010             *(DWORD*)(dataptr+0x19) = 0; /* FIXME: ptr to case map routine */
2011             break;
2012         case 0x06:
2013             TRACE("\tget pointer to collating sequence table\n");
2014             dataptr[0] = 0x06;
2015             *(DWORD*)(dataptr+1) = MAKELONG(DOSMEM_CollateTable & 0xFFFF,DOSMEM_AllocSelector(DOSMEM_CollateTable>>16));
2016             CX_reg(context)         = 258;/*FIXME: size of table?*/
2017             break;
2018         default:
2019             TRACE("\tunimplemented function %d\n",AL_reg(context));
2020             INT_BARF( context, 0x21 );
2021             SET_CFLAG(context);
2022             break;
2023         }
2024         break;
2025     }
2026     case 0x66: /* GLOBAL CODE PAGE TABLE */
2027         switch (AL_reg(context))
2028         {
2029         case 0x01:
2030             TRACE("GET GLOBAL CODE PAGE TABLE\n");
2031             DX_reg(context) = BX_reg(context) = CodePage;
2032             RESET_CFLAG(context);
2033             break;                      
2034         case 0x02: 
2035             TRACE("SET GLOBAL CODE PAGE TABLE active page %d system page %d\n",
2036                   BX_reg(context),DX_reg(context));
2037             CodePage = BX_reg(context);
2038             RESET_CFLAG(context);
2039             break;
2040         }
2041         break;
2042
2043     case 0x67: /* SET HANDLE COUNT */
2044         TRACE("SET HANDLE COUNT to %d\n",BX_reg(context) );
2045         SetHandleCount16( BX_reg(context) );
2046         if (GetLastError()) bSetDOSExtendedError = TRUE;
2047         break;
2048
2049     case 0x68: /* "FFLUSH" - COMMIT FILE */
2050     case 0x6a: /* COMMIT FILE */
2051         TRACE("FFLUSH/COMMIT handle %d\n",BX_reg(context));
2052         bSetDOSExtendedError = (!FlushFileBuffers( FILE_GetHandle(BX_reg(context)) ));
2053         break;          
2054         
2055     case 0x69: /* DISK SERIAL NUMBER */
2056         switch (AL_reg(context))
2057         {
2058         case 0x00:
2059             TRACE("GET DISK SERIAL NUMBER for drive %s\n",
2060                   INT21_DriveName(BL_reg(context)));
2061             if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
2062             else AX_reg(context) = 0;
2063             break;
2064
2065         case 0x01:
2066             TRACE("SET DISK SERIAL NUMBER for drive %s\n",
2067                   INT21_DriveName(BL_reg(context)));
2068             if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
2069             else AX_reg(context) = 1;
2070             break;
2071         }
2072         break;
2073     
2074     case 0x6C: /* Extended Open/Create*/
2075         TRACE("EXTENDED OPEN/CREATE %s\n",
2076               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDI_reg(context)));
2077         bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
2078         break;
2079         
2080     case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
2081         if ((GetVersion()&0xC0000004)!=0xC0000004) {
2082             /* not supported on anything but Win95 */
2083             TRACE("LONG FILENAME functions supported only by win95\n");
2084             SET_CFLAG(context);
2085             AL_reg(context) = 0;
2086         } else
2087         switch(AL_reg(context))
2088         {
2089         case 0x39:  /* Create directory */
2090             TRACE("LONG FILENAME - MAKE DIRECTORY %s\n",
2091                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),EDX_reg(context)));
2092             bSetDOSExtendedError = (!CreateDirectoryA( 
2093                                         CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),
2094                                                   EDX_reg(context) ), NULL));
2095             break;
2096         case 0x3a:  /* Remove directory */
2097             TRACE("LONG FILENAME - REMOVE DIRECTORY %s\n",
2098                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),EDX_reg(context)));
2099             bSetDOSExtendedError = (!RemoveDirectoryA( 
2100                                         CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),
2101                                                         EDX_reg(context) )));
2102             break;
2103         case 0x43:  /* Get/Set file attributes */
2104           TRACE("LONG FILENAME -EXTENDED GET/SET FILE ATTRIBUTES %s\n",
2105                 (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),EDX_reg(context)));
2106         switch (BL_reg(context))
2107         {
2108         case 0x00: /* Get file attributes */
2109             TRACE("\tretrieve attributes\n");
2110             CX_reg(context) = (WORD)GetFileAttributesA(
2111                                           CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2112                                                              EDX_reg(context)));
2113             if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
2114             break;
2115         case 0x01:
2116             TRACE("\tset attributes 0x%04x\n",CX_reg(context));
2117             bSetDOSExtendedError = (!SetFileAttributesA( 
2118                                         CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2119                                                            EDX_reg(context)),
2120                                         CX_reg(context)  ) );
2121             break;
2122         default:
2123           FIXME("Unimplemented long file name function:\n");
2124           INT_BARF( context, 0x21 );
2125           SET_CFLAG(context);
2126           AL_reg(context) = 0;
2127           break;
2128         }
2129         break;
2130         case 0x47:  /* Get current directory */
2131             TRACE(" LONG FILENAME - GET CURRENT DIRECTORY for drive %s\n",
2132                   INT21_DriveName(DL_reg(context)));
2133             bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
2134             break;
2135
2136         case 0x4e:  /* Find first file */
2137             TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
2138                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)));
2139             /* FIXME: use attributes in CX */
2140             if ((AX_reg(context) = FindFirstFile16(
2141                    CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
2142                    (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2143                                                             EDI_reg(context))))
2144                 == INVALID_HANDLE_VALUE16)
2145                 bSetDOSExtendedError = TRUE;
2146             break;
2147         case 0x4f:  /* Find next file */
2148             TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
2149                   BX_reg(context));
2150             if (!FindNextFile16( BX_reg(context),
2151                     (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2152                                                              EDI_reg(context))))
2153                 bSetDOSExtendedError = TRUE;
2154             break;
2155         case 0xa1:  /* Find close */
2156             TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
2157                   BX_reg(context));
2158             bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
2159             break;
2160         case 0xa0:
2161             TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive %s stub\n",
2162                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context),EDX_reg(context)));
2163             break;
2164         case 0x60:  
2165           switch(CL_reg(context))
2166           {
2167             case 0x01:  /*Get short filename or path */
2168               if (!GetShortPathNameA
2169                   ( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2170                                        ESI_reg(context)),
2171                     CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2172                                        EDI_reg(context)), 67))
2173                 bSetDOSExtendedError = TRUE;
2174               else AX_reg(context) = 0;
2175               break;
2176             case 0x02:  /*Get canonical long filename or path */
2177               if (!GetFullPathNameA
2178                   ( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2179                                        ESI_reg(context)), 128,
2180                     CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2181                                        EDI_reg(context)),NULL))
2182                 bSetDOSExtendedError = TRUE;
2183               else AX_reg(context) = 0;
2184               break;
2185             default:
2186               FIXME("Unimplemented long file name function:\n");
2187               INT_BARF( context, 0x21 );
2188               SET_CFLAG(context);
2189               AL_reg(context) = 0;
2190               break;
2191           }
2192             break;
2193         case 0x6c:  /* Create or open file */
2194             TRACE("LONG FILENAME - CREATE OR OPEN FILE %s\n",
2195                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), ESI_reg(context))); 
2196           /* translate Dos 7 action to Dos 6 action */
2197             bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
2198             break;
2199
2200         case 0x3b:  /* Change directory */
2201             TRACE("LONG FILENAME - CHANGE DIRECTORY %s\n",
2202                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context))); 
2203             if (!SetCurrentDirectoryA(CTX_SEG_OFF_TO_LIN(context, 
2204                                                 DS_reg(context),
2205                                                 EDX_reg(context)
2206                                         ))
2207             ) {
2208                 SET_CFLAG(context);
2209                 AL_reg(context) = GetLastError();
2210             }
2211             break;
2212         case 0x41:  /* Delete file */
2213             TRACE("LONG FILENAME - DELETE FILE %s\n",
2214                  (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context))); 
2215             if (!DeleteFileA(CTX_SEG_OFF_TO_LIN(context, 
2216                                         DS_reg(context),
2217                                         EDX_reg(context))
2218             )) {
2219                 SET_CFLAG(context);
2220                 AL_reg(context) = GetLastError();
2221             }
2222             break;
2223         case 0x56:  /* Move (rename) file */
2224             TRACE("LONG FILENAME - RENAME FILE %s to %s stub\n",
2225                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  DS_reg(context), EDX_reg(context)),
2226                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  ES_reg(context), EDI_reg(context))); 
2227         default:
2228             FIXME("Unimplemented long file name function:\n");
2229             INT_BARF( context, 0x21 );
2230             SET_CFLAG(context);
2231             AL_reg(context) = 0;
2232             break;
2233         }
2234         break;
2235
2236     case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
2237     case 0x72: /* MS-DOS 7 (Windows95) - ??? */
2238     case 0x73: /* MS-DOS 7 (Windows95) - DRIVE LOCKING ??? */
2239         TRACE("windows95 function AX %04x\n",
2240                     AX_reg(context));
2241         WARN("        returning unimplemented\n");
2242         SET_CFLAG(context);
2243         AL_reg(context) = 0;
2244         break;
2245
2246     case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
2247     case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
2248         break;
2249
2250     default:
2251         INT_BARF( context, 0x21 );
2252         break;
2253
2254     } /* END OF SWITCH */
2255
2256     if( bSetDOSExtendedError )          /* set general error condition */
2257     {   
2258         AX_reg(context) = GetLastError();
2259         SET_CFLAG(context);
2260     }
2261
2262     if ((EFL_reg(context) & 0x0001))
2263         TRACE("failed, error 0x%04lx\n", GetLastError() );
2264  
2265     TRACE("returning: AX=%04x BX=%04x CX=%04x DX=%04x "
2266                  "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
2267                  AX_reg(context), BX_reg(context), CX_reg(context),
2268                  DX_reg(context), SI_reg(context), DI_reg(context),
2269                  (WORD)DS_reg(context), (WORD)ES_reg(context),
2270                  EFL_reg(context));
2271 }
2272
2273 FARPROC16 WINAPI GetSetKernelDOSProc16(FARPROC16 DosProc)
2274 {
2275         FIXME("(DosProc=0x%08x): stub\n", (UINT)DosProc);
2276         return NULL;
2277 }
2278