Release 960114
[wine/gsoc-2012-control.git] / miscemu / int21.c
blob16b5f8f9864439f2394ee66dc054585526898dd2
1 /*
2 * (c) 1993, 1994 Erik Bos
3 */
5 #include <time.h>
6 #include <fcntl.h>
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/file.h>
11 #include <string.h>
12 #include <sys/stat.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <utime.h>
17 #include <ctype.h>
18 #include "dos_fs.h"
19 #include "drive.h"
20 #include "file.h"
21 #include "windows.h"
22 #include "msdos.h"
23 #include "registers.h"
24 #include "ldt.h"
25 #include "task.h"
26 #include "options.h"
27 #include "miscemu.h"
28 #include "xmalloc.h"
29 #include "stddebug.h"
30 #include "debug.h"
31 #ifdef __svr4__
32 /* SVR4 DOESNTdo locking the same way must implement properly */
33 #define LOCK_EX 0
34 #define LOCK_SH 1
35 #define LOCK_NB 8
36 #endif
39 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
41 /* Define the drive parameter block, as used by int21/1F
42 * and int21/32. This table can be accessed through the
43 * global 'dpb' pointer, which points into the local dos
44 * heap.
46 struct DPB
48 BYTE drive_num; /* 0=A, etc. */
49 BYTE unit_num; /* Drive's unit number (?) */
50 WORD sector_size; /* Sector size in bytes */
51 BYTE high_sector; /* Highest sector in a cluster */
52 BYTE shift; /* Shift count (?) */
53 WORD reserved; /* Number of reserved sectors at start */
54 BYTE num_FAT; /* Number of FATs */
55 WORD dir_entries; /* Number of root dir entries */
56 WORD first_data; /* First data sector */
57 WORD high_cluster; /* Highest cluster number */
58 WORD sectors_in_FAT; /* Number of sectors per FAT */
59 WORD start_dir; /* Starting sector of first dir */
60 DWORD driver_head; /* Address of device driver header (?) */
61 BYTE media_ID; /* Media ID */
62 BYTE access_flag; /* Prev. accessed flag (0=yes,0xFF=no) */
63 DWORD next; /* Pointer to next DPB in list */
64 WORD free_search; /* Free cluster search start */
65 WORD free_clusters; /* Number of free clusters (0xFFFF=unknown) */
68 WORD CodePage = 437;
69 struct DPB *dpb;
70 DWORD dpbsegptr;
72 struct DosHeap {
73 BYTE InDosFlag;
74 BYTE mediaID;
75 BYTE biosdate[8];
76 struct DPB dpb;
78 static struct DosHeap *heap;
79 static WORD DosHeapHandle;
81 WORD sharing_retries = 3; /* number of retries at sharing violation */
82 WORD sharing_pause = 1; /* pause between retries */
84 extern char TempDirectory[];
86 static int Error(int e, int class, int el)
88 return DOS_ERROR( e, class, SA_Abort, el );
92 BYTE *GetCurrentDTA(void)
94 TDB *pTask = (TDB *)GlobalLock( GetCurrentTask() );
95 return (BYTE *)PTR_SEG_TO_LIN( pTask->dta );
99 void ChopOffWhiteSpace(char *string)
101 int length;
103 for (length = strlen(string) ; length ; length--)
104 if (string[length] == ' ')
105 string[length] = '\0';
108 static void CreateBPB(int drive, BYTE *data)
110 if (drive > 1) {
111 setword(data, 512);
112 data[2] = 2;
113 setword(&data[3], 0);
114 data[5] = 2;
115 setword(&data[6], 240);
116 setword(&data[8], 64000);
117 data[0x0a] = 0xf8;
118 setword(&data[0x0b], 40);
119 setword(&data[0x0d], 56);
120 setword(&data[0x0f], 2);
121 setword(&data[0x11], 0);
122 setword(&data[0x1f], 800);
123 data[0x21] = 5;
124 setword(&data[0x22], 1);
125 } else { /* 1.44mb */
126 setword(data, 512);
127 data[2] = 2;
128 setword(&data[3], 0);
129 data[5] = 2;
130 setword(&data[6], 240);
131 setword(&data[8], 2880);
132 data[0x0a] = 0xf8;
133 setword(&data[0x0b], 6);
134 setword(&data[0x0d], 18);
135 setword(&data[0x0f], 2);
136 setword(&data[0x11], 0);
137 setword(&data[0x1f], 80);
138 data[0x21] = 7;
139 setword(&data[0x22], 2);
143 static void GetFreeDiskSpace(struct sigcontext_struct *context)
145 long size,available;
146 int drive = DOS_GET_DRIVE( DL_reg(context) );
148 if (!DRIVE_IsValid(drive))
150 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
151 AX_reg(context) = 0xffff;
152 return;
155 if (!DOS_GetFreeSpace(drive, &size, &available)) {
156 DOS_ERROR( ER_GeneralFailure, EC_MediaError, SA_Abort, EL_Disk );
157 AX_reg(context) = 0xffff;
158 return;
161 AX_reg(context) = (drive < 2) ? 1 : 64; /* 64 for hard-disks, 1 for diskettes */
162 CX_reg(context) = 512;
163 BX_reg(context) = (available / (CX_reg(context) * AX_reg(context)));
164 DX_reg(context) = (size / (CX_reg(context) * AX_reg(context)));
165 Error (0,0,0);
168 static void GetDriveAllocInfo(struct sigcontext_struct *context)
170 long size, available;
171 int drive = DOS_GET_DRIVE( DL_reg(context) );
173 if (!DRIVE_IsValid(drive))
175 AX_reg(context) = 4;
176 CX_reg(context) = 512;
177 DX_reg(context) = 0;
178 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
179 return;
182 if (!DOS_GetFreeSpace(drive, &size, &available))
184 DOS_ERROR( ER_GeneralFailure, EC_MediaError, SA_Abort, EL_Disk );
185 AX_reg(context) = 0xffff;
186 return;
189 AX_reg(context) = (drive < 2) ? 1 : 64; /* 64 for hard-disks, 1 for diskettes */
190 CX_reg(context) = 512;
191 DX_reg(context) = (size / (CX_reg(context) * AX_reg(context)));
193 heap->mediaID = 0xf0;
195 DS_reg(context) = DosHeapHandle;
196 BX_reg(context) = (int)&heap->mediaID - (int)heap;
197 Error (0,0,0);
200 static void GetDrivePB(struct sigcontext_struct *context, int drive)
202 if(!DRIVE_IsValid(drive))
204 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
205 AX_reg(context) = 0x00ff;
207 else
209 dprintf_int(stddeb, "int21: GetDrivePB not fully implemented.\n");
211 /* FIXME: I have no idea what a lot of this information should
212 * say or whether it even really matters since we're not allowing
213 * direct block access. However, some programs seem to depend on
214 * getting at least _something_ back from here. The 'next' pointer
215 * does worry me, though. Should we have a complete table of
216 * separate DPBs per drive? Probably, but I'm lazy. :-) -CH
218 dpb->drive_num = dpb->unit_num = drive; /* The same? */
219 dpb->sector_size = 512;
220 dpb->high_sector = 1;
221 dpb->shift = drive < 2 ? 0 : 6; /* 6 for HD, 0 for floppy */
222 dpb->reserved = 0;
223 dpb->num_FAT = 1;
224 dpb->dir_entries = 2;
225 dpb->first_data = 2;
226 dpb->high_cluster = 64000;
227 dpb->sectors_in_FAT = 1;
228 dpb->start_dir = 1;
229 dpb->driver_head = 0;
230 dpb->media_ID = (drive > 1) ? 0xF8 : 0xF0;
231 dpb->access_flag = 0;
232 dpb->next = 0;
233 dpb->free_search = 0;
234 dpb->free_clusters = 0xFFFF; /* unknown */
236 AL_reg(context) = 0x00;
237 DS_reg(context) = SELECTOROF(dpbsegptr);
238 BX_reg(context) = OFFSETOF(dpbsegptr);
243 static void ioctlGetDeviceInfo(struct sigcontext_struct *context)
246 dprintf_int (stddeb, "int21: ioctl (%d, GetDeviceInfo)\n", BX_reg(context));
248 switch (BX_reg(context))
250 case 0:
251 case 1:
252 DX_reg(context) = 2; /* FIXME */
253 break;
254 case 2:
255 DX_reg(context) = 0x80d0 | (1 << (BX_reg(context) != 0));
256 RESET_CFLAG(context);
257 break;
259 default:
261 struct stat sbuf;
263 if (fstat(BX_reg(context), &sbuf) < 0)
265 INT_BARF( context, 0x21 );
266 SET_CFLAG(context);
267 return;
270 DX_reg(context) = 0x0943;
271 /* bits 0-5 are current drive
272 * bit 6 - file has NOT been written..FIXME: correct?
273 * bit 8 - generate int24 if no diskspace on write/ read past end of file
274 * bit 11 - media not removable
278 RESET_CFLAG(context);
281 static void ioctlGenericBlkDevReq(struct sigcontext_struct *context)
283 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
284 int drive = DOS_GET_DRIVE( BL_reg(context) );
286 if (!DRIVE_IsValid(drive))
288 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
289 AX_reg(context) = DOS_ExtendedError;
290 SET_CFLAG(context);
291 return;
294 if (CH_reg(context) != 0x08)
296 INT_BARF( context, 0x21 );
297 return;
299 switch (CL_reg(context)) {
300 case 0x60: /* get device parameters */
301 /* used by w4wgrp's winfile */
302 memset(dataptr, 0, 0x26);
303 dataptr[0] = 0x04;
304 dataptr[6] = 0; /* media type */
305 if (drive > 1)
307 dataptr[1] = 0x05; /* fixed disk */
308 setword(&dataptr[2], 0x01); /* non removable */
309 setword(&dataptr[4], 0x300); /* # of cylinders */
311 else
313 dataptr[1] = 0x07; /* block dev, floppy */
314 setword(&dataptr[2], 0x02); /* removable */
315 setword(&dataptr[4], 80); /* # of cylinders */
317 CreateBPB(drive, &dataptr[7]);
318 RESET_CFLAG(context);
319 return;
320 default:
321 INT_BARF( context, 0x21 );
325 static void GetSystemDate(struct sigcontext_struct *context)
327 struct tm *now;
328 time_t ltime;
330 ltime = time(NULL);
331 now = localtime(&ltime);
333 CX_reg(context) = now->tm_year + 1900;
334 DX_reg(context) = ((now->tm_mon + 1) << 8) | now->tm_mday;
335 AX_reg(context) = now->tm_wday;
338 static void INT21_GetSystemTime(struct sigcontext_struct *context)
340 struct tm *now;
341 struct timeval tv;
342 time_t seconds;
344 gettimeofday(&tv,NULL); /* Note use of gettimeofday(), instead of time() */
345 seconds = tv.tv_sec;
346 now = localtime(&seconds);
348 CX_reg(context) = (now->tm_hour<<8) | now->tm_min;
349 DX_reg(context) = (now->tm_sec<<8) | tv.tv_usec/10000;
350 /* Note hundredths of seconds */
353 static void CreateFile(struct sigcontext_struct *context)
355 AX_reg(context) = _lcreat( PTR_SEG_OFF_TO_LIN( DS_reg(context),
356 DX_reg(context) ), CX_reg(context) );
357 if (AX_reg(context) == (WORD)HFILE_ERROR)
359 AX_reg(context) = DOS_ExtendedError;
360 SET_CFLAG(context);
365 void OpenExistingFile(struct sigcontext_struct *context)
367 AX_reg(context) = _lopen( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
368 AL_reg(context) );
369 if (AX_reg(context) == (WORD)HFILE_ERROR)
371 AX_reg(context) = DOS_ExtendedError;
372 SET_CFLAG(context);
374 #if 0
375 int handle;
376 int mode;
377 int lock;
379 dprintf_int (stddeb, "int21: open (%s, %d) = %d\n",
380 DOS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),
381 DX_reg(context))), mode, handle);
383 switch (AX_reg(context) & 0x0070)
385 case 0x00: /* compatability mode */
386 case 0x40: /* DENYNONE */
387 lock = -1;
388 break;
390 case 0x30: /* DENYREAD */
391 dprintf_int(stddeb,
392 "OpenExistingFile (%s): DENYREAD changed to DENYALL\n",
393 (char *)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)));
394 case 0x10: /* DENYALL */
395 lock = LOCK_EX;
396 break;
398 case 0x20: /* DENYWRITE */
399 lock = LOCK_SH;
400 break;
402 default:
403 lock = -1;
406 if (lock != -1)
409 int result,retries=sharing_retries;
411 #ifdef __svr4__
412 printf("Should call flock and needs porting to lockf\n");
413 result = 0;
414 retries = 0;
415 #else
416 result = flock(handle, lock | LOCK_NB);
417 #endif
418 if ( retries && (!result) )
420 int i;
421 for(i=0;i<32768*((int)sharing_pause);i++)
422 result++; /* stop the optimizer */
423 for(i=0;i<32768*((int)sharing_pause);i++)
424 result--;
427 while( (!result) && (!(retries--)) );
429 if(result)
431 errno_to_doserr();
432 AX_reg(context) = ExtendedError;
433 close(handle);
434 SET_CFLAG(context);
435 return;
440 Error (0,0,0);
441 AX_reg(context) = handle;
442 RESET_CFLAG(context);
443 #endif
446 static void CloseFile(struct sigcontext_struct *context)
448 if ((AX_reg(context) = _lclose( BX_reg(context) )) != 0)
450 AX_reg(context) = DOS_ExtendedError;
451 SET_CFLAG(context);
455 void ExtendedOpenCreateFile(struct sigcontext_struct *context)
457 dprintf_int(stddeb, "int21: extended open/create: file= %s \n",
458 DOSFS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),SI_reg(context)),FALSE));
459 /* Shuffle arguments to call OpenExistingFile */
460 AL_reg(context) = BL_reg(context);
461 DX_reg(context) = SI_reg(context);
462 /* BX,CX and DX should be preserved */
463 OpenExistingFile(context);
464 if ((EFL_reg(context) & 0x0001)==0)
465 { /* It exists */
466 dprintf_int(stddeb, "int21: extended open/create %s exists \n",
467 DOSFS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),SI_reg(context)),TRUE));
468 /* Now decide what do do */
469 if ((DL_reg(context) & 0x0007)== 0)
471 BX_reg(context) = AX_reg(context);
472 CloseFile(context);
473 AX_reg(context) = 0x0050;/*File exists*/
474 CX_reg(context) = 0;
475 SET_CFLAG(context);
476 dprintf_int(stddeb, "int21: extended open/create: failed because file exixts \n");
477 return;
479 if ((DL_reg(context) & 0x0007)== 2) {
480 /* Truncate it, but first check if opend for write */
481 if ((BL_reg(context) & 0x0007)== 0) {
482 BX_reg(context) = AX_reg(context);
483 CloseFile(context);
484 dprintf_int(stddeb, "int21: extended open/create: failed, trunc on ro file");
485 AX_reg(context) = 0x000C;/*Access code invalid*/
486 CX_reg(context) = 0;
487 SET_CFLAG(context);
488 return;
490 /* Shuffle arguments to call CloseFile */
491 dprintf_int(stddeb, "int21: extended open/create: Closing before truncate\n");
492 BX_reg(context) = AX_reg(context);
493 /* BX and DX should be preserved */
494 CloseFile(context);
495 if (EFL_reg(context) & 0x0001) {
496 dprintf_int(stddeb, "int21: extended open/create: close before trunc failed");
497 AX_reg(context) = 0x0019;/*Seek Error*/
498 CX_reg(context) = 0;
499 SET_CFLAG(context);
501 /* Shuffle arguments to call CreateFile */
502 dprintf_int(stddeb, "int21: extended open/create: Truncating\n");
503 AL_reg(context) = BL_reg(context);
504 /* CX is still the same */
505 DX_reg(context) = SI_reg(context);
506 CreateFile(context);
507 if (EFL_reg(context) & 0x0001) { /*no file open, flags set */
508 dprintf_int(stddeb, "int21: extended open/create: truncfailed");
509 return;
511 CX_reg(context) = 3;
512 return;
514 CX_reg(context) = 1;
515 return;
517 else /* file does not exist */
519 dprintf_int(stddeb, "int21: extended open/create %s dosen't exists \n",
520 DOSFS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),SI_reg(context)),FALSE));
521 if ((DL_reg(context) & 0x00F0)== 0) {
522 CX_reg(context) = 0;
523 SET_CFLAG(context);
524 dprintf_int(stddeb, "int21: extended open/create: failed, file dosen't exist\n");
525 return;
527 /* Shuffle arguments to call CreateFile */
528 dprintf_int(stddeb, "int21: extended open/create: Creating\n");
529 AL_reg(context) = BL_reg(context);
530 /* CX should still be the same */
531 DX_reg(context) = SI_reg(context);
532 CreateFile(context);
533 if (EFL_reg(context) & 0x0001) { /*no file open, flags set */
534 dprintf_int(stddeb, "int21: extended open/create: create failed\n");
535 return;
537 CX_reg(context) = 2;
538 return;
543 static int INT21_RenameFile(struct sigcontext_struct *context)
545 const char *newname, *oldname;
546 char *buffer;
548 /* FIXME: should not rename over an existing file */
549 dprintf_int(stddeb,"int21: renaming %s to %s\n",
550 (char *)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
551 (char *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context)));
553 oldname = DOSFS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS_reg(context),
554 DX_reg(context)), TRUE );
555 if (!oldname) return 0;
556 buffer = xstrdup( oldname );
557 newname = DOSFS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(ES_reg(context),
558 DI_reg(context)), FALSE );
559 if (!newname)
561 free( buffer );
562 return 0;
565 if (rename( buffer, newname) == -1)
567 FILE_SetDosError();
568 free( buffer );
569 return 0;
571 free( buffer );
572 return 1;
576 static void INT21_ChangeDir(struct sigcontext_struct *context)
578 int drive;
579 char *dirname = PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context));
581 dprintf_int(stddeb,"int21: changedir %s\n", dirname);
582 if (dirname[1] == ':')
584 drive = toupper(dirname[0]) - 'A';
585 dirname += 2;
587 else drive = DRIVE_GetCurrentDrive();
588 if (!DRIVE_Chdir( drive, dirname ))
590 AX_reg(context) = DOS_ExtendedError;
591 SET_CFLAG(context);
596 static int INT21_FindFirst(struct sigcontext_struct *context)
598 const char *path, *unixPath, *mask;
599 char *p;
600 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA();
602 path = (const char *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
603 dta->unixPath = NULL;
604 if (!(unixPath = DOSFS_GetUnixFileName( path, FALSE )))
606 AX_reg(context) = DOS_ExtendedError;
607 SET_CFLAG(context);
608 return 0;
610 dta->unixPath = xstrdup( unixPath );
611 p = strrchr( dta->unixPath, '/' );
612 *p = '\0';
613 if (!(mask = DOSFS_ToDosFCBFormat( p + 1 )))
615 free( dta->unixPath );
616 dta->unixPath = NULL;
617 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
618 AX_reg(context) = ER_FileNotFound;
619 SET_CFLAG(context);
620 return 0;
622 memcpy( dta->mask, mask, sizeof(dta->mask) );
623 dta->drive = (path[1] == ':') ? toupper(path[0]) - 'A'
624 : DRIVE_GetCurrentDrive();
625 dta->count = 0;
626 dta->search_attr = CL_reg(context);
627 return 1;
631 static int INT21_FindNext(struct sigcontext_struct *context)
633 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA();
634 DOS_DIRENT entry;
635 int count;
637 if (!dta->unixPath) return 0;
638 if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, dta->drive,
639 dta->search_attr, dta->count, &entry )))
641 free( dta->unixPath );
642 dta->unixPath = NULL;
643 return 0;
645 if ((int)dta->count + count > 0xffff)
647 fprintf( stderr, "Too many directory entries in %s\n", dta->unixPath );
648 free( dta->unixPath );
649 dta->unixPath = NULL;
650 return 0;
652 dta->count += count;
653 dta->fileattr = entry.attr;
654 dta->filetime = entry.time;
655 dta->filedate = entry.date;
656 dta->filesize = entry.size;
657 strcpy( dta->filename, DOSFS_ToDosDTAFormat( entry.name ) );
658 return 1;
662 static int INT21_SetFileDateTime(struct sigcontext_struct *context)
664 fprintf( stderr, "INT21_SetFileDateTime: not implemented yet.\n" );
665 return 1;
666 #if 0
667 char *filename;
668 struct utimbuf filetime;
670 /* FIXME: Argument isn't the name of the file in DS:DX,
671 but the file handle in BX */
672 filename = DOSFS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS_reg(context),
673 DX_reg(context)),TRUE );
675 filetime.actime = 0L;
676 filetime.modtime = filetime.actime;
678 utime(filename, &filetime);
679 RESET_CFLAG(context);
680 #endif
684 static int INT21_CreateTempFile(struct sigcontext_struct *context)
686 static int counter = 0;
687 char *name = PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context) );
688 char *p = name + strlen(name);
690 for (;;)
692 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
693 counter = (counter + 1) % 1000;
695 if ((AX_reg(context) = _lcreat_uniq( name, 0 )) != (WORD)HFILE_ERROR)
697 dprintf_int( stddeb, "INT21_CreateTempFile: created %s\n", name );
698 return 1;
700 if (DOS_ExtendedError != ER_FileExists) return 0;
705 static int INT21_GetCurrentDirectory(struct sigcontext_struct *context)
707 int drive = DOS_GET_DRIVE( DL_reg(context) );
708 char *ptr = (char *)PTR_SEG_OFF_TO_LIN( DS_reg(context), SI_reg(context) );
710 if (!DRIVE_IsValid(drive))
712 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
713 return 0;
716 lstrcpyn( ptr, DRIVE_GetDosCwd(drive), 64 );
717 if (!ptr[0]) strcpy( ptr, "\\" );
718 return 1;
722 static int INT21_GetDiskSerialNumber(struct sigcontext_struct *context)
724 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
725 int drive = DOS_GET_DRIVE( BL_reg(context) );
727 if (!DRIVE_IsValid(drive))
729 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
730 return 0;
733 *(WORD *)dataptr = 0;
734 *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
735 memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
736 strncpy(dataptr + 0x11, "FAT16 ", 8);
737 return 1;
741 static int INT21_SetDiskSerialNumber(struct sigcontext_struct *context)
743 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
744 int drive = DOS_GET_DRIVE( BL_reg(context) );
746 if (!DRIVE_IsValid(drive))
748 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
749 return 0;
752 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
753 return 1;
757 static void DumpFCB(BYTE *fcb)
759 int x, y;
761 fcb -= 7;
763 for (y = 0; y !=2 ; y++) {
764 for (x = 0; x!=15;x++)
765 dprintf_int(stddeb, "%02x ", *fcb++);
766 dprintf_int(stddeb,"\n");
770 /* microsoft's programmers should be shot for using CP/M style int21
771 calls in Windows for Workgroup's winfile.exe */
773 static void FindFirstFCB(struct sigcontext_struct *context)
775 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
776 struct fcb *standard_fcb;
777 struct fcb *output_fcb;
778 int drive;
779 char path[12];
781 BYTE *dta = GetCurrentDTA();
783 DumpFCB( fcb );
785 if ((*fcb) == 0xff)
787 standard_fcb = (struct fcb *)(fcb + 7);
788 output_fcb = (struct fcb *)(dta + 7);
789 *dta = 0xff;
791 else
793 standard_fcb = (struct fcb *)fcb;
794 output_fcb = (struct fcb *)dta;
797 if (standard_fcb->drive)
799 drive = standard_fcb->drive - 1;
800 if (!DRIVE_IsValid(drive))
802 DOS_ERROR(ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk);
803 AX_reg(context) = 0xff;
804 return;
807 else
808 drive = DRIVE_GetCurrentDrive();
810 output_fcb->drive = drive;
812 if (*(fcb) == 0xff)
814 if (*(fcb+6) & FA_LABEL) /* return volume label */
816 *(dta+6) = FA_LABEL;
817 memset(&output_fcb->name, ' ', 11);
818 memcpy(output_fcb->name, DRIVE_GetLabel(drive), 11);
819 AX_reg(context) = 0x00;
820 return;
824 strncpy(output_fcb->name, standard_fcb->name, 11);
825 if (*fcb == 0xff)
826 *(dta+6) = ( *(fcb+6) & (!FA_DIRECTORY));
828 #if 0
829 sprintf(path,"%c:*.*",drive+'A');
830 if ((output_fcb->directory = DOS_opendir(path))==NULL)
832 Error (PathNotFound, EC_MediaError, EL_Disk);
833 AX_reg(context) = 0xff;
834 return;
836 #endif
840 static void DeleteFileFCB(struct sigcontext_struct *context)
842 fprintf( stderr, "DeleteFileFCB: not implemented yet\n" );
843 #if 0
844 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
845 struct dosdirent *dp;
846 char temp[256], *ptr;
847 int drive = DOS_GET_DRIVE( *fcb );
849 DumpFCB( fcb );
851 temp[0] = '\\';
852 strcpy(temp+1, DRIVE_GetDosCwd(drive));
853 strcat(temp, "\\");
854 strncat(temp, fcb + 1, 8);
855 ChopOffWhiteSpace(temp);
856 strncat(temp, fcb + 9, 3);
857 ChopOffWhiteSpace(temp);
859 if ((dp = DOS_opendir(temp)) == NULL) {
860 Error(InvalidDrive, EC_MediaError , EL_Disk);
861 AX_reg(context) = 0xff;
862 return;
865 temp[0] = '\\';
866 strcpy(temp+1, DRIVE_GetDosCwd(drive) );
867 strcat(temp, "\\");
869 ptr = temp + strlen(temp);
871 while (DOS_readdir(dp) != NULL)
873 strcpy(ptr, dp->filename);
874 dprintf_int(stddeb, "int21: delete file %s\n", temp);
875 /* unlink(DOS_GetUnixFileName(temp)); */
877 DOS_closedir(dp);
878 AX_reg(context) = 0;
879 #endif
882 static void RenameFileFCB(struct sigcontext_struct *context)
884 fprintf( stderr, "RenameFileFCB: not implemented yet\n" );
885 #if 0
886 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
887 struct dosdirent *dp;
888 char temp[256], oldname[256], newname[256], *oldnameptr, *newnameptr;
889 int drive = DOS_GET_DRIVE( *fcb );
891 DumpFCB( fcb );
893 temp[0] = '\\';
894 strcpy(temp+1, DRIVE_GetDosCwd(drive) );
895 strcat(temp, "\\");
896 strncat(temp, fcb + 1, 8);
897 ChopOffWhiteSpace(temp);
898 strncat(temp, fcb + 9, 3);
899 ChopOffWhiteSpace(temp);
901 if ((dp = DOS_opendir(temp)) == NULL) {
902 Error(InvalidDrive, EC_MediaError , EL_Disk);
903 AX_reg(context) = 0xff;
904 return;
907 oldname[0] = '\\';
908 strcpy(oldname+1, DRIVE_GetDosCwd(drive) );
909 strcat(oldname, "\\");
910 strcpy( newname, oldname );
911 oldnameptr = oldname + strlen(oldname);
912 newnameptr = newname + strlen(newname);
914 while (DOS_readdir(dp) != NULL)
916 strcpy(oldnameptr, dp->filename);
917 strcpy(newnameptr, fcb + 1);
918 dprintf_int(stddeb, "int21: renamefile %s -> %s\n",
919 oldname, newname);
921 DOS_closedir(dp);
922 AX_reg(context) = 0;
923 #endif
928 static void fLock (struct sigcontext_struct * context)
930 #if 0
931 struct flock f;
932 int result,retries=sharing_retries;
934 f.l_start = MAKELONG(DX_reg(context),CX_reg(context));
935 f.l_len = MAKELONG(DI_reg(context),SI_reg(context));
936 f.l_whence = 0;
937 f.l_pid = 0;
939 switch ( AX_reg(context) & 0xff )
941 case 0x00: /* LOCK */
942 f.l_type = F_WRLCK;
943 break;
945 case 0x01: /* UNLOCK */
946 f.l_type = F_UNLCK;
947 break;
949 default:
950 AX_reg(context) = 0x0001;
951 SET_CFLAG(context);
952 return;
956 result = fcntl(BX_reg(context),F_SETLK,&f);
957 if ( retries && (!result) )
959 int i;
960 for(i=0;i<32768*((int)sharing_pause);i++)
961 result++; /* stop the optimizer */
962 for(i=0;i<32768*((int)sharing_pause);i++)
963 result--;
966 while( (!result) && (!(retries--)) );
968 if(result)
970 FILE_SetDosError();
971 AX_reg(context) = DOS_ExtendedError;
972 SET_CFLAG(context);
973 return;
975 #endif
979 static int INT21_GetFileAttribute (struct sigcontext_struct * context)
981 const char *unixName;
983 unixName = DOSFS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)), TRUE );
984 if (!unixName) return 0;
985 if (!FILE_Stat( unixName, &CL_reg(context), NULL, NULL, NULL )) return 0;
986 CH_reg(context) = 0;
987 dprintf_int( stddeb, "INT21_GetFileAttributes(%s) = 0x%x\n",
988 unixName, CX_reg(context) );
989 return 1;
993 extern void LOCAL_PrintHeap (WORD ds);
995 /***********************************************************************
996 * DOS3Call (KERNEL.102)
998 void DOS3Call( struct sigcontext_struct context )
1000 dprintf_int( stddeb, "int21: AX=%04x BX=%04x CX=%04x DX=%04x "
1001 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1002 AX_reg(&context), BX_reg(&context), CX_reg(&context),
1003 DX_reg(&context), SI_reg(&context), DI_reg(&context),
1004 DS_reg(&context), ES_reg(&context), EFL_reg(&context));
1006 if (AH_reg(&context) == 0x59) /* Get extended error info */
1008 AX_reg(&context) = DOS_ExtendedError;
1009 BH_reg(&context) = DOS_ErrorClass;
1010 BL_reg(&context) = DOS_ErrorAction;
1011 CH_reg(&context) = DOS_ErrorLocus;
1012 return;
1015 DOS_ERROR( 0, 0, 0, 0 );
1016 RESET_CFLAG(&context); /* Not sure if this is a good idea */
1018 switch(AH_reg(&context))
1020 case 0x00: /* TERMINATE PROGRAM */
1021 TASK_KillCurrentTask( 0 );
1022 break;
1024 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
1025 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
1026 case 0x03: /* READ CHARACTER FROM STDAUX */
1027 case 0x04: /* WRITE CHARACTER TO STDAUX */
1028 case 0x05: /* WRITE CHARACTER TO PRINTER */
1029 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
1030 case 0x07: /* DIRECT CHARACTER INPUT, WITHOUT ECHO */
1031 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
1032 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
1033 case 0x0a: /* BUFFERED INPUT */
1034 case 0x0b: /* GET STDIN STATUS */
1035 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
1036 case 0x0f: /* OPEN FILE USING FCB */
1037 case 0x10: /* CLOSE FILE USING FCB */
1038 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1039 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
1040 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
1041 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
1042 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
1043 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
1044 case 0x23: /* GET FILE SIZE FOR FCB */
1045 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
1046 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
1047 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
1048 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
1049 case 0x29: /* PARSE FILENAME INTO FCB */
1050 case 0x2e: /* SET VERIFY FLAG */
1051 case 0x37: /* "SWITCHAR" - GET SWITCH CHARACTER
1052 "SWITCHAR" - SET SWITCH CHARACTER
1053 "AVAILDEV" - SPECIFY \DEV\ PREFIX USE */
1054 case 0x54: /* GET VERIFY FLAG */
1055 INT_BARF( &context, 0x21 );
1056 break;
1058 case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
1059 case 0x1d:
1060 case 0x1e:
1061 case 0x20:
1062 case 0x6b: /* NULL FUNCTION */
1063 AL_reg(&context) = 0;
1064 break;
1066 case 0x5c: /* "FLOCK" - RECORD LOCKING */
1067 fLock(&context);
1068 break;
1070 case 0x0d: /* DISK BUFFER FLUSH */
1071 RESET_CFLAG(&context); /* dos 6+ only */
1072 break;
1074 case 0x0e: /* SELECT DEFAULT DRIVE */
1075 DRIVE_SetCurrentDrive( DL_reg(&context) );
1076 AL_reg(&context) = MAX_DOS_DRIVES;
1077 break;
1079 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1080 FindFirstFCB(&context);
1081 break;
1083 case 0x13: /* DELETE FILE USING FCB */
1084 DeleteFileFCB(&context);
1085 break;
1087 case 0x17: /* RENAME FILE USING FCB */
1088 RenameFileFCB(&context);
1089 break;
1091 case 0x19: /* GET CURRENT DEFAULT DRIVE */
1092 AL_reg(&context) = DRIVE_GetCurrentDrive();
1093 break;
1095 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1097 TDB *pTask = (TDB *)GlobalLock( GetCurrentTask() );
1098 pTask->dta = MAKELONG( DX_reg(&context), DS_reg(&context) );
1099 dprintf_int(stddeb, "int21: Set DTA: %08lx\n", pTask->dta);
1101 break;
1103 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1104 DL_reg(&context) = 0;
1105 GetDriveAllocInfo(&context);
1106 break;
1108 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1109 GetDriveAllocInfo(&context);
1110 break;
1112 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1113 GetDrivePB(&context, DRIVE_GetCurrentDrive());
1114 break;
1116 case 0x25: /* SET INTERRUPT VECTOR */
1117 INT_SetHandler( AL_reg(&context),
1118 MAKELONG( DX_reg(&context), DS_reg(&context) ) );
1119 break;
1121 case 0x2a: /* GET SYSTEM DATE */
1122 GetSystemDate(&context);
1123 break;
1125 case 0x2b: /* SET SYSTEM DATE */
1126 fprintf( stdnimp, "SetSystemDate(%02d/%02d/%04d): not allowed\n",
1127 DL_reg(&context), DH_reg(&context), CX_reg(&context) );
1128 AL_reg(&context) = 0; /* Let's pretend we succeeded */
1129 break;
1131 case 0x2c: /* GET SYSTEM TIME */
1132 INT21_GetSystemTime(&context);
1133 break;
1135 case 0x2d: /* SET SYSTEM TIME */
1136 fprintf( stdnimp, "SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1137 CH_reg(&context), CL_reg(&context),
1138 DH_reg(&context), DL_reg(&context) );
1139 AL_reg(&context) = 0; /* Let's pretend we succeeded */
1140 break;
1142 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1144 TDB *pTask = (TDB *)GlobalLock( GetCurrentTask() );
1145 ES_reg(&context) = SELECTOROF( pTask->dta );
1146 BX_reg(&context) = OFFSETOF( pTask->dta );
1148 break;
1150 case 0x30: /* GET DOS VERSION */
1151 AX_reg(&context) = DOSVERSION;
1152 BX_reg(&context) = 0x0012; /* 0x123456 is Wine's serial # */
1153 CX_reg(&context) = 0x3456;
1154 break;
1156 case 0x31: /* TERMINATE AND STAY RESIDENT */
1157 INT_BARF( &context, 0x21 );
1158 break;
1160 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1161 GetDrivePB(&context, DOS_GET_DRIVE( DL_reg(&context) ) );
1162 break;
1164 case 0x33: /* MULTIPLEXED */
1165 switch (AL_reg(&context))
1167 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1168 DL_reg(&context) = 0;
1169 break;
1171 case 0x01: /* SET EXTENDED BREAK STATE */
1172 break;
1174 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1175 DL_reg(&context) = 0;
1176 break;
1178 case 0x05: /* GET BOOT DRIVE */
1179 DL_reg(&context) = 2;
1180 /* c: is Wine's bootdrive */
1181 break;
1183 case 0x06: /* GET TRUE VERSION NUMBER */
1184 BX_reg(&context) = DOSVERSION;
1185 DX_reg(&context) = 0x00;
1186 break;
1188 default:
1189 INT_BARF( &context, 0x21 );
1190 break;
1192 break;
1194 case 0x34: /* GET ADDRESS OF INDOS FLAG */
1195 ES_reg(&context) = DosHeapHandle;
1196 BX_reg(&context) = (int)&heap->InDosFlag - (int)heap;
1197 break;
1199 case 0x35: /* GET INTERRUPT VECTOR */
1201 SEGPTR addr = INT_GetHandler( AL_reg(&context) );
1202 ES_reg(&context) = SELECTOROF(addr);
1203 BX_reg(&context) = OFFSETOF(addr);
1205 break;
1207 case 0x36: /* GET FREE DISK SPACE */
1208 GetFreeDiskSpace(&context);
1209 break;
1211 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1212 AX_reg(&context) = 0x02; /* no country support available */
1213 SET_CFLAG(&context);
1214 break;
1216 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1217 if (!FILE_MakeDir( PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1218 DX_reg(&context) )))
1220 AX_reg(&context) = DOS_ExtendedError;
1221 SET_CFLAG(&context);
1223 break;
1225 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1226 if (!FILE_RemoveDir( PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1227 DX_reg(&context) )))
1229 AX_reg(&context) = DOS_ExtendedError;
1230 SET_CFLAG(&context);
1232 break;
1234 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1235 INT21_ChangeDir(&context);
1236 break;
1238 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1239 AX_reg(&context) = _lcreat( PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1240 DX_reg(&context) ), CX_reg(&context) );
1241 if (AX_reg(&context) == (WORD)HFILE_ERROR)
1243 AX_reg(&context) = DOS_ExtendedError;
1244 SET_CFLAG(&context);
1246 break;
1248 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1249 OpenExistingFile(&context);
1250 break;
1252 case 0x3e: /* "CLOSE" - CLOSE FILE */
1253 if ((AX_reg(&context) = _lclose( BX_reg(&context) )) != 0)
1255 AX_reg(&context) = DOS_ExtendedError;
1256 SET_CFLAG(&context);
1258 break;
1260 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1261 if ((AX_reg(&context) = _lread( BX_reg(&context),
1262 PTR_SEG_OFF_TO_LIN( DS_reg(&context),DX_reg(&context) ),
1263 CX_reg(&context))) == (WORD)HFILE_ERROR)
1265 AX_reg(&context) = DOS_ExtendedError;
1266 SET_CFLAG(&context);
1268 break;
1270 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1271 if ((AX_reg(&context) = _lwrite( BX_reg(&context),
1272 PTR_SEG_OFF_TO_LIN( DS_reg(&context),DX_reg(&context) ),
1273 CX_reg(&context))) == (WORD)HFILE_ERROR)
1275 AX_reg(&context) = DOS_ExtendedError;
1276 SET_CFLAG(&context);
1278 break;
1280 case 0x41: /* "UNLINK" - DELETE FILE */
1281 if (!FILE_Unlink( PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1282 DX_reg(&context) )))
1284 AX_reg(&context) = DOS_ExtendedError;
1285 SET_CFLAG(&context);
1287 break;
1289 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1291 LONG status = _llseek( BX_reg(&context),
1292 MAKELONG(DX_reg(&context),CX_reg(&context)),
1293 AL_reg(&context) );
1294 if (status == HFILE_ERROR)
1296 AX_reg(&context) = DOS_ExtendedError;
1297 SET_CFLAG(&context);
1298 break;
1300 AX_reg(&context) = LOWORD(status);
1301 DX_reg(&context) = HIWORD(status);
1303 break;
1305 case 0x43: /* FILE ATTRIBUTES */
1306 switch (AL_reg(&context))
1308 case 0x00:
1309 if (!INT21_GetFileAttribute(&context))
1311 AX_reg(&context) = DOS_ExtendedError;
1312 SET_CFLAG(&context);
1314 break;
1315 case 0x01:
1316 RESET_CFLAG(&context);
1317 break;
1319 break;
1321 case 0x44: /* IOCTL */
1322 switch (AL_reg(&context))
1324 case 0x00:
1325 ioctlGetDeviceInfo(&context);
1326 break;
1328 case 0x01:
1329 break;
1331 case 0x08: /* Check if drive is removable. */
1332 switch(GetDriveType( DOS_GET_DRIVE( BL_reg(&context) )))
1334 case DRIVE_CANNOTDETERMINE:
1335 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
1336 AX_reg(&context) = ER_InvalidDrive;
1337 SET_CFLAG(&context);
1338 break;
1339 case DRIVE_REMOVABLE:
1340 AX_reg(&context) = 0; /* removable */
1341 break;
1342 default:
1343 AX_reg(&context) = 1; /* not removable */
1344 break;
1346 break;
1348 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
1349 switch(GetDriveType( DOS_GET_DRIVE( BL_reg(&context) )))
1351 case DRIVE_CANNOTDETERMINE:
1352 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
1353 AX_reg(&context) = ER_InvalidDrive;
1354 SET_CFLAG(&context);
1355 break;
1356 case DRIVE_REMOTE:
1357 DX_reg(&context) = (1<<9) | (1<<12); /* remote */
1358 break;
1359 default:
1360 DX_reg(&context) = 0; /* FIXME: use driver attr here */
1361 break;
1363 break;
1365 case 0x0a: /* check if handle (BX) is remote */
1366 /* returns DX, bit 15 set if remote, bit 14 set if date/time
1367 * not set on close
1369 DX_reg(&context) = 0;
1370 break;
1372 case 0x0b: /* SET SHARING RETRY COUNT */
1373 if (!CX_reg(&context))
1375 AX_reg(&context) = 1;
1376 SET_CFLAG(&context);
1377 break;
1379 sharing_pause = CX_reg(&context);
1380 if (!DX_reg(&context))
1381 sharing_retries = DX_reg(&context);
1382 RESET_CFLAG(&context);
1383 break;
1385 case 0x0d:
1386 ioctlGenericBlkDevReq(&context);
1387 break;
1389 case 0x0F: /* Set logical drive mapping */
1390 /* FIXME: Not implemented at the moment, always returns error
1392 INT_BARF( &context, 0x21 );
1393 AX_reg(&context) = 0x0001; /* invalid function */
1394 SET_CFLAG(&context);
1395 break;
1397 default:
1398 INT_BARF( &context, 0x21 );
1399 break;
1401 break;
1403 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1404 if ((AX_reg(&context) = FILE_Dup(BX_reg(&context))) == (WORD)HFILE_ERROR)
1406 AX_reg(&context) = DOS_ExtendedError;
1407 SET_CFLAG(&context);
1409 break;
1411 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1412 if (FILE_Dup2( BX_reg(&context), CX_reg(&context) ) == HFILE_ERROR)
1414 AX_reg(&context) = DOS_ExtendedError;
1415 SET_CFLAG(&context);
1417 break;
1419 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1420 if (!INT21_GetCurrentDirectory(&context))
1422 AX_reg(&context) = DOS_ExtendedError;
1423 SET_CFLAG(&context);
1425 else AX_reg(&context) = 0x0100;
1426 /* intlist: many Microsoft products for Windows rely on this */
1427 break;
1429 case 0x48: /* ALLOCATE MEMORY */
1430 case 0x49: /* FREE MEMORY */
1431 case 0x4a: /* RESIZE MEMORY BLOCK */
1432 INT_BARF( &context, 0x21 );
1433 break;
1435 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1436 WinExec( PTR_SEG_OFF_TO_LIN( DS_reg(&context), DX_reg(&context) ),
1437 SW_NORMAL );
1438 break;
1440 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1441 TASK_KillCurrentTask( AL_reg(&context) );
1442 break;
1444 case 0x4d: /* GET RETURN CODE */
1445 AX_reg(&context) = 0; /* normal exit */
1446 break;
1448 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1449 if (!INT21_FindFirst(&context)) break;
1450 /* fall through */
1452 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1453 if (!INT21_FindNext(&context))
1455 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Disk );
1456 AX_reg(&context) = ER_NoMoreFiles;
1457 SET_CFLAG(&context);
1459 break;
1461 case 0x51: /* GET PSP ADDRESS */
1462 case 0x62: /* GET PSP ADDRESS */
1463 /* FIXME: should we return the original DOS PSP upon */
1464 /* Windows startup ? */
1465 BX_reg(&context) = GetCurrentPDB();
1466 break;
1468 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1469 ES_reg(&context) = 0x0;
1470 BX_reg(&context) = 0x0;
1471 INT_BARF( &context, 0x21 );
1472 break;
1474 case 0x56: /* "RENAME" - RENAME FILE */
1475 if (!INT21_RenameFile(&context))
1477 AX_reg(&context) = DOS_ExtendedError;
1478 SET_CFLAG(&context);
1480 break;
1482 case 0x57: /* FILE DATE AND TIME */
1483 switch (AL_reg(&context))
1485 case 0x00:
1486 if (!FILE_Fstat( BX_reg(&context), NULL, NULL,
1487 &DX_reg(&context), &CX_reg(&context) ))
1489 AX_reg(&context) = DOS_ExtendedError;
1490 SET_CFLAG(&context);
1492 break;
1493 case 0x01:
1494 if (!INT21_SetFileDateTime(&context))
1496 AX_reg(&context) = DOS_ExtendedError;
1497 SET_CFLAG(&context);
1499 break;
1501 break;
1503 case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1504 switch (AL_reg(&context))
1506 case 0x00:
1507 AX_reg(&context) = 1;
1508 break;
1509 case 0x02:
1510 AX_reg(&context) = 0;
1511 break;
1512 case 0x01:
1513 case 0x03:
1514 break;
1516 RESET_CFLAG(&context);
1517 break;
1519 case 0x5a: /* CREATE TEMPORARY FILE */
1520 if (!INT21_CreateTempFile(&context))
1522 AX_reg(&context) = DOS_ExtendedError;
1523 SET_CFLAG(&context);
1525 break;
1527 case 0x5b: /* CREATE NEW FILE */
1528 if ((AX_reg(&context) = _lcreat_uniq( PTR_SEG_OFF_TO_LIN(DS_reg(&context),DX_reg(&context)), 0 )) == (WORD)HFILE_ERROR)
1530 AX_reg(&context) = DOS_ExtendedError;
1531 SET_CFLAG(&context);
1533 break;
1535 case 0x5d: /* NETWORK */
1536 case 0x5e:
1537 /* network software not installed */
1538 DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
1539 AX_reg(&context) = DOS_ExtendedError;
1540 SET_CFLAG(&context);
1541 break;
1543 case 0x5f: /* NETWORK */
1544 switch (AL_reg(&context))
1546 case 0x07: /* ENABLE DRIVE */
1547 if (!DRIVE_Enable( DL_reg(&context) ))
1549 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
1550 AX_reg(&context) = DOS_ExtendedError;
1551 SET_CFLAG(&context);
1553 break;
1555 case 0x08: /* DISABLE DRIVE */
1556 if (!DRIVE_Disable( DL_reg(&context) ))
1558 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
1559 AX_reg(&context) = DOS_ExtendedError;
1560 SET_CFLAG(&context);
1562 break;
1564 default:
1565 /* network software not installed */
1566 DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
1567 AX_reg(&context) = DOS_ExtendedError;
1568 SET_CFLAG(&context);
1569 break;
1571 break;
1573 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1575 const char *truename = DOSFS_GetDosTrueName( PTR_SEG_OFF_TO_LIN(DS_reg(&context),SI_reg(&context) ), FALSE );
1576 if (!truename)
1578 AX_reg(&context) = DOS_ExtendedError;
1579 SET_CFLAG(&context);
1581 else
1583 lstrcpyn(PTR_SEG_OFF_TO_LIN(ES_reg(&context),DI_reg(&context)),
1584 truename, 128 );
1585 AX_reg(&context) = 0;
1588 break;
1590 case 0x61: /* UNUSED */
1591 case 0x63: /* UNUSED */
1592 case 0x64: /* OS/2 DOS BOX */
1593 case 0x65: /* GET EXTENDED COUNTRY INFORMATION */
1594 INT_BARF( &context, 0x21 );
1595 break;
1597 case 0x66: /* GLOBAL CODE PAGE TABLE */
1598 switch (AL_reg(&context))
1600 case 0x01:
1601 DX_reg(&context) = BX_reg(&context) = CodePage;
1602 RESET_CFLAG(&context);
1603 break;
1604 case 0x02:
1605 CodePage = BX_reg(&context);
1606 RESET_CFLAG(&context);
1607 break;
1609 break;
1611 case 0x67: /* SET HANDLE COUNT */
1612 SetHandleCount( BX_reg(&context) );
1613 if (DOS_ExtendedError)
1615 AX_reg(&context) = DOS_ExtendedError;
1616 SET_CFLAG(&context);
1618 break;
1620 case 0x68: /* "FFLUSH" - COMMIT FILE */
1621 case 0x6a: /* COMMIT FILE */
1622 if (fsync( FILE_GetUnixHandle( BX_reg(&context) )) == -1)
1624 FILE_SetDosError();
1625 AX_reg(&context) = DOS_ExtendedError;
1626 SET_CFLAG(&context);
1628 break;
1630 case 0x69: /* DISK SERIAL NUMBER */
1631 switch (AL_reg(&context))
1633 case 0x00:
1634 if (!INT21_GetDiskSerialNumber(&context))
1636 AX_reg(&context) = DOS_ExtendedError;
1637 SET_CFLAG(&context);
1639 else AX_reg(&context) = 0;
1640 break;
1641 case 0x01:
1642 if (!INT21_SetDiskSerialNumber(&context))
1644 AX_reg(&context) = DOS_ExtendedError;
1645 SET_CFLAG(&context);
1647 else AX_reg(&context) = 1;
1648 break;
1650 break;
1652 case 0x6C: /* Extended Open/Create*/
1653 ExtendedOpenCreateFile(&context);
1654 break;
1656 case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
1657 case 0x71: /* MS-DOS 7 (Chicago) - LONG FILENAME FUNCTIONS */
1658 case 0x72: /* MS-DOS 7 (Windows95) - ??? */
1659 case 0x73: /* MS-DOS 7 (Windows95) - DRIVE LOCKING ??? */
1660 dprintf_int(stddeb,"int21: windows95 function AX %04x\n",
1661 AX_reg(&context));
1662 dprintf_int(stddeb, " returning unimplemented\n");
1663 SET_CFLAG(&context);
1664 AL_reg(&context) = 0;
1665 break;
1667 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
1668 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
1669 break;
1671 default:
1672 INT_BARF( &context, 0x21 );
1673 break;
1675 dprintf_int( stddeb, "ret21: AX=%04x BX=%04x CX=%04x DX=%04x "
1676 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1677 AX_reg(&context), BX_reg(&context), CX_reg(&context),
1678 DX_reg(&context), SI_reg(&context), DI_reg(&context),
1679 DS_reg(&context), ES_reg(&context), EFL_reg(&context));
1683 void INT21_Init(void)
1685 if ((DosHeapHandle = GlobalAlloc(GMEM_FIXED,sizeof(struct DosHeap))) == 0)
1687 fprintf( stderr, "INT21_Init: Out of memory\n");
1688 exit(1);
1690 heap = (struct DosHeap *) GlobalLock(DosHeapHandle);
1692 dpb = &heap->dpb;
1693 dpbsegptr = MAKELONG( (int)&heap->dpb - (int)heap, DosHeapHandle );
1694 heap->InDosFlag = 0;
1695 strcpy(heap->biosdate, "01/01/80");