ethr-wake: can use ether_hostton on uclibc >= 0.9.30
[oweals/busybox.git] / procps / top.c
index b481d395ed27d591c01bb06b1abb253ef9b00b5d..62b9c1e1b4c717c2d182b528def310e94421fe2d 100644 (file)
@@ -4,7 +4,7 @@
  *
  * This is written specifically for the linux /proc/<PID>/stat(m)
  * files format.
-
+ *
  * This reads the PIDs of all processes and their status and shows
  * the status of processes (first ones that fit to screen) at given
  * intervals.
  * - CPU where Process was last seen running
  *   (to see effect of sched_setaffinity() etc)
  * - CPU Time Split (idle/IO/wait etc) PER CPU
- */
-
-/* Original code Copyrights */
-/*
+ *
  * Copyright (c) 1992 Branko Lankester
  * Copyright (c) 1992 Roger Binns
  * Copyright (C) 1994-1996 Charles L. Blake.
  * Copyright (C) 1992-1998 Michael K. Johnson
- * May be distributed under the conditions of the
- * GNU Library General Public License
+ *
+ * Licensed under GPLv2, see file LICENSE in this tarball for details.
  */
 
 #include "libbb.h"
@@ -53,7 +50,9 @@ typedef struct top_status_t {
 } top_status_t;
 
 typedef struct jiffy_counts_t {
-       unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
+       /* Linux 2.4.x has only first four */
+       unsigned long long usr, nic, sys, idle;
+       unsigned long long iowait, irq, softirq, steal;
        unsigned long long total;
        unsigned long long busy;
 } jiffy_counts_t;
@@ -182,15 +181,12 @@ static int mult_lvl_cmp(void* a, void* b)
        return 0;
 }
 
-/* NOINLINE so that complier doesn't unfold the call
- * causing multiple copies of the arithmatic instrns
- */
 static NOINLINE int read_cpu_jiffy(FILE *fp, jiffy_counts_t *p_jif)
 {
 #if !ENABLE_FEATURE_TOP_SMP_CPU
-       static const char fmt[] = "cpu %lld %lld %lld %lld %lld %lld %lld %lld";
+       static const char fmt[] = "cpu %llu %llu %llu %llu %llu %llu %llu %llu";
 #else
-       static const char fmt[] = "cp%*s %lld %lld %lld %lld %lld %lld %lld %lld";
+       static const char fmt[] = "cp%*s %llu %llu %llu %llu %llu %llu %llu %llu";
 #endif
        int ret;
 
@@ -200,7 +196,7 @@ static NOINLINE int read_cpu_jiffy(FILE *fp, jiffy_counts_t *p_jif)
                        &p_jif->usr, &p_jif->nic, &p_jif->sys, &p_jif->idle,
                        &p_jif->iowait, &p_jif->irq, &p_jif->softirq,
                        &p_jif->steal);
-       if (ret > 4) {
+       if (ret >= 4) {
                p_jif->total = p_jif->usr + p_jif->nic + p_jif->sys + p_jif->idle
                        + p_jif->iowait + p_jif->irq + p_jif->softirq + p_jif->steal;
                /* procps 2.x does not count iowait as busy time */
@@ -644,12 +640,10 @@ static void clearmems(void)
 }
 
 #if ENABLE_FEATURE_USE_TERMIOS
-#include <termios.h>
-#include <signal.h>
 
 static void reset_term(void)
 {
-       tcsetattr(0, TCSANOW, &initial_settings);
+       tcsetattr_stdin_TCSANOW(&initial_settings);
        if (ENABLE_FEATURE_CLEAN_UP) {
                clearmems();
 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
@@ -877,9 +871,7 @@ enum {
                | PSSCAN_UTIME
                | PSSCAN_STATE
                | PSSCAN_COMM
-#if ENABLE_FEATURE_TOP_SMP_PROCESS
                | PSSCAN_CPU
-#endif
                | PSSCAN_UIDGID,
        TOPMEM_MASK = 0
                | PSSCAN_PID
@@ -894,8 +886,8 @@ int top_main(int argc UNUSED_PARAM, char **argv)
        unsigned lines, col;
        int lines_rem;
        unsigned interval;
-       char *sinterval;
-       SKIP_FEATURE_TOPMEM(const) unsigned scan_mask = TOP_MASK;
+       char *str_interval, *str_iterations;
+       IF_NOT_FEATURE_TOPMEM(const) unsigned scan_mask = TOP_MASK;
 #if ENABLE_FEATURE_USE_TERMIOS
        struct termios new_settings;
        struct pollfd pfd[1];
@@ -917,10 +909,19 @@ int top_main(int argc UNUSED_PARAM, char **argv)
 #endif
 
        /* all args are options; -n NUM */
-       opt_complementary = "-:n+";
-       if (getopt32(argv, "d:n:b", &sinterval, &iterations) & OPT_d) {
+       opt_complementary = "-";
+       col = getopt32(argv, "d:n:b", &str_interval, &str_iterations);
+       if (col & OPT_d) {
+               /* work around for "-d 1" -> "-d -1" done by getopt32 */
+               if (str_interval[0] == '-')
+                       str_interval++;
                /* Need to limit it to not overflow poll timeout */
-               interval = xatou16(sinterval); /* -d */
+               interval = xatou16(str_interval);
+       }
+       if (col & OPT_n) {
+               if (str_iterations[0] == '-')
+                       str_iterations++;
+               iterations = xatou(str_iterations);
        }
 
        /* change to /proc */
@@ -932,7 +933,7 @@ int top_main(int argc UNUSED_PARAM, char **argv)
        new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
 
        bb_signals(BB_FATAL_SIGS, sig_catcher);
-       tcsetattr(0, TCSANOW, (void *) &new_settings);
+       tcsetattr_stdin_TCSANOW(&new_settings);
 #endif /* FEATURE_USE_TERMIOS */
 
 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
@@ -1051,11 +1052,11 @@ int top_main(int argc UNUSED_PARAM, char **argv)
                        if (c == 'q')
                                break;
                        if (c == 'n') {
-                               USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
+                               IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
                                sort_function[0] = pid_sort;
                        }
                        if (c == 'm') {
-                               USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
+                               IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
                                sort_function[0] = mem_sort;
 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
                                sort_function[1] = pcpu_sort;
@@ -1064,13 +1065,13 @@ int top_main(int argc UNUSED_PARAM, char **argv)
                        }
 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
                        if (c == 'p') {
-                               USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
+                               IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
                                sort_function[0] = pcpu_sort;
                                sort_function[1] = mem_sort;
                                sort_function[2] = time_sort;
                        }
                        if (c == 't') {
-                               USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
+                               IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
                                sort_function[0] = time_sort;
                                sort_function[1] = mem_sort;
                                sort_function[2] = pcpu_sort;