FreeBASIC  0.91.0
hdynload.c
Go to the documentation of this file.
1 /* Internal dynamic library functions loading */
2 
3 #include "../fb.h"
4 #include "../fb_private_hdynload.h"
5 
6 #include <dlfcn.h>
7 #define hDylibFree( lib ) dlclose( lib )
8 #define hDylibSymbol( lib, sym ) dlsym( lib, sym )
9 
10 FB_DYLIB fb_hDynLoad(const char *libname, const char **funcname, void **funcptr)
11 {
12  FB_DYLIB lib;
13  ssize_t i;
14 
15  /* First look if library was already statically linked with current executable */
16  if (!(lib = dlopen(NULL, RTLD_LAZY)))
17  return NULL;
18  if (!dlsym(lib, funcname[0])) {
19  dlclose(lib);
20  if (!(lib = dlopen(libname, RTLD_LAZY)))
21  return NULL;
22  }
23 
24  /* Load functions */
25  for (i = 0; funcname[i]; i++) {
26  funcptr[i] = hDylibSymbol(lib, funcname[i]);
27  if (!funcptr[i]) {
28  hDylibFree(lib);
29  return NULL;
30  }
31  }
32 
33  return lib;
34 }
35 
36 int fb_hDynLoadAlso( FB_DYLIB lib, const char **funcname, void **funcptr, ssize_t count )
37 {
38  ssize_t i;
39 
40  /* Load functions */
41  for (i = 0; i < count; i++) {
42  funcptr[i] = hDylibSymbol(lib, funcname[i]);
43  if (!funcptr[i])
44  return -1;
45  }
46 
47  return 0;
48 }
49 
51 {
52  if (*lib) {
53  hDylibFree( *lib );
54  *lib = NULL;
55  }
56 }