Created
February 2, 2018 23:49
-
-
Save stek29/181e051a85c1b147d3022933f9994e4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/sysctl.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, char* argv[]) { | |
if (argc != 2) { | |
printf("usage: %s sysctlname\n", argv[0]); | |
return 1; | |
} | |
const char* sysctlname = argv[1]; | |
size_t arrsiz = 1; | |
for (const char *c = sysctlname; *c != '\0'; ++c) { | |
if (*c == '.') ++arrsiz; | |
} | |
int *mibs = malloc(arrsiz * sizeof(int)); | |
int ret; | |
ret = sysctlnametomib(sysctlname, mibs, &arrsiz); | |
if (ret == -1) { | |
ret = errno; | |
printf("sysctlnametomib failed: 0x%x (%s)", ret, strerror(ret)); | |
return ret; | |
} | |
printf("arrsiz: %zu\n", arrsiz); | |
for (size_t i = 0; i != arrsiz; ++i) { | |
printf("mib[\t%zu] = %d\n", i, mibs[i]); | |
} | |
free(mibs); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment