Files
fastfetch/src/common/impl/sysctl.c
T

94 lines
2.8 KiB
C
Raw Normal View History

2026-01-06 09:48:38 +08:00
#include "common/sysctl.h"
2022-09-19 21:31:50 +02:00
2022-09-19 21:37:01 +02:00
#include <stdlib.h>
2022-09-19 21:31:50 +02:00
2024-10-04 15:58:17 +08:00
#ifdef __OpenBSD__
2026-03-29 09:28:49 +08:00
const char* ffSysctlGetString(int mib1, int mib2, FFstrbuf* result) {
2024-10-04 15:58:17 +08:00
size_t neededLength;
2026-07-10 18:08:04 +08:00
if (sysctl((int[]) { mib1, mib2 }, 2, nullptr, &neededLength, nullptr, 0) != 0 || neededLength == 1) { // neededLength is 1 for empty strings, because of the null terminator
2026-04-02 20:05:47 +08:00
return "sysctl() length query failed";
2026-03-29 09:28:49 +08:00
}
2024-10-04 15:58:17 +08:00
ffStrbufEnsureFree(result, (uint32_t) neededLength - 1);
2026-07-10 18:08:04 +08:00
if (sysctl((int[]) { mib1, mib2 }, 2, result->chars + result->length, &neededLength, nullptr, 0) != 0) {
2026-04-02 20:05:47 +08:00
return "sysctl() failed to retrieve string data";
2026-03-29 09:28:49 +08:00
}
2024-10-04 15:58:17 +08:00
2026-04-02 20:05:47 +08:00
result->length += (uint32_t) neededLength - 1;
2024-10-04 15:58:17 +08:00
result->chars[result->length] = '\0';
2026-07-10 18:08:04 +08:00
return nullptr;
2024-10-04 15:58:17 +08:00
}
2026-03-29 09:28:49 +08:00
int ffSysctlGetInt(int mib1, int mib2, int defaultValue) {
2024-10-04 15:58:17 +08:00
int result;
size_t neededLength = sizeof(result);
2026-07-10 18:08:04 +08:00
if (sysctl((int[]) { mib1, mib2 }, 2, &result, &neededLength, nullptr, 0) != 0) {
2024-10-04 15:58:17 +08:00
return defaultValue;
2026-03-29 09:28:49 +08:00
}
2024-10-04 15:58:17 +08:00
return result;
}
2026-03-29 09:28:49 +08:00
int64_t ffSysctlGetInt64(int mib1, int mib2, int64_t defaultValue) {
2024-10-04 15:58:17 +08:00
int64_t result;
size_t neededLength = sizeof(result);
2026-07-10 18:08:04 +08:00
if (sysctl((int[]) { mib1, mib2 }, 2, &result, &neededLength, nullptr, 0) != 0) {
2024-10-04 15:58:17 +08:00
return defaultValue;
2026-03-29 09:28:49 +08:00
}
2024-10-04 15:58:17 +08:00
return result;
}
#else
2026-03-29 09:28:49 +08:00
const char* ffSysctlGetString(const char* propName, FFstrbuf* result) {
2022-09-19 21:31:50 +02:00
size_t neededLength;
2026-07-10 18:08:04 +08:00
if (sysctlbyname(propName, nullptr, &neededLength, nullptr, 0) != 0 || neededLength == 1) { // neededLength is 1 for empty strings, because of the null terminator
2022-10-27 10:46:33 +08:00
return "sysctlbyname() failed";
2026-03-29 09:28:49 +08:00
}
2022-09-19 21:31:50 +02:00
2022-09-19 23:16:43 +02:00
ffStrbufEnsureFree(result, (uint32_t) neededLength - 1);
2022-09-19 21:31:50 +02:00
2026-07-10 18:08:04 +08:00
if (sysctlbyname(propName, result->chars + result->length, &neededLength, nullptr, 0) != 0) {
return "sysctlbyname() failed to retrieve string data";
2026-03-29 09:28:49 +08:00
}
2022-09-19 21:31:50 +02:00
result->length += (uint32_t) neededLength - 1;
2022-09-19 21:31:50 +02:00
result->chars[result->length] = '\0';
2022-10-27 10:46:33 +08:00
2026-07-10 18:08:04 +08:00
return nullptr;
2022-09-19 21:31:50 +02:00
}
2026-03-29 09:28:49 +08:00
int ffSysctlGetInt(const char* propName, int defaultValue) {
2022-09-19 21:31:50 +02:00
int result;
size_t neededLength = sizeof(result);
2026-07-10 18:08:04 +08:00
if (sysctlbyname(propName, &result, &neededLength, nullptr, 0) != 0) {
2022-09-19 21:31:50 +02:00
return defaultValue;
2026-03-29 09:28:49 +08:00
}
2022-09-19 21:31:50 +02:00
return result;
}
2026-03-29 09:28:49 +08:00
int64_t ffSysctlGetInt64(const char* propName, int64_t defaultValue) {
2022-09-19 21:31:50 +02:00
int64_t result;
size_t neededLength = sizeof(result);
2026-07-10 18:08:04 +08:00
if (sysctlbyname(propName, &result, &neededLength, nullptr, 0) != 0) {
2022-09-19 21:31:50 +02:00
return defaultValue;
2026-03-29 09:28:49 +08:00
}
2022-09-19 21:31:50 +02:00
return result;
}
2024-10-04 15:58:17 +08:00
#endif // OpenBSD
2022-09-19 21:31:50 +02:00
2026-03-29 09:28:49 +08:00
void* ffSysctlGetData(int* request, u_int requestLength, size_t* resultLength) {
2026-07-10 18:08:04 +08:00
if (sysctl(request, requestLength, nullptr, resultLength, nullptr, 0) != 0) {
return nullptr;
2026-03-29 09:28:49 +08:00
}
2022-09-19 21:31:50 +02:00
2022-09-19 21:37:01 +02:00
void* data = malloc(*resultLength);
2022-09-19 21:31:50 +02:00
2026-07-10 18:08:04 +08:00
if (sysctl(request, requestLength, data, resultLength, nullptr, 0) != 0) {
2022-09-19 21:31:50 +02:00
free(data);
2026-07-10 18:08:04 +08:00
return nullptr;
2022-09-19 21:31:50 +02:00
}
return data;
}