FreeBASIC  0.91.0
sys_dylib.c
Go to the documentation of this file.
1 /* Dynamic library loading functions */
2 
3 #include "../fb.h"
4 #include "fb_private_console.h"
5 #include <dlfcn.h>
6 
8 {
9  void *res = NULL;
10  int i;
11  char libname[MAX_PATH];
12  char *libnameformat[] = { "%s",
13  "lib%s",
14  "lib%s.so",
15  "./%s",
16  "./lib%s",
17  "./lib%s.so",
18  NULL };
19 
20  // Just in case the shared lib is an FB one, temporarily reset the
21  // terminal, to let the 2nd rtlib capture the original terminal state.
22  // That way both rtlibs can restore the terminal properly on exit.
23  // Note: The shared lib rtlib exits *after* the program rtlib, in case
24  // the user forgot to dylibfree().
25  FB_LOCK( );
27  FB_UNLOCK( );
28 
29  libname[MAX_PATH-1] = '\0';
30  if( (library) && (library->data) ) {
31  for( i = 0; libnameformat[i]; i++ ) {
32  snprintf( libname, MAX_PATH-1, libnameformat[i], library->data );
33  fb_hConvertPath( libname );
34  res = dlopen( libname, RTLD_LAZY );
35  if( res )
36  break;
37  }
38  }
39 
40  /* del if temp */
41  fb_hStrDelTemp( library );
42 
43  FB_LOCK( );
45  FB_UNLOCK( );
46 
47  return res;
48 }
49 
50 FBCALL void *fb_DylibSymbol( void *library, FBSTRING *symbol )
51 {
52  void *proc = NULL;
53 
54  if( library == NULL )
55  library = dlopen( NULL, RTLD_LAZY );
56 
57  if( (symbol) && (symbol->data) )
58  proc = dlsym( library, symbol->data );
59 
60  /* del if temp */
61  fb_hStrDelTemp( symbol );
62 
63  return proc;
64 }
65 
66 FBCALL void *fb_DylibSymbolByOrd( void *library, short int symbol )
67 {
68  /* Not applicable to Linux */
69  return NULL;
70 }
71 
73 {
74  // See above; if it's an FB lib it will restore the terminal state
75  // on shutdown
76  FB_LOCK( );
78  FB_UNLOCK( );
79 
80  dlclose( library );
81 
82  FB_LOCK( );
84  FB_UNLOCK( );
85 }