| 1 | n/a | /* -*- C -*- *********************************************** |
|---|
| 2 | n/a | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|---|
| 3 | n/a | The Netherlands. |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | All Rights Reserved |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | Permission to use, copy, modify, and distribute this software and its |
|---|
| 8 | n/a | documentation for any purpose and without fee is hereby granted, |
|---|
| 9 | n/a | provided that the above copyright notice appear in all copies and that |
|---|
| 10 | n/a | both that copyright notice and this permission notice appear in |
|---|
| 11 | n/a | supporting documentation, and that the names of Stichting Mathematisch |
|---|
| 12 | n/a | Centrum or CWI or Corporation for National Research Initiatives or |
|---|
| 13 | n/a | CNRI not be used in advertising or publicity pertaining to |
|---|
| 14 | n/a | distribution of the software without specific, written prior |
|---|
| 15 | n/a | permission. |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | While CWI is the initial source for this software, a modified version |
|---|
| 18 | n/a | is made available by the Corporation for National Research Initiatives |
|---|
| 19 | n/a | (CNRI) at the Internet address ftp://ftp.python.org. |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|---|
| 22 | n/a | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|---|
| 23 | n/a | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|---|
| 24 | n/a | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|---|
| 25 | n/a | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|---|
| 26 | n/a | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|---|
| 27 | n/a | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|---|
| 28 | n/a | PERFORMANCE OF THIS SOFTWARE. |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | ******************************************************************/ |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | /* This library implements dlopen() - Unix-like dynamic linking |
|---|
| 33 | n/a | * emulation functions for OS/2 using DosLoadModule() and company. |
|---|
| 34 | n/a | */ |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | #define INCL_DOS |
|---|
| 37 | n/a | #define INCL_DOSERRORS |
|---|
| 38 | n/a | #define INCL_DOSSESMGR |
|---|
| 39 | n/a | #define INCL_WINPROGRAMLIST |
|---|
| 40 | n/a | #define INCL_WINFRAMEMGR |
|---|
| 41 | n/a | #include <os2.h> |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | #include <stdio.h> |
|---|
| 44 | n/a | #include <stdlib.h> |
|---|
| 45 | n/a | #include <string.h> |
|---|
| 46 | n/a | #include <malloc.h> |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | typedef struct _track_rec { |
|---|
| 49 | n/a | char *name; |
|---|
| 50 | n/a | HMODULE handle; |
|---|
| 51 | n/a | void *id; |
|---|
| 52 | n/a | struct _track_rec *next; |
|---|
| 53 | n/a | } tDLLchain, *DLLchain; |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | static DLLchain dlload = NULL; /* A simple chained list of DLL names */ |
|---|
| 56 | n/a | static char dlerr [256]; /* last error text string */ |
|---|
| 57 | n/a | static void *last_id; |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | static DLLchain find_id(void *id) |
|---|
| 60 | n/a | { |
|---|
| 61 | n/a | DLLchain tmp; |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | for (tmp = dlload; tmp; tmp = tmp->next) |
|---|
| 64 | n/a | if (id == tmp->id) |
|---|
| 65 | n/a | return tmp; |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | return NULL; |
|---|
| 68 | n/a | } |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | /* load a dynamic-link library and return handle */ |
|---|
| 71 | n/a | void *dlopen(char *filename, int flags) |
|---|
| 72 | n/a | { |
|---|
| 73 | n/a | HMODULE hm; |
|---|
| 74 | n/a | DLLchain tmp; |
|---|
| 75 | n/a | char err[256]; |
|---|
| 76 | n/a | char *errtxt; |
|---|
| 77 | n/a | int rc = 0, set_chain = 0; |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | for (tmp = dlload; tmp; tmp = tmp->next) |
|---|
| 80 | n/a | if (strnicmp(tmp->name, filename, 999) == 0) |
|---|
| 81 | n/a | break; |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | if (!tmp) |
|---|
| 84 | n/a | { |
|---|
| 85 | n/a | tmp = (DLLchain) malloc(sizeof(tDLLchain)); |
|---|
| 86 | n/a | if (!tmp) |
|---|
| 87 | n/a | goto nomem; |
|---|
| 88 | n/a | tmp->name = strdup(filename); |
|---|
| 89 | n/a | tmp->next = dlload; |
|---|
| 90 | n/a | set_chain = 1; |
|---|
| 91 | n/a | } |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | switch (rc = DosLoadModule((PSZ)&err, sizeof(err), filename, &hm)) |
|---|
| 94 | n/a | { |
|---|
| 95 | n/a | case NO_ERROR: |
|---|
| 96 | n/a | tmp->handle = hm; |
|---|
| 97 | n/a | if (set_chain) |
|---|
| 98 | n/a | { |
|---|
| 99 | n/a | do |
|---|
| 100 | n/a | last_id++; |
|---|
| 101 | n/a | while ((last_id == 0) || (find_id(last_id))); |
|---|
| 102 | n/a | tmp->id = last_id; |
|---|
| 103 | n/a | dlload = tmp; |
|---|
| 104 | n/a | } |
|---|
| 105 | n/a | return tmp->id; |
|---|
| 106 | n/a | case ERROR_FILE_NOT_FOUND: |
|---|
| 107 | n/a | case ERROR_PATH_NOT_FOUND: |
|---|
| 108 | n/a | errtxt = "module `%s' not found"; |
|---|
| 109 | n/a | break; |
|---|
| 110 | n/a | case ERROR_TOO_MANY_OPEN_FILES: |
|---|
| 111 | n/a | case ERROR_NOT_ENOUGH_MEMORY: |
|---|
| 112 | n/a | case ERROR_SHARING_BUFFER_EXCEEDED: |
|---|
| 113 | n/a | nomem: |
|---|
| 114 | n/a | errtxt = "out of system resources"; |
|---|
| 115 | n/a | break; |
|---|
| 116 | n/a | case ERROR_ACCESS_DENIED: |
|---|
| 117 | n/a | errtxt = "access denied"; |
|---|
| 118 | n/a | break; |
|---|
| 119 | n/a | case ERROR_BAD_FORMAT: |
|---|
| 120 | n/a | case ERROR_INVALID_SEGMENT_NUMBER: |
|---|
| 121 | n/a | case ERROR_INVALID_ORDINAL: |
|---|
| 122 | n/a | case ERROR_INVALID_MODULETYPE: |
|---|
| 123 | n/a | case ERROR_INVALID_EXE_SIGNATURE: |
|---|
| 124 | n/a | case ERROR_EXE_MARKED_INVALID: |
|---|
| 125 | n/a | case ERROR_ITERATED_DATA_EXCEEDS_64K: |
|---|
| 126 | n/a | case ERROR_INVALID_MINALLOCSIZE: |
|---|
| 127 | n/a | case ERROR_INVALID_SEGDPL: |
|---|
| 128 | n/a | case ERROR_AUTODATASEG_EXCEEDS_64K: |
|---|
| 129 | n/a | case ERROR_RELOCSRC_CHAIN_EXCEEDS_SEGLIMIT: |
|---|
| 130 | n/a | errtxt = "invalid module format"; |
|---|
| 131 | n/a | break; |
|---|
| 132 | n/a | case ERROR_INVALID_NAME: |
|---|
| 133 | n/a | errtxt = "filename doesn't match module name"; |
|---|
| 134 | n/a | break; |
|---|
| 135 | n/a | case ERROR_SHARING_VIOLATION: |
|---|
| 136 | n/a | case ERROR_LOCK_VIOLATION: |
|---|
| 137 | n/a | errtxt = "sharing violation"; |
|---|
| 138 | n/a | break; |
|---|
| 139 | n/a | case ERROR_INIT_ROUTINE_FAILED: |
|---|
| 140 | n/a | errtxt = "module initialization failed"; |
|---|
| 141 | n/a | break; |
|---|
| 142 | n/a | default: |
|---|
| 143 | n/a | errtxt = "cause `%s', error code = %d"; |
|---|
| 144 | n/a | break; |
|---|
| 145 | n/a | } |
|---|
| 146 | n/a | snprintf(dlerr, sizeof(dlerr), errtxt, &err, rc); |
|---|
| 147 | n/a | if (tmp) |
|---|
| 148 | n/a | { |
|---|
| 149 | n/a | if (tmp->name) |
|---|
| 150 | n/a | free(tmp->name); |
|---|
| 151 | n/a | free(tmp); |
|---|
| 152 | n/a | } |
|---|
| 153 | n/a | return 0; |
|---|
| 154 | n/a | } |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | /* return a pointer to the `symbol' in DLL */ |
|---|
| 157 | n/a | void *dlsym(void *handle, char *symbol) |
|---|
| 158 | n/a | { |
|---|
| 159 | n/a | int rc = 0; |
|---|
| 160 | n/a | PFN addr; |
|---|
| 161 | n/a | char *errtxt; |
|---|
| 162 | n/a | int symord = 0; |
|---|
| 163 | n/a | DLLchain tmp = find_id(handle); |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | if (!tmp) |
|---|
| 166 | n/a | goto inv_handle; |
|---|
| 167 | n/a | |
|---|
| 168 | n/a | if (*symbol == '#') |
|---|
| 169 | n/a | symord = atoi(symbol + 1); |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | switch (rc = DosQueryProcAddr(tmp->handle, symord, symbol, &addr)) |
|---|
| 172 | n/a | { |
|---|
| 173 | n/a | case NO_ERROR: |
|---|
| 174 | n/a | return (void *)addr; |
|---|
| 175 | n/a | case ERROR_INVALID_HANDLE: |
|---|
| 176 | n/a | inv_handle: |
|---|
| 177 | n/a | errtxt = "invalid module handle"; |
|---|
| 178 | n/a | break; |
|---|
| 179 | n/a | case ERROR_PROC_NOT_FOUND: |
|---|
| 180 | n/a | case ERROR_INVALID_NAME: |
|---|
| 181 | n/a | errtxt = "no symbol `%s' in module"; |
|---|
| 182 | n/a | break; |
|---|
| 183 | n/a | default: |
|---|
| 184 | n/a | errtxt = "symbol `%s', error code = %d"; |
|---|
| 185 | n/a | break; |
|---|
| 186 | n/a | } |
|---|
| 187 | n/a | snprintf(dlerr, sizeof(dlerr), errtxt, symbol, rc); |
|---|
| 188 | n/a | return NULL; |
|---|
| 189 | n/a | } |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | /* free dynamically-linked library */ |
|---|
| 192 | n/a | int dlclose(void *handle) |
|---|
| 193 | n/a | { |
|---|
| 194 | n/a | int rc; |
|---|
| 195 | n/a | DLLchain tmp = find_id(handle); |
|---|
| 196 | n/a | |
|---|
| 197 | n/a | if (!tmp) |
|---|
| 198 | n/a | goto inv_handle; |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | switch (rc = DosFreeModule(tmp->handle)) |
|---|
| 201 | n/a | { |
|---|
| 202 | n/a | case NO_ERROR: |
|---|
| 203 | n/a | free(tmp->name); |
|---|
| 204 | n/a | dlload = tmp->next; |
|---|
| 205 | n/a | free(tmp); |
|---|
| 206 | n/a | return 0; |
|---|
| 207 | n/a | case ERROR_INVALID_HANDLE: |
|---|
| 208 | n/a | inv_handle: |
|---|
| 209 | n/a | strcpy(dlerr, "invalid module handle"); |
|---|
| 210 | n/a | return -1; |
|---|
| 211 | n/a | case ERROR_INVALID_ACCESS: |
|---|
| 212 | n/a | strcpy(dlerr, "access denied"); |
|---|
| 213 | n/a | return -1; |
|---|
| 214 | n/a | default: |
|---|
| 215 | n/a | return -1; |
|---|
| 216 | n/a | } |
|---|
| 217 | n/a | } |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | /* return a string describing last occurred dl error */ |
|---|
| 220 | n/a | char *dlerror() |
|---|
| 221 | n/a | { |
|---|
| 222 | n/a | return dlerr; |
|---|
| 223 | n/a | } |
|---|