2 * DOS interrupt 21h handler
4 * Copyright 1993, 1994 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1997 Andreas Mohr
7 * Copyright 1998 Uwe Bonnes
8 * Copyright 1998, 1999 Ove Kaaven
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/port.h"
33 #ifdef HAVE_SYS_FILE_H
34 # include <sys/file.h>
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
40 #include <sys/types.h>
52 #include "winuser.h" /* SW_NORMAL */
53 #include "wine/winbase16.h"
61 #include "wine/unicode.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(int21);
65 #if defined(__svr4__) || defined(_SCO_DS)
66 /* SVR4 DOESNT do locking the same way must implement properly */
73 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
75 /* Define the drive parameter block, as used by int21/1F
76 * and int21/32. This table can be accessed through the
77 * global 'dpb' pointer, which points into the local dos
82 BYTE drive_num; /* 0=A, etc. */
83 BYTE unit_num; /* Drive's unit number (?) */
84 WORD sector_size; /* Sector size in bytes */
85 BYTE high_sector; /* Highest sector in a cluster */
86 BYTE shift; /* Shift count (?) */
87 WORD reserved; /* Number of reserved sectors at start */
88 BYTE num_FAT; /* Number of FATs */
89 WORD dir_entries; /* Number of root dir entries */
90 WORD first_data; /* First data sector */
91 WORD high_cluster; /* Highest cluster number */
92 WORD sectors_in_FAT; /* Number of sectors per FAT */
93 WORD start_dir; /* Starting sector of first dir */
94 DWORD driver_head; /* Address of device driver header (?) */
95 BYTE media_ID; /* Media ID */
96 BYTE access_flag; /* Prev. accessed flag (0=yes,0xFF=no) */
97 DWORD next; /* Pointer to next DPB in list */
98 WORD free_search; /* Free cluster search start */
99 WORD free_clusters; /* Number of free clusters (0xFFFF=unknown) */
102 struct EDPB /* FAT32 extended Drive Parameter Block */
103 { /* from Ralf Brown's Interrupt List */
104 struct DPB dpb; /* first 24 bytes = original DPB */
106 BYTE edpb_flags; /* undocumented/unknown flags */
107 DWORD next_edpb; /* pointer to next EDPB */
108 WORD free_cluster; /* cluster to start search for free space on write, typically
109 the last cluster allocated */
110 WORD clusters_free; /* number of free clusters on drive or FFFF = unknown */
111 WORD clusters_free_hi; /* hiword of clusters_free */
112 WORD mirroring_flags; /* mirroring flags: bit 7 set = do not mirror active FAT */
113 /* bits 0-3 = 0-based number of the active FAT */
114 WORD info_sector; /* sector number of file system info sector, or FFFF for none */
115 WORD spare_boot_sector; /* sector number of backup boot sector, or FFFF for none */
116 DWORD first_cluster; /* sector number of the first cluster */
117 DWORD max_cluster; /* sector number of the last cluster */
118 DWORD fat_clusters; /* number of clusters occupied by FAT */
119 DWORD root_cluster; /* cluster number of start of root directory */
120 DWORD free_cluster2; /* same as free_cluster: cluster at which to start
121 search for free space when writing */
132 static struct DosHeap *heap;
133 static WORD DosHeapHandle;
135 extern char TempDirectory[];
137 static void INT21_ReadConfigSys(void)
140 if (!done) DOSCONF_ReadConfig();
144 static BOOL INT21_CreateHeap(void)
146 if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
148 WARN("Out of memory\n");
151 heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
152 dpbsegptr = MAKESEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
153 strcpy(heap->biosdate, "01/01/80");
157 static BYTE *GetCurrentDTA( CONTEXT86 *context )
159 TDB *pTask = TASK_GetCurrent();
161 /* FIXME: This assumes DTA was set correctly! */
162 return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
163 (DWORD)OFFSETOF(pTask->dta) );
167 void CreateBPB(int drive, BYTE *data, BOOL16 limited)
168 /* limited == TRUE is used with INT 0x21/0x440d */
173 setword(&data[3], 0);
175 setword(&data[6], 240);
176 setword(&data[8], 64000);
178 setword(&data[0x0b], 40);
179 setword(&data[0x0d], 56);
180 setword(&data[0x0f], 2);
181 setword(&data[0x11], 0);
183 setword(&data[0x1f], 800);
185 setword(&data[0x22], 1);
187 } else { /* 1.44mb */
190 setword(&data[3], 0);
192 setword(&data[6], 240);
193 setword(&data[8], 2880);
195 setword(&data[0x0b], 6);
196 setword(&data[0x0d], 18);
197 setword(&data[0x0f], 2);
198 setword(&data[0x11], 0);
200 setword(&data[0x1f], 80);
202 setword(&data[0x22], 2);
207 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
209 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
210 char root[] = "A:\\";
212 *root += DOS_GET_DRIVE( DL_reg(context) );
213 if (!GetDiskFreeSpaceA( root, &cluster_sectors, §or_bytes,
214 &free_clusters, &total_clusters )) return 0;
215 SET_AX( context, cluster_sectors );
216 SET_BX( context, free_clusters );
217 SET_CX( context, sector_bytes );
218 SET_DX( context, total_clusters );
222 static int INT21_GetDriveAllocInfo( CONTEXT86 *context )
224 if (!INT21_GetFreeDiskSpace( context )) return 0;
225 if (!heap && !INT21_CreateHeap()) return 0;
226 heap->mediaID = 0xf0;
227 context->SegDs = DosHeapHandle;
228 SET_BX( context, (int)&heap->mediaID - (int)heap );
232 static int FillInDrivePB( int drive )
234 if(!DRIVE_IsValid(drive))
236 SetLastError( ERROR_INVALID_DRIVE );
239 else if (heap || INT21_CreateHeap())
241 /* FIXME: I have no idea what a lot of this information should
242 * say or whether it even really matters since we're not allowing
243 * direct block access. However, some programs seem to depend on
244 * getting at least _something_ back from here. The 'next' pointer
245 * does worry me, though. Should we have a complete table of
246 * separate DPBs per drive? Probably, but I'm lazy. :-) -CH
248 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
249 heap->dpb.sector_size = 512;
250 heap->dpb.high_sector = 1;
251 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
252 heap->dpb.reserved = 0;
253 heap->dpb.num_FAT = 1;
254 heap->dpb.dir_entries = 2;
255 heap->dpb.first_data = 2;
256 heap->dpb.high_cluster = 64000;
257 heap->dpb.sectors_in_FAT = 1;
258 heap->dpb.start_dir = 1;
259 heap->dpb.driver_head = 0;
260 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
261 heap->dpb.access_flag = 0;
263 heap->dpb.free_search = 0;
264 heap->dpb.free_clusters = 0xFFFF; /* unknown */
271 static void GetDrivePB( CONTEXT86 *context, int drive )
273 if (FillInDrivePB( drive ))
275 SET_AL( context, 0x00 );
276 context->SegDs = SELECTOROF(dpbsegptr);
277 SET_BX( context, OFFSETOF(dpbsegptr) );
281 SET_AX( context, 0x00ff );
286 static void ioctlGetDeviceInfo( CONTEXT86 *context )
289 const DOS_DEVICE *dev;
291 TRACE("(%d)\n", BX_reg(context));
293 RESET_CFLAG(context);
296 if ((dev = DOSFS_GetDeviceByHandle( DosFileHandleToWin32Handle(BX_reg(context)) )))
298 SET_DX( context, dev->flags );
302 /* it seems to be a file */
303 curr_drive = DRIVE_GetCurrentDrive();
304 SET_DX( context, 0x0140 + curr_drive + ((curr_drive > 1) ? 0x0800 : 0) );
306 /* bits 0-5 are current drive
307 * bit 6 - file has NOT been written..FIXME: correct?
308 * bit 8 - generate int24 if no diskspace on write/ read past end of file
309 * bit 11 - media not removable
310 * bit 14 - don't set file date/time on closing
311 * bit 15 - file is remote
315 static BOOL ioctlGenericBlkDevReq( CONTEXT86 *context )
317 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
318 int drive = DOS_GET_DRIVE( BL_reg(context) );
320 if (!DRIVE_IsValid(drive))
322 SetLastError( ERROR_FILE_NOT_FOUND );
326 if (CH_reg(context) != 0x08)
328 INT_BARF( context, 0x21 );
332 switch (CL_reg(context))
334 case 0x4a: /* lock logical volume */
335 TRACE("lock logical volume (%d) level %d mode %d\n",drive,BH_reg(context),DX_reg(context));
338 case 0x60: /* get device parameters */
339 /* used by w4wgrp's winfile */
340 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
342 dataptr[6] = 0; /* media type */
345 dataptr[1] = 0x05; /* fixed disk */
346 setword(&dataptr[2], 0x01); /* non removable */
347 setword(&dataptr[4], 0x300); /* # of cylinders */
351 dataptr[1] = 0x07; /* block dev, floppy */
352 setword(&dataptr[2], 0x02); /* removable */
353 setword(&dataptr[4], 80); /* # of cylinders */
355 CreateBPB(drive, &dataptr[7], TRUE);
356 RESET_CFLAG(context);
359 case 0x41: /* write logical device track */
360 case 0x61: /* read logical device track */
362 BYTE drive = BL_reg(context) ?
363 BL_reg(context) : DRIVE_GetCurrentDrive();
364 WORD head = *(WORD *)dataptr+1;
365 WORD cyl = *(WORD *)dataptr+3;
366 WORD sect = *(WORD *)dataptr+5;
367 WORD nrsect = *(WORD *)dataptr+7;
368 BYTE *data = (BYTE *)dataptr+9;
369 int (*raw_func)(BYTE, DWORD, DWORD, BYTE *, BOOL);
371 raw_func = (CL_reg(context) == 0x41) ?
372 DRIVE_RawWrite : DRIVE_RawRead;
374 if (raw_func(drive, head*cyl*sect, nrsect, data, FALSE))
375 RESET_CFLAG(context);
378 SET_AX( context, 0x1e ); /* read fault */
383 case 0x66:/* get disk serial number */
385 char label[12],fsname[9],path[4];
388 strcpy(path,"x:\\");path[0]=drive+'A';
389 GetVolumeInformationA(
390 path,label,12,&serial,NULL,NULL,fsname,9
393 memcpy(dataptr+2,&serial,4);
394 memcpy(dataptr+6,label ,11);
395 memcpy(dataptr+17,fsname,8);
400 TRACE("logical volume %d unlocked.\n",drive);
404 memset(dataptr+1, '\0', dataptr[0]-1);
405 dataptr[1] = dataptr[0];
406 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
407 dataptr[3] = 0xFF; /* no physical drive */
411 /* Trail on error implementation */
412 SET_AX( context, GetDriveType16(BL_reg(context)) == DRIVE_UNKNOWN ? 0x0f : 0x01 );
413 SET_CFLAG(context); /* Seems to be set all the time */
417 INT_BARF( context, 0x21 );
422 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
425 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
427 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
433 SET_AL( context, 0xff ); /* failed */
435 TRACE("filename: '%s'\n", filename);
441 if ((*s != ' ') && (*s != '\r') && (*s != '\n'))
450 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
451 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
452 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
454 DOSFS_ToDosFCBFormat(buffer, fcbW);
455 HeapFree(GetProcessHeap(), 0, buffer);
456 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
458 TRACE("FCB: '%s'\n", fcb + 1);
460 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
462 /* point DS:SI to first unparsed character */
463 SET_SI( context, context->Esi + (int)s - (int)filename );
467 /* Many calls translate a drive argument like this:
468 drive number (00h = default, 01h = A:, etc)
470 static char drivestring[]="default";
472 char *INT21_DriveName(int drive)
477 drivestring[0]= (unsigned char)drive + '@';
483 static BOOL INT21_CreateFile( CONTEXT86 *context )
485 SET_AX( context, _lcreat16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
486 context->Edx ), CX_reg(context) ) );
487 return (AX_reg(context) == (WORD)HFILE_ERROR16);
490 static HFILE16 _lcreat16_uniq( LPCSTR path, INT attr )
492 /* Mask off all flags not explicitly allowed by the doc */
493 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
494 return Win32HandleToDosFileHandle( CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
495 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
496 CREATE_NEW, attr, 0 ));
499 static void OpenExistingFile( CONTEXT86 *context )
501 SET_AX( context, _lopen16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
503 if (AX_reg(context) == (WORD)HFILE_ERROR16)
505 SET_AX( context, GetLastError() );
510 static BOOL INT21_ExtendedOpenCreateFile(CONTEXT86 *context )
512 BOOL bExtendedError = FALSE;
513 BYTE action = DL_reg(context);
515 /* Shuffle arguments to call OpenExistingFile */
516 SET_AL( context, BL_reg(context) );
517 SET_DX( context, SI_reg(context) );
518 /* BX,CX and DX should be preserved */
519 OpenExistingFile(context);
521 if ((context->EFlags & 0x0001) == 0) /* File exists */
523 UINT16 uReturnCX = 0;
525 /* Now decide what do do */
527 if ((action & 0x07) == 0)
529 _lclose16( AX_reg(context) );
530 SET_AX( context, 0x0050 ); /*File exists*/
532 WARN("extended open/create: failed because file exists \n");
534 else if ((action & 0x07) == 2)
536 /* Truncate it, but first check if opened for write */
537 if ((BL_reg(context) & 0x0007)== 0)
539 _lclose16( AX_reg(context) );
540 WARN("extended open/create: failed, trunc on ro file\n");
541 SET_AX( context, 0x000C ); /*Access code invalid*/
546 TRACE("extended open/create: Closing before truncate\n");
547 if (_lclose16( AX_reg(context) ))
549 WARN("extended open/create: close before trunc failed\n");
550 SET_AX( context, 0x0019 ); /*Seek Error*/
551 SET_CX( context, 0 );
554 /* Shuffle arguments to call CreateFile */
556 TRACE("extended open/create: Truncating\n");
557 SET_AL( context, BL_reg(context) );
558 /* CX is still the same */
559 SET_DX( context, SI_reg(context) );
560 bExtendedError = INT21_CreateFile(context);
562 if (context->EFlags & 0x0001) /*no file open, flags set */
564 WARN("extended open/create: trunc failed\n");
565 return bExtendedError;
570 else uReturnCX = 0x1;
572 SET_CX( context, uReturnCX );
574 else /* file does not exist */
576 RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
577 if ((action & 0xF0)== 0)
579 SET_CX( context, 0 );
581 WARN("extended open/create: failed, file dosen't exist\n");
585 /* Shuffle arguments to call CreateFile */
586 TRACE("extended open/create: Creating\n");
587 SET_AL( context, BL_reg(context) );
588 /* CX should still be the same */
589 SET_DX( context, SI_reg(context) );
590 bExtendedError = INT21_CreateFile(context);
591 if (context->EFlags & 0x0001) /*no file open, flags set */
593 WARN("extended open/create: create failed\n");
594 return bExtendedError;
596 SET_CX( context, 2 );
600 return bExtendedError;
604 static BOOL INT21_ChangeDir( CONTEXT86 *context )
607 char *dirname = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
608 WCHAR dirnameW[MAX_PATH];
610 TRACE("changedir %s\n", dirname);
611 if (dirname[0] && (dirname[1] == ':'))
613 drive = toupper(dirname[0]) - 'A';
616 else drive = DRIVE_GetCurrentDrive();
617 MultiByteToWideChar(CP_OEMCP, 0, dirname, -1, dirnameW, MAX_PATH);
618 return DRIVE_Chdir( drive, dirnameW );
622 static int INT21_FindFirst( CONTEXT86 *context )
626 DOS_FULL_NAME full_name;
627 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
628 WCHAR pathW[MAX_PATH];
631 path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
632 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
634 dta->unixPath = NULL;
635 if (!DOSFS_GetFullName( pathW, FALSE, &full_name ))
637 SET_AX( context, GetLastError() );
641 dta->unixPath = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.long_name)+1 );
642 strcpy( dta->unixPath, full_name.long_name );
643 p = strrchr( dta->unixPath, '/' );
646 MultiByteToWideChar(CP_OEMCP, 0, p + 1, -1, pathW, MAX_PATH);
648 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
649 * (doesn't matter as it is set below anyway)
651 if (!DOSFS_ToDosFCBFormat( pathW, maskW ))
653 HeapFree( GetProcessHeap(), 0, dta->unixPath );
654 dta->unixPath = NULL;
655 SetLastError( ERROR_FILE_NOT_FOUND );
656 SET_AX( context, ERROR_FILE_NOT_FOUND );
660 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
661 dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
662 : DRIVE_GetCurrentDrive();
664 dta->search_attr = CL_reg(context);
669 static int INT21_FindNext( CONTEXT86 *context )
671 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
672 WIN32_FIND_DATAA entry;
675 if (!dta->unixPath) return 0;
676 if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
677 dta->search_attr, dta->count, &entry )))
679 HeapFree( GetProcessHeap(), 0, dta->unixPath );
680 dta->unixPath = NULL;
683 if ((int)dta->count + count > 0xffff)
685 WARN("Too many directory entries in %s\n", dta->unixPath );
686 HeapFree( GetProcessHeap(), 0, dta->unixPath );
687 dta->unixPath = NULL;
691 dta->fileattr = entry.dwFileAttributes;
692 dta->filesize = entry.nFileSizeLow;
693 FileTimeToDosDateTime( &entry.ftLastWriteTime,
694 &dta->filedate, &dta->filetime );
695 strcpy( dta->filename, entry.cAlternateFileName );
696 if (!memchr(dta->mask,'?',11)) {
697 /* wildcardless search, release resources in case no findnext will
698 * be issued, and as a workaround in case file creation messes up
699 * findnext, as sometimes happens with pkunzip */
700 HeapFree( GetProcessHeap(), 0, dta->unixPath );
701 dta->unixPath = NULL;
707 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
709 static int counter = 0;
710 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
711 char *p = name + strlen(name);
713 /* despite what Ralf Brown says, some programs seem to call without
714 * ending backslash (DOS accepts that, so we accept it too) */
715 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
719 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
720 counter = (counter + 1) % 1000;
722 if ((SET_AX( context, _lcreat16_uniq( name, 0 ))) != (WORD)HFILE_ERROR16)
724 TRACE("created %s\n", name );
727 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
732 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context )
734 int drive = DOS_GET_DRIVE( DL_reg(context) );
735 char *ptr = (char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
737 if (!DRIVE_IsValid(drive))
739 SetLastError( ERROR_INVALID_DRIVE );
742 WideCharToMultiByte(CP_OEMCP, 0, DRIVE_GetDosCwd(drive), -1, ptr, 64, NULL, NULL);
743 ptr[63] = 0; /* ensure 0 termination */
744 SET_AX( context, 0x0100 ); /* success return code */
749 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
751 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
752 int drive = DOS_GET_DRIVE( BL_reg(context) );
754 if (!DRIVE_IsValid(drive))
756 SetLastError( ERROR_INVALID_DRIVE );
760 *(WORD *)dataptr = 0;
761 *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
762 memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
763 strncpy(dataptr + 0x11, "FAT16 ", 8);
768 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
770 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
771 int drive = DOS_GET_DRIVE( BL_reg(context) );
773 if (!DRIVE_IsValid(drive))
775 SetLastError( ERROR_INVALID_DRIVE );
779 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
784 /* microsoft's programmers should be shot for using CP/M style int21
785 calls in Windows for Workgroup's winfile.exe */
787 static int INT21_FindFirstFCB( CONTEXT86 *context )
789 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
794 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
795 else pFCB = (FINDFILE_FCB *)fcb;
796 drive = DOS_GET_DRIVE( pFCB->drive );
797 if (!DRIVE_IsValid( drive )) return 0;
798 root = DRIVE_GetRoot( drive );
799 cwd = DRIVE_GetUnixCwd( drive );
800 pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
801 strlen(root)+strlen(cwd)+2 );
802 if (!pFCB->unixPath) return 0;
803 strcpy( pFCB->unixPath, root );
804 strcat( pFCB->unixPath, "/" );
805 strcat( pFCB->unixPath, cwd );
811 static int INT21_FindNextFCB( CONTEXT86 *context )
813 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
815 DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA(context);
816 WIN32_FIND_DATAA entry;
820 if (*fcb == 0xff) /* extended FCB ? */
823 pFCB = (FINDFILE_FCB *)(fcb + 7);
828 pFCB = (FINDFILE_FCB *)fcb;
831 if (!pFCB->unixPath) return 0;
832 if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
833 DOS_GET_DRIVE( pFCB->drive ), attr,
834 pFCB->count, &entry )))
836 HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
837 pFCB->unixPath = NULL;
840 pFCB->count += count;
842 if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
843 *(BYTE *)pResult = 0xff;
844 (BYTE *)pResult +=6; /* leave reserved field behind */
845 *(BYTE *)pResult = entry.dwFileAttributes;
848 *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
850 pResult->fileattr = entry.dwFileAttributes;
851 pResult->cluster = 0; /* what else? */
852 pResult->filesize = entry.nFileSizeLow;
853 memset( pResult->reserved, 0, sizeof(pResult->reserved) );
854 FileTimeToDosDateTime( &entry.ftLastWriteTime,
855 &pResult->filedate, &pResult->filetime );
857 /* Convert file name to FCB format */
859 memset( pResult->filename, ' ', sizeof(pResult->filename) );
860 if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
861 else if (!strcmp( entry.cAlternateFileName, ".." ))
862 pResult->filename[0] = pResult->filename[1] = '.';
865 char *p = strrchr( entry.cAlternateFileName, '.' );
866 if (p && p[1] && (p != entry.cAlternateFileName))
868 memcpy( pResult->filename, entry.cAlternateFileName,
869 min( (p - entry.cAlternateFileName), 8 ) );
870 memcpy( pResult->filename + 8, p + 1, min( strlen(p), 3 ) );
873 memcpy( pResult->filename, entry.cAlternateFileName,
874 min( strlen(entry.cAlternateFileName), 8 ) );
880 static void DeleteFileFCB( CONTEXT86 *context )
882 FIXME("(%p): stub\n", context);
885 static void RenameFileFCB( CONTEXT86 *context )
887 FIXME("(%p): stub\n", context);
892 static void fLock( CONTEXT86 * context )
895 switch ( AX_reg(context) & 0xff )
897 case 0x00: /* LOCK */
898 TRACE("lock handle %d offset %ld length %ld\n",
900 MAKELONG(DX_reg(context),CX_reg(context)),
901 MAKELONG(DI_reg(context),SI_reg(context))) ;
902 if (!LockFile(DosFileHandleToWin32Handle(BX_reg(context)),
903 MAKELONG(DX_reg(context),CX_reg(context)), 0,
904 MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
905 SET_AX( context, GetLastError() );
910 case 0x01: /* UNLOCK */
911 TRACE("unlock handle %d offset %ld length %ld\n",
913 MAKELONG(DX_reg(context),CX_reg(context)),
914 MAKELONG(DI_reg(context),SI_reg(context))) ;
915 if (!UnlockFile(DosFileHandleToWin32Handle(BX_reg(context)),
916 MAKELONG(DX_reg(context),CX_reg(context)), 0,
917 MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
918 SET_AX( context, GetLastError() );
923 SET_AX( context, 0x0001 );
930 INT21_networkfunc (CONTEXT86 *context)
932 switch (AL_reg(context)) {
933 case 0x00: /* Get machine name. */
935 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
936 TRACE("getting machine name to %p\n", dst);
937 if (gethostname (dst, 15))
940 SetLastError( ER_NoNetwork );
943 int len = strlen (dst);
947 SET_CH( context, 1 ); /* Valid */
948 SET_CL( context, 1 ); /* NETbios number??? */
949 TRACE("returning %s\n", debugstr_an (dst, 16));
955 SetLastError( ER_NoNetwork );
961 static void ASPI_DOS_HandleInt( CONTEXT86 *context )
963 if (!Dosvm.ASPIHandler && !DPMI_LoadDosSystem())
965 ERR("could not setup ASPI handler\n");
968 Dosvm.ASPIHandler( context );
972 /***********************************************************************
975 void WINAPI INT_Int21Handler( CONTEXT86 *context )
977 BOOL bSetDOSExtendedError = FALSE;
979 switch(AH_reg(context))
981 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
982 TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
983 context->SegDs,DX_reg(context) );
985 LPSTR data = CTX_SEG_OFF_TO_LIN(context,context->SegDs,context->Edx);
987 /* do NOT use strchr() to calculate the string length,
988 as '\0' is valid string content, too !
989 Maybe we should check for non-'$' strings, but DOS doesn't. */
990 while (*p != '$') p++;
991 _hwrite16( 1, data, (int)p - (int)data);
992 SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
996 case 0x0a: /* BUFFERED INPUT */
998 char *buffer = ((char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1002 TRACE("BUFFERED INPUT (size=%d)\n",buffer[0]);
1004 TRACE("Handle old chars in buffer!\n");
1005 res=_lread16( 0, buffer+2,buffer[0]);
1007 if(buffer[res+1] == '\n')
1008 buffer[res+1] = '\r';
1012 case 0x5c: /* "FLOCK" - RECORD LOCKING */
1016 case 0x0e: /* SELECT DEFAULT DRIVE */
1017 TRACE("SELECT DEFAULT DRIVE %d\n", DL_reg(context));
1018 DRIVE_SetCurrentDrive( DL_reg(context) );
1019 SET_AL( context, MAX_DOS_DRIVES );
1022 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1023 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
1024 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1025 if (!INT21_FindFirstFCB(context))
1027 SET_AL( context, 0xff );
1030 /* else fall through */
1032 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1033 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
1036 case 0x13: /* DELETE FILE USING FCB */
1037 DeleteFileFCB(context);
1040 case 0x17: /* RENAME FILE USING FCB */
1041 RenameFileFCB(context);
1044 case 0x19: /* GET CURRENT DEFAULT DRIVE */
1045 SET_AL( context, DRIVE_GetCurrentDrive() );
1048 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1050 TDB *pTask = TASK_GetCurrent();
1051 pTask->dta = MAKESEGPTR(context->SegDs,DX_reg(context));
1052 TRACE("Set DTA: %08lx\n", pTask->dta);
1056 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1057 SET_DL( context, 0 );
1058 if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
1061 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1062 if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
1065 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1066 GetDrivePB(context, DRIVE_GetCurrentDrive());
1069 case 0x29: /* PARSE FILENAME INTO FCB */
1070 INT21_ParseFileNameIntoFCB(context);
1073 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1074 TRACE("GET DISK TRANSFER AREA ADDRESS\n");
1076 TDB *pTask = TASK_GetCurrent();
1077 context->SegEs = SELECTOROF( pTask->dta );
1078 SET_BX( context, OFFSETOF( pTask->dta ) );
1082 case 0x30: /* GET DOS VERSION */
1083 TRACE("GET DOS VERSION %s requested\n",
1084 (AL_reg(context) == 0x00)?"OEM number":"version flag");
1085 SET_AX( context, (HIWORD(GetVersion16()) >> 8) | (HIWORD(GetVersion16()) << 8) );
1087 SET_AH( context, 0x7 );
1088 SET_AL( context, 0xA );
1091 SET_BX( context, 0x00FF ); /* 0x123456 is Wine's serial # */
1092 SET_CX( context, 0x0000 );
1095 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1096 TRACE("GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
1097 INT21_DriveName( DL_reg(context)));
1098 GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
1101 case 0x33: /* MULTIPLEXED */
1102 switch (AL_reg(context))
1104 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1105 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
1106 INT21_ReadConfigSys();
1107 SET_DL( context, DOSCONF_config.brk_flag );
1110 case 0x01: /* SET EXTENDED BREAK STATE */
1111 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
1112 INT21_ReadConfigSys();
1113 DOSCONF_config.brk_flag = (DL_reg(context) > 0);
1116 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1117 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
1118 INT21_ReadConfigSys();
1119 /* ugly coding in order to stay reentrant */
1120 if (DL_reg(context))
1122 SET_DL( context, DOSCONF_config.brk_flag );
1123 DOSCONF_config.brk_flag = 1;
1127 SET_DL( context, DOSCONF_config.brk_flag );
1128 DOSCONF_config.brk_flag = 0;
1132 case 0x05: /* GET BOOT DRIVE */
1133 TRACE("GET BOOT DRIVE\n");
1134 SET_DL( context, 3 );
1135 /* c: is Wine's bootdrive (a: is 1)*/
1138 case 0x06: /* GET TRUE VERSION NUMBER */
1139 TRACE("GET TRUE VERSION NUMBER\n");
1140 SET_BX( context, (HIWORD(GetVersion16() >> 8)) | (HIWORD(GetVersion16() << 8)) );
1141 SET_DX( context, 0x00 );
1145 INT_BARF( context, 0x21 );
1150 case 0x36: /* GET FREE DISK SPACE */
1151 TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
1152 INT21_DriveName( DL_reg(context)));
1153 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
1158 unsigned char switchchar='/';
1159 switch (AL_reg(context))
1161 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
1162 TRACE("SWITCHAR - GET SWITCH CHARACTER\n");
1163 SET_AL( context, 0x00 ); /* success*/
1164 SET_DL( context, switchchar );
1166 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
1167 TRACE("SWITCHAR - SET SWITCH CHARACTER\n");
1168 switchchar = DL_reg(context);
1169 SET_AL( context, 0x00 ); /* success*/
1171 default: /*"AVAILDEV" - SPECIFY \DEV\ PREFIX USE*/
1172 INT_BARF( context, 0x21 );
1178 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1180 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1181 bSetDOSExtendedError = (!CreateDirectory16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1182 context->Edx ), NULL));
1183 /* FIXME: CreateDirectory's LastErrors will clash with the ones
1184 * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1185 * denied), while CreateDirectory return several ones. remap some of
1188 if (bSetDOSExtendedError) {
1189 switch (GetLastError()) {
1190 case ERROR_ALREADY_EXISTS:
1191 case ERROR_FILENAME_EXCED_RANGE:
1192 case ERROR_DISK_FULL:
1193 SetLastError(ERROR_ACCESS_DENIED);
1200 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1202 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1203 bSetDOSExtendedError = (!RemoveDirectory16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1207 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1209 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1210 bSetDOSExtendedError = !INT21_ChangeDir(context);
1213 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1214 TRACE("CREAT flag 0x%02x %s\n",CX_reg(context),
1215 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1216 bSetDOSExtendedError = INT21_CreateFile( context );
1219 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1220 TRACE("OPEN mode 0x%02x %s\n",AL_reg(context),
1221 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1222 OpenExistingFile(context);
1225 case 0x3e: /* "CLOSE" - CLOSE FILE */
1226 TRACE("CLOSE handle %d\n",BX_reg(context));
1227 SET_AX( context, _lclose16( BX_reg(context) ));
1228 bSetDOSExtendedError = (AX_reg(context) != 0);
1231 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1232 TRACE("READ from %d to %04lX:%04X for %d byte\n",BX_reg(context),
1233 context->SegDs,DX_reg(context),CX_reg(context) );
1237 result = _hread16( BX_reg(context),
1238 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1242 result = WIN16_hread( BX_reg(context),
1243 MAKESEGPTR( context->SegDs, context->Edx ),
1245 if (result == -1) bSetDOSExtendedError = TRUE;
1246 else SET_AX( context, (WORD)result );
1250 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1251 TRACE("WRITE from %04lX:%04X to handle %d for %d byte\n",
1252 context->SegDs,DX_reg(context),BX_reg(context),CX_reg(context) );
1254 LONG result = _hwrite16( BX_reg(context),
1255 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1258 if (result == -1) bSetDOSExtendedError = TRUE;
1259 else SET_AX( context, (WORD)result );
1263 case 0x41: /* "UNLINK" - DELETE FILE */
1264 TRACE("UNLINK %s\n",
1265 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1266 bSetDOSExtendedError = (!DeleteFileA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1270 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1271 TRACE("LSEEK handle %d offset %ld from %s\n",
1272 BX_reg(context), MAKELONG(DX_reg(context),CX_reg(context)),
1273 (AL_reg(context)==0)?"start of file":(AL_reg(context)==1)?
1274 "current file position":"end of file");
1276 LONG status = _llseek16( BX_reg(context),
1277 MAKELONG(DX_reg(context),CX_reg(context)),
1279 if (status == -1) bSetDOSExtendedError = TRUE;
1282 SET_AX( context, LOWORD(status) );
1283 SET_DX( context, HIWORD(status) );
1288 case 0x43: /* FILE ATTRIBUTES */
1289 switch (AL_reg(context))
1292 TRACE("GET FILE ATTRIBUTES for %s\n",
1293 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1294 SET_AX( context, GetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1296 if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1297 else SET_CX( context, AX_reg(context) );
1301 TRACE("SET FILE ATTRIBUTES 0x%02x for %s\n", CX_reg(context),
1302 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1303 bSetDOSExtendedError =
1304 (!SetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1309 FIXME("GET COMPRESSED FILE SIZE for %s stub\n",
1310 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1314 case 0x44: /* IOCTL */
1315 switch (AL_reg(context))
1318 ioctlGetDeviceInfo(context);
1324 const DOS_DEVICE *dev;
1325 static const WCHAR scsimgrW[] = {'S','C','S','I','M','G','R','$',0};
1326 if ((dev = DOSFS_GetDeviceByHandle( DosFileHandleToWin32Handle(BX_reg(context)) )) &&
1327 !strcmpiW( dev->name, scsimgrW ))
1329 ASPI_DOS_HandleInt(context);
1333 case 0x05:{ /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1334 /*BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);*/
1335 int drive = DOS_GET_DRIVE(BL_reg(context));
1337 FIXME("program tried to write to block device control channel of drive %d:\n",drive);
1338 /* for (i=0;i<CX_reg(context);i++)
1339 fprintf(stdnimp,"%02x ",dataptr[i]);
1340 fprintf(stdnimp,"\n");*/
1341 SET_AX( context, context->Ecx );
1344 case 0x08: /* Check if drive is removable. */
1345 TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOVABLE for drive %s\n",
1346 INT21_DriveName( BL_reg(context)));
1347 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1350 SetLastError( ERROR_INVALID_DRIVE );
1351 SET_AX( context, ERROR_INVALID_DRIVE );
1354 case DRIVE_REMOVABLE:
1355 SET_AX( context, 0 ); /* removable */
1358 SET_AX( context, 1 ); /* not removable */
1363 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
1364 TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOTE for drive %s\n",
1365 INT21_DriveName( BL_reg(context)));
1366 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1369 SetLastError( ERROR_INVALID_DRIVE );
1370 SET_AX( context, ERROR_INVALID_DRIVE );
1374 SET_DX( context, (1<<9) | (1<<12) ); /* remote */
1377 SET_DX( context, 0 ); /* FIXME: use driver attr here */
1382 case 0x0a: /* check if handle (BX) is remote */
1383 TRACE("IOCTL - CHECK IF HANDLE %d IS REMOTE\n",BX_reg(context));
1384 /* returns DX, bit 15 set if remote, bit 14 set if date/time
1387 SET_DX( context, 0 );
1391 TRACE("IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
1392 INT21_DriveName( BL_reg(context)));
1393 bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1396 case 0x0e: /* get logical drive mapping */
1397 TRACE("IOCTL - GET LOGICAL DRIVE MAP for drive %s\n",
1398 INT21_DriveName( BL_reg(context)));
1399 SET_AL( context, 0 ); /* drive has no mapping - FIXME: may be wrong*/
1402 case 0x0F: /* Set logical drive mapping */
1405 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
1406 INT21_DriveName( BL_reg(context)));
1407 drive = DOS_GET_DRIVE ( BL_reg(context) );
1408 if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
1411 SET_AX( context, 0x000F ); /* invalid drive */
1416 case 0xe0: /* Sun PC-NFS API */
1420 case 0x52: /* DR-DOS version */
1421 /* This is not DR-DOS */
1423 TRACE("GET DR-DOS VERSION requested\n");
1425 SET_AX( context, 0x0001 ); /* Invalid function */
1426 SET_CFLAG(context); /* Error */
1427 SetLastError( ERROR_INVALID_FUNCTION );
1431 INT_BARF( context, 0x21 );
1436 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1439 TRACE("DUP - DUPLICATE FILE HANDLE %d\n",BX_reg(context));
1440 if ((bSetDOSExtendedError = !DuplicateHandle( GetCurrentProcess(),
1441 DosFileHandleToWin32Handle(BX_reg(context)),
1442 GetCurrentProcess(), &handle,
1443 0, TRUE, DUPLICATE_SAME_ACCESS )))
1444 SET_AX( context, HFILE_ERROR16 );
1446 SET_AX( context, Win32HandleToDosFileHandle(handle) );
1450 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1451 TRACE("FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
1452 BX_reg(context),CX_reg(context));
1453 bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR16);
1456 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1457 TRACE("CWD - GET CURRENT DIRECTORY for drive %s\n",
1458 INT21_DriveName( DL_reg(context)));
1459 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1462 case 0x4a: /* RESIZE MEMORY BLOCK */
1463 TRACE("RESIZE MEMORY segment %04lX to %d paragraphs\n", context->SegEs, BX_reg(context));
1464 if (!ISV86(context))
1465 FIXME("RESIZE MEMORY probably insufficient implementation. Expect crash soon\n");
1467 LPVOID *mem = DOSMEM_ResizeBlock(DOSMEM_MapDosToLinear(context->SegEs<<4),
1468 BX_reg(context)<<4,NULL);
1470 SET_AX( context, DOSMEM_MapLinearToDos(mem)>>4 );
1473 SET_AX( context, 0x0008 ); /* insufficient memory */
1474 SET_BX( context, DOSMEM_Available()>>4 ); /* not quite right */
1479 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1480 TRACE("EXEC %s\n", (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx ));
1481 SET_AX( context, WinExec16( CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx ),
1483 if (AX_reg(context) < 32) SET_CFLAG(context);
1486 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1487 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
1488 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1489 if (!INT21_FindFirst(context)) break;
1492 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1493 TRACE("FINDNEXT\n");
1494 if (!INT21_FindNext(context))
1496 SetLastError( ERROR_NO_MORE_FILES );
1497 SET_AX( context, ERROR_NO_MORE_FILES );
1500 else SET_AX( context, 0 ); /* OK */
1503 case 0x56: /* "RENAME" - RENAME FILE */
1504 TRACE("RENAME %s to %s\n",
1505 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1506 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi));
1507 bSetDOSExtendedError =
1508 (!MoveFileA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1509 CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi)));
1512 case 0x57: /* FILE DATE AND TIME */
1513 switch (AL_reg(context))
1515 case 0x00: /* Get */
1518 TRACE("GET FILE DATE AND TIME for handle %d\n",
1520 if (!GetFileTime( DosFileHandleToWin32Handle(BX_reg(context)), NULL, NULL, &filetime ))
1521 bSetDOSExtendedError = TRUE;
1525 FileTimeToDosDateTime( &filetime, &date, &time );
1526 SET_DX( context, date );
1527 SET_CX( context, time );
1532 case 0x01: /* Set */
1535 TRACE("SET FILE DATE AND TIME for handle %d\n",
1537 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
1539 bSetDOSExtendedError =
1540 (!SetFileTime( DosFileHandleToWin32Handle(BX_reg(context)),
1541 NULL, NULL, &filetime ));
1547 case 0x5a: /* CREATE TEMPORARY FILE */
1548 TRACE("CREATE TEMPORARY FILE\n");
1549 bSetDOSExtendedError = !INT21_CreateTempFile(context);
1552 case 0x5b: /* CREATE NEW FILE */
1553 TRACE("CREATE NEW FILE 0x%02x for %s\n", CX_reg(context),
1554 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1556 _lcreat16_uniq( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1558 bSetDOSExtendedError = (AX_reg(context) != 0);
1561 case 0x5d: /* NETWORK */
1562 FIXME("Function 0x%04x not implemented.\n", AX_reg (context));
1563 /* Fix the following while you're at it. */
1564 SetLastError( ER_NoNetwork );
1565 bSetDOSExtendedError = TRUE;
1569 bSetDOSExtendedError = INT21_networkfunc (context);
1572 case 0x5f: /* NETWORK */
1573 switch (AL_reg(context))
1575 case 0x07: /* ENABLE DRIVE */
1576 TRACE("ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1577 if (!DRIVE_Enable( DL_reg(context) ))
1579 SetLastError( ERROR_INVALID_DRIVE );
1580 bSetDOSExtendedError = TRUE;
1584 case 0x08: /* DISABLE DRIVE */
1585 TRACE("DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1586 if (!DRIVE_Disable( DL_reg(context) ))
1588 SetLastError( ERROR_INVALID_DRIVE );
1589 bSetDOSExtendedError = TRUE;
1594 /* network software not installed */
1595 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
1596 SetLastError( ER_NoNetwork );
1597 bSetDOSExtendedError = TRUE;
1602 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1603 TRACE("TRUENAME %s\n",
1604 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
1606 if (!GetFullPathNameA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1608 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1609 context->Edi),NULL))
1610 bSetDOSExtendedError = TRUE;
1611 else SET_AX( context, 0 );
1615 case 0x68: /* "FFLUSH" - COMMIT FILE */
1616 case 0x6a: /* COMMIT FILE */
1617 TRACE("FFLUSH/COMMIT handle %d\n",BX_reg(context));
1618 bSetDOSExtendedError = (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ));
1621 case 0x69: /* DISK SERIAL NUMBER */
1622 switch (AL_reg(context))
1625 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
1626 INT21_DriveName(BL_reg(context)));
1627 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1628 else SET_AX( context, 0 );
1632 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
1633 INT21_DriveName(BL_reg(context)));
1634 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1635 else SET_AX( context, 1 );
1640 case 0x6C: /* Extended Open/Create*/
1641 TRACE("EXTENDED OPEN/CREATE %s\n",
1642 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edi));
1643 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1646 case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
1647 if ((GetVersion()&0xC0000004)!=0xC0000004) {
1648 /* not supported on anything but Win95 */
1649 TRACE("LONG FILENAME functions supported only by win95\n");
1651 SET_AL( context, 0 );
1653 switch(AL_reg(context))
1655 case 0x39: /* Create directory */
1656 TRACE("LONG FILENAME - MAKE DIRECTORY %s\n",
1657 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
1658 bSetDOSExtendedError = (!CreateDirectoryA(
1659 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1660 context->Edx ), NULL));
1661 /* FIXME: CreateDirectory's LastErrors will clash with the ones
1662 * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1663 * denied), while CreateDirectory return several ones. remap some of
1666 if (bSetDOSExtendedError) {
1667 switch (GetLastError()) {
1668 case ERROR_ALREADY_EXISTS:
1669 case ERROR_FILENAME_EXCED_RANGE:
1670 case ERROR_DISK_FULL:
1671 SetLastError(ERROR_ACCESS_DENIED);
1677 case 0x3a: /* Remove directory */
1678 TRACE("LONG FILENAME - REMOVE DIRECTORY %s\n",
1679 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
1680 bSetDOSExtendedError = (!RemoveDirectoryA(
1681 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1684 case 0x43: /* Get/Set file attributes */
1685 TRACE("LONG FILENAME -EXTENDED GET/SET FILE ATTRIBUTES %s\n",
1686 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
1687 switch (BL_reg(context))
1689 case 0x00: /* Get file attributes */
1690 TRACE("\tretrieve attributes\n");
1691 SET_CX( context, GetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1693 if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1696 TRACE("\tset attributes 0x%04x\n",CX_reg(context));
1697 bSetDOSExtendedError = (!SetFileAttributesA(
1698 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1700 CX_reg(context) ) );
1703 FIXME("Unimplemented long file name function:\n");
1704 INT_BARF( context, 0x21 );
1706 SET_AL( context, 0 );
1710 case 0x47: /* Get current directory */
1711 TRACE(" LONG FILENAME - GET CURRENT DIRECTORY for drive %s\n",
1712 INT21_DriveName(DL_reg(context)));
1713 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1716 case 0x4e: /* Find first file */
1717 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
1718 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
1719 /* FIXME: use attributes in CX */
1720 if ((SET_AX( context, FindFirstFile16(
1721 CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1722 (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1724 == INVALID_HANDLE_VALUE16)
1725 bSetDOSExtendedError = TRUE;
1727 case 0x4f: /* Find next file */
1728 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
1730 if (!FindNextFile16( BX_reg(context),
1731 (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1733 bSetDOSExtendedError = TRUE;
1737 LPCSTR driveroot = (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
1738 LPSTR buffer = (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi);
1739 DWORD filename_len, flags;
1741 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", driveroot);
1742 SET_AX( context, 0 );
1743 if (!GetVolumeInformationA( driveroot, NULL, 0, NULL, &filename_len,
1744 &flags, buffer, 8 ))
1746 INT_BARF( context, 0x21 );
1750 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
1751 SET_CX( context, filename_len );
1752 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
1755 case 0xa1: /* Find close */
1756 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
1758 bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
1761 switch(CL_reg(context))
1763 case 0x01: /* Get short filename or path */
1764 if (!GetShortPathNameA
1765 ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1767 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1769 bSetDOSExtendedError = TRUE;
1770 else SET_AX( context, 0 );
1772 case 0x02: /* Get canonical long filename or path */
1773 if (!GetFullPathNameA
1774 ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1776 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1777 context->Edi),NULL))
1778 bSetDOSExtendedError = TRUE;
1779 else SET_AX( context, 0 );
1782 FIXME("Unimplemented long file name function:\n");
1783 INT_BARF( context, 0x21 );
1785 SET_AL( context, 0 );
1789 case 0x6c: /* Create or open file */
1790 TRACE("LONG FILENAME - CREATE OR OPEN FILE %s\n",
1791 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi));
1792 /* translate Dos 7 action to Dos 6 action */
1793 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1796 case 0x3b: /* Change directory */
1797 TRACE("LONG FILENAME - CHANGE DIRECTORY %s\n",
1798 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1799 if (!SetCurrentDirectoryA(CTX_SEG_OFF_TO_LIN(context,
1805 SET_AL( context, GetLastError() );
1808 case 0x41: /* Delete file */
1809 TRACE("LONG FILENAME - DELETE FILE %s\n",
1810 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1811 if (!DeleteFileA(CTX_SEG_OFF_TO_LIN(context,
1816 SET_AL( context, GetLastError() );
1819 case 0x56: /* Move (rename) file */
1821 LPCSTR fn1 = (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1822 LPCSTR fn2 = (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
1823 TRACE("LONG FILENAME - RENAME FILE %s to %s\n", fn1, fn2);
1824 if (!MoveFileA(fn1, fn2))
1827 SET_AL( context, GetLastError() );
1832 FIXME("Unimplemented long file name function:\n");
1833 INT_BARF( context, 0x21 );
1835 SET_AL( context, 0 );
1840 case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
1841 case 0x72: /* MS-DOS 7 (Windows95) - ??? */
1842 TRACE("windows95 function AX %04x\n",
1844 WARN(" returning unimplemented\n");
1846 SET_AL( context, 0 );
1849 case 0x73: /* MULTIPLEXED: Win95 OSR2/Win98 FAT32 calls */
1850 TRACE("windows95 function AX %04x\n",
1853 switch (AL_reg(context))
1855 case 0x02: /* Get Extended Drive Parameter Block for specific drive */
1856 /* ES:DI points to word with length of data (should be 0x3d) */
1860 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
1861 char root[] = "A:\\";
1863 buffer = (WORD *)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
1865 TRACE("Get Extended DPB: linear buffer address is %p\n", buffer);
1867 /* validate passed-in buffer lengths */
1868 if ((*buffer != 0x3d) || (context->Ecx != 0x3f))
1870 WARN("Get Extended DPB: buffer lengths incorrect\n");
1871 WARN("CX = %lx, buffer[0] = %x\n", context->Ecx, *buffer);
1873 SET_AL( context, 0x18 ); /* bad buffer length */
1876 /* buffer checks out */
1877 buffer++; /* skip over length word now */
1878 if (FillInDrivePB( DX_reg(context) ) )
1880 edpb = (struct EDPB *)buffer;
1882 /* copy down the old-style DPB portion first */
1883 memcpy(&edpb->dpb, &heap->dpb, sizeof(struct DPB));
1885 /* now fill in the extended entries */
1886 edpb->edpb_flags = 0;
1887 edpb->next_edpb = 0;
1888 edpb->free_cluster = edpb->free_cluster2 = 0;
1890 /* determine free disk space */
1891 *root += DOS_GET_DRIVE( DX_reg(context) );
1892 GetDiskFreeSpaceA( root, &cluster_sectors, §or_bytes,
1893 &free_clusters, &total_clusters );
1895 edpb->clusters_free = (free_clusters&0xffff);
1897 edpb->clusters_free_hi = free_clusters >> 16;
1898 edpb->mirroring_flags = 0;
1899 edpb->info_sector = 0xffff;
1900 edpb->spare_boot_sector = 0xffff;
1901 edpb->first_cluster = 0;
1902 edpb->max_cluster = total_clusters;
1903 edpb->fat_clusters = 32; /* made-up value */
1904 edpb->root_cluster = 0;
1906 RESET_CFLAG(context); /* clear carry */
1907 SET_AX( context, 0 );
1911 SET_AX( context, 0x00ff );
1917 case 0x03: /* Get Extended free space on drive */
1918 case 0x04: /* Set DPB for formatting */
1919 case 0x05: /* extended absolute disk read/write */
1920 FIXME("Unimplemented FAT32 int32 function %04x\n", AX_reg(context));
1922 SET_AL( context, 0 );
1928 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
1929 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
1933 INT_BARF( context, 0x21 );
1936 } /* END OF SWITCH */
1938 if( bSetDOSExtendedError ) /* set general error condition */
1940 SET_AX( context, GetLastError() );