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