dbghelp: Remove superfluous casts to self.
[wine.git] / dlls / dbghelp / path.c
blobba757a55b0c805f0fd3ef2e9e68f0ea899e8c0dd
1 /*
2 * File path.c - managing path in debugging environments
4 * Copyright (C) 2004,2008, Eric Pouech
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include "dbghelp_private.h"
26 #include "image_private.h"
27 #include "winnls.h"
28 #include "winternl.h"
29 #include "wine/debug.h"
30 #include "wine/heap.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
34 #ifdef __i386__
35 static const WCHAR pe_dir[] = L"\\i386-windows";
36 static const WCHAR so_dir[] = L"\\i386-unix";
37 #elif defined __x86_64__
38 static const WCHAR pe_dir[] = L"\\x86_64-windows";
39 static const WCHAR so_dir[] = L"\\x86_64-unix";
40 #elif defined __arm__
41 static const WCHAR pe_dir[] = L"\\arm-windows";
42 static const WCHAR so_dir[] = L"\\arm-unix";
43 #elif defined __aarch64__
44 static const WCHAR pe_dir[] = L"\\aarch64-windows";
45 static const WCHAR so_dir[] = L"\\aarch64-unix";
46 #else
47 static const WCHAR pe_dir[] = L"";
48 static const WCHAR so_dir[] = L"";
49 #endif
51 static inline BOOL is_sepA(char ch) {return ch == '/' || ch == '\\';}
52 static inline BOOL is_sep(WCHAR ch) {return ch == '/' || ch == '\\';}
54 const char* file_nameA(const char* str)
56 const char* p;
58 for (p = str + strlen(str) - 1; p >= str && !is_sepA(*p); p--);
59 return p + 1;
62 const WCHAR* file_name(const WCHAR* str)
64 const WCHAR* p;
66 for (p = str + lstrlenW(str) - 1; p >= str && !is_sep(*p); p--);
67 return p + 1;
70 static inline void file_pathW(const WCHAR *src, WCHAR *dst)
72 int len;
74 for (len = lstrlenW(src) - 1; (len > 0) && (!is_sep(src[len])); len--);
75 memcpy( dst, src, len * sizeof(WCHAR) );
76 dst[len] = 0;
79 /******************************************************************
80 * FindDebugInfoFile (DBGHELP.@)
83 HANDLE WINAPI FindDebugInfoFile(PCSTR FileName, PCSTR SymbolPath, PSTR DebugFilePath)
85 HANDLE h;
87 h = CreateFileA(FileName, GENERIC_READ, FILE_SHARE_READ, NULL,
88 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
89 if (h == INVALID_HANDLE_VALUE)
91 if (!SearchPathA(SymbolPath, file_nameA(FileName), NULL, MAX_PATH, DebugFilePath, NULL))
92 return NULL;
93 h = CreateFileA(DebugFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
94 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
96 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
99 /******************************************************************
100 * FindDebugInfoFileEx (DBGHELP.@)
103 HANDLE WINAPI FindDebugInfoFileEx(PCSTR FileName, PCSTR SymbolPath,
104 PSTR DebugFilePath,
105 PFIND_DEBUG_FILE_CALLBACK Callback,
106 PVOID CallerData)
108 FIXME("(%s %s %s %p %p): stub\n", debugstr_a(FileName), debugstr_a(SymbolPath),
109 debugstr_a(DebugFilePath), Callback, CallerData);
110 return NULL;
113 /******************************************************************
114 * FindExecutableImageExW (DBGHELP.@)
117 HANDLE WINAPI FindExecutableImageExW(PCWSTR FileName, PCWSTR SymbolPath, PWSTR ImageFilePath,
118 PFIND_EXE_FILE_CALLBACKW Callback, PVOID user)
120 HANDLE h;
122 if (Callback) FIXME("Unsupported callback yet\n");
123 if (!SearchPathW(SymbolPath, FileName, NULL, MAX_PATH, ImageFilePath, NULL))
124 return NULL;
125 h = CreateFileW(ImageFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
126 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
127 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
130 /******************************************************************
131 * FindExecutableImageEx (DBGHELP.@)
134 HANDLE WINAPI FindExecutableImageEx(PCSTR FileName, PCSTR SymbolPath, PSTR ImageFilePath,
135 PFIND_EXE_FILE_CALLBACK Callback, PVOID user)
137 HANDLE h;
139 if (Callback) FIXME("Unsupported callback yet\n");
140 if (!SearchPathA(SymbolPath, FileName, NULL, MAX_PATH, ImageFilePath, NULL))
141 return NULL;
142 h = CreateFileA(ImageFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
143 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
144 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
147 /******************************************************************
148 * FindExecutableImage (DBGHELP.@)
151 HANDLE WINAPI FindExecutableImage(PCSTR FileName, PCSTR SymbolPath, PSTR ImageFilePath)
153 return FindExecutableImageEx(FileName, SymbolPath, ImageFilePath, NULL, NULL);
156 /***********************************************************************
157 * MakeSureDirectoryPathExists (DBGHELP.@)
159 BOOL WINAPI MakeSureDirectoryPathExists(PCSTR DirPath)
161 char path[MAX_PATH];
162 const char *p = DirPath;
163 int n;
165 if (p[0] && p[1] == ':') p += 2;
166 while (*p == '\\') p++; /* skip drive root */
167 while ((p = strchr(p, '\\')) != NULL)
169 n = p - DirPath + 1;
170 memcpy(path, DirPath, n);
171 path[n] = '\0';
172 if( !CreateDirectoryA(path, NULL) &&
173 (GetLastError() != ERROR_ALREADY_EXISTS))
174 return FALSE;
175 p++;
177 if (GetLastError() == ERROR_ALREADY_EXISTS)
178 SetLastError(ERROR_SUCCESS);
180 return TRUE;
183 /******************************************************************
184 * SymMatchFileNameW (DBGHELP.@)
187 BOOL WINAPI SymMatchFileNameW(PCWSTR file, PCWSTR match,
188 PWSTR* filestop, PWSTR* matchstop)
190 PCWSTR fptr;
191 PCWSTR mptr;
193 TRACE("(%s %s %p %p)\n",
194 debugstr_w(file), debugstr_w(match), filestop, matchstop);
196 fptr = file + lstrlenW(file) - 1;
197 mptr = match + lstrlenW(match) - 1;
199 while (fptr >= file && mptr >= match)
201 if (towupper(*fptr) != towupper(*mptr) && !(is_sep(*fptr) && is_sep(*mptr)))
202 break;
203 fptr--; mptr--;
205 if (filestop) *filestop = (PWSTR)fptr;
206 if (matchstop) *matchstop = (PWSTR)mptr;
208 return mptr == match - 1;
211 /******************************************************************
212 * SymMatchFileName (DBGHELP.@)
215 BOOL WINAPI SymMatchFileName(PCSTR file, PCSTR match,
216 PSTR* filestop, PSTR* matchstop)
218 PCSTR fptr;
219 PCSTR mptr;
221 TRACE("(%s %s %p %p)\n", debugstr_a(file), debugstr_a(match), filestop, matchstop);
223 fptr = file + strlen(file) - 1;
224 mptr = match + strlen(match) - 1;
226 while (fptr >= file && mptr >= match)
228 if (toupper(*fptr) != toupper(*mptr) && !(is_sepA(*fptr) && is_sepA(*mptr)))
229 break;
230 fptr--; mptr--;
232 if (filestop) *filestop = (PSTR)fptr;
233 if (matchstop) *matchstop = (PSTR)mptr;
235 return mptr == match - 1;
238 static BOOL do_searchW(PCWSTR file, PWSTR buffer, BOOL recurse,
239 PENUMDIRTREE_CALLBACKW cb, PVOID user)
241 HANDLE h;
242 WIN32_FIND_DATAW fd;
243 unsigned pos;
244 BOOL found = FALSE;
245 static const WCHAR S_AllW[] = {'*','.','*','\0'};
246 static const WCHAR S_DotW[] = {'.','\0'};
247 static const WCHAR S_DotDotW[] = {'.','.','\0'};
249 pos = lstrlenW(buffer);
250 if (pos == 0) return FALSE;
251 if (buffer[pos - 1] != '\\') buffer[pos++] = '\\';
252 lstrcpyW(buffer + pos, S_AllW);
253 if ((h = FindFirstFileW(buffer, &fd)) == INVALID_HANDLE_VALUE)
254 return FALSE;
255 /* doc doesn't specify how the tree is enumerated...
256 * doing a depth first based on, but may be wrong
260 if (!wcscmp(fd.cFileName, S_DotW) || !wcscmp(fd.cFileName, S_DotDotW)) continue;
262 lstrcpyW(buffer + pos, fd.cFileName);
263 if (recurse && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
264 found = do_searchW(file, buffer, TRUE, cb, user);
265 else if (SymMatchFileNameW(buffer, file, NULL, NULL))
267 if (!cb || cb(buffer, user)) found = TRUE;
269 } while (!found && FindNextFileW(h, &fd));
270 if (!found) buffer[--pos] = '\0';
271 FindClose(h);
273 return found;
276 /***********************************************************************
277 * SearchTreeForFileW (DBGHELP.@)
279 BOOL WINAPI SearchTreeForFileW(PCWSTR root, PCWSTR file, PWSTR buffer)
281 TRACE("(%s, %s, %p)\n",
282 debugstr_w(root), debugstr_w(file), buffer);
283 lstrcpyW(buffer, root);
284 return do_searchW(file, buffer, TRUE, NULL, NULL);
287 /***********************************************************************
288 * SearchTreeForFile (DBGHELP.@)
290 BOOL WINAPI SearchTreeForFile(PCSTR root, PCSTR file, PSTR buffer)
292 WCHAR rootW[MAX_PATH];
293 WCHAR fileW[MAX_PATH];
294 WCHAR bufferW[MAX_PATH];
295 BOOL ret;
297 MultiByteToWideChar(CP_ACP, 0, root, -1, rootW, MAX_PATH);
298 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, MAX_PATH);
299 ret = SearchTreeForFileW(rootW, fileW, bufferW);
300 if (ret)
301 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
302 return ret;
305 /******************************************************************
306 * EnumDirTreeW (DBGHELP.@)
310 BOOL WINAPI EnumDirTreeW(HANDLE hProcess, PCWSTR root, PCWSTR file,
311 PWSTR buffer, PENUMDIRTREE_CALLBACKW cb, PVOID user)
313 TRACE("(%p %s %s %p %p %p)\n",
314 hProcess, debugstr_w(root), debugstr_w(file), buffer, cb, user);
316 lstrcpyW(buffer, root);
317 return do_searchW(file, buffer, TRUE, cb, user);
320 /******************************************************************
321 * EnumDirTree (DBGHELP.@)
325 struct enum_dir_treeWA
327 PENUMDIRTREE_CALLBACK cb;
328 void* user;
329 char name[MAX_PATH];
332 static BOOL CALLBACK enum_dir_treeWA(PCWSTR name, PVOID user)
334 struct enum_dir_treeWA* edt = user;
336 WideCharToMultiByte(CP_ACP, 0, name, -1, edt->name, MAX_PATH, NULL, NULL);
337 return edt->cb(edt->name, edt->user);
340 BOOL WINAPI EnumDirTree(HANDLE hProcess, PCSTR root, PCSTR file,
341 PSTR buffer, PENUMDIRTREE_CALLBACK cb, PVOID user)
343 WCHAR rootW[MAX_PATH];
344 WCHAR fileW[MAX_PATH];
345 WCHAR bufferW[MAX_PATH];
346 struct enum_dir_treeWA edt;
347 BOOL ret;
349 edt.cb = cb;
350 edt.user = user;
351 MultiByteToWideChar(CP_ACP, 0, root, -1, rootW, MAX_PATH);
352 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, MAX_PATH);
353 if ((ret = EnumDirTreeW(hProcess, rootW, fileW, bufferW, enum_dir_treeWA, &edt)))
354 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
355 return ret;
358 struct sffip
360 PFINDFILEINPATHCALLBACKW cb;
361 void* user;
364 /* checks that buffer (as found by matching the name) matches the info
365 * (information is based on file type)
366 * returns TRUE when file is found, FALSE to continue searching
367 * (NB this is the opposite convention of SymFindFileInPathProc)
369 static BOOL CALLBACK sffip_cb(PCWSTR buffer, PVOID user)
371 struct sffip* s = user;
373 if (!s->cb) return TRUE;
374 /* yes, EnumDirTree/do_search and SymFindFileInPath callbacks use the opposite
375 * convention to stop/continue enumeration. sigh.
377 return !(s->cb)(buffer, s->user);
380 /******************************************************************
381 * SymFindFileInPathW (DBGHELP.@)
384 BOOL WINAPI SymFindFileInPathW(HANDLE hProcess, PCWSTR searchPath, PCWSTR full_path,
385 PVOID id, DWORD two, DWORD three, DWORD flags,
386 PWSTR buffer, PFINDFILEINPATHCALLBACKW cb,
387 PVOID user)
389 struct sffip s;
390 struct process* pcs = process_find_by_handle(hProcess);
391 WCHAR tmp[MAX_PATH];
392 WCHAR* ptr;
393 const WCHAR* filename;
395 TRACE("(hProcess = %p, searchPath = %s, full_path = %s, id = %p, two = 0x%08x, three = 0x%08x, flags = 0x%08x, buffer = %p, cb = %p, user = %p)\n",
396 hProcess, debugstr_w(searchPath), debugstr_w(full_path),
397 id, two, three, flags, buffer, cb, user);
399 if (!pcs) return FALSE;
400 if (!searchPath) searchPath = pcs->search_path;
402 s.cb = cb;
403 s.user = user;
405 filename = file_name(full_path);
407 /* first check full path to file */
408 if (sffip_cb(full_path, &s))
410 lstrcpyW(buffer, full_path);
411 return TRUE;
414 while (searchPath)
416 ptr = wcschr(searchPath, ';');
417 if (ptr)
419 memcpy(tmp, searchPath, (ptr - searchPath) * sizeof(WCHAR));
420 tmp[ptr - searchPath] = 0;
421 searchPath = ptr + 1;
423 else
425 lstrcpyW(tmp, searchPath);
426 searchPath = NULL;
428 if (do_searchW(filename, tmp, FALSE, sffip_cb, &s))
430 lstrcpyW(buffer, tmp);
431 return TRUE;
434 return FALSE;
437 /******************************************************************
438 * SymFindFileInPath (DBGHELP.@)
441 BOOL WINAPI SymFindFileInPath(HANDLE hProcess, PCSTR searchPath, PCSTR full_path,
442 PVOID id, DWORD two, DWORD three, DWORD flags,
443 PSTR buffer, PFINDFILEINPATHCALLBACK cb,
444 PVOID user)
446 WCHAR searchPathW[MAX_PATH];
447 WCHAR full_pathW[MAX_PATH];
448 WCHAR bufferW[MAX_PATH];
449 struct enum_dir_treeWA edt;
450 BOOL ret;
452 /* a PFINDFILEINPATHCALLBACK and a PENUMDIRTREE_CALLBACK have actually the
453 * same signature & semantics, hence we can reuse the EnumDirTree W->A
454 * conversion helper
456 edt.cb = cb;
457 edt.user = user;
458 if (searchPath)
459 MultiByteToWideChar(CP_ACP, 0, searchPath, -1, searchPathW, MAX_PATH);
460 MultiByteToWideChar(CP_ACP, 0, full_path, -1, full_pathW, MAX_PATH);
461 if ((ret = SymFindFileInPathW(hProcess, searchPath ? searchPathW : NULL, full_pathW,
462 id, two, three, flags,
463 bufferW, enum_dir_treeWA, &edt)))
464 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
465 return ret;
468 struct module_find
470 enum module_type kind;
471 /* pe: dw1 DWORD:timestamp
472 * dw2 size of image (from PE header)
473 * pdb: guid PDB guid (if DS PDB file)
474 * or dw1 PDB timestamp (if JG PDB file)
475 * dw2 PDB age
476 * elf: dw1 DWORD:CRC 32 of ELF image (Wine only)
478 const GUID* guid;
479 DWORD dw1;
480 DWORD dw2;
481 WCHAR filename[MAX_PATH];
482 unsigned matched;
485 /* checks that buffer (as found by matching the name) matches the info
486 * (information is based on file type)
487 * returns TRUE when file is found, FALSE to continue searching
488 * (NB this is the opposite convention of SymFindFileInPathProc)
490 static BOOL CALLBACK module_find_cb(PCWSTR buffer, PVOID user)
492 struct module_find* mf = user;
493 DWORD size, timestamp;
494 unsigned matched = 0;
496 /* the matching weights:
497 * +1 if a file with same name is found and is a decent file of expected type
498 * +1 if first parameter and second parameter match
501 /* FIXME: should check that id/two match the file pointed
502 * by buffer
504 switch (mf->kind)
506 case DMT_PE:
508 HANDLE hFile, hMap;
509 void* mapping;
511 timestamp = ~mf->dw1;
512 size = ~mf->dw2;
513 hFile = CreateFileW(buffer, GENERIC_READ, FILE_SHARE_READ, NULL,
514 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
515 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
516 if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
518 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
520 IMAGE_NT_HEADERS* nth = RtlImageNtHeader(mapping);
521 if (!nth)
523 UnmapViewOfFile(mapping);
524 CloseHandle(hMap);
525 CloseHandle(hFile);
526 return FALSE;
528 matched++;
529 timestamp = nth->FileHeader.TimeDateStamp;
530 size = nth->OptionalHeader.SizeOfImage;
531 UnmapViewOfFile(mapping);
533 CloseHandle(hMap);
535 CloseHandle(hFile);
536 if (timestamp != mf->dw1)
537 WARN("Found %s, but wrong timestamp\n", debugstr_w(buffer));
538 if (size != mf->dw2)
539 WARN("Found %s, but wrong size\n", debugstr_w(buffer));
540 if (timestamp == mf->dw1 && size == mf->dw2) matched++;
542 break;
543 case DMT_PDB:
545 struct pdb_lookup pdb_lookup;
546 char fn[MAX_PATH];
548 WideCharToMultiByte(CP_ACP, 0, buffer, -1, fn, MAX_PATH, NULL, NULL);
549 pdb_lookup.filename = fn;
551 if (mf->guid)
553 pdb_lookup.kind = PDB_DS;
554 pdb_lookup.timestamp = 0;
555 pdb_lookup.guid = *mf->guid;
557 else
559 pdb_lookup.kind = PDB_JG;
560 pdb_lookup.timestamp = mf->dw1;
561 /* pdb_loopkup.guid = */
563 pdb_lookup.age = mf->dw2;
565 if (!pdb_fetch_file_info(&pdb_lookup, &matched)) return FALSE;
567 break;
568 case DMT_DBG:
570 HANDLE hFile, hMap;
571 void* mapping;
573 timestamp = ~mf->dw1;
574 hFile = CreateFileW(buffer, GENERIC_READ, FILE_SHARE_READ, NULL,
575 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
576 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
577 if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
579 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
581 const IMAGE_SEPARATE_DEBUG_HEADER* hdr;
582 hdr = mapping;
584 if (hdr->Signature == IMAGE_SEPARATE_DEBUG_SIGNATURE)
586 matched++;
587 timestamp = hdr->TimeDateStamp;
589 UnmapViewOfFile(mapping);
591 CloseHandle(hMap);
593 CloseHandle(hFile);
594 if (timestamp == mf->dw1) matched++;
595 else WARN("Found %s, but wrong timestamp\n", debugstr_w(buffer));
597 break;
598 default:
599 FIXME("What the heck??\n");
600 return FALSE;
602 if (matched > mf->matched)
604 lstrcpyW(mf->filename, buffer);
605 mf->matched = matched;
607 /* yes, EnumDirTree/do_search and SymFindFileInPath callbacks use the opposite
608 * convention to stop/continue enumeration. sigh.
610 return mf->matched == 2;
613 BOOL path_find_symbol_file(const struct process* pcs, const struct module* module,
614 PCSTR full_path, enum module_type type, const GUID* guid, DWORD dw1, DWORD dw2,
615 WCHAR *buffer, BOOL* is_unmatched)
617 struct module_find mf;
618 WCHAR full_pathW[MAX_PATH];
619 WCHAR* ptr;
620 const WCHAR* filename;
621 WCHAR* searchPath = pcs->search_path;
623 TRACE("(pcs = %p, full_path = %s, guid = %s, dw1 = 0x%08x, dw2 = 0x%08x, buffer = %p)\n",
624 pcs, debugstr_a(full_path), debugstr_guid(guid), dw1, dw2, buffer);
626 mf.guid = guid;
627 mf.dw1 = dw1;
628 mf.dw2 = dw2;
629 mf.matched = 0;
631 MultiByteToWideChar(CP_ACP, 0, full_path, -1, full_pathW, MAX_PATH);
632 filename = file_name(full_pathW);
633 mf.kind = type;
634 *is_unmatched = FALSE;
636 /* first check full path to file */
637 if (module_find_cb(full_pathW, &mf))
639 lstrcpyW( buffer, full_pathW );
640 return TRUE;
643 /* FIXME: Use Environment-Variables (see MS docs)
644 _NT_SYMBOL_PATH and _NT_ALT_SYMBOL_PATH
645 FIXME: Implement "Standard Path Elements" (Path) ... (see MS docs)
646 do a search for (every?) path-element like this ...
647 <path>
648 <path>\dll
649 <path>\symbols\dll
650 (dll may be exe, or sys depending on the file extension) */
652 /* 2. check module-path */
653 file_pathW(module->module.LoadedImageName, buffer);
654 if (do_searchW(filename, buffer, FALSE, module_find_cb, &mf)) return TRUE;
655 if (module->real_path)
657 file_pathW(module->real_path, buffer);
658 if (do_searchW(filename, buffer, FALSE, module_find_cb, &mf)) return TRUE;
661 while (searchPath)
663 ptr = wcschr(searchPath, ';');
664 if (ptr)
666 memcpy(buffer, searchPath, (ptr - searchPath) * sizeof(WCHAR));
667 buffer[ptr - searchPath] = '\0';
668 searchPath = ptr + 1;
670 else
672 lstrcpyW(buffer, searchPath);
673 searchPath = NULL;
675 /* return first fully matched file */
676 if (do_searchW(filename, buffer, FALSE, module_find_cb, &mf)) return TRUE;
678 /* if no fully matching file is found, return the best matching file if any */
679 if ((dbghelp_options & SYMOPT_LOAD_ANYTHING) && mf.matched)
681 lstrcpyW( buffer, mf.filename );
682 *is_unmatched = TRUE;
683 return TRUE;
685 return FALSE;
688 WCHAR *get_dos_file_name(const WCHAR *filename)
690 WCHAR *dos_path;
691 size_t len;
693 if (*filename == '/')
695 char *unix_path;
696 len = WideCharToMultiByte(CP_UNIXCP, 0, filename, -1, NULL, 0, NULL, NULL);
697 unix_path = heap_alloc(len * sizeof(WCHAR));
698 WideCharToMultiByte(CP_UNIXCP, 0, filename, -1, unix_path, len, NULL, NULL);
699 dos_path = wine_get_dos_file_name(unix_path);
700 heap_free(unix_path);
702 else
704 len = lstrlenW(filename);
705 dos_path = heap_alloc((len + 1) * sizeof(WCHAR));
706 memcpy(dos_path, filename, (len + 1) * sizeof(WCHAR));
708 return dos_path;
711 BOOL search_dll_path(const struct process *process, const WCHAR *name, BOOL (*match)(void*, HANDLE, const WCHAR*), void *param)
713 const WCHAR *env;
714 WCHAR *p, *end;
715 size_t len, i;
716 HANDLE file;
717 WCHAR *buf;
718 BOOL ret;
720 name = file_name(name);
722 if ((env = process_getenv(process, L"WINEBUILDDIR")))
724 const WCHAR dllsW[] = { '\\','d','l','l','s','\\' };
725 const WCHAR programsW[] = { '\\','p','r','o','g','r','a','m','s','\\' };
726 const WCHAR dot_dllW[] = {'.','d','l','l',0};
727 const WCHAR dot_exeW[] = {'.','e','x','e',0};
728 const WCHAR dot_soW[] = {'.','s','o',0};
731 len = lstrlenW(env);
732 if (!(buf = heap_alloc((len + 8 + 3 * lstrlenW(name)) * sizeof(WCHAR)))) return FALSE;
733 wcscpy(buf, env);
734 end = buf + len;
736 memcpy(end, dllsW, sizeof(dllsW));
737 lstrcpyW(end + ARRAY_SIZE(dllsW), name);
738 if ((p = wcsrchr(end, '.')) && !lstrcmpW(p, dot_soW)) *p = 0;
739 if ((p = wcsrchr(end, '.')) && !lstrcmpW(p, dot_dllW)) *p = 0;
740 p = end + lstrlenW(end);
741 *p++ = '\\';
742 lstrcpyW(p, name);
743 file = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
744 if (file != INVALID_HANDLE_VALUE)
746 ret = match(param, file, buf);
747 CloseHandle(file);
748 if (ret) goto found;
751 memcpy(end, programsW, sizeof(programsW));
752 end += ARRAY_SIZE(programsW);
753 lstrcpyW(end, name);
754 if ((p = wcsrchr(end, '.')) && !lstrcmpW(p, dot_soW)) *p = 0;
755 if ((p = wcsrchr(end, '.')) && !lstrcmpW(p, dot_exeW)) *p = 0;
756 p = end + lstrlenW(end);
757 *p++ = '\\';
758 lstrcpyW(p, name);
759 file = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
760 if (file != INVALID_HANDLE_VALUE)
762 ret = match(param, file, buf);
763 CloseHandle(file);
764 if (ret) goto found;
767 heap_free(buf);
770 for (i = 0;; i++)
772 WCHAR env_name[64];
773 swprintf(env_name, ARRAY_SIZE(env_name), L"WINEDLLDIR%u", i);
774 if (!(env = process_getenv(process, env_name))) return FALSE;
775 len = wcslen(env) + wcslen(pe_dir) + wcslen(name) + 2;
776 if (!(buf = heap_alloc(len * sizeof(WCHAR)))) return FALSE;
777 if ((p = wcsrchr(name, '.')) && !lstrcmpW(p, L".so"))
778 swprintf(buf, len, L"%s%s\\%s", env, so_dir, name);
779 else
780 swprintf(buf, len, L"%s%s\\%s", env, pe_dir, name);
781 file = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
782 if (file != INVALID_HANDLE_VALUE)
784 ret = match(param, file, buf);
785 CloseHandle(file);
786 if (ret) goto found;
788 swprintf(buf, len, L"%s\\%s", env, name);
789 file = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
790 if (file != INVALID_HANDLE_VALUE)
792 ret = match(param, file, buf);
793 CloseHandle(file);
794 if (ret) goto found;
796 heap_free(buf);
799 return FALSE;
801 found:
802 TRACE("found %s\n", debugstr_w(buf));
803 heap_free(buf);
804 return TRUE;
807 BOOL search_unix_path(const WCHAR *name, const WCHAR *path, BOOL (*match)(void*, HANDLE, const WCHAR*), void *param)
809 const WCHAR *iter, *next;
810 size_t size, len;
811 WCHAR *dos_path;
812 char *buf;
813 BOOL ret = FALSE;
815 if (!path) return FALSE;
816 name = file_name(name);
818 size = WideCharToMultiByte(CP_UNIXCP, 0, name, -1, NULL, 0, NULL, NULL)
819 + WideCharToMultiByte(CP_UNIXCP, 0, path, -1, NULL, 0, NULL, NULL);
820 if (!(buf = heap_alloc(size))) return FALSE;
822 for (iter = path;; iter = next + 1)
824 if (!(next = wcschr(iter, ':'))) next = iter + lstrlenW(iter);
825 if (*iter == '/')
827 len = WideCharToMultiByte(CP_UNIXCP, 0, iter, next - iter, buf, size, NULL, NULL);
828 if (buf[len - 1] != '/') buf[len++] = '/';
829 WideCharToMultiByte(CP_UNIXCP, 0, name, -1, buf + len, size - len, NULL, NULL);
830 if ((dos_path = wine_get_dos_file_name(buf)))
832 HANDLE file = CreateFileW(dos_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
833 if (file != INVALID_HANDLE_VALUE)
835 ret = match(param, file, dos_path);
836 CloseHandle(file);
837 if (ret) TRACE("found %s\n", debugstr_w(dos_path));
839 heap_free(dos_path);
840 if (ret) break;
843 if (*next != ':') break;
846 heap_free(buf);
847 return ret;