Release 971221
[wine/multimedia.git] / files / drive.c
blob0fd582d096e8ccf58ab51593938fb4e14291674c
1 /*
2 * DOS drives handling functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 */
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>
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
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"
38 typedef struct
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;
52 static const char * const DRIVE_Types[] =
54 "floppy", /* TYPE_FLOPPY */
55 "hd", /* TYPE_HD */
56 "cdrom", /* TYPE_CDROM */
57 "network" /* TYPE_NETWORK */
61 /* Known filesystem types */
63 typedef struct
65 const char *name;
66 UINT32 flags;
67 } FS_DESCR;
69 static const FS_DESCR DRIVE_Filesystems[] =
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 }
81 static DOSDRIVE DOSDrives[MAX_DOS_DRIVES];
82 static int DRIVE_CurDrive = -1;
84 static HTASK16 DRIVE_LastTask = 0;
87 /***********************************************************************
88 * DRIVE_GetDriveType
90 static DRIVETYPE DRIVE_GetDriveType( const char *name )
92 char buffer[20];
93 int i;
95 PROFILE_GetWineIniString( name, "Type", "hd", buffer, sizeof(buffer) );
96 for (i = 0; i < sizeof(DRIVE_Types)/sizeof(DRIVE_Types[0]); i++)
98 if (!lstrcmpi32A( buffer, DRIVE_Types[i] )) return (DRIVETYPE)i;
100 fprintf( stderr, "%s: unknown type '%s', defaulting to 'hd'.\n",
101 name, buffer );
102 return TYPE_HD;
106 /***********************************************************************
107 * DRIVE_GetFSFlags
109 static UINT32 DRIVE_GetFSFlags( const char *name, const char *value )
111 const FS_DESCR *descr;
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;
121 /***********************************************************************
122 * DRIVE_Init
124 int DRIVE_Init(void)
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;
134 for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, name[6]++, drive++)
136 PROFILE_GetWineIniString( name, "Path", "", path, sizeof(path)-1 );
137 if (path[0])
139 p = path + strlen(path) - 1;
140 while ((p > path) && ((*p == '/') || (*p == '\\'))) *p-- = '\0';
141 if (!path[0]) strcpy( path, "/" );
143 if (stat( path, &drive_stat_buffer ))
145 fprintf( stderr, "Could not stat %s, ignoring drive %c:\n",
146 path, 'A' + i );
147 continue;
149 if (!S_ISDIR(drive_stat_buffer.st_mode))
151 fprintf( stderr, "%s is not a directory, ignoring drive %c:\n",
152 path, 'A' + i );
153 continue;
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;
164 /* Get the drive label */
165 PROFILE_GetWineIniString( name, "Label", name, drive->label, 12 );
166 if ((len = strlen(drive->label)) < 11)
168 /* Pad label with spaces */
169 memset( drive->label + len, ' ', 11 - len );
170 drive->label[12] = '\0';
173 /* Get the serial number */
174 PROFILE_GetWineIniString( name, "Serial", "12345678",
175 buffer, sizeof(buffer) );
176 drive->serial = strtoul( buffer, NULL, 16 );
178 /* Get the filesystem type */
179 PROFILE_GetWineIniString( name, "Filesystem", "unix",
180 buffer, sizeof(buffer) );
181 drive->flags = DRIVE_GetFSFlags( name, buffer );
183 /* Make the first hard disk the current drive */
184 if ((DRIVE_CurDrive == -1) && (drive->type == TYPE_HD))
185 DRIVE_CurDrive = i;
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 );
193 else dprintf_dosfs( stddeb, "%s: not defined\n", name );
196 if (!count)
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;
210 /* Make sure the current drive is valid */
211 if (DRIVE_CurDrive == -1)
213 for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, drive++)
215 if (drive->root && !(drive->flags & DRIVE_DISABLED))
217 DRIVE_CurDrive = i;
218 break;
223 return 1;
227 /***********************************************************************
228 * DRIVE_IsValid
230 int DRIVE_IsValid( int drive )
232 if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
233 return (DOSDrives[drive].root &&
234 !(DOSDrives[drive].flags & DRIVE_DISABLED));
238 /***********************************************************************
239 * DRIVE_GetCurrentDrive
241 int DRIVE_GetCurrentDrive(void)
243 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
244 if (pTask && (pTask->curdrive & 0x80)) return pTask->curdrive & ~0x80;
245 return DRIVE_CurDrive;
249 /***********************************************************************
250 * DRIVE_SetCurrentDrive
252 int DRIVE_SetCurrentDrive( int drive )
254 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
255 if (!DRIVE_IsValid( drive ))
257 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
258 return 0;
260 dprintf_dosfs( stddeb, "DRIVE_SetCurrentDrive: %c:\n", 'A' + drive );
261 DRIVE_CurDrive = drive;
262 if (pTask) pTask->curdrive = drive | 0x80;
263 return 1;
267 /***********************************************************************
268 * DRIVE_FindDriveRoot
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.
275 int DRIVE_FindDriveRoot( const char **path )
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.
283 int drive, rootdrive = -1;
284 char buffer[MAX_PATHNAME_LEN];
285 char *next = buffer;
286 const char *p = *path;
287 struct stat st;
289 strcpy( buffer, "/" );
290 for (;;)
292 if (stat( buffer, &st ) || !S_ISDIR( st.st_mode )) break;
294 /* Find the drive */
296 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
298 if (!DOSDrives[drive].root ||
299 (DOSDrives[drive].flags & DRIVE_DISABLED)) continue;
301 if ((DOSDrives[drive].dev == st.st_dev) &&
302 (DOSDrives[drive].ino == st.st_ino))
304 rootdrive = drive;
305 *path = p;
309 /* Get the next path component */
311 *next++ = '/';
312 while ((*p == '/') || (*p == '\\')) p++;
313 if (!*p) break;
314 while (!IS_END_OF_NAME(*p)) *next++ = *p++;
315 *next = 0;
317 *next = 0;
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;
327 /***********************************************************************
328 * DRIVE_GetRoot
330 const char * DRIVE_GetRoot( int drive )
332 if (!DRIVE_IsValid( drive )) return NULL;
333 return DOSDrives[drive].root;
337 /***********************************************************************
338 * DRIVE_GetDosCwd
340 const char * DRIVE_GetDosCwd( int drive )
342 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
343 if (!DRIVE_IsValid( drive )) return NULL;
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 */
350 /* Perform the task-switch */
351 if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
352 DRIVE_LastTask = GetCurrentTask();
354 return DOSDrives[drive].dos_cwd;
358 /***********************************************************************
359 * DRIVE_GetUnixCwd
361 const char * DRIVE_GetUnixCwd( int drive )
363 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
364 if (!DRIVE_IsValid( drive )) return NULL;
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 */
371 /* Perform the task-switch */
372 if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
373 DRIVE_LastTask = GetCurrentTask();
375 return DOSDrives[drive].unix_cwd;
379 /***********************************************************************
380 * DRIVE_GetLabel
382 const char * DRIVE_GetLabel( int drive )
384 if (!DRIVE_IsValid( drive )) return NULL;
385 return DOSDrives[drive].label;
389 /***********************************************************************
390 * DRIVE_GetSerialNumber
392 DWORD DRIVE_GetSerialNumber( int drive )
394 if (!DRIVE_IsValid( drive )) return 0;
395 return DOSDrives[drive].serial;
399 /***********************************************************************
400 * DRIVE_SetSerialNumber
402 int DRIVE_SetSerialNumber( int drive, DWORD serial )
404 if (!DRIVE_IsValid( drive )) return 0;
405 DOSDrives[drive].serial = serial;
406 return 1;
410 /***********************************************************************
411 * DRIVE_GetType
413 DRIVETYPE DRIVE_GetType( int drive )
415 if (!DRIVE_IsValid( drive )) return TYPE_INVALID;
416 return DOSDrives[drive].type;
420 /***********************************************************************
421 * DRIVE_GetFlags
423 UINT32 DRIVE_GetFlags( int drive )
425 if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
426 return DOSDrives[drive].flags;
430 /***********************************************************************
431 * DRIVE_Chdir
433 int DRIVE_Chdir( int drive, const char *path )
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() );
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 );
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))
450 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
451 return 0;
453 unix_cwd = full_name.long_name + strlen( DOSDrives[drive].root );
454 while (*unix_cwd == '/') unix_cwd++;
456 dprintf_dosfs( stddeb, "DRIVE_Chdir(%c:): unix_cwd=%s dos_cwd=%s\n",
457 'A' + drive, unix_cwd, full_name.short_name + 3 );
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 );
465 if (pTask && (pTask->curdrive & 0x80) &&
466 ((pTask->curdrive & ~0x80) == drive))
468 lstrcpyn32A( pTask->curdir, full_name.short_name + 2,
469 sizeof(pTask->curdir) );
470 DRIVE_LastTask = GetCurrentTask();
472 return 1;
476 /***********************************************************************
477 * DRIVE_Disable
479 int DRIVE_Disable( int drive )
481 if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
483 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
484 return 0;
486 DOSDrives[drive].flags |= DRIVE_DISABLED;
487 return 1;
491 /***********************************************************************
492 * DRIVE_Enable
494 int DRIVE_Enable( int drive )
496 if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
498 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
499 return 0;
501 DOSDrives[drive].flags &= ~DRIVE_DISABLED;
502 return 1;
506 /***********************************************************************
507 * DRIVE_SetLogicalMapping
509 int DRIVE_SetLogicalMapping ( int existing_drive, int new_drive )
511 /* If new_drive is already valid, do nothing and return 0
512 otherwise, copy DOSDrives[existing_drive] to DOSDrives[new_drive] */
514 DOSDRIVE *old, *new;
516 old = DOSDrives + existing_drive;
517 new = DOSDrives + new_drive;
519 if ((existing_drive < 0) || (existing_drive >= MAX_DOS_DRIVES) ||
520 !old->root ||
521 (new_drive < 0) || (new_drive >= MAX_DOS_DRIVES))
523 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
524 return 0;
527 if ( new->root )
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;
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;
546 dprintf_dosfs ( stddeb, "Drive %c is now equal to drive %c\n",
547 'A' + new_drive, 'A' + existing_drive );
549 return 1;
554 /***********************************************************************
555 * DRIVE_GetFreeSpace
557 static int DRIVE_GetFreeSpace( int drive, DWORD *size, DWORD *available )
559 struct statfs info;
561 if (!DRIVE_IsValid(drive))
563 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
564 return 0;
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
573 FILE_SetDosError();
574 fprintf(stderr,"dosfs: cannot do statfs(%s)\n", DOSDrives[drive].root);
575 return 0;
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;
588 /***********************************************************************
589 * GetDiskFreeSpace16 (KERNEL.422)
591 BOOL16 WINAPI GetDiskFreeSpace16( LPCSTR root, LPDWORD cluster_sectors,
592 LPDWORD sector_bytes, LPDWORD free_clusters,
593 LPDWORD total_clusters )
595 return GetDiskFreeSpace32A( root, cluster_sectors, sector_bytes,
596 free_clusters, total_clusters );
600 /***********************************************************************
601 * GetDiskFreeSpace32A (KERNEL32.206)
603 BOOL32 WINAPI GetDiskFreeSpace32A( LPCSTR root, LPDWORD cluster_sectors,
604 LPDWORD sector_bytes, LPDWORD free_clusters,
605 LPDWORD total_clusters )
607 int drive;
608 DWORD size,available;
610 if (!root) drive = DRIVE_GetCurrentDrive();
611 else
613 if ((root[1]) && ((root[1] != ':') || (root[2] != '\\')))
615 fprintf( stderr, "GetDiskFreeSpaceA: invalid root '%s'\n", root );
616 return FALSE;
618 drive = toupper(root[0]) - 'A';
620 if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
622 /* Cap the size and available at 2GB as per specs. */
623 if (size > 0x7fffffff) size = 0x7fffffff;
624 if (available > 0x7fffffff) available = 0x7fffffff;
626 *sector_bytes = 512;
627 size /= 512;
628 available /= 512;
629 *cluster_sectors = 1;
630 while (*cluster_sectors * 65530 < size) *cluster_sectors *= 2;
631 *free_clusters = available/ *cluster_sectors;
632 *total_clusters = size/ *cluster_sectors;
633 return TRUE;
637 /***********************************************************************
638 * GetDiskFreeSpace32W (KERNEL32.207)
640 BOOL32 WINAPI GetDiskFreeSpace32W( LPCWSTR root, LPDWORD cluster_sectors,
641 LPDWORD sector_bytes, LPDWORD free_clusters,
642 LPDWORD total_clusters )
644 LPSTR xroot;
645 BOOL32 ret;
647 xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
648 ret = GetDiskFreeSpace32A( xroot,cluster_sectors, sector_bytes,
649 free_clusters, total_clusters );
650 HeapFree( GetProcessHeap(), 0, xroot );
651 return ret;
655 /***********************************************************************
656 * GetDiskFreeSpaceEx32A (KERNEL32.871)
658 BOOL32 WINAPI GetDiskFreeSpaceEx32A( LPCSTR root,
659 LPULARGE_INTEGER avail,
660 LPULARGE_INTEGER total,
661 LPULARGE_INTEGER totalfree)
663 int drive;
664 DWORD size,available;
666 if (!root) drive = DRIVE_GetCurrentDrive();
667 else
669 if ((root[1]) && ((root[1] != ':') || (root[2] != '\\')))
671 fprintf( stderr, "GetDiskFreeSpaceExA: invalid root '%s'\n",
672 root );
673 return FALSE;
675 drive = toupper(root[0]) - 'A';
677 if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
678 /*FIXME: Do we have the number of bytes available to the user? */
679 avail->HighPart = total->HighPart = 0;
680 avail->LowPart = available;
681 total->LowPart = size;
682 if(totalfree)
684 totalfree->HighPart =0;
685 totalfree->LowPart= available;
687 return TRUE;
690 /***********************************************************************
691 * GetDiskFreeSpaceEx32W (KERNEL32.873)
693 BOOL32 WINAPI GetDiskFreeSpaceEx32W( LPCWSTR root, LPULARGE_INTEGER avail,
694 LPULARGE_INTEGER total,
695 LPULARGE_INTEGER totalfree)
697 LPSTR xroot;
698 BOOL32 ret;
700 xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
701 ret = GetDiskFreeSpaceEx32A( xroot, avail, total, totalfree);
702 HeapFree( GetProcessHeap(), 0, xroot );
703 return ret;
706 /***********************************************************************
707 * GetDriveType16 (KERNEL.136)
709 UINT16 WINAPI GetDriveType16( UINT16 drive )
711 dprintf_dosfs( stddeb, "GetDriveType16(%c:)\n", 'A' + drive );
712 switch(DRIVE_GetType(drive))
714 case TYPE_FLOPPY: return DRIVE_REMOVABLE;
715 case TYPE_HD: return DRIVE_FIXED;
716 case TYPE_CDROM: return DRIVE_REMOTE;
717 case TYPE_NETWORK: return DRIVE_REMOTE;
718 case TYPE_INVALID:
719 default: return DRIVE_CANNOTDETERMINE;
724 /***********************************************************************
725 * GetDriveType32A (KERNEL32.208)
727 UINT32 WINAPI GetDriveType32A( LPCSTR root )
729 dprintf_dosfs( stddeb, "GetDriveType32A(%s)\n", root );
730 if ((root[1]) && (root[1] != ':'))
732 fprintf( stderr, "GetDriveType32A: invalid root '%s'\n", root );
733 return DRIVE_DOESNOTEXIST;
735 switch(DRIVE_GetType(toupper(root[0]) - 'A'))
737 case TYPE_FLOPPY: return DRIVE_REMOVABLE;
738 case TYPE_HD: return DRIVE_FIXED;
739 case TYPE_CDROM: return DRIVE_CDROM;
740 case TYPE_NETWORK: return DRIVE_REMOTE;
741 case TYPE_INVALID:
742 default: return DRIVE_CANNOTDETERMINE;
747 /***********************************************************************
748 * GetDriveType32W (KERNEL32.209)
750 UINT32 WINAPI GetDriveType32W( LPCWSTR root )
752 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
753 UINT32 ret = GetDriveType32A( xpath );
754 HeapFree( GetProcessHeap(), 0, xpath );
755 return ret;
759 /***********************************************************************
760 * GetCurrentDirectory16 (KERNEL.411)
762 UINT16 WINAPI GetCurrentDirectory16( UINT16 buflen, LPSTR buf )
764 return (UINT16)GetCurrentDirectory32A( buflen, buf );
768 /***********************************************************************
769 * GetCurrentDirectory32A (KERNEL32.196)
771 * Returns "X:\\path\\etc\\".
773 UINT32 WINAPI GetCurrentDirectory32A( UINT32 buflen, LPSTR buf )
775 char *pref = "A:\\";
776 const char *s = DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );
777 assert(s);
778 lstrcpyn32A( buf, pref, MIN( 4, buflen ) );
779 if (buflen) buf[0] += DRIVE_GetCurrentDrive();
780 if (buflen > 3) lstrcpyn32A( buf + 3, s, buflen - 3 );
781 return strlen(s) + 3; /* length of WHOLE current directory */
785 /***********************************************************************
786 * GetCurrentDirectory32W (KERNEL32.197)
788 UINT32 WINAPI GetCurrentDirectory32W( UINT32 buflen, LPWSTR buf )
790 LPSTR xpath = HeapAlloc( GetProcessHeap(), 0, buflen+1 );
791 UINT32 ret = GetCurrentDirectory32A( buflen, xpath );
792 lstrcpyAtoW( buf, xpath );
793 HeapFree( GetProcessHeap(), 0, xpath );
794 return ret;
798 /***********************************************************************
799 * SetCurrentDirectory (KERNEL.412)
801 BOOL16 WINAPI SetCurrentDirectory16( LPCSTR dir )
803 return SetCurrentDirectory32A( dir );
807 /***********************************************************************
808 * SetCurrentDirectory32A (KERNEL32.479)
810 BOOL32 WINAPI SetCurrentDirectory32A( LPCSTR dir )
812 int drive = DRIVE_GetCurrentDrive();
814 if (dir[0] && (dir[1]==':'))
816 drive = tolower( *dir ) - 'a';
817 if (!DRIVE_IsValid( drive ))
819 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
820 return FALSE;
822 dir += 2;
824 /* FIXME: what about empty strings? Add a \\ ? */
825 if (!DRIVE_Chdir( drive, dir )) return FALSE;
826 if (drive == DRIVE_GetCurrentDrive()) return TRUE;
827 return DRIVE_SetCurrentDrive( drive );
831 /***********************************************************************
832 * SetCurrentDirectory32W (KERNEL32.480)
834 BOOL32 WINAPI SetCurrentDirectory32W( LPCWSTR dirW )
836 LPSTR dir = HEAP_strdupWtoA( GetProcessHeap(), 0, dirW );
837 BOOL32 res = SetCurrentDirectory32A( dir );
838 HeapFree( GetProcessHeap(), 0, dir );
839 return res;
843 /***********************************************************************
844 * GetLogicalDriveStrings32A (KERNEL32.231)
846 UINT32 WINAPI GetLogicalDriveStrings32A( UINT32 len, LPSTR buffer )
848 int drive, count;
850 for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
851 if (DRIVE_IsValid(drive)) count++;
852 if (count * 4 * sizeof(char) <= len)
854 LPSTR p = buffer;
855 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
856 if (DRIVE_IsValid(drive))
858 *p++ = 'a' + drive;
859 *p++ = ':';
860 *p++ = '\\';
861 *p++ = '\0';
863 *p = '\0';
865 return count * 4 * sizeof(char);
869 /***********************************************************************
870 * GetLogicalDriveStrings32W (KERNEL32.232)
872 UINT32 WINAPI GetLogicalDriveStrings32W( UINT32 len, LPWSTR buffer )
874 int drive, count;
876 for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
877 if (DRIVE_IsValid(drive)) count++;
878 if (count * 4 * sizeof(WCHAR) <= len)
880 LPWSTR p = buffer;
881 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
882 if (DRIVE_IsValid(drive))
884 *p++ = (WCHAR)('a' + drive);
885 *p++ = (WCHAR)':';
886 *p++ = (WCHAR)'\\';
887 *p++ = (WCHAR)'\0';
889 *p = (WCHAR)'\0';
891 return count * 4 * sizeof(WCHAR);
895 /***********************************************************************
896 * GetLogicalDrives (KERNEL32.233)
898 DWORD WINAPI GetLogicalDrives(void)
900 DWORD ret = 0;
901 int drive;
903 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
904 if (DRIVE_IsValid(drive)) ret |= (1 << drive);
905 return ret;
909 /***********************************************************************
910 * GetVolumeInformation32A (KERNEL32.309)
912 BOOL32 WINAPI GetVolumeInformation32A( LPCSTR root, LPSTR label,
913 DWORD label_len, DWORD *serial,
914 DWORD *filename_len, DWORD *flags,
915 LPSTR fsname, DWORD fsname_len )
917 int drive;
919 /* FIXME, SetLastErrors missing */
921 if (!root) drive = DRIVE_GetCurrentDrive();
922 else
924 if ((root[1]) &&((root[1] != ':') || (root[2] != '\\')))
926 fprintf( stderr, "GetVolumeInformation: invalid root '%s'\n",root);
927 return FALSE;
929 drive = toupper(root[0]) - 'A';
931 if (!DRIVE_IsValid( drive )) return FALSE;
932 if (label) lstrcpyn32A( label, DOSDrives[drive].label, label_len );
933 if (serial) *serial = DOSDrives[drive].serial;
935 /* Set the filesystem information */
936 /* Note: we only emulate a FAT fs at the present */
938 if (filename_len) *filename_len = 12;
939 if (flags) *flags = 0;
940 if (fsname) lstrcpyn32A( fsname, "FAT16", fsname_len );
941 return TRUE;
945 /***********************************************************************
946 * GetVolumeInformation32W (KERNEL32.310)
948 BOOL32 WINAPI GetVolumeInformation32W( LPCWSTR root, LPWSTR label,
949 DWORD label_len, DWORD *serial,
950 DWORD *filename_len, DWORD *flags,
951 LPWSTR fsname, DWORD fsname_len )
953 LPSTR xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
954 LPSTR xvolname = label ? HeapAlloc(GetProcessHeap(),0,label_len) : NULL;
955 LPSTR xfsname = fsname ? HeapAlloc(GetProcessHeap(),0,fsname_len) : NULL;
956 BOOL32 ret = GetVolumeInformation32A( xroot, xvolname, label_len, serial,
957 filename_len, flags, xfsname,
958 fsname_len );
959 if (ret)
961 if (label) lstrcpyAtoW( label, xvolname );
962 if (fsname) lstrcpyAtoW( fsname, xfsname );
964 HeapFree( GetProcessHeap(), 0, xroot );
965 HeapFree( GetProcessHeap(), 0, xvolname );
966 HeapFree( GetProcessHeap(), 0, xfsname );
967 return ret;