Store the windows and system directories as long path names.
[wine.git] / files / directory.c
blob5a053a2d1be3ebf59e2a952ab87d78ecf14762fa
1 /*
2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #include <errno.h>
34 #ifdef HAVE_SYS_ERRNO_H
35 #include <sys/errno.h>
36 #endif
38 #include "ntstatus.h"
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wine/winbase16.h"
42 #include "wingdi.h"
43 #include "wine/winuser16.h"
44 #include "winerror.h"
45 #include "winreg.h"
46 #include "winternl.h"
47 #include "wine/unicode.h"
48 #include "file.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(dosfs);
52 WINE_DECLARE_DEBUG_CHANNEL(file);
54 static WCHAR *DIR_Windows;
55 static WCHAR *DIR_System;
57 /***********************************************************************
58 * DIR_GetPath
60 * Get a path name from the wine.ini file and make sure it is valid.
62 static WCHAR *DIR_GetPath( HKEY hkey, LPCWSTR keyname, LPCWSTR defval, BOOL warn )
64 UNICODE_STRING nameW;
65 DWORD dummy;
66 WCHAR tmp[MAX_PATHNAME_LEN];
67 const WCHAR *path;
68 const char *mess;
69 WCHAR *ret;
70 DWORD attr;
72 RtlInitUnicodeString( &nameW, keyname );
73 if (hkey && !NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
74 path = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
75 else
76 path = defval;
78 attr = GetFileAttributesW( path );
79 if (attr == INVALID_FILE_ATTRIBUTES) mess = "does not exist";
80 else if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) mess = "not a directory";
81 else
83 DWORD len = GetFullPathNameW( path, 0, NULL, NULL );
84 ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
85 if (ret) GetFullPathNameW( path, len, ret, NULL );
86 return ret;
89 if (warn)
91 MESSAGE("Invalid path %s for %s directory: %s.\n",
92 debugstr_w(path), debugstr_w(keyname), mess);
93 MESSAGE("Perhaps you have not properly edited your Wine configuration file (%s/config)\n",
94 wine_get_config_dir());
96 return NULL;
100 /***********************************************************************
101 * DIR_Init
103 int DIR_Init(void)
105 OBJECT_ATTRIBUTES attr;
106 UNICODE_STRING nameW;
107 HKEY hkey;
108 char path[MAX_PATHNAME_LEN];
109 WCHAR longpath[MAX_PATHNAME_LEN];
110 WCHAR *tmp_dir, *profile_dir;
111 int drive;
112 const char *cwd;
113 static const WCHAR wineW[] = {'M','a','c','h','i','n','e','\\',
114 'S','o','f','t','w','a','r','e','\\',
115 'W','i','n','e','\\','W','i','n','e','\\',
116 'C','o','n','f','i','g','\\','W','i','n','e',0};
117 static const WCHAR windowsW[] = {'w','i','n','d','o','w','s',0};
118 static const WCHAR systemW[] = {'s','y','s','t','e','m',0};
119 static const WCHAR tempW[] = {'t','e','m','p',0};
120 static const WCHAR profileW[] = {'p','r','o','f','i','l','e',0};
121 static const WCHAR windows_dirW[] = {'c',':','\\','w','i','n','d','o','w','s',0};
122 static const WCHAR system_dirW[] = {'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
123 static const WCHAR temp_dirW[] = {'c',':','\\','w','i','n','d','o','w','s','\\','t','e','m','p',0};
124 static const WCHAR pathW[] = {'p','a','t','h',0};
125 static const WCHAR path_dirW[] = {'c',':','\\','w','i','n','d','o','w','s',';',
126 'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
127 static const WCHAR path_capsW[] = {'P','A','T','H',0};
128 static const WCHAR temp_capsW[] = {'T','E','M','P',0};
129 static const WCHAR tmp_capsW[] = {'T','M','P',0};
130 static const WCHAR windirW[] = {'w','i','n','d','i','r',0};
131 static const WCHAR winsysdirW[] = {'w','i','n','s','y','s','d','i','r',0};
132 static const WCHAR userprofileW[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
133 static const WCHAR systemrootW[] = {'S','Y','S','T','E','M','R','O','O','T',0};
134 static const WCHAR wcmdW[] = {'\\','w','c','m','d','.','e','x','e',0};
135 static const WCHAR comspecW[] = {'C','O','M','S','P','E','C',0};
136 static const WCHAR empty_strW[] = { 0 };
138 if (!getcwd( path, MAX_PATHNAME_LEN ))
140 perror( "Could not get current directory" );
141 return 0;
143 cwd = path;
144 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
146 MESSAGE("Warning: could not find wine config [Drive x] entry "
147 "for current working directory %s; "
148 "starting in windows directory.\n", cwd );
150 else
152 longpath[0] = 'a' + drive;
153 longpath[1] = ':';
154 MultiByteToWideChar(CP_UNIXCP, 0, cwd, -1, longpath + 2, MAX_PATHNAME_LEN);
155 SetCurrentDirectoryW( longpath );
156 if(GetDriveTypeW(longpath)==DRIVE_CDROM)
157 chdir("/"); /* change to root directory so as not to lock cdroms */
160 attr.Length = sizeof(attr);
161 attr.RootDirectory = 0;
162 attr.ObjectName = &nameW;
163 attr.Attributes = 0;
164 attr.SecurityDescriptor = NULL;
165 attr.SecurityQualityOfService = NULL;
167 RtlInitUnicodeString( &nameW, wineW );
168 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) hkey = 0;
170 if (!(DIR_Windows = DIR_GetPath( hkey, windowsW, windows_dirW, TRUE )) ||
171 !(DIR_System = DIR_GetPath( hkey, systemW, system_dirW, TRUE )) ||
172 !(tmp_dir = DIR_GetPath( hkey, tempW, temp_dirW, TRUE )))
174 if (hkey) NtClose( hkey );
175 return 0;
178 if (drive == -1) SetCurrentDirectoryW( DIR_Windows );
180 /* Set the environment variables */
182 /* set COMSPEC only if it doesn't exist already */
183 if (!GetEnvironmentVariableW( comspecW, NULL, 0 ))
185 strcpyW( longpath, DIR_System );
186 strcatW( longpath, wcmdW );
187 SetEnvironmentVariableW( comspecW, longpath );
190 /* set PATH only if not set already */
191 if (!GetEnvironmentVariableW( path_capsW, NULL, 0 ))
193 WCHAR tmp[MAX_PATHNAME_LEN];
194 DWORD dummy;
195 const WCHAR *path = path_dirW;
197 RtlInitUnicodeString( &nameW, pathW );
198 if (hkey && !NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
199 tmp, sizeof(tmp), &dummy ))
201 path = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
204 if (strchrW(path, '/'))
206 MESSAGE("Fix your wine config (%s/config) to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n", wine_get_config_dir() );
207 ExitProcess(1);
209 SetEnvironmentVariableW( path_capsW, path );
210 TRACE("Path = %s\n", debugstr_w(path) );
213 if (!GetEnvironmentVariableW( temp_capsW, NULL, 0 ))
214 SetEnvironmentVariableW( temp_capsW, tmp_dir );
215 if (!GetEnvironmentVariableW( tmp_capsW, NULL, 0 ))
216 SetEnvironmentVariableW( tmp_capsW, tmp_dir );
218 SetEnvironmentVariableW( windirW, DIR_Windows );
219 SetEnvironmentVariableW( systemrootW, DIR_Windows );
220 SetEnvironmentVariableW( winsysdirW, DIR_System );
222 TRACE("WindowsDir = %s\n", debugstr_w(DIR_Windows) );
223 TRACE("SystemDir = %s\n", debugstr_w(DIR_System) );
224 TRACE("TempDir = %s\n", debugstr_w(tmp_dir) );
225 TRACE("SYSTEMROOT = %s\n", debugstr_w(DIR_Windows) );
226 TRACE("Cwd = %c:\\%s\n",
227 'A' + drive, debugstr_w(DRIVE_GetDosCwd(drive)) );
229 HeapFree( GetProcessHeap(), 0, tmp_dir );
231 if ((profile_dir = DIR_GetPath( hkey, profileW, empty_strW, FALSE )))
233 TRACE("USERPROFILE= %s\n", debugstr_w(profile_dir) );
234 SetEnvironmentVariableW( userprofileW, profile_dir );
235 HeapFree( GetProcessHeap(), 0, profile_dir );
238 if (hkey) NtClose( hkey );
240 return 1;
244 /***********************************************************************
245 * GetWindowsDirectoryW (KERNEL32.@)
247 * See comment for GetWindowsDirectoryA.
249 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
251 UINT len = strlenW( DIR_Windows ) + 1;
252 if (path && count >= len)
254 strcpyW( path, DIR_Windows );
255 len--;
257 return len;
261 /***********************************************************************
262 * GetWindowsDirectoryA (KERNEL32.@)
264 * Return value:
265 * If buffer is large enough to hold full path and terminating '\0' character
266 * function copies path to buffer and returns length of the path without '\0'.
267 * Otherwise function returns required size including '\0' character and
268 * does not touch the buffer.
270 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
272 UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_Windows, -1, NULL, 0, NULL, NULL );
273 if (path && count >= len)
275 WideCharToMultiByte( CP_ACP, 0, DIR_Windows, -1, path, count, NULL, NULL );
276 len--;
278 return len;
282 /***********************************************************************
283 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
285 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
287 return GetWindowsDirectoryA( path, count );
291 /***********************************************************************
292 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
294 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
296 return GetWindowsDirectoryW( path, count );
300 /***********************************************************************
301 * GetSystemDirectoryW (KERNEL32.@)
303 * See comment for GetWindowsDirectoryA.
305 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
307 UINT len = strlenW( DIR_System ) + 1;
308 if (path && count >= len)
310 strcpyW( path, DIR_System );
311 len--;
313 return len;
317 /***********************************************************************
318 * GetSystemDirectoryA (KERNEL32.@)
320 * See comment for GetWindowsDirectoryA.
322 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
324 UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_System, -1, NULL, 0, NULL, NULL );
325 if (path && count >= len)
327 WideCharToMultiByte( CP_ACP, 0, DIR_System, -1, path, count, NULL, NULL );
328 len--;
330 return len;
334 /***********************************************************************
335 * CreateDirectoryW (KERNEL32.@)
336 * RETURNS:
337 * TRUE : success
338 * FALSE : failure
339 * ERROR_DISK_FULL: on full disk
340 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
341 * ERROR_ACCESS_DENIED: on permission problems
342 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
344 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
345 LPSECURITY_ATTRIBUTES lpsecattribs )
347 DOS_FULL_NAME full_name;
349 if (!path || !*path)
351 SetLastError(ERROR_PATH_NOT_FOUND);
352 return FALSE;
355 TRACE_(file)("(%s,%p)\n", debugstr_w(path), lpsecattribs );
357 if (RtlIsDosDeviceName_U( path ))
359 TRACE_(file)("cannot use device %s!\n", debugstr_w(path));
360 SetLastError( ERROR_ACCESS_DENIED );
361 return FALSE;
363 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
364 if (mkdir( full_name.long_name, 0777 ) == -1) {
365 WARN_(file)("Error '%s' trying to create directory '%s'\n", strerror(errno), full_name.long_name);
366 /* the FILE_SetDosError() generated error codes don't match the
367 * CreateDirectory ones for some errnos */
368 switch (errno) {
369 case EEXIST:
371 if (!strcmp(DRIVE_GetRoot(full_name.drive), full_name.long_name))
372 SetLastError(ERROR_ACCESS_DENIED);
373 else
374 SetLastError(ERROR_ALREADY_EXISTS);
375 break;
377 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
378 default: FILE_SetDosError();break;
380 return FALSE;
382 return TRUE;
386 /***********************************************************************
387 * CreateDirectoryA (KERNEL32.@)
389 BOOL WINAPI CreateDirectoryA( LPCSTR path,
390 LPSECURITY_ATTRIBUTES lpsecattribs )
392 UNICODE_STRING pathW;
393 BOOL ret = FALSE;
395 if (!path || !*path)
397 SetLastError(ERROR_PATH_NOT_FOUND);
398 return FALSE;
401 if (RtlCreateUnicodeStringFromAsciiz(&pathW, path))
403 ret = CreateDirectoryW(pathW.Buffer, lpsecattribs);
404 RtlFreeUnicodeString(&pathW);
406 else
407 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
408 return ret;
412 /***********************************************************************
413 * CreateDirectoryExA (KERNEL32.@)
415 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
416 LPSECURITY_ATTRIBUTES lpsecattribs)
418 return CreateDirectoryA(path,lpsecattribs);
422 /***********************************************************************
423 * CreateDirectoryExW (KERNEL32.@)
425 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
426 LPSECURITY_ATTRIBUTES lpsecattribs)
428 return CreateDirectoryW(path,lpsecattribs);
432 /***********************************************************************
433 * RemoveDirectoryW (KERNEL32.@)
435 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
437 DOS_FULL_NAME full_name;
439 if (!path)
441 SetLastError(ERROR_INVALID_PARAMETER);
442 return FALSE;
445 TRACE_(file)("%s\n", debugstr_w(path));
447 if (RtlIsDosDeviceName_U( path ))
449 TRACE_(file)("cannot remove device %s!\n", debugstr_w(path));
450 SetLastError( ERROR_FILE_NOT_FOUND );
451 return FALSE;
453 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
454 if (rmdir( full_name.long_name ) == -1)
456 FILE_SetDosError();
457 return FALSE;
459 return TRUE;
463 /***********************************************************************
464 * RemoveDirectoryA (KERNEL32.@)
466 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
468 UNICODE_STRING pathW;
469 BOOL ret = FALSE;
471 if (!path)
473 SetLastError(ERROR_INVALID_PARAMETER);
474 return FALSE;
477 if (RtlCreateUnicodeStringFromAsciiz(&pathW, path))
479 ret = RemoveDirectoryW(pathW.Buffer);
480 RtlFreeUnicodeString(&pathW);
482 else
483 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
484 return ret;