2 * (c) 1993, 1994 Erik Bos
14 #include <sys/types.h>
21 #include "registers.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
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;
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)
78 Action = SA_Ask4Retry;
85 void errno_to_doserr(void)
89 Error (ShareViolation, EC_Temporary, EL_Unknown);
92 Error (InvalidHandle, EC_AppError, EL_Unknown);
95 Error (DiskFull, EC_MediaError, EL_Disk);
100 Error (WriteProtected, EC_AccessDenied, EL_Unknown);
103 Error (LockViolation, EC_AccessDenied, EL_Unknown);
106 Error (FileNotFound, EC_NotFound, EL_Unknown);
109 Error (CanNotMakeDir, EC_AccessDenied, EL_Unknown);
113 Error (NoMoreFiles, EC_MediaError, EL_Unknown);
116 Error (FileExists, EC_Exists, EL_Disk);
119 dprintf_int(stddeb, "int21: unknown errno %d!\n", errno);
120 Error (GeneralFailure, EC_SystemFailure, EL_Unknown);
125 BYTE *GetCurrentDTA(void)
127 TDB *pTask = (TDB *)GlobalLock( GetCurrentTask() );
128 return (BYTE *)PTR_SEG_TO_LIN( pTask->dta );
132 void ChopOffWhiteSpace(char *string)
136 for (length = strlen(string) ; length ; length--)
137 if (string[length] == ' ')
138 string[length] = '\0';
141 static void CreateBPB(int drive, BYTE *data)
146 setword(&data[3], 0);
148 setword(&data[6], 240);
149 setword(&data[8], 64000);
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);
157 setword(&data[0x22], 1);
158 } else { /* 1.44mb */
161 setword(&data[3], 0);
163 setword(&data[6], 240);
164 setword(&data[8], 2880);
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);
172 setword(&data[0x22], 2);
176 static void GetFreeDiskSpace(struct sigcontext_struct *context)
182 drive = DOS_GetDefaultDrive();
186 if (!DOS_ValidDrive(drive)) {
187 Error(InvalidDrive, EC_MediaError , EL_Disk);
192 if (!DOS_GetFreeSpace(drive, &size, &available)) {
193 Error(GeneralFailure, EC_MediaError , EL_Disk);
198 AX = (drive < 2) ? 1 : 64; /* 64 for hard-disks, 1 for diskettes */
201 BX = (available / (CX * AX));
202 DX = (size / (CX * AX));
206 static void GetDriveAllocInfo(struct sigcontext_struct *context)
209 long size, available;
212 drive = DOS_GetDefaultDrive();
216 if (!DOS_ValidDrive(DL)) {
220 Error (InvalidDrive, EC_MediaError, EL_Disk);
224 if (!DOS_GetFreeSpace(DL, &size, &available)) {
225 Error(GeneralFailure, EC_MediaError , EL_Disk);
230 AX = (drive < 2) ? 1 : 64; /* 64 for hard-disks, 1 for diskettes */
232 DX = (size / (CX * AX));
234 heap->mediaID = 0xf0;
237 BX = (int)&heap->mediaID - (int)heap;
241 static void GetDrivePB(struct sigcontext_struct *context, int drive)
243 if(!DOS_ValidDrive(drive))
245 Error (InvalidDrive, EC_MediaError, EL_Disk);
250 dprintf_int(stddeb, "int21: GetDrivePB not fully implemented.\n");
252 /* FIXME: I have no idea what a lot of this information should
253 * say or whether it even really matters since we're not allowing
254 * direct block access. However, some programs seem to depend on
255 * getting at least _something_ back from here. The 'next' pointer
256 * does worry me, though. Should we have a complete table of
257 * separate DPBs per drive? Probably, but I'm lazy. :-) -CH
259 dpb->drive_num = dpb->unit_num = drive; /* The same? */
260 dpb->sector_size = 512;
261 dpb->high_sector = 1;
262 dpb->shift = drive < 2 ? 0 : 6; /* 6 for HD, 0 for floppy */
265 dpb->dir_entries = 2;
267 dpb->high_cluster = 64000;
268 dpb->sectors_in_FAT = 1;
270 dpb->driver_head = 0;
271 dpb->media_ID = (drive > 1) ? 0xF8 : 0xF0;
272 dpb->access_flag = 0;
274 dpb->free_search = 0;
275 dpb->free_clusters = 0xFFFF; /* unknown */
278 DS = SELECTOROF(dpbsegptr);
279 BX = OFFSETOF(dpbsegptr);
283 static void ReadFile(struct sigcontext_struct *context)
288 /* can't read from stdout / stderr */
289 if ((BX == 1) || (BX == 2)) {
290 Error (InvalidHandle, EL_Unknown, EC_Unknown);
294 "int21: read (%d, void *, 0x%x) = EBADF\n", BX, CX);
298 ptr = PTR_SEG_OFF_TO_LIN (DS,DX);
305 "int21: read (%d, void *, 0x%x) = EOF\n", BX, CX);
308 size = read(BX, ptr, CX);
310 "int21: read (%d, void *, 0x%x) = 0x%x\n",
324 static void WriteFile(struct sigcontext_struct *context)
329 ptr = PTR_SEG_OFF_TO_LIN (DS,DX);
332 Error (InvalidHandle, EC_Unknown, EL_Unknown);
339 for (x = 0;x != CX;x++) {
340 dprintf_int(stddeb, "%c", *ptr++);
348 size = write(BX, ptr , CX);
350 Error (WriteFault, EC_Unknown, EL_Unknown);
367 static void SeekFile(struct sigcontext_struct *context)
369 off_t status, fileoffset;
372 case 1: fileoffset = SEEK_CUR;
374 case 2: fileoffset = SEEK_END;
377 case 0: fileoffset = SEEK_SET;
380 status = lseek(BX, (CX << 16) + DX, fileoffset);
382 dprintf_int (stddeb, "int21: seek (%d, 0x%x, %d) = 0x%lx\n",
383 BX, (CX << 16) + DX, AL, status);
397 static void ioctlGetDeviceInfo(struct sigcontext_struct *context)
400 dprintf_int (stddeb, "int21: ioctl (%d, GetDeviceInfo)\n", BX);
406 DX = 0x80d0 | (1 << (BX != 0));
414 if (fstat(BX, &sbuf) < 0)
421 /* This isn't the right answer, but should be close enough. */
428 static void ioctlGenericBlkDevReq(struct sigcontext_struct *context)
430 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS, DX);
434 drive = DOS_GetDefaultDrive();
438 if (!DOS_ValidDrive(drive)) {
439 Error( FileNotFound, EC_NotFound, EL_Disk );
450 case 0x60: /* get device parameters */
451 /* used by w4wgrp's winfile */
452 memset(dataptr, 0, 0x26);
454 dataptr[6] = 0; /* media type */
457 dataptr[1] = 0x05; /* fixed disk */
458 setword(&dataptr[2], 0x01); /* non removable */
459 setword(&dataptr[4], 0x300); /* # of cylinders */
463 dataptr[1] = 0x07; /* block dev, floppy */
464 setword(&dataptr[2], 0x02); /* removable */
465 setword(&dataptr[4], 80); /* # of cylinders */
467 CreateBPB(drive, &dataptr[7]);
475 static void GetSystemDate(struct sigcontext_struct *context)
481 now = localtime(<ime);
483 CX = now->tm_year + 1900;
484 DX = ((now->tm_mon + 1) << 8) | now->tm_mday;
488 static void GetSystemTime(struct sigcontext_struct *context)
493 gettimeofday(&tv,NULL); /* Note use of gettimeofday(), instead of time() */
494 now = localtime(&tv.tv_sec);
496 CX = (now->tm_hour<<8) | now->tm_min;
497 DX = (now->tm_sec<<8) | tv.tv_usec/10000;
498 /* Note hundredths of seconds */
501 static void GetExtendedErrorInfo(struct sigcontext_struct *context)
504 BX = (ErrorClass << 8) | Action;
508 static void CreateFile(struct sigcontext_struct *context)
511 handle = open(DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS,DX)),
512 O_CREAT | O_TRUNC | O_RDWR, 0666 );
524 void OpenExistingFile(struct sigcontext_struct *context)
545 if ((handle = open(DOS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS,DX)),
548 if( Options.allowReadOnly )
549 handle = open( DOS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS,DX)),
553 dprintf_int (stddeb, "int21: open (%s, %d) = -1\n",
554 DOS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS,DX)), mode);
562 dprintf_int (stddeb, "int21: open (%s, %d) = %d\n",
563 DOS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS,DX)), mode, handle);
567 case 0x00: /* compatability mode */
568 case 0x40: /* DENYNONE */
572 case 0x30: /* DENYREAD */
574 "OpenExistingFile (%s): DENYREAD changed to DENYALL\n",
575 (char *)PTR_SEG_OFF_TO_LIN(DS,DX));
576 case 0x10: /* DENYALL */
580 case 0x20: /* DENYWRITE */
591 int result,retries=sharing_retries;
593 result = flock(handle, lock | LOCK_NB);
594 if ( retries && (!result) )
597 for(i=0;i<32768*((int)sharing_pause);i++)
598 result++; /* stop the optimizer */
599 for(i=0;i<32768*((int)sharing_pause);i++)
603 while( (!result) && (!(retries--)) );
621 static void CloseFile(struct sigcontext_struct *context)
623 dprintf_int (stddeb, "int21: close (%d)\n", BX);
625 if (close(BX) == -1) {
636 static void RenameFile(struct sigcontext_struct *context)
638 char *newname, *oldname;
640 /* FIXME: should not rename over an existing file */
641 dprintf_int(stddeb,"int21: renaming %s to %s\n",
642 (char *)PTR_SEG_OFF_TO_LIN(DS,DX), (char *)PTR_SEG_OFF_TO_LIN(ES,DI) );
644 oldname = DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS,DX) );
645 newname = DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(ES,DI) );
647 rename( oldname, newname);
652 static void MakeDir(struct sigcontext_struct *context)
656 dprintf_int(stddeb,"int21: makedir %s\n", (char *)PTR_SEG_OFF_TO_LIN(DS,DX) );
658 if ((dirname = DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS,DX) ))== NULL) {
659 Error( CanNotMakeDir, EC_AccessDenied, EL_Disk );
665 if (mkdir(dirname,0) == -1) {
666 Error( CanNotMakeDir, EC_AccessDenied, EL_Disk );
674 static void ChangeDir(struct sigcontext_struct *context)
677 char *dirname = PTR_SEG_OFF_TO_LIN(DS,DX);
678 drive = DOS_GetDefaultDrive();
679 dprintf_int(stddeb,"int21: changedir %s\n", dirname);
680 if (dirname != NULL && dirname[1] == ':') {
681 drive = toupper(dirname[0]) - 'A';
684 if (!DOS_ChangeDir(drive, dirname))
691 static void RemoveDir(struct sigcontext_struct *context)
695 dprintf_int(stddeb,"int21: removedir %s\n", (char *)PTR_SEG_OFF_TO_LIN(DS,DX) );
697 if ((dirname = DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS,DX) ))== NULL) {
698 Error( PathNotFound, EC_NotFound, EL_Disk );
705 if (strcmp(unixname,DosDrives[drive].CurrentDirectory)) {
706 AL = CanNotRemoveCwd;
710 if (rmdir(dirname) == -1) {
711 Error( AccessDenied, EC_AccessDenied, EL_Disk );
719 static void FindNext(struct sigcontext_struct *context)
721 struct dosdirent *dp;
723 BYTE *dta = GetCurrentDTA();
725 memcpy(&dp, dta+0x11, sizeof(dp));
727 dprintf_int(stddeb, "int21: FindNext, dta %p, dp %p\n", dta, dp);
729 if ((dp = DOS_readdir(dp)) == NULL) {
730 Error(NoMoreFiles, EC_MediaError , EL_Disk);
735 } /* while (*(dta + 0x0c) != dp->attribute);*/
736 while ( ( dp->search_attribute & dp->attribute) != dp->attribute);
738 *(dta + 0x15) = dp->attribute;
739 setword(&dta[0x0d], dp->entnum);
741 t = localtime(&(dp->filetime));
742 setword(&dta[0x16], (t->tm_hour << 11) + (t->tm_min << 5) +
743 (t->tm_sec / 2)); /* time */
744 setword(&dta[0x18], ((t->tm_year - 80) << 9) + (t->tm_mon << 5) +
745 (t->tm_mday)); /* date */
746 setdword(&dta[0x1a], dp->filesize);
747 strncpy(dta + 0x1e, dp->filename, 13);
752 dprintf_int(stddeb, "int21: FindNext -- (%s) index=%d size=%ld\n", dp->filename, dp->entnum, dp->filesize);
756 static void FindFirst(struct sigcontext_struct *context)
758 BYTE drive, *path = PTR_SEG_OFF_TO_LIN(DS, DX);
759 struct dosdirent *dp;
761 BYTE *dta = GetCurrentDTA();
763 dprintf_int(stddeb, "int21: FindFirst path = %s\n", path);
765 if ((*path)&&(path[1] == ':')) {
766 drive = (islower(*path) ? toupper(*path) : *path) - 'A';
768 if (!DOS_ValidDrive(drive)) {
769 Error(InvalidDrive, EC_MediaError , EL_Disk);
775 drive = DOS_GetDefaultDrive();
778 memset(dta + 1 , '?', 11);
779 *(dta + 0x0c) = ECX & (FA_LABEL | FA_DIREC);
781 if ((dp = DOS_opendir(path)) == NULL) {
782 Error(PathNotFound, EC_MediaError, EL_Disk);
788 dp->search_attribute = ECX & (FA_LABEL | FA_DIREC);
789 memcpy(dta + 0x11, &dp, sizeof(dp));
793 static void GetFileDateTime(struct sigcontext_struct *context)
795 struct stat filestat;
798 fstat( BX, &filestat );
799 now = localtime (&filestat.st_mtime);
801 CX = ((now->tm_hour * 0x2000) + (now->tm_min * 0x20) + now->tm_sec/2);
802 DX = (((now->tm_year + 1900 - 1980) * 0x200) +
803 (now->tm_mon * 0x20) + now->tm_mday);
808 static void SetFileDateTime(struct sigcontext_struct *context)
811 struct utimbuf filetime;
813 /* FIXME: Argument isn't the name of the file in DS:DX,
814 but the file handle in BX */
815 filename = DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS,DX) );
817 filetime.actime = 0L;
818 filetime.modtime = filetime.actime;
820 utime(filename, &filetime);
824 static void CreateTempFile(struct sigcontext_struct *context)
829 sprintf(temp,"%s\\win%d.tmp",TempDirectory,(int) getpid());
831 dprintf_int(stddeb,"CreateTempFile %s\n",temp);
833 handle = open(DOS_GetUnixFileName(temp), O_CREAT | O_TRUNC | O_RDWR, 0666);
836 Error( WriteProtected, EC_AccessDenied, EL_Disk );
842 strcpy(PTR_SEG_OFF_TO_LIN(DS,DX), temp);
848 static void CreateNewFile(struct sigcontext_struct *context)
852 if ((handle = open(DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS,DX) ), O_CREAT | O_EXCL | O_RDWR, 0666)) == -1) {
853 Error( WriteProtected, EC_AccessDenied, EL_Disk );
863 static void GetCurrentDirectory(struct sigcontext_struct *context)
868 drive = DOS_GetDefaultDrive();
872 if (!DOS_ValidDrive(drive)) {
873 Error( InvalidDrive, EC_NotFound, EL_Disk );
879 strcpy(PTR_SEG_OFF_TO_LIN(DS,SI), DOS_GetCurrentDir(drive) );
883 static void GetDiskSerialNumber(struct sigcontext_struct *context)
886 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS, DX);
890 drive = DOS_GetDefaultDrive();
894 if (!DOS_ValidDrive(drive)) {
895 Error( InvalidDrive, EC_NotFound, EL_Disk );
901 DOS_GetSerialNumber(drive, &serialnumber);
904 setdword(&dataptr[2], serialnumber);
905 strncpy(dataptr + 6, DOS_GetVolumeLabel(drive), 8);
906 strncpy(dataptr + 0x11, "FAT16 ", 8);
912 static void SetDiskSerialNumber(struct sigcontext_struct *context)
915 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS, DX);
919 drive = DOS_GetDefaultDrive();
923 if (!DOS_ValidDrive(drive)) {
924 Error( InvalidDrive, EC_NotFound, EL_Disk );
930 serialnumber = dataptr[1] + (dataptr[2] << 8) + (dataptr[3] << 16) +
933 DOS_SetSerialNumber(drive, serialnumber);
938 static void DumpFCB(BYTE *fcb)
944 for (y = 0; y !=2 ; y++) {
945 for (x = 0; x!=15;x++)
946 dprintf_int(stddeb, "%02x ", *fcb++);
947 dprintf_int(stddeb,"\n");
951 /* microsoft's programmers should be shot for using CP/M style int21
952 calls in Windows for Workgroup's winfile.exe */
954 static void FindFirstFCB(struct sigcontext_struct *context)
956 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS, DX);
957 struct fcb *standard_fcb;
958 struct fcb *output_fcb;
962 BYTE *dta = GetCurrentDTA();
968 standard_fcb = (struct fcb *)(fcb + 7);
969 output_fcb = (struct fcb *)(dta + 7);
974 standard_fcb = (struct fcb *)fcb;
975 output_fcb = (struct fcb *)dta;
978 if (standard_fcb->drive)
980 drive = standard_fcb->drive - 1;
981 if (!DOS_ValidDrive(drive))
983 Error (InvalidDrive, EC_MediaError, EL_Disk);
989 drive = DOS_GetDefaultDrive();
991 output_fcb->drive = drive;
995 if (*(fcb+6) & FA_LABEL) /* return volume label */
998 memset(&output_fcb->name, ' ', 11);
999 if (DOS_GetVolumeLabel(drive) != NULL)
1001 strncpy(output_fcb->name, DOS_GetVolumeLabel(drive), 11);
1008 strncpy(output_fcb->name, standard_fcb->name, 11);
1010 *(dta+6) = ( *(fcb+6) & (!FA_DIREC));
1012 sprintf(path,"%c:*.*",drive+'A');
1013 if ((output_fcb->directory = DOS_opendir(path))==NULL)
1015 Error (PathNotFound, EC_MediaError, EL_Disk);
1023 static void DeleteFileFCB(struct sigcontext_struct *context)
1025 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS, DX);
1027 struct dosdirent *dp;
1028 char temp[256], *ptr;
1035 drive = DOS_GetDefaultDrive();
1037 strcpy(temp, DOS_GetCurrentDir(drive));
1039 strncat(temp, fcb + 1, 8);
1040 ChopOffWhiteSpace(temp);
1041 strncat(temp, fcb + 9, 3);
1042 ChopOffWhiteSpace(temp);
1044 if ((dp = DOS_opendir(temp)) == NULL) {
1045 Error(InvalidDrive, EC_MediaError , EL_Disk);
1050 strcpy(temp, DOS_GetCurrentDir(drive) );
1053 ptr = temp + strlen(temp);
1055 while (DOS_readdir(dp) != NULL)
1057 strcpy(ptr, dp->filename);
1058 dprintf_int(stddeb, "int21: delete file %s\n", temp);
1059 /* unlink(DOS_GetUnixFileName(temp)); */
1065 static void RenameFileFCB(struct sigcontext_struct *context)
1067 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS, DX);
1069 struct dosdirent *dp;
1070 char temp[256], oldname[256], newname[256], *oldnameptr, *newnameptr;
1077 drive = DOS_GetDefaultDrive();
1079 strcpy(temp, DOS_GetCurrentDir(drive));
1081 strncat(temp, fcb + 1, 8);
1082 ChopOffWhiteSpace(temp);
1083 strncat(temp, fcb + 9, 3);
1084 ChopOffWhiteSpace(temp);
1086 if ((dp = DOS_opendir(temp)) == NULL) {
1087 Error(InvalidDrive, EC_MediaError , EL_Disk);
1092 strcpy(oldname, DOS_GetCurrentDir(drive) );
1093 strcat(oldname, "\\");
1094 oldnameptr = oldname + strlen(oldname);
1096 strcpy(newname, DOS_GetCurrentDir(drive) );
1097 strcat(newname, "\\");
1098 newnameptr = newname + strlen(newname);
1100 while (DOS_readdir(dp) != NULL)
1102 strcpy(oldnameptr, dp->filename);
1103 strcpy(newnameptr, fcb + 1);
1104 dprintf_int(stddeb, "int21: renamefile %s -> %s\n",
1113 static void fLock (struct sigcontext_struct * context)
1116 int result,retries=sharing_retries;
1118 f.l_start = MAKELONG(DX,CX);
1119 f.l_len = MAKELONG(DI,SI);
1123 switch ( AX & 0xff )
1125 case 0x00: /* LOCK */
1129 case 0x01: /* UNLOCK */
1140 result = fcntl(BX,F_SETLK,&f);
1141 if ( retries && (!result) )
1144 for(i=0;i<32768*((int)sharing_pause);i++)
1145 result++; /* stop the optimizer */
1146 for(i=0;i<32768*((int)sharing_pause);i++)
1150 while( (!result) && (!(retries--)) );
1165 static void GetFileAttribute (struct sigcontext_struct * context)
1167 char *filename = PTR_SEG_OFF_TO_LIN (DS,DX);
1171 res = stat(DOS_GetUnixFileName(filename), &s);
1181 if (S_ISDIR(s.st_mode))
1183 if ((S_IWRITE & s.st_mode) != S_IWRITE)
1186 dprintf_int (stddeb, "int21: GetFileAttributes (%s) = 0x%x\n",
1194 extern void LOCAL_PrintHeap (WORD ds);
1196 /***********************************************************************
1197 * DOS3Call (KERNEL.102)
1199 void DOS3Call( struct sigcontext_struct sigcontext )
1201 #define context (&sigcontext)
1206 GetExtendedErrorInfo(context);
1214 case 0x00: /* TERMINATE PROGRAM */
1215 TASK_KillCurrentTask( 0 );
1218 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
1219 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
1220 case 0x03: /* READ CHARACTER FROM STDAUX */
1221 case 0x04: /* WRITE CHARACTER TO STDAUX */
1222 case 0x05: /* WRITE CHARACTER TO PRINTER */
1223 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
1224 case 0x07: /* DIRECT CHARACTER INPUT, WITHOUT ECHO */
1225 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
1226 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
1227 case 0x0a: /* BUFFERED INPUT */
1228 case 0x0b: /* GET STDIN STATUS */
1229 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
1230 case 0x0f: /* OPEN FILE USING FCB */
1231 case 0x10: /* CLOSE FILE USING FCB */
1232 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
1233 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
1234 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
1235 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
1236 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
1237 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
1238 case 0x23: /* GET FILE SIZE FOR FCB */
1239 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
1240 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
1241 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
1242 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
1243 case 0x29: /* PARSE FILENAME INTO FCB */
1244 case 0x2e: /* SET VERIFY FLAG */
1248 case 0x2b: /* SET SYSTEM DATE */
1249 case 0x2d: /* SET SYSTEM TIME */
1250 case 0x37: /* "SWITCHAR" - GET SWITCH CHARACTER
1251 "SWITCHAR" - SET SWITCH CHARACTER
1252 "AVAILDEV" - SPECIFY \DEV\ PREFIX USE */
1253 case 0x54: /* GET VERIFY FLAG */
1257 case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
1261 case 0x6b: /* NULL FUNCTION */
1265 case 0x5c: /* "FLOCK" - RECORD LOCKING */
1269 case 0x0d: /* DISK BUFFER FLUSH */
1270 ResetCflag; /* dos 6+ only */
1273 case 0x0e: /* SELECT DEFAULT DRIVE */
1274 if (!DOS_ValidDrive(DL))
1275 Error (InvalidDrive, EC_MediaError, EL_Disk);
1278 DOS_SetDefaultDrive(DL);
1281 AL = MAX_DOS_DRIVES;
1284 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
1285 FindFirstFCB(context);
1288 case 0x13: /* DELETE FILE USING FCB */
1289 DeleteFileFCB(context);
1292 case 0x17: /* RENAME FILE USING FCB */
1293 RenameFileFCB(context);
1296 case 0x19: /* GET CURRENT DEFAULT DRIVE */
1297 AL = DOS_GetDefaultDrive();
1301 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1303 TDB *pTask = (TDB *)GlobalLock( GetCurrentTask() );
1304 pTask->dta = MAKELONG( DX, DS );
1305 dprintf_int(stddeb, "int21: Set DTA: %08lx\n", pTask->dta);
1309 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1311 GetDriveAllocInfo(context);
1314 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1315 GetDriveAllocInfo(context);
1318 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1319 GetDrivePB(context, DOS_GetDefaultDrive());
1322 case 0x25: /* SET INTERRUPT VECTOR */
1323 INT_SetHandler( AL, MAKELONG( DX, DS ) );
1326 case 0x2a: /* GET SYSTEM DATE */
1327 GetSystemDate(context);
1330 case 0x2c: /* GET SYSTEM TIME */
1331 GetSystemTime(context);
1334 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1336 TDB *pTask = (TDB *)GlobalLock( GetCurrentTask() );
1337 ES = SELECTOROF( pTask->dta );
1338 BX = OFFSETOF( pTask->dta );
1342 case 0x30: /* GET DOS VERSION */
1344 BX = 0x0012; /* 0x123456 is Wine's serial # */
1348 case 0x31: /* TERMINATE AND STAY RESIDENT */
1352 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1353 GetDrivePB(context, (DL == 0) ? (DOS_GetDefaultDrive()) : (DL-1));
1356 case 0x33: /* MULTIPLEXED */
1358 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1362 case 0x01: /* SET EXTENDED BREAK STATE */
1365 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1369 case 0x05: /* GET BOOT DRIVE */
1371 /* c: is Wine's bootdrive */
1374 case 0x06: /* GET TRUE VERSION NUMBER */
1385 case 0x34: /* GET ADDRESS OF INDOS FLAG */
1387 BX = (int)&heap->InDosFlag - (int)heap;
1390 case 0x35: /* GET INTERRUPT VECTOR */
1392 SEGPTR addr = INT_GetHandler( AL );
1393 ES = SELECTOROF(addr);
1394 BX = OFFSETOF(addr);
1398 case 0x36: /* GET FREE DISK SPACE */
1399 GetFreeDiskSpace(context);
1402 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1403 AX = 0x02; /* no country support available */
1407 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1411 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1415 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1419 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1420 CreateFile(context);
1423 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1424 OpenExistingFile(context);
1427 case 0x3e: /* "CLOSE" - CLOSE FILE */
1431 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1435 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1439 case 0x41: /* "UNLINK" - DELETE FILE */
1440 if (unlink( DOS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS,DX)) ) == -1) {
1450 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1454 case 0x43: /* FILE ATTRIBUTES */
1458 GetFileAttribute(context);
1466 case 0x44: /* IOCTL */
1470 ioctlGetDeviceInfo(context);
1473 case 0x08: /* Check if drive is removable. */
1474 drive = BL ? (BL - 1) : DOS_GetDefaultDrive();
1475 if(!DOS_ValidDrive(drive))
1477 Error( InvalidDrive, EC_NotFound, EL_Disk );
1484 AX = 1; /* not removable */
1486 AX = 0; /* removable */
1491 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
1492 drive = BL ? (BL - 1) : DOS_GetDefaultDrive();
1493 if(!DOS_ValidDrive(drive))
1495 Error( InvalidDrive, EC_NotFound, EL_Disk );
1501 DX = (1<<9) | (1<<12) | (1<<15);
1506 case 0x0b: /* SET SHARING RETRY COUNT */
1515 sharing_retries = DX;
1520 ioctlGenericBlkDevReq(context);
1523 case 0x0F: /* Set logical drive mapping */
1524 /* FIXME: Not implemented at the moment, always returns error
1526 AX = 0x0001; /* invalid function */
1536 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1537 if ((AX = dup(BX)) == 0xffff)
1546 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1547 if (dup2( BX, CX ) == -1)
1556 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1557 GetCurrentDirectory(context);
1559 /* intlist: many Microsoft products for Windows rely on this */
1562 case 0x48: /* ALLOCATE MEMORY */
1563 case 0x49: /* FREE MEMORY */
1564 case 0x4a: /* RESIZE MEMORY BLOCK */
1568 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1569 WinExec( PTR_SEG_OFF_TO_LIN(DS,DX), SW_NORMAL );
1572 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1573 TASK_KillCurrentTask( AL );
1576 case 0x4d: /* GET RETURN CODE */
1577 AX = NoError; /* normal exit */
1580 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1584 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1588 case 0x51: /* GET PSP ADDRESS */
1589 case 0x62: /* GET PSP ADDRESS */
1590 /* FIXME: should we return the original DOS PSP upon */
1591 /* Windows startup ? */
1592 BX = GetCurrentPDB();
1595 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1601 case 0x56: /* "RENAME" - RENAME FILE */
1602 RenameFile(context);
1605 case 0x57: /* FILE DATE AND TIME */
1609 GetFileDateTime(context);
1612 SetFileDateTime(context);
1617 case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1633 case 0x5a: /* CREATE TEMPORARY FILE */
1634 CreateTempFile(context);
1637 case 0x5b: /* CREATE NEW FILE */
1638 CreateNewFile(context);
1641 case 0x5d: /* NETWORK */
1643 /* network software not installed */
1648 case 0x5f: /* NETWORK */
1651 case 0x07: /* ENABLE DRIVE */
1652 if (!DOS_EnableDrive(DL))
1654 Error(InvalidDrive, EC_MediaError , EL_Disk);
1664 case 0x08: /* DISABLE DRIVE */
1665 if (!DOS_DisableDrive(DL))
1667 Error(InvalidDrive, EC_MediaError , EL_Disk);
1678 /* network software not installed */
1685 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1686 strncpy(PTR_SEG_OFF_TO_LIN(ES,DI), PTR_SEG_OFF_TO_LIN(DS,SI), strlen(PTR_SEG_OFF_TO_LIN(DS,SI)) & 0x7f);
1690 case 0x61: /* UNUSED */
1691 case 0x63: /* UNUSED */
1692 case 0x64: /* OS/2 DOS BOX */
1693 case 0x65: /* GET EXTENDED COUNTRY INFORMATION */
1697 case 0x66: /* GLOBAL CODE PAGE TABLE */
1711 case 0x67: /* SET HANDLE COUNT */
1715 case 0x68: /* "FFLUSH" - COMMIT FILE */
1716 case 0x6a: /* COMMIT FILE */
1721 case 0x69: /* DISK SERIAL NUMBER */
1725 GetDiskSerialNumber(context);
1728 SetDiskSerialNumber(context);
1733 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
1741 dprintf_int(stddeb,"ret21: AX %04x, BX %04x, CX %04x, DX %04x, "
1742 "SI %04x, DI %04x, DS %04x, ES %04x EFL %08lx\n",
1743 AX, BX, CX, DX, SI, DI, DS, ES, EFL);
1749 void INT21_Init(void)
1751 if ((DosHeapHandle = GlobalAlloc(GMEM_FIXED,sizeof(struct DosHeap))) == 0)
1753 fprintf( stderr, "INT21_Init: Out of memory\n");
1756 heap = (struct DosHeap *) GlobalLock(DosHeapHandle);
1759 dpbsegptr = MAKELONG( (int)&heap->dpb - (int)heap, DosHeapHandle );
1760 heap->InDosFlag = 0;
1761 strcpy(heap->biosdate, "01/01/80");