Author: eadler
Date: Wed Jun 13 08:52:09 2018
New Revision: 335038
URL: https://svnweb.freebsd.org/changeset/base/335038

Log:
  top(1): format_time, format_k, etc.
  
  - Use humanize_number for format_k and format_k2
  - Fix some style nits in format_time

Modified:
  head/usr.bin/top/Makefile
  head/usr.bin/top/machine.c
  head/usr.bin/top/utils.c
  head/usr.bin/top/utils.h

Modified: head/usr.bin/top/Makefile
==============================================================================
--- head/usr.bin/top/Makefile   Wed Jun 13 08:52:06 2018        (r335037)
+++ head/usr.bin/top/Makefile   Wed Jun 13 08:52:09 2018        (r335038)
@@ -16,5 +16,5 @@ NO_WERROR=
 .endif
 CFLAGS.clang=-Wno-error=incompatible-pointer-types-discards-qualifiers 
-Wno-error=cast-qual
 
-LIBADD=        ncursesw m kvm jail
+LIBADD=        ncursesw m kvm jail util
 .include <bsd.prog.mk>

Modified: head/usr.bin/top/machine.c
==============================================================================
--- head/usr.bin/top/machine.c  Wed Jun 13 08:52:06 2018        (r335037)
+++ head/usr.bin/top/machine.c  Wed Jun 13 08:52:09 2018        (r335038)
@@ -1087,7 +1087,7 @@ format_next_process(void* xhandle, char *(*get_userid)
        else
                snprintf(swap_buf, sizeof(swap_buf), "%*s",
                    swaplength - 1,
-                   format_k2(pagetok(ki_swap(pp)))); /* XXX */
+                   format_k(pagetok(ki_swap(pp)))); /* XXX */
 
        if (displaymode == DISP_IO) {
                oldp = get_old_proc(pp);
@@ -1148,8 +1148,8 @@ format_next_process(void* xhandle, char *(*get_userid)
            thr_buf,
            pp->ki_pri.pri_level - PZERO,
            format_nice(pp),
-           format_k2(PROCSIZE(pp)),
-           format_k2(pagetok(pp->ki_rssize)),
+           format_k(PROCSIZE(pp)),
+           format_k(pagetok(pp->ki_rssize)),
            swaplength, swaplength, swap_buf,
            status,
            cpu,

Modified: head/usr.bin/top/utils.c
==============================================================================
--- head/usr.bin/top/utils.c    Wed Jun 13 08:52:06 2018        (r335037)
+++ head/usr.bin/top/utils.c    Wed Jun 13 08:52:09 2018        (r335038)
@@ -19,6 +19,7 @@
 #include <sys/sysctl.h>
 #include <sys/user.h>
 
+#include <libutil.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -262,35 +263,34 @@ percentages(int cnt, int *out, long *new, long *old, l
    exceed 9999.9, we use "???".
  */
 
-char *
+const char *
 format_time(long seconds)
 {
-    static char result[10];
+       static char result[10];
 
-    /* sanity protection */
-    if (seconds < 0 || seconds > (99999l * 360l))
-    {
-       strcpy(result, "   ???");
-    }
-    else if (seconds >= (1000l * 60l))
-    {
-       /* alternate (slow) method displaying hours and tenths */
-       sprintf(result, "%5.1fH", (double)seconds / (double)(60l * 60l));
+       /* sanity protection */
+       if (seconds < 0 || seconds > (99999l * 360l))
+       {
+               strcpy(result, "   ???");
+       }
+       else if (seconds >= (1000l * 60l))
+       {
+               /* alternate (slow) method displaying hours and tenths */
+               sprintf(result, "%5.1fH", (double)seconds / (double)(60l * 
60l));
 
-       /* It is possible that the sprintf took more than 6 characters.
-          If so, then the "H" appears as result[6].  If not, then there
-          is a \0 in result[6].  Either way, it is safe to step on.
-        */
-       result[6] = '\0';
-    }
-    else
-    {
-       /* standard method produces MMM:SS */
-       /* we avoid printf as must as possible to make this quick */
-       sprintf(result, "%3ld:%02ld",
-           (long)(seconds / 60), (long)(seconds % 60));
-    }
-    return(result);
+               /* It is possible that the sprintf took more than 6 characters.
+                  If so, then the "H" appears as result[6].  If not, then there
+                  is a \0 in result[6].  Either way, it is safe to step on.
+                  */
+               result[6] = '\0';
+       }
+       else
+       {
+               /* standard method produces MMM:SS */
+               sprintf(result, "%3ld:%02ld",
+                               seconds / 60l, seconds % 60l);
+       }
+       return(result);
 }
 
 /*
@@ -319,63 +319,16 @@ format_time(long seconds)
 #define NUM_STRINGS 8
 
 char *
-format_k(long amt)
+format_k(int64_t amt)
 {
     static char retarray[NUM_STRINGS][16];
     static int index = 0;
-    char *p;
     char *ret;
-    char tag = 'K';
 
-    p = ret = retarray[index];
-    index = (index + 1) % NUM_STRINGS;
-
-    if (amt >= 10000)
-    {
-       amt = (amt + 512) / 1024;
-       tag = 'M';
-       if (amt >= 10000)
-       {
-           amt = (amt + 512) / 1024;
-           tag = 'G';
-       }
-    }
-
-    p = stpcpy(p, itoa(amt));
-    *p++ = tag;
-    *p = '\0';
-
-    return(ret);
-}
-
-char *
-format_k2(unsigned long long amt)
-{
-    static char retarray[NUM_STRINGS][16];
-    static int index = 0;
-    char *p;
-    char *ret;
-    char tag = 'K';
-
-    p = ret = retarray[index];
-    index = (index + 1) % NUM_STRINGS;
-
-    if (amt >= 100000)
-    {
-       amt = (amt + 512) / 1024;
-       tag = 'M';
-       if (amt >= 100000)
-       {
-           amt = (amt + 512) / 1024;
-           tag = 'G';
-       }
-    }
-
-    p = stpcpy(p, itoa((int)amt));
-    *p++ = tag;
-    *p = '\0';
-
-    return(ret);
+    ret = retarray[index];
+       index = (index + 1) % NUM_STRINGS;
+       humanize_number(ret, 6, amt * 1024, "", HN_AUTOSCALE, HN_NOSPACE);
+       return (ret);
 }
 
 int

Modified: head/usr.bin/top/utils.h
==============================================================================
--- head/usr.bin/top/utils.h    Wed Jun 13 08:52:06 2018        (r335037)
+++ head/usr.bin/top/utils.h    Wed Jun 13 08:52:09 2018        (r335038)
@@ -18,9 +18,8 @@ char *itoa7(int);
 int digits(int);
 const char * const *argparse(char *, int *);
 long percentages(int, int *, long *, long *, long *);
-char *format_time(long);
-char *format_k(long);
-char *format_k2(unsigned long long);
+const char *format_time(long);
+char *format_k(int64_t);
 int string_index(const char *string, const char * const *array);
 int find_pid(pid_t pid);
 
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to