Fixed some issues found by winapi_check.
[wine/dcerpc.git] / msdos / int21.c
blob36232074cf25e43774e2e781d59a531ccc77f3d5
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"
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #ifdef HAVE_SYS_FILE_H
33 # include <sys/file.h>
34 #endif
35 #include <string.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <utime.h>
40 #include <ctype.h>
41 #include "windef.h"
42 #include "winbase.h"
43 #include "ntddk.h"
44 #include "wingdi.h"
45 #include "winuser.h" /* SW_NORMAL */
46 #include "wine/winbase16.h"
47 #include "winerror.h"
48 #include "drive.h"
49 #include "file.h"
50 #include "callback.h"
51 #include "msdos.h"
52 #include "options.h"
53 #include "miscemu.h"
54 #include "task.h"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(int21);
58 #if defined(__svr4__) || defined(_SCO_DS)
59 /* SVR4 DOESNT do locking the same way must implement properly */
60 #define LOCK_EX 0
61 #define LOCK_SH 1
62 #define LOCK_NB 8
63 #endif
66 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
68 /* Define the drive parameter block, as used by int21/1F
69 * and int21/32. This table can be accessed through the
70 * global 'dpb' pointer, which points into the local dos
71 * heap.
73 struct DPB
75 BYTE drive_num; /* 0=A, etc. */
76 BYTE unit_num; /* Drive's unit number (?) */
77 WORD sector_size; /* Sector size in bytes */
78 BYTE high_sector; /* Highest sector in a cluster */
79 BYTE shift; /* Shift count (?) */
80 WORD reserved; /* Number of reserved sectors at start */
81 BYTE num_FAT; /* Number of FATs */
82 WORD dir_entries; /* Number of root dir entries */
83 WORD first_data; /* First data sector */
84 WORD high_cluster; /* Highest cluster number */
85 WORD sectors_in_FAT; /* Number of sectors per FAT */
86 WORD start_dir; /* Starting sector of first dir */
87 DWORD driver_head; /* Address of device driver header (?) */
88 BYTE media_ID; /* Media ID */
89 BYTE access_flag; /* Prev. accessed flag (0=yes,0xFF=no) */
90 DWORD next; /* Pointer to next DPB in list */
91 WORD free_search; /* Free cluster search start */
92 WORD free_clusters; /* Number of free clusters (0xFFFF=unknown) */
95 struct EDPB /* FAT32 extended Drive Parameter Block */
96 { /* from Ralf Brown's Interrupt List */
97 struct DPB dpb; /* first 24 bytes = original DPB */
99 BYTE edpb_flags; /* undocumented/unknown flags */
100 DWORD next_edpb; /* pointer to next EDPB */
101 WORD free_cluster; /* cluster to start search for free space on write, typically
102 the last cluster allocated */
103 WORD clusters_free; /* number of free clusters on drive or FFFF = unknown */
104 WORD clusters_free_hi; /* hiword of clusters_free */
105 WORD mirroring_flags; /* mirroring flags: bit 7 set = do not mirror active FAT */
106 /* bits 0-3 = 0-based number of the active FAT */
107 WORD info_sector; /* sector number of file system info sector, or FFFF for none */
108 WORD spare_boot_sector; /* sector number of backup boot sector, or FFFF for none */
109 DWORD first_cluster; /* sector number of the first cluster */
110 DWORD max_cluster; /* sector number of the last cluster */
111 DWORD fat_clusters; /* number of clusters occupied by FAT */
112 DWORD root_cluster; /* cluster number of start of root directory */
113 DWORD free_cluster2; /* same as free_cluster: cluster at which to start
114 search for free space when writing */
118 WORD CodePage = 437;
119 DWORD dpbsegptr;
121 struct DosHeap {
122 BYTE InDosFlag;
123 BYTE mediaID;
124 BYTE biosdate[8];
125 struct DPB dpb;
126 BYTE DummyDBCSLeadTable[6];
128 static struct DosHeap *heap;
129 static WORD DosHeapHandle;
131 extern char TempDirectory[];
133 static void INT21_ReadConfigSys(void)
135 static int done;
136 if (!done) DOSCONF_ReadConfig();
137 done = 1;
140 static BOOL INT21_CreateHeap(void)
142 if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
144 WARN("Out of memory\n");
145 return FALSE;
147 heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
148 dpbsegptr = MAKESEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
149 heap->InDosFlag = 0;
150 strcpy(heap->biosdate, "01/01/80");
151 memset(heap->DummyDBCSLeadTable, 0, 6);
152 return TRUE;
155 static BYTE *GetCurrentDTA( CONTEXT86 *context )
157 TDB *pTask = TASK_GetCurrent();
159 /* FIXME: This assumes DTA was set correctly! */
160 return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
161 (DWORD)OFFSETOF(pTask->dta) );
165 void CreateBPB(int drive, BYTE *data, BOOL16 limited)
166 /* limited == TRUE is used with INT 0x21/0x440d */
168 if (drive > 1) {
169 setword(data, 512);
170 data[2] = 2;
171 setword(&data[3], 0);
172 data[5] = 2;
173 setword(&data[6], 240);
174 setword(&data[8], 64000);
175 data[0x0a] = 0xf8;
176 setword(&data[0x0b], 40);
177 setword(&data[0x0d], 56);
178 setword(&data[0x0f], 2);
179 setword(&data[0x11], 0);
180 if (!limited) {
181 setword(&data[0x1f], 800);
182 data[0x21] = 5;
183 setword(&data[0x22], 1);
185 } else { /* 1.44mb */
186 setword(data, 512);
187 data[2] = 2;
188 setword(&data[3], 0);
189 data[5] = 2;
190 setword(&data[6], 240);
191 setword(&data[8], 2880);
192 data[0x0a] = 0xf8;
193 setword(&data[0x0b], 6);
194 setword(&data[0x0d], 18);
195 setword(&data[0x0f], 2);
196 setword(&data[0x11], 0);
197 if (!limited) {
198 setword(&data[0x1f], 80);
199 data[0x21] = 7;
200 setword(&data[0x22], 2);
205 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
207 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
208 char root[] = "A:\\";
210 *root += DOS_GET_DRIVE( DL_reg(context) );
211 if (!GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
212 &free_clusters, &total_clusters )) return 0;
213 AX_reg(context) = cluster_sectors;
214 BX_reg(context) = free_clusters;
215 CX_reg(context) = sector_bytes;
216 DX_reg(context) = total_clusters;
217 return 1;
220 static int INT21_GetDriveAllocInfo( CONTEXT86 *context )
222 if (!INT21_GetFreeDiskSpace( context )) return 0;
223 if (!heap && !INT21_CreateHeap()) return 0;
224 heap->mediaID = 0xf0;
225 context->SegDs = DosHeapHandle;
226 BX_reg(context) = (int)&heap->mediaID - (int)heap;
227 return 1;
230 static int FillInDrivePB( int drive )
232 if(!DRIVE_IsValid(drive))
234 SetLastError( ERROR_INVALID_DRIVE );
235 return 0;
237 else if (heap || INT21_CreateHeap())
239 /* FIXME: I have no idea what a lot of this information should
240 * say or whether it even really matters since we're not allowing
241 * direct block access. However, some programs seem to depend on
242 * getting at least _something_ back from here. The 'next' pointer
243 * does worry me, though. Should we have a complete table of
244 * separate DPBs per drive? Probably, but I'm lazy. :-) -CH
246 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
247 heap->dpb.sector_size = 512;
248 heap->dpb.high_sector = 1;
249 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
250 heap->dpb.reserved = 0;
251 heap->dpb.num_FAT = 1;
252 heap->dpb.dir_entries = 2;
253 heap->dpb.first_data = 2;
254 heap->dpb.high_cluster = 64000;
255 heap->dpb.sectors_in_FAT = 1;
256 heap->dpb.start_dir = 1;
257 heap->dpb.driver_head = 0;
258 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
259 heap->dpb.access_flag = 0;
260 heap->dpb.next = 0;
261 heap->dpb.free_search = 0;
262 heap->dpb.free_clusters = 0xFFFF; /* unknown */
263 return 1;
266 return 0;
269 static void GetDrivePB( CONTEXT86 *context, int drive )
271 if (FillInDrivePB( drive ))
273 AL_reg(context) = 0x00;
274 context->SegDs = SELECTOROF(dpbsegptr);
275 BX_reg(context) = OFFSETOF(dpbsegptr);
277 else
279 AX_reg(context) = 0x00ff;
284 static void ioctlGetDeviceInfo( CONTEXT86 *context )
286 int curr_drive;
287 const DOS_DEVICE *dev;
289 TRACE("(%d)\n", BX_reg(context));
291 RESET_CFLAG(context);
293 /* DOS device ? */
294 if ((dev = DOSFS_GetDeviceByHandle( DosFileHandleToWin32Handle(BX_reg(context)) )))
296 DX_reg(context) = dev->flags;
297 return;
300 /* it seems to be a file */
301 curr_drive = DRIVE_GetCurrentDrive();
302 DX_reg(context) = 0x0140 + curr_drive + ((curr_drive > 1) ? 0x0800 : 0);
303 /* no floppy */
304 /* bits 0-5 are current drive
305 * bit 6 - file has NOT been written..FIXME: correct?
306 * bit 8 - generate int24 if no diskspace on write/ read past end of file
307 * bit 11 - media not removable
308 * bit 14 - don't set file date/time on closing
309 * bit 15 - file is remote
313 static BOOL ioctlGenericBlkDevReq( CONTEXT86 *context )
315 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
316 int drive = DOS_GET_DRIVE( BL_reg(context) );
318 if (!DRIVE_IsValid(drive))
320 SetLastError( ERROR_FILE_NOT_FOUND );
321 return TRUE;
324 if (CH_reg(context) != 0x08)
326 INT_BARF( context, 0x21 );
327 return FALSE;
330 switch (CL_reg(context))
332 case 0x4a: /* lock logical volume */
333 TRACE("lock logical volume (%d) level %d mode %d\n",drive,BH_reg(context),DX_reg(context));
334 break;
336 case 0x60: /* get device parameters */
337 /* used by w4wgrp's winfile */
338 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
339 dataptr[0] = 0x04;
340 dataptr[6] = 0; /* media type */
341 if (drive > 1)
343 dataptr[1] = 0x05; /* fixed disk */
344 setword(&dataptr[2], 0x01); /* non removable */
345 setword(&dataptr[4], 0x300); /* # of cylinders */
347 else
349 dataptr[1] = 0x07; /* block dev, floppy */
350 setword(&dataptr[2], 0x02); /* removable */
351 setword(&dataptr[4], 80); /* # of cylinders */
353 CreateBPB(drive, &dataptr[7], TRUE);
354 RESET_CFLAG(context);
355 break;
357 case 0x41: /* write logical device track */
358 case 0x61: /* read logical device track */
360 BYTE drive = BL_reg(context) ?
361 BL_reg(context) : DRIVE_GetCurrentDrive();
362 WORD head = *(WORD *)dataptr+1;
363 WORD cyl = *(WORD *)dataptr+3;
364 WORD sect = *(WORD *)dataptr+5;
365 WORD nrsect = *(WORD *)dataptr+7;
366 BYTE *data = (BYTE *)dataptr+9;
367 int (*raw_func)(BYTE, DWORD, DWORD, BYTE *, BOOL);
369 raw_func = (CL_reg(context) == 0x41) ?
370 DRIVE_RawWrite : DRIVE_RawRead;
372 if (raw_func(drive, head*cyl*sect, nrsect, data, FALSE))
373 RESET_CFLAG(context);
374 else
376 AX_reg(context) = 0x1e; /* read fault */
377 SET_CFLAG(context);
380 break;
381 case 0x66:/* get disk serial number */
383 char label[12],fsname[9],path[4];
384 DWORD serial;
386 strcpy(path,"x:\\");path[0]=drive+'A';
387 GetVolumeInformationA(
388 path,label,12,&serial,NULL,NULL,fsname,9
390 *(WORD*)dataptr = 0;
391 memcpy(dataptr+2,&serial,4);
392 memcpy(dataptr+6,label ,11);
393 memcpy(dataptr+17,fsname,8);
395 break;
397 case 0x6a:
398 TRACE("logical volume %d unlocked.\n",drive);
399 break;
401 case 0x6f:
402 memset(dataptr+1, '\0', dataptr[0]-1);
403 dataptr[1] = dataptr[0];
404 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
405 dataptr[3] = 0xFF; /* no physical drive */
406 break;
408 case 0x72:
409 /* Trail on error implementation */
410 AX_reg(context) = GetDriveType16(BL_reg(context)) == DRIVE_UNKNOWN ? 0x0f : 0x01;
411 SET_CFLAG(context); /* Seems to be set all the time */
412 break;
414 default:
415 INT_BARF( context, 0x21 );
417 return FALSE;
420 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
422 char *filename =
423 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
424 char *fcb =
425 CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
426 char *buffer, *s, *d;
428 AL_reg(context) = 0xff; /* failed */
430 TRACE("filename: '%s'\n", filename);
432 buffer = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + 1);
434 s = filename;
435 d = buffer;
436 while (*s)
438 if ((*s != ' ') && (*s != '\r') && (*s != '\n'))
439 *d++ = *s++;
440 else
441 break;
444 *d = '\0';
445 DOSFS_ToDosFCBFormat(buffer, fcb + 1);
446 *fcb = 0;
447 TRACE("FCB: '%s'\n", ((CHAR *)fcb + 1));
449 HeapFree( GetProcessHeap(), 0, buffer);
451 AL_reg(context) =
452 ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0;
454 /* point DS:SI to first unparsed character */
455 SI_reg(context) += (int)s - (int)filename;
458 static void INT21_GetSystemDate( CONTEXT86 *context )
460 SYSTEMTIME systime;
461 GetLocalTime( &systime );
462 CX_reg(context) = systime.wYear;
463 DX_reg(context) = (systime.wMonth << 8) | systime.wDay;
464 AX_reg(context) = systime.wDayOfWeek;
467 static void INT21_GetSystemTime( CONTEXT86 *context )
469 SYSTEMTIME systime;
470 GetLocalTime( &systime );
471 CX_reg(context) = (systime.wHour << 8) | systime.wMinute;
472 DX_reg(context) = (systime.wSecond << 8) | (systime.wMilliseconds / 10);
475 /* Many calls translate a drive argument like this:
476 drive number (00h = default, 01h = A:, etc)
478 static char drivestring[]="default";
480 char *INT21_DriveName(int drive)
483 if(drive >0)
485 drivestring[0]= (unsigned char)drive + '@';
486 drivestring[1]=':';
487 drivestring[2]=0;
489 return drivestring;
491 static BOOL INT21_CreateFile( CONTEXT86 *context )
493 AX_reg(context) = _lcreat16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
494 context->Edx ), CX_reg(context) );
495 return (AX_reg(context) == (WORD)HFILE_ERROR16);
498 static HFILE16 _lcreat16_uniq( LPCSTR path, INT attr )
500 /* Mask off all flags not explicitly allowed by the doc */
501 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
502 return Win32HandleToDosFileHandle( CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
503 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
504 CREATE_NEW, attr, 0 ));
507 static void OpenExistingFile( CONTEXT86 *context )
509 AX_reg(context) = _lopen16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
510 AL_reg(context) );
511 if (AX_reg(context) == (WORD)HFILE_ERROR16)
513 AX_reg(context) = GetLastError();
514 SET_CFLAG(context);
518 static BOOL INT21_ExtendedOpenCreateFile(CONTEXT86 *context )
520 BOOL bExtendedError = FALSE;
521 BYTE action = DL_reg(context);
523 /* Shuffle arguments to call OpenExistingFile */
524 AL_reg(context) = BL_reg(context);
525 DX_reg(context) = SI_reg(context);
526 /* BX,CX and DX should be preserved */
527 OpenExistingFile(context);
529 if ((context->EFlags & 0x0001) == 0) /* File exists */
531 UINT16 uReturnCX = 0;
533 /* Now decide what do do */
535 if ((action & 0x07) == 0)
537 _lclose16( AX_reg(context) );
538 AX_reg(context) = 0x0050; /*File exists*/
539 SET_CFLAG(context);
540 WARN("extended open/create: failed because file exists \n");
542 else if ((action & 0x07) == 2)
544 /* Truncate it, but first check if opened for write */
545 if ((BL_reg(context) & 0x0007)== 0)
547 _lclose16( AX_reg(context) );
548 WARN("extended open/create: failed, trunc on ro file\n");
549 AX_reg(context) = 0x000C; /*Access code invalid*/
550 SET_CFLAG(context);
552 else
554 TRACE("extended open/create: Closing before truncate\n");
555 if (_lclose16( AX_reg(context) ))
557 WARN("extended open/create: close before trunc failed\n");
558 AX_reg(context) = 0x0019; /*Seek Error*/
559 CX_reg(context) = 0;
560 SET_CFLAG(context);
562 /* Shuffle arguments to call CreateFile */
564 TRACE("extended open/create: Truncating\n");
565 AL_reg(context) = BL_reg(context);
566 /* CX is still the same */
567 DX_reg(context) = SI_reg(context);
568 bExtendedError = INT21_CreateFile(context);
570 if (context->EFlags & 0x0001) /*no file open, flags set */
572 WARN("extended open/create: trunc failed\n");
573 return bExtendedError;
575 uReturnCX = 0x3;
578 else uReturnCX = 0x1;
580 CX_reg(context) = uReturnCX;
582 else /* file does not exist */
584 RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
585 if ((action & 0xF0)== 0)
587 CX_reg(context) = 0;
588 SET_CFLAG(context);
589 WARN("extended open/create: failed, file dosen't exist\n");
591 else
593 /* Shuffle arguments to call CreateFile */
594 TRACE("extended open/create: Creating\n");
595 AL_reg(context) = BL_reg(context);
596 /* CX should still be the same */
597 DX_reg(context) = SI_reg(context);
598 bExtendedError = INT21_CreateFile(context);
599 if (context->EFlags & 0x0001) /*no file open, flags set */
601 WARN("extended open/create: create failed\n");
602 return bExtendedError;
604 CX_reg(context) = 2;
608 return bExtendedError;
612 static BOOL INT21_ChangeDir( CONTEXT86 *context )
614 int drive;
615 char *dirname = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
617 TRACE("changedir %s\n", dirname);
618 if (dirname[0] && (dirname[1] == ':'))
620 drive = toupper(dirname[0]) - 'A';
621 dirname += 2;
623 else drive = DRIVE_GetCurrentDrive();
624 return DRIVE_Chdir( drive, dirname );
628 static int INT21_FindFirst( CONTEXT86 *context )
630 char *p;
631 const char *path;
632 DOS_FULL_NAME full_name;
633 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
635 path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
636 dta->unixPath = NULL;
637 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
639 AX_reg(context) = GetLastError();
640 SET_CFLAG(context);
641 return 0;
643 dta->unixPath = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.long_name)+1 );
644 strcpy( dta->unixPath, full_name.long_name );
645 p = strrchr( dta->unixPath, '/' );
646 *p = '\0';
648 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
649 * (doesn't matter as it is set below anyway)
651 if (!DOSFS_ToDosFCBFormat( p + 1, dta->mask ))
653 HeapFree( GetProcessHeap(), 0, dta->unixPath );
654 dta->unixPath = NULL;
655 SetLastError( ERROR_FILE_NOT_FOUND );
656 AX_reg(context) = ERROR_FILE_NOT_FOUND;
657 SET_CFLAG(context);
658 return 0;
660 dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
661 : DRIVE_GetCurrentDrive();
662 dta->count = 0;
663 dta->search_attr = CL_reg(context);
664 return 1;
668 static int INT21_FindNext( CONTEXT86 *context )
670 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
671 WIN32_FIND_DATAA entry;
672 int count;
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;
680 return 0;
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;
687 return 0;
689 dta->count += count;
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;
702 return 1;
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++ = '\\';
716 for (;;)
718 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
719 counter = (counter + 1) % 1000;
721 if ((AX_reg(context) = _lcreat16_uniq( name, 0 )) != (WORD)HFILE_ERROR16)
723 TRACE("created %s\n", name );
724 return TRUE;
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 );
739 return FALSE;
741 lstrcpynA( ptr, DRIVE_GetDosCwd(drive), 64 );
742 AX_reg(context) = 0x0100; /* success return code */
743 return TRUE;
747 static void INT21_GetDBCSLeadTable( CONTEXT86 *context )
749 if (heap || INT21_CreateHeap())
750 { /* return an empty table just as DOS 4.0+ does */
751 context->SegDs = DosHeapHandle;
752 SI_reg(context) = (int)&heap->DummyDBCSLeadTable - (int)heap;
754 else
756 AX_reg(context) = 0x1; /* error */
757 SET_CFLAG(context);
762 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
764 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
765 int drive = DOS_GET_DRIVE( BL_reg(context) );
767 if (!DRIVE_IsValid(drive))
769 SetLastError( ERROR_INVALID_DRIVE );
770 return 0;
773 *(WORD *)dataptr = 0;
774 *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
775 memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
776 strncpy(dataptr + 0x11, "FAT16 ", 8);
777 return 1;
781 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
783 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
784 int drive = DOS_GET_DRIVE( BL_reg(context) );
786 if (!DRIVE_IsValid(drive))
788 SetLastError( ERROR_INVALID_DRIVE );
789 return 0;
792 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
793 return 1;
797 /* microsoft's programmers should be shot for using CP/M style int21
798 calls in Windows for Workgroup's winfile.exe */
800 static int INT21_FindFirstFCB( CONTEXT86 *context )
802 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
803 FINDFILE_FCB *pFCB;
804 LPCSTR root, cwd;
805 int drive;
807 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
808 else pFCB = (FINDFILE_FCB *)fcb;
809 drive = DOS_GET_DRIVE( pFCB->drive );
810 if (!DRIVE_IsValid( drive )) return 0;
811 root = DRIVE_GetRoot( drive );
812 cwd = DRIVE_GetUnixCwd( drive );
813 pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
814 strlen(root)+strlen(cwd)+2 );
815 if (!pFCB->unixPath) return 0;
816 strcpy( pFCB->unixPath, root );
817 strcat( pFCB->unixPath, "/" );
818 strcat( pFCB->unixPath, cwd );
819 pFCB->count = 0;
820 return 1;
824 static int INT21_FindNextFCB( CONTEXT86 *context )
826 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
827 FINDFILE_FCB *pFCB;
828 DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA(context);
829 WIN32_FIND_DATAA entry;
830 BYTE attr;
831 int count;
833 if (*fcb == 0xff) /* extended FCB ? */
835 attr = fcb[6];
836 pFCB = (FINDFILE_FCB *)(fcb + 7);
838 else
840 attr = 0;
841 pFCB = (FINDFILE_FCB *)fcb;
844 if (!pFCB->unixPath) return 0;
845 if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
846 DOS_GET_DRIVE( pFCB->drive ), attr,
847 pFCB->count, &entry )))
849 HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
850 pFCB->unixPath = NULL;
851 return 0;
853 pFCB->count += count;
855 if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
856 *(BYTE *)pResult = 0xff;
857 (BYTE *)pResult +=6; /* leave reserved field behind */
858 *(BYTE *)pResult = entry.dwFileAttributes;
859 ((BYTE *)pResult)++;
861 *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
862 ((BYTE *)pResult)++;
863 pResult->fileattr = entry.dwFileAttributes;
864 pResult->cluster = 0; /* what else? */
865 pResult->filesize = entry.nFileSizeLow;
866 memset( pResult->reserved, 0, sizeof(pResult->reserved) );
867 FileTimeToDosDateTime( &entry.ftLastWriteTime,
868 &pResult->filedate, &pResult->filetime );
870 /* Convert file name to FCB format */
872 memset( pResult->filename, ' ', sizeof(pResult->filename) );
873 if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
874 else if (!strcmp( entry.cAlternateFileName, ".." ))
875 pResult->filename[0] = pResult->filename[1] = '.';
876 else
878 char *p = strrchr( entry.cAlternateFileName, '.' );
879 if (p && p[1] && (p != entry.cAlternateFileName))
881 memcpy( pResult->filename, entry.cAlternateFileName,
882 min( (p - entry.cAlternateFileName), 8 ) );
883 memcpy( pResult->filename + 8, p + 1, min( strlen(p), 3 ) );
885 else
886 memcpy( pResult->filename, entry.cAlternateFileName,
887 min( strlen(entry.cAlternateFileName), 8 ) );
889 return 1;
893 static void DeleteFileFCB( CONTEXT86 *context )
895 FIXME("(%p): stub\n", context);
898 static void RenameFileFCB( CONTEXT86 *context )
900 FIXME("(%p): stub\n", context);
905 static void fLock( CONTEXT86 * context )
908 switch ( AX_reg(context) & 0xff )
910 case 0x00: /* LOCK */
911 TRACE("lock handle %d offset %ld length %ld\n",
912 BX_reg(context),
913 MAKELONG(DX_reg(context),CX_reg(context)),
914 MAKELONG(DI_reg(context),SI_reg(context))) ;
915 if (!LockFile(DosFileHandleToWin32Handle(BX_reg(context)),
916 MAKELONG(DX_reg(context),CX_reg(context)), 0,
917 MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
918 AX_reg(context) = GetLastError();
919 SET_CFLAG(context);
921 break;
923 case 0x01: /* UNLOCK */
924 TRACE("unlock handle %d offset %ld length %ld\n",
925 BX_reg(context),
926 MAKELONG(DX_reg(context),CX_reg(context)),
927 MAKELONG(DI_reg(context),SI_reg(context))) ;
928 if (!UnlockFile(DosFileHandleToWin32Handle(BX_reg(context)),
929 MAKELONG(DX_reg(context),CX_reg(context)), 0,
930 MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
931 AX_reg(context) = GetLastError();
932 SET_CFLAG(context);
934 return;
935 default:
936 AX_reg(context) = 0x0001;
937 SET_CFLAG(context);
938 return;
942 static BOOL
943 INT21_networkfunc (CONTEXT86 *context)
945 switch (AL_reg(context)) {
946 case 0x00: /* Get machine name. */
948 char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
949 TRACE("getting machine name to %p\n", dst);
950 if (gethostname (dst, 15))
952 WARN("failed!\n");
953 SetLastError( ER_NoNetwork );
954 return TRUE;
955 } else {
956 int len = strlen (dst);
957 while (len < 15)
958 dst[len++] = ' ';
959 dst[15] = 0;
960 CH_reg(context) = 1; /* Valid */
961 CL_reg(context) = 1; /* NETbios number??? */
962 TRACE("returning %s\n", debugstr_an (dst, 16));
963 return FALSE;
967 default:
968 SetLastError( ER_NoNetwork );
969 return TRUE;
974 static void ASPI_DOS_HandleInt( CONTEXT86 *context )
976 if (!Dosvm.ASPIHandler && !DPMI_LoadDosSystem())
978 ERR("could not setup ASPI handler\n");
979 return;
981 Dosvm.ASPIHandler( context );
984 /***********************************************************************
985 * INT21_GetExtendedError
987 static void INT21_GetExtendedError( CONTEXT86 *context )
989 BYTE class, action, locus;
990 WORD error = GetLastError();
992 switch(error)
994 case ERROR_SUCCESS:
995 class = action = locus = 0;
996 break;
997 case ERROR_DIR_NOT_EMPTY:
998 class = EC_Exists;
999 action = SA_Ignore;
1000 locus = EL_Disk;
1001 break;
1002 case ERROR_ACCESS_DENIED:
1003 class = EC_AccessDenied;
1004 action = SA_Abort;
1005 locus = EL_Disk;
1006 break;
1007 case ERROR_CANNOT_MAKE:
1008 class = EC_AccessDenied;
1009 action = SA_Abort;
1010 locus = EL_Unknown;
1011 break;
1012 case ERROR_DISK_FULL:
1013 case ERROR_HANDLE_DISK_FULL:
1014 class = EC_MediaError;
1015 action = SA_Abort;
1016 locus = EL_Disk;
1017 break;
1018 case ERROR_FILE_EXISTS:
1019 case ERROR_ALREADY_EXISTS:
1020 class = EC_Exists;
1021 action = SA_Abort;
1022 locus = EL_Disk;
1023 break;
1024 case ERROR_FILE_NOT_FOUND:
1025 class = EC_NotFound;
1026 action = SA_Abort;
1027 locus = EL_Disk;
1028 break;
1029 case ER_GeneralFailure:
1030 class = EC_SystemFailure;
1031 action = SA_Abort;
1032 locus = EL_Unknown;
1033 break;
1034 case ERROR_INVALID_DRIVE:
1035 class = EC_MediaError;
1036 action = SA_Abort;
1037 locus = EL_Disk;
1038 break;
1039 case ERROR_INVALID_HANDLE:
1040 class = EC_ProgramError;
1041 action = SA_Abort;
1042 locus = EL_Disk;
1043 break;
1044 case ERROR_LOCK_VIOLATION:
1045 class = EC_AccessDenied;
1046 action = SA_Abort;
1047 locus = EL_Disk;
1048 break;
1049 case ERROR_NO_MORE_FILES:
1050 class = EC_MediaError;
1051 action = SA_Abort;
1052 locus = EL_Disk;
1053 break;
1054 case ER_NoNetwork:
1055 class = EC_NotFound;
1056 action = SA_Abort;
1057 locus = EL_Network;
1058 break;
1059 case ERROR_NOT_ENOUGH_MEMORY:
1060 class = EC_OutOfResource;
1061 action = SA_Abort;
1062 locus = EL_Memory;
1063 break;
1064 case ERROR_PATH_NOT_FOUND:
1065 class = EC_NotFound;
1066 action = SA_Abort;
1067 locus = EL_Disk;
1068 break;
1069 case ERROR_SEEK:
1070 class = EC_NotFound;
1071 action = SA_Ignore;
1072 locus = EL_Disk;
1073 break;
1074 case ERROR_SHARING_VIOLATION:
1075 class = EC_Temporary;
1076 action = SA_Retry;
1077 locus = EL_Disk;
1078 break;
1079 case ERROR_TOO_MANY_OPEN_FILES:
1080 class = EC_ProgramError;
1081 action = SA_Abort;
1082 locus = EL_Disk;
1083 break;
1084 default:
1085 FIXME("Unknown error %d\n", error );
1086 class = EC_SystemFailure;
1087 action = SA_Abort;
1088 locus = EL_Unknown;
1089 break;
1091 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
1092 error, class, action, locus );
1093 AX_reg(context) = error;
1094 BH_reg(context) = class;
1095 BL_reg(context) = action;
1096 CH_reg(context) = locus;
1099 /***********************************************************************
1100 * DOS3Call (KERNEL.102)
1101 * INT_Int21Handler (WPROCS.133)
1103 void WINAPI DOS3Call( CONTEXT86 *context )
1105 BOOL bSetDOSExtendedError = FALSE;
1108 TRACE("AX=%04x BX=%04x CX=%04x DX=%04x "
1109 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1110 AX_reg(context), BX_reg(context), CX_reg(context), DX_reg(context),
1111 SI_reg(context), DI_reg(context),
1112 (WORD)context->SegDs, (WORD)context->SegEs,
1113 context->EFlags );
1116 if (AH_reg(context) == 0x59) /* Get extended error info */
1118 INT21_GetExtendedError( context );
1119 return;
1122 if (AH_reg(context) == 0x0C) /* Flush buffer and read standard input */
1124 TRACE("FLUSH BUFFER AND READ STANDARD INPUT\n");
1125 /* no flush here yet */
1126 AH_reg(context) = AL_reg(context);
1129 if (AH_reg(context)>=0x2f) {
1130 /* extended error is used by (at least) functions 0x2f to 0x62 */
1131 SetLastError(0);
1133 RESET_CFLAG(context); /* Not sure if this is a good idea */
1135 switch(AH_reg(context))
1137 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
1138 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
1139 case 0x03: /* READ CHARACTER FROM STDAUX */
1140 case 0x04: /* WRITE CHARACTER TO STDAUX */
1141 case 0x05: /* WRITE CHARACTER TO PRINTER */
1142 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
1143 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
1144 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
1145 case 0x0b: /* GET STDIN STATUS */
1146 case 0x0f: /* OPEN FILE USING FCB */
1147 case 0x10: /* CLOSE FILE USING FCB */
1148 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
1149 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
1150 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
1151 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
1152 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
1153 case 0x23: /* GET FILE SIZE FOR FCB */
1154 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
1155 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
1156 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
1157 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
1158 case 0x4d: /* GET RETURN CODE */
1159 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
1160 INT_BARF( context, 0x21 );
1161 break;
1163 case 0x00: /* TERMINATE PROGRAM */
1164 TRACE("TERMINATE PROGRAM\n");
1165 ExitThread( 0 );
1166 break;
1168 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
1169 TRACE("WRITE '$'-terminated string from %04lX:%04X to stdout\n",
1170 context->SegDs,DX_reg(context) );
1172 LPSTR data = CTX_SEG_OFF_TO_LIN(context,context->SegDs,context->Edx);
1173 LPSTR p = data;
1174 /* do NOT use strchr() to calculate the string length,
1175 as '\0' is valid string content, too !
1176 Maybe we should check for non-'$' strings, but DOS doesn't. */
1177 while (*p != '$') p++;
1178 _hwrite16( 1, data, (int)p - (int)data);
1179 AL_reg(context) = '$'; /* yes, '$' (0x24) gets returned in AL */
1181 break;
1183 case 0x0a: /* BUFFERED INPUT */
1185 char *buffer = ((char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1186 context->Edx ));
1187 int res;
1189 TRACE("BUFFERED INPUT (size=%d)\n",buffer[0]);
1190 if (buffer[1])
1191 TRACE("Handle old chars in buffer!\n");
1192 res=_lread16( 0, buffer+2,buffer[0]);
1193 buffer[1]=res;
1194 if(buffer[res+1] == '\n')
1195 buffer[res+1] = '\r';
1196 break;
1199 case 0x2e: /* SET VERIFY FLAG */
1200 TRACE("SET VERIFY FLAG ignored\n");
1201 /* we cannot change the behaviour anyway, so just ignore it */
1202 break;
1204 case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
1205 case 0x1d:
1206 case 0x1e:
1207 case 0x20:
1208 case 0x6b: /* NULL FUNCTION */
1209 AL_reg(context) = 0;
1210 break;
1212 case 0x5c: /* "FLOCK" - RECORD LOCKING */
1213 fLock(context);
1214 break;
1216 case 0x0d: /* DISK BUFFER FLUSH */
1217 TRACE("DISK BUFFER FLUSH ignored\n");
1218 RESET_CFLAG(context); /* dos 6+ only */
1219 break;
1221 case 0x0e: /* SELECT DEFAULT DRIVE */
1222 TRACE("SELECT DEFAULT DRIVE %d\n", DL_reg(context));
1223 DRIVE_SetCurrentDrive( DL_reg(context) );
1224 AL_reg(context) = MAX_DOS_DRIVES;
1225 break;
1227 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1228 TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
1229 CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1230 if (!INT21_FindFirstFCB(context))
1232 AL_reg(context) = 0xff;
1233 break;
1235 /* else fall through */
1237 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1238 AL_reg(context) = INT21_FindNextFCB(context) ? 0x00 : 0xff;
1239 break;
1241 case 0x13: /* DELETE FILE USING FCB */
1242 DeleteFileFCB(context);
1243 break;
1245 case 0x17: /* RENAME FILE USING FCB */
1246 RenameFileFCB(context);
1247 break;
1249 case 0x19: /* GET CURRENT DEFAULT DRIVE */
1250 AL_reg(context) = DRIVE_GetCurrentDrive();
1251 break;
1253 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1255 TDB *pTask = TASK_GetCurrent();
1256 pTask->dta = MAKESEGPTR(context->SegDs,DX_reg(context));
1257 TRACE("Set DTA: %08lx\n", pTask->dta);
1259 break;
1261 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1262 DL_reg(context) = 0;
1263 if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1264 break;
1266 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1267 if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1268 break;
1270 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1271 GetDrivePB(context, DRIVE_GetCurrentDrive());
1272 break;
1274 case 0x25: /* SET INTERRUPT VECTOR */
1275 INT_SetPMHandler( AL_reg(context), (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context)));
1276 break;
1278 case 0x29: /* PARSE FILENAME INTO FCB */
1279 INT21_ParseFileNameIntoFCB(context);
1280 break;
1282 case 0x2a: /* GET SYSTEM DATE */
1283 INT21_GetSystemDate(context);
1284 break;
1286 case 0x2b: /* SET SYSTEM DATE */
1287 FIXME("SetSystemDate(%02d/%02d/%04d): not allowed\n",
1288 DL_reg(context), DH_reg(context), CX_reg(context) );
1289 AL_reg(context) = 0; /* Let's pretend we succeeded */
1290 break;
1292 case 0x2c: /* GET SYSTEM TIME */
1293 INT21_GetSystemTime(context);
1294 break;
1296 case 0x2d: /* SET SYSTEM TIME */
1297 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1298 CH_reg(context), CL_reg(context),
1299 DH_reg(context), DL_reg(context) );
1300 AL_reg(context) = 0; /* Let's pretend we succeeded */
1301 break;
1303 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1304 TRACE("GET DISK TRANSFER AREA ADDRESS\n");
1306 TDB *pTask = TASK_GetCurrent();
1307 context->SegEs = SELECTOROF( pTask->dta );
1308 BX_reg(context) = OFFSETOF( pTask->dta );
1310 break;
1312 case 0x30: /* GET DOS VERSION */
1313 TRACE("GET DOS VERSION %s requested\n",
1314 (AL_reg(context) == 0x00)?"OEM number":"version flag");
1315 AX_reg(context) = (HIWORD(GetVersion16()) >> 8) |
1316 (HIWORD(GetVersion16()) << 8);
1317 #if 0
1318 AH_reg(context) = 0x7;
1319 AL_reg(context) = 0xA;
1320 #endif
1322 BX_reg(context) = 0x00FF; /* 0x123456 is Wine's serial # */
1323 CX_reg(context) = 0x0000;
1324 break;
1326 case 0x31: /* TERMINATE AND STAY RESIDENT */
1327 FIXME("TERMINATE AND STAY RESIDENT stub\n");
1328 break;
1330 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1331 TRACE("GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
1332 INT21_DriveName( DL_reg(context)));
1333 GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
1334 break;
1336 case 0x33: /* MULTIPLEXED */
1337 switch (AL_reg(context))
1339 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1340 TRACE("GET CURRENT EXTENDED BREAK STATE\n");
1341 INT21_ReadConfigSys();
1342 DL_reg(context) = DOSCONF_config.brk_flag;
1343 break;
1345 case 0x01: /* SET EXTENDED BREAK STATE */
1346 TRACE("SET CURRENT EXTENDED BREAK STATE\n");
1347 INT21_ReadConfigSys();
1348 DOSCONF_config.brk_flag = (DL_reg(context) > 0);
1349 break;
1351 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1352 TRACE("GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
1353 INT21_ReadConfigSys();
1354 /* ugly coding in order to stay reentrant */
1355 if (DL_reg(context))
1357 DL_reg(context) = DOSCONF_config.brk_flag;
1358 DOSCONF_config.brk_flag = 1;
1360 else
1362 DL_reg(context) = DOSCONF_config.brk_flag;
1363 DOSCONF_config.brk_flag = 0;
1365 break;
1367 case 0x05: /* GET BOOT DRIVE */
1368 TRACE("GET BOOT DRIVE\n");
1369 DL_reg(context) = 3;
1370 /* c: is Wine's bootdrive (a: is 1)*/
1371 break;
1373 case 0x06: /* GET TRUE VERSION NUMBER */
1374 TRACE("GET TRUE VERSION NUMBER\n");
1375 BX_reg(context) = (HIWORD(GetVersion16() >> 8)) |
1376 (HIWORD(GetVersion16() << 8));
1377 DX_reg(context) = 0x00;
1378 break;
1380 default:
1381 INT_BARF( context, 0x21 );
1382 break;
1384 break;
1386 case 0x34: /* GET ADDRESS OF INDOS FLAG */
1387 TRACE("GET ADDRESS OF INDOS FLAG\n");
1388 if (!heap) INT21_CreateHeap();
1389 context->SegEs = DosHeapHandle;
1390 BX_reg(context) = (int)&heap->InDosFlag - (int)heap;
1391 break;
1393 case 0x35: /* GET INTERRUPT VECTOR */
1394 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
1396 FARPROC16 addr = INT_GetPMHandler( AL_reg(context) );
1397 context->SegEs = SELECTOROF(addr);
1398 BX_reg(context) = OFFSETOF(addr);
1400 break;
1402 case 0x36: /* GET FREE DISK SPACE */
1403 TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
1404 INT21_DriveName( DL_reg(context)));
1405 if (!INT21_GetFreeDiskSpace(context)) AX_reg(context) = 0xffff;
1406 break;
1408 case 0x37:
1410 unsigned char switchchar='/';
1411 switch (AL_reg(context))
1413 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
1414 TRACE("SWITCHAR - GET SWITCH CHARACTER\n");
1415 AL_reg(context) = 0x00; /* success*/
1416 DL_reg(context) = switchchar;
1417 break;
1418 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
1419 TRACE("SWITCHAR - SET SWITCH CHARACTER\n");
1420 switchchar = DL_reg(context);
1421 AL_reg(context) = 0x00; /* success*/
1422 break;
1423 default: /*"AVAILDEV" - SPECIFY \DEV\ PREFIX USE*/
1424 INT_BARF( context, 0x21 );
1425 break;
1427 break;
1430 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1431 TRACE("GET COUNTRY-SPECIFIC INFORMATION for country 0x%02x\n",
1432 AL_reg(context));
1433 AX_reg(context) = 0x02; /* no country support available */
1434 SET_CFLAG(context);
1435 break;
1437 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1438 TRACE("MKDIR %s\n",
1439 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1440 bSetDOSExtendedError = (!CreateDirectory16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1441 context->Edx ), NULL));
1442 /* FIXME: CreateDirectory's LastErrors will clash with the ones
1443 * used by dos. AH=39 only returns 3 (path not found) and 5 (access
1444 * denied), while CreateDirectory return several ones. remap some of
1445 * them. -Marcus
1447 if (bSetDOSExtendedError) {
1448 switch (GetLastError()) {
1449 case ERROR_ALREADY_EXISTS:
1450 case ERROR_FILENAME_EXCED_RANGE:
1451 case ERROR_DISK_FULL:
1452 SetLastError(ERROR_ACCESS_DENIED);
1453 break;
1454 default: break;
1457 break;
1459 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1460 TRACE("RMDIR %s\n",
1461 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1462 bSetDOSExtendedError = (!RemoveDirectory16( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1463 context->Edx )));
1464 break;
1466 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1467 TRACE("CHDIR %s\n",
1468 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1469 bSetDOSExtendedError = !INT21_ChangeDir(context);
1470 break;
1472 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1473 TRACE("CREAT flag 0x%02x %s\n",CX_reg(context),
1474 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1475 bSetDOSExtendedError = INT21_CreateFile( context );
1476 break;
1478 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1479 TRACE("OPEN mode 0x%02x %s\n",AL_reg(context),
1480 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1481 OpenExistingFile(context);
1482 break;
1484 case 0x3e: /* "CLOSE" - CLOSE FILE */
1485 TRACE("CLOSE handle %d\n",BX_reg(context));
1486 bSetDOSExtendedError = ((AX_reg(context) = _lclose16( BX_reg(context) )) != 0);
1487 break;
1489 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1490 TRACE("READ from %d to %04lX:%04X for %d byte\n",BX_reg(context),
1491 context->SegDs,DX_reg(context),CX_reg(context) );
1493 LONG result;
1494 if (ISV86(context))
1495 result = _hread16( BX_reg(context),
1496 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1497 context->Edx ),
1498 CX_reg(context) );
1499 else
1500 result = WIN16_hread( BX_reg(context),
1501 MAKESEGPTR( context->SegDs, context->Edx ),
1502 CX_reg(context) );
1503 if (result == -1) bSetDOSExtendedError = TRUE;
1504 else AX_reg(context) = (WORD)result;
1506 break;
1508 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1509 TRACE("WRITE from %04lX:%04X to handle %d for %d byte\n",
1510 context->SegDs,DX_reg(context),BX_reg(context),CX_reg(context) );
1512 LONG result = _hwrite16( BX_reg(context),
1513 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1514 context->Edx ),
1515 CX_reg(context) );
1516 if (result == -1) bSetDOSExtendedError = TRUE;
1517 else AX_reg(context) = (WORD)result;
1519 break;
1521 case 0x41: /* "UNLINK" - DELETE FILE */
1522 TRACE("UNLINK %s\n",
1523 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1524 bSetDOSExtendedError = (!DeleteFileA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1525 context->Edx )));
1526 break;
1528 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1529 TRACE("LSEEK handle %d offset %ld from %s\n",
1530 BX_reg(context), MAKELONG(DX_reg(context),CX_reg(context)),
1531 (AL_reg(context)==0)?"start of file":(AL_reg(context)==1)?
1532 "current file position":"end of file");
1534 LONG status = _llseek16( BX_reg(context),
1535 MAKELONG(DX_reg(context),CX_reg(context)),
1536 AL_reg(context) );
1537 if (status == -1) bSetDOSExtendedError = TRUE;
1538 else
1540 AX_reg(context) = LOWORD(status);
1541 DX_reg(context) = HIWORD(status);
1544 break;
1546 case 0x43: /* FILE ATTRIBUTES */
1547 switch (AL_reg(context))
1549 case 0x00:
1550 TRACE("GET FILE ATTRIBUTES for %s\n",
1551 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1552 AX_reg(context) = (WORD)GetFileAttributesA(
1553 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1554 context->Edx));
1555 if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1556 else CX_reg(context) = AX_reg(context);
1557 break;
1559 case 0x01:
1560 TRACE("SET FILE ATTRIBUTES 0x%02x for %s\n", CX_reg(context),
1561 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1562 bSetDOSExtendedError =
1563 (!SetFileAttributesA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1564 context->Edx),
1565 CX_reg(context) ));
1566 break;
1567 case 0x02:
1568 FIXME("GET COMPRESSED FILE SIZE for %s stub\n",
1569 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1571 break;
1573 case 0x44: /* IOCTL */
1574 switch (AL_reg(context))
1576 case 0x00:
1577 ioctlGetDeviceInfo(context);
1578 break;
1580 case 0x01:
1581 break;
1582 case 0x02:{
1583 const DOS_DEVICE *dev;
1584 if ((dev = DOSFS_GetDeviceByHandle( DosFileHandleToWin32Handle(BX_reg(context)) )) &&
1585 !strcasecmp( dev->name, "SCSIMGR$" ))
1587 ASPI_DOS_HandleInt(context);
1589 break;
1591 case 0x05:{ /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1592 /*BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);*/
1593 int drive = DOS_GET_DRIVE(BL_reg(context));
1595 FIXME("program tried to write to block device control channel of drive %d:\n",drive);
1596 /* for (i=0;i<CX_reg(context);i++)
1597 fprintf(stdnimp,"%02x ",dataptr[i]);
1598 fprintf(stdnimp,"\n");*/
1599 AX_reg(context)=CX_reg(context);
1600 break;
1602 case 0x08: /* Check if drive is removable. */
1603 TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOVABLE for drive %s\n",
1604 INT21_DriveName( BL_reg(context)));
1605 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1607 case DRIVE_UNKNOWN:
1608 SetLastError( ERROR_INVALID_DRIVE );
1609 AX_reg(context) = ERROR_INVALID_DRIVE;
1610 SET_CFLAG(context);
1611 break;
1612 case DRIVE_REMOVABLE:
1613 AX_reg(context) = 0; /* removable */
1614 break;
1615 default:
1616 AX_reg(context) = 1; /* not removable */
1617 break;
1619 break;
1621 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
1622 TRACE("IOCTL - CHECK IF BLOCK DEVICE REMOTE for drive %s\n",
1623 INT21_DriveName( BL_reg(context)));
1624 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1626 case DRIVE_UNKNOWN:
1627 SetLastError( ERROR_INVALID_DRIVE );
1628 AX_reg(context) = ERROR_INVALID_DRIVE;
1629 SET_CFLAG(context);
1630 break;
1631 case DRIVE_REMOTE:
1632 DX_reg(context) = (1<<9) | (1<<12); /* remote */
1633 break;
1634 default:
1635 DX_reg(context) = 0; /* FIXME: use driver attr here */
1636 break;
1638 break;
1640 case 0x0a: /* check if handle (BX) is remote */
1641 TRACE("IOCTL - CHECK IF HANDLE %d IS REMOTE\n",BX_reg(context));
1642 /* returns DX, bit 15 set if remote, bit 14 set if date/time
1643 * not set on close
1645 DX_reg(context) = 0;
1646 break;
1648 case 0x0d:
1649 TRACE("IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
1650 INT21_DriveName( BL_reg(context)));
1651 bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1652 break;
1654 case 0x0e: /* get logical drive mapping */
1655 TRACE("IOCTL - GET LOGICAL DRIVE MAP for drive %s\n",
1656 INT21_DriveName( BL_reg(context)));
1657 AL_reg(context) = 0; /* drive has no mapping - FIXME: may be wrong*/
1658 break;
1660 case 0x0F: /* Set logical drive mapping */
1662 int drive;
1663 TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
1664 INT21_DriveName( BL_reg(context)));
1665 drive = DOS_GET_DRIVE ( BL_reg(context) );
1666 if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
1668 SET_CFLAG(context);
1669 AX_reg(context) = 0x000F; /* invalid drive */
1671 break;
1674 case 0xe0: /* Sun PC-NFS API */
1675 /* not installed */
1676 break;
1678 case 0x52: /* DR-DOS version */
1679 /* This is not DR-DOS */
1681 TRACE("GET DR-DOS VERSION requested\n");
1683 AX_reg(context) = 0x0001; /* Invalid function */
1684 SET_CFLAG(context); /* Error */
1685 SetLastError( ERROR_INVALID_FUNCTION );
1686 break;
1688 default:
1689 INT_BARF( context, 0x21 );
1690 break;
1692 break;
1694 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1696 HANDLE handle;
1697 TRACE("DUP - DUPLICATE FILE HANDLE %d\n",BX_reg(context));
1698 if ((bSetDOSExtendedError = !DuplicateHandle( GetCurrentProcess(),
1699 DosFileHandleToWin32Handle(BX_reg(context)),
1700 GetCurrentProcess(), &handle,
1701 0, TRUE, DUPLICATE_SAME_ACCESS )))
1702 AX_reg(context) = HFILE_ERROR16;
1703 else
1704 AX_reg(context) = Win32HandleToDosFileHandle(handle);
1705 break;
1708 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1709 TRACE("FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
1710 BX_reg(context),CX_reg(context));
1711 bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR16);
1712 break;
1714 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1715 TRACE("CWD - GET CURRENT DIRECTORY for drive %s\n",
1716 INT21_DriveName( DL_reg(context)));
1717 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1718 break;
1720 case 0x48: /* ALLOCATE MEMORY */
1721 TRACE("ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context));
1723 LPVOID *mem;
1724 if (ISV86(context))
1726 mem= DOSMEM_GetBlock((DWORD)BX_reg(context)<<4,NULL);
1727 if (mem)
1728 AX_reg(context) = DOSMEM_MapLinearToDos(mem)>>4;
1730 else
1732 mem = (LPVOID)GlobalDOSAlloc16(BX_reg(context)<<4);
1733 if (mem)
1734 AX_reg(context) = (DWORD)mem&0xffff;
1736 if (!mem)
1738 SET_CFLAG(context);
1739 AX_reg(context) = 0x0008; /* insufficient memory */
1740 BX_reg(context) = DOSMEM_Available()>>4;
1743 break;
1745 case 0x49: /* FREE MEMORY */
1746 TRACE("FREE MEMORY segment %04lX\n", context->SegEs);
1748 BOOL ret;
1749 if (ISV86(context))
1750 ret= DOSMEM_FreeBlock(DOSMEM_MapDosToLinear(context->SegEs<<4));
1751 else
1753 ret = !GlobalDOSFree16(context->SegEs);
1754 /* If we don't reset ES_reg, we will fail in the relay code */
1755 context->SegEs=ret;
1757 if (!ret)
1759 TRACE("FREE MEMORY failed\n");
1760 SET_CFLAG(context);
1761 AX_reg(context) = 0x0009; /* memory block address invalid */
1764 break;
1766 case 0x4a: /* RESIZE MEMORY BLOCK */
1767 TRACE("RESIZE MEMORY segment %04lX to %d paragraphs\n", context->SegEs, BX_reg(context));
1768 if (!ISV86(context))
1769 FIXME("RESIZE MEMORY probably insufficient implementation. Expect crash soon\n");
1771 LPVOID *mem = DOSMEM_ResizeBlock(DOSMEM_MapDosToLinear(context->SegEs<<4),
1772 BX_reg(context)<<4,NULL);
1773 if (mem)
1774 AX_reg(context) = DOSMEM_MapLinearToDos(mem)>>4;
1775 else {
1776 SET_CFLAG(context);
1777 AX_reg(context) = 0x0008; /* insufficient memory */
1778 BX_reg(context) = DOSMEM_Available()>>4; /* not quite right */
1781 break;
1783 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1784 TRACE("EXEC %s\n", (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx ));
1785 AX_reg(context) = WinExec16( CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx ),
1786 SW_NORMAL );
1787 if (AX_reg(context) < 32) SET_CFLAG(context);
1788 break;
1790 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1791 TRACE("EXIT with return code %d\n",AL_reg(context));
1792 ExitThread( AL_reg(context) );
1793 break;
1795 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1796 TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
1797 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1798 if (!INT21_FindFirst(context)) break;
1799 /* fall through */
1801 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1802 TRACE("FINDNEXT\n");
1803 if (!INT21_FindNext(context))
1805 SetLastError( ERROR_NO_MORE_FILES );
1806 AX_reg(context) = ERROR_NO_MORE_FILES;
1807 SET_CFLAG(context);
1809 else AX_reg(context) = 0; /* OK */
1810 break;
1811 case 0x51: /* GET PSP ADDRESS */
1812 TRACE("GET CURRENT PROCESS ID (GET PSP ADDRESS)\n");
1813 /* FIXME: should we return the original DOS PSP upon */
1814 /* Windows startup ? */
1815 BX_reg(context) = GetCurrentPDB16();
1816 break;
1817 case 0x62: /* GET PSP ADDRESS */
1818 /* FIXME: should we return the original DOS PSP upon */
1819 /* Windows startup ? */
1820 BX_reg(context) = GetCurrentPDB16();
1821 break;
1823 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1824 TRACE("SYSVARS - GET LIST OF LISTS\n");
1826 context->SegEs = LOWORD(DOS_LOLSeg);
1827 BX_reg(context) = FIELD_OFFSET(DOS_LISTOFLISTS, ptr_first_DPB);
1829 break;
1831 case 0x54: /* Get Verify Flag */
1832 TRACE("Get Verify Flag - Not Supported\n");
1833 AL_reg(context) = 0x00; /* pretend we can tell. 00h = off 01h = on */
1834 break;
1836 case 0x56: /* "RENAME" - RENAME FILE */
1837 TRACE("RENAME %s to %s\n",
1838 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1839 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi));
1840 bSetDOSExtendedError =
1841 (!MoveFileA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1842 CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi)));
1843 break;
1845 case 0x57: /* FILE DATE AND TIME */
1846 switch (AL_reg(context))
1848 case 0x00: /* Get */
1850 FILETIME filetime;
1851 TRACE("GET FILE DATE AND TIME for handle %d\n",
1852 BX_reg(context));
1853 if (!GetFileTime( DosFileHandleToWin32Handle(BX_reg(context)), NULL, NULL, &filetime ))
1854 bSetDOSExtendedError = TRUE;
1855 else FileTimeToDosDateTime( &filetime, &DX_reg(context),
1856 &CX_reg(context) );
1858 break;
1860 case 0x01: /* Set */
1862 FILETIME filetime;
1863 TRACE("SET FILE DATE AND TIME for handle %d\n",
1864 BX_reg(context));
1865 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
1866 &filetime );
1867 bSetDOSExtendedError =
1868 (!SetFileTime( DosFileHandleToWin32Handle(BX_reg(context)),
1869 NULL, NULL, &filetime ));
1871 break;
1873 break;
1875 case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1876 TRACE("GET OR SET MEMORY/UMB ALLOCATION STRATEGY subfunction %d\n",
1877 AL_reg(context));
1878 switch (AL_reg(context))
1880 case 0x00:
1881 AX_reg(context) = 1;
1882 break;
1883 case 0x02:
1884 AX_reg(context) = 0;
1885 break;
1886 case 0x01:
1887 case 0x03:
1888 break;
1890 RESET_CFLAG(context);
1891 break;
1893 case 0x5a: /* CREATE TEMPORARY FILE */
1894 TRACE("CREATE TEMPORARY FILE\n");
1895 bSetDOSExtendedError = !INT21_CreateTempFile(context);
1896 break;
1898 case 0x5b: /* CREATE NEW FILE */
1899 TRACE("CREATE NEW FILE 0x%02x for %s\n", CX_reg(context),
1900 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
1901 bSetDOSExtendedError = ((AX_reg(context) =
1902 _lcreat16_uniq( CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
1903 CX_reg(context) )) == (WORD)HFILE_ERROR16);
1904 break;
1906 case 0x5d: /* NETWORK */
1907 FIXME("Function 0x%04x not implemented.\n", AX_reg (context));
1908 /* Fix the following while you're at it. */
1909 SetLastError( ER_NoNetwork );
1910 bSetDOSExtendedError = TRUE;
1911 break;
1913 case 0x5e:
1914 bSetDOSExtendedError = INT21_networkfunc (context);
1915 break;
1917 case 0x5f: /* NETWORK */
1918 switch (AL_reg(context))
1920 case 0x07: /* ENABLE DRIVE */
1921 TRACE("ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1922 if (!DRIVE_Enable( DL_reg(context) ))
1924 SetLastError( ERROR_INVALID_DRIVE );
1925 bSetDOSExtendedError = TRUE;
1927 break;
1929 case 0x08: /* DISABLE DRIVE */
1930 TRACE("DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1931 if (!DRIVE_Disable( DL_reg(context) ))
1933 SetLastError( ERROR_INVALID_DRIVE );
1934 bSetDOSExtendedError = TRUE;
1936 break;
1938 default:
1939 /* network software not installed */
1940 TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
1941 SetLastError( ER_NoNetwork );
1942 bSetDOSExtendedError = TRUE;
1943 break;
1945 break;
1947 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1948 TRACE("TRUENAME %s\n",
1949 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
1951 if (!GetFullPathNameA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
1952 context->Esi), 128,
1953 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
1954 context->Edi),NULL))
1955 bSetDOSExtendedError = TRUE;
1956 else AX_reg(context) = 0;
1958 break;
1960 case 0x61: /* UNUSED */
1961 case 0x63: /* misc. language support */
1962 switch (AL_reg(context)) {
1963 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
1964 INT21_GetDBCSLeadTable(context);
1965 break;
1967 break;
1968 case 0x64: /* OS/2 DOS BOX */
1969 INT_BARF( context, 0x21 );
1970 SET_CFLAG(context);
1971 break;
1973 case 0x65:{/* GET EXTENDED COUNTRY INFORMATION */
1974 BYTE *dataptr=CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi);
1975 TRACE("GET EXTENDED COUNTRY INFORMATION code page %d country %d\n",
1976 BX_reg(context), DX_reg(context));
1977 switch (AL_reg(context)) {
1978 case 0x01:
1979 TRACE("\tget general internationalization info\n");
1980 dataptr[0] = 0x1;
1981 *(WORD*)(dataptr+1) = 41;
1982 *(WORD*)(dataptr+3) = GetSystemDefaultLangID();
1983 *(WORD*)(dataptr+5) = CodePage;
1984 *(DWORD*)(dataptr+0x19) = 0; /* FIXME: ptr to case map routine */
1985 break;
1986 case 0x06:
1987 TRACE("\tget pointer to collating sequence table\n");
1988 dataptr[0] = 0x06;
1989 *(DWORD*)(dataptr+1) = MAKELONG(DOSMEM_CollateTable & 0xFFFF,DOSMEM_AllocSelector(DOSMEM_CollateTable>>16));
1990 CX_reg(context) = 258;/*FIXME: size of table?*/
1991 break;
1992 case 0x20:
1993 TRACE("\tConvert char to uppercase\n");
1994 DL_reg(context) = toupper(DL_reg(context));
1995 break;
1996 case 0x21:
1997 TRACE("\tconvert string to uppercase with length\n");
1999 char *ptr = (char *)CTX_SEG_OFF_TO_LIN(context,context->SegDs,context->Edx);
2000 WORD len = CX_reg(context);
2001 while (len--) { *ptr = toupper(*ptr); ptr++; }
2003 break;
2004 case 0x22:
2005 TRACE("\tConvert ASCIIZ string to uppercase\n");
2006 _strupr( (LPSTR)CTX_SEG_OFF_TO_LIN(context,context->SegDs,context->Edx) );
2007 break;
2008 default:
2009 TRACE("\tunimplemented function %d\n",AL_reg(context));
2010 INT_BARF( context, 0x21 );
2011 SET_CFLAG(context);
2012 break;
2014 break;
2016 case 0x66: /* GLOBAL CODE PAGE TABLE */
2017 switch (AL_reg(context))
2019 case 0x01:
2020 TRACE("GET GLOBAL CODE PAGE TABLE\n");
2021 DX_reg(context) = BX_reg(context) = CodePage;
2022 RESET_CFLAG(context);
2023 break;
2024 case 0x02:
2025 TRACE("SET GLOBAL CODE PAGE TABLE active page %d system page %d\n",
2026 BX_reg(context),DX_reg(context));
2027 CodePage = BX_reg(context);
2028 RESET_CFLAG(context);
2029 break;
2031 break;
2033 case 0x67: /* SET HANDLE COUNT */
2034 TRACE("SET HANDLE COUNT to %d\n",BX_reg(context) );
2035 SetHandleCount16( BX_reg(context) );
2036 if (GetLastError()) bSetDOSExtendedError = TRUE;
2037 break;
2039 case 0x68: /* "FFLUSH" - COMMIT FILE */
2040 case 0x6a: /* COMMIT FILE */
2041 TRACE("FFLUSH/COMMIT handle %d\n",BX_reg(context));
2042 bSetDOSExtendedError = (!FlushFileBuffers( DosFileHandleToWin32Handle(BX_reg(context)) ));
2043 break;
2045 case 0x69: /* DISK SERIAL NUMBER */
2046 switch (AL_reg(context))
2048 case 0x00:
2049 TRACE("GET DISK SERIAL NUMBER for drive %s\n",
2050 INT21_DriveName(BL_reg(context)));
2051 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
2052 else AX_reg(context) = 0;
2053 break;
2055 case 0x01:
2056 TRACE("SET DISK SERIAL NUMBER for drive %s\n",
2057 INT21_DriveName(BL_reg(context)));
2058 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
2059 else AX_reg(context) = 1;
2060 break;
2062 break;
2064 case 0x6C: /* Extended Open/Create*/
2065 TRACE("EXTENDED OPEN/CREATE %s\n",
2066 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edi));
2067 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
2068 break;
2070 case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
2071 if ((GetVersion()&0xC0000004)!=0xC0000004) {
2072 /* not supported on anything but Win95 */
2073 TRACE("LONG FILENAME functions supported only by win95\n");
2074 SET_CFLAG(context);
2075 AL_reg(context) = 0;
2076 } else
2077 switch(AL_reg(context))
2079 case 0x39: /* Create directory */
2080 TRACE("LONG FILENAME - MAKE DIRECTORY %s\n",
2081 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
2082 bSetDOSExtendedError = (!CreateDirectoryA(
2083 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
2084 context->Edx ), NULL));
2085 /* FIXME: CreateDirectory's LastErrors will clash with the ones
2086 * used by dos. AH=39 only returns 3 (path not found) and 5 (access
2087 * denied), while CreateDirectory return several ones. remap some of
2088 * them. -Marcus
2090 if (bSetDOSExtendedError) {
2091 switch (GetLastError()) {
2092 case ERROR_ALREADY_EXISTS:
2093 case ERROR_FILENAME_EXCED_RANGE:
2094 case ERROR_DISK_FULL:
2095 SetLastError(ERROR_ACCESS_DENIED);
2096 break;
2097 default: break;
2100 break;
2101 case 0x3a: /* Remove directory */
2102 TRACE("LONG FILENAME - REMOVE DIRECTORY %s\n",
2103 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
2104 bSetDOSExtendedError = (!RemoveDirectoryA(
2105 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
2106 context->Edx )));
2107 break;
2108 case 0x43: /* Get/Set file attributes */
2109 TRACE("LONG FILENAME -EXTENDED GET/SET FILE ATTRIBUTES %s\n",
2110 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
2111 switch (BL_reg(context))
2113 case 0x00: /* Get file attributes */
2114 TRACE("\tretrieve attributes\n");
2115 CX_reg(context) = (WORD)GetFileAttributesA(
2116 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
2117 context->Edx));
2118 if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
2119 break;
2120 case 0x01:
2121 TRACE("\tset attributes 0x%04x\n",CX_reg(context));
2122 bSetDOSExtendedError = (!SetFileAttributesA(
2123 CTX_SEG_OFF_TO_LIN(context, context->SegDs,
2124 context->Edx),
2125 CX_reg(context) ) );
2126 break;
2127 default:
2128 FIXME("Unimplemented long file name function:\n");
2129 INT_BARF( context, 0x21 );
2130 SET_CFLAG(context);
2131 AL_reg(context) = 0;
2132 break;
2134 break;
2135 case 0x47: /* Get current directory */
2136 TRACE(" LONG FILENAME - GET CURRENT DIRECTORY for drive %s\n",
2137 INT21_DriveName(DL_reg(context)));
2138 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
2139 break;
2141 case 0x4e: /* Find first file */
2142 TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
2143 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
2144 /* FIXME: use attributes in CX */
2145 if ((AX_reg(context) = FindFirstFile16(
2146 CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
2147 (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
2148 context->Edi)))
2149 == INVALID_HANDLE_VALUE16)
2150 bSetDOSExtendedError = TRUE;
2151 break;
2152 case 0x4f: /* Find next file */
2153 TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
2154 BX_reg(context));
2155 if (!FindNextFile16( BX_reg(context),
2156 (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
2157 context->Edi)))
2158 bSetDOSExtendedError = TRUE;
2159 break;
2160 case 0xa0:
2162 LPCSTR driveroot = (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx);
2163 LPSTR buffer = (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs,context->Edi);
2164 DWORD filename_len, flags;
2166 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", driveroot);
2167 AX_reg(context) = 0;
2168 if (!GetVolumeInformationA( driveroot, NULL, 0, NULL, &filename_len,
2169 &flags, buffer, 8 ))
2171 INT_BARF( context, 0x21 );
2172 SET_CFLAG(context);
2173 break;
2175 BX_reg(context) = flags | 0x4000; /* support for LFN functions */
2176 CX_reg(context) = filename_len;
2177 DX_reg(context) = MAX_PATH; /* FIXME: which len if DRIVE_SHORT_NAMES ? */
2179 break;
2180 case 0xa1: /* Find close */
2181 TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
2182 BX_reg(context));
2183 bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
2184 break;
2185 case 0x60:
2186 switch(CL_reg(context))
2188 case 0x01: /* Get short filename or path */
2189 if (!GetShortPathNameA
2190 ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
2191 context->Esi),
2192 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
2193 context->Edi), 67))
2194 bSetDOSExtendedError = TRUE;
2195 else AX_reg(context) = 0;
2196 break;
2197 case 0x02: /* Get canonical long filename or path */
2198 if (!GetFullPathNameA
2199 ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
2200 context->Esi), 128,
2201 CTX_SEG_OFF_TO_LIN(context, context->SegEs,
2202 context->Edi),NULL))
2203 bSetDOSExtendedError = TRUE;
2204 else AX_reg(context) = 0;
2205 break;
2206 default:
2207 FIXME("Unimplemented long file name function:\n");
2208 INT_BARF( context, 0x21 );
2209 SET_CFLAG(context);
2210 AL_reg(context) = 0;
2211 break;
2213 break;
2214 case 0x6c: /* Create or open file */
2215 TRACE("LONG FILENAME - CREATE OR OPEN FILE %s\n",
2216 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi));
2217 /* translate Dos 7 action to Dos 6 action */
2218 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
2219 break;
2221 case 0x3b: /* Change directory */
2222 TRACE("LONG FILENAME - CHANGE DIRECTORY %s\n",
2223 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
2224 if (!SetCurrentDirectoryA(CTX_SEG_OFF_TO_LIN(context,
2225 context->SegDs,
2226 context->Edx
2229 SET_CFLAG(context);
2230 AL_reg(context) = GetLastError();
2232 break;
2233 case 0x41: /* Delete file */
2234 TRACE("LONG FILENAME - DELETE FILE %s\n",
2235 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
2236 if (!DeleteFileA(CTX_SEG_OFF_TO_LIN(context,
2237 context->SegDs,
2238 context->Edx)
2239 )) {
2240 SET_CFLAG(context);
2241 AL_reg(context) = GetLastError();
2243 break;
2244 case 0x56: /* Move (rename) file */
2246 LPCSTR fn1 = (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
2247 LPCSTR fn2 = (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
2248 TRACE("LONG FILENAME - RENAME FILE %s to %s\n", fn1, fn2);
2249 if (!MoveFileA(fn1, fn2))
2251 SET_CFLAG(context);
2252 AL_reg(context) = GetLastError();
2255 break;
2256 default:
2257 FIXME("Unimplemented long file name function:\n");
2258 INT_BARF( context, 0x21 );
2259 SET_CFLAG(context);
2260 AL_reg(context) = 0;
2261 break;
2263 break;
2265 case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
2266 case 0x72: /* MS-DOS 7 (Windows95) - ??? */
2267 TRACE("windows95 function AX %04x\n",
2268 AX_reg(context));
2269 WARN(" returning unimplemented\n");
2270 SET_CFLAG(context);
2271 AL_reg(context) = 0;
2272 break;
2274 case 0x73: /* MULTIPLEXED: Win95 OSR2/Win98 FAT32 calls */
2275 TRACE("windows95 function AX %04x\n",
2276 AX_reg(context));
2278 switch (AL_reg(context))
2280 case 0x02: /* Get Extended Drive Parameter Block for specific drive */
2281 /* ES:DI points to word with length of data (should be 0x3d) */
2283 WORD *buffer;
2284 struct EDPB *edpb;
2285 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
2286 char root[] = "A:\\";
2288 buffer = (WORD *)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
2290 TRACE("Get Extended DPB: linear buffer address is %p\n", buffer);
2292 /* validate passed-in buffer lengths */
2293 if ((*buffer != 0x3d) || (context->Ecx != 0x3f))
2295 WARN("Get Extended DPB: buffer lengths incorrect\n");
2296 WARN("CX = %lx, buffer[0] = %x\n", context->Ecx, *buffer);
2297 SET_CFLAG(context);
2298 AL_reg(context) = 0x18; /* bad buffer length */
2301 /* buffer checks out */
2302 buffer++; /* skip over length word now */
2303 if (FillInDrivePB( DX_reg(context) ) )
2305 edpb = (struct EDPB *)buffer;
2307 /* copy down the old-style DPB portion first */
2308 memcpy(&edpb->dpb, &heap->dpb, sizeof(struct DPB));
2310 /* now fill in the extended entries */
2311 edpb->edpb_flags = 0;
2312 edpb->next_edpb = 0;
2313 edpb->free_cluster = edpb->free_cluster2 = 0;
2315 /* determine free disk space */
2316 *root += DOS_GET_DRIVE( DX_reg(context) );
2317 GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
2318 &free_clusters, &total_clusters );
2320 edpb->clusters_free = (free_clusters&0xffff);
2322 edpb->clusters_free_hi = free_clusters >> 16;
2323 edpb->mirroring_flags = 0;
2324 edpb->info_sector = 0xffff;
2325 edpb->spare_boot_sector = 0xffff;
2326 edpb->first_cluster = 0;
2327 edpb->max_cluster = total_clusters;
2328 edpb->fat_clusters = 32; /* made-up value */
2329 edpb->root_cluster = 0;
2331 RESET_CFLAG(context); /* clear carry */
2332 AX_reg(context) = 0;
2334 else
2336 AX_reg(context) = 0x00ff;
2337 SET_CFLAG(context);
2340 break;
2342 case 0x03: /* Get Extended free space on drive */
2343 case 0x04: /* Set DPB for formatting */
2344 case 0x05: /* extended absolute disk read/write */
2345 FIXME("Unimplemented FAT32 int32 function %04x\n", AX_reg(context));
2346 SET_CFLAG(context);
2347 AL_reg(context) = 0;
2348 break;
2351 break;
2354 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
2355 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
2356 break;
2358 default:
2359 INT_BARF( context, 0x21 );
2360 break;
2362 } /* END OF SWITCH */
2364 if( bSetDOSExtendedError ) /* set general error condition */
2366 AX_reg(context) = GetLastError();
2367 SET_CFLAG(context);
2370 if ((context->EFlags & 0x0001))
2371 TRACE("failed, error %ld\n", GetLastError() );
2373 TRACE("returning: AX=%04x BX=%04x CX=%04x DX=%04x "
2374 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
2375 AX_reg(context), BX_reg(context), CX_reg(context),
2376 DX_reg(context), SI_reg(context), DI_reg(context),
2377 (WORD)context->SegDs, (WORD)context->SegEs,
2378 context->EFlags);
2381 /***********************************************************************
2382 * GetSetKernelDOSProc (KERNEL.311)
2384 FARPROC16 WINAPI GetSetKernelDOSProc16(FARPROC16 DosProc)
2386 FIXME("(DosProc=0x%08x): stub\n", (UINT)DosProc);
2387 return NULL;