mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-01 10:32:35 +01:00
Chore: run clang-format
```bash find . -type f \( -name "*.c" -o -name "*.h" -o -name "*.cpp" -o -name "*.hpp" \) ! -path "src/3rdparty/*" -print0 | xargs -0 clang-format -i ```
This commit is contained in:
@@ -3,8 +3,7 @@
|
||||
#include "fastfetch.h"
|
||||
#include "modules/users/option.h"
|
||||
|
||||
typedef struct FFUserResult
|
||||
{
|
||||
typedef struct FFUserResult {
|
||||
FFstrbuf name;
|
||||
FFstrbuf hostName;
|
||||
FFstrbuf clientIp;
|
||||
|
||||
@@ -6,22 +6,21 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#if FF_HAVE_UTMPX
|
||||
#include <utmpx.h>
|
||||
# include <utmpx.h>
|
||||
#else
|
||||
//for Android compatibility
|
||||
#include <utmp.h>
|
||||
#define utmpx utmp
|
||||
#define setutxent setutent
|
||||
#define getutxent getutent
|
||||
// for Android compatibility
|
||||
# include <utmp.h>
|
||||
# define utmpx utmp
|
||||
# define setutxent setutent
|
||||
# define getutxent getutent
|
||||
#endif
|
||||
#if __linux__ || __GNU__
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
# include <netinet/in.h>
|
||||
# include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
#if __linux__
|
||||
bool detectUserBySystemd(const FFstrbuf* pathUsers, FFlist* users)
|
||||
{
|
||||
bool detectUserBySystemd(const FFstrbuf* pathUsers, FFlist* users) {
|
||||
FF_STRBUF_AUTO_DESTROY state = ffStrbufCreate();
|
||||
FF_STRBUF_AUTO_DESTROY userName = ffStrbufCreate();
|
||||
FF_STRBUF_AUTO_DESTROY loginTime = ffStrbufCreate();
|
||||
@@ -29,12 +28,14 @@ bool detectUserBySystemd(const FFstrbuf* pathUsers, FFlist* users)
|
||||
|
||||
// WARNING: This is private data. Do not parse
|
||||
if (!ffParsePropFileValues(pathUsers->chars, 4, (FFpropquery[]) {
|
||||
{"NAME=", &userName},
|
||||
{"STATE=", &state},
|
||||
{"REALTIME=", &loginTime},
|
||||
{"ONLINE_SESSIONS=", &sessions},
|
||||
}) || !ffStrbufEqualS(&state, "active"))
|
||||
{"NAME=", &userName},
|
||||
{"STATE=", &state},
|
||||
{"REALTIME=", &loginTime},
|
||||
{"ONLINE_SESSIONS=", &sessions},
|
||||
}) ||
|
||||
!ffStrbufEqualS(&state, "active")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
FFUserResult* user = FF_LIST_ADD(FFUserResult, *users);
|
||||
ffStrbufInitMove(&user->name, &userName);
|
||||
@@ -53,8 +54,7 @@ bool detectUserBySystemd(const FFstrbuf* pathUsers, FFlist* users)
|
||||
|
||||
char* token = NULL;
|
||||
size_t n = 0;
|
||||
while (ffStrbufGetdelim(&token, &n, ' ', &sessions))
|
||||
{
|
||||
while (ffStrbufGetdelim(&token, &n, ' ', &sessions)) {
|
||||
ffStrbufSubstrBefore(&pathSessions, pathSessionsBaseLen);
|
||||
ffStrbufAppendS(&pathSessions, token);
|
||||
|
||||
@@ -65,23 +65,21 @@ bool detectUserBySystemd(const FFstrbuf* pathUsers, FFlist* users)
|
||||
|
||||
// WARNING: This is private data. Do not parse
|
||||
if (ffParsePropFileValues(pathSessions.chars, 4, (FFpropquery[]) {
|
||||
{"REMOTE_HOST=", &remoteHost},
|
||||
{"TTY=", &tty},
|
||||
{"SERVICE=", &service},
|
||||
{"REALTIME=", &loginTime},
|
||||
}) && !ffStrbufEqualS(&service, "systemd-user"))
|
||||
{
|
||||
if (remoteHost.length)
|
||||
{
|
||||
{"REMOTE_HOST=", &remoteHost},
|
||||
{"TTY=", &tty},
|
||||
{"SERVICE=", &service},
|
||||
{"REALTIME=", &loginTime},
|
||||
}) &&
|
||||
!ffStrbufEqualS(&service, "systemd-user")) {
|
||||
if (remoteHost.length) {
|
||||
ffStrbufTrimRight(&remoteHost, ']');
|
||||
ffStrbufTrimLeft(&remoteHost, '[');
|
||||
ffStrbufInitMove(&user->hostName, &remoteHost);
|
||||
}
|
||||
else
|
||||
} else {
|
||||
ffStrbufSetStatic(&user->hostName, "localhost");
|
||||
}
|
||||
ffStrbufInitMove(&user->sessionName, tty.length ? &tty : &service);
|
||||
if (loginTime.length)
|
||||
{
|
||||
if (loginTime.length) {
|
||||
ffStrbufSubstrBefore(&loginTime, loginTime.length - 3); // converts us to ms
|
||||
user->loginTime = ffStrbufToUInt(&loginTime, 0);
|
||||
}
|
||||
@@ -92,27 +90,26 @@ bool detectUserBySystemd(const FFstrbuf* pathUsers, FFlist* users)
|
||||
return true;
|
||||
}
|
||||
|
||||
const char* detectBySystemd(FFUsersOptions* options, FFlist* users)
|
||||
{
|
||||
const char* detectBySystemd(FFUsersOptions* options, FFlist* users) {
|
||||
// For some reason, debian/ubuntu no longer updates `/var/run/utmp` (#2064)
|
||||
// Query systemd instead
|
||||
FF_STRBUF_AUTO_DESTROY pathUsers = ffStrbufCreateS("/run/systemd/users/");
|
||||
|
||||
if (options->myselfOnly)
|
||||
{
|
||||
if (options->myselfOnly) {
|
||||
ffStrbufAppendUInt(&pathUsers, instance.state.platform.uid);
|
||||
detectUserBySystemd(&pathUsers, users);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
const uint32_t pathUsersBaseLen = pathUsers.length;
|
||||
FF_AUTO_CLOSE_DIR DIR* dirp = opendir(pathUsers.chars);
|
||||
if (!dirp) return "opendir(\"/run/systemd/users/\") failed";
|
||||
if (!dirp) {
|
||||
return "opendir(\"/run/systemd/users/\") failed";
|
||||
}
|
||||
|
||||
struct dirent* entry;
|
||||
while ((entry = readdir(dirp)))
|
||||
{
|
||||
if (entry->d_type != DT_REG) continue;
|
||||
while ((entry = readdir(dirp))) {
|
||||
if (entry->d_type != DT_REG) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ffStrbufAppendS(&pathUsers, entry->d_name);
|
||||
detectUserBySystemd(&pathUsers, users);
|
||||
@@ -124,8 +121,7 @@ const char* detectBySystemd(FFUsersOptions* options, FFlist* users)
|
||||
#endif
|
||||
|
||||
#if __linux__ || __GNU__
|
||||
static void fillUtmpIpAddr(FFUserResult* user, struct utmpx* n)
|
||||
{
|
||||
static void fillUtmpIpAddr(FFUserResult* user, struct utmpx* n) {
|
||||
bool isIpv6 = false;
|
||||
for (int i = 1; i < 4; ++i) {
|
||||
if (n->ut_addr_v6[i] != 0) {
|
||||
@@ -147,32 +143,28 @@ static void fillUtmpIpAddr(FFUserResult* user, struct utmpx* n)
|
||||
}
|
||||
}
|
||||
#else
|
||||
static void fillUtmpIpAddr(FF_MAYBE_UNUSED FFUserResult* user, FF_MAYBE_UNUSED struct utmpx* n)
|
||||
{
|
||||
static void fillUtmpIpAddr(FF_MAYBE_UNUSED FFUserResult* user, FF_MAYBE_UNUSED struct utmpx* n) {
|
||||
}
|
||||
#endif
|
||||
|
||||
const char* detectByUtmp(FFUsersOptions* options, FFlist* users)
|
||||
{
|
||||
const char* detectByUtmp(FFUsersOptions* options, FFlist* users) {
|
||||
struct utmpx* n = NULL;
|
||||
setutxent();
|
||||
|
||||
next:
|
||||
while ((n = getutxent()))
|
||||
{
|
||||
if (n->ut_type != USER_PROCESS)
|
||||
while ((n = getutxent())) {
|
||||
if (n->ut_type != USER_PROCESS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (options->myselfOnly && !ffStrbufEqualS(&instance.state.platform.userName, n->ut_user))
|
||||
if (options->myselfOnly && !ffStrbufEqualS(&instance.state.platform.userName, n->ut_user)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FF_LIST_FOR_EACH(FFUserResult, user, *users)
|
||||
{
|
||||
if(ffStrbufEqualS(&user->name, n->ut_user))
|
||||
{
|
||||
FF_LIST_FOR_EACH (FFUserResult, user, *users) {
|
||||
if (ffStrbufEqualS(&user->name, n->ut_user)) {
|
||||
uint64_t newLoginTime = (uint64_t) n->ut_tv.tv_sec * 1000 + (uint64_t) n->ut_tv.tv_usec / 1000;
|
||||
if (newLoginTime > user->loginTime)
|
||||
{
|
||||
if (newLoginTime > user->loginTime) {
|
||||
ffStrbufSetS(&user->hostName, n->ut_host);
|
||||
ffStrbufSetS(&user->sessionName, n->ut_line);
|
||||
fillUtmpIpAddr(user, n);
|
||||
@@ -196,14 +188,17 @@ next:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* ffDetectUsers(FFUsersOptions* options, FFlist* users)
|
||||
{
|
||||
const char* ffDetectUsers(FFUsersOptions* options, FFlist* users) {
|
||||
const char* err = detectByUtmp(options, users);
|
||||
if (err) return err;
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
#if __linux__
|
||||
if (users->length == 0) detectBySystemd(options, users);
|
||||
#endif
|
||||
#if __linux__
|
||||
if (users->length == 0) {
|
||||
detectBySystemd(options, users);
|
||||
}
|
||||
#endif
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "fastfetch.h"
|
||||
#include "users.h"
|
||||
|
||||
const char* ffDetectUsers(FFUsersOptions* options, FFlist* users)
|
||||
{
|
||||
const char* ffDetectUsers(FFUsersOptions* options, FFlist* users) {
|
||||
return "Not supported on this platform";
|
||||
}
|
||||
|
||||
@@ -4,24 +4,27 @@
|
||||
|
||||
#include <utmp.h>
|
||||
|
||||
const char* ffDetectUsers(FF_MAYBE_UNUSED FFUsersOptions* options, FFlist* users)
|
||||
{
|
||||
const char* ffDetectUsers(FF_MAYBE_UNUSED FFUsersOptions* options, FFlist* users) {
|
||||
FF_AUTO_CLOSE_FILE FILE* fp = fopen(_PATH_UTMP, "r");
|
||||
if (!fp) return "fopen(_PATH_UTMP, r) failed";
|
||||
if (!fp) {
|
||||
return "fopen(_PATH_UTMP, r) failed";
|
||||
}
|
||||
|
||||
struct utmp n;
|
||||
next:
|
||||
while (fread(&n, sizeof(n), 1, fp) == 1)
|
||||
{
|
||||
if (!n.ut_name[0]) continue;
|
||||
|
||||
if (options->myselfOnly && !ffStrbufEqualS(&instance.state.platform.userName, n.ut_name))
|
||||
while (fread(&n, sizeof(n), 1, fp) == 1) {
|
||||
if (!n.ut_name[0]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FF_LIST_FOR_EACH(FFUserResult, user, *users)
|
||||
{
|
||||
if (ffStrbufEqualS(&user->name, n.ut_name))
|
||||
if (options->myselfOnly && !ffStrbufEqualS(&instance.state.platform.userName, n.ut_name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FF_LIST_FOR_EACH (FFUserResult, user, *users) {
|
||||
if (ffStrbufEqualS(&user->name, n.ut_name)) {
|
||||
goto next;
|
||||
}
|
||||
}
|
||||
|
||||
FFUserResult* user = (FFUserResult*) ffListAdd(users);
|
||||
|
||||
@@ -6,25 +6,26 @@
|
||||
#include <wtsapi32.h>
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
const char* ffDetectUsers(FFUsersOptions* options, FFlist* users)
|
||||
{
|
||||
const char* ffDetectUsers(FFUsersOptions* options, FFlist* users) {
|
||||
WTS_SESSION_INFO_1W* sessionInfo;
|
||||
DWORD sessionCount;
|
||||
DWORD level = 1;
|
||||
|
||||
if (!WTSEnumerateSessionsExW(WTS_CURRENT_SERVER_HANDLE, &level, 0, &sessionInfo, &sessionCount))
|
||||
if (!WTSEnumerateSessionsExW(WTS_CURRENT_SERVER_HANDLE, &level, 0, &sessionInfo, &sessionCount)) {
|
||||
return "WTSEnumerateSessionsW(WTS_CURRENT_SERVER_HANDLE) failed";
|
||||
}
|
||||
|
||||
for (DWORD i = 0; i < sessionCount; i++)
|
||||
{
|
||||
for (DWORD i = 0; i < sessionCount; i++) {
|
||||
WTS_SESSION_INFO_1W* session = &sessionInfo[i];
|
||||
if (session->State != WTSActive)
|
||||
if (session->State != WTSActive) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY userName = ffStrbufCreateWS(session->pUserName);
|
||||
|
||||
if (options->myselfOnly && !ffStrbufEqual(&instance.state.platform.userName, &userName))
|
||||
if (options->myselfOnly && !ffStrbufEqual(&instance.state.platform.userName, &userName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FFUserResult* user = (FFUserResult*) ffListAdd(users);
|
||||
ffStrbufInitMove(&user->name, &userName);
|
||||
@@ -35,14 +36,12 @@ const char* ffDetectUsers(FFUsersOptions* options, FFlist* users)
|
||||
|
||||
DWORD bytes = 0;
|
||||
PWTS_CLIENT_ADDRESS address = NULL;
|
||||
if (WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, session->SessionId, WTSClientAddress, (LPWSTR *) &address, &bytes))
|
||||
{
|
||||
if (address->AddressFamily == AF_INET)
|
||||
if (WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, session->SessionId, WTSClientAddress, (LPWSTR*) &address, &bytes)) {
|
||||
if (address->AddressFamily == AF_INET) {
|
||||
ffStrbufSetF(&user->clientIp, "%u.%u.%u.%u", address->Address[2], address->Address[3], address->Address[4], address->Address[5]);
|
||||
else if (address->AddressFamily == AF_INET6)
|
||||
{
|
||||
} else if (address->AddressFamily == AF_INET6) {
|
||||
char ipStr[INET6_ADDRSTRLEN];
|
||||
const char* end = RtlIpv6AddressToStringA((const IN6_ADDR *) address->Address, ipStr);
|
||||
const char* end = RtlIpv6AddressToStringA((const IN6_ADDR*) address->Address, ipStr);
|
||||
ffStrbufSetNS(&user->clientIp, (uint32_t) (end - ipStr), ipStr);
|
||||
}
|
||||
WTSFreeMemory(address);
|
||||
@@ -50,8 +49,7 @@ const char* ffDetectUsers(FFUsersOptions* options, FFlist* users)
|
||||
|
||||
bytes = 0;
|
||||
PWTSINFOW wtsInfo = NULL;
|
||||
if (WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, session->SessionId, WTSSessionInfo, (LPWSTR *) &wtsInfo, &bytes))
|
||||
{
|
||||
if (WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, session->SessionId, WTSSessionInfo, (LPWSTR*) &wtsInfo, &bytes)) {
|
||||
user->loginTime = ffFileTimeToUnixMs((uint64_t) wtsInfo->LogonTime.QuadPart);
|
||||
WTSFreeMemory(wtsInfo);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user