Files
fastfetch/src/common/library.c
T

57 lines
1.2 KiB
C
Raw Normal View History

#include "fastfetch.h"
#include <stdarg.h>
2022-06-18 14:25:31 +02:00
#include <dlfcn.h>
2022-04-11 17:41:21 +02:00
static void* libraryLoad(const char* path, int maxVersion)
{
void* result = dlopen(path, RTLD_LAZY);
if(result != NULL)
return result;
FFstrbuf pathbuf;
ffStrbufInitA(&pathbuf, 64);
ffStrbufAppendS(&pathbuf, path);
ffStrbufAppendC(&pathbuf, '.');
for(int i = maxVersion; i >= 0; --i)
{
uint32_t originalLength = pathbuf.length;
ffStrbufAppendF(&pathbuf, "%i", i);
result = dlopen(pathbuf.chars, RTLD_LAZY);
if(result != NULL)
break;
ffStrbufSubstrBefore(&pathbuf, originalLength);
}
ffStrbufDestroy(&pathbuf);
return result;
}
void* ffLibraryLoad(const FFstrbuf* userProvidedName, ...)
{
if(userProvidedName->length > 0)
return dlopen(userProvidedName->chars, RTLD_LAZY);
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;
}