Stubbed GetDeviceGammaRamp.
[wine.git] / msdos / int21.c
blobacfbce0cb7aecfa3a0ae8bcc377de521d0409c92
1 /*
2 * DOS interrupt 21h handler
3 */
5 #include "config.h"
7 #include <time.h>
8 #include <fcntl.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #ifdef HAVE_SYS_FILE_H
12 # include <sys/file.h>
13 #endif
14 #include <string.h>
15 #include <sys/stat.h>
16 #include <sys/time.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #include <utime.h>
20 #include <ctype.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "ntddk.h"
24 #include "wingdi.h"
25 #include "winuser.h" /* SW_NORMAL */
26 #include "wine/winbase16.h"
27 #include "winerror.h"
28 #include "drive.h"
29 #include "file.h"
30 #include "heap.h"
31 #include "msdos.h"
32 #include "ldt.h"
33 #include "options.h"
34 #include "miscemu.h"
35 #include "task.h"
36 #include "dosexe.h" /* for the MZ_SUPPORTED define */
37 #include "debugtools.h"
38 #include "console.h"
40 DEFAULT_DEBUG_CHANNEL(int21)
41 #if defined(__svr4__) || defined(_SCO_DS)
42 /* SVR4 DOESNT do locking the same way must implement properly */
43 #define LOCK_EX 0
44 #define LOCK_SH 1
45 #define LOCK_NB 8
46 #endif
49 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
51 /* Define the drive parameter block, as used by int21/1F
52 * and int21/32. This table can be accessed through the
53 * global 'dpb' pointer, which points into the local dos
54 * heap.
56 struct DPB
58 BYTE drive_num; /* 0=A, etc. */
59 BYTE unit_num; /* Drive's unit number (?) */
60 WORD sector_size; /* Sector size in bytes */
61 BYTE high_sector; /* Highest sector in a cluster */
62 BYTE shift; /* Shift count (?) */
63 WORD reserved; /* Number of reserved sectors at start */
64 BYTE num_FAT; /* Number of FATs */
65 WORD dir_entries; /* Number of root dir entries */
66 WORD first_data; /* First data sector */
67 WORD high_cluster; /* Highest cluster number */
68 WORD sectors_in_FAT; /* Number of sectors per FAT */
69 WORD start_dir; /* Starting sector of first dir */
70 DWORD driver_head; /* Address of device driver header (?) */
71 BYTE media_ID; /* Media ID */
72 BYTE access_flag; /* Prev. accessed flag (0=yes,0xFF=no) */
73 DWORD next; /* Pointer to next DPB in list */
74 WORD free_search; /* Free cluster search start */
75 WORD free_clusters; /* Number of free clusters (0xFFFF=unknown) */
78 struct EDPB /* FAT32 extended Drive Parameter Block */
79 { /* from Ralf Brown's Interrupt List */
80 struct DPB dpb; /* first 24 bytes = original DPB */
82 BYTE edpb_flags; /* undocumented/unknown flags */
83 DWORD next_edpb; /* pointer to next EDPB */
84 WORD free_cluster; /* cluster to start search for free space on write, typically
85 the last cluster allocated */
86 WORD clusters_free; /* number of free clusters on drive or FFFF = unknown */
87 WORD clusters_free_hi; /* hiword of clusters_free */
88 WORD mirroring_flags; /* mirroring flags: bit 7 set = do not mirror active FAT */
89 /* bits 0-3 = 0-based number of the active FAT */
90 WORD info_sector; /* sector number of file system info sector, or FFFF for none */
91 WORD spare_boot_sector; /* sector number of backup boot sector, or FFFF for none */
92 DWORD first_cluster; /* sector number of the first cluster */
93 DWORD max_cluster; /* sector number of the last cluster */
94 DWORD fat_clusters; /* number of clusters occupied by FAT */
95 DWORD root_cluster; /* cluster number of start of root directory */
96 DWORD free_cluster2; /* same as free_cluster: cluster at which to start
97 search for free space when writing */
101 WORD CodePage = 437;
102 DWORD dpbsegptr;
104 struct DosHeap {
105 BYTE InDosFlag;
106 BYTE mediaID;
107 BYTE biosdate[8];
108 struct DPB dpb;
109 BYTE DummyDBCSLeadTable[6];
111 static struct DosHeap *heap;
112 static WORD DosHeapHandle;
114 extern char TempDirectory[];
116 static BOOL INT21_CreateHeap(void)
118 if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
120 WARN("Out of memory\n");
121 return FALSE;
123 heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
124 dpbsegptr = PTR_SEG_OFF_TO_SEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
125 heap->InDosFlag = 0;
126 strcpy(heap->biosdate, "01/01/80");
127 memset(heap->DummyDBCSLeadTable, 0, 6);
128 return TRUE;
131 static BYTE *GetCurrentDTA( CONTEXT86 *context )
133 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
135 /* FIXME: This assumes DTA was set correctly! */
136 return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
137 (DWORD)OFFSETOF(pTask->dta) );
141 void CreateBPB(int drive, BYTE *data, BOOL16 limited)
142 /* limited == TRUE is used with INT 0x21/0x440d */
144 if (drive > 1) {
145 setword(data, 512);
146 data[2] = 2;
147 setword(&data[3], 0);
148 data[5] = 2;
149 setword(&data[6], 240);
150 setword(&data[8], 64000);
151 data[0x0a] = 0xf8;
152 setword(&data[0x0b], 40);
153 setword(&data[0x0d], 56);
154 setword(&data[0x0f], 2);
155 setword(&data[0x11], 0);
156 if (!limited) {
157 setword(&data[0x1f], 800);
158 data[0x21] = 5;
159 setword(&data[0x22], 1);
161 } else { /* 1.44mb */
162 setword(data, 512);
163 data[2] = 2;
164 setword(&data[3], 0);
165 data[5] = 2;
166 setword(&data[6], 240);
167 setword(&data[8], 2880);
168 data[0x0a] = 0xf8;
169 setword(&data[0x0b], 6);
170 setword(&data[0x0d], 18);
171 setword(&data[0x0f], 2);
172 setword(&data[0x11], 0);
173 if (!limited) {
174 setword(&data[0x1f], 80);
175 data[0x21] = 7;
176 setword(&data[0x22], 2);
181 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
183 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
184 char root[] = "A:\\";
186 *root += DOS_GET_DRIVE( DL_reg(context) );
187 if (!GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
188 &free_clusters, &total_clusters )) return 0;
189 AX_reg(context) = cluster_sectors;
190 BX_reg(context) = free_clusters;
191 CX_reg(context) = sector_bytes;
192 DX_reg(context) = total_clusters;
193 return 1;
196 static int INT21_GetDriveAllocInfo( CONTEXT86 *context )
198 if (!INT21_GetFreeDiskSpace( context )) return 0;
199 if (!heap && !INT21_CreateHeap()) return 0;
200 heap->mediaID = 0xf0;
201 context->SegDs = DosHeapHandle;
202 BX_reg(context) = (int)&heap->mediaID - (int)heap;
203 return 1;
206 static int FillInDrivePB( int drive )
208 if(!DRIVE_IsValid(drive))
210 SetLastError( ERROR_INVALID_DRIVE );
211 return 0;
213 else if (heap || INT21_CreateHeap())
215 /* FIXME: I have no idea what a lot of this information should
216 * say or whether it even really matters since we're not allowing
217 * direct block access. However, some programs seem to depend on
218 * getting at least _something_ back from here. The 'next' pointer
219 * does worry me, though. Should we have a complete table of
220 * separate DPBs per drive? Probably, but I'm lazy. :-) -CH
222 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
223 heap->dpb.sector_size = 512;
224 heap->dpb.high_sector = 1;
225 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
226 heap->dpb.reserved = 0;
227 heap->dpb.num_FAT = 1;
228 heap->dpb.dir_entries = 2;
229 heap->dpb.first_data = 2;
230 heap->dpb.high_cluster = 64000;
231 heap->dpb.sectors_in_FAT = 1;
232 heap->dpb.start_dir = 1;
233 heap->dpb.driver_head = 0;
234 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
235 heap->dpb.access_flag = 0;
236 heap->dpb.next = 0;
237 heap->dpb.free_search = 0;
238 heap->dpb.free_clusters = 0xFFFF; /* unknown */
239 return 1;
242 return 0;
245 static void GetDrivePB( CONTEXT86 *context, int drive )
247 if (FillInDrivePB( drive ))
249 AL_reg(context) = 0x00;
250 context->SegDs = SELECTOROF(dpbsegptr);
251 BX_reg(context) = OFFSETOF(dpbsegptr);
253 else
255 AX_reg(context) = 0x00ff;
260 static void ioctlGetDeviceInfo( CONTEXT86 *context )
262 int curr_drive;
263 const DOS_DEVICE *dev;
265 TRACE("(%d)\n", BX_reg(context));
267 RESET_CFLAG(context);
269 /* DOS device ? */
270 if ((dev = DOSFS_GetDeviceByHandle( FILE_GetHandle(BX_reg(context)) )))
272 DX_reg(context) = dev->flags;
273 return;
276 /* it seems to be a file */
277 curr_drive = DRIVE_GetCurrentDrive();
278 DX_reg(context) = 0x0140 + curr_drive + ((curr_drive > 1) ? 0x0800 : 0);
279 /* no floppy */
280 /* bits 0-5 are current drive
281 * bit 6 - file has NOT been written..FIXME: correct?
282 * bit 8 - generate int24 if no diskspace on write/ read past end of file
283 * bit 11 - media not removable
284 * bit 14 - don't set file date/time on closing
285 * bit 15 - file is remote
289 static BOOL ioctlGenericBlkDevReq( CONTEXT86 *context )
291 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
292 int drive = DOS_GET_DRIVE( BL_reg(context) );
294 if (!DRIVE_IsValid(drive))
296 SetLastError( ERROR_FILE_NOT_FOUND );
297 return TRUE;
300 if (CH_reg(context) != 0x08)
302 INT_BARF( context, 0x21 );
303 return FALSE;
306 switch (CL_reg(context))
308 case 0x4a: /* lock logical volume */
309 TRACE("lock logical volume (%d) level %d mode %d\n",drive,BH_reg(context),DX_reg(context));
310 break;
312 case 0x60: /* get device parameters */
313 /* used by w4wgrp's winfile */
314 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
315 dataptr[0] = 0x04;
316 dataptr[6] = 0; /* media type */
317 if (drive > 1)
319 dataptr[1] = 0x05; /* fixed disk */
320 setword(&dataptr[2], 0x01); /* non removable */
321 setword(&dataptr[4], 0x300); /* # of cylinders */
323 else
325 dataptr[1] = 0x07; /* block dev, floppy */
326 setword(&dataptr[2], 0x02); /* removable */
327 setword(&dataptr[4], 80); /* # of cylinders */
329 CreateBPB(drive, &dataptr[7], TRUE);
330 RESET_CFLAG(context);
331 break;
333 case 0x41: /* write logical device track */
334 case 0x61: /* read logical device track */
336 BYTE drive = BL_reg(context) ?
337 BL_reg(context) : DRIVE_GetCurrentDrive();
338 WORD head = *(WORD *)dataptr+1;
339 WORD cyl = *(WORD *)dataptr+3;
340 WORD sect = *(WORD *)dataptr+5;
341 WORD nrsect = *(WORD *)dataptr+7;
342 BYTE *data = (BYTE *)dataptr+9;
343 int (*raw_func)(BYTE, DWORD, DWORD, BYTE *, BOOL);
345 raw_func = (CL_reg(context) == 0x41) ?
346 DRIVE_RawWrite : DRIVE_RawRead;
348 if (raw_func(drive, head*cyl*sect, nrsect, data, FALSE))
349 RESET_CFLAG(context);
350 else
352 AX_reg(context) = 0x1e; /* read fault */
353 SET_CFLAG(context);
356 break;
357 case 0x66:/* get disk serial number */
359 char label[12],fsname[9],path[4];
360 DWORD serial;
362 strcpy(path,"x:\\");path[0]=drive+'A';
363 GetVolumeInformationA(
364 path,label,12,&serial,NULL,NULL,fsname,9
366 *(WORD*)dataptr = 0;
367 memcpy(dataptr+2,&serial,4);
368 memcpy(dataptr+6,label ,11);
369 memcpy(dataptr+17,fsname,8);
371 break;
373 case 0x6a:
374 TRACE("logical volume %d unlocked.\n",drive);
375 break;
377 case 0x6f:
378 memset(dataptr+1, '\0', dataptr[0]-1);
379 dataptr[1] = dataptr[0];
380 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
381 dataptr[3] = 0xFF; /* no physical drive */
382 break;
384 case 0x72:
385 /* Trail on error implementation */
386 AX_reg(context) = GetDriveType16(BL_reg(context)) == DRIVE_CANNOTDETERMINE ? 0x0f : 0x01;
387 SET_CFLAG(context); /* Seems to be set all the time */
388 break;
390 default:
391 INT_BARF( context, 0x21 );
393 return FALSE;
396 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
398 char *filename =
399 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
400 char *fcb =
401 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
402 char *buffer, *s, *d;
404 AL_reg(context) = 0xff; /* failed */
406 TRACE("filename: '%s'\n", filename);
408 buffer = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + 1);
410 s = filename;
411 d = buffer;
412 while (*s)
414 if ((*s != ' ') && (*s != '\r') && (*s != '\n'))
415 *d++ = *s++;
416 else
417 break;
420 *d = '\0';
421 DOSFS_ToDosFCBFormat(buffer, fcb + 1);
422 *fcb = 0;
423 TRACE("FCB: '%s'\n", ((CHAR *)fcb + 1));
425 HeapFree( GetProcessHeap(), 0, buffer);
427 AL_reg(context) =
428 ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0;
430 /* point DS:SI to first unparsed character */
431 SI_reg(context) += (int)s - (int)filename;
434 static void INT21_GetSystemDate( CONTEXT86 *context )
436 SYSTEMTIME systime;
437 GetLocalTime( &systime );
438 CX_reg(context) = systime.wYear;
439 DX_reg(context) = (systime.wMonth << 8) | systime.wDay;
440 AX_reg(context) = systime.wDayOfWeek;
443 static void INT21_GetSystemTime( CONTEXT86 *context )
445 SYSTEMTIME systime;
446 GetLocalTime( &systime );
447 CX_reg(context) = (systime.wHour << 8) | systime.wMinute;
448 DX_reg(context) = (systime.wSecond << 8) | (systime.wMilliseconds / 10);
451 /* Many calls translate a drive argument like this:
452 drive number (00h = default, 01h = A:, etc)
454 static char drivestring[]="default";
456 char *INT21_DriveName(int drive)
459 if(drive >0)
461 drivestring[0]= (unsigned char)drive + '@';
462 drivestring[1]=':';
463 drivestring[2]=0;
465 return drivestring;
467 static BOOL INT21_CreateFile( CONTEXT86 *context )
469 AX_reg(context) = _lcreat16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
470 context->Edx ), CX_reg(context) );
471 return (AX_reg(context) == (WORD)HFILE_ERROR16);
474 static HFILE16 _lcreat16_uniq( LPCSTR path, INT attr )
476 /* Mask off all flags not explicitly allowed by the doc */
477 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
478 return FILE_AllocDosHandle( CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
479 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
480 CREATE_NEW, attr, -1 ));
483 static void OpenExistingFile( CONTEXT86 *context )
485 AX_reg(context) = _lopen16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
486 AL_reg(context) );
487 if (AX_reg(context) == (WORD)HFILE_ERROR16)
489 AX_reg(context) = GetLastError();
490 SET_CFLAG(context);
494 static BOOL INT21_ExtendedOpenCreateFile(CONTEXT86 *context )
496 BOOL bExtendedError = FALSE;
497 BYTE action = DL_reg(context);
499 /* Shuffle arguments to call OpenExistingFile */
500 AL_reg(context) = BL_reg(context);
501 DX_reg(context) = SI_reg(context);
502 /* BX,CX and DX should be preserved */
503 OpenExistingFile(context);
505 if ((context->EFlags & 0x0001) == 0) /* File exists */
507 UINT16 uReturnCX = 0;
509 /* Now decide what do do */
511 if ((action & 0x07) == 0)
513 _lclose16( AX_reg(context) );
514 AX_reg(context) = 0x0050; /*File exists*/
515 SET_CFLAG(context);
516 WARN("extended open/create: failed because file exists \n");
518 else if ((action & 0x07) == 2)
520 /* Truncate it, but first check if opened for write */
521 if ((BL_reg(context) & 0x0007)== 0)
523 _lclose16( AX_reg(context) );
524 WARN("extended open/create: failed, trunc on ro file\n");
525 AX_reg(context) = 0x000C; /*Access code invalid*/
526 SET_CFLAG(context);
528 else
530 TRACE("extended open/create: Closing before truncate\n");
531 if (_lclose16( AX_reg(context) ))
533 WARN("extended open/create: close before trunc failed\n");
534 AX_reg(context) = 0x0019; /*Seek Error*/
535 CX_reg(context) = 0;
536 SET_CFLAG(context);
538 /* Shuffle arguments to call CreateFile */
540 TRACE("extended open/create: Truncating\n");
541 AL_reg(context) = BL_reg(context);
542 /* CX is still the same */
543 DX_reg(context) = SI_reg(context);
544 bExtendedError = INT21_CreateFile(context);
546 if (context->EFlags & 0x0001) /*no file open, flags set */
548 WARN("extended open/create: trunc failed\n");
549 return bExtendedError;
551 uReturnCX = 0x3;
554 else uReturnCX = 0x1;
556 CX_reg(context) = uReturnCX;
558 else /* file does not exist */
560 RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
561 if ((action & 0xF0)== 0)
563 CX_reg(context) = 0;
564 SET_CFLAG(context);
565 WARN("extended open/create: failed, file dosen't exist\n");
567 else
569 /* Shuffle arguments to call CreateFile */
570 TRACE("extended open/create: Creating\n");
571 AL_reg(context) = BL_reg(context);
572 /* CX should still be the same */
573 DX_reg(context) = SI_reg(context);
574 bExtendedError = INT21_CreateFile(context);
575 if (context->EFlags & 0x0001) /*no file open, flags set */
577 WARN("extended open/create: create failed\n");
578 return bExtendedError;
580 CX_reg(context) = 2;
584 return bExtendedError;
588 static BOOL INT21_ChangeDir( CONTEXT86 *context )
590 int drive;
591 char *dirname = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
593 TRACE("changedir %s\n", dirname);
594 if (dirname[0] && (dirname[1] == ':'))
596 drive = toupper(dirname[0]) - 'A';
597 dirname += 2;
599 else drive = DRIVE_GetCurrentDrive();
600 return DRIVE_Chdir( drive, dirname );
604 static int INT21_FindFirst( CONTEXT86 *context )
606 char *p;
607 const char *path;
608 DOS_FULL_NAME full_name;
609 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
611 path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
612 dta->unixPath = NULL;
613 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
615 AX_reg(context) = GetLastError();
616 SET_CFLAG(context);
617 return 0;
619 dta->unixPath = HEAP_strdupA( GetProcessHeap(), 0, full_name.long_name );
620 p = strrchr( dta->unixPath, '/' );
621 *p = '\0';
623 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
624 * (doesn't matter as it is set below anyway)
626 if (!DOSFS_ToDosFCBFormat( p + 1, dta->mask ))
628 HeapFree( GetProcessHeap(), 0, dta->unixPath );
629 dta->unixPath = NULL;
630 SetLastError( ERROR_FILE_NOT_FOUND );
631 AX_reg(context) = ERROR_FILE_NOT_FOUND;
632 SET_CFLAG(context);
633 return 0;
635 dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
636 : DRIVE_GetCurrentDrive();
637 dta->count = 0;
638 dta->search_attr = CL_reg(context);
639 return 1;
643 static int INT21_FindNext( CONTEXT86 *context )
645 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
646 WIN32_FIND_DATAA entry;
647 int count;
649 if (!dta->unixPath) return 0;
650 if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
651 dta->search_attr, dta->count, &entry )))
653 HeapFree( GetProcessHeap(), 0, dta->unixPath );
654 dta->unixPath = NULL;
655 return 0;
657 if ((int)dta->count + count > 0xffff)
659 WARN("Too many directory entries in %s\n", dta->unixPath );
660 HeapFree( GetProcessHeap(), 0, dta->unixPath );
661 dta->unixPath = NULL;
662 return 0;
664 dta->count += count;
665 dta->fileattr = entry.dwFileAttributes;
666 dta->filesize = entry.nFileSizeLow;
667 FileTimeToDosDateTime( &entry.ftLastWriteTime,
668 &dta->filedate, &dta->filetime );
669 strcpy( dta->filename, entry.cAlternateFileName );
670 if (!memchr(dta->mask,'?',11)) {
671 /* wildcardless search, release resources in case no findnext will
672 * be issued, and as a workaround in case file creation messes up
673 * findnext, as sometimes happens with pkunzip */
674 HeapFree( GetProcessHeap(), 0, dta->unixPath );
675 dta->unixPath = NULL;
677 return 1;
681 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
683 static int counter = 0;
684 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
685 char *p = name + strlen(name);
687 /* despite what Ralf Brown says, some programs seem to call without
688 * ending backslash (DOS accepts that, so we accept it too) */
689 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
691 for (;;)
693 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
694 counter = (counter + 1) % 1000;
696 if ((AX_reg(context) = _lcreat16_uniq( name, 0 )) != (WORD)HFILE_ERROR16)
698 TRACE("created %s\n", name );
699 return TRUE;
701 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
706 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context )
708 int drive = DOS_GET_DRIVE( DL_reg(context) );
709 char *ptr = (char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
711 if (!DRIVE_IsValid(drive))
713 SetLastError( ERROR_INVALID_DRIVE );
714 return FALSE;
716 lstrcpynA( ptr, DRIVE_GetDosCwd(drive), 64 );
717 AX_reg(context) = 0x0100; /* success return code */
718 return TRUE;
722 static void INT21_GetDBCSLeadTable( CONTEXT86 *context )
724 if (heap || INT21_CreateHeap())
725 { /* return an empty table just as DOS 4.0+ does */
726 context->SegDs = DosHeapHandle;
727 SI_reg(context) = (int)&heap->DummyDBCSLeadTable - (int)heap;
729 else
731 AX_reg(context) = 0x1; /* error */
732 SET_CFLAG(context);
737 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
739 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
740 int drive = DOS_GET_DRIVE( BL_reg(context) );
742 if (!DRIVE_IsValid(drive))
744 SetLastError( ERROR_INVALID_DRIVE );
745 return 0;
748 *(WORD *)dataptr = 0;
749 *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
750 memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
751 strncpy(dataptr + 0x11, "FAT16 ", 8);
752 return 1;
756 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
758 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
759 int drive = DOS_GET_DRIVE( BL_reg(context) );
761 if (!DRIVE_IsValid(drive))
763 SetLastError( ERROR_INVALID_DRIVE );
764 return 0;
767 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
768 return 1;
772 /* microsoft's programmers should be shot for using CP/M style int21
773 calls in Windows for Workgroup's winfile.exe */
775 static int INT21_FindFirstFCB( CONTEXT86 *context )
777 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
778 FINDFILE_FCB *pFCB;
779 LPCSTR root, cwd;
780 int drive;
782 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
783 else pFCB = (FINDFILE_FCB *)fcb;
784 drive = DOS_GET_DRIVE( pFCB->drive );
785 if (!DRIVE_IsValid( drive )) return 0;
786 root = DRIVE_GetRoot( drive );
787 cwd = DRIVE_GetUnixCwd( drive );
788 pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
789 strlen(root)+strlen(cwd)+2 );
790 if (!pFCB->unixPath) return 0;
791 strcpy( pFCB->unixPath, root );
792 strcat( pFCB->unixPath, "/" );
793 strcat( pFCB->unixPath, cwd );
794 pFCB->count = 0;
795 return 1;
799 static int INT21_FindNextFCB( CONTEXT86 *context )
801 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
802 FINDFILE_FCB *pFCB;
803 DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA(context);
804 WIN32_FIND_DATAA entry;
805 BYTE attr;
806 int count;
808 if (*fcb == 0xff) /* extended FCB ? */
810 attr = fcb[6];
811 pFCB = (FINDFILE_FCB *)(fcb + 7);
813 else
815 attr = 0;
816 pFCB = (FINDFILE_FCB *)fcb;
819 if (!pFCB->unixPath) return 0;
820 if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
821 DOS_GET_DRIVE( pFCB->drive ), attr,
822 pFCB->count, &entry )))
824 HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
825 pFCB->unixPath = NULL;
826 return 0;
828 pFCB->count += count;
830 if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
831 *(BYTE *)pResult = 0xff;
832 (BYTE *)pResult +=6; /* leave reserved field behind */
833 *(BYTE *)pResult = entry.dwFileAttributes;
834 ((BYTE *)pResult)++;
836 *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
837 ((BYTE *)pResult)++;
838 pResult->fileattr = entry.dwFileAttributes;
839 pResult->cluster = 0; /* what else? */
840 pResult->filesize = entry.nFileSizeLow;
841 memset( pResult->reserved, 0, sizeof(pResult->reserved) );
842 FileTimeToDosDateTime( &entry.ftLastWriteTime,
843 &pResult->filedate, &pResult->filetime );
845 /* Convert file name to FCB format */
847 memset( pResult->filename, ' ', sizeof(pResult->filename) );
848 if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
849 else if (!strcmp( entry.cAlternateFileName, ".." ))
850 pResult->filename[0] = pResult->filename[1] = '.';
851 else
853 char *p = strrchr( entry.cAlternateFileName, '.' );
854 if (p && p[1] && (p != entry.cAlternateFileName))
856 memcpy( pResult->filename, entry.cAlternateFileName,
857 min( (p - entry.cAlternateFileName), 8 ) );
858 memcpy( pResult->filename + 8, p + 1, min( strlen(p), 3 ) );
860 else
861 memcpy( pResult->filename, entry.cAlternateFileName,
862 min( strlen(entry.cAlternateFileName), 8 ) );
864 return 1;
868 static void DeleteFileFCB( CONTEXT86 *context )
870 FIXME("(%p): stub\n", context);
873 static void RenameFileFCB( CONTEXT86 *context )
875 FIXME("(%p): stub\n", context);
880 static void fLock( CONTEXT86 * context )
883 switch ( AX_reg(context) & 0xff )
885 case 0x00: /* LOCK */
886 TRACE("lock handle %d offset %ld length %ld\n",
887 BX_reg(context),
888 MAKELONG(DX_reg(context),CX_reg(context)),
889 MAKELONG(DI_reg(context),SI_reg(context))) ;
890 if (!LockFile(FILE_GetHandle(BX_reg(context)),
891 MAKELONG(DX_reg(context),CX_reg(context)), 0,
892 MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
893 AX_reg(context) = GetLastError();
894 SET_CFLAG(context);
896 break;
898 case 0x01: /* UNLOCK */
899 TRACE("unlock handle %d offset %ld length %ld\n",
900 BX_reg(context),
901 MAKELONG(DX_reg(context),CX_reg(context)),
902 MAKELONG(DI_reg(context),SI_reg(context))) ;
903 if (!UnlockFile(FILE_GetHandle(BX_reg(context)),
904 MAKELONG(DX_reg(context),CX_reg(context)), 0,
905 MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
906 AX_reg(context) = GetLastError();
907 SET_CFLAG(context);
909 return;
910 default:
911 AX_reg(context) = 0x0001;
912 SET_CFLAG(context);
913 return;
917 static BOOL
918 INT21_networkfunc (CONTEXT86 *context)
920 switch (AL_reg(context)) {
921 case 0x00: /* Get machine name. */
923 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
924 TRACE("getting machine name to %p\n", dst);
925 if (gethostname (dst, 15))
927 WARN("failed!\n");
928 SetLastError( ER_NoNetwork );
929 return TRUE;
930 } else {
931 int len = strlen (dst);
932 while (len < 15)
933 dst[len++] = ' ';
934 dst[15] = 0;
935 CH_reg(context) = 1; /* Valid */
936 CL_reg(context) = 1; /* NETbios number??? */
937 TRACE("returning %s\n", debugstr_an (dst, 16));
938 return FALSE;
942 default:
943 SetLastError( ER_NoNetwork );
944 return TRUE;
948 static void INT21_SetCurrentPSP(WORD psp)
950 LPDOSTASK lpDosTask = MZ_Current();
951 if (lpDosTask)
952 lpDosTask->psp_seg = psp;
953 else
954 ERR("Cannot change PSP for non-DOS task!\n");
957 static WORD INT21_GetCurrentPSP()
959 LPDOSTASK lpDosTask = MZ_Current();
960 if (lpDosTask)
961 return lpDosTask->psp_seg;
962 else
963 return GetCurrentPDB16();
967 /***********************************************************************
968 * INT21_GetExtendedError
970 static void INT21_GetExtendedError( CONTEXT86 *context )
972 BYTE class, action, locus;
973 WORD error = GetLastError();
975 switch(error)
977 case ERROR_SUCCESS:
978 class = action = locus = 0;
979 break;
980 case ERROR_DIR_NOT_EMPTY:
981 class = EC_Exists;
982 action = SA_Ignore;
983 locus = EL_Disk;
984 break;
985 case ERROR_ACCESS_DENIED:
986 class = EC_AccessDenied;
987 action = SA_Abort;
988 locus = EL_Disk;
989 break;
990 case ERROR_CANNOT_MAKE:
991 class = EC_AccessDenied;
992 action = SA_Abort;
993 locus = EL_Unknown;
994 break;
995 case ERROR_DISK_FULL:
996 case ERROR_HANDLE_DISK_FULL:
997 class = EC_MediaError;
998 action = SA_Abort;
999 locus = EL_Disk;
1000 break;
1001 case ERROR_FILE_EXISTS:
1002 case ERROR_ALREADY_EXISTS:
1003 class = EC_Exists;
1004 action = SA_Abort;
1005 locus = EL_Disk;
1006 break;
1007 case ERROR_FILE_NOT_FOUND:
1008 class = EC_NotFound;
1009 action = SA_Abort;
1010 locus = EL_Disk;
1011 break;
1012 case ER_GeneralFailure:
1013 class = EC_SystemFailure;
1014 action = SA_Abort;
1015 locus = EL_Unknown;
1016 break;
1017 case ERROR_INVALID_DRIVE:
1018 class = EC_MediaError;
1019 action = SA_Abort;
1020 locus = EL_Disk;
1021 break;
1022 case ERROR_INVALID_HANDLE:
1023 class = EC_ProgramError;
1024 action = SA_Abort;
1025 locus = EL_Disk;
1026 break;
1027 case ERROR_LOCK_VIOLATION:
1028 class = EC_AccessDenied;
1029 action = SA_Abort;
1030 locus = EL_Disk;
1031 break;
1032 case ERROR_NO_MORE_FILES:
1033 class = EC_MediaError;
1034 action = SA_Abort;
1035 locus = EL_Disk;
1036 break;
1037 case ER_NoNetwork:
1038 class = EC_NotFound;
1039 action = SA_Abort;
1040 locus = EL_Network;
1041 break;
1042 case ERROR_NOT_ENOUGH_MEMORY:
1043 class = EC_OutOfResource;
1044 action = SA_Abort;
1045 locus = EL_Memory;
1046 break;
1047 case ERROR_PATH_NOT_FOUND:
1048 class = EC_NotFound;
1049 action = SA_Abort;
1050 locus = EL_Disk;
1051 break;
1052 case ERROR_SEEK:
1053 class = EC_NotFound;
1054 action = SA_Ignore;
1055 locus = EL_Disk;
1056 break;
1057 case ERROR_SHARING_VIOLATION:
1058 class = EC_Temporary;
1059 action = SA_Retry;
1060 locus = EL_Disk;
1061 break;
1062 case ERROR_TOO_MANY_OPEN_FILES:
1063 class = EC_ProgramError;
1064 action = SA_Abort;
1065 locus = EL_Disk;
1066 break;
1067 default:
1068 FIXME("Unknown error %d\n", error );
1069 class = EC_SystemFailure;
1070 action = SA_Abort;
1071 locus = EL_Unknown;
1072 break;
1074 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
1075 error, class, action, locus );
1076 AX_reg(context) = error;
1077 BH_reg(context) = class;
1078 BL_reg(context) = action;
1079 CH_reg(context) = locus;
1082 /***********************************************************************
1083 * DOS3Call (KERNEL.102)
1085 void WINAPI DOS3Call( CONTEXT86 *context )
1087 BOOL bSetDOSExtendedError = FALSE;
1090 TRACE("AX=%04x BX=%04x CX=%04x DX=%04x "
1091 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1092 AX_reg(context), BX_reg(context), CX_reg(context), DX_reg(context),
1093 SI_reg(context), DI_reg(context),
1094 (WORD)context->SegDs, (WORD)context->SegEs,
1095 context->EFlags );
1098 if (AH_reg(context) == 0x59) /* Get extended error info */
1100 INT21_GetExtendedError( context );
1101 return;
1104 if (AH_reg(context) == 0x0C) /* Flush buffer and read standard input */
1106 TRACE("FLUSH BUFFER AND READ STANDARD INPUT\n");
1107 /* no flush here yet */
1108 AH_reg(context) = AL_reg(context);
1111 if (AH_reg(context)>=0x2f) {
1112 /* extended error is used by (at least) functions 0x2f to 0x62 */
1113 SetLastError(0);
1115 RESET_CFLAG(context); /* Not sure if this is a good idea */
1117 switch(AH_reg(context))
1119 case 0x03: /* READ CHARACTER FROM STDAUX */
1120 case 0x04: /* WRITE CHARACTER TO STDAUX */
1121 case 0x05: /* WRITE CHARACTER TO PRINTER */
1122 case 0x0f: /* OPEN FILE USING FCB */
1123 case 0x10: /* CLOSE FILE USING FCB */
1124 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
1125 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
1126 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
1127 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
1128 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
1129 case 0x23: /* GET FILE SIZE FOR FCB */
1130 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
1131 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
1132 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
1133 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
1134 INT_BARF( context, 0x21 );
1135 break;
1137 case 0x00: /* TERMINATE PROGRAM */
1138 TRACE("TERMINATE PROGRAM\n");
1139 ExitThread( 0 );
1140 break;
1142 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
1143 TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
1144 AL_reg(context) = CONSOLE_GetCharacter();
1145 /* FIXME: no echo */
1146 break;
1148 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
1149 TRACE("Write Character to Standard Output\n");
1150 CONSOLE_Write(DL_reg(context), 0, 0, 0);
1151 break;
1153 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
1154 /* FIXME: Use DOSDEV_Peek/Read/Write(DOSDEV_Console(),...) !! */
1155 if (DL_reg(context) == 0xff) {
1156 static char scan = 0;
1157 TRACE("Direct Console Input\n");
1158 if (scan) {
1159 /* return pending scancode */
1160 AL_reg(context) = scan;
1161 RESET_ZFLAG(context);
1162 scan = 0;
1163 } else {
1164 char ascii;
1165 if (INT_Int16ReadChar(&ascii,&scan,TRUE)) {
1166 INT_Int16ReadChar(&ascii,&scan,FALSE);
1167 /* return ASCII code */
1168 AL_reg(context) = ascii;
1169 RESET_ZFLAG(context);
1170 /* return scan code on next call only if ascii==0 */
1171 if (ascii) scan = 0;
1172 } else {
1173 /* nothing pending, clear everything */
1174 AL_reg(context) = 0;
1175 SET_ZFLAG(context);
1176 scan = 0; /* just in case */
1179 } else {
1180 TRACE("Direct Console Output\n");
1181 CONSOLE_Write(DL_reg(context), 0, 0, 0);
1183 break;
1185 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
1186 /* FIXME: Use DOSDEV_Peek/Read(DOSDEV_Console(),...) !! */
1187 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
1188 INT_Int16ReadChar(&AL_reg(context), NULL, FALSE);
1189 break;
1191 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
1192 /* FIXME: Use DOSDEV_Peek/Read(DOSDEV_Console(),...) !! */
1193 TRACE("CHARACTER INPUT WITHOUT ECHO\n");
1194 INT_Int16ReadChar(&AL_reg(context), NULL, FALSE);
1195 break;
1197 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
1198 TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
1199 context->SegDs,DX_reg(context) );
1201 LPSTR data = CTX_SEG_OFF_TO_LIN(context,context->SegDs,context->Edx);
1202 LPSTR p = data;
1203 /* do NOT use strchr() to calculate the string length,
1204 as '\0' is valid string content, too !
1205 Maybe we should check for non-'$' strings, but DOS doesn't. */
1206 while (*p != '$') p++;
1207 _hwrite16( 1, data, (int)p - (int)data);
1208 AL_reg(context) = '$'; /* yes, '$' (0x24) gets returned in AL */
1210 break;
1212 case 0x0a: /* BUFFERED INPUT */
1214 char *buffer = ((char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1215 context->Edx ));
1216 int res;
1218 TRACE("BUFFERED INPUT (size=%d)\n",buffer[0]);
1219 if (buffer[1])
1220 TRACE("Handle old chars in buffer!\n");
1221 res=_lread16( 0, buffer+2,buffer[0]);
1222 buffer[1]=res;
1223 if(buffer[res+1] == '\n')
1224 buffer[res+1] = '\r';
1225 break;
1228 case 0x0b: {/* GET STDIN STATUS */
1229 char x1,x2;
1231 if (CONSOLE_CheckForKeystroke(&x1,&x2))
1232 AL_reg(context) = 0xff;
1233 else
1234 AL_reg(context) = 0;
1235 break;
1237 case 0x2e: /* SET VERIFY FLAG */
1238 TRACE("SET VERIFY FLAG ignored\n");
1239 /* we cannot change the behaviour anyway, so just ignore it */
1240 break;
1242 case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
1243 case 0x1d:
1244 case 0x1e:
1245 case 0x20:
1246 case 0x6b: /* NULL FUNCTION */
1247 AL_reg(context) = 0;
1248 break;
1250 case 0x5c: /* "FLOCK" - RECORD LOCKING */
1251 fLock(context);
1252 break;
1254 case 0x0d: /* DISK BUFFER FLUSH */
1255 TRACE("DISK BUFFER FLUSH ignored\n");
1256 RESET_CFLAG(context); /* dos 6+ only */
1257 break;
1259 case 0x0e: /* SELECT DEFAULT DRIVE */
1260 TRACE("SELECT DEFAULT DRIVE %d\n", DL_reg(context));
1261 DRIVE_SetCurrentDrive( DL_reg(context) );
1262 AL_reg(context) = MAX_DOS_DRIVES;
1263 break;
1265 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1266 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
1267 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1268 if (!INT21_FindFirstFCB(context))
1270 AL_reg(context) = 0xff;
1271 break;
1273 /* else fall through */
1275 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1276 AL_reg(context) = INT21_FindNextFCB(context) ? 0x00 : 0xff;
1277 break;
1279 case 0x13: /* DELETE FILE USING FCB */
1280 DeleteFileFCB(context);
1281 break;
1283 case 0x17: /* RENAME FILE USING FCB */
1284 RenameFileFCB(context);
1285 break;
1287 case 0x19: /* GET CURRENT DEFAULT DRIVE */
1288 AL_reg(context) = DRIVE_GetCurrentDrive();
1289 break;
1291 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1293 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1294 pTask->dta = PTR_SEG_OFF_TO_SEGPTR(context->SegDs,DX_reg(context));
1295 TRACE("Set DTA: %08lx\n", pTask->dta);
1297 break;
1299 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1300 DL_reg(context) = 0;
1301 if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1302 break;
1304 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1305 if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1306 break;
1308 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1309 GetDrivePB(context, DRIVE_GetCurrentDrive());
1310 break;
1312 case 0x25: /* SET INTERRUPT VECTOR */
1313 INT_CtxSetHandler( context, AL_reg(context),
1314 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( context->SegDs,
1315 DX_reg(context)));
1316 break;
1318 case 0x29: /* PARSE FILENAME INTO FCB */
1319 INT21_ParseFileNameIntoFCB(context);
1320 break;
1322 case 0x2a: /* GET SYSTEM DATE */
1323 INT21_GetSystemDate(context);
1324 break;
1326 case 0x2b: /* SET SYSTEM DATE */
1327 FIXME("SetSystemDate(%02d/%02d/%04d): not allowed\n",
1328 DL_reg(context), DH_reg(context), CX_reg(context) );
1329 AL_reg(context) = 0; /* Let's pretend we succeeded */
1330 break;
1332 case 0x2c: /* GET SYSTEM TIME */
1333 INT21_GetSystemTime(context);
1334 break;
1336 case 0x2d: /* SET SYSTEM TIME */
1337 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1338 CH_reg(context), CL_reg(context),
1339 DH_reg(context), DL_reg(context) );
1340 AL_reg(context) = 0; /* Let's pretend we succeeded */
1341 break;
1343 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1344 TRACE("GET DISK TRANSFER AREA ADDRESS\n");
1346 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1347 context->SegEs = SELECTOROF( pTask->dta );
1348 BX_reg(context) = OFFSETOF( pTask->dta );
1350 break;
1352 case 0x30: /* GET DOS VERSION */
1353 TRACE("GET DOS VERSION %s requested\n",
1354 (AL_reg(context) == 0x00)?"OEM number":"version flag");
1355 AX_reg(context) = (HIWORD(GetVersion16()) >> 8) |
1356 (HIWORD(GetVersion16()) << 8);
1357 #if 0
1358 AH_reg(context) = 0x7;
1359 AL_reg(context) = 0xA;
1360 #endif
1362 BX_reg(context) = 0x00FF; /* 0x123456 is Wine's serial # */
1363 CX_reg(context) = 0x0000;
1364 break;
1366 case 0x31: /* TERMINATE AND STAY RESIDENT */
1367 FIXME("TERMINATE AND STAY RESIDENT stub\n");
1368 break;
1370 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1371 TRACE("GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
1372 INT21_DriveName( DL_reg(context)));
1373 GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
1374 break;
1376 case 0x33: /* MULTIPLEXED */
1377 switch (AL_reg(context))
1379 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1380 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
1381 DL_reg(context) = DOSCONF_config.brk_flag;
1382 break;
1384 case 0x01: /* SET EXTENDED BREAK STATE */
1385 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
1386 DOSCONF_config.brk_flag = (DL_reg(context) > 0);
1387 break;
1389 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1390 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
1391 /* ugly coding in order to stay reentrant */
1392 if (DL_reg(context))
1394 DL_reg(context) = DOSCONF_config.brk_flag;
1395 DOSCONF_config.brk_flag = 1;
1397 else
1399 DL_reg(context) = DOSCONF_config.brk_flag;
1400 DOSCONF_config.brk_flag = 0;
1402 break;
1404 case 0x05: /* GET BOOT DRIVE */
1405 TRACE("GET BOOT DRIVE\n");
1406 DL_reg(context) = 3;
1407 /* c: is Wine's bootdrive (a: is 1)*/
1408 break;
1410 case 0x06: /* GET TRUE VERSION NUMBER */
1411 TRACE("GET TRUE VERSION NUMBER\n");
1412 BX_reg(context) = (HIWORD(GetVersion16() >> 8)) |
1413 (HIWORD(GetVersion16() << 8));
1414 DX_reg(context) = 0x00;
1415 break;
1417 default:
1418 INT_BARF( context, 0x21 );
1419 break;
1421 break;
1423 case 0x34: /* GET ADDRESS OF INDOS FLAG */
1424 TRACE("GET ADDRESS OF INDOS FLAG\n");
1425 if (!heap) INT21_CreateHeap();
1426 context->SegEs = DosHeapHandle;
1427 BX_reg(context) = (int)&heap->InDosFlag - (int)heap;
1428 break;
1430 case 0x35: /* GET INTERRUPT VECTOR */
1431 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
1433 FARPROC16 addr = INT_CtxGetHandler( context, AL_reg(context) );
1434 context->SegEs = SELECTOROF(addr);
1435 BX_reg(context) = OFFSETOF(addr);
1437 break;
1439 case 0x36: /* GET FREE DISK SPACE */
1440 TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
1441 INT21_DriveName( DL_reg(context)));
1442 if (!INT21_GetFreeDiskSpace(context)) AX_reg(context) = 0xffff;
1443 break;
1445 case 0x37:
1447 unsigned char switchchar='/';
1448 switch (AL_reg(context))
1450 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
1451 TRACE("SWITCHAR - GET SWITCH CHARACTER\n");
1452 AL_reg(context) = 0x00; /* success*/
1453 DL_reg(context) = switchchar;
1454 break;
1455 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
1456 TRACE("SWITCHAR - SET SWITCH CHARACTER\n");
1457 switchchar = DL_reg(context);
1458 AL_reg(context) = 0x00; /* success*/
1459 break;
1460 default: /*"AVAILDEV" - SPECIFY \DEV\ PREFIX USE*/
1461 INT_BARF( context, 0x21 );
1462 break;
1464 break;
1467 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1468 TRACE("GET COUNTRY-SPECIFIC INFORMATION for country 0x%02x\n",
1469 AL_reg(context));
1470 AX_reg(context) = 0x02; /* no country support available */
1471 SET_CFLAG(context);
1472 break;
1474 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1475 TRACE("MKDIR %s\n",
1476 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1477 bSetDOSExtendedError = (!CreateDirectory16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1478 context->Edx ), NULL));
1479 /* FIXME: CreateDirectory's LastErrors will clash with the ones
1480 * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1481 * denied), while CreateDirectory return several ones. remap some of
1482 * them. -Marcus
1484 if (bSetDOSExtendedError) {
1485 switch (GetLastError()) {
1486 case ERROR_ALREADY_EXISTS:
1487 case ERROR_FILENAME_EXCED_RANGE:
1488 case ERROR_DISK_FULL:
1489 SetLastError(ERROR_ACCESS_DENIED);
1490 break;
1491 default: break;
1494 break;
1496 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1497 TRACE("RMDIR %s\n",
1498 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1499 bSetDOSExtendedError = (!RemoveDirectory16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1500 context->Edx )));
1501 break;
1503 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1504 TRACE("CHDIR %s\n",
1505 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1506 bSetDOSExtendedError = !INT21_ChangeDir(context);
1507 break;
1509 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1510 TRACE("CREAT flag 0x%02x %s\n",CX_reg(context),
1511 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1512 bSetDOSExtendedError = INT21_CreateFile( context );
1513 break;
1515 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1516 TRACE("OPEN mode 0x%02x %s\n",AL_reg(context),
1517 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1518 OpenExistingFile(context);
1519 break;
1521 case 0x3e: /* "CLOSE" - CLOSE FILE */
1522 TRACE("CLOSE handle %d\n",BX_reg(context));
1523 bSetDOSExtendedError = ((AX_reg(context) = _lclose16( BX_reg(context) )) != 0);
1524 break;
1526 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1527 TRACE("READ from %d to %04lX:%04X for %d byte\n",BX_reg(context),
1528 context->SegDs,DX_reg(context),CX_reg(context) );
1530 LONG result;
1531 if (ISV86(context))
1532 result = _hread16( BX_reg(context),
1533 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1534 context->Edx ),
1535 CX_reg(context) );
1536 else
1537 result = WIN16_hread( BX_reg(context),
1538 PTR_SEG_OFF_TO_SEGPTR( context->SegDs,
1539 context->Edx ),
1540 CX_reg(context) );
1541 if (result == -1) bSetDOSExtendedError = TRUE;
1542 else AX_reg(context) = (WORD)result;
1544 break;
1546 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1547 TRACE("WRITE from %04lX:%04X to handle %d for %d byte\n",
1548 context->SegDs,DX_reg(context),BX_reg(context),CX_reg(context) );
1550 LONG result = _hwrite16( BX_reg(context),
1551 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1552 context->Edx ),
1553 CX_reg(context) );
1554 if (result == -1) bSetDOSExtendedError = TRUE;
1555 else AX_reg(context) = (WORD)result;
1557 break;
1559 case 0x41: /* "UNLINK" - DELETE FILE */
1560 TRACE("UNLINK %s\n",
1561 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1562 bSetDOSExtendedError = (!DeleteFileA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1563 context->Edx )));
1564 break;
1566 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1567 TRACE("LSEEK handle %d offset %ld from %s\n",
1568 BX_reg(context), MAKELONG(DX_reg(context),CX_reg(context)),
1569 (AL_reg(context)==0)?"start of file":(AL_reg(context)==1)?
1570 "current file position":"end of file");
1572 LONG status = _llseek16( BX_reg(context),
1573 MAKELONG(DX_reg(context),CX_reg(context)),
1574 AL_reg(context) );
1575 if (status == -1) bSetDOSExtendedError = TRUE;
1576 else
1578 AX_reg(context) = LOWORD(status);
1579 DX_reg(context) = HIWORD(status);
1582 break;
1584 case 0x43: /* FILE ATTRIBUTES */
1585 switch (AL_reg(context))
1587 case 0x00:
1588 TRACE("GET FILE ATTRIBUTES for %s\n",
1589 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1590 AX_reg(context) = (WORD)GetFileAttributesA(
1591 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1592 context->Edx));
1593 if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1594 else CX_reg(context) = AX_reg(context);
1595 break;
1597 case 0x01:
1598 TRACE("SET FILE ATTRIBUTES 0x%02x for %s\n", CX_reg(context),
1599 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1600 bSetDOSExtendedError =
1601 (!SetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1602 context->Edx),
1603 CX_reg(context) ));
1604 break;
1605 case 0x02:
1606 FIXME("GET COMPRESSED FILE SIZE for %s stub\n",
1607 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1609 break;
1611 case 0x44: /* IOCTL */
1612 switch (AL_reg(context))
1614 case 0x00:
1615 ioctlGetDeviceInfo(context);
1616 break;
1618 case 0x01:
1619 break;
1620 case 0x02:{
1621 const DOS_DEVICE *dev;
1622 if ((dev = DOSFS_GetDeviceByHandle( FILE_GetHandle(BX_reg(context)) )) &&
1623 !strcasecmp( dev->name, "SCSIMGR$" ))
1625 ASPI_DOS_HandleInt(context);
1627 break;
1629 case 0x05:{ /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1630 /*BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);*/
1631 int drive = DOS_GET_DRIVE(BL_reg(context));
1633 FIXME("program tried to write to block device control channel of drive %d:\n",drive);
1634 /* for (i=0;i<CX_reg(context);i++)
1635 fprintf(stdnimp,"%02x ",dataptr[i]);
1636 fprintf(stdnimp,"\n");*/
1637 AX_reg(context)=CX_reg(context);
1638 break;
1640 case 0x08: /* Check if drive is removable. */
1641 TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOVABLE for drive %s\n",
1642 INT21_DriveName( BL_reg(context)));
1643 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1645 case DRIVE_CANNOTDETERMINE:
1646 SetLastError( ERROR_INVALID_DRIVE );
1647 AX_reg(context) = ERROR_INVALID_DRIVE;
1648 SET_CFLAG(context);
1649 break;
1650 case DRIVE_REMOVABLE:
1651 AX_reg(context) = 0; /* removable */
1652 break;
1653 default:
1654 AX_reg(context) = 1; /* not removable */
1655 break;
1657 break;
1659 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
1660 TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOTE for drive %s\n",
1661 INT21_DriveName( BL_reg(context)));
1662 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1664 case DRIVE_CANNOTDETERMINE:
1665 SetLastError( ERROR_INVALID_DRIVE );
1666 AX_reg(context) = ERROR_INVALID_DRIVE;
1667 SET_CFLAG(context);
1668 break;
1669 case DRIVE_REMOTE:
1670 DX_reg(context) = (1<<9) | (1<<12); /* remote */
1671 break;
1672 default:
1673 DX_reg(context) = 0; /* FIXME: use driver attr here */
1674 break;
1676 break;
1678 case 0x0a: /* check if handle (BX) is remote */
1679 TRACE("IOCTL - CHECK IF HANDLE %d IS REMOTE\n",BX_reg(context));
1680 /* returns DX, bit 15 set if remote, bit 14 set if date/time
1681 * not set on close
1683 DX_reg(context) = 0;
1684 break;
1686 case 0x0b: /* SET SHARING RETRY COUNT */
1687 TRACE("IOCTL - SET SHARING RETRY COUNT pause %d retries %d\n",
1688 CX_reg(context), DX_reg(context));
1689 if (!CX_reg(context))
1691 AX_reg(context) = 1;
1692 SET_CFLAG(context);
1693 break;
1695 DOSMEM_LOL()->sharing_retry_delay = CX_reg(context);
1696 if (!DX_reg(context))
1697 DOSMEM_LOL()->sharing_retry_count = DX_reg(context);
1698 RESET_CFLAG(context);
1699 break;
1701 case 0x0d:
1702 TRACE("IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
1703 INT21_DriveName( BL_reg(context)));
1704 bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1705 break;
1707 case 0x0e: /* get logical drive mapping */
1708 TRACE("IOCTL - GET LOGICAL DRIVE MAP for drive %s\n",
1709 INT21_DriveName( BL_reg(context)));
1710 AL_reg(context) = 0; /* drive has no mapping - FIXME: may be wrong*/
1711 break;
1713 case 0x0F: /* Set logical drive mapping */
1715 int drive;
1716 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
1717 INT21_DriveName( BL_reg(context)));
1718 drive = DOS_GET_DRIVE ( BL_reg(context) );
1719 if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
1721 SET_CFLAG(context);
1722 AX_reg(context) = 0x000F; /* invalid drive */
1724 break;
1727 case 0xe0: /* Sun PC-NFS API */
1728 /* not installed */
1729 break;
1731 case 0x52: /* DR-DOS version */
1732 /* This is not DR-DOS */
1734 TRACE("GET DR-DOS VERSION requested");
1736 AX_reg(context) = 0x0001; /* Invalid function */
1737 SET_CFLAG(context); /* Error */
1738 SetLastError( ERROR_INVALID_FUNCTION );
1739 break;
1741 default:
1742 INT_BARF( context, 0x21 );
1743 break;
1745 break;
1747 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1749 HANDLE handle;
1750 TRACE("DUP - DUPLICATE FILE HANDLE %d\n",BX_reg(context));
1751 if ((bSetDOSExtendedError = !DuplicateHandle( GetCurrentProcess(),
1752 FILE_GetHandle(BX_reg(context)),
1753 GetCurrentProcess(), &handle,
1754 0, TRUE, DUPLICATE_SAME_ACCESS )))
1755 AX_reg(context) = HFILE_ERROR16;
1756 else
1757 AX_reg(context) = FILE_AllocDosHandle(handle);
1758 break;
1761 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1762 TRACE("FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
1763 BX_reg(context),CX_reg(context));
1764 bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR16);
1765 break;
1767 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1768 TRACE("CWD - GET CURRENT DIRECTORY for drive %s\n",
1769 INT21_DriveName( DL_reg(context)));
1770 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1771 break;
1773 case 0x48: /* ALLOCATE MEMORY */
1774 TRACE("ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context));
1776 LPVOID *mem;
1777 if (ISV86(context))
1779 mem= DOSMEM_GetBlock((DWORD)BX_reg(context)<<4,NULL);
1780 if (mem)
1781 AX_reg(context) = DOSMEM_MapLinearToDos(mem)>>4;
1783 else
1785 mem = (LPVOID)GlobalDOSAlloc16(BX_reg(context)<<4);
1786 if (mem)
1787 AX_reg(context) = (DWORD)mem&0xffff;
1789 if (!mem)
1791 SET_CFLAG(context);
1792 AX_reg(context) = 0x0008; /* insufficient memory */
1793 BX_reg(context) = DOSMEM_Available()>>4;
1796 break;
1798 case 0x49: /* FREE MEMORY */
1799 TRACE("FREE MEMORY segment %04lX\n", context->SegEs);
1801 BOOL ret;
1802 if (ISV86(context))
1803 ret= DOSMEM_FreeBlock(DOSMEM_MapDosToLinear(context->SegEs<<4));
1804 else
1806 ret = !GlobalDOSFree16(context->SegEs);
1807 /* If we don't reset ES_reg, we will fail in the relay code */
1808 context->SegEs=ret;
1810 if (!ret)
1812 TRACE("FREE MEMORY failed\n");
1813 SET_CFLAG(context);
1814 AX_reg(context) = 0x0009; /* memory block address invalid */
1817 break;
1819 case 0x4a: /* RESIZE MEMORY BLOCK */
1820 TRACE("RESIZE MEMORY segment %04lX to %d paragraphs\n", context->SegEs, BX_reg(context));
1821 if (!ISV86(context))
1822 FIXME("RESIZE MEMORY probably insufficient implementation. Expect crash soon\n");
1824 LPVOID *mem = DOSMEM_ResizeBlock(DOSMEM_MapDosToLinear(context->SegEs<<4),
1825 BX_reg(context)<<4,NULL);
1826 if (mem)
1827 AX_reg(context) = DOSMEM_MapLinearToDos(mem)>>4;
1828 else {
1829 SET_CFLAG(context);
1830 AX_reg(context) = 0x0008; /* insufficient memory */
1831 BX_reg(context) = DOSMEM_Available()>>4; /* not quite right */
1834 break;
1836 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1837 TRACE("EXEC %s\n",
1838 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx ));
1839 AX_reg(context) = WinExec16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1840 context->Edx ),
1841 SW_NORMAL );
1842 if (AX_reg(context) < 32) SET_CFLAG(context);
1843 break;
1845 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1846 TRACE("EXIT with return code %d\n",AL_reg(context));
1847 ExitThread( AL_reg(context) );
1848 break;
1850 case 0x4d: /* GET RETURN CODE */
1851 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
1852 AX_reg(context) = 0; /* normal exit */
1853 break;
1855 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1856 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
1857 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1858 if (!INT21_FindFirst(context)) break;
1859 /* fall through */
1861 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1862 TRACE("FINDNEXT\n");
1863 if (!INT21_FindNext(context))
1865 SetLastError( ERROR_NO_MORE_FILES );
1866 AX_reg(context) = ERROR_NO_MORE_FILES;
1867 SET_CFLAG(context);
1869 else AX_reg(context) = 0; /* OK */
1870 break;
1871 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
1872 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
1873 INT21_SetCurrentPSP(BX_reg(context));
1874 break;
1875 case 0x51: /* GET PSP ADDRESS */
1876 TRACE("GET CURRENT PROCESS ID (GET PSP ADDRESS)\n");
1877 /* FIXME: should we return the original DOS PSP upon */
1878 /* Windows startup ? */
1879 BX_reg(context) = INT21_GetCurrentPSP();
1880 break;
1881 case 0x62: /* GET PSP ADDRESS */
1882 TRACE("GET CURRENT PSP ADDRESS\n");
1883 /* FIXME: should we return the original DOS PSP upon */
1884 /* Windows startup ? */
1885 BX_reg(context) = INT21_GetCurrentPSP();
1886 break;
1888 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1889 TRACE("SYSVARS - GET LIST OF LISTS\n");
1891 context->SegEs = ISV86(context) ? HIWORD(DOS_LOLSeg) : LOWORD(DOS_LOLSeg);
1892 BX_reg(context) = FIELD_OFFSET(DOS_LISTOFLISTS, ptr_first_DPB);
1894 break;
1896 case 0x54: /* Get Verify Flag */
1897 TRACE("Get Verify Flag - Not Supported\n");
1898 AL_reg(context) = 0x00; /* pretend we can tell. 00h = off 01h = on */
1899 break;
1901 case 0x56: /* "RENAME" - RENAME FILE */
1902 TRACE("RENAME %s to %s\n",
1903 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1904 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi));
1905 bSetDOSExtendedError =
1906 (!MoveFileA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1907 CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi)));
1908 break;
1910 case 0x57: /* FILE DATE AND TIME */
1911 switch (AL_reg(context))
1913 case 0x00: /* Get */
1915 FILETIME filetime;
1916 TRACE("GET FILE DATE AND TIME for handle %d\n",
1917 BX_reg(context));
1918 if (!GetFileTime( FILE_GetHandle(BX_reg(context)), NULL, NULL, &filetime ))
1919 bSetDOSExtendedError = TRUE;
1920 else FileTimeToDosDateTime( &filetime, &DX_reg(context),
1921 &CX_reg(context) );
1923 break;
1925 case 0x01: /* Set */
1927 FILETIME filetime;
1928 TRACE("SET FILE DATE AND TIME for handle %d\n",
1929 BX_reg(context));
1930 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
1931 &filetime );
1932 bSetDOSExtendedError =
1933 (!SetFileTime( FILE_GetHandle(BX_reg(context)),
1934 NULL, NULL, &filetime ));
1936 break;
1938 break;
1940 case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1941 TRACE("GET OR SET MEMORY/UMB ALLOCATION STRATEGY subfunction %d\n",
1942 AL_reg(context));
1943 switch (AL_reg(context))
1945 case 0x00:
1946 AX_reg(context) = 1;
1947 break;
1948 case 0x02:
1949 AX_reg(context) = 0;
1950 break;
1951 case 0x01:
1952 case 0x03:
1953 break;
1955 RESET_CFLAG(context);
1956 break;
1958 case 0x5a: /* CREATE TEMPORARY FILE */
1959 TRACE("CREATE TEMPORARY FILE\n");
1960 bSetDOSExtendedError = !INT21_CreateTempFile(context);
1961 break;
1963 case 0x5b: /* CREATE NEW FILE */
1964 TRACE("CREATE NEW FILE 0x%02x for %s\n", CX_reg(context),
1965 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1966 bSetDOSExtendedError = ((AX_reg(context) =
1967 _lcreat16_uniq( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1968 CX_reg(context) )) == (WORD)HFILE_ERROR16);
1969 break;
1971 case 0x5d: /* NETWORK */
1972 FIXME("Function 0x%04x not implemented.\n", AX_reg (context));
1973 /* Fix the following while you're at it. */
1974 SetLastError( ER_NoNetwork );
1975 bSetDOSExtendedError = TRUE;
1976 break;
1978 case 0x5e:
1979 bSetDOSExtendedError = INT21_networkfunc (context);
1980 break;
1982 case 0x5f: /* NETWORK */
1983 switch (AL_reg(context))
1985 case 0x07: /* ENABLE DRIVE */
1986 TRACE("ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1987 if (!DRIVE_Enable( DL_reg(context) ))
1989 SetLastError( ERROR_INVALID_DRIVE );
1990 bSetDOSExtendedError = TRUE;
1992 break;
1994 case 0x08: /* DISABLE DRIVE */
1995 TRACE("DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1996 if (!DRIVE_Disable( DL_reg(context) ))
1998 SetLastError( ERROR_INVALID_DRIVE );
1999 bSetDOSExtendedError = TRUE;
2001 break;
2003 default:
2004 /* network software not installed */
2005 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
2006 SetLastError( ER_NoNetwork );
2007 bSetDOSExtendedError = TRUE;
2008 break;
2010 break;
2012 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
2013 TRACE("TRUENAME %s\n",
2014 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
2016 if (!GetFullPathNameA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
2017 context->Esi), 128,
2018 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
2019 context->Edi),NULL))
2020 bSetDOSExtendedError = TRUE;
2021 else AX_reg(context) = 0;
2023 break;
2025 case 0x61: /* UNUSED */
2026 case 0x63: /* misc. language support */
2027 switch (AL_reg(context)) {
2028 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
2029 INT21_GetDBCSLeadTable(context);
2030 break;
2032 break;
2033 case 0x64: /* OS/2 DOS BOX */
2034 INT_BARF( context, 0x21 );
2035 SET_CFLAG(context);
2036 break;
2038 case 0x65:{/* GET EXTENDED COUNTRY INFORMATION */
2039 BYTE *dataptr=CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi);
2040 TRACE("GET EXTENDED COUNTRY INFORMATION code page %d country %d\n",
2041 BX_reg(context), DX_reg(context));
2042 switch (AL_reg(context)) {
2043 case 0x01:
2044 TRACE("\tget general internationalization info\n");
2045 dataptr[0] = 0x1;
2046 *(WORD*)(dataptr+1) = 41;
2047 *(WORD*)(dataptr+3) = GetSystemDefaultLangID();
2048 *(WORD*)(dataptr+5) = CodePage;
2049 *(DWORD*)(dataptr+0x19) = 0; /* FIXME: ptr to case map routine */
2050 break;
2051 case 0x06:
2052 TRACE("\tget pointer to collating sequence table\n");
2053 dataptr[0] = 0x06;
2054 *(DWORD*)(dataptr+1) = MAKELONG(DOSMEM_CollateTable & 0xFFFF,DOSMEM_AllocSelector(DOSMEM_CollateTable>>16));
2055 CX_reg(context) = 258;/*FIXME: size of table?*/
2056 break;
2057 case 0x20:
2058 TRACE("\tConvert char to uppercase\n");
2059 DL_reg(context) = toupper(DL_reg(context));
2060 break;
2061 case 0x21:
2062 TRACE("\tconvert string to uppercase with length\n");
2064 char *ptr = (char *)CTX_SEG_OFF_TO_LIN(context,context->SegDs,context->Edx);
2065 WORD len = CX_reg(context);
2066 while (len--) { *ptr = toupper(*ptr); ptr++; }
2068 break;
2069 case 0x22:
2070 TRACE("\tConvert ASCIIZ string to uppercase\n");
2071 _strupr( (LPSTR)CTX_SEG_OFF_TO_LIN(context,context->SegDs,context->Edx) );
2072 break;
2073 default:
2074 TRACE("\tunimplemented function %d\n",AL_reg(context));
2075 INT_BARF( context, 0x21 );
2076 SET_CFLAG(context);
2077 break;
2079 break;
2081 case 0x66: /* GLOBAL CODE PAGE TABLE */
2082 switch (AL_reg(context))
2084 case 0x01:
2085 TRACE("GET GLOBAL CODE PAGE TABLE\n");
2086 DX_reg(context) = BX_reg(context) = CodePage;
2087 RESET_CFLAG(context);
2088 break;
2089 case 0x02:
2090 TRACE("SET GLOBAL CODE PAGE TABLE active page %d system page %d\n",
2091 BX_reg(context),DX_reg(context));
2092 CodePage = BX_reg(context);
2093 RESET_CFLAG(context);
2094 break;
2096 break;
2098 case 0x67: /* SET HANDLE COUNT */
2099 TRACE("SET HANDLE COUNT to %d\n",BX_reg(context) );
2100 SetHandleCount16( BX_reg(context) );
2101 if (GetLastError()) bSetDOSExtendedError = TRUE;
2102 break;
2104 case 0x68: /* "FFLUSH" - COMMIT FILE */
2105 case 0x6a: /* COMMIT FILE */
2106 TRACE("FFLUSH/COMMIT handle %d\n",BX_reg(context));
2107 bSetDOSExtendedError = (!FlushFileBuffers( FILE_GetHandle(BX_reg(context)) ));
2108 break;
2110 case 0x69: /* DISK SERIAL NUMBER */
2111 switch (AL_reg(context))
2113 case 0x00:
2114 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
2115 INT21_DriveName(BL_reg(context)));
2116 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
2117 else AX_reg(context) = 0;
2118 break;
2120 case 0x01:
2121 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
2122 INT21_DriveName(BL_reg(context)));
2123 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
2124 else AX_reg(context) = 1;
2125 break;
2127 break;
2129 case 0x6C: /* Extended Open/Create*/
2130 TRACE("EXTENDED OPEN/CREATE %s\n",
2131 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edi));
2132 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
2133 break;
2135 case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
2136 if ((GetVersion()&0xC0000004)!=0xC0000004) {
2137 /* not supported on anything but Win95 */
2138 TRACE("LONG FILENAME functions supported only by win95\n");
2139 SET_CFLAG(context);
2140 AL_reg(context) = 0;
2141 } else
2142 switch(AL_reg(context))
2144 case 0x39: /* Create directory */
2145 TRACE("LONG FILENAME - MAKE DIRECTORY %s\n",
2146 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
2147 bSetDOSExtendedError = (!CreateDirectoryA(
2148 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
2149 context->Edx ), NULL));
2150 /* FIXME: CreateDirectory's LastErrors will clash with the ones
2151 * used by dos. AH=39 only returns 3 (path not found) and 5 (access
2152 * denied), while CreateDirectory return several ones. remap some of
2153 * them. -Marcus
2155 if (bSetDOSExtendedError) {
2156 switch (GetLastError()) {
2157 case ERROR_ALREADY_EXISTS:
2158 case ERROR_FILENAME_EXCED_RANGE:
2159 case ERROR_DISK_FULL:
2160 SetLastError(ERROR_ACCESS_DENIED);
2161 break;
2162 default: break;
2165 break;
2166 case 0x3a: /* Remove directory */
2167 TRACE("LONG FILENAME - REMOVE DIRECTORY %s\n",
2168 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
2169 bSetDOSExtendedError = (!RemoveDirectoryA(
2170 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
2171 context->Edx )));
2172 break;
2173 case 0x43: /* Get/Set file attributes */
2174 TRACE("LONG FILENAME -EXTENDED GET/SET FILE ATTRIBUTES %s\n",
2175 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
2176 switch (BL_reg(context))
2178 case 0x00: /* Get file attributes */
2179 TRACE("\tretrieve attributes\n");
2180 CX_reg(context) = (WORD)GetFileAttributesA(
2181 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
2182 context->Edx));
2183 if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
2184 break;
2185 case 0x01:
2186 TRACE("\tset attributes 0x%04x\n",CX_reg(context));
2187 bSetDOSExtendedError = (!SetFileAttributesA(
2188 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
2189 context->Edx),
2190 CX_reg(context) ) );
2191 break;
2192 default:
2193 FIXME("Unimplemented long file name function:\n");
2194 INT_BARF( context, 0x21 );
2195 SET_CFLAG(context);
2196 AL_reg(context) = 0;
2197 break;
2199 break;
2200 case 0x47: /* Get current directory */
2201 TRACE(" LONG FILENAME - GET CURRENT DIRECTORY for drive %s\n",
2202 INT21_DriveName(DL_reg(context)));
2203 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
2204 break;
2206 case 0x4e: /* Find first file */
2207 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
2208 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
2209 /* FIXME: use attributes in CX */
2210 if ((AX_reg(context) = FindFirstFile16(
2211 CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
2212 (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
2213 context->Edi)))
2214 == INVALID_HANDLE_VALUE16)
2215 bSetDOSExtendedError = TRUE;
2216 break;
2217 case 0x4f: /* Find next file */
2218 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
2219 BX_reg(context));
2220 if (!FindNextFile16( BX_reg(context),
2221 (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
2222 context->Edi)))
2223 bSetDOSExtendedError = TRUE;
2224 break;
2225 case 0xa0:
2227 LPCSTR driveroot = (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
2228 LPSTR buffer = (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi);
2229 int drive;
2230 UINT flags;
2232 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", driveroot);
2233 AX_reg(context) = 0;
2234 if (!driveroot)
2236 INT_BARF( context, 0x21 );
2237 SET_CFLAG(context);
2238 break;
2240 drive = toupper(driveroot[0]) - 'A';
2241 flags = DRIVE_GetFlags(toupper(driveroot[0]) - 'A');
2242 BX_reg(context) = 0x4000; /* support for LFN functions */
2243 if (flags & DRIVE_CASE_SENSITIVE)
2244 BX_reg(context) |= 1;
2245 if (flags & DRIVE_CASE_PRESERVING)
2246 BX_reg(context) |= 2;
2247 /***** FIXME: not supported yet (needed ?)
2248 if (flags & DRIVE_UNICODE)
2249 BX_reg(context) |= 4;
2250 if (flags & DRIVE_COMPRESSED)
2251 BX_reg(context) |= 0x8000;
2252 *****/
2253 CX_reg(context) = (flags & DRIVE_SHORT_NAMES) ? 11 : 255; /* FIXME: 12 ? */
2254 DX_reg(context) = MAX_PATH; /* FIXME: which len if DRIVE_SHORT_NAMES ? */
2255 if (DRIVE_GetType(drive) == TYPE_CDROM)
2256 /* valid for data _and_ audio */
2257 strcpy(buffer, "CDFS"); /* FIXME: fail if no CD in drive */
2258 else
2259 strcpy(buffer, "FAT");
2261 break;
2262 case 0xa1: /* Find close */
2263 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
2264 BX_reg(context));
2265 bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
2266 break;
2267 case 0x60:
2268 switch(CL_reg(context))
2270 case 0x01: /*Get short filename or path */
2271 if (!GetShortPathNameA
2272 ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
2273 context->Esi),
2274 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
2275 context->Edi), 67))
2276 bSetDOSExtendedError = TRUE;
2277 else AX_reg(context) = 0;
2278 break;
2279 case 0x02: /*Get canonical long filename or path */
2280 if (!GetFullPathNameA
2281 ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
2282 context->Esi), 128,
2283 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
2284 context->Edi),NULL))
2285 bSetDOSExtendedError = TRUE;
2286 else AX_reg(context) = 0;
2287 break;
2288 default:
2289 FIXME("Unimplemented long file name function:\n");
2290 INT_BARF( context, 0x21 );
2291 SET_CFLAG(context);
2292 AL_reg(context) = 0;
2293 break;
2295 break;
2296 case 0x6c: /* Create or open file */
2297 TRACE("LONG FILENAME - CREATE OR OPEN FILE %s\n",
2298 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi));
2299 /* translate Dos 7 action to Dos 6 action */
2300 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
2301 break;
2303 case 0x3b: /* Change directory */
2304 TRACE("LONG FILENAME - CHANGE DIRECTORY %s\n",
2305 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
2306 if (!SetCurrentDirectoryA(CTX_SEG_OFF_TO_LIN(context,
2307 context->SegDs,
2308 context->Edx
2311 SET_CFLAG(context);
2312 AL_reg(context) = GetLastError();
2314 break;
2315 case 0x41: /* Delete file */
2316 TRACE("LONG FILENAME - DELETE FILE %s\n",
2317 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
2318 if (!DeleteFileA(CTX_SEG_OFF_TO_LIN(context,
2319 context->SegDs,
2320 context->Edx)
2321 )) {
2322 SET_CFLAG(context);
2323 AL_reg(context) = GetLastError();
2325 break;
2326 case 0x56: /* Move (rename) file */
2327 FIXME("LONG FILENAME - RENAME FILE %s to %s stub\n",
2328 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx),
2329 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi));
2330 default:
2331 FIXME("Unimplemented long file name function:\n");
2332 INT_BARF( context, 0x21 );
2333 SET_CFLAG(context);
2334 AL_reg(context) = 0;
2335 break;
2337 break;
2339 case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
2340 case 0x72: /* MS-DOS 7 (Windows95) - ??? */
2341 TRACE("windows95 function AX %04x\n",
2342 AX_reg(context));
2343 WARN(" returning unimplemented\n");
2344 SET_CFLAG(context);
2345 AL_reg(context) = 0;
2346 break;
2348 case 0x73: /* MULTIPLEXED: Win95 OSR2/Win98 FAT32 calls */
2349 TRACE("windows95 function AX %04x\n",
2350 AX_reg(context));
2352 switch (AL_reg(context))
2354 case 0x02: /* Get Extended Drive Parameter Block for specific drive */
2355 /* ES:DI points to word with length of data (should be 0x3d) */
2357 WORD *buffer;
2358 struct EDPB *edpb;
2359 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
2360 char root[] = "A:\\";
2362 buffer = (WORD *)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
2364 TRACE("Get Extended DPB: linear buffer address is %p\n", buffer);
2366 /* validate passed-in buffer lengths */
2367 if ((*buffer != 0x3d) || (context->Ecx != 0x3f))
2369 WARN("Get Extended DPB: buffer lengths incorrect\n");
2370 WARN("CX = %lx, buffer[0] = %x\n", context->Ecx, *buffer);
2371 SET_CFLAG(context);
2372 AL_reg(context) = 0x18; /* bad buffer length */
2375 /* buffer checks out */
2376 buffer++; /* skip over length word now */
2377 if (FillInDrivePB( DX_reg(context) ) )
2379 edpb = (struct EDPB *)buffer;
2381 /* copy down the old-style DPB portion first */
2382 memcpy(&edpb->dpb, &heap->dpb, sizeof(struct DPB));
2384 /* now fill in the extended entries */
2385 edpb->edpb_flags = 0;
2386 edpb->next_edpb = 0;
2387 edpb->free_cluster = edpb->free_cluster2 = 0;
2389 /* determine free disk space */
2390 *root += DOS_GET_DRIVE( DX_reg(context) );
2391 GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
2392 &free_clusters, &total_clusters );
2394 edpb->clusters_free = (free_clusters&0xffff);
2396 edpb->clusters_free_hi = free_clusters >> 16;
2397 edpb->mirroring_flags = 0;
2398 edpb->info_sector = 0xffff;
2399 edpb->spare_boot_sector = 0xffff;
2400 edpb->first_cluster = 0;
2401 edpb->max_cluster = total_clusters;
2402 edpb->fat_clusters = 32; /* made-up value */
2403 edpb->root_cluster = 0;
2405 RESET_CFLAG(context); /* clear carry */
2406 AX_reg(context) = 0;
2408 else
2410 AX_reg(context) = 0x00ff;
2411 SET_CFLAG(context);
2414 break;
2416 case 0x03: /* Get Extended free space on drive */
2417 case 0x04: /* Set DPB for formatting */
2418 case 0x05: /* extended absolute disk read/write */
2419 FIXME("Unimplemented FAT32 int32 function %04x\n", AX_reg(context));
2420 SET_CFLAG(context);
2421 AL_reg(context) = 0;
2422 break;
2425 break;
2428 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
2429 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
2430 break;
2432 default:
2433 INT_BARF( context, 0x21 );
2434 break;
2436 } /* END OF SWITCH */
2438 if( bSetDOSExtendedError ) /* set general error condition */
2440 AX_reg(context) = GetLastError();
2441 SET_CFLAG(context);
2444 if ((context->EFlags & 0x0001))
2445 TRACE("failed, error 0x%04lx\n", GetLastError() );
2447 TRACE("returning: AX=%04x BX=%04x CX=%04x DX=%04x "
2448 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
2449 AX_reg(context), BX_reg(context), CX_reg(context),
2450 DX_reg(context), SI_reg(context), DI_reg(context),
2451 (WORD)context->SegDs, (WORD)context->SegEs,
2452 context->EFlags);
2455 /***********************************************************************
2456 * GetSetKernelDOSProc
2458 FARPROC16 WINAPI GetSetKernelDOSProc16(FARPROC16 DosProc)
2460 FIXME("(DosProc=0x%08x): stub\n", (UINT)DosProc);
2461 return NULL;