|
/* |
|
* MIT License |
|
* |
|
* Copyright (c) 2019 James Fenn <me@jfenn.me> |
|
* |
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
* of this software and associated documentation files (the "Software"), to deal |
|
* in the Software without restriction, including without limitation the rights |
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
* copies of the Software, and to permit persons to whom the Software is |
|
* furnished to do so, subject to the following conditions: |
|
* |
|
* The above copyright notice and this permission notice shall be included in all |
|
* copies or substantial portions of the Software. |
|
* |
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
* SOFTWARE. |
|
*/ |
|
|
|
#include <string.h> |
|
#include <dlfcn.h> |
|
#include <stdarg.h> |
|
|
|
/** |
|
* Link a method pointer to a specified function in a dynamic |
|
* library. |
|
* |
|
* @param dlib The dynamic library to be linked, created |
|
* with dlopen(). |
|
* @param method_ptr A function pointer to load the method into. |
|
* @param method_name The (string) name of the method to link. |
|
* @return 1 if linking was successful; 0 if not. |
|
*/ |
|
char dlib_link(void* dlib, void* method_ptr, char* method_name) { |
|
void (*method)() = dlsym(dlib, method_name); // load the method |
|
if (method == NULL) { // fail if null |
|
// Error: couldn't load method '%s' from shared library |
|
return 0; |
|
} |
|
|
|
// copy method pointer to the location of the pointer passed in the array |
|
memcpy((void*) method_ptr, &method, sizeof(void*)); |
|
return 1; |
|
} |
|
|
|
/** |
|
* Load the specified dynamic library and assign its |
|
* function pointers to the ones provided. |
|
* |
|
* @param name The name of the shared library. |
|
* @param count The number of methods to load. |
|
* @param ... Variadic arguments alternating between |
|
* function pointers to write to and the |
|
* method names to load into them. |
|
* @return 1 if linking was successful; 0 if not. |
|
*/ |
|
char dlib_import(char* name, unsigned int count, ...) { |
|
va_list methods; |
|
va_start(methods, count); |
|
|
|
void* lib = dlopen(name, RTLD_NOW); // open the library |
|
if (lib == NULL) { // fail if null |
|
// "Error: couldn't open shared library |
|
return 0; |
|
} |
|
|
|
for (unsigned int i = 0; i < count; i++) { |
|
char status = dlib_link( |
|
lib, |
|
(void*) va_arg(methods, char*), |
|
va_arg(methods, char*) |
|
); |
|
|
|
if (status == 0) { |
|
// Error: couldn't load specific method. |
|
return 0; |
|
} |
|
} |
|
|
|
va_end(methods); |
|
|
|
return 1; |
|
} |