From fce3a7325a8849b58c71b811b5c38d1680f881c7 Mon Sep 17 00:00:00 2001 From: bde Date: Tue, 14 Feb 2006 11:57:02 +0000 Subject: Fix some minor bugs: Always use snprintf()'s return value, since discarding it is a style bug at best and using it here gives slightly simpler code and better error checking. Use snprintf() in putlongdouble() the same as in putfloat(). (1.25 changed most sprintf()'s to snprintf()'s to fix non-bugs without changing the logic to use the result of snprintf(); 1.27 restored one of the sprintf()s by cloning a stale version of putfloat().) Don't print a too-long field in the unlikely case that the fallback to M units in putint() leaves the field still too long. (The fallback to printing stars was lost in rev.1.58 when the fallback to M units was added.) --- usr.bin/systat/vmstat.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/systat/vmstat.c b/usr.bin/systat/vmstat.c index 7709a05..2fc003a 100644 --- a/usr.bin/systat/vmstat.c +++ b/usr.bin/systat/vmstat.c @@ -670,6 +670,7 @@ static void putint(n, l, lc, w) int n, l, lc, w; { + int snr; char b[128]; move(l, lc); @@ -678,11 +679,16 @@ putint(n, l, lc, w) addch(' '); return; } - snprintf(b, sizeof(b), "%*d", w, n); - if ((int)strlen(b) > w) - snprintf(b, sizeof(b), "%*dk", w - 1, n / 1000); - if ((int)strlen(b) > w) - snprintf(b, sizeof(b), "%*dM", w - 1, n / 1000000); + snr = snprintf(b, sizeof(b), "%*d", w, n); + if (snr != w) + snr = snprintf(b, sizeof(b), "%*dk", w - 1, n / 1000); + if (snr != w) + snr = snprintf(b, sizeof(b), "%*dM", w - 1, n / 1000000); + if (snr != w) { + while (w-- > 0) + addch('*'); + return; + } addstr(b); } @@ -691,6 +697,7 @@ putfloat(f, l, lc, w, d, nz) double f; int l, lc, w, d, nz; { + int snr; char b[128]; move(l, lc); @@ -699,10 +706,10 @@ putfloat(f, l, lc, w, d, nz) addch(' '); return; } - snprintf(b, sizeof(b), "%*.*f", w, d, f); - if ((int)strlen(b) > w) - snprintf(b, sizeof(b), "%*.0f", w, f); - if ((int)strlen(b) > w) { + snr = snprintf(b, sizeof(b), "%*.*f", w, d, f); + if (snr != w) + snr = snprintf(b, sizeof(b), "%*.0f", w, f); + if (snr != w) { while (--w >= 0) addch('*'); return; @@ -715,6 +722,7 @@ putlongdouble(f, l, lc, w, d, nz) long double f; int l, lc, w, d, nz; { + int snr; char b[128]; move(l, lc); @@ -723,10 +731,10 @@ putlongdouble(f, l, lc, w, d, nz) addch(' '); return; } - sprintf(b, "%*.*Lf", w, d, f); - if ((int)strlen(b) > w) - sprintf(b, "%*.0Lf", w, f); - if ((int)strlen(b) > w) { + snr = snprintf(b, sizeof(b), "%*.*Lf", w, d, f); + if (snr != w) + snr = snprintf(b, sizeof(b), "%*.0Lf", w, f); + if (snr != w) { while (--w >= 0) addch('*'); return; -- cgit v1.1