Changeset View
Changeset View
Standalone View
Standalone View
head/sbin/sysctl/sysctl.c
Show First 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | |||||
static int oidfmt(int *, int, char *, u_int *); | static int oidfmt(int *, int, char *, u_int *); | ||||
static int parsefile(const char *); | static int parsefile(const char *); | ||||
static int parse(const char *, int); | static int parse(const char *, int); | ||||
static int show_var(int *, int); | static int show_var(int *, int); | ||||
static int sysctl_all(int *oid, int len); | static int sysctl_all(int *oid, int len); | ||||
static int name2oid(const char *, int *); | static int name2oid(const char *, int *); | ||||
static int strIKtoi(const char *, char **); | static int strIKtoi(const char *, char **, const char *); | ||||
static int ctl_sign[CTLTYPE+1] = { | static int ctl_sign[CTLTYPE+1] = { | ||||
[CTLTYPE_INT] = 1, | [CTLTYPE_INT] = 1, | ||||
[CTLTYPE_LONG] = 1, | [CTLTYPE_LONG] = 1, | ||||
[CTLTYPE_S64] = 1, | [CTLTYPE_S64] = 1, | ||||
}; | }; | ||||
static int ctl_size[CTLTYPE+1] = { | static int ctl_size[CTLTYPE+1] = { | ||||
▲ Show 20 Lines • Show All 238 Lines • ▼ Show 20 Lines | default: | ||||
kind & CTLTYPE, line); | kind & CTLTYPE, line); | ||||
return (1); | return (1); | ||||
} | } | ||||
errno = 0; | errno = 0; | ||||
switch (kind & CTLTYPE) { | switch (kind & CTLTYPE) { | ||||
case CTLTYPE_INT: | case CTLTYPE_INT: | ||||
if (strcmp(fmt, "IK") == 0) | if (strncmp(fmt, "IK", 2) == 0) | ||||
intval = strIKtoi(newvalstr, &endptr); | intval = strIKtoi(newvalstr, &endptr, fmt); | ||||
else | else | ||||
intval = (int)strtol(newvalstr, &endptr, | intval = (int)strtol(newvalstr, &endptr, | ||||
0); | 0); | ||||
newval = &intval; | newval = &intval; | ||||
newsize = sizeof(intval); | newsize = sizeof(intval); | ||||
break; | break; | ||||
case CTLTYPE_UINT: | case CTLTYPE_UINT: | ||||
uintval = (int) strtoul(newvalstr, &endptr, 0); | uintval = (int) strtoul(newvalstr, &endptr, 0); | ||||
▲ Show 20 Lines • Show All 312 Lines • ▼ Show 20 Lines | for (smap = p; smap < end; smap++) | ||||
printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx", | printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx", | ||||
smap->type, smap->xattr, (uintmax_t)smap->base, | smap->type, smap->xattr, (uintmax_t)smap->base, | ||||
(uintmax_t)smap->length); | (uintmax_t)smap->length); | ||||
return (0); | return (0); | ||||
} | } | ||||
#endif | #endif | ||||
static int | static int | ||||
strIKtoi(const char *str, char **endptrp) | strIKtoi(const char *str, char **endptrp, const char *fmt) | ||||
{ | { | ||||
int kelv; | int kelv; | ||||
float temp; | float temp; | ||||
size_t len; | size_t len; | ||||
const char *p; | const char *p; | ||||
int prec, i; | |||||
assert(errno == 0); | assert(errno == 0); | ||||
len = strlen(str); | len = strlen(str); | ||||
/* caller already checked this */ | /* caller already checked this */ | ||||
assert(len > 0); | assert(len > 0); | ||||
/* | |||||
* A format of "IK" is in deciKelvin. A format of "IK3" is in | |||||
* milliKelvin. The single digit following IK is log10 of the | |||||
* multiplying factor to convert Kelvin into the untis of this sysctl, | |||||
* or the dividing factor to convert the sysctl value to Kelvin. Numbers | |||||
* larger than 6 will run into precision issues with 32-bit integers. | |||||
* Characters that aren't ASCII digits after the 'K' are ignored. No | |||||
* localization is present because this is an interface from the kernel | |||||
* to this program (eg not an end-user interface), so isdigit() isn't | |||||
* used here. | |||||
*/ | |||||
if (fmt[2] != '\0' && fmt[2] >= '0' && fmt[2] <= '9') | |||||
prec = fmt[2] - '0'; | |||||
else | |||||
prec = 1; | |||||
p = &str[len - 1]; | p = &str[len - 1]; | ||||
if (*p == 'C' || *p == 'F') { | if (*p == 'C' || *p == 'F' || *p == 'K') { | ||||
temp = strtof(str, endptrp); | temp = strtof(str, endptrp); | ||||
if (*endptrp != str && *endptrp == p && errno == 0) { | if (*endptrp != str && *endptrp == p && errno == 0) { | ||||
if (*p == 'F') | if (*p == 'F') | ||||
temp = (temp - 32) * 5 / 9; | temp = (temp - 32) * 5 / 9; | ||||
*endptrp = NULL; | *endptrp = NULL; | ||||
return (temp * 10 + 2732); | if (*p != 'K') | ||||
temp += 273.15; | |||||
for (i = 0; i < prec; i++) | |||||
temp *= 10.0; | |||||
return ((int)(temp + 0.5)); | |||||
} | } | ||||
} else { | } else { | ||||
/* No unit specified -> treat it as a raw number */ | |||||
kelv = (int)strtol(str, endptrp, 10); | kelv = (int)strtol(str, endptrp, 10); | ||||
if (*endptrp != str && *endptrp == p && errno == 0) { | if (*endptrp != str && *endptrp == p && errno == 0) { | ||||
*endptrp = NULL; | *endptrp = NULL; | ||||
return (kelv); | return (kelv); | ||||
} | } | ||||
} | } | ||||
errno = ERANGE; | errno = ERANGE; | ||||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | show_var(int *oid, int nlen) | ||||
const char *sep, *sep1; | const char *sep, *sep1; | ||||
int qoid[CTL_MAXNAME+2]; | int qoid[CTL_MAXNAME+2]; | ||||
uintmax_t umv; | uintmax_t umv; | ||||
intmax_t mv; | intmax_t mv; | ||||
int i, hexlen, sign, ctltype; | int i, hexlen, sign, ctltype; | ||||
size_t intlen; | size_t intlen; | ||||
size_t j, len; | size_t j, len; | ||||
u_int kind; | u_int kind; | ||||
float base; | |||||
int (*func)(size_t, void *); | int (*func)(size_t, void *); | ||||
int prec; | |||||
/* Silence GCC. */ | /* Silence GCC. */ | ||||
umv = mv = intlen = 0; | umv = mv = intlen = 0; | ||||
bzero(buf, BUFSIZ); | bzero(buf, BUFSIZ); | ||||
bzero(fmt, BUFSIZ); | bzero(fmt, BUFSIZ); | ||||
bzero(name, BUFSIZ); | bzero(name, BUFSIZ); | ||||
qoid[0] = 0; | qoid[0] = 0; | ||||
▲ Show 20 Lines • Show All 104 Lines • ▼ Show 20 Lines | while (len >= intlen) { | ||||
fputs(sep1, stdout); | fputs(sep1, stdout); | ||||
if (xflag) | if (xflag) | ||||
printf("%#0*jx", hexlen, umv); | printf("%#0*jx", hexlen, umv); | ||||
else if (!sign) | else if (!sign) | ||||
printf(hflag ? "%'ju" : "%ju", umv); | printf(hflag ? "%'ju" : "%ju", umv); | ||||
else if (fmt[1] == 'K') { | else if (fmt[1] == 'K') { | ||||
if (mv < 0) | if (mv < 0) | ||||
printf("%jd", mv); | printf("%jd", mv); | ||||
else | else { | ||||
printf("%.1fC", (mv - 2732.0) / 10); | /* | ||||
* See strIKtoi for details on fmt. | |||||
*/ | |||||
prec = 1; | |||||
if (fmt[2] != '\0') | |||||
prec = fmt[2] - '0'; | |||||
base = 1.0; | |||||
for (int i = 0; i < prec; i++) | |||||
base *= 10.0; | |||||
printf("%.*fC", prec, | |||||
(float)mv / base - 273.15); | |||||
} | |||||
} else | } else | ||||
printf(hflag ? "%'jd" : "%jd", mv); | printf(hflag ? "%'jd" : "%jd", mv); | ||||
sep1 = " "; | sep1 = " "; | ||||
len -= intlen; | len -= intlen; | ||||
p += intlen; | p += intlen; | ||||
} | } | ||||
free(oval); | free(oval); | ||||
return (0); | return (0); | ||||
▲ Show 20 Lines • Show All 92 Lines • Show Last 20 Lines |