dl_posix.c 1.22 KB
Newer Older
1 2 3 4
#if HAVE_CONFIG_H
#include "config.h"
#endif

5 6 7 8
#include "dl.h"
#include "util/macro.h"
#include "util/logging.h"

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include <stdlib.h>
#include <dlfcn.h>

// Note the dlopen takes just the name part. "aacs", internally we
// translate to "libaacs.so" "libaacs.dylib" or "aacs.dll".
void   *dl_dlopen  ( const char* name )
{
    char *path;
    int len;
    void *result;

#ifdef __APPLE__
    len = strlen(name) + 3 + 6 + 1;
    path = (char *) malloc(len);
    if (!path) return NULL;
    snprintf(path, len, "lib%s.dylib", name);
#else
    len = strlen(name) + 3 + 3 + 1;
    path = (char *) malloc(len);
    if (!path) return NULL;
    snprintf(path, len, "lib%s.so", name);
#endif
    DEBUG(DBG_BDPLUS, "searching for library '%s' ...\n", path);
    result = dlopen(path, RTLD_LAZY);
hpi1's avatar
hpi1 committed
33
    if (!result) {
hpi1's avatar
hpi1 committed
34
        DEBUG(DBG_FILE | DBG_CRIT, "can't open library '%s': %s\n", path, dlerror());
hpi1's avatar
hpi1 committed
35
    }
36 37 38 39 40 41
    free(path);
    return result;
}

void   *dl_dlsym   ( void* handle, const char* symbol )
{
hpi1's avatar
hpi1 committed
42 43 44
    void *result = dlsym(handle, symbol);

    if (!result) {
hpi1's avatar
hpi1 committed
45
      DEBUG(DBG_FILE | DBG_CRIT, "dlsym(%p, '%s') failed: %s\n", handle, symbol, dlerror());
hpi1's avatar
hpi1 committed
46 47 48
    }

    return result;
49 50 51 52 53 54
}

int     dl_dlclose ( void* handle )
{
    return dlclose(handle);
}