2022-09-26 14:57:27 +08:00
|
|
|
#include "fastfetch.h"
|
|
|
|
|
#include "common/printing.h"
|
|
|
|
|
|
2022-09-26 23:23:54 +08:00
|
|
|
#if FF_HAVE_UTMPX_H
|
|
|
|
|
#include <utmpx.h>
|
|
|
|
|
#else
|
|
|
|
|
//for Android compatibility
|
|
|
|
|
#include <utmp.h>
|
|
|
|
|
#define utmpx utmp
|
|
|
|
|
#define setutxent setutent
|
|
|
|
|
#define getutxent getutent
|
|
|
|
|
#endif
|
2022-09-26 14:57:27 +08:00
|
|
|
|
|
|
|
|
#define FF_USERS_MODULE_NAME "Users"
|
|
|
|
|
#define FF_USERS_NUM_FORMAT_ARGS 1
|
|
|
|
|
|
|
|
|
|
void ffPrintUsers(FFinstance* instance)
|
|
|
|
|
{
|
|
|
|
|
struct utmpx* n = NULL;
|
|
|
|
|
setutxent();
|
|
|
|
|
|
|
|
|
|
FFlist users;
|
|
|
|
|
ffListInit(&users, sizeof(n->ut_user));
|
|
|
|
|
|
|
|
|
|
next:
|
|
|
|
|
while((n = getutxent()))
|
|
|
|
|
{
|
|
|
|
|
if(n->ut_type == USER_PROCESS)
|
|
|
|
|
{
|
|
|
|
|
for(uint32_t i = 0; i < users.length; ++i)
|
|
|
|
|
{
|
|
|
|
|
if(strcmp((const char*)ffListGet(&users, i), n->ut_user) == 0)
|
|
|
|
|
goto next;
|
|
|
|
|
}
|
|
|
|
|
strcpy((char*)ffListAdd(&users), n->ut_user);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 23:45:46 +08:00
|
|
|
if(users.length == 0)
|
|
|
|
|
{
|
|
|
|
|
ffListDestroy(&users);
|
|
|
|
|
ffPrintError(instance, FF_USERS_MODULE_NAME, 0, &instance->config.users, "Unable to detect users");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 14:57:27 +08:00
|
|
|
FFstrbuf result;
|
|
|
|
|
ffStrbufInit(&result);
|
|
|
|
|
for(uint32_t i = 0; i < users.length; ++i)
|
|
|
|
|
{
|
|
|
|
|
if(i > 0)
|
|
|
|
|
ffStrbufAppendS(&result, ", ");
|
|
|
|
|
ffStrbufAppendS(&result, (const char*)ffListGet(&users, i));
|
|
|
|
|
}
|
|
|
|
|
ffListDestroy(&users);
|
|
|
|
|
|
2022-09-26 23:45:46 +08:00
|
|
|
if(instance->config.users.outputFormat.length == 0)
|
2022-09-26 14:57:27 +08:00
|
|
|
{
|
2022-09-26 23:45:46 +08:00
|
|
|
ffPrintLogoAndKey(instance, FF_USERS_MODULE_NAME, 0, &instance->config.users.key);
|
2022-09-26 14:57:27 +08:00
|
|
|
puts(result.chars);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-09-26 23:45:46 +08:00
|
|
|
ffPrintFormat(instance, FF_USERS_MODULE_NAME, 0, &instance->config.users, FF_USERS_NUM_FORMAT_ARGS, (FFformatarg[]){
|
2022-09-26 14:57:27 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &result},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffStrbufDestroy(&result);
|
|
|
|
|
}
|