mfiutil: Change mfi_autolearn_(period|mode) to write to a FILE

This avoids using fragile logic with snprintf() to build strings.

For the calling code in mfi_show.c, I chose to pass stdout directly
instead of using fmemopen() to write to the temporary buffer since
that is simpler and avoids having to deal with output truncation.

Differential Revision:	https://reviews.freebsd.org/D50881
This commit is contained in:
John Baldwin 2025-07-07 12:36:47 -04:00
parent 96f544bcc1
commit fe1cd734a1
3 changed files with 19 additions and 35 deletions

View file

@ -40,41 +40,23 @@
/* The autolearn period is given in seconds. */
void
mfi_autolearn_period(uint32_t period, char *buf, size_t sz)
mfi_autolearn_period(FILE *fp, uint32_t period)
{
unsigned int d, h;
char *tmp;
d = period / (24 * 3600);
h = (period % (24 * 3600)) / 3600;
tmp = buf;
if (d != 0) {
int fmt_len;
fmt_len = snprintf(buf, sz, "%u day%s", d, d == 1 ? "" : "s");
if (fmt_len < 0) {
*buf = 0;
return;
}
if ((size_t)fmt_len >= sz) {
return;
}
tmp += fmt_len;
sz -= tmp - buf;
if (h != 0) {
fmt_len = snprintf(tmp, sz, ", ");
if (fmt_len < 0 || (size_t)fmt_len >= sz) {
return;
}
tmp += fmt_len;
sz -= 2;
}
fprintf(fp, "%u day%s", d, d == 1 ? "" : "s");
if (h != 0)
fprintf(fp, ", ");
}
if (h != 0)
snprintf(tmp, sz, "%u hour%s", h, h == 1 ? "" : "s");
fprintf(fp, "%u hour%s", h, h == 1 ? "" : "s");
if (d == 0 && h == 0)
snprintf(tmp, sz, "less than 1 hour");
fprintf(fp, "less than 1 hour");
}
/* The time to the next relearn is given in seconds since 1/1/2000. */
@ -96,21 +78,21 @@ mfi_next_learn_time(uint32_t next_learn_time, char *buf, size_t sz)
}
void
mfi_autolearn_mode(uint8_t mode, char *buf, size_t sz)
mfi_autolearn_mode(FILE *fp, uint8_t mode)
{
switch (mode) {
case 0:
snprintf(buf, sz, "enabled");
fprintf(fp, "enabled");
break;
case 1:
snprintf(buf, sz, "disabled");
fprintf(fp, "disabled");
break;
case 2:
snprintf(buf, sz, "warn via event");
fprintf(fp, "warn via event");
break;
default:
snprintf(buf, sz, "mode 0x%02x", mode);
fprintf(fp, "mode 0x%02x", mode);
break;
}
}

View file

@ -218,8 +218,9 @@ show_battery(int ac, char **av __unused)
printf(" Current Voltage: %d mV\n", stat.voltage);
printf(" Temperature: %d C\n", stat.temperature);
if (show_props) {
mfi_autolearn_period(props.auto_learn_period, buf, sizeof(buf));
printf(" Autolearn period: %s\n", buf);
printf(" Autolearn period: ");
mfi_autolearn_period(stdout, props.auto_learn_period);
printf("\n");
if (props.auto_learn_mode != 0)
snprintf(buf, sizeof(buf), "never");
else
@ -229,8 +230,9 @@ show_battery(int ac, char **av __unused)
printf(" Learn delay interval: %u hour%s\n",
props.learn_delay_interval,
props.learn_delay_interval != 1 ? "s" : "");
mfi_autolearn_mode(props.auto_learn_mode, buf, sizeof(buf));
printf(" Autolearn mode: %s\n", buf);
printf(" Autolearn mode: ");
mfi_autolearn_mode(stdout, props.auto_learn_mode);
printf("\n");
if (props.bbu_mode != 0)
printf(" BBU Mode: %d\n", props.bbu_mode);
}

View file

@ -175,9 +175,9 @@ int mfi_bbu_get_props(int fd, struct mfi_bbu_properties *props,
uint8_t *statusp);
int mfi_bbu_set_props(int fd, struct mfi_bbu_properties *props,
uint8_t *statusp);
void mfi_autolearn_period(uint32_t, char *, size_t);
void mfi_autolearn_period(FILE *, uint32_t);
void mfi_next_learn_time(uint32_t, char *, size_t);
void mfi_autolearn_mode(uint8_t, char *, size_t);
void mfi_autolearn_mode(FILE *, uint8_t);
int get_mfi_unit(const char *dev);
char *get_mfi_type(const char *dev);