From 7aa9f970e88382f2508ee55122e27cdcfd8cdbda Mon Sep 17 00:00:00 2001 From: James McCoy Date: Thu, 1 May 2014 11:14:31 -0400 Subject: all: Use macros for printf/scanf format specifiers Any types that are fixed width should use the standard format specifier macros (PRI... for printf-type functions, SCN... for scanf-type functions) to ensure proper data access. Prior to this ifpps was crashing in 32-bit environments due to the following call mvwprintw(screen, (*voff)++, 2, "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u" " ", uts.release, machine, ifname, drvinf.driver, buff, ms_interval, top_cpus, top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus); since ms_interval is a uint64_t but %lu expects an unsigned long, which is only 32 bits. Signed-off-by: James McCoy Signed-off-by: Tobias Klauser --- ifpps.c | 61 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 30 deletions(-) (limited to 'ifpps.c') diff --git a/ifpps.c b/ifpps.c index 0d13b08..4cdcbf4 100644 --- a/ifpps.c +++ b/ifpps.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -452,7 +453,7 @@ static int stats_proc_system(struct ifstat *stats) cpu = strtol(ptr, &ptr, 10); bug_on(cpu > cpus); - if (sscanf(ptr, "%lu%lu%lu%lu%lu", + if (sscanf(ptr, "%"SCNu64"%"SCNu64"%"SCNu64"%"SCNu64"%"SCNu64, &stats->cpu_user[cpu], &stats->cpu_nice[cpu], &stats->cpu_sys[cpu], @@ -730,7 +731,7 @@ static void screen_header(WINDOW *screen, const char *ifname, int *voff, slprintf(machine, sizeof(machine), " %s,", uts.machine); mvwprintw(screen, (*voff)++, 2, - "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u" + "%s,%s %s (%s%s), t=%"PRIu64"ms, cpus=%u%s/%u" " ", uts.release, machine, ifname, drvinf.driver, buff, ms_interval, top_cpus, top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus); @@ -798,27 +799,27 @@ static void screen_sys(WINDOW *screen, const struct ifstat *rel, const struct ifstat *abs, int *voff) { mvwprintw(screen, (*voff)++, 2, - "sys: %14u cs/t " - "%11u procs " - "%11u running " - "%10u iowait", + "sys: %14"PRIu32" cs/t " + "%11"PRIu32" procs " + "%11"PRIu32" running " + "%10"PRIu32" iowait", rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow); } static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff) { mvwprintw(screen, (*voff)++, 2, - "mem: %13uM total " - "%9uM used " - "%11uM active " - "%10uM inactive", + "mem: %13"PRIu64"M total " + "%9"PRIu64"M used " + "%11"PRIu64"M active " + "%10"PRIu64"M inactive", abs->mem_total / 1024, (abs->mem_total - abs->mem_free) / 1024, abs->mem_active / 1024, abs->mem_inactive / 1024); mvwprintw(screen, (*voff)++, 2, - "swap: %12uM total " + "swap: %12"PRIu64"M total " "%9uM used " "%11uM cached", abs->swap_total / 1024, @@ -1187,25 +1188,25 @@ static void term_csv(const struct ifstat *rel, const struct ifstat *abs) printf("%llu ", abs->tx_drops); printf("%llu ", abs->tx_errors); - printf("%u ", rel->cswitch); - printf("%lu ", abs->mem_free); - printf("%lu ", abs->mem_total - abs->mem_free); - printf("%lu ", abs->mem_total); - printf("%lu ", abs->swap_free); - printf("%lu ", abs->swap_total - abs->swap_free); - printf("%lu ", abs->swap_total); - printf("%u ", abs->procs_total); - printf("%u ", abs->procs_run); - printf("%u ", abs->procs_iow); + printf("%"PRIu32" ", rel->cswitch); + printf("%"PRIu64" ", abs->mem_free); + printf("%"PRIu64" ", abs->mem_total - abs->mem_free); + printf("%"PRIu64" ", abs->mem_total); + printf("%"PRIu64" ", abs->swap_free); + printf("%"PRIu64" ", abs->swap_total - abs->swap_free); + printf("%"PRIu64" ", abs->swap_total); + printf("%"PRIu32" ", abs->procs_total); + printf("%"PRIu32" ", abs->procs_run); + printf("%"PRIu32" ", abs->procs_iow); cpus = get_number_cpus(); for (i = 0; i < cpus; ++i) { - printf("%lu ", rel->cpu_user[i]); - printf("%lu ", rel->cpu_nice[i]); - printf("%lu ", rel->cpu_sys[i]); - printf("%lu ", rel->cpu_idle[i]); - printf("%lu ", rel->cpu_iow[i]); + printf("%"PRIu64" ", rel->cpu_user[i]); + printf("%"PRIu64" ", rel->cpu_nice[i]); + printf("%"PRIu64" ", rel->cpu_sys[i]); + printf("%"PRIu64" ", rel->cpu_idle[i]); + printf("%"PRIu64" ", rel->cpu_iow[i]); printf("%llu ", rel->irqs[i]); printf("%llu ", abs->irqs[i]); @@ -1218,9 +1219,9 @@ static void term_csv(const struct ifstat *rel, const struct ifstat *abs) } if (iswireless(abs)) { - printf("%u ", rel->wifi.link_qual); - printf("%u ", abs->wifi.link_qual); - printf("%u ", abs->wifi.link_qual_max); + printf("%"PRIu16" ", rel->wifi.link_qual); + printf("%"PRIu16" ", abs->wifi.link_qual); + printf("%"PRIu16" ", abs->wifi.link_qual_max); printf("%d ", rel->wifi.signal_level); printf("%d ", abs->wifi.signal_level); @@ -1237,7 +1238,7 @@ static void term_csv_header(const char *ifname, const struct ifstat *abs, printf("# gnuplot dump (#col:description)\n"); printf("# networking interface: %s\n", ifname); - printf("# sampling interval (t): %lu ms\n", ms_interval); + printf("# sampling interval (t): %"PRIu64" ms\n", ms_interval); printf("# %d:unixtime ", j++); printf("%d:rx-bytes-per-t ", j++); -- cgit v1.2.3-54-g00ecf