2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 * redistribution must be conditioned upon including a substantially
16 * similar Disclaimer requirement for further binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
40 bsdstat_setfmt(struct bsdstat *sf, const char *fmt0)
42 #define N(a) (sizeof(a)/sizeof(a[0]))
48 strlcpy(fmt, fmt0, sizeof(fmt));
49 for (fp = fmt; (tok = strsep(&fp, ", ")) != NULL;) {
50 for (i = 0; i < sf->nstats; i++)
51 if (strcasecmp(tok, sf->stats[i].name) == 0)
53 if (i >= sf->nstats) {
54 fprintf(stderr, "%s: unknown statistic name \"%s\" "
55 "skipped\n", sf->name, tok);
58 if (j+4 > (int) sizeof(sf->fmts)) {
59 fprintf(stderr, "%s: not enough room for all stats; "
60 "stopped at %s\n", sf->name, tok);
65 sf->fmts[j++] = FMTS_IS_STAT;
66 sf->fmts[j++] = i & 0xff;
67 sf->fmts[j++] = (i >> 8) & 0xff;
74 bsdstat_collect(struct bsdstat *sf)
76 fprintf(stderr, "%s: don't know how to collect data\n", sf->name);
80 bsdstat_update_tot(struct bsdstat *sf)
82 fprintf(stderr, "%s: don't know how to update total data\n", sf->name);
86 bsdstat_get(struct bsdstat *sf, int s, char b[] __unused, size_t bs __unused)
88 fprintf(stderr, "%s: don't know how to get stat #%u\n", sf->name, s);
93 bsdstat_print_header(struct bsdstat *sf, FILE *fd)
95 const unsigned char *cp;
99 for (cp = sf->fmts; *cp != '\0'; cp++) {
100 if (*cp == FMTS_IS_STAT) {
102 i |= ((int) *(++cp)) << 8;
104 fprintf(fd, "%*s", f->width, f->label);
112 bsdstat_print_current(struct bsdstat *sf, FILE *fd)
115 const unsigned char *cp;
119 for (cp = sf->fmts; *cp != '\0'; cp++) {
120 if (*cp == FMTS_IS_STAT) {
122 i |= ((int) *(++cp)) << 8;
124 if (sf->get_curstat(sf, i, buf, sizeof(buf)))
125 fprintf(fd, "%*s", f->width, buf);
133 bsdstat_print_total(struct bsdstat *sf, FILE *fd)
136 const unsigned char *cp;
140 for (cp = sf->fmts; *cp != '\0'; cp++) {
141 if (*cp == FMTS_IS_STAT) {
143 i |= ((int) *(++cp)) << 8;
145 if (sf->get_totstat(sf, i, buf, sizeof(buf)))
146 fprintf(fd, "%*s", f->width, buf);
154 bsdstat_print_verbose(struct bsdstat *sf, FILE *fd)
161 for (i = 0; i < sf->nstats; i++) {
163 if (f->width > width)
166 for (i = 0; i < sf->nstats; i++) {
168 if (sf->get_totstat(sf, i, s, sizeof(s)) && strcmp(s, "0"))
169 fprintf(fd, "%-*s %s\n", width, s, f->desc);
174 bsdstat_print_fields(struct bsdstat *sf, FILE *fd)
179 for (i = 0; i < sf->nstats; i++) {
180 w = strlen(sf->stats[i].name);
184 for (i = 0; i < sf->nstats; i++) {
185 const struct fmt *f = &sf->stats[i];
187 fprintf(fd, "%-*s %s\n", width, f->name, f->desc);
192 bsdstat_init(struct bsdstat *sf, const char *name, const struct fmt *stats, int nstats)
197 sf->setfmt = bsdstat_setfmt;
198 sf->collect_cur = bsdstat_collect;
199 sf->collect_tot = bsdstat_collect;
200 sf->update_tot = bsdstat_update_tot;
201 sf->get_curstat = bsdstat_get;
202 sf->get_totstat = bsdstat_get;
203 sf->print_header = bsdstat_print_header;
204 sf->print_current = bsdstat_print_current;
205 sf->print_total = bsdstat_print_total;
206 sf->print_verbose = bsdstat_print_verbose;
207 sf->print_fields = bsdstat_print_fields;