Removed a number of internal file functions that are no longer used.
[wine] / files / directory.c
1 /*
2  * DOS directories functions
3  *
4  * Copyright 1995 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
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
37
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"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(dosfs);
52 WINE_DECLARE_DEBUG_CHANNEL(file);
53
54 static WCHAR *DIR_Windows;
55 static WCHAR *DIR_System;
56
57 /***********************************************************************
58  *           DIR_GetPath
59  *
60  * Get a path name from the wine.ini file and make sure it is valid.
61  */
62 static WCHAR *DIR_GetPath( HKEY hkey, LPCWSTR keyname, LPCWSTR defval, BOOL warn )
63 {
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;
71
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;
77
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
82     {
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;
87     }
88
89     if (warn)
90     {
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());
95     }
96     return NULL;
97 }
98
99
100 /***********************************************************************
101  *           DIR_Init
102  */
103 int DIR_Init(void)
104 {
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 };
137
138     if (!getcwd( path, MAX_PATHNAME_LEN ))
139     {
140         perror( "Could not get current directory" );
141         return 0;
142     }
143     cwd = path;
144     if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
145     {
146         MESSAGE("Warning: could not find wine config [Drive x] entry "
147             "for current working directory %s; "
148             "starting in windows directory.\n", cwd );
149     }
150     else
151     {
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 */
158     }
159
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;
166
167     RtlInitUnicodeString( &nameW, wineW );
168     if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) hkey = 0;
169
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 )))
173     {
174         if (hkey) NtClose( hkey );
175         return 0;
176     }
177
178     if (drive == -1) SetCurrentDirectoryW( DIR_Windows );
179
180     /* Set the environment variables */
181
182     /* set COMSPEC only if it doesn't exist already */
183     if (!GetEnvironmentVariableW( comspecW, NULL, 0 ))
184     {
185         strcpyW( longpath, DIR_System );
186         strcatW( longpath, wcmdW );
187         SetEnvironmentVariableW( comspecW, longpath );
188     }
189
190     /* set PATH only if not set already */
191     if (!GetEnvironmentVariableW( path_capsW, NULL, 0 ))
192     {
193         WCHAR tmp[MAX_PATHNAME_LEN];
194         DWORD dummy;
195         const WCHAR *path = path_dirW;
196
197         RtlInitUnicodeString( &nameW, pathW );
198         if (hkey && !NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
199                                       tmp, sizeof(tmp), &dummy ))
200         {
201             path = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
202         }
203
204         if (strchrW(path, '/'))
205         {
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);
208         }
209         SetEnvironmentVariableW( path_capsW, path );
210         TRACE("Path       = %s\n", debugstr_w(path) );
211     }
212
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 );
217
218     SetEnvironmentVariableW( windirW, DIR_Windows );
219     SetEnvironmentVariableW( systemrootW, DIR_Windows );
220     SetEnvironmentVariableW( winsysdirW, DIR_System );
221
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
227     HeapFree( GetProcessHeap(), 0, tmp_dir );
228
229     if ((profile_dir = DIR_GetPath( hkey, profileW, empty_strW, FALSE )))
230     {
231         TRACE("USERPROFILE= %s\n", debugstr_w(profile_dir) );
232         SetEnvironmentVariableW( userprofileW, profile_dir );
233         HeapFree( GetProcessHeap(), 0, profile_dir );
234     }
235
236     if (hkey) NtClose( hkey );
237
238     return 1;
239 }
240
241
242 /***********************************************************************
243  *           GetWindowsDirectoryW   (KERNEL32.@)
244  *
245  * See comment for GetWindowsDirectoryA.
246  */
247 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
248 {
249     UINT len = strlenW( DIR_Windows ) + 1;
250     if (path && count >= len)
251     {
252         strcpyW( path, DIR_Windows );
253         len--;
254     }
255     return len;
256 }
257
258
259 /***********************************************************************
260  *           GetWindowsDirectoryA   (KERNEL32.@)
261  *
262  * Return value:
263  * If buffer is large enough to hold full path and terminating '\0' character
264  * function copies path to buffer and returns length of the path without '\0'.
265  * Otherwise function returns required size including '\0' character and
266  * does not touch the buffer.
267  */
268 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
269 {
270     UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_Windows, -1, NULL, 0, NULL, NULL );
271     if (path && count >= len)
272     {
273         WideCharToMultiByte( CP_ACP, 0, DIR_Windows, -1, path, count, NULL, NULL );
274         len--;
275     }
276     return len;
277 }
278
279
280 /***********************************************************************
281  *           GetSystemWindowsDirectoryA   (KERNEL32.@) W2K, TS4.0SP4
282  */
283 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
284 {
285     return GetWindowsDirectoryA( path, count );
286 }
287
288
289 /***********************************************************************
290  *           GetSystemWindowsDirectoryW   (KERNEL32.@) W2K, TS4.0SP4
291  */
292 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
293 {
294     return GetWindowsDirectoryW( path, count );
295 }
296
297
298 /***********************************************************************
299  *           GetSystemDirectoryW   (KERNEL32.@)
300  *
301  * See comment for GetWindowsDirectoryA.
302  */
303 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
304 {
305     UINT len = strlenW( DIR_System ) + 1;
306     if (path && count >= len)
307     {
308         strcpyW( path, DIR_System );
309         len--;
310     }
311     return len;
312 }
313
314
315 /***********************************************************************
316  *           GetSystemDirectoryA   (KERNEL32.@)
317  *
318  * See comment for GetWindowsDirectoryA.
319  */
320 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
321 {
322     UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_System, -1, NULL, 0, NULL, NULL );
323     if (path && count >= len)
324     {
325         WideCharToMultiByte( CP_ACP, 0, DIR_System, -1, path, count, NULL, NULL );
326         len--;
327     }
328     return len;
329 }