diff options
-rw-r--r-- | src/library/loader.c | 33 | ||||
-rw-r--r-- | tests/library/test_loader.c | 2 |
2 files changed, 13 insertions, 22 deletions
diff --git a/src/library/loader.c b/src/library/loader.c index 912afa4..3e58e0f 100644 --- a/src/library/loader.c +++ b/src/library/loader.c @@ -22,13 +22,18 @@ #include "port/stdlib.h" /* for malloc, free */ #include "port/stdio.h" /* for snprintf */ #include "port/gettext.h" /* for i18n */ +#include "port/string.h" /* for strncpy */ #ifdef HAVE_DLFCN #include <dlfcn.h> /* for dlopen. dlclose functions */ #endif +#define INTERNAL_ERRBUF_SIZE 256 + struct wolf_library_t { - void *handle; /**< the OS handle for the library */ + void *handle; /**< the OS handle for the library */ + char errbuf[INTERNAL_ERRBUF_SIZE]; /**< internal buffer to hold the error message + * we get with dlerror */ }; wolf_library_p wolf_library_load( const char *name, wolf_error_t *error ) { @@ -49,8 +54,9 @@ wolf_library_p wolf_library_load( const char *name, wolf_error_t *error ) { l->handle = dlopen( name, flags ); if( l->handle == NULL ) { + strncpy( l->errbuf, dlerror( ), INTERNAL_ERRBUF_SIZE ); *error = WOLF_ERR_INTERNAL; - return NULL; + return l; } #else #error Not using DLFCN as shared loader. Port first! @@ -69,7 +75,7 @@ wolf_error_t wolf_library_unload( wolf_library_p l ) { res = dlclose( l->handle ); if( res != 0 ) { - return WOLF_ERR_INVALID_STATE; + return WOLF_ERR_INTERNAL; } free( l ); @@ -78,26 +84,11 @@ wolf_error_t wolf_library_unload( wolf_library_p l ) { return WOLF_OK; } -char *wolf_libary_errmsg( const wolf_error_t error, const wolf_library_p library, char *buf, size_t buflen ) { - WOLF_UNUSED( library ); +char *wolf_libary_errmsg( const wolf_error_t error, const wolf_library_p l, char *buf, size_t buflen ) { (void)wolf_error_msg( error, buf, buflen ); - -#if 0 - switch( error ) { - /* TODO: we repeat ourself here! Have a function in errors.h for generic mapping? */ - case WOLF_ERR_OUT_OF_MEMORY: - snprintf( buf, buflen, _( "Out of memory" ) ); - break; - - case WOLF_ERR_INVALID_STATE: - snprintf( buf, buflen, _( "Invalid state" ) ); - break; - - default: - snprintf( buf, buflen, _( "<unknown error>" ) ); - break; + if( error == WOLF_ERR_INTERNAL ) { + strncpy( buf + strlen( buf ), l->errbuf, buflen - strlen( buf ) ); } -#endif return buf; } diff --git a/tests/library/test_loader.c b/tests/library/test_loader.c index fd07ed4..f6f78dd 100644 --- a/tests/library/test_loader.c +++ b/tests/library/test_loader.c @@ -26,7 +26,7 @@ int main( void ) { char errbuf[512]; library = wolf_library_load( "testlib.so", &error ); - if( library == NULL ) { + if( error != WOLF_OK ) { fprintf( stderr, "Error loading the library: %s (%d)\n", wolf_libary_errmsg( error, library, errbuf, 512 ), error ); return EXIT_FAILURE; |