2 * DOS drives handling functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
11 #include <sys/types.h>
14 #if defined(__linux__) || defined(sun) || defined(hpux)
17 #if defined(__NetBSD__) || defined(__FreeBSD__)
18 #include <sys/param.h>
19 #include <sys/mount.h>
20 #include <sys/errno.h>
22 #if defined(__svr4__) || defined(_SCO_DS)
23 #include <sys/statfs.h>
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 UINT32 flags
; /* drive flags */
51 static const char * const DRIVE_Types
[] =
53 "floppy", /* TYPE_FLOPPY */
55 "cdrom", /* TYPE_CDROM */
56 "network" /* TYPE_NETWORK */
60 /* Known filesystem types */
68 static const FS_DESCR DRIVE_Filesystems
[] =
70 { "unix", DRIVE_CASE_SENSITIVE
| DRIVE_CASE_PRESERVING
},
71 { "msdos", DRIVE_SHORT_NAMES
},
72 { "dos", DRIVE_SHORT_NAMES
},
73 { "fat", DRIVE_SHORT_NAMES
},
74 { "vfat", DRIVE_CASE_PRESERVING
},
75 { "win95", DRIVE_CASE_PRESERVING
},
80 static DOSDRIVE DOSDrives
[MAX_DOS_DRIVES
];
81 static int DRIVE_CurDrive
= -1;
83 static HTASK16 DRIVE_LastTask
= 0;
86 /***********************************************************************
89 static DRIVETYPE
DRIVE_GetDriveType( const char *name
)
94 PROFILE_GetWineIniString( name
, "Type", "hd", buffer
, sizeof(buffer
) );
95 for (i
= 0; i
< sizeof(DRIVE_Types
)/sizeof(DRIVE_Types
[0]); i
++)
97 if (!lstrcmpi32A( buffer
, DRIVE_Types
[i
] )) return (DRIVETYPE
)i
;
99 fprintf( stderr
, "%s: unknown type '%s', defaulting to 'hd'.\n",
105 /***********************************************************************
108 static UINT32
DRIVE_GetFSFlags( const char *name
, const char *value
)
110 const FS_DESCR
*descr
;
112 for (descr
= DRIVE_Filesystems
; descr
->name
; descr
++)
113 if (!lstrcmpi32A( value
, descr
->name
)) return descr
->flags
;
114 fprintf( stderr
, "%s: unknown filesystem type '%s', defaulting to 'unix'.\n",
116 return DRIVE_CASE_SENSITIVE
| DRIVE_CASE_PRESERVING
;
120 /***********************************************************************
125 int i
, len
, count
= 0;
126 char name
[] = "Drive A";
127 char path
[MAX_PATHNAME_LEN
];
132 for (i
= 0, drive
= DOSDrives
; i
< MAX_DOS_DRIVES
; i
++, name
[6]++, drive
++)
134 PROFILE_GetWineIniString( name
, "Path", "", path
, sizeof(path
)-1 );
137 p
= path
+ strlen(path
) - 1;
138 while ((p
> path
) && ((*p
== '/') || (*p
== '\\'))) *p
-- = '\0';
139 drive
->root
= xstrdup( path
);
140 drive
->dos_cwd
= xstrdup( "" );
141 drive
->unix_cwd
= xstrdup( "" );
142 drive
->type
= DRIVE_GetDriveType( name
);
145 /* Get the drive label */
146 PROFILE_GetWineIniString( name
, "Label", name
, drive
->label
, 12 );
147 if ((len
= strlen(drive
->label
)) < 11)
149 /* Pad label with spaces */
150 memset( drive
->label
+ len
, ' ', 11 - len
);
151 drive
->label
[12] = '\0';
154 /* Get the serial number */
155 PROFILE_GetWineIniString( name
, "Serial", "12345678",
156 buffer
, sizeof(buffer
) );
157 drive
->serial
= strtoul( buffer
, NULL
, 16 );
159 /* Get the filesystem type */
160 PROFILE_GetWineIniString( name
, "Filesystem", "unix",
161 buffer
, sizeof(buffer
) );
162 drive
->flags
= DRIVE_GetFSFlags( name
, buffer
);
164 /* Make the first hard disk the current drive */
165 if ((DRIVE_CurDrive
== -1) && (drive
->type
== TYPE_HD
))
169 dprintf_dosfs( stddeb
, "%s: path=%s type=%s label='%s' serial=%08lx flags=%08x\n",
170 name
, path
, DRIVE_Types
[drive
->type
],
171 drive
->label
, drive
->serial
, drive
->flags
);
173 else dprintf_dosfs( stddeb
, "%s: not defined\n", name
);
178 fprintf( stderr
, "Warning: no valid DOS drive found, check your configuration file.\n" );
179 /* Create a C drive pointing to Unix root dir */
180 DOSDrives
[2].root
= xstrdup( "/" );
181 DOSDrives
[2].dos_cwd
= xstrdup( "" );
182 DOSDrives
[2].unix_cwd
= xstrdup( "" );
183 strcpy( DOSDrives
[2].label
, "Drive C " );
184 DOSDrives
[2].serial
= 0x12345678;
185 DOSDrives
[2].type
= TYPE_HD
;
186 DOSDrives
[2].flags
= 0;
190 /* Make sure the current drive is valid */
191 if (DRIVE_CurDrive
== -1)
193 for (i
= 0, drive
= DOSDrives
; i
< MAX_DOS_DRIVES
; i
++, drive
++)
195 if (drive
->root
&& !(drive
->flags
& DRIVE_DISABLED
))
207 /***********************************************************************
210 int DRIVE_IsValid( int drive
)
212 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
)) return 0;
213 return (DOSDrives
[drive
].root
&&
214 !(DOSDrives
[drive
].flags
& DRIVE_DISABLED
));
218 /***********************************************************************
219 * DRIVE_GetCurrentDrive
221 int DRIVE_GetCurrentDrive(void)
223 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
224 if (pTask
&& (pTask
->curdrive
& 0x80)) return pTask
->curdrive
& ~0x80;
225 return DRIVE_CurDrive
;
229 /***********************************************************************
230 * DRIVE_SetCurrentDrive
232 int DRIVE_SetCurrentDrive( int drive
)
234 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
235 if (!DRIVE_IsValid( drive
))
237 DOS_ERROR( ER_InvalidDrive
, EC_MediaError
, SA_Abort
, EL_Disk
);
240 dprintf_dosfs( stddeb
, "DRIVE_SetCurrentDrive: %c:\n", 'A' + drive
);
241 DRIVE_CurDrive
= drive
;
242 if (pTask
) pTask
->curdrive
= drive
| 0x80;
247 /***********************************************************************
248 * DRIVE_FindDriveRoot
250 * Find a drive for which the root matches the begginning of the given path.
251 * This can be used to translate a Unix path into a drive + DOS path.
252 * Return value is the drive, or -1 on error. On success, path is modified
253 * to point to the beginning of the DOS path.
254 * FIXME: this only does a textual comparison of the path names, and won't
255 * work well in the presence of symbolic links.
257 int DRIVE_FindDriveRoot( const char **path
)
259 int drive
, rootdrive
= -1;
262 dprintf_dosfs( stddeb
, "DRIVE_FindDriveRoot: searching '%s'\n", *path
);
263 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
265 if (!DOSDrives
[drive
].root
||
266 (DOSDrives
[drive
].flags
& DRIVE_DISABLED
)) continue;
268 p2
= DOSDrives
[drive
].root
;
269 dprintf_dosfs( stddeb
, "DRIVE_FindDriveRoot: checking %c: '%s'\n",
272 while (*p2
== '/') p2
++;
276 continue; /* Look if there's a better match */
280 while ((*p1
== '\\') || (*p1
== '/')) p1
++;
281 while (*p2
== '/') p2
++;
282 while ((*p1
== *p2
) && (*p2
) && (*p2
!= '/')) p1
++, p2
++;
285 if (IS_END_OF_NAME(*p1
)) /* OK, found it */
293 if (IS_END_OF_NAME(*p1
))
294 continue; /* Go to next path element */
296 break; /* No match, go to next drive */
303 /***********************************************************************
306 const char * DRIVE_GetRoot( int drive
)
308 if (!DRIVE_IsValid( drive
)) return NULL
;
309 return DOSDrives
[drive
].root
;
313 /***********************************************************************
316 const char * DRIVE_GetDosCwd( int drive
)
318 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
319 if (!DRIVE_IsValid( drive
)) return NULL
;
321 /* Check if we need to change the directory to the new task. */
322 if (pTask
&& (pTask
->curdrive
& 0x80) && /* The task drive is valid */
323 ((pTask
->curdrive
& ~0x80) == drive
) && /* and it's the one we want */
324 (DRIVE_LastTask
!= GetCurrentTask())) /* and the task changed */
326 /* Perform the task-switch */
327 if (!DRIVE_Chdir( drive
, pTask
->curdir
)) DRIVE_Chdir( drive
, "\\" );
328 DRIVE_LastTask
= GetCurrentTask();
330 return DOSDrives
[drive
].dos_cwd
;
334 /***********************************************************************
337 const char * DRIVE_GetUnixCwd( int drive
)
339 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
340 if (!DRIVE_IsValid( drive
)) return NULL
;
342 /* Check if we need to change the directory to the new task. */
343 if (pTask
&& (pTask
->curdrive
& 0x80) && /* The task drive is valid */
344 ((pTask
->curdrive
& ~0x80) == drive
) && /* and it's the one we want */
345 (DRIVE_LastTask
!= GetCurrentTask())) /* and the task changed */
347 /* Perform the task-switch */
348 if (!DRIVE_Chdir( drive
, pTask
->curdir
)) DRIVE_Chdir( drive
, "\\" );
349 DRIVE_LastTask
= GetCurrentTask();
351 return DOSDrives
[drive
].unix_cwd
;
355 /***********************************************************************
358 const char * DRIVE_GetLabel( int drive
)
360 if (!DRIVE_IsValid( drive
)) return NULL
;
361 return DOSDrives
[drive
].label
;
365 /***********************************************************************
366 * DRIVE_GetSerialNumber
368 DWORD
DRIVE_GetSerialNumber( int drive
)
370 if (!DRIVE_IsValid( drive
)) return 0;
371 return DOSDrives
[drive
].serial
;
375 /***********************************************************************
376 * DRIVE_SetSerialNumber
378 int DRIVE_SetSerialNumber( int drive
, DWORD serial
)
380 if (!DRIVE_IsValid( drive
)) return 0;
381 DOSDrives
[drive
].serial
= serial
;
386 /***********************************************************************
389 DRIVETYPE
DRIVE_GetType( int drive
)
391 if (!DRIVE_IsValid( drive
)) return TYPE_INVALID
;
392 return DOSDrives
[drive
].type
;
396 /***********************************************************************
399 UINT32
DRIVE_GetFlags( int drive
)
401 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
)) return 0;
402 return DOSDrives
[drive
].flags
;
406 /***********************************************************************
409 int DRIVE_Chdir( int drive
, const char *path
)
411 char buffer
[MAX_PATHNAME_LEN
];
412 const char *unix_cwd
, *dos_cwd
;
414 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
416 dprintf_dosfs( stddeb
, "DRIVE_Chdir(%c:,%s)\n", 'A' + drive
, path
);
417 strcpy( buffer
, "A:" );
419 lstrcpyn32A( buffer
+ 2, path
, sizeof(buffer
) - 2 );
421 if (!(unix_cwd
= DOSFS_GetUnixFileName( buffer
, TRUE
))) return 0;
422 if (!FILE_Stat( unix_cwd
, &attr
, NULL
, NULL
, NULL
)) return 0;
423 if (!(attr
& FA_DIRECTORY
))
425 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
428 unix_cwd
+= strlen( DOSDrives
[drive
].root
);
429 while (*unix_cwd
== '/') unix_cwd
++;
431 lstrcpyn32A( buffer
+ 3, unix_cwd
, sizeof(buffer
) - 3 );
432 if (!(dos_cwd
= DOSFS_GetDosTrueName( buffer
, TRUE
))) return 0;
434 dprintf_dosfs( stddeb
, "DRIVE_Chdir(%c:): unix_cwd=%s dos_cwd=%s\n",
435 'A' + drive
, unix_cwd
, dos_cwd
+ 3 );
437 free( DOSDrives
[drive
].dos_cwd
);
438 free( DOSDrives
[drive
].unix_cwd
);
439 DOSDrives
[drive
].dos_cwd
= xstrdup( dos_cwd
+ 3 );
440 DOSDrives
[drive
].unix_cwd
= xstrdup( unix_cwd
);
442 if (pTask
&& (pTask
->curdrive
& 0x80) &&
443 ((pTask
->curdrive
& ~0x80) == drive
))
445 lstrcpyn32A( pTask
->curdir
, dos_cwd
+ 2, sizeof(pTask
->curdir
) );
446 DRIVE_LastTask
= GetCurrentTask();
452 /***********************************************************************
455 int DRIVE_Disable( int drive
)
457 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
) || !DOSDrives
[drive
].root
)
459 DOS_ERROR( ER_InvalidDrive
, EC_MediaError
, SA_Abort
, EL_Disk
);
462 DOSDrives
[drive
].flags
|= DRIVE_DISABLED
;
467 /***********************************************************************
470 int DRIVE_Enable( int drive
)
472 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
) || !DOSDrives
[drive
].root
)
474 DOS_ERROR( ER_InvalidDrive
, EC_MediaError
, SA_Abort
, EL_Disk
);
477 DOSDrives
[drive
].flags
&= ~DRIVE_DISABLED
;
482 /***********************************************************************
485 static int DRIVE_GetFreeSpace( int drive
, DWORD
*size
, DWORD
*available
)
489 if (!DRIVE_IsValid(drive
))
491 DOS_ERROR( ER_InvalidDrive
, EC_MediaError
, SA_Abort
, EL_Disk
);
495 #if defined(__svr4__) || defined(_SCO_DS)
496 if (statfs( DOSDrives
[drive
].root
, &info
, 0, 0) < 0)
498 if (statfs( DOSDrives
[drive
].root
, &info
) < 0)
502 fprintf(stderr
,"dosfs: cannot do statfs(%s)\n", DOSDrives
[drive
].root
);
506 *size
= info
.f_bsize
* info
.f_blocks
;
507 #if defined(__svr4__) || defined(_SCO_DS)
508 *available
= info
.f_bfree
* info
.f_bsize
;
510 *available
= info
.f_bavail
* info
.f_bsize
;
516 /***********************************************************************
517 * GetDiskFreeSpace16 (KERNEL.422)
519 BOOL16
GetDiskFreeSpace16( LPCSTR root
, LPDWORD cluster_sectors
,
520 LPDWORD sector_bytes
, LPDWORD free_clusters
,
521 LPDWORD total_clusters
)
523 return GetDiskFreeSpace32A( root
, cluster_sectors
, sector_bytes
,
524 free_clusters
, total_clusters
);
528 /***********************************************************************
529 * GetDiskFreeSpaceA (KERNEL32.206)
531 BOOL32
GetDiskFreeSpace32A( LPCSTR root
, LPDWORD cluster_sectors
,
532 LPDWORD sector_bytes
, LPDWORD free_clusters
,
533 LPDWORD total_clusters
)
536 DWORD size
,available
;
538 if (!root
) drive
= DRIVE_GetCurrentDrive();
541 if ((root
[1] != ':') || (root
[2] != '\\'))
543 fprintf( stderr
, "GetDiskFreeSpaceA: invalid root '%s'\n", root
);
546 drive
= toupper(root
[0]) - 'A';
548 if (!DRIVE_GetFreeSpace(drive
, &size
, &available
)) return FALSE
;
553 *cluster_sectors
= 1;
554 while (*cluster_sectors
* 65530 < size
) *cluster_sectors
*= 2;
555 *free_clusters
= available
/ *cluster_sectors
;
556 *total_clusters
= size
/ *cluster_sectors
;
561 /***********************************************************************
562 * GetDiskFreeSpaceW (KERNEL32.207)
564 BOOL32
GetDiskFreeSpace32W( LPCWSTR root
, LPDWORD cluster_sectors
,
565 LPDWORD sector_bytes
, LPDWORD free_clusters
,
566 LPDWORD total_clusters
)
571 xroot
= STRING32_DupUniToAnsi(root
);
572 ret
= GetDiskFreeSpace32A( xroot
,cluster_sectors
, sector_bytes
,
573 free_clusters
, total_clusters
);
579 /***********************************************************************
580 * GetDriveType16 (KERNEL.136)
582 UINT16
GetDriveType16( UINT16 drive
)
584 dprintf_dosfs( stddeb
, "GetDriveType(%c:)\n", 'A' + drive
);
585 switch(DRIVE_GetType(drive
))
587 case TYPE_FLOPPY
: return DRIVE_REMOVABLE
;
588 case TYPE_HD
: return DRIVE_FIXED
;
589 case TYPE_CDROM
: return DRIVE_REMOTE
;
590 case TYPE_NETWORK
: return DRIVE_REMOTE
;
592 default: return DRIVE_CANNOTDETERMINE
;
597 /***********************************************************************
598 * GetDriveType32A (KERNEL32.208)
600 UINT32
GetDriveType32A( LPCSTR root
)
602 dprintf_dosfs( stddeb
, "GetDriveType32A(%s)\n", root
);
603 if ((root
[1] != ':') || (root
[2] != '\\'))
605 fprintf( stderr
, "GetDriveType32A: invalid root '%s'\n", root
);
606 return DRIVE_DOESNOTEXIST
;
608 switch(DRIVE_GetType(toupper(root
[0]) - 'A'))
610 case TYPE_FLOPPY
: return DRIVE_REMOVABLE
;
611 case TYPE_HD
: return DRIVE_FIXED
;
612 case TYPE_CDROM
: return DRIVE_CDROM
;
613 case TYPE_NETWORK
: return DRIVE_REMOTE
;
615 default: return DRIVE_CANNOTDETERMINE
;
620 /***********************************************************************
621 * GetDriveType32W (KERNEL32.209)
623 UINT32
GetDriveType32W( LPCWSTR root
)
625 LPSTR xpath
=STRING32_DupUniToAnsi(root
);
628 ret
= GetDriveType32A(xpath
);
634 /***********************************************************************
635 * GetCurrentDirectory16 (KERNEL.411)
637 UINT16
GetCurrentDirectory16( UINT16 buflen
, LPSTR buf
)
639 return (UINT16
)GetCurrentDirectory32A( buflen
, buf
);
643 /***********************************************************************
644 * GetCurrentDirectory32A (KERNEL32.196)
646 * Returns "X:\\path\\etc\\".
648 UINT32
GetCurrentDirectory32A( UINT32 buflen
, LPSTR buf
)
651 const char *s
= DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );
657 lstrcpyn32A( buf
, pref
, 3 );
658 if (buflen
) buf
[0] += DRIVE_GetCurrentDrive();
659 if (buflen
>= 3) lstrcpyn32A( buf
+ 3, s
, buflen
- 3 );
660 return strlen(s
) + 3; /* length of WHOLE current directory */
664 /***********************************************************************
665 * GetCurrentDirectory32W (KERNEL32.197)
667 UINT32
GetCurrentDirectory32W( UINT32 buflen
, LPWSTR buf
)
669 LPSTR xpath
=(char*)xmalloc(buflen
+1);
672 ret
= GetCurrentDirectory32A(buflen
,xpath
);
673 STRING32_AnsiToUni(buf
,xpath
);
679 /***********************************************************************
680 * SetCurrentDirectory (KERNEL.412)
682 BOOL32
SetCurrentDirectory( LPCSTR dir
)
684 return DRIVE_Chdir( DRIVE_GetCurrentDrive(), dir
);
688 /***********************************************************************
689 * GetLogicalDriveStrings32A (KERNEL32.231)
691 UINT32
GetLogicalDriveStrings32A( UINT32 len
, LPSTR buffer
)
695 for (drive
= count
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
696 if (DRIVE_IsValid(drive
)) count
++;
697 if (count
* 4 * sizeof(char) <= len
)
700 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
701 if (DRIVE_IsValid(drive
))
710 return count
* 4 * sizeof(char);
714 /***********************************************************************
715 * GetLogicalDriveStrings32W (KERNEL32.232)
717 UINT32
GetLogicalDriveStrings32W( UINT32 len
, LPWSTR buffer
)
721 for (drive
= count
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
722 if (DRIVE_IsValid(drive
)) count
++;
723 if (count
* 4 * sizeof(WCHAR
) <= len
)
726 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
727 if (DRIVE_IsValid(drive
))
729 *p
++ = (WCHAR
)('a' + drive
);
736 return count
* 4 * sizeof(WCHAR
);
740 /***********************************************************************
741 * GetLogicalDrives (KERNEL32.233)
743 DWORD
GetLogicalDrives(void)
748 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
749 if (DRIVE_IsValid(drive
)) ret
|= (1 << drive
);
754 /***********************************************************************
755 * GetVolumeInformation32A (KERNEL32.309)
757 BOOL32
GetVolumeInformation32A( LPCSTR root
, LPSTR label
, DWORD label_len
,
758 DWORD
*serial
, DWORD
*filename_len
,
759 DWORD
*flags
, LPSTR fsname
, DWORD fsname_len
)
763 /* FIXME, SetLastErrors missing */
765 if (!root
) drive
= DRIVE_GetCurrentDrive();
768 if ((root
[1] != ':') || (root
[2] != '\\'))
770 fprintf( stderr
, "GetVolumeInformation: invalid root '%s'\n",root
);
773 drive
= toupper(root
[0]) - 'A';
775 if (!DRIVE_IsValid( drive
)) return FALSE
;
776 if (label
) lstrcpyn32A( label
, DOSDrives
[drive
].label
, label_len
);
777 if (serial
) *serial
= DOSDrives
[drive
].serial
;
779 /* Set the filesystem information */
780 /* Note: we only emulate a FAT fs at the present */
782 if (filename_len
) *filename_len
= 12;
783 if (flags
) *flags
= 0;
784 if (fsname
) lstrcpyn32A( fsname
, "FAT", fsname_len
);
789 /***********************************************************************
790 * GetVolumeInformation32W (KERNEL32.310)
792 BOOL32
GetVolumeInformation32W( LPCWSTR root
, LPWSTR label
, DWORD label_len
,
793 DWORD
*serial
, DWORD
*filename_len
,
794 DWORD
*flags
, LPWSTR fsname
, DWORD fsname_len
)
796 LPSTR xroot
= STRING32_DupUniToAnsi(root
);
797 LPSTR xvolname
= (char*)xmalloc( label_len
);
798 LPSTR xfsname
= (char*)xmalloc( fsname_len
);
799 BOOL32 ret
= GetVolumeInformation32A( xroot
, xvolname
, label_len
, serial
,
800 filename_len
, flags
, xfsname
,
804 STRING32_AnsiToUni( label
, xvolname
);
805 STRING32_AnsiToUni( fsname
, xfsname
);