dl_posix.c 1.24 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
#include <stdlib.h>
#include <dlfcn.h>
11
#include <string.h>
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

// 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
34
    if (!result) {
hpi1's avatar
hpi1 committed
35
        DEBUG(DBG_FILE | DBG_CRIT, "can't open library '%s': %s\n", path, dlerror());
hpi1's avatar
hpi1 committed
36
    }
37 38 39 40 41 42
    free(path);
    return result;
}

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

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

    return result;
50 51 52 53 54 55
}

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