27
27
#include <dlfcn.h>
28
28
#include <err.h>
29
29
#include <sysexits.h>
30
+ #include <sys/types.h>
31
+ #include <sys/stat.h>
32
+ #include <unistd.h>
30
33
31
34
#include "ccpp_ipd.h"
32
35
@@ -56,19 +59,27 @@ ccpp_ipd_open(const char *scheme, const char *lib, const char *ver,
56
59
int i = 0 ;
57
60
int n = 0 ;
58
61
const char cap [] = "_cap" ;
62
+ const char * l = NULL ;
59
63
char * library = NULL ;
60
64
char * scheme_cap = NULL ;
61
65
char * error = NULL ;
66
+ struct stat sbuf = {0 };
62
67
63
- /* Generate the library name with the platform suffix */
64
- n = (strlen (prefix ) + strlen (lib ) + strlen (suffix ) + strlen (ver ) + 2 )
65
- * sizeof (char );
66
- library = malloc (n );
67
- memset (library , 0 , n );
68
- if (strcmp (ver , "" ) != 0 ) {
69
- snprintf (library , n , "%s%s.%s%s" , prefix , lib , ver , suffix );
68
+ /* Did we get an actual library file? */
69
+ if (stat (lib , & sbuf ) == 0 ) {
70
+ l = lib ;
70
71
} else {
71
- snprintf (library , n , "%s%s%s" , prefix , lib , suffix );
72
+ /* Generate the library name with the platform suffix */
73
+ n = (strlen (prefix ) + strlen (lib ) + strlen (suffix ) + strlen (ver ) + 2 )
74
+ * sizeof (char );
75
+ library = malloc (n );
76
+ memset (library , 0 , n );
77
+ if (strcmp (ver , "" ) != 0 ) {
78
+ snprintf (library , n , "%s%s.%s%s" , prefix , lib , ver , suffix );
79
+ } else {
80
+ snprintf (library , n , "%s%s%s" , prefix , lib , suffix );
81
+ }
82
+ l = library ;
72
83
}
73
84
74
85
/* Generate the scheme cap function name */
@@ -83,16 +94,13 @@ ccpp_ipd_open(const char *scheme, const char *lib, const char *ver,
83
94
strncat (scheme_cap , cap , n );
84
95
85
96
/* Open a handle to the library */
86
- * lhdl = dlopen (library , RTLD_NOW );
97
+ * lhdl = dlopen (l , RTLD_NOW );
87
98
if (!* lhdl ) {
88
99
warnx ("%s" , dlerror ());
89
100
return (EXIT_FAILURE );
90
101
}
91
102
92
103
dlerror ();
93
- /*
94
- *(void **)(&(*shdl)) = dlsym(*lhdl, scheme_cap);
95
- */
96
104
* (void * * )shdl = dlsym (* lhdl , scheme_cap );
97
105
if ((error = dlerror ()) != NULL ) {
98
106
warnx ("%s" , error );
0 commit comments