New stubs.
[wine/multimedia.git] / files / drive.c
blobd1c190609a81f4560bbdd18d45a4f35494465b3e
1 /*
2 * DOS drives handling functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include "config.h"
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>
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
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"
44 typedef struct
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;
59 static const char * const DRIVE_Types[] =
61 "floppy", /* TYPE_FLOPPY */
62 "hd", /* TYPE_HD */
63 "cdrom", /* TYPE_CDROM */
64 "network" /* TYPE_NETWORK */
68 /* Known filesystem types */
70 typedef struct
72 const char *name;
73 UINT32 flags;
74 } FS_DESCR;
76 static const FS_DESCR DRIVE_Filesystems[] =
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 }
88 static DOSDRIVE DOSDrives[MAX_DOS_DRIVES];
89 static int DRIVE_CurDrive = -1;
91 static HTASK16 DRIVE_LastTask = 0;
94 /***********************************************************************
95 * DRIVE_GetDriveType
97 static DRIVETYPE DRIVE_GetDriveType( const char *name )
99 char buffer[20];
100 int i;
102 PROFILE_GetWineIniString( name, "Type", "hd", buffer, sizeof(buffer) );
103 for (i = 0; i < sizeof(DRIVE_Types)/sizeof(DRIVE_Types[0]); i++)
105 if (!strcasecmp( buffer, DRIVE_Types[i] )) return (DRIVETYPE)i;
107 MSG("%s: unknown type '%s', defaulting to 'hd'.\n", name, buffer );
108 return TYPE_HD;
112 /***********************************************************************
113 * DRIVE_GetFSFlags
115 static UINT32 DRIVE_GetFSFlags( const char *name, const char *value )
117 const FS_DESCR *descr;
119 for (descr = DRIVE_Filesystems; descr->name; descr++)
120 if (!strcasecmp( 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;
127 /***********************************************************************
128 * DRIVE_Init
130 int DRIVE_Init(void)
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;
140 for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, name[6]++, drive++)
142 PROFILE_GetWineIniString( name, "Path", "", path, sizeof(path)-1 );
143 if (path[0])
145 p = path + strlen(path) - 1;
146 while ((p > path) && ((*p == '/') || (*p == '\\'))) *p-- = '\0';
147 if (!path[0]) strcpy( path, "/" );
149 if (stat( path, &drive_stat_buffer ))
151 MSG("Could not stat %s, ignoring drive %c:\n", path, 'A' + i );
152 continue;
154 if (!S_ISDIR(drive_stat_buffer.st_mode))
156 MSG("%s is not a directory, ignoring drive %c:\n",
157 path, 'A' + i );
158 continue;
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;
170 /* Get the drive label */
171 PROFILE_GetWineIniString( name, "Label", name, drive->label, 12 );
172 if ((len = strlen(drive->label)) < 11)
174 /* Pad label with spaces */
175 memset( drive->label + len, ' ', 11 - len );
176 drive->label[12] = '\0';
179 /* Get the serial number */
180 PROFILE_GetWineIniString( name, "Serial", "12345678",
181 buffer, sizeof(buffer) );
182 drive->serial = strtoul( buffer, NULL, 16 );
184 /* Get the filesystem type */
185 PROFILE_GetWineIniString( name, "Filesystem", "unix",
186 buffer, sizeof(buffer) );
187 drive->flags = DRIVE_GetFSFlags( name, buffer );
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 );
195 /* Make the first hard disk the current drive */
196 if ((DRIVE_CurDrive == -1) && (drive->type == TYPE_HD))
197 DRIVE_CurDrive = i;
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 );
205 else WARN(dosfs, "%s: not defined\n", name );
208 if (!count)
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;
222 /* Make sure the current drive is valid */
223 if (DRIVE_CurDrive == -1)
225 for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, drive++)
227 if (drive->root && !(drive->flags & DRIVE_DISABLED))
229 DRIVE_CurDrive = i;
230 break;
235 return 1;
239 /***********************************************************************
240 * DRIVE_IsValid
242 int DRIVE_IsValid( int drive )
244 if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
245 return (DOSDrives[drive].root &&
246 !(DOSDrives[drive].flags & DRIVE_DISABLED));
250 /***********************************************************************
251 * DRIVE_GetCurrentDrive
253 int DRIVE_GetCurrentDrive(void)
255 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
256 if (pTask && (pTask->curdrive & 0x80)) return pTask->curdrive & ~0x80;
257 return DRIVE_CurDrive;
261 /***********************************************************************
262 * DRIVE_SetCurrentDrive
264 int DRIVE_SetCurrentDrive( int drive )
266 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
267 if (!DRIVE_IsValid( drive ))
269 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
270 return 0;
272 TRACE(dosfs, "%c:\n", 'A' + drive );
273 DRIVE_CurDrive = drive;
274 if (pTask) pTask->curdrive = drive | 0x80;
275 return 1;
279 /***********************************************************************
280 * DRIVE_FindDriveRoot
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.
287 int DRIVE_FindDriveRoot( const char **path )
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.
295 int drive, rootdrive = -1;
296 char buffer[MAX_PATHNAME_LEN];
297 char *next = buffer;
298 const char *p = *path;
299 struct stat st;
301 strcpy( buffer, "/" );
302 for (;;)
304 if (stat( buffer, &st ) || !S_ISDIR( st.st_mode )) break;
306 /* Find the drive */
308 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
310 if (!DOSDrives[drive].root ||
311 (DOSDrives[drive].flags & DRIVE_DISABLED)) continue;
313 if ((DOSDrives[drive].dev == st.st_dev) &&
314 (DOSDrives[drive].ino == st.st_ino))
316 rootdrive = drive;
317 *path = p;
321 /* Get the next path component */
323 *next++ = '/';
324 while ((*p == '/') || (*p == '\\')) p++;
325 if (!*p) break;
326 while (!IS_END_OF_NAME(*p)) *next++ = *p++;
327 *next = 0;
329 *next = 0;
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;
339 /***********************************************************************
340 * DRIVE_GetRoot
342 const char * DRIVE_GetRoot( int drive )
344 if (!DRIVE_IsValid( drive )) return NULL;
345 return DOSDrives[drive].root;
349 /***********************************************************************
350 * DRIVE_GetDosCwd
352 const char * DRIVE_GetDosCwd( int drive )
354 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
355 if (!DRIVE_IsValid( drive )) return NULL;
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 */
362 /* Perform the task-switch */
363 if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
364 DRIVE_LastTask = GetCurrentTask();
366 return DOSDrives[drive].dos_cwd;
370 /***********************************************************************
371 * DRIVE_GetUnixCwd
373 const char * DRIVE_GetUnixCwd( int drive )
375 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
376 if (!DRIVE_IsValid( drive )) return NULL;
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 */
383 /* Perform the task-switch */
384 if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
385 DRIVE_LastTask = GetCurrentTask();
387 return DOSDrives[drive].unix_cwd;
391 /***********************************************************************
392 * DRIVE_GetLabel
394 const char * DRIVE_GetLabel( int drive )
396 if (!DRIVE_IsValid( drive )) return NULL;
397 return DOSDrives[drive].label;
401 /***********************************************************************
402 * DRIVE_GetSerialNumber
404 DWORD DRIVE_GetSerialNumber( int drive )
406 if (!DRIVE_IsValid( drive )) return 0;
407 return DOSDrives[drive].serial;
411 /***********************************************************************
412 * DRIVE_SetSerialNumber
414 int DRIVE_SetSerialNumber( int drive, DWORD serial )
416 if (!DRIVE_IsValid( drive )) return 0;
417 DOSDrives[drive].serial = serial;
418 return 1;
422 /***********************************************************************
423 * DRIVE_GetType
425 DRIVETYPE DRIVE_GetType( int drive )
427 if (!DRIVE_IsValid( drive )) return TYPE_INVALID;
428 return DOSDrives[drive].type;
432 /***********************************************************************
433 * DRIVE_GetFlags
435 UINT32 DRIVE_GetFlags( int drive )
437 if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
438 return DOSDrives[drive].flags;
442 /***********************************************************************
443 * DRIVE_Chdir
445 int DRIVE_Chdir( int drive, const char *path )
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() );
453 strcpy( buffer, "A:" );
454 buffer[0] += drive;
455 TRACE(dosfs, "(%c:,%s)\n", buffer[0], path );
456 lstrcpyn32A( buffer + 2, path, sizeof(buffer) - 2 );
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))
462 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
463 return 0;
465 unix_cwd = full_name.long_name + strlen( DOSDrives[drive].root );
466 while (*unix_cwd == '/') unix_cwd++;
468 TRACE(dosfs, "(%c:): unix_cwd=%s dos_cwd=%s\n",
469 'A' + drive, unix_cwd, full_name.short_name + 3 );
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 );
477 if (pTask && (pTask->curdrive & 0x80) &&
478 ((pTask->curdrive & ~0x80) == drive))
480 lstrcpyn32A( pTask->curdir, full_name.short_name + 2,
481 sizeof(pTask->curdir) );
482 DRIVE_LastTask = GetCurrentTask();
484 return 1;
488 /***********************************************************************
489 * DRIVE_Disable
491 int DRIVE_Disable( int drive )
493 if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
495 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
496 return 0;
498 DOSDrives[drive].flags |= DRIVE_DISABLED;
499 return 1;
503 /***********************************************************************
504 * DRIVE_Enable
506 int DRIVE_Enable( int drive )
508 if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
510 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
511 return 0;
513 DOSDrives[drive].flags &= ~DRIVE_DISABLED;
514 return 1;
518 /***********************************************************************
519 * DRIVE_SetLogicalMapping
521 int DRIVE_SetLogicalMapping ( int existing_drive, int new_drive )
523 /* If new_drive is already valid, do nothing and return 0
524 otherwise, copy DOSDrives[existing_drive] to DOSDrives[new_drive] */
526 DOSDRIVE *old, *new;
528 old = DOSDrives + existing_drive;
529 new = DOSDrives + new_drive;
531 if ((existing_drive < 0) || (existing_drive >= MAX_DOS_DRIVES) ||
532 !old->root ||
533 (new_drive < 0) || (new_drive >= MAX_DOS_DRIVES))
535 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
536 return 0;
539 if ( new->root )
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 /* it is already mapped there, so return success */
546 if (!strcmp(old->root,new->root))
547 return 1;
548 return 0;
551 new->root = HEAP_strdupA( SystemHeap, 0, old->root );
552 new->dos_cwd = HEAP_strdupA( SystemHeap, 0, old->dos_cwd );
553 new->unix_cwd = HEAP_strdupA( SystemHeap, 0, old->unix_cwd );
554 memcpy ( new->label, old->label, 12 );
555 new->serial = old->serial;
556 new->type = old->type;
557 new->flags = old->flags;
558 new->dev = old->dev;
559 new->ino = old->ino;
561 TRACE(dosfs, "Drive %c is now equal to drive %c\n",
562 'A' + new_drive, 'A' + existing_drive );
564 return 1;
568 /***********************************************************************
569 * DRIVE_OpenDevice
571 * Open the drive raw device and return a Unix fd (or -1 on error).
573 int DRIVE_OpenDevice( int drive, int flags )
575 if (!DRIVE_IsValid( drive )) return -1;
576 return open( DOSDrives[drive].device, flags );
580 /***********************************************************************
581 * DRIVE_GetFreeSpace
583 static int DRIVE_GetFreeSpace( int drive, LPULARGE_INTEGER size,
584 LPULARGE_INTEGER available )
586 struct statfs info;
587 unsigned long long bigsize,bigavail=0;
589 if (!DRIVE_IsValid(drive))
591 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
592 return 0;
595 /* FIXME: add autoconf check for this */
596 #if defined(__svr4__) || defined(_SCO_DS)
597 if (statfs( DOSDrives[drive].root, &info, 0, 0) < 0)
598 #else
599 if (statfs( DOSDrives[drive].root, &info) < 0)
600 #endif
602 FILE_SetDosError();
603 WARN(dosfs, "cannot do statfs(%s)\n", DOSDrives[drive].root);
604 return 0;
607 bigsize = (unsigned long long)info.f_bsize
608 * (unsigned long long)info.f_blocks;
609 #ifdef STATFS_HAS_BAVAIL
610 bigavail = (unsigned long long)info.f_bavail
611 * (unsigned long long)info.f_bsize;
612 #else
613 # ifdef STATFS_HAS_BFREE
614 bigavail = (unsigned long long)info.f_bfree
615 * (unsigned long long)info.f_bsize;
616 # else
617 # error "statfs has no bfree/bavail member!"
618 # endif
619 #endif
620 size->LowPart = (DWORD)bigsize;
621 size->HighPart = (DWORD)(bigsize>>32);
622 available->LowPart = (DWORD)bigavail;
623 available->HighPart = (DWORD)(bigavail>>32);
624 return 1;
628 /***********************************************************************
629 * GetDiskFreeSpace16 (KERNEL.422)
631 BOOL16 WINAPI GetDiskFreeSpace16( LPCSTR root, LPDWORD cluster_sectors,
632 LPDWORD sector_bytes, LPDWORD free_clusters,
633 LPDWORD total_clusters )
635 return GetDiskFreeSpace32A( root, cluster_sectors, sector_bytes,
636 free_clusters, total_clusters );
640 /***********************************************************************
641 * GetDiskFreeSpace32A (KERNEL32.206)
643 * Fails if expression resulting from current drive's dir and "root"
644 * is not a root dir of the target drive.
646 * UNDOC: setting some LPDWORDs to NULL is perfectly possible
647 * if the corresponding info is unneeded.
649 * FIXME: needs to support UNC names from Win95 OSR2 on.
651 * Behaviour under Win95a:
652 * CurrDir root result
653 * "E:\\TEST" "E:" FALSE
654 * "E:\\" "E:" TRUE
655 * "E:\\" "E" FALSE
656 * "E:\\" "\\" TRUE
657 * "E:\\TEST" "\\" TRUE
658 * "E:\\TEST" ":\\" FALSE
659 * "E:\\TEST" "E:\\" TRUE
660 * "E:\\TEST" "" FALSE
661 * "E:\\" "" FALSE (!)
662 * "E:\\" 0x0 TRUE
663 * "E:\\TEST" 0x0 TRUE (!)
664 * "E:\\TEST" "C:" TRUE (when CurrDir of "C:" set to "\\")
665 * "E:\\TEST" "C:" FALSE (when CurrDir of "C:" set to "\\TEST")
667 BOOL32 WINAPI GetDiskFreeSpace32A( LPCSTR root, LPDWORD cluster_sectors,
668 LPDWORD sector_bytes, LPDWORD free_clusters,
669 LPDWORD total_clusters )
671 int drive;
672 ULARGE_INTEGER size,available;
673 LPCSTR path;
674 DWORD cluster_sec;
676 if ((!root) || (root == "\\"))
677 drive = DRIVE_GetCurrentDrive();
678 else
679 if ( (strlen(root) >= 2) && (root[1] == ':')) /* root contains drive tag */
681 drive = toupper(root[0]) - 'A';
682 path = &root[2];
683 if (path[0] == '\0')
684 path = DRIVE_GetDosCwd(drive);
685 else
686 if (path[0] == '\\')
687 path++;
688 if (strlen(path)) /* oops, we are in a subdir */
689 return FALSE;
691 else
692 return FALSE;
694 if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
696 /* Cap the size and available at 2GB as per specs. */
697 if ((size.HighPart) ||(size.LowPart > 0x7fffffff))
699 size.HighPart = 0;
700 size.LowPart = 0x7fffffff;
702 if ((available.HighPart) ||(available.LowPart > 0x7fffffff))
704 available.HighPart =0;
705 available.LowPart = 0x7fffffff;
707 if (DRIVE_GetType(drive)==TYPE_CDROM) {
708 if (sector_bytes)
709 *sector_bytes = 2048;
710 size.LowPart /= 2048;
711 available.LowPart /= 2048;
712 } else {
713 if (sector_bytes)
714 *sector_bytes = 512;
715 size.LowPart /= 512;
716 available.LowPart /= 512;
718 /* fixme: probably have to adjust those variables too for CDFS */
719 cluster_sec = 1;
720 while (cluster_sec * 65536 < size.LowPart) cluster_sec *= 2;
722 if (cluster_sectors)
723 *cluster_sectors = cluster_sec;
724 if (free_clusters)
725 *free_clusters = available.LowPart / cluster_sec;
726 if (total_clusters)
727 *total_clusters = size.LowPart / cluster_sec;
728 return TRUE;
732 /***********************************************************************
733 * GetDiskFreeSpace32W (KERNEL32.207)
735 BOOL32 WINAPI GetDiskFreeSpace32W( LPCWSTR root, LPDWORD cluster_sectors,
736 LPDWORD sector_bytes, LPDWORD free_clusters,
737 LPDWORD total_clusters )
739 LPSTR xroot;
740 BOOL32 ret;
742 xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
743 ret = GetDiskFreeSpace32A( xroot,cluster_sectors, sector_bytes,
744 free_clusters, total_clusters );
745 HeapFree( GetProcessHeap(), 0, xroot );
746 return ret;
750 /***********************************************************************
751 * GetDiskFreeSpaceEx32A (KERNEL32.871)
753 BOOL32 WINAPI GetDiskFreeSpaceEx32A( LPCSTR root,
754 LPULARGE_INTEGER avail,
755 LPULARGE_INTEGER total,
756 LPULARGE_INTEGER totalfree)
758 int drive;
759 ULARGE_INTEGER size,available;
761 if (!root) drive = DRIVE_GetCurrentDrive();
762 else
764 if ((root[1]) && ((root[1] != ':') || (root[2] != '\\')))
766 WARN(dosfs, "invalid root '%s'\n", root );
767 return FALSE;
769 drive = toupper(root[0]) - 'A';
771 if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
772 /*FIXME: Do we have the number of bytes available to the user? */
773 avail->HighPart = available.HighPart;
774 totalfree->HighPart = size.HighPart;
775 avail->LowPart = available.LowPart ;
776 totalfree->LowPart = size.LowPart ;
777 return TRUE;
780 /***********************************************************************
781 * GetDiskFreeSpaceEx32W (KERNEL32.873)
783 BOOL32 WINAPI GetDiskFreeSpaceEx32W( LPCWSTR root, LPULARGE_INTEGER avail,
784 LPULARGE_INTEGER total,
785 LPULARGE_INTEGER totalfree)
787 LPSTR xroot;
788 BOOL32 ret;
790 xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
791 ret = GetDiskFreeSpaceEx32A( xroot, avail, total, totalfree);
792 HeapFree( GetProcessHeap(), 0, xroot );
793 return ret;
796 /***********************************************************************
797 * GetDriveType16 (KERNEL.136)
798 * This functions returns the drivetype of a drive in Win16.
799 * Note that it returns DRIVE_REMOTE for CD-ROMs, since MSCDEX uses the
800 * remote drive API. The returnvalue DRIVE_REMOTE for CD-ROMs has been
801 * verified on Win3.11 and Windows 95. Some programs rely on it, so don't
802 * do any pseudo-clever changes.
804 * RETURNS
805 * drivetype DRIVE_xxx
807 UINT16 WINAPI GetDriveType16(
808 UINT16 drive /* [in] number (NOT letter) of drive */
810 TRACE(dosfs, "(%c:)\n", 'A' + drive );
811 switch(DRIVE_GetType(drive))
813 case TYPE_FLOPPY: return DRIVE_REMOVABLE;
814 case TYPE_HD: return DRIVE_FIXED;
815 case TYPE_CDROM: return DRIVE_REMOTE;
816 case TYPE_NETWORK: return DRIVE_REMOTE;
817 case TYPE_INVALID:
818 default: return DRIVE_CANNOTDETERMINE;
823 /***********************************************************************
824 * GetDriveType32A (KERNEL32.208)
826 * Returns the type of the disk drive specified. If root is NULL the
827 * root of the current directory is used.
829 * RETURNS
831 * Type of drive (from Win32 SDK):
833 * DRIVE_UNKNOWN unable to find out anything about the drive
834 * DRIVE_NO_ROOT_DIR nonexistand root dir
835 * DRIVE_REMOVABLE the disk can be removed from the machine
836 * DRIVE_FIXED the disk can not be removed from the machine
837 * DRIVE_REMOTE network disk
838 * DRIVE_CDROM CDROM drive
839 * DRIVE_RAMDISK virtual disk in ram
841 * DRIVE_DOESNOTEXIST XXX Not valid return value
842 * DRIVE_CANNOTDETERMINE XXX Not valid return value
844 * BUGS
846 * Currently returns DRIVE_DOESNOTEXIST and DRIVE_CANNOTDETERMINE
847 * when it really should return DRIVE_NO_ROOT_DIR and DRIVE_UNKNOWN.
848 * Why where the former defines used?
850 * DRIVE_RAMDISK is unsupported.
852 UINT32 WINAPI GetDriveType32A(LPCSTR root /* String describing drive */)
854 int drive;
855 TRACE(dosfs, "(%s)\n", debugstr_a(root));
857 if (NULL == root) drive = DRIVE_GetCurrentDrive();
858 else
860 if ((root[1]) && (root[1] != ':'))
862 WARN(dosfs, "invalid root '%s'\n", debugstr_a(root));
863 return DRIVE_DOESNOTEXIST;
865 drive = toupper(root[0]) - 'A';
867 switch(DRIVE_GetType(drive))
869 case TYPE_FLOPPY: return DRIVE_REMOVABLE;
870 case TYPE_HD: return DRIVE_FIXED;
871 case TYPE_CDROM: return DRIVE_CDROM;
872 case TYPE_NETWORK: return DRIVE_REMOTE;
873 case TYPE_INVALID: return DRIVE_DOESNOTEXIST;
874 default: return DRIVE_CANNOTDETERMINE;
879 /***********************************************************************
880 * GetDriveType32W (KERNEL32.209)
882 UINT32 WINAPI GetDriveType32W( LPCWSTR root )
884 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
885 UINT32 ret = GetDriveType32A( xpath );
886 HeapFree( GetProcessHeap(), 0, xpath );
887 return ret;
891 /***********************************************************************
892 * GetCurrentDirectory16 (KERNEL.411)
894 UINT16 WINAPI GetCurrentDirectory16( UINT16 buflen, LPSTR buf )
896 return (UINT16)GetCurrentDirectory32A( buflen, buf );
900 /***********************************************************************
901 * GetCurrentDirectory32A (KERNEL32.196)
903 * Returns "X:\\path\\etc\\".
905 * Despite the API description, return required length including the
906 * terminating null when buffer too small. This is the real behaviour.
908 UINT32 WINAPI GetCurrentDirectory32A( UINT32 buflen, LPSTR buf )
910 UINT32 ret;
911 const char *s = DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );
913 assert(s);
914 ret = strlen(s) + 3; /* length of WHOLE current directory */
915 if (ret >= buflen) return ret + 1;
916 lstrcpyn32A( buf, "A:\\", MIN( 4, buflen ) );
917 if (buflen) buf[0] += DRIVE_GetCurrentDrive();
918 if (buflen > 3) lstrcpyn32A( buf + 3, s, buflen - 3 );
919 return ret;
923 /***********************************************************************
924 * GetCurrentDirectory32W (KERNEL32.197)
926 UINT32 WINAPI GetCurrentDirectory32W( UINT32 buflen, LPWSTR buf )
928 LPSTR xpath = HeapAlloc( GetProcessHeap(), 0, buflen+1 );
929 UINT32 ret = GetCurrentDirectory32A( buflen, xpath );
930 lstrcpyAtoW( buf, xpath );
931 HeapFree( GetProcessHeap(), 0, xpath );
932 return ret;
936 /***********************************************************************
937 * SetCurrentDirectory (KERNEL.412)
939 BOOL16 WINAPI SetCurrentDirectory16( LPCSTR dir )
941 return SetCurrentDirectory32A( dir );
945 /***********************************************************************
946 * SetCurrentDirectory32A (KERNEL32.479)
948 BOOL32 WINAPI SetCurrentDirectory32A( LPCSTR dir )
950 int olddrive, drive = DRIVE_GetCurrentDrive();
952 if (!dir) {
953 ERR(file,"(NULL)!\n");
954 return FALSE;
956 if (dir[0] && (dir[1]==':'))
958 drive = tolower( *dir ) - 'a';
959 dir += 2;
962 /* WARNING: we need to set the drive before the dir, as DRIVE_Chdir
963 sets pTask->curdir only if pTask->curdrive is drive */
964 olddrive = drive; /* in case DRIVE_Chdir fails */
965 if (!(DRIVE_SetCurrentDrive( drive )))
966 return FALSE;
967 /* FIXME: what about empty strings? Add a \\ ? */
968 if (!DRIVE_Chdir( drive, dir )) {
969 DRIVE_SetCurrentDrive(olddrive);
970 return FALSE;
972 return TRUE;
976 /***********************************************************************
977 * SetCurrentDirectory32W (KERNEL32.480)
979 BOOL32 WINAPI SetCurrentDirectory32W( LPCWSTR dirW )
981 LPSTR dir = HEAP_strdupWtoA( GetProcessHeap(), 0, dirW );
982 BOOL32 res = SetCurrentDirectory32A( dir );
983 HeapFree( GetProcessHeap(), 0, dir );
984 return res;
988 /***********************************************************************
989 * GetLogicalDriveStrings32A (KERNEL32.231)
991 UINT32 WINAPI GetLogicalDriveStrings32A( UINT32 len, LPSTR buffer )
993 int drive, count;
995 for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
996 if (DRIVE_IsValid(drive)) count++;
997 if (count * 4 * sizeof(char) <= len)
999 LPSTR p = buffer;
1000 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
1001 if (DRIVE_IsValid(drive))
1003 *p++ = 'a' + drive;
1004 *p++ = ':';
1005 *p++ = '\\';
1006 *p++ = '\0';
1008 *p = '\0';
1010 return count * 4 * sizeof(char);
1014 /***********************************************************************
1015 * GetLogicalDriveStrings32W (KERNEL32.232)
1017 UINT32 WINAPI GetLogicalDriveStrings32W( UINT32 len, LPWSTR buffer )
1019 int drive, count;
1021 for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
1022 if (DRIVE_IsValid(drive)) count++;
1023 if (count * 4 * sizeof(WCHAR) <= len)
1025 LPWSTR p = buffer;
1026 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
1027 if (DRIVE_IsValid(drive))
1029 *p++ = (WCHAR)('a' + drive);
1030 *p++ = (WCHAR)':';
1031 *p++ = (WCHAR)'\\';
1032 *p++ = (WCHAR)'\0';
1034 *p = (WCHAR)'\0';
1036 return count * 4 * sizeof(WCHAR);
1040 /***********************************************************************
1041 * GetLogicalDrives (KERNEL32.233)
1043 DWORD WINAPI GetLogicalDrives(void)
1045 DWORD ret = 0;
1046 int drive;
1048 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
1049 if (DRIVE_IsValid(drive)) ret |= (1 << drive);
1050 return ret;
1054 /***********************************************************************
1055 * GetVolumeInformation32A (KERNEL32.309)
1057 BOOL32 WINAPI GetVolumeInformation32A( LPCSTR root, LPSTR label,
1058 DWORD label_len, DWORD *serial,
1059 DWORD *filename_len, DWORD *flags,
1060 LPSTR fsname, DWORD fsname_len )
1062 int drive;
1063 char *cp;
1065 /* FIXME, SetLastErrors missing */
1067 if (!root) drive = DRIVE_GetCurrentDrive();
1068 else
1070 if ((root[1]) && (root[1] != ':'))
1072 WARN(dosfs, "invalid root '%s'\n",root);
1073 return FALSE;
1075 drive = toupper(root[0]) - 'A';
1077 if (!DRIVE_IsValid( drive )) return FALSE;
1078 if (label)
1080 lstrcpyn32A( label, DRIVE_GetLabel(drive), label_len );
1081 for (cp = label; *cp; cp++);
1082 while (cp != label && *(cp-1) == ' ') cp--;
1083 *cp = '\0';
1085 if (serial) *serial = DRIVE_GetSerialNumber(drive);
1087 /* Set the filesystem information */
1088 /* Note: we only emulate a FAT fs at the present */
1090 if (filename_len) {
1091 if (DOSDrives[drive].flags & DRIVE_SHORT_NAMES)
1092 *filename_len = 12;
1093 else
1094 *filename_len = 255;
1096 if (flags) *flags = 0;
1097 if (fsname) {
1098 /* Diablo checks that return code ... */
1099 if (DRIVE_GetType(drive)==TYPE_CDROM)
1100 lstrcpyn32A( fsname, "CDFS", fsname_len );
1101 else
1102 lstrcpyn32A( fsname, "FAT", fsname_len );
1104 return TRUE;
1108 /***********************************************************************
1109 * GetVolumeInformation32W (KERNEL32.310)
1111 BOOL32 WINAPI GetVolumeInformation32W( LPCWSTR root, LPWSTR label,
1112 DWORD label_len, DWORD *serial,
1113 DWORD *filename_len, DWORD *flags,
1114 LPWSTR fsname, DWORD fsname_len )
1116 LPSTR xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
1117 LPSTR xvolname = label ? HeapAlloc(GetProcessHeap(),0,label_len) : NULL;
1118 LPSTR xfsname = fsname ? HeapAlloc(GetProcessHeap(),0,fsname_len) : NULL;
1119 BOOL32 ret = GetVolumeInformation32A( xroot, xvolname, label_len, serial,
1120 filename_len, flags, xfsname,
1121 fsname_len );
1122 if (ret)
1124 if (label) lstrcpyAtoW( label, xvolname );
1125 if (fsname) lstrcpyAtoW( fsname, xfsname );
1127 HeapFree( GetProcessHeap(), 0, xroot );
1128 HeapFree( GetProcessHeap(), 0, xvolname );
1129 HeapFree( GetProcessHeap(), 0, xfsname );
1130 return ret;
1133 BOOL32 WINAPI SetVolumeLabel32A(LPCSTR rootpath,LPCSTR volname) {
1134 FIXME(dosfs,"(%s,%s),stub!\n",rootpath,volname);
1135 return TRUE;