2002-02-17 05:23:30 +00:00
|
|
|
/* -*- C -*- ***********************************************
|
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
|
|
|
provided that the above copyright notice appear in all copies and that
|
|
|
|
both that copyright notice and this permission notice appear in
|
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
2003-04-22 03:21:42 +00:00
|
|
|
/* This library implements dlopen() - Unix-like dynamic linking
|
|
|
|
* emulation functions for OS/2 using DosLoadModule() and company.
|
|
|
|
*/
|
2002-02-17 05:23:30 +00:00
|
|
|
|
|
|
|
#define INCL_DOS
|
|
|
|
#define INCL_DOSERRORS
|
|
|
|
#define INCL_DOSSESMGR
|
|
|
|
#define INCL_WINPROGRAMLIST
|
|
|
|
#define INCL_WINFRAMEMGR
|
|
|
|
#include <os2.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
typedef struct _track_rec {
|
|
|
|
char *name;
|
|
|
|
HMODULE handle;
|
|
|
|
void *id;
|
|
|
|
struct _track_rec *next;
|
|
|
|
} tDLLchain, *DLLchain;
|
|
|
|
|
2003-04-22 03:21:42 +00:00
|
|
|
static DLLchain dlload = NULL; /* A simple chained list of DLL names */
|
|
|
|
static char dlerr [256]; /* last error text string */
|
2002-02-17 05:23:30 +00:00
|
|
|
static void *last_id;
|
|
|
|
|
|
|
|
static DLLchain find_id(void *id)
|
|
|
|
{
|
|
|
|
DLLchain tmp;
|
|
|
|
|
|
|
|
for (tmp = dlload; tmp; tmp = tmp->next)
|
|
|
|
if (id == tmp->id)
|
2003-04-22 03:21:42 +00:00
|
|
|
return tmp;
|
2002-02-17 05:23:30 +00:00
|
|
|
|
2003-04-22 03:21:42 +00:00
|
|
|
return NULL;
|
2002-02-17 05:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* load a dynamic-link library and return handle */
|
2003-04-22 03:21:42 +00:00
|
|
|
void *dlopen(char *filename, int flags)
|
2002-02-17 05:23:30 +00:00
|
|
|
{
|
|
|
|
HMODULE hm;
|
|
|
|
DLLchain tmp;
|
|
|
|
char err[256];
|
|
|
|
char *errtxt;
|
|
|
|
int rc = 0, set_chain = 0;
|
|
|
|
|
|
|
|
for (tmp = dlload; tmp; tmp = tmp->next)
|
|
|
|
if (strnicmp(tmp->name, filename, 999) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!tmp)
|
|
|
|
{
|
2003-04-22 03:21:42 +00:00
|
|
|
tmp = (DLLchain) malloc(sizeof(tDLLchain));
|
2002-02-17 05:23:30 +00:00
|
|
|
if (!tmp)
|
|
|
|
goto nomem;
|
2003-04-22 03:21:42 +00:00
|
|
|
tmp->name = strdup(filename);
|
2002-02-17 05:23:30 +00:00
|
|
|
tmp->next = dlload;
|
|
|
|
set_chain = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (rc = DosLoadModule((PSZ)&err, sizeof(err), filename, &hm))
|
|
|
|
{
|
|
|
|
case NO_ERROR:
|
|
|
|
tmp->handle = hm;
|
2003-04-22 03:21:42 +00:00
|
|
|
if (set_chain)
|
|
|
|
{
|
|
|
|
do
|
2002-02-17 05:23:30 +00:00
|
|
|
last_id++;
|
2003-04-22 03:21:42 +00:00
|
|
|
while ((last_id == 0) || (find_id(last_id)));
|
2002-02-17 05:23:30 +00:00
|
|
|
tmp->id = last_id;
|
|
|
|
dlload = tmp;
|
|
|
|
}
|
2003-04-22 03:21:42 +00:00
|
|
|
return tmp->id;
|
2002-02-17 05:23:30 +00:00
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
|
|
|
errtxt = "module `%s' not found";
|
|
|
|
break;
|
|
|
|
case ERROR_TOO_MANY_OPEN_FILES:
|
|
|
|
case ERROR_NOT_ENOUGH_MEMORY:
|
|
|
|
case ERROR_SHARING_BUFFER_EXCEEDED:
|
|
|
|
nomem:
|
|
|
|
errtxt = "out of system resources";
|
|
|
|
break;
|
|
|
|
case ERROR_ACCESS_DENIED:
|
|
|
|
errtxt = "access denied";
|
|
|
|
break;
|
|
|
|
case ERROR_BAD_FORMAT:
|
|
|
|
case ERROR_INVALID_SEGMENT_NUMBER:
|
|
|
|
case ERROR_INVALID_ORDINAL:
|
|
|
|
case ERROR_INVALID_MODULETYPE:
|
|
|
|
case ERROR_INVALID_EXE_SIGNATURE:
|
|
|
|
case ERROR_EXE_MARKED_INVALID:
|
|
|
|
case ERROR_ITERATED_DATA_EXCEEDS_64K:
|
|
|
|
case ERROR_INVALID_MINALLOCSIZE:
|
|
|
|
case ERROR_INVALID_SEGDPL:
|
|
|
|
case ERROR_AUTODATASEG_EXCEEDS_64K:
|
|
|
|
case ERROR_RELOCSRC_CHAIN_EXCEEDS_SEGLIMIT:
|
|
|
|
errtxt = "invalid module format";
|
|
|
|
break;
|
|
|
|
case ERROR_INVALID_NAME:
|
|
|
|
errtxt = "filename doesn't match module name";
|
|
|
|
break;
|
|
|
|
case ERROR_SHARING_VIOLATION:
|
|
|
|
case ERROR_LOCK_VIOLATION:
|
|
|
|
errtxt = "sharing violation";
|
|
|
|
break;
|
|
|
|
case ERROR_INIT_ROUTINE_FAILED:
|
|
|
|
errtxt = "module initialization failed";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errtxt = "cause `%s', error code = %d";
|
|
|
|
break;
|
|
|
|
}
|
2003-04-22 03:21:42 +00:00
|
|
|
snprintf(dlerr, sizeof(dlerr), errtxt, &err, rc);
|
|
|
|
if (tmp)
|
|
|
|
{
|
2002-02-17 05:23:30 +00:00
|
|
|
if (tmp->name)
|
|
|
|
free(tmp->name);
|
2003-04-22 03:21:42 +00:00
|
|
|
free(tmp);
|
2002-02-17 05:23:30 +00:00
|
|
|
}
|
2003-04-22 03:21:42 +00:00
|
|
|
return 0;
|
2002-02-17 05:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return a pointer to the `symbol' in DLL */
|
2003-04-22 03:21:42 +00:00
|
|
|
void *dlsym(void *handle, char *symbol)
|
2002-02-17 05:23:30 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
PFN addr;
|
|
|
|
char *errtxt;
|
|
|
|
int symord = 0;
|
2003-04-22 03:21:42 +00:00
|
|
|
DLLchain tmp = find_id(handle);
|
2002-02-17 05:23:30 +00:00
|
|
|
|
|
|
|
if (!tmp)
|
|
|
|
goto inv_handle;
|
|
|
|
|
|
|
|
if (*symbol == '#')
|
2003-04-22 03:21:42 +00:00
|
|
|
symord = atoi(symbol + 1);
|
2002-02-17 05:23:30 +00:00
|
|
|
|
|
|
|
switch (rc = DosQueryProcAddr(tmp->handle, symord, symbol, &addr))
|
|
|
|
{
|
|
|
|
case NO_ERROR:
|
2003-04-22 03:21:42 +00:00
|
|
|
return (void *)addr;
|
2002-02-17 05:23:30 +00:00
|
|
|
case ERROR_INVALID_HANDLE:
|
|
|
|
inv_handle:
|
|
|
|
errtxt = "invalid module handle";
|
|
|
|
break;
|
|
|
|
case ERROR_PROC_NOT_FOUND:
|
|
|
|
case ERROR_INVALID_NAME:
|
|
|
|
errtxt = "no symbol `%s' in module";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errtxt = "symbol `%s', error code = %d";
|
|
|
|
break;
|
|
|
|
}
|
2003-04-22 03:21:42 +00:00
|
|
|
snprintf(dlerr, sizeof(dlerr), errtxt, symbol, rc);
|
|
|
|
return NULL;
|
2002-02-17 05:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* free dynamicaly-linked library */
|
2003-04-22 03:21:42 +00:00
|
|
|
int dlclose(void *handle)
|
2002-02-17 05:23:30 +00:00
|
|
|
{
|
|
|
|
int rc;
|
2003-04-22 03:21:42 +00:00
|
|
|
DLLchain tmp = find_id(handle);
|
2002-02-17 05:23:30 +00:00
|
|
|
|
|
|
|
if (!tmp)
|
|
|
|
goto inv_handle;
|
|
|
|
|
2003-04-22 03:21:42 +00:00
|
|
|
switch (rc = DosFreeModule(tmp->handle))
|
2002-02-17 05:23:30 +00:00
|
|
|
{
|
|
|
|
case NO_ERROR:
|
2003-04-22 03:21:42 +00:00
|
|
|
free(tmp->name);
|
2002-02-17 05:23:30 +00:00
|
|
|
dlload = tmp->next;
|
2003-04-22 03:21:42 +00:00
|
|
|
free(tmp);
|
|
|
|
return 0;
|
2002-02-17 05:23:30 +00:00
|
|
|
case ERROR_INVALID_HANDLE:
|
|
|
|
inv_handle:
|
|
|
|
strcpy(dlerr, "invalid module handle");
|
2003-04-22 03:21:42 +00:00
|
|
|
return -1;
|
2002-02-17 05:23:30 +00:00
|
|
|
case ERROR_INVALID_ACCESS:
|
2003-04-22 03:21:42 +00:00
|
|
|
strcpy(dlerr, "access denied");
|
|
|
|
return -1;
|
2002-02-17 05:23:30 +00:00
|
|
|
default:
|
2003-04-22 03:21:42 +00:00
|
|
|
return -1;
|
2002-02-17 05:23:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-28 14:39:47 +00:00
|
|
|
/* return a string describing last occurred dl error */
|
2002-02-17 05:23:30 +00:00
|
|
|
char *dlerror()
|
|
|
|
{
|
2003-04-22 03:21:42 +00:00
|
|
|
return dlerr;
|
2002-02-17 05:23:30 +00:00
|
|
|
}
|