Add include for LoadLibrary.
[wine.git] / msdos / int21.c
blobcbb692f532aaaac45baa41212ed8e866548f49e8
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 "wingdi.h"
24 #include "winuser.h" /* SW_NORMAL */
25 #include "wine/winbase16.h"
26 #include "winerror.h"
27 #include "drive.h"
28 #include "file.h"
29 #include "heap.h"
30 #include "msdos.h"
31 #include "ldt.h"
32 #include "task.h"
33 #include "options.h"
34 #include "miscemu.h"
35 #include "dosexe.h" /* for the MZ_SUPPORTED define */
36 #include "module.h"
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 DS_reg(context) = 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 DS_reg(context) = 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, DS_reg(context), EDX_reg(context));
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, DS_reg(context), ESI_reg(context) );
400 char *fcb =
401 CTX_SEG_OFF_TO_LIN(context, ES_reg(context), EDI_reg(context) );
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, DS_reg(context),
470 EDX_reg(context) ), 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, DS_reg(context),EDX_reg(context)),
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 ((EFL_reg(context) & 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 (EFL_reg(context) & 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 (EFL_reg(context) & 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, DS_reg(context),EDX_reg(context));
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, DS_reg(context), EDX_reg(context));
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, DS_reg(context), EDX_reg(context) );
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, DS_reg(context), ESI_reg(context) );
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 DS_reg(context) = 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, DS_reg(context), EDX_reg(context));
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, DS_reg(context), EDX_reg(context));
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, DS_reg(context), EDX_reg(context));
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, DS_reg(context), EDX_reg(context));
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,DS_reg(context),EDX_reg(context));
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 #ifdef MZ_SUPPORTED
951 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
952 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
954 GlobalUnlock16( GetCurrentTask() );
955 if (pModule->lpDosTask)
956 pModule->lpDosTask->psp_seg = psp;
957 else
958 #endif
959 ERR("Cannot change PSP for non-DOS task!\n");
962 static WORD INT21_GetCurrentPSP()
964 #ifdef MZ_SUPPORTED
965 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
966 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
968 GlobalUnlock16( GetCurrentTask() );
969 if (pModule->lpDosTask)
970 return pModule->lpDosTask->psp_seg;
971 else
972 #endif
973 return GetCurrentPDB16();
977 /***********************************************************************
978 * INT21_GetExtendedError
980 static void INT21_GetExtendedError( CONTEXT86 *context )
982 BYTE class, action, locus;
983 WORD error = GetLastError();
985 switch(error)
987 case ERROR_SUCCESS:
988 class = action = locus = 0;
989 break;
990 case ERROR_DIR_NOT_EMPTY:
991 class = EC_Exists;
992 action = SA_Ignore;
993 locus = EL_Disk;
994 break;
995 case ERROR_ACCESS_DENIED:
996 class = EC_AccessDenied;
997 action = SA_Abort;
998 locus = EL_Disk;
999 break;
1000 case ERROR_CANNOT_MAKE:
1001 class = EC_AccessDenied;
1002 action = SA_Abort;
1003 locus = EL_Unknown;
1004 break;
1005 case ERROR_DISK_FULL:
1006 case ERROR_HANDLE_DISK_FULL:
1007 class = EC_MediaError;
1008 action = SA_Abort;
1009 locus = EL_Disk;
1010 break;
1011 case ERROR_FILE_EXISTS:
1012 case ERROR_ALREADY_EXISTS:
1013 class = EC_Exists;
1014 action = SA_Abort;
1015 locus = EL_Disk;
1016 break;
1017 case ERROR_FILE_NOT_FOUND:
1018 class = EC_NotFound;
1019 action = SA_Abort;
1020 locus = EL_Disk;
1021 break;
1022 case ER_GeneralFailure:
1023 class = EC_SystemFailure;
1024 action = SA_Abort;
1025 locus = EL_Unknown;
1026 break;
1027 case ERROR_INVALID_DRIVE:
1028 class = EC_MediaError;
1029 action = SA_Abort;
1030 locus = EL_Disk;
1031 break;
1032 case ERROR_INVALID_HANDLE:
1033 class = EC_ProgramError;
1034 action = SA_Abort;
1035 locus = EL_Disk;
1036 break;
1037 case ERROR_LOCK_VIOLATION:
1038 class = EC_AccessDenied;
1039 action = SA_Abort;
1040 locus = EL_Disk;
1041 break;
1042 case ERROR_NO_MORE_FILES:
1043 class = EC_MediaError;
1044 action = SA_Abort;
1045 locus = EL_Disk;
1046 break;
1047 case ER_NoNetwork:
1048 class = EC_NotFound;
1049 action = SA_Abort;
1050 locus = EL_Network;
1051 break;
1052 case ERROR_NOT_ENOUGH_MEMORY:
1053 class = EC_OutOfResource;
1054 action = SA_Abort;
1055 locus = EL_Memory;
1056 break;
1057 case ERROR_PATH_NOT_FOUND:
1058 class = EC_NotFound;
1059 action = SA_Abort;
1060 locus = EL_Disk;
1061 break;
1062 case ERROR_SEEK:
1063 class = EC_NotFound;
1064 action = SA_Ignore;
1065 locus = EL_Disk;
1066 break;
1067 case ERROR_SHARING_VIOLATION:
1068 class = EC_Temporary;
1069 action = SA_Retry;
1070 locus = EL_Disk;
1071 break;
1072 case ERROR_TOO_MANY_OPEN_FILES:
1073 class = EC_ProgramError;
1074 action = SA_Abort;
1075 locus = EL_Disk;
1076 break;
1077 default:
1078 FIXME("Unknown error %d\n", error );
1079 class = EC_SystemFailure;
1080 action = SA_Abort;
1081 locus = EL_Unknown;
1082 break;
1084 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
1085 error, class, action, locus );
1086 AX_reg(context) = error;
1087 BH_reg(context) = class;
1088 BL_reg(context) = action;
1089 CH_reg(context) = locus;
1092 /***********************************************************************
1093 * DOS3Call (KERNEL.102)
1095 void WINAPI DOS3Call( CONTEXT86 *context )
1097 BOOL bSetDOSExtendedError = FALSE;
1100 TRACE("AX=%04x BX=%04x CX=%04x DX=%04x "
1101 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1102 AX_reg(context), BX_reg(context), CX_reg(context), DX_reg(context),
1103 SI_reg(context), DI_reg(context),
1104 (WORD)DS_reg(context), (WORD)ES_reg(context),
1105 EFL_reg(context) );
1108 if (AH_reg(context) == 0x59) /* Get extended error info */
1110 INT21_GetExtendedError( context );
1111 return;
1114 if (AH_reg(context) == 0x0C) /* Flush buffer and read standard input */
1116 TRACE("FLUSH BUFFER AND READ STANDARD INPUT\n");
1117 /* no flush here yet */
1118 AH_reg(context) = AL_reg(context);
1121 if (AH_reg(context)>=0x2f) {
1122 /* extended error is used by (at least) functions 0x2f to 0x62 */
1123 SetLastError(0);
1125 RESET_CFLAG(context); /* Not sure if this is a good idea */
1127 switch(AH_reg(context))
1129 case 0x03: /* READ CHARACTER FROM STDAUX */
1130 case 0x04: /* WRITE CHARACTER TO STDAUX */
1131 case 0x05: /* WRITE CHARACTER TO PRINTER */
1132 case 0x0f: /* OPEN FILE USING FCB */
1133 case 0x10: /* CLOSE FILE USING FCB */
1134 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
1135 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
1136 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
1137 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
1138 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
1139 case 0x23: /* GET FILE SIZE FOR FCB */
1140 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
1141 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
1142 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
1143 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
1144 INT_BARF( context, 0x21 );
1145 break;
1147 case 0x00: /* TERMINATE PROGRAM */
1148 TRACE("TERMINATE PROGRAM\n");
1149 ExitThread( 0 );
1150 break;
1152 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
1153 TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
1154 AL_reg(context) = CONSOLE_GetCharacter();
1155 /* FIXME: no echo */
1156 break;
1158 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
1159 TRACE("Write Character to Standard Output\n");
1160 CONSOLE_Write(DL_reg(context), 0, 0, 0);
1161 break;
1163 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
1164 if (DL_reg(context) == 0xff) {
1165 static char scan = 0;
1166 TRACE("Direct Console Input\n");
1167 if (scan) {
1168 /* return pending scancode */
1169 AL_reg(context) = scan;
1170 EFL_reg(context) &= ~0x40; /* clear ZF */
1171 scan = 0;
1172 } else {
1173 char ascii;
1174 if (CONSOLE_CheckForKeystroke(&scan,&ascii)) {
1175 CONSOLE_GetKeystroke(&scan,&ascii);
1176 /* return ASCII code */
1177 AL_reg(context) = ascii;
1178 EFL_reg(context) &= ~0x40; /* clear ZF */
1179 /* return scan code on next call only if ascii==0 */
1180 if (ascii) scan = 0;
1181 } else {
1182 /* nothing pending, clear everything */
1183 AL_reg(context) = 0;
1184 EFL_reg(context) |= 0x40; /* set ZF */
1185 scan = 0; /* just in case */
1188 } else {
1189 TRACE("Direct Console Output\n");
1190 CONSOLE_Write(DL_reg(context), 0, 0, 0);
1192 break;
1194 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
1195 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
1196 AL_reg(context) = CONSOLE_GetCharacter();
1197 break;
1199 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
1200 TRACE("CHARACTER INPUT WITHOUT ECHO\n");
1201 AL_reg(context) = CONSOLE_GetCharacter();
1202 break;
1204 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
1205 TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
1206 DS_reg(context),DX_reg(context) );
1208 LPSTR data = CTX_SEG_OFF_TO_LIN(context,DS_reg(context),EDX_reg(context));
1209 LPSTR p = data;
1210 /* do NOT use strchr() to calculate the string length,
1211 as '\0' is valid string content, too !
1212 Maybe we should check for non-'$' strings, but DOS doesn't. */
1213 while (*p != '$') p++;
1214 _hwrite16( 1, data, (int)p - (int)data);
1215 AL_reg(context) = '$'; /* yes, '$' (0x24) gets returned in AL */
1217 break;
1219 case 0x0a: /* BUFFERED INPUT */
1221 char *buffer = ((char *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1222 EDX_reg(context) ));
1223 int res;
1225 TRACE("BUFFERED INPUT (size=%d)\n",buffer[0]);
1226 if (buffer[1])
1227 TRACE("Handle old chars in buffer!\n");
1228 res=_lread16( 0, buffer+2,buffer[0]);
1229 buffer[1]=res;
1230 if(buffer[res+1] == '\n')
1231 buffer[res+1] = '\r';
1232 break;
1235 case 0x0b: {/* GET STDIN STATUS */
1236 char x1,x2;
1238 if (CONSOLE_CheckForKeystroke(&x1,&x2))
1239 AL_reg(context) = 0xff;
1240 else
1241 AL_reg(context) = 0;
1242 break;
1244 case 0x2e: /* SET VERIFY FLAG */
1245 TRACE("SET VERIFY FLAG ignored\n");
1246 /* we cannot change the behaviour anyway, so just ignore it */
1247 break;
1249 case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
1250 case 0x1d:
1251 case 0x1e:
1252 case 0x20:
1253 case 0x6b: /* NULL FUNCTION */
1254 AL_reg(context) = 0;
1255 break;
1257 case 0x5c: /* "FLOCK" - RECORD LOCKING */
1258 fLock(context);
1259 break;
1261 case 0x0d: /* DISK BUFFER FLUSH */
1262 TRACE("DISK BUFFER FLUSH ignored\n");
1263 RESET_CFLAG(context); /* dos 6+ only */
1264 break;
1266 case 0x0e: /* SELECT DEFAULT DRIVE */
1267 TRACE("SELECT DEFAULT DRIVE %d\n", DL_reg(context));
1268 DRIVE_SetCurrentDrive( DL_reg(context) );
1269 AL_reg(context) = MAX_DOS_DRIVES;
1270 break;
1272 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1273 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
1274 CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1275 if (!INT21_FindFirstFCB(context))
1277 AL_reg(context) = 0xff;
1278 break;
1280 /* else fall through */
1282 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1283 AL_reg(context) = INT21_FindNextFCB(context) ? 0x00 : 0xff;
1284 break;
1286 case 0x13: /* DELETE FILE USING FCB */
1287 DeleteFileFCB(context);
1288 break;
1290 case 0x17: /* RENAME FILE USING FCB */
1291 RenameFileFCB(context);
1292 break;
1294 case 0x19: /* GET CURRENT DEFAULT DRIVE */
1295 AL_reg(context) = DRIVE_GetCurrentDrive();
1296 break;
1298 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1300 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1301 pTask->dta = PTR_SEG_OFF_TO_SEGPTR(DS_reg(context),DX_reg(context));
1302 TRACE("Set DTA: %08lx\n", pTask->dta);
1304 break;
1306 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1307 DL_reg(context) = 0;
1308 if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1309 break;
1311 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1312 if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1313 break;
1315 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1316 GetDrivePB(context, DRIVE_GetCurrentDrive());
1317 break;
1319 case 0x25: /* SET INTERRUPT VECTOR */
1320 INT_CtxSetHandler( context, AL_reg(context),
1321 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1322 DX_reg(context)));
1323 break;
1325 case 0x29: /* PARSE FILENAME INTO FCB */
1326 INT21_ParseFileNameIntoFCB(context);
1327 break;
1329 case 0x2a: /* GET SYSTEM DATE */
1330 INT21_GetSystemDate(context);
1331 break;
1333 case 0x2b: /* SET SYSTEM DATE */
1334 FIXME("SetSystemDate(%02d/%02d/%04d): not allowed\n",
1335 DL_reg(context), DH_reg(context), CX_reg(context) );
1336 AL_reg(context) = 0; /* Let's pretend we succeeded */
1337 break;
1339 case 0x2c: /* GET SYSTEM TIME */
1340 INT21_GetSystemTime(context);
1341 break;
1343 case 0x2d: /* SET SYSTEM TIME */
1344 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1345 CH_reg(context), CL_reg(context),
1346 DH_reg(context), DL_reg(context) );
1347 AL_reg(context) = 0; /* Let's pretend we succeeded */
1348 break;
1350 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1351 TRACE("GET DISK TRANSFER AREA ADDRESS\n");
1353 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1354 ES_reg(context) = SELECTOROF( pTask->dta );
1355 BX_reg(context) = OFFSETOF( pTask->dta );
1357 break;
1359 case 0x30: /* GET DOS VERSION */
1360 TRACE("GET DOS VERSION %s requested\n",
1361 (AL_reg(context) == 0x00)?"OEM number":"version flag");
1362 AX_reg(context) = (HIWORD(GetVersion16()) >> 8) |
1363 (HIWORD(GetVersion16()) << 8);
1364 #if 0
1365 AH_reg(context) = 0x7;
1366 AL_reg(context) = 0xA;
1367 #endif
1369 BX_reg(context) = 0x00FF; /* 0x123456 is Wine's serial # */
1370 CX_reg(context) = 0x0000;
1371 break;
1373 case 0x31: /* TERMINATE AND STAY RESIDENT */
1374 FIXME("TERMINATE AND STAY RESIDENT stub\n");
1375 break;
1377 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1378 TRACE("GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
1379 INT21_DriveName( DL_reg(context)));
1380 GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
1381 break;
1383 case 0x33: /* MULTIPLEXED */
1384 switch (AL_reg(context))
1386 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1387 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
1388 DL_reg(context) = DOSCONF_config.brk_flag;
1389 break;
1391 case 0x01: /* SET EXTENDED BREAK STATE */
1392 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
1393 DOSCONF_config.brk_flag = (DL_reg(context) > 0);
1394 break;
1396 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1397 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
1398 /* ugly coding in order to stay reentrant */
1399 if (DL_reg(context))
1401 DL_reg(context) = DOSCONF_config.brk_flag;
1402 DOSCONF_config.brk_flag = 1;
1404 else
1406 DL_reg(context) = DOSCONF_config.brk_flag;
1407 DOSCONF_config.brk_flag = 0;
1409 break;
1411 case 0x05: /* GET BOOT DRIVE */
1412 TRACE("GET BOOT DRIVE\n");
1413 DL_reg(context) = 3;
1414 /* c: is Wine's bootdrive (a: is 1)*/
1415 break;
1417 case 0x06: /* GET TRUE VERSION NUMBER */
1418 TRACE("GET TRUE VERSION NUMBER\n");
1419 BX_reg(context) = (HIWORD(GetVersion16() >> 8)) |
1420 (HIWORD(GetVersion16() << 8));
1421 DX_reg(context) = 0x00;
1422 break;
1424 default:
1425 INT_BARF( context, 0x21 );
1426 break;
1428 break;
1430 case 0x34: /* GET ADDRESS OF INDOS FLAG */
1431 TRACE("GET ADDRESS OF INDOS FLAG\n");
1432 if (!heap) INT21_CreateHeap();
1433 ES_reg(context) = DosHeapHandle;
1434 BX_reg(context) = (int)&heap->InDosFlag - (int)heap;
1435 break;
1437 case 0x35: /* GET INTERRUPT VECTOR */
1438 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
1440 FARPROC16 addr = INT_CtxGetHandler( context, AL_reg(context) );
1441 ES_reg(context) = SELECTOROF(addr);
1442 BX_reg(context) = OFFSETOF(addr);
1444 break;
1446 case 0x36: /* GET FREE DISK SPACE */
1447 TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
1448 INT21_DriveName( DL_reg(context)));
1449 if (!INT21_GetFreeDiskSpace(context)) AX_reg(context) = 0xffff;
1450 break;
1452 case 0x37:
1454 unsigned char switchchar='/';
1455 switch (AL_reg(context))
1457 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
1458 TRACE("SWITCHAR - GET SWITCH CHARACTER\n");
1459 AL_reg(context) = 0x00; /* success*/
1460 DL_reg(context) = switchchar;
1461 break;
1462 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
1463 TRACE("SWITCHAR - SET SWITCH CHARACTER\n");
1464 switchchar = DL_reg(context);
1465 AL_reg(context) = 0x00; /* success*/
1466 break;
1467 default: /*"AVAILDEV" - SPECIFY \DEV\ PREFIX USE*/
1468 INT_BARF( context, 0x21 );
1469 break;
1471 break;
1474 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1475 TRACE("GET COUNTRY-SPECIFIC INFORMATION for country 0x%02x\n",
1476 AL_reg(context));
1477 AX_reg(context) = 0x02; /* no country support available */
1478 SET_CFLAG(context);
1479 break;
1481 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1482 TRACE("MKDIR %s\n",
1483 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1484 bSetDOSExtendedError = (!CreateDirectory16( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1485 EDX_reg(context) ), NULL));
1486 /* FIXME: CreateDirectory's LastErrors will clash with the ones
1487 * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1488 * denied), while CreateDirectory return several ones. remap some of
1489 * them. -Marcus
1491 if (bSetDOSExtendedError) {
1492 switch (GetLastError()) {
1493 case ERROR_ALREADY_EXISTS:
1494 case ERROR_FILENAME_EXCED_RANGE:
1495 case ERROR_DISK_FULL:
1496 SetLastError(ERROR_ACCESS_DENIED);
1497 break;
1498 default: break;
1501 break;
1503 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1504 TRACE("RMDIR %s\n",
1505 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1506 bSetDOSExtendedError = (!RemoveDirectory16( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1507 EDX_reg(context) )));
1508 break;
1510 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1511 TRACE("CHDIR %s\n",
1512 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1513 bSetDOSExtendedError = !INT21_ChangeDir(context);
1514 break;
1516 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1517 TRACE("CREAT flag 0x%02x %s\n",CX_reg(context),
1518 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1519 bSetDOSExtendedError = INT21_CreateFile( context );
1520 break;
1522 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1523 TRACE("OPEN mode 0x%02x %s\n",AL_reg(context),
1524 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1525 OpenExistingFile(context);
1526 break;
1528 case 0x3e: /* "CLOSE" - CLOSE FILE */
1529 TRACE("CLOSE handle %d\n",BX_reg(context));
1530 bSetDOSExtendedError = ((AX_reg(context) = _lclose16( BX_reg(context) )) != 0);
1531 break;
1533 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1534 TRACE("READ from %d to %04lX:%04X for %d byte\n",BX_reg(context),
1535 DS_reg(context),DX_reg(context),CX_reg(context) );
1537 LONG result;
1538 if (ISV86(context))
1539 result = _hread16( BX_reg(context),
1540 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1541 EDX_reg(context) ),
1542 CX_reg(context) );
1543 else
1544 result = WIN16_hread( BX_reg(context),
1545 PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1546 EDX_reg(context) ),
1547 CX_reg(context) );
1548 if (result == -1) bSetDOSExtendedError = TRUE;
1549 else AX_reg(context) = (WORD)result;
1551 break;
1553 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1554 TRACE("WRITE from %04lX:%04X to handle %d for %d byte\n",
1555 DS_reg(context),DX_reg(context),BX_reg(context),CX_reg(context) );
1557 LONG result = _hwrite16( BX_reg(context),
1558 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1559 EDX_reg(context) ),
1560 CX_reg(context) );
1561 if (result == -1) bSetDOSExtendedError = TRUE;
1562 else AX_reg(context) = (WORD)result;
1564 break;
1566 case 0x41: /* "UNLINK" - DELETE FILE */
1567 TRACE("UNLINK %s\n",
1568 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1569 bSetDOSExtendedError = (!DeleteFileA( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1570 EDX_reg(context) )));
1571 break;
1573 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1574 TRACE("LSEEK handle %d offset %ld from %s\n",
1575 BX_reg(context), MAKELONG(DX_reg(context),CX_reg(context)),
1576 (AL_reg(context)==0)?"start of file":(AL_reg(context)==1)?
1577 "current file position":"end of file");
1579 LONG status = _llseek16( BX_reg(context),
1580 MAKELONG(DX_reg(context),CX_reg(context)),
1581 AL_reg(context) );
1582 if (status == -1) bSetDOSExtendedError = TRUE;
1583 else
1585 AX_reg(context) = LOWORD(status);
1586 DX_reg(context) = HIWORD(status);
1589 break;
1591 case 0x43: /* FILE ATTRIBUTES */
1592 switch (AL_reg(context))
1594 case 0x00:
1595 TRACE("GET FILE ATTRIBUTES for %s\n",
1596 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1597 AX_reg(context) = (WORD)GetFileAttributesA(
1598 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1599 EDX_reg(context)));
1600 if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1601 else CX_reg(context) = AX_reg(context);
1602 break;
1604 case 0x01:
1605 TRACE("SET FILE ATTRIBUTES 0x%02x for %s\n", CX_reg(context),
1606 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1607 bSetDOSExtendedError =
1608 (!SetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1609 EDX_reg(context)),
1610 CX_reg(context) ));
1611 break;
1612 case 0x02:
1613 FIXME("GET COMPRESSED FILE SIZE for %s stub\n",
1614 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1616 break;
1618 case 0x44: /* IOCTL */
1619 switch (AL_reg(context))
1621 case 0x00:
1622 ioctlGetDeviceInfo(context);
1623 break;
1625 case 0x01:
1626 break;
1627 case 0x02:{
1628 const DOS_DEVICE *dev;
1629 if ((dev = DOSFS_GetDeviceByHandle( FILE_GetHandle(BX_reg(context)) )) &&
1630 !strcasecmp( dev->name, "SCSIMGR$" ))
1632 ASPI_DOS_HandleInt(context);
1634 break;
1636 case 0x05:{ /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1637 /*BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context));*/
1638 int drive = DOS_GET_DRIVE(BL_reg(context));
1640 FIXME("program tried to write to block device control channel of drive %d:\n",drive);
1641 /* for (i=0;i<CX_reg(context);i++)
1642 fprintf(stdnimp,"%02x ",dataptr[i]);
1643 fprintf(stdnimp,"\n");*/
1644 AX_reg(context)=CX_reg(context);
1645 break;
1647 case 0x08: /* Check if drive is removable. */
1648 TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOVABLE for drive %s\n",
1649 INT21_DriveName( BL_reg(context)));
1650 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1652 case DRIVE_CANNOTDETERMINE:
1653 SetLastError( ERROR_INVALID_DRIVE );
1654 AX_reg(context) = ERROR_INVALID_DRIVE;
1655 SET_CFLAG(context);
1656 break;
1657 case DRIVE_REMOVABLE:
1658 AX_reg(context) = 0; /* removable */
1659 break;
1660 default:
1661 AX_reg(context) = 1; /* not removable */
1662 break;
1664 break;
1666 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
1667 TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOTE for drive %s\n",
1668 INT21_DriveName( BL_reg(context)));
1669 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1671 case DRIVE_CANNOTDETERMINE:
1672 SetLastError( ERROR_INVALID_DRIVE );
1673 AX_reg(context) = ERROR_INVALID_DRIVE;
1674 SET_CFLAG(context);
1675 break;
1676 case DRIVE_REMOTE:
1677 DX_reg(context) = (1<<9) | (1<<12); /* remote */
1678 break;
1679 default:
1680 DX_reg(context) = 0; /* FIXME: use driver attr here */
1681 break;
1683 break;
1685 case 0x0a: /* check if handle (BX) is remote */
1686 TRACE("IOCTL - CHECK IF HANDLE %d IS REMOTE\n",BX_reg(context));
1687 /* returns DX, bit 15 set if remote, bit 14 set if date/time
1688 * not set on close
1690 DX_reg(context) = 0;
1691 break;
1693 case 0x0b: /* SET SHARING RETRY COUNT */
1694 TRACE("IOCTL - SET SHARING RETRY COUNT pause %d retries %d\n",
1695 CX_reg(context), DX_reg(context));
1696 if (!CX_reg(context))
1698 AX_reg(context) = 1;
1699 SET_CFLAG(context);
1700 break;
1702 DOSMEM_LOL()->sharing_retry_delay = CX_reg(context);
1703 if (!DX_reg(context))
1704 DOSMEM_LOL()->sharing_retry_count = DX_reg(context);
1705 RESET_CFLAG(context);
1706 break;
1708 case 0x0d:
1709 TRACE("IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
1710 INT21_DriveName( BL_reg(context)));
1711 bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1712 break;
1714 case 0x0e: /* get logical drive mapping */
1715 TRACE("IOCTL - GET LOGICAL DRIVE MAP for drive %s\n",
1716 INT21_DriveName( BL_reg(context)));
1717 AL_reg(context) = 0; /* drive has no mapping - FIXME: may be wrong*/
1718 break;
1720 case 0x0F: /* Set logical drive mapping */
1722 int drive;
1723 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
1724 INT21_DriveName( BL_reg(context)));
1725 drive = DOS_GET_DRIVE ( BL_reg(context) );
1726 if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
1728 SET_CFLAG(context);
1729 AX_reg(context) = 0x000F; /* invalid drive */
1731 break;
1734 case 0xe0: /* Sun PC-NFS API */
1735 /* not installed */
1736 break;
1738 default:
1739 INT_BARF( context, 0x21 );
1740 break;
1742 break;
1744 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1746 HANDLE handle;
1747 TRACE("DUP - DUPLICATE FILE HANDLE %d\n",BX_reg(context));
1748 if ((bSetDOSExtendedError = !DuplicateHandle( GetCurrentProcess(),
1749 FILE_GetHandle(BX_reg(context)),
1750 GetCurrentProcess(), &handle,
1751 0, TRUE, DUPLICATE_SAME_ACCESS )))
1752 AX_reg(context) = HFILE_ERROR16;
1753 else
1754 AX_reg(context) = FILE_AllocDosHandle(handle);
1755 break;
1758 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1759 TRACE("FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
1760 BX_reg(context),CX_reg(context));
1761 bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR16);
1762 break;
1764 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1765 TRACE("CWD - GET CURRENT DIRECTORY for drive %s\n",
1766 INT21_DriveName( DL_reg(context)));
1767 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1768 break;
1770 case 0x48: /* ALLOCATE MEMORY */
1771 TRACE("ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context));
1773 LPVOID *mem;
1774 if (ISV86(context))
1776 mem= DOSMEM_GetBlock(0,(DWORD)BX_reg(context)<<4,NULL);
1777 if (mem)
1778 AX_reg(context) = DOSMEM_MapLinearToDos(mem)>>4;
1780 else
1782 mem = (LPVOID)GlobalDOSAlloc16(BX_reg(context)<<4);
1783 if (mem)
1784 AX_reg(context) = (DWORD)mem&0xffff;
1786 if (!mem)
1788 SET_CFLAG(context);
1789 AX_reg(context) = 0x0008; /* insufficient memory */
1790 BX_reg(context) = DOSMEM_Available(0)>>4;
1793 break;
1795 case 0x49: /* FREE MEMORY */
1796 TRACE("FREE MEMORY segment %04lX\n", ES_reg(context));
1798 BOOL ret;
1799 if (ISV86(context))
1800 ret= DOSMEM_FreeBlock(0,DOSMEM_MapDosToLinear(ES_reg(context)<<4));
1801 else
1803 ret = !GlobalDOSFree16(ES_reg(context));
1804 /* If we don't reset ES_reg, we will fail in the relay code */
1805 ES_reg(context)=ret;
1807 if (!ret)
1809 TRACE("FREE MEMORY failed\n");
1810 SET_CFLAG(context);
1811 AX_reg(context) = 0x0009; /* memory block address invalid */
1814 break;
1816 case 0x4a: /* RESIZE MEMORY BLOCK */
1817 TRACE("RESIZE MEMORY segment %04lX to %d paragraphs\n", ES_reg(context), BX_reg(context));
1818 if (!ISV86(context))
1819 FIXME("RESIZE MEMORY probably insufficient implementation. Expect crash soon\n");
1821 LPVOID *mem = DOSMEM_ResizeBlock(0,DOSMEM_MapDosToLinear(ES_reg(context)<<4),
1822 BX_reg(context)<<4,NULL);
1823 if (mem)
1824 AX_reg(context) = DOSMEM_MapLinearToDos(mem)>>4;
1825 else {
1826 SET_CFLAG(context);
1827 AX_reg(context) = 0x0008; /* insufficient memory */
1828 BX_reg(context) = DOSMEM_Available(0)>>4; /* not quite right */
1831 break;
1833 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1834 TRACE("EXEC %s\n",
1835 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context) ));
1836 AX_reg(context) = WinExec16( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1837 EDX_reg(context) ),
1838 SW_NORMAL );
1839 if (AX_reg(context) < 32) SET_CFLAG(context);
1840 break;
1842 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1843 TRACE("EXIT with return code %d\n",AL_reg(context));
1844 ExitThread( AL_reg(context) );
1845 break;
1847 case 0x4d: /* GET RETURN CODE */
1848 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
1849 AX_reg(context) = 0; /* normal exit */
1850 break;
1852 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1853 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
1854 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1855 if (!INT21_FindFirst(context)) break;
1856 /* fall through */
1858 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1859 TRACE("FINDNEXT\n");
1860 if (!INT21_FindNext(context))
1862 SetLastError( ERROR_NO_MORE_FILES );
1863 AX_reg(context) = ERROR_NO_MORE_FILES;
1864 SET_CFLAG(context);
1866 else AX_reg(context) = 0; /* OK */
1867 break;
1868 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
1869 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
1870 INT21_SetCurrentPSP(BX_reg(context));
1871 break;
1872 case 0x51: /* GET PSP ADDRESS */
1873 TRACE("GET CURRENT PROCESS ID (GET PSP ADDRESS)\n");
1874 /* FIXME: should we return the original DOS PSP upon */
1875 /* Windows startup ? */
1876 BX_reg(context) = INT21_GetCurrentPSP();
1877 break;
1878 case 0x62: /* GET PSP ADDRESS */
1879 TRACE("GET CURRENT PSP ADDRESS\n");
1880 /* FIXME: should we return the original DOS PSP upon */
1881 /* Windows startup ? */
1882 BX_reg(context) = INT21_GetCurrentPSP();
1883 break;
1885 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1886 TRACE("SYSVARS - GET LIST OF LISTS\n");
1888 ES_reg(context) = ISV86(context) ? HIWORD(DOS_LOLSeg) : LOWORD(DOS_LOLSeg);
1889 BX_reg(context) = FIELD_OFFSET(DOS_LISTOFLISTS, ptr_first_DPB);
1891 break;
1893 case 0x54: /* Get Verify Flag */
1894 TRACE("Get Verify Flag - Not Supported\n");
1895 AL_reg(context) = 0x00; /* pretend we can tell. 00h = off 01h = on */
1896 break;
1898 case 0x56: /* "RENAME" - RENAME FILE */
1899 TRACE("RENAME %s to %s\n",
1900 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
1901 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, ES_reg(context),EDI_reg(context)));
1902 bSetDOSExtendedError =
1903 (!MoveFileA( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
1904 CTX_SEG_OFF_TO_LIN(context, ES_reg(context),EDI_reg(context))));
1905 break;
1907 case 0x57: /* FILE DATE AND TIME */
1908 switch (AL_reg(context))
1910 case 0x00: /* Get */
1912 FILETIME filetime;
1913 TRACE("GET FILE DATE AND TIME for handle %d\n",
1914 BX_reg(context));
1915 if (!GetFileTime( FILE_GetHandle(BX_reg(context)), NULL, NULL, &filetime ))
1916 bSetDOSExtendedError = TRUE;
1917 else FileTimeToDosDateTime( &filetime, &DX_reg(context),
1918 &CX_reg(context) );
1920 break;
1922 case 0x01: /* Set */
1924 FILETIME filetime;
1925 TRACE("SET FILE DATE AND TIME for handle %d\n",
1926 BX_reg(context));
1927 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
1928 &filetime );
1929 bSetDOSExtendedError =
1930 (!SetFileTime( FILE_GetHandle(BX_reg(context)),
1931 NULL, NULL, &filetime ));
1933 break;
1935 break;
1937 case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1938 TRACE("GET OR SET MEMORY/UMB ALLOCATION STRATEGY subfunction %d\n",
1939 AL_reg(context));
1940 switch (AL_reg(context))
1942 case 0x00:
1943 AX_reg(context) = 1;
1944 break;
1945 case 0x02:
1946 AX_reg(context) = 0;
1947 break;
1948 case 0x01:
1949 case 0x03:
1950 break;
1952 RESET_CFLAG(context);
1953 break;
1955 case 0x5a: /* CREATE TEMPORARY FILE */
1956 TRACE("CREATE TEMPORARY FILE\n");
1957 bSetDOSExtendedError = !INT21_CreateTempFile(context);
1958 break;
1960 case 0x5b: /* CREATE NEW FILE */
1961 TRACE("CREATE NEW FILE 0x%02x for %s\n", CX_reg(context),
1962 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1963 bSetDOSExtendedError = ((AX_reg(context) =
1964 _lcreat16_uniq( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
1965 CX_reg(context) )) == (WORD)HFILE_ERROR16);
1966 break;
1968 case 0x5d: /* NETWORK */
1969 FIXME("Function 0x%04x not implemented.\n", AX_reg (context));
1970 /* Fix the following while you're at it. */
1971 SetLastError( ER_NoNetwork );
1972 bSetDOSExtendedError = TRUE;
1973 break;
1975 case 0x5e:
1976 bSetDOSExtendedError = INT21_networkfunc (context);
1977 break;
1979 case 0x5f: /* NETWORK */
1980 switch (AL_reg(context))
1982 case 0x07: /* ENABLE DRIVE */
1983 TRACE("ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1984 if (!DRIVE_Enable( DL_reg(context) ))
1986 SetLastError( ERROR_INVALID_DRIVE );
1987 bSetDOSExtendedError = TRUE;
1989 break;
1991 case 0x08: /* DISABLE DRIVE */
1992 TRACE("DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1993 if (!DRIVE_Disable( DL_reg(context) ))
1995 SetLastError( ERROR_INVALID_DRIVE );
1996 bSetDOSExtendedError = TRUE;
1998 break;
2000 default:
2001 /* network software not installed */
2002 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
2003 SetLastError( ER_NoNetwork );
2004 bSetDOSExtendedError = TRUE;
2005 break;
2007 break;
2009 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
2010 TRACE("TRUENAME %s\n",
2011 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),ESI_reg(context)));
2013 if (!GetFullPathNameA( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2014 ESI_reg(context)), 128,
2015 CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2016 EDI_reg(context)),NULL))
2017 bSetDOSExtendedError = TRUE;
2018 else AX_reg(context) = 0;
2020 break;
2022 case 0x61: /* UNUSED */
2023 case 0x63: /* misc. language support */
2024 switch (AL_reg(context)) {
2025 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
2026 INT21_GetDBCSLeadTable(context);
2027 break;
2029 break;
2030 case 0x64: /* OS/2 DOS BOX */
2031 INT_BARF( context, 0x21 );
2032 SET_CFLAG(context);
2033 break;
2035 case 0x65:{/* GET EXTENDED COUNTRY INFORMATION */
2036 extern WORD WINE_LanguageId;
2037 BYTE *dataptr=CTX_SEG_OFF_TO_LIN(context, ES_reg(context),EDI_reg(context));
2038 TRACE("GET EXTENDED COUNTRY INFORMATION code page %d country %d\n",
2039 BX_reg(context), DX_reg(context));
2040 switch (AL_reg(context)) {
2041 case 0x01:
2042 TRACE("\tget general internationalization info\n");
2043 dataptr[0] = 0x1;
2044 *(WORD*)(dataptr+1) = 41;
2045 *(WORD*)(dataptr+3) = WINE_LanguageId;
2046 *(WORD*)(dataptr+5) = CodePage;
2047 *(DWORD*)(dataptr+0x19) = 0; /* FIXME: ptr to case map routine */
2048 break;
2049 case 0x06:
2050 TRACE("\tget pointer to collating sequence table\n");
2051 dataptr[0] = 0x06;
2052 *(DWORD*)(dataptr+1) = MAKELONG(DOSMEM_CollateTable & 0xFFFF,DOSMEM_AllocSelector(DOSMEM_CollateTable>>16));
2053 CX_reg(context) = 258;/*FIXME: size of table?*/
2054 break;
2055 case 0x20:
2056 TRACE("\tConvert char to uppercase\n");
2057 DL_reg(context) = toupper(DL_reg(context));
2058 break;
2059 case 0x21:
2060 TRACE("\tconvert string to uppercase with length\n");
2061 CharUpperBuffA( (LPSTR)CTX_SEG_OFF_TO_LIN(context,DS_reg(context),EDX_reg(context)),
2062 CX_reg(context) );
2063 break;
2064 case 0x22:
2065 TRACE("\tConvert ASCIIZ string to uppercase\n");
2066 CharUpperA( (LPSTR)CTX_SEG_OFF_TO_LIN(context,DS_reg(context),EDX_reg(context)) );
2067 break;
2068 default:
2069 TRACE("\tunimplemented function %d\n",AL_reg(context));
2070 INT_BARF( context, 0x21 );
2071 SET_CFLAG(context);
2072 break;
2074 break;
2076 case 0x66: /* GLOBAL CODE PAGE TABLE */
2077 switch (AL_reg(context))
2079 case 0x01:
2080 TRACE("GET GLOBAL CODE PAGE TABLE\n");
2081 DX_reg(context) = BX_reg(context) = CodePage;
2082 RESET_CFLAG(context);
2083 break;
2084 case 0x02:
2085 TRACE("SET GLOBAL CODE PAGE TABLE active page %d system page %d\n",
2086 BX_reg(context),DX_reg(context));
2087 CodePage = BX_reg(context);
2088 RESET_CFLAG(context);
2089 break;
2091 break;
2093 case 0x67: /* SET HANDLE COUNT */
2094 TRACE("SET HANDLE COUNT to %d\n",BX_reg(context) );
2095 SetHandleCount16( BX_reg(context) );
2096 if (GetLastError()) bSetDOSExtendedError = TRUE;
2097 break;
2099 case 0x68: /* "FFLUSH" - COMMIT FILE */
2100 case 0x6a: /* COMMIT FILE */
2101 TRACE("FFLUSH/COMMIT handle %d\n",BX_reg(context));
2102 bSetDOSExtendedError = (!FlushFileBuffers( FILE_GetHandle(BX_reg(context)) ));
2103 break;
2105 case 0x69: /* DISK SERIAL NUMBER */
2106 switch (AL_reg(context))
2108 case 0x00:
2109 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
2110 INT21_DriveName(BL_reg(context)));
2111 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
2112 else AX_reg(context) = 0;
2113 break;
2115 case 0x01:
2116 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
2117 INT21_DriveName(BL_reg(context)));
2118 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
2119 else AX_reg(context) = 1;
2120 break;
2122 break;
2124 case 0x6C: /* Extended Open/Create*/
2125 TRACE("EXTENDED OPEN/CREATE %s\n",
2126 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDI_reg(context)));
2127 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
2128 break;
2130 case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
2131 if ((GetVersion()&0xC0000004)!=0xC0000004) {
2132 /* not supported on anything but Win95 */
2133 TRACE("LONG FILENAME functions supported only by win95\n");
2134 SET_CFLAG(context);
2135 AL_reg(context) = 0;
2136 } else
2137 switch(AL_reg(context))
2139 case 0x39: /* Create directory */
2140 TRACE("LONG FILENAME - MAKE DIRECTORY %s\n",
2141 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)));
2142 bSetDOSExtendedError = (!CreateDirectoryA(
2143 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2144 EDX_reg(context) ), NULL));
2145 /* FIXME: CreateDirectory's LastErrors will clash with the ones
2146 * used by dos. AH=39 only returns 3 (path not found) and 5 (access
2147 * denied), while CreateDirectory return several ones. remap some of
2148 * them. -Marcus
2150 if (bSetDOSExtendedError) {
2151 switch (GetLastError()) {
2152 case ERROR_ALREADY_EXISTS:
2153 case ERROR_FILENAME_EXCED_RANGE:
2154 case ERROR_DISK_FULL:
2155 SetLastError(ERROR_ACCESS_DENIED);
2156 break;
2157 default: break;
2160 break;
2161 case 0x3a: /* Remove directory */
2162 TRACE("LONG FILENAME - REMOVE DIRECTORY %s\n",
2163 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)));
2164 bSetDOSExtendedError = (!RemoveDirectoryA(
2165 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2166 EDX_reg(context) )));
2167 break;
2168 case 0x43: /* Get/Set file attributes */
2169 TRACE("LONG FILENAME -EXTENDED GET/SET FILE ATTRIBUTES %s\n",
2170 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)));
2171 switch (BL_reg(context))
2173 case 0x00: /* Get file attributes */
2174 TRACE("\tretrieve attributes\n");
2175 CX_reg(context) = (WORD)GetFileAttributesA(
2176 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2177 EDX_reg(context)));
2178 if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
2179 break;
2180 case 0x01:
2181 TRACE("\tset attributes 0x%04x\n",CX_reg(context));
2182 bSetDOSExtendedError = (!SetFileAttributesA(
2183 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2184 EDX_reg(context)),
2185 CX_reg(context) ) );
2186 break;
2187 default:
2188 FIXME("Unimplemented long file name function:\n");
2189 INT_BARF( context, 0x21 );
2190 SET_CFLAG(context);
2191 AL_reg(context) = 0;
2192 break;
2194 break;
2195 case 0x47: /* Get current directory */
2196 TRACE(" LONG FILENAME - GET CURRENT DIRECTORY for drive %s\n",
2197 INT21_DriveName(DL_reg(context)));
2198 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
2199 break;
2201 case 0x4e: /* Find first file */
2202 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
2203 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)));
2204 /* FIXME: use attributes in CX */
2205 if ((AX_reg(context) = FindFirstFile16(
2206 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
2207 (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2208 EDI_reg(context))))
2209 == INVALID_HANDLE_VALUE16)
2210 bSetDOSExtendedError = TRUE;
2211 break;
2212 case 0x4f: /* Find next file */
2213 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
2214 BX_reg(context));
2215 if (!FindNextFile16( BX_reg(context),
2216 (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2217 EDI_reg(context))))
2218 bSetDOSExtendedError = TRUE;
2219 break;
2220 case 0xa0:
2222 LPCSTR driveroot = (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context));
2223 LPSTR buffer = (LPSTR)CTX_SEG_OFF_TO_LIN(context, ES_reg(context),EDI_reg(context));
2224 int drive;
2225 UINT flags;
2227 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", driveroot);
2228 AX_reg(context) = 0;
2229 if (!driveroot)
2231 INT_BARF( context, 0x21 );
2232 SET_CFLAG(context);
2233 break;
2235 drive = toupper(driveroot[0]) - 'A';
2236 flags = DRIVE_GetFlags(toupper(driveroot[0]) - 'A');
2237 BX_reg(context) = 0x4000; /* support for LFN functions */
2238 if (flags & DRIVE_CASE_SENSITIVE)
2239 BX_reg(context) |= 1;
2240 if (flags & DRIVE_CASE_PRESERVING)
2241 BX_reg(context) |= 2;
2242 /***** FIXME: not supported yet (needed ?)
2243 if (flags & DRIVE_UNICODE)
2244 BX_reg(context) |= 4;
2245 if (flags & DRIVE_COMPRESSED)
2246 BX_reg(context) |= 0x8000;
2247 *****/
2248 CX_reg(context) = (flags & DRIVE_SHORT_NAMES) ? 11 : 255; /* FIXME: 12 ? */
2249 DX_reg(context) = MAX_PATH; /* FIXME: which len if DRIVE_SHORT_NAMES ? */
2250 if (DRIVE_GetType(drive) == TYPE_CDROM)
2251 /* valid for data _and_ audio */
2252 strcpy(buffer, "CDFS"); /* FIXME: fail if no CD in drive */
2253 else
2254 strcpy(buffer, "FAT");
2256 break;
2257 case 0xa1: /* Find close */
2258 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
2259 BX_reg(context));
2260 bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
2261 break;
2262 case 0x60:
2263 switch(CL_reg(context))
2265 case 0x01: /*Get short filename or path */
2266 if (!GetShortPathNameA
2267 ( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2268 ESI_reg(context)),
2269 CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2270 EDI_reg(context)), 67))
2271 bSetDOSExtendedError = TRUE;
2272 else AX_reg(context) = 0;
2273 break;
2274 case 0x02: /*Get canonical long filename or path */
2275 if (!GetFullPathNameA
2276 ( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2277 ESI_reg(context)), 128,
2278 CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2279 EDI_reg(context)),NULL))
2280 bSetDOSExtendedError = TRUE;
2281 else AX_reg(context) = 0;
2282 break;
2283 default:
2284 FIXME("Unimplemented long file name function:\n");
2285 INT_BARF( context, 0x21 );
2286 SET_CFLAG(context);
2287 AL_reg(context) = 0;
2288 break;
2290 break;
2291 case 0x6c: /* Create or open file */
2292 TRACE("LONG FILENAME - CREATE OR OPEN FILE %s\n",
2293 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), ESI_reg(context)));
2294 /* translate Dos 7 action to Dos 6 action */
2295 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
2296 break;
2298 case 0x3b: /* Change directory */
2299 TRACE("LONG FILENAME - CHANGE DIRECTORY %s\n",
2300 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
2301 if (!SetCurrentDirectoryA(CTX_SEG_OFF_TO_LIN(context,
2302 DS_reg(context),
2303 EDX_reg(context)
2306 SET_CFLAG(context);
2307 AL_reg(context) = GetLastError();
2309 break;
2310 case 0x41: /* Delete file */
2311 TRACE("LONG FILENAME - DELETE FILE %s\n",
2312 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
2313 if (!DeleteFileA(CTX_SEG_OFF_TO_LIN(context,
2314 DS_reg(context),
2315 EDX_reg(context))
2316 )) {
2317 SET_CFLAG(context);
2318 AL_reg(context) = GetLastError();
2320 break;
2321 case 0x56: /* Move (rename) file */
2322 FIXME("LONG FILENAME - RENAME FILE %s to %s stub\n",
2323 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)),
2324 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, ES_reg(context), EDI_reg(context)));
2325 default:
2326 FIXME("Unimplemented long file name function:\n");
2327 INT_BARF( context, 0x21 );
2328 SET_CFLAG(context);
2329 AL_reg(context) = 0;
2330 break;
2332 break;
2334 case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
2335 case 0x72: /* MS-DOS 7 (Windows95) - ??? */
2336 TRACE("windows95 function AX %04x\n",
2337 AX_reg(context));
2338 WARN(" returning unimplemented\n");
2339 SET_CFLAG(context);
2340 AL_reg(context) = 0;
2341 break;
2343 case 0x73: /* MULTIPLEXED: Win95 OSR2/Win98 FAT32 calls */
2344 TRACE("windows95 function AX %04x\n",
2345 AX_reg(context));
2347 switch (AL_reg(context))
2349 case 0x02: /* Get Extended Drive Parameter Block for specific drive */
2350 /* ES:DI points to word with length of data (should be 0x3d) */
2352 WORD *buffer;
2353 struct EDPB *edpb;
2354 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
2355 char root[] = "A:\\";
2357 buffer = (WORD *)CTX_SEG_OFF_TO_LIN(context, ES_reg(context), EDI_reg(context));
2359 TRACE("Get Extended DPB: linear buffer address is %p\n", buffer);
2361 /* validate passed-in buffer lengths */
2362 if ((*buffer != 0x3d) || (ECX_reg(context) != 0x3f))
2364 WARN("Get Extended DPB: buffer lengths incorrect\n");
2365 WARN("CX = %lx, buffer[0] = %x\n", ECX_reg(context), *buffer);
2366 SET_CFLAG(context);
2367 AL_reg(context) = 0x18; /* bad buffer length */
2370 /* buffer checks out */
2371 buffer++; /* skip over length word now */
2372 if (FillInDrivePB( DX_reg(context) ) )
2374 edpb = (struct EDPB *)buffer;
2376 /* copy down the old-style DPB portion first */
2377 memcpy(&edpb->dpb, &heap->dpb, sizeof(struct DPB));
2379 /* now fill in the extended entries */
2380 edpb->edpb_flags = 0;
2381 edpb->next_edpb = 0;
2382 edpb->free_cluster = edpb->free_cluster2 = 0;
2384 /* determine free disk space */
2385 *root += DOS_GET_DRIVE( DX_reg(context) );
2386 GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
2387 &free_clusters, &total_clusters );
2389 edpb->clusters_free = (free_clusters&0xffff);
2391 edpb->clusters_free_hi = free_clusters >> 16;
2392 edpb->mirroring_flags = 0;
2393 edpb->info_sector = 0xffff;
2394 edpb->spare_boot_sector = 0xffff;
2395 edpb->first_cluster = 0;
2396 edpb->max_cluster = total_clusters;
2397 edpb->fat_clusters = 32; /* made-up value */
2398 edpb->root_cluster = 0;
2400 RESET_CFLAG(context); /* clear carry */
2401 AX_reg(context) = 0;
2403 else
2405 AX_reg(context) = 0x00ff;
2406 SET_CFLAG(context);
2409 break;
2411 case 0x03: /* Get Extended free space on drive */
2412 case 0x04: /* Set DPB for formatting */
2413 case 0x05: /* extended absolute disk read/write */
2414 FIXME("Unimplemented FAT32 int32 function %04x\n", AX_reg(context));
2415 SET_CFLAG(context);
2416 AL_reg(context) = 0;
2417 break;
2420 break;
2423 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
2424 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
2425 break;
2427 default:
2428 INT_BARF( context, 0x21 );
2429 break;
2431 } /* END OF SWITCH */
2433 if( bSetDOSExtendedError ) /* set general error condition */
2435 AX_reg(context) = GetLastError();
2436 SET_CFLAG(context);
2439 if ((EFL_reg(context) & 0x0001))
2440 TRACE("failed, error 0x%04lx\n", GetLastError() );
2442 TRACE("returning: AX=%04x BX=%04x CX=%04x DX=%04x "
2443 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
2444 AX_reg(context), BX_reg(context), CX_reg(context),
2445 DX_reg(context), SI_reg(context), DI_reg(context),
2446 (WORD)DS_reg(context), (WORD)ES_reg(context),
2447 EFL_reg(context));
2450 /***********************************************************************
2451 * GetSetKernelDOSProc
2453 FARPROC16 WINAPI GetSetKernelDOSProc16(FARPROC16 DosProc)
2455 FIXME("(DosProc=0x%08x): stub\n", (UINT)DosProc);
2456 return NULL;