Slightly different handling of printf/snprintf for unaligned uint64_t,

which should improve readability, and also to ease the port to
platforms that do not support %llu

MFC after:	3 days
This commit is contained in:
Luigi Rizzo 2010-04-19 15:11:45 +00:00
parent 3560b8af1f
commit 37133ba702
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=206843
3 changed files with 40 additions and 36 deletions

View File

@ -203,9 +203,9 @@ list_flow(struct dn_flow *ni)
inet_ntop(AF_INET6, &(id->dst_ip6), buff, sizeof(buff)), inet_ntop(AF_INET6, &(id->dst_ip6), buff, sizeof(buff)),
id->dst_port); id->dst_port);
} }
printf("%4llu %8llu %2u %4u %3u\n", pr_u64(&ni->tot_pkts, 4);
align_uint64(&ni->tot_pkts), pr_u64(&ni->tot_bytes, 8);
align_uint64(&ni->tot_bytes), printf("%2u %4u %3u\n",
ni->length, ni->len_bytes, ni->drops); ni->length, ni->len_bytes, ni->drops);
} }
@ -290,8 +290,8 @@ static void
list_pipes(struct dn_id *oid, struct dn_id *end) list_pipes(struct dn_id *oid, struct dn_id *end)
{ {
char buf[160]; /* pending buffer */ char buf[160]; /* pending buffer */
buf[0] = '\0';
buf[0] = '\0';
for (; oid != end; oid = O_NEXT(oid, oid->len)) { for (; oid != end; oid = O_NEXT(oid, oid->len)) {
if (oid->len < sizeof(*oid)) if (oid->len < sizeof(*oid))
errx(1, "invalid oid len %d\n", oid->len); errx(1, "invalid oid len %d\n", oid->len);

View File

@ -314,22 +314,27 @@ static struct _s_x rule_options[] = {
{ NULL, 0 } /* terminator */ { NULL, 0 } /* terminator */
}; };
/* /*
* The following is used to generate a printable argument for * Helper routine to print a possibly unaligned uint64_t on
* 64-bit numbers, irrespective of platform alignment and bit size. * various platform. If width > 0, print the value with
* Because all the printf in this program use %llu as a format, * the desired width, followed by a space;
* we just return an unsigned long long, which is larger than * otherwise, return the required width.
* we need in certain cases, but saves the hassle of using
* PRIu64 as a format specifier.
* We don't care about inlining, this is not performance critical code.
*/ */
unsigned long long int
align_uint64(const uint64_t *pll) pr_u64(uint64_t *pd, int width)
{ {
uint64_t ret; #ifdef TCC
#define U64_FMT "I64"
#else
#define U64_FMT "llu"
#endif
uint64_t d;
bcopy (pll, &ret, sizeof(ret)); bcopy (pd, &d, sizeof(d));
return ret; return (width > 0) ?
printf("%*" U64_FMT " ", width, d) :
snprintf(NULL, 0, "%" U64_FMT, d) ;
#undef U64_FMT
} }
void * void *
@ -973,9 +978,10 @@ show_ipfw(struct ip_fw *rule, int pcwidth, int bcwidth)
} }
printf("%05u ", rule->rulenum); printf("%05u ", rule->rulenum);
if (pcwidth>0 || bcwidth>0) if (pcwidth > 0 || bcwidth > 0) {
printf("%*llu %*llu ", pcwidth, align_uint64(&rule->pcnt), pr_u64(&rule->pcnt, pcwidth);
bcwidth, align_uint64(&rule->bcnt)); pr_u64(&rule->bcnt, bcwidth);
}
if (co.do_time == 2) if (co.do_time == 2)
printf("%10u ", rule->timestamp); printf("%10u ", rule->timestamp);
@ -1577,10 +1583,12 @@ show_dyn_ipfw(ipfw_dyn_rule *d, int pcwidth, int bcwidth)
} }
bcopy(&d->rule, &rulenum, sizeof(rulenum)); bcopy(&d->rule, &rulenum, sizeof(rulenum));
printf("%05d", rulenum); printf("%05d", rulenum);
if (pcwidth>0 || bcwidth>0) if (pcwidth > 0 || bcwidth > 0) {
printf(" %*llu %*llu (%ds)", pcwidth, printf(" ");
align_uint64(&d->pcnt), bcwidth, pr_u64(&d->pcnt, pcwidth);
align_uint64(&d->bcnt), d->expire); pr_u64(&d->bcnt, bcwidth);
printf("(%ds)", d->expire);
}
switch (d->dyn_type) { switch (d->dyn_type) {
case O_LIMIT_PARENT: case O_LIMIT_PARENT:
printf(" PARENT %d", d->count); printf(" PARENT %d", d->count);
@ -1645,9 +1653,9 @@ ipfw_sets_handler(char *av[])
free(data); free(data);
nalloc = nalloc * 2 + 200; nalloc = nalloc * 2 + 200;
nbytes = nalloc; nbytes = nalloc;
data = safe_calloc(1, nbytes); data = safe_calloc(1, nbytes);
if (do_cmd(IP_FW_GET, data, (uintptr_t)&nbytes) < 0) if (do_cmd(IP_FW_GET, data, (uintptr_t)&nbytes) < 0)
err(EX_OSERR, "getsockopt(IP_FW_GET)"); err(EX_OSERR, "getsockopt(IP_FW_GET)");
} }
bcopy(&((struct ip_fw *)data)->next_rule, bcopy(&((struct ip_fw *)data)->next_rule,
@ -1837,14 +1845,12 @@ ipfw_list(int ac, char *av[], int show_counters)
continue; continue;
/* packet counter */ /* packet counter */
width = snprintf(NULL, 0, "%llu", width = pr_u64(&r->pcnt, 0);
align_uint64(&r->pcnt));
if (width > pcwidth) if (width > pcwidth)
pcwidth = width; pcwidth = width;
/* byte counter */ /* byte counter */
width = snprintf(NULL, 0, "%llu", width = pr_u64(&r->bcnt, 0);
align_uint64(&r->bcnt));
if (width > bcwidth) if (width > bcwidth)
bcwidth = width; bcwidth = width;
} }
@ -1858,13 +1864,11 @@ ipfw_list(int ac, char *av[], int show_counters)
if (set != co.use_set - 1) if (set != co.use_set - 1)
continue; continue;
} }
width = snprintf(NULL, 0, "%llu", width = pr_u64(&d->pcnt, 0);
align_uint64(&d->pcnt));
if (width > pcwidth) if (width > pcwidth)
pcwidth = width; pcwidth = width;
width = snprintf(NULL, 0, "%llu", width = pr_u64(&d->bcnt, 0);
align_uint64(&d->bcnt));
if (width > bcwidth) if (width > bcwidth)
bcwidth = width; bcwidth = width;
} }

View File

@ -207,7 +207,7 @@ enum tokens {
#define NEED(_p, msg) {if (!_p) errx(EX_USAGE, msg);} #define NEED(_p, msg) {if (!_p) errx(EX_USAGE, msg);}
#define NEED1(msg) {if (!(*av)) errx(EX_USAGE, msg);} #define NEED1(msg) {if (!(*av)) errx(EX_USAGE, msg);}
unsigned long long align_uint64(const uint64_t *pll); int pr_u64(uint64_t *pd, int width);
/* memory allocation support */ /* memory allocation support */
void *safe_calloc(size_t number, size_t size); void *safe_calloc(size_t number, size_t size);