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