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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <syscall.h> #include <syscall.h>
@ -26,51 +28,112 @@ SysCtlEntry SYSCTLTable[] = {
#undef SYSCTL_INT #undef SYSCTL_INT
#undef SYSCTL_BOOL #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 int
main(int argc, const char *argv[]) main(int argc, const char *argv[])
{ {
int i; if (argc == 2 && strcmp(argv[1],"-h") == 0) {
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) {
printf("Usage: sysctl [NODE] [VALUE]\n"); printf("Usage: sysctl [NODE] [VALUE]\n");
return 1; return 1;
} }
for (i = 0; SYSCTLTable[i].type != 0; i++) { if (argc == 2 && strcmp(argv[1],"-d") == 0) {
SysCtlString scStr; printf("%-20s %s\n", "Name", "Description");
SysCtlInt scInt; for (int i = 0; SYSCTLTable[i].type != 0; i++) {
SysCtlBool scBool; printf("%-20s %s\n",
SYSCTLTable[i].path,
SYSCTLTable[i].description);
}
return 0;
}
switch (SYSCTLTable[i].type) { if (argc == 2 || argc == 3) {
case SYSCTL_TYPE_STR: for (int i = 0; SYSCTLTable[i].type != 0; i++) {
OSSysCtl(SYSCTLTable[i].path, &scStr, NULL); if (strcmp(SYSCTLTable[i].path, argv[1]) == 0) {
printf("%s: %s\n", SYSCTLTable[i].path, scStr.value); if (argc == 2)
break; PrintVal(i);
case SYSCTL_TYPE_INT: else
OSSysCtl(SYSCTLTable[i].path, &scInt, NULL); UpdateVal(i, argv[2]);
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;
} }
} }
return 0; return 0;
} }
for (int i = 0; SYSCTLTable[i].type != 0; i++) {
PrintVal(i);
}
return 0;
}