Release 960611
[wine/multimedia.git] / miscemu / int21.c
blobb698d2c66e8f2ac3a452b33a61638759d6d2e034
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 #if defined(__svr4__) || defined(_SCO_DS)
32 /* SVR4 DOESNT do 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 BYTE *GetCurrentDTA(void)
88 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
89 return (BYTE *)PTR_SEG_TO_LIN( pTask->dta );
93 void ChopOffWhiteSpace(char *string)
95 int length;
97 for (length = strlen(string) ; length ; length--)
98 if (string[length] == ' ')
99 string[length] = '\0';
102 static void CreateBPB(int drive, BYTE *data)
104 if (drive > 1) {
105 setword(data, 512);
106 data[2] = 2;
107 setword(&data[3], 0);
108 data[5] = 2;
109 setword(&data[6], 240);
110 setword(&data[8], 64000);
111 data[0x0a] = 0xf8;
112 setword(&data[0x0b], 40);
113 setword(&data[0x0d], 56);
114 setword(&data[0x0f], 2);
115 setword(&data[0x11], 0);
116 setword(&data[0x1f], 800);
117 data[0x21] = 5;
118 setword(&data[0x22], 1);
119 } else { /* 1.44mb */
120 setword(data, 512);
121 data[2] = 2;
122 setword(&data[3], 0);
123 data[5] = 2;
124 setword(&data[6], 240);
125 setword(&data[8], 2880);
126 data[0x0a] = 0xf8;
127 setword(&data[0x0b], 6);
128 setword(&data[0x0d], 18);
129 setword(&data[0x0f], 2);
130 setword(&data[0x11], 0);
131 setword(&data[0x1f], 80);
132 data[0x21] = 7;
133 setword(&data[0x22], 2);
137 static int INT21_GetFreeDiskSpace(struct sigcontext_struct *context)
139 DWORD size, available;
140 int drive = DOS_GET_DRIVE( DL_reg(context) );
142 if (!DRIVE_GetFreeSpace(drive, &size, &available)) return 0;
144 CX_reg(context) = 512; /* bytes per sector */
145 size /= 512;
146 available /= 512;
147 AX_reg(context) = 1; /* sectors per cluster */
148 while (AX_reg(context) * 65530 < size) AX_reg(context) *= 2;
149 BX_reg(context) = available / AX_reg(context); /* free clusters */
150 DX_reg(context) = size / AX_reg(context); /* total clusters */
151 return 1;
154 static int INT21_GetDriveAllocInfo(struct sigcontext_struct *context)
156 if (!INT21_GetFreeDiskSpace( context )) return 0;
157 heap->mediaID = 0xf0;
158 DS_reg(context) = DosHeapHandle;
159 BX_reg(context) = (int)&heap->mediaID - (int)heap;
160 return 1;
163 static void GetDrivePB(struct sigcontext_struct *context, int drive)
165 if(!DRIVE_IsValid(drive))
167 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
168 AX_reg(context) = 0x00ff;
170 else
172 dprintf_int(stddeb, "int21: GetDrivePB not fully implemented.\n");
174 /* FIXME: I have no idea what a lot of this information should
175 * say or whether it even really matters since we're not allowing
176 * direct block access. However, some programs seem to depend on
177 * getting at least _something_ back from here. The 'next' pointer
178 * does worry me, though. Should we have a complete table of
179 * separate DPBs per drive? Probably, but I'm lazy. :-) -CH
181 dpb->drive_num = dpb->unit_num = drive; /* The same? */
182 dpb->sector_size = 512;
183 dpb->high_sector = 1;
184 dpb->shift = drive < 2 ? 0 : 6; /* 6 for HD, 0 for floppy */
185 dpb->reserved = 0;
186 dpb->num_FAT = 1;
187 dpb->dir_entries = 2;
188 dpb->first_data = 2;
189 dpb->high_cluster = 64000;
190 dpb->sectors_in_FAT = 1;
191 dpb->start_dir = 1;
192 dpb->driver_head = 0;
193 dpb->media_ID = (drive > 1) ? 0xF8 : 0xF0;
194 dpb->access_flag = 0;
195 dpb->next = 0;
196 dpb->free_search = 0;
197 dpb->free_clusters = 0xFFFF; /* unknown */
199 AL_reg(context) = 0x00;
200 DS_reg(context) = SELECTOROF(dpbsegptr);
201 BX_reg(context) = OFFSETOF(dpbsegptr);
206 static void ioctlGetDeviceInfo(struct sigcontext_struct *context)
208 dprintf_int (stddeb, "int21: ioctl (%d, GetDeviceInfo)\n", BX_reg(context));
210 DX_reg(context) = 0x0942;
211 /* bits 0-5 are current drive
212 * bit 6 - file has NOT been written..FIXME: correct?
213 * bit 8 - generate int24 if no diskspace on write/ read past end of file
214 * bit 11 - media not removable
216 RESET_CFLAG(context);
219 static void ioctlGenericBlkDevReq(struct sigcontext_struct *context)
221 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
222 int drive = DOS_GET_DRIVE( BL_reg(context) );
224 if (!DRIVE_IsValid(drive))
226 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
227 AX_reg(context) = DOS_ExtendedError;
228 SET_CFLAG(context);
229 return;
232 if (CH_reg(context) != 0x08)
234 INT_BARF( context, 0x21 );
235 return;
237 switch (CL_reg(context)) {
238 case 0x60: /* get device parameters */
239 /* used by w4wgrp's winfile */
240 memset(dataptr, 0, 0x26);
241 dataptr[0] = 0x04;
242 dataptr[6] = 0; /* media type */
243 if (drive > 1)
245 dataptr[1] = 0x05; /* fixed disk */
246 setword(&dataptr[2], 0x01); /* non removable */
247 setword(&dataptr[4], 0x300); /* # of cylinders */
249 else
251 dataptr[1] = 0x07; /* block dev, floppy */
252 setword(&dataptr[2], 0x02); /* removable */
253 setword(&dataptr[4], 80); /* # of cylinders */
255 CreateBPB(drive, &dataptr[7]);
256 RESET_CFLAG(context);
257 return;
258 default:
259 INT_BARF( context, 0x21 );
263 static void GetSystemDate(struct sigcontext_struct *context)
265 struct tm *now;
266 time_t ltime;
268 ltime = time(NULL);
269 now = localtime(&ltime);
271 CX_reg(context) = now->tm_year + 1900;
272 DX_reg(context) = ((now->tm_mon + 1) << 8) | now->tm_mday;
273 AX_reg(context) = now->tm_wday;
276 static void INT21_GetSystemTime(struct sigcontext_struct *context)
278 struct tm *now;
279 struct timeval tv;
280 time_t seconds;
282 gettimeofday(&tv,NULL); /* Note use of gettimeofday(), instead of time() */
283 seconds = tv.tv_sec;
284 now = localtime(&seconds);
286 CX_reg(context) = (now->tm_hour<<8) | now->tm_min;
287 DX_reg(context) = (now->tm_sec<<8) | tv.tv_usec/10000;
288 /* Note hundredths of seconds */
291 static void CreateFile(struct sigcontext_struct *context)
293 AX_reg(context) = _lcreat( PTR_SEG_OFF_TO_LIN( DS_reg(context),
294 DX_reg(context) ), CX_reg(context) );
295 if (AX_reg(context) == (WORD)HFILE_ERROR)
297 AX_reg(context) = DOS_ExtendedError;
298 SET_CFLAG(context);
303 void OpenExistingFile(struct sigcontext_struct *context)
305 AX_reg(context) = _lopen( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
306 AL_reg(context) );
307 if (AX_reg(context) == (WORD)HFILE_ERROR)
309 AX_reg(context) = DOS_ExtendedError;
310 SET_CFLAG(context);
312 #if 0
313 int handle;
314 int mode;
315 int lock;
317 dprintf_int (stddeb, "int21: open (%s, %d) = %d\n",
318 DOS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),
319 DX_reg(context))), mode, handle);
321 switch (AX_reg(context) & 0x0070)
323 case 0x00: /* compatability mode */
324 case 0x40: /* DENYNONE */
325 lock = -1;
326 break;
328 case 0x30: /* DENYREAD */
329 dprintf_int(stddeb,
330 "OpenExistingFile (%s): DENYREAD changed to DENYALL\n",
331 (char *)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)));
332 case 0x10: /* DENYALL */
333 lock = LOCK_EX;
334 break;
336 case 0x20: /* DENYWRITE */
337 lock = LOCK_SH;
338 break;
340 default:
341 lock = -1;
344 if (lock != -1)
347 int result,retries=sharing_retries;
349 #if defined(__svr4__) || defined(_SCO_DS)
350 printf("Should call flock and needs porting to lockf\n");
351 result = 0;
352 retries = 0;
353 #else
354 result = flock(handle, lock | LOCK_NB);
355 #endif
356 if ( retries && (!result) )
358 int i;
359 for(i=0;i<32768*((int)sharing_pause);i++)
360 result++; /* stop the optimizer */
361 for(i=0;i<32768*((int)sharing_pause);i++)
362 result--;
365 while( (!result) && (!(retries--)) );
367 if(result)
369 errno_to_doserr();
370 AX_reg(context) = ExtendedError;
371 close(handle);
372 SET_CFLAG(context);
373 return;
378 Error (0,0,0);
379 AX_reg(context) = handle;
380 RESET_CFLAG(context);
381 #endif
384 static void CloseFile(struct sigcontext_struct *context)
386 if ((AX_reg(context) = _lclose( BX_reg(context) )) != 0)
388 AX_reg(context) = DOS_ExtendedError;
389 SET_CFLAG(context);
393 void ExtendedOpenCreateFile(struct sigcontext_struct *context)
395 BYTE action=DL_reg(context);
396 dprintf_int(stddeb, "int21: extended open/create: file= %s \n",
397 DOSFS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),SI_reg(context)),FALSE));
398 /* Shuffle arguments to call OpenExistingFile */
399 AL_reg(context) = BL_reg(context);
400 DX_reg(context) = SI_reg(context);
401 /* BX,CX and DX should be preserved */
402 OpenExistingFile(context);
403 if ((EFL_reg(context) & 0x0001)==0)
404 { /* It exists */
405 dprintf_int(stddeb, "int21: extended open/create %s exists \n",
406 DOSFS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),SI_reg(context)),TRUE));
407 /* Now decide what do do */
408 if ((action & 0x07)== 0)
410 BX_reg(context) = AX_reg(context);
411 CloseFile(context);
412 AX_reg(context) = 0x0050;/*File exists*/
413 CX_reg(context) = 0;
414 SET_CFLAG(context);
415 dprintf_int(stddeb, "int21: extended open/create: failed because file exixts \n");
416 return;
418 if ((action & 0x07)== 2) {
419 /* Truncate it, but first check if opend for write */
420 if ((BL_reg(context) & 0x0007)== 0) {
421 BX_reg(context) = AX_reg(context);
422 CloseFile(context);
423 dprintf_int(stddeb, "int21: extended open/create: failed, trunc on ro file");
424 AX_reg(context) = 0x000C;/*Access code invalid*/
425 CX_reg(context) = 0;
426 SET_CFLAG(context);
427 return;
429 /* Shuffle arguments to call CloseFile */
430 dprintf_int(stddeb, "int21: extended open/create: Closing before truncate\n");
431 BX_reg(context) = AX_reg(context);
432 /* BX and DX should be preserved */
433 CloseFile(context);
434 if (EFL_reg(context) & 0x0001) {
435 dprintf_int(stddeb, "int21: extended open/create: close before trunc failed");
436 AX_reg(context) = 0x0019;/*Seek Error*/
437 CX_reg(context) = 0;
438 SET_CFLAG(context);
440 /* Shuffle arguments to call CreateFile */
441 dprintf_int(stddeb, "int21: extended open/create: Truncating\n");
442 AL_reg(context) = BL_reg(context);
443 /* CX is still the same */
444 DX_reg(context) = SI_reg(context);
445 CreateFile(context);
446 if (EFL_reg(context) & 0x0001) { /*no file open, flags set */
447 dprintf_int(stddeb, "int21: extended open/create: truncfailed");
448 return;
450 CX_reg(context) = 3;
451 return;
453 CX_reg(context) = 1;
454 return;
456 else /* file does not exist */
458 RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
459 dprintf_int(stddeb, "int21: extended open/create %s dosen't exists \n",
460 DOSFS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),SI_reg(context)),FALSE));
461 if ((action & 0xF0)== 0) {
462 CX_reg(context) = 0;
463 SET_CFLAG(context);
464 dprintf_int(stddeb, "int21: extended open/create: failed, file dosen't exist\n");
465 return;
467 /* Shuffle arguments to call CreateFile */
468 dprintf_int(stddeb, "int21: extended open/create: Creating\n");
469 AL_reg(context) = BL_reg(context);
470 /* CX should still be the same */
471 DX_reg(context) = SI_reg(context);
472 CreateFile(context);
473 if (EFL_reg(context) & 0x0001) { /*no file open, flags set */
474 dprintf_int(stddeb, "int21: extended open/create: create failed\n");
475 return;
477 CX_reg(context) = 2;
478 return;
483 static int INT21_RenameFile(struct sigcontext_struct *context)
485 const char *newname, *oldname;
486 char *buffer;
488 /* FIXME: should not rename over an existing file */
489 dprintf_int(stddeb,"int21: renaming %s to %s\n",
490 (char *)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
491 (char *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context)));
493 oldname = DOSFS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS_reg(context),
494 DX_reg(context)), TRUE );
495 if (!oldname) return 0;
496 buffer = xstrdup( oldname );
497 newname = DOSFS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(ES_reg(context),
498 DI_reg(context)), FALSE );
499 if (!newname)
501 free( buffer );
502 return 0;
505 if (rename( buffer, newname) == -1)
507 FILE_SetDosError();
508 free( buffer );
509 return 0;
511 free( buffer );
512 return 1;
516 static void INT21_ChangeDir(struct sigcontext_struct *context)
518 int drive;
519 char *dirname = PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context));
521 dprintf_int(stddeb,"int21: changedir %s\n", dirname);
522 if (dirname[0] && (dirname[1] == ':'))
524 drive = toupper(dirname[0]) - 'A';
525 dirname += 2;
527 else drive = DRIVE_GetCurrentDrive();
528 if (!DRIVE_Chdir( drive, dirname ))
530 AX_reg(context) = DOS_ExtendedError;
531 SET_CFLAG(context);
536 static int INT21_FindFirst(struct sigcontext_struct *context)
538 const char *path, *unixPath, *mask;
539 char *p;
540 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA();
542 path = (const char *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
543 dta->unixPath = NULL;
544 if (!(unixPath = DOSFS_GetUnixFileName( path, FALSE )))
546 AX_reg(context) = DOS_ExtendedError;
547 SET_CFLAG(context);
548 return 0;
550 dta->unixPath = xstrdup( unixPath );
551 p = strrchr( dta->unixPath, '/' );
552 *p = '\0';
553 if (!(mask = DOSFS_ToDosFCBFormat( p + 1 )))
555 free( dta->unixPath );
556 dta->unixPath = NULL;
557 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
558 AX_reg(context) = ER_FileNotFound;
559 SET_CFLAG(context);
560 return 0;
562 memcpy( dta->mask, mask, sizeof(dta->mask) );
563 dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
564 : DRIVE_GetCurrentDrive();
565 dta->count = 0;
566 dta->search_attr = CL_reg(context);
567 return 1;
571 static int INT21_FindNext(struct sigcontext_struct *context)
573 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA();
574 DOS_DIRENT entry;
575 int count;
577 if (!dta->unixPath) return 0;
578 if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, dta->drive,
579 dta->search_attr, dta->count, &entry )))
581 free( dta->unixPath );
582 dta->unixPath = NULL;
583 return 0;
585 if ((int)dta->count + count > 0xffff)
587 fprintf( stderr, "Too many directory entries in %s\n", dta->unixPath );
588 free( dta->unixPath );
589 dta->unixPath = NULL;
590 return 0;
592 dta->count += count;
593 dta->fileattr = entry.attr;
594 dta->filetime = entry.time;
595 dta->filedate = entry.date;
596 dta->filesize = entry.size;
597 strcpy( dta->filename, DOSFS_ToDosDTAFormat( entry.name ) );
598 return 1;
602 static int INT21_CreateTempFile(struct sigcontext_struct *context)
604 static int counter = 0;
605 char *name = PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context) );
606 char *p = name + strlen(name);
608 for (;;)
610 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
611 counter = (counter + 1) % 1000;
613 if ((AX_reg(context) = _lcreat_uniq( name, 0 )) != (WORD)HFILE_ERROR)
615 dprintf_int( stddeb, "INT21_CreateTempFile: created %s\n", name );
616 return 1;
618 if (DOS_ExtendedError != ER_FileExists) return 0;
623 static int INT21_GetCurrentDirectory(struct sigcontext_struct *context)
625 int drive = DOS_GET_DRIVE( DL_reg(context) );
626 char *ptr = (char *)PTR_SEG_OFF_TO_LIN( DS_reg(context), SI_reg(context) );
628 if (!DRIVE_IsValid(drive))
630 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
631 return 0;
634 lstrcpyn32A( ptr, DRIVE_GetDosCwd(drive), 64 );
635 if (!ptr[0]) strcpy( ptr, "\\" );
636 return 1;
640 static int INT21_GetDiskSerialNumber(struct sigcontext_struct *context)
642 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
643 int drive = DOS_GET_DRIVE( BL_reg(context) );
645 if (!DRIVE_IsValid(drive))
647 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
648 return 0;
651 *(WORD *)dataptr = 0;
652 *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
653 memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
654 strncpy(dataptr + 0x11, "FAT16 ", 8);
655 return 1;
659 static int INT21_SetDiskSerialNumber(struct sigcontext_struct *context)
661 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
662 int drive = DOS_GET_DRIVE( BL_reg(context) );
664 if (!DRIVE_IsValid(drive))
666 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
667 return 0;
670 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
671 return 1;
675 /* microsoft's programmers should be shot for using CP/M style int21
676 calls in Windows for Workgroup's winfile.exe */
678 static int INT21_FindFirstFCB( struct sigcontext_struct *context )
680 BYTE *fcb = (BYTE *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
681 FINDFILE_FCB *pFCB;
682 BYTE attr;
683 char buffer[] = "A:.";
684 const char *unixPath;
686 if (*fcb == 0xff)
688 attr = fcb[6];
689 pFCB = (FINDFILE_FCB *)(fcb + 7);
691 else
693 attr = 0;
694 pFCB = (FINDFILE_FCB *)fcb;
697 buffer[0] += DOS_GET_DRIVE( pFCB->drive );
698 pFCB->unixPath = NULL;
699 if (!(unixPath = DOSFS_GetUnixFileName( buffer, TRUE ))) return 0;
700 pFCB->unixPath = xstrdup( unixPath );
701 pFCB->count = 0;
702 return 1;
706 static int INT21_FindNextFCB( struct sigcontext_struct *context )
708 BYTE *fcb = (BYTE *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
709 FINDFILE_FCB *pFCB;
710 DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA();
711 DOS_DIRENT entry;
712 BYTE attr;
713 int count;
715 if (*fcb == 0xff)
717 attr = fcb[6];
718 pFCB = (FINDFILE_FCB *)(fcb + 7);
720 else
722 attr = 0;
723 pFCB = (FINDFILE_FCB *)fcb;
726 if (!pFCB->unixPath) return 0;
727 if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename,
728 DOS_GET_DRIVE( pFCB->drive ), attr,
729 pFCB->count, &entry )))
731 free( pFCB->unixPath );
732 pFCB->unixPath = NULL;
733 return 0;
735 pFCB->count += count;
737 memcpy( pResult->filename, entry.name, sizeof(pResult->filename) );
738 pResult->fileattr = entry.attr;
739 memset( pResult->reserved, 0, sizeof(pResult->reserved) );
740 pResult->filetime = entry.time;
741 pResult->filedate = entry.date;
742 pResult->cluster = 0; /* what else? */
743 pResult->filesize = entry.size;
744 return 1;
748 static void DeleteFileFCB(struct sigcontext_struct *context)
750 fprintf( stderr, "DeleteFileFCB: not implemented yet\n" );
751 #if 0
752 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
753 struct dosdirent *dp;
754 char temp[256], *ptr;
755 int drive = DOS_GET_DRIVE( *fcb );
757 DumpFCB( fcb );
759 temp[0] = '\\';
760 strcpy(temp+1, DRIVE_GetDosCwd(drive));
761 strcat(temp, "\\");
762 strncat(temp, fcb + 1, 8);
763 ChopOffWhiteSpace(temp);
764 strncat(temp, fcb + 9, 3);
765 ChopOffWhiteSpace(temp);
767 if ((dp = DOS_opendir(temp)) == NULL) {
768 Error(InvalidDrive, EC_MediaError , EL_Disk);
769 AX_reg(context) = 0xff;
770 return;
773 temp[0] = '\\';
774 strcpy(temp+1, DRIVE_GetDosCwd(drive) );
775 strcat(temp, "\\");
777 ptr = temp + strlen(temp);
779 while (DOS_readdir(dp) != NULL)
781 strcpy(ptr, dp->filename);
782 dprintf_int(stddeb, "int21: delete file %s\n", temp);
783 /* unlink(DOS_GetUnixFileName(temp)); */
785 DOS_closedir(dp);
786 AX_reg(context) = 0;
787 #endif
790 static void RenameFileFCB(struct sigcontext_struct *context)
792 fprintf( stderr, "RenameFileFCB: not implemented yet\n" );
793 #if 0
794 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
795 struct dosdirent *dp;
796 char temp[256], oldname[256], newname[256], *oldnameptr, *newnameptr;
797 int drive = DOS_GET_DRIVE( *fcb );
799 DumpFCB( fcb );
801 temp[0] = '\\';
802 strcpy(temp+1, DRIVE_GetDosCwd(drive) );
803 strcat(temp, "\\");
804 strncat(temp, fcb + 1, 8);
805 ChopOffWhiteSpace(temp);
806 strncat(temp, fcb + 9, 3);
807 ChopOffWhiteSpace(temp);
809 if ((dp = DOS_opendir(temp)) == NULL) {
810 Error(InvalidDrive, EC_MediaError , EL_Disk);
811 AX_reg(context) = 0xff;
812 return;
815 oldname[0] = '\\';
816 strcpy(oldname+1, DRIVE_GetDosCwd(drive) );
817 strcat(oldname, "\\");
818 strcpy( newname, oldname );
819 oldnameptr = oldname + strlen(oldname);
820 newnameptr = newname + strlen(newname);
822 while (DOS_readdir(dp) != NULL)
824 strcpy(oldnameptr, dp->filename);
825 strcpy(newnameptr, fcb + 1);
826 dprintf_int(stddeb, "int21: renamefile %s -> %s\n",
827 oldname, newname);
829 DOS_closedir(dp);
830 AX_reg(context) = 0;
831 #endif
836 static void fLock (struct sigcontext_struct * context)
838 #if 0
839 struct flock f;
840 int result,retries=sharing_retries;
842 f.l_start = MAKELONG(DX_reg(context),CX_reg(context));
843 f.l_len = MAKELONG(DI_reg(context),SI_reg(context));
844 f.l_whence = 0;
845 f.l_pid = 0;
847 switch ( AX_reg(context) & 0xff )
849 case 0x00: /* LOCK */
850 f.l_type = F_WRLCK;
851 break;
853 case 0x01: /* UNLOCK */
854 f.l_type = F_UNLCK;
855 break;
857 default:
858 AX_reg(context) = 0x0001;
859 SET_CFLAG(context);
860 return;
864 result = fcntl(BX_reg(context),F_SETLK,&f);
865 if ( retries && (!result) )
867 int i;
868 for(i=0;i<32768*((int)sharing_pause);i++)
869 result++; /* stop the optimizer */
870 for(i=0;i<32768*((int)sharing_pause);i++)
871 result--;
874 while( (!result) && (!(retries--)) );
876 if(result)
878 FILE_SetDosError();
879 AX_reg(context) = DOS_ExtendedError;
880 SET_CFLAG(context);
881 return;
883 #endif
887 static int INT21_GetFileAttribute (struct sigcontext_struct * context)
889 const char *unixName;
891 unixName = DOSFS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)), TRUE );
892 if (!unixName) return 0;
893 if (!FILE_Stat( unixName, &CL_reg(context), NULL, NULL, NULL )) return 0;
894 CH_reg(context) = 0;
895 dprintf_int( stddeb, "INT21_GetFileAttributes(%s) = 0x%x\n",
896 unixName, CX_reg(context) );
897 return 1;
901 extern void LOCAL_PrintHeap (WORD ds);
903 /***********************************************************************
904 * DOS3Call (KERNEL.102)
906 void DOS3Call( struct sigcontext_struct context )
908 dprintf_int( stddeb, "int21: AX=%04x BX=%04x CX=%04x DX=%04x "
909 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
910 AX_reg(&context), BX_reg(&context), CX_reg(&context),
911 DX_reg(&context), SI_reg(&context), DI_reg(&context),
912 DS_reg(&context), ES_reg(&context), EFL_reg(&context));
914 if (AH_reg(&context) == 0x59) /* Get extended error info */
916 AX_reg(&context) = DOS_ExtendedError;
917 BH_reg(&context) = DOS_ErrorClass;
918 BL_reg(&context) = DOS_ErrorAction;
919 CH_reg(&context) = DOS_ErrorLocus;
920 return;
923 DOS_ERROR( 0, 0, 0, 0 );
924 RESET_CFLAG(&context); /* Not sure if this is a good idea */
926 switch(AH_reg(&context))
928 case 0x00: /* TERMINATE PROGRAM */
929 TASK_KillCurrentTask( 0 );
930 break;
932 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
933 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
934 case 0x03: /* READ CHARACTER FROM STDAUX */
935 case 0x04: /* WRITE CHARACTER TO STDAUX */
936 case 0x05: /* WRITE CHARACTER TO PRINTER */
937 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
938 case 0x07: /* DIRECT CHARACTER INPUT, WITHOUT ECHO */
939 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
940 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
941 case 0x0a: /* BUFFERED INPUT */
942 case 0x0b: /* GET STDIN STATUS */
943 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
944 case 0x0f: /* OPEN FILE USING FCB */
945 case 0x10: /* CLOSE FILE USING FCB */
946 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
947 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
948 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
949 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
950 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
951 case 0x23: /* GET FILE SIZE FOR FCB */
952 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
953 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
954 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
955 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
956 case 0x29: /* PARSE FILENAME INTO FCB */
957 case 0x2e: /* SET VERIFY FLAG */
958 case 0x37: /* "SWITCHAR" - GET SWITCH CHARACTER
959 "SWITCHAR" - SET SWITCH CHARACTER
960 "AVAILDEV" - SPECIFY \DEV\ PREFIX USE */
961 case 0x54: /* GET VERIFY FLAG */
962 INT_BARF( &context, 0x21 );
963 break;
965 case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
966 case 0x1d:
967 case 0x1e:
968 case 0x20:
969 case 0x6b: /* NULL FUNCTION */
970 AL_reg(&context) = 0;
971 break;
973 case 0x5c: /* "FLOCK" - RECORD LOCKING */
974 fLock(&context);
975 break;
977 case 0x0d: /* DISK BUFFER FLUSH */
978 RESET_CFLAG(&context); /* dos 6+ only */
979 break;
981 case 0x0e: /* SELECT DEFAULT DRIVE */
982 DRIVE_SetCurrentDrive( DL_reg(&context) );
983 AL_reg(&context) = MAX_DOS_DRIVES;
984 break;
986 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
987 if (!INT21_FindFirstFCB(&context))
989 AL_reg(&context) = 0xff;
990 break;
992 /* else fall through */
994 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
995 AL_reg(&context) = INT21_FindNextFCB(&context) ? 0x00 : 0xff;
996 break;
998 case 0x13: /* DELETE FILE USING FCB */
999 DeleteFileFCB(&context);
1000 break;
1002 case 0x17: /* RENAME FILE USING FCB */
1003 RenameFileFCB(&context);
1004 break;
1006 case 0x19: /* GET CURRENT DEFAULT DRIVE */
1007 AL_reg(&context) = DRIVE_GetCurrentDrive();
1008 break;
1010 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1012 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1013 pTask->dta = PTR_SEG_OFF_TO_SEGPTR(DS_reg(&context),DX_reg(&context));
1014 dprintf_int(stddeb, "int21: Set DTA: %08lx\n", pTask->dta);
1016 break;
1018 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1019 DL_reg(&context) = 0;
1020 if (!INT21_GetDriveAllocInfo(&context)) AX_reg(&context) = 0xffff;
1021 break;
1023 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1024 if (!INT21_GetDriveAllocInfo(&context)) AX_reg(&context) = 0xffff;
1025 break;
1027 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1028 GetDrivePB(&context, DRIVE_GetCurrentDrive());
1029 break;
1031 case 0x25: /* SET INTERRUPT VECTOR */
1032 INT_SetHandler( AL_reg(&context),
1033 PTR_SEG_OFF_TO_SEGPTR( DS_reg(&context), DX_reg(&context)));
1034 break;
1036 case 0x2a: /* GET SYSTEM DATE */
1037 GetSystemDate(&context);
1038 break;
1040 case 0x2b: /* SET SYSTEM DATE */
1041 fprintf( stdnimp, "SetSystemDate(%02d/%02d/%04d): not allowed\n",
1042 DL_reg(&context), DH_reg(&context), CX_reg(&context) );
1043 AL_reg(&context) = 0; /* Let's pretend we succeeded */
1044 break;
1046 case 0x2c: /* GET SYSTEM TIME */
1047 INT21_GetSystemTime(&context);
1048 break;
1050 case 0x2d: /* SET SYSTEM TIME */
1051 fprintf( stdnimp, "SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1052 CH_reg(&context), CL_reg(&context),
1053 DH_reg(&context), DL_reg(&context) );
1054 AL_reg(&context) = 0; /* Let's pretend we succeeded */
1055 break;
1057 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1059 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1060 ES_reg(&context) = SELECTOROF( pTask->dta );
1061 BX_reg(&context) = OFFSETOF( pTask->dta );
1063 break;
1065 case 0x30: /* GET DOS VERSION */
1066 AX_reg(&context) = DOSVERSION;
1067 BX_reg(&context) = 0x0012; /* 0x123456 is Wine's serial # */
1068 CX_reg(&context) = 0x3456;
1069 break;
1071 case 0x31: /* TERMINATE AND STAY RESIDENT */
1072 INT_BARF( &context, 0x21 );
1073 break;
1075 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1076 GetDrivePB(&context, DOS_GET_DRIVE( DL_reg(&context) ) );
1077 break;
1079 case 0x33: /* MULTIPLEXED */
1080 switch (AL_reg(&context))
1082 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1083 DL_reg(&context) = 0;
1084 break;
1086 case 0x01: /* SET EXTENDED BREAK STATE */
1087 break;
1089 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1090 DL_reg(&context) = 0;
1091 break;
1093 case 0x05: /* GET BOOT DRIVE */
1094 DL_reg(&context) = 2;
1095 /* c: is Wine's bootdrive */
1096 break;
1098 case 0x06: /* GET TRUE VERSION NUMBER */
1099 BX_reg(&context) = DOSVERSION;
1100 DX_reg(&context) = 0x00;
1101 break;
1103 default:
1104 INT_BARF( &context, 0x21 );
1105 break;
1107 break;
1109 case 0x34: /* GET ADDRESS OF INDOS FLAG */
1110 ES_reg(&context) = DosHeapHandle;
1111 BX_reg(&context) = (int)&heap->InDosFlag - (int)heap;
1112 break;
1114 case 0x35: /* GET INTERRUPT VECTOR */
1116 SEGPTR addr = INT_GetHandler( AL_reg(&context) );
1117 ES_reg(&context) = SELECTOROF(addr);
1118 BX_reg(&context) = OFFSETOF(addr);
1120 break;
1122 case 0x36: /* GET FREE DISK SPACE */
1123 if (!INT21_GetFreeDiskSpace(&context)) AX_reg(&context) = 0xffff;
1124 break;
1126 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1127 AX_reg(&context) = 0x02; /* no country support available */
1128 SET_CFLAG(&context);
1129 break;
1131 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1132 if (!FILE_MakeDir( PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1133 DX_reg(&context) )))
1135 AX_reg(&context) = DOS_ExtendedError;
1136 SET_CFLAG(&context);
1138 break;
1140 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1141 if (!FILE_RemoveDir( PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1142 DX_reg(&context) )))
1144 AX_reg(&context) = DOS_ExtendedError;
1145 SET_CFLAG(&context);
1147 break;
1149 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1150 INT21_ChangeDir(&context);
1151 break;
1153 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1154 AX_reg(&context) = _lcreat( PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1155 DX_reg(&context) ), CX_reg(&context) );
1156 if (AX_reg(&context) == (WORD)HFILE_ERROR)
1158 AX_reg(&context) = DOS_ExtendedError;
1159 SET_CFLAG(&context);
1161 break;
1163 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1164 OpenExistingFile(&context);
1165 break;
1167 case 0x3e: /* "CLOSE" - CLOSE FILE */
1168 if ((AX_reg(&context) = _lclose( BX_reg(&context) )) != 0)
1170 AX_reg(&context) = DOS_ExtendedError;
1171 SET_CFLAG(&context);
1173 break;
1175 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1177 LONG result = _hread( BX_reg(&context),
1178 PTR_SEG_OFF_TO_SEGPTR( DS_reg(&context),
1179 DX_reg(&context) ),
1180 CX_reg(&context) );
1181 if (result == -1)
1183 AX_reg(&context) = DOS_ExtendedError;
1184 SET_CFLAG(&context);
1186 else AX_reg(&context) = (WORD)result;
1188 break;
1190 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1192 LONG result = _hwrite( BX_reg(&context),
1193 PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1194 DX_reg(&context) ),
1195 CX_reg(&context) );
1196 if (result == -1)
1198 AX_reg(&context) = DOS_ExtendedError;
1199 SET_CFLAG(&context);
1201 else AX_reg(&context) = (WORD)result;
1203 break;
1205 case 0x41: /* "UNLINK" - DELETE FILE */
1206 if (!FILE_Unlink( PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1207 DX_reg(&context) )))
1209 AX_reg(&context) = DOS_ExtendedError;
1210 SET_CFLAG(&context);
1212 break;
1214 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1216 LONG status = _llseek( BX_reg(&context),
1217 MAKELONG(DX_reg(&context),CX_reg(&context)),
1218 AL_reg(&context) );
1219 if (status == HFILE_ERROR)
1221 AX_reg(&context) = DOS_ExtendedError;
1222 SET_CFLAG(&context);
1223 break;
1225 AX_reg(&context) = LOWORD(status);
1226 DX_reg(&context) = HIWORD(status);
1228 break;
1230 case 0x43: /* FILE ATTRIBUTES */
1231 switch (AL_reg(&context))
1233 case 0x00:
1234 if (!INT21_GetFileAttribute(&context))
1236 AX_reg(&context) = DOS_ExtendedError;
1237 SET_CFLAG(&context);
1239 break;
1240 case 0x01:
1241 RESET_CFLAG(&context);
1242 break;
1244 break;
1246 case 0x44: /* IOCTL */
1247 switch (AL_reg(&context))
1249 case 0x00:
1250 ioctlGetDeviceInfo(&context);
1251 break;
1253 case 0x01:
1254 break;
1256 case 0x08: /* Check if drive is removable. */
1257 switch(GetDriveType( DOS_GET_DRIVE( BL_reg(&context) )))
1259 case DRIVE_CANNOTDETERMINE:
1260 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
1261 AX_reg(&context) = ER_InvalidDrive;
1262 SET_CFLAG(&context);
1263 break;
1264 case DRIVE_REMOVABLE:
1265 AX_reg(&context) = 0; /* removable */
1266 break;
1267 default:
1268 AX_reg(&context) = 1; /* not removable */
1269 break;
1271 break;
1273 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
1274 switch(GetDriveType( DOS_GET_DRIVE( BL_reg(&context) )))
1276 case DRIVE_CANNOTDETERMINE:
1277 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
1278 AX_reg(&context) = ER_InvalidDrive;
1279 SET_CFLAG(&context);
1280 break;
1281 case DRIVE_REMOTE:
1282 DX_reg(&context) = (1<<9) | (1<<12); /* remote */
1283 break;
1284 default:
1285 DX_reg(&context) = 0; /* FIXME: use driver attr here */
1286 break;
1288 break;
1290 case 0x0a: /* check if handle (BX) is remote */
1291 /* returns DX, bit 15 set if remote, bit 14 set if date/time
1292 * not set on close
1294 DX_reg(&context) = 0;
1295 break;
1297 case 0x0b: /* SET SHARING RETRY COUNT */
1298 if (!CX_reg(&context))
1300 AX_reg(&context) = 1;
1301 SET_CFLAG(&context);
1302 break;
1304 sharing_pause = CX_reg(&context);
1305 if (!DX_reg(&context))
1306 sharing_retries = DX_reg(&context);
1307 RESET_CFLAG(&context);
1308 break;
1310 case 0x0d:
1311 ioctlGenericBlkDevReq(&context);
1312 break;
1314 case 0x0F: /* Set logical drive mapping */
1315 /* FIXME: Not implemented at the moment, always returns error
1317 INT_BARF( &context, 0x21 );
1318 AX_reg(&context) = 0x0001; /* invalid function */
1319 SET_CFLAG(&context);
1320 break;
1322 default:
1323 INT_BARF( &context, 0x21 );
1324 break;
1326 break;
1328 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1329 if ((AX_reg(&context) = FILE_Dup(BX_reg(&context))) == (WORD)HFILE_ERROR)
1331 AX_reg(&context) = DOS_ExtendedError;
1332 SET_CFLAG(&context);
1334 break;
1336 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1337 if (FILE_Dup2( BX_reg(&context), CX_reg(&context) ) == HFILE_ERROR)
1339 AX_reg(&context) = DOS_ExtendedError;
1340 SET_CFLAG(&context);
1342 break;
1344 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1345 if (!INT21_GetCurrentDirectory(&context))
1347 AX_reg(&context) = DOS_ExtendedError;
1348 SET_CFLAG(&context);
1350 else AX_reg(&context) = 0x0100;
1351 /* intlist: many Microsoft products for Windows rely on this */
1352 break;
1354 case 0x48: /* ALLOCATE MEMORY */
1355 case 0x49: /* FREE MEMORY */
1356 case 0x4a: /* RESIZE MEMORY BLOCK */
1357 INT_BARF( &context, 0x21 );
1358 break;
1360 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1361 AX_reg(&context) = WinExec( PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1362 DX_reg(&context) ),
1363 SW_NORMAL );
1364 if (AX_reg(&context) < 32) SET_CFLAG(&context);
1365 break;
1367 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1368 TASK_KillCurrentTask( AL_reg(&context) );
1369 break;
1371 case 0x4d: /* GET RETURN CODE */
1372 AX_reg(&context) = 0; /* normal exit */
1373 break;
1375 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1376 if (!INT21_FindFirst(&context)) break;
1377 /* fall through */
1379 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1380 if (!INT21_FindNext(&context))
1382 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Disk );
1383 AX_reg(&context) = ER_NoMoreFiles;
1384 SET_CFLAG(&context);
1386 break;
1388 case 0x51: /* GET PSP ADDRESS */
1389 case 0x62: /* GET PSP ADDRESS */
1390 /* FIXME: should we return the original DOS PSP upon */
1391 /* Windows startup ? */
1392 BX_reg(&context) = GetCurrentPDB();
1393 break;
1395 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1396 ES_reg(&context) = 0x0;
1397 BX_reg(&context) = 0x0;
1398 INT_BARF( &context, 0x21 );
1399 break;
1401 case 0x56: /* "RENAME" - RENAME FILE */
1402 if (!INT21_RenameFile(&context))
1404 AX_reg(&context) = DOS_ExtendedError;
1405 SET_CFLAG(&context);
1407 break;
1409 case 0x57: /* FILE DATE AND TIME */
1410 switch (AL_reg(&context))
1412 case 0x00: /* Get */
1413 if (!FILE_GetDateTime( BX_reg(&context), &DX_reg(&context),
1414 &CX_reg(&context), TRUE ))
1416 AX_reg(&context) = DOS_ExtendedError;
1417 SET_CFLAG(&context);
1419 break;
1421 case 0x01: /* Set */
1422 if (!FILE_SetDateTime( BX_reg(&context), DX_reg(&context),
1423 CX_reg(&context) ))
1425 AX_reg(&context) = DOS_ExtendedError;
1426 SET_CFLAG(&context);
1428 break;
1430 break;
1432 case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1433 switch (AL_reg(&context))
1435 case 0x00:
1436 AX_reg(&context) = 1;
1437 break;
1438 case 0x02:
1439 AX_reg(&context) = 0;
1440 break;
1441 case 0x01:
1442 case 0x03:
1443 break;
1445 RESET_CFLAG(&context);
1446 break;
1448 case 0x5a: /* CREATE TEMPORARY FILE */
1449 if (!INT21_CreateTempFile(&context))
1451 AX_reg(&context) = DOS_ExtendedError;
1452 SET_CFLAG(&context);
1454 break;
1456 case 0x5b: /* CREATE NEW FILE */
1457 if ((AX_reg(&context) = _lcreat_uniq( PTR_SEG_OFF_TO_LIN(DS_reg(&context),DX_reg(&context)), 0 )) == (WORD)HFILE_ERROR)
1459 AX_reg(&context) = DOS_ExtendedError;
1460 SET_CFLAG(&context);
1462 break;
1464 case 0x5d: /* NETWORK */
1465 case 0x5e:
1466 /* network software not installed */
1467 DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
1468 AX_reg(&context) = DOS_ExtendedError;
1469 SET_CFLAG(&context);
1470 break;
1472 case 0x5f: /* NETWORK */
1473 switch (AL_reg(&context))
1475 case 0x07: /* ENABLE DRIVE */
1476 if (!DRIVE_Enable( DL_reg(&context) ))
1478 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
1479 AX_reg(&context) = DOS_ExtendedError;
1480 SET_CFLAG(&context);
1482 break;
1484 case 0x08: /* DISABLE DRIVE */
1485 if (!DRIVE_Disable( DL_reg(&context) ))
1487 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
1488 AX_reg(&context) = DOS_ExtendedError;
1489 SET_CFLAG(&context);
1491 break;
1493 default:
1494 /* network software not installed */
1495 DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
1496 AX_reg(&context) = DOS_ExtendedError;
1497 SET_CFLAG(&context);
1498 break;
1500 break;
1502 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1504 const char *truename = DOSFS_GetDosTrueName( PTR_SEG_OFF_TO_LIN(DS_reg(&context),SI_reg(&context) ), FALSE );
1505 if (!truename)
1507 AX_reg(&context) = DOS_ExtendedError;
1508 SET_CFLAG(&context);
1510 else
1512 lstrcpyn32A( PTR_SEG_OFF_TO_LIN( ES_reg(&context),
1513 DI_reg(&context) ),
1514 truename, 128 );
1515 AX_reg(&context) = 0;
1518 break;
1520 case 0x61: /* UNUSED */
1521 case 0x63: /* UNUSED */
1522 case 0x64: /* OS/2 DOS BOX */
1523 case 0x65: /* GET EXTENDED COUNTRY INFORMATION */
1524 INT_BARF( &context, 0x21 );
1525 SET_CFLAG(&context);
1526 break;
1528 case 0x66: /* GLOBAL CODE PAGE TABLE */
1529 switch (AL_reg(&context))
1531 case 0x01:
1532 DX_reg(&context) = BX_reg(&context) = CodePage;
1533 RESET_CFLAG(&context);
1534 break;
1535 case 0x02:
1536 CodePage = BX_reg(&context);
1537 RESET_CFLAG(&context);
1538 break;
1540 break;
1542 case 0x67: /* SET HANDLE COUNT */
1543 SetHandleCount( BX_reg(&context) );
1544 if (DOS_ExtendedError)
1546 AX_reg(&context) = DOS_ExtendedError;
1547 SET_CFLAG(&context);
1549 break;
1551 case 0x68: /* "FFLUSH" - COMMIT FILE */
1552 case 0x6a: /* COMMIT FILE */
1553 if (!FILE_Sync( BX_reg(&context) ))
1555 AX_reg(&context) = DOS_ExtendedError;
1556 SET_CFLAG(&context);
1558 break;
1560 case 0x69: /* DISK SERIAL NUMBER */
1561 switch (AL_reg(&context))
1563 case 0x00:
1564 if (!INT21_GetDiskSerialNumber(&context))
1566 AX_reg(&context) = DOS_ExtendedError;
1567 SET_CFLAG(&context);
1569 else AX_reg(&context) = 0;
1570 break;
1571 case 0x01:
1572 if (!INT21_SetDiskSerialNumber(&context))
1574 AX_reg(&context) = DOS_ExtendedError;
1575 SET_CFLAG(&context);
1577 else AX_reg(&context) = 1;
1578 break;
1580 break;
1582 case 0x6C: /* Extended Open/Create*/
1583 ExtendedOpenCreateFile(&context);
1584 break;
1586 case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
1587 case 0x71: /* MS-DOS 7 (Chicago) - LONG FILENAME FUNCTIONS */
1588 case 0x72: /* MS-DOS 7 (Windows95) - ??? */
1589 case 0x73: /* MS-DOS 7 (Windows95) - DRIVE LOCKING ??? */
1590 dprintf_int(stddeb,"int21: windows95 function AX %04x\n",
1591 AX_reg(&context));
1592 dprintf_int(stddeb, " returning unimplemented\n");
1593 SET_CFLAG(&context);
1594 AL_reg(&context) = 0;
1595 break;
1597 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
1598 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
1599 break;
1601 default:
1602 INT_BARF( &context, 0x21 );
1603 break;
1605 dprintf_int( stddeb, "ret21: AX=%04x BX=%04x CX=%04x DX=%04x "
1606 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1607 AX_reg(&context), BX_reg(&context), CX_reg(&context),
1608 DX_reg(&context), SI_reg(&context), DI_reg(&context),
1609 DS_reg(&context), ES_reg(&context), EFL_reg(&context));
1613 BOOL INT21_Init(void)
1615 if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
1617 fprintf( stderr, "INT21_Init: Out of memory\n");
1618 return FALSE;
1620 heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
1622 dpb = &heap->dpb;
1623 dpbsegptr = PTR_SEG_OFF_TO_SEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
1624 heap->InDosFlag = 0;
1625 strcpy(heap->biosdate, "01/01/80");
1626 return TRUE;