Files
fastfetch/src/detection/users/users_linux.c
T

53 lines
1.5 KiB
C
Raw Normal View History

2022-10-06 17:47:49 +08:00
#include "fastfetch.h"
#include "users.h"
2023-08-14 10:05:43 +08:00
#if __has_include(<utmpx.h>)
2022-10-06 17:47:49 +08:00
#include <utmpx.h>
#else
//for Android compatibility
#include <utmp.h>
#define utmpx utmp
#define setutxent setutent
#define getutxent getutent
#endif
2024-03-08 09:17:06 +08:00
#ifdef __linux__
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
2022-10-06 17:47:49 +08:00
2024-06-12 14:02:35 +08:00
const char* ffDetectUsers(FFUsersOptions* options, FFlist* users)
2022-10-06 17:47:49 +08:00
{
struct utmpx* n = NULL;
setutxent();
next:
2024-06-12 14:02:35 +08:00
while ((n = getutxent()))
2022-10-06 17:47:49 +08:00
{
2024-06-12 14:02:35 +08:00
if (n->ut_type != USER_PROCESS)
continue;
if (options->myselfOnly && !ffStrbufEqualS(&instance.state.platform.userName, n->ut_user))
2022-10-06 17:47:49 +08:00
continue;
2023-09-08 00:07:14 +08:00
FF_LIST_FOR_EACH(FFUserResult, user, *users)
2022-10-06 17:47:49 +08:00
{
2023-09-08 00:07:14 +08:00
if(ffStrbufEqualS(&user->name, n->ut_user))
2022-10-06 17:47:49 +08:00
goto next;
}
2023-09-08 00:07:14 +08:00
FFUserResult* user = (FFUserResult*) ffListAdd(users);
ffStrbufInitS(&user->name, n->ut_user);
2023-09-08 14:32:01 +08:00
ffStrbufInitS(&user->hostName, n->ut_host);
ffStrbufInitS(&user->sessionName, n->ut_line);
2023-09-08 00:07:14 +08:00
#ifdef __linux__
2024-03-08 09:17:06 +08:00
// https://www.linuxquestions.org/questions/programming-9/get-the-ip-addr-out-from-an-int32_t-value-287687/#post1458622
ffStrbufInitS(&user->clientIp, inet_ntoa((struct in_addr) { .s_addr = (in_addr_t) n->ut_addr_v6[0] }));
#else
2023-09-08 20:03:26 +08:00
ffStrbufInit(&user->clientIp);
2024-03-08 09:17:06 +08:00
#endif
2023-09-08 00:07:14 +08:00
user->loginTime = (uint64_t) n->ut_tv.tv_sec * 1000 + (uint64_t) n->ut_tv.tv_usec / 1000;
2022-10-06 17:47:49 +08:00
}
2023-09-08 00:07:14 +08:00
return NULL;
2022-10-06 17:47:49 +08:00
}