1 /* -*- C -*- ***********************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* This library implements dlopen() - Unix-like dynamic linking
33 * emulation functions for OS/2 using DosLoadModule() and company.
37 #define INCL_DOSERRORS
38 #define INCL_DOSSESMGR
39 #define INCL_WINPROGRAMLIST
40 #define INCL_WINFRAMEMGR
48 typedef struct _track_rec
{
52 struct _track_rec
*next
;
53 } tDLLchain
, *DLLchain
;
55 static DLLchain dlload
= NULL
; /* A simple chained list of DLL names */
56 static char dlerr
[256]; /* last error text string */
59 static DLLchain
find_id(void *id
)
63 for (tmp
= dlload
; tmp
; tmp
= tmp
->next
)
70 /* load a dynamic-link library and return handle */
71 void *dlopen(char *filename
, int flags
)
77 int rc
= 0, set_chain
= 0;
79 for (tmp
= dlload
; tmp
; tmp
= tmp
->next
)
80 if (strnicmp(tmp
->name
, filename
, 999) == 0)
85 tmp
= (DLLchain
) malloc(sizeof(tDLLchain
));
88 tmp
->name
= strdup(filename
);
93 switch (rc
= DosLoadModule((PSZ
)&err
, sizeof(err
), filename
, &hm
))
101 while ((last_id
== 0) || (find_id(last_id
)));
106 case ERROR_FILE_NOT_FOUND
:
107 case ERROR_PATH_NOT_FOUND
:
108 errtxt
= "module `%s' not found";
110 case ERROR_TOO_MANY_OPEN_FILES
:
111 case ERROR_NOT_ENOUGH_MEMORY
:
112 case ERROR_SHARING_BUFFER_EXCEEDED
:
114 errtxt
= "out of system resources";
116 case ERROR_ACCESS_DENIED
:
117 errtxt
= "access denied";
119 case ERROR_BAD_FORMAT
:
120 case ERROR_INVALID_SEGMENT_NUMBER
:
121 case ERROR_INVALID_ORDINAL
:
122 case ERROR_INVALID_MODULETYPE
:
123 case ERROR_INVALID_EXE_SIGNATURE
:
124 case ERROR_EXE_MARKED_INVALID
:
125 case ERROR_ITERATED_DATA_EXCEEDS_64K
:
126 case ERROR_INVALID_MINALLOCSIZE
:
127 case ERROR_INVALID_SEGDPL
:
128 case ERROR_AUTODATASEG_EXCEEDS_64K
:
129 case ERROR_RELOCSRC_CHAIN_EXCEEDS_SEGLIMIT
:
130 errtxt
= "invalid module format";
132 case ERROR_INVALID_NAME
:
133 errtxt
= "filename doesn't match module name";
135 case ERROR_SHARING_VIOLATION
:
136 case ERROR_LOCK_VIOLATION
:
137 errtxt
= "sharing violation";
139 case ERROR_INIT_ROUTINE_FAILED
:
140 errtxt
= "module initialization failed";
143 errtxt
= "cause `%s', error code = %d";
146 snprintf(dlerr
, sizeof(dlerr
), errtxt
, &err
, rc
);
156 /* return a pointer to the `symbol' in DLL */
157 void *dlsym(void *handle
, char *symbol
)
163 DLLchain tmp
= find_id(handle
);
169 symord
= atoi(symbol
+ 1);
171 switch (rc
= DosQueryProcAddr(tmp
->handle
, symord
, symbol
, &addr
))
175 case ERROR_INVALID_HANDLE
:
177 errtxt
= "invalid module handle";
179 case ERROR_PROC_NOT_FOUND
:
180 case ERROR_INVALID_NAME
:
181 errtxt
= "no symbol `%s' in module";
184 errtxt
= "symbol `%s', error code = %d";
187 snprintf(dlerr
, sizeof(dlerr
), errtxt
, symbol
, rc
);
191 /* free dynamicaly-linked library */
192 int dlclose(void *handle
)
195 DLLchain tmp
= find_id(handle
);
200 switch (rc
= DosFreeModule(tmp
->handle
))
207 case ERROR_INVALID_HANDLE
:
209 strcpy(dlerr
, "invalid module handle");
211 case ERROR_INVALID_ACCESS
:
212 strcpy(dlerr
, "access denied");
219 /* return a string describing last occured dl error */