Implemented DllCanUnloadNow.
[wine/multimedia.git] / dlls / dbghelp / path.c
blob604998fde3e754d0d68d14afcb438e9f16427b0e
1 /*
2 * File path.c - managing path in debugging environments
4 * Copyright (C) 2004, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
26 #include "dbghelp_private.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
31 static inline BOOL is_sep(char ch) {return ch == '/' || ch == '\\';}
33 static inline char* file_name(char* str)
35 char* p;
37 for (p = str + strlen(str) - 1; p >= str && !is_sep(*p); p--);
38 return p + 1;
41 /******************************************************************
42 * FindDebugInfoFile (DBGHELP.@)
45 HANDLE WINAPI FindDebugInfoFile(PSTR FileName, PSTR SymbolPath, PSTR DebugFilePath)
47 HANDLE h;
49 h = CreateFileA(DebugFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
50 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
51 if (h == INVALID_HANDLE_VALUE)
53 if (!SearchPathA(SymbolPath, file_name(FileName), NULL, MAX_PATH, DebugFilePath, NULL))
54 return NULL;
55 h = CreateFileA(DebugFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
56 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
58 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
61 /******************************************************************
62 * FindDebugInfoFileEx (DBGHELP.@)
65 HANDLE WINAPI FindDebugInfoFileEx(PSTR FileName, PSTR SymbolPath,
66 PSTR DebugFilePath,
67 PFIND_DEBUG_FILE_CALLBACK Callback,
68 PVOID CallerData)
70 FIXME("(%s %s %p %p %p): stub\n",
71 FileName, SymbolPath, DebugFilePath, Callback, CallerData);
72 return NULL;
75 /******************************************************************
76 * FindExecutableImage (DBGHELP.@)
79 HANDLE WINAPI FindExecutableImage(PSTR FileName, PSTR SymbolPath, PSTR ImageFilePath)
81 HANDLE h;
82 if (!SearchPathA(SymbolPath, FileName, NULL, MAX_PATH, ImageFilePath, NULL))
83 return NULL;
84 h = CreateFileA(ImageFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
85 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
86 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
89 /***********************************************************************
90 * MakeSureDirectoryPathExists (DBGHELP.@)
92 BOOL WINAPI MakeSureDirectoryPathExists(LPCSTR DirPath)
94 char path[MAX_PATH];
95 const char *p = DirPath;
96 int n;
98 if (p[0] && p[1] == ':') p += 2;
99 while (*p == '\\') p++; /* skip drive root */
100 while ((p = strchr(p, '\\')) != NULL)
102 n = p - DirPath + 1;
103 memcpy(path, DirPath, n);
104 path[n] = '\0';
105 if( !CreateDirectoryA(path, NULL) &&
106 (GetLastError() != ERROR_ALREADY_EXISTS))
107 return FALSE;
108 p++;
110 if (GetLastError() == ERROR_ALREADY_EXISTS)
111 SetLastError(ERROR_SUCCESS);
113 return TRUE;
116 /******************************************************************
117 * SymMatchFileName (DBGHELP.@)
120 BOOL WINAPI SymMatchFileName(char* file, char* match,
121 char** filestop, char** matchstop)
123 char* fptr;
124 char* mptr;
126 TRACE("(%s %s %p %p)\n", file, match, filestop, matchstop);
128 fptr = file + strlen(file) - 1;
129 mptr = match + strlen(match) - 1;
131 while (fptr >= file && mptr >= match)
133 if (toupper(*fptr) != toupper(*mptr) && !(is_sep(*fptr) && is_sep(*mptr)))
134 break;
135 fptr--; mptr--;
137 if (filestop) *filestop = fptr;
138 if (matchstop) *matchstop = mptr;
140 return mptr == match - 1;
143 static BOOL do_search(const char* file, char* buffer,
144 PENUMDIRTREE_CALLBACK cb, void* user)
146 HANDLE h;
147 WIN32_FIND_DATAA fd;
148 unsigned pos;
149 BOOL found = FALSE;
151 pos = strlen(buffer);
152 if (buffer[pos - 1] != '\\') buffer[pos++] = '\\';
153 strcpy(buffer + pos, "*.*");
154 if ((h = FindFirstFileA(buffer, &fd)) == INVALID_HANDLE_VALUE)
155 return FALSE;
156 /* doc doesn't specify how the tree is enumerated...
157 * doing a depth first based on, but may be wrong
161 if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, "..")) continue;
163 strcpy(buffer + pos, fd.cFileName);
164 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
165 found = do_search(file, buffer, cb, user);
166 else if (SymMatchFileName(buffer, (char*)file, NULL, NULL))
168 if (!cb || cb(buffer, user)) found = TRUE;
170 } while (!found && FindNextFileA(h, &fd));
171 if (!found) buffer[--pos] = '\0';
172 FindClose(h);
174 return found;
177 /***********************************************************************
178 * SearchTreeForFile (DBGHELP.@)
180 BOOL WINAPI SearchTreeForFile(LPSTR root, LPSTR file, LPSTR buffer)
182 TRACE("(%s, %s, %p)\n",
183 debugstr_a(root), debugstr_a(file), buffer);
184 strcpy(buffer, root);
185 return do_search(file, buffer, NULL, NULL);
188 /******************************************************************
189 * EnumDirTree (DBGHELP.@)
193 BOOL WINAPI EnumDirTree(HANDLE hProcess, PCSTR root, PCSTR file,
194 LPSTR buffer, PENUMDIRTREE_CALLBACK cb, PVOID user)
196 TRACE("(%p %s %s %p %p %p)\n", hProcess, root, file, buffer, cb, user);
198 strcpy(buffer, root);
199 return do_search(file, buffer, cb, user);
202 struct sffip
204 PVOID id;
205 DWORD two;
206 DWORD three;
207 DWORD flags;
208 PFINDFILEINPATHCALLBACK cb;
209 void* user;
212 static BOOL CALLBACK sffip_cb(LPCSTR buffer, void* user)
214 struct sffip* s = (struct sffip*)user;
216 /* FIXME: should check that id/two/three match the file pointed
217 * by buffer
219 /* yes, EnumDirTree and SymFindFileInPath callbacks use the opposite
220 * convention to stop/continue enumeration. sigh.
222 return !(s->cb)((char*)buffer, s->user);
225 /******************************************************************
226 * SymFindFileInPath (DBGHELP.@)
229 BOOL WINAPI SymFindFileInPath(HANDLE hProcess, LPSTR searchPath, LPSTR file,
230 PVOID id, DWORD two, DWORD three, DWORD flags,
231 LPSTR buffer, PFINDFILEINPATHCALLBACK cb,
232 PVOID user)
234 struct sffip s;
235 struct process* pcs = process_find_by_handle(hProcess);
236 char tmp[MAX_PATH];
237 char* ptr;
239 TRACE("(%p %s %s %p %08lx %08lx %08lx %p %p %p)\n",
240 hProcess, searchPath, file, id, two, three, flags,
241 buffer, cb, user);
243 if (!pcs) return FALSE;
244 if (!searchPath) searchPath = pcs->search_path;
246 s.id = id;
247 s.two = two;
248 s.three = three;
249 s.flags = flags;
250 s.cb = cb;
251 s.user = user;
253 file = file_name(file);
255 while (searchPath)
257 ptr = strchr(searchPath, ';');
258 if (ptr)
260 memcpy(tmp, searchPath, ptr - searchPath);
261 tmp[ptr - searchPath] = 0;
262 searchPath = ptr + 1;
264 else
266 strcpy(tmp, searchPath);
267 searchPath = NULL;
269 if (EnumDirTree(hProcess, tmp, file, buffer, sffip_cb, &s)) return TRUE;
271 return FALSE;