Files
fastfetch/src/common/sysctl.c
T

54 lines
1.4 KiB
C
Raw Normal View History

2022-09-19 21:31:50 +02:00
#include "sysctl.h"
2022-09-19 21:37:01 +02:00
#include <stdlib.h>
2022-09-19 21:31:50 +02:00
void ffSysctlGetString(const char* propName, FFstrbuf* result)
{
size_t neededLength;
2022-09-19 23:16:43 +02:00
if(sysctlbyname(propName, NULL, &neededLength, NULL, 0) != 0 || neededLength == 1) //neededLength is 1 for empty strings, because of the null terminator
2022-09-19 21:31:50 +02:00
return;
2022-09-19 23:16:43 +02:00
ffStrbufEnsureFree(result, (uint32_t) neededLength - 1);
2022-09-19 21:31:50 +02:00
if(sysctlbyname(propName, result->chars + result->length, &neededLength, NULL, 0) == 0)
2022-09-19 23:16:43 +02:00
result->length += (uint32_t) neededLength - 1;
2022-09-19 21:31:50 +02:00
result->chars[result->length] = '\0';
}
int ffSysctlGetInt(const char* propName, int defaultValue)
{
int result;
size_t neededLength = sizeof(result);
if(sysctlbyname(propName, &result, &neededLength, NULL, 0) != 0)
return defaultValue;
return result;
}
int64_t ffSysctlGetInt64(const char* propName, int64_t defaultValue)
{
int64_t result;
size_t neededLength = sizeof(result);
if(sysctlbyname(propName, &result, &neededLength, NULL, 0) != 0)
return defaultValue;
return result;
}
2022-09-19 21:45:39 +02:00
void* ffSysctlGetData(int* request, u_int requestLength, size_t* resultLength)
2022-09-19 21:31:50 +02:00
{
2022-09-19 21:37:01 +02:00
if(sysctl(request, requestLength, NULL, resultLength, NULL, 0) != 0)
2022-09-19 21:31:50 +02:00
return NULL;
2022-09-19 21:37:01 +02:00
void* data = malloc(*resultLength);
2022-09-19 21:31:50 +02:00
if(data == NULL)
return NULL;
2022-09-19 21:37:01 +02:00
if(sysctl(request, requestLength, data, resultLength, NULL, 0) != 0)
2022-09-19 21:31:50 +02:00
{
free(data);
return NULL;
}
return data;
}