Release 980315
[wine/multimedia.git] / msdos / int21.c
blobce1b51c7de6621e8c64511d9482c916bfecc014e
1 /*
2 * DOS interrupt 21h handler
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 "windows.h"
19 #include "drive.h"
20 #include "file.h"
21 #include "heap.h"
22 #include "msdos.h"
23 #include "ldt.h"
24 #include "task.h"
25 #include "options.h"
26 #include "miscemu.h"
27 #include "debug.h"
28 #if defined(__svr4__) || defined(_SCO_DS)
29 /* SVR4 DOESNT do locking the same way must implement properly */
30 #define LOCK_EX 0
31 #define LOCK_SH 1
32 #define LOCK_NB 8
33 #endif
36 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
38 /* Define the drive parameter block, as used by int21/1F
39 * and int21/32. This table can be accessed through the
40 * global 'dpb' pointer, which points into the local dos
41 * heap.
43 struct DPB
45 BYTE drive_num; /* 0=A, etc. */
46 BYTE unit_num; /* Drive's unit number (?) */
47 WORD sector_size; /* Sector size in bytes */
48 BYTE high_sector; /* Highest sector in a cluster */
49 BYTE shift; /* Shift count (?) */
50 WORD reserved; /* Number of reserved sectors at start */
51 BYTE num_FAT; /* Number of FATs */
52 WORD dir_entries; /* Number of root dir entries */
53 WORD first_data; /* First data sector */
54 WORD high_cluster; /* Highest cluster number */
55 WORD sectors_in_FAT; /* Number of sectors per FAT */
56 WORD start_dir; /* Starting sector of first dir */
57 DWORD driver_head; /* Address of device driver header (?) */
58 BYTE media_ID; /* Media ID */
59 BYTE access_flag; /* Prev. accessed flag (0=yes,0xFF=no) */
60 DWORD next; /* Pointer to next DPB in list */
61 WORD free_search; /* Free cluster search start */
62 WORD free_clusters; /* Number of free clusters (0xFFFF=unknown) */
65 WORD CodePage = 437;
66 DWORD dpbsegptr;
68 struct DosHeap {
69 BYTE InDosFlag;
70 BYTE mediaID;
71 BYTE biosdate[8];
72 struct DPB dpb;
74 static struct DosHeap *heap;
75 static WORD DosHeapHandle;
77 WORD sharing_retries = 3; /* number of retries at sharing violation */
78 WORD sharing_pause = 1; /* pause between retries */
80 extern char TempDirectory[];
82 static BOOL32 INT21_CreateHeap(void)
84 if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
86 fprintf( stderr, "INT21_Init: Out of memory\n");
87 return FALSE;
89 heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
90 dpbsegptr = PTR_SEG_OFF_TO_SEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
91 heap->InDosFlag = 0;
92 strcpy(heap->biosdate, "01/01/80");
93 return TRUE;
96 static BYTE *GetCurrentDTA(void)
98 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
99 return (BYTE *)PTR_SEG_TO_LIN( pTask->dta );
103 void CreateBPB(int drive, BYTE *data)
105 if (drive > 1) {
106 setword(data, 512);
107 data[2] = 2;
108 setword(&data[3], 0);
109 data[5] = 2;
110 setword(&data[6], 240);
111 setword(&data[8], 64000);
112 data[0x0a] = 0xf8;
113 setword(&data[0x0b], 40);
114 setword(&data[0x0d], 56);
115 setword(&data[0x0f], 2);
116 setword(&data[0x11], 0);
117 setword(&data[0x1f], 800);
118 data[0x21] = 5;
119 setword(&data[0x22], 1);
120 } else { /* 1.44mb */
121 setword(data, 512);
122 data[2] = 2;
123 setword(&data[3], 0);
124 data[5] = 2;
125 setword(&data[6], 240);
126 setword(&data[8], 2880);
127 data[0x0a] = 0xf8;
128 setword(&data[0x0b], 6);
129 setword(&data[0x0d], 18);
130 setword(&data[0x0f], 2);
131 setword(&data[0x11], 0);
132 setword(&data[0x1f], 80);
133 data[0x21] = 7;
134 setword(&data[0x22], 2);
138 static int INT21_GetFreeDiskSpace( CONTEXT *context )
140 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
141 char root[] = "A:\\";
143 *root += DOS_GET_DRIVE( DL_reg(context) );
144 if (!GetDiskFreeSpace32A( root, &cluster_sectors, &sector_bytes,
145 &free_clusters, &total_clusters )) return 0;
146 AX_reg(context) = cluster_sectors;
147 BX_reg(context) = free_clusters;
148 CX_reg(context) = sector_bytes;
149 DX_reg(context) = total_clusters;
150 return 1;
153 static int INT21_GetDriveAllocInfo( CONTEXT *context )
155 if (!INT21_GetFreeDiskSpace( context )) return 0;
156 if (!heap && !INT21_CreateHeap()) 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( CONTEXT *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 if (heap || INT21_CreateHeap())
172 FIXME(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 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
182 heap->dpb.sector_size = 512;
183 heap->dpb.high_sector = 1;
184 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
185 heap->dpb.reserved = 0;
186 heap->dpb.num_FAT = 1;
187 heap->dpb.dir_entries = 2;
188 heap->dpb.first_data = 2;
189 heap->dpb.high_cluster = 64000;
190 heap->dpb.sectors_in_FAT = 1;
191 heap->dpb.start_dir = 1;
192 heap->dpb.driver_head = 0;
193 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
194 heap->dpb.access_flag = 0;
195 heap->dpb.next = 0;
196 heap->dpb.free_search = 0;
197 heap->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( CONTEXT *context )
208 int curr_drive;
209 TRACE(int21, "(%d)\n", BX_reg(context));
211 curr_drive = DRIVE_GetCurrentDrive();
212 DX_reg(context) = 0x0140 + curr_drive + ((curr_drive > 1) ? 0x0800 : 0); /* no floppy */
213 /* bits 0-5 are current drive
214 * bit 6 - file has NOT been written..FIXME: correct?
215 * bit 8 - generate int24 if no diskspace on write/ read past end of file
216 * bit 11 - media not removable
218 RESET_CFLAG(context);
221 static BOOL32 ioctlGenericBlkDevReq( CONTEXT *context )
223 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
224 int drive = DOS_GET_DRIVE( BL_reg(context) );
226 if (!DRIVE_IsValid(drive))
228 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
229 return TRUE;
232 if (CH_reg(context) != 0x08)
234 INT_BARF( context, 0x21 );
235 return FALSE;
238 switch (CL_reg(context))
240 case 0x4a: /* lock logical volume */
241 TRACE(int21,"lock logical volume (%d) level %d mode %d\n",drive,BH_reg(context),DX_reg(context));
242 break;
244 case 0x60: /* get device parameters */
245 /* used by w4wgrp's winfile */
246 memset(dataptr, 0, 0x26);
247 dataptr[0] = 0x04;
248 dataptr[6] = 0; /* media type */
249 if (drive > 1)
251 dataptr[1] = 0x05; /* fixed disk */
252 setword(&dataptr[2], 0x01); /* non removable */
253 setword(&dataptr[4], 0x300); /* # of cylinders */
255 else
257 dataptr[1] = 0x07; /* block dev, floppy */
258 setword(&dataptr[2], 0x02); /* removable */
259 setword(&dataptr[4], 80); /* # of cylinders */
261 CreateBPB(drive, &dataptr[7]);
262 RESET_CFLAG(context);
263 break;
265 case 0x66:/* get disk serial number */
267 char label[12],fsname[9],path[4];
268 DWORD serial;
270 strcpy(path,"x:\\");path[0]=drive+'A';
271 GetVolumeInformation32A(
272 path,label,12,&serial,NULL,NULL,fsname,9
274 *(WORD*)dataptr = 0;
275 memcpy(dataptr+2,&serial,4);
276 memcpy(dataptr+6,label ,11);
277 memcpy(dataptr+17,fsname,8);
279 break;
281 case 0x6a:
282 TRACE(int21,"logical volume %d unlocked.\n",drive);
283 break;
285 default:
286 INT_BARF( context, 0x21 );
288 return FALSE;
291 static void INT21_GetSystemDate( CONTEXT *context )
293 SYSTEMTIME systime;
294 GetLocalTime( &systime );
295 CX_reg(context) = systime.wYear;
296 DX_reg(context) = (systime.wMonth << 8) | systime.wDay;
297 AX_reg(context) = systime.wDayOfWeek;
300 static void INT21_GetSystemTime( CONTEXT *context )
302 SYSTEMTIME systime;
303 GetLocalTime( &systime );
304 CX_reg(context) = (systime.wHour << 8) | systime.wMinute;
305 DX_reg(context) = (systime.wSecond << 8) | (systime.wMilliseconds / 10);
308 static BOOL32 INT21_CreateFile( CONTEXT *context )
310 AX_reg(context) = _lcreat16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
311 DX_reg(context) ), CX_reg(context) );
312 return (AX_reg(context) == (WORD)HFILE_ERROR16);
316 static void OpenExistingFile( CONTEXT *context )
318 AX_reg(context) = _lopen16( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
319 AL_reg(context) );
320 if (AX_reg(context) == (WORD)HFILE_ERROR16)
322 AX_reg(context) = DOS_ExtendedError;
323 SET_CFLAG(context);
325 #if 0
327 int handle;
328 int mode;
329 int lock;
331 switch (AX_reg(context) & 0x0070)
333 case 0x00: /* compatability mode */
334 case 0x40: /* DENYNONE */
335 lock = -1;
336 break;
338 case 0x30: /* DENYREAD */
339 TRACE(int21, "(%s): DENYREAD changed to DENYALL\n",
340 (char *)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)));
341 case 0x10: /* DENYALL */
342 lock = LOCK_EX;
343 break;
345 case 0x20: /* DENYWRITE */
346 lock = LOCK_SH;
347 break;
349 default:
350 lock = -1;
353 if (lock != -1)
356 int result,retries=sharing_retries;
358 #if defined(__svr4__) || defined(_SCO_DS)
359 printf("Should call flock and needs porting to lockf\n");
360 result = 0;
361 retries = 0;
362 #else
363 result = flock(handle, lock | LOCK_NB);
364 #endif
365 if ( retries && (!result) )
367 int i;
368 for(i=0;i<32768*((int)sharing_pause);i++)
369 result++; /* stop the optimizer */
370 for(i=0;i<32768*((int)sharing_pause);i++)
371 result--;
374 while( (!result) && (!(retries--)) );
376 if(result)
378 errno_to_doserr();
379 AX_reg(context) = DOS_ExtendedError;
380 close(handle);
381 SET_CFLAG(context);
382 return;
387 Error (0,0,0);
388 AX_reg(context) = handle;
389 RESET_CFLAG(context);
391 #endif
394 static void CloseFile( CONTEXT *context )
396 if ((AX_reg(context) = _lclose16( BX_reg(context) )) != 0)
398 AX_reg(context) = DOS_ExtendedError;
399 SET_CFLAG(context);
403 static BOOL32 INT21_ExtendedOpenCreateFile(CONTEXT *context )
405 BOOL32 bExtendedError = FALSE;
406 BYTE action = DL_reg(context);
408 /* Shuffle arguments to call OpenExistingFile */
409 AL_reg(context) = BL_reg(context);
410 DX_reg(context) = SI_reg(context);
411 /* BX,CX and DX should be preserved */
412 OpenExistingFile(context);
414 if ((EFL_reg(context) & 0x0001) == 0) /* File exists */
416 UINT16 uReturnCX = 0;
418 /* Now decide what do do */
420 if ((action & 0x07) == 0)
422 BX_reg(context) = AX_reg(context);
423 CloseFile(context);
424 AX_reg(context) = 0x0050; /*File exists*/
425 SET_CFLAG(context);
426 WARN(int21, "extended open/create: failed because file exists \n");
428 else if ((action & 0x07) == 2)
430 /* Truncate it, but first check if opened for write */
431 if ((BL_reg(context) & 0x0007)== 0)
433 BX_reg(context) = AX_reg(context);
434 CloseFile(context);
435 WARN(int21, "extended open/create: failed, trunc on ro file\n");
436 AX_reg(context) = 0x000C; /*Access code invalid*/
437 SET_CFLAG(context);
439 else
441 /* Shuffle arguments to call CloseFile while
442 * preserving BX and DX */
444 TRACE(int21, "extended open/create: Closing before truncate\n");
445 BX_reg(context) = AX_reg(context);
446 CloseFile(context);
447 if (EFL_reg(context) & 0x0001)
449 WARN(int21, "extended open/create: close before trunc failed\n");
450 AX_reg(context) = 0x0019; /*Seek Error*/
451 CX_reg(context) = 0;
452 SET_CFLAG(context);
454 /* Shuffle arguments to call CreateFile */
456 TRACE(int21, "extended open/create: Truncating\n");
457 AL_reg(context) = BL_reg(context);
458 /* CX is still the same */
459 DX_reg(context) = SI_reg(context);
460 bExtendedError = INT21_CreateFile(context);
462 if (EFL_reg(context) & 0x0001) /*no file open, flags set */
464 WARN(int21, "extended open/create: trunc failed\n");
465 return bExtendedError;
467 uReturnCX = 0x3;
470 else uReturnCX = 0x1;
472 CX_reg(context) = uReturnCX;
474 else /* file does not exist */
476 RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
477 if ((action & 0xF0)== 0)
479 CX_reg(context) = 0;
480 SET_CFLAG(context);
481 WARN(int21, "extended open/create: failed, file dosen't exist\n");
483 else
485 /* Shuffle arguments to call CreateFile */
486 TRACE(int21, "extended open/create: Creating\n");
487 AL_reg(context) = BL_reg(context);
488 /* CX should still be the same */
489 DX_reg(context) = SI_reg(context);
490 bExtendedError = INT21_CreateFile(context);
491 if (EFL_reg(context) & 0x0001) /*no file open, flags set */
493 WARN(int21, "extended open/create: create failed\n");
494 return bExtendedError;
496 CX_reg(context) = 2;
500 return bExtendedError;
504 static BOOL32 INT21_ChangeDir( CONTEXT *context )
506 int drive;
507 char *dirname = PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context));
509 TRACE(int21,"changedir %s\n", dirname);
510 if (dirname[0] && (dirname[1] == ':'))
512 drive = toupper(dirname[0]) - 'A';
513 dirname += 2;
515 else drive = DRIVE_GetCurrentDrive();
516 return DRIVE_Chdir( drive, dirname );
520 static int INT21_FindFirst( CONTEXT *context )
522 char *p;
523 const char *path;
524 DOS_FULL_NAME full_name;
525 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA();
527 path = (const char *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
528 dta->unixPath = NULL;
529 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
531 AX_reg(context) = DOS_ExtendedError;
532 SET_CFLAG(context);
533 return 0;
535 dta->unixPath = HEAP_strdupA( GetProcessHeap(), 0, full_name.long_name );
536 p = strrchr( dta->unixPath, '/' );
537 *p = '\0';
539 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
540 * (doesn't matter as it is set below anyway)
542 if (!DOSFS_ToDosFCBFormat( p + 1, dta->mask ))
544 HeapFree( GetProcessHeap(), 0, dta->unixPath );
545 dta->unixPath = NULL;
546 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
547 AX_reg(context) = ER_FileNotFound;
548 SET_CFLAG(context);
549 return 0;
551 dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
552 : DRIVE_GetCurrentDrive();
553 dta->count = 0;
554 dta->search_attr = CL_reg(context);
555 return 1;
559 static int INT21_FindNext( CONTEXT *context )
561 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA();
562 WIN32_FIND_DATA32A entry;
563 int count;
565 if (!dta->unixPath) return 0;
566 if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
567 dta->search_attr, dta->count, &entry )))
569 HeapFree( GetProcessHeap(), 0, dta->unixPath );
570 dta->unixPath = NULL;
571 return 0;
573 if ((int)dta->count + count > 0xffff)
575 fprintf( stderr, "Too many directory entries in %s\n", dta->unixPath );
576 HeapFree( GetProcessHeap(), 0, dta->unixPath );
577 dta->unixPath = NULL;
578 return 0;
580 dta->count += count;
581 dta->fileattr = entry.dwFileAttributes;
582 dta->filesize = entry.nFileSizeLow;
583 FileTimeToDosDateTime( &entry.ftLastWriteTime,
584 &dta->filedate, &dta->filetime );
585 strcpy( dta->filename, entry.cAlternateFileName );
586 return 1;
590 static BOOL32 INT21_CreateTempFile( CONTEXT *context )
592 static int counter = 0;
593 char *name = PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context) );
594 char *p = name + strlen(name);
596 /* despite what Ralf Brown says, some programs seem to call without
597 * ending backslash (DOS accepts that, so we accept it too) */
598 if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
600 for (;;)
602 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
603 counter = (counter + 1) % 1000;
605 if ((AX_reg(context) = _lcreat_uniq( name, 0 )) != (WORD)HFILE_ERROR16)
607 TRACE(int21, "created %s\n", name );
608 return TRUE;
610 if (DOS_ExtendedError != ER_FileExists) return FALSE;
615 static BOOL32 INT21_GetCurrentDirectory( CONTEXT *context )
617 int drive = DOS_GET_DRIVE( DL_reg(context) );
618 char *ptr = (char *)PTR_SEG_OFF_TO_LIN( DS_reg(context), SI_reg(context) );
620 if (!DRIVE_IsValid(drive))
622 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
623 return FALSE;
625 lstrcpyn32A( ptr, DRIVE_GetDosCwd(drive), 64 );
626 AX_reg(context) = 0x0100; /* success return code */
627 return TRUE;
631 static int INT21_GetDiskSerialNumber( CONTEXT *context )
633 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
634 int drive = DOS_GET_DRIVE( BL_reg(context) );
636 if (!DRIVE_IsValid(drive))
638 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
639 return 0;
642 *(WORD *)dataptr = 0;
643 *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
644 memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
645 strncpy(dataptr + 0x11, "FAT16 ", 8);
646 return 1;
650 static int INT21_SetDiskSerialNumber( CONTEXT *context )
652 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
653 int drive = DOS_GET_DRIVE( BL_reg(context) );
655 if (!DRIVE_IsValid(drive))
657 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
658 return 0;
661 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
662 return 1;
666 /* microsoft's programmers should be shot for using CP/M style int21
667 calls in Windows for Workgroup's winfile.exe */
669 static int INT21_FindFirstFCB( CONTEXT *context )
671 BYTE *fcb = (BYTE *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
672 FINDFILE_FCB *pFCB;
673 LPCSTR root, cwd;
674 int drive;
676 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
677 else pFCB = (FINDFILE_FCB *)fcb;
678 drive = DOS_GET_DRIVE( pFCB->drive );
679 root = DRIVE_GetRoot( drive );
680 cwd = DRIVE_GetUnixCwd( drive );
681 pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
682 strlen(root)+strlen(cwd)+2 );
683 if (!pFCB->unixPath) return 0;
684 strcpy( pFCB->unixPath, root );
685 strcat( pFCB->unixPath, "/" );
686 strcat( pFCB->unixPath, cwd );
687 pFCB->count = 0;
688 return 1;
692 static int INT21_FindNextFCB( CONTEXT *context )
694 BYTE *fcb = (BYTE *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
695 FINDFILE_FCB *pFCB;
696 DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA();
697 WIN32_FIND_DATA32A entry;
698 BYTE attr;
699 int count;
701 if (*fcb == 0xff) /* extended FCB ? */
703 attr = fcb[6];
704 pFCB = (FINDFILE_FCB *)(fcb + 7);
706 else
708 attr = 0;
709 pFCB = (FINDFILE_FCB *)fcb;
712 if (!pFCB->unixPath) return 0;
713 if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
714 DOS_GET_DRIVE( pFCB->drive ), attr,
715 pFCB->count, &entry )))
717 HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
718 pFCB->unixPath = NULL;
719 return 0;
721 pFCB->count += count;
723 if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
724 *(BYTE *)pResult = 0xff;
725 (BYTE *)pResult +=6; /* leave reserved field behind */
726 *(BYTE *)pResult = entry.dwFileAttributes;
727 ((BYTE *)pResult)++;
729 *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
730 ((BYTE *)pResult)++;
731 pResult->fileattr = entry.dwFileAttributes;
732 pResult->cluster = 0; /* what else? */
733 pResult->filesize = entry.nFileSizeLow;
734 memset( pResult->reserved, 0, sizeof(pResult->reserved) );
735 FileTimeToDosDateTime( &entry.ftLastWriteTime,
736 &pResult->filedate, &pResult->filetime );
738 /* Convert file name to FCB format */
740 memset( pResult->filename, ' ', sizeof(pResult->filename) );
741 if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
742 else if (!strcmp( entry.cAlternateFileName, ".." ))
743 pResult->filename[0] = pResult->filename[1] = '.';
744 else
746 char *p = strrchr( entry.cAlternateFileName, '.' );
747 if (p && p[1] && (p != entry.cAlternateFileName))
749 memcpy( pResult->filename, entry.cAlternateFileName,
750 MIN( (p - entry.cAlternateFileName), 8 ) );
751 memcpy( pResult->filename + 8, p + 1, MIN( strlen(p), 3 ) );
753 else
754 memcpy( pResult->filename, entry.cAlternateFileName,
755 MIN( strlen(entry.cAlternateFileName), 8 ) );
757 return 1;
761 static void DeleteFileFCB( CONTEXT *context )
763 fprintf( stderr, "DeleteFileFCB: not implemented yet\n" );
766 static void RenameFileFCB( CONTEXT *context )
768 fprintf( stderr, "RenameFileFCB: not implemented yet\n" );
773 static void fLock( CONTEXT * context )
776 switch ( AX_reg(context) & 0xff )
778 case 0x00: /* LOCK */
779 if (!LockFile(BX_reg(context),
780 MAKELONG(DX_reg(context),CX_reg(context)), 0,
781 MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
782 AX_reg(context) = DOS_ExtendedError;
783 SET_CFLAG(context);
785 break;
787 case 0x01: /* UNLOCK */
788 if (!UnlockFile(BX_reg(context),
789 MAKELONG(DX_reg(context),CX_reg(context)), 0,
790 MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
791 AX_reg(context) = DOS_ExtendedError;
792 SET_CFLAG(context);
794 return;
795 default:
796 AX_reg(context) = 0x0001;
797 SET_CFLAG(context);
798 return;
802 SEGPTR INT21_GetListOfLists()
804 static DOS_LISTOFLISTS *LOL;
805 static SEGPTR seg_LOL;
808 Output of DOS 6.22:
810 0133:0020 6A 13-33 01 CC 00 33 01 59 00 j.3...3.Y.
811 0133:0030 70 00 00 00 72 02 00 02-6D 00 33 01 00 00 2E 05 p...r...m.3.....
812 0133:0040 00 00 FC 04 00 00 03 08-92 21 11 E0 04 80 C6 0D .........!......
813 0133:0050 CC 0D 4E 55 4C 20 20 20-20 20 00 00 00 00 00 00 ..NUL ......
814 0133:0060 00 4B BA C1 06 14 00 00-00 03 01 00 04 70 CE FF .K...........p..
815 0133:0070 FF 00 00 00 00 00 00 00-00 01 00 00 0D 05 00 00 ................
816 0133:0080 00 FF FF 00 00 00 00 FE-00 00 F8 03 FF 9F 70 02 ..............p.
817 0133:0090 D0 44 C8 FD D4 44 C8 FD-D4 44 C8 FD D0 44 C8 FD .D...D...D...D..
818 0133:00A0 D0 44 C8 FD D0 44 .D...D
820 if (!LOL) {
821 LOL = SEGPTR_ALLOC(sizeof(DOS_LISTOFLISTS));
823 LOL->CX_Int21_5e01 = 0x0;
824 LOL->LRU_count_FCB_cache = 0x0;
825 LOL->LRU_count_FCB_open = 0x0;
826 LOL->OEM_func_handler = -1; /* not available */
827 LOL->INT21_offset = 0x0;
828 LOL->sharing_retry_count = sharing_retries; /* default value: 3 */
829 LOL->sharing_retry_delay = sharing_pause; /* default value: 1 */
830 LOL->ptr_disk_buf = 0x0;
831 LOL->offs_unread_CON = 0x0;
832 LOL->seg_first_MCB = 0x0;
833 LOL->ptr_first_DPB = 0x0;
834 LOL->ptr_first_SysFileTable = 0x0;
835 LOL->ptr_clock_dev_hdr = 0x0;
836 LOL->ptr_CON_dev_hdr = 0x0;
837 LOL->max_byte_per_sec = 512;
838 LOL->ptr_disk_buf_info = 0x0;
839 LOL->ptr_array_CDS = 0x0;
840 LOL->ptr_sys_FCB = 0x0;
841 LOL->nr_protect_FCB = 0x0;
842 LOL->nr_block_dev = 0x0;
843 LOL->nr_avail_drive_letters = 26; /* A - Z */
844 LOL->nr_drives_JOINed = 0x0;
845 LOL->ptr_spec_prg_names = 0x0;
846 LOL->ptr_SETVER_prg_list = 0x0; /* no SETVER list */
847 LOL->DOS_HIGH_A20_func_offs = 0x0;
848 LOL->PSP_last_exec = 0x0;
849 LOL->BUFFERS_val = 99; /* maximum: 99 */
850 LOL->BUFFERS_nr_lookahead = 8; /* maximum: 8 */
851 LOL->boot_drive = 3; /* C: */
852 LOL->flag_DWORD_moves = 0x01; /* i386+ */
853 LOL->size_extended_mem = 0xf000; /* very high value */
855 if (!seg_LOL) seg_LOL = SEGPTR_GET(LOL);
856 return seg_LOL+(WORD)&((DOS_LISTOFLISTS*)0)->ptr_first_DPB;
860 extern void LOCAL_PrintHeap (WORD ds);
862 /***********************************************************************
863 * DOS3Call (KERNEL.102)
865 void WINAPI DOS3Call( CONTEXT *context )
867 BOOL32 bSetDOSExtendedError = FALSE;
869 TRACE(int21, "AX=%04x BX=%04x CX=%04x DX=%04x "
870 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
871 AX_reg(context), BX_reg(context), CX_reg(context),
872 DX_reg(context), SI_reg(context), DI_reg(context),
873 (WORD)DS_reg(context), (WORD)ES_reg(context),
874 EFL_reg(context) );
876 if (AH_reg(context) == 0x59) /* Get extended error info */
878 AX_reg(context) = DOS_ExtendedError;
879 BH_reg(context) = DOS_ErrorClass;
880 BL_reg(context) = DOS_ErrorAction;
881 CH_reg(context) = DOS_ErrorLocus;
882 return;
885 DOS_ERROR( 0, 0, 0, 0 );
886 RESET_CFLAG(context); /* Not sure if this is a good idea */
888 switch(AH_reg(context))
890 case 0x00: /* TERMINATE PROGRAM */
891 TASK_KillCurrentTask( 0 );
892 break;
894 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
895 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
896 case 0x03: /* READ CHARACTER FROM STDAUX */
897 case 0x04: /* WRITE CHARACTER TO STDAUX */
898 case 0x05: /* WRITE CHARACTER TO PRINTER */
899 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
900 case 0x07: /* DIRECT CHARACTER INPUT, WITHOUT ECHO */
901 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
902 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
903 case 0x0a: /* BUFFERED INPUT */
904 case 0x0b: /* GET STDIN STATUS */
905 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
906 case 0x0f: /* OPEN FILE USING FCB */
907 case 0x10: /* CLOSE FILE USING FCB */
908 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
909 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
910 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
911 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
912 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
913 case 0x23: /* GET FILE SIZE FOR FCB */
914 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
915 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
916 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
917 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
918 case 0x29: /* PARSE FILENAME INTO FCB */
919 case 0x37: /* "SWITCHAR" - GET SWITCH CHARACTER
920 "SWITCHAR" - SET SWITCH CHARACTER
921 "AVAILDEV" - SPECIFY \DEV\ PREFIX USE */
922 case 0x54: /* GET VERIFY FLAG */
923 INT_BARF( context, 0x21 );
924 break;
925 case 0x2e: /* SET VERIFY FLAG */
926 /* we cannot change the behaviour anyway, so just ignore it */
927 break;
929 case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
930 case 0x1d:
931 case 0x1e:
932 case 0x20:
933 case 0x6b: /* NULL FUNCTION */
934 AL_reg(context) = 0;
935 break;
937 case 0x5c: /* "FLOCK" - RECORD LOCKING */
938 fLock(context);
939 break;
941 case 0x0d: /* DISK BUFFER FLUSH */
942 RESET_CFLAG(context); /* dos 6+ only */
943 break;
945 case 0x0e: /* SELECT DEFAULT DRIVE */
946 DRIVE_SetCurrentDrive( DL_reg(context) );
947 AL_reg(context) = MAX_DOS_DRIVES;
948 break;
950 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
951 if (!INT21_FindFirstFCB(context))
953 AL_reg(context) = 0xff;
954 break;
956 /* else fall through */
958 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
959 AL_reg(context) = INT21_FindNextFCB(context) ? 0x00 : 0xff;
960 break;
962 case 0x13: /* DELETE FILE USING FCB */
963 DeleteFileFCB(context);
964 break;
966 case 0x17: /* RENAME FILE USING FCB */
967 RenameFileFCB(context);
968 break;
970 case 0x19: /* GET CURRENT DEFAULT DRIVE */
971 AL_reg(context) = DRIVE_GetCurrentDrive();
972 break;
974 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
976 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
977 pTask->dta = PTR_SEG_OFF_TO_SEGPTR(DS_reg(context),DX_reg(context));
978 TRACE(int21, "Set DTA: %08lx\n", pTask->dta);
980 break;
982 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
983 DL_reg(context) = 0;
984 if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
985 break;
987 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
988 if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
989 break;
991 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
992 GetDrivePB(context, DRIVE_GetCurrentDrive());
993 break;
995 case 0x25: /* SET INTERRUPT VECTOR */
996 INT_SetHandler( AL_reg(context),
997 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
998 DX_reg(context)));
999 break;
1001 case 0x2a: /* GET SYSTEM DATE */
1002 INT21_GetSystemDate(context);
1003 break;
1005 case 0x2b: /* SET SYSTEM DATE */
1006 fprintf( stdnimp, "SetSystemDate(%02d/%02d/%04d): not allowed\n",
1007 DL_reg(context), DH_reg(context), CX_reg(context) );
1008 AL_reg(context) = 0; /* Let's pretend we succeeded */
1009 break;
1011 case 0x2c: /* GET SYSTEM TIME */
1012 INT21_GetSystemTime(context);
1013 break;
1015 case 0x2d: /* SET SYSTEM TIME */
1016 fprintf( stdnimp, "SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1017 CH_reg(context), CL_reg(context),
1018 DH_reg(context), DL_reg(context) );
1019 AL_reg(context) = 0; /* Let's pretend we succeeded */
1020 break;
1022 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1024 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1025 ES_reg(context) = SELECTOROF( pTask->dta );
1026 BX_reg(context) = OFFSETOF( pTask->dta );
1028 break;
1030 case 0x30: /* GET DOS VERSION */
1031 AX_reg(context) = (HIWORD(GetVersion16()) >> 8) |
1032 (HIWORD(GetVersion16()) << 8);
1033 BX_reg(context) = 0x0012; /* 0x123456 is Wine's serial # */
1034 CX_reg(context) = 0x3456;
1035 break;
1037 case 0x31: /* TERMINATE AND STAY RESIDENT */
1038 INT_BARF( context, 0x21 );
1039 break;
1041 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1042 GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
1043 break;
1045 case 0x33: /* MULTIPLEXED */
1046 switch (AL_reg(context))
1048 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1049 DL_reg(context) = 0;
1050 break;
1052 case 0x01: /* SET EXTENDED BREAK STATE */
1053 break;
1055 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1056 DL_reg(context) = 0;
1057 break;
1059 case 0x05: /* GET BOOT DRIVE */
1060 DL_reg(context) = 3;
1061 /* c: is Wine's bootdrive (a: is 1)*/
1062 break;
1064 case 0x06: /* GET TRUE VERSION NUMBER */
1065 BX_reg(context) = (HIWORD(GetVersion16() >> 8)) |
1066 (HIWORD(GetVersion16() << 8));
1067 DX_reg(context) = 0x00;
1068 break;
1070 default:
1071 INT_BARF( context, 0x21 );
1072 break;
1074 break;
1076 case 0x34: /* GET ADDRESS OF INDOS FLAG */
1077 if (!heap) INT21_CreateHeap();
1078 ES_reg(context) = DosHeapHandle;
1079 BX_reg(context) = (int)&heap->InDosFlag - (int)heap;
1080 break;
1082 case 0x35: /* GET INTERRUPT VECTOR */
1084 FARPROC16 addr = INT_GetHandler( AL_reg(context) );
1085 ES_reg(context) = SELECTOROF(addr);
1086 BX_reg(context) = OFFSETOF(addr);
1088 break;
1090 case 0x36: /* GET FREE DISK SPACE */
1091 if (!INT21_GetFreeDiskSpace(context)) AX_reg(context) = 0xffff;
1092 break;
1094 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1095 AX_reg(context) = 0x02; /* no country support available */
1096 SET_CFLAG(context);
1097 break;
1099 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1100 bSetDOSExtendedError = (!CreateDirectory16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1101 DX_reg(context) ), NULL));
1102 break;
1104 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1105 bSetDOSExtendedError = (!RemoveDirectory16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1106 DX_reg(context) )));
1107 break;
1109 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1110 bSetDOSExtendedError = !INT21_ChangeDir(context);
1111 break;
1113 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1114 AX_reg(context) = _lcreat16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1115 DX_reg(context) ), CX_reg(context) );
1116 bSetDOSExtendedError = (AX_reg(context) == (WORD)HFILE_ERROR16);
1117 break;
1119 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1120 OpenExistingFile(context);
1121 break;
1123 case 0x3e: /* "CLOSE" - CLOSE FILE */
1124 bSetDOSExtendedError = ((AX_reg(context) = _lclose16( BX_reg(context) )) != 0);
1125 break;
1127 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1129 LONG result = WIN16_hread( BX_reg(context),
1130 PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1131 DX_reg(context) ),
1132 CX_reg(context) );
1133 if (result == -1) bSetDOSExtendedError = TRUE;
1134 else AX_reg(context) = (WORD)result;
1136 break;
1138 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1140 LONG result = _hwrite16( BX_reg(context),
1141 PTR_SEG_OFF_TO_LIN( DS_reg(context),
1142 DX_reg(context) ),
1143 CX_reg(context) );
1144 if (result == -1) bSetDOSExtendedError = TRUE;
1145 else AX_reg(context) = (WORD)result;
1147 break;
1149 case 0x41: /* "UNLINK" - DELETE FILE */
1150 bSetDOSExtendedError = (!DeleteFile32A( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1151 DX_reg(context) )));
1152 break;
1154 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1156 LONG status = _llseek16( BX_reg(context),
1157 MAKELONG(DX_reg(context),CX_reg(context)),
1158 AL_reg(context) );
1159 if (status == -1) bSetDOSExtendedError = TRUE;
1160 else
1162 AX_reg(context) = LOWORD(status);
1163 DX_reg(context) = HIWORD(status);
1166 break;
1168 case 0x43: /* FILE ATTRIBUTES */
1169 switch (AL_reg(context))
1171 case 0x00:
1172 AX_reg(context) = (WORD)GetFileAttributes32A(
1173 PTR_SEG_OFF_TO_LIN(DS_reg(context),
1174 DX_reg(context)));
1175 if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1176 else CX_reg(context) = AX_reg(context);
1177 break;
1179 case 0x01:
1180 bSetDOSExtendedError =
1181 (!SetFileAttributes32A( PTR_SEG_OFF_TO_LIN(DS_reg(context),
1182 DX_reg(context)),
1183 CX_reg(context) ));
1184 break;
1186 break;
1188 case 0x44: /* IOCTL */
1189 switch (AL_reg(context))
1191 case 0x00:
1192 ioctlGetDeviceInfo(context);
1193 break;
1195 case 0x01:
1196 break;
1197 case 0x05:{ /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1198 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context));
1199 int i;
1200 int drive = DOS_GET_DRIVE(BL_reg(context));
1202 fprintf(stdnimp,"program tried to write to block device control channel of drive %d:\n",drive);
1203 for (i=0;i<CX_reg(context);i++)
1204 fprintf(stdnimp,"%02x ",dataptr[i]);
1205 fprintf(stdnimp,"\n");
1206 AX_reg(context)=CX_reg(context);
1207 break;
1209 case 0x08: /* Check if drive is removable. */
1210 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1212 case DRIVE_CANNOTDETERMINE:
1213 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
1214 AX_reg(context) = ER_InvalidDrive;
1215 SET_CFLAG(context);
1216 break;
1217 case DRIVE_REMOVABLE:
1218 AX_reg(context) = 0; /* removable */
1219 break;
1220 default:
1221 AX_reg(context) = 1; /* not removable */
1222 break;
1224 break;
1226 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
1227 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1229 case DRIVE_CANNOTDETERMINE:
1230 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
1231 AX_reg(context) = ER_InvalidDrive;
1232 SET_CFLAG(context);
1233 break;
1234 case DRIVE_REMOTE:
1235 DX_reg(context) = (1<<9) | (1<<12); /* remote */
1236 break;
1237 default:
1238 DX_reg(context) = 0; /* FIXME: use driver attr here */
1239 break;
1241 break;
1243 case 0x0a: /* check if handle (BX) is remote */
1244 /* returns DX, bit 15 set if remote, bit 14 set if date/time
1245 * not set on close
1247 DX_reg(context) = 0;
1248 break;
1250 case 0x0b: /* SET SHARING RETRY COUNT */
1251 if (!CX_reg(context))
1253 AX_reg(context) = 1;
1254 SET_CFLAG(context);
1255 break;
1257 sharing_pause = CX_reg(context);
1258 if (!DX_reg(context))
1259 sharing_retries = DX_reg(context);
1260 RESET_CFLAG(context);
1261 break;
1263 case 0x0d:
1264 bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1265 break;
1267 case 0x0e: /* get logical drive mapping */
1268 AL_reg(context) = 0; /* drive has no mapping - FIXME: may be wrong*/
1269 break;
1271 case 0x0F: /* Set logical drive mapping */
1273 int drive;
1274 drive = DOS_GET_DRIVE ( BL_reg(context) );
1275 if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
1277 SET_CFLAG(context);
1278 AX_reg(context) = 0x000F; /* invalid drive */
1280 break;
1283 default:
1284 INT_BARF( context, 0x21 );
1285 break;
1287 break;
1289 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1290 bSetDOSExtendedError = ((AX_reg(context) = FILE_Dup(BX_reg(context))) == (WORD)HFILE_ERROR16);
1291 break;
1293 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1294 bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR32);
1295 break;
1297 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1298 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1299 break;
1301 case 0x48: /* ALLOCATE MEMORY */
1302 case 0x49: /* FREE MEMORY */
1303 case 0x4a: /* RESIZE MEMORY BLOCK */
1304 INT_BARF( context, 0x21 );
1305 break;
1307 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1308 AX_reg(context) = WinExec16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1309 DX_reg(context) ),
1310 SW_NORMAL );
1311 if (AX_reg(context) < 32) SET_CFLAG(context);
1312 break;
1314 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1315 TASK_KillCurrentTask( AL_reg(context) );
1316 break;
1318 case 0x4d: /* GET RETURN CODE */
1319 AX_reg(context) = 0; /* normal exit */
1320 break;
1322 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1323 if (!INT21_FindFirst(context)) break;
1324 /* fall through */
1326 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1327 if (!INT21_FindNext(context))
1329 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Disk );
1330 AX_reg(context) = ER_NoMoreFiles;
1331 SET_CFLAG(context);
1333 else AX_reg(context) = 0; /* OK */
1334 break;
1336 case 0x51: /* GET PSP ADDRESS */
1337 case 0x62: /* GET PSP ADDRESS */
1338 /* FIXME: should we return the original DOS PSP upon */
1339 /* Windows startup ? */
1340 BX_reg(context) = GetCurrentPDB();
1341 break;
1343 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1345 SEGPTR lol;
1346 lol = INT21_GetListOfLists();
1347 ES_reg(context) = HIWORD(lol);
1348 BX_reg(context) = LOWORD(lol);
1350 break;
1352 case 0x56: /* "RENAME" - RENAME FILE */
1353 bSetDOSExtendedError =
1354 (!MoveFile32A( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
1355 PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context))));
1356 break;
1358 case 0x57: /* FILE DATE AND TIME */
1359 switch (AL_reg(context))
1361 case 0x00: /* Get */
1363 FILETIME filetime;
1364 if (!GetFileTime( BX_reg(context), NULL, NULL, &filetime ))
1365 bSetDOSExtendedError = TRUE;
1366 else FileTimeToDosDateTime( &filetime, &DX_reg(context),
1367 &CX_reg(context) );
1369 break;
1371 case 0x01: /* Set */
1373 FILETIME filetime;
1374 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
1375 &filetime );
1376 bSetDOSExtendedError =
1377 (!SetFileTime( BX_reg(context), NULL, NULL, &filetime ));
1379 break;
1381 break;
1383 case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1384 switch (AL_reg(context))
1386 case 0x00:
1387 AX_reg(context) = 1;
1388 break;
1389 case 0x02:
1390 AX_reg(context) = 0;
1391 break;
1392 case 0x01:
1393 case 0x03:
1394 break;
1396 RESET_CFLAG(context);
1397 break;
1399 case 0x5a: /* CREATE TEMPORARY FILE */
1400 bSetDOSExtendedError = !INT21_CreateTempFile(context);
1401 break;
1403 case 0x5b: /* CREATE NEW FILE */
1404 bSetDOSExtendedError = ((AX_reg(context) =
1405 _lcreat_uniq( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)), 0 ))
1406 == (WORD)HFILE_ERROR16);
1407 break;
1409 case 0x5d: /* NETWORK */
1410 case 0x5e:
1411 /* network software not installed */
1412 DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
1413 bSetDOSExtendedError = TRUE;
1414 break;
1416 case 0x5f: /* NETWORK */
1417 switch (AL_reg(context))
1419 case 0x07: /* ENABLE DRIVE */
1420 if (!DRIVE_Enable( DL_reg(context) ))
1422 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
1423 bSetDOSExtendedError = TRUE;
1425 break;
1427 case 0x08: /* DISABLE DRIVE */
1428 if (!DRIVE_Disable( DL_reg(context) ))
1430 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
1431 bSetDOSExtendedError = TRUE;
1433 break;
1435 default:
1436 /* network software not installed */
1437 DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
1438 bSetDOSExtendedError = TRUE;
1439 break;
1441 break;
1443 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1445 if (!GetFullPathName32A( PTR_SEG_OFF_TO_LIN(DS_reg(context),
1446 SI_reg(context)), 128,
1447 PTR_SEG_OFF_TO_LIN(ES_reg(context),
1448 DI_reg(context)),NULL))
1449 bSetDOSExtendedError = TRUE;
1450 else AX_reg(context) = 0;
1452 break;
1454 case 0x61: /* UNUSED */
1455 case 0x63: /* UNUSED */
1456 case 0x64: /* OS/2 DOS BOX */
1457 INT_BARF( context, 0x21 );
1458 SET_CFLAG(context);
1459 break;
1461 case 0x65:{/* GET EXTENDED COUNTRY INFORMATION */
1462 extern WORD WINE_LanguageId;
1463 BYTE *dataptr=PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));;
1464 switch (AL_reg(context)) {
1465 case 0x01:
1466 dataptr[0] = 0x1;
1467 *(WORD*)(dataptr+1) = 41;
1468 *(WORD*)(dataptr+3) = WINE_LanguageId;
1469 *(WORD*)(dataptr+5) = CodePage;
1470 break;
1471 case 0x06:
1472 dataptr[0] = 0x06;
1473 *(DWORD*)(dataptr+1) = MAKELONG(DOSMEM_CollateTable & 0xFFFF,DOSMEM_AllocSelector(DOSMEM_CollateTable>>16));
1474 CX_reg(context) = 258;/*FIXME: size of table?*/
1475 break;
1476 default:
1477 INT_BARF( context, 0x21 );
1478 SET_CFLAG(context);
1479 break;
1481 break;
1483 case 0x66: /* GLOBAL CODE PAGE TABLE */
1484 switch (AL_reg(context))
1486 case 0x01:
1487 DX_reg(context) = BX_reg(context) = CodePage;
1488 RESET_CFLAG(context);
1489 break;
1490 case 0x02:
1491 CodePage = BX_reg(context);
1492 RESET_CFLAG(context);
1493 break;
1495 break;
1497 case 0x67: /* SET HANDLE COUNT */
1498 SetHandleCount16( BX_reg(context) );
1499 if (DOS_ExtendedError) bSetDOSExtendedError = TRUE;
1500 break;
1502 case 0x68: /* "FFLUSH" - COMMIT FILE */
1503 case 0x6a: /* COMMIT FILE */
1504 bSetDOSExtendedError = (!FlushFileBuffers( BX_reg(context) ));
1505 break;
1507 case 0x69: /* DISK SERIAL NUMBER */
1508 switch (AL_reg(context))
1510 case 0x00:
1511 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1512 else AX_reg(context) = 0;
1513 break;
1515 case 0x01:
1516 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1517 else AX_reg(context) = 1;
1518 break;
1520 break;
1522 case 0x6C: /* Extended Open/Create*/
1523 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1524 break;
1526 case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
1527 switch(AL_reg(context))
1529 case 0x39: /* Create directory */
1530 bSetDOSExtendedError = (!CreateDirectory32A(
1531 PTR_SEG_OFF_TO_LIN( DS_reg(context),
1532 DX_reg(context) ), NULL));
1533 break;
1534 case 0x3a: /* Remove directory */
1535 bSetDOSExtendedError = (!RemoveDirectory32A(
1536 PTR_SEG_OFF_TO_LIN( DS_reg(context),
1537 DX_reg(context) )));
1538 break;
1539 case 0x43: /* Get/Set file attributes */
1540 switch (BL_reg(context))
1542 case 0x00: /* Get file attributes */
1543 CX_reg(context) = (WORD)GetFileAttributes32A(
1544 PTR_SEG_OFF_TO_LIN(DS_reg(context),
1545 DX_reg(context)));
1546 if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1547 break;
1548 case 0x01:
1549 bSetDOSExtendedError = (!SetFileAttributes32A(
1550 PTR_SEG_OFF_TO_LIN(DS_reg(context),
1551 DX_reg(context)),
1552 CX_reg(context) ) );
1553 break;
1554 default:
1555 fprintf( stderr,
1556 "Unimplemented int21 long file name function:\n");
1557 INT_BARF( context, 0x21 );
1558 SET_CFLAG(context);
1559 AL_reg(context) = 0;
1560 break;
1562 break;
1563 case 0x47: /* Get current directory */
1564 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1565 break;
1567 case 0x4e: /* Find first file */
1568 /* FIXME: use attributes in CX */
1569 if ((AX_reg(context) = FindFirstFile16(
1570 PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
1571 (WIN32_FIND_DATA32A *)PTR_SEG_OFF_TO_LIN(ES_reg(context),
1572 DI_reg(context))))
1573 == INVALID_HANDLE_VALUE16)
1574 bSetDOSExtendedError = TRUE;
1575 break;
1576 case 0x4f: /* Find next file */
1577 if (!FindNextFile16( BX_reg(context),
1578 (WIN32_FIND_DATA32A *)PTR_SEG_OFF_TO_LIN(ES_reg(context),
1579 DI_reg(context))))
1580 bSetDOSExtendedError = TRUE;
1581 break;
1582 case 0xa1: /* Find close */
1583 bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
1584 break;
1585 case 0xa0:
1586 break;
1587 case 0x60:
1588 switch(CL_reg(context))
1590 case 0x02: /*Get canonical long filename or path */
1591 if (!GetFullPathName32A
1592 ( PTR_SEG_OFF_TO_LIN(DS_reg(context),
1593 SI_reg(context)), 128,
1594 PTR_SEG_OFF_TO_LIN(ES_reg(context),
1595 DI_reg(context)),NULL))
1596 bSetDOSExtendedError = TRUE;
1597 else AX_reg(context) = 0;
1598 break;
1599 default:
1600 fprintf( stderr,
1601 "Unimplemented int21 long file name function:\n");
1602 INT_BARF( context, 0x21 );
1603 SET_CFLAG(context);
1604 AL_reg(context) = 0;
1605 break;
1607 break;
1608 case 0x6c: /* Create or open file */
1609 /* translate Dos 7 action to Dos 6 action */
1610 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1611 break;
1613 case 0x3b: /* Change directory */
1614 if (!SetCurrentDirectory32A(PTR_SEG_OFF_TO_LIN(
1615 DS_reg(context),
1616 DX_reg(context)
1619 SET_CFLAG(context);
1620 AL_reg(context) = DOS_ExtendedError;
1622 break;
1623 case 0x41: /* Delete file */
1624 if (!DeleteFile32A(PTR_SEG_OFF_TO_LIN(
1625 DS_reg(context),
1626 DX_reg(context))
1627 )) {
1628 SET_CFLAG(context);
1629 AL_reg(context) = DOS_ExtendedError;
1631 break;
1632 case 0x56: /* Move (rename) file */
1633 default:
1634 fprintf( stderr, "Unimplemented int21 long file name function:\n");
1635 INT_BARF( context, 0x21 );
1636 SET_CFLAG(context);
1637 AL_reg(context) = 0;
1638 break;
1640 break;
1642 case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
1643 case 0x72: /* MS-DOS 7 (Windows95) - ??? */
1644 case 0x73: /* MS-DOS 7 (Windows95) - DRIVE LOCKING ??? */
1645 TRACE(int21,"windows95 function AX %04x\n",
1646 AX_reg(context));
1647 WARN(int21, " returning unimplemented\n");
1648 SET_CFLAG(context);
1649 AL_reg(context) = 0;
1650 break;
1652 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
1653 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
1654 break;
1656 default:
1657 INT_BARF( context, 0x21 );
1658 break;
1660 } /* END OF SWITCH */
1662 if( bSetDOSExtendedError ) /* set general error condition */
1664 AX_reg(context) = DOS_ExtendedError;
1665 SET_CFLAG(context);
1668 TRACE(int21, "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
1669 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1670 AX_reg(context), BX_reg(context), CX_reg(context),
1671 DX_reg(context), SI_reg(context), DI_reg(context),
1672 (WORD)DS_reg(context), (WORD)ES_reg(context),
1673 EFL_reg(context));
1676 FARPROC16 WINAPI GetSetKernelDOSProc(FARPROC16 DosProc)
1678 fprintf(stderr, "GetSetKernelDOSProc(DosProc: %08x);\n", (UINT32)DosProc);
1679 return NULL;