Release 970509
[wine.git] / msdos / int21.c
blob4af3a539597b8acf8bf34ec95561171bad65459b
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 "xmalloc.h"
28 #include "stddebug.h"
29 #include "debug.h"
30 #if defined(__svr4__) || defined(_SCO_DS)
31 /* SVR4 DOESNT do locking the same way must implement properly */
32 #define LOCK_EX 0
33 #define LOCK_SH 1
34 #define LOCK_NB 8
35 #endif
38 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
40 /* Define the drive parameter block, as used by int21/1F
41 * and int21/32. This table can be accessed through the
42 * global 'dpb' pointer, which points into the local dos
43 * heap.
45 struct DPB
47 BYTE drive_num; /* 0=A, etc. */
48 BYTE unit_num; /* Drive's unit number (?) */
49 WORD sector_size; /* Sector size in bytes */
50 BYTE high_sector; /* Highest sector in a cluster */
51 BYTE shift; /* Shift count (?) */
52 WORD reserved; /* Number of reserved sectors at start */
53 BYTE num_FAT; /* Number of FATs */
54 WORD dir_entries; /* Number of root dir entries */
55 WORD first_data; /* First data sector */
56 WORD high_cluster; /* Highest cluster number */
57 WORD sectors_in_FAT; /* Number of sectors per FAT */
58 WORD start_dir; /* Starting sector of first dir */
59 DWORD driver_head; /* Address of device driver header (?) */
60 BYTE media_ID; /* Media ID */
61 BYTE access_flag; /* Prev. accessed flag (0=yes,0xFF=no) */
62 DWORD next; /* Pointer to next DPB in list */
63 WORD free_search; /* Free cluster search start */
64 WORD free_clusters; /* Number of free clusters (0xFFFF=unknown) */
67 WORD CodePage = 437;
68 DWORD dpbsegptr;
70 struct DosHeap {
71 BYTE InDosFlag;
72 BYTE mediaID;
73 BYTE biosdate[8];
74 struct DPB dpb;
76 static struct DosHeap *heap;
77 static WORD DosHeapHandle;
79 WORD sharing_retries = 3; /* number of retries at sharing violation */
80 WORD sharing_pause = 1; /* pause between retries */
82 extern char TempDirectory[];
84 static BOOL32 INT21_CreateHeap(void)
86 if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
88 fprintf( stderr, "INT21_Init: Out of memory\n");
89 return FALSE;
91 heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
92 dpbsegptr = PTR_SEG_OFF_TO_SEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
93 heap->InDosFlag = 0;
94 strcpy(heap->biosdate, "01/01/80");
95 return TRUE;
98 BYTE *GetCurrentDTA(void)
100 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
101 return (BYTE *)PTR_SEG_TO_LIN( pTask->dta );
105 void ChopOffWhiteSpace(char *string)
107 int length;
109 for (length = strlen(string) ; length ; length--)
110 if (string[length] == ' ')
111 string[length] = '\0';
114 void CreateBPB(int drive, BYTE *data)
116 if (drive > 1) {
117 setword(data, 512);
118 data[2] = 2;
119 setword(&data[3], 0);
120 data[5] = 2;
121 setword(&data[6], 240);
122 setword(&data[8], 64000);
123 data[0x0a] = 0xf8;
124 setword(&data[0x0b], 40);
125 setword(&data[0x0d], 56);
126 setword(&data[0x0f], 2);
127 setword(&data[0x11], 0);
128 setword(&data[0x1f], 800);
129 data[0x21] = 5;
130 setword(&data[0x22], 1);
131 } else { /* 1.44mb */
132 setword(data, 512);
133 data[2] = 2;
134 setword(&data[3], 0);
135 data[5] = 2;
136 setword(&data[6], 240);
137 setword(&data[8], 2880);
138 data[0x0a] = 0xf8;
139 setword(&data[0x0b], 6);
140 setword(&data[0x0d], 18);
141 setword(&data[0x0f], 2);
142 setword(&data[0x11], 0);
143 setword(&data[0x1f], 80);
144 data[0x21] = 7;
145 setword(&data[0x22], 2);
149 static int INT21_GetFreeDiskSpace( CONTEXT *context )
151 DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
152 char root[] = "A:\\";
154 *root += DOS_GET_DRIVE( DL_reg(context) );
155 if (!GetDiskFreeSpace32A( root, &cluster_sectors, &sector_bytes,
156 &free_clusters, &total_clusters )) return 0;
157 AX_reg(context) = cluster_sectors;
158 BX_reg(context) = free_clusters;
159 CX_reg(context) = sector_bytes;
160 DX_reg(context) = total_clusters;
161 return 1;
164 static int INT21_GetDriveAllocInfo( CONTEXT *context )
166 if (!INT21_GetFreeDiskSpace( context )) return 0;
167 if (!heap && !INT21_CreateHeap()) return 0;
168 heap->mediaID = 0xf0;
169 DS_reg(context) = DosHeapHandle;
170 BX_reg(context) = (int)&heap->mediaID - (int)heap;
171 return 1;
174 static void GetDrivePB( CONTEXT *context, int drive )
176 if(!DRIVE_IsValid(drive))
178 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
179 AX_reg(context) = 0x00ff;
181 else if (heap || INT21_CreateHeap())
183 dprintf_int(stddeb, "int21: GetDrivePB not fully implemented.\n");
185 /* FIXME: I have no idea what a lot of this information should
186 * say or whether it even really matters since we're not allowing
187 * direct block access. However, some programs seem to depend on
188 * getting at least _something_ back from here. The 'next' pointer
189 * does worry me, though. Should we have a complete table of
190 * separate DPBs per drive? Probably, but I'm lazy. :-) -CH
192 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
193 heap->dpb.sector_size = 512;
194 heap->dpb.high_sector = 1;
195 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
196 heap->dpb.reserved = 0;
197 heap->dpb.num_FAT = 1;
198 heap->dpb.dir_entries = 2;
199 heap->dpb.first_data = 2;
200 heap->dpb.high_cluster = 64000;
201 heap->dpb.sectors_in_FAT = 1;
202 heap->dpb.start_dir = 1;
203 heap->dpb.driver_head = 0;
204 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
205 heap->dpb.access_flag = 0;
206 heap->dpb.next = 0;
207 heap->dpb.free_search = 0;
208 heap->dpb.free_clusters = 0xFFFF; /* unknown */
210 AL_reg(context) = 0x00;
211 DS_reg(context) = SELECTOROF(dpbsegptr);
212 BX_reg(context) = OFFSETOF(dpbsegptr);
217 static void ioctlGetDeviceInfo( CONTEXT *context )
219 dprintf_int (stddeb, "int21: ioctl (%d, GetDeviceInfo)\n", BX_reg(context));
221 DX_reg(context) = 0x0942;
222 /* bits 0-5 are current drive
223 * bit 6 - file has NOT been written..FIXME: correct?
224 * bit 8 - generate int24 if no diskspace on write/ read past end of file
225 * bit 11 - media not removable
227 RESET_CFLAG(context);
230 static BOOL32 ioctlGenericBlkDevReq( CONTEXT *context )
232 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
233 int drive = DOS_GET_DRIVE( BL_reg(context) );
235 if (!DRIVE_IsValid(drive))
237 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
238 return TRUE;
241 if (CH_reg(context) != 0x08)
243 INT_BARF( context, 0x21 );
244 return FALSE;
247 switch (CL_reg(context))
249 case 0x4a: /* lock logical volume */
250 dprintf_int(stddeb,"int21: lock logical volume (%d) level %d mode %d\n",drive,BH_reg(context),DX_reg(context));
251 break;
253 case 0x60: /* get device parameters */
254 /* used by w4wgrp's winfile */
255 memset(dataptr, 0, 0x26);
256 dataptr[0] = 0x04;
257 dataptr[6] = 0; /* media type */
258 if (drive > 1)
260 dataptr[1] = 0x05; /* fixed disk */
261 setword(&dataptr[2], 0x01); /* non removable */
262 setword(&dataptr[4], 0x300); /* # of cylinders */
264 else
266 dataptr[1] = 0x07; /* block dev, floppy */
267 setword(&dataptr[2], 0x02); /* removable */
268 setword(&dataptr[4], 80); /* # of cylinders */
270 CreateBPB(drive, &dataptr[7]);
271 RESET_CFLAG(context);
272 break;
274 case 0x66:/* get disk serial number */
276 char label[12],fsname[9],path[4];
277 DWORD serial;
279 strcpy(path,"x:\\");path[0]=drive+'A';
280 GetVolumeInformation32A(
281 path,label,12,&serial,NULL,NULL,fsname,9
283 *(WORD*)dataptr = 0;
284 memcpy(dataptr+2,&serial,4);
285 memcpy(dataptr+6,label ,11);
286 memcpy(dataptr+17,fsname,8);
288 break;
290 case 0x6a:
291 dprintf_int(stddeb,"int21: logical volume %d unlocked.\n",drive);
292 break;
294 default:
295 INT_BARF( context, 0x21 );
297 return FALSE;
300 static void INT21_GetSystemDate( CONTEXT *context )
302 SYSTEMTIME systime;
303 GetLocalTime( &systime );
304 CX_reg(context) = systime.wYear;
305 DX_reg(context) = (systime.wMonth << 8) | systime.wDay;
306 AX_reg(context) = systime.wDayOfWeek;
309 static void INT21_GetSystemTime( CONTEXT *context )
311 SYSTEMTIME systime;
312 GetLocalTime( &systime );
313 CX_reg(context) = (systime.wHour << 8) | systime.wMinute;
314 DX_reg(context) = (systime.wSecond << 8) | (systime.wMilliseconds / 10);
317 static BOOL32 INT21_CreateFile( CONTEXT *context )
319 AX_reg(context) = _lcreat16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
320 DX_reg(context) ), CX_reg(context) );
321 return (AX_reg(context) == (WORD)HFILE_ERROR16);
325 void OpenExistingFile( CONTEXT *context )
327 AX_reg(context) = _lopen16( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
328 AL_reg(context) );
329 if (AX_reg(context) == (WORD)HFILE_ERROR16)
331 AX_reg(context) = DOS_ExtendedError;
332 SET_CFLAG(context);
334 #if 0
335 int handle;
336 int mode;
337 int lock;
339 switch (AX_reg(context) & 0x0070)
341 case 0x00: /* compatability mode */
342 case 0x40: /* DENYNONE */
343 lock = -1;
344 break;
346 case 0x30: /* DENYREAD */
347 dprintf_int(stddeb,
348 "OpenExistingFile (%s): DENYREAD changed to DENYALL\n",
349 (char *)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)));
350 case 0x10: /* DENYALL */
351 lock = LOCK_EX;
352 break;
354 case 0x20: /* DENYWRITE */
355 lock = LOCK_SH;
356 break;
358 default:
359 lock = -1;
362 if (lock != -1)
365 int result,retries=sharing_retries;
367 #if defined(__svr4__) || defined(_SCO_DS)
368 printf("Should call flock and needs porting to lockf\n");
369 result = 0;
370 retries = 0;
371 #else
372 result = flock(handle, lock | LOCK_NB);
373 #endif
374 if ( retries && (!result) )
376 int i;
377 for(i=0;i<32768*((int)sharing_pause);i++)
378 result++; /* stop the optimizer */
379 for(i=0;i<32768*((int)sharing_pause);i++)
380 result--;
383 while( (!result) && (!(retries--)) );
385 if(result)
387 errno_to_doserr();
388 AX_reg(context) = ExtendedError;
389 close(handle);
390 SET_CFLAG(context);
391 return;
396 Error (0,0,0);
397 AX_reg(context) = handle;
398 RESET_CFLAG(context);
399 #endif
402 static void CloseFile( CONTEXT *context )
404 if ((AX_reg(context) = _lclose16( BX_reg(context) )) != 0)
406 AX_reg(context) = DOS_ExtendedError;
407 SET_CFLAG(context);
411 static BOOL32 INT21_ExtendedOpenCreateFile(CONTEXT *context )
413 BOOL32 bExtendedError = FALSE;
414 BYTE action = DL_reg(context);
416 /* Shuffle arguments to call OpenExistingFile */
417 AL_reg(context) = BL_reg(context);
418 DX_reg(context) = SI_reg(context);
419 /* BX,CX and DX should be preserved */
420 OpenExistingFile(context);
422 if ((EFL_reg(context) & 0x0001) == 0) /* File exists */
424 UINT16 uReturnCX = 0;
426 /* Now decide what do do */
428 if ((action & 0x07) == 0)
430 BX_reg(context) = AX_reg(context);
431 CloseFile(context);
432 AX_reg(context) = 0x0050; /*File exists*/
433 SET_CFLAG(context);
434 dprintf_int(stddeb, "int21: extended open/create: failed because file exists \n");
436 else if ((action & 0x07) == 2)
438 /* Truncate it, but first check if opened for write */
439 if ((BL_reg(context) & 0x0007)== 0)
441 BX_reg(context) = AX_reg(context);
442 CloseFile(context);
443 dprintf_int(stddeb, "int21: extended open/create: failed, trunc on ro file");
444 AX_reg(context) = 0x000C; /*Access code invalid*/
445 SET_CFLAG(context);
447 else
449 /* Shuffle arguments to call CloseFile while
450 * preserving BX and DX */
452 dprintf_int(stddeb, "int21: extended open/create: Closing before truncate\n");
453 BX_reg(context) = AX_reg(context);
454 CloseFile(context);
455 if (EFL_reg(context) & 0x0001)
457 dprintf_int(stddeb, "int21: extended open/create: close before trunc failed");
458 AX_reg(context) = 0x0019; /*Seek Error*/
459 CX_reg(context) = 0;
460 SET_CFLAG(context);
462 /* Shuffle arguments to call CreateFile */
464 dprintf_int(stddeb, "int21: extended open/create: Truncating\n");
465 AL_reg(context) = BL_reg(context);
466 /* CX is still the same */
467 DX_reg(context) = SI_reg(context);
468 bExtendedError = INT21_CreateFile(context);
470 if (EFL_reg(context) & 0x0001) /*no file open, flags set */
472 dprintf_int(stddeb, "int21: extended open/create: trunc failed");
473 return bExtendedError;
475 uReturnCX = 0x3;
478 else uReturnCX = 0x1;
480 CX_reg(context) = uReturnCX;
482 else /* file does not exist */
484 RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
485 if ((action & 0xF0)== 0)
487 CX_reg(context) = 0;
488 SET_CFLAG(context);
489 dprintf_int(stddeb, "int21: extended open/create: failed, file dosen't exist\n");
491 else
493 /* Shuffle arguments to call CreateFile */
494 dprintf_int(stddeb, "int21: extended open/create: Creating\n");
495 AL_reg(context) = BL_reg(context);
496 /* CX should still be the same */
497 DX_reg(context) = SI_reg(context);
498 bExtendedError = INT21_CreateFile(context);
499 if (EFL_reg(context) & 0x0001) /*no file open, flags set */
501 dprintf_int(stddeb, "int21: extended open/create: create failed\n");
502 return bExtendedError;
504 CX_reg(context) = 2;
508 return bExtendedError;
512 static BOOL32 INT21_ChangeDir( CONTEXT *context )
514 int drive;
515 char *dirname = PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context));
517 dprintf_int(stddeb,"int21: changedir %s\n", dirname);
518 if (dirname[0] && (dirname[1] == ':'))
520 drive = toupper(dirname[0]) - 'A';
521 dirname += 2;
523 else drive = DRIVE_GetCurrentDrive();
524 return DRIVE_Chdir( drive, dirname );
528 static int INT21_FindFirst( CONTEXT *context )
530 char *p;
531 const char *path;
532 DOS_FULL_NAME full_name;
533 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA();
535 path = (const char *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
536 dta->unixPath = NULL;
537 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
539 AX_reg(context) = DOS_ExtendedError;
540 SET_CFLAG(context);
541 return 0;
543 dta->unixPath = xstrdup( full_name.long_name );
544 p = strrchr( dta->unixPath, '/' );
545 *p = '\0';
547 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
548 * (doesn't matter as it is set below anyway)
550 if (!DOSFS_ToDosFCBFormat( p + 1, dta->mask ))
552 free( dta->unixPath );
553 dta->unixPath = NULL;
554 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
555 AX_reg(context) = ER_FileNotFound;
556 SET_CFLAG(context);
557 return 0;
559 dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
560 : DRIVE_GetCurrentDrive();
561 dta->count = 0;
562 dta->search_attr = CL_reg(context);
563 return 1;
567 static int INT21_FindNext( CONTEXT *context )
569 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA();
570 WIN32_FIND_DATA32A entry;
571 int count;
573 if (!dta->unixPath) return 0;
574 if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
575 dta->search_attr, dta->count, &entry )))
577 free( dta->unixPath );
578 dta->unixPath = NULL;
579 return 0;
581 if ((int)dta->count + count > 0xffff)
583 fprintf( stderr, "Too many directory entries in %s\n", dta->unixPath );
584 free( dta->unixPath );
585 dta->unixPath = NULL;
586 return 0;
588 dta->count += count;
589 dta->fileattr = entry.dwFileAttributes;
590 dta->filesize = entry.nFileSizeLow;
591 FileTimeToDosDateTime( &entry.ftLastWriteTime,
592 &dta->filedate, &dta->filetime );
593 strcpy( dta->filename, entry.cAlternateFileName );
594 return 1;
598 static BOOL32 INT21_CreateTempFile( CONTEXT *context )
600 static int counter = 0;
601 char *name = PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context) );
602 char *p = name + strlen(name);
604 for (;;)
606 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
607 counter = (counter + 1) % 1000;
609 if ((AX_reg(context) = _lcreat_uniq( name, 0 )) != (WORD)HFILE_ERROR16)
611 dprintf_int( stddeb, "INT21_CreateTempFile: created %s\n", name );
612 return TRUE;
614 if (DOS_ExtendedError != ER_FileExists) return FALSE;
619 static BOOL32 INT21_GetCurrentDirectory( CONTEXT *context )
621 int drive = DOS_GET_DRIVE( DL_reg(context) );
622 char *ptr = (char *)PTR_SEG_OFF_TO_LIN( DS_reg(context), SI_reg(context) );
624 if (!DRIVE_IsValid(drive))
626 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
627 return FALSE;
629 lstrcpyn32A( ptr, DRIVE_GetDosCwd(drive), 64 );
630 AX_reg(context) = 0x0100; /* success return code */
631 return TRUE;
635 static int INT21_GetDiskSerialNumber( CONTEXT *context )
637 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
638 int drive = DOS_GET_DRIVE( BL_reg(context) );
640 if (!DRIVE_IsValid(drive))
642 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
643 return 0;
646 *(WORD *)dataptr = 0;
647 *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
648 memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
649 strncpy(dataptr + 0x11, "FAT16 ", 8);
650 return 1;
654 static int INT21_SetDiskSerialNumber( CONTEXT *context )
656 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
657 int drive = DOS_GET_DRIVE( BL_reg(context) );
659 if (!DRIVE_IsValid(drive))
661 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
662 return 0;
665 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
666 return 1;
670 /* microsoft's programmers should be shot for using CP/M style int21
671 calls in Windows for Workgroup's winfile.exe */
673 static int INT21_FindFirstFCB( CONTEXT *context )
675 BYTE *fcb = (BYTE *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
676 FINDFILE_FCB *pFCB;
677 LPCSTR root, cwd;
678 int drive;
680 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
681 else pFCB = (FINDFILE_FCB *)fcb;
682 drive = DOS_GET_DRIVE( pFCB->drive );
683 root = DRIVE_GetRoot( drive );
684 cwd = DRIVE_GetUnixCwd( drive );
685 pFCB->unixPath = HeapAlloc( SystemHeap, 0, strlen(root)+strlen(cwd)+2 );
686 if (!pFCB->unixPath) return 0;
687 strcpy( pFCB->unixPath, root );
688 strcat( pFCB->unixPath, "/" );
689 strcat( pFCB->unixPath, cwd );
690 pFCB->count = 0;
691 return 1;
695 static int INT21_FindNextFCB( CONTEXT *context )
697 BYTE *fcb = (BYTE *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
698 FINDFILE_FCB *pFCB;
699 DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA();
700 WIN32_FIND_DATA32A entry;
701 BYTE attr;
702 int count;
704 if (*fcb == 0xff)
706 attr = fcb[6];
707 pFCB = (FINDFILE_FCB *)(fcb + 7);
709 else
711 attr = 0;
712 pFCB = (FINDFILE_FCB *)fcb;
715 if (!pFCB->unixPath) return 0;
716 if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
717 DOS_GET_DRIVE( pFCB->drive ), attr,
718 pFCB->count, &entry )))
720 HeapFree( SystemHeap, 0, pFCB->unixPath );
721 pFCB->unixPath = NULL;
722 return 0;
724 pFCB->count += count;
726 pResult->fileattr = entry.dwFileAttributes;
727 pResult->cluster = 0; /* what else? */
728 pResult->filesize = entry.nFileSizeLow;
729 memset( pResult->reserved, 0, sizeof(pResult->reserved) );
730 FileTimeToDosDateTime( &entry.ftLastWriteTime,
731 &pResult->filedate, &pResult->filetime );
733 /* Convert file name to FCB format */
735 memset( pResult->filename, ' ', sizeof(pResult->filename) );
736 if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
737 else if (!strcmp( entry.cAlternateFileName, ".." ))
738 pResult->filename[0] = pResult->filename[1] = '.';
739 else
741 char *p = strrchr( entry.cAlternateFileName, '.' );
742 if (p && p[1] && (p != entry.cAlternateFileName))
744 memcpy( pResult->filename, entry.cAlternateFileName,
745 MIN( (p - entry.cAlternateFileName), 8 ) );
746 memcpy( pResult->filename + 8, p + 1, MIN( strlen(p), 3 ) );
748 else
749 memcpy( pResult->filename, entry.cAlternateFileName,
750 MIN( strlen(entry.cAlternateFileName), 8 ) );
752 return 1;
756 static void DeleteFileFCB( CONTEXT *context )
758 fprintf( stderr, "DeleteFileFCB: not implemented yet\n" );
759 #if 0
760 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
761 struct dosdirent *dp;
762 char temp[256], *ptr;
763 int drive = DOS_GET_DRIVE( *fcb );
765 DumpFCB( fcb );
767 temp[0] = '\\';
768 strcpy(temp+1, DRIVE_GetDosCwd(drive));
769 strcat(temp, "\\");
770 strncat(temp, fcb + 1, 8);
771 ChopOffWhiteSpace(temp);
772 strncat(temp, fcb + 9, 3);
773 ChopOffWhiteSpace(temp);
775 if ((dp = DOS_opendir(temp)) == NULL) {
776 Error(InvalidDrive, EC_MediaError , EL_Disk);
777 AX_reg(context) = 0xff;
778 return;
781 temp[0] = '\\';
782 strcpy(temp+1, DRIVE_GetDosCwd(drive) );
783 strcat(temp, "\\");
785 ptr = temp + strlen(temp);
787 while (DOS_readdir(dp) != NULL)
789 strcpy(ptr, dp->filename);
790 dprintf_int(stddeb, "int21: delete file %s\n", temp);
791 /* unlink(DOS_GetUnixFileName(temp)); */
793 DOS_closedir(dp);
794 AX_reg(context) = 0;
795 #endif
798 static void RenameFileFCB( CONTEXT *context )
800 fprintf( stderr, "RenameFileFCB: not implemented yet\n" );
801 #if 0
802 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
803 struct dosdirent *dp;
804 char temp[256], oldname[256], newname[256], *oldnameptr, *newnameptr;
805 int drive = DOS_GET_DRIVE( *fcb );
807 DumpFCB( fcb );
809 temp[0] = '\\';
810 strcpy(temp+1, DRIVE_GetDosCwd(drive) );
811 strcat(temp, "\\");
812 strncat(temp, fcb + 1, 8);
813 ChopOffWhiteSpace(temp);
814 strncat(temp, fcb + 9, 3);
815 ChopOffWhiteSpace(temp);
817 if ((dp = DOS_opendir(temp)) == NULL) {
818 Error(InvalidDrive, EC_MediaError , EL_Disk);
819 AX_reg(context) = 0xff;
820 return;
823 oldname[0] = '\\';
824 strcpy(oldname+1, DRIVE_GetDosCwd(drive) );
825 strcat(oldname, "\\");
826 strcpy( newname, oldname );
827 oldnameptr = oldname + strlen(oldname);
828 newnameptr = newname + strlen(newname);
830 while (DOS_readdir(dp) != NULL)
832 strcpy(oldnameptr, dp->filename);
833 strcpy(newnameptr, fcb + 1);
834 dprintf_int(stddeb, "int21: renamefile %s -> %s\n",
835 oldname, newname);
837 DOS_closedir(dp);
838 AX_reg(context) = 0;
839 #endif
844 static void fLock( CONTEXT * context )
846 #if 0
847 struct flock f;
848 int result,retries=sharing_retries;
850 f.l_start = MAKELONG(DX_reg(context),CX_reg(context));
851 f.l_len = MAKELONG(DI_reg(context),SI_reg(context));
852 f.l_whence = 0;
853 f.l_pid = 0;
855 switch ( AX_reg(context) & 0xff )
857 case 0x00: /* LOCK */
858 f.l_type = F_WRLCK;
859 break;
861 case 0x01: /* UNLOCK */
862 f.l_type = F_UNLCK;
863 break;
865 default:
866 AX_reg(context) = 0x0001;
867 SET_CFLAG(context);
868 return;
872 result = fcntl(BX_reg(context),F_SETLK,&f);
873 if ( retries && (!result) )
875 int i;
876 for(i=0;i<32768*((int)sharing_pause);i++)
877 result++; /* stop the optimizer */
878 for(i=0;i<32768*((int)sharing_pause);i++)
879 result--;
882 while( (!result) && (!(retries--)) );
884 if(result)
886 FILE_SetDosError();
887 AX_reg(context) = DOS_ExtendedError;
888 SET_CFLAG(context);
889 return;
891 #endif
895 extern void LOCAL_PrintHeap (WORD ds);
897 /***********************************************************************
898 * DOS3Call (KERNEL.102)
900 void DOS3Call( CONTEXT *context )
902 BOOL32 bSetDOSExtendedError = FALSE;
904 dprintf_int( stddeb, "int21: AX=%04x BX=%04x CX=%04x DX=%04x "
905 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
906 AX_reg(context), BX_reg(context), CX_reg(context),
907 DX_reg(context), SI_reg(context), DI_reg(context),
908 (WORD)DS_reg(context), (WORD)ES_reg(context),
909 EFL_reg(context) );
911 if (AH_reg(context) == 0x59) /* Get extended error info */
913 AX_reg(context) = DOS_ExtendedError;
914 BH_reg(context) = DOS_ErrorClass;
915 BL_reg(context) = DOS_ErrorAction;
916 CH_reg(context) = DOS_ErrorLocus;
917 return;
920 DOS_ERROR( 0, 0, 0, 0 );
921 RESET_CFLAG(context); /* Not sure if this is a good idea */
923 switch(AH_reg(context))
925 case 0x00: /* TERMINATE PROGRAM */
926 TASK_KillCurrentTask( 0 );
927 break;
929 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
930 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
931 case 0x03: /* READ CHARACTER FROM STDAUX */
932 case 0x04: /* WRITE CHARACTER TO STDAUX */
933 case 0x05: /* WRITE CHARACTER TO PRINTER */
934 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
935 case 0x07: /* DIRECT CHARACTER INPUT, WITHOUT ECHO */
936 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
937 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
938 case 0x0a: /* BUFFERED INPUT */
939 case 0x0b: /* GET STDIN STATUS */
940 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
941 case 0x0f: /* OPEN FILE USING FCB */
942 case 0x10: /* CLOSE FILE USING FCB */
943 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
944 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
945 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
946 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
947 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
948 case 0x23: /* GET FILE SIZE FOR FCB */
949 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
950 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
951 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
952 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
953 case 0x29: /* PARSE FILENAME INTO FCB */
954 case 0x2e: /* SET VERIFY FLAG */
955 case 0x37: /* "SWITCHAR" - GET SWITCH CHARACTER
956 "SWITCHAR" - SET SWITCH CHARACTER
957 "AVAILDEV" - SPECIFY \DEV\ PREFIX USE */
958 case 0x54: /* GET VERIFY FLAG */
959 INT_BARF( context, 0x21 );
960 break;
962 case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
963 case 0x1d:
964 case 0x1e:
965 case 0x20:
966 case 0x6b: /* NULL FUNCTION */
967 AL_reg(context) = 0;
968 break;
970 case 0x5c: /* "FLOCK" - RECORD LOCKING */
971 fLock(context);
972 break;
974 case 0x0d: /* DISK BUFFER FLUSH */
975 RESET_CFLAG(context); /* dos 6+ only */
976 break;
978 case 0x0e: /* SELECT DEFAULT DRIVE */
979 DRIVE_SetCurrentDrive( DL_reg(context) );
980 AL_reg(context) = MAX_DOS_DRIVES;
981 break;
983 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
984 if (!INT21_FindFirstFCB(context))
986 AL_reg(context) = 0xff;
987 break;
989 /* else fall through */
991 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
992 AL_reg(context) = INT21_FindNextFCB(context) ? 0x00 : 0xff;
993 break;
995 case 0x13: /* DELETE FILE USING FCB */
996 DeleteFileFCB(context);
997 break;
999 case 0x17: /* RENAME FILE USING FCB */
1000 RenameFileFCB(context);
1001 break;
1003 case 0x19: /* GET CURRENT DEFAULT DRIVE */
1004 AL_reg(context) = DRIVE_GetCurrentDrive();
1005 break;
1007 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
1009 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1010 pTask->dta = PTR_SEG_OFF_TO_SEGPTR(DS_reg(context),DX_reg(context));
1011 dprintf_int(stddeb, "int21: Set DTA: %08lx\n", pTask->dta);
1013 break;
1015 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
1016 DL_reg(context) = 0;
1017 if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1018 break;
1020 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1021 if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1022 break;
1024 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1025 GetDrivePB(context, DRIVE_GetCurrentDrive());
1026 break;
1028 case 0x25: /* SET INTERRUPT VECTOR */
1029 INT_SetHandler( AL_reg(context),
1030 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1031 DX_reg(context)));
1032 break;
1034 case 0x2a: /* GET SYSTEM DATE */
1035 INT21_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) = 3;
1093 /* c: is Wine's bootdrive (a: is 1)*/
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 if (!heap) INT21_CreateHeap();
1109 ES_reg(context) = DosHeapHandle;
1110 BX_reg(context) = (int)&heap->InDosFlag - (int)heap;
1111 break;
1113 case 0x35: /* GET INTERRUPT VECTOR */
1115 FARPROC16 addr = INT_GetHandler( AL_reg(context) );
1116 ES_reg(context) = SELECTOROF(addr);
1117 BX_reg(context) = OFFSETOF(addr);
1119 break;
1121 case 0x36: /* GET FREE DISK SPACE */
1122 if (!INT21_GetFreeDiskSpace(context)) AX_reg(context) = 0xffff;
1123 break;
1125 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1126 AX_reg(context) = 0x02; /* no country support available */
1127 SET_CFLAG(context);
1128 break;
1130 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1131 bSetDOSExtendedError = (!CreateDirectory16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1132 DX_reg(context) ), NULL));
1133 break;
1135 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1136 bSetDOSExtendedError = (!RemoveDirectory16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1137 DX_reg(context) )));
1138 break;
1140 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1141 bSetDOSExtendedError = !INT21_ChangeDir(context);
1142 break;
1144 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1145 AX_reg(context) = _lcreat16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1146 DX_reg(context) ), CX_reg(context) );
1147 bSetDOSExtendedError = (AX_reg(context) == (WORD)HFILE_ERROR16);
1148 break;
1150 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1151 OpenExistingFile(context);
1152 break;
1154 case 0x3e: /* "CLOSE" - CLOSE FILE */
1155 bSetDOSExtendedError = ((AX_reg(context) = _lclose16( BX_reg(context) )) != 0);
1156 break;
1158 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1160 LONG result = WIN16_hread( BX_reg(context),
1161 PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1162 DX_reg(context) ),
1163 CX_reg(context) );
1164 if (result == -1) bSetDOSExtendedError = TRUE;
1165 else AX_reg(context) = (WORD)result;
1167 break;
1169 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1171 LONG result = _hwrite16( BX_reg(context),
1172 PTR_SEG_OFF_TO_LIN( DS_reg(context),
1173 DX_reg(context) ),
1174 CX_reg(context) );
1175 if (result == -1) bSetDOSExtendedError = TRUE;
1176 else AX_reg(context) = (WORD)result;
1178 break;
1180 case 0x41: /* "UNLINK" - DELETE FILE */
1181 bSetDOSExtendedError = (!DeleteFile32A( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1182 DX_reg(context) )));
1183 break;
1185 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1187 LONG status = _llseek16( BX_reg(context),
1188 MAKELONG(DX_reg(context),CX_reg(context)),
1189 AL_reg(context) );
1190 if (status == -1) bSetDOSExtendedError = TRUE;
1191 else
1193 AX_reg(context) = LOWORD(status);
1194 DX_reg(context) = HIWORD(status);
1197 break;
1199 case 0x43: /* FILE ATTRIBUTES */
1200 switch (AL_reg(context))
1202 case 0x00:
1203 AX_reg(context) = (WORD)GetFileAttributes32A(
1204 PTR_SEG_OFF_TO_LIN(DS_reg(context),
1205 DX_reg(context)));
1206 if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1207 else CX_reg(context) = AX_reg(context);
1208 break;
1210 case 0x01:
1211 bSetDOSExtendedError =
1212 (!SetFileAttributes32A( PTR_SEG_OFF_TO_LIN(DS_reg(context),
1213 DX_reg(context)),
1214 CX_reg(context) ));
1215 break;
1217 break;
1219 case 0x44: /* IOCTL */
1220 switch (AL_reg(context))
1222 case 0x00:
1223 ioctlGetDeviceInfo(context);
1224 break;
1226 case 0x01:
1227 break;
1228 case 0x05:{ /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1229 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context));
1230 int i;
1231 int drive = DOS_GET_DRIVE(BL_reg(context));
1233 fprintf(stdnimp,"int21: program tried to write to block device control channel of drive %d:\n",drive);
1234 for (i=0;i<CX_reg(context);i++)
1235 fprintf(stdnimp,"%02x ",dataptr[i]);
1236 fprintf(stdnimp,"\n");
1237 AX_reg(context)=CX_reg(context);
1238 break;
1240 case 0x08: /* Check if drive is removable. */
1241 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1243 case DRIVE_CANNOTDETERMINE:
1244 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
1245 AX_reg(context) = ER_InvalidDrive;
1246 SET_CFLAG(context);
1247 break;
1248 case DRIVE_REMOVABLE:
1249 AX_reg(context) = 0; /* removable */
1250 break;
1251 default:
1252 AX_reg(context) = 1; /* not removable */
1253 break;
1255 break;
1257 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
1258 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1260 case DRIVE_CANNOTDETERMINE:
1261 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
1262 AX_reg(context) = ER_InvalidDrive;
1263 SET_CFLAG(context);
1264 break;
1265 case DRIVE_REMOTE:
1266 DX_reg(context) = (1<<9) | (1<<12); /* remote */
1267 break;
1268 default:
1269 DX_reg(context) = 0; /* FIXME: use driver attr here */
1270 break;
1272 break;
1274 case 0x0a: /* check if handle (BX) is remote */
1275 /* returns DX, bit 15 set if remote, bit 14 set if date/time
1276 * not set on close
1278 DX_reg(context) = 0;
1279 break;
1281 case 0x0b: /* SET SHARING RETRY COUNT */
1282 if (!CX_reg(context))
1284 AX_reg(context) = 1;
1285 SET_CFLAG(context);
1286 break;
1288 sharing_pause = CX_reg(context);
1289 if (!DX_reg(context))
1290 sharing_retries = DX_reg(context);
1291 RESET_CFLAG(context);
1292 break;
1294 case 0x0d:
1295 bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1296 break;
1298 case 0x0e: /* get logical drive mapping */
1299 AL_reg(context) = 0; /* drive has no mapping */
1300 break;
1302 case 0x0F: /* Set logical drive mapping */
1303 /* FIXME: Not implemented at the moment, always returns error
1305 INT_BARF( context, 0x21 );
1306 AX_reg(context) = 0x0001; /* invalid function */
1307 SET_CFLAG(context);
1308 break;
1310 default:
1311 INT_BARF( context, 0x21 );
1312 break;
1314 break;
1316 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1317 bSetDOSExtendedError = ((AX_reg(context) = FILE_Dup(BX_reg(context))) == (WORD)HFILE_ERROR16);
1318 break;
1320 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1321 bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR32);
1322 break;
1324 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1325 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1326 break;
1328 case 0x48: /* ALLOCATE MEMORY */
1329 case 0x49: /* FREE MEMORY */
1330 case 0x4a: /* RESIZE MEMORY BLOCK */
1331 INT_BARF( context, 0x21 );
1332 break;
1334 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1335 AX_reg(context) = WinExec16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1336 DX_reg(context) ),
1337 SW_NORMAL );
1338 if (AX_reg(context) < 32) SET_CFLAG(context);
1339 break;
1341 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1342 TASK_KillCurrentTask( AL_reg(context) );
1343 break;
1345 case 0x4d: /* GET RETURN CODE */
1346 AX_reg(context) = 0; /* normal exit */
1347 break;
1349 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1350 if (!INT21_FindFirst(context)) break;
1351 /* fall through */
1353 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1354 if (!INT21_FindNext(context))
1356 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Disk );
1357 AX_reg(context) = ER_NoMoreFiles;
1358 SET_CFLAG(context);
1360 break;
1362 case 0x51: /* GET PSP ADDRESS */
1363 case 0x62: /* GET PSP ADDRESS */
1364 /* FIXME: should we return the original DOS PSP upon */
1365 /* Windows startup ? */
1366 BX_reg(context) = GetCurrentPDB();
1367 break;
1369 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1370 ES_reg(context) = 0x0;
1371 BX_reg(context) = 0x0;
1372 INT_BARF( context, 0x21 );
1373 break;
1375 case 0x56: /* "RENAME" - RENAME FILE */
1376 bSetDOSExtendedError =
1377 (!MoveFile32A( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
1378 PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context))));
1379 break;
1381 case 0x57: /* FILE DATE AND TIME */
1382 switch (AL_reg(context))
1384 case 0x00: /* Get */
1386 FILETIME filetime;
1387 if (!GetFileTime( BX_reg(context), NULL, NULL, &filetime ))
1388 bSetDOSExtendedError = TRUE;
1389 else FileTimeToDosDateTime( &filetime, &DX_reg(context),
1390 &CX_reg(context) );
1392 break;
1394 case 0x01: /* Set */
1396 FILETIME filetime;
1397 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
1398 &filetime );
1399 bSetDOSExtendedError =
1400 (!SetFileTime( BX_reg(context), NULL, NULL, &filetime ));
1402 break;
1404 break;
1406 case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1407 switch (AL_reg(context))
1409 case 0x00:
1410 AX_reg(context) = 1;
1411 break;
1412 case 0x02:
1413 AX_reg(context) = 0;
1414 break;
1415 case 0x01:
1416 case 0x03:
1417 break;
1419 RESET_CFLAG(context);
1420 break;
1422 case 0x5a: /* CREATE TEMPORARY FILE */
1423 bSetDOSExtendedError = !INT21_CreateTempFile(context);
1424 break;
1426 case 0x5b: /* CREATE NEW FILE */
1427 bSetDOSExtendedError = ((AX_reg(context) =
1428 _lcreat_uniq( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)), 0 ))
1429 == (WORD)HFILE_ERROR16);
1430 break;
1432 case 0x5d: /* NETWORK */
1433 case 0x5e:
1434 /* network software not installed */
1435 DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
1436 bSetDOSExtendedError = TRUE;
1437 break;
1439 case 0x5f: /* NETWORK */
1440 switch (AL_reg(context))
1442 case 0x07: /* ENABLE DRIVE */
1443 if (!DRIVE_Enable( DL_reg(context) ))
1445 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
1446 bSetDOSExtendedError = TRUE;
1448 break;
1450 case 0x08: /* DISABLE DRIVE */
1451 if (!DRIVE_Disable( DL_reg(context) ))
1453 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
1454 bSetDOSExtendedError = TRUE;
1456 break;
1458 default:
1459 /* network software not installed */
1460 DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
1461 bSetDOSExtendedError = TRUE;
1462 break;
1464 break;
1466 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1468 if (!GetFullPathName32A( PTR_SEG_OFF_TO_LIN(DS_reg(context),
1469 SI_reg(context)), 128,
1470 PTR_SEG_OFF_TO_LIN(ES_reg(context),
1471 DI_reg(context)),NULL))
1472 bSetDOSExtendedError = TRUE;
1473 else AX_reg(context) = 0;
1475 break;
1477 case 0x61: /* UNUSED */
1478 case 0x63: /* UNUSED */
1479 case 0x64: /* OS/2 DOS BOX */
1480 INT_BARF( context, 0x21 );
1481 SET_CFLAG(context);
1482 break;
1484 case 0x65:{/* GET EXTENDED COUNTRY INFORMATION */
1485 extern WORD WINE_LanguageId;
1486 BYTE *dataptr=PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));;
1487 switch (AL_reg(context)) {
1488 case 0x01:
1489 dataptr[0] = 0x1;
1490 *(WORD*)(dataptr+1) = 41;
1491 *(WORD*)(dataptr+3) = WINE_LanguageId;
1492 *(WORD*)(dataptr+5) = CodePage;
1493 break;
1494 case 0x06:
1495 dataptr[0] = 0x06;
1496 *(DWORD*)(dataptr+1) = MAKELONG(DOSMEM_CollateTable & 0xFFFF,DOSMEM_AllocSelector(DOSMEM_CollateTable>>16));
1497 CX_reg(context) = 258;/*FIXME: size of table?*/
1498 break;
1499 default:
1500 INT_BARF( context, 0x21 );
1501 SET_CFLAG(context);
1502 break;
1504 break;
1506 case 0x66: /* GLOBAL CODE PAGE TABLE */
1507 switch (AL_reg(context))
1509 case 0x01:
1510 DX_reg(context) = BX_reg(context) = CodePage;
1511 RESET_CFLAG(context);
1512 break;
1513 case 0x02:
1514 CodePage = BX_reg(context);
1515 RESET_CFLAG(context);
1516 break;
1518 break;
1520 case 0x67: /* SET HANDLE COUNT */
1521 SetHandleCount16( BX_reg(context) );
1522 if (DOS_ExtendedError) bSetDOSExtendedError = TRUE;
1523 break;
1525 case 0x68: /* "FFLUSH" - COMMIT FILE */
1526 case 0x6a: /* COMMIT FILE */
1527 bSetDOSExtendedError = (!FlushFileBuffers( BX_reg(context) ));
1528 break;
1530 case 0x69: /* DISK SERIAL NUMBER */
1531 switch (AL_reg(context))
1533 case 0x00:
1534 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1535 else AX_reg(context) = 0;
1536 break;
1538 case 0x01:
1539 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1540 else AX_reg(context) = 1;
1541 break;
1543 break;
1545 case 0x6C: /* Extended Open/Create*/
1546 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1547 break;
1549 case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
1550 switch(AL_reg(context))
1552 case 0x39: /* Create directory */
1553 bSetDOSExtendedError = (!CreateDirectory32A(
1554 PTR_SEG_OFF_TO_LIN( DS_reg(context),
1555 DX_reg(context) ), NULL));
1556 break;
1557 case 0x3a: /* Remove directory */
1558 bSetDOSExtendedError = (!RemoveDirectory32A(
1559 PTR_SEG_OFF_TO_LIN( DS_reg(context),
1560 DX_reg(context) )));
1561 break;
1562 case 0x43: /* Get/Set file attributes */
1563 switch (BL_reg(context))
1565 case 0x00: /* Get file attributes */
1566 CX_reg(context) = (WORD)GetFileAttributes32A(
1567 PTR_SEG_OFF_TO_LIN(DS_reg(context),
1568 DX_reg(context)));
1569 if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1570 break;
1571 case 0x01:
1572 bSetDOSExtendedError = (!SetFileAttributes32A(
1573 PTR_SEG_OFF_TO_LIN(DS_reg(context),
1574 DX_reg(context)),
1575 CX_reg(context) ) );
1576 break;
1577 default:
1578 fprintf( stderr,
1579 "Unimplemented int21 long file name function:\n");
1580 INT_BARF( context, 0x21 );
1581 SET_CFLAG(context);
1582 AL_reg(context) = 0;
1583 break;
1585 break;
1586 case 0x47: /* Get current directory */
1587 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1588 break;
1590 case 0x4e: /* Find first file */
1591 /* FIXME: use attributes in CX */
1592 if ((AX_reg(context) = FindFirstFile16(
1593 PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
1594 (WIN32_FIND_DATA32A *)PTR_SEG_OFF_TO_LIN(ES_reg(context),
1595 DI_reg(context))))
1596 == INVALID_HANDLE_VALUE16)
1597 bSetDOSExtendedError = TRUE;
1598 break;
1599 case 0x4f: /* Find next file */
1600 if (!FindNextFile16( BX_reg(context),
1601 (WIN32_FIND_DATA32A *)PTR_SEG_OFF_TO_LIN(ES_reg(context),
1602 DI_reg(context))))
1603 bSetDOSExtendedError = TRUE;
1604 break;
1605 case 0xa1: /* Find close */
1606 bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
1607 break;
1608 case 0xa0:
1609 break;
1610 case 0x60:
1611 switch(CL_reg(context))
1613 case 0x02: /*Get canonical long filename or path */
1614 if (!GetFullPathName32A
1615 ( PTR_SEG_OFF_TO_LIN(DS_reg(context),
1616 SI_reg(context)), 128,
1617 PTR_SEG_OFF_TO_LIN(ES_reg(context),
1618 DI_reg(context)),NULL))
1619 bSetDOSExtendedError = TRUE;
1620 else AX_reg(context) = 0;
1621 break;
1622 default:
1623 fprintf( stderr,
1624 "Unimplemented int21 long file name function:\n");
1625 INT_BARF( context, 0x21 );
1626 SET_CFLAG(context);
1627 AL_reg(context) = 0;
1628 break;
1630 break;
1631 case 0x6c: /* Create or open file */
1632 /* translate Dos 7 action to Dos 6 action */
1633 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1634 break;
1636 case 0x3b: /* Change directory */
1637 if (!SetCurrentDirectory32A(PTR_SEG_OFF_TO_LIN(
1638 DS_reg(context),
1639 DX_reg(context)
1642 SET_CFLAG(context);
1643 AL_reg(context) = DOS_ExtendedError;
1645 break;
1646 case 0x41: /* Delete file */
1647 case 0x56: /* Move (rename) file */
1648 default:
1649 fprintf( stderr, "Unimplemented int21 long file name function:\n");
1650 INT_BARF( context, 0x21 );
1651 SET_CFLAG(context);
1652 AL_reg(context) = 0;
1653 break;
1655 break;
1657 case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
1658 case 0x72: /* MS-DOS 7 (Windows95) - ??? */
1659 case 0x73: /* MS-DOS 7 (Windows95) - DRIVE LOCKING ??? */
1660 dprintf_int(stddeb,"int21: windows95 function AX %04x\n",
1661 AX_reg(context));
1662 dprintf_int(stddeb, " returning unimplemented\n");
1663 SET_CFLAG(context);
1664 AL_reg(context) = 0;
1665 break;
1667 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
1668 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
1669 break;
1671 default:
1672 INT_BARF( context, 0x21 );
1673 break;
1675 } /* END OF SWITCH */
1677 if( bSetDOSExtendedError ) /* set general error condition */
1679 AX_reg(context) = DOS_ExtendedError;
1680 SET_CFLAG(context);
1683 dprintf_int( stddeb, "ret21: AX=%04x BX=%04x CX=%04x DX=%04x "
1684 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1685 AX_reg(context), BX_reg(context), CX_reg(context),
1686 DX_reg(context), SI_reg(context), DI_reg(context),
1687 (WORD)DS_reg(context), (WORD)ES_reg(context),
1688 EFL_reg(context));