[Toybox] vmstat bug + patch

Graham Watt gmwatt at gmail.com
Sat May 9 22:47:19 PDT 2015


There's a bug in vmstat that causes junk to be printed instead of the
headers when repeating. This can be duplicated by running `vmstat 1`
and waiting for the headers to be printed again.
Here is a patch for the issue.


diff --git a/toys/other/vmstat.c b/toys/other/vmstat.c
index eed7945..2685c11 100644
--- a/toys/other/vmstat.c
+++ b/toys/other/vmstat.c
@@ -77,8 +77,9 @@ void vmstat_main(void)
   int i, loop_delay = 0, loop_max = 0;
   unsigned loop, rows = (toys.optflags & FLAG_n) ? 0 : 25,
            page_kb = sysconf(_SC_PAGESIZE)/1024;
-  char *headers="r\0b\0swpd\0free\0buff\0cache\0si\0so\0bi\0bo\0in\0cs\0us\0"
-                "sy\0id\0wa", lengths[] = {2,2,6,6,6,6,4,4,5,5,4,4,2,2,2,2};
+  char *headers[]={"r","b","swpd","free","buff","cache","si","so","bi","bo",
+                  "in","cs","us","sy","id","wa"},
+       lengths[] = {2,2,6,6,6,6,4,4,5,5,4,4,2,2,2,2};

   memset(top, 0, sizeof(top));
   if (toys.optc) loop_delay = atolx_range(toys.optargs[0], 0, INT_MAX);
@@ -99,8 +100,7 @@ void vmstat_main(void)
       printf("procs -----------memory---------- ---swap-- -----io----
-system-- ----cpu----\n");

       for (i=0; i<sizeof(lengths); i++) {
-        printf(" %*s"+!i, lengths[i], headers);
-        headers += strlen(headers)+1;
+        printf(" %*s"+!i, lengths[i], headers[i]);
       }
       xputc('\n');
     }

-- 
Graham Watt


More information about the Toybox mailing list