randomtest fixes
[oweals/busybox.git] / procps / top.c
index 9e8ad1d1f06b364b0267015480d30797832f41d6..f5c0a123ff074c38ec0623e53f9874d36a0e9505 100644 (file)
@@ -100,10 +100,8 @@ struct globals {
        int num_cpus;
 #endif
        char line_buf[80];
-};
-
+}; //FIX_ALIASING; - large code growth
 enum { LINE_BUF_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line_buf) };
-
 #define G (*(struct globals*)&bb_common_bufsiz1)
 #define INIT_G() do { \
        struct G_sizecheck { \
@@ -513,8 +511,8 @@ static NOINLINE void display_process_list(int lines_rem, int scr_width)
        /* xxx_shift and xxx_scale variables allow us to replace
         * expensive divides with multiply and shift */
        unsigned pmem_shift, pmem_scale, pmem_half;
-       unsigned tmp_unsigned;
 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+       unsigned tmp_unsigned;
        unsigned pcpu_shift, pcpu_scale, pcpu_half;
        unsigned busy_jifs;
 #endif
@@ -522,12 +520,8 @@ static NOINLINE void display_process_list(int lines_rem, int scr_width)
        /* what info of the processes is shown */
        printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
                "  PID  PPID USER     STAT   VSZ %MEM"
-#if ENABLE_FEATURE_TOP_SMP_PROCESS
-               " CPU"
-#endif
-#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
-               " %CPU"
-#endif
+               IF_FEATURE_TOP_SMP_PROCESS(" CPU")
+               IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE(" %CPU")
                " COMMAND");
        lines_rem--;
 
@@ -606,22 +600,14 @@ static NOINLINE void display_process_list(int lines_rem, int scr_width)
                /* PID PPID USER STAT VSZ %MEM [%CPU] COMMAND */
                col = snprintf(line_buf, scr_width,
                                "\n" "%5u%6u %-8.8s %s%s" FMT
-#if ENABLE_FEATURE_TOP_SMP_PROCESS
-                               " %3d"
-#endif
-#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
-                               FMT
-#endif
+                               IF_FEATURE_TOP_SMP_PROCESS(" %3d")
+                               IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE(FMT)
                                " ",
                                s->pid, s->ppid, get_cached_username(s->uid),
                                s->state, vsz_str_buf,
                                SHOW_STAT(pmem)
-#if ENABLE_FEATURE_TOP_SMP_PROCESS
-                               , s->last_seen_on_cpu
-#endif
-#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
-                               , SHOW_STAT(pcpu)
-#endif
+                               IF_FEATURE_TOP_SMP_PROCESS(, s->last_seen_on_cpu)
+                               IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE(, SHOW_STAT(pcpu))
                );
                if ((int)(col + 1) < scr_width)
                        read_cmdline(line_buf + col, scr_width - col, s->pid, s->comm);
@@ -632,7 +618,7 @@ static NOINLINE void display_process_list(int lines_rem, int scr_width)
        }
        /* printf(" %d", hist_iterations); */
        bb_putchar(OPT_BATCH_MODE ? '\n' : '\r');
-       fflush(stdout);
+       fflush_all();
 }
 #undef UPSCALE
 #undef SHOW_STAT
@@ -710,12 +696,12 @@ static int topmem_sort(char *a, char *b)
        return inverted ? -n : n;
 }
 
-/* Cut "NNNN " out of "    NNNN kb" */
+/* Cut "NNNN" out of "    NNNN kb" */
 static char *grab_number(char *str, const char *match, unsigned sz)
 {
        if (strncmp(str, match, sz) == 0) {
                str = skip_whitespace(str + sz);
-               (skip_non_whitespace(str))[1] = '\0';
+               (skip_non_whitespace(str))[0] = '\0';
                return xstrdup(str);
        }
        return NULL;
@@ -782,19 +768,19 @@ static void display_topmem_header(int scr_width, int *lines_rem_p)
        }
        fclose(fp);
 
-#define S(s) (s ? s : "0 ")
+#define S(s) (s ? s : "0")
        snprintf(linebuf, sizeof(linebuf),
-               "Mem %stotal %sanon %smap %sfree",
+               "Mem total:%s anon:%s map:%s free:%s",
                S(total), S(anon), S(map), S(mfree));
        printf(OPT_BATCH_MODE ? "%.*s\n" : "\e[H\e[J%.*s\n", scr_width, linebuf);
 
        snprintf(linebuf, sizeof(linebuf),
-               " %sslab %sbuf %scache %sdirty %swrite",
+               " slab:%s buf:%s cache:%s dirty:%s write:%s",
                S(slab), S(buf), S(cache), S(dirty), S(mwrite));
        printf("%.*s\n", scr_width, linebuf);
 
        snprintf(linebuf, sizeof(linebuf),
-               "Swap %stotal %sfree", // TODO: % used?
+               "Swap total:%s free:%s", // TODO: % used?
                S(swaptotal), S(swapfree));
        printf("%.*s\n", scr_width, linebuf);
 
@@ -856,7 +842,7 @@ static NOINLINE void display_topmem_process_list(int lines_rem, int scr_width)
                s++;
        }
        bb_putchar(OPT_BATCH_MODE ? '\n' : '\r');
-       fflush(stdout);
+       fflush_all();
 #undef HDR_STR
 #undef MIN_WIDTH
 }