winspool.drv: Avoid bitwise operation.
[wine.git] / dlls / ntdll / loadorder.c
blob401d256f1c3c7b879f290cabb941fdf7a524cf5e
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 "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
30 #include "windef.h"
31 #include "winternl.h"
32 #include "ntdll_misc.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(module);
39 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
41 typedef struct module_loadorder
43 const WCHAR *modulename;
44 enum loadorder loadorder;
45 } module_loadorder_t;
47 struct loadorder_list
49 int count;
50 int alloc;
51 module_loadorder_t *order;
54 static const WCHAR separatorsW[] = {',',' ','\t',0};
56 static BOOL init_done;
57 static struct loadorder_list env_list;
60 /***************************************************************************
61 * cmp_sort_func (internal, static)
63 * Sorting and comparing function used in sort and search of loadorder
64 * entries.
66 static int cmp_sort_func(const void *s1, const void *s2)
68 return strcmpiW(((const module_loadorder_t *)s1)->modulename, ((const module_loadorder_t *)s2)->modulename);
72 /***************************************************************************
73 * get_basename
75 * Return the base name of a file name (i.e. remove the path components).
77 static const WCHAR *get_basename( const WCHAR *name )
79 const WCHAR *ptr;
81 if (name[0] && name[1] == ':') name += 2; /* strip drive specification */
82 if ((ptr = strrchrW( name, '\\' ))) name = ptr + 1;
83 if ((ptr = strrchrW( name, '/' ))) name = ptr + 1;
84 return name;
87 /***************************************************************************
88 * remove_dll_ext
90 * Remove extension if it is ".dll".
92 static inline void remove_dll_ext( WCHAR *ext )
94 if (ext[0] == '.' &&
95 toupperW(ext[1]) == 'D' &&
96 toupperW(ext[2]) == 'L' &&
97 toupperW(ext[3]) == 'L' &&
98 !ext[4]) ext[0] = 0;
102 /***************************************************************************
103 * debugstr_loadorder
105 * Return a loadorder in printable form.
107 static const char *debugstr_loadorder( enum loadorder lo )
109 switch(lo)
111 case LO_DISABLED: return "";
112 case LO_NATIVE: return "n";
113 case LO_BUILTIN: return "b";
114 case LO_NATIVE_BUILTIN: return "n,b";
115 case LO_BUILTIN_NATIVE: return "b,n";
116 case LO_DEFAULT: return "default";
117 default: return "??";
122 /***************************************************************************
123 * parse_load_order
125 * Parses the loadorder options from the configuration and puts it into
126 * a structure.
128 static enum loadorder parse_load_order( const WCHAR *order )
130 enum loadorder ret = LO_DISABLED;
132 while (*order)
134 order += strspnW( order, separatorsW );
135 switch(*order)
137 case 'N': /* native */
138 case 'n':
139 if (ret == LO_DISABLED) ret = LO_NATIVE;
140 else if (ret == LO_BUILTIN) return LO_BUILTIN_NATIVE;
141 break;
142 case 'B': /* builtin */
143 case 'b':
144 if (ret == LO_DISABLED) ret = LO_BUILTIN;
145 else if (ret == LO_NATIVE) return LO_NATIVE_BUILTIN;
146 break;
148 order += strcspnW( order, separatorsW );
150 return ret;
154 /***************************************************************************
155 * add_load_order
157 * Adds an entry in the list of environment overrides.
159 static void add_load_order( const module_loadorder_t *plo )
161 int i;
163 for(i = 0; i < env_list.count; i++)
165 if(!cmp_sort_func(plo, &env_list.order[i] ))
167 /* replace existing option */
168 env_list.order[i].loadorder = plo->loadorder;
169 return;
173 if (i >= env_list.alloc)
175 /* No space in current array, make it larger */
176 env_list.alloc += LOADORDER_ALLOC_CLUSTER;
177 if (env_list.order)
178 env_list.order = RtlReAllocateHeap(GetProcessHeap(), 0, env_list.order,
179 env_list.alloc * sizeof(module_loadorder_t));
180 else
181 env_list.order = RtlAllocateHeap(GetProcessHeap(), 0,
182 env_list.alloc * sizeof(module_loadorder_t));
183 if(!env_list.order)
185 MESSAGE("Virtual memory exhausted\n");
186 exit(1);
189 env_list.order[i].loadorder = plo->loadorder;
190 env_list.order[i].modulename = plo->modulename;
191 env_list.count++;
195 /***************************************************************************
196 * add_load_order_set
198 * Adds a set of entries in the list of command-line overrides from the key parameter.
200 static void add_load_order_set( WCHAR *entry )
202 module_loadorder_t ldo;
203 WCHAR *end = strchrW( entry, '=' );
205 if (!end) return;
206 *end++ = 0;
207 ldo.loadorder = parse_load_order( end );
209 while (*entry)
211 entry += strspnW( entry, separatorsW );
212 end = entry + strcspnW( entry, separatorsW );
213 if (*end) *end++ = 0;
214 if (*entry)
216 WCHAR *ext = strrchrW(entry, '.');
217 if (ext) remove_dll_ext( ext );
218 ldo.modulename = entry;
219 add_load_order( &ldo );
220 entry = end;
226 /***************************************************************************
227 * init_load_order
229 static void init_load_order(void)
231 const char *order = getenv( "WINEDLLOVERRIDES" );
232 UNICODE_STRING strW;
233 WCHAR *entry, *next;
235 init_done = TRUE;
236 if (!order) return;
238 if (!strcmp( order, "help" ))
240 MESSAGE( "Syntax:\n"
241 " WINEDLLOVERRIDES=\"entry;entry;entry...\"\n"
242 " where each entry is of the form:\n"
243 " module[,module...]={native|builtin}[,{b|n}]\n"
244 "\n"
245 " Only the first letter of the override (native or builtin)\n"
246 " is significant.\n\n"
247 "Example:\n"
248 " WINEDLLOVERRIDES=\"comdlg32=n,b;shell32,shlwapi=b\"\n" );
249 exit(0);
252 RtlCreateUnicodeStringFromAsciiz( &strW, order );
253 entry = strW.Buffer;
254 while (*entry)
256 while (*entry && *entry == ';') entry++;
257 if (!*entry) break;
258 next = strchrW( entry, ';' );
259 if (next) *next++ = 0;
260 else next = entry + strlenW(entry);
261 add_load_order_set( entry );
262 entry = next;
265 /* sort the array for quick lookup */
266 if (env_list.count)
267 qsort(env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func);
269 /* Note: we don't free the Unicode string because the
270 * stored module names point inside it */
274 /***************************************************************************
275 * get_env_load_order
277 * Get the load order for a given module from the WINEDLLOVERRIDES environment variable.
279 static inline enum loadorder get_env_load_order( const WCHAR *module )
281 module_loadorder_t tmp, *res;
283 tmp.modulename = module;
284 /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
285 if (env_list.count &&
286 (res = bsearch(&tmp, env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func)))
287 return res->loadorder;
288 return LO_INVALID;
292 /***************************************************************************
293 * get_standard_key
295 * Return a handle to the standard DllOverrides registry section.
297 static HANDLE get_standard_key(void)
299 static const WCHAR DllOverridesW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
300 'D','l','l','O','v','e','r','r','i','d','e','s',0};
301 static HANDLE std_key = (HANDLE)-1;
303 if (std_key == (HANDLE)-1)
305 OBJECT_ATTRIBUTES attr;
306 UNICODE_STRING nameW;
307 HANDLE root;
309 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
310 attr.Length = sizeof(attr);
311 attr.RootDirectory = root;
312 attr.ObjectName = &nameW;
313 attr.Attributes = 0;
314 attr.SecurityDescriptor = NULL;
315 attr.SecurityQualityOfService = NULL;
316 RtlInitUnicodeString( &nameW, DllOverridesW );
318 /* @@ Wine registry key: HKCU\Software\Wine\DllOverrides */
319 if (NtOpenKey( &std_key, KEY_ALL_ACCESS, &attr )) std_key = 0;
320 NtClose( root );
322 return std_key;
326 /***************************************************************************
327 * get_app_key
329 * Get the registry key for the app-specific DllOverrides list.
331 static HANDLE get_app_key( const WCHAR *app_name )
333 OBJECT_ATTRIBUTES attr;
334 UNICODE_STRING nameW;
335 HANDLE root;
336 WCHAR *str;
337 static const WCHAR AppDefaultsW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
338 'A','p','p','D','e','f','a','u','l','t','s','\\',0};
339 static const WCHAR DllOverridesW[] = {'\\','D','l','l','O','v','e','r','r','i','d','e','s',0};
340 static HANDLE app_key = (HANDLE)-1;
342 if (app_key != (HANDLE)-1) return app_key;
344 str = RtlAllocateHeap( GetProcessHeap(), 0,
345 sizeof(AppDefaultsW) + sizeof(DllOverridesW) +
346 strlenW(app_name) * sizeof(WCHAR) );
347 if (!str) return 0;
348 strcpyW( str, AppDefaultsW );
349 strcatW( str, app_name );
350 strcatW( str, DllOverridesW );
352 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
353 attr.Length = sizeof(attr);
354 attr.RootDirectory = root;
355 attr.ObjectName = &nameW;
356 attr.Attributes = 0;
357 attr.SecurityDescriptor = NULL;
358 attr.SecurityQualityOfService = NULL;
359 RtlInitUnicodeString( &nameW, str );
361 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DllOverrides */
362 if (NtOpenKey( &app_key, KEY_ALL_ACCESS, &attr )) app_key = 0;
363 NtClose( root );
364 RtlFreeHeap( GetProcessHeap(), 0, str );
365 return app_key;
369 /***************************************************************************
370 * get_registry_value
372 * Load the registry loadorder value for a given module.
374 static enum loadorder get_registry_value( HANDLE hkey, const WCHAR *module )
376 UNICODE_STRING valueW;
377 char buffer[80];
378 DWORD count;
380 RtlInitUnicodeString( &valueW, module );
382 if (!NtQueryValueKey( hkey, &valueW, KeyValuePartialInformation,
383 buffer, sizeof(buffer), &count ))
385 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
386 return parse_load_order( str );
388 return LO_INVALID;
392 /***************************************************************************
393 * get_load_order_value
395 * Get the load order for the exact specified module string, looking in:
396 * 1. The WINEDLLOVERRIDES environment variable
397 * 2. The per-application DllOverrides key
398 * 3. The standard DllOverrides key
400 static enum loadorder get_load_order_value( HANDLE std_key, HANDLE app_key, const WCHAR *module )
402 enum loadorder ret;
404 if ((ret = get_env_load_order( module )) != LO_INVALID)
406 TRACE( "got environment %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
407 return ret;
410 if (app_key && ((ret = get_registry_value( app_key, module )) != LO_INVALID))
412 TRACE( "got app defaults %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
413 return ret;
416 if (std_key && ((ret = get_registry_value( std_key, module )) != LO_INVALID))
418 TRACE( "got standard key %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
419 return ret;
422 return ret;
426 /***************************************************************************
427 * get_load_order (internal)
429 * Return the loadorder of a module.
430 * The system directory and '.dll' extension is stripped from the path.
432 enum loadorder get_load_order( const WCHAR *app_name, const WCHAR *path )
434 enum loadorder ret = LO_INVALID;
435 HANDLE std_key, app_key = 0;
436 WCHAR *module, *basename;
437 UNICODE_STRING path_str;
438 int len;
440 if (!init_done) init_load_order();
441 std_key = get_standard_key();
442 if (app_name) app_key = get_app_key( app_name );
444 TRACE("looking for %s\n", debugstr_w(path));
446 /* Strip path information if the module resides in the system directory
448 RtlInitUnicodeString( &path_str, path );
449 if (RtlPrefixUnicodeString( &system_dir, &path_str, TRUE ))
451 const WCHAR *p = path + system_dir.Length / sizeof(WCHAR);
452 while (*p == '\\' || *p == '/') p++;
453 if (!strchrW( p, '\\' ) && !strchrW( p, '/' )) path = p;
456 if (!(len = strlenW(path))) return ret;
457 if (!(module = RtlAllocateHeap( GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR) ))) return ret;
458 strcpyW( module+1, path ); /* reserve module[0] for the wildcard char */
459 basename = (WCHAR *)get_basename( module+1 );
461 if (len >= 4) remove_dll_ext( module + 1 + len - 4 );
463 /* first explicit module name */
464 if ((ret = get_load_order_value( std_key, app_key, module+1 )) != LO_INVALID)
465 goto done;
467 /* then module basename preceded by '*' */
468 basename[-1] = '*';
469 if ((ret = get_load_order_value( std_key, app_key, basename-1 )) != LO_INVALID)
470 goto done;
472 /* then module basename without '*' (only if explicit path) */
473 if (basename != module+1 && ((ret = get_load_order_value( std_key, app_key, basename )) != LO_INVALID))
474 goto done;
476 /* if loading the main exe with an explicit path, try native first */
477 if (!app_name && basename != module+1)
479 ret = LO_NATIVE_BUILTIN;
480 TRACE( "got main exe default %s for %s\n", debugstr_loadorder(ret), debugstr_w(path) );
481 goto done;
484 /* and last the hard-coded default */
485 ret = LO_DEFAULT;
486 TRACE( "got hardcoded %s for %s\n", debugstr_loadorder(ret), debugstr_w(path) );
488 done:
489 RtlFreeHeap( GetProcessHeap(), 0, module );
490 return ret;