-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmac-sysctl.cpp
67 lines (58 loc) · 1.85 KB
/
mac-sysctl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//----------------------------------------------------------------------------
//
// Arm64 CPU system registers tools
// Copyright (c) 2023, Thierry Lelegard
// BSD-2-Clause license, see the LICENSE file.
//
// macOS demo program: display Arm features as returned by sysctl().
// Using "-v" (verbose), also display the systcl() OID.
//
//----------------------------------------------------------------------------
#include "strutils.h"
#include <iostream>
#include <vector>
#include <cstddef>
#include <cstdlib>
#include <sys/types.h>
#include <sys/sysctl.h>
// List of hw.optional.arm sysctl.
struct Param {
std::string name;
std::string sysctl;
};
const std::vector<Param> AllParams {
// Automatically generated file:
#include "_sysctl.h"
};
// Program entry point
int main(int argc, char* argv[])
{
const bool verbose = argc > 1 && std::string(argv[1]) == "-v";
size_t name_width = 0;
for (const auto& param : AllParams) {
name_width = std::max(name_width, param.name.length());
}
for (const auto& param : AllParams) {
int value = 0;
size_t len = sizeof(value);
int oid[16];
size_t oidcount = sizeof(oid) / sizeof(oid[0]);
if (::sysctlbyname(param.sysctl.c_str(), &value, &len, nullptr, 0) < 0) {
::perror(param.sysctl.c_str());
}
else if (verbose && ::sysctlnametomib(param.sysctl.c_str(), oid, &oidcount) < 0) {
::perror(param.sysctl.c_str());
}
else {
std::cout << Pad(param.name + " ", name_width + 1) << " " << Pad(YesNo(value), 3, ' ', false);
if (verbose) {
std::cout << " OID:";
for (size_t i = 0; i < oidcount; i++) {
std::cout << " " << oid[i];
}
}
std::cout << std::endl;
}
}
return EXIT_SUCCESS;
}