makefiles: Explicitly create destination dirs when installing symlinks.
[wine/zf.git] / dlls / ntdll / loadorder.c
blobc55806df2b82d45a0a9cc09d7ae18412e0e3a40a
1 /*
2 * Dlls load order support
4 * Copyright 1999 Bertho Stultiens
5 * Copyright 2003 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winternl.h"
31 #include "ntdll_misc.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(module);
37 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
39 typedef struct module_loadorder
41 const WCHAR *modulename;
42 enum loadorder loadorder;
43 } module_loadorder_t;
45 struct loadorder_list
47 int count;
48 int alloc;
49 module_loadorder_t *order;
52 static const WCHAR separatorsW[] = {',',' ','\t',0};
54 static BOOL init_done;
55 static struct loadorder_list env_list;
58 /***************************************************************************
59 * cmp_sort_func (internal, static)
61 * Sorting and comparing function used in sort and search of loadorder
62 * entries.
64 static int __cdecl cmp_sort_func(const void *s1, const void *s2)
66 return wcsicmp(((const module_loadorder_t *)s1)->modulename, ((const module_loadorder_t *)s2)->modulename);
70 /***************************************************************************
71 * get_basename
73 * Return the base name of a file name (i.e. remove the path components).
75 static const WCHAR *get_basename( const WCHAR *name )
77 const WCHAR *ptr;
79 if (name[0] && name[1] == ':') name += 2; /* strip drive specification */
80 if ((ptr = wcsrchr( name, '\\' ))) name = ptr + 1;
81 if ((ptr = wcsrchr( name, '/' ))) name = ptr + 1;
82 return name;
85 /***************************************************************************
86 * remove_dll_ext
88 * Remove extension if it is ".dll".
90 static inline void remove_dll_ext( WCHAR *name )
92 static const WCHAR dllW[] = {'.','d','l','l',0};
93 WCHAR *p = wcsrchr( name, '.' );
95 if (p && !wcsicmp( p, dllW )) *p = 0;
99 /***************************************************************************
100 * debugstr_loadorder
102 * Return a loadorder in printable form.
104 static const char *debugstr_loadorder( enum loadorder lo )
106 switch(lo)
108 case LO_DISABLED: return "";
109 case LO_NATIVE: return "n";
110 case LO_BUILTIN: return "b";
111 case LO_NATIVE_BUILTIN: return "n,b";
112 case LO_BUILTIN_NATIVE: return "b,n";
113 case LO_DEFAULT: return "default";
114 default: return "??";
119 /***************************************************************************
120 * parse_load_order
122 * Parses the loadorder options from the configuration and puts it into
123 * a structure.
125 static enum loadorder parse_load_order( const WCHAR *order )
127 enum loadorder ret = LO_DISABLED;
129 while (*order)
131 order += wcsspn( order, separatorsW );
132 switch(*order)
134 case 'N': /* native */
135 case 'n':
136 if (ret == LO_DISABLED) ret = LO_NATIVE;
137 else if (ret == LO_BUILTIN) return LO_BUILTIN_NATIVE;
138 break;
139 case 'B': /* builtin */
140 case 'b':
141 if (ret == LO_DISABLED) ret = LO_BUILTIN;
142 else if (ret == LO_NATIVE) return LO_NATIVE_BUILTIN;
143 break;
145 order += wcscspn( order, separatorsW );
147 return ret;
151 /***************************************************************************
152 * add_load_order
154 * Adds an entry in the list of environment overrides.
156 static void add_load_order( const module_loadorder_t *plo )
158 int i;
160 for(i = 0; i < env_list.count; i++)
162 if(!cmp_sort_func(plo, &env_list.order[i] ))
164 /* replace existing option */
165 env_list.order[i].loadorder = plo->loadorder;
166 return;
170 if (i >= env_list.alloc)
172 /* No space in current array, make it larger */
173 env_list.alloc += LOADORDER_ALLOC_CLUSTER;
174 if (env_list.order)
175 env_list.order = RtlReAllocateHeap(GetProcessHeap(), 0, env_list.order,
176 env_list.alloc * sizeof(module_loadorder_t));
177 else
178 env_list.order = RtlAllocateHeap(GetProcessHeap(), 0,
179 env_list.alloc * sizeof(module_loadorder_t));
180 if(!env_list.order)
182 MESSAGE("Virtual memory exhausted\n");
183 NtTerminateProcess( GetCurrentProcess(), 1 );
186 env_list.order[i].loadorder = plo->loadorder;
187 env_list.order[i].modulename = plo->modulename;
188 env_list.count++;
192 /***************************************************************************
193 * add_load_order_set
195 * Adds a set of entries in the list of command-line overrides from the key parameter.
197 static void add_load_order_set( WCHAR *entry )
199 module_loadorder_t ldo;
200 WCHAR *end = wcschr( entry, '=' );
202 if (!end) return;
203 *end++ = 0;
204 ldo.loadorder = parse_load_order( end );
206 while (*entry)
208 entry += wcsspn( entry, separatorsW );
209 end = entry + wcscspn( entry, separatorsW );
210 if (*end) *end++ = 0;
211 if (*entry)
213 remove_dll_ext( entry );
214 ldo.modulename = entry;
215 add_load_order( &ldo );
216 entry = end;
222 /***************************************************************************
223 * init_load_order
225 static void init_load_order(void)
227 static const WCHAR winedlloverridesW[] = {'W','I','N','E','D','L','L','O','V','E','R','R','I','D','E','S',0};
228 WCHAR *entry, *next, *order;
229 SIZE_T len = 1024;
230 NTSTATUS status;
232 init_done = TRUE;
234 for (;;)
236 order = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR) );
237 status = RtlQueryEnvironmentVariable( NULL, winedlloverridesW, wcslen(winedlloverridesW),
238 order, len - 1, &len );
239 if (!status)
241 order[len] = 0;
242 break;
244 RtlFreeHeap( GetProcessHeap(), 0, order );
245 if (status != STATUS_BUFFER_TOO_SMALL) return;
248 entry = order;
249 while (*entry)
251 while (*entry == ';') entry++;
252 if (!*entry) break;
253 next = wcschr( entry, ';' );
254 if (next) *next++ = 0;
255 else next = entry + wcslen(entry);
256 add_load_order_set( entry );
257 entry = next;
260 /* sort the array for quick lookup */
261 if (env_list.count)
262 qsort(env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func);
264 /* note: we don't free the string because the stored module names point inside it */
268 /***************************************************************************
269 * get_env_load_order
271 * Get the load order for a given module from the WINEDLLOVERRIDES environment variable.
273 static inline enum loadorder get_env_load_order( const WCHAR *module )
275 module_loadorder_t tmp, *res;
277 tmp.modulename = module;
278 /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
279 if (env_list.count &&
280 (res = bsearch(&tmp, env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func)))
281 return res->loadorder;
282 return LO_INVALID;
286 /***************************************************************************
287 * get_standard_key
289 * Return a handle to the standard DllOverrides registry section.
291 static HANDLE get_standard_key(void)
293 static const WCHAR DllOverridesW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
294 'D','l','l','O','v','e','r','r','i','d','e','s',0};
295 static HANDLE std_key = (HANDLE)-1;
297 if (std_key == (HANDLE)-1)
299 OBJECT_ATTRIBUTES attr;
300 UNICODE_STRING nameW;
301 HANDLE root;
303 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
304 attr.Length = sizeof(attr);
305 attr.RootDirectory = root;
306 attr.ObjectName = &nameW;
307 attr.Attributes = 0;
308 attr.SecurityDescriptor = NULL;
309 attr.SecurityQualityOfService = NULL;
310 RtlInitUnicodeString( &nameW, DllOverridesW );
312 /* @@ Wine registry key: HKCU\Software\Wine\DllOverrides */
313 if (NtOpenKey( &std_key, KEY_ALL_ACCESS, &attr )) std_key = 0;
314 NtClose( root );
316 return std_key;
320 /***************************************************************************
321 * get_app_key
323 * Get the registry key for the app-specific DllOverrides list.
325 static HANDLE get_app_key( const WCHAR *app_name )
327 OBJECT_ATTRIBUTES attr;
328 UNICODE_STRING nameW;
329 HANDLE root;
330 WCHAR *str;
331 static const WCHAR AppDefaultsW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
332 'A','p','p','D','e','f','a','u','l','t','s','\\',0};
333 static const WCHAR DllOverridesW[] = {'\\','D','l','l','O','v','e','r','r','i','d','e','s',0};
334 static HANDLE app_key = (HANDLE)-1;
336 if (app_key != (HANDLE)-1) return app_key;
338 str = RtlAllocateHeap( GetProcessHeap(), 0,
339 sizeof(AppDefaultsW) + sizeof(DllOverridesW) +
340 wcslen(app_name) * sizeof(WCHAR) );
341 if (!str) return 0;
342 wcscpy( str, AppDefaultsW );
343 wcscat( str, app_name );
344 wcscat( str, DllOverridesW );
346 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
347 attr.Length = sizeof(attr);
348 attr.RootDirectory = root;
349 attr.ObjectName = &nameW;
350 attr.Attributes = 0;
351 attr.SecurityDescriptor = NULL;
352 attr.SecurityQualityOfService = NULL;
353 RtlInitUnicodeString( &nameW, str );
355 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DllOverrides */
356 if (NtOpenKey( &app_key, KEY_ALL_ACCESS, &attr )) app_key = 0;
357 NtClose( root );
358 RtlFreeHeap( GetProcessHeap(), 0, str );
359 return app_key;
363 /***************************************************************************
364 * get_registry_value
366 * Load the registry loadorder value for a given module.
368 static enum loadorder get_registry_value( HANDLE hkey, const WCHAR *module )
370 UNICODE_STRING valueW;
371 char buffer[80];
372 DWORD count;
374 RtlInitUnicodeString( &valueW, module );
376 if (!NtQueryValueKey( hkey, &valueW, KeyValuePartialInformation,
377 buffer, sizeof(buffer), &count ))
379 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
380 return parse_load_order( str );
382 return LO_INVALID;
386 /***************************************************************************
387 * get_load_order_value
389 * Get the load order for the exact specified module string, looking in:
390 * 1. The WINEDLLOVERRIDES environment variable
391 * 2. The per-application DllOverrides key
392 * 3. The standard DllOverrides key
394 static enum loadorder get_load_order_value( HANDLE std_key, HANDLE app_key, const WCHAR *module )
396 enum loadorder ret;
398 if ((ret = get_env_load_order( module )) != LO_INVALID)
400 TRACE( "got environment %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
401 return ret;
404 if (app_key && ((ret = get_registry_value( app_key, module )) != LO_INVALID))
406 TRACE( "got app defaults %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
407 return ret;
410 if (std_key && ((ret = get_registry_value( std_key, module )) != LO_INVALID))
412 TRACE( "got standard key %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
413 return ret;
416 return ret;
420 /***************************************************************************
421 * get_load_order (internal)
423 * Return the loadorder of a module.
424 * The system directory and '.dll' extension is stripped from the path.
426 enum loadorder get_load_order( const WCHAR *app_name, const UNICODE_STRING *nt_name )
428 static const WCHAR nt_prefixW[] = {'\\','?','?','\\',0};
429 enum loadorder ret = LO_INVALID;
430 HANDLE std_key, app_key = 0;
431 const WCHAR *path = nt_name->Buffer;
432 WCHAR *module, *basename;
433 int len;
435 if (!init_done) init_load_order();
436 std_key = get_standard_key();
437 if (app_name) app_key = get_app_key( app_name );
438 if (!wcsncmp( path, nt_prefixW, 4 )) path += 4;
440 TRACE("looking for %s\n", debugstr_w(path));
442 /* Strip path information if the module resides in the system directory
444 if (!wcsnicmp( system_dir, path, wcslen( system_dir )))
446 const WCHAR *p = path + wcslen( system_dir );
447 while (*p == '\\' || *p == '/') p++;
448 if (!wcschr( p, '\\' ) && !wcschr( p, '/' )) path = p;
451 if (!(len = wcslen(path))) return ret;
452 if (!(module = RtlAllocateHeap( GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR) ))) return ret;
453 wcscpy( module+1, path ); /* reserve module[0] for the wildcard char */
454 remove_dll_ext( module + 1 );
455 basename = (WCHAR *)get_basename( module+1 );
457 /* first explicit module name */
458 if ((ret = get_load_order_value( std_key, app_key, module+1 )) != LO_INVALID)
459 goto done;
461 /* then module basename preceded by '*' */
462 basename[-1] = '*';
463 if ((ret = get_load_order_value( std_key, app_key, basename-1 )) != LO_INVALID)
464 goto done;
466 /* then module basename without '*' (only if explicit path) */
467 if (basename != module+1 && ((ret = get_load_order_value( std_key, app_key, basename )) != LO_INVALID))
468 goto done;
470 /* if loading the main exe with an explicit path, try native first */
471 if (!app_name && basename != module+1)
473 ret = LO_NATIVE_BUILTIN;
474 TRACE( "got main exe default %s for %s\n", debugstr_loadorder(ret), debugstr_w(path) );
475 goto done;
478 /* and last the hard-coded default */
479 ret = LO_DEFAULT;
480 TRACE( "got hardcoded %s for %s\n", debugstr_loadorder(ret), debugstr_w(path) );
482 done:
483 RtlFreeHeap( GetProcessHeap(), 0, module );
484 return ret;