Release 950918
[wine/multimedia.git] / miscemu / int21.c
blobd580ec5cb48bbb496b6b4a2705c8a801f3639c18
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 "windows.h"
20 #include "msdos.h"
21 #include "registers.h"
22 #include "ldt.h"
23 #include "task.h"
24 #include "options.h"
25 #include "miscemu.h"
26 #include "stddebug.h"
27 #include "debug.h"
29 /* Define the drive parameter block, as used by int21/1F
30 * and int21/32. This table can be accessed through the
31 * global 'dpb' pointer, which points into the local dos
32 * heap.
34 struct DPB
36 BYTE drive_num; /* 0=A, etc. */
37 BYTE unit_num; /* Drive's unit number (?) */
38 WORD sector_size; /* Sector size in bytes */
39 BYTE high_sector; /* Highest sector in a cluster */
40 BYTE shift; /* Shift count (?) */
41 WORD reserved; /* Number of reserved sectors at start */
42 BYTE num_FAT; /* Number of FATs */
43 WORD dir_entries; /* Number of root dir entries */
44 WORD first_data; /* First data sector */
45 WORD high_cluster; /* Highest cluster number */
46 WORD sectors_in_FAT; /* Number of sectors per FAT */
47 WORD start_dir; /* Starting sector of first dir */
48 DWORD driver_head; /* Address of device driver header (?) */
49 BYTE media_ID; /* Media ID */
50 BYTE access_flag; /* Prev. accessed flag (0=yes,0xFF=no) */
51 DWORD next; /* Pointer to next DPB in list */
52 WORD free_search; /* Free cluster search start */
53 WORD free_clusters; /* Number of free clusters (0xFFFF=unknown) */
56 WORD ExtendedError, CodePage = 437;
57 BYTE ErrorClass, Action, ErrorLocus;
58 struct DPB *dpb;
59 DWORD dpbsegptr;
61 struct DosHeap {
62 BYTE InDosFlag;
63 BYTE mediaID;
64 BYTE biosdate[8];
65 struct DPB dpb;
67 static struct DosHeap *heap;
68 static WORD DosHeapHandle;
70 WORD sharing_retries = 3; /* number of retries at sharing violation */
71 WORD sharing_pause = 1; /* pause between retries */
73 extern char TempDirectory[];
75 static int Error(int e, int class, int el)
77 ErrorClass = class;
78 Action = SA_Ask4Retry;
79 ErrorLocus = el;
80 ExtendedError = e;
82 return e;
85 void errno_to_doserr(void)
87 switch (errno) {
88 case EAGAIN:
89 Error (ShareViolation, EC_Temporary, EL_Unknown);
90 break;
91 case EBADF:
92 Error (InvalidHandle, EC_AppError, EL_Unknown);
93 break;
94 case ENOSPC:
95 Error (DiskFull, EC_MediaError, EL_Disk);
96 break;
97 case EACCES:
98 case EPERM:
99 case EROFS:
100 Error (WriteProtected, EC_AccessDenied, EL_Unknown);
101 break;
102 case EBUSY:
103 Error (LockViolation, EC_AccessDenied, EL_Unknown);
104 break;
105 case ENOENT:
106 Error (FileNotFound, EC_NotFound, EL_Unknown);
107 break;
108 case EISDIR:
109 Error (CanNotMakeDir, EC_AccessDenied, EL_Unknown);
110 break;
111 case ENFILE:
112 case EMFILE:
113 Error (NoMoreFiles, EC_MediaError, EL_Unknown);
114 break;
115 case EEXIST:
116 Error (FileExists, EC_Exists, EL_Disk);
117 break;
118 default:
119 dprintf_int(stddeb, "int21: unknown errno %d!\n", errno);
120 Error (GeneralFailure, EC_SystemFailure, EL_Unknown);
121 break;
125 BYTE *GetCurrentDTA(void)
127 TDB *pTask = (TDB *)GlobalLock( GetCurrentTask() );
128 return (BYTE *)PTR_SEG_TO_LIN( pTask->dta );
132 void ChopOffWhiteSpace(char *string)
134 int length;
136 for (length = strlen(string) ; length ; length--)
137 if (string[length] == ' ')
138 string[length] = '\0';
141 static void CreateBPB(int drive, BYTE *data)
143 if (drive > 1) {
144 setword(data, 512);
145 data[2] = 2;
146 setword(&data[3], 0);
147 data[5] = 2;
148 setword(&data[6], 240);
149 setword(&data[8], 64000);
150 data[0x0a] = 0xf8;
151 setword(&data[0x0b], 40);
152 setword(&data[0x0d], 56);
153 setword(&data[0x0f], 2);
154 setword(&data[0x11], 0);
155 setword(&data[0x1f], 800);
156 data[0x21] = 5;
157 setword(&data[0x22], 1);
158 } else { /* 1.44mb */
159 setword(data, 512);
160 data[2] = 2;
161 setword(&data[3], 0);
162 data[5] = 2;
163 setword(&data[6], 240);
164 setword(&data[8], 2880);
165 data[0x0a] = 0xf8;
166 setword(&data[0x0b], 6);
167 setword(&data[0x0d], 18);
168 setword(&data[0x0f], 2);
169 setword(&data[0x11], 0);
170 setword(&data[0x1f], 80);
171 data[0x21] = 7;
172 setword(&data[0x22], 2);
176 static void GetFreeDiskSpace(struct sigcontext_struct *context)
178 int drive;
179 long size,available;
181 if (DL_reg(context) == 0) drive = DOS_GetDefaultDrive();
182 else drive = DL_reg(context) - 1;
184 if (!DOS_ValidDrive(drive)) {
185 Error(InvalidDrive, EC_MediaError , EL_Disk);
186 AX_reg(context) = 0xffff;
187 return;
190 if (!DOS_GetFreeSpace(drive, &size, &available)) {
191 Error(GeneralFailure, EC_MediaError , EL_Disk);
192 AX_reg(context) = 0xffff;
193 return;
196 AX_reg(context) = (drive < 2) ? 1 : 64; /* 64 for hard-disks, 1 for diskettes */
197 CX_reg(context) = 512;
199 BX_reg(context) = (available / (CX_reg(context) * AX_reg(context)));
200 DX_reg(context) = (size / (CX_reg(context) * AX_reg(context)));
201 Error (0,0,0);
204 static void GetDriveAllocInfo(struct sigcontext_struct *context)
206 int drive;
207 long size, available;
209 if (DL_reg(context) == 0) drive = DOS_GetDefaultDrive();
210 else drive = DL_reg(context) - 1;
212 if (!DOS_ValidDrive(drive))
214 AX_reg(context) = 4;
215 CX_reg(context) = 512;
216 DX_reg(context) = 0;
217 Error (InvalidDrive, EC_MediaError, EL_Disk);
218 return;
221 if (!DOS_GetFreeSpace(drive, &size, &available)) {
222 Error(GeneralFailure, EC_MediaError , EL_Disk);
223 AX_reg(context) = 0xffff;
224 return;
227 AX_reg(context) = (drive < 2) ? 1 : 64; /* 64 for hard-disks, 1 for diskettes */
228 CX_reg(context) = 512;
229 DX_reg(context) = (size / (CX_reg(context) * AX_reg(context)));
231 heap->mediaID = 0xf0;
233 DS_reg(context) = DosHeapHandle;
234 BX_reg(context) = (int)&heap->mediaID - (int)heap;
235 Error (0,0,0);
238 static void GetDrivePB(struct sigcontext_struct *context, int drive)
240 if(!DOS_ValidDrive(drive))
242 Error (InvalidDrive, EC_MediaError, EL_Disk);
243 AX_reg(context) = 0x00ff;
245 else
247 dprintf_int(stddeb, "int21: GetDrivePB not fully implemented.\n");
249 /* FIXME: I have no idea what a lot of this information should
250 * say or whether it even really matters since we're not allowing
251 * direct block access. However, some programs seem to depend on
252 * getting at least _something_ back from here. The 'next' pointer
253 * does worry me, though. Should we have a complete table of
254 * separate DPBs per drive? Probably, but I'm lazy. :-) -CH
256 dpb->drive_num = dpb->unit_num = drive; /* The same? */
257 dpb->sector_size = 512;
258 dpb->high_sector = 1;
259 dpb->shift = drive < 2 ? 0 : 6; /* 6 for HD, 0 for floppy */
260 dpb->reserved = 0;
261 dpb->num_FAT = 1;
262 dpb->dir_entries = 2;
263 dpb->first_data = 2;
264 dpb->high_cluster = 64000;
265 dpb->sectors_in_FAT = 1;
266 dpb->start_dir = 1;
267 dpb->driver_head = 0;
268 dpb->media_ID = (drive > 1) ? 0xF8 : 0xF0;
269 dpb->access_flag = 0;
270 dpb->next = 0;
271 dpb->free_search = 0;
272 dpb->free_clusters = 0xFFFF; /* unknown */
274 AL_reg(context) = 0x00;
275 DS_reg(context) = SELECTOROF(dpbsegptr);
276 BX_reg(context) = OFFSETOF(dpbsegptr);
280 static void ReadFile(struct sigcontext_struct *context)
282 char *ptr;
283 int size;
285 /* can't read from stdout / stderr */
286 if ((BX_reg(context) == 1) || (BX_reg(context) == 2)) {
287 Error (InvalidHandle, EL_Unknown, EC_Unknown);
288 AX_reg(context) = InvalidHandle;
289 SET_CFLAG(context);
290 dprintf_int(stddeb, "int21: read (%d, void *, 0x%x) = EBADF\n",
291 BX_reg(context), CX_reg(context));
292 return;
295 ptr = PTR_SEG_OFF_TO_LIN (DS_reg(context),DX_reg(context));
296 if (BX_reg(context) == 0) {
297 *ptr = EOF;
298 Error (0,0,0);
299 AX_reg(context) = 1;
300 RESET_CFLAG(context);
301 dprintf_int(stddeb, "int21: read (%d, void *, 0x%x) = EOF\n",
302 BX_reg(context), CX_reg(context));
303 return;
304 } else {
305 size = read(BX_reg(context), ptr, CX_reg(context));
306 dprintf_int(stddeb,"int21: read(%d, %04x:%04x, 0x%x) = 0x%x\n",
307 BX_reg(context), DS_reg(context), DX_reg(context),
308 CX_reg(context), size );
309 if (size == -1) {
310 errno_to_doserr();
311 AX_reg(context) = ExtendedError;
312 SET_CFLAG(context);
313 return;
315 Error (0,0,0);
316 AX_reg(context) = size;
317 RESET_CFLAG(context);
321 static void WriteFile(struct sigcontext_struct *context)
323 char *ptr;
324 int x,size;
326 ptr = PTR_SEG_OFF_TO_LIN (DS_reg(context),DX_reg(context));
328 if (BX_reg(context) == 0) {
329 Error (InvalidHandle, EC_Unknown, EL_Unknown);
330 EAX_reg(context) = InvalidHandle;
331 SET_CFLAG(context);
332 return;
335 if (BX_reg(context) < 3) {
336 for (x = 0;x != CX_reg(context);x++) {
337 dprintf_int(stddeb, "%c", *ptr++);
339 fflush(stddeb);
341 Error (0,0,0);
342 AX_reg(context) = CX_reg(context);
343 RESET_CFLAG(context);
344 } else {
345 /* well, this function already handles everything we need */
346 size = _lwrite(BX_reg(context),ptr,CX_reg(context));
347 if (size == -1) { /* HFILE_ERROR == -1 */
348 errno_to_doserr();
349 AX_reg(context) = ExtendedError;
350 SET_CFLAG(context);
351 return;
353 Error (0,0,0);
354 AX_reg(context) = size;
355 RESET_CFLAG(context);
359 static void SeekFile(struct sigcontext_struct *context)
361 off_t status, fileoffset;
363 switch (AL_reg(context))
365 case 1: fileoffset = SEEK_CUR;
366 break;
367 case 2: fileoffset = SEEK_END;
368 break;
369 default:
370 case 0: fileoffset = SEEK_SET;
371 break;
373 status = lseek(BX_reg(context), (CX_reg(context) << 16) + DX_reg(context), fileoffset);
375 dprintf_int (stddeb, "int21: seek (%d, 0x%x, %d) = 0x%lx\n",
376 BX_reg(context), (CX_reg(context) << 16) + DX_reg(context), AL_reg(context), status);
378 if (status == -1)
380 errno_to_doserr();
381 AX_reg(context) = ExtendedError;
382 SET_CFLAG(context);
383 return;
385 Error (0,0,0);
386 AX_reg(context) = LOWORD(status);
387 DX_reg(context) = HIWORD(status);
388 RESET_CFLAG(context);
391 static void ioctlGetDeviceInfo(struct sigcontext_struct *context)
394 dprintf_int (stddeb, "int21: ioctl (%d, GetDeviceInfo)\n", BX_reg(context));
396 switch (BX_reg(context))
398 case 0:
399 case 1:
400 case 2:
401 DX_reg(context) = 0x80d0 | (1 << (BX_reg(context) != 0));
402 RESET_CFLAG(context);
403 break;
405 default:
407 struct stat sbuf;
409 if (fstat(BX_reg(context), &sbuf) < 0)
411 INT_BARF( context, 0x21 );
412 SET_CFLAG(context);
413 return;
416 DX_reg(context) = 0x0943;
417 /* bits 0-5 are current drive
418 * bit 6 - file has NOT been written..FIXME: correct?
419 * bit 8 - generate int24 if no diskspace on write/ read past end of file
420 * bit 11 - media not removable
424 RESET_CFLAG(context);
427 static void ioctlGenericBlkDevReq(struct sigcontext_struct *context)
429 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
430 int drive;
432 if (BL_reg(context) == 0) drive = DOS_GetDefaultDrive();
433 else drive = BL_reg(context) - 1;
435 if (!DOS_ValidDrive(drive))
437 Error( FileNotFound, EC_NotFound, EL_Disk );
438 AX_reg(context) = FileNotFound;
439 SET_CFLAG(context);
440 return;
443 if (CH_reg(context) != 0x08)
445 INT_BARF( context, 0x21 );
446 return;
448 switch (CL_reg(context)) {
449 case 0x60: /* get device parameters */
450 /* used by w4wgrp's winfile */
451 memset(dataptr, 0, 0x26);
452 dataptr[0] = 0x04;
453 dataptr[6] = 0; /* media type */
454 if (drive > 1)
456 dataptr[1] = 0x05; /* fixed disk */
457 setword(&dataptr[2], 0x01); /* non removable */
458 setword(&dataptr[4], 0x300); /* # of cylinders */
460 else
462 dataptr[1] = 0x07; /* block dev, floppy */
463 setword(&dataptr[2], 0x02); /* removable */
464 setword(&dataptr[4], 80); /* # of cylinders */
466 CreateBPB(drive, &dataptr[7]);
467 RESET_CFLAG(context);
468 return;
469 default:
470 INT_BARF( context, 0x21 );
474 static void GetSystemDate(struct sigcontext_struct *context)
476 struct tm *now;
477 time_t ltime;
479 ltime = time(NULL);
480 now = localtime(&ltime);
482 CX_reg(context) = now->tm_year + 1900;
483 DX_reg(context) = ((now->tm_mon + 1) << 8) | now->tm_mday;
484 AX_reg(context) = now->tm_wday;
487 static void GetSystemTime(struct sigcontext_struct *context)
489 struct tm *now;
490 struct timeval tv;
492 gettimeofday(&tv,NULL); /* Note use of gettimeofday(), instead of time() */
493 now = localtime(&tv.tv_sec);
495 CX_reg(context) = (now->tm_hour<<8) | now->tm_min;
496 DX_reg(context) = (now->tm_sec<<8) | tv.tv_usec/10000;
497 /* Note hundredths of seconds */
500 static void GetExtendedErrorInfo(struct sigcontext_struct *context)
502 AX_reg(context) = ExtendedError;
503 BX_reg(context) = (ErrorClass << 8) | Action;
504 CH_reg(context) = ErrorLocus;
507 static void CreateFile(struct sigcontext_struct *context)
509 int handle;
510 handle = open(DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS_reg(context),
511 DX_reg(context))),
512 O_CREAT | O_TRUNC | O_RDWR, 0666 );
513 if (handle == -1) {
514 errno_to_doserr();
515 AX_reg(context) = ExtendedError;
516 SET_CFLAG(context);
517 return;
519 Error (0,0,0);
520 AX_reg(context) = handle;
521 RESET_CFLAG(context);
524 void OpenExistingFile(struct sigcontext_struct *context)
526 int handle;
527 int mode;
528 int lock;
530 switch (AX_reg(context) & 0x0007)
532 case 0:
533 mode = O_RDONLY;
534 break;
536 case 1:
537 mode = O_WRONLY;
538 break;
540 default:
541 mode = O_RDWR;
542 break;
545 if ((handle = open(DOS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context))),
546 mode)) == -1)
548 if( Options.allowReadOnly )
549 handle = open( DOS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context))),
550 O_RDONLY );
551 if( handle == -1 )
553 dprintf_int (stddeb, "int21: open (%s, %d) = -1\n",
554 DOS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context))), mode);
555 errno_to_doserr();
556 AX_reg(context) = ExtendedError;
557 SET_CFLAG(context);
558 return;
562 dprintf_int (stddeb, "int21: open (%s, %d) = %d\n",
563 DOS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),
564 DX_reg(context))), mode, handle);
566 switch (AX_reg(context) & 0x0070)
568 case 0x00: /* compatability mode */
569 case 0x40: /* DENYNONE */
570 lock = -1;
571 break;
573 case 0x30: /* DENYREAD */
574 dprintf_int(stddeb,
575 "OpenExistingFile (%s): DENYREAD changed to DENYALL\n",
576 (char *)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)));
577 case 0x10: /* DENYALL */
578 lock = LOCK_EX;
579 break;
581 case 0x20: /* DENYWRITE */
582 lock = LOCK_SH;
583 break;
585 default:
586 lock = -1;
589 if (lock != -1)
592 int result,retries=sharing_retries;
594 result = flock(handle, lock | LOCK_NB);
595 if ( retries && (!result) )
597 int i;
598 for(i=0;i<32768*((int)sharing_pause);i++)
599 result++; /* stop the optimizer */
600 for(i=0;i<32768*((int)sharing_pause);i++)
601 result--;
604 while( (!result) && (!(retries--)) );
606 if(result)
608 errno_to_doserr();
609 AX_reg(context) = ExtendedError;
610 close(handle);
611 SET_CFLAG(context);
612 return;
617 Error (0,0,0);
618 AX_reg(context) = handle;
619 RESET_CFLAG(context);
622 static void CloseFile(struct sigcontext_struct *context)
624 dprintf_int (stddeb, "int21: close (%d)\n", BX_reg(context));
626 if (_lclose( BX_reg(context) ))
628 errno_to_doserr();
629 AX_reg(context) = ExtendedError;
630 SET_CFLAG(context);
631 return;
633 Error (0,0,0);
634 AX_reg(context) = NoError;
635 RESET_CFLAG(context);
638 static void RenameFile(struct sigcontext_struct *context)
640 char *newname, *oldname;
642 /* FIXME: should not rename over an existing file */
643 dprintf_int(stddeb,"int21: renaming %s to %s\n",
644 (char *)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
645 (char *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context)));
647 oldname = DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS_reg(context),
648 DX_reg(context)) );
649 newname = DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(ES_reg(context),
650 DI_reg(context)) );
652 rename( oldname, newname);
653 RESET_CFLAG(context);
657 static void MakeDir(struct sigcontext_struct *context)
659 char *dirname;
661 dprintf_int(stddeb,"int21: makedir %s\n", (char *)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)) );
663 if ((dirname = DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS_reg(context),
664 DX_reg(context)) ))== NULL) {
665 Error( CanNotMakeDir, EC_AccessDenied, EL_Disk );
666 AX_reg(context) = CanNotMakeDir;
667 SET_CFLAG(context);
668 return;
671 if (mkdir(dirname,0) == -1)
673 Error( CanNotMakeDir, EC_AccessDenied, EL_Disk );
674 AX_reg(context) = CanNotMakeDir;
675 SET_CFLAG(context);
676 return;
678 RESET_CFLAG(context);
681 static void ChangeDir(struct sigcontext_struct *context)
683 int drive;
684 char *dirname = PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context));
685 drive = DOS_GetDefaultDrive();
686 dprintf_int(stddeb,"int21: changedir %s\n", dirname);
687 if (dirname != NULL && dirname[1] == ':') {
688 drive = toupper(dirname[0]) - 'A';
689 dirname += 2;
691 if (!DOS_ChangeDir(drive, dirname))
693 SET_CFLAG(context);
694 AX_reg(context)=0x03;
698 static void RemoveDir(struct sigcontext_struct *context)
700 char *dirname;
702 dprintf_int(stddeb,"int21: removedir %s\n", (char *)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)) );
704 if ((dirname = DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)) ))== NULL) {
705 Error( PathNotFound, EC_NotFound, EL_Disk );
706 AX_reg(context) = PathNotFound;
707 SET_CFLAG(context);
708 return;
712 if (strcmp(unixname,DosDrives[drive].CurrentDirectory)) {
713 AL = CanNotRemoveCwd;
714 SET_CFLAG(context);
717 if (rmdir(dirname) == -1) {
718 Error( AccessDenied, EC_AccessDenied, EL_Disk );
719 AX_reg(context) = AccessDenied;
720 SET_CFLAG(context);
721 return;
723 RESET_CFLAG(context);
726 static void FindNext(struct sigcontext_struct *context)
728 struct dosdirent *dp;
729 struct tm *t;
730 BYTE *dta = GetCurrentDTA();
732 memcpy(&dp, dta+0x11, sizeof(dp));
734 dprintf_int(stddeb, "int21: FindNext, dta %p, dp %p\n", dta, dp);
735 do {
736 if ((dp = DOS_readdir(dp)) == NULL) {
737 Error(NoMoreFiles, EC_MediaError , EL_Disk);
738 AX_reg(context) = NoMoreFiles;
739 SET_CFLAG(context);
740 return;
742 } /* while (*(dta + 0x0c) != dp->attribute);*/
743 while ( ( dp->search_attribute & dp->attribute) != dp->attribute);
745 *(dta + 0x15) = dp->attribute;
746 setword(&dta[0x0d], dp->entnum);
748 t = localtime(&(dp->filetime));
749 setword(&dta[0x16], (t->tm_hour << 11) + (t->tm_min << 5) +
750 (t->tm_sec / 2)); /* time */
751 setword(&dta[0x18], ((t->tm_year - 80) << 9) + (t->tm_mon << 5) +
752 (t->tm_mday)); /* date */
753 setdword(&dta[0x1a], dp->filesize);
754 strncpy(dta + 0x1e, dp->filename, 13);
756 AX_reg(context) = 0;
757 RESET_CFLAG(context);
759 dprintf_int(stddeb, "int21: FindNext -- (%s) index=%d size=%ld\n", dp->filename, dp->entnum, dp->filesize);
760 return;
763 static void FindFirst(struct sigcontext_struct *context)
765 BYTE drive, *path = PTR_SEG_OFF_TO_LIN(DS_reg(context),
766 DX_reg(context));
767 struct dosdirent *dp;
769 BYTE *dta = GetCurrentDTA();
771 dprintf_int(stddeb, "int21: FindFirst path = %s\n", path);
773 if ((*path)&&(path[1] == ':')) {
774 drive = (islower(*path) ? toupper(*path) : *path) - 'A';
776 if (!DOS_ValidDrive(drive)) {
777 Error(InvalidDrive, EC_MediaError , EL_Disk);
778 AX_reg(context) = InvalidDrive;
779 SET_CFLAG(context);
780 return;
782 } else
783 drive = DOS_GetDefaultDrive();
785 *dta = drive;
786 memset(dta + 1 , '?', 11);
787 *(dta + 0x0c) = ECX_reg(context) & (FA_LABEL | FA_DIREC);
789 if ((dp = DOS_opendir(path)) == NULL) {
790 Error(PathNotFound, EC_MediaError, EL_Disk);
791 AX_reg(context) = FileNotFound;
792 SET_CFLAG(context);
793 return;
796 dp->search_attribute = ECX_reg(context) & (FA_LABEL | FA_DIREC);
797 memcpy(dta + 0x11, &dp, sizeof(dp));
798 FindNext(context);
801 static void GetFileDateTime(struct sigcontext_struct *context)
803 struct stat filestat;
804 struct tm *now;
806 fstat( BX_reg(context), &filestat );
807 now = localtime (&filestat.st_mtime);
809 CX_reg(context) = ((now->tm_hour * 0x2000) + (now->tm_min * 0x20) + now->tm_sec/2);
810 DX_reg(context) = (((now->tm_year + 1900 - 1980) * 0x200) +
811 (now->tm_mon * 0x20) + now->tm_mday);
813 RESET_CFLAG(context);
816 static void SetFileDateTime(struct sigcontext_struct *context)
818 char *filename;
819 struct utimbuf filetime;
821 /* FIXME: Argument isn't the name of the file in DS:DX,
822 but the file handle in BX */
823 filename = DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS_reg(context),
824 DX_reg(context)) );
826 filetime.actime = 0L;
827 filetime.modtime = filetime.actime;
829 utime(filename, &filetime);
830 RESET_CFLAG(context);
833 static void CreateTempFile(struct sigcontext_struct *context)
835 char temp[256];
836 int handle;
838 sprintf(temp,"%s\\win%d.tmp",TempDirectory,(int) getpid());
840 dprintf_int(stddeb,"CreateTempFile %s\n",temp);
842 handle = open(DOS_GetUnixFileName(temp), O_CREAT | O_TRUNC | O_RDWR, 0666);
844 if (handle == -1) {
845 Error( WriteProtected, EC_AccessDenied, EL_Disk );
846 AX_reg(context) = WriteProtected;
847 SET_CFLAG(context);
848 return;
851 strcpy(PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)), temp);
853 AX_reg(context) = handle;
854 RESET_CFLAG(context);
857 static void CreateNewFile(struct sigcontext_struct *context)
859 int handle;
861 if ((handle = open(DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)) ),
862 O_CREAT | O_EXCL | O_RDWR, 0666)) == -1)
864 Error( WriteProtected, EC_AccessDenied, EL_Disk );
865 AX_reg(context) = WriteProtected;
866 SET_CFLAG(context);
867 return;
870 AX_reg(context) = handle;
871 RESET_CFLAG(context);
874 static void GetCurrentDirectory(struct sigcontext_struct *context)
876 int drive;
878 if (DL_reg(context) == 0) drive = DOS_GetDefaultDrive();
879 else drive = DL_reg(context) - 1;
881 if (!DOS_ValidDrive(drive))
883 Error( InvalidDrive, EC_NotFound, EL_Disk );
884 AX_reg(context) = InvalidDrive;
885 SET_CFLAG(context);
886 return;
889 strcpy(PTR_SEG_OFF_TO_LIN(DS_reg(context),SI_reg(context)),
890 DOS_GetCurrentDir(drive) );
891 RESET_CFLAG(context);
894 static void GetDiskSerialNumber(struct sigcontext_struct *context)
896 int drive;
897 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
898 DWORD serialnumber;
900 if (BL_reg(context) == 0) drive = DOS_GetDefaultDrive();
901 else drive = BL_reg(context) - 1;
903 if (!DOS_ValidDrive(drive))
905 Error( InvalidDrive, EC_NotFound, EL_Disk );
906 AX_reg(context) = InvalidDrive;
907 SET_CFLAG(context);
908 return;
911 DOS_GetSerialNumber(drive, &serialnumber);
913 setword(dataptr, 0);
914 setdword(&dataptr[2], serialnumber);
915 strncpy(dataptr + 6, DOS_GetVolumeLabel(drive), 8);
916 strncpy(dataptr + 0x11, "FAT16 ", 8);
918 AX_reg(context) = 0;
919 RESET_CFLAG(context);
922 static void SetDiskSerialNumber(struct sigcontext_struct *context)
924 int drive;
925 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
926 DWORD serialnumber;
928 if (BL_reg(context) == 0) drive = DOS_GetDefaultDrive();
929 else drive = BL_reg(context) - 1;
931 if (!DOS_ValidDrive(drive))
933 Error( InvalidDrive, EC_NotFound, EL_Disk );
934 AX_reg(context) = InvalidDrive;
935 SET_CFLAG(context);
936 return;
939 serialnumber = dataptr[1] + (dataptr[2] << 8) + (dataptr[3] << 16) +
940 (dataptr[4] << 24);
942 DOS_SetSerialNumber(drive, serialnumber);
943 AX_reg(context) = 1;
944 RESET_CFLAG(context);
947 static void DumpFCB(BYTE *fcb)
949 int x, y;
951 fcb -= 7;
953 for (y = 0; y !=2 ; y++) {
954 for (x = 0; x!=15;x++)
955 dprintf_int(stddeb, "%02x ", *fcb++);
956 dprintf_int(stddeb,"\n");
960 /* microsoft's programmers should be shot for using CP/M style int21
961 calls in Windows for Workgroup's winfile.exe */
963 static void FindFirstFCB(struct sigcontext_struct *context)
965 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
966 struct fcb *standard_fcb;
967 struct fcb *output_fcb;
968 int drive;
969 char path[12];
971 BYTE *dta = GetCurrentDTA();
973 DumpFCB( fcb );
975 if ((*fcb) == 0xff)
977 standard_fcb = (struct fcb *)(fcb + 7);
978 output_fcb = (struct fcb *)(dta + 7);
979 *dta = 0xff;
981 else
983 standard_fcb = (struct fcb *)fcb;
984 output_fcb = (struct fcb *)dta;
987 if (standard_fcb->drive)
989 drive = standard_fcb->drive - 1;
990 if (!DOS_ValidDrive(drive))
992 Error (InvalidDrive, EC_MediaError, EL_Disk);
993 AX_reg(context) = 0xff;
994 return;
997 else
998 drive = DOS_GetDefaultDrive();
1000 output_fcb->drive = drive;
1002 if (*(fcb) == 0xff)
1004 if (*(fcb+6) & FA_LABEL) /* return volume label */
1006 *(dta+6) = FA_LABEL;
1007 memset(&output_fcb->name, ' ', 11);
1008 if (DOS_GetVolumeLabel(drive) != NULL)
1010 strncpy(output_fcb->name, DOS_GetVolumeLabel(drive), 11);
1011 AX_reg(context) = 0x00;
1012 return;
1017 strncpy(output_fcb->name, standard_fcb->name, 11);
1018 if (*fcb == 0xff)
1019 *(dta+6) = ( *(fcb+6) & (!FA_DIREC));
1021 sprintf(path,"%c:*.*",drive+'A');
1022 if ((output_fcb->directory = DOS_opendir(path))==NULL)
1024 Error (PathNotFound, EC_MediaError, EL_Disk);
1025 AX_reg(context) = 0xff;
1026 return;
1032 static void DeleteFileFCB(struct sigcontext_struct *context)
1034 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
1035 int drive;
1036 struct dosdirent *dp;
1037 char temp[256], *ptr;
1039 DumpFCB( fcb );
1041 if (*fcb)
1042 drive = *fcb - 1;
1043 else
1044 drive = DOS_GetDefaultDrive();
1046 strcpy(temp, DOS_GetCurrentDir(drive));
1047 strcat(temp, "\\");
1048 strncat(temp, fcb + 1, 8);
1049 ChopOffWhiteSpace(temp);
1050 strncat(temp, fcb + 9, 3);
1051 ChopOffWhiteSpace(temp);
1053 if ((dp = DOS_opendir(temp)) == NULL) {
1054 Error(InvalidDrive, EC_MediaError , EL_Disk);
1055 AX_reg(context) = 0xff;
1056 return;
1059 strcpy(temp, DOS_GetCurrentDir(drive) );
1060 strcat(temp, "\\");
1062 ptr = temp + strlen(temp);
1064 while (DOS_readdir(dp) != NULL)
1066 strcpy(ptr, dp->filename);
1067 dprintf_int(stddeb, "int21: delete file %s\n", temp);
1068 /* unlink(DOS_GetUnixFileName(temp)); */
1070 DOS_closedir(dp);
1071 AX_reg(context) = 0;
1074 static void RenameFileFCB(struct sigcontext_struct *context)
1076 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
1077 int drive;
1078 struct dosdirent *dp;
1079 char temp[256], oldname[256], newname[256], *oldnameptr, *newnameptr;
1081 DumpFCB( fcb );
1083 if (*fcb)
1084 drive = *fcb - 1;
1085 else
1086 drive = DOS_GetDefaultDrive();
1088 strcpy(temp, DOS_GetCurrentDir(drive));
1089 strcat(temp, "\\");
1090 strncat(temp, fcb + 1, 8);
1091 ChopOffWhiteSpace(temp);
1092 strncat(temp, fcb + 9, 3);
1093 ChopOffWhiteSpace(temp);
1095 if ((dp = DOS_opendir(temp)) == NULL) {
1096 Error(InvalidDrive, EC_MediaError , EL_Disk);
1097 AX_reg(context) = 0xff;
1098 return;
1101 strcpy(oldname, DOS_GetCurrentDir(drive) );
1102 strcat(oldname, "\\");
1103 oldnameptr = oldname + strlen(oldname);
1105 strcpy(newname, DOS_GetCurrentDir(drive) );
1106 strcat(newname, "\\");
1107 newnameptr = newname + strlen(newname);
1109 while (DOS_readdir(dp) != NULL)
1111 strcpy(oldnameptr, dp->filename);
1112 strcpy(newnameptr, fcb + 1);
1113 dprintf_int(stddeb, "int21: renamefile %s -> %s\n",
1114 oldname, newname);
1116 DOS_closedir(dp);
1117 AX_reg(context) = 0;
1122 static void fLock (struct sigcontext_struct * context)
1124 struct flock f;
1125 int result,retries=sharing_retries;
1127 f.l_start = MAKELONG(DX_reg(context),CX_reg(context));
1128 f.l_len = MAKELONG(DI_reg(context),SI_reg(context));
1129 f.l_whence = 0;
1130 f.l_pid = 0;
1132 switch ( AX_reg(context) & 0xff )
1134 case 0x00: /* LOCK */
1135 f.l_type = F_WRLCK;
1136 break;
1138 case 0x01: /* UNLOCK */
1139 f.l_type = F_UNLCK;
1140 break;
1142 default:
1143 AX_reg(context) = 0x0001;
1144 SET_CFLAG(context);
1145 return;
1149 result = fcntl(BX_reg(context),F_SETLK,&f);
1150 if ( retries && (!result) )
1152 int i;
1153 for(i=0;i<32768*((int)sharing_pause);i++)
1154 result++; /* stop the optimizer */
1155 for(i=0;i<32768*((int)sharing_pause);i++)
1156 result--;
1159 while( (!result) && (!(retries--)) );
1161 if(result)
1163 errno_to_doserr();
1164 AX_reg(context) = ExtendedError;
1165 SET_CFLAG(context);
1166 return;
1169 Error (0,0,0);
1170 RESET_CFLAG(context);
1174 static void GetFileAttribute (struct sigcontext_struct * context)
1176 char *filename = PTR_SEG_OFF_TO_LIN (DS_reg(context),DX_reg(context));
1177 struct stat s;
1178 int res;
1180 res = stat(DOS_GetUnixFileName(filename), &s);
1181 if (res==-1)
1183 errno_to_doserr();
1184 AX_reg(context) = ExtendedError;
1185 SET_CFLAG(context);
1186 return;
1189 CX_reg(context) = 0;
1190 if (S_ISDIR(s.st_mode))
1191 CX_reg(context) |= 0x10;
1192 if ((S_IWRITE & s.st_mode) != S_IWRITE)
1193 CX_reg(context) |= 0x01;
1195 dprintf_int (stddeb, "int21: GetFileAttributes (%s) = 0x%x\n",
1196 filename, CX_reg(context) );
1198 RESET_CFLAG(context);
1199 Error (0,0,0);
1203 extern void LOCAL_PrintHeap (WORD ds);
1205 /***********************************************************************
1206 * DOS3Call (KERNEL.102)
1208 void DOS3Call( struct sigcontext_struct context )
1210 int drive;
1212 if (AH_reg(&context) == 0x59)
1214 GetExtendedErrorInfo(&context);
1215 return;
1217 else
1219 Error (0,0,0);
1220 switch(AH_reg(&context))
1222 case 0x00: /* TERMINATE PROGRAM */
1223 TASK_KillCurrentTask( 0 );
1224 break;
1226 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
1227 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
1228 case 0x03: /* READ CHARACTER FROM STDAUX */
1229 case 0x04: /* WRITE CHARACTER TO STDAUX */
1230 case 0x05: /* WRITE CHARACTER TO PRINTER */
1231 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
1232 case 0x07: /* DIRECT CHARACTER INPUT, WITHOUT ECHO */
1233 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
1234 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
1235 case 0x0a: /* BUFFERED INPUT */
1236 case 0x0b: /* GET STDIN STATUS */
1237 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
1238 case 0x0f: /* OPEN FILE USING FCB */
1239 case 0x10: /* CLOSE FILE USING FCB */
1240 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1241 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
1242 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
1243 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
1244 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
1245 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
1246 case 0x23: /* GET FILE SIZE FOR FCB */
1247 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
1248 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
1249 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
1250 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
1251 case 0x29: /* PARSE FILENAME INTO FCB */
1252 case 0x2e: /* SET VERIFY FLAG */
1253 INT_BARF( &context, 0x21 );
1254 break;
1256 case 0x37: /* "SWITCHAR" - GET SWITCH CHARACTER
1257 "SWITCHAR" - SET SWITCH CHARACTER
1258 "AVAILDEV" - SPECIFY \DEV\ PREFIX USE */
1259 case 0x54: /* GET VERIFY FLAG */
1260 INT_BARF( &context, 0x21 );
1261 break;
1263 case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
1264 case 0x1d:
1265 case 0x1e:
1266 case 0x20:
1267 case 0x6b: /* NULL FUNCTION */
1268 AL_reg(&context) = 0;
1269 break;
1271 case 0x5c: /* "FLOCK" - RECORD LOCKING */
1272 fLock(&context);
1273 break;
1275 case 0x0d: /* DISK BUFFER FLUSH */
1276 RESET_CFLAG(&context); /* dos 6+ only */
1277 break;
1279 case 0x0e: /* SELECT DEFAULT DRIVE */
1280 if (!DOS_ValidDrive(DL_reg(&context)))
1281 Error (InvalidDrive, EC_MediaError, EL_Disk);
1282 else
1284 DOS_SetDefaultDrive(DL_reg(&context));
1285 Error(0,0,0);
1287 AL_reg(&context) = MAX_DOS_DRIVES;
1288 break;
1290 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1291 FindFirstFCB(&context);
1292 break;
1294 case 0x13: /* DELETE FILE USING FCB */
1295 DeleteFileFCB(&context);
1296 break;
1298 case 0x17: /* RENAME FILE USING FCB */
1299 RenameFileFCB(&context);
1300 break;
1302 case 0x19: /* GET CURRENT DEFAULT DRIVE */
1303 AL_reg(&context) = DOS_GetDefaultDrive();
1304 Error (0,0,0);
1305 break;
1307 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1309 TDB *pTask = (TDB *)GlobalLock( GetCurrentTask() );
1310 pTask->dta = MAKELONG( DX_reg(&context), DS_reg(&context) );
1311 dprintf_int(stddeb, "int21: Set DTA: %08lx\n", pTask->dta);
1313 break;
1315 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1316 DL_reg(&context) = 0;
1317 GetDriveAllocInfo(&context);
1318 break;
1320 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1321 GetDriveAllocInfo(&context);
1322 break;
1324 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1325 GetDrivePB(&context, DOS_GetDefaultDrive());
1326 break;
1328 case 0x25: /* SET INTERRUPT VECTOR */
1329 INT_SetHandler( AL_reg(&context),
1330 MAKELONG( DX_reg(&context), DS_reg(&context) ) );
1331 break;
1333 case 0x2a: /* GET SYSTEM DATE */
1334 GetSystemDate(&context);
1335 break;
1337 case 0x2b: /* SET SYSTEM DATE */
1338 fprintf( stdnimp, "SetSystemDate(%02d/%02d/%04d): not allowed\n",
1339 DL_reg(&context), DH_reg(&context), CX_reg(&context) );
1340 AL_reg(&context) = 0; /* Let's pretend we succeeded */
1341 break;
1343 case 0x2c: /* GET SYSTEM TIME */
1344 GetSystemTime(&context);
1345 break;
1347 case 0x2d: /* SET SYSTEM TIME */
1348 fprintf( stdnimp, "SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1349 CH_reg(&context), CL_reg(&context),
1350 DH_reg(&context), DL_reg(&context) );
1351 AL_reg(&context) = 0; /* Let's pretend we succeeded */
1352 break;
1354 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1356 TDB *pTask = (TDB *)GlobalLock( GetCurrentTask() );
1357 ES_reg(&context) = SELECTOROF( pTask->dta );
1358 BX_reg(&context) = OFFSETOF( pTask->dta );
1360 break;
1362 case 0x30: /* GET DOS VERSION */
1363 AX_reg(&context) = DOSVERSION;
1364 BX_reg(&context) = 0x0012; /* 0x123456 is Wine's serial # */
1365 CX_reg(&context) = 0x3456;
1366 break;
1368 case 0x31: /* TERMINATE AND STAY RESIDENT */
1369 INT_BARF( &context, 0x21 );
1370 break;
1372 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1373 GetDrivePB(&context, (DL_reg(&context) == 0) ?
1374 (DOS_GetDefaultDrive()) : (DL_reg(&context)-1));
1375 break;
1377 case 0x33: /* MULTIPLEXED */
1378 switch (AL_reg(&context))
1380 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1381 DL_reg(&context) = 0;
1382 break;
1384 case 0x01: /* SET EXTENDED BREAK STATE */
1385 break;
1387 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1388 DL_reg(&context) = 0;
1389 break;
1391 case 0x05: /* GET BOOT DRIVE */
1392 DL_reg(&context) = 2;
1393 /* c: is Wine's bootdrive */
1394 break;
1396 case 0x06: /* GET TRUE VERSION NUMBER */
1397 BX_reg(&context) = DOSVERSION;
1398 DX_reg(&context) = 0x00;
1399 break;
1401 default:
1402 INT_BARF( &context, 0x21 );
1403 break;
1405 break;
1407 case 0x34: /* GET ADDRESS OF INDOS FLAG */
1408 ES_reg(&context) = DosHeapHandle;
1409 BX_reg(&context) = (int)&heap->InDosFlag - (int)heap;
1410 break;
1412 case 0x35: /* GET INTERRUPT VECTOR */
1414 SEGPTR addr = INT_GetHandler( AL_reg(&context) );
1415 ES_reg(&context) = SELECTOROF(addr);
1416 BX_reg(&context) = OFFSETOF(addr);
1418 break;
1420 case 0x36: /* GET FREE DISK SPACE */
1421 GetFreeDiskSpace(&context);
1422 break;
1424 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1425 AX_reg(&context) = 0x02; /* no country support available */
1426 SET_CFLAG(&context);
1427 break;
1429 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1430 MakeDir(&context);
1431 break;
1433 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1434 RemoveDir(&context);
1435 break;
1437 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1438 ChangeDir(&context);
1439 break;
1441 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1442 CreateFile(&context);
1443 break;
1445 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1446 OpenExistingFile(&context);
1447 break;
1449 case 0x3e: /* "CLOSE" - CLOSE FILE */
1450 CloseFile(&context);
1451 break;
1453 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1454 ReadFile(&context);
1455 break;
1457 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1458 WriteFile(&context);
1459 break;
1461 case 0x41: /* "UNLINK" - DELETE FILE */
1462 if (unlink( DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS_reg(&context),DX_reg(&context))) ) == -1) {
1463 errno_to_doserr();
1464 AX_reg(&context) = ExtendedError;
1465 SET_CFLAG(&context);
1466 break;
1468 Error(0,0,0);
1469 RESET_CFLAG(&context);
1470 break;
1472 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1473 SeekFile(&context);
1474 break;
1476 case 0x43: /* FILE ATTRIBUTES */
1477 switch (AL_reg(&context))
1479 case 0x00:
1480 GetFileAttribute(&context);
1481 break;
1482 case 0x01:
1483 RESET_CFLAG(&context);
1484 break;
1486 break;
1488 case 0x44: /* IOCTL */
1489 switch (AL_reg(&context))
1491 case 0x00:
1492 ioctlGetDeviceInfo(&context);
1493 break;
1495 case 0x08: /* Check if drive is removable. */
1496 drive = BL_reg(&context) ? (BL_reg(&context) - 1)
1497 : DOS_GetDefaultDrive();
1498 if(!DOS_ValidDrive(drive))
1500 Error( InvalidDrive, EC_NotFound, EL_Disk );
1501 AX_reg(&context) = InvalidDrive;
1502 SET_CFLAG(&context);
1504 else
1506 if (drive > 1)
1507 AX_reg(&context) = 1; /* not removable */
1508 else
1509 AX_reg(&context) = 0; /* removable */
1510 RESET_CFLAG(&context);
1512 break;
1514 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
1515 drive = BL_reg(&context) ? (BL_reg(&context) - 1)
1516 : DOS_GetDefaultDrive();
1517 if(!DOS_ValidDrive(drive))
1519 Error( InvalidDrive, EC_NotFound, EL_Disk );
1520 AX_reg(&context) = InvalidDrive;
1521 SET_CFLAG(&context);
1523 else
1525 DX_reg(&context) = (1<<9) | (1<<12) | (1<<15);
1526 RESET_CFLAG(&context);
1528 break;
1529 case 0x0a: /* check if handle (BX) is remote */
1530 /* returns DX, bit 15 set if remote, bit 14 set if date/time
1531 * not set on close
1533 DX_reg(&context) = 0;
1534 RESET_CFLAG(&context);
1535 break;
1536 case 0x0b: /* SET SHARING RETRY COUNT */
1537 if (!CX_reg(&context))
1539 AX_reg(&context) = 1;
1540 SET_CFLAG(&context);
1541 break;
1543 sharing_pause = CX_reg(&context);
1544 if (!DX_reg(&context))
1545 sharing_retries = DX_reg(&context);
1546 RESET_CFLAG(&context);
1547 break;
1549 case 0x0d:
1550 ioctlGenericBlkDevReq(&context);
1551 break;
1553 case 0x0F: /* Set logical drive mapping */
1554 /* FIXME: Not implemented at the moment, always returns error
1556 AX_reg(&context) = 0x0001; /* invalid function */
1557 SET_CFLAG(&context);
1558 break;
1560 default:
1561 INT_BARF( &context, 0x21 );
1562 break;
1564 break;
1566 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1567 if ((AX_reg(&context) = dup(BX_reg(&context))) == 0xffff)
1569 errno_to_doserr();
1570 AX_reg(&context) = ExtendedError;
1571 SET_CFLAG(&context);
1573 else RESET_CFLAG(&context);
1574 break;
1576 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1577 if (dup2( BX_reg(&context), CX_reg(&context) ) == -1)
1579 errno_to_doserr();
1580 AX_reg(&context) = ExtendedError;
1581 SET_CFLAG(&context);
1583 else RESET_CFLAG(&context);
1584 break;
1586 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1587 GetCurrentDirectory(&context);
1588 AX_reg(&context) = 0x0100;
1589 /* intlist: many Microsoft products for Windows rely on this */
1590 break;
1592 case 0x48: /* ALLOCATE MEMORY */
1593 case 0x49: /* FREE MEMORY */
1594 case 0x4a: /* RESIZE MEMORY BLOCK */
1595 INT_BARF( &context, 0x21 );
1596 break;
1598 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1599 WinExec( PTR_SEG_OFF_TO_LIN(DS_reg(&context),DX_reg(&context)),
1600 SW_NORMAL );
1601 break;
1603 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1604 TASK_KillCurrentTask( AL_reg(&context) );
1605 break;
1607 case 0x4d: /* GET RETURN CODE */
1608 AX_reg(&context) = NoError; /* normal exit */
1609 break;
1611 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1612 FindFirst(&context);
1613 break;
1615 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1616 FindNext(&context);
1617 break;
1619 case 0x51: /* GET PSP ADDRESS */
1620 case 0x62: /* GET PSP ADDRESS */
1621 /* FIXME: should we return the original DOS PSP upon */
1622 /* Windows startup ? */
1623 BX_reg(&context) = GetCurrentPDB();
1624 break;
1626 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1627 ES_reg(&context) = 0x0;
1628 BX_reg(&context) = 0x0;
1629 INT_BARF( &context, 0x21 );
1630 break;
1632 case 0x56: /* "RENAME" - RENAME FILE */
1633 RenameFile(&context);
1634 break;
1636 case 0x57: /* FILE DATE AND TIME */
1637 switch (AL_reg(&context))
1639 case 0x00:
1640 GetFileDateTime(&context);
1641 break;
1642 case 0x01:
1643 SetFileDateTime(&context);
1644 break;
1646 break;
1648 case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1649 switch (AL_reg(&context))
1651 case 0x00:
1652 AX_reg(&context) = 1;
1653 break;
1654 case 0x02:
1655 AX_reg(&context) = 0;
1656 break;
1657 case 0x01:
1658 case 0x03:
1659 break;
1661 RESET_CFLAG(&context);
1662 break;
1664 case 0x5a: /* CREATE TEMPORARY FILE */
1665 CreateTempFile(&context);
1666 break;
1668 case 0x5b: /* CREATE NEW FILE */
1669 CreateNewFile(&context);
1670 break;
1672 case 0x5d: /* NETWORK */
1673 case 0x5e:
1674 /* network software not installed */
1675 AX_reg(&context) = NoNetwork;
1676 SET_CFLAG(&context);
1677 break;
1679 case 0x5f: /* NETWORK */
1680 switch (AL_reg(&context))
1682 case 0x07: /* ENABLE DRIVE */
1683 if (!DOS_EnableDrive(DL_reg(&context)))
1685 Error(InvalidDrive, EC_MediaError , EL_Disk);
1686 AX_reg(&context) = InvalidDrive;
1687 SET_CFLAG(&context);
1688 break;
1690 else
1692 RESET_CFLAG(&context);
1693 break;
1695 case 0x08: /* DISABLE DRIVE */
1696 if (!DOS_DisableDrive(DL_reg(&context)))
1698 Error(InvalidDrive, EC_MediaError , EL_Disk);
1699 AX_reg(&context) = InvalidDrive;
1700 SET_CFLAG(&context);
1701 break;
1703 else
1705 RESET_CFLAG(&context);
1706 break;
1708 default:
1709 /* network software not installed */
1710 AX_reg(&context) = NoNetwork;
1711 SET_CFLAG(&context);
1712 break;
1714 break;
1716 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1717 strncpy(PTR_SEG_OFF_TO_LIN(ES_reg(&context),DI_reg(&context)),
1718 PTR_SEG_OFF_TO_LIN(DS_reg(&context),SI_reg(&context)),
1719 strlen(PTR_SEG_OFF_TO_LIN(DS_reg(&context),SI_reg(&context))) & 0x7f);
1720 RESET_CFLAG(&context);
1721 break;
1723 case 0x61: /* UNUSED */
1724 case 0x63: /* UNUSED */
1725 case 0x64: /* OS/2 DOS BOX */
1726 case 0x65: /* GET EXTENDED COUNTRY INFORMATION */
1727 INT_BARF( &context, 0x21 );
1728 break;
1730 case 0x66: /* GLOBAL CODE PAGE TABLE */
1731 switch (AL_reg(&context))
1733 case 0x01:
1734 DX_reg(&context) = BX_reg(&context) = CodePage;
1735 RESET_CFLAG(&context);
1736 break;
1737 case 0x02:
1738 CodePage = BX_reg(&context);
1739 RESET_CFLAG(&context);
1740 break;
1742 break;
1744 case 0x67: /* SET HANDLE COUNT */
1745 RESET_CFLAG(&context);
1746 break;
1748 case 0x68: /* "FFLUSH" - COMMIT FILE */
1749 case 0x6a: /* COMMIT FILE */
1750 fsync( BX_reg(&context) );
1751 RESET_CFLAG(&context);
1752 break;
1754 case 0x69: /* DISK SERIAL NUMBER */
1755 switch (AL_reg(&context))
1757 case 0x00:
1758 GetDiskSerialNumber(&context);
1759 break;
1760 case 0x01:
1761 SetDiskSerialNumber(&context);
1762 break;
1764 break;
1766 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
1767 break;
1769 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
1770 break;
1772 default:
1773 INT_BARF( &context, 0x21 );
1774 break;
1777 dprintf_int(stddeb,"ret21: AX %04x, BX %04x, CX %04x, DX %04x, "
1778 "SI %04x, DI %04x, DS %04x, ES %04x EFL %08lx\n",
1779 AX_reg(&context), BX_reg(&context), CX_reg(&context),
1780 DX_reg(&context), SI_reg(&context), DI_reg(&context),
1781 DS_reg(&context), ES_reg(&context), EFL_reg(&context));
1785 void INT21_Init(void)
1787 if ((DosHeapHandle = GlobalAlloc(GMEM_FIXED,sizeof(struct DosHeap))) == 0)
1789 fprintf( stderr, "INT21_Init: Out of memory\n");
1790 exit(1);
1792 heap = (struct DosHeap *) GlobalLock(DosHeapHandle);
1794 dpb = &heap->dpb;
1795 dpbsegptr = MAKELONG( (int)&heap->dpb - (int)heap, DosHeapHandle );
1796 heap->InDosFlag = 0;
1797 strcpy(heap->biosdate, "01/01/80");