Release 960623
[wine/multimedia.git] / files / drive.c
blobf886bdabae7701f4ab216d53b6d3cf29b039a987
1 /*
2 * DOS drives handling functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include <ctype.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
14 #if defined(__linux__) || defined(sun)
15 #include <sys/vfs.h>
16 #endif
17 #if defined(__NetBSD__) || defined(__FreeBSD__)
18 #include <sys/param.h>
19 #include <sys/mount.h>
20 #include <sys/errno.h>
21 #endif
22 #if defined(__svr4__) || defined(_SCO_DS)
23 #include <sys/statfs.h>
24 #endif
26 #include "windows.h"
27 #include "winbase.h"
28 #include "dos_fs.h"
29 #include "drive.h"
30 #include "file.h"
31 #include "msdos.h"
32 #include "options.h"
33 #include "task.h"
34 #include "xmalloc.h"
35 #include "string32.h"
36 #include "stddebug.h"
37 #include "debug.h"
39 typedef struct
41 char *root; /* root dir in Unix format without trailing / */
42 char *dos_cwd; /* cwd in DOS format without leading or trailing \ */
43 char *unix_cwd; /* cwd in Unix format without leading or trailing / */
44 char label[12]; /* drive label */
45 DWORD serial; /* drive serial number */
46 DRIVETYPE type; /* drive type */
47 BYTE disabled; /* disabled flag */
48 } DOSDRIVE;
51 static const char *DRIVE_Types[] =
53 "floppy", /* TYPE_FLOPPY */
54 "hd", /* TYPE_HD */
55 "cdrom", /* TYPE_CDROM */
56 "network" /* TYPE_NETWORK */
60 static DOSDRIVE DOSDrives[MAX_DOS_DRIVES];
61 static int DRIVE_CurDrive = -1;
63 static HTASK DRIVE_LastTask = 0;
66 /***********************************************************************
67 * DRIVE_GetDriveType
69 static DRIVETYPE DRIVE_GetDriveType( const char *name )
71 char buffer[20];
72 int i;
74 PROFILE_GetWineIniString( name, "Type", "hd", buffer, sizeof(buffer) );
75 for (i = 0; i < sizeof(DRIVE_Types)/sizeof(DRIVE_Types[0]); i++)
77 if (!lstrcmpi32A( buffer, DRIVE_Types[i] )) return (DRIVETYPE)i;
79 fprintf( stderr, "%s: unknown type '%s', defaulting to 'hd'.\n",
80 name, buffer );
81 return TYPE_HD;
85 /***********************************************************************
86 * DRIVE_Init
88 int DRIVE_Init(void)
90 int i, len, count = 0;
91 char name[] = "Drive A";
92 char path[MAX_PATHNAME_LEN];
93 char buffer[20];
94 char *p;
95 DOSDRIVE *drive;
97 for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, name[6]++, drive++)
99 PROFILE_GetWineIniString( name, "Path", "", path, sizeof(path)-1 );
100 if (path[0])
102 p = path + strlen(path) - 1;
103 while ((p > path) && ((*p == '/') || (*p == '\\'))) *p-- = '\0';
104 drive->root = xstrdup( path );
105 drive->dos_cwd = xstrdup( "" );
106 drive->unix_cwd = xstrdup( "" );
107 drive->type = DRIVE_GetDriveType( name );
108 drive->disabled = 0;
110 /* Get the drive label */
111 PROFILE_GetWineIniString( name, "Label", name, drive->label, 12 );
112 if ((len = strlen(drive->label)) < 11)
114 /* Pad label with spaces */
115 memset( drive->label + len, ' ', 11 - len );
116 drive->label[12] = '\0';
119 /* Get the serial number */
120 PROFILE_GetWineIniString( name, "Serial", "12345678",
121 buffer, sizeof(buffer) );
122 drive->serial = strtoul( buffer, NULL, 16 );
124 /* Make the first hard disk the current drive */
125 if ((DRIVE_CurDrive == -1) && (drive->type == TYPE_HD))
126 DRIVE_CurDrive = i;
128 count++;
129 dprintf_dosfs( stddeb, "%s: path=%s type=%s label='%s' serial=%08lx\n",
130 name, path, DRIVE_Types[drive->type],
131 drive->label, drive->serial );
133 else dprintf_dosfs( stddeb, "%s: not defined\n", name );
136 if (!count)
138 fprintf( stderr, "Warning: no valid DOS drive found, check your configuration file.\n" );
139 /* Create a C drive pointing to Unix root dir */
140 DOSDrives[2].root = xstrdup( "/" );
141 DOSDrives[2].dos_cwd = xstrdup( "" );
142 DOSDrives[2].unix_cwd = xstrdup( "" );
143 strcpy( DOSDrives[2].label, "Drive C " );
144 DOSDrives[2].serial = 0x12345678;
145 DOSDrives[2].type = TYPE_HD;
146 DOSDrives[2].disabled = 0;
147 DRIVE_CurDrive = 2;
150 /* Make sure the current drive is valid */
151 if (DRIVE_CurDrive == -1)
153 for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, drive++)
155 if (drive->root && !drive->disabled)
157 DRIVE_CurDrive = i;
158 break;
163 return 1;
167 /***********************************************************************
168 * DRIVE_IsValid
170 int DRIVE_IsValid( int drive )
172 if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
173 return (DOSDrives[drive].root && !DOSDrives[drive].disabled);
177 /***********************************************************************
178 * DRIVE_GetCurrentDrive
180 int DRIVE_GetCurrentDrive(void)
182 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
183 if (pTask && (pTask->curdrive & 0x80)) return pTask->curdrive & ~0x80;
184 return DRIVE_CurDrive;
188 /***********************************************************************
189 * DRIVE_SetCurrentDrive
191 int DRIVE_SetCurrentDrive( int drive )
193 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
194 if (!DRIVE_IsValid( drive ))
196 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
197 return 0;
199 dprintf_dosfs( stddeb, "DRIVE_SetCurrentDrive: %c:\n", 'A' + drive );
200 DRIVE_CurDrive = drive;
201 if (pTask) pTask->curdrive = drive | 0x80;
202 return 1;
206 /***********************************************************************
207 * DRIVE_FindDriveRoot
209 * Find a drive for which the root matches the begginning of the given path.
210 * This can be used to translate a Unix path into a drive + DOS path.
211 * Return value is the drive, or -1 on error. On success, path is modified
212 * to point to the beginning of the DOS path.
213 * FIXME: this only does a textual comparison of the path names, and won't
214 * work well in the presence of symbolic links.
216 int DRIVE_FindDriveRoot( const char **path )
218 int drive, rootdrive = -1;
219 const char *p1, *p2;
221 dprintf_dosfs( stddeb, "DRIVE_FindDriveRoot: searching '%s'\n", *path );
222 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
224 if (!DOSDrives[drive].root || DOSDrives[drive].disabled) continue;
225 p1 = *path;
226 p2 = DOSDrives[drive].root;
227 dprintf_dosfs( stddeb, "DRIVE_FindDriveRoot: checking %c: '%s'\n",
228 'A' + drive, p2 );
230 while (*p2 == '/') p2++;
231 if (!*p2)
233 rootdrive = drive;
234 continue; /* Look if there's a better match */
236 for (;;)
238 while ((*p1 == '\\') || (*p1 == '/')) p1++;
239 while (*p2 == '/') p2++;
240 while ((*p1 == *p2) && (*p2) && (*p2 != '/')) p1++, p2++;
241 if (!*p2)
243 if (IS_END_OF_NAME(*p1)) /* OK, found it */
245 *path = p1;
246 return drive;
249 else if (*p2 == '/')
251 if (IS_END_OF_NAME(*p1))
252 continue; /* Go to next path element */
254 break; /* No match, go to next drive */
257 return rootdrive;
261 /***********************************************************************
262 * DRIVE_GetRoot
264 const char * DRIVE_GetRoot( int drive )
266 if (!DRIVE_IsValid( drive )) return NULL;
267 return DOSDrives[drive].root;
271 /***********************************************************************
272 * DRIVE_GetDosCwd
274 const char * DRIVE_GetDosCwd( int drive )
276 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
277 if (!DRIVE_IsValid( drive )) return NULL;
279 /* Check if we need to change the directory to the new task. */
280 if (pTask && (pTask->curdrive & 0x80) && /* The task drive is valid */
281 ((pTask->curdrive & ~0x80) == drive) && /* and it's the one we want */
282 (DRIVE_LastTask != GetCurrentTask())) /* and the task changed */
284 /* Perform the task-switch */
285 if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
286 DRIVE_LastTask = GetCurrentTask();
288 return DOSDrives[drive].dos_cwd;
292 /***********************************************************************
293 * DRIVE_GetUnixCwd
295 const char * DRIVE_GetUnixCwd( int drive )
297 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
298 if (!DRIVE_IsValid( drive )) return NULL;
300 /* Check if we need to change the directory to the new task. */
301 if (pTask && (pTask->curdrive & 0x80) && /* The task drive is valid */
302 ((pTask->curdrive & ~0x80) == drive) && /* and it's the one we want */
303 (DRIVE_LastTask != GetCurrentTask())) /* and the task changed */
305 /* Perform the task-switch */
306 if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
307 DRIVE_LastTask = GetCurrentTask();
309 return DOSDrives[drive].unix_cwd;
313 /***********************************************************************
314 * DRIVE_GetLabel
316 const char * DRIVE_GetLabel( int drive )
318 if (!DRIVE_IsValid( drive )) return NULL;
319 return DOSDrives[drive].label;
323 /***********************************************************************
324 * DRIVE_GetSerialNumber
326 DWORD DRIVE_GetSerialNumber( int drive )
328 if (!DRIVE_IsValid( drive )) return 0;
329 return DOSDrives[drive].serial;
333 /***********************************************************************
334 * DRIVE_SetSerialNumber
336 int DRIVE_SetSerialNumber( int drive, DWORD serial )
338 if (!DRIVE_IsValid( drive )) return 0;
339 DOSDrives[drive].serial = serial;
340 return 1;
344 /***********************************************************************
345 * DRIVE_GetType
347 DRIVETYPE DRIVE_GetType( int drive )
349 if (!DRIVE_IsValid( drive )) return TYPE_INVALID;
350 return DOSDrives[drive].type;
354 /***********************************************************************
355 * DRIVE_Chdir
357 int DRIVE_Chdir( int drive, const char *path )
359 char buffer[MAX_PATHNAME_LEN];
360 const char *unix_cwd, *dos_cwd;
361 BYTE attr;
362 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
364 dprintf_dosfs( stddeb, "DRIVE_Chdir(%c:,%s)\n", 'A' + drive, path );
365 strcpy( buffer, "A:" );
366 buffer[0] += drive;
367 lstrcpyn32A( buffer + 2, path, sizeof(buffer) - 2 );
369 if (!(unix_cwd = DOSFS_GetUnixFileName( buffer, TRUE ))) return 0;
370 if (!FILE_Stat( unix_cwd, &attr, NULL, NULL, NULL )) return 0;
371 if (!(attr & FA_DIRECTORY))
373 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
374 return 0;
376 unix_cwd += strlen( DOSDrives[drive].root );
377 while (*unix_cwd == '/') unix_cwd++;
378 buffer[2] = '/';
379 lstrcpyn32A( buffer + 3, unix_cwd, sizeof(buffer) - 3 );
380 if (!(dos_cwd = DOSFS_GetDosTrueName( buffer, TRUE ))) return 0;
382 dprintf_dosfs( stddeb, "DRIVE_Chdir(%c:): unix_cwd=%s dos_cwd=%s\n",
383 'A' + drive, unix_cwd, dos_cwd + 3 );
385 free( DOSDrives[drive].dos_cwd );
386 free( DOSDrives[drive].unix_cwd );
387 DOSDrives[drive].dos_cwd = xstrdup( dos_cwd + 3 );
388 DOSDrives[drive].unix_cwd = xstrdup( unix_cwd );
390 if (pTask && (pTask->curdrive & 0x80) &&
391 ((pTask->curdrive & ~0x80) == drive))
393 lstrcpyn32A( pTask->curdir, dos_cwd + 2, sizeof(pTask->curdir) );
394 DRIVE_LastTask = GetCurrentTask();
396 return 1;
400 /***********************************************************************
401 * DRIVE_Disable
403 int DRIVE_Disable( int drive )
405 if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
407 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
408 return 0;
410 DOSDrives[drive].disabled = 1;
411 return 1;
415 /***********************************************************************
416 * DRIVE_Enable
418 int DRIVE_Enable( int drive )
420 if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
422 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
423 return 0;
425 DOSDrives[drive].disabled = 0;
426 return 1;
430 /***********************************************************************
431 * DRIVE_GetFreeSpace
433 static int DRIVE_GetFreeSpace( int drive, DWORD *size, DWORD *available )
435 struct statfs info;
437 if (!DRIVE_IsValid(drive))
439 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
440 return 0;
443 #if defined(__svr4__) || defined(_SCO_DS)
444 if (statfs( DOSDrives[drive].root, &info, 0, 0) < 0)
445 #else
446 if (statfs( DOSDrives[drive].root, &info) < 0)
447 #endif
449 FILE_SetDosError();
450 fprintf(stderr,"dosfs: cannot do statfs(%s)\n", DOSDrives[drive].root);
451 return 0;
454 *size = info.f_bsize * info.f_blocks;
455 #if defined(__svr4__) || defined(_SCO_DS)
456 *available = info.f_bfree * info.f_bsize;
457 #else
458 *available = info.f_bavail * info.f_bsize;
459 #endif
460 return 1;
464 /***********************************************************************
465 * GetDiskFreeSpace16 (KERNEL.422)
467 BOOL16 GetDiskFreeSpace16( LPCSTR root, LPDWORD cluster_sectors,
468 LPDWORD sector_bytes, LPDWORD free_clusters,
469 LPDWORD total_clusters )
471 return GetDiskFreeSpace32A( root, cluster_sectors, sector_bytes,
472 free_clusters, total_clusters );
476 /***********************************************************************
477 * GetDiskFreeSpaceA (KERNEL32.206)
479 BOOL32 GetDiskFreeSpace32A( LPCSTR root, LPDWORD cluster_sectors,
480 LPDWORD sector_bytes, LPDWORD free_clusters,
481 LPDWORD total_clusters )
483 int drive;
484 DWORD size,available;
486 if (!root) drive = DRIVE_GetCurrentDrive();
487 else
489 if ((root[1] != ':') || (root[2] != '\\'))
491 fprintf( stderr, "GetDiskFreeSpaceA: invalid root '%s'\n", root );
492 return FALSE;
494 drive = toupper(root[0]) - 'A';
496 if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
498 *sector_bytes = 512;
499 size /= 512;
500 available /= 512;
501 *cluster_sectors = 1;
502 while (*cluster_sectors * 65530 < size) *cluster_sectors *= 2;
503 *free_clusters = available/ *cluster_sectors;
504 *total_clusters = size/ *cluster_sectors;
505 return TRUE;
509 /***********************************************************************
510 * GetDiskFreeSpaceW (KERNEL32.207)
512 BOOL32 GetDiskFreeSpace32W( LPCWSTR root, LPDWORD cluster_sectors,
513 LPDWORD sector_bytes, LPDWORD free_clusters,
514 LPDWORD total_clusters )
516 LPSTR xroot;
517 BOOL ret;
519 xroot = STRING32_DupUniToAnsi(root);
520 ret = GetDiskFreeSpace32A( xroot,cluster_sectors, sector_bytes,
521 free_clusters, total_clusters );
522 free( xroot );
523 return ret;
527 /***********************************************************************
528 * GetDriveType (KERNEL.136)
530 WORD GetDriveType( INT drive )
532 dprintf_dosfs( stddeb, "GetDriveType(%c:)\n", 'A' + drive );
533 switch(DRIVE_GetType(drive))
535 case TYPE_FLOPPY: return DRIVE_REMOVABLE;
536 case TYPE_HD: return DRIVE_FIXED;
537 case TYPE_CDROM: return DRIVE_REMOVABLE;
538 case TYPE_NETWORK: return DRIVE_REMOTE;
539 case TYPE_INVALID:
540 default: return DRIVE_CANNOTDETERMINE;
545 /***********************************************************************
546 * GetDriveType32A (KERNEL32.)
548 WORD GetDriveType32A( LPCSTR root )
550 dprintf_dosfs( stddeb, "GetDriveType32A(%s)\n", root );
551 if ((root[1] != ':') || (root[2] != '\\'))
553 fprintf( stderr, "GetDriveType32A: invalid root '%s'\n", root );
554 return DRIVE_DOESNOTEXIST;
556 switch(DRIVE_GetType(toupper(root[0]) - 'A'))
558 case TYPE_FLOPPY: return DRIVE_REMOVABLE;
559 case TYPE_HD: return DRIVE_FIXED;
560 case TYPE_CDROM: return DRIVE_REMOVABLE;
561 case TYPE_NETWORK: return DRIVE_REMOTE;
562 case TYPE_INVALID:
563 default: return DRIVE_CANNOTDETERMINE;
568 /***********************************************************************
569 * GetCurrentDirectory (KERNEL.411) (KERNEL32.196)
571 UINT32 GetCurrentDirectory( UINT32 buflen, LPSTR buf )
573 const char *s = DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );
574 if (!s)
576 *buf = '\0';
577 return 0;
579 lstrcpyn32A( buf, s, buflen );
580 return strlen(s); /* yes */
584 /***********************************************************************
585 * SetCurrentDirectory (KERNEL.412)
587 BOOL32 SetCurrentDirectory( LPCSTR dir )
589 return DRIVE_Chdir( DRIVE_GetCurrentDrive(), dir );
593 /***********************************************************************
594 * GetLogicalDriveStrings32A (KERNEL32.231)
596 UINT32 GetLogicalDriveStrings32A( UINT32 len, LPSTR buffer )
598 int drive, count;
600 for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
601 if (DRIVE_IsValid(drive)) count++;
602 if (count * 4 * sizeof(char) <= len)
604 LPSTR p = buffer;
605 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
606 if (DRIVE_IsValid(drive))
608 *p++ = 'a' + drive;
609 *p++ = ':';
610 *p++ = '\\';
611 *p++ = '\0';
613 *p = '\0';
615 return count * 4 * sizeof(char);
619 /***********************************************************************
620 * GetLogicalDriveStrings32W (KERNEL32.232)
622 UINT32 GetLogicalDriveStrings32W( UINT32 len, LPWSTR buffer )
624 int drive, count;
626 for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
627 if (DRIVE_IsValid(drive)) count++;
628 if (count * 4 * sizeof(WCHAR) <= len)
630 LPWSTR p = buffer;
631 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
632 if (DRIVE_IsValid(drive))
634 *p++ = (WCHAR)('a' + drive);
635 *p++ = (WCHAR)':';
636 *p++ = (WCHAR)'\\';
637 *p++ = (WCHAR)'\0';
639 *p = (WCHAR)'\0';
641 return count * 4 * sizeof(WCHAR);
645 /***********************************************************************
646 * GetLogicalDrives (KERNEL32.233)
648 DWORD GetLogicalDrives(void)
650 DWORD ret = 0;
651 int drive;
653 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
654 if (DRIVE_IsValid(drive)) ret |= (1 << drive);
655 return ret;
659 /***********************************************************************
660 * GetVolumeInformation32A (KERNEL32.309)
662 BOOL32 GetVolumeInformation32A( LPCSTR root, LPSTR label, DWORD label_len,
663 DWORD *serial, DWORD *filename_len,
664 DWORD *flags, LPSTR fsname, DWORD fsname_len )
666 int drive;
668 /* FIXME, SetLastErrors missing */
670 if (!root) drive = DRIVE_GetCurrentDrive();
671 else
673 if ((root[1] != ':') || (root[2] != '\\'))
675 fprintf( stderr, "GetVolumeInformation: invalid root '%s'\n",root);
676 return FALSE;
678 drive = toupper(root[0]) - 'A';
680 if (!DRIVE_IsValid( drive )) return FALSE;
681 if (label) lstrcpyn32A( label, DOSDrives[drive].label, label_len );
682 if (serial) *serial = DOSDrives[drive].serial;
684 /* Set the filesystem information */
685 /* Note: we only emulate a FAT fs at the present */
687 if (filename_len) *filename_len = 12;
688 if (flags) *flags = 0;
689 if (fsname) lstrcpyn32A( fsname, "FAT", fsname_len );
690 return TRUE;
694 /***********************************************************************
695 * GetVolumeInformation32W (KERNEL32.310)
697 BOOL32 GetVolumeInformation32W( LPCWSTR root, LPWSTR label, DWORD label_len,
698 DWORD *serial, DWORD *filename_len,
699 DWORD *flags, LPWSTR fsname, DWORD fsname_len)
701 LPSTR xroot = STRING32_DupUniToAnsi(root);
702 LPSTR xvolname = (char*)xmalloc( label_len );
703 LPSTR xfsname = (char*)xmalloc( fsname_len );
704 BOOL32 ret = GetVolumeInformation32A( xroot, xvolname, label_len, serial,
705 filename_len, flags, xfsname,
706 fsname_len );
707 if (ret)
709 STRING32_AnsiToUni( label, xvolname );
710 STRING32_AnsiToUni( fsname, xfsname );
712 free(xroot);
713 free(xvolname);
714 free(xfsname);
715 return ret;