Release 960705
[wine/multimedia.git] / miscemu / int21.c
blobd301ca62c9ec726bb7dc09e2ef7797ddce52a337
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( SIGCONTEXT *context )
139 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
140 char root[] = "A:\\";
142 *root += DOS_GET_DRIVE( DL_reg(context) );
143 if (!GetDiskFreeSpace32A( root, &cluster_sectors, &sector_bytes,
144 &free_clusters, &total_clusters )) return 0;
145 AX_reg(context) = cluster_sectors;
146 BX_reg(context) = free_clusters;
147 CX_reg(context) = sector_bytes;
148 DX_reg(context) = total_clusters;
149 return 1;
152 static int INT21_GetDriveAllocInfo( SIGCONTEXT *context )
154 if (!INT21_GetFreeDiskSpace( context )) return 0;
155 heap->mediaID = 0xf0;
156 DS_reg(context) = DosHeapHandle;
157 BX_reg(context) = (int)&heap->mediaID - (int)heap;
158 return 1;
161 static void GetDrivePB( SIGCONTEXT *context, int drive )
163 if(!DRIVE_IsValid(drive))
165 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
166 AX_reg(context) = 0x00ff;
168 else
170 dprintf_int(stddeb, "int21: GetDrivePB not fully implemented.\n");
172 /* FIXME: I have no idea what a lot of this information should
173 * say or whether it even really matters since we're not allowing
174 * direct block access. However, some programs seem to depend on
175 * getting at least _something_ back from here. The 'next' pointer
176 * does worry me, though. Should we have a complete table of
177 * separate DPBs per drive? Probably, but I'm lazy. :-) -CH
179 dpb->drive_num = dpb->unit_num = drive; /* The same? */
180 dpb->sector_size = 512;
181 dpb->high_sector = 1;
182 dpb->shift = drive < 2 ? 0 : 6; /* 6 for HD, 0 for floppy */
183 dpb->reserved = 0;
184 dpb->num_FAT = 1;
185 dpb->dir_entries = 2;
186 dpb->first_data = 2;
187 dpb->high_cluster = 64000;
188 dpb->sectors_in_FAT = 1;
189 dpb->start_dir = 1;
190 dpb->driver_head = 0;
191 dpb->media_ID = (drive > 1) ? 0xF8 : 0xF0;
192 dpb->access_flag = 0;
193 dpb->next = 0;
194 dpb->free_search = 0;
195 dpb->free_clusters = 0xFFFF; /* unknown */
197 AL_reg(context) = 0x00;
198 DS_reg(context) = SELECTOROF(dpbsegptr);
199 BX_reg(context) = OFFSETOF(dpbsegptr);
204 static void ioctlGetDeviceInfo( SIGCONTEXT *context )
206 dprintf_int (stddeb, "int21: ioctl (%d, GetDeviceInfo)\n", BX_reg(context));
208 DX_reg(context) = 0x0942;
209 /* bits 0-5 are current drive
210 * bit 6 - file has NOT been written..FIXME: correct?
211 * bit 8 - generate int24 if no diskspace on write/ read past end of file
212 * bit 11 - media not removable
214 RESET_CFLAG(context);
217 static void ioctlGenericBlkDevReq( SIGCONTEXT *context )
219 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
220 int drive = DOS_GET_DRIVE( BL_reg(context) );
222 if (!DRIVE_IsValid(drive))
224 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
225 AX_reg(context) = DOS_ExtendedError;
226 SET_CFLAG(context);
227 return;
230 if (CH_reg(context) != 0x08)
232 INT_BARF( context, 0x21 );
233 return;
235 switch (CL_reg(context)) {
236 case 0x60: /* get device parameters */
237 /* used by w4wgrp's winfile */
238 memset(dataptr, 0, 0x26);
239 dataptr[0] = 0x04;
240 dataptr[6] = 0; /* media type */
241 if (drive > 1)
243 dataptr[1] = 0x05; /* fixed disk */
244 setword(&dataptr[2], 0x01); /* non removable */
245 setword(&dataptr[4], 0x300); /* # of cylinders */
247 else
249 dataptr[1] = 0x07; /* block dev, floppy */
250 setword(&dataptr[2], 0x02); /* removable */
251 setword(&dataptr[4], 80); /* # of cylinders */
253 CreateBPB(drive, &dataptr[7]);
254 RESET_CFLAG(context);
255 return;
256 default:
257 INT_BARF( context, 0x21 );
261 static void GetSystemDate( SIGCONTEXT *context )
263 struct tm *now;
264 time_t ltime;
266 ltime = time(NULL);
267 now = localtime(&ltime);
269 CX_reg(context) = now->tm_year + 1900;
270 DX_reg(context) = ((now->tm_mon + 1) << 8) | now->tm_mday;
271 AX_reg(context) = now->tm_wday;
274 static void INT21_GetSystemTime( SIGCONTEXT *context )
276 struct tm *now;
277 struct timeval tv;
278 time_t seconds;
280 gettimeofday(&tv,NULL); /* Note use of gettimeofday(), instead of time() */
281 seconds = tv.tv_sec;
282 now = localtime(&seconds);
284 CX_reg(context) = (now->tm_hour<<8) | now->tm_min;
285 DX_reg(context) = (now->tm_sec<<8) | tv.tv_usec/10000;
286 /* Note hundredths of seconds */
289 static void CreateFile( SIGCONTEXT *context )
291 AX_reg(context) = _lcreat( PTR_SEG_OFF_TO_LIN( DS_reg(context),
292 DX_reg(context) ), CX_reg(context) );
293 if (AX_reg(context) == (WORD)HFILE_ERROR)
295 AX_reg(context) = DOS_ExtendedError;
296 SET_CFLAG(context);
301 void OpenExistingFile( SIGCONTEXT *context )
303 AX_reg(context) = _lopen( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
304 AL_reg(context) );
305 if (AX_reg(context) == (WORD)HFILE_ERROR)
307 AX_reg(context) = DOS_ExtendedError;
308 SET_CFLAG(context);
310 #if 0
311 int handle;
312 int mode;
313 int lock;
315 dprintf_int (stddeb, "int21: open (%s, %d) = %d\n",
316 DOS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),
317 DX_reg(context))), mode, handle);
319 switch (AX_reg(context) & 0x0070)
321 case 0x00: /* compatability mode */
322 case 0x40: /* DENYNONE */
323 lock = -1;
324 break;
326 case 0x30: /* DENYREAD */
327 dprintf_int(stddeb,
328 "OpenExistingFile (%s): DENYREAD changed to DENYALL\n",
329 (char *)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)));
330 case 0x10: /* DENYALL */
331 lock = LOCK_EX;
332 break;
334 case 0x20: /* DENYWRITE */
335 lock = LOCK_SH;
336 break;
338 default:
339 lock = -1;
342 if (lock != -1)
345 int result,retries=sharing_retries;
347 #if defined(__svr4__) || defined(_SCO_DS)
348 printf("Should call flock and needs porting to lockf\n");
349 result = 0;
350 retries = 0;
351 #else
352 result = flock(handle, lock | LOCK_NB);
353 #endif
354 if ( retries && (!result) )
356 int i;
357 for(i=0;i<32768*((int)sharing_pause);i++)
358 result++; /* stop the optimizer */
359 for(i=0;i<32768*((int)sharing_pause);i++)
360 result--;
363 while( (!result) && (!(retries--)) );
365 if(result)
367 errno_to_doserr();
368 AX_reg(context) = ExtendedError;
369 close(handle);
370 SET_CFLAG(context);
371 return;
376 Error (0,0,0);
377 AX_reg(context) = handle;
378 RESET_CFLAG(context);
379 #endif
382 static void CloseFile( SIGCONTEXT *context )
384 if ((AX_reg(context) = _lclose( BX_reg(context) )) != 0)
386 AX_reg(context) = DOS_ExtendedError;
387 SET_CFLAG(context);
391 void ExtendedOpenCreateFile(SIGCONTEXT *context )
393 BYTE action=DL_reg(context);
394 dprintf_int(stddeb, "int21: extended open/create: file= %s \n",
395 DOSFS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),SI_reg(context)),FALSE));
396 /* Shuffle arguments to call OpenExistingFile */
397 AL_reg(context) = BL_reg(context);
398 DX_reg(context) = SI_reg(context);
399 /* BX,CX and DX should be preserved */
400 OpenExistingFile(context);
401 if ((EFL_reg(context) & 0x0001)==0)
402 { /* It exists */
403 dprintf_int(stddeb, "int21: extended open/create %s exists \n",
404 DOSFS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),SI_reg(context)),TRUE));
405 /* Now decide what do do */
406 if ((action & 0x07)== 0)
408 BX_reg(context) = AX_reg(context);
409 CloseFile(context);
410 AX_reg(context) = 0x0050;/*File exists*/
411 CX_reg(context) = 0;
412 SET_CFLAG(context);
413 dprintf_int(stddeb, "int21: extended open/create: failed because file exixts \n");
414 return;
416 if ((action & 0x07)== 2) {
417 /* Truncate it, but first check if opend for write */
418 if ((BL_reg(context) & 0x0007)== 0) {
419 BX_reg(context) = AX_reg(context);
420 CloseFile(context);
421 dprintf_int(stddeb, "int21: extended open/create: failed, trunc on ro file");
422 AX_reg(context) = 0x000C;/*Access code invalid*/
423 CX_reg(context) = 0;
424 SET_CFLAG(context);
425 return;
427 /* Shuffle arguments to call CloseFile */
428 dprintf_int(stddeb, "int21: extended open/create: Closing before truncate\n");
429 BX_reg(context) = AX_reg(context);
430 /* BX and DX should be preserved */
431 CloseFile(context);
432 if (EFL_reg(context) & 0x0001) {
433 dprintf_int(stddeb, "int21: extended open/create: close before trunc failed");
434 AX_reg(context) = 0x0019;/*Seek Error*/
435 CX_reg(context) = 0;
436 SET_CFLAG(context);
438 /* Shuffle arguments to call CreateFile */
439 dprintf_int(stddeb, "int21: extended open/create: Truncating\n");
440 AL_reg(context) = BL_reg(context);
441 /* CX is still the same */
442 DX_reg(context) = SI_reg(context);
443 CreateFile(context);
444 if (EFL_reg(context) & 0x0001) { /*no file open, flags set */
445 dprintf_int(stddeb, "int21: extended open/create: truncfailed");
446 return;
448 CX_reg(context) = 3;
449 return;
451 CX_reg(context) = 1;
452 return;
454 else /* file does not exist */
456 RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
457 dprintf_int(stddeb, "int21: extended open/create %s dosen't exists \n",
458 DOSFS_GetUnixFileName(PTR_SEG_OFF_TO_LIN(DS_reg(context),SI_reg(context)),FALSE));
459 if ((action & 0xF0)== 0) {
460 CX_reg(context) = 0;
461 SET_CFLAG(context);
462 dprintf_int(stddeb, "int21: extended open/create: failed, file dosen't exist\n");
463 return;
465 /* Shuffle arguments to call CreateFile */
466 dprintf_int(stddeb, "int21: extended open/create: Creating\n");
467 AL_reg(context) = BL_reg(context);
468 /* CX should still be the same */
469 DX_reg(context) = SI_reg(context);
470 CreateFile(context);
471 if (EFL_reg(context) & 0x0001) { /*no file open, flags set */
472 dprintf_int(stddeb, "int21: extended open/create: create failed\n");
473 return;
475 CX_reg(context) = 2;
476 return;
481 static int INT21_RenameFile( SIGCONTEXT *context )
483 const char *newname, *oldname;
484 char *buffer;
486 /* FIXME: should not rename over an existing file */
487 dprintf_int(stddeb,"int21: renaming %s to %s\n",
488 (char *)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
489 (char *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context)));
491 oldname = DOSFS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS_reg(context),
492 DX_reg(context)), TRUE );
493 if (!oldname) return 0;
494 buffer = xstrdup( oldname );
495 newname = DOSFS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(ES_reg(context),
496 DI_reg(context)), FALSE );
497 if (!newname)
499 free( buffer );
500 return 0;
503 if (rename( buffer, newname) == -1)
505 FILE_SetDosError();
506 free( buffer );
507 return 0;
509 free( buffer );
510 return 1;
514 static void INT21_ChangeDir( SIGCONTEXT *context )
516 int drive;
517 char *dirname = PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context));
519 dprintf_int(stddeb,"int21: changedir %s\n", dirname);
520 if (dirname[0] && (dirname[1] == ':'))
522 drive = toupper(dirname[0]) - 'A';
523 dirname += 2;
525 else drive = DRIVE_GetCurrentDrive();
526 if (!DRIVE_Chdir( drive, dirname ))
528 AX_reg(context) = DOS_ExtendedError;
529 SET_CFLAG(context);
534 static int INT21_FindFirst( SIGCONTEXT *context )
536 const char *path, *unixPath, *mask;
537 char *p;
538 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA();
540 path = (const char *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
541 dta->unixPath = NULL;
542 if (!(unixPath = DOSFS_GetUnixFileName( path, FALSE )))
544 AX_reg(context) = DOS_ExtendedError;
545 SET_CFLAG(context);
546 return 0;
548 dta->unixPath = xstrdup( unixPath );
549 p = strrchr( dta->unixPath, '/' );
550 *p = '\0';
551 if (!(mask = DOSFS_ToDosFCBFormat( p + 1 )))
553 free( dta->unixPath );
554 dta->unixPath = NULL;
555 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
556 AX_reg(context) = ER_FileNotFound;
557 SET_CFLAG(context);
558 return 0;
560 memcpy( dta->mask, mask, sizeof(dta->mask) );
561 dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
562 : DRIVE_GetCurrentDrive();
563 dta->count = 0;
564 dta->search_attr = CL_reg(context);
565 return 1;
569 static int INT21_FindNext( SIGCONTEXT *context )
571 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA();
572 DOS_DIRENT entry;
573 int count;
575 if (!dta->unixPath) return 0;
576 if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, dta->drive,
577 dta->search_attr, dta->count, &entry )))
579 free( dta->unixPath );
580 dta->unixPath = NULL;
581 return 0;
583 if ((int)dta->count + count > 0xffff)
585 fprintf( stderr, "Too many directory entries in %s\n", dta->unixPath );
586 free( dta->unixPath );
587 dta->unixPath = NULL;
588 return 0;
590 dta->count += count;
591 dta->fileattr = entry.attr;
592 dta->filetime = entry.time;
593 dta->filedate = entry.date;
594 dta->filesize = entry.size;
595 strcpy( dta->filename, DOSFS_ToDosDTAFormat( entry.name ) );
596 return 1;
600 static int INT21_CreateTempFile( SIGCONTEXT *context )
602 static int counter = 0;
603 char *name = PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context) );
604 char *p = name + strlen(name);
606 for (;;)
608 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
609 counter = (counter + 1) % 1000;
611 if ((AX_reg(context) = _lcreat_uniq( name, 0 )) != (WORD)HFILE_ERROR)
613 dprintf_int( stddeb, "INT21_CreateTempFile: created %s\n", name );
614 return 1;
616 if (DOS_ExtendedError != ER_FileExists) return 0;
621 static int INT21_GetCurrentDirectory( SIGCONTEXT *context )
623 int drive = DOS_GET_DRIVE( DL_reg(context) );
624 char *ptr = (char *)PTR_SEG_OFF_TO_LIN( DS_reg(context), SI_reg(context) );
626 if (!DRIVE_IsValid(drive))
628 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
629 return 0;
632 lstrcpyn32A( ptr, DRIVE_GetDosCwd(drive), 64 );
633 if (!ptr[0]) strcpy( ptr, "\\" );
634 return 1;
638 static int INT21_GetDiskSerialNumber( SIGCONTEXT *context )
640 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
641 int drive = DOS_GET_DRIVE( BL_reg(context) );
643 if (!DRIVE_IsValid(drive))
645 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
646 return 0;
649 *(WORD *)dataptr = 0;
650 *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
651 memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
652 strncpy(dataptr + 0x11, "FAT16 ", 8);
653 return 1;
657 static int INT21_SetDiskSerialNumber( SIGCONTEXT *context )
659 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
660 int drive = DOS_GET_DRIVE( BL_reg(context) );
662 if (!DRIVE_IsValid(drive))
664 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
665 return 0;
668 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
669 return 1;
673 /* microsoft's programmers should be shot for using CP/M style int21
674 calls in Windows for Workgroup's winfile.exe */
676 static int INT21_FindFirstFCB( SIGCONTEXT *context )
678 BYTE *fcb = (BYTE *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
679 FINDFILE_FCB *pFCB;
680 BYTE attr;
681 char buffer[] = "A:.";
682 const char *unixPath;
684 if (*fcb == 0xff)
686 attr = fcb[6];
687 pFCB = (FINDFILE_FCB *)(fcb + 7);
689 else
691 attr = 0;
692 pFCB = (FINDFILE_FCB *)fcb;
695 buffer[0] += DOS_GET_DRIVE( pFCB->drive );
696 pFCB->unixPath = NULL;
697 if (!(unixPath = DOSFS_GetUnixFileName( buffer, TRUE ))) return 0;
698 pFCB->unixPath = xstrdup( unixPath );
699 pFCB->count = 0;
700 return 1;
704 static int INT21_FindNextFCB( SIGCONTEXT *context )
706 BYTE *fcb = (BYTE *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
707 FINDFILE_FCB *pFCB;
708 DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA();
709 DOS_DIRENT entry;
710 BYTE attr;
711 int count;
713 if (*fcb == 0xff)
715 attr = fcb[6];
716 pFCB = (FINDFILE_FCB *)(fcb + 7);
718 else
720 attr = 0;
721 pFCB = (FINDFILE_FCB *)fcb;
724 if (!pFCB->unixPath) return 0;
725 if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename,
726 DOS_GET_DRIVE( pFCB->drive ), attr,
727 pFCB->count, &entry )))
729 free( pFCB->unixPath );
730 pFCB->unixPath = NULL;
731 return 0;
733 pFCB->count += count;
735 memcpy( pResult->filename, entry.name, sizeof(pResult->filename) );
736 pResult->fileattr = entry.attr;
737 memset( pResult->reserved, 0, sizeof(pResult->reserved) );
738 pResult->filetime = entry.time;
739 pResult->filedate = entry.date;
740 pResult->cluster = 0; /* what else? */
741 pResult->filesize = entry.size;
742 return 1;
746 static void DeleteFileFCB( SIGCONTEXT *context )
748 fprintf( stderr, "DeleteFileFCB: not implemented yet\n" );
749 #if 0
750 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
751 struct dosdirent *dp;
752 char temp[256], *ptr;
753 int drive = DOS_GET_DRIVE( *fcb );
755 DumpFCB( fcb );
757 temp[0] = '\\';
758 strcpy(temp+1, DRIVE_GetDosCwd(drive));
759 strcat(temp, "\\");
760 strncat(temp, fcb + 1, 8);
761 ChopOffWhiteSpace(temp);
762 strncat(temp, fcb + 9, 3);
763 ChopOffWhiteSpace(temp);
765 if ((dp = DOS_opendir(temp)) == NULL) {
766 Error(InvalidDrive, EC_MediaError , EL_Disk);
767 AX_reg(context) = 0xff;
768 return;
771 temp[0] = '\\';
772 strcpy(temp+1, DRIVE_GetDosCwd(drive) );
773 strcat(temp, "\\");
775 ptr = temp + strlen(temp);
777 while (DOS_readdir(dp) != NULL)
779 strcpy(ptr, dp->filename);
780 dprintf_int(stddeb, "int21: delete file %s\n", temp);
781 /* unlink(DOS_GetUnixFileName(temp)); */
783 DOS_closedir(dp);
784 AX_reg(context) = 0;
785 #endif
788 static void RenameFileFCB( SIGCONTEXT *context )
790 fprintf( stderr, "RenameFileFCB: not implemented yet\n" );
791 #if 0
792 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
793 struct dosdirent *dp;
794 char temp[256], oldname[256], newname[256], *oldnameptr, *newnameptr;
795 int drive = DOS_GET_DRIVE( *fcb );
797 DumpFCB( fcb );
799 temp[0] = '\\';
800 strcpy(temp+1, DRIVE_GetDosCwd(drive) );
801 strcat(temp, "\\");
802 strncat(temp, fcb + 1, 8);
803 ChopOffWhiteSpace(temp);
804 strncat(temp, fcb + 9, 3);
805 ChopOffWhiteSpace(temp);
807 if ((dp = DOS_opendir(temp)) == NULL) {
808 Error(InvalidDrive, EC_MediaError , EL_Disk);
809 AX_reg(context) = 0xff;
810 return;
813 oldname[0] = '\\';
814 strcpy(oldname+1, DRIVE_GetDosCwd(drive) );
815 strcat(oldname, "\\");
816 strcpy( newname, oldname );
817 oldnameptr = oldname + strlen(oldname);
818 newnameptr = newname + strlen(newname);
820 while (DOS_readdir(dp) != NULL)
822 strcpy(oldnameptr, dp->filename);
823 strcpy(newnameptr, fcb + 1);
824 dprintf_int(stddeb, "int21: renamefile %s -> %s\n",
825 oldname, newname);
827 DOS_closedir(dp);
828 AX_reg(context) = 0;
829 #endif
834 static void fLock( SIGCONTEXT * context )
836 #if 0
837 struct flock f;
838 int result,retries=sharing_retries;
840 f.l_start = MAKELONG(DX_reg(context),CX_reg(context));
841 f.l_len = MAKELONG(DI_reg(context),SI_reg(context));
842 f.l_whence = 0;
843 f.l_pid = 0;
845 switch ( AX_reg(context) & 0xff )
847 case 0x00: /* LOCK */
848 f.l_type = F_WRLCK;
849 break;
851 case 0x01: /* UNLOCK */
852 f.l_type = F_UNLCK;
853 break;
855 default:
856 AX_reg(context) = 0x0001;
857 SET_CFLAG(context);
858 return;
862 result = fcntl(BX_reg(context),F_SETLK,&f);
863 if ( retries && (!result) )
865 int i;
866 for(i=0;i<32768*((int)sharing_pause);i++)
867 result++; /* stop the optimizer */
868 for(i=0;i<32768*((int)sharing_pause);i++)
869 result--;
872 while( (!result) && (!(retries--)) );
874 if(result)
876 FILE_SetDosError();
877 AX_reg(context) = DOS_ExtendedError;
878 SET_CFLAG(context);
879 return;
881 #endif
885 static int INT21_GetFileAttribute( SIGCONTEXT * context )
887 const char *unixName;
889 unixName = DOSFS_GetUnixFileName( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)), TRUE );
890 if (!unixName) return 0;
891 if (!FILE_Stat( unixName, &CL_reg(context), NULL, NULL, NULL )) return 0;
892 CH_reg(context) = 0;
893 dprintf_int( stddeb, "INT21_GetFileAttributes(%s) = 0x%x\n",
894 unixName, CX_reg(context) );
895 return 1;
899 extern void LOCAL_PrintHeap (WORD ds);
901 /***********************************************************************
902 * DOS3Call (KERNEL.102)
904 void DOS3Call( SIGCONTEXT context )
906 dprintf_int( stddeb, "int21: AX=%04x BX=%04x CX=%04x DX=%04x "
907 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
908 AX_reg(&context), BX_reg(&context), CX_reg(&context),
909 DX_reg(&context), SI_reg(&context), DI_reg(&context),
910 DS_reg(&context), ES_reg(&context), EFL_reg(&context));
912 if (AH_reg(&context) == 0x59) /* Get extended error info */
914 AX_reg(&context) = DOS_ExtendedError;
915 BH_reg(&context) = DOS_ErrorClass;
916 BL_reg(&context) = DOS_ErrorAction;
917 CH_reg(&context) = DOS_ErrorLocus;
918 return;
921 DOS_ERROR( 0, 0, 0, 0 );
922 RESET_CFLAG(&context); /* Not sure if this is a good idea */
924 switch(AH_reg(&context))
926 case 0x00: /* TERMINATE PROGRAM */
927 TASK_KillCurrentTask( 0 );
928 break;
930 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
931 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
932 case 0x03: /* READ CHARACTER FROM STDAUX */
933 case 0x04: /* WRITE CHARACTER TO STDAUX */
934 case 0x05: /* WRITE CHARACTER TO PRINTER */
935 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
936 case 0x07: /* DIRECT CHARACTER INPUT, WITHOUT ECHO */
937 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
938 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
939 case 0x0a: /* BUFFERED INPUT */
940 case 0x0b: /* GET STDIN STATUS */
941 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
942 case 0x0f: /* OPEN FILE USING FCB */
943 case 0x10: /* CLOSE FILE USING FCB */
944 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
945 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
946 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
947 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
948 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
949 case 0x23: /* GET FILE SIZE FOR FCB */
950 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
951 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
952 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
953 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
954 case 0x29: /* PARSE FILENAME INTO FCB */
955 case 0x2e: /* SET VERIFY FLAG */
956 case 0x37: /* "SWITCHAR" - GET SWITCH CHARACTER
957 "SWITCHAR" - SET SWITCH CHARACTER
958 "AVAILDEV" - SPECIFY \DEV\ PREFIX USE */
959 case 0x54: /* GET VERIFY FLAG */
960 INT_BARF( &context, 0x21 );
961 break;
963 case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
964 case 0x1d:
965 case 0x1e:
966 case 0x20:
967 case 0x6b: /* NULL FUNCTION */
968 AL_reg(&context) = 0;
969 break;
971 case 0x5c: /* "FLOCK" - RECORD LOCKING */
972 fLock(&context);
973 break;
975 case 0x0d: /* DISK BUFFER FLUSH */
976 RESET_CFLAG(&context); /* dos 6+ only */
977 break;
979 case 0x0e: /* SELECT DEFAULT DRIVE */
980 DRIVE_SetCurrentDrive( DL_reg(&context) );
981 AL_reg(&context) = MAX_DOS_DRIVES;
982 break;
984 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
985 if (!INT21_FindFirstFCB(&context))
987 AL_reg(&context) = 0xff;
988 break;
990 /* else fall through */
992 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
993 AL_reg(&context) = INT21_FindNextFCB(&context) ? 0x00 : 0xff;
994 break;
996 case 0x13: /* DELETE FILE USING FCB */
997 DeleteFileFCB(&context);
998 break;
1000 case 0x17: /* RENAME FILE USING FCB */
1001 RenameFileFCB(&context);
1002 break;
1004 case 0x19: /* GET CURRENT DEFAULT DRIVE */
1005 AL_reg(&context) = DRIVE_GetCurrentDrive();
1006 break;
1008 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1010 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1011 pTask->dta = PTR_SEG_OFF_TO_SEGPTR(DS_reg(&context),DX_reg(&context));
1012 dprintf_int(stddeb, "int21: Set DTA: %08lx\n", pTask->dta);
1014 break;
1016 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1017 DL_reg(&context) = 0;
1018 if (!INT21_GetDriveAllocInfo(&context)) AX_reg(&context) = 0xffff;
1019 break;
1021 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1022 if (!INT21_GetDriveAllocInfo(&context)) AX_reg(&context) = 0xffff;
1023 break;
1025 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1026 GetDrivePB(&context, DRIVE_GetCurrentDrive());
1027 break;
1029 case 0x25: /* SET INTERRUPT VECTOR */
1030 INT_SetHandler( AL_reg(&context),
1031 PTR_SEG_OFF_TO_SEGPTR( DS_reg(&context), DX_reg(&context)));
1032 break;
1034 case 0x2a: /* GET SYSTEM DATE */
1035 GetSystemDate(&context);
1036 break;
1038 case 0x2b: /* SET SYSTEM DATE */
1039 fprintf( stdnimp, "SetSystemDate(%02d/%02d/%04d): not allowed\n",
1040 DL_reg(&context), DH_reg(&context), CX_reg(&context) );
1041 AL_reg(&context) = 0; /* Let's pretend we succeeded */
1042 break;
1044 case 0x2c: /* GET SYSTEM TIME */
1045 INT21_GetSystemTime(&context);
1046 break;
1048 case 0x2d: /* SET SYSTEM TIME */
1049 fprintf( stdnimp, "SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1050 CH_reg(&context), CL_reg(&context),
1051 DH_reg(&context), DL_reg(&context) );
1052 AL_reg(&context) = 0; /* Let's pretend we succeeded */
1053 break;
1055 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1057 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1058 ES_reg(&context) = SELECTOROF( pTask->dta );
1059 BX_reg(&context) = OFFSETOF( pTask->dta );
1061 break;
1063 case 0x30: /* GET DOS VERSION */
1064 AX_reg(&context) = DOSVERSION;
1065 BX_reg(&context) = 0x0012; /* 0x123456 is Wine's serial # */
1066 CX_reg(&context) = 0x3456;
1067 break;
1069 case 0x31: /* TERMINATE AND STAY RESIDENT */
1070 INT_BARF( &context, 0x21 );
1071 break;
1073 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1074 GetDrivePB(&context, DOS_GET_DRIVE( DL_reg(&context) ) );
1075 break;
1077 case 0x33: /* MULTIPLEXED */
1078 switch (AL_reg(&context))
1080 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1081 DL_reg(&context) = 0;
1082 break;
1084 case 0x01: /* SET EXTENDED BREAK STATE */
1085 break;
1087 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1088 DL_reg(&context) = 0;
1089 break;
1091 case 0x05: /* GET BOOT DRIVE */
1092 DL_reg(&context) = 2;
1093 /* c: is Wine's bootdrive */
1094 break;
1096 case 0x06: /* GET TRUE VERSION NUMBER */
1097 BX_reg(&context) = DOSVERSION;
1098 DX_reg(&context) = 0x00;
1099 break;
1101 default:
1102 INT_BARF( &context, 0x21 );
1103 break;
1105 break;
1107 case 0x34: /* GET ADDRESS OF INDOS FLAG */
1108 ES_reg(&context) = DosHeapHandle;
1109 BX_reg(&context) = (int)&heap->InDosFlag - (int)heap;
1110 break;
1112 case 0x35: /* GET INTERRUPT VECTOR */
1114 SEGPTR addr = INT_GetHandler( AL_reg(&context) );
1115 ES_reg(&context) = SELECTOROF(addr);
1116 BX_reg(&context) = OFFSETOF(addr);
1118 break;
1120 case 0x36: /* GET FREE DISK SPACE */
1121 if (!INT21_GetFreeDiskSpace(&context)) AX_reg(&context) = 0xffff;
1122 break;
1124 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1125 AX_reg(&context) = 0x02; /* no country support available */
1126 SET_CFLAG(&context);
1127 break;
1129 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1130 if (!CreateDirectory32A( PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1131 DX_reg(&context) ), NULL))
1133 AX_reg(&context) = DOS_ExtendedError;
1134 SET_CFLAG(&context);
1136 break;
1138 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1139 if (!RemoveDirectory32A( PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1140 DX_reg(&context) )))
1142 AX_reg(&context) = DOS_ExtendedError;
1143 SET_CFLAG(&context);
1145 break;
1147 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1148 INT21_ChangeDir(&context);
1149 break;
1151 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1152 AX_reg(&context) = _lcreat( PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1153 DX_reg(&context) ), CX_reg(&context) );
1154 if (AX_reg(&context) == (WORD)HFILE_ERROR)
1156 AX_reg(&context) = DOS_ExtendedError;
1157 SET_CFLAG(&context);
1159 break;
1161 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1162 OpenExistingFile(&context);
1163 break;
1165 case 0x3e: /* "CLOSE" - CLOSE FILE */
1166 if ((AX_reg(&context) = _lclose( BX_reg(&context) )) != 0)
1168 AX_reg(&context) = DOS_ExtendedError;
1169 SET_CFLAG(&context);
1171 break;
1173 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1175 LONG result = _hread( BX_reg(&context),
1176 PTR_SEG_OFF_TO_SEGPTR( DS_reg(&context),
1177 DX_reg(&context) ),
1178 CX_reg(&context) );
1179 if (result == -1)
1181 AX_reg(&context) = DOS_ExtendedError;
1182 SET_CFLAG(&context);
1184 else AX_reg(&context) = (WORD)result;
1186 break;
1188 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1190 LONG result = _hwrite( BX_reg(&context),
1191 PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1192 DX_reg(&context) ),
1193 CX_reg(&context) );
1194 if (result == -1)
1196 AX_reg(&context) = DOS_ExtendedError;
1197 SET_CFLAG(&context);
1199 else AX_reg(&context) = (WORD)result;
1201 break;
1203 case 0x41: /* "UNLINK" - DELETE FILE */
1204 if (!DeleteFile32A( PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1205 DX_reg(&context) )))
1207 AX_reg(&context) = DOS_ExtendedError;
1208 SET_CFLAG(&context);
1210 break;
1212 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1214 LONG status = _llseek( BX_reg(&context),
1215 MAKELONG(DX_reg(&context),CX_reg(&context)),
1216 AL_reg(&context) );
1217 if (status == HFILE_ERROR)
1219 AX_reg(&context) = DOS_ExtendedError;
1220 SET_CFLAG(&context);
1221 break;
1223 AX_reg(&context) = LOWORD(status);
1224 DX_reg(&context) = HIWORD(status);
1226 break;
1228 case 0x43: /* FILE ATTRIBUTES */
1229 switch (AL_reg(&context))
1231 case 0x00:
1232 if (!INT21_GetFileAttribute(&context))
1234 AX_reg(&context) = DOS_ExtendedError;
1235 SET_CFLAG(&context);
1237 break;
1238 case 0x01:
1239 RESET_CFLAG(&context);
1240 break;
1242 break;
1244 case 0x44: /* IOCTL */
1245 switch (AL_reg(&context))
1247 case 0x00:
1248 ioctlGetDeviceInfo(&context);
1249 break;
1251 case 0x01:
1252 break;
1254 case 0x08: /* Check if drive is removable. */
1255 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(&context) )))
1257 case DRIVE_CANNOTDETERMINE:
1258 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
1259 AX_reg(&context) = ER_InvalidDrive;
1260 SET_CFLAG(&context);
1261 break;
1262 case DRIVE_REMOVABLE:
1263 AX_reg(&context) = 0; /* removable */
1264 break;
1265 default:
1266 AX_reg(&context) = 1; /* not removable */
1267 break;
1269 break;
1271 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
1272 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(&context) )))
1274 case DRIVE_CANNOTDETERMINE:
1275 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
1276 AX_reg(&context) = ER_InvalidDrive;
1277 SET_CFLAG(&context);
1278 break;
1279 case DRIVE_REMOTE:
1280 DX_reg(&context) = (1<<9) | (1<<12); /* remote */
1281 break;
1282 default:
1283 DX_reg(&context) = 0; /* FIXME: use driver attr here */
1284 break;
1286 break;
1288 case 0x0a: /* check if handle (BX) is remote */
1289 /* returns DX, bit 15 set if remote, bit 14 set if date/time
1290 * not set on close
1292 DX_reg(&context) = 0;
1293 break;
1295 case 0x0b: /* SET SHARING RETRY COUNT */
1296 if (!CX_reg(&context))
1298 AX_reg(&context) = 1;
1299 SET_CFLAG(&context);
1300 break;
1302 sharing_pause = CX_reg(&context);
1303 if (!DX_reg(&context))
1304 sharing_retries = DX_reg(&context);
1305 RESET_CFLAG(&context);
1306 break;
1308 case 0x0d:
1309 ioctlGenericBlkDevReq(&context);
1310 break;
1312 case 0x0F: /* Set logical drive mapping */
1313 /* FIXME: Not implemented at the moment, always returns error
1315 INT_BARF( &context, 0x21 );
1316 AX_reg(&context) = 0x0001; /* invalid function */
1317 SET_CFLAG(&context);
1318 break;
1320 default:
1321 INT_BARF( &context, 0x21 );
1322 break;
1324 break;
1326 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1327 if ((AX_reg(&context) = FILE_Dup(BX_reg(&context))) == (WORD)HFILE_ERROR)
1329 AX_reg(&context) = DOS_ExtendedError;
1330 SET_CFLAG(&context);
1332 break;
1334 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1335 if (FILE_Dup2( BX_reg(&context), CX_reg(&context) ) == HFILE_ERROR)
1337 AX_reg(&context) = DOS_ExtendedError;
1338 SET_CFLAG(&context);
1340 break;
1342 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1343 if (!INT21_GetCurrentDirectory(&context))
1345 AX_reg(&context) = DOS_ExtendedError;
1346 SET_CFLAG(&context);
1348 else AX_reg(&context) = 0x0100;
1349 /* intlist: many Microsoft products for Windows rely on this */
1350 break;
1352 case 0x48: /* ALLOCATE MEMORY */
1353 case 0x49: /* FREE MEMORY */
1354 case 0x4a: /* RESIZE MEMORY BLOCK */
1355 INT_BARF( &context, 0x21 );
1356 break;
1358 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1359 AX_reg(&context) = WinExec( PTR_SEG_OFF_TO_LIN( DS_reg(&context),
1360 DX_reg(&context) ),
1361 SW_NORMAL );
1362 if (AX_reg(&context) < 32) SET_CFLAG(&context);
1363 break;
1365 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1366 TASK_KillCurrentTask( AL_reg(&context) );
1367 break;
1369 case 0x4d: /* GET RETURN CODE */
1370 AX_reg(&context) = 0; /* normal exit */
1371 break;
1373 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1374 if (!INT21_FindFirst(&context)) break;
1375 /* fall through */
1377 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1378 if (!INT21_FindNext(&context))
1380 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Disk );
1381 AX_reg(&context) = ER_NoMoreFiles;
1382 SET_CFLAG(&context);
1384 break;
1386 case 0x51: /* GET PSP ADDRESS */
1387 case 0x62: /* GET PSP ADDRESS */
1388 /* FIXME: should we return the original DOS PSP upon */
1389 /* Windows startup ? */
1390 BX_reg(&context) = GetCurrentPDB();
1391 break;
1393 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1394 ES_reg(&context) = 0x0;
1395 BX_reg(&context) = 0x0;
1396 INT_BARF( &context, 0x21 );
1397 break;
1399 case 0x56: /* "RENAME" - RENAME FILE */
1400 if (!INT21_RenameFile(&context))
1402 AX_reg(&context) = DOS_ExtendedError;
1403 SET_CFLAG(&context);
1405 break;
1407 case 0x57: /* FILE DATE AND TIME */
1408 switch (AL_reg(&context))
1410 case 0x00: /* Get */
1411 if (!FILE_GetDateTime( BX_reg(&context), &DX_reg(&context),
1412 &CX_reg(&context), TRUE ))
1414 AX_reg(&context) = DOS_ExtendedError;
1415 SET_CFLAG(&context);
1417 break;
1419 case 0x01: /* Set */
1420 if (!FILE_SetDateTime( BX_reg(&context), DX_reg(&context),
1421 CX_reg(&context) ))
1423 AX_reg(&context) = DOS_ExtendedError;
1424 SET_CFLAG(&context);
1426 break;
1428 break;
1430 case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1431 switch (AL_reg(&context))
1433 case 0x00:
1434 AX_reg(&context) = 1;
1435 break;
1436 case 0x02:
1437 AX_reg(&context) = 0;
1438 break;
1439 case 0x01:
1440 case 0x03:
1441 break;
1443 RESET_CFLAG(&context);
1444 break;
1446 case 0x5a: /* CREATE TEMPORARY FILE */
1447 if (!INT21_CreateTempFile(&context))
1449 AX_reg(&context) = DOS_ExtendedError;
1450 SET_CFLAG(&context);
1452 break;
1454 case 0x5b: /* CREATE NEW FILE */
1455 if ((AX_reg(&context) = _lcreat_uniq( PTR_SEG_OFF_TO_LIN(DS_reg(&context),DX_reg(&context)), 0 )) == (WORD)HFILE_ERROR)
1457 AX_reg(&context) = DOS_ExtendedError;
1458 SET_CFLAG(&context);
1460 break;
1462 case 0x5d: /* NETWORK */
1463 case 0x5e:
1464 /* network software not installed */
1465 DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
1466 AX_reg(&context) = DOS_ExtendedError;
1467 SET_CFLAG(&context);
1468 break;
1470 case 0x5f: /* NETWORK */
1471 switch (AL_reg(&context))
1473 case 0x07: /* ENABLE DRIVE */
1474 if (!DRIVE_Enable( DL_reg(&context) ))
1476 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
1477 AX_reg(&context) = DOS_ExtendedError;
1478 SET_CFLAG(&context);
1480 break;
1482 case 0x08: /* DISABLE DRIVE */
1483 if (!DRIVE_Disable( DL_reg(&context) ))
1485 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
1486 AX_reg(&context) = DOS_ExtendedError;
1487 SET_CFLAG(&context);
1489 break;
1491 default:
1492 /* network software not installed */
1493 DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
1494 AX_reg(&context) = DOS_ExtendedError;
1495 SET_CFLAG(&context);
1496 break;
1498 break;
1500 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1502 const char *truename = DOSFS_GetDosTrueName( PTR_SEG_OFF_TO_LIN(DS_reg(&context),SI_reg(&context) ), FALSE );
1503 if (!truename)
1505 AX_reg(&context) = DOS_ExtendedError;
1506 SET_CFLAG(&context);
1508 else
1510 lstrcpyn32A( PTR_SEG_OFF_TO_LIN( ES_reg(&context),
1511 DI_reg(&context) ),
1512 truename, 128 );
1513 AX_reg(&context) = 0;
1516 break;
1518 case 0x61: /* UNUSED */
1519 case 0x63: /* UNUSED */
1520 case 0x64: /* OS/2 DOS BOX */
1521 case 0x65: /* GET EXTENDED COUNTRY INFORMATION */
1522 INT_BARF( &context, 0x21 );
1523 SET_CFLAG(&context);
1524 break;
1526 case 0x66: /* GLOBAL CODE PAGE TABLE */
1527 switch (AL_reg(&context))
1529 case 0x01:
1530 DX_reg(&context) = BX_reg(&context) = CodePage;
1531 RESET_CFLAG(&context);
1532 break;
1533 case 0x02:
1534 CodePage = BX_reg(&context);
1535 RESET_CFLAG(&context);
1536 break;
1538 break;
1540 case 0x67: /* SET HANDLE COUNT */
1541 SetHandleCount( BX_reg(&context) );
1542 if (DOS_ExtendedError)
1544 AX_reg(&context) = DOS_ExtendedError;
1545 SET_CFLAG(&context);
1547 break;
1549 case 0x68: /* "FFLUSH" - COMMIT FILE */
1550 case 0x6a: /* COMMIT FILE */
1551 if (!FlushFileBuffers( BX_reg(&context) ))
1553 AX_reg(&context) = DOS_ExtendedError;
1554 SET_CFLAG(&context);
1556 break;
1558 case 0x69: /* DISK SERIAL NUMBER */
1559 switch (AL_reg(&context))
1561 case 0x00:
1562 if (!INT21_GetDiskSerialNumber(&context))
1564 AX_reg(&context) = DOS_ExtendedError;
1565 SET_CFLAG(&context);
1567 else AX_reg(&context) = 0;
1568 break;
1569 case 0x01:
1570 if (!INT21_SetDiskSerialNumber(&context))
1572 AX_reg(&context) = DOS_ExtendedError;
1573 SET_CFLAG(&context);
1575 else AX_reg(&context) = 1;
1576 break;
1578 break;
1580 case 0x6C: /* Extended Open/Create*/
1581 ExtendedOpenCreateFile(&context);
1582 break;
1584 case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
1585 case 0x71: /* MS-DOS 7 (Chicago) - LONG FILENAME FUNCTIONS */
1586 case 0x72: /* MS-DOS 7 (Windows95) - ??? */
1587 case 0x73: /* MS-DOS 7 (Windows95) - DRIVE LOCKING ??? */
1588 dprintf_int(stddeb,"int21: windows95 function AX %04x\n",
1589 AX_reg(&context));
1590 dprintf_int(stddeb, " returning unimplemented\n");
1591 SET_CFLAG(&context);
1592 AL_reg(&context) = 0;
1593 break;
1595 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
1596 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
1597 break;
1599 default:
1600 INT_BARF( &context, 0x21 );
1601 break;
1603 dprintf_int( stddeb, "ret21: AX=%04x BX=%04x CX=%04x DX=%04x "
1604 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1605 AX_reg(&context), BX_reg(&context), CX_reg(&context),
1606 DX_reg(&context), SI_reg(&context), DI_reg(&context),
1607 DS_reg(&context), ES_reg(&context), EFL_reg(&context));
1611 BOOL INT21_Init(void)
1613 if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
1615 fprintf( stderr, "INT21_Init: Out of memory\n");
1616 return FALSE;
1618 heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
1620 dpb = &heap->dpb;
1621 dpbsegptr = PTR_SEG_OFF_TO_SEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
1622 heap->InDosFlag = 0;
1623 strcpy(heap->biosdate, "01/01/80");
1624 return TRUE;