Implement NetBIOS resolution for UNC pathnames.
[wine/multimedia.git] / loader / loadorder.c
blob171708a07a9c3af534e573804c6aa54e65b22916
1 /*
2 * Module/Library loadorder
4 * Copyright 1999 Bertho Stultiens
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 <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
27 #include "windef.h"
28 #include "winreg.h"
29 #include "winerror.h"
30 #include "options.h"
31 #include "file.h"
32 #include "module.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 char *modulename;
42 enum loadorder_type loadorder[LOADORDER_NTYPES];
43 } module_loadorder_t;
45 struct loadorder_list
47 int count;
48 int alloc;
49 module_loadorder_t *order;
52 /* default load-order if nothing specified */
53 /* the list must remain sorted by dll name */
54 static module_loadorder_t default_order_list[] =
56 { "display", { LOADORDER_BI, 0, 0, 0 } },
57 { "gdi.exe", { LOADORDER_BI, 0, 0, 0 } },
58 { "gdi32", { LOADORDER_BI, 0, 0, 0 } },
59 { "glide2x", { LOADORDER_SO, LOADORDER_DLL, 0, 0 } },
60 { "glide3x", { LOADORDER_SO, LOADORDER_DLL, 0, 0 } },
61 { "icmp", { LOADORDER_BI, 0, 0, 0 } },
62 { "kernel", { LOADORDER_BI, 0, 0, 0 } },
63 { "kernel32", { LOADORDER_BI, 0, 0, 0 } },
64 { "keyboard", { LOADORDER_BI, 0, 0, 0 } },
65 { "krnl386.exe", { LOADORDER_BI, 0, 0, 0 } },
66 { "mmsystem", { LOADORDER_BI, 0, 0, 0 } },
67 { "mouse", { LOADORDER_BI, 0, 0, 0 } },
68 { "ntdll", { LOADORDER_BI, 0, 0, 0 } },
69 { "odbc32", { LOADORDER_BI, 0, 0, 0 } },
70 { "system", { LOADORDER_BI, 0, 0, 0 } },
71 { "toolhelp", { LOADORDER_BI, 0, 0, 0 } },
72 { "ttydrv", { LOADORDER_BI, 0, 0, 0 } },
73 { "user.exe", { LOADORDER_BI, 0, 0, 0 } },
74 { "user32", { LOADORDER_BI, 0, 0, 0 } },
75 { "w32skrnl", { LOADORDER_BI, 0, 0, 0 } },
76 { "winaspi", { LOADORDER_BI, 0, 0, 0 } },
77 { "windebug", { LOADORDER_DLL, LOADORDER_BI, 0, 0 } },
78 { "winedos", { LOADORDER_BI, 0, 0, 0 } },
79 { "wineps", { LOADORDER_BI, 0, 0, 0 } },
80 { "wing", { LOADORDER_BI, 0, 0, 0 } },
81 { "winmm", { LOADORDER_BI, 0, 0, 0 } },
82 { "winsock", { LOADORDER_BI, 0, 0, 0 } },
83 { "wnaspi32", { LOADORDER_BI, 0, 0, 0 } },
84 { "wow32", { LOADORDER_BI, 0, 0, 0 } },
85 { "wprocs", { LOADORDER_BI, 0, 0, 0 } },
86 { "ws2_32", { LOADORDER_BI, 0, 0, 0 } },
87 { "wsock32", { LOADORDER_BI, 0, 0, 0 } },
88 { "x11drv", { LOADORDER_BI, 0, 0, 0 } }
91 static const struct loadorder_list default_list =
93 sizeof(default_order_list)/sizeof(default_order_list[0]),
94 sizeof(default_order_list)/sizeof(default_order_list[0]),
95 default_order_list
98 static struct loadorder_list cmdline_list;
101 /***************************************************************************
102 * cmp_sort_func (internal, static)
104 * Sorting and comparing function used in sort and search of loadorder
105 * entries.
107 static int cmp_sort_func(const void *s1, const void *s2)
109 return FILE_strcasecmp(((module_loadorder_t *)s1)->modulename,
110 ((module_loadorder_t *)s2)->modulename);
114 /***************************************************************************
115 * get_tok (internal, static)
117 * strtok wrapper for non-destructive buffer writing.
118 * NOTE: strtok is not reentrant and therefore this code is neither.
120 static char *get_tok(const char *str, const char *delim)
122 static char *buf = NULL;
123 char *cptr;
125 if(!str && !buf)
126 return NULL;
128 if(str && buf)
130 HeapFree(GetProcessHeap(), 0, buf);
131 buf = NULL;
134 if(str && !buf)
136 buf = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
137 strcpy( buf, str );
138 cptr = strtok(buf, delim);
140 else
142 cptr = strtok(NULL, delim);
145 if(!cptr)
147 HeapFree(GetProcessHeap(), 0, buf);
148 buf = NULL;
150 return cptr;
154 /***************************************************************************
155 * ParseLoadOrder (internal, static)
157 * Parses the loadorder options from the configuration and puts it into
158 * a structure.
160 static BOOL ParseLoadOrder(char *order, enum loadorder_type lo[])
162 static int warn;
163 char *cptr;
164 int n = 0;
166 cptr = get_tok(order, ", \t");
167 while(cptr)
169 enum loadorder_type type = LOADORDER_INVALID;
171 if(n >= LOADORDER_NTYPES-1)
173 ERR("More than existing %d module-types specified, rest ignored\n", LOADORDER_NTYPES-1);
174 break;
177 switch(*cptr)
179 case 'N': /* Native */
180 case 'n': type = LOADORDER_DLL; break;
182 case 'E': /* Elfdll */
183 case 'e':
184 if (!warn++) MESSAGE("Load order 'elfdll' no longer supported, ignored\n");
185 break;
186 case 'S': /* So */
187 case 's': type = LOADORDER_SO; break;
189 case 'B': /* Builtin */
190 case 'b': type = LOADORDER_BI; break;
192 default:
193 ERR("Invalid load order module-type '%s', ignored\n", cptr);
196 if(type != LOADORDER_INVALID) lo[n++] = type;
197 cptr = get_tok(NULL, ", \t");
199 lo[n] = LOADORDER_INVALID;
200 return TRUE;
204 /***************************************************************************
205 * AddLoadOrder (internal, static)
207 * Adds an entry in the list of command-line overrides.
209 static BOOL AddLoadOrder(module_loadorder_t *plo)
211 int i;
213 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
215 for(i = 0; i < cmdline_list.count; i++)
217 if(!cmp_sort_func(plo, &cmdline_list.order[i] ))
219 /* replace existing option */
220 memcpy( cmdline_list.order[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
221 return TRUE;
225 if (i >= cmdline_list.alloc)
227 /* No space in current array, make it larger */
228 cmdline_list.alloc += LOADORDER_ALLOC_CLUSTER;
229 cmdline_list.order = HeapReAlloc(GetProcessHeap(), 0, cmdline_list.order,
230 cmdline_list.alloc * sizeof(module_loadorder_t));
231 if(!cmdline_list.order)
233 MESSAGE("Virtual memory exhausted\n");
234 exit(1);
237 memcpy(cmdline_list.order[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
238 cmdline_list.order[i].modulename = HeapAlloc(GetProcessHeap(), 0, strlen(plo->modulename)+1);
239 strcpy( (char *)cmdline_list.order[i].modulename, plo->modulename );
240 cmdline_list.count++;
241 return TRUE;
245 /***************************************************************************
246 * AddLoadOrderSet (internal, static)
248 * Adds a set of entries in the list of command-line overrides from the key parameter.
250 static BOOL AddLoadOrderSet(char *key, char *order)
252 module_loadorder_t ldo;
253 char *cptr;
255 /* Parse the loadorder before the rest because strtok is not reentrant */
256 if(!ParseLoadOrder(order, ldo.loadorder))
257 return FALSE;
259 cptr = get_tok(key, ", \t");
260 while(cptr)
262 char *ext = strrchr(cptr, '.');
263 if(ext && !FILE_strcasecmp( ext, ".dll" )) *ext = 0;
264 ldo.modulename = cptr;
265 if(!AddLoadOrder(&ldo)) return FALSE;
266 cptr = get_tok(NULL, ", \t");
268 return TRUE;
272 /***************************************************************************
273 * MODULE_AddLoadOrderOption
275 * The commandline option is in the form:
276 * name[,name,...]=native[,b,...]
278 void MODULE_AddLoadOrderOption( const char *option )
280 char *value, *key = HeapAlloc(GetProcessHeap(), 0, strlen(option)+1);
282 strcpy( key, option );
283 if (!(value = strchr(key, '='))) goto error;
284 *value++ = '\0';
286 TRACE("Commandline override '%s' = '%s'\n", key, value);
288 if (!AddLoadOrderSet(key, value)) goto error;
289 HeapFree(GetProcessHeap(), 0, key);
291 /* sort the array for quick lookup */
292 qsort(cmdline_list.order, cmdline_list.count, sizeof(cmdline_list.order[0]), cmp_sort_func);
293 return;
295 error:
296 MESSAGE( "Syntax: -dll name[,name[,...]]={native|so|builtin}[,{n|s|b}[,...]]\n"
297 " - 'name' is the name of any dll without extension\n"
298 " - the order of loading (native, so and builtin) can be abbreviated\n"
299 " with the first letter\n"
300 " - the option can be specified multiple times\n"
301 " Example:\n"
302 " -dll comdlg32,commdlg=n -dll shell,shell32=b\n" );
303 ExitProcess(1);
307 /***************************************************************************
308 * set_registry_keys
310 * Set individual registry keys for a multiple dll specification
311 * Helper for MODULE_InitLoadOrder().
313 inline static void set_registry_keys( HKEY hkey, char *module, const char *buffer )
315 static int warn;
316 char *p = get_tok( module, ", \t" );
318 TRACE( "converting \"%s\" = \"%s\"\n", module, buffer );
320 if (!warn)
321 MESSAGE( "Warning: setting multiple modules in a single DllOverrides entry is no longer\n"
322 "recommended. It is suggested that you rewrite the configuration file entry:\n\n"
323 "\"%s\" = \"%s\"\n\n"
324 "into something like:\n\n", module, buffer );
325 while (p)
327 if (!warn) MESSAGE( "\"%s\" = \"%s\"\n", p, buffer );
328 /* only set it if not existing already */
329 if (RegQueryValueExA( hkey, p, 0, NULL, NULL, NULL ) == ERROR_FILE_NOT_FOUND)
330 RegSetValueExA( hkey, p, 0, REG_SZ, buffer, strlen(buffer)+1 );
331 p = get_tok( NULL, ", \t" );
333 if (!warn) MESSAGE( "\n" );
334 warn = 1;
338 /***************************************************************************
339 * MODULE_InitLoadOrder
341 * Convert entries containing multiple dll names (old syntax) to the
342 * new one dll module per entry syntax
344 void MODULE_InitLoadOrder(void)
346 char module[80];
347 char buffer[1024];
348 char *p;
349 HKEY hkey;
350 DWORD index = 0;
352 if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\DllOverrides", &hkey ))
353 return;
355 for (;;)
357 DWORD type, count = sizeof(buffer), name_len = sizeof(module);
359 if (RegEnumValueA( hkey, index, module, &name_len, NULL, &type, buffer, &count )) break;
360 p = module;
361 while (isspace(*p)) p++;
362 p += strcspn( p, ", \t" );
363 while (isspace(*p)) p++;
364 if (*p)
366 RegDeleteValueA( hkey, module );
367 set_registry_keys( hkey, module, buffer );
369 else index++;
371 RegCloseKey( hkey );
375 /***************************************************************************
376 * get_list_load_order
378 * Get the load order for a given module from the command-line or
379 * default lists.
381 static BOOL get_list_load_order( const char *module, const struct loadorder_list *list,
382 enum loadorder_type lo[] )
384 module_loadorder_t tmp, *res = NULL;
386 tmp.modulename = module;
387 /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
388 if (list->count && (res = bsearch(&tmp, list->order, list->count, sizeof(list->order[0]), cmp_sort_func)))
389 memcpy( lo, res->loadorder, sizeof(res->loadorder) );
390 return (res != NULL);
394 /***************************************************************************
395 * get_app_load_order
397 * Get the load order for a given module from the app-specific DllOverrides list.
398 * Also look for default '*' key if no module key found.
400 static BOOL get_app_load_order( const char *module, enum loadorder_type lo[], BOOL *got_default )
402 HKEY hkey, appkey;
403 DWORD count, type, res;
404 char buffer[MAX_PATH+16], *appname, *p;
406 if (!GetModuleFileName16( GetCurrentTask(), buffer, MAX_PATH ) &&
407 !GetModuleFileNameA( 0, buffer, MAX_PATH ))
409 WARN( "could not get module file name loading %s\n", module );
410 return FALSE;
412 appname = buffer;
413 if ((p = strrchr( appname, '/' ))) appname = p + 1;
414 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
416 TRACE( "searching '%s' in AppDefaults\\%s\\DllOverrides\n", module, appname );
418 if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &hkey ))
419 return FALSE;
421 /* open AppDefaults\\appname\\DllOverrides key */
422 strcat( appname, "\\DllOverrides" );
423 res = RegOpenKeyA( hkey, appname, &appkey );
424 RegCloseKey( hkey );
425 if (res) return FALSE;
427 count = sizeof(buffer);
428 if ((res = RegQueryValueExA( appkey, module, NULL, &type, buffer, &count )))
430 if (!(res = RegQueryValueExA( appkey, "*", NULL, &type, buffer, &count )))
431 *got_default = TRUE;
433 else TRACE( "got app loadorder '%s' for '%s'\n", buffer, module );
434 RegCloseKey( appkey );
435 if (res) return FALSE;
436 return ParseLoadOrder( buffer, lo );
440 /***************************************************************************
441 * get_standard_load_order
443 * Get the load order for a given module from the main DllOverrides list
444 * Also look for default '*' key if no module key found.
446 static BOOL get_standard_load_order( const char *module, enum loadorder_type lo[],
447 BOOL *got_default )
449 HKEY hkey;
450 DWORD count, type, res;
451 char buffer[80];
453 if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\DllOverrides", &hkey ))
454 return FALSE;
456 count = sizeof(buffer);
457 if ((res = RegQueryValueExA( hkey, module, NULL, &type, buffer, &count )))
459 if (!(res = RegQueryValueExA( hkey, "*", NULL, &type, buffer, &count )))
460 *got_default = TRUE;
462 else TRACE( "got standard loadorder '%s' for '%s'\n", buffer, module );
463 RegCloseKey( hkey );
464 if (res) return FALSE;
465 return ParseLoadOrder( buffer, lo );
469 /***************************************************************************
470 * get_default_load_order
472 * Get the default load order if nothing specified for a given dll.
474 static void get_default_load_order( enum loadorder_type lo[] )
476 DWORD res;
477 static enum loadorder_type default_loadorder[LOADORDER_NTYPES];
478 static int loaded;
480 if (!loaded)
482 char buffer[80];
483 HKEY hkey;
485 if (!(res = RegOpenKeyA( HKEY_LOCAL_MACHINE,
486 "Software\\Wine\\Wine\\Config\\DllDefaults", &hkey )))
488 DWORD type, count = sizeof(buffer);
490 res = RegQueryValueExA( hkey, "DefaultLoadOrder", NULL, &type, buffer, &count );
491 RegCloseKey( hkey );
493 if (res) strcpy( buffer, "n,b,s" );
494 ParseLoadOrder( buffer, default_loadorder );
495 loaded = 1;
496 TRACE( "got default loadorder '%s'\n", buffer );
498 memcpy( lo, default_loadorder, sizeof(default_loadorder) );
502 /***************************************************************************
503 * MODULE_GetLoadOrder (internal)
505 * Locate the loadorder of a module.
506 * Any path is stripped from the path-argument and so are the extension
507 * '.dll' and '.exe'. A lookup in the table can yield an override for
508 * the specific dll. Otherwise the default load order is returned.
510 void MODULE_GetLoadOrder( enum loadorder_type loadorder[], const char *path, BOOL win32 )
512 char fname[256];
513 char sysdir[MAX_PATH+1];
514 char *cptr;
515 char *name;
516 int len;
517 BOOL got_app_default = FALSE, got_std_default = FALSE;
518 enum loadorder_type lo_default[LOADORDER_NTYPES];
520 TRACE("looking for %s\n", path);
522 if ( ! GetSystemDirectoryA ( sysdir, MAX_PATH ) ) goto done;
524 /* Strip path information for 16 bit modules or if the module
525 resides in the system directory */
526 if ( !win32 || !FILE_strncasecmp ( sysdir, path, strlen (sysdir) ) )
529 cptr = strrchr(path, '\\');
530 if(!cptr)
531 name = strrchr(path, '/');
532 else
533 name = strrchr(cptr, '/');
535 if(!name)
536 name = cptr ? cptr+1 : (char *)path;
537 else
538 name++;
540 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
541 name = cptr+1;
543 else
544 name = (char *)path;
546 len = strlen(name);
547 if(len >= sizeof(fname) || len <= 0)
549 WARN("Path '%s' -> '%s' reduces to zilch or just too large...\n", path, name);
550 goto done;
553 strcpy(fname, name);
554 if(len >= 4 && !FILE_strcasecmp(fname+len-4, ".dll")) fname[len-4] = '\0';
556 /* check command-line first */
557 if (get_list_load_order( fname, &cmdline_list, loadorder )) return;
559 /* then app-specific config */
560 if (get_app_load_order( fname, loadorder, &got_app_default ))
562 if (!got_app_default) return;
563 /* save the default value for later on */
564 memcpy( lo_default, loadorder, sizeof(lo_default) );
567 /* then standard config */
568 if (get_standard_load_order( fname, loadorder, &got_std_default ))
570 if (!got_std_default) return;
571 /* save the default value for later on */
572 if (!got_app_default) memcpy( lo_default, loadorder, sizeof(lo_default) );
575 /* then compiled-in defaults */
576 if (get_list_load_order( fname, &default_list, loadorder )) return;
578 done:
579 /* last, return the default */
580 if (got_app_default || got_std_default)
581 memcpy( loadorder, lo_default, sizeof(lo_default) );
582 else
583 get_default_load_order( loadorder );