Release 980517
[wine] / files / drive.c
1 /*
2  * DOS drives handling functions
3  *
4  * Copyright 1993 Erik Bos
5  * Copyright 1996 Alexandre Julliard
6  */
7
8 #include "config.h"
9
10 #include <assert.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <errno.h>
18
19 #ifdef HAVE_SYS_PARAM_H
20 # include <sys/param.h>
21 #endif
22 #ifdef STATFS_DEFINED_BY_SYS_VFS
23 # include <sys/vfs.h>
24 #else
25 # ifdef STATFS_DEFINED_BY_SYS_MOUNT
26 #  include <sys/mount.h>
27 # else
28 #  ifdef STATFS_DEFINED_BY_SYS_STATFS
29 #   include <sys/statfs.h>
30 #  endif
31 # endif
32 #endif
33
34 #include "windows.h"
35 #include "winbase.h"
36 #include "drive.h"
37 #include "file.h"
38 #include "heap.h"
39 #include "msdos.h"
40 #include "options.h"
41 #include "task.h"
42 #include "debug.h"
43
44 typedef struct
45 {
46     char     *root;      /* root dir in Unix format without trailing / */
47     char     *dos_cwd;   /* cwd in DOS format without leading or trailing \ */
48     char     *unix_cwd;  /* cwd in Unix format without leading or trailing / */
49     char     *device;    /* raw device path */
50     char      label[12]; /* drive label */
51     DWORD     serial;    /* drive serial number */
52     DRIVETYPE type;      /* drive type */
53     UINT32    flags;     /* drive flags */
54     dev_t     dev;       /* unix device number */
55     ino_t     ino;       /* unix inode number */
56 } DOSDRIVE;
57
58
59 static const char * const DRIVE_Types[] =
60 {
61     "floppy",   /* TYPE_FLOPPY */
62     "hd",       /* TYPE_HD */
63     "cdrom",    /* TYPE_CDROM */
64     "network"   /* TYPE_NETWORK */
65 };
66
67
68 /* Known filesystem types */
69
70 typedef struct
71 {
72     const char *name;
73     UINT32      flags;
74 } FS_DESCR;
75
76 static const FS_DESCR DRIVE_Filesystems[] =
77 {
78     { "unix",   DRIVE_CASE_SENSITIVE | DRIVE_CASE_PRESERVING },
79     { "msdos",  DRIVE_SHORT_NAMES },
80     { "dos",    DRIVE_SHORT_NAMES },
81     { "fat",    DRIVE_SHORT_NAMES },
82     { "vfat",   DRIVE_CASE_PRESERVING },
83     { "win95",  DRIVE_CASE_PRESERVING },
84     { NULL, 0 }
85 };
86
87
88 static DOSDRIVE DOSDrives[MAX_DOS_DRIVES];
89 static int DRIVE_CurDrive = -1;
90
91 static HTASK16 DRIVE_LastTask = 0;
92
93
94 /***********************************************************************
95  *           DRIVE_GetDriveType
96  */
97 static DRIVETYPE DRIVE_GetDriveType( const char *name )
98 {
99     char buffer[20];
100     int i;
101
102     PROFILE_GetWineIniString( name, "Type", "hd", buffer, sizeof(buffer) );
103     for (i = 0; i < sizeof(DRIVE_Types)/sizeof(DRIVE_Types[0]); i++)
104     {
105         if (!lstrcmpi32A( buffer, DRIVE_Types[i] )) return (DRIVETYPE)i;
106     }
107     MSG("%s: unknown type '%s', defaulting to 'hd'.\n", name, buffer );
108     return TYPE_HD;
109 }
110
111
112 /***********************************************************************
113  *           DRIVE_GetFSFlags
114  */
115 static UINT32 DRIVE_GetFSFlags( const char *name, const char *value )
116 {
117     const FS_DESCR *descr;
118
119     for (descr = DRIVE_Filesystems; descr->name; descr++)
120         if (!lstrcmpi32A( value, descr->name )) return descr->flags;
121     MSG("%s: unknown filesystem type '%s', defaulting to 'unix'.\n",
122         name, value );
123     return DRIVE_CASE_SENSITIVE | DRIVE_CASE_PRESERVING;
124 }
125
126
127 /***********************************************************************
128  *           DRIVE_Init
129  */
130 int DRIVE_Init(void)
131 {
132     int i, len, count = 0;
133     char name[] = "Drive A";
134     char path[MAX_PATHNAME_LEN];
135     char buffer[80];
136     struct stat drive_stat_buffer;
137     char *p;
138     DOSDRIVE *drive;
139
140     for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, name[6]++, drive++)
141     {
142         PROFILE_GetWineIniString( name, "Path", "", path, sizeof(path)-1 );
143         if (path[0])
144         {
145             p = path + strlen(path) - 1;
146             while ((p > path) && ((*p == '/') || (*p == '\\'))) *p-- = '\0';
147             if (!path[0]) strcpy( path, "/" );
148
149             if (stat( path, &drive_stat_buffer ))
150             {
151                 MSG("Could not stat %s, ignoring drive %c:\n", path, 'A' + i );
152                 continue;
153             }
154             if (!S_ISDIR(drive_stat_buffer.st_mode))
155             {
156                 MSG("%s is not a directory, ignoring drive %c:\n",
157                     path, 'A' + i );
158                 continue;
159             }
160
161             drive->root = HEAP_strdupA( SystemHeap, 0, path );
162             drive->dos_cwd  = HEAP_strdupA( SystemHeap, 0, "" );
163             drive->unix_cwd = HEAP_strdupA( SystemHeap, 0, "" );
164             drive->type     = DRIVE_GetDriveType( name );
165             drive->device   = NULL;
166             drive->flags    = 0;
167             drive->dev      = drive_stat_buffer.st_dev;
168             drive->ino      = drive_stat_buffer.st_ino;
169
170             /* Get the drive label */
171             PROFILE_GetWineIniString( name, "Label", name, drive->label, 12 );
172             if ((len = strlen(drive->label)) < 11)
173             {
174                 /* Pad label with spaces */
175                 memset( drive->label + len, ' ', 11 - len );
176                 drive->label[12] = '\0';
177             }
178
179             /* Get the serial number */
180             PROFILE_GetWineIniString( name, "Serial", "12345678",
181                                       buffer, sizeof(buffer) );
182             drive->serial = strtoul( buffer, NULL, 16 );
183
184             /* Get the filesystem type */
185             PROFILE_GetWineIniString( name, "Filesystem", "unix",
186                                       buffer, sizeof(buffer) );
187             drive->flags = DRIVE_GetFSFlags( name, buffer );
188
189             /* Get the device */
190             PROFILE_GetWineIniString( name, "Device", "",
191                                       buffer, sizeof(buffer) );
192             if (buffer[0])
193                 drive->device = HEAP_strdupA( SystemHeap, 0, buffer );
194
195             /* Make the first hard disk the current drive */
196             if ((DRIVE_CurDrive == -1) && (drive->type == TYPE_HD))
197                 DRIVE_CurDrive = i;
198
199             count++;
200             TRACE(dosfs, "%s: path=%s type=%s label='%s' serial=%08lx flags=%08x dev=%x ino=%x\n",
201                            name, path, DRIVE_Types[drive->type],
202                            drive->label, drive->serial, drive->flags,
203                            (int)drive->dev, (int)drive->ino );
204         }
205         else WARN(dosfs, "%s: not defined\n", name );
206     }
207
208     if (!count) 
209     {
210         MSG("Warning: no valid DOS drive found, check your configuration file.\n" );
211         /* Create a C drive pointing to Unix root dir */
212         DOSDrives[2].root     = HEAP_strdupA( SystemHeap, 0, "/" );
213         DOSDrives[2].dos_cwd  = HEAP_strdupA( SystemHeap, 0, "" );
214         DOSDrives[2].unix_cwd = HEAP_strdupA( SystemHeap, 0, "" );
215         strcpy( DOSDrives[2].label, "Drive C    " );
216         DOSDrives[2].serial   = 0x12345678;
217         DOSDrives[2].type     = TYPE_HD;
218         DOSDrives[2].flags    = 0;
219         DRIVE_CurDrive = 2;
220     }
221
222     /* Make sure the current drive is valid */
223     if (DRIVE_CurDrive == -1)
224     {
225         for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, drive++)
226         {
227             if (drive->root && !(drive->flags & DRIVE_DISABLED))
228             {
229                 DRIVE_CurDrive = i;
230                 break;
231             }
232         }
233     }
234
235     return 1;
236 }
237
238
239 /***********************************************************************
240  *           DRIVE_IsValid
241  */
242 int DRIVE_IsValid( int drive )
243 {
244     if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
245     return (DOSDrives[drive].root &&
246             !(DOSDrives[drive].flags & DRIVE_DISABLED));
247 }
248
249
250 /***********************************************************************
251  *           DRIVE_GetCurrentDrive
252  */
253 int DRIVE_GetCurrentDrive(void)
254 {
255     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
256     if (pTask && (pTask->curdrive & 0x80)) return pTask->curdrive & ~0x80;
257     return DRIVE_CurDrive;
258 }
259
260
261 /***********************************************************************
262  *           DRIVE_SetCurrentDrive
263  */
264 int DRIVE_SetCurrentDrive( int drive )
265 {
266     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
267     if (!DRIVE_IsValid( drive ))
268     {
269         DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
270         return 0;
271     }
272     TRACE(dosfs, "%c:\n", 'A' + drive );
273     DRIVE_CurDrive = drive;
274     if (pTask) pTask->curdrive = drive | 0x80;
275     return 1;
276 }
277
278
279 /***********************************************************************
280  *           DRIVE_FindDriveRoot
281  *
282  * Find a drive for which the root matches the begginning of the given path.
283  * This can be used to translate a Unix path into a drive + DOS path.
284  * Return value is the drive, or -1 on error. On success, path is modified
285  * to point to the beginning of the DOS path.
286  */
287 int DRIVE_FindDriveRoot( const char **path )
288 {
289     /* idea: check at all '/' positions.
290      * If the device and inode of that path is identical with the
291      * device and inode of the current drive then we found a solution.
292      * If there is another drive pointing to a deeper position in
293      * the file tree, we want to find that one, not the earlier solution.
294      */
295     int drive, rootdrive = -1;
296     char buffer[MAX_PATHNAME_LEN];
297     char *next = buffer;
298     const char *p = *path;
299     struct stat st;
300
301     strcpy( buffer, "/" );
302     for (;;)
303     {
304         if (stat( buffer, &st ) || !S_ISDIR( st.st_mode )) break;
305
306         /* Find the drive */
307
308         for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
309         {
310            if (!DOSDrives[drive].root ||
311                (DOSDrives[drive].flags & DRIVE_DISABLED)) continue;
312
313            if ((DOSDrives[drive].dev == st.st_dev) &&
314                (DOSDrives[drive].ino == st.st_ino))
315            {
316                rootdrive = drive;
317                *path = p;
318            }
319         }
320
321         /* Get the next path component */
322
323         *next++ = '/';
324         while ((*p == '/') || (*p == '\\')) p++;
325         if (!*p) break;
326         while (!IS_END_OF_NAME(*p)) *next++ = *p++;
327         *next = 0;
328     }
329     *next = 0;
330
331     if (rootdrive != -1)
332         TRACE(dosfs, "%s -> drive %c:, root='%s', name='%s'\n",
333                        buffer, 'A' + rootdrive,
334                        DOSDrives[rootdrive].root, *path );
335     return rootdrive;
336 }
337
338
339 /***********************************************************************
340  *           DRIVE_GetRoot
341  */
342 const char * DRIVE_GetRoot( int drive )
343 {
344     if (!DRIVE_IsValid( drive )) return NULL;
345     return DOSDrives[drive].root;
346 }
347
348
349 /***********************************************************************
350  *           DRIVE_GetDosCwd
351  */
352 const char * DRIVE_GetDosCwd( int drive )
353 {
354     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
355     if (!DRIVE_IsValid( drive )) return NULL;
356
357     /* Check if we need to change the directory to the new task. */
358     if (pTask && (pTask->curdrive & 0x80) &&    /* The task drive is valid */
359         ((pTask->curdrive & ~0x80) == drive) && /* and it's the one we want */
360         (DRIVE_LastTask != GetCurrentTask()))   /* and the task changed */
361     {
362         /* Perform the task-switch */
363         if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
364         DRIVE_LastTask = GetCurrentTask();
365     }
366     return DOSDrives[drive].dos_cwd;
367 }
368
369
370 /***********************************************************************
371  *           DRIVE_GetUnixCwd
372  */
373 const char * DRIVE_GetUnixCwd( int drive )
374 {
375     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
376     if (!DRIVE_IsValid( drive )) return NULL;
377
378     /* Check if we need to change the directory to the new task. */
379     if (pTask && (pTask->curdrive & 0x80) &&    /* The task drive is valid */
380         ((pTask->curdrive & ~0x80) == drive) && /* and it's the one we want */
381         (DRIVE_LastTask != GetCurrentTask()))   /* and the task changed */
382     {
383         /* Perform the task-switch */
384         if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
385         DRIVE_LastTask = GetCurrentTask();
386     }
387     return DOSDrives[drive].unix_cwd;
388 }
389
390
391 /***********************************************************************
392  *           DRIVE_GetLabel
393  */
394 const char * DRIVE_GetLabel( int drive )
395 {
396     if (!DRIVE_IsValid( drive )) return NULL;
397     return DOSDrives[drive].label;
398 }
399
400
401 /***********************************************************************
402  *           DRIVE_GetSerialNumber
403  */
404 DWORD DRIVE_GetSerialNumber( int drive )
405 {
406     if (!DRIVE_IsValid( drive )) return 0;
407     return DOSDrives[drive].serial;
408 }
409
410
411 /***********************************************************************
412  *           DRIVE_SetSerialNumber
413  */
414 int DRIVE_SetSerialNumber( int drive, DWORD serial )
415 {
416     if (!DRIVE_IsValid( drive )) return 0;
417     DOSDrives[drive].serial = serial;
418     return 1;
419 }
420
421
422 /***********************************************************************
423  *           DRIVE_GetType
424  */
425 DRIVETYPE DRIVE_GetType( int drive )
426 {
427     if (!DRIVE_IsValid( drive )) return TYPE_INVALID;
428     return DOSDrives[drive].type;
429 }
430
431
432 /***********************************************************************
433  *           DRIVE_GetFlags
434  */
435 UINT32 DRIVE_GetFlags( int drive )
436 {
437     if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
438     return DOSDrives[drive].flags;
439 }
440
441
442 /***********************************************************************
443  *           DRIVE_Chdir
444  */
445 int DRIVE_Chdir( int drive, const char *path )
446 {
447     DOS_FULL_NAME full_name;
448     char buffer[MAX_PATHNAME_LEN];
449     LPSTR unix_cwd;
450     BY_HANDLE_FILE_INFORMATION info;
451     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
452
453     TRACE(dosfs, "(%c:,%s)\n", 'A' + drive, path );
454     strcpy( buffer, "A:" );
455     buffer[0] += drive;
456     lstrcpyn32A( buffer + 2, path, sizeof(buffer) - 2 );
457
458     if (!DOSFS_GetFullName( buffer, TRUE, &full_name )) return 0;
459     if (!FILE_Stat( full_name.long_name, &info )) return 0;
460     if (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
461     {
462         DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
463         return 0;
464     }
465     unix_cwd = full_name.long_name + strlen( DOSDrives[drive].root );
466     while (*unix_cwd == '/') unix_cwd++;
467
468     TRACE(dosfs, "(%c:): unix_cwd=%s dos_cwd=%s\n",
469                    'A' + drive, unix_cwd, full_name.short_name + 3 );
470
471     HeapFree( SystemHeap, 0, DOSDrives[drive].dos_cwd );
472     HeapFree( SystemHeap, 0, DOSDrives[drive].unix_cwd );
473     DOSDrives[drive].dos_cwd  = HEAP_strdupA( SystemHeap, 0,
474                                               full_name.short_name + 3 );
475     DOSDrives[drive].unix_cwd = HEAP_strdupA( SystemHeap, 0, unix_cwd );
476
477     if (pTask && (pTask->curdrive & 0x80) && 
478         ((pTask->curdrive & ~0x80) == drive))
479     {
480         lstrcpyn32A( pTask->curdir, full_name.short_name + 2,
481                      sizeof(pTask->curdir) );
482         DRIVE_LastTask = GetCurrentTask();
483     }
484     return 1;
485 }
486
487
488 /***********************************************************************
489  *           DRIVE_Disable
490  */
491 int DRIVE_Disable( int drive  )
492 {
493     if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
494     {
495         DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
496         return 0;
497     }
498     DOSDrives[drive].flags |= DRIVE_DISABLED;
499     return 1;
500 }
501
502
503 /***********************************************************************
504  *           DRIVE_Enable
505  */
506 int DRIVE_Enable( int drive  )
507 {
508     if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
509     {
510         DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
511         return 0;
512     }
513     DOSDrives[drive].flags &= ~DRIVE_DISABLED;
514     return 1;
515 }
516
517
518 /***********************************************************************
519  *           DRIVE_SetLogicalMapping
520  */
521 int DRIVE_SetLogicalMapping ( int existing_drive, int new_drive )
522 {
523  /* If new_drive is already valid, do nothing and return 0
524     otherwise, copy DOSDrives[existing_drive] to DOSDrives[new_drive] */
525   
526     DOSDRIVE *old, *new;
527     
528     old = DOSDrives + existing_drive;
529     new = DOSDrives + new_drive;
530
531     if ((existing_drive < 0) || (existing_drive >= MAX_DOS_DRIVES) ||
532         !old->root ||
533         (new_drive < 0) || (new_drive >= MAX_DOS_DRIVES))
534     {
535         DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
536         return 0;
537     }
538
539     if ( new->root )
540     {
541         TRACE(dosfs, "Can\'t map drive %c to drive %c - "
542                                 "drive %c already exists\n",
543                         'A' + existing_drive, 'A' + new_drive,
544                         'A' + new_drive );
545         return 0;
546     }
547
548     new->root = HEAP_strdupA( SystemHeap, 0, old->root );
549     new->dos_cwd = HEAP_strdupA( SystemHeap, 0, old->dos_cwd );
550     new->unix_cwd = HEAP_strdupA( SystemHeap, 0, old->unix_cwd );
551     memcpy ( new->label, old->label, 12 );
552     new->serial = old->serial;
553     new->type = old->type;
554     new->flags = old->flags;
555     new->dev = old->dev;
556     new->ino = old->ino;
557
558     TRACE(dosfs, "Drive %c is now equal to drive %c\n",
559                     'A' + new_drive, 'A' + existing_drive );
560
561     return 1;
562 }
563
564
565 /***********************************************************************
566  *           DRIVE_OpenDevice
567  *
568  * Open the drive raw device and return a Unix fd (or -1 on error).
569  */
570 int DRIVE_OpenDevice( int drive, int flags )
571 {
572     if (!DRIVE_IsValid( drive )) return -1;
573     return open( DOSDrives[drive].device, flags );
574 }
575
576
577 /***********************************************************************
578  *           DRIVE_GetFreeSpace
579  */
580 static int DRIVE_GetFreeSpace( int drive, DWORD *size, DWORD *available )
581 {
582     struct statfs info;
583
584     if (!DRIVE_IsValid(drive))
585     {
586         DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
587         return 0;
588     }
589
590 /* FIXME: add autoconf check for this */
591 #if defined(__svr4__) || defined(_SCO_DS)
592     if (statfs( DOSDrives[drive].root, &info, 0, 0) < 0)
593 #else
594     if (statfs( DOSDrives[drive].root, &info) < 0)
595 #endif
596     {
597         FILE_SetDosError();
598         WARN(dosfs, "cannot do statfs(%s)\n", DOSDrives[drive].root);
599         return 0;
600     }
601
602     *size = info.f_bsize * info.f_blocks;
603 #ifdef STATFS_HAS_BAVAIL
604     *available = info.f_bavail * info.f_bsize;
605 #else
606 # ifdef STATFS_HAS_BFREE
607     *available = info.f_bfree * info.f_bsize;
608 # else
609 #  error "statfs has no bfree/bavail member!"
610 # endif
611 #endif
612     return 1;
613 }
614
615
616 /***********************************************************************
617  *           GetDiskFreeSpace16   (KERNEL.422)
618  */
619 BOOL16 WINAPI GetDiskFreeSpace16( LPCSTR root, LPDWORD cluster_sectors,
620                                   LPDWORD sector_bytes, LPDWORD free_clusters,
621                                   LPDWORD total_clusters )
622 {
623     return GetDiskFreeSpace32A( root, cluster_sectors, sector_bytes,
624                                 free_clusters, total_clusters );
625 }
626
627
628 /***********************************************************************
629  *           GetDiskFreeSpace32A   (KERNEL32.206)
630  */
631 BOOL32 WINAPI GetDiskFreeSpace32A( LPCSTR root, LPDWORD cluster_sectors,
632                                    LPDWORD sector_bytes, LPDWORD free_clusters,
633                                    LPDWORD total_clusters )
634 {
635     int drive;
636     DWORD size,available;
637
638     if (!root) drive = DRIVE_GetCurrentDrive();
639     else
640     {
641         if ((root[1]) && ((root[1] != ':') || (root[2] != '\\')))
642         {
643             WARN(dosfs, "invalid root '%s'\n", root );
644             return FALSE;
645         }
646         drive = toupper(root[0]) - 'A';
647     }
648     if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
649
650     /* Cap the size and available at 2GB as per specs.  */
651     if (size > 0x7fffffff) size = 0x7fffffff;
652     if (available > 0x7fffffff) available = 0x7fffffff;
653
654     if (DRIVE_GetType(drive)==TYPE_CDROM) {
655         *sector_bytes    = 2048;
656         size            /= 2048;
657         available       /= 2048;
658     } else {
659         *sector_bytes    = 512;
660         size            /= 512;
661         available       /= 512;
662     }
663     /* fixme: probably have to adjust those variables too for CDFS */
664     *cluster_sectors = 1;
665     while (*cluster_sectors * 65536 < size) *cluster_sectors *= 2;
666     *free_clusters   = available/ *cluster_sectors;
667     *total_clusters  = size/ *cluster_sectors;
668     return TRUE;
669 }
670
671
672 /***********************************************************************
673  *           GetDiskFreeSpace32W   (KERNEL32.207)
674  */
675 BOOL32 WINAPI GetDiskFreeSpace32W( LPCWSTR root, LPDWORD cluster_sectors,
676                                    LPDWORD sector_bytes, LPDWORD free_clusters,
677                                    LPDWORD total_clusters )
678 {
679     LPSTR xroot;
680     BOOL32 ret;
681
682     xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
683     ret = GetDiskFreeSpace32A( xroot,cluster_sectors, sector_bytes,
684                                free_clusters, total_clusters );
685     HeapFree( GetProcessHeap(), 0, xroot );
686     return ret;
687 }
688
689
690 /***********************************************************************
691  *           GetDiskFreeSpaceEx32A   (KERNEL32.871)
692  */
693 BOOL32 WINAPI GetDiskFreeSpaceEx32A( LPCSTR root,
694                                      LPULARGE_INTEGER avail,
695                                      LPULARGE_INTEGER total,
696                                      LPULARGE_INTEGER totalfree)
697 {
698     int drive;
699     DWORD size,available;
700
701     if (!root) drive = DRIVE_GetCurrentDrive();
702     else
703     {
704         if ((root[1]) && ((root[1] != ':') || (root[2] != '\\')))
705         {
706             WARN(dosfs, "invalid root '%s'\n", root );
707             return FALSE;
708         }
709         drive = toupper(root[0]) - 'A';
710     }
711     if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
712     /*FIXME: Do we have the number of bytes available to the user? */
713     avail->HighPart = total->HighPart = 0;
714     avail->LowPart = available;
715     total->LowPart = size;
716     if(totalfree)
717       {
718         totalfree->HighPart =0;
719         totalfree->LowPart=  available;
720       }
721     return TRUE;
722 }
723
724 /***********************************************************************
725  *           GetDiskFreeSpaceEx32W   (KERNEL32.873)
726  */
727 BOOL32 WINAPI GetDiskFreeSpaceEx32W( LPCWSTR root, LPULARGE_INTEGER avail,
728                                      LPULARGE_INTEGER total,
729                                      LPULARGE_INTEGER  totalfree)
730 {
731     LPSTR xroot;
732     BOOL32 ret;
733
734     xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
735     ret = GetDiskFreeSpaceEx32A( xroot, avail, total, totalfree);
736     HeapFree( GetProcessHeap(), 0, xroot );
737     return ret;
738 }
739
740 /***********************************************************************
741  *           GetDriveType16   (KERNEL.136)
742  */
743 UINT16 WINAPI GetDriveType16( UINT16 drive )
744 {
745     TRACE(dosfs, "(%c:)\n", 'A' + drive );
746     switch(DRIVE_GetType(drive))
747     {
748     case TYPE_FLOPPY:  return DRIVE_REMOVABLE;
749     case TYPE_HD:      return DRIVE_FIXED;
750     case TYPE_CDROM:   return DRIVE_REMOVABLE;
751     case TYPE_NETWORK: return DRIVE_REMOTE;
752     case TYPE_INVALID:
753     default:           return DRIVE_CANNOTDETERMINE;
754     }
755 }
756
757
758 /***********************************************************************
759  *           GetDriveType32A   (KERNEL32.208)
760  */
761 UINT32 WINAPI GetDriveType32A( LPCSTR root )
762 {
763     TRACE(dosfs, "(%s)\n", root );
764     if ((root[1]) && (root[1] != ':'))
765     {
766         WARN(dosfs, "invalid root '%s'\n", root );
767         return DRIVE_DOESNOTEXIST;
768     }
769     switch(DRIVE_GetType(toupper(root[0]) - 'A'))
770     {
771     case TYPE_FLOPPY:  return DRIVE_REMOVABLE;
772     case TYPE_HD:      return DRIVE_FIXED;
773     case TYPE_CDROM:   return DRIVE_CDROM;
774     case TYPE_NETWORK: return DRIVE_REMOTE;
775     case TYPE_INVALID:
776     default:           return DRIVE_CANNOTDETERMINE;
777     }
778 }
779
780
781 /***********************************************************************
782  *           GetDriveType32W   (KERNEL32.209)
783  */
784 UINT32 WINAPI GetDriveType32W( LPCWSTR root )
785 {
786     LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
787     UINT32 ret = GetDriveType32A( xpath );
788     HeapFree( GetProcessHeap(), 0, xpath );
789     return ret;
790 }
791
792
793 /***********************************************************************
794  *           GetCurrentDirectory16   (KERNEL.411)
795  */
796 UINT16 WINAPI GetCurrentDirectory16( UINT16 buflen, LPSTR buf )
797 {
798     return (UINT16)GetCurrentDirectory32A( buflen, buf );
799 }
800
801
802 /***********************************************************************
803  *           GetCurrentDirectory32A   (KERNEL32.196)
804  *
805  * Returns "X:\\path\\etc\\".
806  *
807  * Despite the API description, return required length including the 
808  * terminating null when buffer too small. This is the real behaviour.
809  */
810 UINT32 WINAPI GetCurrentDirectory32A( UINT32 buflen, LPSTR buf )
811 {
812     UINT32 ret;
813     const char *s = DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );
814
815     assert(s);
816     ret = strlen(s) + 3; /* length of WHOLE current directory */
817     if (ret >= buflen) return ret + 1;
818     lstrcpyn32A( buf, "A:\\", MIN( 4, buflen ) );
819     if (buflen) buf[0] += DRIVE_GetCurrentDrive();
820     if (buflen > 3) lstrcpyn32A( buf + 3, s, buflen - 3 );
821     return ret;
822 }
823
824
825 /***********************************************************************
826  *           GetCurrentDirectory32W   (KERNEL32.197)
827  */
828 UINT32 WINAPI GetCurrentDirectory32W( UINT32 buflen, LPWSTR buf )
829 {
830     LPSTR xpath = HeapAlloc( GetProcessHeap(), 0, buflen+1 );
831     UINT32 ret = GetCurrentDirectory32A( buflen, xpath );
832     lstrcpyAtoW( buf, xpath );
833     HeapFree( GetProcessHeap(), 0, xpath );
834     return ret;
835 }
836
837
838 /***********************************************************************
839  *           SetCurrentDirectory   (KERNEL.412)
840  */
841 BOOL16 WINAPI SetCurrentDirectory16( LPCSTR dir )
842 {
843     return SetCurrentDirectory32A( dir );
844 }
845
846
847 /***********************************************************************
848  *           SetCurrentDirectory32A   (KERNEL32.479)
849  */
850 BOOL32 WINAPI SetCurrentDirectory32A( LPCSTR dir )
851 {
852     int drive = DRIVE_GetCurrentDrive();
853
854     if (dir[0] && (dir[1]==':'))
855     {
856         drive = tolower( *dir ) - 'a';
857         if (!DRIVE_IsValid( drive ))
858         {
859             DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
860             return FALSE;
861         }
862         dir += 2;
863     }
864     /* FIXME: what about empty strings? Add a \\ ? */
865     if (!DRIVE_Chdir( drive, dir )) return FALSE;
866     if (drive == DRIVE_GetCurrentDrive()) return TRUE;
867     return DRIVE_SetCurrentDrive( drive );
868 }
869
870
871 /***********************************************************************
872  *           SetCurrentDirectory32W   (KERNEL32.480)
873  */
874 BOOL32 WINAPI SetCurrentDirectory32W( LPCWSTR dirW )
875 {
876     LPSTR dir = HEAP_strdupWtoA( GetProcessHeap(), 0, dirW );
877     BOOL32 res = SetCurrentDirectory32A( dir );
878     HeapFree( GetProcessHeap(), 0, dir );
879     return res;
880 }
881
882
883 /***********************************************************************
884  *           GetLogicalDriveStrings32A   (KERNEL32.231)
885  */
886 UINT32 WINAPI GetLogicalDriveStrings32A( UINT32 len, LPSTR buffer )
887 {
888     int drive, count;
889
890     for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
891         if (DRIVE_IsValid(drive)) count++;
892     if (count * 4 * sizeof(char) <= len)
893     {
894         LPSTR p = buffer;
895         for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
896             if (DRIVE_IsValid(drive))
897             {
898                 *p++ = 'a' + drive;
899                 *p++ = ':';
900                 *p++ = '\\';
901                 *p++ = '\0';
902             }
903         *p = '\0';
904     }
905     return count * 4 * sizeof(char);
906 }
907
908
909 /***********************************************************************
910  *           GetLogicalDriveStrings32W   (KERNEL32.232)
911  */
912 UINT32 WINAPI GetLogicalDriveStrings32W( UINT32 len, LPWSTR buffer )
913 {
914     int drive, count;
915
916     for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
917         if (DRIVE_IsValid(drive)) count++;
918     if (count * 4 * sizeof(WCHAR) <= len)
919     {
920         LPWSTR p = buffer;
921         for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
922             if (DRIVE_IsValid(drive))
923             {
924                 *p++ = (WCHAR)('a' + drive);
925                 *p++ = (WCHAR)':';
926                 *p++ = (WCHAR)'\\';
927                 *p++ = (WCHAR)'\0';
928             }
929         *p = (WCHAR)'\0';
930     }
931     return count * 4 * sizeof(WCHAR);
932 }
933
934
935 /***********************************************************************
936  *           GetLogicalDrives   (KERNEL32.233)
937  */
938 DWORD WINAPI GetLogicalDrives(void)
939 {
940     DWORD ret = 0;
941     int drive;
942
943     for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
944         if (DRIVE_IsValid(drive)) ret |= (1 << drive);
945     return ret;
946 }
947
948
949 /***********************************************************************
950  *           GetVolumeInformation32A   (KERNEL32.309)
951  */
952 BOOL32 WINAPI GetVolumeInformation32A( LPCSTR root, LPSTR label,
953                                        DWORD label_len, DWORD *serial,
954                                        DWORD *filename_len, DWORD *flags,
955                                        LPSTR fsname, DWORD fsname_len )
956 {
957     int drive;
958
959     /* FIXME, SetLastErrors missing */
960
961     if (!root) drive = DRIVE_GetCurrentDrive();
962     else
963     {
964         if ((root[1]) && (root[1] != ':'))
965         {
966             WARN(dosfs, "invalid root '%s'\n",root);
967             return FALSE;
968         }
969         drive = toupper(root[0]) - 'A';
970     }
971     if (!DRIVE_IsValid( drive )) return FALSE;
972     if (label) lstrcpyn32A( label, DOSDrives[drive].label, label_len );
973     if (serial) *serial = DOSDrives[drive].serial;
974
975     /* Set the filesystem information */
976     /* Note: we only emulate a FAT fs at the present */
977
978     if (filename_len) *filename_len = 12;
979     if (flags) *flags = 0;
980     if (fsname) {
981         /* Diablo checks that return code ... */
982         if (DRIVE_GetType(drive)==TYPE_CDROM)
983             lstrcpyn32A( fsname, "CDFS", fsname_len );
984         else
985             lstrcpyn32A( fsname, "FAT", fsname_len );
986     }
987     return TRUE;
988 }
989
990
991 /***********************************************************************
992  *           GetVolumeInformation32W   (KERNEL32.310)
993  */
994 BOOL32 WINAPI GetVolumeInformation32W( LPCWSTR root, LPWSTR label,
995                                        DWORD label_len, DWORD *serial,
996                                        DWORD *filename_len, DWORD *flags,
997                                        LPWSTR fsname, DWORD fsname_len )
998 {
999     LPSTR xroot    = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
1000     LPSTR xvolname = label ? HeapAlloc(GetProcessHeap(),0,label_len) : NULL;
1001     LPSTR xfsname  = fsname ? HeapAlloc(GetProcessHeap(),0,fsname_len) : NULL;
1002     BOOL32 ret = GetVolumeInformation32A( xroot, xvolname, label_len, serial,
1003                                           filename_len, flags, xfsname,
1004                                           fsname_len );
1005     if (ret)
1006     {
1007         if (label) lstrcpyAtoW( label, xvolname );
1008         if (fsname) lstrcpyAtoW( fsname, xfsname );
1009     }
1010     HeapFree( GetProcessHeap(), 0, xroot );
1011     HeapFree( GetProcessHeap(), 0, xvolname );
1012     HeapFree( GetProcessHeap(), 0, xfsname );
1013     return ret;
1014 }