Files
fastfetch/src/common/library.c
T

84 lines
1.9 KiB
C
Raw Normal View History

#include "fastfetch.h"
#include "common/library.h"
#ifndef FF_DISABLE_DLOPEN
2024-05-23 16:35:16 +08:00
#include <stdarg.h>
2022-09-22 22:02:58 +02:00
//Clang doesn't define __SANITIZE_ADDRESS__ but defines __has_feature(address_sanitizer)
2024-04-26 08:48:58 +08:00
#if !defined(__SANITIZE_ADDRESS__) && defined(__has_feature)
2022-09-22 22:02:58 +02:00
#if __has_feature(address_sanitizer)
#define __SANITIZE_ADDRESS__
#endif
#endif
2022-10-13 17:00:36 +08:00
#ifndef FF_DLOPEN_FLAGS
#ifdef __SANITIZE_ADDRESS__
#define FF_DLOPEN_FLAGS RTLD_LAZY | RTLD_NODELETE
#else
#define FF_DLOPEN_FLAGS RTLD_LAZY
#endif
2022-09-22 22:02:58 +02:00
#endif
2022-04-11 17:41:21 +02:00
static void* libraryLoad(const char* path, int maxVersion)
{
2022-09-22 22:02:58 +02:00
void* result = dlopen(path, FF_DLOPEN_FLAGS);
2022-10-09 02:04:41 +08:00
2022-10-14 16:39:48 +08:00
#ifdef _WIN32
2022-10-09 02:04:41 +08:00
// libX.dll.1 never exists on Windows, while libX-1.dll may exist
FF_UNUSED(maxVersion)
#else
2022-09-14 12:41:43 +02:00
if(result != NULL || maxVersion < 0)
2022-04-11 17:41:21 +02:00
return result;
FF_STRBUF_AUTO_DESTROY pathbuf = ffStrbufCreateA(64);
2022-04-11 17:41:21 +02:00
ffStrbufAppendS(&pathbuf, path);
ffStrbufAppendC(&pathbuf, '.');
for(int i = maxVersion; i >= 0; --i)
{
uint32_t originalLength = pathbuf.length;
ffStrbufAppendF(&pathbuf, "%i", i);
2022-09-22 22:02:58 +02:00
result = dlopen(pathbuf.chars, FF_DLOPEN_FLAGS);
2022-04-11 17:41:21 +02:00
if(result != NULL)
break;
ffStrbufSubstrBefore(&pathbuf, originalLength);
}
2022-10-09 02:04:41 +08:00
#endif
2022-04-11 17:41:21 +02:00
return result;
}
void* ffLibraryLoad(const FFstrbuf* userProvidedName, ...)
{
if(userProvidedName != NULL && userProvidedName->length > 0)
2022-09-22 22:02:58 +02:00
return dlopen(userProvidedName->chars, FF_DLOPEN_FLAGS);
va_list defaultNames;
va_start(defaultNames, userProvidedName);
void* result = NULL;
2022-04-11 17:41:21 +02:00
while(result == NULL)
{
2022-04-11 17:41:21 +02:00
const char* path = va_arg(defaultNames, const char*);
if(path == NULL)
break;
int maxVersion = va_arg(defaultNames, int);
result = libraryLoad(path, maxVersion);
}
va_end(defaultNames);
return result;
}
2024-05-23 16:35:16 +08:00
#endif