From 7a6ec996ae523c667ccc0bec24d0aa274b914a49 Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Mon, 21 Aug 2023 17:01:14 -0400 Subject: [PATCH] Cleanup sysctl command --- sbin/sysctl/sysctl.c | 133 +++++++++++++++++++++++++++++++------------ 1 file changed, 98 insertions(+), 35 deletions(-) diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c index e647939..91990b4 100644 --- a/sbin/sysctl/sysctl.c +++ b/sbin/sysctl/sysctl.c @@ -1,5 +1,7 @@ #include +#include +#include #include #include @@ -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;