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