Release 980301
[wine/hacks.git] / files / drive.c
blob35266cd3a5defc28bc9cdec584923b4d70474c98
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>
14 #include <fcntl.h>
16 #if defined(__linux__) || defined(sun) || defined(hpux)
17 #include <sys/vfs.h>
18 #endif
19 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
20 #include <sys/param.h>
21 #include <sys/mount.h>
22 #include <sys/errno.h>
23 #endif
24 #if defined(__svr4__) || defined(_SCO_DS) || defined(__EMX__)
25 #include <sys/statfs.h>
26 #endif
28 #include "windows.h"
29 #include "winbase.h"
30 #include "drive.h"
31 #include "file.h"
32 #include "heap.h"
33 #include "msdos.h"
34 #include "options.h"
35 #include "task.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 *device; /* raw device path */
44 char label[12]; /* drive label */
45 DWORD serial; /* drive serial number */
46 DRIVETYPE type; /* drive type */
47 UINT32 flags; /* drive flags */
48 dev_t dev; /* unix device number */
49 ino_t ino; /* unix inode number */
50 } DOSDRIVE;
53 static const char * const DRIVE_Types[] =
55 "floppy", /* TYPE_FLOPPY */
56 "hd", /* TYPE_HD */
57 "cdrom", /* TYPE_CDROM */
58 "network" /* TYPE_NETWORK */
62 /* Known filesystem types */
64 typedef struct
66 const char *name;
67 UINT32 flags;
68 } FS_DESCR;
70 static const FS_DESCR DRIVE_Filesystems[] =
72 { "unix", DRIVE_CASE_SENSITIVE | DRIVE_CASE_PRESERVING },
73 { "msdos", DRIVE_SHORT_NAMES },
74 { "dos", DRIVE_SHORT_NAMES },
75 { "fat", DRIVE_SHORT_NAMES },
76 { "vfat", DRIVE_CASE_PRESERVING },
77 { "win95", DRIVE_CASE_PRESERVING },
78 { NULL, 0 }
82 static DOSDRIVE DOSDrives[MAX_DOS_DRIVES];
83 static int DRIVE_CurDrive = -1;
85 static HTASK16 DRIVE_LastTask = 0;
88 /***********************************************************************
89 * DRIVE_GetDriveType
91 static DRIVETYPE DRIVE_GetDriveType( const char *name )
93 char buffer[20];
94 int i;
96 PROFILE_GetWineIniString( name, "Type", "hd", buffer, sizeof(buffer) );
97 for (i = 0; i < sizeof(DRIVE_Types)/sizeof(DRIVE_Types[0]); i++)
99 if (!lstrcmpi32A( buffer, DRIVE_Types[i] )) return (DRIVETYPE)i;
101 fprintf( stderr, "%s: unknown type '%s', defaulting to 'hd'.\n",
102 name, buffer );
103 return TYPE_HD;
107 /***********************************************************************
108 * DRIVE_GetFSFlags
110 static UINT32 DRIVE_GetFSFlags( const char *name, const char *value )
112 const FS_DESCR *descr;
114 for (descr = DRIVE_Filesystems; descr->name; descr++)
115 if (!lstrcmpi32A( value, descr->name )) return descr->flags;
116 fprintf( stderr, "%s: unknown filesystem type '%s', defaulting to 'unix'.\n",
117 name, value );
118 return DRIVE_CASE_SENSITIVE | DRIVE_CASE_PRESERVING;
122 /***********************************************************************
123 * DRIVE_Init
125 int DRIVE_Init(void)
127 int i, len, count = 0;
128 char name[] = "Drive A";
129 char path[MAX_PATHNAME_LEN];
130 char buffer[80];
131 struct stat drive_stat_buffer;
132 char *p;
133 DOSDRIVE *drive;
135 for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, name[6]++, drive++)
137 PROFILE_GetWineIniString( name, "Path", "", path, sizeof(path)-1 );
138 if (path[0])
140 p = path + strlen(path) - 1;
141 while ((p > path) && ((*p == '/') || (*p == '\\'))) *p-- = '\0';
142 if (!path[0]) strcpy( path, "/" );
144 if (stat( path, &drive_stat_buffer ))
146 fprintf( stderr, "Could not stat %s, ignoring drive %c:\n",
147 path, 'A' + i );
148 continue;
150 if (!S_ISDIR(drive_stat_buffer.st_mode))
152 fprintf( stderr, "%s is not a directory, ignoring drive %c:\n",
153 path, 'A' + i );
154 continue;
157 drive->root = HEAP_strdupA( SystemHeap, 0, path );
158 drive->dos_cwd = HEAP_strdupA( SystemHeap, 0, "" );
159 drive->unix_cwd = HEAP_strdupA( SystemHeap, 0, "" );
160 drive->type = DRIVE_GetDriveType( name );
161 drive->device = NULL;
162 drive->flags = 0;
163 drive->dev = drive_stat_buffer.st_dev;
164 drive->ino = drive_stat_buffer.st_ino;
166 /* Get the drive label */
167 PROFILE_GetWineIniString( name, "Label", name, drive->label, 12 );
168 if ((len = strlen(drive->label)) < 11)
170 /* Pad label with spaces */
171 memset( drive->label + len, ' ', 11 - len );
172 drive->label[12] = '\0';
175 /* Get the serial number */
176 PROFILE_GetWineIniString( name, "Serial", "12345678",
177 buffer, sizeof(buffer) );
178 drive->serial = strtoul( buffer, NULL, 16 );
180 /* Get the filesystem type */
181 PROFILE_GetWineIniString( name, "Filesystem", "unix",
182 buffer, sizeof(buffer) );
183 drive->flags = DRIVE_GetFSFlags( name, buffer );
185 /* Get the device */
186 PROFILE_GetWineIniString( name, "Device", "",
187 buffer, sizeof(buffer) );
188 if (buffer[0])
189 drive->device = HEAP_strdupA( SystemHeap, 0, buffer );
191 /* Make the first hard disk the current drive */
192 if ((DRIVE_CurDrive == -1) && (drive->type == TYPE_HD))
193 DRIVE_CurDrive = i;
195 count++;
196 dprintf_info(dosfs, "%s: path=%s type=%s label='%s' serial=%08lx flags=%08x dev=%x ino=%x\n",
197 name, path, DRIVE_Types[drive->type],
198 drive->label, drive->serial, drive->flags,
199 (int)drive->dev, (int)drive->ino );
201 else dprintf_warn(dosfs, "%s: not defined\n", name );
204 if (!count)
206 fprintf( stderr, "Warning: no valid DOS drive found, check your configuration file.\n" );
207 /* Create a C drive pointing to Unix root dir */
208 DOSDrives[2].root = HEAP_strdupA( SystemHeap, 0, "/" );
209 DOSDrives[2].dos_cwd = HEAP_strdupA( SystemHeap, 0, "" );
210 DOSDrives[2].unix_cwd = HEAP_strdupA( SystemHeap, 0, "" );
211 strcpy( DOSDrives[2].label, "Drive C " );
212 DOSDrives[2].serial = 0x12345678;
213 DOSDrives[2].type = TYPE_HD;
214 DOSDrives[2].flags = 0;
215 DRIVE_CurDrive = 2;
218 /* Make sure the current drive is valid */
219 if (DRIVE_CurDrive == -1)
221 for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, drive++)
223 if (drive->root && !(drive->flags & DRIVE_DISABLED))
225 DRIVE_CurDrive = i;
226 break;
231 return 1;
235 /***********************************************************************
236 * DRIVE_IsValid
238 int DRIVE_IsValid( int drive )
240 if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
241 return (DOSDrives[drive].root &&
242 !(DOSDrives[drive].flags & DRIVE_DISABLED));
246 /***********************************************************************
247 * DRIVE_GetCurrentDrive
249 int DRIVE_GetCurrentDrive(void)
251 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
252 if (pTask && (pTask->curdrive & 0x80)) return pTask->curdrive & ~0x80;
253 return DRIVE_CurDrive;
257 /***********************************************************************
258 * DRIVE_SetCurrentDrive
260 int DRIVE_SetCurrentDrive( int drive )
262 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
263 if (!DRIVE_IsValid( drive ))
265 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
266 return 0;
268 dprintf_info(dosfs, "DRIVE_SetCurrentDrive: %c:\n", 'A' + drive );
269 DRIVE_CurDrive = drive;
270 if (pTask) pTask->curdrive = drive | 0x80;
271 return 1;
275 /***********************************************************************
276 * DRIVE_FindDriveRoot
278 * Find a drive for which the root matches the begginning of the given path.
279 * This can be used to translate a Unix path into a drive + DOS path.
280 * Return value is the drive, or -1 on error. On success, path is modified
281 * to point to the beginning of the DOS path.
283 int DRIVE_FindDriveRoot( const char **path )
285 /* idea: check at all '/' positions.
286 * If the device and inode of that path is identical with the
287 * device and inode of the current drive then we found a solution.
288 * If there is another drive pointing to a deeper position in
289 * the file tree, we want to find that one, not the earlier solution.
291 int drive, rootdrive = -1;
292 char buffer[MAX_PATHNAME_LEN];
293 char *next = buffer;
294 const char *p = *path;
295 struct stat st;
297 strcpy( buffer, "/" );
298 for (;;)
300 if (stat( buffer, &st ) || !S_ISDIR( st.st_mode )) break;
302 /* Find the drive */
304 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
306 if (!DOSDrives[drive].root ||
307 (DOSDrives[drive].flags & DRIVE_DISABLED)) continue;
309 if ((DOSDrives[drive].dev == st.st_dev) &&
310 (DOSDrives[drive].ino == st.st_ino))
312 rootdrive = drive;
313 *path = p;
317 /* Get the next path component */
319 *next++ = '/';
320 while ((*p == '/') || (*p == '\\')) p++;
321 if (!*p) break;
322 while (!IS_END_OF_NAME(*p)) *next++ = *p++;
323 *next = 0;
325 *next = 0;
327 if (rootdrive != -1)
328 dprintf_info(dosfs, "DRIVE_FindDriveRoot: %s -> drive %c:, root='%s', name='%s'\n",
329 buffer, 'A' + rootdrive,
330 DOSDrives[rootdrive].root, *path );
331 return rootdrive;
335 /***********************************************************************
336 * DRIVE_GetRoot
338 const char * DRIVE_GetRoot( int drive )
340 if (!DRIVE_IsValid( drive )) return NULL;
341 return DOSDrives[drive].root;
345 /***********************************************************************
346 * DRIVE_GetDosCwd
348 const char * DRIVE_GetDosCwd( int drive )
350 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
351 if (!DRIVE_IsValid( drive )) return NULL;
353 /* Check if we need to change the directory to the new task. */
354 if (pTask && (pTask->curdrive & 0x80) && /* The task drive is valid */
355 ((pTask->curdrive & ~0x80) == drive) && /* and it's the one we want */
356 (DRIVE_LastTask != GetCurrentTask())) /* and the task changed */
358 /* Perform the task-switch */
359 if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
360 DRIVE_LastTask = GetCurrentTask();
362 return DOSDrives[drive].dos_cwd;
366 /***********************************************************************
367 * DRIVE_GetUnixCwd
369 const char * DRIVE_GetUnixCwd( int drive )
371 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
372 if (!DRIVE_IsValid( drive )) return NULL;
374 /* Check if we need to change the directory to the new task. */
375 if (pTask && (pTask->curdrive & 0x80) && /* The task drive is valid */
376 ((pTask->curdrive & ~0x80) == drive) && /* and it's the one we want */
377 (DRIVE_LastTask != GetCurrentTask())) /* and the task changed */
379 /* Perform the task-switch */
380 if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
381 DRIVE_LastTask = GetCurrentTask();
383 return DOSDrives[drive].unix_cwd;
387 /***********************************************************************
388 * DRIVE_GetLabel
390 const char * DRIVE_GetLabel( int drive )
392 if (!DRIVE_IsValid( drive )) return NULL;
393 return DOSDrives[drive].label;
397 /***********************************************************************
398 * DRIVE_GetSerialNumber
400 DWORD DRIVE_GetSerialNumber( int drive )
402 if (!DRIVE_IsValid( drive )) return 0;
403 return DOSDrives[drive].serial;
407 /***********************************************************************
408 * DRIVE_SetSerialNumber
410 int DRIVE_SetSerialNumber( int drive, DWORD serial )
412 if (!DRIVE_IsValid( drive )) return 0;
413 DOSDrives[drive].serial = serial;
414 return 1;
418 /***********************************************************************
419 * DRIVE_GetType
421 DRIVETYPE DRIVE_GetType( int drive )
423 if (!DRIVE_IsValid( drive )) return TYPE_INVALID;
424 return DOSDrives[drive].type;
428 /***********************************************************************
429 * DRIVE_GetFlags
431 UINT32 DRIVE_GetFlags( int drive )
433 if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
434 return DOSDrives[drive].flags;
438 /***********************************************************************
439 * DRIVE_Chdir
441 int DRIVE_Chdir( int drive, const char *path )
443 DOS_FULL_NAME full_name;
444 char buffer[MAX_PATHNAME_LEN];
445 LPSTR unix_cwd;
446 BY_HANDLE_FILE_INFORMATION info;
447 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
449 dprintf_info(dosfs, "DRIVE_Chdir(%c:,%s)\n", 'A' + drive, path );
450 strcpy( buffer, "A:" );
451 buffer[0] += drive;
452 lstrcpyn32A( buffer + 2, path, sizeof(buffer) - 2 );
454 if (!DOSFS_GetFullName( buffer, TRUE, &full_name )) return 0;
455 if (!FILE_Stat( full_name.long_name, &info )) return 0;
456 if (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
458 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
459 return 0;
461 unix_cwd = full_name.long_name + strlen( DOSDrives[drive].root );
462 while (*unix_cwd == '/') unix_cwd++;
464 dprintf_info(dosfs, "DRIVE_Chdir(%c:): unix_cwd=%s dos_cwd=%s\n",
465 'A' + drive, unix_cwd, full_name.short_name + 3 );
467 HeapFree( SystemHeap, 0, DOSDrives[drive].dos_cwd );
468 HeapFree( SystemHeap, 0, DOSDrives[drive].unix_cwd );
469 DOSDrives[drive].dos_cwd = HEAP_strdupA( SystemHeap, 0,
470 full_name.short_name + 3 );
471 DOSDrives[drive].unix_cwd = HEAP_strdupA( SystemHeap, 0, unix_cwd );
473 if (pTask && (pTask->curdrive & 0x80) &&
474 ((pTask->curdrive & ~0x80) == drive))
476 lstrcpyn32A( pTask->curdir, full_name.short_name + 2,
477 sizeof(pTask->curdir) );
478 DRIVE_LastTask = GetCurrentTask();
480 return 1;
484 /***********************************************************************
485 * DRIVE_Disable
487 int DRIVE_Disable( int drive )
489 if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
491 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
492 return 0;
494 DOSDrives[drive].flags |= DRIVE_DISABLED;
495 return 1;
499 /***********************************************************************
500 * DRIVE_Enable
502 int DRIVE_Enable( int drive )
504 if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
506 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
507 return 0;
509 DOSDrives[drive].flags &= ~DRIVE_DISABLED;
510 return 1;
514 /***********************************************************************
515 * DRIVE_SetLogicalMapping
517 int DRIVE_SetLogicalMapping ( int existing_drive, int new_drive )
519 /* If new_drive is already valid, do nothing and return 0
520 otherwise, copy DOSDrives[existing_drive] to DOSDrives[new_drive] */
522 DOSDRIVE *old, *new;
524 old = DOSDrives + existing_drive;
525 new = DOSDrives + new_drive;
527 if ((existing_drive < 0) || (existing_drive >= MAX_DOS_DRIVES) ||
528 !old->root ||
529 (new_drive < 0) || (new_drive >= MAX_DOS_DRIVES))
531 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
532 return 0;
535 if ( new->root )
537 dprintf_info(dosfs, "Can\'t map drive %c to drive %c - "
538 "drive %c already exists\n",
539 'A' + existing_drive, 'A' + new_drive,
540 'A' + new_drive );
541 return 0;
544 new->root = HEAP_strdupA( SystemHeap, 0, old->root );
545 new->dos_cwd = HEAP_strdupA( SystemHeap, 0, old->dos_cwd );
546 new->unix_cwd = HEAP_strdupA( SystemHeap, 0, old->unix_cwd );
547 memcpy ( new->label, old->label, 12 );
548 new->serial = old->serial;
549 new->type = old->type;
550 new->flags = old->flags;
551 new->dev = old->dev;
552 new->ino = old->ino;
554 dprintf_info(dosfs, "Drive %c is now equal to drive %c\n",
555 'A' + new_drive, 'A' + existing_drive );
557 return 1;
561 /***********************************************************************
562 * DRIVE_OpenDevice
564 * Open the drive raw device and return a Unix fd (or -1 on error).
566 int DRIVE_OpenDevice( int drive, int flags )
568 if (!DRIVE_IsValid( drive )) return -1;
569 return open( DOSDrives[drive].device, flags );
573 /***********************************************************************
574 * DRIVE_GetFreeSpace
576 static int DRIVE_GetFreeSpace( int drive, DWORD *size, DWORD *available )
578 struct statfs info;
580 if (!DRIVE_IsValid(drive))
582 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
583 return 0;
586 #if defined(__svr4__) || defined(_SCO_DS)
587 if (statfs( DOSDrives[drive].root, &info, 0, 0) < 0)
588 #else
589 if (statfs( DOSDrives[drive].root, &info) < 0)
590 #endif
592 FILE_SetDosError();
593 fprintf(stderr,"dosfs: cannot do statfs(%s)\n", DOSDrives[drive].root);
594 return 0;
597 *size = info.f_bsize * info.f_blocks;
598 #if defined(__svr4__) || defined(_SCO_DS) || defined(__EMX__)
599 *available = info.f_bfree * info.f_bsize;
600 #else
601 *available = info.f_bavail * info.f_bsize;
602 #endif
603 return 1;
607 /***********************************************************************
608 * GetDiskFreeSpace16 (KERNEL.422)
610 BOOL16 WINAPI GetDiskFreeSpace16( LPCSTR root, LPDWORD cluster_sectors,
611 LPDWORD sector_bytes, LPDWORD free_clusters,
612 LPDWORD total_clusters )
614 return GetDiskFreeSpace32A( root, cluster_sectors, sector_bytes,
615 free_clusters, total_clusters );
619 /***********************************************************************
620 * GetDiskFreeSpace32A (KERNEL32.206)
622 BOOL32 WINAPI GetDiskFreeSpace32A( LPCSTR root, LPDWORD cluster_sectors,
623 LPDWORD sector_bytes, LPDWORD free_clusters,
624 LPDWORD total_clusters )
626 int drive;
627 DWORD size,available;
629 if (!root) drive = DRIVE_GetCurrentDrive();
630 else
632 if ((root[1]) && ((root[1] != ':') || (root[2] != '\\')))
634 fprintf( stderr, "GetDiskFreeSpaceA: invalid root '%s'\n", root );
635 return FALSE;
637 drive = toupper(root[0]) - 'A';
639 if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
641 /* Cap the size and available at 2GB as per specs. */
642 if (size > 0x7fffffff) size = 0x7fffffff;
643 if (available > 0x7fffffff) available = 0x7fffffff;
645 if (DRIVE_GetType(drive)==TYPE_CDROM) {
646 *sector_bytes = 2048;
647 size /= 2048;
648 available /= 2048;
649 } else {
650 *sector_bytes = 512;
651 size /= 512;
652 available /= 512;
654 /* fixme: probably have to adjust those variables too for CDFS */
655 *cluster_sectors = 1;
656 while (*cluster_sectors * 65530 < size) *cluster_sectors *= 2;
657 *free_clusters = available/ *cluster_sectors;
658 *total_clusters = size/ *cluster_sectors;
659 return TRUE;
663 /***********************************************************************
664 * GetDiskFreeSpace32W (KERNEL32.207)
666 BOOL32 WINAPI GetDiskFreeSpace32W( LPCWSTR root, LPDWORD cluster_sectors,
667 LPDWORD sector_bytes, LPDWORD free_clusters,
668 LPDWORD total_clusters )
670 LPSTR xroot;
671 BOOL32 ret;
673 xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
674 ret = GetDiskFreeSpace32A( xroot,cluster_sectors, sector_bytes,
675 free_clusters, total_clusters );
676 HeapFree( GetProcessHeap(), 0, xroot );
677 return ret;
681 /***********************************************************************
682 * GetDiskFreeSpaceEx32A (KERNEL32.871)
684 BOOL32 WINAPI GetDiskFreeSpaceEx32A( LPCSTR root,
685 LPULARGE_INTEGER avail,
686 LPULARGE_INTEGER total,
687 LPULARGE_INTEGER totalfree)
689 int drive;
690 DWORD size,available;
692 if (!root) drive = DRIVE_GetCurrentDrive();
693 else
695 if ((root[1]) && ((root[1] != ':') || (root[2] != '\\')))
697 fprintf( stderr, "GetDiskFreeSpaceExA: invalid root '%s'\n",
698 root );
699 return FALSE;
701 drive = toupper(root[0]) - 'A';
703 if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
704 /*FIXME: Do we have the number of bytes available to the user? */
705 avail->HighPart = total->HighPart = 0;
706 avail->LowPart = available;
707 total->LowPart = size;
708 if(totalfree)
710 totalfree->HighPart =0;
711 totalfree->LowPart= available;
713 return TRUE;
716 /***********************************************************************
717 * GetDiskFreeSpaceEx32W (KERNEL32.873)
719 BOOL32 WINAPI GetDiskFreeSpaceEx32W( LPCWSTR root, LPULARGE_INTEGER avail,
720 LPULARGE_INTEGER total,
721 LPULARGE_INTEGER totalfree)
723 LPSTR xroot;
724 BOOL32 ret;
726 xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
727 ret = GetDiskFreeSpaceEx32A( xroot, avail, total, totalfree);
728 HeapFree( GetProcessHeap(), 0, xroot );
729 return ret;
732 /***********************************************************************
733 * GetDriveType16 (KERNEL.136)
735 UINT16 WINAPI GetDriveType16( UINT16 drive )
737 dprintf_info(dosfs, "GetDriveType16(%c:)\n", 'A' + drive );
738 switch(DRIVE_GetType(drive))
740 case TYPE_FLOPPY: return DRIVE_REMOVABLE;
741 case TYPE_HD: return DRIVE_FIXED;
742 case TYPE_CDROM: return DRIVE_REMOTE;
743 case TYPE_NETWORK: return DRIVE_REMOTE;
744 case TYPE_INVALID:
745 default: return DRIVE_CANNOTDETERMINE;
750 /***********************************************************************
751 * GetDriveType32A (KERNEL32.208)
753 UINT32 WINAPI GetDriveType32A( LPCSTR root )
755 dprintf_info(dosfs, "GetDriveType32A(%s)\n", root );
756 if ((root[1]) && (root[1] != ':'))
758 fprintf( stderr, "GetDriveType32A: invalid root '%s'\n", root );
759 return DRIVE_DOESNOTEXIST;
761 switch(DRIVE_GetType(toupper(root[0]) - 'A'))
763 case TYPE_FLOPPY: return DRIVE_REMOVABLE;
764 case TYPE_HD: return DRIVE_FIXED;
765 case TYPE_CDROM: return DRIVE_CDROM;
766 case TYPE_NETWORK: return DRIVE_REMOTE;
767 case TYPE_INVALID:
768 default: return DRIVE_CANNOTDETERMINE;
773 /***********************************************************************
774 * GetDriveType32W (KERNEL32.209)
776 UINT32 WINAPI GetDriveType32W( LPCWSTR root )
778 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
779 UINT32 ret = GetDriveType32A( xpath );
780 HeapFree( GetProcessHeap(), 0, xpath );
781 return ret;
785 /***********************************************************************
786 * GetCurrentDirectory16 (KERNEL.411)
788 UINT16 WINAPI GetCurrentDirectory16( UINT16 buflen, LPSTR buf )
790 return (UINT16)GetCurrentDirectory32A( buflen, buf );
794 /***********************************************************************
795 * GetCurrentDirectory32A (KERNEL32.196)
797 * Returns "X:\\path\\etc\\".
799 UINT32 WINAPI GetCurrentDirectory32A( UINT32 buflen, LPSTR buf )
801 char *pref = "A:\\";
802 const char *s = DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );
803 assert(s);
804 lstrcpyn32A( buf, pref, MIN( 4, buflen ) );
805 if (buflen) buf[0] += DRIVE_GetCurrentDrive();
806 if (buflen > 3) lstrcpyn32A( buf + 3, s, buflen - 3 );
807 return strlen(s) + 3; /* length of WHOLE current directory */
811 /***********************************************************************
812 * GetCurrentDirectory32W (KERNEL32.197)
814 UINT32 WINAPI GetCurrentDirectory32W( UINT32 buflen, LPWSTR buf )
816 LPSTR xpath = HeapAlloc( GetProcessHeap(), 0, buflen+1 );
817 UINT32 ret = GetCurrentDirectory32A( buflen, xpath );
818 lstrcpyAtoW( buf, xpath );
819 HeapFree( GetProcessHeap(), 0, xpath );
820 return ret;
824 /***********************************************************************
825 * SetCurrentDirectory (KERNEL.412)
827 BOOL16 WINAPI SetCurrentDirectory16( LPCSTR dir )
829 return SetCurrentDirectory32A( dir );
833 /***********************************************************************
834 * SetCurrentDirectory32A (KERNEL32.479)
836 BOOL32 WINAPI SetCurrentDirectory32A( LPCSTR dir )
838 int drive = DRIVE_GetCurrentDrive();
840 if (dir[0] && (dir[1]==':'))
842 drive = tolower( *dir ) - 'a';
843 if (!DRIVE_IsValid( drive ))
845 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
846 return FALSE;
848 dir += 2;
850 /* FIXME: what about empty strings? Add a \\ ? */
851 if (!DRIVE_Chdir( drive, dir )) return FALSE;
852 if (drive == DRIVE_GetCurrentDrive()) return TRUE;
853 return DRIVE_SetCurrentDrive( drive );
857 /***********************************************************************
858 * SetCurrentDirectory32W (KERNEL32.480)
860 BOOL32 WINAPI SetCurrentDirectory32W( LPCWSTR dirW )
862 LPSTR dir = HEAP_strdupWtoA( GetProcessHeap(), 0, dirW );
863 BOOL32 res = SetCurrentDirectory32A( dir );
864 HeapFree( GetProcessHeap(), 0, dir );
865 return res;
869 /***********************************************************************
870 * GetLogicalDriveStrings32A (KERNEL32.231)
872 UINT32 WINAPI GetLogicalDriveStrings32A( UINT32 len, LPSTR 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(char) <= len)
880 LPSTR p = buffer;
881 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
882 if (DRIVE_IsValid(drive))
884 *p++ = 'a' + drive;
885 *p++ = ':';
886 *p++ = '\\';
887 *p++ = '\0';
889 *p = '\0';
891 return count * 4 * sizeof(char);
895 /***********************************************************************
896 * GetLogicalDriveStrings32W (KERNEL32.232)
898 UINT32 WINAPI GetLogicalDriveStrings32W( UINT32 len, LPWSTR buffer )
900 int drive, count;
902 for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
903 if (DRIVE_IsValid(drive)) count++;
904 if (count * 4 * sizeof(WCHAR) <= len)
906 LPWSTR p = buffer;
907 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
908 if (DRIVE_IsValid(drive))
910 *p++ = (WCHAR)('a' + drive);
911 *p++ = (WCHAR)':';
912 *p++ = (WCHAR)'\\';
913 *p++ = (WCHAR)'\0';
915 *p = (WCHAR)'\0';
917 return count * 4 * sizeof(WCHAR);
921 /***********************************************************************
922 * GetLogicalDrives (KERNEL32.233)
924 DWORD WINAPI GetLogicalDrives(void)
926 DWORD ret = 0;
927 int drive;
929 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
930 if (DRIVE_IsValid(drive)) ret |= (1 << drive);
931 return ret;
935 /***********************************************************************
936 * GetVolumeInformation32A (KERNEL32.309)
938 BOOL32 WINAPI GetVolumeInformation32A( LPCSTR root, LPSTR label,
939 DWORD label_len, DWORD *serial,
940 DWORD *filename_len, DWORD *flags,
941 LPSTR fsname, DWORD fsname_len )
943 int drive;
945 /* FIXME, SetLastErrors missing */
947 if (!root) drive = DRIVE_GetCurrentDrive();
948 else
950 if ((root[1]) && (root[1] != ':'))
952 fprintf( stderr, "GetVolumeInformation: invalid root '%s'\n",root);
953 return FALSE;
955 drive = toupper(root[0]) - 'A';
957 if (!DRIVE_IsValid( drive )) return FALSE;
958 if (label) lstrcpyn32A( label, DOSDrives[drive].label, label_len );
959 if (serial) *serial = DOSDrives[drive].serial;
961 /* Set the filesystem information */
962 /* Note: we only emulate a FAT fs at the present */
964 if (filename_len) *filename_len = 12;
965 if (flags) *flags = 0;
966 if (fsname) {
967 /* Diablo checks that return code ... */
968 if (DRIVE_GetType(drive)==TYPE_CDROM)
969 lstrcpyn32A( fsname, "CDFS", fsname_len );
970 else
971 lstrcpyn32A( fsname, "FAT", fsname_len );
973 return TRUE;
977 /***********************************************************************
978 * GetVolumeInformation32W (KERNEL32.310)
980 BOOL32 WINAPI GetVolumeInformation32W( LPCWSTR root, LPWSTR label,
981 DWORD label_len, DWORD *serial,
982 DWORD *filename_len, DWORD *flags,
983 LPWSTR fsname, DWORD fsname_len )
985 LPSTR xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
986 LPSTR xvolname = label ? HeapAlloc(GetProcessHeap(),0,label_len) : NULL;
987 LPSTR xfsname = fsname ? HeapAlloc(GetProcessHeap(),0,fsname_len) : NULL;
988 BOOL32 ret = GetVolumeInformation32A( xroot, xvolname, label_len, serial,
989 filename_len, flags, xfsname,
990 fsname_len );
991 if (ret)
993 if (label) lstrcpyAtoW( label, xvolname );
994 if (fsname) lstrcpyAtoW( fsname, xfsname );
996 HeapFree( GetProcessHeap(), 0, xroot );
997 HeapFree( GetProcessHeap(), 0, xvolname );
998 HeapFree( GetProcessHeap(), 0, xfsname );
999 return ret;