1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-17 10:26:15 +00:00

Fix up this local copy of statfoo to support > 128 statistics.

This allows all of the athstats statistics to work again.

Specifics:

* The previous code used chars < 0x80 as printable, and chars >= 0x80
  as "statistics"
* .. which meant any statistic above 127 would wrap around to 0;
* .. so once I added the 802.11n TX/RX statistics to athstats, the tail
  end of the statistics list weren't accessible.

This patch:

* adds a define which represents the magic character, rather than a hard
  coded one
* the statistic in question is little endian encoded after the magic
  character.

Notes:

* statfoo is useful enough to possibly warrant turning into a library API.
This commit is contained in:
Adrian Chadd 2012-02-17 08:24:58 +00:00
parent 81ed1a1e1b
commit caee7d9ca4
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=231863
2 changed files with 24 additions and 9 deletions

View File

@ -60,7 +60,9 @@ statfoo_setfmt(struct statfoo *sf, const char *fmt0)
}
if (j != 0)
sf->fmts[j++] = ' ';
sf->fmts[j++] = 0x80 | i;
sf->fmts[j++] = FMTS_IS_STAT;
sf->fmts[j++] = i & 0xff;
sf->fmts[j++] = (i >> 8) & 0xff;
}
sf->fmts[j] = '\0';
#undef N
@ -89,10 +91,14 @@ static void
statfoo_print_header(struct statfoo *sf, FILE *fd)
{
const unsigned char *cp;
int i;
const struct fmt *f;
for (cp = sf->fmts; *cp != '\0'; cp++) {
if (*cp & 0x80) {
const struct fmt *f = &sf->stats[*cp &~ 0x80];
if (*cp == FMTS_IS_STAT) {
i = *(++cp);
i |= ((int) *(++cp)) << 8;
f = &sf->stats[i];
fprintf(fd, "%*s", f->width, f->label);
} else
putc(*cp, fd);
@ -105,11 +111,15 @@ statfoo_print_current(struct statfoo *sf, FILE *fd)
{
char buf[32];
const unsigned char *cp;
int i;
const struct fmt *f;
for (cp = sf->fmts; *cp != '\0'; cp++) {
if (*cp & 0x80) {
const struct fmt *f = &sf->stats[*cp &~ 0x80];
if (sf->get_curstat(sf, *cp &~ 0x80, buf, sizeof(buf)))
if (*cp == FMTS_IS_STAT) {
i = *(++cp);
i |= ((int) *(++cp)) << 8;
f = &sf->stats[i];
if (sf->get_curstat(sf, i, buf, sizeof(buf)))
fprintf(fd, "%*s", f->width, buf);
} else
putc(*cp, fd);
@ -122,11 +132,15 @@ statfoo_print_total(struct statfoo *sf, FILE *fd)
{
char buf[32];
const unsigned char *cp;
const struct fmt *f;
int i;
for (cp = sf->fmts; *cp != '\0'; cp++) {
if (*cp & 0x80) {
const struct fmt *f = &sf->stats[*cp &~ 0x80];
if (sf->get_totstat(sf, *cp &~ 0x80, buf, sizeof(buf)))
if (*cp == FMTS_IS_STAT) {
i = *(++cp);
i |= ((int) *(++cp)) << 8;
f = &sf->stats[i];
if (sf->get_totstat(sf, i, buf, sizeof(buf)))
fprintf(fd, "%*s", f->width, buf);
} else
putc(*cp, fd);

View File

@ -79,6 +79,7 @@ struct statfoo {
const char *name; /* statistics name, e.g. wlanstats */
const struct fmt *stats; /* statistics in class */
int nstats; /* number of stats */
#define FMTS_IS_STAT 0x80 /* the following two bytes are the stat id */
unsigned char fmts[4096]; /* private: compiled stats to display */
STATFOO_DECL_METHODS(struct statfoo *);