2 * DOS drives handling functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
12 #include <sys/types.h>
15 #if defined(__linux__) || defined(sun) || defined(hpux)
18 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
19 #include <sys/param.h>
20 #include <sys/mount.h>
21 #include <sys/errno.h>
23 #if defined(__svr4__) || defined(_SCO_DS) || defined(__EMX__)
24 #include <sys/statfs.h>
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 */
52 static const char * const DRIVE_Types
[] =
54 "floppy", /* TYPE_FLOPPY */
56 "cdrom", /* TYPE_CDROM */
57 "network" /* TYPE_NETWORK */
61 /* Known filesystem types */
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
},
81 static DOSDRIVE DOSDrives
[MAX_DOS_DRIVES
];
82 static int DRIVE_CurDrive
= -1;
84 static HTASK16 DRIVE_LastTask
= 0;
87 /***********************************************************************
90 static DRIVETYPE
DRIVE_GetDriveType( const char *name
)
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",
106 /***********************************************************************
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",
117 return DRIVE_CASE_SENSITIVE
| DRIVE_CASE_PRESERVING
;
121 /***********************************************************************
126 int i
, len
, count
= 0;
127 char name
[] = "Drive A";
128 char path
[MAX_PATHNAME_LEN
];
130 struct stat drive_stat_buffer
;
134 for (i
= 0, drive
= DOSDrives
; i
< MAX_DOS_DRIVES
; i
++, name
[6]++, drive
++)
136 PROFILE_GetWineIniString( name
, "Path", "", path
, sizeof(path
)-1 );
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",
149 if (!S_ISDIR(drive_stat_buffer
.st_mode
))
151 fprintf( stderr
, "%s is not a directory, ignoring drive %c:\n",
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
);
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
))
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
);
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;
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
))
227 /***********************************************************************
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
);
260 dprintf_dosfs( stddeb
, "DRIVE_SetCurrentDrive: %c:\n", 'A' + drive
);
261 DRIVE_CurDrive
= drive
;
262 if (pTask
) pTask
->curdrive
= drive
| 0x80;
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
];
286 const char *p
= *path
;
289 strcpy( buffer
, "/" );
292 if (stat( buffer
, &st
) || !S_ISDIR( st
.st_mode
)) break;
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
))
309 /* Get the next path component */
312 while ((*p
== '/') || (*p
== '\\')) p
++;
314 while (!IS_END_OF_NAME(*p
)) *next
++ = *p
++;
320 dprintf_dosfs( stddeb
, "DRIVE_FindDriveRoot: %s -> drive %c:, root='%s', name='%s'\n",
321 buffer
, 'A' + rootdrive
,
322 DOSDrives
[rootdrive
].root
, *path
);
327 /***********************************************************************
330 const char * DRIVE_GetRoot( int drive
)
332 if (!DRIVE_IsValid( drive
)) return NULL
;
333 return DOSDrives
[drive
].root
;
337 /***********************************************************************
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 /***********************************************************************
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 /***********************************************************************
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
;
410 /***********************************************************************
413 DRIVETYPE
DRIVE_GetType( int drive
)
415 if (!DRIVE_IsValid( drive
)) return TYPE_INVALID
;
416 return DOSDrives
[drive
].type
;
420 /***********************************************************************
423 UINT32
DRIVE_GetFlags( int drive
)
425 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
)) return 0;
426 return DOSDrives
[drive
].flags
;
430 /***********************************************************************
433 int DRIVE_Chdir( int drive
, const char *path
)
435 DOS_FULL_NAME full_name
;
436 char buffer
[MAX_PATHNAME_LEN
];
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:" );
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
);
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();
476 /***********************************************************************
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
);
486 DOSDrives
[drive
].flags
|= DRIVE_DISABLED
;
491 /***********************************************************************
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
);
501 DOSDrives
[drive
].flags
&= ~DRIVE_DISABLED
;
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] */
516 old
= DOSDrives
+ existing_drive
;
517 new = DOSDrives
+ new_drive
;
519 if ((existing_drive
< 0) || (existing_drive
>= MAX_DOS_DRIVES
) ||
521 (new_drive
< 0) || (new_drive
>= MAX_DOS_DRIVES
))
523 DOS_ERROR( ER_InvalidDrive
, EC_MediaError
, SA_Abort
, EL_Disk
);
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
,
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
;
546 dprintf_dosfs ( stddeb
, "Drive %c is now equal to drive %c\n",
547 'A' + new_drive
, 'A' + existing_drive
);
554 /***********************************************************************
557 static int DRIVE_GetFreeSpace( int drive
, DWORD
*size
, DWORD
*available
)
561 if (!DRIVE_IsValid(drive
))
563 DOS_ERROR( ER_InvalidDrive
, EC_MediaError
, SA_Abort
, EL_Disk
);
567 #if defined(__svr4__) || defined(_SCO_DS)
568 if (statfs( DOSDrives
[drive
].root
, &info
, 0, 0) < 0)
570 if (statfs( DOSDrives
[drive
].root
, &info
) < 0)
574 fprintf(stderr
,"dosfs: cannot do statfs(%s)\n", DOSDrives
[drive
].root
);
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
;
582 *available
= info
.f_bavail
* info
.f_bsize
;
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
)
608 DWORD size
,available
;
610 if (!root
) drive
= DRIVE_GetCurrentDrive();
613 if ((root
[1]) && ((root
[1] != ':') || (root
[2] != '\\')))
615 fprintf( stderr
, "GetDiskFreeSpaceA: invalid root '%s'\n", root
);
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;
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
;
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
)
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
);
655 /***********************************************************************
656 * GetDiskFreeSpaceEx32A (KERNEL32.871)
658 BOOL32 WINAPI
GetDiskFreeSpaceEx32A( LPCSTR root
,
659 LPULARGE_INTEGER avail
,
660 LPULARGE_INTEGER total
,
661 LPULARGE_INTEGER totalfree
)
664 DWORD size
,available
;
666 if (!root
) drive
= DRIVE_GetCurrentDrive();
669 if ((root
[1]) && ((root
[1] != ':') || (root
[2] != '\\')))
671 fprintf( stderr
, "GetDiskFreeSpaceExA: invalid root '%s'\n",
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
;
684 totalfree
->HighPart
=0;
685 totalfree
->LowPart
= available
;
690 /***********************************************************************
691 * GetDiskFreeSpaceEx32W (KERNEL32.873)
693 BOOL32 WINAPI
GetDiskFreeSpaceEx32W( LPCWSTR root
, LPULARGE_INTEGER avail
,
694 LPULARGE_INTEGER total
,
695 LPULARGE_INTEGER totalfree
)
700 xroot
= HEAP_strdupWtoA( GetProcessHeap(), 0, root
);
701 ret
= GetDiskFreeSpaceEx32A( xroot
, avail
, total
, totalfree
);
702 HeapFree( GetProcessHeap(), 0, xroot
);
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
;
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
;
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
);
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
)
776 const char *s
= DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );
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
);
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
);
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
);
843 /***********************************************************************
844 * GetLogicalDriveStrings32A (KERNEL32.231)
846 UINT32 WINAPI
GetLogicalDriveStrings32A( UINT32 len
, LPSTR buffer
)
850 for (drive
= count
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
851 if (DRIVE_IsValid(drive
)) count
++;
852 if (count
* 4 * sizeof(char) <= len
)
855 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
856 if (DRIVE_IsValid(drive
))
865 return count
* 4 * sizeof(char);
869 /***********************************************************************
870 * GetLogicalDriveStrings32W (KERNEL32.232)
872 UINT32 WINAPI
GetLogicalDriveStrings32W( UINT32 len
, LPWSTR buffer
)
876 for (drive
= count
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
877 if (DRIVE_IsValid(drive
)) count
++;
878 if (count
* 4 * sizeof(WCHAR
) <= len
)
881 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
882 if (DRIVE_IsValid(drive
))
884 *p
++ = (WCHAR
)('a' + drive
);
891 return count
* 4 * sizeof(WCHAR
);
895 /***********************************************************************
896 * GetLogicalDrives (KERNEL32.233)
898 DWORD WINAPI
GetLogicalDrives(void)
903 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
904 if (DRIVE_IsValid(drive
)) ret
|= (1 << drive
);
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
)
919 /* FIXME, SetLastErrors missing */
921 if (!root
) drive
= DRIVE_GetCurrentDrive();
924 if ((root
[1]) &&((root
[1] != ':') || (root
[2] != '\\')))
926 fprintf( stderr
, "GetVolumeInformation: invalid root '%s'\n",root
);
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
);
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
,
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
);