mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Processes: code refactor
This commit is contained in:
@@ -5,6 +5,6 @@
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
uint32_t ffDetectProcesses(FFstrbuf* error);
|
||||
const char* ffDetectProcesses(uint32_t* result);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,15 +6,14 @@
|
||||
#include <sys/user.h>
|
||||
#endif
|
||||
|
||||
uint32_t ffDetectProcesses(FFstrbuf* error)
|
||||
const char* ffDetectProcesses(uint32_t* result)
|
||||
{
|
||||
int request[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
|
||||
size_t length;
|
||||
|
||||
if(sysctl(request, sizeof(request) / sizeof(*request), NULL, &length, NULL, 0) != 0)
|
||||
{
|
||||
ffStrbufAppendS(error, "sysctl() failed");
|
||||
return 0;
|
||||
}
|
||||
return (uint32_t)(length / sizeof(struct kinfo_proc));
|
||||
return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_ALL}) failed";
|
||||
|
||||
*result = (uint32_t)(length / sizeof(struct kinfo_proc));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
|
||||
#include <sys/sysinfo.h>
|
||||
|
||||
uint32_t ffDetectProcesses(FFstrbuf* error)
|
||||
const char* ffDetectProcesses(uint32_t* result)
|
||||
{
|
||||
struct sysinfo info;
|
||||
if(sysinfo(&info) != 0)
|
||||
ffStrbufAppendS(error, "sysinfo() failed");
|
||||
return (uint32_t) info.procs;
|
||||
return "sysinfo() failed";
|
||||
|
||||
*result = (uint32_t) info.procs;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -8,57 +8,47 @@ extern "C" {
|
||||
#include <ntstatus.h>
|
||||
#include <winternl.h>
|
||||
|
||||
uint32_t ffDetectProcesses(FFstrbuf* error)
|
||||
const char* ffDetectProcesses(uint32_t* result)
|
||||
{
|
||||
ULONG size = 0;
|
||||
if(NtQuerySystemInformation(SystemProcessInformation, nullptr, 0, &size) != STATUS_INFO_LENGTH_MISMATCH)
|
||||
{
|
||||
ffStrbufAppendS(error, "NtQuerySystemInformation(SystemProcessInformation, NULL) failed");
|
||||
return 0;
|
||||
}
|
||||
return "NtQuerySystemInformation(SystemProcessInformation, NULL) failed";
|
||||
|
||||
size += sizeof(SystemProcessInformation) * 5; //What if new processes are created during two syscalls?
|
||||
|
||||
SYSTEM_PROCESS_INFORMATION* FF_AUTO_FREE pstart = (SYSTEM_PROCESS_INFORMATION*)malloc(size);
|
||||
if(!pstart)
|
||||
{
|
||||
ffStrbufAppendF(error, "malloc(%u) failed", (unsigned)size);
|
||||
return 0;
|
||||
}
|
||||
return "malloc(size) failed";
|
||||
|
||||
if(!NT_SUCCESS(NtQuerySystemInformation(SystemProcessInformation, pstart, size, nullptr)))
|
||||
{
|
||||
ffStrbufAppendS(error, "NtQuerySystemInformation(SystemProcessInformation, pstart) failed");
|
||||
return 0;
|
||||
}
|
||||
return "NtQuerySystemInformation(SystemProcessInformation, pstart) failed";
|
||||
|
||||
uint32_t result = 1; //Init with 1 because we test for ptr->NextEntryOffset
|
||||
*result = 1; //Init with 1 because we test for ptr->NextEntryOffset
|
||||
for (auto ptr = pstart; ptr->NextEntryOffset; ptr = (SYSTEM_PROCESS_INFORMATION*)((uint8_t*)ptr + ptr->NextEntryOffset))
|
||||
++result;
|
||||
++*result;
|
||||
|
||||
return result;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include "util/windows/wmi.hpp"
|
||||
|
||||
uint32_t ffDetectProcesses(FFstrbuf* error)
|
||||
const char* ffDetectProcesses(uint32_t* result)
|
||||
{
|
||||
FFWmiQuery query(L"SELECT NumberOfProcesses FROM Win32_OperatingSystem", error);
|
||||
FFWmiQuery query(L"SELECT NumberOfProcesses FROM Win32_OperatingSystem", NULL);
|
||||
if(!query)
|
||||
return 0;
|
||||
return "Query WMI service failed";
|
||||
|
||||
if(FFWmiRecord record = query.next())
|
||||
{
|
||||
uint64_t result = 0;
|
||||
record.getUnsigned(L"NumberOfProcesses", &result);
|
||||
return (uint32_t)result;
|
||||
}
|
||||
else
|
||||
{
|
||||
ffStrbufAppendS(error, "No Wmi result returned");
|
||||
return 0;
|
||||
uint64_t value = 0;
|
||||
record.getUnsigned(L"NumberOfProcesses", &value);
|
||||
*result = (uint32_t)value;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return "No Wmi result returned";
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -7,17 +7,14 @@
|
||||
|
||||
void ffPrintProcesses(FFinstance* instance)
|
||||
{
|
||||
FFstrbuf error;
|
||||
ffStrbufInit(&error);
|
||||
uint32_t numProcesses = ffDetectProcesses(&error);
|
||||
uint32_t numProcesses = 0;
|
||||
const char* error = ffDetectProcesses(&numProcesses);
|
||||
|
||||
if(error.length > 0)
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_PROCESSES_MODULE_NAME, 0, &instance->config.processes, "%*s", error.length, error.chars);
|
||||
ffStrbufDestroy(&error);
|
||||
ffPrintError(instance, FF_PROCESSES_MODULE_NAME, 0, &instance->config.processes, "%s", error);
|
||||
return;
|
||||
}
|
||||
ffStrbufDestroy(&error);
|
||||
|
||||
if(instance->config.processes.outputFormat.length == 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user