sysctl utils

This commit is contained in:
Linus Dierheimer
2022-09-19 21:31:50 +02:00
parent 49cdacd5a9
commit 2a61d29be9
10 changed files with 88 additions and 74 deletions
-35
View File
@@ -358,38 +358,3 @@ void ffSettingsGetAndroidProperty(const char* propName, FFstrbuf* result) {
result->chars[result->length] = '\0';
}
#endif //__ANDROID__
#ifdef __APPLE__
#include <sys/sysctl.h>
void ffSettingsGetAppleProperty(const char* propName, FFstrbuf* result)
{
size_t neededLength;
if(sysctlbyname(propName, NULL, &neededLength, NULL, 0) != 0 || neededLength == 0)
return;
ffStrbufEnsureFree(result, (uint32_t) neededLength);
if(sysctlbyname(propName, result->chars + result->length, &neededLength, NULL, 0) == 0)
result->length += (uint32_t) neededLength;
result->chars[result->length] = '\0';
}
int ffSettingsGetAppleInt(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 ffSettingsGetAppleInt64(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;
}
#endif //__APPLE__
-6
View File
@@ -36,10 +36,4 @@ int ffSettingsGetSQLite3Int(const FFinstance* instance, const char* dbPath, cons
void ffSettingsGetAndroidProperty(const char* propName, FFstrbuf* result);
#endif
#ifdef __APPLE__
void ffSettingsGetAppleProperty(const char* propName, FFstrbuf* result);
int ffSettingsGetAppleInt(const char* propName, int defaultValue);
int64_t ffSettingsGetAppleInt64(const char* propName, int64_t defaultValue);
#endif
#endif
+53
View File
@@ -0,0 +1,53 @@
#include "sysctl.h"
#include <sys/sysctl.h>
void ffSysctlGetString(const char* propName, FFstrbuf* result)
{
size_t neededLength;
if(sysctlbyname(propName, NULL, &neededLength, NULL, 0) != 0 || neededLength == 0)
return;
ffStrbufEnsureFree(result, (uint32_t) neededLength);
if(sysctlbyname(propName, result->chars + result->length, &neededLength, NULL, 0) == 0)
result->length += (uint32_t) neededLength;
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;
}
void* ffSysctlGetData(int* request, int requestLength, size_t* resultLength)
{
if(sysctl(request, requestLength, NULL, &length, NULL, 0) != 0)
return NULL;
void* data = malloc(*length);
if(data == NULL)
return NULL;
if(sysctl(request, requestLength, data, length, NULL, 0) != 0)
{
free(data);
return NULL;
}
return data;
}
+13
View File
@@ -0,0 +1,13 @@
#pragma once
#ifndef FF_INCLUDED_common_sysctl
#define FF_INCLUDED_common_sysctl
#include "fastfetch.h"
void ffSysctlGetString(const char* propName, FFstrbuf* result);
int ffSysctlGetInt(const char* propName, int defaultValue);
int64_t ffSysctlGetInt64(const char* propName, int64_t defaultValue);
void* ffSysctlGetData(int* request, int requestLength, size_t* resultLength);
#endif