Cleanup sysctl command

This commit is contained in:
Ali Mashtizadeh 2023-08-21 17:01:14 -04:00
parent c4f5deba68
commit 7a6ec996ae

View File

@ -1,5 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <syscall.h>
@ -26,49 +28,110 @@ SysCtlEntry SYSCTLTable[] = {
#undef SYSCTL_INT
#undef SYSCTL_BOOL
void
PrintVal(int idx)
{
switch (SYSCTLTable[idx].type) {
case SYSCTL_TYPE_STR: {
SysCtlString scStr;
OSSysCtl(SYSCTLTable[idx].path, &scStr, NULL);
printf("%s: %s\n", SYSCTLTable[idx].path, scStr.value);
break;
}
case SYSCTL_TYPE_INT: {
SysCtlInt scInt;
OSSysCtl(SYSCTLTable[idx].path, &scInt, NULL);
printf("%s: %ld\n", SYSCTLTable[idx].path, scInt.value);
break;
}
case SYSCTL_TYPE_BOOL: {
SysCtlBool scBool;
OSSysCtl(SYSCTLTable[idx].path, &scBool, NULL);
printf("%s: %s\n", SYSCTLTable[idx].path,
scBool.value ? "true" : "false");
break;
}
default:
printf("%s: Unsupported type\n", SYSCTLTable[idx].path);
break;
}
}
void
UpdateVal(int idx, const char *val)
{
switch (SYSCTLTable[idx].type) {
case SYSCTL_TYPE_STR: {
SysCtlString scStr;
strncpy(scStr.value, val, sizeof(scStr.value) - 1);
OSSysCtl(SYSCTLTable[idx].path, NULL, &scStr);
break;
}
case SYSCTL_TYPE_INT: {
SysCtlInt scInt;
scInt.value = atoi(val);
printf("%ld\n", scInt.value);
OSSysCtl(SYSCTLTable[idx].path, NULL, &scInt);
break;
}
case SYSCTL_TYPE_BOOL: {
SysCtlBool scBool;
if (strcmp(val,"true") == 0)
scBool.value = true;
else if (strcmp(val,"false") == 0)
scBool.value = false;
else {
printf("Value must be true or false\n");
exit(1);
}
OSSysCtl(SYSCTLTable[idx].path, NULL, &scBool);
break;
}
default:
printf("%s: Unsupported type\n", SYSCTLTable[idx].path);
break;
}
}
int
main(int argc, const char *argv[])
{
int i;
printf("%d\n", argc);
if (true) {
printf("%-20s %s\n", "Name", "Description");
for (i = 0; SYSCTLTable[i].type != 0; i++) {
printf("%-20s %s\n",
SYSCTLTable[i].path,
SYSCTLTable[i].description);
}
}
if (false) {
if (argc == 2 && strcmp(argv[1],"-h") == 0) {
printf("Usage: sysctl [NODE] [VALUE]\n");
return 1;
}
for (i = 0; SYSCTLTable[i].type != 0; i++) {
SysCtlString scStr;
SysCtlInt scInt;
SysCtlBool scBool;
switch (SYSCTLTable[i].type) {
case SYSCTL_TYPE_STR:
OSSysCtl(SYSCTLTable[i].path, &scStr, NULL);
printf("%s: %s\n", SYSCTLTable[i].path, scStr.value);
break;
case SYSCTL_TYPE_INT:
OSSysCtl(SYSCTLTable[i].path, &scInt, NULL);
printf("%s: %ld\n", SYSCTLTable[i].path, scInt.value);
break;
case SYSCTL_TYPE_BOOL:
OSSysCtl(SYSCTLTable[i].path, &scBool, NULL);
printf("%s: %s\n", SYSCTLTable[i].path,
scBool.value ? "true" : "false");
break;
default:
printf("%s: Unsupported type\n", SYSCTLTable[i].path);
break;
if (argc == 2 && strcmp(argv[1],"-d") == 0) {
printf("%-20s %s\n", "Name", "Description");
for (int i = 0; SYSCTLTable[i].type != 0; i++) {
printf("%-20s %s\n",
SYSCTLTable[i].path,
SYSCTLTable[i].description);
}
return 0;
}
if (argc == 2 || argc == 3) {
for (int i = 0; SYSCTLTable[i].type != 0; i++) {
if (strcmp(SYSCTLTable[i].path, argv[1]) == 0) {
if (argc == 2)
PrintVal(i);
else
UpdateVal(i, argv[2]);
}
}
return 0;
}
for (int i = 0; SYSCTLTable[i].type != 0; i++) {
PrintVal(i);
}
return 0;