Release 960818
[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 <ctype.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13
14 #if defined(__linux__) || defined(sun) || defined(hpux)
15 #include <sys/vfs.h>
16 #endif
17 #if defined(__NetBSD__) || defined(__FreeBSD__)
18 #include <sys/param.h>
19 #include <sys/mount.h>
20 #include <sys/errno.h>
21 #endif
22 #if defined(__svr4__) || defined(_SCO_DS)
23 #include <sys/statfs.h>
24 #endif
25
26 #include "windows.h"
27 #include "winbase.h"
28 #include "dos_fs.h"
29 #include "drive.h"
30 #include "file.h"
31 #include "msdos.h"
32 #include "options.h"
33 #include "task.h"
34 #include "xmalloc.h"
35 #include "string32.h"
36 #include "stddebug.h"
37 #include "debug.h"
38
39 typedef struct
40 {
41     char     *root;      /* root dir in Unix format without trailing / */
42     char     *dos_cwd;   /* cwd in DOS format without leading or trailing \ */
43     char     *unix_cwd;  /* cwd in Unix format without leading or trailing / */
44     char      label[12]; /* drive label */
45     DWORD     serial;    /* drive serial number */
46     DRIVETYPE type;      /* drive type */
47     UINT32    flags;     /* drive flags */
48 } DOSDRIVE;
49
50
51 static const char * const DRIVE_Types[] =
52 {
53     "floppy",   /* TYPE_FLOPPY */
54     "hd",       /* TYPE_HD */
55     "cdrom",    /* TYPE_CDROM */
56     "network"   /* TYPE_NETWORK */
57 };
58
59
60 /* Known filesystem types */
61
62 typedef struct
63 {
64     const char *name;
65     UINT32      flags;
66 } FS_DESCR;
67
68 static const FS_DESCR DRIVE_Filesystems[] =
69 {
70     { "unix",   DRIVE_CASE_SENSITIVE | DRIVE_CASE_PRESERVING },
71     { "msdos",  DRIVE_SHORT_NAMES },
72     { "dos",    DRIVE_SHORT_NAMES },
73     { "fat",    DRIVE_SHORT_NAMES },
74     { "vfat",   DRIVE_CASE_PRESERVING },
75     { "win95",  DRIVE_CASE_PRESERVING },
76     { NULL, 0 }
77 };
78
79
80 static DOSDRIVE DOSDrives[MAX_DOS_DRIVES];
81 static int DRIVE_CurDrive = -1;
82
83 static HTASK DRIVE_LastTask = 0;
84
85
86 /***********************************************************************
87  *           DRIVE_GetDriveType
88  */
89 static DRIVETYPE DRIVE_GetDriveType( const char *name )
90 {
91     char buffer[20];
92     int i;
93
94     PROFILE_GetWineIniString( name, "Type", "hd", buffer, sizeof(buffer) );
95     for (i = 0; i < sizeof(DRIVE_Types)/sizeof(DRIVE_Types[0]); i++)
96     {
97         if (!lstrcmpi32A( buffer, DRIVE_Types[i] )) return (DRIVETYPE)i;
98     }
99     fprintf( stderr, "%s: unknown type '%s', defaulting to 'hd'.\n",
100              name, buffer );
101     return TYPE_HD;
102 }
103
104
105 /***********************************************************************
106  *           DRIVE_GetFSFlags
107  */
108 static UINT32 DRIVE_GetFSFlags( const char *name, const char *value )
109 {
110     const FS_DESCR *descr;
111
112     for (descr = DRIVE_Filesystems; descr->name; descr++)
113         if (!lstrcmpi32A( value, descr->name )) return descr->flags;
114     fprintf( stderr, "%s: unknown filesystem type '%s', defaulting to 'unix'.\n",
115              name, value );
116     return DRIVE_CASE_SENSITIVE | DRIVE_CASE_PRESERVING;
117 }
118
119
120 /***********************************************************************
121  *           DRIVE_Init
122  */
123 int DRIVE_Init(void)
124 {
125     int i, len, count = 0;
126     char name[] = "Drive A";
127     char path[MAX_PATHNAME_LEN];
128     char buffer[20];
129     char *p;
130     DOSDRIVE *drive;
131
132     for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, name[6]++, drive++)
133     {
134         PROFILE_GetWineIniString( name, "Path", "", path, sizeof(path)-1 );
135         if (path[0])
136         {
137             p = path + strlen(path) - 1;
138             while ((p > path) && ((*p == '/') || (*p == '\\'))) *p-- = '\0';
139             drive->root     = xstrdup( path );
140             drive->dos_cwd  = xstrdup( "" );
141             drive->unix_cwd = xstrdup( "" );
142             drive->type     = DRIVE_GetDriveType( name );
143             drive->flags    = 0;
144
145             /* Get the drive label */
146             PROFILE_GetWineIniString( name, "Label", name, drive->label, 12 );
147             if ((len = strlen(drive->label)) < 11)
148             {
149                 /* Pad label with spaces */
150                 memset( drive->label + len, ' ', 11 - len );
151                 drive->label[12] = '\0';
152             }
153
154             /* Get the serial number */
155             PROFILE_GetWineIniString( name, "Serial", "12345678",
156                                       buffer, sizeof(buffer) );
157             drive->serial = strtoul( buffer, NULL, 16 );
158
159             /* Get the filesystem type */
160             PROFILE_GetWineIniString( name, "Filesystem", "unix",
161                                       buffer, sizeof(buffer) );
162             drive->flags = DRIVE_GetFSFlags( name, buffer );
163
164             /* Make the first hard disk the current drive */
165             if ((DRIVE_CurDrive == -1) && (drive->type == TYPE_HD))
166                 DRIVE_CurDrive = i;
167
168             count++;
169             dprintf_dosfs( stddeb, "%s: path=%s type=%s label='%s' serial=%08lx flags=%08x\n",
170                            name, path, DRIVE_Types[drive->type],
171                            drive->label, drive->serial, drive->flags );
172         }
173         else dprintf_dosfs( stddeb, "%s: not defined\n", name );
174     }
175
176     if (!count) 
177     {
178         fprintf( stderr, "Warning: no valid DOS drive found, check your configuration file.\n" );
179         /* Create a C drive pointing to Unix root dir */
180         DOSDrives[2].root     = xstrdup( "/" );
181         DOSDrives[2].dos_cwd  = xstrdup( "" );
182         DOSDrives[2].unix_cwd = xstrdup( "" );
183         strcpy( DOSDrives[2].label, "Drive C    " );
184         DOSDrives[2].serial   = 0x12345678;
185         DOSDrives[2].type     = TYPE_HD;
186         DOSDrives[2].flags    = 0;
187         DRIVE_CurDrive = 2;
188     }
189
190     /* Make sure the current drive is valid */
191     if (DRIVE_CurDrive == -1)
192     {
193         for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, drive++)
194         {
195             if (drive->root && !(drive->flags & DRIVE_DISABLED))
196             {
197                 DRIVE_CurDrive = i;
198                 break;
199             }
200         }
201     }
202
203     return 1;
204 }
205
206
207 /***********************************************************************
208  *           DRIVE_IsValid
209  */
210 int DRIVE_IsValid( int drive )
211 {
212     if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
213     return (DOSDrives[drive].root &&
214             !(DOSDrives[drive].flags & DRIVE_DISABLED));
215 }
216
217
218 /***********************************************************************
219  *           DRIVE_GetCurrentDrive
220  */
221 int DRIVE_GetCurrentDrive(void)
222 {
223     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
224     if (pTask && (pTask->curdrive & 0x80)) return pTask->curdrive & ~0x80;
225     return DRIVE_CurDrive;
226 }
227
228
229 /***********************************************************************
230  *           DRIVE_SetCurrentDrive
231  */
232 int DRIVE_SetCurrentDrive( int drive )
233 {
234     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
235     if (!DRIVE_IsValid( drive ))
236     {
237         DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
238         return 0;
239     }
240     dprintf_dosfs( stddeb, "DRIVE_SetCurrentDrive: %c:\n", 'A' + drive );
241     DRIVE_CurDrive = drive;
242     if (pTask) pTask->curdrive = drive | 0x80;
243     return 1;
244 }
245
246
247 /***********************************************************************
248  *           DRIVE_FindDriveRoot
249  *
250  * Find a drive for which the root matches the begginning of the given path.
251  * This can be used to translate a Unix path into a drive + DOS path.
252  * Return value is the drive, or -1 on error. On success, path is modified
253  * to point to the beginning of the DOS path.
254  * FIXME: this only does a textual comparison of the path names, and won't
255  *        work well in the presence of symbolic links.
256  */
257 int DRIVE_FindDriveRoot( const char **path )
258 {
259     int drive, rootdrive = -1;
260     const char *p1, *p2;
261
262     dprintf_dosfs( stddeb, "DRIVE_FindDriveRoot: searching '%s'\n", *path );
263     for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
264     {
265         if (!DOSDrives[drive].root ||
266             (DOSDrives[drive].flags & DRIVE_DISABLED)) continue;
267         p1 = *path;
268         p2 = DOSDrives[drive].root;
269         dprintf_dosfs( stddeb, "DRIVE_FindDriveRoot: checking %c: '%s'\n",
270                        'A' + drive, p2 );
271         
272         while (*p2 == '/') p2++;
273         if (!*p2)
274         {
275             rootdrive = drive;
276             continue;  /* Look if there's a better match */
277         }
278         for (;;)
279         {
280             while ((*p1 == '\\') || (*p1 == '/')) p1++;
281             while (*p2 == '/') p2++;
282             while ((*p1 == *p2) && (*p2) && (*p2 != '/')) p1++, p2++;
283             if (!*p2)
284             {
285                 if (IS_END_OF_NAME(*p1)) /* OK, found it */
286                 {
287                     *path = p1;
288                     return drive;
289                 }
290             }
291             else if (*p2 == '/')
292             {
293                 if (IS_END_OF_NAME(*p1))
294                     continue;  /* Go to next path element */
295             }
296             break;  /* No match, go to next drive */
297         }
298     }
299     return rootdrive;
300 }
301
302
303 /***********************************************************************
304  *           DRIVE_GetRoot
305  */
306 const char * DRIVE_GetRoot( int drive )
307 {
308     if (!DRIVE_IsValid( drive )) return NULL;
309     return DOSDrives[drive].root;
310 }
311
312
313 /***********************************************************************
314  *           DRIVE_GetDosCwd
315  */
316 const char * DRIVE_GetDosCwd( int drive )
317 {
318     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
319     if (!DRIVE_IsValid( drive )) return NULL;
320
321     /* Check if we need to change the directory to the new task. */
322     if (pTask && (pTask->curdrive & 0x80) &&    /* The task drive is valid */
323         ((pTask->curdrive & ~0x80) == drive) && /* and it's the one we want */
324         (DRIVE_LastTask != GetCurrentTask()))   /* and the task changed */
325     {
326         /* Perform the task-switch */
327         if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
328         DRIVE_LastTask = GetCurrentTask();
329     }
330     return DOSDrives[drive].dos_cwd;
331 }
332
333
334 /***********************************************************************
335  *           DRIVE_GetUnixCwd
336  */
337 const char * DRIVE_GetUnixCwd( int drive )
338 {
339     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
340     if (!DRIVE_IsValid( drive )) return NULL;
341
342     /* Check if we need to change the directory to the new task. */
343     if (pTask && (pTask->curdrive & 0x80) &&    /* The task drive is valid */
344         ((pTask->curdrive & ~0x80) == drive) && /* and it's the one we want */
345         (DRIVE_LastTask != GetCurrentTask()))   /* and the task changed */
346     {
347         /* Perform the task-switch */
348         if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
349         DRIVE_LastTask = GetCurrentTask();
350     }
351     return DOSDrives[drive].unix_cwd;
352 }
353
354
355 /***********************************************************************
356  *           DRIVE_GetLabel
357  */
358 const char * DRIVE_GetLabel( int drive )
359 {
360     if (!DRIVE_IsValid( drive )) return NULL;
361     return DOSDrives[drive].label;
362 }
363
364
365 /***********************************************************************
366  *           DRIVE_GetSerialNumber
367  */
368 DWORD DRIVE_GetSerialNumber( int drive )
369 {
370     if (!DRIVE_IsValid( drive )) return 0;
371     return DOSDrives[drive].serial;
372 }
373
374
375 /***********************************************************************
376  *           DRIVE_SetSerialNumber
377  */
378 int DRIVE_SetSerialNumber( int drive, DWORD serial )
379 {
380     if (!DRIVE_IsValid( drive )) return 0;
381     DOSDrives[drive].serial = serial;
382     return 1;
383 }
384
385
386 /***********************************************************************
387  *           DRIVE_GetType
388  */
389 DRIVETYPE DRIVE_GetType( int drive )
390 {
391     if (!DRIVE_IsValid( drive )) return TYPE_INVALID;
392     return DOSDrives[drive].type;
393 }
394
395
396 /***********************************************************************
397  *           DRIVE_GetFlags
398  */
399 UINT32 DRIVE_GetFlags( int drive )
400 {
401     if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
402     return DOSDrives[drive].flags;
403 }
404
405
406 /***********************************************************************
407  *           DRIVE_Chdir
408  */
409 int DRIVE_Chdir( int drive, const char *path )
410 {
411     char buffer[MAX_PATHNAME_LEN];
412     const char *unix_cwd, *dos_cwd;
413     BYTE attr;
414     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
415
416     dprintf_dosfs( stddeb, "DRIVE_Chdir(%c:,%s)\n", 'A' + drive, path );
417     strcpy( buffer, "A:" );
418     buffer[0] += drive;
419     lstrcpyn32A( buffer + 2, path, sizeof(buffer) - 2 );
420
421     if (!(unix_cwd = DOSFS_GetUnixFileName( buffer, TRUE ))) return 0;
422     if (!FILE_Stat( unix_cwd, &attr, NULL, NULL, NULL )) return 0;
423     if (!(attr & FA_DIRECTORY))
424     {
425         DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
426         return 0;
427     }
428     unix_cwd += strlen( DOSDrives[drive].root );
429     while (*unix_cwd == '/') unix_cwd++;
430     buffer[2] = '/';
431     lstrcpyn32A( buffer + 3, unix_cwd, sizeof(buffer) - 3 );
432     if (!(dos_cwd = DOSFS_GetDosTrueName( buffer, TRUE ))) return 0;
433
434     dprintf_dosfs( stddeb, "DRIVE_Chdir(%c:): unix_cwd=%s dos_cwd=%s\n",
435                    'A' + drive, unix_cwd, dos_cwd + 3 );
436
437     free( DOSDrives[drive].dos_cwd );
438     free( DOSDrives[drive].unix_cwd );
439     DOSDrives[drive].dos_cwd  = xstrdup( dos_cwd + 3 );
440     DOSDrives[drive].unix_cwd = xstrdup( unix_cwd );
441
442     if (pTask && (pTask->curdrive & 0x80) && 
443         ((pTask->curdrive & ~0x80) == drive))
444     {
445         lstrcpyn32A( pTask->curdir, dos_cwd + 2, sizeof(pTask->curdir) );
446         DRIVE_LastTask = GetCurrentTask();
447     }
448     return 1;
449 }
450
451
452 /***********************************************************************
453  *           DRIVE_Disable
454  */
455 int DRIVE_Disable( int drive  )
456 {
457     if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
458     {
459         DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
460         return 0;
461     }
462     DOSDrives[drive].flags |= DRIVE_DISABLED;
463     return 1;
464 }
465
466
467 /***********************************************************************
468  *           DRIVE_Enable
469  */
470 int DRIVE_Enable( int drive  )
471 {
472     if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
473     {
474         DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
475         return 0;
476     }
477     DOSDrives[drive].flags &= ~DRIVE_DISABLED;
478     return 1;
479 }
480
481
482 /***********************************************************************
483  *           DRIVE_GetFreeSpace
484  */
485 static int DRIVE_GetFreeSpace( int drive, DWORD *size, DWORD *available )
486 {
487     struct statfs info;
488
489     if (!DRIVE_IsValid(drive))
490     {
491         DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
492         return 0;
493     }
494
495 #if defined(__svr4__) || defined(_SCO_DS)
496     if (statfs( DOSDrives[drive].root, &info, 0, 0) < 0)
497 #else
498     if (statfs( DOSDrives[drive].root, &info) < 0)
499 #endif
500     {
501         FILE_SetDosError();
502         fprintf(stderr,"dosfs: cannot do statfs(%s)\n", DOSDrives[drive].root);
503         return 0;
504     }
505
506     *size = info.f_bsize * info.f_blocks;
507 #if defined(__svr4__) || defined(_SCO_DS)
508     *available = info.f_bfree * info.f_bsize;
509 #else
510     *available = info.f_bavail * info.f_bsize;
511 #endif
512     return 1;
513 }
514
515
516 /***********************************************************************
517  *           GetDiskFreeSpace16   (KERNEL.422)
518  */
519 BOOL16 GetDiskFreeSpace16( LPCSTR root, LPDWORD cluster_sectors,
520                            LPDWORD sector_bytes, LPDWORD free_clusters,
521                            LPDWORD total_clusters )
522 {
523     return GetDiskFreeSpace32A( root, cluster_sectors, sector_bytes,
524                                 free_clusters, total_clusters );
525 }
526
527
528 /***********************************************************************
529  *           GetDiskFreeSpaceA   (KERNEL32.206)
530  */
531 BOOL32 GetDiskFreeSpace32A( LPCSTR root, LPDWORD cluster_sectors,
532                             LPDWORD sector_bytes, LPDWORD free_clusters,
533                             LPDWORD total_clusters )
534 {
535     int drive;
536     DWORD size,available;
537
538     if (!root) drive = DRIVE_GetCurrentDrive();
539     else
540     {
541         if ((root[1] != ':') || (root[2] != '\\'))
542         {
543             fprintf( stderr, "GetDiskFreeSpaceA: invalid root '%s'\n", root );
544             return FALSE;
545         }
546         drive = toupper(root[0]) - 'A';
547     }
548     if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
549
550     *sector_bytes    = 512;
551     size            /= 512;
552     available       /= 512;
553     *cluster_sectors = 1;
554     while (*cluster_sectors * 65530 < size) *cluster_sectors *= 2;
555     *free_clusters   = available/ *cluster_sectors;
556     *total_clusters  = size/ *cluster_sectors;
557     return TRUE;
558 }
559
560
561 /***********************************************************************
562  *           GetDiskFreeSpaceW   (KERNEL32.207)
563  */
564 BOOL32 GetDiskFreeSpace32W( LPCWSTR root, LPDWORD cluster_sectors,
565                             LPDWORD sector_bytes, LPDWORD free_clusters,
566                             LPDWORD total_clusters )
567 {
568     LPSTR xroot;
569     BOOL ret;
570
571     xroot = STRING32_DupUniToAnsi(root);
572     ret = GetDiskFreeSpace32A( xroot,cluster_sectors, sector_bytes,
573                                free_clusters, total_clusters );
574     free( xroot );
575     return ret;
576 }
577
578
579 /***********************************************************************
580  *           GetDriveType16   (KERNEL.136)
581  */
582 UINT16 GetDriveType16( UINT16 drive )
583 {
584     dprintf_dosfs( stddeb, "GetDriveType(%c:)\n", 'A' + drive );
585     switch(DRIVE_GetType(drive))
586     {
587     case TYPE_FLOPPY:  return DRIVE_REMOVABLE;
588     case TYPE_HD:      return DRIVE_FIXED;
589     case TYPE_CDROM:   return DRIVE_REMOVABLE;
590     case TYPE_NETWORK: return DRIVE_REMOTE;
591     case TYPE_INVALID:
592     default:           return DRIVE_CANNOTDETERMINE;
593     }
594 }
595
596
597 /***********************************************************************
598  *           GetDriveType32A   (KERNEL32.208)
599  */
600 UINT32 GetDriveType32A( LPCSTR root )
601 {
602     dprintf_dosfs( stddeb, "GetDriveType32A(%s)\n", root );
603     if ((root[1] != ':') || (root[2] != '\\'))
604     {
605         fprintf( stderr, "GetDriveType32A: invalid root '%s'\n", root );
606         return DRIVE_DOESNOTEXIST;
607     }
608     switch(DRIVE_GetType(toupper(root[0]) - 'A'))
609     {
610     case TYPE_FLOPPY:  return DRIVE_REMOVABLE;
611     case TYPE_HD:      return DRIVE_FIXED;
612     case TYPE_CDROM:   return DRIVE_CDROM;
613     case TYPE_NETWORK: return DRIVE_REMOTE;
614     case TYPE_INVALID:
615     default:           return DRIVE_CANNOTDETERMINE;
616     }
617 }
618
619
620 /***********************************************************************
621  *           GetDriveType32W   (KERNEL32.209)
622  */
623 UINT32 GetDriveType32W( LPCWSTR root )
624 {
625     LPSTR xpath=STRING32_DupUniToAnsi(root);
626     UINT32 ret;
627
628     ret = GetDriveType32A(xpath);
629     free(xpath);
630     return ret;
631 }
632
633
634 /***********************************************************************
635  *           GetCurrentDirectory16   (KERNEL.411)
636  */
637 UINT16 GetCurrentDirectory16( UINT16 buflen, LPSTR buf )
638 {
639     return (UINT16)GetCurrentDirectory32A( buflen, buf );
640 }
641
642
643 /***********************************************************************
644  *           GetCurrentDirectory32A   (KERNEL32.196)
645  *
646  * Returns "X:\\path\\etc\\".
647  */
648 UINT32 GetCurrentDirectory32A( UINT32 buflen, LPSTR buf )
649 {
650     char *pref = "A:\\";
651     const char *s = DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );
652     if (!s)
653     {
654         *buf = '\0';
655         return 0;
656     }
657     lstrcpyn32A( buf, pref, 3 );
658     if (buflen) buf[0] += DRIVE_GetCurrentDrive();
659     if (buflen >= 3) lstrcpyn32A( buf + 3, s, buflen - 3 );
660     return strlen(s) + 3; /* length of WHOLE current directory */
661 }
662
663
664 /***********************************************************************
665  *           GetCurrentDirectory32W   (KERNEL32.197)
666  */
667 UINT32 GetCurrentDirectory32W( UINT32 buflen, LPWSTR buf )
668 {
669     LPSTR xpath=(char*)xmalloc(buflen+1);
670     UINT32 ret;
671
672     ret = GetCurrentDirectory32A(buflen,xpath);
673     STRING32_AnsiToUni(buf,xpath);
674     free(xpath);
675     return ret;
676 }
677
678
679 /***********************************************************************
680  *           SetCurrentDirectory   (KERNEL.412)
681  */
682 BOOL32 SetCurrentDirectory( LPCSTR dir )
683 {
684     return DRIVE_Chdir( DRIVE_GetCurrentDrive(), dir );
685 }
686
687
688 /***********************************************************************
689  *           GetLogicalDriveStrings32A   (KERNEL32.231)
690  */
691 UINT32 GetLogicalDriveStrings32A( UINT32 len, LPSTR buffer )
692 {
693     int drive, count;
694
695     for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
696         if (DRIVE_IsValid(drive)) count++;
697     if (count * 4 * sizeof(char) <= len)
698     {
699         LPSTR p = buffer;
700         for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
701             if (DRIVE_IsValid(drive))
702             {
703                 *p++ = 'a' + drive;
704                 *p++ = ':';
705                 *p++ = '\\';
706                 *p++ = '\0';
707             }
708         *p = '\0';
709     }
710     return count * 4 * sizeof(char);
711 }
712
713
714 /***********************************************************************
715  *           GetLogicalDriveStrings32W   (KERNEL32.232)
716  */
717 UINT32 GetLogicalDriveStrings32W( UINT32 len, LPWSTR buffer )
718 {
719     int drive, count;
720
721     for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
722         if (DRIVE_IsValid(drive)) count++;
723     if (count * 4 * sizeof(WCHAR) <= len)
724     {
725         LPWSTR p = buffer;
726         for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
727             if (DRIVE_IsValid(drive))
728             {
729                 *p++ = (WCHAR)('a' + drive);
730                 *p++ = (WCHAR)':';
731                 *p++ = (WCHAR)'\\';
732                 *p++ = (WCHAR)'\0';
733             }
734         *p = (WCHAR)'\0';
735     }
736     return count * 4 * sizeof(WCHAR);
737 }
738
739
740 /***********************************************************************
741  *           GetLogicalDrives   (KERNEL32.233)
742  */
743 DWORD GetLogicalDrives(void)
744 {
745     DWORD ret = 0;
746     int drive;
747
748     for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
749         if (DRIVE_IsValid(drive)) ret |= (1 << drive);
750     return ret;
751 }
752
753
754 /***********************************************************************
755  *           GetVolumeInformation32A   (KERNEL32.309)
756  */
757 BOOL32 GetVolumeInformation32A( LPCSTR root, LPSTR label, DWORD label_len,
758                                 DWORD *serial, DWORD *filename_len,
759                                 DWORD *flags, LPSTR fsname, DWORD fsname_len )
760 {
761     int drive;
762
763     /* FIXME, SetLastErrors missing */
764
765     if (!root) drive = DRIVE_GetCurrentDrive();
766     else
767     {
768         if ((root[1] != ':') || (root[2] != '\\'))
769         {
770             fprintf( stderr, "GetVolumeInformation: invalid root '%s'\n",root);
771             return FALSE;
772         }
773         drive = toupper(root[0]) - 'A';
774     }
775     if (!DRIVE_IsValid( drive )) return FALSE;
776     if (label) lstrcpyn32A( label, DOSDrives[drive].label, label_len );
777     if (serial) *serial = DOSDrives[drive].serial;
778
779     /* Set the filesystem information */
780     /* Note: we only emulate a FAT fs at the present */
781
782     if (filename_len) *filename_len = 12;
783     if (flags) *flags = 0;
784     if (fsname) lstrcpyn32A( fsname, "FAT", fsname_len );
785     return TRUE;
786 }
787
788
789 /***********************************************************************
790  *           GetVolumeInformation32W   (KERNEL32.310)
791  */
792 BOOL32 GetVolumeInformation32W( LPCWSTR root, LPWSTR label, DWORD label_len,
793                                 DWORD *serial, DWORD *filename_len,
794                                 DWORD *flags, LPWSTR fsname, DWORD fsname_len)
795 {
796     LPSTR xroot    = STRING32_DupUniToAnsi(root);
797     LPSTR xvolname = (char*)xmalloc( label_len );
798     LPSTR xfsname  = (char*)xmalloc( fsname_len );
799     BOOL32 ret = GetVolumeInformation32A( xroot, xvolname, label_len, serial,
800                                           filename_len, flags, xfsname,
801                                           fsname_len );
802     if (ret)
803     {
804         STRING32_AnsiToUni( label, xvolname );
805         STRING32_AnsiToUni( fsname, xfsname );
806     }
807     free(xroot);
808     free(xvolname);
809     free(xfsname);
810     return ret;
811 }