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"
32 #ifdef HAVE_SYS_FILE_H
33 # include <sys/file.h>
36 #ifdef HAVE_SYS_TIME_H
37 # include <sys/time.h>
39 #include <sys/types.h>
51 #include "winuser.h" /* SW_NORMAL */
52 #include "wine/winbase16.h"
60 #include "wine/unicode.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(int21);
64 #if defined(__svr4__) || defined(_SCO_DS)
65 /* SVR4 DOESNT do locking the same way must implement properly */
72 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
74 /* Define the drive parameter block, as used by int21/1F
75 * and int21/32. This table can be accessed through the
76 * global 'dpb' pointer, which points into the local dos
81 BYTE drive_num; /* 0=A, etc. */
82 BYTE unit_num; /* Drive's unit number (?) */
83 WORD sector_size; /* Sector size in bytes */
84 BYTE high_sector; /* Highest sector in a cluster */
85 BYTE shift; /* Shift count (?) */
86 WORD reserved; /* Number of reserved sectors at start */
87 BYTE num_FAT; /* Number of FATs */
88 WORD dir_entries; /* Number of root dir entries */
89 WORD first_data; /* First data sector */
90 WORD high_cluster; /* Highest cluster number */
91 WORD sectors_in_FAT; /* Number of sectors per FAT */
92 WORD start_dir; /* Starting sector of first dir */
93 DWORD driver_head; /* Address of device driver header (?) */
94 BYTE media_ID; /* Media ID */
95 BYTE access_flag; /* Prev. accessed flag (0=yes,0xFF=no) */
96 DWORD next; /* Pointer to next DPB in list */
97 WORD free_search; /* Free cluster search start */
98 WORD free_clusters; /* Number of free clusters (0xFFFF=unknown) */
101 struct EDPB /* FAT32 extended Drive Parameter Block */
102 { /* from Ralf Brown's Interrupt List */
103 struct DPB dpb; /* first 24 bytes = original DPB */
105 BYTE edpb_flags; /* undocumented/unknown flags */
106 DWORD next_edpb; /* pointer to next EDPB */
107 WORD free_cluster; /* cluster to start search for free space on write, typically
108 the last cluster allocated */
109 WORD clusters_free; /* number of free clusters on drive or FFFF = unknown */
110 WORD clusters_free_hi; /* hiword of clusters_free */
111 WORD mirroring_flags; /* mirroring flags: bit 7 set = do not mirror active FAT */
112 /* bits 0-3 = 0-based number of the active FAT */
113 WORD info_sector; /* sector number of file system info sector, or FFFF for none */
114 WORD spare_boot_sector; /* sector number of backup boot sector, or FFFF for none */
115 DWORD first_cluster; /* sector number of the first cluster */
116 DWORD max_cluster; /* sector number of the last cluster */
117 DWORD fat_clusters; /* number of clusters occupied by FAT */
118 DWORD root_cluster; /* cluster number of start of root directory */
119 DWORD free_cluster2; /* same as free_cluster: cluster at which to start
120 search for free space when writing */
131 static struct DosHeap *heap;
132 static WORD DosHeapHandle;
134 extern char TempDirectory[];
136 static void INT21_ReadConfigSys(void)
139 if (!done) DOSCONF_ReadConfig();
143 static BOOL INT21_CreateHeap(void)
145 if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
147 WARN("Out of memory\n");
150 heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
151 dpbsegptr = MAKESEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
152 strcpy(heap->biosdate, "01/01/80");
156 static BYTE *GetCurrentDTA( CONTEXT86 *context )
158 TDB *pTask = TASK_GetCurrent();
160 /* FIXME: This assumes DTA was set correctly! */
161 return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
162 (DWORD)OFFSETOF(pTask->dta) );
166 void CreateBPB(int drive, BYTE *data, BOOL16 limited)
167 /* limited == TRUE is used with INT 0x21/0x440d */
172 setword(&data[3], 0);
174 setword(&data[6], 240);
175 setword(&data[8], 64000);
177 setword(&data[0x0b], 40);
178 setword(&data[0x0d], 56);
179 setword(&data[0x0f], 2);
180 setword(&data[0x11], 0);
182 setword(&data[0x1f], 800);
184 setword(&data[0x22], 1);
186 } else { /* 1.44mb */
189 setword(&data[3], 0);
191 setword(&data[6], 240);
192 setword(&data[8], 2880);
194 setword(&data[0x0b], 6);
195 setword(&data[0x0d], 18);
196 setword(&data[0x0f], 2);
197 setword(&data[0x11], 0);
199 setword(&data[0x1f], 80);
201 setword(&data[0x22], 2);
206 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
208 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
209 char root[] = "A:\\";
211 *root += DOS_GET_DRIVE( DL_reg(context) );
212 if (!GetDiskFreeSpaceA( root, &cluster_sectors, §or_bytes,
213 &free_clusters, &total_clusters )) return 0;
214 SET_AX( context, cluster_sectors );
215 SET_BX( context, free_clusters );
216 SET_CX( context, sector_bytes );
217 SET_DX( context, total_clusters );
221 static int INT21_GetDriveAllocInfo( CONTEXT86 *context )
223 if (!INT21_GetFreeDiskSpace( context )) return 0;
224 if (!heap && !INT21_CreateHeap()) return 0;
225 heap->mediaID = 0xf0;
226 context->SegDs = DosHeapHandle;
227 SET_BX( context, (int)&heap->mediaID - (int)heap );
231 static int FillInDrivePB( int drive )
233 if(!DRIVE_IsValid(drive))
235 SetLastError( ERROR_INVALID_DRIVE );
238 else if (heap || INT21_CreateHeap())
240 /* FIXME: I have no idea what a lot of this information should
241 * say or whether it even really matters since we're not allowing
242 * direct block access. However, some programs seem to depend on
243 * getting at least _something_ back from here. The 'next' pointer
244 * does worry me, though. Should we have a complete table of
245 * separate DPBs per drive? Probably, but I'm lazy. :-) -CH
247 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
248 heap->dpb.sector_size = 512;
249 heap->dpb.high_sector = 1;
250 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
251 heap->dpb.reserved = 0;
252 heap->dpb.num_FAT = 1;
253 heap->dpb.dir_entries = 2;
254 heap->dpb.first_data = 2;
255 heap->dpb.high_cluster = 64000;
256 heap->dpb.sectors_in_FAT = 1;
257 heap->dpb.start_dir = 1;
258 heap->dpb.driver_head = 0;
259 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
260 heap->dpb.access_flag = 0;
262 heap->dpb.free_search = 0;
263 heap->dpb.free_clusters = 0xFFFF; /* unknown */
270 static void GetDrivePB( CONTEXT86 *context, int drive )
272 if (FillInDrivePB( drive ))
274 SET_AL( context, 0x00 );
275 context->SegDs = SELECTOROF(dpbsegptr);
276 SET_BX( context, OFFSETOF(dpbsegptr) );
280 SET_AX( context, 0x00ff );
285 static void ioctlGetDeviceInfo( CONTEXT86 *context )
288 const DOS_DEVICE *dev;
290 TRACE("(%d)\n", BX_reg(context));
292 RESET_CFLAG(context);
295 if ((dev = DOSFS_GetDeviceByHandle( DosFileHandleToWin32Handle(BX_reg(context)) )))
297 SET_DX( context, dev->flags );
301 /* it seems to be a file */
302 curr_drive = DRIVE_GetCurrentDrive();
303 SET_DX( context, 0x0140 + curr_drive + ((curr_drive > 1) ? 0x0800 : 0) );
305 /* bits 0-5 are current drive
306 * bit 6 - file has NOT been written..FIXME: correct?
307 * bit 8 - generate int24 if no diskspace on write/ read past end of file
308 * bit 11 - media not removable
309 * bit 14 - don't set file date/time on closing
310 * bit 15 - file is remote
314 static BOOL ioctlGenericBlkDevReq( CONTEXT86 *context )
316 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
317 int drive = DOS_GET_DRIVE( BL_reg(context) );
319 if (!DRIVE_IsValid(drive))
321 SetLastError( ERROR_FILE_NOT_FOUND );
325 if (CH_reg(context) != 0x08)
327 INT_BARF( context, 0x21 );
331 switch (CL_reg(context))
333 case 0x4a: /* lock logical volume */
334 TRACE("lock logical volume (%d) level %d mode %d\n",drive,BH_reg(context),DX_reg(context));
337 case 0x60: /* get device parameters */
338 /* used by w4wgrp's winfile */
339 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
341 dataptr[6] = 0; /* media type */
344 dataptr[1] = 0x05; /* fixed disk */
345 setword(&dataptr[2], 0x01); /* non removable */
346 setword(&dataptr[4], 0x300); /* # of cylinders */
350 dataptr[1] = 0x07; /* block dev, floppy */
351 setword(&dataptr[2], 0x02); /* removable */
352 setword(&dataptr[4], 80); /* # of cylinders */
354 CreateBPB(drive, &dataptr[7], TRUE);
355 RESET_CFLAG(context);
358 case 0x41: /* write logical device track */
359 case 0x61: /* read logical device track */
361 BYTE drive = BL_reg(context) ?
362 BL_reg(context) : DRIVE_GetCurrentDrive();
363 WORD head = *(WORD *)dataptr+1;
364 WORD cyl = *(WORD *)dataptr+3;
365 WORD sect = *(WORD *)dataptr+5;
366 WORD nrsect = *(WORD *)dataptr+7;
367 BYTE *data = (BYTE *)dataptr+9;
368 int (*raw_func)(BYTE, DWORD, DWORD, BYTE *, BOOL);
370 raw_func = (CL_reg(context) == 0x41) ?
371 DRIVE_RawWrite : DRIVE_RawRead;
373 if (raw_func(drive, head*cyl*sect, nrsect, data, FALSE))
374 RESET_CFLAG(context);
377 SET_AX( context, 0x1e ); /* read fault */
382 case 0x66:/* get disk serial number */
384 char label[12],fsname[9],path[4];
387 strcpy(path,"x:\\");path[0]=drive+'A';
388 GetVolumeInformationA(
389 path,label,12,&serial,NULL,NULL,fsname,9
392 memcpy(dataptr+2,&serial,4);
393 memcpy(dataptr+6,label ,11);
394 memcpy(dataptr+17,fsname,8);
399 TRACE("logical volume %d unlocked.\n",drive);
403 memset(dataptr+1, '\0', dataptr[0]-1);
404 dataptr[1] = dataptr[0];
405 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
406 dataptr[3] = 0xFF; /* no physical drive */
410 /* Trail on error implementation */
411 SET_AX( context, GetDriveType16(BL_reg(context)) == DRIVE_UNKNOWN ? 0x0f : 0x01 );
412 SET_CFLAG(context); /* Seems to be set all the time */
416 INT_BARF( context, 0x21 );
421 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
424 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
426 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
432 SET_AL( context, 0xff ); /* failed */
434 TRACE("filename: '%s'\n", filename);
440 if ((*s != ' ') && (*s != '\r') && (*s != '\n'))
449 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
450 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
451 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
453 DOSFS_ToDosFCBFormat(buffer, fcbW);
454 HeapFree(GetProcessHeap(), 0, buffer);
455 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
457 TRACE("FCB: '%s'\n", fcb + 1);
459 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
461 /* point DS:SI to first unparsed character */
462 SET_SI( context, context->Esi + (int)s - (int)filename );
466 /* Many calls translate a drive argument like this:
467 drive number (00h = default, 01h = A:, etc)
469 static char drivestring[]="default";
471 char *INT21_DriveName(int drive)
476 drivestring[0]= (unsigned char)drive + '@';
482 static BOOL INT21_CreateFile( CONTEXT86 *context )
484 SET_AX( context, _lcreat16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
485 context->Edx ), CX_reg(context) ) );
486 return (AX_reg(context) == (WORD)HFILE_ERROR16);
489 static HFILE16 _lcreat16_uniq( LPCSTR path, INT attr )
491 /* Mask off all flags not explicitly allowed by the doc */
492 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
493 return Win32HandleToDosFileHandle( CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
494 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
495 CREATE_NEW, attr, 0 ));
498 static void OpenExistingFile( CONTEXT86 *context )
500 SET_AX( context, _lopen16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
502 if (AX_reg(context) == (WORD)HFILE_ERROR16)
504 SET_AX( context, GetLastError() );
509 static BOOL INT21_ExtendedOpenCreateFile(CONTEXT86 *context )
511 BOOL bExtendedError = FALSE;
512 BYTE action = DL_reg(context);
514 /* Shuffle arguments to call OpenExistingFile */
515 SET_AL( context, BL_reg(context) );
516 SET_DX( context, SI_reg(context) );
517 /* BX,CX and DX should be preserved */
518 OpenExistingFile(context);
520 if ((context->EFlags & 0x0001) == 0) /* File exists */
522 UINT16 uReturnCX = 0;
524 /* Now decide what do do */
526 if ((action & 0x07) == 0)
528 _lclose16( AX_reg(context) );
529 SET_AX( context, 0x0050 ); /*File exists*/
531 WARN("extended open/create: failed because file exists \n");
533 else if ((action & 0x07) == 2)
535 /* Truncate it, but first check if opened for write */
536 if ((BL_reg(context) & 0x0007)== 0)
538 _lclose16( AX_reg(context) );
539 WARN("extended open/create: failed, trunc on ro file\n");
540 SET_AX( context, 0x000C ); /*Access code invalid*/
545 TRACE("extended open/create: Closing before truncate\n");
546 if (_lclose16( AX_reg(context) ))
548 WARN("extended open/create: close before trunc failed\n");
549 SET_AX( context, 0x0019 ); /*Seek Error*/
550 SET_CX( context, 0 );
553 /* Shuffle arguments to call CreateFile */
555 TRACE("extended open/create: Truncating\n");
556 SET_AL( context, BL_reg(context) );
557 /* CX is still the same */
558 SET_DX( context, SI_reg(context) );
559 bExtendedError = INT21_CreateFile(context);
561 if (context->EFlags & 0x0001) /*no file open, flags set */
563 WARN("extended open/create: trunc failed\n");
564 return bExtendedError;
569 else uReturnCX = 0x1;
571 SET_CX( context, uReturnCX );
573 else /* file does not exist */
575 RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
576 if ((action & 0xF0)== 0)
578 SET_CX( context, 0 );
580 WARN("extended open/create: failed, file dosen't exist\n");
584 /* Shuffle arguments to call CreateFile */
585 TRACE("extended open/create: Creating\n");
586 SET_AL( context, BL_reg(context) );
587 /* CX should still be the same */
588 SET_DX( context, SI_reg(context) );
589 bExtendedError = INT21_CreateFile(context);
590 if (context->EFlags & 0x0001) /*no file open, flags set */
592 WARN("extended open/create: create failed\n");
593 return bExtendedError;
595 SET_CX( context, 2 );
599 return bExtendedError;
603 static BOOL INT21_ChangeDir( CONTEXT86 *context )
606 char *dirname = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
607 WCHAR dirnameW[MAX_PATH];
609 TRACE("changedir %s\n", dirname);
610 if (dirname[0] && (dirname[1] == ':'))
612 drive = toupper(dirname[0]) - 'A';
615 else drive = DRIVE_GetCurrentDrive();
616 MultiByteToWideChar(CP_OEMCP, 0, dirname, -1, dirnameW, MAX_PATH);
617 return DRIVE_Chdir( drive, dirnameW );
621 static int INT21_FindFirst( CONTEXT86 *context )
625 DOS_FULL_NAME full_name;
626 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
627 WCHAR pathW[MAX_PATH];
630 path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
631 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
633 dta->unixPath = NULL;
634 if (!DOSFS_GetFullName( pathW, FALSE, &full_name ))
636 SET_AX( context, GetLastError() );
640 dta->unixPath = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.long_name)+1 );
641 strcpy( dta->unixPath, full_name.long_name );
642 p = strrchr( dta->unixPath, '/' );
645 MultiByteToWideChar(CP_OEMCP, 0, p + 1, -1, pathW, MAX_PATH);
647 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
648 * (doesn't matter as it is set below anyway)
650 if (!DOSFS_ToDosFCBFormat( pathW, maskW ))
652 HeapFree( GetProcessHeap(), 0, dta->unixPath );
653 dta->unixPath = NULL;
654 SetLastError( ERROR_FILE_NOT_FOUND );
655 SET_AX( context, ERROR_FILE_NOT_FOUND );
659 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
660 dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
661 : DRIVE_GetCurrentDrive();
663 dta->search_attr = CL_reg(context);
668 static int INT21_FindNext( CONTEXT86 *context )
670 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
671 WIN32_FIND_DATAA entry;
674 if (!dta->unixPath) return 0;
675 if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
676 dta->search_attr, dta->count, &entry )))
678 HeapFree( GetProcessHeap(), 0, dta->unixPath );
679 dta->unixPath = NULL;
682 if ((int)dta->count + count > 0xffff)
684 WARN("Too many directory entries in %s\n", dta->unixPath );
685 HeapFree( GetProcessHeap(), 0, dta->unixPath );
686 dta->unixPath = NULL;
690 dta->fileattr = entry.dwFileAttributes;
691 dta->filesize = entry.nFileSizeLow;
692 FileTimeToDosDateTime( &entry.ftLastWriteTime,
693 &dta->filedate, &dta->filetime );
694 strcpy( dta->filename, entry.cAlternateFileName );
695 if (!memchr(dta->mask,'?',11)) {
696 /* wildcardless search, release resources in case no findnext will
697 * be issued, and as a workaround in case file creation messes up
698 * findnext, as sometimes happens with pkunzip */
699 HeapFree( GetProcessHeap(), 0, dta->unixPath );
700 dta->unixPath = NULL;
706 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
708 static int counter = 0;
709 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
710 char *p = name + strlen(name);
712 /* despite what Ralf Brown says, some programs seem to call without
713 * ending backslash (DOS accepts that, so we accept it too) */
714 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
718 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
719 counter = (counter + 1) % 1000;
721 if ((SET_AX( context, _lcreat16_uniq( name, 0 ))) != (WORD)HFILE_ERROR16)
723 TRACE("created %s\n", name );
726 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
731 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context )
733 int drive = DOS_GET_DRIVE( DL_reg(context) );
734 char *ptr = (char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
736 if (!DRIVE_IsValid(drive))
738 SetLastError( ERROR_INVALID_DRIVE );
741 WideCharToMultiByte(CP_OEMCP, 0, DRIVE_GetDosCwd(drive), -1, ptr, 64, NULL, NULL);
742 ptr[63] = 0; /* ensure 0 termination */
743 SET_AX( context, 0x0100 ); /* success return code */
748 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
750 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
751 int drive = DOS_GET_DRIVE( BL_reg(context) );
753 if (!DRIVE_IsValid(drive))
755 SetLastError( ERROR_INVALID_DRIVE );
759 *(WORD *)dataptr = 0;
760 *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
761 memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
762 strncpy(dataptr + 0x11, "FAT16 ", 8);
767 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
769 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
770 int drive = DOS_GET_DRIVE( BL_reg(context) );
772 if (!DRIVE_IsValid(drive))
774 SetLastError( ERROR_INVALID_DRIVE );
778 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
783 /* microsoft's programmers should be shot for using CP/M style int21
784 calls in Windows for Workgroup's winfile.exe */
786 static int INT21_FindFirstFCB( CONTEXT86 *context )
788 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
793 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
794 else pFCB = (FINDFILE_FCB *)fcb;
795 drive = DOS_GET_DRIVE( pFCB->drive );
796 if (!DRIVE_IsValid( drive )) return 0;
797 root = DRIVE_GetRoot( drive );
798 cwd = DRIVE_GetUnixCwd( drive );
799 pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
800 strlen(root)+strlen(cwd)+2 );
801 if (!pFCB->unixPath) return 0;
802 strcpy( pFCB->unixPath, root );
803 strcat( pFCB->unixPath, "/" );
804 strcat( pFCB->unixPath, cwd );
810 static int INT21_FindNextFCB( CONTEXT86 *context )
812 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
814 DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA(context);
815 WIN32_FIND_DATAA entry;
819 if (*fcb == 0xff) /* extended FCB ? */
822 pFCB = (FINDFILE_FCB *)(fcb + 7);
827 pFCB = (FINDFILE_FCB *)fcb;
830 if (!pFCB->unixPath) return 0;
831 if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
832 DOS_GET_DRIVE( pFCB->drive ), attr,
833 pFCB->count, &entry )))
835 HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
836 pFCB->unixPath = NULL;
839 pFCB->count += count;
841 if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
842 *(BYTE *)pResult = 0xff;
843 (BYTE *)pResult +=6; /* leave reserved field behind */
844 *(BYTE *)pResult = entry.dwFileAttributes;
847 *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
849 pResult->fileattr = entry.dwFileAttributes;
850 pResult->cluster = 0; /* what else? */
851 pResult->filesize = entry.nFileSizeLow;
852 memset( pResult->reserved, 0, sizeof(pResult->reserved) );
853 FileTimeToDosDateTime( &entry.ftLastWriteTime,
854 &pResult->filedate, &pResult->filetime );
856 /* Convert file name to FCB format */
858 memset( pResult->filename, ' ', sizeof(pResult->filename) );
859 if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
860 else if (!strcmp( entry.cAlternateFileName, ".." ))
861 pResult->filename[0] = pResult->filename[1] = '.';
864 char *p = strrchr( entry.cAlternateFileName, '.' );
865 if (p && p[1] && (p != entry.cAlternateFileName))
867 memcpy( pResult->filename, entry.cAlternateFileName,
868 min( (p - entry.cAlternateFileName), 8 ) );
869 memcpy( pResult->filename + 8, p + 1, min( strlen(p), 3 ) );
872 memcpy( pResult->filename, entry.cAlternateFileName,
873 min( strlen(entry.cAlternateFileName), 8 ) );
880 INT21_networkfunc (CONTEXT86 *context)
882 switch (AL_reg(context)) {
883 case 0x00: /* Get machine name. */
885 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
886 TRACE("getting machine name to %p\n", dst);
887 if (gethostname (dst, 15))
890 SetLastError( ER_NoNetwork );
893 int len = strlen (dst);
897 SET_CH( context, 1 ); /* Valid */
898 SET_CL( context, 1 ); /* NETbios number??? */
899 TRACE("returning %s\n", debugstr_an (dst, 16));
905 SetLastError( ER_NoNetwork );
911 static void ASPI_DOS_HandleInt( CONTEXT86 *context )
913 if (!Dosvm.ASPIHandler && !DPMI_LoadDosSystem())
915 ERR("could not setup ASPI handler\n");
918 Dosvm.ASPIHandler( context );
922 /***********************************************************************
925 void WINAPI INT_Int21Handler( CONTEXT86 *context )
927 BOOL bSetDOSExtendedError = FALSE;
929 switch(AH_reg(context))
931 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
932 TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
933 context->SegDs,DX_reg(context) );
935 LPSTR data = CTX_SEG_OFF_TO_LIN(context,context->SegDs,context->Edx);
937 /* do NOT use strchr() to calculate the string length,
938 as '\0' is valid string content, too !
939 Maybe we should check for non-'$' strings, but DOS doesn't. */
940 while (*p != '$') p++;
941 _hwrite16( 1, data, (int)p - (int)data);
942 SET_AL( context, '$' ); /* yes, '$' (0x24) gets returned in AL */
946 case 0x0e: /* SELECT DEFAULT DRIVE */
947 TRACE("SELECT DEFAULT DRIVE %d\n", DL_reg(context));
948 DRIVE_SetCurrentDrive( DL_reg(context) );
949 SET_AL( context, MAX_DOS_DRIVES );
952 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
953 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
954 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
955 if (!INT21_FindFirstFCB(context))
957 SET_AL( context, 0xff );
960 /* else fall through */
962 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
963 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
966 case 0x19: /* GET CURRENT DEFAULT DRIVE */
967 SET_AL( context, DRIVE_GetCurrentDrive() );
970 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
972 TDB *pTask = TASK_GetCurrent();
973 pTask->dta = MAKESEGPTR(context->SegDs,DX_reg(context));
974 TRACE("Set DTA: %08lx\n", pTask->dta);
978 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
979 SET_DL( context, 0 );
980 if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
983 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
984 if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
987 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
988 GetDrivePB(context, DRIVE_GetCurrentDrive());
991 case 0x29: /* PARSE FILENAME INTO FCB */
992 INT21_ParseFileNameIntoFCB(context);
995 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
996 TRACE("GET DISK TRANSFER AREA ADDRESS\n");
998 TDB *pTask = TASK_GetCurrent();
999 context->SegEs = SELECTOROF( pTask->dta );
1000 SET_BX( context, OFFSETOF( pTask->dta ) );
1004 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1005 TRACE("GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
1006 INT21_DriveName( DL_reg(context)));
1007 GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
1010 case 0x33: /* MULTIPLEXED */
1011 switch (AL_reg(context))
1013 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1014 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
1015 INT21_ReadConfigSys();
1016 SET_DL( context, DOSCONF_config.brk_flag );
1019 case 0x01: /* SET EXTENDED BREAK STATE */
1020 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
1021 INT21_ReadConfigSys();
1022 DOSCONF_config.brk_flag = (DL_reg(context) > 0);
1025 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1026 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
1027 INT21_ReadConfigSys();
1028 /* ugly coding in order to stay reentrant */
1029 if (DL_reg(context))
1031 SET_DL( context, DOSCONF_config.brk_flag );
1032 DOSCONF_config.brk_flag = 1;
1036 SET_DL( context, DOSCONF_config.brk_flag );
1037 DOSCONF_config.brk_flag = 0;
1041 case 0x05: /* GET BOOT DRIVE */
1042 TRACE("GET BOOT DRIVE\n");
1043 SET_DL( context, 3 );
1044 /* c: is Wine's bootdrive (a: is 1)*/
1047 case 0x06: /* GET TRUE VERSION NUMBER */
1048 TRACE("GET TRUE VERSION NUMBER\n");
1049 SET_BX( context, (HIWORD(GetVersion16() >> 8)) | (HIWORD(GetVersion16() << 8)) );
1050 SET_DX( context, 0x00 );
1054 INT_BARF( context, 0x21 );
1059 case 0x36: /* GET FREE DISK SPACE */
1060 TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
1061 INT21_DriveName( DL_reg(context)));
1062 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
1067 unsigned char switchchar='/';
1068 switch (AL_reg(context))
1070 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
1071 TRACE("SWITCHAR - GET SWITCH CHARACTER\n");
1072 SET_AL( context, 0x00 ); /* success*/
1073 SET_DL( context, switchchar );
1075 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
1076 TRACE("SWITCHAR - SET SWITCH CHARACTER\n");
1077 switchchar = DL_reg(context);
1078 SET_AL( context, 0x00 ); /* success*/
1080 default: /*"AVAILDEV" - SPECIFY \DEV\ PREFIX USE*/
1081 INT_BARF( context, 0x21 );
1087 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1089 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1090 bSetDOSExtendedError = (!CreateDirectory16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1091 context->Edx ), NULL));
1092 /* FIXME: CreateDirectory's LastErrors will clash with the ones
1093 * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1094 * denied), while CreateDirectory return several ones. remap some of
1097 if (bSetDOSExtendedError) {
1098 switch (GetLastError()) {
1099 case ERROR_ALREADY_EXISTS:
1100 case ERROR_FILENAME_EXCED_RANGE:
1101 case ERROR_DISK_FULL:
1102 SetLastError(ERROR_ACCESS_DENIED);
1109 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1111 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1112 bSetDOSExtendedError = (!RemoveDirectory16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1116 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1118 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1119 bSetDOSExtendedError = !INT21_ChangeDir(context);
1122 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1123 TRACE("CREAT flag 0x%02x %s\n",CX_reg(context),
1124 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1125 bSetDOSExtendedError = INT21_CreateFile( context );
1128 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1129 TRACE("OPEN mode 0x%02x %s\n",AL_reg(context),
1130 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1131 OpenExistingFile(context);
1134 case 0x3e: /* "CLOSE" - CLOSE FILE */
1135 TRACE("CLOSE handle %d\n",BX_reg(context));
1136 SET_AX( context, _lclose16( BX_reg(context) ));
1137 bSetDOSExtendedError = (AX_reg(context) != 0);
1140 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1141 TRACE("READ from %d to %04lX:%04X for %d byte\n",BX_reg(context),
1142 context->SegDs,DX_reg(context),CX_reg(context) );
1146 result = _hread16( BX_reg(context),
1147 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1151 result = WIN16_hread( BX_reg(context),
1152 MAKESEGPTR( context->SegDs, context->Edx ),
1154 if (result == -1) bSetDOSExtendedError = TRUE;
1155 else SET_AX( context, (WORD)result );
1159 case 0x41: /* "UNLINK" - DELETE FILE */
1160 TRACE("UNLINK %s\n",
1161 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1162 bSetDOSExtendedError = (!DeleteFileA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1166 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1167 TRACE("LSEEK handle %d offset %ld from %s\n",
1168 BX_reg(context), MAKELONG(DX_reg(context),CX_reg(context)),
1169 (AL_reg(context)==0)?"start of file":(AL_reg(context)==1)?
1170 "current file position":"end of file");
1172 LONG status = _llseek16( BX_reg(context),
1173 MAKELONG(DX_reg(context),CX_reg(context)),
1175 if (status == -1) bSetDOSExtendedError = TRUE;
1178 SET_AX( context, LOWORD(status) );
1179 SET_DX( context, HIWORD(status) );
1184 case 0x43: /* FILE ATTRIBUTES */
1185 switch (AL_reg(context))
1188 TRACE("GET FILE ATTRIBUTES for %s\n",
1189 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1190 SET_AX( context, GetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1192 if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1193 else SET_CX( context, AX_reg(context) );
1197 TRACE("SET FILE ATTRIBUTES 0x%02x for %s\n", CX_reg(context),
1198 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1199 bSetDOSExtendedError =
1200 (!SetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1205 FIXME("GET COMPRESSED FILE SIZE for %s stub\n",
1206 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1210 case 0x44: /* IOCTL */
1211 switch (AL_reg(context))
1214 ioctlGetDeviceInfo(context);
1220 const DOS_DEVICE *dev;
1221 static const WCHAR scsimgrW[] = {'S','C','S','I','M','G','R','$',0};
1222 if ((dev = DOSFS_GetDeviceByHandle( DosFileHandleToWin32Handle(BX_reg(context)) )) &&
1223 !strcmpiW( dev->name, scsimgrW ))
1225 ASPI_DOS_HandleInt(context);
1229 case 0x05:{ /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1230 /*BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);*/
1231 int drive = DOS_GET_DRIVE(BL_reg(context));
1233 FIXME("program tried to write to block device control channel of drive %d:\n",drive);
1234 /* for (i=0;i<CX_reg(context);i++)
1235 fprintf(stdnimp,"%02x ",dataptr[i]);
1236 fprintf(stdnimp,"\n");*/
1237 SET_AX( context, context->Ecx );
1240 case 0x08: /* Check if drive is removable. */
1241 TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOVABLE for drive %s\n",
1242 INT21_DriveName( BL_reg(context)));
1243 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1246 SetLastError( ERROR_INVALID_DRIVE );
1247 SET_AX( context, ERROR_INVALID_DRIVE );
1250 case DRIVE_REMOVABLE:
1251 SET_AX( context, 0 ); /* removable */
1254 SET_AX( context, 1 ); /* not removable */
1259 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
1260 TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOTE for drive %s\n",
1261 INT21_DriveName( BL_reg(context)));
1262 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1265 SetLastError( ERROR_INVALID_DRIVE );
1266 SET_AX( context, ERROR_INVALID_DRIVE );
1270 SET_DX( context, (1<<9) | (1<<12) ); /* remote */
1273 SET_DX( context, 0 ); /* FIXME: use driver attr here */
1278 case 0x0a: /* check if handle (BX) is remote */
1279 TRACE("IOCTL - CHECK IF HANDLE %d IS REMOTE\n",BX_reg(context));
1280 /* returns DX, bit 15 set if remote, bit 14 set if date/time
1283 SET_DX( context, 0 );
1287 TRACE("IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
1288 INT21_DriveName( BL_reg(context)));
1289 bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1292 case 0x0e: /* get logical drive mapping */
1293 TRACE("IOCTL - GET LOGICAL DRIVE MAP for drive %s\n",
1294 INT21_DriveName( BL_reg(context)));
1295 SET_AL( context, 0 ); /* drive has no mapping - FIXME: may be wrong*/
1298 case 0x0F: /* Set logical drive mapping */
1301 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
1302 INT21_DriveName( BL_reg(context)));
1303 drive = DOS_GET_DRIVE ( BL_reg(context) );
1304 if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
1307 SET_AX( context, 0x000F ); /* invalid drive */
1312 case 0xe0: /* Sun PC-NFS API */
1316 case 0x52: /* DR-DOS version */
1317 /* This is not DR-DOS */
1319 TRACE("GET DR-DOS VERSION requested\n");
1321 SET_AX( context, 0x0001 ); /* Invalid function */
1322 SET_CFLAG(context); /* Error */
1323 SetLastError( ERROR_INVALID_FUNCTION );
1327 INT_BARF( context, 0x21 );
1332 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1333 TRACE("FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
1334 BX_reg(context),CX_reg(context));
1335 bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR16);
1338 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1339 TRACE("CWD - GET CURRENT DIRECTORY for drive %s\n",
1340 INT21_DriveName( DL_reg(context)));
1341 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1344 case 0x4a: /* RESIZE MEMORY BLOCK */
1345 TRACE("RESIZE MEMORY segment %04lX to %d paragraphs\n", context->SegEs, BX_reg(context));
1346 if (!ISV86(context))
1347 FIXME("RESIZE MEMORY probably insufficient implementation. Expect crash soon\n");
1349 LPVOID *mem = DOSMEM_ResizeBlock(DOSMEM_MapDosToLinear(context->SegEs<<4),
1350 BX_reg(context)<<4,NULL);
1352 SET_AX( context, DOSMEM_MapLinearToDos(mem)>>4 );
1355 SET_AX( context, 0x0008 ); /* insufficient memory */
1356 SET_BX( context, DOSMEM_Available()>>4 ); /* not quite right */
1361 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1362 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
1363 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1364 if (!INT21_FindFirst(context)) break;
1367 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1368 TRACE("FINDNEXT\n");
1369 if (!INT21_FindNext(context))
1371 SetLastError( ERROR_NO_MORE_FILES );
1372 SET_AX( context, ERROR_NO_MORE_FILES );
1375 else SET_AX( context, 0 ); /* OK */
1378 case 0x56: /* "RENAME" - RENAME FILE */
1379 TRACE("RENAME %s to %s\n",
1380 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1381 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi));
1382 bSetDOSExtendedError =
1383 (!MoveFileA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1384 CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi)));
1387 case 0x5a: /* CREATE TEMPORARY FILE */
1388 TRACE("CREATE TEMPORARY FILE\n");
1389 bSetDOSExtendedError = !INT21_CreateTempFile(context);
1392 case 0x5b: /* CREATE NEW FILE */
1393 TRACE("CREATE NEW FILE 0x%02x for %s\n", CX_reg(context),
1394 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1396 _lcreat16_uniq( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1398 bSetDOSExtendedError = (AX_reg(context) != 0);
1402 bSetDOSExtendedError = INT21_networkfunc (context);
1405 case 0x5f: /* NETWORK */
1406 switch (AL_reg(context))
1408 case 0x07: /* ENABLE DRIVE */
1409 TRACE("ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1410 if (!DRIVE_Enable( DL_reg(context) ))
1412 SetLastError( ERROR_INVALID_DRIVE );
1413 bSetDOSExtendedError = TRUE;
1417 case 0x08: /* DISABLE DRIVE */
1418 TRACE("DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1419 if (!DRIVE_Disable( DL_reg(context) ))
1421 SetLastError( ERROR_INVALID_DRIVE );
1422 bSetDOSExtendedError = TRUE;
1427 /* network software not installed */
1428 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
1429 SetLastError( ER_NoNetwork );
1430 bSetDOSExtendedError = TRUE;
1435 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1436 TRACE("TRUENAME %s\n",
1437 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
1439 if (!GetFullPathNameA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1441 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1442 context->Edi),NULL))
1443 bSetDOSExtendedError = TRUE;
1444 else SET_AX( context, 0 );
1448 case 0x69: /* DISK SERIAL NUMBER */
1449 switch (AL_reg(context))
1452 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
1453 INT21_DriveName(BL_reg(context)));
1454 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1455 else SET_AX( context, 0 );
1459 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
1460 INT21_DriveName(BL_reg(context)));
1461 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1462 else SET_AX( context, 1 );
1467 case 0x6C: /* Extended Open/Create*/
1468 TRACE("EXTENDED OPEN/CREATE %s\n",
1469 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edi));
1470 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1473 case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
1474 if ((GetVersion()&0xC0000004)!=0xC0000004) {
1475 /* not supported on anything but Win95 */
1476 TRACE("LONG FILENAME functions supported only by win95\n");
1478 SET_AL( context, 0 );
1480 switch(AL_reg(context))
1482 case 0x39: /* Create directory */
1483 TRACE("LONG FILENAME - MAKE DIRECTORY %s\n",
1484 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
1485 bSetDOSExtendedError = (!CreateDirectoryA(
1486 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1487 context->Edx ), NULL));
1488 /* FIXME: CreateDirectory's LastErrors will clash with the ones
1489 * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1490 * denied), while CreateDirectory return several ones. remap some of
1493 if (bSetDOSExtendedError) {
1494 switch (GetLastError()) {
1495 case ERROR_ALREADY_EXISTS:
1496 case ERROR_FILENAME_EXCED_RANGE:
1497 case ERROR_DISK_FULL:
1498 SetLastError(ERROR_ACCESS_DENIED);
1504 case 0x3a: /* Remove directory */
1505 TRACE("LONG FILENAME - REMOVE DIRECTORY %s\n",
1506 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
1507 bSetDOSExtendedError = (!RemoveDirectoryA(
1508 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1511 case 0x43: /* Get/Set file attributes */
1512 TRACE("LONG FILENAME -EXTENDED GET/SET FILE ATTRIBUTES %s\n",
1513 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
1514 switch (BL_reg(context))
1516 case 0x00: /* Get file attributes */
1517 TRACE("\tretrieve attributes\n");
1518 SET_CX( context, GetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1520 if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1523 TRACE("\tset attributes 0x%04x\n",CX_reg(context));
1524 bSetDOSExtendedError = (!SetFileAttributesA(
1525 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1527 CX_reg(context) ) );
1530 FIXME("Unimplemented long file name function:\n");
1531 INT_BARF( context, 0x21 );
1533 SET_AL( context, 0 );
1537 case 0x47: /* Get current directory */
1538 TRACE(" LONG FILENAME - GET CURRENT DIRECTORY for drive %s\n",
1539 INT21_DriveName(DL_reg(context)));
1540 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1543 case 0x4e: /* Find first file */
1544 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
1545 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
1546 /* FIXME: use attributes in CX */
1547 if ((SET_AX( context, FindFirstFile16(
1548 CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1549 (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1551 == INVALID_HANDLE_VALUE16)
1552 bSetDOSExtendedError = TRUE;
1554 case 0x4f: /* Find next file */
1555 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
1557 if (!FindNextFile16( BX_reg(context),
1558 (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1560 bSetDOSExtendedError = TRUE;
1564 LPCSTR driveroot = (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
1565 LPSTR buffer = (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi);
1566 DWORD filename_len, flags;
1568 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", driveroot);
1569 SET_AX( context, 0 );
1570 if (!GetVolumeInformationA( driveroot, NULL, 0, NULL, &filename_len,
1571 &flags, buffer, 8 ))
1573 INT_BARF( context, 0x21 );
1577 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
1578 SET_CX( context, filename_len );
1579 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
1582 case 0xa1: /* Find close */
1583 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
1585 bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
1588 switch(CL_reg(context))
1590 case 0x01: /* Get short filename or path */
1591 if (!GetShortPathNameA
1592 ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1594 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1596 bSetDOSExtendedError = TRUE;
1597 else SET_AX( context, 0 );
1599 case 0x02: /* Get canonical long filename or path */
1600 if (!GetFullPathNameA
1601 ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1603 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1604 context->Edi),NULL))
1605 bSetDOSExtendedError = TRUE;
1606 else SET_AX( context, 0 );
1609 FIXME("Unimplemented long file name function:\n");
1610 INT_BARF( context, 0x21 );
1612 SET_AL( context, 0 );
1616 case 0x6c: /* Create or open file */
1617 TRACE("LONG FILENAME - CREATE OR OPEN FILE %s\n",
1618 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi));
1619 /* translate Dos 7 action to Dos 6 action */
1620 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1623 case 0x3b: /* Change directory */
1624 TRACE("LONG FILENAME - CHANGE DIRECTORY %s\n",
1625 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1626 if (!SetCurrentDirectoryA(CTX_SEG_OFF_TO_LIN(context,
1632 SET_AL( context, GetLastError() );
1635 case 0x41: /* Delete file */
1636 TRACE("LONG FILENAME - DELETE FILE %s\n",
1637 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1638 if (!DeleteFileA(CTX_SEG_OFF_TO_LIN(context,
1643 SET_AL( context, GetLastError() );
1646 case 0x56: /* Move (rename) file */
1648 LPCSTR fn1 = (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
1649 LPCSTR fn2 = (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
1650 TRACE("LONG FILENAME - RENAME FILE %s to %s\n", fn1, fn2);
1651 if (!MoveFileA(fn1, fn2))
1654 SET_AL( context, GetLastError() );
1659 FIXME("Unimplemented long file name function:\n");
1660 INT_BARF( context, 0x21 );
1662 SET_AL( context, 0 );
1668 case 0x73: /* MULTIPLEXED: Win95 OSR2/Win98 FAT32 calls */
1669 TRACE("windows95 function AX %04x\n",
1672 switch (AL_reg(context))
1674 case 0x02: /* Get Extended Drive Parameter Block for specific drive */
1675 /* ES:DI points to word with length of data (should be 0x3d) */
1679 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
1680 char root[] = "A:\\";
1682 buffer = (WORD *)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
1684 TRACE("Get Extended DPB: linear buffer address is %p\n", buffer);
1686 /* validate passed-in buffer lengths */
1687 if ((*buffer != 0x3d) || (context->Ecx != 0x3f))
1689 WARN("Get Extended DPB: buffer lengths incorrect\n");
1690 WARN("CX = %lx, buffer[0] = %x\n", context->Ecx, *buffer);
1692 SET_AL( context, 0x18 ); /* bad buffer length */
1695 /* buffer checks out */
1696 buffer++; /* skip over length word now */
1697 if (FillInDrivePB( DX_reg(context) ) )
1699 edpb = (struct EDPB *)buffer;
1701 /* copy down the old-style DPB portion first */
1702 memcpy(&edpb->dpb, &heap->dpb, sizeof(struct DPB));
1704 /* now fill in the extended entries */
1705 edpb->edpb_flags = 0;
1706 edpb->next_edpb = 0;
1707 edpb->free_cluster = edpb->free_cluster2 = 0;
1709 /* determine free disk space */
1710 *root += DOS_GET_DRIVE( DX_reg(context) );
1711 GetDiskFreeSpaceA( root, &cluster_sectors, §or_bytes,
1712 &free_clusters, &total_clusters );
1714 edpb->clusters_free = (free_clusters&0xffff);
1716 edpb->clusters_free_hi = free_clusters >> 16;
1717 edpb->mirroring_flags = 0;
1718 edpb->info_sector = 0xffff;
1719 edpb->spare_boot_sector = 0xffff;
1720 edpb->first_cluster = 0;
1721 edpb->max_cluster = total_clusters;
1722 edpb->fat_clusters = 32; /* made-up value */
1723 edpb->root_cluster = 0;
1725 RESET_CFLAG(context); /* clear carry */
1726 SET_AX( context, 0 );
1730 SET_AX( context, 0x00ff );
1736 case 0x03: /* Get Extended free space on drive */
1737 case 0x04: /* Set DPB for formatting */
1738 case 0x05: /* extended absolute disk read/write */
1739 FIXME("Unimplemented FAT32 int32 function %04x\n", AX_reg(context));
1741 SET_AL( context, 0 );
1748 INT_BARF( context, 0x21 );
1751 } /* END OF SWITCH */
1753 if( bSetDOSExtendedError ) /* set general error condition */
1755 SET_AX( context, GetLastError() );