Release 970804
[wine/multimedia.git] / msdos / int21.c
blob46b0004ed591136b4eefee879cbcacc9fa325dea
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 int curr_drive;
220 dprintf_int (stddeb, "int21: ioctl (%d, GetDeviceInfo)\n", BX_reg(context));
222 curr_drive = DRIVE_GetCurrentDrive();
223 DX_reg(context) = 0x0140 + curr_drive + ((curr_drive > 1) ? 0x0800 : 0); /* no floppy */
224 /* bits 0-5 are current drive
225 * bit 6 - file has NOT been written..FIXME: correct?
226 * bit 8 - generate int24 if no diskspace on write/ read past end of file
227 * bit 11 - media not removable
229 RESET_CFLAG(context);
232 static BOOL32 ioctlGenericBlkDevReq( CONTEXT *context )
234 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
235 int drive = DOS_GET_DRIVE( BL_reg(context) );
237 if (!DRIVE_IsValid(drive))
239 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
240 return TRUE;
243 if (CH_reg(context) != 0x08)
245 INT_BARF( context, 0x21 );
246 return FALSE;
249 switch (CL_reg(context))
251 case 0x4a: /* lock logical volume */
252 dprintf_int(stddeb,"int21: lock logical volume (%d) level %d mode %d\n",drive,BH_reg(context),DX_reg(context));
253 break;
255 case 0x60: /* get device parameters */
256 /* used by w4wgrp's winfile */
257 memset(dataptr, 0, 0x26);
258 dataptr[0] = 0x04;
259 dataptr[6] = 0; /* media type */
260 if (drive > 1)
262 dataptr[1] = 0x05; /* fixed disk */
263 setword(&dataptr[2], 0x01); /* non removable */
264 setword(&dataptr[4], 0x300); /* # of cylinders */
266 else
268 dataptr[1] = 0x07; /* block dev, floppy */
269 setword(&dataptr[2], 0x02); /* removable */
270 setword(&dataptr[4], 80); /* # of cylinders */
272 CreateBPB(drive, &dataptr[7]);
273 RESET_CFLAG(context);
274 break;
276 case 0x66:/* get disk serial number */
278 char label[12],fsname[9],path[4];
279 DWORD serial;
281 strcpy(path,"x:\\");path[0]=drive+'A';
282 GetVolumeInformation32A(
283 path,label,12,&serial,NULL,NULL,fsname,9
285 *(WORD*)dataptr = 0;
286 memcpy(dataptr+2,&serial,4);
287 memcpy(dataptr+6,label ,11);
288 memcpy(dataptr+17,fsname,8);
290 break;
292 case 0x6a:
293 dprintf_int(stddeb,"int21: logical volume %d unlocked.\n",drive);
294 break;
296 default:
297 INT_BARF( context, 0x21 );
299 return FALSE;
302 static void INT21_GetSystemDate( CONTEXT *context )
304 SYSTEMTIME systime;
305 GetLocalTime( &systime );
306 CX_reg(context) = systime.wYear;
307 DX_reg(context) = (systime.wMonth << 8) | systime.wDay;
308 AX_reg(context) = systime.wDayOfWeek;
311 static void INT21_GetSystemTime( CONTEXT *context )
313 SYSTEMTIME systime;
314 GetLocalTime( &systime );
315 CX_reg(context) = (systime.wHour << 8) | systime.wMinute;
316 DX_reg(context) = (systime.wSecond << 8) | (systime.wMilliseconds / 10);
319 static BOOL32 INT21_CreateFile( CONTEXT *context )
321 AX_reg(context) = _lcreat16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
322 DX_reg(context) ), CX_reg(context) );
323 return (AX_reg(context) == (WORD)HFILE_ERROR16);
327 void OpenExistingFile( CONTEXT *context )
329 AX_reg(context) = _lopen16( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
330 AL_reg(context) );
331 if (AX_reg(context) == (WORD)HFILE_ERROR16)
333 AX_reg(context) = DOS_ExtendedError;
334 SET_CFLAG(context);
336 #if 0
338 int handle;
339 int mode;
340 int lock;
342 switch (AX_reg(context) & 0x0070)
344 case 0x00: /* compatability mode */
345 case 0x40: /* DENYNONE */
346 lock = -1;
347 break;
349 case 0x30: /* DENYREAD */
350 dprintf_int(stddeb,
351 "OpenExistingFile (%s): DENYREAD changed to DENYALL\n",
352 (char *)PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)));
353 case 0x10: /* DENYALL */
354 lock = LOCK_EX;
355 break;
357 case 0x20: /* DENYWRITE */
358 lock = LOCK_SH;
359 break;
361 default:
362 lock = -1;
365 if (lock != -1)
368 int result,retries=sharing_retries;
370 #if defined(__svr4__) || defined(_SCO_DS)
371 printf("Should call flock and needs porting to lockf\n");
372 result = 0;
373 retries = 0;
374 #else
375 result = flock(handle, lock | LOCK_NB);
376 #endif
377 if ( retries && (!result) )
379 int i;
380 for(i=0;i<32768*((int)sharing_pause);i++)
381 result++; /* stop the optimizer */
382 for(i=0;i<32768*((int)sharing_pause);i++)
383 result--;
386 while( (!result) && (!(retries--)) );
388 if(result)
390 errno_to_doserr();
391 AX_reg(context) = DOS_ExtendedError;
392 close(handle);
393 SET_CFLAG(context);
394 return;
399 Error (0,0,0);
400 AX_reg(context) = handle;
401 RESET_CFLAG(context);
403 #endif
406 static void CloseFile( CONTEXT *context )
408 if ((AX_reg(context) = _lclose16( BX_reg(context) )) != 0)
410 AX_reg(context) = DOS_ExtendedError;
411 SET_CFLAG(context);
415 static BOOL32 INT21_ExtendedOpenCreateFile(CONTEXT *context )
417 BOOL32 bExtendedError = FALSE;
418 BYTE action = DL_reg(context);
420 /* Shuffle arguments to call OpenExistingFile */
421 AL_reg(context) = BL_reg(context);
422 DX_reg(context) = SI_reg(context);
423 /* BX,CX and DX should be preserved */
424 OpenExistingFile(context);
426 if ((EFL_reg(context) & 0x0001) == 0) /* File exists */
428 UINT16 uReturnCX = 0;
430 /* Now decide what do do */
432 if ((action & 0x07) == 0)
434 BX_reg(context) = AX_reg(context);
435 CloseFile(context);
436 AX_reg(context) = 0x0050; /*File exists*/
437 SET_CFLAG(context);
438 dprintf_int(stddeb, "int21: extended open/create: failed because file exists \n");
440 else if ((action & 0x07) == 2)
442 /* Truncate it, but first check if opened for write */
443 if ((BL_reg(context) & 0x0007)== 0)
445 BX_reg(context) = AX_reg(context);
446 CloseFile(context);
447 dprintf_int(stddeb, "int21: extended open/create: failed, trunc on ro file");
448 AX_reg(context) = 0x000C; /*Access code invalid*/
449 SET_CFLAG(context);
451 else
453 /* Shuffle arguments to call CloseFile while
454 * preserving BX and DX */
456 dprintf_int(stddeb, "int21: extended open/create: Closing before truncate\n");
457 BX_reg(context) = AX_reg(context);
458 CloseFile(context);
459 if (EFL_reg(context) & 0x0001)
461 dprintf_int(stddeb, "int21: extended open/create: close before trunc failed");
462 AX_reg(context) = 0x0019; /*Seek Error*/
463 CX_reg(context) = 0;
464 SET_CFLAG(context);
466 /* Shuffle arguments to call CreateFile */
468 dprintf_int(stddeb, "int21: extended open/create: Truncating\n");
469 AL_reg(context) = BL_reg(context);
470 /* CX is still the same */
471 DX_reg(context) = SI_reg(context);
472 bExtendedError = INT21_CreateFile(context);
474 if (EFL_reg(context) & 0x0001) /*no file open, flags set */
476 dprintf_int(stddeb, "int21: extended open/create: trunc failed");
477 return bExtendedError;
479 uReturnCX = 0x3;
482 else uReturnCX = 0x1;
484 CX_reg(context) = uReturnCX;
486 else /* file does not exist */
488 RESET_CFLAG(context); /* was set by OpenExistingFile(context) */
489 if ((action & 0xF0)== 0)
491 CX_reg(context) = 0;
492 SET_CFLAG(context);
493 dprintf_int(stddeb, "int21: extended open/create: failed, file dosen't exist\n");
495 else
497 /* Shuffle arguments to call CreateFile */
498 dprintf_int(stddeb, "int21: extended open/create: Creating\n");
499 AL_reg(context) = BL_reg(context);
500 /* CX should still be the same */
501 DX_reg(context) = SI_reg(context);
502 bExtendedError = INT21_CreateFile(context);
503 if (EFL_reg(context) & 0x0001) /*no file open, flags set */
505 dprintf_int(stddeb, "int21: extended open/create: create failed\n");
506 return bExtendedError;
508 CX_reg(context) = 2;
512 return bExtendedError;
516 static BOOL32 INT21_ChangeDir( CONTEXT *context )
518 int drive;
519 char *dirname = PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context));
521 dprintf_int(stddeb,"int21: changedir %s\n", dirname);
522 if (dirname[0] && (dirname[1] == ':'))
524 drive = toupper(dirname[0]) - 'A';
525 dirname += 2;
527 else drive = DRIVE_GetCurrentDrive();
528 return DRIVE_Chdir( drive, dirname );
532 static int INT21_FindFirst( CONTEXT *context )
534 char *p;
535 const char *path;
536 DOS_FULL_NAME full_name;
537 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA();
539 path = (const char *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
540 dta->unixPath = NULL;
541 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
543 AX_reg(context) = DOS_ExtendedError;
544 SET_CFLAG(context);
545 return 0;
547 dta->unixPath = xstrdup( full_name.long_name );
548 p = strrchr( dta->unixPath, '/' );
549 *p = '\0';
551 /* Note: terminating NULL in dta->mask overwrites dta->search_attr
552 * (doesn't matter as it is set below anyway)
554 if (!DOSFS_ToDosFCBFormat( p + 1, dta->mask ))
556 free( dta->unixPath );
557 dta->unixPath = NULL;
558 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
559 AX_reg(context) = ER_FileNotFound;
560 SET_CFLAG(context);
561 return 0;
563 dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
564 : DRIVE_GetCurrentDrive();
565 dta->count = 0;
566 dta->search_attr = CL_reg(context);
567 return 1;
571 static int INT21_FindNext( CONTEXT *context )
573 FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA();
574 WIN32_FIND_DATA32A entry;
575 int count;
577 if (!dta->unixPath) return 0;
578 if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
579 dta->search_attr, dta->count, &entry )))
581 free( dta->unixPath );
582 dta->unixPath = NULL;
583 return 0;
585 if ((int)dta->count + count > 0xffff)
587 fprintf( stderr, "Too many directory entries in %s\n", dta->unixPath );
588 free( dta->unixPath );
589 dta->unixPath = NULL;
590 return 0;
592 dta->count += count;
593 dta->fileattr = entry.dwFileAttributes;
594 dta->filesize = entry.nFileSizeLow;
595 FileTimeToDosDateTime( &entry.ftLastWriteTime,
596 &dta->filedate, &dta->filetime );
597 strcpy( dta->filename, entry.cAlternateFileName );
598 return 1;
602 static BOOL32 INT21_CreateTempFile( CONTEXT *context )
604 static int counter = 0;
605 char *name = PTR_SEG_OFF_TO_LIN( DS_reg(context), DX_reg(context) );
606 char *p = name + strlen(name);
608 for (;;)
610 sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
611 counter = (counter + 1) % 1000;
613 if ((AX_reg(context) = _lcreat_uniq( name, 0 )) != (WORD)HFILE_ERROR16)
615 dprintf_int( stddeb, "INT21_CreateTempFile: created %s\n", name );
616 return TRUE;
618 if (DOS_ExtendedError != ER_FileExists) return FALSE;
623 static BOOL32 INT21_GetCurrentDirectory( CONTEXT *context )
625 int drive = DOS_GET_DRIVE( DL_reg(context) );
626 char *ptr = (char *)PTR_SEG_OFF_TO_LIN( DS_reg(context), SI_reg(context) );
628 if (!DRIVE_IsValid(drive))
630 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
631 return FALSE;
633 lstrcpyn32A( ptr, DRIVE_GetDosCwd(drive), 64 );
634 AX_reg(context) = 0x0100; /* success return code */
635 return TRUE;
639 static int INT21_GetDiskSerialNumber( CONTEXT *context )
641 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
642 int drive = DOS_GET_DRIVE( BL_reg(context) );
644 if (!DRIVE_IsValid(drive))
646 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
647 return 0;
650 *(WORD *)dataptr = 0;
651 *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
652 memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
653 strncpy(dataptr + 0x11, "FAT16 ", 8);
654 return 1;
658 static int INT21_SetDiskSerialNumber( CONTEXT *context )
660 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
661 int drive = DOS_GET_DRIVE( BL_reg(context) );
663 if (!DRIVE_IsValid(drive))
665 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
666 return 0;
669 DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
670 return 1;
674 /* microsoft's programmers should be shot for using CP/M style int21
675 calls in Windows for Workgroup's winfile.exe */
677 static int INT21_FindFirstFCB( CONTEXT *context )
679 BYTE *fcb = (BYTE *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
680 FINDFILE_FCB *pFCB;
681 LPCSTR root, cwd;
682 int drive;
684 if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
685 else pFCB = (FINDFILE_FCB *)fcb;
686 drive = DOS_GET_DRIVE( pFCB->drive );
687 root = DRIVE_GetRoot( drive );
688 cwd = DRIVE_GetUnixCwd( drive );
689 pFCB->unixPath = HeapAlloc( SystemHeap, 0, strlen(root)+strlen(cwd)+2 );
690 if (!pFCB->unixPath) return 0;
691 strcpy( pFCB->unixPath, root );
692 strcat( pFCB->unixPath, "/" );
693 strcat( pFCB->unixPath, cwd );
694 pFCB->count = 0;
695 return 1;
699 static int INT21_FindNextFCB( CONTEXT *context )
701 BYTE *fcb = (BYTE *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
702 FINDFILE_FCB *pFCB;
703 DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA();
704 WIN32_FIND_DATA32A entry;
705 BYTE attr;
706 int count;
708 if (*fcb == 0xff)
710 attr = fcb[6];
711 pFCB = (FINDFILE_FCB *)(fcb + 7);
713 else
715 attr = 0;
716 pFCB = (FINDFILE_FCB *)fcb;
719 if (!pFCB->unixPath) return 0;
720 if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
721 DOS_GET_DRIVE( pFCB->drive ), attr,
722 pFCB->count, &entry )))
724 HeapFree( SystemHeap, 0, pFCB->unixPath );
725 pFCB->unixPath = NULL;
726 return 0;
728 pFCB->count += count;
730 pResult->fileattr = entry.dwFileAttributes;
731 pResult->cluster = 0; /* what else? */
732 pResult->filesize = entry.nFileSizeLow;
733 memset( pResult->reserved, 0, sizeof(pResult->reserved) );
734 FileTimeToDosDateTime( &entry.ftLastWriteTime,
735 &pResult->filedate, &pResult->filetime );
737 /* Convert file name to FCB format */
739 memset( pResult->filename, ' ', sizeof(pResult->filename) );
740 if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
741 else if (!strcmp( entry.cAlternateFileName, ".." ))
742 pResult->filename[0] = pResult->filename[1] = '.';
743 else
745 char *p = strrchr( entry.cAlternateFileName, '.' );
746 if (p && p[1] && (p != entry.cAlternateFileName))
748 memcpy( pResult->filename, entry.cAlternateFileName,
749 MIN( (p - entry.cAlternateFileName), 8 ) );
750 memcpy( pResult->filename + 8, p + 1, MIN( strlen(p), 3 ) );
752 else
753 memcpy( pResult->filename, entry.cAlternateFileName,
754 MIN( strlen(entry.cAlternateFileName), 8 ) );
756 return 1;
760 static void DeleteFileFCB( CONTEXT *context )
762 fprintf( stderr, "DeleteFileFCB: not implemented yet\n" );
763 #if 0
764 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
765 struct dosdirent *dp;
766 char temp[256], *ptr;
767 int drive = DOS_GET_DRIVE( *fcb );
769 DumpFCB( fcb );
771 temp[0] = '\\';
772 strcpy(temp+1, DRIVE_GetDosCwd(drive));
773 strcat(temp, "\\");
774 strncat(temp, fcb + 1, 8);
775 ChopOffWhiteSpace(temp);
776 strncat(temp, fcb + 9, 3);
777 ChopOffWhiteSpace(temp);
779 if ((dp = DOS_opendir(temp)) == NULL) {
780 Error(InvalidDrive, EC_MediaError , EL_Disk);
781 AX_reg(context) = 0xff;
782 return;
785 temp[0] = '\\';
786 strcpy(temp+1, DRIVE_GetDosCwd(drive) );
787 strcat(temp, "\\");
789 ptr = temp + strlen(temp);
791 while (DOS_readdir(dp) != NULL)
793 strcpy(ptr, dp->filename);
794 dprintf_int(stddeb, "int21: delete file %s\n", temp);
795 /* unlink(DOS_GetUnixFileName(temp)); */
797 DOS_closedir(dp);
798 AX_reg(context) = 0;
799 #endif
802 static void RenameFileFCB( CONTEXT *context )
804 fprintf( stderr, "RenameFileFCB: not implemented yet\n" );
805 #if 0
806 BYTE *fcb = PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context));
807 struct dosdirent *dp;
808 char temp[256], oldname[256], newname[256], *oldnameptr, *newnameptr;
809 int drive = DOS_GET_DRIVE( *fcb );
811 DumpFCB( fcb );
813 temp[0] = '\\';
814 strcpy(temp+1, DRIVE_GetDosCwd(drive) );
815 strcat(temp, "\\");
816 strncat(temp, fcb + 1, 8);
817 ChopOffWhiteSpace(temp);
818 strncat(temp, fcb + 9, 3);
819 ChopOffWhiteSpace(temp);
821 if ((dp = DOS_opendir(temp)) == NULL) {
822 Error(InvalidDrive, EC_MediaError , EL_Disk);
823 AX_reg(context) = 0xff;
824 return;
827 oldname[0] = '\\';
828 strcpy(oldname+1, DRIVE_GetDosCwd(drive) );
829 strcat(oldname, "\\");
830 strcpy( newname, oldname );
831 oldnameptr = oldname + strlen(oldname);
832 newnameptr = newname + strlen(newname);
834 while (DOS_readdir(dp) != NULL)
836 strcpy(oldnameptr, dp->filename);
837 strcpy(newnameptr, fcb + 1);
838 dprintf_int(stddeb, "int21: renamefile %s -> %s\n",
839 oldname, newname);
841 DOS_closedir(dp);
842 AX_reg(context) = 0;
843 #endif
848 static void fLock( CONTEXT * context )
851 switch ( AX_reg(context) & 0xff )
853 case 0x00: /* LOCK */
854 if (!LockFile(BX_reg(context),
855 MAKELONG(DX_reg(context),CX_reg(context)), 0,
856 MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
857 AX_reg(context) = DOS_ExtendedError;
858 SET_CFLAG(context);
860 break;
862 case 0x01: /* UNLOCK */
863 if (!UnlockFile(BX_reg(context),
864 MAKELONG(DX_reg(context),CX_reg(context)), 0,
865 MAKELONG(DI_reg(context),SI_reg(context)), 0)) {
866 AX_reg(context) = DOS_ExtendedError;
867 SET_CFLAG(context);
869 return;
870 default:
871 AX_reg(context) = 0x0001;
872 SET_CFLAG(context);
873 return;
878 extern void LOCAL_PrintHeap (WORD ds);
880 /***********************************************************************
881 * DOS3Call (KERNEL.102)
883 void DOS3Call( CONTEXT *context )
885 BOOL32 bSetDOSExtendedError = FALSE;
887 dprintf_int( stddeb, "int21: AX=%04x BX=%04x CX=%04x DX=%04x "
888 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
889 AX_reg(context), BX_reg(context), CX_reg(context),
890 DX_reg(context), SI_reg(context), DI_reg(context),
891 (WORD)DS_reg(context), (WORD)ES_reg(context),
892 EFL_reg(context) );
894 if (AH_reg(context) == 0x59) /* Get extended error info */
896 AX_reg(context) = DOS_ExtendedError;
897 BH_reg(context) = DOS_ErrorClass;
898 BL_reg(context) = DOS_ErrorAction;
899 CH_reg(context) = DOS_ErrorLocus;
900 return;
903 DOS_ERROR( 0, 0, 0, 0 );
904 RESET_CFLAG(context); /* Not sure if this is a good idea */
906 switch(AH_reg(context))
908 case 0x00: /* TERMINATE PROGRAM */
909 TASK_KillCurrentTask( 0 );
910 break;
912 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
913 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
914 case 0x03: /* READ CHARACTER FROM STDAUX */
915 case 0x04: /* WRITE CHARACTER TO STDAUX */
916 case 0x05: /* WRITE CHARACTER TO PRINTER */
917 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
918 case 0x07: /* DIRECT CHARACTER INPUT, WITHOUT ECHO */
919 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
920 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
921 case 0x0a: /* BUFFERED INPUT */
922 case 0x0b: /* GET STDIN STATUS */
923 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
924 case 0x0f: /* OPEN FILE USING FCB */
925 case 0x10: /* CLOSE FILE USING FCB */
926 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
927 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
928 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
929 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
930 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
931 case 0x23: /* GET FILE SIZE FOR FCB */
932 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
933 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
934 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
935 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
936 case 0x29: /* PARSE FILENAME INTO FCB */
937 case 0x2e: /* SET VERIFY FLAG */
938 case 0x37: /* "SWITCHAR" - GET SWITCH CHARACTER
939 "SWITCHAR" - SET SWITCH CHARACTER
940 "AVAILDEV" - SPECIFY \DEV\ PREFIX USE */
941 case 0x54: /* GET VERIFY FLAG */
942 INT_BARF( context, 0x21 );
943 break;
945 case 0x18: /* NULL FUNCTIONS FOR CP/M COMPATIBILITY */
946 case 0x1d:
947 case 0x1e:
948 case 0x20:
949 case 0x6b: /* NULL FUNCTION */
950 AL_reg(context) = 0;
951 break;
953 case 0x5c: /* "FLOCK" - RECORD LOCKING */
954 fLock(context);
955 break;
957 case 0x0d: /* DISK BUFFER FLUSH */
958 RESET_CFLAG(context); /* dos 6+ only */
959 break;
961 case 0x0e: /* SELECT DEFAULT DRIVE */
962 DRIVE_SetCurrentDrive( DL_reg(context) );
963 AL_reg(context) = MAX_DOS_DRIVES;
964 break;
966 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
967 if (!INT21_FindFirstFCB(context))
969 AL_reg(context) = 0xff;
970 break;
972 /* else fall through */
974 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
975 AL_reg(context) = INT21_FindNextFCB(context) ? 0x00 : 0xff;
976 break;
978 case 0x13: /* DELETE FILE USING FCB */
979 DeleteFileFCB(context);
980 break;
982 case 0x17: /* RENAME FILE USING FCB */
983 RenameFileFCB(context);
984 break;
986 case 0x19: /* GET CURRENT DEFAULT DRIVE */
987 AL_reg(context) = DRIVE_GetCurrentDrive();
988 break;
990 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
992 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
993 pTask->dta = PTR_SEG_OFF_TO_SEGPTR(DS_reg(context),DX_reg(context));
994 dprintf_int(stddeb, "int21: Set DTA: %08lx\n", pTask->dta);
996 break;
998 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
999 DL_reg(context) = 0;
1000 if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1001 break;
1003 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
1004 if (!INT21_GetDriveAllocInfo(context)) AX_reg(context) = 0xffff;
1005 break;
1007 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
1008 GetDrivePB(context, DRIVE_GetCurrentDrive());
1009 break;
1011 case 0x25: /* SET INTERRUPT VECTOR */
1012 INT_SetHandler( AL_reg(context),
1013 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1014 DX_reg(context)));
1015 break;
1017 case 0x2a: /* GET SYSTEM DATE */
1018 INT21_GetSystemDate(context);
1019 break;
1021 case 0x2b: /* SET SYSTEM DATE */
1022 fprintf( stdnimp, "SetSystemDate(%02d/%02d/%04d): not allowed\n",
1023 DL_reg(context), DH_reg(context), CX_reg(context) );
1024 AL_reg(context) = 0; /* Let's pretend we succeeded */
1025 break;
1027 case 0x2c: /* GET SYSTEM TIME */
1028 INT21_GetSystemTime(context);
1029 break;
1031 case 0x2d: /* SET SYSTEM TIME */
1032 fprintf( stdnimp, "SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
1033 CH_reg(context), CL_reg(context),
1034 DH_reg(context), DL_reg(context) );
1035 AL_reg(context) = 0; /* Let's pretend we succeeded */
1036 break;
1038 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
1040 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1041 ES_reg(context) = SELECTOROF( pTask->dta );
1042 BX_reg(context) = OFFSETOF( pTask->dta );
1044 break;
1046 case 0x30: /* GET DOS VERSION */
1047 AX_reg(context) = DOSVERSION;
1048 BX_reg(context) = 0x0012; /* 0x123456 is Wine's serial # */
1049 CX_reg(context) = 0x3456;
1050 break;
1052 case 0x31: /* TERMINATE AND STAY RESIDENT */
1053 INT_BARF( context, 0x21 );
1054 break;
1056 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
1057 GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
1058 break;
1060 case 0x33: /* MULTIPLEXED */
1061 switch (AL_reg(context))
1063 case 0x00: /* GET CURRENT EXTENDED BREAK STATE */
1064 DL_reg(context) = 0;
1065 break;
1067 case 0x01: /* SET EXTENDED BREAK STATE */
1068 break;
1070 case 0x02: /* GET AND SET EXTENDED CONTROL-BREAK CHECKING STATE*/
1071 DL_reg(context) = 0;
1072 break;
1074 case 0x05: /* GET BOOT DRIVE */
1075 DL_reg(context) = 3;
1076 /* c: is Wine's bootdrive (a: is 1)*/
1077 break;
1079 case 0x06: /* GET TRUE VERSION NUMBER */
1080 BX_reg(context) = DOSVERSION;
1081 DX_reg(context) = 0x00;
1082 break;
1084 default:
1085 INT_BARF( context, 0x21 );
1086 break;
1088 break;
1090 case 0x34: /* GET ADDRESS OF INDOS FLAG */
1091 if (!heap) INT21_CreateHeap();
1092 ES_reg(context) = DosHeapHandle;
1093 BX_reg(context) = (int)&heap->InDosFlag - (int)heap;
1094 break;
1096 case 0x35: /* GET INTERRUPT VECTOR */
1098 FARPROC16 addr = INT_GetHandler( AL_reg(context) );
1099 ES_reg(context) = SELECTOROF(addr);
1100 BX_reg(context) = OFFSETOF(addr);
1102 break;
1104 case 0x36: /* GET FREE DISK SPACE */
1105 if (!INT21_GetFreeDiskSpace(context)) AX_reg(context) = 0xffff;
1106 break;
1108 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
1109 AX_reg(context) = 0x02; /* no country support available */
1110 SET_CFLAG(context);
1111 break;
1113 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
1114 bSetDOSExtendedError = (!CreateDirectory16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1115 DX_reg(context) ), NULL));
1116 break;
1118 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
1119 bSetDOSExtendedError = (!RemoveDirectory16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1120 DX_reg(context) )));
1121 break;
1123 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
1124 bSetDOSExtendedError = !INT21_ChangeDir(context);
1125 break;
1127 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
1128 AX_reg(context) = _lcreat16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1129 DX_reg(context) ), CX_reg(context) );
1130 bSetDOSExtendedError = (AX_reg(context) == (WORD)HFILE_ERROR16);
1131 break;
1133 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
1134 OpenExistingFile(context);
1135 break;
1137 case 0x3e: /* "CLOSE" - CLOSE FILE */
1138 bSetDOSExtendedError = ((AX_reg(context) = _lclose16( BX_reg(context) )) != 0);
1139 break;
1141 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
1143 LONG result = WIN16_hread( BX_reg(context),
1144 PTR_SEG_OFF_TO_SEGPTR( DS_reg(context),
1145 DX_reg(context) ),
1146 CX_reg(context) );
1147 if (result == -1) bSetDOSExtendedError = TRUE;
1148 else AX_reg(context) = (WORD)result;
1150 break;
1152 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
1154 LONG result = _hwrite16( BX_reg(context),
1155 PTR_SEG_OFF_TO_LIN( DS_reg(context),
1156 DX_reg(context) ),
1157 CX_reg(context) );
1158 if (result == -1) bSetDOSExtendedError = TRUE;
1159 else AX_reg(context) = (WORD)result;
1161 break;
1163 case 0x41: /* "UNLINK" - DELETE FILE */
1164 bSetDOSExtendedError = (!DeleteFile32A( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1165 DX_reg(context) )));
1166 break;
1168 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
1170 LONG status = _llseek16( BX_reg(context),
1171 MAKELONG(DX_reg(context),CX_reg(context)),
1172 AL_reg(context) );
1173 if (status == -1) bSetDOSExtendedError = TRUE;
1174 else
1176 AX_reg(context) = LOWORD(status);
1177 DX_reg(context) = HIWORD(status);
1180 break;
1182 case 0x43: /* FILE ATTRIBUTES */
1183 switch (AL_reg(context))
1185 case 0x00:
1186 AX_reg(context) = (WORD)GetFileAttributes32A(
1187 PTR_SEG_OFF_TO_LIN(DS_reg(context),
1188 DX_reg(context)));
1189 if (AX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1190 else CX_reg(context) = AX_reg(context);
1191 break;
1193 case 0x01:
1194 bSetDOSExtendedError =
1195 (!SetFileAttributes32A( PTR_SEG_OFF_TO_LIN(DS_reg(context),
1196 DX_reg(context)),
1197 CX_reg(context) ));
1198 break;
1200 break;
1202 case 0x44: /* IOCTL */
1203 switch (AL_reg(context))
1205 case 0x00:
1206 ioctlGetDeviceInfo(context);
1207 break;
1209 case 0x01:
1210 break;
1211 case 0x05:{ /* IOCTL - WRITE TO BLOCK DEVICE CONTROL CHANNEL */
1212 BYTE *dataptr = PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context));
1213 int i;
1214 int drive = DOS_GET_DRIVE(BL_reg(context));
1216 fprintf(stdnimp,"int21: program tried to write to block device control channel of drive %d:\n",drive);
1217 for (i=0;i<CX_reg(context);i++)
1218 fprintf(stdnimp,"%02x ",dataptr[i]);
1219 fprintf(stdnimp,"\n");
1220 AX_reg(context)=CX_reg(context);
1221 break;
1223 case 0x08: /* Check if drive is removable. */
1224 switch(GetDriveType16( DOS_GET_DRIVE( BL_reg(context) )))
1226 case DRIVE_CANNOTDETERMINE:
1227 DOS_ERROR( ER_InvalidDrive, EC_NotFound, SA_Abort, EL_Disk );
1228 AX_reg(context) = ER_InvalidDrive;
1229 SET_CFLAG(context);
1230 break;
1231 case DRIVE_REMOVABLE:
1232 AX_reg(context) = 0; /* removable */
1233 break;
1234 default:
1235 AX_reg(context) = 1; /* not removable */
1236 break;
1238 break;
1240 case 0x09: /* CHECK IF BLOCK DEVICE REMOTE */
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_REMOTE:
1249 DX_reg(context) = (1<<9) | (1<<12); /* remote */
1250 break;
1251 default:
1252 DX_reg(context) = 0; /* FIXME: use driver attr here */
1253 break;
1255 break;
1257 case 0x0a: /* check if handle (BX) is remote */
1258 /* returns DX, bit 15 set if remote, bit 14 set if date/time
1259 * not set on close
1261 DX_reg(context) = 0;
1262 break;
1264 case 0x0b: /* SET SHARING RETRY COUNT */
1265 if (!CX_reg(context))
1267 AX_reg(context) = 1;
1268 SET_CFLAG(context);
1269 break;
1271 sharing_pause = CX_reg(context);
1272 if (!DX_reg(context))
1273 sharing_retries = DX_reg(context);
1274 RESET_CFLAG(context);
1275 break;
1277 case 0x0d:
1278 bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
1279 break;
1281 case 0x0e: /* get logical drive mapping */
1282 AL_reg(context) = 0; /* drive has no mapping */
1283 break;
1285 case 0x0F: /* Set logical drive mapping */
1286 /* FIXME: Not implemented at the moment, always returns error
1288 INT_BARF( context, 0x21 );
1289 AX_reg(context) = 0x0001; /* invalid function */
1290 SET_CFLAG(context);
1291 break;
1293 default:
1294 INT_BARF( context, 0x21 );
1295 break;
1297 break;
1299 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
1300 bSetDOSExtendedError = ((AX_reg(context) = FILE_Dup(BX_reg(context))) == (WORD)HFILE_ERROR16);
1301 break;
1303 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
1304 bSetDOSExtendedError = (FILE_Dup2( BX_reg(context), CX_reg(context) ) == HFILE_ERROR32);
1305 break;
1307 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
1308 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1309 break;
1311 case 0x48: /* ALLOCATE MEMORY */
1312 case 0x49: /* FREE MEMORY */
1313 case 0x4a: /* RESIZE MEMORY BLOCK */
1314 INT_BARF( context, 0x21 );
1315 break;
1317 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
1318 AX_reg(context) = WinExec16( PTR_SEG_OFF_TO_LIN( DS_reg(context),
1319 DX_reg(context) ),
1320 SW_NORMAL );
1321 if (AX_reg(context) < 32) SET_CFLAG(context);
1322 break;
1324 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
1325 TASK_KillCurrentTask( AL_reg(context) );
1326 break;
1328 case 0x4d: /* GET RETURN CODE */
1329 AX_reg(context) = 0; /* normal exit */
1330 break;
1332 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
1333 if (!INT21_FindFirst(context)) break;
1334 /* fall through */
1336 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
1337 if (!INT21_FindNext(context))
1339 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Disk );
1340 AX_reg(context) = ER_NoMoreFiles;
1341 SET_CFLAG(context);
1343 break;
1345 case 0x51: /* GET PSP ADDRESS */
1346 case 0x62: /* GET PSP ADDRESS */
1347 /* FIXME: should we return the original DOS PSP upon */
1348 /* Windows startup ? */
1349 BX_reg(context) = GetCurrentPDB();
1350 break;
1352 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1353 ES_reg(context) = 0x0;
1354 BX_reg(context) = 0x0;
1355 INT_BARF( context, 0x21 );
1356 break;
1358 case 0x56: /* "RENAME" - RENAME FILE */
1359 bSetDOSExtendedError =
1360 (!MoveFile32A( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
1361 PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context))));
1362 break;
1364 case 0x57: /* FILE DATE AND TIME */
1365 switch (AL_reg(context))
1367 case 0x00: /* Get */
1369 FILETIME filetime;
1370 if (!GetFileTime( BX_reg(context), NULL, NULL, &filetime ))
1371 bSetDOSExtendedError = TRUE;
1372 else FileTimeToDosDateTime( &filetime, &DX_reg(context),
1373 &CX_reg(context) );
1375 break;
1377 case 0x01: /* Set */
1379 FILETIME filetime;
1380 DosDateTimeToFileTime( DX_reg(context), CX_reg(context),
1381 &filetime );
1382 bSetDOSExtendedError =
1383 (!SetFileTime( BX_reg(context), NULL, NULL, &filetime ));
1385 break;
1387 break;
1389 case 0x58: /* GET OR SET MEMORY/UMB ALLOCATION STRATEGY */
1390 switch (AL_reg(context))
1392 case 0x00:
1393 AX_reg(context) = 1;
1394 break;
1395 case 0x02:
1396 AX_reg(context) = 0;
1397 break;
1398 case 0x01:
1399 case 0x03:
1400 break;
1402 RESET_CFLAG(context);
1403 break;
1405 case 0x5a: /* CREATE TEMPORARY FILE */
1406 bSetDOSExtendedError = !INT21_CreateTempFile(context);
1407 break;
1409 case 0x5b: /* CREATE NEW FILE */
1410 bSetDOSExtendedError = ((AX_reg(context) =
1411 _lcreat_uniq( PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)), 0 ))
1412 == (WORD)HFILE_ERROR16);
1413 break;
1415 case 0x5d: /* NETWORK */
1416 case 0x5e:
1417 /* network software not installed */
1418 DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
1419 bSetDOSExtendedError = TRUE;
1420 break;
1422 case 0x5f: /* NETWORK */
1423 switch (AL_reg(context))
1425 case 0x07: /* ENABLE DRIVE */
1426 if (!DRIVE_Enable( DL_reg(context) ))
1428 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
1429 bSetDOSExtendedError = TRUE;
1431 break;
1433 case 0x08: /* DISABLE DRIVE */
1434 if (!DRIVE_Disable( DL_reg(context) ))
1436 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
1437 bSetDOSExtendedError = TRUE;
1439 break;
1441 default:
1442 /* network software not installed */
1443 DOS_ERROR( ER_NoNetwork, EC_NotFound, SA_Abort, EL_Network );
1444 bSetDOSExtendedError = TRUE;
1445 break;
1447 break;
1449 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1451 if (!GetFullPathName32A( PTR_SEG_OFF_TO_LIN(DS_reg(context),
1452 SI_reg(context)), 128,
1453 PTR_SEG_OFF_TO_LIN(ES_reg(context),
1454 DI_reg(context)),NULL))
1455 bSetDOSExtendedError = TRUE;
1456 else AX_reg(context) = 0;
1458 break;
1460 case 0x61: /* UNUSED */
1461 case 0x63: /* UNUSED */
1462 case 0x64: /* OS/2 DOS BOX */
1463 INT_BARF( context, 0x21 );
1464 SET_CFLAG(context);
1465 break;
1467 case 0x65:{/* GET EXTENDED COUNTRY INFORMATION */
1468 extern WORD WINE_LanguageId;
1469 BYTE *dataptr=PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));;
1470 switch (AL_reg(context)) {
1471 case 0x01:
1472 dataptr[0] = 0x1;
1473 *(WORD*)(dataptr+1) = 41;
1474 *(WORD*)(dataptr+3) = WINE_LanguageId;
1475 *(WORD*)(dataptr+5) = CodePage;
1476 break;
1477 case 0x06:
1478 dataptr[0] = 0x06;
1479 *(DWORD*)(dataptr+1) = MAKELONG(DOSMEM_CollateTable & 0xFFFF,DOSMEM_AllocSelector(DOSMEM_CollateTable>>16));
1480 CX_reg(context) = 258;/*FIXME: size of table?*/
1481 break;
1482 default:
1483 INT_BARF( context, 0x21 );
1484 SET_CFLAG(context);
1485 break;
1487 break;
1489 case 0x66: /* GLOBAL CODE PAGE TABLE */
1490 switch (AL_reg(context))
1492 case 0x01:
1493 DX_reg(context) = BX_reg(context) = CodePage;
1494 RESET_CFLAG(context);
1495 break;
1496 case 0x02:
1497 CodePage = BX_reg(context);
1498 RESET_CFLAG(context);
1499 break;
1501 break;
1503 case 0x67: /* SET HANDLE COUNT */
1504 SetHandleCount16( BX_reg(context) );
1505 if (DOS_ExtendedError) bSetDOSExtendedError = TRUE;
1506 break;
1508 case 0x68: /* "FFLUSH" - COMMIT FILE */
1509 case 0x6a: /* COMMIT FILE */
1510 bSetDOSExtendedError = (!FlushFileBuffers( BX_reg(context) ));
1511 break;
1513 case 0x69: /* DISK SERIAL NUMBER */
1514 switch (AL_reg(context))
1516 case 0x00:
1517 if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1518 else AX_reg(context) = 0;
1519 break;
1521 case 0x01:
1522 if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
1523 else AX_reg(context) = 1;
1524 break;
1526 break;
1528 case 0x6C: /* Extended Open/Create*/
1529 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1530 break;
1532 case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
1533 switch(AL_reg(context))
1535 case 0x39: /* Create directory */
1536 bSetDOSExtendedError = (!CreateDirectory32A(
1537 PTR_SEG_OFF_TO_LIN( DS_reg(context),
1538 DX_reg(context) ), NULL));
1539 break;
1540 case 0x3a: /* Remove directory */
1541 bSetDOSExtendedError = (!RemoveDirectory32A(
1542 PTR_SEG_OFF_TO_LIN( DS_reg(context),
1543 DX_reg(context) )));
1544 break;
1545 case 0x43: /* Get/Set file attributes */
1546 switch (BL_reg(context))
1548 case 0x00: /* Get file attributes */
1549 CX_reg(context) = (WORD)GetFileAttributes32A(
1550 PTR_SEG_OFF_TO_LIN(DS_reg(context),
1551 DX_reg(context)));
1552 if (CX_reg(context) == 0xffff) bSetDOSExtendedError = TRUE;
1553 break;
1554 case 0x01:
1555 bSetDOSExtendedError = (!SetFileAttributes32A(
1556 PTR_SEG_OFF_TO_LIN(DS_reg(context),
1557 DX_reg(context)),
1558 CX_reg(context) ) );
1559 break;
1560 default:
1561 fprintf( stderr,
1562 "Unimplemented int21 long file name function:\n");
1563 INT_BARF( context, 0x21 );
1564 SET_CFLAG(context);
1565 AL_reg(context) = 0;
1566 break;
1568 break;
1569 case 0x47: /* Get current directory */
1570 bSetDOSExtendedError = !INT21_GetCurrentDirectory(context);
1571 break;
1573 case 0x4e: /* Find first file */
1574 /* FIXME: use attributes in CX */
1575 if ((AX_reg(context) = FindFirstFile16(
1576 PTR_SEG_OFF_TO_LIN(DS_reg(context),DX_reg(context)),
1577 (WIN32_FIND_DATA32A *)PTR_SEG_OFF_TO_LIN(ES_reg(context),
1578 DI_reg(context))))
1579 == INVALID_HANDLE_VALUE16)
1580 bSetDOSExtendedError = TRUE;
1581 break;
1582 case 0x4f: /* Find next file */
1583 if (!FindNextFile16( BX_reg(context),
1584 (WIN32_FIND_DATA32A *)PTR_SEG_OFF_TO_LIN(ES_reg(context),
1585 DI_reg(context))))
1586 bSetDOSExtendedError = TRUE;
1587 break;
1588 case 0xa1: /* Find close */
1589 bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
1590 break;
1591 case 0xa0:
1592 break;
1593 case 0x60:
1594 switch(CL_reg(context))
1596 case 0x02: /*Get canonical long filename or path */
1597 if (!GetFullPathName32A
1598 ( PTR_SEG_OFF_TO_LIN(DS_reg(context),
1599 SI_reg(context)), 128,
1600 PTR_SEG_OFF_TO_LIN(ES_reg(context),
1601 DI_reg(context)),NULL))
1602 bSetDOSExtendedError = TRUE;
1603 else AX_reg(context) = 0;
1604 break;
1605 default:
1606 fprintf( stderr,
1607 "Unimplemented int21 long file name function:\n");
1608 INT_BARF( context, 0x21 );
1609 SET_CFLAG(context);
1610 AL_reg(context) = 0;
1611 break;
1613 break;
1614 case 0x6c: /* Create or open file */
1615 /* translate Dos 7 action to Dos 6 action */
1616 bSetDOSExtendedError = INT21_ExtendedOpenCreateFile(context);
1617 break;
1619 case 0x3b: /* Change directory */
1620 if (!SetCurrentDirectory32A(PTR_SEG_OFF_TO_LIN(
1621 DS_reg(context),
1622 DX_reg(context)
1625 SET_CFLAG(context);
1626 AL_reg(context) = DOS_ExtendedError;
1628 break;
1629 case 0x41: /* Delete file */
1630 case 0x56: /* Move (rename) file */
1631 default:
1632 fprintf( stderr, "Unimplemented int21 long file name function:\n");
1633 INT_BARF( context, 0x21 );
1634 SET_CFLAG(context);
1635 AL_reg(context) = 0;
1636 break;
1638 break;
1640 case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
1641 case 0x72: /* MS-DOS 7 (Windows95) - ??? */
1642 case 0x73: /* MS-DOS 7 (Windows95) - DRIVE LOCKING ??? */
1643 dprintf_int(stddeb,"int21: windows95 function AX %04x\n",
1644 AX_reg(context));
1645 dprintf_int(stddeb, " returning unimplemented\n");
1646 SET_CFLAG(context);
1647 AL_reg(context) = 0;
1648 break;
1650 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
1651 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
1652 break;
1654 default:
1655 INT_BARF( context, 0x21 );
1656 break;
1658 } /* END OF SWITCH */
1660 if( bSetDOSExtendedError ) /* set general error condition */
1662 AX_reg(context) = DOS_ExtendedError;
1663 SET_CFLAG(context);
1666 dprintf_int( stddeb, "ret21: AX=%04x BX=%04x CX=%04x DX=%04x "
1667 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1668 AX_reg(context), BX_reg(context), CX_reg(context),
1669 DX_reg(context), SI_reg(context), DI_reg(context),
1670 (WORD)DS_reg(context), (WORD)ES_reg(context),
1671 EFL_reg(context));