Fix the case of product and company names.
[wine] / msdos / int21.c
1 /*
2  * DOS interrupt 21h handler
3  *
4  * Copyright 1993, 1994 Erik Bos
5  * Copyright 1996 Alexandre Julliard
6  * Copyright 1997 Andreas Mohr
7  * Copyright 1998 Uwe Bonnes
8  * Copyright 1998, 1999 Ove Kaaven
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <time.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #ifdef HAVE_SYS_FILE_H
34 # include <sys/file.h>
35 #endif
36 #include <string.h>
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
39 #endif
40 #include <sys/types.h>
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
44 #ifdef HAVE_UTIME_H
45 # include <utime.h>
46 #endif
47 #include <ctype.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winreg.h"
51 #include "winternl.h"
52 #include "wingdi.h"
53 #include "winuser.h" /* SW_NORMAL */
54 #include "wine/winbase16.h"
55 #include "winerror.h"
56 #include "drive.h"
57 #include "file.h"
58 #include "callback.h"
59 #include "msdos.h"
60 #include "miscemu.h"
61 #include "task.h"
62 #include "wine/unicode.h"
63 #include "wine/debug.h"
64
65 WINE_DEFAULT_DEBUG_CHANNEL(int21);
66 #if defined(__svr4__) || defined(_SCO_DS)
67 /* SVR4 DOESNT do locking the same way must implement properly */
68 #define LOCK_EX 0
69 #define LOCK_SH  1
70 #define LOCK_NB  8
71 #endif
72
73
74 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
75
76 /* Define the drive parameter block, as used by int21/1F
77  * and int21/32.  This table can be accessed through the
78  * global 'dpb' pointer, which points into the local dos
79  * heap.
80  */
81 struct DPB
82 {
83     BYTE drive_num;         /* 0=A, etc. */
84     BYTE unit_num;          /* Drive's unit number (?) */
85     WORD sector_size;       /* Sector size in bytes */
86     BYTE high_sector;       /* Highest sector in a cluster */
87     BYTE shift;             /* Shift count (?) */
88     WORD reserved;          /* Number of reserved sectors at start */
89     BYTE num_FAT;           /* Number of FATs */
90     WORD dir_entries;       /* Number of root dir entries */
91     WORD first_data;        /* First data sector */
92     WORD high_cluster;      /* Highest cluster number */
93     WORD sectors_in_FAT;    /* Number of sectors per FAT */
94     WORD start_dir;         /* Starting sector of first dir */
95     DWORD driver_head;      /* Address of device driver header (?) */
96     BYTE media_ID;          /* Media ID */
97     BYTE access_flag;       /* Prev. accessed flag (0=yes,0xFF=no) */
98     DWORD next;             /* Pointer to next DPB in list */
99     WORD free_search;       /* Free cluster search start */
100     WORD free_clusters;     /* Number of free clusters (0xFFFF=unknown) */
101 };
102
103 struct EDPB                     /* FAT32 extended Drive Parameter Block */
104 {                               /* from Ralf Brown's Interrupt List */
105         struct DPB dpb;         /* first 24 bytes = original DPB */
106
107         BYTE edpb_flags;        /* undocumented/unknown flags */
108         DWORD next_edpb;        /* pointer to next EDPB */
109         WORD free_cluster;      /* cluster to start search for free space on write, typically
110                                    the last cluster allocated */
111         WORD clusters_free;     /* number of free clusters on drive or FFFF = unknown */
112         WORD clusters_free_hi;  /* hiword of clusters_free */
113         WORD mirroring_flags;   /* mirroring flags: bit 7 set = do not mirror active FAT */
114                                 /* bits 0-3 = 0-based number of the active FAT */
115         WORD info_sector;       /* sector number of file system info sector, or FFFF for none */
116         WORD spare_boot_sector; /* sector number of backup boot sector, or FFFF for none */
117         DWORD first_cluster;    /* sector number of the first cluster */
118         DWORD max_cluster;      /* sector number of the last cluster */
119         DWORD fat_clusters;     /* number of clusters occupied by FAT */
120         DWORD root_cluster;     /* cluster number of start of root directory */
121         DWORD free_cluster2;    /* same as free_cluster: cluster at which to start
122                                    search for free space when writing */
123
124 };
125
126 DWORD dpbsegptr;
127
128 struct DosHeap {
129         BYTE mediaID;
130         BYTE biosdate[8];
131         struct DPB dpb;
132 };
133 static struct DosHeap *heap;
134 static WORD DosHeapHandle;
135
136 extern char TempDirectory[];
137
138 static BOOL INT21_CreateHeap(void)
139 {
140     if (!(DosHeapHandle = GlobalAlloc16(GMEM_FIXED,sizeof(struct DosHeap))))
141     {
142         WARN("Out of memory\n");
143         return FALSE;
144     }
145     heap = (struct DosHeap *) GlobalLock16(DosHeapHandle);
146     dpbsegptr = MAKESEGPTR(DosHeapHandle,(int)&heap->dpb-(int)heap);
147     strcpy(heap->biosdate, "01/01/80");
148     return TRUE;
149 }
150
151 static BYTE *GetCurrentDTA( CONTEXT86 *context )
152 {
153     TDB *pTask = GlobalLock16(GetCurrentTask());
154
155     /* FIXME: This assumes DTA was set correctly! */
156     return (BYTE *)CTX_SEG_OFF_TO_LIN( context, SELECTOROF(pTask->dta),
157                                                 (DWORD)OFFSETOF(pTask->dta) );
158 }
159
160
161 void CreateBPB(int drive, BYTE *data, BOOL16 limited)
162 /* limited == TRUE is used with INT 0x21/0x440d */
163 {
164         if (drive > 1) {
165                 setword(data, 512);
166                 data[2] = 2;
167                 setword(&data[3], 0);
168                 data[5] = 2;
169                 setword(&data[6], 240);
170                 setword(&data[8], 64000);
171                 data[0x0a] = 0xf8;
172                 setword(&data[0x0b], 40);
173                 setword(&data[0x0d], 56);
174                 setword(&data[0x0f], 2);
175                 setword(&data[0x11], 0);
176                 if (!limited) {
177                     setword(&data[0x1f], 800);
178                     data[0x21] = 5;
179                     setword(&data[0x22], 1);
180                 }
181         } else { /* 1.44mb */
182                 setword(data, 512);
183                 data[2] = 2;
184                 setword(&data[3], 0);
185                 data[5] = 2;
186                 setword(&data[6], 240);
187                 setword(&data[8], 2880);
188                 data[0x0a] = 0xf8;
189                 setword(&data[0x0b], 6);
190                 setword(&data[0x0d], 18);
191                 setword(&data[0x0f], 2);
192                 setword(&data[0x11], 0);
193                 if (!limited) {
194                     setword(&data[0x1f], 80);
195                     data[0x21] = 7;
196                     setword(&data[0x22], 2);
197                 }
198         }
199 }
200
201 static int INT21_GetFreeDiskSpace( CONTEXT86 *context )
202 {
203     DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
204     char root[] = "A:\\";
205
206     *root += DOS_GET_DRIVE( DL_reg(context) );
207     if (!GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
208                               &free_clusters, &total_clusters )) return 0;
209     SET_AX( context, cluster_sectors );
210     SET_BX( context, free_clusters );
211     SET_CX( context, sector_bytes );
212     SET_DX( context, total_clusters );
213     return 1;
214 }
215
216 static int INT21_GetDriveAllocInfo( CONTEXT86 *context )
217 {
218     if (!INT21_GetFreeDiskSpace( context )) return 0;
219     if (!heap && !INT21_CreateHeap()) return 0;
220     heap->mediaID = 0xf0;
221     context->SegDs = DosHeapHandle;
222     SET_BX( context, (int)&heap->mediaID - (int)heap );
223     return 1;
224 }
225
226 static int FillInDrivePB( int drive )
227 {
228         if(!DRIVE_IsValid(drive))
229         {
230             SetLastError( ERROR_INVALID_DRIVE );
231                         return 0;
232         }
233         else if (heap || INT21_CreateHeap())
234         {
235                 /* FIXME: I have no idea what a lot of this information should
236                  * say or whether it even really matters since we're not allowing
237                  * direct block access.  However, some programs seem to depend on
238                  * getting at least _something_ back from here.  The 'next' pointer
239                  * does worry me, though.  Should we have a complete table of
240                  * separate DPBs per drive?  Probably, but I'm lazy. :-)  -CH
241                  */
242                 heap->dpb.drive_num = heap->dpb.unit_num = drive; /*The same?*/
243                 heap->dpb.sector_size = 512;
244                 heap->dpb.high_sector = 1;
245                 heap->dpb.shift = drive < 2 ? 0 : 6; /*6 for HD, 0 for floppy*/
246                 heap->dpb.reserved = 0;
247                 heap->dpb.num_FAT = 1;
248                 heap->dpb.dir_entries = 2;
249                 heap->dpb.first_data = 2;
250                 heap->dpb.high_cluster = 64000;
251                 heap->dpb.sectors_in_FAT = 1;
252                 heap->dpb.start_dir = 1;
253                 heap->dpb.driver_head = 0;
254                 heap->dpb.media_ID = (drive > 1) ? 0xF8 : 0xF0;
255                 heap->dpb.access_flag = 0;
256                 heap->dpb.next = 0;
257                 heap->dpb.free_search = 0;
258                 heap->dpb.free_clusters = 0xFFFF;    /* unknown */
259                                 return 1;
260                 }
261
262                 return 0;
263 }
264
265 static void GetDrivePB( CONTEXT86 *context, int drive )
266 {
267         if (FillInDrivePB( drive ))
268         {
269                 SET_AL( context, 0x00 );
270                 context->SegDs = SELECTOROF(dpbsegptr);
271                 SET_BX( context, OFFSETOF(dpbsegptr) );
272         }
273         else
274         {
275         SET_AX( context, 0x00ff );
276         }
277 }
278
279
280 static BOOL ioctlGenericBlkDevReq( CONTEXT86 *context )
281 {
282         BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
283         int drive = DOS_GET_DRIVE( BL_reg(context) );
284
285         if (!DRIVE_IsValid(drive))
286         {
287             SetLastError( ERROR_FILE_NOT_FOUND );
288             return TRUE;
289         }
290
291         if (CH_reg(context) != 0x08)
292         {
293             INT_BARF( context, 0x21 );
294             return FALSE;
295         }
296
297         switch (CL_reg(context))
298         {
299                 case 0x60: /* get device parameters */
300                            /* used by w4wgrp's winfile */
301                         memset(dataptr, 0, 0x20); /* DOS 6.22 uses 0x20 bytes */
302                         dataptr[0] = 0x04;
303                         dataptr[6] = 0; /* media type */
304                         if (drive > 1)
305                         {
306                                 dataptr[1] = 0x05; /* fixed disk */
307                                 setword(&dataptr[2], 0x01); /* non removable */
308                                 setword(&dataptr[4], 0x300); /* # of cylinders */
309                         }
310                         else
311                         {
312                                 dataptr[1] = 0x07; /* block dev, floppy */
313                                 setword(&dataptr[2], 0x02); /* removable */
314                                 setword(&dataptr[4], 80); /* # of cylinders */
315                         }
316                         CreateBPB(drive, &dataptr[7], TRUE);
317                         RESET_CFLAG(context);
318                         break;
319
320                 case 0x66:/*  get disk serial number */
321                         {
322                                 char    label[12],fsname[9],path[4];
323                                 DWORD   serial;
324
325                                 strcpy(path,"x:\\");path[0]=drive+'A';
326                                 GetVolumeInformationA(
327                                         path,label,12,&serial,NULL,NULL,fsname,9
328                                 );
329                                 *(WORD*)dataptr         = 0;
330                                 memcpy(dataptr+2,&serial,4);
331                                 memcpy(dataptr+6,label  ,11);
332                                 memcpy(dataptr+17,fsname,8);
333                         }
334                         break;
335
336                 case 0x6f:
337                         memset(dataptr+1, '\0', dataptr[0]-1);
338                         dataptr[1] = dataptr[0];
339                         dataptr[2] = 0x07; /* protected mode driver; no eject; no notification */
340                         dataptr[3] = 0xFF; /* no physical drive */
341                         break;
342
343                 case 0x72:
344                         /* Trail on error implementation */
345                         SET_AX( context, GetDriveType16(BL_reg(context)) == DRIVE_UNKNOWN ? 0x0f : 0x01 );
346                         SET_CFLAG(context);     /* Seems to be set all the time */
347                         break;
348
349                 default:
350                         INT_BARF( context, 0x21 );
351         }
352         return FALSE;
353 }
354
355 static void INT21_ParseFileNameIntoFCB( CONTEXT86 *context )
356 {
357     char *filename =
358         CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Esi );
359     char *fcb =
360         CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi );
361     char *s;
362     WCHAR *buffer;
363     WCHAR fcbW[12];
364     INT buffer_len, len;
365
366     SET_AL( context, 0xff ); /* failed */
367
368     TRACE("filename: '%s'\n", filename);
369
370     s = filename;
371     len = 0;
372     while (*s)
373     {
374         if ((*s != ' ') && (*s != '\r') && (*s != '\n'))
375         {
376             s++;
377             len++;
378         }
379         else
380             break;
381     }
382
383     buffer_len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, NULL, 0);
384     buffer = HeapAlloc( GetProcessHeap(), 0, (buffer_len + 1) * sizeof(WCHAR));
385     len = MultiByteToWideChar(CP_OEMCP, 0, filename, len, buffer, buffer_len);
386     buffer[len] = 0;
387     DOSFS_ToDosFCBFormat(buffer, fcbW);
388     HeapFree(GetProcessHeap(), 0, buffer);
389     WideCharToMultiByte(CP_OEMCP, 0, fcbW, 12, fcb + 1, 12, NULL, NULL);
390     *fcb = 0;
391     TRACE("FCB: '%s'\n", fcb + 1);
392
393     SET_AL( context, ((strchr(filename, '*')) || (strchr(filename, '$'))) != 0 );
394
395     /* point DS:SI to first unparsed character */
396     SET_SI( context, context->Esi + (int)s - (int)filename );
397 }
398
399
400 /* Many calls translate a drive argument like this:
401    drive number (00h = default, 01h = A:, etc)
402    */
403 static char drivestring[]="default";
404
405 char *INT21_DriveName(int drive)
406 {
407
408     if(drive >0)
409       {
410         drivestring[0]= (unsigned char)drive + '@';
411         drivestring[1]=':';
412         drivestring[2]=0;
413       }
414     return drivestring;
415 }
416
417 static HFILE16 _lcreat16_uniq( LPCSTR path, INT attr )
418 {
419     /* Mask off all flags not explicitly allowed by the doc */
420     attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
421     return Win32HandleToDosFileHandle( CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
422                                              FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
423                                              CREATE_NEW, attr, 0 ));
424 }
425
426
427 static int INT21_FindFirst( CONTEXT86 *context )
428 {
429     char *p;
430     const char *path;
431     DOS_FULL_NAME full_name;
432     FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
433     WCHAR pathW[MAX_PATH];
434     WCHAR maskW[12];
435
436     path = (const char *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
437     MultiByteToWideChar(CP_OEMCP, 0, path, -1, pathW, MAX_PATH);
438
439     dta->unixPath = NULL;
440     if (!DOSFS_GetFullName( pathW, FALSE, &full_name ))
441     {
442         SET_AX( context, GetLastError() );
443         SET_CFLAG(context);
444         return 0;
445     }
446     dta->unixPath = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.long_name)+1 );
447     strcpy( dta->unixPath, full_name.long_name );
448     p = strrchr( dta->unixPath, '/' );
449     *p = '\0';
450
451     MultiByteToWideChar(CP_OEMCP, 0, p + 1, -1, pathW, MAX_PATH);
452
453     /* Note: terminating NULL in dta->mask overwrites dta->search_attr
454      *       (doesn't matter as it is set below anyway)
455      */
456     if (!DOSFS_ToDosFCBFormat( pathW, maskW ))
457     {
458         HeapFree( GetProcessHeap(), 0, dta->unixPath );
459         dta->unixPath = NULL;
460         SetLastError( ERROR_FILE_NOT_FOUND );
461         SET_AX( context, ERROR_FILE_NOT_FOUND );
462         SET_CFLAG(context);
463         return 0;
464     }
465     WideCharToMultiByte(CP_OEMCP, 0, maskW, 12, dta->mask, sizeof(dta->mask), NULL, NULL);
466     dta->drive = (path[0] && (path[1] == ':')) ? toupper(path[0]) - 'A'
467                                                : DRIVE_GetCurrentDrive();
468     dta->count = 0;
469     dta->search_attr = CL_reg(context);
470     return 1;
471 }
472
473
474 static int INT21_FindNext( CONTEXT86 *context )
475 {
476     FINDFILE_DTA *dta = (FINDFILE_DTA *)GetCurrentDTA(context);
477     WIN32_FIND_DATAA entry;
478     int count;
479
480     if (!dta->unixPath) return 0;
481     if (!(count = DOSFS_FindNext( dta->unixPath, dta->mask, NULL, dta->drive,
482                                   dta->search_attr, dta->count, &entry )))
483     {
484         HeapFree( GetProcessHeap(), 0, dta->unixPath );
485         dta->unixPath = NULL;
486         return 0;
487     }
488     if ((int)dta->count + count > 0xffff)
489     {
490         WARN("Too many directory entries in %s\n", dta->unixPath );
491         HeapFree( GetProcessHeap(), 0, dta->unixPath );
492         dta->unixPath = NULL;
493         return 0;
494     }
495     dta->count += count;
496     dta->fileattr = entry.dwFileAttributes;
497     dta->filesize = entry.nFileSizeLow;
498     FileTimeToDosDateTime( &entry.ftLastWriteTime,
499                            &dta->filedate, &dta->filetime );
500     strcpy( dta->filename, entry.cAlternateFileName );
501     if (!memchr(dta->mask,'?',11)) {
502         /* wildcardless search, release resources in case no findnext will
503          * be issued, and as a workaround in case file creation messes up
504          * findnext, as sometimes happens with pkunzip */
505         HeapFree( GetProcessHeap(), 0, dta->unixPath );
506         dta->unixPath = NULL;
507     }
508     return 1;
509 }
510
511
512 static BOOL INT21_CreateTempFile( CONTEXT86 *context )
513 {
514     static int counter = 0;
515     char *name = CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx );
516     char *p = name + strlen(name);
517
518     /* despite what Ralf Brown says, some programs seem to call without
519      * ending backslash (DOS accepts that, so we accept it too) */
520     if ((p == name) || (p[-1] != '\\')) *p++ = '\\';
521
522     for (;;)
523     {
524         sprintf( p, "wine%04x.%03d", (int)getpid(), counter );
525         counter = (counter + 1) % 1000;
526
527         SET_AX( context, _lcreat16_uniq( name, 0 ) );
528         if (AX_reg(context) != HFILE_ERROR16)
529         {
530             TRACE("created %s\n", name );
531             return TRUE;
532         }
533         if (GetLastError() != ERROR_FILE_EXISTS) return FALSE;
534     }
535 }
536
537
538 static int INT21_GetDiskSerialNumber( CONTEXT86 *context )
539 {
540     BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
541     int drive = DOS_GET_DRIVE( BL_reg(context) );
542
543     if (!DRIVE_IsValid(drive))
544     {
545         SetLastError( ERROR_INVALID_DRIVE );
546         return 0;
547     }
548
549     *(WORD *)dataptr = 0;
550     *(DWORD *)(dataptr + 2) = DRIVE_GetSerialNumber( drive );
551     memcpy( dataptr + 6, DRIVE_GetLabel( drive ), 11 );
552     strncpy(dataptr + 0x11, "FAT16   ", 8);
553     return 1;
554 }
555
556
557 static int INT21_SetDiskSerialNumber( CONTEXT86 *context )
558 {
559     BYTE *dataptr = CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
560     int drive = DOS_GET_DRIVE( BL_reg(context) );
561
562     if (!DRIVE_IsValid(drive))
563     {
564         SetLastError( ERROR_INVALID_DRIVE );
565         return 0;
566     }
567
568     DRIVE_SetSerialNumber( drive, *(DWORD *)(dataptr + 2) );
569     return 1;
570 }
571
572
573 /* microsoft's programmers should be shot for using CP/M style int21
574    calls in Windows for Workgroup's winfile.exe */
575
576 static int INT21_FindFirstFCB( CONTEXT86 *context )
577 {
578     BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
579     FINDFILE_FCB *pFCB;
580     LPCSTR root, cwd;
581     int drive;
582
583     if (*fcb == 0xff) pFCB = (FINDFILE_FCB *)(fcb + 7);
584     else pFCB = (FINDFILE_FCB *)fcb;
585     drive = DOS_GET_DRIVE( pFCB->drive );
586     if (!DRIVE_IsValid( drive )) return 0;
587     root = DRIVE_GetRoot( drive );
588     cwd  = DRIVE_GetUnixCwd( drive );
589     pFCB->unixPath = HeapAlloc( GetProcessHeap(), 0,
590                                 strlen(root)+strlen(cwd)+2 );
591     if (!pFCB->unixPath) return 0;
592     strcpy( pFCB->unixPath, root );
593     strcat( pFCB->unixPath, "/" );
594     strcat( pFCB->unixPath, cwd );
595     pFCB->count = 0;
596     return 1;
597 }
598
599
600 static int INT21_FindNextFCB( CONTEXT86 *context )
601 {
602     BYTE *fcb = (BYTE *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
603     FINDFILE_FCB *pFCB;
604     DOS_DIRENTRY_LAYOUT *pResult = (DOS_DIRENTRY_LAYOUT *)GetCurrentDTA(context);
605     WIN32_FIND_DATAA entry;
606     BYTE attr;
607     int count;
608
609     if (*fcb == 0xff) /* extended FCB ? */
610     {
611         attr = fcb[6];
612         pFCB = (FINDFILE_FCB *)(fcb + 7);
613     }
614     else
615     {
616         attr = 0;
617         pFCB = (FINDFILE_FCB *)fcb;
618     }
619
620     if (!pFCB->unixPath) return 0;
621     if (!(count = DOSFS_FindNext( pFCB->unixPath, pFCB->filename, NULL,
622                                   DOS_GET_DRIVE( pFCB->drive ), attr,
623                                   pFCB->count, &entry )))
624     {
625         HeapFree( GetProcessHeap(), 0, pFCB->unixPath );
626         pFCB->unixPath = NULL;
627         return 0;
628     }
629     pFCB->count += count;
630
631     if (*fcb == 0xff) { /* place extended FCB header before pResult if called with extended FCB */
632         *(BYTE *)pResult = 0xff;
633         (BYTE *)pResult +=6; /* leave reserved field behind */
634         *(BYTE *)pResult = entry.dwFileAttributes;
635         ((BYTE *)pResult)++;
636     }
637     *(BYTE *)pResult = DOS_GET_DRIVE( pFCB->drive ); /* DOS_DIRENTRY_LAYOUT after current drive number */
638     ((BYTE *)pResult)++;
639     pResult->fileattr = entry.dwFileAttributes;
640     pResult->cluster  = 0;  /* what else? */
641     pResult->filesize = entry.nFileSizeLow;
642     memset( pResult->reserved, 0, sizeof(pResult->reserved) );
643     FileTimeToDosDateTime( &entry.ftLastWriteTime,
644                            &pResult->filedate, &pResult->filetime );
645
646     /* Convert file name to FCB format */
647
648     memset( pResult->filename, ' ', sizeof(pResult->filename) );
649     if (!strcmp( entry.cAlternateFileName, "." )) pResult->filename[0] = '.';
650     else if (!strcmp( entry.cAlternateFileName, ".." ))
651         pResult->filename[0] = pResult->filename[1] = '.';
652     else
653     {
654         char *p = strrchr( entry.cAlternateFileName, '.' );
655         if (p && p[1] && (p != entry.cAlternateFileName))
656         {
657             memcpy( pResult->filename, entry.cAlternateFileName,
658                     min( (p - entry.cAlternateFileName), 8 ) );
659             memcpy( pResult->filename + 8, p + 1, min( strlen(p), 3 ) );
660         }
661         else
662             memcpy( pResult->filename, entry.cAlternateFileName,
663                     min( strlen(entry.cAlternateFileName), 8 ) );
664     }
665     return 1;
666 }
667
668
669 static BOOL
670 INT21_networkfunc (CONTEXT86 *context)
671 {
672      switch (AL_reg(context)) {
673      case 0x00: /* Get machine name. */
674      {
675           char *dst = CTX_SEG_OFF_TO_LIN (context,context->SegDs,context->Edx);
676           TRACE("getting machine name to %p\n", dst);
677           if (gethostname (dst, 15))
678           {
679                WARN("failed!\n");
680                SetLastError( ER_NoNetwork );
681                return TRUE;
682           } else {
683                int len = strlen (dst);
684                while (len < 15)
685                     dst[len++] = ' ';
686                dst[15] = 0;
687                SET_CH( context, 1 ); /* Valid */
688                SET_CL( context, 1 ); /* NETbios number??? */
689                TRACE("returning %s\n", debugstr_an (dst, 16));
690                return FALSE;
691           }
692      }
693
694      default:
695           SetLastError( ER_NoNetwork );
696           return TRUE;
697      }
698 }
699
700
701 /***********************************************************************
702  *           INT_Int21Handler
703  */
704 void WINAPI INT_Int21Handler( CONTEXT86 *context )
705 {
706     BOOL        bSetDOSExtendedError = FALSE;
707
708     switch(AH_reg(context))
709     {
710     case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
711         TRACE("FIND FIRST MATCHING FILE USING FCB %p\n",
712               CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx));
713         if (!INT21_FindFirstFCB(context))
714         {
715             SET_AL( context, 0xff );
716             break;
717         }
718         /* else fall through */
719
720     case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
721         SET_AL( context, INT21_FindNextFCB(context) ? 0x00 : 0xff );
722         break;
723
724     case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
725         SET_DL( context, 0 );
726         if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
727         break;
728
729     case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
730         if (!INT21_GetDriveAllocInfo(context)) SET_AX( context, 0xffff );
731         break;
732
733     case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
734         GetDrivePB(context, DRIVE_GetCurrentDrive());
735         break;
736
737     case 0x29: /* PARSE FILENAME INTO FCB */
738         INT21_ParseFileNameIntoFCB(context);
739         break;
740
741     case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
742         TRACE("GET DOS DRIVE PARAMETER BLOCK FOR DRIVE %s\n",
743               INT21_DriveName( DL_reg(context)));
744         GetDrivePB(context, DOS_GET_DRIVE( DL_reg(context) ) );
745         break;
746
747     case 0x36: /* GET FREE DISK SPACE */
748         TRACE("GET FREE DISK SPACE FOR DRIVE %s\n",
749               INT21_DriveName( DL_reg(context)));
750         if (!INT21_GetFreeDiskSpace(context)) SET_AX( context, 0xffff );
751         break;
752
753     case 0x44: /* IOCTL */
754         switch (AL_reg(context))
755         {
756         case 0x0d:
757             TRACE("IOCTL - GENERIC BLOCK DEVICE REQUEST %s\n",
758                   INT21_DriveName( BL_reg(context)));
759             bSetDOSExtendedError = ioctlGenericBlkDevReq(context);
760             break;
761
762         case 0x0F:   /* Set logical drive mapping */
763             {
764             int drive;
765             TRACE("IOCTL - SET LOGICAL DRIVE MAP for drive %s\n",
766                   INT21_DriveName( BL_reg(context)));
767             drive = DOS_GET_DRIVE ( BL_reg(context) );
768             if ( ! DRIVE_SetLogicalMapping ( drive, drive+1 ) )
769             {
770                 SET_CFLAG(context);
771                 SET_AX( context, 0x000F );  /* invalid drive */
772             }
773             break;
774             }
775         }
776         break;
777
778     case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
779         TRACE("FINDFIRST mask 0x%04x spec %s\n",CX_reg(context),
780               (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs, context->Edx));
781         if (!INT21_FindFirst(context)) break;
782         /* fall through */
783
784     case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
785         TRACE("FINDNEXT\n");
786         if (!INT21_FindNext(context))
787         {
788             SetLastError( ERROR_NO_MORE_FILES );
789             SET_AX( context, ERROR_NO_MORE_FILES );
790             SET_CFLAG(context);
791         }
792         else SET_AX( context, 0 );  /* OK */
793         break;
794
795     case 0x5a: /* CREATE TEMPORARY FILE */
796         TRACE("CREATE TEMPORARY FILE\n");
797         bSetDOSExtendedError = !INT21_CreateTempFile(context);
798         break;
799
800     case 0x5e:
801         bSetDOSExtendedError = INT21_networkfunc (context);
802         break;
803
804     case 0x5f: /* NETWORK */
805         switch (AL_reg(context))
806         {
807         case 0x07: /* ENABLE DRIVE */
808             TRACE("ENABLE DRIVE %c:\n",(DL_reg(context)+'A'));
809             if (!DRIVE_Enable( DL_reg(context) ))
810             {
811                 SetLastError( ERROR_INVALID_DRIVE );
812                 bSetDOSExtendedError = TRUE;
813             }
814             break;
815
816         case 0x08: /* DISABLE DRIVE */
817             TRACE("DISABLE DRIVE %c:\n",(DL_reg(context)+'A'));
818             if (!DRIVE_Disable( DL_reg(context) ))
819             {
820                 SetLastError( ERROR_INVALID_DRIVE );
821                 bSetDOSExtendedError = TRUE;
822             }
823             break;
824
825         default:
826             /* network software not installed */
827             TRACE("NETWORK function AX=%04x not implemented\n",AX_reg(context));
828             SetLastError( ER_NoNetwork );
829             bSetDOSExtendedError = TRUE;
830             break;
831         }
832         break;
833
834     case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
835         TRACE("TRUENAME %s\n",
836               (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Esi));
837         {
838             if (!GetFullPathNameA( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
839                                                         context->Esi), 128,
840                                      CTX_SEG_OFF_TO_LIN(context, context->SegEs,
841                                                         context->Edi),NULL))
842                 bSetDOSExtendedError = TRUE;
843             else SET_AX( context, 0 );
844         }
845         break;
846
847     case 0x69: /* DISK SERIAL NUMBER */
848         switch (AL_reg(context))
849         {
850         case 0x00:
851             TRACE("GET DISK SERIAL NUMBER for drive %s\n",
852                   INT21_DriveName(BL_reg(context)));
853             if (!INT21_GetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
854             else SET_AX( context, 0 );
855             break;
856
857         case 0x01:
858             TRACE("SET DISK SERIAL NUMBER for drive %s\n",
859                   INT21_DriveName(BL_reg(context)));
860             if (!INT21_SetDiskSerialNumber(context)) bSetDOSExtendedError = TRUE;
861             else SET_AX( context, 1 );
862             break;
863         }
864         break;
865
866     case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
867         switch(AL_reg(context))
868         {
869         case 0x4e:  /* Find first file */
870             TRACE(" LONG FILENAME - FIND FIRST MATCHING FILE for %s\n",
871                   (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx));
872             /* FIXME: use attributes in CX */
873             SET_AX( context, FindFirstFile16(
874                         CTX_SEG_OFF_TO_LIN(context, context->SegDs,context->Edx),
875                         (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
876                                                                context->Edi)));
877             if (AX_reg(context) == INVALID_HANDLE_VALUE16)
878                 bSetDOSExtendedError = TRUE;
879             break;
880         case 0x4f:  /* Find next file */
881             TRACE("LONG FILENAME - FIND NEXT MATCHING FILE for handle %d\n",
882                   BX_reg(context));
883             if (!FindNextFile16( BX_reg(context),
884                     (WIN32_FIND_DATAA *)CTX_SEG_OFF_TO_LIN(context, context->SegEs,
885                                                              context->Edi)))
886                 bSetDOSExtendedError = TRUE;
887             break;
888         case 0xa0:
889             {
890                 LPCSTR driveroot = (LPCSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegDs,context->Edx);
891                 LPSTR buffer = (LPSTR)CTX_SEG_OFF_TO_LIN(context,  context->SegEs,context->Edi);
892                 DWORD filename_len, flags;
893
894                 TRACE("LONG FILENAME - GET VOLUME INFORMATION for drive having root dir '%s'.\n", driveroot);
895                 SET_AX( context, 0 );
896                 if (!GetVolumeInformationA( driveroot, NULL, 0, NULL, &filename_len,
897                                             &flags, buffer, 8 ))
898                 {
899                     INT_BARF( context, 0x21 );
900                     SET_CFLAG(context);
901                     break;
902                 }
903                 SET_BX( context, flags | 0x4000 ); /* support for LFN functions */
904                 SET_CX( context, filename_len );
905                 SET_DX( context, MAX_PATH ); /* FIXME: which len if DRIVE_SHORT_NAMES ? */
906             }
907             break;
908         case 0xa1:  /* Find close */
909             TRACE("LONG FILENAME - FINDCLOSE for handle %d\n",
910                   BX_reg(context));
911             bSetDOSExtendedError = (!FindClose16( BX_reg(context) ));
912             break;
913         case 0x60:
914           switch(CL_reg(context))
915           {
916             case 0x01:  /* Get short filename or path */
917               if (!GetShortPathNameA
918                   ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
919                                        context->Esi),
920                     CTX_SEG_OFF_TO_LIN(context, context->SegEs,
921                                        context->Edi), 67))
922                 bSetDOSExtendedError = TRUE;
923               else SET_AX( context, 0 );
924               break;
925             case 0x02:  /* Get canonical long filename or path */
926               if (!GetFullPathNameA
927                   ( CTX_SEG_OFF_TO_LIN(context, context->SegDs,
928                                        context->Esi), 128,
929                     CTX_SEG_OFF_TO_LIN(context, context->SegEs,
930                                        context->Edi),NULL))
931                 bSetDOSExtendedError = TRUE;
932               else SET_AX( context, 0 );
933               break;
934             default:
935               FIXME("Unimplemented long file name function:\n");
936               INT_BARF( context, 0x21 );
937               SET_CFLAG(context);
938               SET_AL( context, 0 );
939               break;
940           }
941           break;
942
943         default:
944             FIXME("Unimplemented long file name function:\n");
945             INT_BARF( context, 0x21 );
946             SET_CFLAG(context);
947             SET_AL( context, 0 );
948             break;
949         }
950         break;
951
952
953     case 0x73: /* MULTIPLEXED: Win95 OSR2/Win98 FAT32 calls */
954         TRACE("windows95 function AX %04x\n",
955                     AX_reg(context));
956
957         switch (AL_reg(context))
958                 {
959                 case 0x02:      /* Get Extended Drive Parameter Block for specific drive */
960                                 /* ES:DI points to word with length of data (should be 0x3d) */
961                         {
962                                 WORD *buffer;
963                                 struct EDPB *edpb;
964                             DWORD cluster_sectors, sector_bytes, free_clusters, total_clusters;
965                             char root[] = "A:\\";
966
967                                 buffer = (WORD *)CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Edi);
968
969                                 TRACE("Get Extended DPB: linear buffer address is %p\n", buffer);
970
971                                 /* validate passed-in buffer lengths */
972                                 if ((*buffer != 0x3d) || (context->Ecx != 0x3f))
973                                 {
974                                         WARN("Get Extended DPB: buffer lengths incorrect\n");
975                                         WARN("CX = %lx, buffer[0] = %x\n", context->Ecx, *buffer);
976                                         SET_CFLAG(context);
977                                         SET_AL( context, 0x18 );                /* bad buffer length */
978                                 }
979
980                                 /* buffer checks out */
981                                 buffer++;               /* skip over length word now */
982                                 if (FillInDrivePB( DX_reg(context) ) )
983                                 {
984                                         edpb = (struct EDPB *)buffer;
985
986                                         /* copy down the old-style DPB portion first */
987                                         memcpy(&edpb->dpb, &heap->dpb, sizeof(struct DPB));
988
989                                         /* now fill in the extended entries */
990                                         edpb->edpb_flags = 0;
991                                         edpb->next_edpb = 0;
992                                         edpb->free_cluster = edpb->free_cluster2 = 0;
993
994                                         /* determine free disk space */
995                                     *root += DOS_GET_DRIVE( DX_reg(context) );
996                                     GetDiskFreeSpaceA( root, &cluster_sectors, &sector_bytes,
997                               &free_clusters, &total_clusters );
998
999                                         edpb->clusters_free = (free_clusters&0xffff);
1000
1001                                         edpb->clusters_free_hi = free_clusters >> 16;
1002                                         edpb->mirroring_flags = 0;
1003                                         edpb->info_sector = 0xffff;
1004                                         edpb->spare_boot_sector = 0xffff;
1005                                         edpb->first_cluster = 0;
1006                                         edpb->max_cluster = total_clusters;
1007                                         edpb->fat_clusters = 32;        /* made-up value */
1008                                         edpb->root_cluster = 0;
1009
1010                                         RESET_CFLAG(context);   /* clear carry */
1011                                         SET_AX( context, 0 );
1012                                 }
1013                                 else
1014                                 {
1015                                     SET_AX( context, 0x00ff );
1016                                     SET_CFLAG(context);
1017                                 }
1018                         }
1019                         break;
1020
1021                 case 0x03:      /* Get Extended free space on drive */
1022                 case 0x04:  /* Set DPB for formatting */
1023                 case 0x05:  /* extended absolute disk read/write */
1024                         FIXME("Unimplemented FAT32 int32 function %04x\n", AX_reg(context));
1025                         SET_CFLAG(context);
1026                         SET_AL( context, 0 );
1027                         break;
1028                 }
1029
1030                 break;
1031
1032     default:
1033         INT_BARF( context, 0x21 );
1034         break;
1035
1036     } /* END OF SWITCH */
1037
1038     if( bSetDOSExtendedError )          /* set general error condition */
1039     {
1040         SET_AX( context, GetLastError() );
1041         SET_CFLAG(context);
1042     }
1043 }