Changed DOS extended error handling to be based on SetLastError;
[wine.git] / msdos / int21.c
blobdf36e8ce538498443e40e0c7e3f4011b719c3e2c
1 /*
2 * DOS interrupt 21h handler
3 */
5 #include <time.h>
6 #include <fcntl.h>
7 #include <errno.h>
8 #include <stdlib.h>
9 #ifdef HAVE_SYS_FILE_H
10 # include <sys/file.h>
11 #endif
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <sys/time.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 #include <utime.h>
18 #include <ctype.h>
19 #include "windows.h"
20 #include "winerror.h"
21 #include "drive.h"
22 #include "file.h"
23 #include "heap.h"
24 #include "msdos.h"
25 #include "ldt.h"
26 #include "task.h"
27 #include "options.h"
28 #include "miscemu.h"
29 #include "dosexe.h" /* for the MZ_SUPPORTED define */
30 #include "module.h"
31 #include "debug.h"
32 #include "console.h"
33 #if defined(__svr4__) || defined(_SCO_DS)
34 /* SVR4 DOESNT do locking the same way must implement properly */
35 #define LOCK_EX 0
36 #define LOCK_SH 1
37 #define LOCK_NB 8
38 #endif
41 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
43 /* Define the drive parameter block, as used by int21/1F
44 * and int21/32. This table can be accessed through the
45 * global 'dpb' pointer, which points into the local dos
46 * heap.
48 struct DPB
50 BYTE drive_num; /* 0=A, etc. */
51 BYTE unit_num; /* Drive's unit number (?) */
52 WORD sector_size; /* Sector size in bytes */
53 BYTE high_sector; /* Highest sector in a cluster */
54 BYTE shift; /* Shift count (?) */
55 WORD reserved; /* Number of reserved sectors at start */
56 BYTE num_FAT; /* Number of FATs */
57 WORD dir_entries; /* Number of root dir entries */
58 WORD first_data; /* First data sector */
59 WORD high_cluster; /* Highest cluster number */
60 WORD sectors_in_FAT; /* Number of sectors per FAT */
61 WORD start_dir; /* Starting sector of first dir */
62 DWORD driver_head; /* Address of device driver header (?) */
63 BYTE media_ID; /* Media ID */
64 BYTE access_flag; /* Prev. accessed flag (0=yes,0xFF=no) */
65 DWORD next; /* Pointer to next DPB in list */
66 WORD free_search; /* Free cluster search start */
67 WORD free_clusters; /* Number of free clusters (0xFFFF=unknown) */
70 WORD CodePage = 437;
71 DWORD dpbsegptr;
73 struct DosHeap {
74 BYTE InDosFlag;
75 BYTE mediaID;
76 BYTE biosdate[8];
77 struct DPB dpb;
78 BYTE DummyDBCSLeadTable[6];
80 static struct DosHeap *heap;
81 static WORD DosHeapHandle;
83 WORD sharing_retries = 3; /* number of retries at sharing violation */
84 WORD sharing_pause = 1; /* pause between retries */
86 extern char TempDirectory[];
88 static BOOL32 INT21_CreateHeap(void)
90 if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
92 WARN(int21, "Out of memory\n");
93 return FALSE;
95 heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
96 dpbsegptr = PTR_SEG_OFF_TO_SEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
97 heap->InDosFlag = 0;
98 strcpy(heap->biosdate, "01/01/80");
99 memset(heap->DummyDBCSLeadTable, 0, 6);
100 return TRUE;
103 static BYTE *GetCurrentDTA( CONTEXT *context )
105 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
107 /* FIXME: This assumes DTA was set correctly! */
108 return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
109 (DWORD)OFFSETOF(pTask->dta) );
113 void CreateBPB(int drive, BYTE *data, BOOL16 limited)
114 /* limited == TRUE is used with INT 0x21/0x440d */
116 if (drive > 1) {
117 setword(data, 512);
118 data[2] = 2;
119 setword(&data[3], 0);
120 data[5] = 2;
121 setword(&data[6], 240);
122 setword(&data[8], 64000);
123 data[0x0a] = 0xf8;
124 setword(&data[0x0b], 40);
125 setword(&data[0x0d], 56);
126 setword(&data[0x0f], 2);
127 setword(&data[0x11], 0);
128 if (!limited) {
129 setword(&data[0x1f], 800);
130 data[0x21] = 5;
131 setword(&data[0x22], 1);
133 } else { /* 1.44mb */
134 setword(data, 512);
135 data[2] = 2;
136 setword(&data[3], 0);
137 data[5] = 2;
138 setword(&data[6], 240);
139 setword(&data[8], 2880);
140 data[0x0a] = 0xf8;
141 setword(&data[0x0b], 6);
142 setword(&data[0x0d], 18);
143 setword(&data[0x0f], 2);
144 setword(&data[0x11], 0);
145 if (!limited) {
146 setword(&data[0x1f], 80);
147 data[0x21] = 7;
148 setword(&data[0x22], 2);
153 static int INT21_GetFreeDiskSpace( CONTEXT *context )
155 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
156 char root[] = "A:\\";
158 *root += DOS_GET_DRIVE( DL_reg(context) );
159 if (!GetDiskFreeSpace32A( root, &cluster_sectors, &sector_bytes,
160 &free_clusters, &total_clusters )) return 0;
161 AX_reg(context) = cluster_sectors;
162 BX_reg(context) = free_clusters;
163 CX_reg(context) = sector_bytes;
164 DX_reg(context) = total_clusters;
165 return 1;
168 static int INT21_GetDriveAllocInfo( CONTEXT *context )
170 if (!INT21_GetFreeDiskSpace( context )) return 0;
171 if (!heap && !INT21_CreateHeap()) return 0;
172 heap->mediaID = 0xf0;
173 DS_reg(context) = DosHeapHandle;
174 BX_reg(context) = (int)&heap->mediaID - (int)heap;
175 return 1;
178 static void GetDrivePB( CONTEXT *context, int drive )
180 if(!DRIVE_IsValid(drive))
182 SetLastError( ERROR_INVALID_DRIVE );
183 AX_reg(context) = 0x00ff;
185 else if (heap || INT21_CreateHeap())
187 FIXME(int21, "GetDrivePB not fully implemented.\n");
189 /* FIXME: I have no idea what a lot of this information should
190 * say or whether it even really matters since we're not allowing
191 * direct block access. However, some programs seem to depend on
192 * getting at least _something_ back from here. The 'next' pointer
193 * does worry me, though. Should we have a complete table of
194 * separate DPBs per drive? Probably, but I'm lazy. :-) -CH
196 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
197 heap->dpb.sector_size = 512;
198 heap->dpb.high_sector = 1;
199 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
200 heap->dpb.reserved = 0;
201 heap->dpb.num_FAT = 1;
202 heap->dpb.dir_entries = 2;
203 heap->dpb.first_data = 2;
204 heap->dpb.high_cluster = 64000;
205 heap->dpb.sectors_in_FAT = 1;
206 heap->dpb.start_dir = 1;
207 heap->dpb.driver_head = 0;
208 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
209 heap->dpb.access_flag = 0;
210 heap->dpb.next = 0;
211 heap->dpb.free_search = 0;
212 heap->dpb.free_clusters = 0xFFFF; /* unknown */
214 AL_reg(context) = 0x00;
215 DS_reg(context) = SELECTOROF(dpbsegptr);
216 BX_reg(context) = OFFSETOF(dpbsegptr);
221 static void ioctlGetDeviceInfo( CONTEXT *context )
223 int curr_drive;
224 const DOS_DEVICE *dev;
226 TRACE(int21, "(%d)\n", BX_reg(context));
228 RESET_CFLAG(context);
230 /* DOS device ? */
231 if ((dev = DOSFS_GetDeviceByHandle( FILE_GetHandle32(BX_reg(context)) )))
233 DX_reg(context) = dev->flags;
234 return;
237 /* it seems to be a file */
238 curr_drive = DRIVE_GetCurrentDrive();
239 DX_reg(context) = 0x0140 + curr_drive + ((curr_drive > 1) ? 0x0800 : 0);
240 /* no floppy */
241 /* bits 0-5 are current drive
242 * bit 6 - file has NOT been written..FIXME: correct?
243 * bit 8 - generate int24 if no diskspace on write/ read past end of file
244 * bit 11 - media not removable
245 * bit 14 - don't set file date/time on closing
246 * bit 15 - file is remote
250 static BOOL32 ioctlGenericBlkDevReq( CONTEXT *context )
252 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
253 int drive = DOS_GET_DRIVE( BL_reg(context) );
255 if (!DRIVE_IsValid(drive))
257 SetLastError( ERROR_FILE_NOT_FOUND );
258 return TRUE;
261 if (CH_reg(context) != 0x08)
263 INT_BARF( context, 0x21 );
264 return FALSE;
267 switch (CL_reg(context))
269 case 0x4a: /* lock logical volume */
270 TRACE(int21,"lock logical volume (%d) level %d mode %d\n",drive,BH_reg(context),DX_reg(context));
271 break;
273 case 0x60: /* get device parameters */
274 /* used by w4wgrp's winfile */
275 memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
276 dataptr[0] = 0x04;
277 dataptr[6] = 0; /* media type */
278 if (drive > 1)
280 dataptr[1] = 0x05; /* fixed disk */
281 setword(&dataptr[2], 0x01); /* non removable */
282 setword(&dataptr[4], 0x300); /* # of cylinders */
284 else
286 dataptr[1] = 0x07; /* block dev, floppy */
287 setword(&dataptr[2], 0x02); /* removable */
288 setword(&dataptr[4], 80); /* # of cylinders */
290 CreateBPB(drive, &dataptr[7], TRUE);
291 RESET_CFLAG(context);
292 break;
294 case 0x41: /* write logical device track */
295 case 0x61: /* read logical device track */
297 BYTE drive = BL_reg(context) ?
298 BL_reg(context) : DRIVE_GetCurrentDrive();
299 WORD head = *(WORD *)dataptr+1;
300 WORD cyl = *(WORD *)dataptr+3;
301 WORD sect = *(WORD *)dataptr+5;
302 WORD nrsect = *(WORD *)dataptr+7;
303 BYTE *data = (BYTE *)dataptr+9;
304 int (*raw_func)(BYTE, DWORD, DWORD, BYTE *, BOOL32);
306 raw_func = (CL_reg(context) == 0x41) ?
307 DRIVE_RawWrite : DRIVE_RawRead;
309 if (raw_func(drive, head*cyl*sect, nrsect, data, FALSE))
310 RESET_CFLAG(context);
311 else
313 AX_reg(context) = 0x1e; /* read fault */
314 SET_CFLAG(context);
317 break;
318 case 0x66:/* get disk serial number */
320 char label[12],fsname[9],path[4];
321 DWORD serial;
323 strcpy(path,"x:\\");path[0]=drive+'A';
324 GetVolumeInformation32A(
325 path,label,12,&serial,NULL,NULL,fsname,9
327 *(WORD*)dataptr = 0;
328 memcpy(dataptr+2,&serial,4);
329 memcpy(dataptr+6,label ,11);
330 memcpy(dataptr+17,fsname,8);
332 break;
334 case 0x6a:
335 TRACE(int21,"logical volume %d unlocked.\n",drive);
336 break;
338 case 0x6f:
339 memset(dataptr+1, '\0', dataptr[0]-1);
340 dataptr[1] = dataptr[0];
341 dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
342 dataptr[3] = 0xFF; /* no physical drive */
343 break;
345 default:
346 INT_BARF( context, 0x21 );
348 return FALSE;
351 static void INT21_ParseFileNameIntoFCB( CONTEXT *context )
353 char *filename =
354 CTX_SEG_OFF_TO_LIN(context, DS_reg(context), ESI_reg(context) );
355 char *fcb =
356 CTX_SEG_OFF_TO_LIN(context, ES_reg(context), EDI_reg(context) );
357 char *buffer, *s, *d;
359 AL_reg(context) = 0xff; /* failed */
361 TRACE(int21, "filename: '%s'\n", filename);
363 buffer = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + 1);
365 s = filename;
366 d = buffer;
367 while (*s)
369 if ((*s != ' ') && (*s != '\r') && (*s != '\n'))
370 *d++ = *s++;
371 else
372 break;
375 *d = '\0';
376 DOSFS_ToDosFCBFormat(buffer, fcb + 1);
377 *fcb = 0;
378 TRACE(int21, "FCB: '%s'\n", ((CHAR *)fcb + 1));
380 HeapFree( GetProcessHeap(), 0, buffer);
382 AL_reg(context) =
383 ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0;
385 /* point DS:SI to first unparsed character */
386 SI_reg(context) += (int)s - (int)filename;
389 static void INT21_GetSystemDate( CONTEXT *context )
391 SYSTEMTIME systime;
392 GetLocalTime( &systime );
393 CX_reg(context) = systime.wYear;
394 DX_reg(context) = (systime.wMonth << 8) | systime.wDay;
395 AX_reg(context) = systime.wDayOfWeek;
398 static void INT21_GetSystemTime( CONTEXT *context )
400 SYSTEMTIME systime;
401 GetLocalTime( &systime );
402 CX_reg(context) = (systime.wHour << 8) | systime.wMinute;
403 DX_reg(context) = (systime.wSecond << 8) | (systime.wMilliseconds / 10);
406 /* Many calls translate a drive argument like this:
407 drive number (00h = default, 01h = A:, etc)
409 static char drivestring[]="default";
411 char *INT21_DriveName(int drive)
414 if(drive >0)
416 drivestring[0]= (unsigned char)drive + '@';
417 drivestring[1]=':';
418 drivestring[2]=0;
420 return drivestring;
422 static BOOL32 INT21_CreateFile( CONTEXT *context )
424 AX_reg(context) = _lcreat16( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
425 EDX_reg(context) ), CX_reg(context) );
426 return (AX_reg(context) == (WORD)HFILE_ERROR16);
430 static void OpenExistingFile( CONTEXT *context )
432 AX_reg(context) = _lopen16( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
433 AL_reg(context) );
434 if (AX_reg(context) == (WORD)HFILE_ERROR16)
436 AX_reg(context) = GetLastError();
437 SET_CFLAG(context);
441 static BOOL32 INT21_ExtendedOpenCreateFile(CONTEXT *context )
443 BOOL32 bExtendedError = FALSE;
444 BYTE action = DL_reg(context);
446 /* Shuffle arguments to call OpenExistingFile */
447 AL_reg(context) = BL_reg(context);
448 DX_reg(context) = SI_reg(context);
449 /* BX,CX and DX should be preserved */
450 OpenExistingFile(context);
452 if ((EFL_reg(context) & 0x0001) == 0) /* File exists */
454 UINT16 uReturnCX = 0;
456 /* Now decide what do do */
458 if ((action & 0x07) == 0)
460 _lclose16( AX_reg(context) );
461 AX_reg(context) = 0x0050; /*File exists*/
462 SET_CFLAG(context);
463 WARN(int21, "extended open/create: failed because file exists \n");
465 else if ((action & 0x07) == 2)
467 /* Truncate it, but first check if opened for write */
468 if ((BL_reg(context) & 0x0007)== 0)
470 _lclose16( AX_reg(context) );
471 WARN(int21, "extended open/create: failed, trunc on ro file\n");
472 AX_reg(context) = 0x000C; /*Access code invalid*/
473 SET_CFLAG(context);
475 else
477 TRACE(int21, "extended open/create: Closing before truncate\n");
478 if (_lclose16( AX_reg(context) ))
480 WARN(int21, "extended open/create: close before trunc failed\n");
481 AX_reg(context) = 0x0019; /*Seek Error*/
482 CX_reg(context) = 0;
483 SET_CFLAG(context);
485 /* Shuffle arguments to call CreateFile */
487 TRACE(int21, "extended open/create: Truncating\n");
488 AL_reg(context) = BL_reg(context);
489 /* CX is still the same */
490 DX_reg(context) = SI_reg(context);
491 bExtendedError = INT21_CreateFile(context);
493 if (EFL_reg(context) & 0x0001) /*no file open, flags set */
495 WARN(int21, "extended open/create: trunc failed\n");
496 return bExtendedError;
498 uReturnCX = 0x3;
501 else uReturnCX = 0x1;
503 CX_reg(context) = uReturnCX;
505 else /* file does not exist */
507 RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
508 if ((action & 0xF0)== 0)
510 CX_reg(context) = 0;
511 SET_CFLAG(context);
512 WARN(int21, "extended open/create: failed, file dosen't exist\n");
514 else
516 /* Shuffle arguments to call CreateFile */
517 TRACE(int21, "extended open/create: Creating\n");
518 AL_reg(context) = BL_reg(context);
519 /* CX should still be the same */
520 DX_reg(context) = SI_reg(context);
521 bExtendedError = INT21_CreateFile(context);
522 if (EFL_reg(context) & 0x0001) /*no file open, flags set */
524 WARN(int21, "extended open/create: create failed\n");
525 return bExtendedError;
527 CX_reg(context) = 2;
531 return bExtendedError;
535 static BOOL32 INT21_ChangeDir( CONTEXT *context )
537 int drive;
538 char *dirname = CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context));
540 TRACE(int21,"changedir %s\n", dirname);
541 if (dirname[0] && (dirname[1] == ':'))
543 drive = toupper(dirname[0]) - 'A';
544 dirname += 2;
546 else drive = DRIVE_GetCurrentDrive();
547 return DRIVE_Chdir( drive, dirname );
551 static int INT21_FindFirst( CONTEXT *context )
553 char *p;
554 const char *path;
555 DOS_FULL_NAME full_name;
556 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
558 path = (const char *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
559 dta->unixPath = NULL;
560 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
562 AX_reg(context) = GetLastError();
563 SET_CFLAG(context);
564 return 0;
566 dta->unixPath = HEAP_strdupA( GetProcessHeap(), 0, full_name.long_name );
567 p = strrchr( dta->unixPath, '/' );
568 *p = '\0';
570 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
571 * (doesn't matter as it is set below anyway)
573 if (!DOSFS_ToDosFCBFormat( p + 1, dta->mask ))
575 HeapFree( GetProcessHeap(), 0, dta->unixPath );
576 dta->unixPath = NULL;
577 SetLastError( ERROR_FILE_NOT_FOUND );
578 AX_reg(context) = ERROR_FILE_NOT_FOUND;
579 SET_CFLAG(context);
580 return 0;
582 dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
583 : DRIVE_GetCurrentDrive();
584 dta->count = 0;
585 dta->search_attr = CL_reg(context);
586 return 1;
590 static int INT21_FindNext( CONTEXT *context )
592 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
593 WIN32_FIND_DATA32A entry;
594 int count;
596 if (!dta->unixPath) return 0;
597 if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
598 dta->search_attr, dta->count, &entry )))
600 HeapFree( GetProcessHeap(), 0, dta->unixPath );
601 dta->unixPath = NULL;
602 return 0;
604 if ((int)dta->count + count > 0xffff)
606 WARN(int21, "Too many directory entries in %s\n", dta->unixPath );
607 HeapFree( GetProcessHeap(), 0, dta->unixPath );
608 dta->unixPath = NULL;
609 return 0;
611 dta->count += count;
612 dta->fileattr = entry.dwFileAttributes;
613 dta->filesize = entry.nFileSizeLow;
614 FileTimeToDosDateTime( &entry.ftLastWriteTime,
615 &dta->filedate, &dta->filetime );
616 strcpy( dta->filename, entry.cAlternateFileName );
617 if (!memchr(dta->mask,'?',11)) {
618 /* wildcardless search, release resources in case no findnext will
619 * be issued, and as a workaround in case file creation messes up
620 * findnext, as sometimes happens with pkunzip */
621 HeapFree( GetProcessHeap(), 0, dta->unixPath );
622 dta->unixPath = NULL;
624 return 1;
628 static BOOL32 INT21_CreateTempFile( CONTEXT *context )
630 static int counter = 0;
631 char *name = CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context) );
632 char *p = name + strlen(name);
634 /* despite what Ralf Brown says, some programs seem to call without
635 * ending backslash (DOS accepts that, so we accept it too) */
636 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
638 for (;;)
640 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
641 counter = (counter + 1) % 1000;
643 if ((AX_reg(context) = _lcreat16_uniq( name, 0 )) != (WORD)HFILE_ERROR16)
645 TRACE(int21, "created %s\n", name );
646 return TRUE;
648 if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
653 static BOOL32 INT21_GetCurrentDirectory( CONTEXT *context )
655 int drive = DOS_GET_DRIVE( DL_reg(context) );
656 char *ptr = (char *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), ESI_reg(context) );
658 if (!DRIVE_IsValid(drive))
660 SetLastError( ERROR_INVALID_DRIVE );
661 return FALSE;
663 lstrcpyn32A( ptr, DRIVE_GetDosCwd(drive), 64 );
664 AX_reg(context) = 0x0100; /* success return code */
665 return TRUE;
669 static void INT21_GetDBCSLeadTable( CONTEXT *context )
671 if (heap || INT21_CreateHeap())
672 { /* return an empty table just as DOS 4.0+ does */
673 DS_reg(context) = DosHeapHandle;
674 SI_reg(context) = (int)&heap->DummyDBCSLeadTable - (int)heap;
676 else
678 AX_reg(context) = 0x1; /* error */
679 SET_CFLAG(context);
684 static int INT21_GetDiskSerialNumber( CONTEXT *context )
686 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
687 int drive = DOS_GET_DRIVE( BL_reg(context) );
689 if (!DRIVE_IsValid(drive))
691 SetLastError( ERROR_INVALID_DRIVE );
692 return 0;
695 *(WORD *)dataptr = 0;
696 *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
697 memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
698 strncpy(dataptr + 0x11, "FAT16 ", 8);
699 return 1;
703 static int INT21_SetDiskSerialNumber( CONTEXT *context )
705 BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
706 int drive = DOS_GET_DRIVE( BL_reg(context) );
708 if (!DRIVE_IsValid(drive))
710 SetLastError( ERROR_INVALID_DRIVE );
711 return 0;
714 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
715 return 1;
719 /* microsoft's programmers should be shot for using CP/M style int21
720 calls in Windows for Workgroup's winfile.exe */
722 static int INT21_FindFirstFCB( CONTEXT *context )
724 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
725 FINDFILE_FCB *pFCB;
726 LPCSTR root, cwd;
727 int drive;
729 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
730 else pFCB = (FINDFILE_FCB *)fcb;
731 drive = DOS_GET_DRIVE( pFCB->drive );
732 if (!DRIVE_IsValid( drive )) return 0;
733 root = DRIVE_GetRoot( drive );
734 cwd = DRIVE_GetUnixCwd( drive );
735 pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
736 strlen(root)+strlen(cwd)+2 );
737 if (!pFCB->unixPath) return 0;
738 strcpy( pFCB->unixPath, root );
739 strcat( pFCB->unixPath, "/" );
740 strcat( pFCB->unixPath, cwd );
741 pFCB->count = 0;
742 return 1;
746 static int INT21_FindNextFCB( CONTEXT *context )
748 BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
749 FINDFILE_FCB *pFCB;
750 DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA(context);
751 WIN32_FIND_DATA32A entry;
752 BYTE attr;
753 int count;
755 if (*fcb == 0xff) /* extended FCB ? */
757 attr = fcb[6];
758 pFCB = (FINDFILE_FCB *)(fcb + 7);
760 else
762 attr = 0;
763 pFCB = (FINDFILE_FCB *)fcb;
766 if (!pFCB->unixPath) return 0;
767 if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
768 DOS_GET_DRIVE( pFCB->drive ), attr,
769 pFCB->count, &entry )))
771 HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
772 pFCB->unixPath = NULL;
773 return 0;
775 pFCB->count += count;
777 if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
778 *(BYTE *)pResult = 0xff;
779 (BYTE *)pResult +=6; /* leave reserved field behind */
780 *(BYTE *)pResult = entry.dwFileAttributes;
781 ((BYTE *)pResult)++;
783 *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
784 ((BYTE *)pResult)++;
785 pResult->fileattr = entry.dwFileAttributes;
786 pResult->cluster = 0; /* what else? */
787 pResult->filesize = entry.nFileSizeLow;
788 memset( pResult->reserved, 0, sizeof(pResult->reserved) );
789 FileTimeToDosDateTime( &entry.ftLastWriteTime,
790 &pResult->filedate, &pResult->filetime );
792 /* Convert file name to FCB format */
794 memset( pResult->filename, ' ', sizeof(pResult->filename) );
795 if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
796 else if (!strcmp( entry.cAlternateFileName, ".." ))
797 pResult->filename[0] = pResult->filename[1] = '.';
798 else
800 char *p = strrchr( entry.cAlternateFileName, '.' );
801 if (p && p[1] && (p != entry.cAlternateFileName))
803 memcpy( pResult->filename, entry.cAlternateFileName,
804 MIN( (p - entry.cAlternateFileName), 8 ) );
805 memcpy( pResult->filename + 8, p + 1, MIN( strlen(p), 3 ) );
807 else
808 memcpy( pResult->filename, entry.cAlternateFileName,
809 MIN( strlen(entry.cAlternateFileName), 8 ) );
811 return 1;
815 static void DeleteFileFCB( CONTEXT *context )
817 FIXME(int21, "(%p): stub\n", context);
820 static void RenameFileFCB( CONTEXT *context )
822 FIXME(int21, "(%p): stub\n", context);
827 static void fLock( CONTEXT * context )
830 switch ( AX_reg(context) & 0xff )
832 case 0x00: /* LOCK */
833 TRACE(int21,"lock handle %d offset %ld length %ld\n",
834 BX_reg(context),
835 MAKELONG(DX_reg(context),CX_reg(context)),
836 MAKELONG(DI_reg(context),SI_reg(context))) ;
837 if (!LockFile(FILE_GetHandle32(BX_reg(context)),
838 MAKELONG(DX_reg(context),CX_reg(context)), 0,
839 MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
840 AX_reg(context) = GetLastError();
841 SET_CFLAG(context);
843 break;
845 case 0x01: /* UNLOCK */
846 TRACE(int21,"unlock handle %d offset %ld length %ld\n",
847 BX_reg(context),
848 MAKELONG(DX_reg(context),CX_reg(context)),
849 MAKELONG(DI_reg(context),SI_reg(context))) ;
850 if (!UnlockFile(FILE_GetHandle32(BX_reg(context)),
851 MAKELONG(DX_reg(context),CX_reg(context)), 0,
852 MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
853 AX_reg(context) = GetLastError();
854 SET_CFLAG(context);
856 return;
857 default:
858 AX_reg(context) = 0x0001;
859 SET_CFLAG(context);
860 return;
864 static BOOL32
865 INT21_networkfunc (CONTEXT *context)
867 switch (AL_reg(context)) {
868 case 0x00: /* Get machine name. */
870 char *dst = CTX_SEG_OFF_TO_LIN (context,DS_reg(context),EDX_reg(context));
871 TRACE(int21, "getting machine name to %p\n", dst);
872 if (gethostname (dst, 15))
874 WARN(int21,"failed!\n");
875 SetLastError( ER_NoNetwork );
876 return TRUE;
877 } else {
878 int len = strlen (dst);
879 while (len < 15)
880 dst[len++] = ' ';
881 dst[15] = 0;
882 CH_reg(context) = 1; /* Valid */
883 CL_reg(context) = 1; /* NETbios number??? */
884 TRACE(int21, "returning %s\n", debugstr_an (dst, 16));
885 return FALSE;
889 default:
890 SetLastError( ER_NoNetwork );
891 return TRUE;
895 static void INT21_SetCurrentPSP(WORD psp)
897 #ifdef MZ_SUPPORTED
898 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
899 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
901 GlobalUnlock16( GetCurrentTask() );
902 if (pModule->lpDosTask)
903 pModule->lpDosTask->psp_seg = psp;
904 else
905 #endif
906 ERR(int21, "Cannot change PSP for non-DOS task!\n");
909 static WORD INT21_GetCurrentPSP()
911 #ifdef MZ_SUPPORTED
912 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
913 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
915 GlobalUnlock16( GetCurrentTask() );
916 if (pModule->lpDosTask)
917 return pModule->lpDosTask->psp_seg;
918 else
919 #endif
920 return GetCurrentPDB();
924 SEGPTR INT21_GetListOfLists()
926 static DOS_LISTOFLISTS *LOL;
927 static SEGPTR seg_LOL;
930 Output of DOS 6.22:
932 0133:0020 6A 13-33 01 CC 00 33 01 59 00 j.3...3.Y.
933 0133:0030 70 00 00 00 72 02 00 02-6D 00 33 01 00 00 2E 05 p...r...m.3.....
934 0133:0040 00 00 FC 04 00 00 03 08-92 21 11 E0 04 80 C6 0D .........!......
935 0133:0050 CC 0D 4E 55 4C 20 20 20-20 20 00 00 00 00 00 00 ..NUL ......
936 0133:0060 00 4B BA C1 06 14 00 00-00 03 01 00 04 70 CE FF .K...........p..
937 0133:0070 FF 00 00 00 00 00 00 00-00 01 00 00 0D 05 00 00 ................
938 0133:0080 00 FF FF 00 00 00 00 FE-00 00 F8 03 FF 9F 70 02 ..............p.
939 0133:0090 D0 44 C8 FD D4 44 C8 FD-D4 44 C8 FD D0 44 C8 FD .D...D...D...D..
940 0133:00A0 D0 44 C8 FD D0 44 .D...D
942 if (!LOL) {
943 LOL = SEGPTR_ALLOC(sizeof(DOS_LISTOFLISTS));
945 LOL->CX_Int21_5e01 = 0x0;
946 LOL->LRU_count_FCB_cache = 0x0;
947 LOL->LRU_count_FCB_open = 0x0;
948 LOL->OEM_func_handler = -1; /* not available */
949 LOL->INT21_offset = 0x0;
950 LOL->sharing_retry_count = sharing_retries; /* default value: 3 */
951 LOL->sharing_retry_delay = sharing_pause; /* default value: 1 */
952 LOL->ptr_disk_buf = 0x0;
953 LOL->offs_unread_CON = 0x0;
954 LOL->seg_first_MCB = 0x0;
955 LOL->ptr_first_DPB = 0x0;
956 LOL->ptr_first_SysFileTable = 0x0;
957 LOL->ptr_clock_dev_hdr = 0x0;
958 LOL->ptr_CON_dev_hdr = 0x0;
959 LOL->max_byte_per_sec = 512;
960 LOL->ptr_disk_buf_info = 0x0;
961 LOL->ptr_array_CDS = 0x0;
962 LOL->ptr_sys_FCB = 0x0;
963 LOL->nr_protect_FCB = 0x0;
964 LOL->nr_block_dev = 0x0;
965 LOL->nr_avail_drive_letters = 26; /* A - Z */
966 LOL->nr_drives_JOINed = 0x0;
967 LOL->ptr_spec_prg_names = 0x0;
968 LOL->ptr_SETVER_prg_list = 0x0; /* no SETVER list */
969 LOL->DOS_HIGH_A20_func_offs = 0x0;
970 LOL->PSP_last_exec = 0x0;
971 LOL->BUFFERS_val = 99; /* maximum: 99 */
972 LOL->BUFFERS_nr_lookahead = 8; /* maximum: 8 */
973 LOL->boot_drive = 3; /* C: */
974 LOL->flag_DWORD_moves = 0x01; /* i386+ */
975 LOL->size_extended_mem = 0xf000; /* very high value */
977 if (!seg_LOL) seg_LOL = SEGPTR_GET(LOL);
978 return seg_LOL+(WORD)&((DOS_LISTOFLISTS*)0)->ptr_first_DPB;
982 /***********************************************************************
983 * INT21_GetExtendedError
985 static void INT21_GetExtendedError( CONTEXT *context )
987 BYTE class, action, locus;
988 WORD error = GetLastError();
990 switch(error)
992 case ERROR_SUCCESS:
993 class = action = locus = 0;
994 break;
995 case ERROR_DIR_NOT_EMPTY:
996 class = EC_Exists;
997 action = SA_Ignore;
998 locus = EL_Disk;
999 break;
1000 case ERROR_ACCESS_DENIED:
1001 class = EC_AccessDenied;
1002 action = SA_Abort;
1003 locus = EL_Disk;
1004 break;
1005 case ERROR_CANNOT_MAKE:
1006 class = EC_AccessDenied;
1007 action = SA_Abort;
1008 locus = EL_Unknown;
1009 break;
1010 case ERROR_HANDLE_DISK_FULL:
1011 class = EC_MediaError;
1012 action = SA_Abort;
1013 locus = EL_Disk;
1014 break;
1015 case ERROR_FILE_EXISTS:
1016 class = EC_Exists;
1017 action = SA_Abort;
1018 locus = EL_Disk;
1019 break;
1020 case ERROR_FILE_NOT_FOUND:
1021 class = EC_NotFound;
1022 action = SA_Abort;
1023 locus = EL_Disk;
1024 break;
1025 case ER_GeneralFailure:
1026 class = EC_SystemFailure;
1027 action = SA_Abort;
1028 locus = EL_Unknown;
1029 break;
1030 case ERROR_INVALID_DRIVE:
1031 class = EC_MediaError;
1032 action = SA_Abort;
1033 locus = EL_Disk;
1034 break;
1035 case ERROR_INVALID_HANDLE:
1036 class = EC_ProgramError;
1037 action = SA_Abort;
1038 locus = EL_Disk;
1039 break;
1040 case ERROR_LOCK_VIOLATION:
1041 class = EC_AccessDenied;
1042 action = SA_Abort;
1043 locus = EL_Disk;
1044 break;
1045 case ERROR_NO_MORE_FILES:
1046 class = EC_MediaError;
1047 action = SA_Abort;
1048 locus = EL_Disk;
1049 break;
1050 case ER_NoNetwork:
1051 class = EC_NotFound;
1052 action = SA_Abort;
1053 locus = EL_Network;
1054 break;
1055 case ERROR_NOT_ENOUGH_MEMORY:
1056 class = EC_OutOfResource;
1057 action = SA_Abort;
1058 locus = EL_Memory;
1059 break;
1060 case ERROR_PATH_NOT_FOUND:
1061 class = EC_NotFound;
1062 action = SA_Abort;
1063 locus = EL_Disk;
1064 break;
1065 case ERROR_SEEK:
1066 class = EC_NotFound;
1067 action = SA_Ignore;
1068 locus = EL_Disk;
1069 break;
1070 case ERROR_SHARING_VIOLATION:
1071 class = EC_Temporary;
1072 action = SA_Retry;
1073 locus = EL_Disk;
1074 break;
1075 case ERROR_TOO_MANY_OPEN_FILES:
1076 class = EC_ProgramError;
1077 action = SA_Abort;
1078 locus = EL_Disk;
1079 break;
1080 default:
1081 FIXME( int21, "Unknown error %d\n", error );
1082 class = EC_SystemFailure;
1083 action = SA_Abort;
1084 locus = EL_Unknown;
1085 break;
1087 TRACE( int21, "GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
1088 error, class, action, locus );
1089 AX_reg(context) = error;
1090 BH_reg(context) = class;
1091 BL_reg(context) = action;
1092 CH_reg(context) = locus;
1095 /***********************************************************************
1096 * DOS3Call (KERNEL.102)
1098 void WINAPI DOS3Call( CONTEXT *context )
1100 BOOL32 bSetDOSExtendedError = FALSE;
1103 TRACE(int21, "AX=%04x BX=%04x CX=%04x DX=%04x "
1104 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1105 AX_reg(context), BX_reg(context), CX_reg(context), DX_reg(context),
1106 SI_reg(context), DI_reg(context),
1107 (WORD)DS_reg(context), (WORD)ES_reg(context),
1108 EFL_reg(context) );
1111 if (AH_reg(context) == 0x59) /* Get extended error info */
1113 INT21_GetExtendedError( context );
1114 return;
1117 if (AH_reg(context) == 0x0C) /* Flush buffer and read standard input */
1119 TRACE(int21, "FLUSH BUFFER AND READ STANDARD INPUT\n");
1120 /* no flush here yet */
1121 AH_reg(context) = AL_reg(context);
1124 if (AH_reg(context)>=0x2f) {
1125 /* extended error is used by (at least) functions 0x2f to 0x62 */
1126 SetLastError(0);
1128 RESET_CFLAG(context); /* Not sure if this is a good idea */
1130 switch(AH_reg(context))
1132 case 0x03: /* READ CHARACTER FROM STDAUX */
1133 case 0x04: /* WRITE CHARACTER TO STDAUX */
1134 case 0x05: /* WRITE CHARACTER TO PRINTER */
1135 case 0x0f: /* OPEN FILE USING FCB */
1136 case 0x10: /* CLOSE FILE USING FCB */
1137 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
1138 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
1139 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
1140 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
1141 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
1142 case 0x23: /* GET FILE SIZE FOR FCB */
1143 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
1144 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
1145 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
1146 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
1147 case 0x54: /* GET VERIFY FLAG */
1148 INT_BARF( context, 0x21 );
1149 break;
1151 case 0x00: /* TERMINATE PROGRAM */
1152 TRACE(int21,"TERMINATE PROGRAM\n");
1153 ExitProcess( 0 );
1154 break;
1156 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
1157 TRACE(int21,"DIRECT CHARACTER INPUT WITH ECHO\n");
1158 AL_reg(context) = CONSOLE_GetCharacter();
1159 /* FIXME: no echo */
1160 break;
1162 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
1163 TRACE(int21, "Write Character to Standard Output\n");
1164 CONSOLE_Write(DL_reg(context), 0, 0, 0);
1165 break;
1167 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
1168 TRACE(int21, "Direct Console Input/Output\n");
1169 if (DL_reg(context) == 0xff) {
1170 FIXME(int21,"Direct Console Input should not block\n");
1171 AL_reg(context) = CONSOLE_GetCharacter();
1172 FL_reg(context) &= ~0x40; /* clear ZF */
1173 } else
1174 CONSOLE_Write(DL_reg(context), 0, 0, 0);
1175 break;
1177 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
1178 TRACE(int21,"DIRECT CHARACTER INPUT WITHOUT ECHO\n");
1179 AL_reg(context) = CONSOLE_GetCharacter();
1180 break;
1182 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
1183 TRACE(int21,"CHARACTER INPUT WITHOUT ECHO\n");
1184 AL_reg(context) = CONSOLE_GetCharacter();
1185 break;
1187 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
1188 TRACE(int21,"WRITE '$'-terminated string from %04lX:%04X to stdout\n",
1189 DS_reg(context),DX_reg(context) );
1191 LPSTR data = CTX_SEG_OFF_TO_LIN(context,DS_reg(context),EDX_reg(context));
1192 LPSTR p = data;
1193 /* do NOT use strchr() to calculate the string length,
1194 as '\0' is valid string content, too !
1195 Maybe we should check for non-'$' strings, but DOS doesn't. */
1196 while (*p != '$') p++;
1197 _hwrite16( 1, data, (int)p - (int)data);
1198 AL_reg(context) = '$'; /* yes, '$' (0x24) gets returned in AL */
1200 break;
1202 case 0x0a: /* BUFFERED INPUT */
1204 char *buffer = ((char *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1205 EDX_reg(context) ));
1206 int res;
1208 TRACE(int21,"BUFFERED INPUT (size=%d)\n",buffer[0]);
1209 if (buffer[1])
1210 TRACE(int21,"Handle old chars in buffer!\n");
1211 res=_lread16( 0, buffer+2,buffer[0]);
1212 buffer[1]=res;
1213 if(buffer[res+1] == '\n')
1214 buffer[res+1] = '\r';
1215 break;
1218 case 0x0b: {/* GET STDIN STATUS */
1219 char x1,x2;
1221 if (CONSOLE_CheckForKeystroke(&x1,&x2))
1222 AL_reg(context) = 0xff;
1223 else
1224 AL_reg(context) = 0;
1225 break;
1227 case 0x2e: /* SET VERIFY FLAG */
1228 TRACE(int21,"SET VERIFY FLAG ignored\n");
1229 /* we cannot change the behaviour anyway, so just ignore it */
1230 break;
1232 case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
1233 case 0x1d:
1234 case 0x1e:
1235 case 0x20:
1236 case 0x6b: /* NULL FUNCTION */
1237 AL_reg(context) = 0;
1238 break;
1240 case 0x5c: /* "FLOCK" - RECORD LOCKING */
1241 fLock(context);
1242 break;
1244 case 0x0d: /* DISK BUFFER FLUSH */
1245 TRACE(int21,"DISK BUFFER FLUSH ignored\n");
1246 RESET_CFLAG(context); /* dos 6+ only */
1247 break;
1249 case 0x0e: /* SELECT DEFAULT DRIVE */
1250 TRACE(int21,"SELECT DEFAULT DRIVE %d\n", DL_reg(context));
1251 DRIVE_SetCurrentDrive( DL_reg(context) );
1252 AL_reg(context) = MAX_DOS_DRIVES;
1253 break;
1255 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1256 TRACE(int21,"FIND FIRST MATCHING FILE USING FCB %p\n",
1257 CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1258 if (!INT21_FindFirstFCB(context))
1260 AL_reg(context) = 0xff;
1261 break;
1263 /* else fall through */
1265 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1266 AL_reg(context) = INT21_FindNextFCB(context) ? 0x00 : 0xff;
1267 break;
1269 case 0x13: /* DELETE FILE USING FCB */
1270 DeleteFileFCB(context);
1271 break;
1273 case 0x17: /* RENAME FILE USING FCB */
1274 RenameFileFCB(context);
1275 break;
1277 case 0x19: /* GET CURRENT DEFAULT DRIVE */
1278 AL_reg(context) = DRIVE_GetCurrentDrive();
1279 break;
1281 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1283 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1284 pTask->dta = PTR_SEG_OFF_TO_SEGPTR(DS_reg(context),DX_reg(context));
1285 TRACE(int21, "Set DTA: %08lx\n", pTask->dta);
1287 break;
1289 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1290 DL_reg(context) = 0;
1291 if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1292 break;
1294 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1295 if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1296 break;
1298 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1299 GetDrivePB(context, DRIVE_GetCurrentDrive());
1300 break;
1302 case 0x25: /* SET INTERRUPT VECTOR */
1303 INT_CtxSetHandler( context, AL_reg(context),
1304 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1305 DX_reg(context)));
1306 break;
1308 case 0x29: /* PARSE FILENAME INTO FCB */
1309 INT21_ParseFileNameIntoFCB(context);
1310 break;
1312 case 0x2a: /* GET SYSTEM DATE */
1313 INT21_GetSystemDate(context);
1314 break;
1316 case 0x2b: /* SET SYSTEM DATE */
1317 FIXME(int21, "SetSystemDate(%02d/%02d/%04d): not allowed\n",
1318 DL_reg(context), DH_reg(context), CX_reg(context) );
1319 AL_reg(context) = 0; /* Let's pretend we succeeded */
1320 break;
1322 case 0x2c: /* GET SYSTEM TIME */
1323 INT21_GetSystemTime(context);
1324 break;
1326 case 0x2d: /* SET SYSTEM TIME */
1327 FIXME(int21, "SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1328 CH_reg(context), CL_reg(context),
1329 DH_reg(context), DL_reg(context) );
1330 AL_reg(context) = 0; /* Let's pretend we succeeded */
1331 break;
1333 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1334 TRACE(int21,"GET DISK TRANSFER AREA ADDRESS\n");
1336 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1337 ES_reg(context) = SELECTOROF( pTask->dta );
1338 BX_reg(context) = OFFSETOF( pTask->dta );
1340 break;
1342 case 0x30: /* GET DOS VERSION */
1343 TRACE(int21,"GET DOS VERSION %s requested\n",
1344 (AL_reg(context) == 0x00)?"OEM number":"version flag");
1345 AX_reg(context) = (HIWORD(GetVersion16()) >> 8) |
1346 (HIWORD(GetVersion16()) << 8);
1347 #if 0
1348 AH_reg(context) = 0x7;
1349 AL_reg(context) = 0xA;
1350 #endif
1352 BX_reg(context) = 0x00FF; /* 0x123456 is Wine's serial # */
1353 CX_reg(context) = 0x0000;
1354 break;
1356 case 0x31: /* TERMINATE AND STAY RESIDENT */
1357 FIXME(int21,"TERMINATE AND STAY RESIDENT stub\n");
1358 break;
1360 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1361 TRACE(int21,"GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
1362 INT21_DriveName( DL_reg(context)));
1363 GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
1364 break;
1366 case 0x33: /* MULTIPLEXED */
1367 switch (AL_reg(context))
1369 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1370 TRACE(int21,"GET CURRENT EXTENDED BREAK STATE\n");
1371 DL_reg(context) = DOSCONF_config.brk_flag;
1372 break;
1374 case 0x01: /* SET EXTENDED BREAK STATE */
1375 TRACE(int21,"SET CURRENT EXTENDED BREAK STATE\n");
1376 DOSCONF_config.brk_flag = (DL_reg(context) > 0);
1377 break;
1379 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1380 TRACE(int21,"GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE\n");
1381 /* ugly coding in order to stay reentrant */
1382 if (DL_reg(context))
1384 DL_reg(context) = DOSCONF_config.brk_flag;
1385 DOSCONF_config.brk_flag = 1;
1387 else
1389 DL_reg(context) = DOSCONF_config.brk_flag;
1390 DOSCONF_config.brk_flag = 0;
1392 break;
1394 case 0x05: /* GET BOOT DRIVE */
1395 TRACE(int21,"GET BOOT DRIVE\n");
1396 DL_reg(context) = 3;
1397 /* c: is Wine's bootdrive (a: is 1)*/
1398 break;
1400 case 0x06: /* GET TRUE VERSION NUMBER */
1401 TRACE(int21,"GET TRUE VERSION NUMBER\n");
1402 BX_reg(context) = (HIWORD(GetVersion16() >> 8)) |
1403 (HIWORD(GetVersion16() << 8));
1404 DX_reg(context) = 0x00;
1405 break;
1407 default:
1408 INT_BARF( context, 0x21 );
1409 break;
1411 break;
1413 case 0x34: /* GET ADDRESS OF INDOS FLAG */
1414 TRACE(int21,"GET ADDRESS OF INDOS FLAG\n");
1415 if (!heap) INT21_CreateHeap();
1416 ES_reg(context) = DosHeapHandle;
1417 BX_reg(context) = (int)&heap->InDosFlag - (int)heap;
1418 break;
1420 case 0x35: /* GET INTERRUPT VECTOR */
1421 TRACE(int21,"GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
1423 FARPROC16 addr = INT_CtxGetHandler( context, AL_reg(context) );
1424 ES_reg(context) = SELECTOROF(addr);
1425 BX_reg(context) = OFFSETOF(addr);
1427 break;
1429 case 0x36: /* GET FREE DISK SPACE */
1430 TRACE(int21,"GET FREE DISK SPACE FOR DRIVE %s\n",
1431 INT21_DriveName( DL_reg(context)));
1432 if (!INT21_GetFreeDiskSpace(context)) AX_reg(context) = 0xffff;
1433 break;
1435 case 0x37:
1437 unsigned char switchchar='/';
1438 switch (AL_reg(context))
1440 case 0x00: /* "SWITCHAR" - GET SWITCH CHARACTER */
1441 TRACE(int21,"SWITCHAR - GET SWITCH CHARACTER\n");
1442 AL_reg(context) = 0x00; /* success*/
1443 DL_reg(context) = switchchar;
1444 break;
1445 case 0x01: /*"SWITCHAR" - SET SWITCH CHARACTER*/
1446 TRACE(int21,"SWITCHAR - SET SWITCH CHARACTER\n");
1447 switchchar = DL_reg(context);
1448 AL_reg(context) = 0x00; /* success*/
1449 break;
1450 default: /*"AVAILDEV" - SPECIFY \DEV\ PREFIX USE*/
1451 INT_BARF( context, 0x21 );
1452 break;
1454 break;
1457 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1458 TRACE(int21,"GET COUNTRY-SPECIFIC INFORMATION for country 0x%02x\n",
1459 AL_reg(context));
1460 AX_reg(context) = 0x02; /* no country support available */
1461 SET_CFLAG(context);
1462 break;
1464 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1465 TRACE(int21,"MKDIR %s\n",
1466 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1467 bSetDOSExtendedError = (!CreateDirectory16( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1468 EDX_reg(context) ), NULL));
1469 break;
1471 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1472 TRACE(int21,"RMDIR %s\n",
1473 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1474 bSetDOSExtendedError = (!RemoveDirectory16( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1475 EDX_reg(context) )));
1476 break;
1478 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1479 TRACE(int21,"CHDIR %s\n",
1480 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1481 bSetDOSExtendedError = !INT21_ChangeDir(context);
1482 break;
1484 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1485 TRACE(int21,"CREAT flag 0x%02x %s\n",CX_reg(context),
1486 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1487 AX_reg(context) = _lcreat16( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1488 EDX_reg(context) ), CX_reg(context) );
1489 bSetDOSExtendedError = (AX_reg(context) == (WORD)HFILE_ERROR16);
1490 break;
1492 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1493 TRACE(int21,"OPEN mode 0x%02x %s\n",AL_reg(context),
1494 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1495 OpenExistingFile(context);
1496 break;
1498 case 0x3e: /* "CLOSE" - CLOSE FILE */
1499 TRACE(int21,"CLOSE handle %d\n",BX_reg(context));
1500 bSetDOSExtendedError = ((AX_reg(context) = _lclose16( BX_reg(context) )) != 0);
1501 break;
1503 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1504 TRACE(int21,"READ from %d to %04lX:%04X for %d byte\n",BX_reg(context),
1505 DS_reg(context),DX_reg(context),CX_reg(context) );
1507 LONG result;
1508 if (ISV86(context))
1509 result = _hread16( BX_reg(context),
1510 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1511 EDX_reg(context) ),
1512 CX_reg(context) );
1513 else
1514 result = WIN16_hread( BX_reg(context),
1515 PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1516 EDX_reg(context) ),
1517 CX_reg(context) );
1518 if (result == -1) bSetDOSExtendedError = TRUE;
1519 else AX_reg(context) = (WORD)result;
1521 break;
1523 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1524 TRACE(int21,"WRITE from %04lX:%04X to handle %d for %d byte\n",
1525 DS_reg(context),DX_reg(context),BX_reg(context),CX_reg(context) );
1527 LONG result = _hwrite16( BX_reg(context),
1528 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1529 EDX_reg(context) ),
1530 CX_reg(context) );
1531 if (result == -1) bSetDOSExtendedError = TRUE;
1532 else AX_reg(context) = (WORD)result;
1534 break;
1536 case 0x41: /* "UNLINK" - DELETE FILE */
1537 TRACE(int21,"UNLINK%s\n",
1538 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1539 bSetDOSExtendedError = (!DeleteFile32A( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1540 EDX_reg(context) )));
1541 break;
1543 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1544 TRACE(int21,"LSEEK handle %d offset %ld from %s\n",
1545 BX_reg(context), MAKELONG(DX_reg(context),CX_reg(context)),
1546 (AL_reg(context)==0)?"start of file":(AL_reg(context)==1)?
1547 "current file position":"end of file");
1549 LONG status = _llseek16( BX_reg(context),
1550 MAKELONG(DX_reg(context),CX_reg(context)),
1551 AL_reg(context) );
1552 if (status == -1) bSetDOSExtendedError = TRUE;
1553 else
1555 AX_reg(context) = LOWORD(status);
1556 DX_reg(context) = HIWORD(status);
1559 break;
1561 case 0x43: /* FILE ATTRIBUTES */
1562 switch (AL_reg(context))
1564 case 0x00:
1565 TRACE(int21,"GET FILE ATTRIBUTES for %s\n",
1566 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1567 AX_reg(context) = (WORD)GetFileAttributes32A(
1568 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1569 EDX_reg(context)));
1570 if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1571 else CX_reg(context) = AX_reg(context);
1572 break;
1574 case 0x01:
1575 TRACE(int21,"SET FILE ATTRIBUTES 0x%02x for %s\n", CX_reg(context),
1576 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1577 bSetDOSExtendedError =
1578 (!SetFileAttributes32A( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1579 EDX_reg(context)),
1580 CX_reg(context) ));
1581 break;
1582 case 0x02:
1583 TRACE(int21,"GET COMPRESSED FILE SIZE for %s stub\n",
1584 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1586 break;
1588 case 0x44: /* IOCTL */
1589 switch (AL_reg(context))
1591 case 0x00:
1592 ioctlGetDeviceInfo(context);
1593 break;
1595 case 0x01:
1596 break;
1597 case 0x02:{
1598 const DOS_DEVICE *dev;
1599 if ((dev = DOSFS_GetDeviceByHandle( FILE_GetHandle32(BX_reg(context)) )) &&
1600 !strcasecmp( dev->name, "SCSIMGR$" ))
1602 ASPI_DOS_HandleInt(context);
1604 break;
1606 case 0x05:{ /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1607 /*BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context));*/
1608 int drive = DOS_GET_DRIVE(BL_reg(context));
1610 FIXME(int21,"program tried to write to block device control channel of drive %d:\n",drive);
1611 /* for (i=0;i<CX_reg(context);i++)
1612 fprintf(stdnimp,"%02x ",dataptr[i]);
1613 fprintf(stdnimp,"\n");*/
1614 AX_reg(context)=CX_reg(context);
1615 break;
1617 case 0x08: /* Check if drive is removable. */
1618 TRACE(int21,"IOCTL - CHECK IF BLOCK DEVICE REMOVABLE for drive %s\n",
1619 INT21_DriveName( BL_reg(context)));
1620 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1622 case DRIVE_CANNOTDETERMINE:
1623 SetLastError( ERROR_INVALID_DRIVE );
1624 AX_reg(context) = ERROR_INVALID_DRIVE;
1625 SET_CFLAG(context);
1626 break;
1627 case DRIVE_REMOVABLE:
1628 AX_reg(context) = 0; /* removable */
1629 break;
1630 default:
1631 AX_reg(context) = 1; /* not removable */
1632 break;
1634 break;
1636 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
1637 TRACE(int21,"IOCTL - CHECK IF BLOCK DEVICE REMOTE for drive %s\n",
1638 INT21_DriveName( BL_reg(context)));
1639 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1641 case DRIVE_CANNOTDETERMINE:
1642 SetLastError( ERROR_INVALID_DRIVE );
1643 AX_reg(context) = ERROR_INVALID_DRIVE;
1644 SET_CFLAG(context);
1645 break;
1646 case DRIVE_REMOTE:
1647 DX_reg(context) = (1<<9) | (1<<12); /* remote */
1648 break;
1649 default:
1650 DX_reg(context) = 0; /* FIXME: use driver attr here */
1651 break;
1653 break;
1655 case 0x0a: /* check if handle (BX) is remote */
1656 TRACE(int21,"IOCTL - CHECK IF HANDLE %d IS REMOTE\n",BX_reg(context));
1657 /* returns DX, bit 15 set if remote, bit 14 set if date/time
1658 * not set on close
1660 DX_reg(context) = 0;
1661 break;
1663 case 0x0b: /* SET SHARING RETRY COUNT */
1664 TRACE(int21,"IOCTL - SET SHARING RETRY COUNT pause %d retries %d\n",
1665 CX_reg(context), DX_reg(context));
1666 if (!CX_reg(context))
1668 AX_reg(context) = 1;
1669 SET_CFLAG(context);
1670 break;
1672 sharing_pause = CX_reg(context);
1673 if (!DX_reg(context))
1674 sharing_retries = DX_reg(context);
1675 RESET_CFLAG(context);
1676 break;
1678 case 0x0d:
1679 TRACE(int21,"IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
1680 INT21_DriveName( BL_reg(context)));
1681 bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1682 break;
1684 case 0x0e: /* get logical drive mapping */
1685 TRACE(int21,"IOCTL - GET LOGICAL DRIVE MAP for drive %s\n",
1686 INT21_DriveName( BL_reg(context)));
1687 AL_reg(context) = 0; /* drive has no mapping - FIXME: may be wrong*/
1688 break;
1690 case 0x0F: /* Set logical drive mapping */
1692 int drive;
1693 TRACE(int21,"IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
1694 INT21_DriveName( BL_reg(context)));
1695 drive = DOS_GET_DRIVE ( BL_reg(context) );
1696 if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
1698 SET_CFLAG(context);
1699 AX_reg(context) = 0x000F; /* invalid drive */
1701 break;
1704 case 0xe0: /* Sun PC-NFS API */
1705 /* not installed */
1706 break;
1708 default:
1709 INT_BARF( context, 0x21 );
1710 break;
1712 break;
1714 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1716 HANDLE32 handle;
1717 TRACE(int21,"DUP - DUPLICATE FILE HANDLE %d\n",BX_reg(context));
1718 if ((bSetDOSExtendedError = !DuplicateHandle( GetCurrentProcess(),
1719 FILE_GetHandle32(BX_reg(context)),
1720 GetCurrentProcess(), &handle,
1721 0, TRUE, DUPLICATE_SAME_ACCESS )))
1722 AX_reg(context) = HFILE_ERROR16;
1723 else
1724 AX_reg(context) = FILE_AllocDosHandle(handle);
1725 break;
1728 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1729 TRACE(int21,"FORCEDUP - FORCE DUPLICATE FILE HANDLE %d to %d\n",
1730 BX_reg(context),CX_reg(context));
1731 bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR16);
1732 break;
1734 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1735 TRACE(int21,"CWD - GET CURRENT DIRECTORY for drive %s\n",
1736 INT21_DriveName( DL_reg(context)));
1737 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1738 break;
1740 case 0x48: /* ALLOCATE MEMORY */
1741 TRACE(int21,"ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context));
1743 LPVOID *mem;
1744 if (ISV86(context))
1746 mem= DOSMEM_GetBlock(0,(DWORD)BX_reg(context)<<4,NULL);
1747 if (mem)
1748 AX_reg(context) = DOSMEM_MapLinearToDos(mem)>>4;
1750 else
1752 mem = (LPVOID)GlobalDOSAlloc(BX_reg(context)<<4);
1753 if (mem)
1754 AX_reg(context) = (DWORD)mem&0xffff;
1756 if (!mem)
1758 SET_CFLAG(context);
1759 AX_reg(context) = 0x0008; /* insufficient memory */
1760 BX_reg(context) = DOSMEM_Available(0)>>4;
1763 break;
1765 case 0x49: /* FREE MEMORY */
1766 TRACE(int21,"FREE MEMORY segment %04lX\n", ES_reg(context));
1768 BOOL32 ret;
1769 if (ISV86(context))
1770 ret= DOSMEM_FreeBlock(0,DOSMEM_MapDosToLinear(ES_reg(context)<<4));
1771 else
1773 ret = !GlobalDOSFree(ES_reg(context));
1774 /* If we don't reset ES_reg, we will fail in the relay code */
1775 ES_reg(context)=ret;
1777 if (!ret)
1779 TRACE(int21,"FREE MEMORY failed\n");
1780 SET_CFLAG(context);
1781 AX_reg(context) = 0x0009; /* memory block address invalid */
1784 break;
1786 case 0x4a: /* RESIZE MEMORY BLOCK */
1787 TRACE(int21,"RESIZE MEMORY segment %04lX to %d paragraphs\n", ES_reg(context), BX_reg(context));
1788 if (!ISV86(context))
1789 FIXME(int21,"RESIZE MEMORY probably insufficent implementation. Expect crash soon\n");
1791 LPVOID *mem = DOSMEM_ResizeBlock(0,DOSMEM_MapDosToLinear(ES_reg(context)<<4),
1792 BX_reg(context)<<4,NULL);
1793 if (mem)
1794 AX_reg(context) = DOSMEM_MapLinearToDos(mem)>>4;
1795 else {
1796 SET_CFLAG(context);
1797 AX_reg(context) = 0x0008; /* insufficient memory */
1798 BX_reg(context) = DOSMEM_Available(0)>>4; /* not quite right */
1801 break;
1803 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1804 TRACE(int21,"EXEC %s\n",
1805 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context) ));
1806 AX_reg(context) = WinExec16( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1807 EDX_reg(context) ),
1808 SW_NORMAL );
1809 if (AX_reg(context) < 32) SET_CFLAG(context);
1810 break;
1812 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1813 TRACE(int21,"EXIT with return code %d\n",AL_reg(context));
1814 ExitProcess( AL_reg(context) );
1815 break;
1817 case 0x4d: /* GET RETURN CODE */
1818 TRACE(int21,"GET RETURN CODE (ERRORLEVEL)\n");
1819 AX_reg(context) = 0; /* normal exit */
1820 break;
1822 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1823 TRACE(int21,"FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
1824 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1825 if (!INT21_FindFirst(context)) break;
1826 /* fall through */
1828 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1829 TRACE(int21,"FINDNEXT\n");
1830 if (!INT21_FindNext(context))
1832 SetLastError( ERROR_NO_MORE_FILES );
1833 AX_reg(context) = ERROR_NO_MORE_FILES;
1834 SET_CFLAG(context);
1836 else AX_reg(context) = 0; /* OK */
1837 break;
1838 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
1839 TRACE(int21, "SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
1840 INT21_SetCurrentPSP(BX_reg(context));
1841 break;
1842 case 0x51: /* GET PSP ADDRESS */
1843 TRACE(int21,"GET CURRENT PROCESS ID (GET PSP ADDRESS)\n");
1844 /* FIXME: should we return the original DOS PSP upon */
1845 /* Windows startup ? */
1846 BX_reg(context) = INT21_GetCurrentPSP();
1847 break;
1848 case 0x62: /* GET PSP ADDRESS */
1849 TRACE(int21,"GET CURRENT PSP ADDRESS\n");
1850 /* FIXME: should we return the original DOS PSP upon */
1851 /* Windows startup ? */
1852 BX_reg(context) = INT21_GetCurrentPSP();
1853 break;
1855 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1856 TRACE(int21,"SYSVARS - GET LIST OF LISTS\n");
1858 SEGPTR lol;
1859 lol = INT21_GetListOfLists();
1860 ES_reg(context) = HIWORD(lol);
1861 BX_reg(context) = LOWORD(lol);
1863 break;
1865 case 0x56: /* "RENAME" - RENAME FILE */
1866 TRACE(int21,"RENAME %s to %s\n",
1867 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
1868 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, ES_reg(context),EDI_reg(context)));
1869 bSetDOSExtendedError =
1870 (!MoveFile32A( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
1871 CTX_SEG_OFF_TO_LIN(context, ES_reg(context),EDI_reg(context))));
1872 break;
1874 case 0x57: /* FILE DATE AND TIME */
1875 switch (AL_reg(context))
1877 case 0x00: /* Get */
1879 FILETIME filetime;
1880 TRACE(int21,"GET FILE DATE AND TIME for handle %d\n",
1881 BX_reg(context));
1882 if (!GetFileTime( FILE_GetHandle32(BX_reg(context)), NULL, NULL, &filetime ))
1883 bSetDOSExtendedError = TRUE;
1884 else FileTimeToDosDateTime( &filetime, &DX_reg(context),
1885 &CX_reg(context) );
1887 break;
1889 case 0x01: /* Set */
1891 FILETIME filetime;
1892 TRACE(int21,"SET FILE DATE AND TIME for handle %d\n",
1893 BX_reg(context));
1894 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
1895 &filetime );
1896 bSetDOSExtendedError =
1897 (!SetFileTime( FILE_GetHandle32(BX_reg(context)),
1898 NULL, NULL, &filetime ));
1900 break;
1902 break;
1904 case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1905 TRACE(int21,"GET OR SET MEMORY/UMB ALLOCATION STRATEGY subfunction %d\n",
1906 AL_reg(context));
1907 switch (AL_reg(context))
1909 case 0x00:
1910 AX_reg(context) = 1;
1911 break;
1912 case 0x02:
1913 AX_reg(context) = 0;
1914 break;
1915 case 0x01:
1916 case 0x03:
1917 break;
1919 RESET_CFLAG(context);
1920 break;
1922 case 0x5a: /* CREATE TEMPORARY FILE */
1923 TRACE(int21,"CREATE TEMPORARY FILE\n");
1924 bSetDOSExtendedError = !INT21_CreateTempFile(context);
1925 break;
1927 case 0x5b: /* CREATE NEW FILE */
1928 TRACE(int21,"CREATE NEW FILE 0x%02x for %s\n", CX_reg(context),
1929 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
1930 bSetDOSExtendedError = ((AX_reg(context) =
1931 _lcreat16_uniq( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)), 0 ))
1932 == (WORD)HFILE_ERROR16);
1933 break;
1935 case 0x5d: /* NETWORK */
1936 FIXME(int21,"Function 0x%04x not implemented.\n", AX_reg (context));
1937 /* Fix the following while you're at it. */
1938 SetLastError( ER_NoNetwork );
1939 bSetDOSExtendedError = TRUE;
1940 break;
1942 case 0x5e:
1943 bSetDOSExtendedError = INT21_networkfunc (context);
1944 break;
1946 case 0x5f: /* NETWORK */
1947 switch (AL_reg(context))
1949 case 0x07: /* ENABLE DRIVE */
1950 TRACE(int21,"ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1951 if (!DRIVE_Enable( DL_reg(context) ))
1953 SetLastError( ERROR_INVALID_DRIVE );
1954 bSetDOSExtendedError = TRUE;
1956 break;
1958 case 0x08: /* DISABLE DRIVE */
1959 TRACE(int21,"DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
1960 if (!DRIVE_Disable( DL_reg(context) ))
1962 SetLastError( ERROR_INVALID_DRIVE );
1963 bSetDOSExtendedError = TRUE;
1965 break;
1967 default:
1968 /* network software not installed */
1969 TRACE(int21,"NETWORK function AX=%04x not implemented\n",AX_reg(context));
1970 SetLastError( ER_NoNetwork );
1971 bSetDOSExtendedError = TRUE;
1972 break;
1974 break;
1976 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1977 TRACE(int21,"TRUENAME %s\n",
1978 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),ESI_reg(context)));
1980 if (!GetFullPathName32A( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
1981 ESI_reg(context)), 128,
1982 CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
1983 EDI_reg(context)),NULL))
1984 bSetDOSExtendedError = TRUE;
1985 else AX_reg(context) = 0;
1987 break;
1989 case 0x61: /* UNUSED */
1990 case 0x63: /* misc. language support */
1991 switch (AL_reg(context)) {
1992 case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
1993 INT21_GetDBCSLeadTable(context);
1994 break;
1996 break;
1997 case 0x64: /* OS/2 DOS BOX */
1998 INT_BARF( context, 0x21 );
1999 SET_CFLAG(context);
2000 break;
2002 case 0x65:{/* GET EXTENDED COUNTRY INFORMATION */
2003 extern WORD WINE_LanguageId;
2004 BYTE *dataptr=CTX_SEG_OFF_TO_LIN(context, ES_reg(context),EDI_reg(context));
2005 TRACE(int21,"GET EXTENDED COUNTRY INFORMATION code page %d country %d\n",
2006 BX_reg(context), DX_reg(context));
2007 switch (AL_reg(context)) {
2008 case 0x01:
2009 TRACE(int21,"\tget general internationalization info\n");
2010 dataptr[0] = 0x1;
2011 *(WORD*)(dataptr+1) = 41;
2012 *(WORD*)(dataptr+3) = WINE_LanguageId;
2013 *(WORD*)(dataptr+5) = CodePage;
2014 *(DWORD*)(dataptr+0x19) = 0; /* FIXME: ptr to case map routine */
2015 break;
2016 case 0x06:
2017 TRACE(int21,"\tget pointer to collating sequence table\n");
2018 dataptr[0] = 0x06;
2019 *(DWORD*)(dataptr+1) = MAKELONG(DOSMEM_CollateTable & 0xFFFF,DOSMEM_AllocSelector(DOSMEM_CollateTable>>16));
2020 CX_reg(context) = 258;/*FIXME: size of table?*/
2021 break;
2022 default:
2023 TRACE(int21,"\tunimplemented function %d\n",AL_reg(context));
2024 INT_BARF( context, 0x21 );
2025 SET_CFLAG(context);
2026 break;
2028 break;
2030 case 0x66: /* GLOBAL CODE PAGE TABLE */
2031 switch (AL_reg(context))
2033 case 0x01:
2034 TRACE(int21,"GET GLOBAL CODE PAGE TABLE\n");
2035 DX_reg(context) = BX_reg(context) = CodePage;
2036 RESET_CFLAG(context);
2037 break;
2038 case 0x02:
2039 TRACE(int21,"SET GLOBAL CODE PAGE TABLE active page %d system page %d\n",
2040 BX_reg(context),DX_reg(context));
2041 CodePage = BX_reg(context);
2042 RESET_CFLAG(context);
2043 break;
2045 break;
2047 case 0x67: /* SET HANDLE COUNT */
2048 TRACE(int21,"SET HANDLE COUNT to %d\n",BX_reg(context) );
2049 SetHandleCount16( BX_reg(context) );
2050 if (GetLastError()) bSetDOSExtendedError = TRUE;
2051 break;
2053 case 0x68: /* "FFLUSH" - COMMIT FILE */
2054 case 0x6a: /* COMMIT FILE */
2055 TRACE(int21,"FFLUSH/COMMIT handle %d\n",BX_reg(context));
2056 bSetDOSExtendedError = (!FlushFileBuffers( FILE_GetHandle32(BX_reg(context)) ));
2057 break;
2059 case 0x69: /* DISK SERIAL NUMBER */
2060 switch (AL_reg(context))
2062 case 0x00:
2063 TRACE(int21,"GET DISK SERIAL NUMBER for drive %s\n",
2064 INT21_DriveName(BL_reg(context)));
2065 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
2066 else AX_reg(context) = 0;
2067 break;
2069 case 0x01:
2070 TRACE(int21,"SET DISK SERIAL NUMBER for drive %s\n",
2071 INT21_DriveName(BL_reg(context)));
2072 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
2073 else AX_reg(context) = 1;
2074 break;
2076 break;
2078 case 0x6C: /* Extended Open/Create*/
2079 TRACE(int21,"EXTENDED OPEN/CREATE %s\n",
2080 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDI_reg(context)));
2081 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
2082 break;
2084 case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
2085 if ((GetVersion32()&0xC0000004)!=0xC0000004) {
2086 /* not supported on anything but Win95 */
2087 TRACE(int21,"LONG FILENAME functions supported only by win95\n");
2088 SET_CFLAG(context);
2089 AL_reg(context) = 0;
2090 } else
2091 switch(AL_reg(context))
2093 case 0x39: /* Create directory */
2094 TRACE(int21,"LONG FILENAME - MAKE DIRECTORY %s\n",
2095 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)));
2096 bSetDOSExtendedError = (!CreateDirectory32A(
2097 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2098 EDX_reg(context) ), NULL));
2099 break;
2100 case 0x3a: /* Remove directory */
2101 TRACE(int21,"LONG FILENAME - REMOVE DIRECTORY %s\n",
2102 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)));
2103 bSetDOSExtendedError = (!RemoveDirectory32A(
2104 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2105 EDX_reg(context) )));
2106 break;
2107 case 0x43: /* Get/Set file attributes */
2108 TRACE(int21,"LONG FILENAME -EXTENDED GET/SET FILE ATTRIBUTES %s\n",
2109 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)));
2110 switch (BL_reg(context))
2112 case 0x00: /* Get file attributes */
2113 TRACE(int21,"\tretrieve attributes\n");
2114 CX_reg(context) = (WORD)GetFileAttributes32A(
2115 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2116 EDX_reg(context)));
2117 if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
2118 break;
2119 case 0x01:
2120 TRACE(int21,"\tset attributes 0x%04x\n",CX_reg(context));
2121 bSetDOSExtendedError = (!SetFileAttributes32A(
2122 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2123 EDX_reg(context)),
2124 CX_reg(context) ) );
2125 break;
2126 default:
2127 FIXME(int21, "Unimplemented long file name function:\n");
2128 INT_BARF( context, 0x21 );
2129 SET_CFLAG(context);
2130 AL_reg(context) = 0;
2131 break;
2133 break;
2134 case 0x47: /* Get current directory */
2135 TRACE(int21," LONG FILENAME - GET CURRENT DIRECTORY for drive %s\n",
2136 INT21_DriveName(DL_reg(context)));
2137 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
2138 break;
2140 case 0x4e: /* Find first file */
2141 TRACE(int21," LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
2142 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)));
2143 /* FIXME: use attributes in CX */
2144 if ((AX_reg(context) = FindFirstFile16(
2145 CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)),
2146 (WIN32_FIND_DATA32A *)CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2147 EDI_reg(context))))
2148 == INVALID_HANDLE_VALUE16)
2149 bSetDOSExtendedError = TRUE;
2150 break;
2151 case 0x4f: /* Find next file */
2152 TRACE(int21,"LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
2153 BX_reg(context));
2154 if (!FindNextFile16( BX_reg(context),
2155 (WIN32_FIND_DATA32A *)CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2156 EDI_reg(context))))
2157 bSetDOSExtendedError = TRUE;
2158 break;
2159 case 0xa1: /* Find close */
2160 TRACE(int21,"LONG FILENAME - FINDCLOSE for handle %d\n",
2161 BX_reg(context));
2162 bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
2163 break;
2164 case 0xa0:
2165 TRACE(int21,"LONG FILENAME - GET VOLUME INFORMATION for drive %s stub\n",
2166 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context),EDX_reg(context)));
2167 break;
2168 case 0x60:
2169 switch(CL_reg(context))
2171 case 0x01: /*Get short filename or path */
2172 if (!GetShortPathName32A
2173 ( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2174 ESI_reg(context)),
2175 CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2176 EDI_reg(context)), 67))
2177 bSetDOSExtendedError = TRUE;
2178 else AX_reg(context) = 0;
2179 break;
2180 case 0x02: /*Get canonical long filename or path */
2181 if (!GetFullPathName32A
2182 ( CTX_SEG_OFF_TO_LIN(context, DS_reg(context),
2183 ESI_reg(context)), 128,
2184 CTX_SEG_OFF_TO_LIN(context, ES_reg(context),
2185 EDI_reg(context)),NULL))
2186 bSetDOSExtendedError = TRUE;
2187 else AX_reg(context) = 0;
2188 break;
2189 default:
2190 FIXME(int21, "Unimplemented long file name function:\n");
2191 INT_BARF( context, 0x21 );
2192 SET_CFLAG(context);
2193 AL_reg(context) = 0;
2194 break;
2196 break;
2197 case 0x6c: /* Create or open file */
2198 TRACE(int21,"LONG FILENAME - CREATE OR OPEN FILE %s\n",
2199 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), ESI_reg(context)));
2200 /* translate Dos 7 action to Dos 6 action */
2201 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
2202 break;
2204 case 0x3b: /* Change directory */
2205 TRACE(int21,"LONG FILENAME - CHANGE DIRECTORY %s\n",
2206 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
2207 if (!SetCurrentDirectory32A(CTX_SEG_OFF_TO_LIN(context,
2208 DS_reg(context),
2209 EDX_reg(context)
2212 SET_CFLAG(context);
2213 AL_reg(context) = GetLastError();
2215 break;
2216 case 0x41: /* Delete file */
2217 TRACE(int21,"LONG FILENAME - DELETE FILE %s\n",
2218 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)));
2219 if (!DeleteFile32A(CTX_SEG_OFF_TO_LIN(context,
2220 DS_reg(context),
2221 EDX_reg(context))
2222 )) {
2223 SET_CFLAG(context);
2224 AL_reg(context) = GetLastError();
2226 break;
2227 case 0x56: /* Move (rename) file */
2228 TRACE(int21,"LONG FILENAME - RENAME FILE %s to %s stub\n",
2229 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context)),
2230 (LPCSTR)CTX_SEG_OFF_TO_LIN(context, ES_reg(context), EDI_reg(context)));
2231 default:
2232 FIXME(int21, "Unimplemented long file name function:\n");
2233 INT_BARF( context, 0x21 );
2234 SET_CFLAG(context);
2235 AL_reg(context) = 0;
2236 break;
2238 break;
2240 case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
2241 case 0x72: /* MS-DOS 7 (Windows95) - ??? */
2242 case 0x73: /* MS-DOS 7 (Windows95) - DRIVE LOCKING ??? */
2243 TRACE(int21,"windows95 function AX %04x\n",
2244 AX_reg(context));
2245 WARN(int21, " returning unimplemented\n");
2246 SET_CFLAG(context);
2247 AL_reg(context) = 0;
2248 break;
2250 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
2251 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
2252 break;
2254 default:
2255 INT_BARF( context, 0x21 );
2256 break;
2258 } /* END OF SWITCH */
2260 if( bSetDOSExtendedError ) /* set general error condition */
2262 AX_reg(context) = GetLastError();
2263 SET_CFLAG(context);
2266 if ((EFL_reg(context) & 0x0001))
2267 TRACE(int21, "failed, error 0x%04lx\n", GetLastError() );
2269 TRACE(int21, "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
2270 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
2271 AX_reg(context), BX_reg(context), CX_reg(context),
2272 DX_reg(context), SI_reg(context), DI_reg(context),
2273 (WORD)DS_reg(context), (WORD)ES_reg(context),
2274 EFL_reg(context));
2277 FARPROC16 WINAPI GetSetKernelDOSProc(FARPROC16 DosProc)
2279 FIXME(int21, "(DosProc=0x%08x): stub\n", (UINT32)DosProc);
2280 return NULL;