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 BOOL INT21_CreateHeap(void)
138 if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
140 WARN("Out of memory\n");
143 heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
144 dpbsegptr = MAKESEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
145 strcpy(heap->biosdate, "01/01/80");
149 static BYTE *GetCurrentDTA( CONTEXT86 *context )
151 TDB *pTask = TASK_GetCurrent();
153 /* FIXME: This assumes DTA was set correctly! */
154 return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
155 (DWORD)OFFSETOF(pTask->dta) );
159 void CreateBPB(int drive, BYTE *data, BOOL16 limited)
160 /* limited == TRUE is used with INT 0x21/0x440d */
165 setword(&data[3], 0);
167 setword(&data[6], 240);
168 setword(&data[8], 64000);
170 setword(&data[0x0b], 40);
171 setword(&data[0x0d], 56);
172 setword(&data[0x0f], 2);
173 setword(&data[0x11], 0);
175 setword(&data[0x1f], 800);
177 setword(&data[0x22], 1);
179 } else { /* 1.44mb */
182 setword(&data[3], 0);
184 setword(&data[6], 240);
185 setword(&data[8], 2880);
187 setword(&data[0x0b], 6);
188 setword(&data[0x0d], 18);
189 setword(&data[0x0f], 2);
190 setword(&data[0x11], 0);
192 setword(&data[0x1f], 80);
194 setword(&data[0x22], 2);
199 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
201 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
202 char root[] = "A:\\";
204 *root += DOS_GET_DRIVE( DL_reg(context) );
205 if (!GetDiskFreeSpaceA( root, &cluster_sectors, §or_bytes,
206 &free_clusters, &total_clusters )) return 0;
207 SET_AX( context, cluster_sectors );
208 SET_BX( context, free_clusters );
209 SET_CX( context, sector_bytes );
210 SET_DX( context, total_clusters );
214 static int INT21_GetDriveAllocInfo( CONTEXT86 *context )
216 if (!INT21_GetFreeDiskSpace( context )) return 0;
217 if (!heap && !INT21_CreateHeap()) return 0;
218 heap->mediaID = 0xf0;
219 context->SegDs = DosHeapHandle;
220 SET_BX( context, (int)&heap->mediaID - (int)heap );
224 static int FillInDrivePB( int drive )
226 if(!DRIVE_IsValid(drive))
228 SetLastError( ERROR_INVALID_DRIVE );
231 else if (heap || INT21_CreateHeap())
233 /* FIXME: I have no idea what a lot of this information should
234 * say or whether it even really matters since we're not allowing
235 * direct block access. However, some programs seem to depend on
236 * getting at least _something_ back from here. The 'next' pointer
237 * does worry me, though. Should we have a complete table of
238 * separate DPBs per drive? Probably, but I'm lazy. :-) -CH
240 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
241 heap->dpb.sector_size = 512;
242 heap->dpb.high_sector = 1;
243 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
244 heap->dpb.reserved = 0;
245 heap->dpb.num_FAT = 1;
246 heap->dpb.dir_entries = 2;
247 heap->dpb.first_data = 2;
248 heap->dpb.high_cluster = 64000;
249 heap->dpb.sectors_in_FAT = 1;
250 heap->dpb.start_dir = 1;
251 heap->dpb.driver_head = 0;
252 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
253 heap->dpb.access_flag = 0;
255 heap->dpb.free_search = 0;
256 heap->dpb.free_clusters = 0xFFFF; /* unknown */
263 static void GetDrivePB( CONTEXT86 *context, int drive )
265 if (FillInDrivePB( drive ))
267 SET_AL( context, 0x00 );
268 context->SegDs = SELECTOROF(dpbsegptr);
269 SET_BX( context, OFFSETOF(dpbsegptr) );
273 SET_AX( context, 0x00ff );
278 static BOOL ioctlGenericBlkDevReq( CONTEXT86 *context )
280 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
281 int drive = DOS_GET_DRIVE( BL_reg(context) );
283 if (!DRIVE_IsValid(drive))
285 SetLastError( ERROR_FILE_NOT_FOUND );
289 if (CH_reg(context) != 0x08)
291 INT_BARF( context, 0x21 );
295 switch (CL_reg(context))
297 case 0x60: /* get device parameters */
298 /* used by w4wgrp's winfile */
299 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
301 dataptr[6] = 0; /* media type */
304 dataptr[1] = 0x05; /* fixed disk */
305 setword(&dataptr[2], 0x01); /* non removable */
306 setword(&dataptr[4], 0x300); /* # of cylinders */
310 dataptr[1] = 0x07; /* block dev, floppy */
311 setword(&dataptr[2], 0x02); /* removable */
312 setword(&dataptr[4], 80); /* # of cylinders */
314 CreateBPB(drive, &dataptr[7], TRUE);
315 RESET_CFLAG(context);
318 case 0x66:/* get disk serial number */
320 char label[12],fsname[9],path[4];
323 strcpy(path,"x:\\");path[0]=drive+'A';
324 GetVolumeInformationA(
325 path,label,12,&serial,NULL,NULL,fsname,9
328 memcpy(dataptr+2,&serial,4);
329 memcpy(dataptr+6,label ,11);
330 memcpy(dataptr+17,fsname,8);
335 memset(dataptr+1, '\0', dataptr[0]-1);
336 dataptr[1] = dataptr[0];
337 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
338 dataptr[3] = 0xFF; /* no physical drive */
342 /* Trail on error implementation */
343 SET_AX( context, GetDriveType16(BL_reg(context)) == DRIVE_UNKNOWN ? 0x0f : 0x01 );
344 SET_CFLAG(context); /* Seems to be set all the time */
348 INT_BARF( context, 0x21 );
353 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
356 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
358 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
364 SET_AL( context, 0xff ); /* failed */
366 TRACE("filename: '%s'\n", filename);
372 if ((*s != ' ') && (*s != '\r') && (*s != '\n'))
381 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
382 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
383 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
385 DOSFS_ToDosFCBFormat(buffer, fcbW);
386 HeapFree(GetProcessHeap(), 0, buffer);
387 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
389 TRACE("FCB: '%s'\n", fcb + 1);
391 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
393 /* point DS:SI to first unparsed character */
394 SET_SI( context, context->Esi + (int)s - (int)filename );
398 /* Many calls translate a drive argument like this:
399 drive number (00h = default, 01h = A:, etc)
401 static char drivestring[]="default";
403 char *INT21_DriveName(int drive)
408 drivestring[0]= (unsigned char)drive + '@';
414 static BOOL INT21_CreateFile( CONTEXT86 *context )
416 SET_AX( context, _lcreat16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
417 context->Edx ), CX_reg(context) ) );
418 return (AX_reg(context) == (WORD)HFILE_ERROR16);
421 static HFILE16 _lcreat16_uniq( LPCSTR path, INT attr )
423 /* Mask off all flags not explicitly allowed by the doc */
424 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
425 return Win32HandleToDosFileHandle( CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
426 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
427 CREATE_NEW, attr, 0 ));
430 static void OpenExistingFile( CONTEXT86 *context )
432 SET_AX( context, _lopen16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
434 if (AX_reg(context) == (WORD)HFILE_ERROR16)
436 SET_AX( context, GetLastError() );
441 static BOOL INT21_ExtendedOpenCreateFile(CONTEXT86 *context )
443 BOOL bExtendedError = FALSE;
444 BYTE action = DL_reg(context);
446 /* Shuffle arguments to call OpenExistingFile */
447 SET_AL( context, BL_reg(context) );
448 SET_DX( context, SI_reg(context) );
449 /* BX,CX and DX should be preserved */
450 OpenExistingFile(context);
452 if ((context->EFlags & 0x0001) == 0) /* File exists */
454 UINT16 uReturnCX = 0;
456 /* Now decide what do do */
458 if ((action & 0x07) == 0)
460 _lclose16( AX_reg(context) );
461 SET_AX( context, 0x0050 ); /*File exists*/
463 WARN("extended open/create: failed because file exists \n");
465 else if ((action & 0x07) == 2)
467 /* Truncate it, but first check if opened for write */
468 if ((BL_reg(context) & 0x0007)== 0)
470 _lclose16( AX_reg(context) );
471 WARN("extended open/create: failed, trunc on ro file\n");
472 SET_AX( context, 0x000C ); /*Access code invalid*/
477 TRACE("extended open/create: Closing before truncate\n");
478 if (_lclose16( AX_reg(context) ))
480 WARN("extended open/create: close before trunc failed\n");
481 SET_AX( context, 0x0019 ); /*Seek Error*/
482 SET_CX( context, 0 );
485 /* Shuffle arguments to call CreateFile */
487 TRACE("extended open/create: Truncating\n");
488 SET_AL( context, BL_reg(context) );
489 /* CX is still the same */
490 SET_DX( context, SI_reg(context) );
491 bExtendedError = INT21_CreateFile(context);
493 if (context->EFlags & 0x0001) /*no file open, flags set */
495 WARN("extended open/create: trunc failed\n");
496 return bExtendedError;
501 else uReturnCX = 0x1;
503 SET_CX( context, uReturnCX );
505 else /* file does not exist */
507 RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
508 if ((action & 0xF0)== 0)
510 SET_CX( context, 0 );
512 WARN("extended open/create: failed, file dosen't exist\n");
516 /* Shuffle arguments to call CreateFile */
517 TRACE("extended open/create: Creating\n");
518 SET_AL( context, BL_reg(context) );
519 /* CX should still be the same */
520 SET_DX( context, SI_reg(context) );
521 bExtendedError = INT21_CreateFile(context);
522 if (context->EFlags & 0x0001) /*no file open, flags set */
524 WARN("extended open/create: create failed\n");
525 return bExtendedError;
527 SET_CX( context, 2 );
531 return bExtendedError;
535 static BOOL INT21_ChangeDir( CONTEXT86 *context )
538 char *dirname = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
539 WCHAR dirnameW[MAX_PATH];
541 TRACE("changedir %s\n", dirname);
542 if (dirname[0] && (dirname[1] == ':'))
544 drive = toupper(dirname[0]) - 'A';
547 else drive = DRIVE_GetCurrentDrive();
548 MultiByteToWideChar(CP_OEMCP, 0, dirname, -1, dirnameW, MAX_PATH);
549 return DRIVE_Chdir( drive, dirnameW );
553 static int INT21_FindFirst( CONTEXT86 *context )
557 DOS_FULL_NAME full_name;
558 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
559 WCHAR pathW[MAX_PATH];
562 path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
563 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
565 dta->unixPath = NULL;
566 if (!DOSFS_GetFullName( pathW, FALSE, &full_name ))
568 SET_AX( context, GetLastError() );
572 dta->unixPath = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.long_name)+1 );
573 strcpy( dta->unixPath, full_name.long_name );
574 p = strrchr( dta->unixPath, '/' );
577 MultiByteToWideChar(CP_OEMCP, 0, p + 1, -1, pathW, MAX_PATH);
579 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
580 * (doesn't matter as it is set below anyway)
582 if (!DOSFS_ToDosFCBFormat( pathW, maskW ))
584 HeapFree( GetProcessHeap(), 0, dta->unixPath );
585 dta->unixPath = NULL;
586 SetLastError( ERROR_FILE_NOT_FOUND );
587 SET_AX( context, ERROR_FILE_NOT_FOUND );
591 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
592 dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
593 : DRIVE_GetCurrentDrive();
595 dta->search_attr = CL_reg(context);
600 static int INT21_FindNext( CONTEXT86 *context )
602 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
603 WIN32_FIND_DATAA entry;
606 if (!dta->unixPath) return 0;
607 if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
608 dta->search_attr, dta->count, &entry )))
610 HeapFree( GetProcessHeap(), 0, dta->unixPath );
611 dta->unixPath = NULL;
614 if ((int)dta->count + count > 0xffff)
616 WARN("Too many directory entries in %s\n", dta->unixPath );
617 HeapFree( GetProcessHeap(), 0, dta->unixPath );
618 dta->unixPath = NULL;
622 dta->fileattr = entry.dwFileAttributes;
623 dta->filesize = entry.nFileSizeLow;
624 FileTimeToDosDateTime( &entry.ftLastWriteTime,
625 &dta->filedate, &dta->filetime );
626 strcpy( dta->filename, entry.cAlternateFileName );
627 if (!memchr(dta->mask,'?',11)) {
628 /* wildcardless search, release resources in case no findnext will
629 * be issued, and as a workaround in case file creation messes up
630 * findnext, as sometimes happens with pkunzip */
631 HeapFree( GetProcessHeap(), 0, dta->unixPath );
632 dta->unixPath = NULL;
638 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
640 static int counter = 0;
641 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
642 char *p = name + strlen(name);
644 /* despite what Ralf Brown says, some programs seem to call without
645 * ending backslash (DOS accepts that, so we accept it too) */
646 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
650 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
651 counter = (counter + 1) % 1000;
653 SET_AX( context, _lcreat16_uniq( name, 0 ) );
654 if (AX_reg(context) != HFILE_ERROR16)
656 TRACE("created %s\n", name );
659 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
664 static BOOL INT21_GetCurrentDirectory( CONTEXT86 *context )
666 int drive = DOS_GET_DRIVE( DL_reg(context) );
667 char *ptr = (char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
669 if (!DRIVE_IsValid(drive))
671 SetLastError( ERROR_INVALID_DRIVE );
674 WideCharToMultiByte(CP_OEMCP, 0, DRIVE_GetDosCwd(drive), -1, ptr, 64, NULL, NULL);
675 ptr[63] = 0; /* ensure 0 termination */
676 SET_AX( context, 0x0100 ); /* success return code */
681 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
683 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
684 int drive = DOS_GET_DRIVE( BL_reg(context) );
686 if (!DRIVE_IsValid(drive))
688 SetLastError( ERROR_INVALID_DRIVE );
692 *(WORD *)dataptr = 0;
693 *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
694 memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
695 strncpy(dataptr + 0x11, "FAT16 ", 8);
700 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
702 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
703 int drive = DOS_GET_DRIVE( BL_reg(context) );
705 if (!DRIVE_IsValid(drive))
707 SetLastError( ERROR_INVALID_DRIVE );
711 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
716 /* microsoft's programmers should be shot for using CP/M style int21
717 calls in Windows for Workgroup's winfile.exe */
719 static int INT21_FindFirstFCB( CONTEXT86 *context )
721 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
726 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
727 else pFCB = (FINDFILE_FCB *)fcb;
728 drive = DOS_GET_DRIVE( pFCB->drive );
729 if (!DRIVE_IsValid( drive )) return 0;
730 root = DRIVE_GetRoot( drive );
731 cwd = DRIVE_GetUnixCwd( drive );
732 pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
733 strlen(root)+strlen(cwd)+2 );
734 if (!pFCB->unixPath) return 0;
735 strcpy( pFCB->unixPath, root );
736 strcat( pFCB->unixPath, "/" );
737 strcat( pFCB->unixPath, cwd );
743 static int INT21_FindNextFCB( CONTEXT86 *context )
745 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
747 DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA(context);
748 WIN32_FIND_DATAA entry;
752 if (*fcb == 0xff) /* extended FCB ? */
755 pFCB = (FINDFILE_FCB *)(fcb + 7);
760 pFCB = (FINDFILE_FCB *)fcb;
763 if (!pFCB->unixPath) return 0;
764 if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
765 DOS_GET_DRIVE( pFCB->drive ), attr,
766 pFCB->count, &entry )))
768 HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
769 pFCB->unixPath = NULL;
772 pFCB->count += count;
774 if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
775 *(BYTE *)pResult = 0xff;
776 (BYTE *)pResult +=6; /* leave reserved field behind */
777 *(BYTE *)pResult = entry.dwFileAttributes;
780 *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
782 pResult->fileattr = entry.dwFileAttributes;
783 pResult->cluster = 0; /* what else? */
784 pResult->filesize = entry.nFileSizeLow;
785 memset( pResult->reserved, 0, sizeof(pResult->reserved) );
786 FileTimeToDosDateTime( &entry.ftLastWriteTime,
787 &pResult->filedate, &pResult->filetime );
789 /* Convert file name to FCB format */
791 memset( pResult->filename, ' ', sizeof(pResult->filename) );
792 if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
793 else if (!strcmp( entry.cAlternateFileName, ".." ))
794 pResult->filename[0] = pResult->filename[1] = '.';
797 char *p = strrchr( entry.cAlternateFileName, '.' );
798 if (p && p[1] && (p != entry.cAlternateFileName))
800 memcpy( pResult->filename, entry.cAlternateFileName,
801 min( (p - entry.cAlternateFileName), 8 ) );
802 memcpy( pResult->filename + 8, p + 1, min( strlen(p), 3 ) );
805 memcpy( pResult->filename, entry.cAlternateFileName,
806 min( strlen(entry.cAlternateFileName), 8 ) );
813 INT21_networkfunc (CONTEXT86 *context)
815 switch (AL_reg(context)) {
816 case 0x00: /* Get machine name. */
818 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
819 TRACE("getting machine name to %p\n", dst);
820 if (gethostname (dst, 15))
823 SetLastError( ER_NoNetwork );
826 int len = strlen (dst);
830 SET_CH( context, 1 ); /* Valid */
831 SET_CL( context, 1 ); /* NETbios number??? */
832 TRACE("returning %s\n", debugstr_an (dst, 16));
838 SetLastError( ER_NoNetwork );
844 /***********************************************************************
847 void WINAPI INT_Int21Handler( CONTEXT86 *context )
849 BOOL bSetDOSExtendedError = FALSE;
851 switch(AH_reg(context))
853 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
854 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
855 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
856 if (!INT21_FindFirstFCB(context))
858 SET_AL( context, 0xff );
861 /* else fall through */
863 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
864 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
867 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
868 SET_DL( context, 0 );
869 if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
872 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
873 if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
876 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
877 GetDrivePB(context, DRIVE_GetCurrentDrive());
880 case 0x29: /* PARSE FILENAME INTO FCB */
881 INT21_ParseFileNameIntoFCB(context);
884 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
885 TRACE("GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
886 INT21_DriveName( DL_reg(context)));
887 GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
890 case 0x36: /* GET FREE DISK SPACE */
891 TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
892 INT21_DriveName( DL_reg(context)));
893 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
896 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
898 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
899 bSetDOSExtendedError = !INT21_ChangeDir(context);
902 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
903 TRACE("CREAT flag 0x%02x %s\n",CX_reg(context),
904 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
905 bSetDOSExtendedError = INT21_CreateFile( context );
908 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
909 TRACE("OPEN mode 0x%02x %s\n",AL_reg(context),
910 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
911 OpenExistingFile(context);
914 case 0x44: /* IOCTL */
915 switch (AL_reg(context))
918 TRACE("IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
919 INT21_DriveName( BL_reg(context)));
920 bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
923 case 0x0F: /* Set logical drive mapping */
926 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
927 INT21_DriveName( BL_reg(context)));
928 drive = DOS_GET_DRIVE ( BL_reg(context) );
929 if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
932 SET_AX( context, 0x000F ); /* invalid drive */
939 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
940 TRACE("CWD - GET CURRENT DIRECTORY for drive %s\n",
941 INT21_DriveName( DL_reg(context)));
942 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
945 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
946 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
947 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
948 if (!INT21_FindFirst(context)) break;
951 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
953 if (!INT21_FindNext(context))
955 SetLastError( ERROR_NO_MORE_FILES );
956 SET_AX( context, ERROR_NO_MORE_FILES );
959 else SET_AX( context, 0 ); /* OK */
962 case 0x5a: /* CREATE TEMPORARY FILE */
963 TRACE("CREATE TEMPORARY FILE\n");
964 bSetDOSExtendedError = !INT21_CreateTempFile(context);
967 case 0x5b: /* CREATE NEW FILE */
968 TRACE("CREATE NEW FILE 0x%02x for %s\n", CX_reg(context),
969 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
971 _lcreat16_uniq( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
973 bSetDOSExtendedError = (AX_reg(context) != 0);
977 bSetDOSExtendedError = INT21_networkfunc (context);
980 case 0x5f: /* NETWORK */
981 switch (AL_reg(context))
983 case 0x07: /* ENABLE DRIVE */
984 TRACE("ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
985 if (!DRIVE_Enable( DL_reg(context) ))
987 SetLastError( ERROR_INVALID_DRIVE );
988 bSetDOSExtendedError = TRUE;
992 case 0x08: /* DISABLE DRIVE */
993 TRACE("DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
994 if (!DRIVE_Disable( DL_reg(context) ))
996 SetLastError( ERROR_INVALID_DRIVE );
997 bSetDOSExtendedError = TRUE;
1002 /* network software not installed */
1003 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
1004 SetLastError( ER_NoNetwork );
1005 bSetDOSExtendedError = TRUE;
1010 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1011 TRACE("TRUENAME %s\n",
1012 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
1014 if (!GetFullPathNameA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1016 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1017 context->Edi),NULL))
1018 bSetDOSExtendedError = TRUE;
1019 else SET_AX( context, 0 );
1023 case 0x69: /* DISK SERIAL NUMBER */
1024 switch (AL_reg(context))
1027 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
1028 INT21_DriveName(BL_reg(context)));
1029 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1030 else SET_AX( context, 0 );
1034 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
1035 INT21_DriveName(BL_reg(context)));
1036 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1037 else SET_AX( context, 1 );
1042 case 0x6C: /* Extended Open/Create*/
1043 TRACE("EXTENDED OPEN/CREATE %s\n",
1044 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edi));
1045 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1048 case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
1049 switch(AL_reg(context))
1051 case 0x47: /* Get current directory */
1052 TRACE(" LONG FILENAME - GET CURRENT DIRECTORY for drive %s\n",
1053 INT21_DriveName(DL_reg(context)));
1054 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1057 case 0x4e: /* Find first file */
1058 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
1059 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
1060 /* FIXME: use attributes in CX */
1061 SET_AX( context, FindFirstFile16(
1062 CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1063 (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1065 if (AX_reg(context) == INVALID_HANDLE_VALUE16)
1066 bSetDOSExtendedError = TRUE;
1068 case 0x4f: /* Find next file */
1069 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
1071 if (!FindNextFile16( BX_reg(context),
1072 (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1074 bSetDOSExtendedError = TRUE;
1078 LPCSTR driveroot = (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
1079 LPSTR buffer = (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi);
1080 DWORD filename_len, flags;
1082 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", driveroot);
1083 SET_AX( context, 0 );
1084 if (!GetVolumeInformationA( driveroot, NULL, 0, NULL, &filename_len,
1085 &flags, buffer, 8 ))
1087 INT_BARF( context, 0x21 );
1091 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
1092 SET_CX( context, filename_len );
1093 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
1096 case 0xa1: /* Find close */
1097 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
1099 bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
1102 switch(CL_reg(context))
1104 case 0x01: /* Get short filename or path */
1105 if (!GetShortPathNameA
1106 ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1108 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1110 bSetDOSExtendedError = TRUE;
1111 else SET_AX( context, 0 );
1113 case 0x02: /* Get canonical long filename or path */
1114 if (!GetFullPathNameA
1115 ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1117 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1118 context->Edi),NULL))
1119 bSetDOSExtendedError = TRUE;
1120 else SET_AX( context, 0 );
1123 FIXME("Unimplemented long file name function:\n");
1124 INT_BARF( context, 0x21 );
1126 SET_AL( context, 0 );
1130 case 0x6c: /* Create or open file */
1131 TRACE("LONG FILENAME - CREATE OR OPEN FILE %s\n",
1132 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi));
1133 /* translate Dos 7 action to Dos 6 action */
1134 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1137 case 0x3b: /* Change directory */
1138 TRACE("LONG FILENAME - CHANGE DIRECTORY %s\n",
1139 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1140 if (!SetCurrentDirectoryA(CTX_SEG_OFF_TO_LIN(context,
1146 SET_AL( context, GetLastError() );
1151 FIXME("Unimplemented long file name function:\n");
1152 INT_BARF( context, 0x21 );
1154 SET_AL( context, 0 );
1160 case 0x73: /* MULTIPLEXED: Win95 OSR2/Win98 FAT32 calls */
1161 TRACE("windows95 function AX %04x\n",
1164 switch (AL_reg(context))
1166 case 0x02: /* Get Extended Drive Parameter Block for specific drive */
1167 /* ES:DI points to word with length of data (should be 0x3d) */
1171 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
1172 char root[] = "A:\\";
1174 buffer = (WORD *)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
1176 TRACE("Get Extended DPB: linear buffer address is %p\n", buffer);
1178 /* validate passed-in buffer lengths */
1179 if ((*buffer != 0x3d) || (context->Ecx != 0x3f))
1181 WARN("Get Extended DPB: buffer lengths incorrect\n");
1182 WARN("CX = %lx, buffer[0] = %x\n", context->Ecx, *buffer);
1184 SET_AL( context, 0x18 ); /* bad buffer length */
1187 /* buffer checks out */
1188 buffer++; /* skip over length word now */
1189 if (FillInDrivePB( DX_reg(context) ) )
1191 edpb = (struct EDPB *)buffer;
1193 /* copy down the old-style DPB portion first */
1194 memcpy(&edpb->dpb, &heap->dpb, sizeof(struct DPB));
1196 /* now fill in the extended entries */
1197 edpb->edpb_flags = 0;
1198 edpb->next_edpb = 0;
1199 edpb->free_cluster = edpb->free_cluster2 = 0;
1201 /* determine free disk space */
1202 *root += DOS_GET_DRIVE( DX_reg(context) );
1203 GetDiskFreeSpaceA( root, &cluster_sectors, §or_bytes,
1204 &free_clusters, &total_clusters );
1206 edpb->clusters_free = (free_clusters&0xffff);
1208 edpb->clusters_free_hi = free_clusters >> 16;
1209 edpb->mirroring_flags = 0;
1210 edpb->info_sector = 0xffff;
1211 edpb->spare_boot_sector = 0xffff;
1212 edpb->first_cluster = 0;
1213 edpb->max_cluster = total_clusters;
1214 edpb->fat_clusters = 32; /* made-up value */
1215 edpb->root_cluster = 0;
1217 RESET_CFLAG(context); /* clear carry */
1218 SET_AX( context, 0 );
1222 SET_AX( context, 0x00ff );
1228 case 0x03: /* Get Extended free space on drive */
1229 case 0x04: /* Set DPB for formatting */
1230 case 0x05: /* extended absolute disk read/write */
1231 FIXME("Unimplemented FAT32 int32 function %04x\n", AX_reg(context));
1233 SET_AL( context, 0 );
1240 INT_BARF( context, 0x21 );
1243 } /* END OF SWITCH */
1245 if( bSetDOSExtendedError ) /* set general error condition */
1247 SET_AX( context, GetLastError() );