- rewrote the file code so that streams manage low level file desc
[wine.git] / files / directory.c
blobb139cc4fda190f89a1b9d49117cdb7d3883a94d0
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 "thread.h"
48 #include "wine/unicode.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(dosfs);
52 WINE_DECLARE_DEBUG_CHANNEL(file);
54 #define MAX_PATHNAME_LEN 1024
56 static WCHAR *DIR_Windows;
57 static WCHAR *DIR_System;
59 /***********************************************************************
60 * DIR_GetPath
62 * Get a path name from the wine.ini file and make sure it is valid.
64 static WCHAR *DIR_GetPath( HKEY hkey, LPCWSTR keyname, LPCWSTR defval, BOOL warn )
66 UNICODE_STRING nameW;
67 DWORD dummy;
68 WCHAR tmp[MAX_PATHNAME_LEN];
69 const WCHAR *path;
70 const char *mess;
71 WCHAR *ret;
72 DWORD attr;
74 RtlInitUnicodeString( &nameW, keyname );
75 if (hkey && !NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
76 path = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
77 else
78 path = defval;
80 attr = GetFileAttributesW( path );
81 if (attr == INVALID_FILE_ATTRIBUTES) mess = "does not exist";
82 else if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) mess = "not a directory";
83 else
85 DWORD len = GetFullPathNameW( path, 0, NULL, NULL );
86 ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
87 if (ret) GetFullPathNameW( path, len, ret, NULL );
88 return ret;
91 if (warn)
93 MESSAGE("Invalid path %s for %s directory: %s.\n",
94 debugstr_w(path), debugstr_w(keyname), mess);
95 MESSAGE("Perhaps you have not properly edited your Wine configuration file (%s/config)\n",
96 wine_get_config_dir());
98 return NULL;
102 /***********************************************************************
103 * DIR_Init
105 int DIR_Init(void)
107 OBJECT_ATTRIBUTES attr;
108 UNICODE_STRING nameW;
109 HKEY hkey;
110 char path[MAX_PATHNAME_LEN];
111 WCHAR longpath[MAX_PATHNAME_LEN];
112 WCHAR *tmp_dir, *profile_dir;
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 attr.Length = sizeof(attr);
139 attr.RootDirectory = 0;
140 attr.ObjectName = &nameW;
141 attr.Attributes = 0;
142 attr.SecurityDescriptor = NULL;
143 attr.SecurityQualityOfService = NULL;
145 RtlInitUnicodeString( &nameW, wineW );
146 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) hkey = 0;
148 if (!(DIR_Windows = DIR_GetPath( hkey, windowsW, windows_dirW, TRUE )) ||
149 !(DIR_System = DIR_GetPath( hkey, systemW, system_dirW, TRUE )) ||
150 !(tmp_dir = DIR_GetPath( hkey, tempW, temp_dirW, TRUE )))
152 if (hkey) NtClose( hkey );
153 return 0;
156 if (!getcwd( path, MAX_PATHNAME_LEN ))
158 MESSAGE("Warning: could not get current Unix working directory, "
159 "starting in the Windows directory.\n" );
160 SetCurrentDirectoryW( DIR_Windows );
162 else
164 MultiByteToWideChar( CP_UNIXCP, 0, path, -1, longpath, MAX_PATHNAME_LEN);
165 GetFullPathNameW( longpath, MAX_PATHNAME_LEN, longpath, NULL );
166 if (!SetCurrentDirectoryW( longpath ))
168 MESSAGE("Warning: could not find DOS drive for current working directory '%s', "
169 "starting in the Windows directory.\n", path );
170 SetCurrentDirectoryW( DIR_Windows );
172 else if (!NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.Handle)
173 chdir("/"); /* change to root directory so as not to lock cdroms */
176 /* Set the environment variables */
178 /* set COMSPEC only if it doesn't exist already */
179 if (!GetEnvironmentVariableW( comspecW, NULL, 0 ))
181 strcpyW( longpath, DIR_System );
182 strcatW( longpath, wcmdW );
183 SetEnvironmentVariableW( comspecW, longpath );
186 /* set PATH only if not set already */
187 if (!GetEnvironmentVariableW( path_capsW, NULL, 0 ))
189 WCHAR tmp[MAX_PATHNAME_LEN];
190 DWORD dummy;
191 const WCHAR *path = path_dirW;
193 RtlInitUnicodeString( &nameW, pathW );
194 if (hkey && !NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
195 tmp, sizeof(tmp), &dummy ))
197 path = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
200 if (strchrW(path, '/'))
202 MESSAGE("Fix your wine config (%s/config) to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n", wine_get_config_dir() );
203 ExitProcess(1);
205 SetEnvironmentVariableW( path_capsW, path );
206 TRACE("Path = %s\n", debugstr_w(path) );
209 if (!GetEnvironmentVariableW( temp_capsW, NULL, 0 ))
210 SetEnvironmentVariableW( temp_capsW, tmp_dir );
211 if (!GetEnvironmentVariableW( tmp_capsW, NULL, 0 ))
212 SetEnvironmentVariableW( tmp_capsW, tmp_dir );
214 SetEnvironmentVariableW( windirW, DIR_Windows );
215 SetEnvironmentVariableW( systemrootW, DIR_Windows );
216 SetEnvironmentVariableW( winsysdirW, DIR_System );
218 TRACE("WindowsDir = %s\n", debugstr_w(DIR_Windows) );
219 TRACE("SystemDir = %s\n", debugstr_w(DIR_System) );
220 TRACE("TempDir = %s\n", debugstr_w(tmp_dir) );
221 TRACE("SYSTEMROOT = %s\n", debugstr_w(DIR_Windows) );
223 HeapFree( GetProcessHeap(), 0, tmp_dir );
225 if ((profile_dir = DIR_GetPath( hkey, profileW, empty_strW, FALSE )))
227 TRACE("USERPROFILE= %s\n", debugstr_w(profile_dir) );
228 SetEnvironmentVariableW( userprofileW, profile_dir );
229 HeapFree( GetProcessHeap(), 0, profile_dir );
232 if (hkey) NtClose( hkey );
234 return 1;
238 /***********************************************************************
239 * GetWindowsDirectoryW (KERNEL32.@)
241 * See comment for GetWindowsDirectoryA.
243 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
245 UINT len = strlenW( DIR_Windows ) + 1;
246 if (path && count >= len)
248 strcpyW( path, DIR_Windows );
249 len--;
251 return len;
255 /***********************************************************************
256 * GetWindowsDirectoryA (KERNEL32.@)
258 * Return value:
259 * If buffer is large enough to hold full path and terminating '\0' character
260 * function copies path to buffer and returns length of the path without '\0'.
261 * Otherwise function returns required size including '\0' character and
262 * does not touch the buffer.
264 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
266 UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_Windows, -1, NULL, 0, NULL, NULL );
267 if (path && count >= len)
269 WideCharToMultiByte( CP_ACP, 0, DIR_Windows, -1, path, count, NULL, NULL );
270 len--;
272 return len;
276 /***********************************************************************
277 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
279 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
281 return GetWindowsDirectoryA( path, count );
285 /***********************************************************************
286 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
288 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
290 return GetWindowsDirectoryW( path, count );
294 /***********************************************************************
295 * GetSystemDirectoryW (KERNEL32.@)
297 * See comment for GetWindowsDirectoryA.
299 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
301 UINT len = strlenW( DIR_System ) + 1;
302 if (path && count >= len)
304 strcpyW( path, DIR_System );
305 len--;
307 return len;
311 /***********************************************************************
312 * GetSystemDirectoryA (KERNEL32.@)
314 * See comment for GetWindowsDirectoryA.
316 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
318 UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_System, -1, NULL, 0, NULL, NULL );
319 if (path && count >= len)
321 WideCharToMultiByte( CP_ACP, 0, DIR_System, -1, path, count, NULL, NULL );
322 len--;
324 return len;