Last active
July 18, 2022 09:49
-
-
Save x1unix/a53532ac3d10c08c4e7b2760dbc60206 to your computer and use it in GitHub Desktop.
sysconf_dump - Dump sysconf() values
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 <stdio.h> | |
#include <unistd.h> | |
#include <sys/sysctl.h> | |
#include <string.h> | |
#define SC_OPT(KEY) { .id = KEY, .label = #KEY } | |
typedef struct sc_opt { | |
int id; | |
char* label; | |
} sc_opt_t; | |
sc_opt_t vals[] = { | |
SC_OPT(_SC_ARG_MAX), SC_OPT(_SC_CHILD_MAX), SC_OPT(_SC_CLK_TCK), SC_OPT(_SC_IOV_MAX), | |
SC_OPT(_SC_NGROUPS_MAX), SC_OPT(_SC_NPROCESSORS_CONF), SC_OPT(_SC_NPROCESSORS_ONLN), | |
SC_OPT(_SC_OPEN_MAX), SC_OPT(_SC_PAGESIZE), SC_OPT(_SC_STREAM_MAX), | |
SC_OPT(_SC_TZNAME_MAX), SC_OPT(_SC_JOB_CONTROL), SC_OPT(_SC_SAVED_IDS), | |
SC_OPT(_SC_VERSION), SC_OPT(_SC_BC_BASE_MAX), SC_OPT(_SC_BC_DIM_MAX), | |
SC_OPT(_SC_BC_SCALE_MAX), SC_OPT(_SC_BC_STRING_MAX), SC_OPT(_SC_COLL_WEIGHTS_MAX), | |
SC_OPT(_SC_EXPR_NEST_MAX), SC_OPT(_SC_LINE_MAX), SC_OPT(_SC_RE_DUP_MAX), | |
SC_OPT(_SC_2_VERSION), SC_OPT(_SC_2_C_BIND), SC_OPT(_SC_2_C_DEV), | |
SC_OPT(_SC_2_C_DEV), SC_OPT(_SC_2_CHAR_TERM), SC_OPT(_SC_2_FORT_DEV), | |
SC_OPT(_SC_2_FORT_RUN), SC_OPT(_SC_2_LOCALEDEF), SC_OPT(_SC_2_SW_DEV), | |
SC_OPT(_SC_2_UPE), SC_OPT(_SC_PHYS_PAGES) | |
}; | |
const int SC_VALS_LEN = sizeof(vals) / sizeof(sc_opt_t); | |
int help(char *bin_name) | |
{ | |
printf("%s - print configurable system variables\n", bin_name); | |
printf("Usage: sysconf [name|all]\n\n"); | |
printf("See sysconf manpage for more info\n\n"); | |
return 0; | |
} | |
void print_sc_val(sc_opt_t *val) | |
{ | |
int sc_value = sysconf(val->id); | |
printf("%02x %-22s %d\n", val->id, val->label, sc_value); | |
} | |
int dump_all() | |
{ | |
for (int i=0; i<SC_VALS_LEN; i++) | |
print_sc_val(vals+i); | |
return 0; | |
} | |
sc_opt_t *sc_opt_from_str(char *str) | |
{ | |
for(int i=0; i<SC_VALS_LEN; i++) | |
if (!strcmp(vals[i].label, str)) | |
return vals+i; | |
return 0; | |
} | |
char *arg2bin_name(int argc, char **argv) | |
{ | |
if (!argc) return "sysconf"; | |
return argv[0]; | |
} | |
int main(int argc, char **argv) | |
{ | |
char* bin_name = arg2bin_name(argc, argv); | |
if (argc < 2) return help(bin_name); | |
char *name = argv[1]; | |
if (!strcmp(name, "all")) | |
return dump_all(); | |
sc_opt_t *val = sc_opt_from_str(name); | |
if (!val) { | |
printf("Invalid argument: %s, see \"man sysconf\"\n", name); | |
return -1; | |
} | |
print_sc_val(val); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment