J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * Maintains a list of currently loaded DLLs (Dynamic Link Libraries) |
| 28 | * and their associated handles. Library names are case-insensitive. |
| 29 | */ |
| 30 | |
| 31 | #include <windows.h> |
| 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | #include <errno.h> |
| 35 | |
| 36 | #include "sys.h" |
| 37 | |
| 38 | #include "path_md.h" |
| 39 | |
| 40 | /* |
| 41 | * From system_md.c v1.54 |
| 42 | */ |
| 43 | int |
| 44 | dbgsysGetLastErrorString(char *buf, int len) |
| 45 | { |
| 46 | long errval; |
| 47 | |
| 48 | if ((errval = GetLastError()) != 0) { |
| 49 | /* DOS error */ |
| 50 | int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, |
| 51 | NULL, errval, |
| 52 | 0, buf, len, NULL); |
| 53 | if (n > 3) { |
| 54 | /* Drop final '.', CR, LF */ |
| 55 | if (buf[n - 1] == '\n') n--; |
| 56 | if (buf[n - 1] == '\r') n--; |
| 57 | if (buf[n - 1] == '.') n--; |
| 58 | buf[n] = '\0'; |
| 59 | } |
| 60 | return n; |
| 61 | } |
| 62 | |
| 63 | if (errno != 0) { |
| 64 | /* C runtime error that has no corresponding DOS error code */ |
| 65 | const char *s = strerror(errno); |
| 66 | int n = (int)strlen(s); |
| 67 | if (n >= len) n = len - 1; |
| 68 | strncpy(buf, s, n); |
| 69 | buf[n] = '\0'; |
| 70 | return n; |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Build a machine dependent library name out of a path and file name. |
| 78 | */ |
| 79 | void |
| 80 | dbgsysBuildLibName(char *holder, int holderlen, char *pname, char *fname) |
| 81 | { |
| 82 | const int pnamelen = pname ? (int)strlen(pname) : 0; |
| 83 | const char c = (pnamelen > 0) ? pname[pnamelen-1] : 0; |
| 84 | |
| 85 | /* Quietly truncates on buffer overflow. Should be an error. */ |
| 86 | if (pnamelen + (int)strlen(fname) + 10 > holderlen) { |
| 87 | *holder = '\0'; |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | if (pnamelen == 0) { |
| 92 | sprintf(holder, "%s.dll", fname); |
| 93 | } else if (c == ':' || c == '\\') { |
| 94 | sprintf(holder, "%s%s.dll", pname, fname); |
| 95 | } else { |
| 96 | sprintf(holder, "%s\\%s.dll", pname, fname); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void * |
| 101 | dbgsysLoadLibrary(const char * name, char *err_buf, int err_buflen) |
| 102 | { |
| 103 | void *result = LoadLibrary(name); |
| 104 | if (result == NULL) { |
| 105 | /* Error message is pretty lame, try to make a better guess. */ |
| 106 | long errcode = GetLastError(); |
| 107 | if (errcode == ERROR_MOD_NOT_FOUND) { |
| 108 | strncpy(err_buf, "Can't find dependent libraries", err_buflen-2); |
| 109 | err_buf[err_buflen-1] = '\0'; |
| 110 | } else { |
| 111 | dbgsysGetLastErrorString(err_buf, err_buflen); |
| 112 | } |
| 113 | } |
| 114 | return result; |
| 115 | } |
| 116 | |
| 117 | void dbgsysUnloadLibrary(void *handle) |
| 118 | { |
| 119 | FreeLibrary(handle); |
| 120 | } |
| 121 | |
| 122 | void * dbgsysFindLibraryEntry(void *handle, const char *name) |
| 123 | { |
| 124 | return GetProcAddress(handle, name); |
| 125 | } |