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
25 #include "dbghelp_private.h"
26 #include "image_private.h"
29 #include "wine/debug.h"
30 #include "wine/heap.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
34 static const struct machine_dir
42 {IMAGE_FILE_MACHINE_I386
, L
"\\i386-windows\\", L
"\\i386-unix\\"},
43 {IMAGE_FILE_MACHINE_AMD64
, L
"\\x86_64-windows\\", L
"\\x86_64-unix\\"},
44 {IMAGE_FILE_MACHINE_ARMNT
, L
"\\arm-windows\\", L
"\\arm-unix\\"},
45 {IMAGE_FILE_MACHINE_ARM64
, L
"\\aarch64-windows\\", L
"\\aarch64-unix\\"},
48 static inline BOOL
is_sepA(char ch
) {return ch
== '/' || ch
== '\\';}
49 static inline BOOL
is_sep(WCHAR ch
) {return ch
== '/' || ch
== '\\';}
51 const char* file_nameA(const char* str
)
55 for (p
= str
+ strlen(str
) - 1; p
>= str
&& !is_sepA(*p
); p
--);
59 const WCHAR
* file_name(const WCHAR
* str
)
63 for (p
= str
+ lstrlenW(str
) - 1; p
>= str
&& !is_sep(*p
); p
--);
67 static inline void file_pathW(const WCHAR
*src
, WCHAR
*dst
)
71 for (len
= lstrlenW(src
) - 1; (len
> 0) && (!is_sep(src
[len
])); len
--);
72 memcpy( dst
, src
, len
* sizeof(WCHAR
) );
76 /******************************************************************
77 * FindDebugInfoFile (DBGHELP.@)
80 HANDLE WINAPI
FindDebugInfoFile(PCSTR FileName
, PCSTR SymbolPath
, PSTR DebugFilePath
)
84 h
= CreateFileA(FileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
85 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
86 if (h
== INVALID_HANDLE_VALUE
)
88 if (!SearchPathA(SymbolPath
, file_nameA(FileName
), NULL
, MAX_PATH
, DebugFilePath
, NULL
))
90 h
= CreateFileA(DebugFilePath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
91 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
93 return (h
== INVALID_HANDLE_VALUE
) ? NULL
: h
;
96 /******************************************************************
97 * FindDebugInfoFileEx (DBGHELP.@)
100 HANDLE WINAPI
FindDebugInfoFileEx(PCSTR FileName
, PCSTR SymbolPath
,
102 PFIND_DEBUG_FILE_CALLBACK Callback
,
105 FIXME("(%s %s %s %p %p): stub\n", debugstr_a(FileName
), debugstr_a(SymbolPath
),
106 debugstr_a(DebugFilePath
), Callback
, CallerData
);
110 /******************************************************************
111 * FindExecutableImageExW (DBGHELP.@)
114 HANDLE WINAPI
FindExecutableImageExW(PCWSTR FileName
, PCWSTR SymbolPath
, PWSTR ImageFilePath
,
115 PFIND_EXE_FILE_CALLBACKW Callback
, PVOID user
)
119 if (Callback
) FIXME("Unsupported callback yet\n");
120 if (!SearchPathW(SymbolPath
, FileName
, NULL
, MAX_PATH
, ImageFilePath
, NULL
))
122 h
= CreateFileW(ImageFilePath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
123 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
124 return (h
== INVALID_HANDLE_VALUE
) ? NULL
: h
;
127 /******************************************************************
128 * FindExecutableImageEx (DBGHELP.@)
131 HANDLE WINAPI
FindExecutableImageEx(PCSTR FileName
, PCSTR SymbolPath
, PSTR ImageFilePath
,
132 PFIND_EXE_FILE_CALLBACK Callback
, PVOID user
)
136 if (Callback
) FIXME("Unsupported callback yet\n");
137 if (!SearchPathA(SymbolPath
, FileName
, NULL
, MAX_PATH
, ImageFilePath
, NULL
))
139 h
= CreateFileA(ImageFilePath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
140 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
141 return (h
== INVALID_HANDLE_VALUE
) ? NULL
: h
;
144 /******************************************************************
145 * FindExecutableImage (DBGHELP.@)
148 HANDLE WINAPI
FindExecutableImage(PCSTR FileName
, PCSTR SymbolPath
, PSTR ImageFilePath
)
150 return FindExecutableImageEx(FileName
, SymbolPath
, ImageFilePath
, NULL
, NULL
);
153 /***********************************************************************
154 * MakeSureDirectoryPathExists (DBGHELP.@)
156 BOOL WINAPI
MakeSureDirectoryPathExists(PCSTR DirPath
)
159 const char *p
= DirPath
;
162 if (p
[0] && p
[1] == ':') p
+= 2;
163 while (*p
== '\\') p
++; /* skip drive root */
164 while ((p
= strchr(p
, '\\')) != NULL
)
167 memcpy(path
, DirPath
, n
);
169 if( !CreateDirectoryA(path
, NULL
) &&
170 (GetLastError() != ERROR_ALREADY_EXISTS
))
174 if (GetLastError() == ERROR_ALREADY_EXISTS
)
175 SetLastError(ERROR_SUCCESS
);
180 /******************************************************************
181 * SymMatchFileNameW (DBGHELP.@)
184 BOOL WINAPI
SymMatchFileNameW(PCWSTR file
, PCWSTR match
,
185 PWSTR
* filestop
, PWSTR
* matchstop
)
190 TRACE("(%s %s %p %p)\n",
191 debugstr_w(file
), debugstr_w(match
), filestop
, matchstop
);
193 fptr
= file
+ lstrlenW(file
) - 1;
194 mptr
= match
+ lstrlenW(match
) - 1;
196 while (fptr
>= file
&& mptr
>= match
)
198 if (towupper(*fptr
) != towupper(*mptr
) && !(is_sep(*fptr
) && is_sep(*mptr
)))
202 if (filestop
) *filestop
= (PWSTR
)fptr
;
203 if (matchstop
) *matchstop
= (PWSTR
)mptr
;
205 return mptr
== match
- 1;
208 /******************************************************************
209 * SymMatchFileName (DBGHELP.@)
212 BOOL WINAPI
SymMatchFileName(PCSTR file
, PCSTR match
,
213 PSTR
* filestop
, PSTR
* matchstop
)
218 TRACE("(%s %s %p %p)\n", debugstr_a(file
), debugstr_a(match
), filestop
, matchstop
);
220 fptr
= file
+ strlen(file
) - 1;
221 mptr
= match
+ strlen(match
) - 1;
223 while (fptr
>= file
&& mptr
>= match
)
225 if (toupper(*fptr
) != toupper(*mptr
) && !(is_sepA(*fptr
) && is_sepA(*mptr
)))
229 if (filestop
) *filestop
= (PSTR
)fptr
;
230 if (matchstop
) *matchstop
= (PSTR
)mptr
;
232 return mptr
== match
- 1;
235 static BOOL
do_searchW(PCWSTR file
, PWSTR buffer
, BOOL recurse
,
236 PENUMDIRTREE_CALLBACKW cb
, PVOID user
)
243 pos
= lstrlenW(buffer
);
244 if (pos
== 0) return FALSE
;
245 if (buffer
[pos
- 1] != '\\') buffer
[pos
++] = '\\';
246 lstrcpyW(buffer
+ pos
, L
"*.*");
247 if ((h
= FindFirstFileW(buffer
, &fd
)) == INVALID_HANDLE_VALUE
)
249 /* doc doesn't specify how the tree is enumerated...
250 * doing a depth first based on, but may be wrong
254 if (!wcscmp(fd
.cFileName
, L
".") || !wcscmp(fd
.cFileName
, L
"..")) continue;
256 lstrcpyW(buffer
+ pos
, fd
.cFileName
);
257 if (recurse
&& (fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
258 found
= do_searchW(file
, buffer
, TRUE
, cb
, user
);
259 else if (SymMatchFileNameW(buffer
, file
, NULL
, NULL
))
261 if (!cb
|| cb(buffer
, user
)) found
= TRUE
;
263 } while (!found
&& FindNextFileW(h
, &fd
));
264 if (!found
) buffer
[--pos
] = '\0';
270 /***********************************************************************
271 * SearchTreeForFileW (DBGHELP.@)
273 BOOL WINAPI
SearchTreeForFileW(PCWSTR root
, PCWSTR file
, PWSTR buffer
)
275 TRACE("(%s, %s, %p)\n",
276 debugstr_w(root
), debugstr_w(file
), buffer
);
277 lstrcpyW(buffer
, root
);
278 return do_searchW(file
, buffer
, TRUE
, NULL
, NULL
);
281 /***********************************************************************
282 * SearchTreeForFile (DBGHELP.@)
284 BOOL WINAPI
SearchTreeForFile(PCSTR root
, PCSTR file
, PSTR buffer
)
286 WCHAR rootW
[MAX_PATH
];
287 WCHAR fileW
[MAX_PATH
];
288 WCHAR bufferW
[MAX_PATH
];
291 MultiByteToWideChar(CP_ACP
, 0, root
, -1, rootW
, MAX_PATH
);
292 MultiByteToWideChar(CP_ACP
, 0, file
, -1, fileW
, MAX_PATH
);
293 ret
= SearchTreeForFileW(rootW
, fileW
, bufferW
);
295 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, MAX_PATH
, NULL
, NULL
);
299 /******************************************************************
300 * EnumDirTreeW (DBGHELP.@)
304 BOOL WINAPI
EnumDirTreeW(HANDLE hProcess
, PCWSTR root
, PCWSTR file
,
305 PWSTR buffer
, PENUMDIRTREE_CALLBACKW cb
, PVOID user
)
307 TRACE("(%p %s %s %p %p %p)\n",
308 hProcess
, debugstr_w(root
), debugstr_w(file
), buffer
, cb
, user
);
310 lstrcpyW(buffer
, root
);
311 return do_searchW(file
, buffer
, TRUE
, cb
, user
);
314 /******************************************************************
315 * EnumDirTree (DBGHELP.@)
319 struct enum_dir_treeWA
321 PENUMDIRTREE_CALLBACK cb
;
326 static BOOL CALLBACK
enum_dir_treeWA(PCWSTR name
, PVOID user
)
328 struct enum_dir_treeWA
* edt
= user
;
330 WideCharToMultiByte(CP_ACP
, 0, name
, -1, edt
->name
, MAX_PATH
, NULL
, NULL
);
331 return edt
->cb(edt
->name
, edt
->user
);
334 BOOL WINAPI
EnumDirTree(HANDLE hProcess
, PCSTR root
, PCSTR file
,
335 PSTR buffer
, PENUMDIRTREE_CALLBACK cb
, PVOID user
)
337 WCHAR rootW
[MAX_PATH
];
338 WCHAR fileW
[MAX_PATH
];
339 WCHAR bufferW
[MAX_PATH
];
340 struct enum_dir_treeWA edt
;
345 MultiByteToWideChar(CP_ACP
, 0, root
, -1, rootW
, MAX_PATH
);
346 MultiByteToWideChar(CP_ACP
, 0, file
, -1, fileW
, MAX_PATH
);
347 if ((ret
= EnumDirTreeW(hProcess
, rootW
, fileW
, bufferW
, enum_dir_treeWA
, &edt
)))
348 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, MAX_PATH
, NULL
, NULL
);
354 PFINDFILEINPATHCALLBACKW cb
;
358 /* checks that buffer (as found by matching the name) matches the info
359 * (information is based on file type)
360 * returns TRUE when file is found, FALSE to continue searching
361 * (NB this is the opposite convention of SymFindFileInPathProc)
363 static BOOL CALLBACK
sffip_cb(PCWSTR buffer
, PVOID user
)
365 struct sffip
* s
= user
;
367 if (!s
->cb
) return TRUE
;
368 /* yes, EnumDirTree/do_search and SymFindFileInPath callbacks use the opposite
369 * convention to stop/continue enumeration. sigh.
371 return !(s
->cb
)(buffer
, s
->user
);
374 /******************************************************************
375 * SymFindFileInPathW (DBGHELP.@)
378 BOOL WINAPI
SymFindFileInPathW(HANDLE hProcess
, PCWSTR searchPath
, PCWSTR full_path
,
379 PVOID id
, DWORD two
, DWORD three
, DWORD flags
,
380 PWSTR buffer
, PFINDFILEINPATHCALLBACKW cb
,
384 struct process
* pcs
= process_find_by_handle(hProcess
);
387 const WCHAR
* filename
;
389 TRACE("(hProcess = %p, searchPath = %s, full_path = %s, id = %p, two = 0x%08lx, three = 0x%08lx, flags = 0x%08lx, buffer = %p, cb = %p, user = %p)\n",
390 hProcess
, debugstr_w(searchPath
), debugstr_w(full_path
),
391 id
, two
, three
, flags
, buffer
, cb
, user
);
393 if (!pcs
) return FALSE
;
394 if (!searchPath
) searchPath
= pcs
->search_path
;
399 filename
= file_name(full_path
);
401 /* first check full path to file */
402 if (sffip_cb(full_path
, &s
))
404 lstrcpyW(buffer
, full_path
);
410 ptr
= wcschr(searchPath
, ';');
413 memcpy(tmp
, searchPath
, (ptr
- searchPath
) * sizeof(WCHAR
));
414 tmp
[ptr
- searchPath
] = 0;
415 searchPath
= ptr
+ 1;
419 lstrcpyW(tmp
, searchPath
);
422 if (do_searchW(filename
, tmp
, FALSE
, sffip_cb
, &s
))
424 lstrcpyW(buffer
, tmp
);
431 /******************************************************************
432 * SymFindFileInPath (DBGHELP.@)
435 BOOL WINAPI
SymFindFileInPath(HANDLE hProcess
, PCSTR searchPath
, PCSTR full_path
,
436 PVOID id
, DWORD two
, DWORD three
, DWORD flags
,
437 PSTR buffer
, PFINDFILEINPATHCALLBACK cb
,
440 WCHAR searchPathW
[MAX_PATH
];
441 WCHAR full_pathW
[MAX_PATH
];
442 WCHAR bufferW
[MAX_PATH
];
443 struct enum_dir_treeWA edt
;
446 /* a PFINDFILEINPATHCALLBACK and a PENUMDIRTREE_CALLBACK have actually the
447 * same signature & semantics, hence we can reuse the EnumDirTree W->A
453 MultiByteToWideChar(CP_ACP
, 0, searchPath
, -1, searchPathW
, MAX_PATH
);
454 MultiByteToWideChar(CP_ACP
, 0, full_path
, -1, full_pathW
, MAX_PATH
);
455 if ((ret
= SymFindFileInPathW(hProcess
, searchPath
? searchPathW
: NULL
, full_pathW
,
456 id
, two
, three
, flags
,
457 bufferW
, enum_dir_treeWA
, &edt
)))
458 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, MAX_PATH
, NULL
, NULL
);
464 enum module_type kind
;
465 /* pe: dw1 DWORD:timestamp
466 * dw2 size of image (from PE header)
467 * pdb: guid PDB guid (if DS PDB file)
468 * or dw1 PDB timestamp (if JG PDB file)
470 * elf: dw1 DWORD:CRC 32 of ELF image (Wine only)
475 WCHAR filename
[MAX_PATH
];
479 /* checks that buffer (as found by matching the name) matches the info
480 * (information is based on file type)
481 * returns TRUE when file is found, FALSE to continue searching
482 * (NB this is the opposite convention of SymFindFileInPathProc)
484 static BOOL CALLBACK
module_find_cb(PCWSTR buffer
, PVOID user
)
486 struct module_find
* mf
= user
;
487 DWORD size
, timestamp
;
488 unsigned matched
= 0;
490 /* the matching weights:
491 * +1 if a file with same name is found and is a decent file of expected type
492 * +1 if first parameter and second parameter match
495 /* FIXME: should check that id/two match the file pointed
505 timestamp
= ~mf
->dw1
;
507 hFile
= CreateFileW(buffer
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
508 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
509 if (hFile
== INVALID_HANDLE_VALUE
) return FALSE
;
510 if ((hMap
= CreateFileMappingW(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
)) != NULL
)
512 if ((mapping
= MapViewOfFile(hMap
, FILE_MAP_READ
, 0, 0, 0)) != NULL
)
514 IMAGE_NT_HEADERS
* nth
= RtlImageNtHeader(mapping
);
517 UnmapViewOfFile(mapping
);
523 timestamp
= nth
->FileHeader
.TimeDateStamp
;
524 size
= nth
->OptionalHeader
.SizeOfImage
;
525 UnmapViewOfFile(mapping
);
530 if (timestamp
!= mf
->dw1
)
531 WARN("Found %s, but wrong timestamp\n", debugstr_w(buffer
));
533 WARN("Found %s, but wrong size\n", debugstr_w(buffer
));
534 if (timestamp
== mf
->dw1
&& size
== mf
->dw2
) matched
++;
539 struct pdb_lookup pdb_lookup
;
542 WideCharToMultiByte(CP_ACP
, 0, buffer
, -1, fn
, MAX_PATH
, NULL
, NULL
);
543 pdb_lookup
.filename
= fn
;
547 pdb_lookup
.kind
= PDB_DS
;
548 pdb_lookup
.timestamp
= 0;
549 pdb_lookup
.guid
= *mf
->guid
;
553 pdb_lookup
.kind
= PDB_JG
;
554 pdb_lookup
.timestamp
= mf
->dw1
;
555 /* pdb_loopkup.guid = */
557 pdb_lookup
.age
= mf
->dw2
;
559 if (!pdb_fetch_file_info(&pdb_lookup
, &matched
)) return FALSE
;
567 timestamp
= ~mf
->dw1
;
568 hFile
= CreateFileW(buffer
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
569 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
570 if (hFile
== INVALID_HANDLE_VALUE
) return FALSE
;
571 if ((hMap
= CreateFileMappingW(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
)) != NULL
)
573 if ((mapping
= MapViewOfFile(hMap
, FILE_MAP_READ
, 0, 0, 0)) != NULL
)
575 const IMAGE_SEPARATE_DEBUG_HEADER
* hdr
;
578 if (hdr
->Signature
== IMAGE_SEPARATE_DEBUG_SIGNATURE
)
581 timestamp
= hdr
->TimeDateStamp
;
583 UnmapViewOfFile(mapping
);
588 if (timestamp
== mf
->dw1
) matched
++;
589 else WARN("Found %s, but wrong timestamp\n", debugstr_w(buffer
));
593 FIXME("What the heck??\n");
596 if (matched
> mf
->matched
)
598 lstrcpyW(mf
->filename
, buffer
);
599 mf
->matched
= matched
;
601 /* yes, EnumDirTree/do_search and SymFindFileInPath callbacks use the opposite
602 * convention to stop/continue enumeration. sigh.
604 return mf
->matched
== 2;
607 BOOL
path_find_symbol_file(const struct process
* pcs
, const struct module
* module
,
608 PCSTR full_path
, enum module_type type
, const GUID
* guid
, DWORD dw1
, DWORD dw2
,
609 WCHAR
*buffer
, BOOL
* is_unmatched
)
611 struct module_find mf
;
612 WCHAR full_pathW
[MAX_PATH
];
614 const WCHAR
* filename
;
615 WCHAR
* searchPath
= pcs
->search_path
;
617 TRACE("(pcs = %p, full_path = %s, guid = %s, dw1 = 0x%08lx, dw2 = 0x%08lx, buffer = %p)\n",
618 pcs
, debugstr_a(full_path
), debugstr_guid(guid
), dw1
, dw2
, buffer
);
625 MultiByteToWideChar(CP_ACP
, 0, full_path
, -1, full_pathW
, MAX_PATH
);
626 filename
= file_name(full_pathW
);
628 *is_unmatched
= FALSE
;
630 /* first check full path to file */
631 if (module_find_cb(full_pathW
, &mf
))
633 lstrcpyW( buffer
, full_pathW
);
637 /* FIXME: Use Environment-Variables (see MS docs)
638 _NT_SYMBOL_PATH and _NT_ALT_SYMBOL_PATH
639 FIXME: Implement "Standard Path Elements" (Path) ... (see MS docs)
640 do a search for (every?) path-element like this ...
644 (dll may be exe, or sys depending on the file extension) */
646 /* 2. check module-path */
647 file_pathW(module
->module
.LoadedImageName
, buffer
);
648 if (do_searchW(filename
, buffer
, FALSE
, module_find_cb
, &mf
)) return TRUE
;
649 if (module
->real_path
)
651 file_pathW(module
->real_path
, buffer
);
652 if (do_searchW(filename
, buffer
, FALSE
, module_find_cb
, &mf
)) return TRUE
;
657 ptr
= wcschr(searchPath
, ';');
660 memcpy(buffer
, searchPath
, (ptr
- searchPath
) * sizeof(WCHAR
));
661 buffer
[ptr
- searchPath
] = '\0';
662 searchPath
= ptr
+ 1;
666 lstrcpyW(buffer
, searchPath
);
669 /* return first fully matched file */
670 if (do_searchW(filename
, buffer
, FALSE
, module_find_cb
, &mf
)) return TRUE
;
672 /* if no fully matching file is found, return the best matching file if any */
673 if ((dbghelp_options
& SYMOPT_LOAD_ANYTHING
) && mf
.matched
)
675 lstrcpyW( buffer
, mf
.filename
);
676 *is_unmatched
= TRUE
;
682 WCHAR
*get_dos_file_name(const WCHAR
*filename
)
687 if (*filename
== '/')
690 len
= WideCharToMultiByte(CP_UNIXCP
, 0, filename
, -1, NULL
, 0, NULL
, NULL
);
691 unix_path
= heap_alloc(len
* sizeof(WCHAR
));
692 WideCharToMultiByte(CP_UNIXCP
, 0, filename
, -1, unix_path
, len
, NULL
, NULL
);
693 dos_path
= wine_get_dos_file_name(unix_path
);
694 heap_free(unix_path
);
698 len
= lstrlenW(filename
);
699 dos_path
= heap_alloc((len
+ 1) * sizeof(WCHAR
));
700 memcpy(dos_path
, filename
, (len
+ 1) * sizeof(WCHAR
));
705 static inline const WCHAR
* get_machine_dir(const struct machine_dir
*machine_dir
, const WCHAR
*name
)
708 if ((ptr
= wcsrchr(name
, L
'.')) && !lstrcmpW(ptr
, L
".so"))
709 return machine_dir
->so_dir
;
710 return machine_dir
->pe_dir
;
713 static BOOL
try_match_file(const WCHAR
*name
, BOOL (*match
)(void*, HANDLE
, const WCHAR
*), void *param
)
715 HANDLE file
= CreateFileW(name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
716 if (file
!= INVALID_HANDLE_VALUE
)
718 BOOL ret
= match(param
, file
, name
);
725 BOOL
search_dll_path(const struct process
*process
, const WCHAR
*name
, BOOL (*match
)(void*, HANDLE
, const WCHAR
*), void *param
)
729 size_t len
, i
, machine_dir_len
;
731 const struct cpu
* cpu
;
732 const struct machine_dir
* machine_dir
;
734 name
= file_name(name
);
736 cpu
= process_get_cpu(process
);
737 for (machine_dir
= all_machine_dir
; machine_dir
< all_machine_dir
+ ARRAY_SIZE(all_machine_dir
); machine_dir
++)
738 if (machine_dir
->machine
== cpu
->machine
) break;
739 if (machine_dir
>= all_machine_dir
+ ARRAY_SIZE(all_machine_dir
)) return FALSE
;
740 machine_dir_len
= max(wcslen(machine_dir
->pe_dir
), wcslen(machine_dir
->so_dir
));
742 if ((env
= process_getenv(process
, L
"WINEBUILDDIR")))
745 if (!(buf
= heap_alloc((len
+ wcslen(L
"\\programs\\") + machine_dir_len
+
746 2 * lstrlenW(name
) + 1) * sizeof(WCHAR
)))) return FALSE
;
750 wcscpy(end
, L
"\\dlls\\");
752 if ((p
= wcsrchr(end
, '.')) && !lstrcmpW(p
, L
".so")) *p
= 0;
753 if ((p
= wcsrchr(end
, '.')) && !lstrcmpW(p
, L
".dll")) *p
= 0;
754 p
= end
+ lstrlenW(end
);
755 /* try multi-arch first */
756 wcscpy(p
, get_machine_dir(machine_dir
, name
));
757 wcscpy(p
+ wcslen(p
), name
);
758 if (try_match_file(buf
, match
, param
)) goto found
;
759 /* then old mono-arch */
762 if (try_match_file(buf
, match
, param
)) goto found
;
764 wcscpy(end
, L
"\\programs\\");
767 if ((p
= wcsrchr(end
, '.')) && !lstrcmpW(p
, L
".so")) *p
= 0;
768 if ((p
= wcsrchr(end
, '.')) && !lstrcmpW(p
, L
".exe")) *p
= 0;
769 p
= end
+ lstrlenW(end
);
770 /* try multi-arch first */
771 wcscpy(p
, get_machine_dir(machine_dir
, name
));
772 wcscpy(p
+ wcslen(p
), name
);
773 if (try_match_file(buf
, match
, param
)) goto found
;
774 /* then old mono-arch */
777 if (try_match_file(buf
, match
, param
)) goto found
;
785 swprintf(env_name
, ARRAY_SIZE(env_name
), L
"WINEDLLDIR%u", i
);
786 if (!(env
= process_getenv(process
, env_name
))) return FALSE
;
787 len
= wcslen(env
) + machine_dir_len
+ wcslen(name
) + 1;
788 if (!(buf
= heap_alloc(len
* sizeof(WCHAR
)))) return FALSE
;
789 swprintf(buf
, len
, L
"%s%s%s", env
, get_machine_dir(machine_dir
, name
), name
);
790 if (try_match_file(buf
, match
, param
)) goto found
;
791 swprintf(buf
, len
, L
"%s\\%s", env
, name
);
792 if (try_match_file(buf
, match
, param
)) goto found
;
799 TRACE("found %s\n", debugstr_w(buf
));
804 BOOL
search_unix_path(const WCHAR
*name
, const WCHAR
*path
, BOOL (*match
)(void*, HANDLE
, const WCHAR
*), void *param
)
806 const WCHAR
*iter
, *next
;
812 if (!path
) return FALSE
;
813 name
= file_name(name
);
815 size
= WideCharToMultiByte(CP_UNIXCP
, 0, name
, -1, NULL
, 0, NULL
, NULL
)
816 + WideCharToMultiByte(CP_UNIXCP
, 0, path
, -1, NULL
, 0, NULL
, NULL
);
817 if (!(buf
= heap_alloc(size
))) return FALSE
;
819 for (iter
= path
;; iter
= next
+ 1)
821 if (!(next
= wcschr(iter
, ':'))) next
= iter
+ lstrlenW(iter
);
824 len
= WideCharToMultiByte(CP_UNIXCP
, 0, iter
, next
- iter
, buf
, size
, NULL
, NULL
);
825 if (buf
[len
- 1] != '/') buf
[len
++] = '/';
826 WideCharToMultiByte(CP_UNIXCP
, 0, name
, -1, buf
+ len
, size
- len
, NULL
, NULL
);
827 if ((dos_path
= wine_get_dos_file_name(buf
)))
829 ret
= try_match_file(dos_path
, match
, param
);
830 if (ret
) TRACE("found %s\n", debugstr_w(dos_path
));
835 if (*next
!= ':') break;