2022-07-22 19:36:00 +02:00
|
|
|
#include "fastfetch.h"
|
2022-07-26 17:29:09 +02:00
|
|
|
#include "detection/title.h"
|
2022-10-12 14:13:20 +08:00
|
|
|
#include "common/thread.h"
|
2022-07-22 19:36:00 +02:00
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2022-09-04 14:37:57 +02:00
|
|
|
#ifndef HOST_NAME_MAX
|
|
|
|
|
#define HOST_NAME_MAX 64
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-09-19 15:57:52 +02:00
|
|
|
#ifdef __linux__
|
2022-10-12 17:53:11 +08:00
|
|
|
#include <netdb.h>
|
|
|
|
|
|
2022-09-19 15:57:52 +02:00
|
|
|
static void detectFQDN(FFTitleResult* title)
|
|
|
|
|
{
|
|
|
|
|
struct addrinfo hints = {0};
|
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
hints.ai_flags = AI_CANONNAME;
|
|
|
|
|
|
|
|
|
|
struct addrinfo* info = NULL;
|
|
|
|
|
|
|
|
|
|
if(getaddrinfo(title->hostname.chars, "80", &hints, &info) == 0)
|
|
|
|
|
{
|
|
|
|
|
struct addrinfo* current = info;
|
|
|
|
|
while(title->fqdn.length == 0 && current != NULL)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendS(&title->fqdn, current->ai_canonname);
|
|
|
|
|
current = current->ai_next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
freeaddrinfo(info);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-07-22 19:36:00 +02:00
|
|
|
const FFTitleResult* ffDetectTitle(const FFinstance* instance)
|
|
|
|
|
{
|
|
|
|
|
static FFTitleResult result;
|
|
|
|
|
|
2022-10-12 14:13:20 +08:00
|
|
|
static FFThreadMutex mutex = FF_THREAD_MUTEX_INITIALIZER;
|
2022-07-22 19:36:00 +02:00
|
|
|
static bool init = false;
|
2022-10-12 14:13:20 +08:00
|
|
|
ffThreadMutexLock(&mutex);
|
2022-07-22 19:36:00 +02:00
|
|
|
if(init)
|
|
|
|
|
{
|
2022-10-12 14:13:20 +08:00
|
|
|
ffThreadMutexUnlock(&mutex);
|
2022-07-22 19:36:00 +02:00
|
|
|
return &result;
|
|
|
|
|
}
|
|
|
|
|
init = true;
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&result.userName);
|
|
|
|
|
ffStrbufAppendS(&result.userName, instance->state.passwd->pw_name);
|
|
|
|
|
|
|
|
|
|
ffStrbufInitA(&result.hostname, HOST_NAME_MAX);
|
|
|
|
|
ffStrbufAppendS(&result.hostname, instance->state.utsname.nodename);
|
|
|
|
|
|
|
|
|
|
ffStrbufInitA(&result.fqdn, HOST_NAME_MAX);
|
2022-10-12 17:53:11 +08:00
|
|
|
#ifdef __linux__
|
2022-09-19 15:57:52 +02:00
|
|
|
detectFQDN(&result);
|
2022-09-04 09:02:11 -07:00
|
|
|
#endif
|
2022-07-22 19:36:00 +02:00
|
|
|
if(result.fqdn.length == 0)
|
|
|
|
|
ffStrbufAppend(&result.fqdn, &result.hostname);
|
|
|
|
|
|
2022-10-12 14:13:20 +08:00
|
|
|
ffThreadMutexUnlock(&mutex);
|
2022-07-22 19:36:00 +02:00
|
|
|
return &result;
|
|
|
|
|
}
|