Release 980628
[wine] / msdos / int21.c
1 /*
2  * DOS interrupt 21h handler
3  */
4
5 #include <time.h>
6 #include <fcntl.h>
7 #include <errno.h>
8 #include <stdlib.h>
9 #include <sys/file.h>
10 #include <string.h>
11 #include <sys/stat.h>
12 #include <sys/time.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15 #include <utime.h>
16 #include <ctype.h>
17 #include "windows.h"
18 #include "drive.h"
19 #include "file.h"
20 #include "heap.h"
21 #include "msdos.h"
22 #include "ldt.h"
23 #include "task.h"
24 #include "options.h"
25 #include "miscemu.h"
26 #include "debug.h"
27 #if defined(__svr4__) || defined(_SCO_DS)
28 /* SVR4 DOESNT do locking the same way must implement properly */
29 #define LOCK_EX 0
30 #define LOCK_SH  1
31 #define LOCK_NB  8
32 #endif
33
34
35 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
36
37 /* Define the drive parameter block, as used by int21/1F
38  * and int21/32.  This table can be accessed through the
39  * global 'dpb' pointer, which points into the local dos
40  * heap.
41  */
42 struct DPB
43 {
44     BYTE drive_num;         /* 0=A, etc. */
45     BYTE unit_num;          /* Drive's unit number (?) */
46     WORD sector_size;       /* Sector size in bytes */
47     BYTE high_sector;       /* Highest sector in a cluster */
48     BYTE shift;             /* Shift count (?) */
49     WORD reserved;          /* Number of reserved sectors at start */
50     BYTE num_FAT;           /* Number of FATs */
51     WORD dir_entries;       /* Number of root dir entries */
52     WORD first_data;        /* First data sector */
53     WORD high_cluster;      /* Highest cluster number */
54     WORD sectors_in_FAT;    /* Number of sectors per FAT */
55     WORD start_dir;         /* Starting sector of first dir */
56     DWORD driver_head;      /* Address of device driver header (?) */
57     BYTE media_ID;          /* Media ID */
58     BYTE access_flag;       /* Prev. accessed flag (0=yes,0xFF=no) */
59     DWORD next;             /* Pointer to next DPB in list */
60     WORD free_search;       /* Free cluster search start */
61     WORD free_clusters;     /* Number of free clusters (0xFFFF=unknown) */
62 };
63
64 WORD CodePage = 437;
65 DWORD dpbsegptr;
66
67 struct DosHeap {
68         BYTE InDosFlag;
69         BYTE mediaID;
70         BYTE biosdate[8];
71         struct DPB dpb;
72 };
73 static struct DosHeap *heap;
74 static WORD DosHeapHandle;
75
76 WORD sharing_retries = 3;      /* number of retries at sharing violation */
77 WORD sharing_pause = 1;        /* pause between retries */
78
79 extern char TempDirectory[];
80
81 static BOOL32 INT21_CreateHeap(void)
82 {
83     if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
84     {
85         WARN(int21, "Out of memory\n");
86         return FALSE;
87     }
88     heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
89     dpbsegptr = PTR_SEG_OFF_TO_SEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
90     heap->InDosFlag = 0;
91     strcpy(heap->biosdate, "01/01/80");
92     return TRUE;
93 }
94
95 static BYTE *GetCurrentDTA(void)
96 {
97     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
98     return (BYTE *)PTR_SEG_TO_LIN( pTask->dta );
99 }
100
101
102 void CreateBPB(int drive, BYTE *data, BOOL16 limited)
103 /* limited == TRUE is used with INT 0x21/0x440d */
104 {
105         if (drive > 1) {
106                 setword(data, 512);
107                 data[2] = 2;
108                 setword(&data[3], 0);
109                 data[5] = 2;
110                 setword(&data[6], 240);
111                 setword(&data[8], 64000);
112                 data[0x0a] = 0xf8;
113                 setword(&data[0x0b], 40);
114                 setword(&data[0x0d], 56);
115                 setword(&data[0x0f], 2);
116                 setword(&data[0x11], 0);
117                 if (!limited) {
118                     setword(&data[0x1f], 800);
119                     data[0x21] = 5;
120                     setword(&data[0x22], 1);
121                 }
122         } else { /* 1.44mb */
123                 setword(data, 512);
124                 data[2] = 2;
125                 setword(&data[3], 0);
126                 data[5] = 2;
127                 setword(&data[6], 240);
128                 setword(&data[8], 2880);
129                 data[0x0a] = 0xf8;
130                 setword(&data[0x0b], 6);
131                 setword(&data[0x0d], 18);
132                 setword(&data[0x0f], 2);
133                 setword(&data[0x11], 0);
134                 if (!limited) {
135                     setword(&data[0x1f], 80);
136                     data[0x21] = 7;
137                     setword(&data[0x22], 2);
138                 }
139         }       
140 }
141
142 static int INT21_GetFreeDiskSpace( CONTEXT *context )
143 {
144     DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
145     char root[] = "A:\\";
146
147     *root += DOS_GET_DRIVE( DL_reg(context) );
148     if (!GetDiskFreeSpace32A( root, &cluster_sectors, &sector_bytes,
149                               &free_clusters, &total_clusters )) return 0;
150     AX_reg(context) = cluster_sectors;
151     BX_reg(context) = free_clusters;
152     CX_reg(context) = sector_bytes;
153     DX_reg(context) = total_clusters;
154     return 1;
155 }
156
157 static int INT21_GetDriveAllocInfo( CONTEXT *context )
158 {
159     if (!INT21_GetFreeDiskSpace( context )) return 0;
160     if (!heap && !INT21_CreateHeap()) return 0;
161     heap->mediaID = 0xf0;
162     DS_reg(context) = DosHeapHandle;
163     BX_reg(context) = (int)&heap->mediaID - (int)heap;
164     return 1;
165 }
166
167 static void GetDrivePB( CONTEXT *context, int drive )
168 {
169         if(!DRIVE_IsValid(drive))
170         {
171             DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
172             AX_reg(context) = 0x00ff;
173         }
174         else if (heap || INT21_CreateHeap())
175         {
176                 FIXME(int21, "GetDrivePB not fully implemented.\n");
177
178                 /* FIXME: I have no idea what a lot of this information should
179                  * say or whether it even really matters since we're not allowing
180                  * direct block access.  However, some programs seem to depend on
181                  * getting at least _something_ back from here.  The 'next' pointer
182                  * does worry me, though.  Should we have a complete table of
183                  * separate DPBs per drive?  Probably, but I'm lazy. :-)  -CH
184                  */
185                 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
186                 heap->dpb.sector_size = 512;
187                 heap->dpb.high_sector = 1;
188                 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
189                 heap->dpb.reserved = 0;
190                 heap->dpb.num_FAT = 1;
191                 heap->dpb.dir_entries = 2;
192                 heap->dpb.first_data = 2;
193                 heap->dpb.high_cluster = 64000;
194                 heap->dpb.sectors_in_FAT = 1;
195                 heap->dpb.start_dir = 1;
196                 heap->dpb.driver_head = 0;
197                 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
198                 heap->dpb.access_flag = 0;
199                 heap->dpb.next = 0;
200                 heap->dpb.free_search = 0;
201                 heap->dpb.free_clusters = 0xFFFF;    /* unknown */
202
203                 AL_reg(context) = 0x00;
204                 DS_reg(context) = SELECTOROF(dpbsegptr);
205                 BX_reg(context) = OFFSETOF(dpbsegptr);
206         }
207 }
208
209
210 static void ioctlGetDeviceInfo( CONTEXT *context )
211 {
212     int curr_drive;
213     TRACE(int21, "(%d)\n", BX_reg(context));
214     
215     curr_drive = DRIVE_GetCurrentDrive();
216     DX_reg(context) = 0x0140 + curr_drive + ((curr_drive > 1) ? 0x0800 : 0); 
217     /* no floppy */
218     /* bits 0-5 are current drive
219      * bit 6 - file has NOT been written..FIXME: correct?
220      * bit 8 - generate int24 if no diskspace on write/ read past end of file
221      * bit 11 - media not removable
222      * bit 14 - don't set file date/time on closing
223      * bit 15 - file is remote
224      */
225     RESET_CFLAG(context);
226 }
227
228 static BOOL32 ioctlGenericBlkDevReq( CONTEXT *context )
229 {
230         BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
231         int drive = DOS_GET_DRIVE( BL_reg(context) );
232
233         if (!DRIVE_IsValid(drive))
234         {
235             DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
236             return TRUE;
237         }
238
239         if (CH_reg(context) != 0x08)
240         {
241             INT_BARF( context, 0x21 );
242             return FALSE;
243         }
244
245         switch (CL_reg(context)) 
246         {
247                 case 0x4a: /* lock logical volume */
248                         TRACE(int21,"lock logical volume (%d) level %d mode %d\n",drive,BH_reg(context),DX_reg(context));
249                         break;
250
251                 case 0x60: /* get device parameters */
252                            /* used by w4wgrp's winfile */
253                         memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
254                         dataptr[0] = 0x04;
255                         dataptr[6] = 0; /* media type */
256                         if (drive > 1) 
257                         {
258                                 dataptr[1] = 0x05; /* fixed disk */
259                                 setword(&dataptr[2], 0x01); /* non removable */
260                                 setword(&dataptr[4], 0x300); /* # of cylinders */
261                         }
262                         else
263                         {
264                                 dataptr[1] = 0x07; /* block dev, floppy */
265                                 setword(&dataptr[2], 0x02); /* removable */
266                                 setword(&dataptr[4], 80); /* # of cylinders */
267                         }
268                         CreateBPB(drive, &dataptr[7], TRUE);
269                         RESET_CFLAG(context);
270                         break;
271
272                 case 0x66:/*  get disk serial number */
273                         {       
274                                 char    label[12],fsname[9],path[4];
275                                 DWORD   serial;
276
277                                 strcpy(path,"x:\\");path[0]=drive+'A';
278                                 GetVolumeInformation32A(
279                                         path,label,12,&serial,NULL,NULL,fsname,9
280                                 );
281                                 *(WORD*)dataptr         = 0;
282                                 memcpy(dataptr+2,&serial,4);
283                                 memcpy(dataptr+6,label  ,11);
284                                 memcpy(dataptr+17,fsname,8);
285                         }
286                         break;
287
288                 case 0x6a:
289                         TRACE(int21,"logical volume %d unlocked.\n",drive);
290                         break;
291
292                 case 0x6f:
293                         memset(dataptr+1, '\0', dataptr[0]-1);
294                         dataptr[1] = dataptr[0]; 
295                         dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
296                         dataptr[3] = 0xFF; /* no physical drive */
297                         break;
298
299                 default:
300                         INT_BARF( context, 0x21 );
301         }
302         return FALSE;
303 }
304
305 static void INT21_GetSystemDate( CONTEXT *context )
306 {
307     SYSTEMTIME systime;
308     GetLocalTime( &systime );
309     CX_reg(context) = systime.wYear;
310     DX_reg(context) = (systime.wMonth << 8) | systime.wDay;
311     AX_reg(context) = systime.wDayOfWeek;
312 }
313
314 static void INT21_GetSystemTime( CONTEXT *context )
315 {
316     SYSTEMTIME systime;
317     GetLocalTime( &systime );
318     CX_reg(context) = (systime.wHour << 8) | systime.wMinute;
319     DX_reg(context) = (systime.wSecond << 8) | (systime.wMilliseconds / 10);
320 }
321
322 /* Many calls translate a drive argument like this:
323    drive number (00h = default, 01h = A:, etc)
324    */  
325 static char drivestring[]="default";
326
327 char *INT21_DriveName(int drive)
328 {
329
330     if(drive >0)
331       {
332         drivestring[0]= (unsigned char)drive + '@';
333         drivestring[1]=':';
334         drivestring[2]=0;
335       }
336     return drivestring;
337 }
338 static BOOL32 INT21_CreateFile( CONTEXT *context )
339 {
340     AX_reg(context) = _lcreat16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
341                                           DX_reg(context) ), CX_reg(context) );
342     return (AX_reg(context) == (WORD)HFILE_ERROR16);
343 }
344
345
346 static void OpenExistingFile( CONTEXT *context )
347 {
348     AX_reg(context) = _lopen16( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
349                               AL_reg(context) );
350     if (AX_reg(context) == (WORD)HFILE_ERROR16)
351     {
352         AX_reg(context) = DOS_ExtendedError;
353         SET_CFLAG(context);
354     }
355 #if 0
356     {
357         int handle;
358         int mode;
359         int lock;
360
361         switch (AX_reg(context) & 0x0070)
362         {
363           case 0x00:    /* compatability mode */
364           case 0x40:    /* DENYNONE */
365             lock = -1;
366             break;
367
368           case 0x30:    /* DENYREAD */
369             TRACE(int21, "(%s): DENYREAD changed to DENYALL\n",
370                          (char *)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)));
371           case 0x10:    /* DENYALL */  
372             lock = LOCK_EX;
373             break;
374
375           case 0x20:    /* DENYWRITE */
376             lock = LOCK_SH;
377             break;
378
379           default:
380             lock = -1;
381         }
382
383         if (lock != -1)
384         {
385
386           int result,retries=sharing_retries;
387           {
388 #if defined(__svr4__) || defined(_SCO_DS)
389               ERR(int21, "Should call flock and needs porting to lockf\n");
390               result = 0;
391               retries = 0;
392 #else
393             result = flock(handle, lock | LOCK_NB);
394 #endif
395             if ( retries && (!result) )
396             {
397               int i;
398               for(i=0;i<32768*((int)sharing_pause);i++)
399                   result++;                          /* stop the optimizer */
400               for(i=0;i<32768*((int)sharing_pause);i++)
401                   result--;
402             }
403           }
404           while( (!result) && (!(retries--)) );
405
406           if(result)  
407           {
408             errno_to_doserr();
409             AX_reg(context) = DOS_ExtendedError;
410             close(handle);
411             SET_CFLAG(context);
412             return;
413           }
414
415         }
416
417         Error (0,0,0);
418         AX_reg(context) = handle;
419         RESET_CFLAG(context);
420     }
421 #endif
422 }
423
424 static void CloseFile( CONTEXT *context )
425 {
426     if ((AX_reg(context) = _lclose16( BX_reg(context) )) != 0)
427     {
428         AX_reg(context) = DOS_ExtendedError;
429         SET_CFLAG(context);
430     }
431 }
432
433 static BOOL32 INT21_ExtendedOpenCreateFile(CONTEXT *context )
434 {
435   BOOL32 bExtendedError = FALSE;
436   BYTE action = DL_reg(context);
437
438   /* Shuffle arguments to call OpenExistingFile */
439   AL_reg(context) = BL_reg(context);
440   DX_reg(context) = SI_reg(context);
441   /* BX,CX and DX should be preserved */
442   OpenExistingFile(context);
443
444   if ((EFL_reg(context) & 0x0001) == 0) /* File exists */
445   {
446       UINT16    uReturnCX = 0;
447
448       /* Now decide what do do */
449
450       if ((action & 0x07) == 0)
451       {
452           BX_reg(context) = AX_reg(context);
453           CloseFile(context);
454           AX_reg(context) = 0x0050;     /*File exists*/
455           SET_CFLAG(context);
456           WARN(int21, "extended open/create: failed because file exists \n");
457       }
458       else if ((action & 0x07) == 2) 
459       {
460         /* Truncate it, but first check if opened for write */
461         if ((BL_reg(context) & 0x0007)== 0) 
462         {
463                   BX_reg(context) = AX_reg(context);
464                   CloseFile(context);
465                   WARN(int21, "extended open/create: failed, trunc on ro file\n");
466                   AX_reg(context) = 0x000C;     /*Access code invalid*/
467                   SET_CFLAG(context);
468         }
469         else
470         {
471                 /* Shuffle arguments to call CloseFile while
472                  * preserving BX and DX */
473
474                 TRACE(int21, "extended open/create: Closing before truncate\n");
475                 BX_reg(context) = AX_reg(context);
476                 CloseFile(context);
477                 if (EFL_reg(context) & 0x0001) 
478                 {
479                    WARN(int21, "extended open/create: close before trunc failed\n");
480                    AX_reg(context) = 0x0019;    /*Seek Error*/
481                    CX_reg(context) = 0;
482                    SET_CFLAG(context);
483                 }
484                 /* Shuffle arguments to call CreateFile */
485
486                 TRACE(int21, "extended open/create: Truncating\n");
487                 AL_reg(context) = BL_reg(context);
488                 /* CX is still the same */
489                 DX_reg(context) = SI_reg(context);
490                 bExtendedError = INT21_CreateFile(context);
491
492                 if (EFL_reg(context) & 0x0001)  /*no file open, flags set */
493                 {
494                     WARN(int21, "extended open/create: trunc failed\n");
495                     return bExtendedError;
496                 }
497                 uReturnCX = 0x3;
498         }
499       } 
500       else uReturnCX = 0x1;
501
502       CX_reg(context) = uReturnCX;
503   }
504   else /* file does not exist */
505   {
506       RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
507       if ((action & 0xF0)== 0)
508       {
509         CX_reg(context) = 0;
510         SET_CFLAG(context);
511         WARN(int21, "extended open/create: failed, file dosen't exist\n");
512       }
513       else
514       {
515         /* Shuffle arguments to call CreateFile */
516         TRACE(int21, "extended open/create: Creating\n");
517         AL_reg(context) = BL_reg(context);
518         /* CX should still be the same */
519         DX_reg(context) = SI_reg(context);
520         bExtendedError = INT21_CreateFile(context);
521         if (EFL_reg(context) & 0x0001)  /*no file open, flags set */
522         {
523             WARN(int21, "extended open/create: create failed\n");
524             return bExtendedError;
525         }
526         CX_reg(context) = 2;
527       }
528   }
529
530   return bExtendedError;
531 }
532
533
534 static BOOL32 INT21_ChangeDir( CONTEXT *context )
535 {
536     int drive;
537     char *dirname = PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context));
538
539     TRACE(int21,"changedir %s\n", dirname);
540     if (dirname[0] && (dirname[1] == ':'))
541     {
542         drive = toupper(dirname[0]) - 'A';
543         dirname += 2;
544     }
545     else drive = DRIVE_GetCurrentDrive();
546     return DRIVE_Chdir( drive, dirname );
547 }
548
549
550 static int INT21_FindFirst( CONTEXT *context )
551 {
552     char *p;
553     const char *path;
554     DOS_FULL_NAME full_name;
555     FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA();
556
557     path = (const char *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
558     dta->unixPath = NULL;
559     if (!DOSFS_GetFullName( path, FALSE, &full_name ))
560     {
561         AX_reg(context) = DOS_ExtendedError;
562         SET_CFLAG(context);
563         return 0;
564     }
565     dta->unixPath = HEAP_strdupA( GetProcessHeap(), 0, full_name.long_name );
566     p = strrchr( dta->unixPath, '/' );
567     *p = '\0';
568
569     /* Note: terminating NULL in dta->mask overwrites dta->search_attr
570      *       (doesn't matter as it is set below anyway)
571      */
572     if (!DOSFS_ToDosFCBFormat( p + 1, dta->mask ))
573     {
574         HeapFree( GetProcessHeap(), 0, dta->unixPath );
575         dta->unixPath = NULL;
576         DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
577         AX_reg(context) = ER_FileNotFound;
578         SET_CFLAG(context);
579         return 0;
580     }
581     dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
582                                                : DRIVE_GetCurrentDrive();
583     dta->count = 0;
584     dta->search_attr = CL_reg(context);
585     return 1;
586 }
587
588
589 static int INT21_FindNext( CONTEXT *context )
590 {
591     FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA();
592     WIN32_FIND_DATA32A entry;
593     int count;
594
595     if (!dta->unixPath) return 0;
596     if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
597                                   dta->search_attr, dta->count, &entry )))
598     {
599         HeapFree( GetProcessHeap(), 0, dta->unixPath );
600         dta->unixPath = NULL;
601         return 0;
602     }
603     if ((int)dta->count + count > 0xffff)
604     {
605         WARN(int21, "Too many directory entries in %s\n", dta->unixPath );
606         HeapFree( GetProcessHeap(), 0, dta->unixPath );
607         dta->unixPath = NULL;
608         return 0;
609     }
610     dta->count += count;
611     dta->fileattr = entry.dwFileAttributes;
612     dta->filesize = entry.nFileSizeLow;
613     FileTimeToDosDateTime( &entry.ftLastWriteTime,
614                            &dta->filedate, &dta->filetime );
615     strcpy( dta->filename, entry.cAlternateFileName );
616     return 1;
617 }
618
619
620 static BOOL32 INT21_CreateTempFile( CONTEXT *context )
621 {
622     static int counter = 0;
623     char *name = PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context) );
624     char *p = name + strlen(name);
625
626     /* despite what Ralf Brown says, some programs seem to call without 
627      * ending backslash (DOS accepts that, so we accept it too) */
628     if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
629
630     for (;;)
631     {
632         sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
633         counter = (counter + 1) % 1000;
634
635         if ((AX_reg(context) = _lcreat_uniq( name, 0 )) != (WORD)HFILE_ERROR16)
636         {
637             TRACE(int21, "created %s\n", name );
638             return TRUE;
639         }
640         if (DOS_ExtendedError != ER_FileExists) return FALSE;
641     }
642 }
643
644
645 static BOOL32 INT21_GetCurrentDirectory( CONTEXT *context ) 
646 {
647     int drive = DOS_GET_DRIVE( DL_reg(context) );
648     char *ptr = (char *)PTR_SEG_OFF_TO_LIN( DS_reg(context), SI_reg(context) );
649
650     if (!DRIVE_IsValid(drive))
651     {
652         DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
653         return FALSE;
654     }
655     lstrcpyn32A( ptr, DRIVE_GetDosCwd(drive), 64 );
656     AX_reg(context) = 0x0100;                        /* success return code */
657     return TRUE;
658 }
659
660
661 static int INT21_GetDiskSerialNumber( CONTEXT *context )
662 {
663     BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
664     int drive = DOS_GET_DRIVE( BL_reg(context) );
665         
666     if (!DRIVE_IsValid(drive))
667     {
668         DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
669         return 0;
670     }
671     
672     *(WORD *)dataptr = 0;
673     *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
674     memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
675     strncpy(dataptr + 0x11, "FAT16   ", 8);
676     return 1;
677 }
678
679
680 static int INT21_SetDiskSerialNumber( CONTEXT *context )
681 {
682     BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
683     int drive = DOS_GET_DRIVE( BL_reg(context) );
684
685     if (!DRIVE_IsValid(drive))
686     {
687         DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
688         return 0;
689     }
690
691     DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
692     return 1;
693 }
694
695
696 /* microsoft's programmers should be shot for using CP/M style int21
697    calls in Windows for Workgroup's winfile.exe */
698
699 static int INT21_FindFirstFCB( CONTEXT *context )
700 {
701     BYTE *fcb = (BYTE *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
702     FINDFILE_FCB *pFCB;
703     LPCSTR root, cwd;
704     int drive;
705
706     if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
707     else pFCB = (FINDFILE_FCB *)fcb;
708     drive = DOS_GET_DRIVE( pFCB->drive );
709     root = DRIVE_GetRoot( drive );
710     cwd  = DRIVE_GetUnixCwd( drive );
711     pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
712                                 strlen(root)+strlen(cwd)+2 );
713     if (!pFCB->unixPath) return 0;
714     strcpy( pFCB->unixPath, root );
715     strcat( pFCB->unixPath, "/" );
716     strcat( pFCB->unixPath, cwd );
717     pFCB->count = 0;
718     return 1;
719 }
720
721
722 static int INT21_FindNextFCB( CONTEXT *context )
723 {
724     BYTE *fcb = (BYTE *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
725     FINDFILE_FCB *pFCB;
726     DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA();
727     WIN32_FIND_DATA32A entry;
728     BYTE attr;
729     int count;
730
731     if (*fcb == 0xff) /* extended FCB ? */
732     {
733         attr = fcb[6];
734         pFCB = (FINDFILE_FCB *)(fcb + 7);
735     }
736     else
737     {
738         attr = 0;
739         pFCB = (FINDFILE_FCB *)fcb;
740     }
741
742     if (!pFCB->unixPath) return 0;
743     if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
744                                   DOS_GET_DRIVE( pFCB->drive ), attr,
745                                   pFCB->count, &entry )))
746     {
747         HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
748         pFCB->unixPath = NULL;
749         return 0;
750     }
751     pFCB->count += count;
752
753     if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
754         *(BYTE *)pResult = 0xff;
755         (BYTE *)pResult +=6; /* leave reserved field behind */
756         *(BYTE *)pResult = entry.dwFileAttributes;
757         ((BYTE *)pResult)++;
758     }
759     *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
760     ((BYTE *)pResult)++;
761     pResult->fileattr = entry.dwFileAttributes;
762     pResult->cluster  = 0;  /* what else? */
763     pResult->filesize = entry.nFileSizeLow;
764     memset( pResult->reserved, 0, sizeof(pResult->reserved) );
765     FileTimeToDosDateTime( &entry.ftLastWriteTime,
766                            &pResult->filedate, &pResult->filetime );
767
768     /* Convert file name to FCB format */
769
770     memset( pResult->filename, ' ', sizeof(pResult->filename) );
771     if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
772     else if (!strcmp( entry.cAlternateFileName, ".." ))
773         pResult->filename[0] = pResult->filename[1] = '.';
774     else
775     {
776         char *p = strrchr( entry.cAlternateFileName, '.' );
777         if (p && p[1] && (p != entry.cAlternateFileName))
778         {
779             memcpy( pResult->filename, entry.cAlternateFileName,
780                     MIN( (p - entry.cAlternateFileName), 8 ) );
781             memcpy( pResult->filename + 8, p + 1, MIN( strlen(p), 3 ) );
782         }
783         else
784             memcpy( pResult->filename, entry.cAlternateFileName,
785                     MIN( strlen(entry.cAlternateFileName), 8 ) );
786     }
787     return 1;
788 }
789
790
791 static void DeleteFileFCB( CONTEXT *context )
792 {
793     FIXME(int21, "(%p): not implemented yet\n", context);
794 }
795
796 static void RenameFileFCB( CONTEXT *context )
797 {
798     FIXME(int21, "(%p): not implemented yet\n", context);
799 }
800
801
802
803 static void fLock( CONTEXT * context )
804 {
805
806     switch ( AX_reg(context) & 0xff )
807     {
808         case 0x00: /* LOCK */
809           TRACE(int21,"lock handle %d offset %ld length %ld\n",
810                 BX_reg(context),
811                 MAKELONG(DX_reg(context),CX_reg(context)),
812                 MAKELONG(DI_reg(context),SI_reg(context))) ;
813           if (!LockFile(BX_reg(context),
814                         MAKELONG(DX_reg(context),CX_reg(context)), 0,
815                         MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
816             AX_reg(context) = DOS_ExtendedError;
817             SET_CFLAG(context);
818           }
819           break;
820
821         case 0x01: /* UNLOCK */
822           TRACE(int21,"unlock handle %d offset %ld length %ld\n",
823                 BX_reg(context),
824                 MAKELONG(DX_reg(context),CX_reg(context)),
825                 MAKELONG(DI_reg(context),SI_reg(context))) ;
826           if (!UnlockFile(BX_reg(context),
827                           MAKELONG(DX_reg(context),CX_reg(context)), 0,
828                           MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
829             AX_reg(context) = DOS_ExtendedError;
830             SET_CFLAG(context);
831           }
832           return;
833         default:
834           AX_reg(context) = 0x0001;
835           SET_CFLAG(context);
836           return;
837      }
838
839
840 static BOOL32
841 INT21_networkfunc (CONTEXT *context)
842 {
843      switch (AL_reg(context)) {
844      case 0x00: /* Get machine name. */
845      {
846           char *dst = PTR_SEG_OFF_TO_LIN (DS_reg(context),DX_reg(context));
847           TRACE(int21, "getting machine name to %p\n", dst);
848           if (gethostname (dst, 15))
849           {
850                WARN(int21,"failed!\n");
851                DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
852                return TRUE;
853           } else {
854                int len = strlen (dst);
855                while (len < 15)
856                     dst[len++] = ' ';
857                dst[15] = 0;
858                CH_reg(context) = 1; /* Valid */
859                CL_reg(context) = 1; /* NETbios number??? */
860                TRACE(int21, "returning %s\n", debugstr_an (dst, 16));
861                return FALSE;
862           }
863      }
864
865      default:
866           DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
867           return TRUE;
868      }
869 }
870
871
872 SEGPTR INT21_GetListOfLists()
873 {
874         static DOS_LISTOFLISTS *LOL;
875         static SEGPTR seg_LOL;
876
877 /*
878 Output of DOS 6.22:
879
880 0133:0020                    6A 13-33 01 CC 00 33 01 59 00         j.3...3.Y.
881 0133:0030  70 00 00 00 72 02 00 02-6D 00 33 01 00 00 2E 05   p...r...m.3.....
882 0133:0040  00 00 FC 04 00 00 03 08-92 21 11 E0 04 80 C6 0D   .........!......
883 0133:0050  CC 0D 4E 55 4C 20 20 20-20 20 00 00 00 00 00 00   ..NUL     ......
884 0133:0060  00 4B BA C1 06 14 00 00-00 03 01 00 04 70 CE FF   .K...........p..
885 0133:0070  FF 00 00 00 00 00 00 00-00 01 00 00 0D 05 00 00   ................
886 0133:0080  00 FF FF 00 00 00 00 FE-00 00 F8 03 FF 9F 70 02   ..............p.
887 0133:0090  D0 44 C8 FD D4 44 C8 FD-D4 44 C8 FD D0 44 C8 FD   .D...D...D...D..
888 0133:00A0  D0 44 C8 FD D0 44                                 .D...D
889 */
890         if (!LOL) {
891                 LOL = SEGPTR_ALLOC(sizeof(DOS_LISTOFLISTS));
892
893                 LOL->CX_Int21_5e01              = 0x0;
894                 LOL->LRU_count_FCB_cache        = 0x0;
895                 LOL->LRU_count_FCB_open         = 0x0;
896                 LOL->OEM_func_handler           = -1; /* not available */
897                 LOL->INT21_offset               = 0x0;
898                 LOL->sharing_retry_count        = sharing_retries; /* default value: 3 */
899                 LOL->sharing_retry_delay        = sharing_pause; /* default value: 1 */
900                 LOL->ptr_disk_buf               = 0x0;
901                 LOL->offs_unread_CON            = 0x0;
902                 LOL->seg_first_MCB              = 0x0;
903                 LOL->ptr_first_DPB              = 0x0;
904                 LOL->ptr_first_SysFileTable     = 0x0;
905                 LOL->ptr_clock_dev_hdr          = 0x0;
906                 LOL->ptr_CON_dev_hdr            = 0x0;
907                 LOL->max_byte_per_sec           = 512;
908                 LOL->ptr_disk_buf_info          = 0x0;
909                 LOL->ptr_array_CDS              = 0x0;
910                 LOL->ptr_sys_FCB                = 0x0;
911                 LOL->nr_protect_FCB             = 0x0;
912                 LOL->nr_block_dev               = 0x0;
913                 LOL->nr_avail_drive_letters     = 26; /* A - Z */
914                 LOL->nr_drives_JOINed           = 0x0;
915                 LOL->ptr_spec_prg_names         = 0x0;
916                 LOL->ptr_SETVER_prg_list        = 0x0; /* no SETVER list */
917                 LOL->DOS_HIGH_A20_func_offs     = 0x0;
918                 LOL->PSP_last_exec              = 0x0;
919                 LOL->BUFFERS_val                = 99; /* maximum: 99 */
920                 LOL->BUFFERS_nr_lookahead       = 8; /* maximum: 8 */
921                 LOL->boot_drive                 = 3; /* C: */
922                 LOL->flag_DWORD_moves           = 0x01; /* i386+ */
923                 LOL->size_extended_mem          = 0xf000; /* very high value */
924         }       
925         if (!seg_LOL) seg_LOL = SEGPTR_GET(LOL);
926         return seg_LOL+(WORD)&((DOS_LISTOFLISTS*)0)->ptr_first_DPB;
927 }
928
929 /***********************************************************************
930  *           DOS3Call  (KERNEL.102)
931  */
932 void WINAPI DOS3Call( CONTEXT *context )
933 {
934     BOOL32      bSetDOSExtendedError = FALSE;
935
936 #if 0
937     TRACE(int21, "AX=%04x BX=%04x CX=%04x DX=%04x "
938           "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
939           AX_reg(context), BX_reg(context), CX_reg(context), DX_reg(context),
940           SI_reg(context), DI_reg(context),
941           (WORD)DS_reg(context), (WORD)ES_reg(context),
942           EFL_reg(context) );
943 #endif
944
945     if (AH_reg(context) == 0x59)  /* Get extended error info */
946     {
947         TRACE(int21, "GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
948               DOS_ExtendedError, DOS_ErrorClass, DOS_ErrorAction, DOS_ErrorLocus);
949         AX_reg(context) = DOS_ExtendedError;
950         BH_reg(context) = DOS_ErrorClass;
951         BL_reg(context) = DOS_ErrorAction;
952         CH_reg(context) = DOS_ErrorLocus;
953         return;
954     }
955
956     DOS_ERROR( 0, 0, 0, 0 );
957     RESET_CFLAG(context);  /* Not sure if this is a good idea */
958
959     switch(AH_reg(context)) 
960     {
961     case 0x00: /* TERMINATE PROGRAM */
962         TRACE(int21,"TERMINATE PROGRAM\n");
963         TASK_KillCurrentTask( 0 );
964         break;
965
966     case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
967     case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
968     case 0x03: /* READ CHARACTER FROM STDAUX  */
969     case 0x04: /* WRITE CHARACTER TO STDAUX */
970     case 0x05: /* WRITE CHARACTER TO PRINTER */
971     case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
972     case 0x07: /* DIRECT CHARACTER INPUT, WITHOUT ECHO */
973     case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
974     case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
975     case 0x0a: /* BUFFERED INPUT */
976     case 0x0b: /* GET STDIN STATUS */
977     case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
978     case 0x0f: /* OPEN FILE USING FCB */
979     case 0x10: /* CLOSE FILE USING FCB */
980     case 0x14: /* SEQUENTIAL READ FROM FCB FILE */              
981     case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
982     case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
983     case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
984     case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
985     case 0x23: /* GET FILE SIZE FOR FCB */
986     case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
987     case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
988     case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
989     case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
990     case 0x29: /* PARSE FILENAME INTO FCB */
991     case 0x37: /* "SWITCHAR" - GET SWITCH CHARACTER
992                   "SWITCHAR" - SET SWITCH CHARACTER
993                   "AVAILDEV" - SPECIFY \DEV\ PREFIX USE */
994     case 0x54: /* GET VERIFY FLAG */
995     case 0x48: /* ALLOCATE MEMORY */
996     case 0x49: /* FREE MEMORY */
997     case 0x4a: /* RESIZE MEMORY BLOCK */
998         INT_BARF( context, 0x21 );
999         break;
1000     case 0x2e: /* SET VERIFY FLAG */
1001         TRACE(int21,"SET VERIFY FLAG ignored\n");
1002         /* we cannot change the behaviour anyway, so just ignore it */
1003         break;
1004
1005     case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
1006     case 0x1d:
1007     case 0x1e:
1008     case 0x20:
1009     case 0x6b: /* NULL FUNCTION */
1010         AL_reg(context) = 0;
1011         break;
1012         
1013     case 0x5c: /* "FLOCK" - RECORD LOCKING */
1014         fLock(context);
1015         break;
1016
1017     case 0x0d: /* DISK BUFFER FLUSH */
1018         TRACE(int21,"DISK BUFFER FLUSH ignored\n");
1019         RESET_CFLAG(context); /* dos 6+ only */
1020         break;
1021
1022     case 0x0e: /* SELECT DEFAULT DRIVE */
1023         TRACE(int21,"SELECT DEFAULT DRIVE %d\n", DL_reg(context));
1024         DRIVE_SetCurrentDrive( DL_reg(context) );
1025         AL_reg(context) = MAX_DOS_DRIVES;
1026         break;
1027
1028     case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1029         TRACE(int21,"FIND FIRST MATCHING FILE USING FCB %p\n", 
1030               PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context)));
1031         if (!INT21_FindFirstFCB(context))
1032         {
1033             AL_reg(context) = 0xff;
1034             break;
1035         }
1036         /* else fall through */
1037
1038     case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1039         AL_reg(context) = INT21_FindNextFCB(context) ? 0x00 : 0xff;
1040         break;
1041
1042     case 0x13: /* DELETE FILE USING FCB */
1043         DeleteFileFCB(context);
1044         break;
1045             
1046     case 0x17: /* RENAME FILE USING FCB */
1047         RenameFileFCB(context);
1048         break;
1049
1050     case 0x19: /* GET CURRENT DEFAULT DRIVE */
1051         AL_reg(context) = DRIVE_GetCurrentDrive();
1052         break;
1053
1054     case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1055         {
1056             TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1057             pTask->dta = PTR_SEG_OFF_TO_SEGPTR(DS_reg(context),DX_reg(context));
1058             TRACE(int21, "Set DTA: %08lx\n", pTask->dta);
1059         }
1060         break;
1061
1062     case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1063         DL_reg(context) = 0;
1064         if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1065         break;
1066         
1067     case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1068         if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1069         break;
1070
1071     case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1072         GetDrivePB(context, DRIVE_GetCurrentDrive());
1073         break;
1074                 
1075     case 0x25: /* SET INTERRUPT VECTOR */
1076         INT_SetHandler( AL_reg(context),
1077                         (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1078                                                           DX_reg(context)));
1079         break;
1080
1081     case 0x2a: /* GET SYSTEM DATE */
1082         INT21_GetSystemDate(context);
1083         break;
1084
1085     case 0x2b: /* SET SYSTEM DATE */
1086         FIXME(int21, "SetSystemDate(%02d/%02d/%04d): not allowed\n",
1087               DL_reg(context), DH_reg(context), CX_reg(context) );
1088         AL_reg(context) = 0;  /* Let's pretend we succeeded */
1089         break;
1090
1091     case 0x2c: /* GET SYSTEM TIME */
1092         INT21_GetSystemTime(context);
1093         break;
1094
1095     case 0x2d: /* SET SYSTEM TIME */
1096         FIXME(int21, "SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1097               CH_reg(context), CL_reg(context),
1098               DH_reg(context), DL_reg(context) );
1099         AL_reg(context) = 0;  /* Let's pretend we succeeded */
1100         break;
1101
1102     case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1103         TRACE(int21,"GET DISK TRANSFER AREA ADDRESS\n");
1104         {
1105             TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1106             ES_reg(context) = SELECTOROF( pTask->dta );
1107             BX_reg(context) = OFFSETOF( pTask->dta );
1108         }
1109         break;
1110             
1111     case 0x30: /* GET DOS VERSION */
1112         TRACE(int21,"GET DOS VERSION %s requested\n",
1113               (AL_reg(context) == 0x00)?"OEM number":"version flag");
1114         AX_reg(context) = (HIWORD(GetVersion16()) >> 8) |
1115                           (HIWORD(GetVersion16()) << 8);
1116         BX_reg(context) = 0x0012;     /* 0x123456 is Wine's serial # */
1117         CX_reg(context) = 0x3456;
1118         break;
1119
1120     case 0x31: /* TERMINATE AND STAY RESIDENT */
1121         TRACE(int21,"TERMINATE AND STAY RESIDENT stub\n");
1122         INT_BARF( context, 0x21 );
1123         break;
1124
1125     case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1126         TRACE(int21,"GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
1127               INT21_DriveName( DL_reg(context)));
1128         GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
1129         break;
1130
1131     case 0x33: /* MULTIPLEXED */
1132         switch (AL_reg(context))
1133         {
1134               case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1135                 TRACE(int21,"GET CURRENT EXTENDED BREAK STATE stub\n");
1136                 DL_reg(context) = 0;
1137                 break;
1138
1139               case 0x01: /* SET EXTENDED BREAK STATE */
1140                 TRACE(int21,"SET CURRENT EXTENDED BREAK STATE stub\n");
1141                 break;          
1142                 
1143               case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1144                 TRACE(int21,"GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE stub\n");
1145                 DL_reg(context) = 0;
1146                 break;
1147
1148               case 0x05: /* GET BOOT DRIVE */
1149                 TRACE(int21,"GET BOOT DRIVE\n");
1150                 DL_reg(context) = 3;
1151                 /* c: is Wine's bootdrive (a: is 1)*/
1152                 break;
1153                                 
1154               case 0x06: /* GET TRUE VERSION NUMBER */
1155                 TRACE(int21,"GET TRUE VERSION NUMBER\n");
1156                 BX_reg(context) = (HIWORD(GetVersion16() >> 8)) |
1157                                   (HIWORD(GetVersion16() << 8));
1158                 DX_reg(context) = 0x00;
1159                 break;
1160
1161               default:
1162                 INT_BARF( context, 0x21 );
1163                 break;                  
1164         }
1165         break;  
1166             
1167     case 0x34: /* GET ADDRESS OF INDOS FLAG */
1168         TRACE(int21,"GET ADDRESS OF INDOS FLAG\n");
1169         if (!heap) INT21_CreateHeap();
1170         ES_reg(context) = DosHeapHandle;
1171         BX_reg(context) = (int)&heap->InDosFlag - (int)heap;
1172         break;
1173
1174     case 0x35: /* GET INTERRUPT VECTOR */
1175         TRACE(int21,"GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
1176         {
1177             FARPROC16 addr = INT_GetHandler( AL_reg(context) );
1178             ES_reg(context) = SELECTOROF(addr);
1179             BX_reg(context) = OFFSETOF(addr);
1180         }
1181         break;
1182
1183     case 0x36: /* GET FREE DISK SPACE */
1184         TRACE(int21,"GET FREE DISK SPACE FOR DRIVE %s\n",
1185               INT21_DriveName( DL_reg(context)));
1186         if (!INT21_GetFreeDiskSpace(context)) AX_reg(context) = 0xffff;
1187         break;
1188
1189     case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1190         TRACE(int21,"GET COUNTRY-SPECIFIC INFORMATION for country 0x%02x\n",
1191               AL_reg(context));
1192         AX_reg(context) = 0x02; /* no country support available */
1193         SET_CFLAG(context);
1194         break;
1195
1196     case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1197         TRACE(int21,"MKDIR %s\n",
1198               (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context)));
1199         bSetDOSExtendedError = (!CreateDirectory16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1200                                                            DX_reg(context) ), NULL));
1201         break;
1202         
1203     case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1204         TRACE(int21,"RMDIR %s\n",
1205               (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context)));
1206         bSetDOSExtendedError = (!RemoveDirectory16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1207                                                                  DX_reg(context) )));
1208         break;
1209
1210     case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1211         TRACE(int21,"CHDIR %s\n",
1212               (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context)));
1213         bSetDOSExtendedError = !INT21_ChangeDir(context);
1214         break;
1215         
1216     case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1217         TRACE(int21,"CREAT flag 0x%02x %s\n",CX_reg(context),
1218               (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context)));
1219         AX_reg(context) = _lcreat16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1220                                     DX_reg(context) ), CX_reg(context) );
1221         bSetDOSExtendedError = (AX_reg(context) == (WORD)HFILE_ERROR16);
1222         break;
1223
1224     case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1225         TRACE(int21,"OPEN mode 0x%02x %s\n",AL_reg(context),
1226               (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context)));
1227         OpenExistingFile(context);
1228         break;
1229
1230     case 0x3e: /* "CLOSE" - CLOSE FILE */
1231         TRACE(int21,"CLOSE handle %d\n",BX_reg(context));
1232         bSetDOSExtendedError = ((AX_reg(context) = _lclose16( BX_reg(context) )) != 0);
1233         break;
1234
1235     case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1236         TRACE(int21,"READ from %d to %ld for %d byte\n",BX_reg(context),
1237               PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),DX_reg(context)),CX_reg(context) );
1238         {
1239             LONG result = WIN16_hread( BX_reg(context),
1240                                        PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1241                                                               DX_reg(context) ),
1242                                        CX_reg(context) );
1243             if (result == -1) bSetDOSExtendedError = TRUE;
1244             else AX_reg(context) = (WORD)result;
1245         }
1246         break;
1247
1248     case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1249         TRACE(int21,"WRITE from %ld to handle %d for %d byte\n",
1250               PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),DX_reg(context)),
1251               BX_reg(context),CX_reg(context) );
1252         {
1253             LONG result = _hwrite16( BX_reg(context),
1254                                      PTR_SEG_OFF_TO_LIN( DS_reg(context),
1255                                                          DX_reg(context) ),
1256                                      CX_reg(context) );
1257             if (result == -1) bSetDOSExtendedError = TRUE;
1258             else AX_reg(context) = (WORD)result;
1259         }
1260         break;
1261
1262     case 0x41: /* "UNLINK" - DELETE FILE */
1263         TRACE(int21,"UNLINK%s\n",
1264               (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context)));
1265         bSetDOSExtendedError = (!DeleteFile32A( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1266                                                              DX_reg(context) )));
1267         break;
1268
1269     case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1270         TRACE(int21,"LSEEK handle %d offset %ld from %s\n",
1271               BX_reg(context), MAKELONG(DX_reg(context),CX_reg(context)),
1272               (AL_reg(context)==0)?"start of file":(AL_reg(context)==1)?
1273               "current file position":"end of file");
1274         {
1275             LONG status = _llseek16( BX_reg(context),
1276                                      MAKELONG(DX_reg(context),CX_reg(context)),
1277                                      AL_reg(context) );
1278             if (status == -1) bSetDOSExtendedError = TRUE;
1279             else
1280             {
1281                 AX_reg(context) = LOWORD(status);
1282                 DX_reg(context) = HIWORD(status);
1283             }
1284         }
1285         break;
1286
1287     case 0x43: /* FILE ATTRIBUTES */
1288         switch (AL_reg(context))
1289         {
1290         case 0x00:
1291             TRACE(int21,"GET FILE ATTRIBUTES for %s\n", 
1292                   (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context)));
1293             AX_reg(context) = (WORD)GetFileAttributes32A(
1294                                           PTR_SEG_OFF_TO_LIN(DS_reg(context),
1295                                                              DX_reg(context)));
1296             if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1297             else CX_reg(context) = AX_reg(context);
1298             break;
1299
1300         case 0x01:
1301             TRACE(int21,"SET FILE ATTRIBUTES 0x%02x for %s\n", CX_reg(context),
1302                   (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context)));
1303             bSetDOSExtendedError = 
1304                 (!SetFileAttributes32A( PTR_SEG_OFF_TO_LIN(DS_reg(context), 
1305                                                            DX_reg(context)),
1306                                                            CX_reg(context) ));
1307             break;
1308         case 0x02:
1309             TRACE(int21,"GET COMPRESSED FILE SIZE for %s stub\n",
1310                   (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context)));
1311         }
1312         break;
1313         
1314     case 0x44: /* IOCTL */
1315         switch (AL_reg(context))
1316         {
1317         case 0x00:
1318             ioctlGetDeviceInfo(context);
1319             break;
1320
1321         case 0x01:
1322             break;
1323         case 0x02:{
1324            FILE_OBJECT *file;
1325            file = FILE_GetFile(BX_reg(context));
1326             if (!strcasecmp(file->unix_name, "SCSIMGR$"))
1327                         ASPI_DOS_HandleInt(context);
1328            break;
1329        }
1330         case 0x05:{     /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1331             /*BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context));*/
1332             int drive = DOS_GET_DRIVE(BL_reg(context));
1333
1334             FIXME(int21,"program tried to write to block device control channel of drive %d:\n",drive);
1335             /* for (i=0;i<CX_reg(context);i++)
1336                 fprintf(stdnimp,"%02x ",dataptr[i]);
1337             fprintf(stdnimp,"\n");*/
1338             AX_reg(context)=CX_reg(context);
1339             break;
1340         }
1341         case 0x08:   /* Check if drive is removable. */
1342             TRACE(int21,"IOCTL - CHECK IF BLOCK DEVICE REMOVABLE for drive %s\n",
1343                  INT21_DriveName( BL_reg(context)));
1344             switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1345             {
1346             case DRIVE_CANNOTDETERMINE:
1347                 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
1348                 AX_reg(context) = ER_InvalidDrive;
1349                 SET_CFLAG(context);
1350                 break;
1351             case DRIVE_REMOVABLE:
1352                 AX_reg(context) = 0;      /* removable */
1353                 break;
1354             default:
1355                 AX_reg(context) = 1;   /* not removable */
1356                 break;
1357             }
1358             break;
1359
1360         case 0x09:   /* CHECK IF BLOCK DEVICE REMOTE */
1361             TRACE(int21,"IOCTL - CHECK IF BLOCK DEVICE REMOTE for drive %s\n",
1362                  INT21_DriveName( BL_reg(context))); 
1363             switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1364             {
1365             case DRIVE_CANNOTDETERMINE:
1366                 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
1367                 AX_reg(context) = ER_InvalidDrive;
1368                 SET_CFLAG(context);
1369                 break;
1370             case DRIVE_REMOTE:
1371                 DX_reg(context) = (1<<9) | (1<<12);  /* remote */
1372                 break;
1373             default:
1374                 DX_reg(context) = 0;  /* FIXME: use driver attr here */
1375                 break;
1376             }
1377             break;
1378
1379         case 0x0a: /* check if handle (BX) is remote */
1380             TRACE(int21,"IOCTL - CHECK IF HANDLE %d IS REMOTE\n",BX_reg(context));
1381             /* returns DX, bit 15 set if remote, bit 14 set if date/time
1382              * not set on close
1383              */
1384             DX_reg(context) = 0;
1385             break;
1386
1387         case 0x0b:   /* SET SHARING RETRY COUNT */
1388             TRACE(int21,"IOCTL - SET SHARING RETRY COUNT pause %d retries %d\n",
1389                  CX_reg(context), DX_reg(context));
1390             if (!CX_reg(context))
1391             { 
1392                 AX_reg(context) = 1;
1393                 SET_CFLAG(context);
1394                 break;
1395             }
1396             sharing_pause = CX_reg(context);
1397             if (!DX_reg(context))
1398                 sharing_retries = DX_reg(context);
1399             RESET_CFLAG(context);
1400             break;
1401
1402         case 0x0d:
1403             TRACE(int21,"IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
1404                   INT21_DriveName( BL_reg(context)));
1405             bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1406             break;
1407
1408         case 0x0e: /* get logical drive mapping */
1409             TRACE(int21,"IOCTL - GET LOGICAL DRIVE MAP for drive %s\n",
1410                   INT21_DriveName( BL_reg(context)));
1411             AL_reg(context) = 0; /* drive has no mapping - FIXME: may be wrong*/
1412             break;
1413
1414         case 0x0F:   /* Set logical drive mapping */
1415             {
1416             int drive;
1417             TRACE(int21,"IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
1418                   INT21_DriveName( BL_reg(context))); 
1419             drive = DOS_GET_DRIVE ( BL_reg(context) );
1420             if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
1421             {
1422                 SET_CFLAG(context);
1423                 AX_reg(context) = 0x000F;  /* invalid drive */
1424             }
1425             break;
1426             }
1427                 
1428         default:
1429             INT_BARF( context, 0x21 );
1430             break;
1431         }
1432         break;
1433
1434     case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1435         TRACE(int21,"DUP - DUPLICATE FILE HANDLE %d\n",BX_reg(context));
1436         bSetDOSExtendedError = ((AX_reg(context) = FILE_Dup(BX_reg(context))) == (WORD)HFILE_ERROR16);
1437         break;
1438
1439     case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1440         TRACE(int21,"FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
1441               BX_reg(context),CX_reg(context));
1442         bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR32);
1443         break;
1444
1445     case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1446         TRACE(int21,"CWD - GET CURRENT DIRECTORY for drive %s\n",
1447               INT21_DriveName( DL_reg(context)));
1448         bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1449         break;
1450
1451     case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1452         TRACE(int21,"EXEC %s\n",
1453               (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context),DX_reg(context) ));
1454         AX_reg(context) = WinExec16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1455                                                          DX_reg(context) ),
1456                                      SW_NORMAL );
1457         if (AX_reg(context) < 32) SET_CFLAG(context);
1458         break;          
1459         
1460     case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1461         TRACE(int21,"EXIT with return code %d\n",AL_reg(context));
1462         TASK_KillCurrentTask( AL_reg(context) );
1463         break;
1464
1465     case 0x4d: /* GET RETURN CODE */
1466         TRACE(int21,"GET RETURN CODE (ERRORLEVEL)\n");
1467         AX_reg(context) = 0; /* normal exit */
1468         break;
1469
1470     case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1471         TRACE(int21,"FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
1472               (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context)));
1473         if (!INT21_FindFirst(context)) break;
1474         /* fall through */
1475
1476     case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1477         TRACE(int21,"FINDNEXT\n");
1478         if (!INT21_FindNext(context))
1479         {
1480             DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Disk );
1481             AX_reg(context) = ER_NoMoreFiles;
1482             SET_CFLAG(context);
1483         }
1484         else AX_reg(context) = 0;  /* OK */
1485         break;
1486
1487     case 0x51: /* GET PSP ADDRESS */
1488         TRACE(int21,"GET CURRENT PROCESS ID (GET PSP ADDRESS)\n");
1489         /* FIXME: should we return the original DOS PSP upon */
1490         /*        Windows startup ? */
1491         BX_reg(context) = GetCurrentPDB();
1492         break;
1493     case 0x62: /* GET PSP ADDRESS */
1494         TRACE(int21,"GET CURRENT PSP ADDRESS\n");
1495         /* FIXME: should we return the original DOS PSP upon */
1496         /*        Windows startup ? */
1497         BX_reg(context) = GetCurrentPDB();
1498         break;
1499
1500     case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1501         TRACE(int21,"SYSVARS - GET LIST OF LISTS\n");
1502         {
1503                 SEGPTR lol;
1504                 lol = INT21_GetListOfLists();
1505                 ES_reg(context) = HIWORD(lol);
1506                 BX_reg(context) = LOWORD(lol);
1507         }
1508         break;
1509
1510     case 0x56: /* "RENAME" - RENAME FILE */
1511         TRACE(int21,"RENAME %s to %s\n",
1512               (LPCSTR)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
1513               (LPCSTR)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context)));
1514         bSetDOSExtendedError = 
1515                 (!MoveFile32A( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
1516                                PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context))));
1517         break;
1518
1519     case 0x57: /* FILE DATE AND TIME */
1520         switch (AL_reg(context))
1521         {
1522         case 0x00:  /* Get */
1523             {
1524                 FILETIME filetime;
1525                 TRACE(int21,"GET FILE DATE AND TIME for handle %d\n",
1526                       BX_reg(context));
1527                 if (!GetFileTime( BX_reg(context), NULL, NULL, &filetime ))
1528                      bSetDOSExtendedError = TRUE;
1529                 else FileTimeToDosDateTime( &filetime, &DX_reg(context),
1530                                             &CX_reg(context) );
1531             }
1532             break;
1533
1534         case 0x01:  /* Set */
1535             {
1536                 FILETIME filetime;
1537                 TRACE(int21,"SET FILE DATE AND TIME for handle %d\n",
1538                       BX_reg(context));
1539                 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
1540                                        &filetime );
1541                 bSetDOSExtendedError = 
1542                         (!SetFileTime( BX_reg(context), NULL, NULL, &filetime ));
1543             }
1544             break;
1545         }
1546         break;
1547
1548     case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1549         TRACE(int21,"GET OR SET MEMORY/UMB ALLOCATION STRATEGY subfunction %d\n",
1550               AL_reg(context));
1551         switch (AL_reg(context))
1552         {
1553         case 0x00:
1554             AX_reg(context) = 1;
1555             break;
1556         case 0x02:
1557             AX_reg(context) = 0;
1558             break;
1559         case 0x01:
1560         case 0x03:
1561             break;
1562         }
1563         RESET_CFLAG(context);
1564         break;
1565
1566     case 0x5a: /* CREATE TEMPORARY FILE */
1567         TRACE(int21,"CREATE TEMPORARY FILE\n");
1568         bSetDOSExtendedError = !INT21_CreateTempFile(context);
1569         break;
1570
1571     case 0x5b: /* CREATE NEW FILE */
1572         TRACE(int21,"CREATE NEW FILE 0x%02x for %s\n", CX_reg(context),
1573               (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context)));
1574         bSetDOSExtendedError = ((AX_reg(context) = 
1575                 _lcreat_uniq( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)), 0 )) 
1576                                                                 == (WORD)HFILE_ERROR16);
1577         break;
1578
1579     case 0x5d: /* NETWORK */
1580         FIXME(int21,"Function 0x%04x not implemented.\n", AX_reg (context));
1581         /* Fix the following while you're at it.  */
1582         DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
1583         bSetDOSExtendedError = TRUE;
1584         break;
1585
1586     case 0x5e:
1587         bSetDOSExtendedError = INT21_networkfunc (context);
1588         break;
1589
1590     case 0x5f: /* NETWORK */
1591         switch (AL_reg(context))
1592         {
1593         case 0x07: /* ENABLE DRIVE */
1594             TRACE(int21,"ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1595             if (!DRIVE_Enable( DL_reg(context) ))
1596             {
1597                 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
1598                 bSetDOSExtendedError = TRUE;
1599             }
1600             break;
1601
1602         case 0x08: /* DISABLE DRIVE */
1603             TRACE(int21,"DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1604             if (!DRIVE_Disable( DL_reg(context) ))
1605             {
1606                 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
1607                 bSetDOSExtendedError = TRUE;
1608             } 
1609             break;
1610
1611         default:
1612             /* network software not installed */
1613             TRACE(int21,"NETWORK function AX=%04x not implemented\n",AX_reg(context));
1614             DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
1615             bSetDOSExtendedError = TRUE;
1616             break;
1617         }
1618         break;
1619
1620     case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1621         TRACE(int21,"TRUENAME %s\n",
1622               (LPCSTR)PTR_SEG_OFF_TO_LIN(DS_reg(context),SI_reg(context)));
1623         {
1624             if (!GetFullPathName32A( PTR_SEG_OFF_TO_LIN(DS_reg(context),
1625                                                         SI_reg(context)), 128,
1626                                      PTR_SEG_OFF_TO_LIN(ES_reg(context),
1627                                                         DI_reg(context)),NULL))
1628                 bSetDOSExtendedError = TRUE;
1629             else AX_reg(context) = 0;
1630         }
1631         break;
1632
1633     case 0x61: /* UNUSED */
1634     case 0x63: /* UNUSED */
1635     case 0x64: /* OS/2 DOS BOX */
1636         INT_BARF( context, 0x21 );
1637         SET_CFLAG(context);
1638         break;
1639
1640     case 0x65:{/* GET EXTENDED COUNTRY INFORMATION */
1641         extern WORD WINE_LanguageId;
1642         BYTE    *dataptr=PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
1643         TRACE(int21,"GET EXTENDED COUNTRY INFORMATION code page %d country %d\n",
1644               BX_reg(context), DX_reg(context));
1645         switch (AL_reg(context)) {
1646         case 0x01:
1647             TRACE(int21,"\tget general internationalization info\n");
1648             dataptr[0] = 0x1;
1649             *(WORD*)(dataptr+1) = 41;
1650             *(WORD*)(dataptr+3) = WINE_LanguageId;
1651             *(WORD*)(dataptr+5) = CodePage;
1652             *(DWORD*)(dataptr+0x19) = NULL; /* FIXME: ptr to case map routine */
1653             break;
1654         case 0x06:
1655             TRACE(int21,"\tget pointer to collating sequence table\n");
1656             dataptr[0] = 0x06;
1657             *(DWORD*)(dataptr+1) = MAKELONG(DOSMEM_CollateTable & 0xFFFF,DOSMEM_AllocSelector(DOSMEM_CollateTable>>16));
1658             CX_reg(context)         = 258;/*FIXME: size of table?*/
1659             break;
1660         default:
1661             TRACE(int21,"\tunimplemented function %d\n",AL_reg(context));
1662             INT_BARF( context, 0x21 );
1663             SET_CFLAG(context);
1664             break;
1665         }
1666         break;
1667     }
1668     case 0x66: /* GLOBAL CODE PAGE TABLE */
1669         switch (AL_reg(context))
1670         {
1671         case 0x01:
1672             TRACE(int21,"GET GLOBAL CODE PAGE TABLE\n");
1673             DX_reg(context) = BX_reg(context) = CodePage;
1674             RESET_CFLAG(context);
1675             break;                      
1676         case 0x02: 
1677             TRACE(int21,"SET GLOBAL CODE PAGE TABLE active page %d system page %d\n",
1678                   BX_reg(context),DX_reg(context));
1679             CodePage = BX_reg(context);
1680             RESET_CFLAG(context);
1681             break;
1682         }
1683         break;
1684
1685     case 0x67: /* SET HANDLE COUNT */
1686         TRACE(int21,"SET HANDLE COUNT to %d\n",BX_reg(context) );
1687         SetHandleCount16( BX_reg(context) );
1688         if (DOS_ExtendedError) bSetDOSExtendedError = TRUE;
1689         break;
1690
1691     case 0x68: /* "FFLUSH" - COMMIT FILE */
1692     case 0x6a: /* COMMIT FILE */
1693         TRACE(int21,"FFLUSH/COMMIT handle %d\n",BX_reg(context));
1694         bSetDOSExtendedError = (!FlushFileBuffers( BX_reg(context) ));
1695         break;          
1696         
1697     case 0x69: /* DISK SERIAL NUMBER */
1698         switch (AL_reg(context))
1699         {
1700         case 0x00:
1701             TRACE(int21,"GET DISK SERIAL NUMBER for drive %s\n",
1702                   INT21_DriveName(BL_reg(context)));
1703             if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1704             else AX_reg(context) = 0;
1705             break;
1706
1707         case 0x01:
1708             TRACE(int21,"SET DISK SERIAL NUMBER for drive %s\n",
1709                   INT21_DriveName(BL_reg(context)));
1710             if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1711             else AX_reg(context) = 1;
1712             break;
1713         }
1714         break;
1715     
1716     case 0x6C: /* Extended Open/Create*/
1717         TRACE(int21,"EXTENDED OPEN/CREATE %s\n",
1718               (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DI_reg(context)));
1719         bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1720         break;
1721         
1722     case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
1723         switch(AL_reg(context))
1724         {
1725         case 0x39:  /* Create directory */
1726             TRACE(int21,"LONG FILENAME - MAKE DIRECTORY %s\n",
1727                   (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context),DX_reg(context)));
1728             bSetDOSExtendedError = (!CreateDirectory32A( 
1729                                         PTR_SEG_OFF_TO_LIN( DS_reg(context),
1730                                                   DX_reg(context) ), NULL));
1731             break;
1732         case 0x3a:  /* Remove directory */
1733             TRACE(int21,"LONG FILENAME - REMOVE DIRECTORY %s\n",
1734                   (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context),DX_reg(context)));
1735             bSetDOSExtendedError = (!RemoveDirectory32A( 
1736                                         PTR_SEG_OFF_TO_LIN( DS_reg(context),
1737                                                         DX_reg(context) )));
1738             break;
1739         case 0x43:  /* Get/Set file attributes */
1740           TRACE(int21,"LONG FILENAME -EXTENDED GET/SET FILE ATTRIBUTES %s\n",
1741                 (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context),DX_reg(context)));
1742         switch (BL_reg(context))
1743         {
1744         case 0x00: /* Get file attributes */
1745             TRACE(int21,"\tretrieve attributes\n");
1746             CX_reg(context) = (WORD)GetFileAttributes32A(
1747                                           PTR_SEG_OFF_TO_LIN(DS_reg(context),
1748                                                              DX_reg(context)));
1749             if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1750             break;
1751         case 0x01:
1752             TRACE(int21,"\tset attributes 0x%04x\n",CX_reg(context));
1753             bSetDOSExtendedError = (!SetFileAttributes32A( 
1754                                         PTR_SEG_OFF_TO_LIN(DS_reg(context),
1755                                                            DX_reg(context)),
1756                                         CX_reg(context)  ) );
1757             break;
1758         default:
1759           FIXME(int21, "Unimplemented long file name function:\n");
1760           INT_BARF( context, 0x21 );
1761           SET_CFLAG(context);
1762           AL_reg(context) = 0;
1763           break;
1764         }
1765         break;
1766         case 0x47:  /* Get current directory */
1767             TRACE(int21," LONG FILENAME - GET CURRENT DIRECTORY for drive %s\n",
1768                   INT21_DriveName(DL_reg(context)));
1769             bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1770             break;
1771
1772         case 0x4e:  /* Find first file */
1773             TRACE(int21," LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
1774                   (LPCSTR)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)));
1775             /* FIXME: use attributes in CX */
1776             if ((AX_reg(context) = FindFirstFile16(
1777                    PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
1778                    (WIN32_FIND_DATA32A *)PTR_SEG_OFF_TO_LIN(ES_reg(context),
1779                                                             DI_reg(context))))
1780                 == INVALID_HANDLE_VALUE16)
1781                 bSetDOSExtendedError = TRUE;
1782             break;
1783         case 0x4f:  /* Find next file */
1784             TRACE(int21,"LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
1785                   BX_reg(context));
1786             if (!FindNextFile16( BX_reg(context),
1787                     (WIN32_FIND_DATA32A *)PTR_SEG_OFF_TO_LIN(ES_reg(context),
1788                                                              DI_reg(context))))
1789                 bSetDOSExtendedError = TRUE;
1790             break;
1791         case 0xa1:  /* Find close */
1792             TRACE(int21,"LONG FILENAME - FINDCLOSE for handle %d\n",
1793                   BX_reg(context));
1794             bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
1795             break;
1796         case 0xa0:
1797             TRACE(int21,"LONG FILENAME - GET VOLUME INFORMATION for drive %s stub\n",
1798                   (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context),DX_reg(context)));
1799             break;
1800         case 0x60:  
1801           switch(CL_reg(context))
1802           {
1803             case 0x02:  /*Get canonical long filename or path */
1804               if (!GetFullPathName32A
1805                   ( PTR_SEG_OFF_TO_LIN(DS_reg(context),
1806                                        SI_reg(context)), 128,
1807                     PTR_SEG_OFF_TO_LIN(ES_reg(context),
1808                                        DI_reg(context)),NULL))
1809                 bSetDOSExtendedError = TRUE;
1810               else AX_reg(context) = 0;
1811               break;
1812             default:
1813               FIXME(int21, "Unimplemented long file name function:\n");
1814               INT_BARF( context, 0x21 );
1815               SET_CFLAG(context);
1816               AL_reg(context) = 0;
1817               break;
1818           }
1819             break;
1820         case 0x6c:  /* Create or open file */
1821             TRACE(int21,"LONG FILENAME - CREATE OR OPEN FILE %s\n",
1822                  (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), SI_reg(context))); 
1823           /* translate Dos 7 action to Dos 6 action */
1824             bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1825             break;
1826
1827         case 0x3b:  /* Change directory */
1828             TRACE(int21,"LONG FILENAME - CHANGE DIRECTORY %s\n",
1829                  (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context))); 
1830             if (!SetCurrentDirectory32A(PTR_SEG_OFF_TO_LIN(
1831                                                 DS_reg(context),
1832                                                 DX_reg(context)
1833                                         ))
1834             ) {
1835                 SET_CFLAG(context);
1836                 AL_reg(context) = DOS_ExtendedError;
1837             }
1838             break;
1839         case 0x41:  /* Delete file */
1840             TRACE(int21,"LONG FILENAME - DELETE FILE %s\n",
1841                  (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context))); 
1842             if (!DeleteFile32A(PTR_SEG_OFF_TO_LIN(
1843                                         DS_reg(context),
1844                                         DX_reg(context))
1845             )) {
1846                 SET_CFLAG(context);
1847                 AL_reg(context) = DOS_ExtendedError;
1848             }
1849             break;
1850         case 0x56:  /* Move (rename) file */
1851             TRACE(int21,"LONG FILENAME - RENAME FILE %s to %s stub\n",
1852                   (LPCSTR)PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context)),
1853                   (LPCSTR)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context))); 
1854         default:
1855             FIXME(int21, "Unimplemented long file name function:\n");
1856             INT_BARF( context, 0x21 );
1857             SET_CFLAG(context);
1858             AL_reg(context) = 0;
1859             break;
1860         }
1861         break;
1862
1863     case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
1864     case 0x72: /* MS-DOS 7 (Windows95) - ??? */
1865     case 0x73: /* MS-DOS 7 (Windows95) - DRIVE LOCKING ??? */
1866         TRACE(int21,"windows95 function AX %04x\n",
1867                     AX_reg(context));
1868         WARN(int21, "        returning unimplemented\n");
1869         SET_CFLAG(context);
1870         AL_reg(context) = 0;
1871         break;
1872
1873     case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
1874     case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
1875         break;
1876
1877     default:
1878         INT_BARF( context, 0x21 );
1879         break;
1880
1881     } /* END OF SWITCH */
1882
1883     if( bSetDOSExtendedError )          /* set general error condition */
1884     {   
1885         AX_reg(context) = DOS_ExtendedError;
1886         SET_CFLAG(context);
1887     }
1888
1889     if ((EFL_reg(context) & 0x0001))
1890       TRACE(int21, "failed, errorcode 0x%02x class 0x%02x action 0x%02x locus %02x\n",
1891           DOS_ExtendedError, DOS_ErrorClass, DOS_ErrorAction, DOS_ErrorLocus);
1892  
1893     TRACE(int21, "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
1894                  "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1895                  AX_reg(context), BX_reg(context), CX_reg(context),
1896                  DX_reg(context), SI_reg(context), DI_reg(context),
1897                  (WORD)DS_reg(context), (WORD)ES_reg(context),
1898                  EFL_reg(context));
1899 }
1900
1901 FARPROC16 WINAPI GetSetKernelDOSProc(FARPROC16 DosProc)
1902 {
1903         FIXME(int21, "(DosProc=%08x);\n", (UINT32)DosProc);
1904         return NULL;
1905 }