Clear a new hardware buffer to proper silence values based on format.
[wine/wine64.git] / msdos / int21.c
blobe4bfd7de2e52d9bc2dec827ac36943b703bbf880
1 /*
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <time.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #ifdef HAVE_SYS_FILE_H
34 # include <sys/file.h>
35 #endif
36 #include <string.h>
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
39 #endif
40 #include <sys/types.h>
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
44 #ifdef HAVE_UTIME_H
45 # include <utime.h>
46 #endif
47 #include <ctype.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winreg.h"
51 #include "winternl.h"
52 #include "wingdi.h"
53 #include "winuser.h" /* SW_NORMAL */
54 #include "wine/winbase16.h"
55 #include "winerror.h"
56 #include "drive.h"
57 #include "file.h"
58 #include "callback.h"
59 #include "msdos.h"
60 #include "miscemu.h"
61 #include "task.h"
62 #include "wine/unicode.h"
63 #include "wine/debug.h"
65 WINE_DEFAULT_DEBUG_CHANNEL(int21);
66 #if defined(__svr4__) || defined(_SCO_DS)
67 /* SVR4 DOESNT do locking the same way must implement properly */
68 #define LOCK_EX 0
69 #define LOCK_SH 1
70 #define LOCK_NB 8
71 #endif
74 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
76 /* Define the drive parameter block, as used by int21/1F
77 * and int21/32. This table can be accessed through the
78 * global 'dpb' pointer, which points into the local dos
79 * heap.
81 struct DPB
83 BYTE drive_num; /* 0=A, etc. */
84 BYTE unit_num; /* Drive's unit number (?) */
85 WORD sector_size; /* Sector size in bytes */
86 BYTE high_sector; /* Highest sector in a cluster */
87 BYTE shift; /* Shift count (?) */
88 WORD reserved; /* Number of reserved sectors at start */
89 BYTE num_FAT; /* Number of FATs */
90 WORD dir_entries; /* Number of root dir entries */
91 WORD first_data; /* First data sector */
92 WORD high_cluster; /* Highest cluster number */
93 WORD sectors_in_FAT; /* Number of sectors per FAT */
94 WORD start_dir; /* Starting sector of first dir */
95 DWORD driver_head; /* Address of device driver header (?) */
96 BYTE media_ID; /* Media ID */
97 BYTE access_flag; /* Prev. accessed flag (0=yes,0xFF=no) */
98 DWORD next; /* Pointer to next DPB in list */
99 WORD free_search; /* Free cluster search start */
100 WORD free_clusters; /* Number of free clusters (0xFFFF=unknown) */
103 struct EDPB /* FAT32 extended Drive Parameter Block */
104 { /* from Ralf Brown's Interrupt List */
105 struct DPB dpb; /* first 24 bytes = original DPB */
107 BYTE edpb_flags; /* undocumented/unknown flags */
108 DWORD next_edpb; /* pointer to next EDPB */
109 WORD free_cluster; /* cluster to start search for free space on write, typically
110 the last cluster allocated */
111 WORD clusters_free; /* number of free clusters on drive or FFFF = unknown */
112 WORD clusters_free_hi; /* hiword of clusters_free */
113 WORD mirroring_flags; /* mirroring flags: bit 7 set = do not mirror active FAT */
114 /* bits 0-3 = 0-based number of the active FAT */
115 WORD info_sector; /* sector number of file system info sector, or FFFF for none */
116 WORD spare_boot_sector; /* sector number of backup boot sector, or FFFF for none */
117 DWORD first_cluster; /* sector number of the first cluster */
118 DWORD max_cluster; /* sector number of the last cluster */
119 DWORD fat_clusters; /* number of clusters occupied by FAT */
120 DWORD root_cluster; /* cluster number of start of root directory */
121 DWORD free_cluster2; /* same as free_cluster: cluster at which to start
122 search for free space when writing */
126 DWORD dpbsegptr;
128 struct DosHeap {
129 BYTE mediaID;
130 BYTE biosdate[8];
131 struct DPB dpb;
133 static struct DosHeap *heap;
134 static WORD DosHeapHandle;
136 extern char TempDirectory[];
138 static BOOL INT21_CreateHeap(void)
140 if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
142 WARN("Out of memory\n");
143 return FALSE;
145 heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
146 dpbsegptr = MAKESEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
147 strcpy(heap->biosdate, "01/01/80");
148 return TRUE;
151 static BYTE *GetCurrentDTA( CONTEXT86 *context )
153 TDB *pTask = GlobalLock16(GetCurrentTask());
155 /* FIXME: This assumes DTA was set correctly! */
156 return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
157 (DWORD)OFFSETOF(pTask->dta) );
161 static void CreateBPB(int drive, BYTE *data, BOOL16 limited)
162 /* limited == TRUE is used with INT 0x21/0x440d */
164 if (drive > 1) {
165 setword(data, 512);
166 data[2] = 2;
167 setword(&data[3], 0);
168 data[5] = 2;
169 setword(&data[6], 240);
170 setword(&data[8], 64000);
171 data[0x0a] = 0xf8;
172 setword(&data[0x0b], 40);
173 setword(&data[0x0d], 56);
174 setword(&data[0x0f], 2);
175 setword(&data[0x11], 0);
176 if (!limited) {
177 setword(&data[0x1f], 800);
178 data[0x21] = 5;
179 setword(&data[0x22], 1);
181 } else { /* 1.44mb */
182 setword(data, 512);
183 data[2] = 2;
184 setword(&data[3], 0);
185 data[5] = 2;
186 setword(&data[6], 240);
187 setword(&data[8], 2880);
188 data[0x0a] = 0xf8;
189 setword(&data[0x0b], 6);
190 setword(&data[0x0d], 18);
191 setword(&data[0x0f], 2);
192 setword(&data[0x11], 0);
193 if (!limited) {
194 setword(&data[0x1f], 80);
195 data[0x21] = 7;
196 setword(&data[0x22], 2);
201 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
203 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
204 char root[] = "A:\\";
206 *root += DOS_GET_DRIVE( DL_reg(context) );
207 if (!GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
208 &free_clusters, &total_clusters )) return 0;
209 SET_AX( context, cluster_sectors );
210 SET_BX( context, free_clusters );
211 SET_CX( context, sector_bytes );
212 SET_DX( context, total_clusters );
213 return 1;
216 static int INT21_GetDriveAllocInfo( CONTEXT86 *context )
218 if (!INT21_GetFreeDiskSpace( context )) return 0;
219 if (!heap && !INT21_CreateHeap()) return 0;
220 heap->mediaID = 0xf0;
221 context->SegDs = DosHeapHandle;
222 SET_BX( context, (int)&heap->mediaID - (int)heap );
223 return 1;
226 static int FillInDrivePB( int drive )
228 if(!DRIVE_IsValid(drive))
230 SetLastError( ERROR_INVALID_DRIVE );
231 return 0;
233 else if (heap || INT21_CreateHeap())
235 /* FIXME: I have no idea what a lot of this information should
236 * say or whether it even really matters since we're not allowing
237 * direct block access. However, some programs seem to depend on
238 * getting at least _something_ back from here. The 'next' pointer
239 * does worry me, though. Should we have a complete table of
240 * separate DPBs per drive? Probably, but I'm lazy. :-) -CH
242 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
243 heap->dpb.sector_size = 512;
244 heap->dpb.high_sector = 1;
245 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
246 heap->dpb.reserved = 0;
247 heap->dpb.num_FAT = 1;
248 heap->dpb.dir_entries = 2;
249 heap->dpb.first_data = 2;
250 heap->dpb.high_cluster = 64000;
251 heap->dpb.sectors_in_FAT = 1;
252 heap->dpb.start_dir = 1;
253 heap->dpb.driver_head = 0;
254 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
255 heap->dpb.access_flag = 0;
256 heap->dpb.next = 0;
257 heap->dpb.free_search = 0;
258 heap->dpb.free_clusters = 0xFFFF; /* unknown */
259 return 1;
262 return 0;
265 static void GetDrivePB( CONTEXT86 *context, int drive )
267 if (FillInDrivePB( drive ))
269 SET_AL( context, 0x00 );
270 context->SegDs = SELECTOROF(dpbsegptr);
271 SET_BX( context, OFFSETOF(dpbsegptr) );
273 else
275 SET_AX( context, 0x00ff );
280 static BOOL ioctlGenericBlkDevReq( CONTEXT86 *context )
282 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
283 int drive = DOS_GET_DRIVE( BL_reg(context) );
285 if (!DRIVE_IsValid(drive))
287 SetLastError( ERROR_FILE_NOT_FOUND );
288 return TRUE;
291 if (CH_reg(context) != 0x08)
293 INT_BARF( context, 0x21 );
294 return FALSE;
297 switch (CL_reg(context))
299 case 0x60: /* get device parameters */
300 /* used by w4wgrp's winfile */
301 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
302 dataptr[0] = 0x04;
303 dataptr[6] = 0; /* media type */
304 if (drive > 1)
306 dataptr[1] = 0x05; /* fixed disk */
307 setword(&dataptr[2], 0x01); /* non removable */
308 setword(&dataptr[4], 0x300); /* # of cylinders */
310 else
312 dataptr[1] = 0x07; /* block dev, floppy */
313 setword(&dataptr[2], 0x02); /* removable */
314 setword(&dataptr[4], 80); /* # of cylinders */
316 CreateBPB(drive, &dataptr[7], TRUE);
317 RESET_CFLAG(context);
318 break;
320 case 0x66:/* get disk serial number */
322 char label[12],fsname[9],path[4];
323 DWORD serial;
325 strcpy(path,"x:\\");path[0]=drive+'A';
326 GetVolumeInformationA(
327 path,label,12,&serial,NULL,NULL,fsname,9
329 *(WORD*)dataptr = 0;
330 memcpy(dataptr+2,&serial,4);
331 memcpy(dataptr+6,label ,11);
332 memcpy(dataptr+17,fsname,8);
334 break;
336 case 0x6f:
337 memset(dataptr+1, '\0', dataptr[0]-1);
338 dataptr[1] = dataptr[0];
339 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
340 dataptr[3] = 0xFF; /* no physical drive */
341 break;
343 case 0x72:
344 /* Trail on error implementation */
345 SET_AX( context, GetDriveType16(BL_reg(context)) == DRIVE_UNKNOWN ? 0x0f : 0x01 );
346 SET_CFLAG(context); /* Seems to be set all the time */
347 break;
349 default:
350 INT_BARF( context, 0x21 );
352 return FALSE;
355 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
357 char *filename =
358 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
359 char *fcb =
360 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
361 char *s;
362 WCHAR *buffer;
363 WCHAR fcbW[12];
364 INT buffer_len, len;
366 SET_AL( context, 0xff ); /* failed */
368 TRACE("filename: '%s'\n", filename);
370 s = filename;
371 len = 0;
372 while (*s)
374 if ((*s != ' ') && (*s != '\r') && (*s != '\n'))
376 s++;
377 len++;
379 else
380 break;
383 buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
384 buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
385 len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
386 buffer[len] = 0;
387 DOSFS_ToDosFCBFormat(buffer, fcbW);
388 HeapFree(GetProcessHeap(), 0, buffer);
389 WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
390 *fcb = 0;
391 TRACE("FCB: '%s'\n", fcb + 1);
393 SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
395 /* point DS:SI to first unparsed character */
396 SET_SI( context, context->Esi + (int)s - (int)filename );
400 /* Many calls translate a drive argument like this:
401 drive number (00h = default, 01h = A:, etc)
403 static const char *INT21_DriveName(int drive)
405 if (drive > 0) return wine_dbg_sprintf( "%c:", 'A' + drive - 1 );
406 return "default";
409 static HFILE16 _lcreat16_uniq( LPCSTR path, INT attr )
411 /* Mask off all flags not explicitly allowed by the doc */
412 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
413 return Win32HandleToDosFileHandle( CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
414 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
415 CREATE_NEW, attr, 0 ));
419 static int INT21_FindFirst( CONTEXT86 *context )
421 char *p;
422 const char *path;
423 DOS_FULL_NAME full_name;
424 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
425 WCHAR pathW[MAX_PATH];
426 WCHAR maskW[12];
428 path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
429 MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
431 dta->unixPath = NULL;
432 if (!DOSFS_GetFullName( pathW, FALSE, &full_name ))
434 SET_AX( context, GetLastError() );
435 SET_CFLAG(context);
436 return 0;
438 dta->unixPath = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.long_name)+1 );
439 strcpy( dta->unixPath, full_name.long_name );
440 p = strrchr( dta->unixPath, '/' );
441 *p = '\0';
443 MultiByteToWideChar(CP_OEMCP, 0, p + 1, -1, pathW, MAX_PATH);
445 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
446 * (doesn't matter as it is set below anyway)
448 if (!DOSFS_ToDosFCBFormat( pathW, maskW ))
450 HeapFree( GetProcessHeap(), 0, dta->unixPath );
451 dta->unixPath = NULL;
452 SetLastError( ERROR_FILE_NOT_FOUND );
453 SET_AX( context, ERROR_FILE_NOT_FOUND );
454 SET_CFLAG(context);
455 return 0;
457 WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
458 dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
459 : DRIVE_GetCurrentDrive();
460 dta->count = 0;
461 dta->search_attr = CL_reg(context);
462 return 1;
466 static int INT21_FindNext( CONTEXT86 *context )
468 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
469 WIN32_FIND_DATAA entry;
470 int count;
472 if (!dta->unixPath) return 0;
473 if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
474 dta->search_attr, dta->count, &entry )))
476 HeapFree( GetProcessHeap(), 0, dta->unixPath );
477 dta->unixPath = NULL;
478 return 0;
480 if ((int)dta->count + count > 0xffff)
482 WARN("Too many directory entries in %s\n", dta->unixPath );
483 HeapFree( GetProcessHeap(), 0, dta->unixPath );
484 dta->unixPath = NULL;
485 return 0;
487 dta->count += count;
488 dta->fileattr = entry.dwFileAttributes;
489 dta->filesize = entry.nFileSizeLow;
490 FileTimeToDosDateTime( &entry.ftLastWriteTime,
491 &dta->filedate, &dta->filetime );
492 strcpy( dta->filename, entry.cAlternateFileName );
493 if (!memchr(dta->mask,'?',11)) {
494 /* wildcardless search, release resources in case no findnext will
495 * be issued, and as a workaround in case file creation messes up
496 * findnext, as sometimes happens with pkunzip */
497 HeapFree( GetProcessHeap(), 0, dta->unixPath );
498 dta->unixPath = NULL;
500 return 1;
504 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
506 static int counter = 0;
507 char *name = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx );
508 char *p = name + strlen(name);
510 /* despite what Ralf Brown says, some programs seem to call without
511 * ending backslash (DOS accepts that, so we accept it too) */
512 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
514 for (;;)
516 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
517 counter = (counter + 1) % 1000;
519 SET_AX( context, _lcreat16_uniq( name, 0 ) );
520 if (AX_reg(context) != HFILE_ERROR16)
522 TRACE("created %s\n", name );
523 return TRUE;
525 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
530 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
532 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
533 int drive = DOS_GET_DRIVE( BL_reg(context) );
535 if (!DRIVE_IsValid(drive))
537 SetLastError( ERROR_INVALID_DRIVE );
538 return 0;
541 *(WORD *)dataptr = 0;
542 *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
543 memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
544 strncpy(dataptr + 0x11, "FAT16 ", 8);
545 return 1;
549 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
551 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
552 int drive = DOS_GET_DRIVE( BL_reg(context) );
554 if (!DRIVE_IsValid(drive))
556 SetLastError( ERROR_INVALID_DRIVE );
557 return 0;
560 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
561 return 1;
565 /* microsoft's programmers should be shot for using CP/M style int21
566 calls in Windows for Workgroup's winfile.exe */
568 static int INT21_FindFirstFCB( CONTEXT86 *context )
570 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
571 FINDFILE_FCB *pFCB;
572 LPCSTR root, cwd;
573 int drive;
575 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
576 else pFCB = (FINDFILE_FCB *)fcb;
577 drive = DOS_GET_DRIVE( pFCB->drive );
578 if (!DRIVE_IsValid( drive )) return 0;
579 root = DRIVE_GetRoot( drive );
580 cwd = DRIVE_GetUnixCwd( drive );
581 pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
582 strlen(root)+strlen(cwd)+2 );
583 if (!pFCB->unixPath) return 0;
584 strcpy( pFCB->unixPath, root );
585 strcat( pFCB->unixPath, "/" );
586 strcat( pFCB->unixPath, cwd );
587 pFCB->count = 0;
588 return 1;
592 static int INT21_FindNextFCB( CONTEXT86 *context )
594 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
595 FINDFILE_FCB *pFCB;
596 DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA(context);
597 WIN32_FIND_DATAA entry;
598 BYTE attr;
599 int count;
601 if (*fcb == 0xff) /* extended FCB ? */
603 attr = fcb[6];
604 pFCB = (FINDFILE_FCB *)(fcb + 7);
606 else
608 attr = 0;
609 pFCB = (FINDFILE_FCB *)fcb;
612 if (!pFCB->unixPath) return 0;
613 if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
614 DOS_GET_DRIVE( pFCB->drive ), attr,
615 pFCB->count, &entry )))
617 HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
618 pFCB->unixPath = NULL;
619 return 0;
621 pFCB->count += count;
623 if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
624 *(BYTE *)pResult = 0xff;
625 (BYTE *)pResult +=6; /* leave reserved field behind */
626 *(BYTE *)pResult = entry.dwFileAttributes;
627 ((BYTE *)pResult)++;
629 *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
630 ((BYTE *)pResult)++;
631 pResult->fileattr = entry.dwFileAttributes;
632 pResult->cluster = 0; /* what else? */
633 pResult->filesize = entry.nFileSizeLow;
634 memset( pResult->reserved, 0, sizeof(pResult->reserved) );
635 FileTimeToDosDateTime( &entry.ftLastWriteTime,
636 &pResult->filedate, &pResult->filetime );
638 /* Convert file name to FCB format */
640 memset( pResult->filename, ' ', sizeof(pResult->filename) );
641 if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
642 else if (!strcmp( entry.cAlternateFileName, ".." ))
643 pResult->filename[0] = pResult->filename[1] = '.';
644 else
646 char *p = strrchr( entry.cAlternateFileName, '.' );
647 if (p && p[1] && (p != entry.cAlternateFileName))
649 memcpy( pResult->filename, entry.cAlternateFileName,
650 min( (p - entry.cAlternateFileName), 8 ) );
651 memcpy( pResult->filename + 8, p + 1, min( strlen(p), 3 ) );
653 else
654 memcpy( pResult->filename, entry.cAlternateFileName,
655 min( strlen(entry.cAlternateFileName), 8 ) );
657 return 1;
661 static BOOL
662 INT21_networkfunc (CONTEXT86 *context)
664 switch (AL_reg(context)) {
665 case 0x00: /* Get machine name. */
667 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
668 TRACE("getting machine name to %p\n", dst);
669 if (gethostname (dst, 15))
671 WARN("failed!\n");
672 SetLastError( ER_NoNetwork );
673 return TRUE;
674 } else {
675 int len = strlen (dst);
676 while (len < 15)
677 dst[len++] = ' ';
678 dst[15] = 0;
679 SET_CH( context, 1 ); /* Valid */
680 SET_CL( context, 1 ); /* NETbios number??? */
681 TRACE("returning %s\n", debugstr_an (dst, 16));
682 return FALSE;
686 default:
687 SetLastError( ER_NoNetwork );
688 return TRUE;
693 /***********************************************************************
694 * INT_Int21Handler
696 void WINAPI INT_Int21Handler( CONTEXT86 *context )
698 BOOL bSetDOSExtendedError = FALSE;
700 switch(AH_reg(context))
702 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
703 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
704 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
705 if (!INT21_FindFirstFCB(context))
707 SET_AL( context, 0xff );
708 break;
710 /* else fall through */
712 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
713 SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
714 break;
716 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
717 SET_DL( context, 0 );
718 if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
719 break;
721 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
722 if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
723 break;
725 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
726 GetDrivePB(context, DRIVE_GetCurrentDrive());
727 break;
729 case 0x29: /* PARSE FILENAME INTO FCB */
730 INT21_ParseFileNameIntoFCB(context);
731 break;
733 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
734 TRACE("GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
735 INT21_DriveName( DL_reg(context)));
736 GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
737 break;
739 case 0x36: /* GET FREE DISK SPACE */
740 TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
741 INT21_DriveName( DL_reg(context)));
742 if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
743 break;
745 case 0x44: /* IOCTL */
746 switch (AL_reg(context))
748 case 0x0d:
749 TRACE("IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
750 INT21_DriveName( BL_reg(context)));
751 bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
752 break;
754 case 0x0F: /* Set logical drive mapping */
756 int drive;
757 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
758 INT21_DriveName( BL_reg(context)));
759 drive = DOS_GET_DRIVE ( BL_reg(context) );
760 if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
762 SET_CFLAG(context);
763 SET_AX( context, 0x000F ); /* invalid drive */
765 break;
768 break;
770 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
771 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
772 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
773 if (!INT21_FindFirst(context)) break;
774 /* fall through */
776 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
777 TRACE("FINDNEXT\n");
778 if (!INT21_FindNext(context))
780 SetLastError( ERROR_NO_MORE_FILES );
781 SET_AX( context, ERROR_NO_MORE_FILES );
782 SET_CFLAG(context);
784 else SET_AX( context, 0 ); /* OK */
785 break;
787 case 0x5a: /* CREATE TEMPORARY FILE */
788 TRACE("CREATE TEMPORARY FILE\n");
789 bSetDOSExtendedError = !INT21_CreateTempFile(context);
790 break;
792 case 0x5e:
793 bSetDOSExtendedError = INT21_networkfunc (context);
794 break;
796 case 0x5f: /* NETWORK */
797 switch (AL_reg(context))
799 case 0x07: /* ENABLE DRIVE */
800 TRACE("ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
801 if (!DRIVE_Enable( DL_reg(context) ))
803 SetLastError( ERROR_INVALID_DRIVE );
804 bSetDOSExtendedError = TRUE;
806 break;
808 case 0x08: /* DISABLE DRIVE */
809 TRACE("DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
810 if (!DRIVE_Disable( DL_reg(context) ))
812 SetLastError( ERROR_INVALID_DRIVE );
813 bSetDOSExtendedError = TRUE;
815 break;
817 default:
818 /* network software not installed */
819 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
820 SetLastError( ER_NoNetwork );
821 bSetDOSExtendedError = TRUE;
822 break;
824 break;
826 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
827 TRACE("TRUENAME %s\n",
828 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
830 if (!GetFullPathNameA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
831 context->Esi), 128,
832 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
833 context->Edi),NULL))
834 bSetDOSExtendedError = TRUE;
835 else SET_AX( context, 0 );
837 break;
839 case 0x69: /* DISK SERIAL NUMBER */
840 switch (AL_reg(context))
842 case 0x00:
843 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
844 INT21_DriveName(BL_reg(context)));
845 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
846 else SET_AX( context, 0 );
847 break;
849 case 0x01:
850 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
851 INT21_DriveName(BL_reg(context)));
852 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
853 else SET_AX( context, 1 );
854 break;
856 break;
858 case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
859 switch(AL_reg(context))
861 case 0x4e: /* Find first file */
862 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
863 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
864 /* FIXME: use attributes in CX */
865 SET_AX( context, FindFirstFile16(
866 CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
867 (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
868 context->Edi)));
869 if (AX_reg(context) == INVALID_HANDLE_VALUE16)
870 bSetDOSExtendedError = TRUE;
871 break;
872 case 0x4f: /* Find next file */
873 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
874 BX_reg(context));
875 if (!FindNextFile16( BX_reg(context),
876 (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
877 context->Edi)))
878 bSetDOSExtendedError = TRUE;
879 break;
880 case 0xa0:
882 LPCSTR driveroot = (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
883 LPSTR buffer = (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi);
884 DWORD filename_len, flags;
886 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", driveroot);
887 SET_AX( context, 0 );
888 if (!GetVolumeInformationA( driveroot, NULL, 0, NULL, &filename_len,
889 &flags, buffer, 8 ))
891 INT_BARF( context, 0x21 );
892 SET_CFLAG(context);
893 break;
895 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
896 SET_CX( context, filename_len );
897 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
899 break;
900 case 0xa1: /* Find close */
901 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
902 BX_reg(context));
903 bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
904 break;
905 case 0x60:
906 switch(CL_reg(context))
908 case 0x01: /* Get short filename or path */
909 if (!GetShortPathNameA
910 ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
911 context->Esi),
912 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
913 context->Edi), 67))
914 bSetDOSExtendedError = TRUE;
915 else SET_AX( context, 0 );
916 break;
917 case 0x02: /* Get canonical long filename or path */
918 if (!GetFullPathNameA
919 ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
920 context->Esi), 128,
921 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
922 context->Edi),NULL))
923 bSetDOSExtendedError = TRUE;
924 else SET_AX( context, 0 );
925 break;
926 default:
927 FIXME("Unimplemented long file name function:\n");
928 INT_BARF( context, 0x21 );
929 SET_CFLAG(context);
930 SET_AL( context, 0 );
931 break;
933 break;
935 default:
936 FIXME("Unimplemented long file name function:\n");
937 INT_BARF( context, 0x21 );
938 SET_CFLAG(context);
939 SET_AL( context, 0 );
940 break;
942 break;
945 case 0x73: /* MULTIPLEXED: Win95 OSR2/Win98 FAT32 calls */
946 TRACE("windows95 function AX %04x\n",
947 AX_reg(context));
949 switch (AL_reg(context))
951 case 0x02: /* Get Extended Drive Parameter Block for specific drive */
952 /* ES:DI points to word with length of data (should be 0x3d) */
954 WORD *buffer;
955 struct EDPB *edpb;
956 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
957 char root[] = "A:\\";
959 buffer = (WORD *)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
961 TRACE("Get Extended DPB: linear buffer address is %p\n", buffer);
963 /* validate passed-in buffer lengths */
964 if ((*buffer != 0x3d) || (context->Ecx != 0x3f))
966 WARN("Get Extended DPB: buffer lengths incorrect\n");
967 WARN("CX = %lx, buffer[0] = %x\n", context->Ecx, *buffer);
968 SET_CFLAG(context);
969 SET_AL( context, 0x18 ); /* bad buffer length */
972 /* buffer checks out */
973 buffer++; /* skip over length word now */
974 if (FillInDrivePB( DX_reg(context) ) )
976 edpb = (struct EDPB *)buffer;
978 /* copy down the old-style DPB portion first */
979 memcpy(&edpb->dpb, &heap->dpb, sizeof(struct DPB));
981 /* now fill in the extended entries */
982 edpb->edpb_flags = 0;
983 edpb->next_edpb = 0;
984 edpb->free_cluster = edpb->free_cluster2 = 0;
986 /* determine free disk space */
987 *root += DOS_GET_DRIVE( DX_reg(context) );
988 GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
989 &free_clusters, &total_clusters );
991 edpb->clusters_free = (free_clusters&0xffff);
993 edpb->clusters_free_hi = free_clusters >> 16;
994 edpb->mirroring_flags = 0;
995 edpb->info_sector = 0xffff;
996 edpb->spare_boot_sector = 0xffff;
997 edpb->first_cluster = 0;
998 edpb->max_cluster = total_clusters;
999 edpb->fat_clusters = 32; /* made-up value */
1000 edpb->root_cluster = 0;
1002 RESET_CFLAG(context); /* clear carry */
1003 SET_AX( context, 0 );
1005 else
1007 SET_AX( context, 0x00ff );
1008 SET_CFLAG(context);
1011 break;
1013 case 0x03: /* Get Extended free space on drive */
1014 case 0x04: /* Set DPB for formatting */
1015 case 0x05: /* extended absolute disk read/write */
1016 FIXME("Unimplemented FAT32 int32 function %04x\n", AX_reg(context));
1017 SET_CFLAG(context);
1018 SET_AL( context, 0 );
1019 break;
1022 break;
1024 default:
1025 INT_BARF( context, 0x21 );
1026 break;
1028 } /* END OF SWITCH */
1030 if( bSetDOSExtendedError ) /* set general error condition */
1032 SET_AX( context, GetLastError() );
1033 SET_CFLAG(context);