From 506b264702008c470492bbe0a27085d25fd24878 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 16 May 2019 17:20:03 -0500 Subject: Minor code shrink. --- toys/posix/ps.c | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/toys/posix/ps.c b/toys/posix/ps.c index 9d7c6445..48bf1e3b 100644 --- a/toys/posix/ps.c +++ b/toys/posix/ps.c @@ -1556,8 +1556,7 @@ static void top_common( // Display "top" header. if (*toys.which->name == 't') { struct ofields field; - char *hr0 = toybuf+sizeof(toybuf)-32, *hr1 = hr0-32, *hr2 = hr1-32, - *hr3 = hr2-32; + char hr[4][32]; long long ll, up = 0; long run[6]; int j; @@ -1577,28 +1576,21 @@ static void top_common( run[2]+run[3], run[4]); lines = header_line(lines, 0); - if (readfile("/proc/meminfo", toybuf, sizeof(toybuf))) { - for (i=0; i<6; i++) { - pos = strafter(toybuf, (char *[]){"MemTotal:","\nMemFree:", - "\nBuffers:","\nCached:","\nSwapTotal:","\nSwapFree:"}[i]); - run[i] = pos ? atol(pos) : 0; + if (readfile("/proc/meminfo", toybuf+256, sizeof(toybuf)-256)) { + for (i = 0; i<6; i++) { + j = i%3; + pos = strafter(toybuf+256, (char *[]){"MemTotal:","\nMemFree:", + "\nBuffers:","\nSwapTotal:","\nSwapFree:","\nCached:"}[i]); + human_readable(hr[j+!!j], 1024*(run[i] = pos ? atol(pos) : 0), 0); + if (j==1) human_readable(hr[1], 1024*(run[i-1]-run[i]), 0); + else if (j==2) { + sprintf(toybuf, (i<3) + ? " Mem: %9s total, %9s used, %9s free, %9s buffers" + : " Swap: %9s total, %9s used, %9s free, %9s cached", + hr[0], hr[1], hr[2], hr[3]); + lines = header_line(lines, 0); + } } - - human_readable(hr0, 1024*run[0], 0); - human_readable(hr1, 1024*(run[0]-run[1]), 0); - human_readable(hr2, 1024*run[1], 0); - human_readable(hr3, 1024*run[2], 0); - sprintf(toybuf, " Mem: %9s total, %9s used, %9s free, %9s buffers", - hr0, hr1, hr2, hr3); - lines = header_line(lines, 0); - - human_readable(hr0, 1024*run[4], 0); - human_readable(hr1, 1024*(run[4]-run[5]), 0); - human_readable(hr2, 1024*run[5], 0); - human_readable(hr3, 1024*run[3], 0); - sprintf(toybuf, " Swap: %9s total, %9s used, %9s free, %9s cached", - hr0, hr1, hr2, hr3); - lines = header_line(lines, 0); } pos = toybuf; -- cgit v1.2.3