2 * DOS drives handling functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
7 * Label & serial number read support.
8 * (c) 1999 Petr Tomasek <tomasek@etf.cuni.cz>
9 * (c) 2000 Andreas Mohr (changes)
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/port.h"
35 #include <sys/types.h>
43 #ifdef HAVE_SYS_PARAM_H
44 # include <sys/param.h>
46 #ifdef STATFS_DEFINED_BY_SYS_VFS
49 # ifdef STATFS_DEFINED_BY_SYS_MOUNT
50 # include <sys/mount.h>
52 # ifdef STATFS_DEFINED_BY_SYS_STATFS
53 # include <sys/statfs.h>
58 #define NONAMELESSUNION
59 #define NONAMELESSSTRUCT
65 #include "wine/winbase16.h" /* for GetCurrentTask */
74 #include "wine/unicode.h"
75 #include "wine/library.h"
76 #include "wine/server.h"
77 #include "wine/debug.h"
79 WINE_DEFAULT_DEBUG_CHANNEL(dosfs
);
80 WINE_DECLARE_DEBUG_CHANNEL(file
);
84 char *root
; /* root dir in Unix format without trailing / */
85 LPWSTR dos_cwd
; /* cwd in DOS format without leading or trailing \ */
86 char *unix_cwd
; /* cwd in Unix format without leading or trailing / */
87 char *device
; /* raw device path */
88 WCHAR label_conf
[12]; /* drive label as cfg'd in wine config */
89 WCHAR label_read
[12]; /* drive label as read from device */
90 DWORD serial_conf
; /* drive serial number as cfg'd in wine config */
91 UINT type
; /* drive type */
92 UINT flags
; /* drive flags */
93 UINT codepage
; /* drive code page */
94 dev_t dev
; /* unix device number */
95 ino_t ino
; /* unix inode number */
99 static const WCHAR DRIVE_Types
[][8] =
101 { 0 }, /* DRIVE_UNKNOWN */
102 { 0 }, /* DRIVE_NO_ROOT_DIR */
103 {'f','l','o','p','p','y',0}, /* DRIVE_REMOVABLE */
104 {'h','d',0}, /* DRIVE_FIXED */
105 {'n','e','t','w','o','r','k',0}, /* DRIVE_REMOTE */
106 {'c','d','r','o','m',0}, /* DRIVE_CDROM */
107 {'r','a','m','d','i','s','k',0} /* DRIVE_RAMDISK */
111 /* Known filesystem types */
119 static const FS_DESCR DRIVE_Filesystems
[] =
121 { {'u','n','i','x',0}, DRIVE_CASE_SENSITIVE
| DRIVE_CASE_PRESERVING
},
122 { {'m','s','d','o','s',0}, DRIVE_SHORT_NAMES
},
123 { {'d','o','s',0}, DRIVE_SHORT_NAMES
},
124 { {'f','a','t',0}, DRIVE_SHORT_NAMES
},
125 { {'v','f','a','t',0}, DRIVE_CASE_PRESERVING
},
126 { {'w','i','n','9','5',0}, DRIVE_CASE_PRESERVING
},
131 static DOSDRIVE DOSDrives
[MAX_DOS_DRIVES
];
132 static int DRIVE_CurDrive
= -1;
134 static HTASK16 DRIVE_LastTask
= 0;
136 /* strdup on the process heap */
137 inline static char *heap_strdup( const char *str
)
139 INT len
= strlen(str
) + 1;
140 LPSTR p
= HeapAlloc( GetProcessHeap(), 0, len
);
141 if (p
) memcpy( p
, str
, len
);
145 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
147 extern void CDROM_InitRegistry(int dev
);
149 /***********************************************************************
152 static inline UINT
DRIVE_GetDriveType( INT drive
, LPCWSTR value
)
156 for (i
= 0; i
< sizeof(DRIVE_Types
)/sizeof(DRIVE_Types
[0]); i
++)
158 if (!strcmpiW( value
, DRIVE_Types
[i
] )) return i
;
160 MESSAGE("Drive %c: unknown drive type %s, defaulting to 'hd'.\n",
161 'A' + drive
, debugstr_w(value
) );
166 /***********************************************************************
169 static UINT
DRIVE_GetFSFlags( INT drive
, LPCWSTR value
)
171 const FS_DESCR
*descr
;
173 for (descr
= DRIVE_Filesystems
; *descr
->name
; descr
++)
174 if (!strcmpiW( value
, descr
->name
)) return descr
->flags
;
175 MESSAGE("Drive %c: unknown filesystem type %s, defaulting to 'win95'.\n",
176 'A' + drive
, debugstr_w(value
) );
177 return DRIVE_CASE_PRESERVING
;
181 /***********************************************************************
186 int i
, len
, count
= 0;
187 WCHAR driveW
[] = {'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
188 'W','i','n','e','\\','W','i','n','e','\\',
189 'C','o','n','f','i','g','\\','D','r','i','v','e',' ','A',0};
190 WCHAR drive_env
[] = {'=','A',':',0};
191 WCHAR path
[MAX_PATHNAME_LEN
];
192 char tmp
[MAX_PATHNAME_LEN
*sizeof(WCHAR
) + sizeof(KEY_VALUE_PARTIAL_INFORMATION
)];
193 struct stat drive_stat_buffer
;
198 OBJECT_ATTRIBUTES attr
;
199 UNICODE_STRING nameW
;
201 static const WCHAR PathW
[] = {'P','a','t','h',0};
202 static const WCHAR CodepageW
[] = {'C','o','d','e','p','a','g','e',0};
203 static const WCHAR LabelW
[] = {'L','a','b','e','l',0};
204 static const WCHAR SerialW
[] = {'S','e','r','i','a','l',0};
205 static const WCHAR TypeW
[] = {'T','y','p','e',0};
206 static const WCHAR FilesystemW
[] = {'F','i','l','e','s','y','s','t','e','m',0};
207 static const WCHAR DeviceW
[] = {'D','e','v','i','c','e',0};
208 static const WCHAR ReadVolInfoW
[] = {'R','e','a','d','V','o','l','I','n','f','o',0};
209 static const WCHAR FailReadOnlyW
[] = {'F','a','i','l','R','e','a','d','O','n','l','y',0};
210 static const WCHAR driveC_labelW
[] = {'D','r','i','v','e',' ','C',' ',' ',' ',' ',0};
213 attr
.Length
= sizeof(attr
);
214 attr
.RootDirectory
= 0;
215 attr
.ObjectName
= &nameW
;
217 attr
.SecurityDescriptor
= NULL
;
218 attr
.SecurityQualityOfService
= NULL
;
220 for (i
= 0, drive
= DOSDrives
; i
< MAX_DOS_DRIVES
; i
++, drive
++)
222 RtlInitUnicodeString( &nameW
, driveW
);
223 nameW
.Buffer
[(nameW
.Length
/ sizeof(WCHAR
)) - 1] = 'A' + i
;
224 if (NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
) != STATUS_SUCCESS
) continue;
226 /* Get the code page number */
227 RtlInitUnicodeString( &nameW
, CodepageW
);
228 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
230 WCHAR
*data
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
231 drive
->codepage
= strtolW( data
, NULL
, 10 );
234 /* Get the root path */
235 RtlInitUnicodeString( &nameW
, PathW
);
236 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
238 WCHAR
*data
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
239 ExpandEnvironmentStringsW( data
, path
, sizeof(path
)/sizeof(WCHAR
) );
241 p
= path
+ strlenW(path
) - 1;
242 while ((p
> path
) && (*p
== '/')) *p
-- = '\0';
246 len
= WideCharToMultiByte(drive
->codepage
, 0, path
, -1, NULL
, 0, NULL
, NULL
);
247 drive
->root
= HeapAlloc(GetProcessHeap(), 0, len
);
248 WideCharToMultiByte(drive
->codepage
, 0, path
, -1, drive
->root
, len
, NULL
, NULL
);
252 /* relative paths are relative to config dir */
253 const char *config
= wine_get_config_dir();
254 len
= strlen(config
);
255 len
+= WideCharToMultiByte(drive
->codepage
, 0, path
, -1, NULL
, 0, NULL
, NULL
) + 2;
256 drive
->root
= HeapAlloc( GetProcessHeap(), 0, len
);
257 len
-= sprintf( drive
->root
, "%s/", config
);
258 WideCharToMultiByte(drive
->codepage
, 0, path
, -1,
259 drive
->root
+ strlen(drive
->root
), len
, NULL
, NULL
);
262 if (stat( drive
->root
, &drive_stat_buffer
))
264 MESSAGE("Could not stat %s (%s), ignoring drive %c:\n",
265 drive
->root
, strerror(errno
), 'A' + i
);
266 HeapFree( GetProcessHeap(), 0, drive
->root
);
270 if (!S_ISDIR(drive_stat_buffer
.st_mode
))
272 MESSAGE("%s is not a directory, ignoring drive %c:\n",
273 drive
->root
, 'A' + i
);
274 HeapFree( GetProcessHeap(), 0, drive
->root
);
279 drive
->dos_cwd
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(drive
->dos_cwd
[0]));
280 drive
->unix_cwd
= heap_strdup( "" );
281 drive
->device
= NULL
;
283 drive
->dev
= drive_stat_buffer
.st_dev
;
284 drive
->ino
= drive_stat_buffer
.st_ino
;
286 /* Get the drive type */
287 RtlInitUnicodeString( &nameW
, TypeW
);
288 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
290 WCHAR
*data
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
291 drive
->type
= DRIVE_GetDriveType( i
, data
);
293 else drive
->type
= DRIVE_FIXED
;
295 /* Get the drive label */
296 RtlInitUnicodeString( &nameW
, LabelW
);
297 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
299 WCHAR
*data
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
300 lstrcpynW( drive
->label_conf
, data
, 12 );
302 if ((len
= strlenW(drive
->label_conf
)) < 11)
304 /* Pad label with spaces */
305 while(len
< 11) drive
->label_conf
[len
++] = ' ';
306 drive
->label_conf
[11] = '\0';
309 /* Get the serial number */
310 RtlInitUnicodeString( &nameW
, SerialW
);
311 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
313 WCHAR
*data
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
314 drive
->serial_conf
= strtoulW( data
, NULL
, 16 );
316 else drive
->serial_conf
= 12345678;
318 /* Get the filesystem type */
319 RtlInitUnicodeString( &nameW
, FilesystemW
);
320 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
322 WCHAR
*data
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
323 drive
->flags
= DRIVE_GetFSFlags( i
, data
);
325 else drive
->flags
= DRIVE_CASE_PRESERVING
;
328 RtlInitUnicodeString( &nameW
, DeviceW
);
329 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
331 WCHAR
*data
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
332 len
= WideCharToMultiByte(CP_ACP
, 0, data
, -1, NULL
, 0, NULL
, NULL
);
333 drive
->device
= HeapAlloc(GetProcessHeap(), 0, len
);
334 WideCharToMultiByte(drive
->codepage
, 0, data
, -1, drive
->device
, len
, NULL
, NULL
);
336 RtlInitUnicodeString( &nameW
, ReadVolInfoW
);
337 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
339 WCHAR
*data
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
340 if (IS_OPTION_TRUE(data
[0])) drive
->flags
|= DRIVE_READ_VOL_INFO
;
342 else drive
->flags
|= DRIVE_READ_VOL_INFO
;
344 if (drive
->type
== DRIVE_CDROM
)
347 if ((cd_fd
= open(drive
->device
, O_RDONLY
|O_NONBLOCK
)) != -1)
349 CDROM_InitRegistry(cd_fd
);
355 /* Get the FailReadOnly flag */
356 RtlInitUnicodeString( &nameW
, FailReadOnlyW
);
357 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
359 WCHAR
*data
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
360 if (IS_OPTION_TRUE(data
[0])) drive
->flags
|= DRIVE_FAIL_READ_ONLY
;
363 /* Make the first hard disk the current drive */
364 if ((DRIVE_CurDrive
== -1) && (drive
->type
== DRIVE_FIXED
))
368 TRACE("Drive %c: path=%s type=%s label=%s serial=%08lx "
369 "flags=%08x codepage=%u dev=%x ino=%x\n",
370 'A' + i
, drive
->root
, debugstr_w(DRIVE_Types
[drive
->type
]),
371 debugstr_w(drive
->label_conf
), drive
->serial_conf
, drive
->flags
,
372 drive
->codepage
, (int)drive
->dev
, (int)drive
->ino
);
381 MESSAGE("Warning: no valid DOS drive found, check your configuration file.\n" );
382 /* Create a C drive pointing to Unix root dir */
383 DOSDrives
[2].root
= heap_strdup( "/" );
384 DOSDrives
[2].dos_cwd
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(DOSDrives
[2].dos_cwd
[0]));
385 DOSDrives
[2].unix_cwd
= heap_strdup( "" );
386 strcpyW( DOSDrives
[2].label_conf
, driveC_labelW
);
387 DOSDrives
[2].serial_conf
= 12345678;
388 DOSDrives
[2].type
= DRIVE_FIXED
;
389 DOSDrives
[2].device
= NULL
;
390 DOSDrives
[2].flags
= 0;
394 /* Make sure the current drive is valid */
395 if (DRIVE_CurDrive
== -1)
397 for (i
= 0, drive
= DOSDrives
; i
< MAX_DOS_DRIVES
; i
++, drive
++)
399 if (drive
->root
&& !(drive
->flags
& DRIVE_DISABLED
))
407 /* get current working directory info for all drives */
408 for (i
= 0; i
< MAX_DOS_DRIVES
; i
++, drive_env
[1]++)
410 if (!GetEnvironmentVariableW(drive_env
, path
, MAX_PATHNAME_LEN
)) continue;
412 if (toupperW(path
[0]) != drive_env
[1] || path
[1] != ':') continue;
413 DRIVE_Chdir( i
, path
+ 2 );
419 /***********************************************************************
422 int DRIVE_IsValid( int drive
)
424 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
)) return 0;
425 return (DOSDrives
[drive
].root
&&
426 !(DOSDrives
[drive
].flags
& DRIVE_DISABLED
));
430 /***********************************************************************
431 * DRIVE_GetCurrentDrive
433 int DRIVE_GetCurrentDrive(void)
435 TDB
*pTask
= GlobalLock16(GetCurrentTask());
436 if (pTask
&& (pTask
->curdrive
& 0x80)) return pTask
->curdrive
& ~0x80;
437 return DRIVE_CurDrive
;
441 /***********************************************************************
442 * DRIVE_SetCurrentDrive
444 int DRIVE_SetCurrentDrive( int drive
)
446 TDB
*pTask
= GlobalLock16(GetCurrentTask());
447 if (!DRIVE_IsValid( drive
))
449 SetLastError( ERROR_INVALID_DRIVE
);
452 TRACE("%c:\n", 'A' + drive
);
453 DRIVE_CurDrive
= drive
;
454 if (pTask
) pTask
->curdrive
= drive
| 0x80;
459 /***********************************************************************
460 * DRIVE_FindDriveRoot
462 * Find a drive for which the root matches the beginning of the given path.
463 * This can be used to translate a Unix path into a drive + DOS path.
464 * Return value is the drive, or -1 on error. On success, path is modified
465 * to point to the beginning of the DOS path.
467 * Note: path must be in the encoding of the underlying Unix file system.
469 int DRIVE_FindDriveRoot( const char **path
)
471 /* Starting with the full path, check if the device and inode match any of
472 * the wine 'drives'. If not then remove the last path component and try
473 * again. If the last component was a '..' then skip a normal component
474 * since it's a directory that's ascended back out of.
476 int drive
, level
, len
;
477 char buffer
[MAX_PATHNAME_LEN
];
481 strcpy( buffer
, *path
);
482 while ((p
= strchr( buffer
, '\\' )) != NULL
)
484 len
= strlen(buffer
);
486 /* strip off trailing slashes */
487 while (len
> 1 && buffer
[len
- 1] == '/') buffer
[--len
] = 0;
492 if (stat( buffer
, &st
) == 0 && S_ISDIR( st
.st_mode
))
494 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
496 if (!DOSDrives
[drive
].root
||
497 (DOSDrives
[drive
].flags
& DRIVE_DISABLED
))
500 if ((DOSDrives
[drive
].dev
== st
.st_dev
) &&
501 (DOSDrives
[drive
].ino
== st
.st_ino
))
503 if (len
== 1) len
= 0; /* preserve root slash in returned path */
504 TRACE( "%s -> drive %c:, root='%s', name='%s'\n",
505 *path
, 'A' + drive
, buffer
, *path
+ len
);
507 if (!**path
) *path
= "\\";
512 if (len
<= 1) return -1; /* reached root */
517 /* find start of the last path component */
518 while (len
> 1 && buffer
[len
- 1] != '/') len
--;
519 if (!buffer
[len
]) break; /* empty component -> reached root */
520 /* does removing it take us up a level? */
521 if (strcmp( buffer
+ len
, "." ) != 0)
522 level
+= strcmp( buffer
+ len
, ".." ) ? 1 : -1;
524 /* strip off trailing slashes */
525 while (len
> 1 && buffer
[len
- 1] == '/') buffer
[--len
] = 0;
531 /***********************************************************************
532 * DRIVE_FindDriveRootW
534 * Unicode version of DRIVE_FindDriveRoot.
536 int DRIVE_FindDriveRootW( LPCWSTR
*path
)
538 int drive
, level
, len
;
539 WCHAR buffer
[MAX_PATHNAME_LEN
];
543 strcpyW( buffer
, *path
);
544 while ((p
= strchrW( buffer
, '\\' )) != NULL
)
546 len
= strlenW(buffer
);
548 /* strip off trailing slashes */
549 while (len
> 1 && buffer
[len
- 1] == '/') buffer
[--len
] = 0;
556 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
558 char buffA
[MAX_PATHNAME_LEN
];
560 if (!DOSDrives
[drive
].root
||
561 (DOSDrives
[drive
].flags
& DRIVE_DISABLED
))
564 if (codepage
!= DOSDrives
[drive
].codepage
)
566 WideCharToMultiByte( DOSDrives
[drive
].codepage
, 0, buffer
, -1,
567 buffA
, sizeof(buffA
), NULL
, NULL
);
568 if (stat( buffA
, &st
) == -1 || !S_ISDIR( st
.st_mode
))
573 codepage
= DOSDrives
[drive
].codepage
;
576 if ((DOSDrives
[drive
].dev
== st
.st_dev
) &&
577 (DOSDrives
[drive
].ino
== st
.st_ino
))
579 static const WCHAR rootW
[] = {'\\',0};
581 if (len
== 1) len
= 0; /* preserve root slash in returned path */
582 TRACE( "%s -> drive %c:, root=%s, name=%s\n",
583 debugstr_w(*path
), 'A' + drive
, debugstr_w(buffer
), debugstr_w(*path
+ len
));
585 if (!**path
) *path
= rootW
;
589 if (len
<= 1) return -1; /* reached root */
594 static const WCHAR dotW
[] = {'.',0};
595 static const WCHAR dotdotW
[] = {'.','.',0};
597 /* find start of the last path component */
598 while (len
> 1 && buffer
[len
- 1] != '/') len
--;
599 if (!buffer
[len
]) break; /* empty component -> reached root */
600 /* does removing it take us up a level? */
601 if (strcmpW( buffer
+ len
, dotW
) != 0)
602 level
+= strcmpW( buffer
+ len
, dotdotW
) ? 1 : -1;
604 /* strip off trailing slashes */
605 while (len
> 1 && buffer
[len
- 1] == '/') buffer
[--len
] = 0;
611 /***********************************************************************
614 const char * DRIVE_GetRoot( int drive
)
616 if (!DRIVE_IsValid( drive
)) return NULL
;
617 return DOSDrives
[drive
].root
;
621 /***********************************************************************
624 LPCWSTR
DRIVE_GetDosCwd( int drive
)
626 TDB
*pTask
= GlobalLock16(GetCurrentTask());
627 if (!DRIVE_IsValid( drive
)) return NULL
;
629 /* Check if we need to change the directory to the new task. */
630 if (pTask
&& (pTask
->curdrive
& 0x80) && /* The task drive is valid */
631 ((pTask
->curdrive
& ~0x80) == drive
) && /* and it's the one we want */
632 (DRIVE_LastTask
!= GetCurrentTask())) /* and the task changed */
634 static const WCHAR rootW
[] = {'\\',0};
635 WCHAR curdirW
[MAX_PATH
];
636 MultiByteToWideChar(CP_ACP
, 0, pTask
->curdir
, -1, curdirW
, MAX_PATH
);
637 /* Perform the task-switch */
638 if (!DRIVE_Chdir( drive
, curdirW
)) DRIVE_Chdir( drive
, rootW
);
639 DRIVE_LastTask
= GetCurrentTask();
641 return DOSDrives
[drive
].dos_cwd
;
645 /***********************************************************************
648 const char * DRIVE_GetUnixCwd( int drive
)
650 TDB
*pTask
= GlobalLock16(GetCurrentTask());
651 if (!DRIVE_IsValid( drive
)) return NULL
;
653 /* Check if we need to change the directory to the new task. */
654 if (pTask
&& (pTask
->curdrive
& 0x80) && /* The task drive is valid */
655 ((pTask
->curdrive
& ~0x80) == drive
) && /* and it's the one we want */
656 (DRIVE_LastTask
!= GetCurrentTask())) /* and the task changed */
658 static const WCHAR rootW
[] = {'\\',0};
659 WCHAR curdirW
[MAX_PATH
];
660 MultiByteToWideChar(CP_ACP
, 0, pTask
->curdir
, -1, curdirW
, MAX_PATH
);
661 /* Perform the task-switch */
662 if (!DRIVE_Chdir( drive
, curdirW
)) DRIVE_Chdir( drive
, rootW
);
663 DRIVE_LastTask
= GetCurrentTask();
665 return DOSDrives
[drive
].unix_cwd
;
669 /***********************************************************************
672 const char * DRIVE_GetDevice( int drive
)
674 return (DRIVE_IsValid( drive
)) ? DOSDrives
[drive
].device
: NULL
;
677 /******************************************************************
678 * static WORD CDROM_Data_FindBestVoldesc
682 static WORD
CDROM_Data_FindBestVoldesc(int fd
)
684 BYTE cur_vd_type
, max_vd_type
= 0;
685 unsigned int offs
, best_offs
= 0, extra_offs
= 0;
688 for (offs
= 0x8000; offs
<= 0x9800; offs
+= 0x800)
690 /* if 'CDROM' occurs at position 8, this is a pre-iso9660 cd, and
691 * the volume label is displaced forward by 8
693 lseek(fd
, offs
+ 11, SEEK_SET
); /* check for non-ISO9660 signature */
695 if ((sig
[0] == 'R') && (sig
[1] == 'O') && (sig
[2]=='M'))
699 lseek(fd
, offs
+ extra_offs
, SEEK_SET
);
700 read(fd
, &cur_vd_type
, 1);
701 if (cur_vd_type
== 0xff) /* voldesc set terminator */
703 if (cur_vd_type
> max_vd_type
)
705 max_vd_type
= cur_vd_type
;
706 best_offs
= offs
+ extra_offs
;
712 /***********************************************************************
713 * DRIVE_ReadSuperblock
716 * DRIVE_SetLabel and DRIVE_SetSerialNumber use this in order
717 * to check, that they are writing on a FAT filesystem !
719 int DRIVE_ReadSuperblock (int drive
, char * buff
)
721 #define DRIVE_SUPER 96
725 struct stat stat_buf
;
727 memset(buff
, 0, DRIVE_SUPER
);
728 /* O_NONBLOCK in case we're opening FIFO; will be reset later */
729 if ((fd
= open(DOSDrives
[drive
].device
, O_RDONLY
|O_NOCTTY
|O_NONBLOCK
)) != -1) {
730 if (fstat(fd
, &stat_buf
) < 0) { /* shouldn't happen since we just opened that file */
731 ERR("fstat() failed for opened device '%s' (drive %c:) ! IT SHOULDN'T HAPPEN !!!\n",
732 DOSDrives
[drive
].device
, 'A'+drive
);
734 } else if (!S_ISBLK(stat_buf
.st_mode
)) {
735 ERR("Device '%s' (drive %c:) is not a block device - check your config\n",
736 DOSDrives
[drive
].device
, 'A'+drive
);
738 /* reset O_NONBLOCK */
739 } else if (fcntl(fd
, F_SETFL
, 0) < 0 || fcntl(fd
, F_GETFL
) & O_NONBLOCK
) {
740 ERR("fcntl() failed to reset O_NONBLOCK for device '%s' (drive %c:)\n",
741 DOSDrives
[drive
].device
, 'A'+drive
);
749 if (!DOSDrives
[drive
].device
)
750 ERR("No device configured for drive %c: !\n", 'A'+drive
);
752 ERR("Couldn't open device '%s' for drive %c: ! (%s)\n", DOSDrives
[drive
].device
, 'A'+drive
,
753 (stat(DOSDrives
[drive
].device
, &stat_buf
)) ?
754 "not available or symlink not valid ?" : "no permission");
757 ERR("Can't read drive volume info ! Either pre-set it or make sure the device to read it from is accessible !\n");
761 switch(DOSDrives
[drive
].type
)
763 case DRIVE_REMOVABLE
:
768 offs
= CDROM_Data_FindBestVoldesc(fd
);
775 if ((offs
) && (lseek(fd
,offs
,SEEK_SET
)!=offs
))
780 if (read(fd
,buff
,DRIVE_SUPER
)!=DRIVE_SUPER
)
786 switch(DOSDrives
[drive
].type
)
788 case DRIVE_REMOVABLE
:
790 if ((buff
[0x26]!=0x29) || /* Check for FAT present */
791 /* FIXME: do really all FAT have their name beginning with
792 "FAT" ? (At least FAT12, FAT16 and FAT32 have :) */
793 memcmp( buff
+0x36,"FAT",3))
795 ERR("The filesystem is not FAT !! (device=%s)\n",
796 DOSDrives
[drive
].device
);
802 if (strncmp(&buff
[1],"CD001",5)) /* Check for iso9660 present */
807 /* FIXME: do we need to check for "CDROM", too ? (high sierra) */
821 /***********************************************************************
822 * DRIVE_WriteSuperblockEntry
825 * We are writing as little as possible (ie. not the whole SuperBlock)
826 * not to interfere with kernel. The drive can be mounted !
828 int DRIVE_WriteSuperblockEntry (int drive
, off_t ofs
, size_t len
, char * buff
)
832 if ((fd
=open(DOSDrives
[drive
].device
,O_WRONLY
))==-1)
834 ERR("Cannot open the device %s (for writing)\n",
835 DOSDrives
[drive
].device
);
838 if (lseek(fd
,ofs
,SEEK_SET
)!=ofs
)
840 ERR("lseek failed on device %s !\n",
841 DOSDrives
[drive
].device
);
845 if (write(fd
,buff
,len
)!=len
)
848 ERR("Cannot write on %s !\n",
849 DOSDrives
[drive
].device
);
855 /******************************************************************
856 * static HANDLE CDROM_Open
860 static HANDLE
CDROM_Open(int drive
)
862 WCHAR root
[] = {'\\','\\','.','\\','A',':',0};
864 return CreateFileW(root
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
867 /**************************************************************************
868 * CDROM_Data_GetLabel [internal]
870 DWORD
CDROM_Data_GetLabel(int drive
, WCHAR
*label
)
872 #define LABEL_LEN 32+1
873 int dev
= open(DOSDrives
[drive
].device
, O_RDONLY
|O_NONBLOCK
);
874 WORD offs
= CDROM_Data_FindBestVoldesc(dev
);
875 WCHAR label_read
[LABEL_LEN
]; /* Unicode possible, too */
876 DWORD unicode_id
= 0;
880 if ((lseek(dev
, offs
+0x58, SEEK_SET
) == offs
+0x58)
881 && (read(dev
, &unicode_id
, 3) == 3))
883 int ver
= (unicode_id
& 0xff0000) >> 16;
885 if ((lseek(dev
, offs
+0x28, SEEK_SET
) != offs
+0x28)
886 || (read(dev
, &label_read
, LABEL_LEN
) != LABEL_LEN
))
890 if ((LOWORD(unicode_id
) == 0x2f25) /* Unicode ID */
891 && ((ver
== 0x40) || (ver
== 0x43) || (ver
== 0x45)))
892 { /* yippee, unicode */
895 for (i
=0; i
<LABEL_LEN
;i
++)
896 { /* Motorola -> Intel Unicode conversion :-\ */
898 label_read
[i
] = (ch
<< 8) | (ch
>> 8);
900 strncpyW(label
, label_read
, 11);
905 MultiByteToWideChar(DOSDrives
[drive
].codepage
, 0, (LPSTR
)label_read
, -1, label
, 11);
913 ERR("error reading label !\n");
917 /**************************************************************************
918 * CDROM_GetLabel [internal]
920 static DWORD
CDROM_GetLabel(int drive
, WCHAR
*label
)
927 h
= CDROM_Open(drive
);
930 r
= DeviceIoControl(h
, IOCTL_CDROM_DISK_TYPE
, NULL
,
931 0, &cdd
, sizeof(cdd
), &br
, 0);
936 switch (cdd
.DiskData
& 0x03)
938 case CDROM_DISK_DATA_TRACK
:
939 if (!CDROM_Data_GetLabel(drive
, label
))
942 case CDROM_DISK_AUDIO_TRACK
:
944 static const WCHAR audioCD
[] = {'A','u','d','i','o',' ','C','D',' ',' ',' ',0};
945 strcpyW(label
, audioCD
);
948 case CDROM_DISK_DATA_TRACK
|CDROM_DISK_AUDIO_TRACK
:
949 FIXME("Need to get the label of a mixed mode CD!\n");
950 /* This assumes that the first track is a data track! */
951 /* I guess the correct way would be to enumerate all data tracks
952 and check each for the title */
953 if (!CDROM_Data_GetLabel(drive
, label
))
960 TRACE("CD: label is %s\n", debugstr_w(label
));
964 /***********************************************************************
967 LPCWSTR
DRIVE_GetLabel( int drive
)
970 char buff
[DRIVE_SUPER
];
973 if (!DRIVE_IsValid( drive
)) return NULL
;
974 if (DOSDrives
[drive
].type
== DRIVE_CDROM
)
976 read
= CDROM_GetLabel(drive
, DOSDrives
[drive
].label_read
);
979 if (DOSDrives
[drive
].flags
& DRIVE_READ_VOL_INFO
)
981 if (DRIVE_ReadSuperblock(drive
,(char *) buff
))
982 ERR("Invalid or unreadable superblock on %s (%c:).\n",
983 DOSDrives
[drive
].device
, (char)(drive
+'A'));
985 if (DOSDrives
[drive
].type
== DRIVE_REMOVABLE
||
986 DOSDrives
[drive
].type
== DRIVE_FIXED
)
989 /* FIXME: ISO9660 uses a 32 bytes long label. Should we do also? */
991 MultiByteToWideChar(DOSDrives
[drive
].codepage
, 0, buff
+offs
, 11,
992 DOSDrives
[drive
].label_read
, 11);
993 DOSDrives
[drive
].label_read
[11]='\0';
999 DOSDrives
[drive
].label_read
: DOSDrives
[drive
].label_conf
;
1002 #define CDFRAMES_PERSEC 75
1003 #define CDFRAMES_PERMIN (CDFRAMES_PERSEC * 60)
1004 #define FRAME_OF_ADDR(a) ((a)[0] * CDFRAMES_PERMIN + (a)[1] * CDFRAMES_PERSEC + (a)[2])
1005 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
1007 /**************************************************************************
1008 * CDROM_Audio_GetSerial [internal]
1010 static DWORD
CDROM_Audio_GetSerial(HANDLE h
)
1012 unsigned long serial
= 0;
1015 DWORD dwStart
, dwEnd
, br
;
1018 if (!DeviceIoControl(h
, IOCTL_CDROM_READ_TOC
, NULL
, 0, &toc
, sizeof(toc
), &br
, 0))
1022 * wMagic collects the wFrames from track 1
1023 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
1025 * There it is collected for correcting the serial when there are less than
1028 wMagic
= toc
.TrackData
[0].Address
[2];
1029 dwStart
= FRAME_OF_TOC(toc
, toc
.FirstTrack
);
1031 for (i
= 0; i
<= toc
.LastTrack
- toc
.FirstTrack
; i
++) {
1032 serial
+= (toc
.TrackData
[i
].Address
[0] << 16) |
1033 (toc
.TrackData
[i
].Address
[1] << 8) | toc
.TrackData
[i
].Address
[2];
1035 dwEnd
= FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1);
1037 if (toc
.LastTrack
- toc
.FirstTrack
+ 1 < 3)
1038 serial
+= wMagic
+ (dwEnd
- dwStart
);
1043 /**************************************************************************
1044 * CDROM_Data_GetSerial [internal]
1046 static DWORD
CDROM_Data_GetSerial(int drive
)
1048 int dev
= open(DOSDrives
[drive
].device
, O_RDONLY
|O_NONBLOCK
);
1054 BYTE b0
= 0, b1
= 1, b2
= 2, b3
= 3;
1057 if (dev
== -1) return 0;
1058 offs
= CDROM_Data_FindBestVoldesc(dev
);
1064 RTL_OSVERSIONINFOEXW ovi
;
1067 lseek(dev
, offs
, SEEK_SET
);
1068 read(dev
, buf
, 2048);
1070 * OK, another braindead one... argh. Just believe it.
1071 * Me$$ysoft chose to reverse the serial number in NT4/W2K.
1072 * It's true and nobody will ever be able to change it.
1074 ovi
.dwOSVersionInfoSize
= sizeof(ovi
);
1075 RtlGetVersion(&ovi
);
1076 if ((ovi
.dwPlatformId
== VER_PLATFORM_WIN32_NT
) && (ovi
.dwMajorVersion
>= 4))
1078 b0
= 3; b1
= 2; b2
= 1; b3
= 0;
1080 for (i
= 0; i
< 2048; i
+= 4)
1082 /* DON'T optimize this into DWORD !! (breaks overflow) */
1083 serial
.p
[b0
] += buf
[i
+b0
];
1084 serial
.p
[b1
] += buf
[i
+b1
];
1085 serial
.p
[b2
] += buf
[i
+b2
];
1086 serial
.p
[b3
] += buf
[i
+b3
];
1093 /**************************************************************************
1094 * CDROM_GetSerial [internal]
1096 static DWORD
CDROM_GetSerial(int drive
)
1100 CDROM_DISK_DATA cdd
;
1104 TRACE("%d\n", drive
);
1106 h
= CDROM_Open(drive
);
1109 r
= DeviceIoControl(h
, IOCTL_CDROM_DISK_TYPE
, NULL
,
1110 0, &cdd
, sizeof(cdd
), &br
, 0);
1117 switch (cdd
.DiskData
& 0x03)
1119 case CDROM_DISK_DATA_TRACK
:
1120 /* hopefully a data CD */
1121 serial
= CDROM_Data_GetSerial(drive
);
1123 case CDROM_DISK_AUDIO_TRACK
:
1125 case CDROM_DISK_DATA_TRACK
|CDROM_DISK_AUDIO_TRACK
:
1126 serial
= CDROM_Audio_GetSerial(h
);
1133 TRACE("CD serial number is %04x-%04x.\n", HIWORD(serial
), LOWORD(serial
));
1140 /***********************************************************************
1141 * DRIVE_GetSerialNumber
1143 DWORD
DRIVE_GetSerialNumber( int drive
)
1146 char buff
[DRIVE_SUPER
];
1148 TRACE("drive %d, type = %d\n", drive
, DOSDrives
[drive
].type
);
1150 if (!DRIVE_IsValid( drive
)) return 0;
1152 if (DOSDrives
[drive
].flags
& DRIVE_READ_VOL_INFO
)
1154 switch(DOSDrives
[drive
].type
)
1156 case DRIVE_REMOVABLE
:
1158 if (DRIVE_ReadSuperblock(drive
,(char *) buff
))
1159 MESSAGE("Invalid or unreadable superblock on %s (%c:)."
1160 " Maybe not FAT?\n" ,
1161 DOSDrives
[drive
].device
, 'A'+drive
);
1163 serial
= *((DWORD
*)(buff
+0x27));
1166 serial
= CDROM_GetSerial(drive
);
1169 FIXME("Serial number reading from file system on drive %c: not supported yet.\n", drive
+'A');
1173 return (serial
) ? serial
: DOSDrives
[drive
].serial_conf
;
1177 /***********************************************************************
1178 * DRIVE_SetSerialNumber
1180 int DRIVE_SetSerialNumber( int drive
, DWORD serial
)
1182 char buff
[DRIVE_SUPER
];
1184 if (!DRIVE_IsValid( drive
)) return 0;
1186 if (DOSDrives
[drive
].flags
& DRIVE_READ_VOL_INFO
)
1188 if ((DOSDrives
[drive
].type
!= DRIVE_REMOVABLE
) &&
1189 (DOSDrives
[drive
].type
!= DRIVE_FIXED
)) return 0;
1190 /* check, if the drive has a FAT filesystem */
1191 if (DRIVE_ReadSuperblock(drive
, buff
)) return 0;
1192 if (DRIVE_WriteSuperblockEntry(drive
, 0x27, 4, (char *) &serial
)) return 0;
1196 if (DOSDrives
[drive
].type
== DRIVE_CDROM
) return 0;
1197 DOSDrives
[drive
].serial_conf
= serial
;
1202 /***********************************************************************
1205 static UINT
DRIVE_GetType( int drive
)
1207 if (!DRIVE_IsValid( drive
)) return DRIVE_NO_ROOT_DIR
;
1208 return DOSDrives
[drive
].type
;
1212 /***********************************************************************
1215 UINT
DRIVE_GetFlags( int drive
)
1217 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
)) return 0;
1218 return DOSDrives
[drive
].flags
;
1221 /***********************************************************************
1224 UINT
DRIVE_GetCodepage( int drive
)
1226 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
)) return 0;
1227 return DOSDrives
[drive
].codepage
;
1231 /***********************************************************************
1234 int DRIVE_Chdir( int drive
, LPCWSTR path
)
1236 DOS_FULL_NAME full_name
;
1237 WCHAR buffer
[MAX_PATHNAME_LEN
];
1239 BY_HANDLE_FILE_INFORMATION info
;
1240 TDB
*pTask
= GlobalLock16(GetCurrentTask());
1242 buffer
[0] = 'A' + drive
;
1245 TRACE("(%s,%s)\n", debugstr_w(buffer
), debugstr_w(path
) );
1246 strncpyW( buffer
+ 2, path
, MAX_PATHNAME_LEN
- 2 );
1247 buffer
[MAX_PATHNAME_LEN
- 1] = 0; /* ensure 0 termination */
1249 if (!DOSFS_GetFullName( buffer
, TRUE
, &full_name
)) return 0;
1250 if (!FILE_Stat( full_name
.long_name
, &info
, NULL
)) return 0;
1251 if (!(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
1253 SetLastError( ERROR_FILE_NOT_FOUND
);
1256 unix_cwd
= full_name
.long_name
+ strlen( DOSDrives
[drive
].root
);
1257 while (*unix_cwd
== '/') unix_cwd
++;
1259 TRACE("(%c:): unix_cwd=%s dos_cwd=%s\n",
1260 'A' + drive
, unix_cwd
, debugstr_w(full_name
.short_name
+ 3) );
1262 HeapFree( GetProcessHeap(), 0, DOSDrives
[drive
].dos_cwd
);
1263 HeapFree( GetProcessHeap(), 0, DOSDrives
[drive
].unix_cwd
);
1264 DOSDrives
[drive
].dos_cwd
= HeapAlloc(GetProcessHeap(), 0, (strlenW(full_name
.short_name
) - 2) * sizeof(WCHAR
));
1265 strcpyW(DOSDrives
[drive
].dos_cwd
, full_name
.short_name
+ 3);
1266 DOSDrives
[drive
].unix_cwd
= heap_strdup( unix_cwd
);
1268 if (drive
== DRIVE_CurDrive
)
1270 UNICODE_STRING dirW
;
1272 RtlInitUnicodeString( &dirW
, full_name
.short_name
);
1273 RtlSetCurrentDirectory_U( &dirW
);
1276 if (pTask
&& (pTask
->curdrive
& 0x80) &&
1277 ((pTask
->curdrive
& ~0x80) == drive
))
1279 WideCharToMultiByte(CP_ACP
, 0, full_name
.short_name
+ 2, -1,
1280 pTask
->curdir
, sizeof(pTask
->curdir
), NULL
, NULL
);
1281 DRIVE_LastTask
= GetCurrentTask();
1287 /***********************************************************************
1290 int DRIVE_Disable( int drive
)
1292 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
) || !DOSDrives
[drive
].root
)
1294 SetLastError( ERROR_INVALID_DRIVE
);
1297 DOSDrives
[drive
].flags
|= DRIVE_DISABLED
;
1302 /***********************************************************************
1305 int DRIVE_Enable( int drive
)
1307 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
) || !DOSDrives
[drive
].root
)
1309 SetLastError( ERROR_INVALID_DRIVE
);
1312 DOSDrives
[drive
].flags
&= ~DRIVE_DISABLED
;
1317 /***********************************************************************
1318 * DRIVE_SetLogicalMapping
1320 int DRIVE_SetLogicalMapping ( int existing_drive
, int new_drive
)
1322 /* If new_drive is already valid, do nothing and return 0
1323 otherwise, copy DOSDrives[existing_drive] to DOSDrives[new_drive] */
1325 DOSDRIVE
*old
, *new;
1327 old
= DOSDrives
+ existing_drive
;
1328 new = DOSDrives
+ new_drive
;
1330 if ((existing_drive
< 0) || (existing_drive
>= MAX_DOS_DRIVES
) ||
1332 (new_drive
< 0) || (new_drive
>= MAX_DOS_DRIVES
))
1334 SetLastError( ERROR_INVALID_DRIVE
);
1340 TRACE("Can't map drive %c: to already existing drive %c:\n",
1341 'A' + existing_drive
, 'A' + new_drive
);
1342 /* it is already mapped there, so return success */
1343 if (!strcmp(old
->root
,new->root
))
1348 new->root
= heap_strdup( old
->root
);
1349 new->dos_cwd
= HeapAlloc(GetProcessHeap(), 0, (strlenW(old
->dos_cwd
) + 1) * sizeof(WCHAR
));
1350 strcpyW(new->dos_cwd
, old
->dos_cwd
);
1351 new->unix_cwd
= heap_strdup( old
->unix_cwd
);
1352 new->device
= heap_strdup( old
->device
);
1353 memcpy ( new->label_conf
, old
->label_conf
, 12 );
1354 memcpy ( new->label_read
, old
->label_read
, 12 );
1355 new->serial_conf
= old
->serial_conf
;
1356 new->type
= old
->type
;
1357 new->flags
= old
->flags
;
1358 new->dev
= old
->dev
;
1359 new->ino
= old
->ino
;
1361 TRACE("Drive %c: is now equal to drive %c:\n",
1362 'A' + new_drive
, 'A' + existing_drive
);
1368 /***********************************************************************
1371 * Open the drive raw device and return a Unix fd (or -1 on error).
1373 int DRIVE_OpenDevice( int drive
, int flags
)
1375 if (!DRIVE_IsValid( drive
)) return -1;
1376 return open( DOSDrives
[drive
].device
, flags
);
1380 /***********************************************************************
1381 * DRIVE_GetFreeSpace
1383 static int DRIVE_GetFreeSpace( int drive
, PULARGE_INTEGER size
,
1384 PULARGE_INTEGER available
)
1388 if (!DRIVE_IsValid(drive
))
1390 SetLastError( ERROR_PATH_NOT_FOUND
);
1394 /* FIXME: add autoconf check for this */
1395 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
1396 if (statfs( DOSDrives
[drive
].root
, &info
, 0, 0) < 0)
1398 if (statfs( DOSDrives
[drive
].root
, &info
) < 0)
1402 WARN("cannot do statfs(%s)\n", DOSDrives
[drive
].root
);
1406 size
->QuadPart
= RtlEnlargedUnsignedMultiply( info
.f_bsize
, info
.f_blocks
);
1407 #ifdef HAVE_STRUCT_STATFS_F_BAVAIL
1408 available
->QuadPart
= RtlEnlargedUnsignedMultiply( info
.f_bavail
, info
.f_bsize
);
1410 # ifdef HAVE_STRUCT_STATFS_F_BFREE
1411 available
->QuadPart
= RtlEnlargedUnsignedMultiply( info
.f_bfree
, info
.f_bsize
);
1413 # error "statfs has no bfree/bavail member!"
1416 if (DOSDrives
[drive
].type
== DRIVE_CDROM
)
1417 { /* ALWAYS 0, even if no real CD-ROM mounted there !! */
1418 available
->QuadPart
= 0;
1423 /***********************************************************************
1424 * DRIVE_GetCurrentDirectory
1425 * Returns "X:\\path\\etc\\".
1427 * Despite the API description, return required length including the
1428 * terminating null when buffer too small. This is the real behaviour.
1430 static UINT
DRIVE_GetCurrentDirectory( UINT buflen
, LPWSTR buf
)
1433 LPCWSTR dos_cwd
= DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );
1434 static const WCHAR driveA_rootW
[] = {'A',':','\\',0};
1436 ret
= strlenW(dos_cwd
) + 3; /* length of WHOLE current directory */
1437 if (ret
>= buflen
) return ret
+ 1;
1439 strcpyW( buf
, driveA_rootW
);
1440 buf
[0] += DRIVE_GetCurrentDrive();
1441 strcatW( buf
, dos_cwd
);
1446 /***********************************************************************
1449 * Build the environment array containing the drives' current directories.
1450 * Resulting pointer must be freed with HeapFree.
1452 char *DRIVE_BuildEnv(void)
1455 LPCWSTR cwd
[MAX_DOS_DRIVES
];
1458 for (i
= 0; i
< MAX_DOS_DRIVES
; i
++)
1460 if ((cwd
[i
] = DRIVE_GetDosCwd(i
)) && cwd
[i
][0])
1461 length
+= WideCharToMultiByte(DRIVE_GetCodepage(i
), 0,
1462 cwd
[i
], -1, NULL
, 0, NULL
, NULL
) + 7;
1464 if (!(env
= HeapAlloc( GetProcessHeap(), 0, length
+1 ))) return NULL
;
1465 for (i
= 0, p
= env
; i
< MAX_DOS_DRIVES
; i
++)
1467 if (cwd
[i
] && cwd
[i
][0])
1469 *p
++ = '='; *p
++ = 'A' + i
; *p
++ = ':';
1470 *p
++ = '='; *p
++ = 'A' + i
; *p
++ = ':'; *p
++ = '\\';
1471 WideCharToMultiByte(DRIVE_GetCodepage(i
), 0, cwd
[i
], -1, p
, 0x7fffffff, NULL
, NULL
);
1480 /***********************************************************************
1481 * GetDiskFreeSpace (KERNEL.422)
1483 BOOL16 WINAPI
GetDiskFreeSpace16( LPCSTR root
, LPDWORD cluster_sectors
,
1484 LPDWORD sector_bytes
, LPDWORD free_clusters
,
1485 LPDWORD total_clusters
)
1487 return GetDiskFreeSpaceA( root
, cluster_sectors
, sector_bytes
,
1488 free_clusters
, total_clusters
);
1492 /***********************************************************************
1493 * GetDiskFreeSpaceW (KERNEL32.@)
1495 * Fails if expression resulting from current drive's dir and "root"
1496 * is not a root dir of the target drive.
1498 * UNDOC: setting some LPDWORDs to NULL is perfectly possible
1499 * if the corresponding info is unneeded.
1501 * FIXME: needs to support UNC names from Win95 OSR2 on.
1503 * Behaviour under Win95a:
1504 * CurrDir root result
1505 * "E:\\TEST" "E:" FALSE
1509 * "E:\\TEST" "\\" TRUE
1510 * "E:\\TEST" ":\\" FALSE
1511 * "E:\\TEST" "E:\\" TRUE
1512 * "E:\\TEST" "" FALSE
1513 * "E:\\" "" FALSE (!)
1515 * "E:\\TEST" 0x0 TRUE (!)
1516 * "E:\\TEST" "C:" TRUE (when CurrDir of "C:" set to "\\")
1517 * "E:\\TEST" "C:" FALSE (when CurrDir of "C:" set to "\\TEST")
1519 BOOL WINAPI
GetDiskFreeSpaceW( LPCWSTR root
, LPDWORD cluster_sectors
,
1520 LPDWORD sector_bytes
, LPDWORD free_clusters
,
1521 LPDWORD total_clusters
)
1523 int drive
, sec_size
;
1524 ULARGE_INTEGER size
,available
;
1528 TRACE("%s,%p,%p,%p,%p\n", debugstr_w(root
), cluster_sectors
, sector_bytes
,
1529 free_clusters
, total_clusters
);
1531 if (!root
|| root
[0] == '\\' || root
[0] == '/')
1532 drive
= DRIVE_GetCurrentDrive();
1534 if (root
[0] && root
[1] == ':') /* root contains drive tag */
1536 drive
= toupperW(root
[0]) - 'A';
1538 if (path
[0] == '\0')
1540 path
= DRIVE_GetDosCwd(drive
);
1543 SetLastError(ERROR_PATH_NOT_FOUND
);
1548 if (path
[0] == '\\')
1551 if (path
[0]) /* oops, we are in a subdir */
1553 SetLastError(ERROR_INVALID_NAME
);
1560 SetLastError(ERROR_PATH_NOT_FOUND
);
1562 SetLastError(ERROR_INVALID_NAME
);
1566 if (!DRIVE_GetFreeSpace(drive
, &size
, &available
)) return FALSE
;
1568 /* Cap the size and available at 2GB as per specs. */
1569 if ((size
.s
.HighPart
) ||(size
.s
.LowPart
> 0x7fffffff))
1571 size
.s
.HighPart
= 0;
1572 size
.s
.LowPart
= 0x7fffffff;
1574 if ((available
.s
.HighPart
) ||(available
.s
.LowPart
> 0x7fffffff))
1576 available
.s
.HighPart
=0;
1577 available
.s
.LowPart
= 0x7fffffff;
1579 sec_size
= (DRIVE_GetType(drive
)==DRIVE_CDROM
) ? 2048 : 512;
1580 size
.s
.LowPart
/= sec_size
;
1581 available
.s
.LowPart
/= sec_size
;
1582 /* FIXME: probably have to adjust those variables too for CDFS */
1584 while (cluster_sec
* 65536 < size
.s
.LowPart
) cluster_sec
*= 2;
1586 if (cluster_sectors
)
1587 *cluster_sectors
= cluster_sec
;
1589 *sector_bytes
= sec_size
;
1591 *free_clusters
= available
.s
.LowPart
/ cluster_sec
;
1593 *total_clusters
= size
.s
.LowPart
/ cluster_sec
;
1598 /***********************************************************************
1599 * GetDiskFreeSpaceA (KERNEL32.@)
1601 BOOL WINAPI
GetDiskFreeSpaceA( LPCSTR root
, LPDWORD cluster_sectors
,
1602 LPDWORD sector_bytes
, LPDWORD free_clusters
,
1603 LPDWORD total_clusters
)
1605 UNICODE_STRING rootW
;
1610 if(!RtlCreateUnicodeStringFromAsciiz(&rootW
, root
))
1612 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1617 rootW
.Buffer
= NULL
;
1619 ret
= GetDiskFreeSpaceW(rootW
.Buffer
, cluster_sectors
, sector_bytes
,
1620 free_clusters
, total_clusters
);
1621 RtlFreeUnicodeString(&rootW
);
1627 /***********************************************************************
1628 * GetDiskFreeSpaceExW (KERNEL32.@)
1630 * This function is used to acquire the size of the available and
1631 * total space on a logical volume.
1635 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1636 * detailed error information.
1639 BOOL WINAPI
GetDiskFreeSpaceExW( LPCWSTR root
,
1640 PULARGE_INTEGER avail
,
1641 PULARGE_INTEGER total
,
1642 PULARGE_INTEGER totalfree
)
1645 ULARGE_INTEGER size
,available
;
1647 if (!root
) drive
= DRIVE_GetCurrentDrive();
1649 { /* C: always works for GetDiskFreeSpaceEx */
1650 if ((root
[1]) && ((root
[1] != ':') || (root
[2] && root
[2] != '\\')))
1652 FIXME("there are valid root names which are not supported yet\n");
1653 /* ..like UNC names, for instance. */
1655 WARN("invalid root '%s'\n", debugstr_w(root
));
1658 drive
= toupperW(root
[0]) - 'A';
1661 if (!DRIVE_GetFreeSpace(drive
, &size
, &available
)) return FALSE
;
1665 total
->s
.HighPart
= size
.s
.HighPart
;
1666 total
->s
.LowPart
= size
.s
.LowPart
;
1671 totalfree
->s
.HighPart
= available
.s
.HighPart
;
1672 totalfree
->s
.LowPart
= available
.s
.LowPart
;
1677 if (FIXME_ON(dosfs
))
1679 /* On Windows2000, we need to check the disk quota
1680 allocated for the user owning the calling process. We
1681 don't want to be more obtrusive than necessary with the
1682 FIXME messages, so don't print the FIXME unless Wine is
1683 actually masquerading as Windows2000. */
1685 RTL_OSVERSIONINFOEXW ovi
;
1686 ovi
.dwOSVersionInfoSize
= sizeof(ovi
);
1687 if (RtlGetVersion(&ovi
))
1689 if (ovi
.dwPlatformId
== VER_PLATFORM_WIN32_NT
&& ovi
.dwMajorVersion
> 4)
1690 FIXME("no per-user quota support yet\n");
1694 /* Quick hack, should eventually be fixed to work 100% with
1695 Windows2000 (see comment above). */
1696 avail
->s
.HighPart
= available
.s
.HighPart
;
1697 avail
->s
.LowPart
= available
.s
.LowPart
;
1703 /***********************************************************************
1704 * GetDiskFreeSpaceExA (KERNEL32.@)
1706 BOOL WINAPI
GetDiskFreeSpaceExA( LPCSTR root
, PULARGE_INTEGER avail
,
1707 PULARGE_INTEGER total
,
1708 PULARGE_INTEGER totalfree
)
1710 UNICODE_STRING rootW
;
1713 if (root
) RtlCreateUnicodeStringFromAsciiz(&rootW
, root
);
1714 else rootW
.Buffer
= NULL
;
1716 ret
= GetDiskFreeSpaceExW( rootW
.Buffer
, avail
, total
, totalfree
);
1718 RtlFreeUnicodeString(&rootW
);
1722 /***********************************************************************
1723 * GetDriveType (KERNEL.136)
1724 * This function returns the type of a drive in Win16.
1725 * Note that it returns DRIVE_REMOTE for CD-ROMs, since MSCDEX uses the
1726 * remote drive API. The return value DRIVE_REMOTE for CD-ROMs has been
1727 * verified on Win 3.11 and Windows 95. Some programs rely on it, so don't
1728 * do any pseudo-clever changes.
1731 * drivetype DRIVE_xxx
1733 UINT16 WINAPI
GetDriveType16( UINT16 drive
) /* [in] number (NOT letter) of drive */
1735 UINT type
= DRIVE_GetType(drive
);
1736 TRACE("(%c:)\n", 'A' + drive
);
1737 if (type
== DRIVE_CDROM
) type
= DRIVE_REMOTE
;
1742 /***********************************************************************
1743 * GetDriveTypeW (KERNEL32.@)
1745 * Returns the type of the disk drive specified. If root is NULL the
1746 * root of the current directory is used.
1750 * Type of drive (from Win32 SDK):
1752 * DRIVE_UNKNOWN unable to find out anything about the drive
1753 * DRIVE_NO_ROOT_DIR nonexistent root dir
1754 * DRIVE_REMOVABLE the disk can be removed from the machine
1755 * DRIVE_FIXED the disk can not be removed from the machine
1756 * DRIVE_REMOTE network disk
1757 * DRIVE_CDROM CDROM drive
1758 * DRIVE_RAMDISK virtual disk in RAM
1760 UINT WINAPI
GetDriveTypeW(LPCWSTR root
) /* [in] String describing drive */
1763 TRACE("(%s)\n", debugstr_w(root
));
1765 if (NULL
== root
) drive
= DRIVE_GetCurrentDrive();
1768 if ((root
[1]) && (root
[1] != ':'))
1770 WARN("invalid root %s\n", debugstr_w(root
));
1771 return DRIVE_NO_ROOT_DIR
;
1773 drive
= toupperW(root
[0]) - 'A';
1775 return DRIVE_GetType(drive
);
1779 /***********************************************************************
1780 * GetDriveTypeA (KERNEL32.@)
1782 UINT WINAPI
GetDriveTypeA( LPCSTR root
)
1784 UNICODE_STRING rootW
;
1789 if( !RtlCreateUnicodeStringFromAsciiz(&rootW
, root
))
1791 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1796 rootW
.Buffer
= NULL
;
1798 ret
= GetDriveTypeW(rootW
.Buffer
);
1800 RtlFreeUnicodeString(&rootW
);
1806 /***********************************************************************
1807 * GetCurrentDirectory (KERNEL.411)
1809 UINT16 WINAPI
GetCurrentDirectory16( UINT16 buflen
, LPSTR buf
)
1811 WCHAR cur_dirW
[MAX_PATH
];
1813 DRIVE_GetCurrentDirectory(MAX_PATH
, cur_dirW
);
1814 return (UINT16
)WideCharToMultiByte(CP_ACP
, 0, cur_dirW
, -1, buf
, buflen
, NULL
, NULL
);
1818 /***********************************************************************
1819 * GetCurrentDirectoryW (KERNEL32.@)
1821 UINT WINAPI
GetCurrentDirectoryW( UINT buflen
, LPWSTR buf
)
1824 WCHAR longname
[MAX_PATHNAME_LEN
];
1825 WCHAR shortname
[MAX_PATHNAME_LEN
];
1827 ret
= DRIVE_GetCurrentDirectory(MAX_PATHNAME_LEN
, shortname
);
1828 if ( ret
> MAX_PATHNAME_LEN
) {
1829 ERR_(file
)("pathnamelength (%d) > MAX_PATHNAME_LEN!\n", ret
);
1832 GetLongPathNameW(shortname
, longname
, MAX_PATHNAME_LEN
);
1833 ret
= strlenW( longname
) + 1;
1834 if (ret
> buflen
) return ret
;
1835 strcpyW(buf
, longname
);
1839 /***********************************************************************
1840 * GetCurrentDirectoryA (KERNEL32.@)
1842 UINT WINAPI
GetCurrentDirectoryA( UINT buflen
, LPSTR buf
)
1844 WCHAR bufferW
[MAX_PATH
];
1847 retW
= GetCurrentDirectoryW(MAX_PATH
, bufferW
);
1851 else if (retW
> MAX_PATH
)
1853 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
1858 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
1861 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buf
, buflen
, NULL
, NULL
);
1862 ret
--; /* length without 0 */
1869 /***********************************************************************
1870 * SetCurrentDirectory (KERNEL.412)
1872 BOOL16 WINAPI
SetCurrentDirectory16( LPCSTR dir
)
1874 return SetCurrentDirectoryA( dir
);
1878 /***********************************************************************
1879 * SetCurrentDirectoryW (KERNEL32.@)
1881 BOOL WINAPI
SetCurrentDirectoryW( LPCWSTR dir
)
1883 int drive
, olddrive
= DRIVE_GetCurrentDrive();
1887 SetLastError(ERROR_INVALID_PARAMETER
);
1890 if (dir
[0] && (dir
[1]==':'))
1892 drive
= toupperW( *dir
) - 'A';
1898 /* WARNING: we need to set the drive before the dir, as DRIVE_Chdir
1899 sets pTask->curdir only if pTask->curdrive is drive */
1900 if (!(DRIVE_SetCurrentDrive( drive
)))
1903 /* FIXME: what about empty strings? Add a \\ ? */
1904 if (!DRIVE_Chdir( drive
, dir
)) {
1905 DRIVE_SetCurrentDrive(olddrive
);
1912 /***********************************************************************
1913 * SetCurrentDirectoryA (KERNEL32.@)
1915 BOOL WINAPI
SetCurrentDirectoryA( LPCSTR dir
)
1917 UNICODE_STRING dirW
;
1922 SetLastError(ERROR_INVALID_PARAMETER
);
1926 if (RtlCreateUnicodeStringFromAsciiz(&dirW
, dir
))
1928 ret
= SetCurrentDirectoryW(dirW
.Buffer
);
1929 RtlFreeUnicodeString(&dirW
);
1932 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1937 /***********************************************************************
1938 * GetLogicalDriveStringsA (KERNEL32.@)
1940 UINT WINAPI
GetLogicalDriveStringsA( UINT len
, LPSTR buffer
)
1944 for (drive
= count
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
1945 if (DRIVE_IsValid(drive
)) count
++;
1946 if ((count
* 4) + 1 <= len
)
1949 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
1950 if (DRIVE_IsValid(drive
))
1961 return (count
* 4) + 1; /* account for terminating null */
1962 /* The API tells about these different return values */
1966 /***********************************************************************
1967 * GetLogicalDriveStringsW (KERNEL32.@)
1969 UINT WINAPI
GetLogicalDriveStringsW( UINT len
, LPWSTR buffer
)
1973 for (drive
= count
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
1974 if (DRIVE_IsValid(drive
)) count
++;
1975 if (count
* 4 * sizeof(WCHAR
) <= len
)
1978 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
1979 if (DRIVE_IsValid(drive
))
1981 *p
++ = (WCHAR
)('a' + drive
);
1988 return count
* 4 * sizeof(WCHAR
);
1992 /***********************************************************************
1993 * GetLogicalDrives (KERNEL32.@)
1995 DWORD WINAPI
GetLogicalDrives(void)
2000 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
2002 if ( (DRIVE_IsValid(drive
)) ||
2003 (DOSDrives
[drive
].type
== DRIVE_CDROM
)) /* audio CD is also valid */
2004 ret
|= (1 << drive
);
2010 /***********************************************************************
2011 * GetVolumeInformationW (KERNEL32.@)
2013 BOOL WINAPI
GetVolumeInformationW( LPCWSTR root
, LPWSTR label
,
2014 DWORD label_len
, DWORD
*serial
,
2015 DWORD
*filename_len
, DWORD
*flags
,
2016 LPWSTR fsname
, DWORD fsname_len
)
2021 /* FIXME, SetLastError()s missing */
2023 if (!root
) drive
= DRIVE_GetCurrentDrive();
2026 if (root
[0] && root
[1] != ':')
2028 WARN("invalid root %s\n", debugstr_w(root
));
2031 drive
= toupperW(root
[0]) - 'A';
2033 if (!DRIVE_IsValid( drive
)) return FALSE
;
2034 if (label
&& label_len
)
2036 strncpyW( label
, DRIVE_GetLabel(drive
), label_len
);
2037 label
[label_len
- 1] = 0; /* ensure 0 termination */
2038 cp
= label
+ strlenW(label
);
2039 while (cp
!= label
&& *(cp
-1) == ' ') cp
--;
2042 if (serial
) *serial
= DRIVE_GetSerialNumber(drive
);
2044 /* Set the filesystem information */
2045 /* Note: we only emulate a FAT fs at present */
2048 if (DOSDrives
[drive
].flags
& DRIVE_SHORT_NAMES
)
2051 *filename_len
= 255;
2056 if (DOSDrives
[drive
].flags
& DRIVE_CASE_SENSITIVE
)
2057 *flags
|=FS_CASE_SENSITIVE
;
2058 if (DOSDrives
[drive
].flags
& DRIVE_CASE_PRESERVING
)
2059 *flags
|=FS_CASE_IS_PRESERVED
;
2061 if (fsname
&& fsname_len
)
2063 /* Diablo checks that return code ... */
2064 if (DOSDrives
[drive
].type
== DRIVE_CDROM
)
2066 static const WCHAR cdfsW
[] = {'C','D','F','S',0};
2067 strncpyW( fsname
, cdfsW
, fsname_len
);
2071 static const WCHAR fatW
[] = {'F','A','T',0};
2072 strncpyW( fsname
, fatW
, fsname_len
);
2074 fsname
[fsname_len
- 1] = 0; /* ensure 0 termination */
2080 /***********************************************************************
2081 * GetVolumeInformationA (KERNEL32.@)
2083 BOOL WINAPI
GetVolumeInformationA( LPCSTR root
, LPSTR label
,
2084 DWORD label_len
, DWORD
*serial
,
2085 DWORD
*filename_len
, DWORD
*flags
,
2086 LPSTR fsname
, DWORD fsname_len
)
2088 UNICODE_STRING rootW
;
2089 LPWSTR labelW
, fsnameW
;
2092 if (root
) RtlCreateUnicodeStringFromAsciiz(&rootW
, root
);
2093 else rootW
.Buffer
= NULL
;
2094 labelW
= label
? HeapAlloc(GetProcessHeap(), 0, label_len
* sizeof(WCHAR
)) : NULL
;
2095 fsnameW
= fsname
? HeapAlloc(GetProcessHeap(), 0, fsname_len
* sizeof(WCHAR
)) : NULL
;
2097 if ((ret
= GetVolumeInformationW(rootW
.Buffer
, labelW
, label_len
, serial
,
2098 filename_len
, flags
, fsnameW
, fsname_len
)))
2100 if (label
) WideCharToMultiByte(CP_ACP
, 0, labelW
, -1, label
, label_len
, NULL
, NULL
);
2101 if (fsname
) WideCharToMultiByte(CP_ACP
, 0, fsnameW
, -1, fsname
, fsname_len
, NULL
, NULL
);
2104 RtlFreeUnicodeString(&rootW
);
2105 if (labelW
) HeapFree( GetProcessHeap(), 0, labelW
);
2106 if (fsnameW
) HeapFree( GetProcessHeap(), 0, fsnameW
);
2110 /***********************************************************************
2111 * SetVolumeLabelW (KERNEL32.@)
2113 BOOL WINAPI
SetVolumeLabelW( LPCWSTR root
, LPCWSTR volname
)
2117 /* FIXME, SetLastErrors missing */
2119 if (!root
) drive
= DRIVE_GetCurrentDrive();
2122 if ((root
[1]) && (root
[1] != ':'))
2124 WARN("invalid root %s\n", debugstr_w(root
));
2127 drive
= toupperW(root
[0]) - 'A';
2129 if (!DRIVE_IsValid( drive
)) return FALSE
;
2131 /* some copy protection stuff check this */
2132 if (DOSDrives
[drive
].type
== DRIVE_CDROM
) return FALSE
;
2134 strncpyW(DOSDrives
[drive
].label_conf
, volname
, 12);
2135 DOSDrives
[drive
].label_conf
[12 - 1] = 0; /* ensure 0 termination */
2139 /***********************************************************************
2140 * SetVolumeLabelA (KERNEL32.@)
2142 BOOL WINAPI
SetVolumeLabelA(LPCSTR root
, LPCSTR volname
)
2144 UNICODE_STRING rootW
, volnameW
;
2147 if (root
) RtlCreateUnicodeStringFromAsciiz(&rootW
, root
);
2148 else rootW
.Buffer
= NULL
;
2149 if (volname
) RtlCreateUnicodeStringFromAsciiz(&volnameW
, volname
);
2150 else volnameW
.Buffer
= NULL
;
2152 ret
= SetVolumeLabelW( rootW
.Buffer
, volnameW
.Buffer
);
2154 RtlFreeUnicodeString(&rootW
);
2155 RtlFreeUnicodeString(&volnameW
);
2159 /***********************************************************************
2160 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
2162 DWORD WINAPI
GetVolumeNameForVolumeMountPointW(LPWSTR str
, DWORD a
, DWORD b
)
2164 FIXME("(%s, %lx, %lx): stub\n", debugstr_w(str
), a
, b
);