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