+ num_cpus = 0;
+ smp_cpu_info = !smp_cpu_info;
+ get_jiffy_counts();
+ continue;
+ }
+# endif
+# endif
+ break; /* unknown key -> force refresh */
+ }
+
+ return scan_mask;
+}
+#endif
+
+//usage:#if ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_TOP_SMP_CPU
+//usage:# define IF_SHOW_THREADS_OR_TOP_SMP(...) __VA_ARGS__
+//usage:#else
+//usage:# define IF_SHOW_THREADS_OR_TOP_SMP(...)
+//usage:#endif
+//usage:#define top_trivial_usage
+//usage: "[-b] [-nCOUNT] [-dSECONDS]" IF_FEATURE_TOPMEM(" [-m]")
+//usage:#define top_full_usage "\n\n"
+//usage: "Provide a view of process activity in real time."
+//usage: "\n""Read the status of all processes from /proc each SECONDS"
+//usage: "\n""and display a screenful of them."
+//usage: "\n""Keys:"
+//usage: "\n"" N/M"
+//usage: IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE("/P")
+//usage: IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE("/T")
+//usage: ": " IF_FEATURE_TOPMEM("show CPU usage, ") "sort by pid/mem"
+//usage: IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE("/cpu")
+//usage: IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE("/time")
+//usage: IF_FEATURE_TOPMEM(
+//usage: "\n"" S: show memory"
+//usage: )
+//usage: "\n"" R: reverse sort"
+//usage: IF_SHOW_THREADS_OR_TOP_SMP(
+//usage: "\n"" "
+//usage: IF_FEATURE_SHOW_THREADS("H: toggle threads")
+//usage: IF_FEATURE_SHOW_THREADS(IF_FEATURE_TOP_SMP_CPU(", "))
+//usage: IF_FEATURE_TOP_SMP_CPU("1: toggle SMP")
+//usage: )
+//usage: "\n"" Q,^C: exit"
+//usage: "\n"
+//usage: "\n""Options:"
+//usage: "\n"" -b Batch mode"
+//usage: "\n"" -n N Exit after N iterations"
+//usage: "\n"" -d N Delay between updates"
+//usage: IF_FEATURE_TOPMEM(
+//usage: "\n"" -m Same as 's' key"
+//usage: )
+
+/* Interactive testing:
+ * echo sss | ./busybox top
+ * - shows memory screen
+ * echo sss | ./busybox top -bn1 >mem
+ * - saves memory screen - the *whole* list, not first NROWS processes!
+ * echo .m.s.s.s.s.s.s.q | ./busybox top -b >z
+ * - saves several different screens, and exits
+ *
+ * TODO: -i STRING param as a better alternative?
+ */
+
+int top_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int top_main(int argc UNUSED_PARAM, char **argv)
+{
+ int iterations;
+ unsigned lines, col;
+ unsigned interval;
+ char *str_interval, *str_iterations;
+ unsigned scan_mask = TOP_MASK;
+#if ENABLE_FEATURE_USE_TERMIOS
+ struct termios new_settings;
+#endif
+
+ INIT_G();
+
+ interval = 5; /* default update interval is 5 seconds */
+ iterations = 0; /* infinite */
+#if ENABLE_FEATURE_TOP_SMP_CPU
+ /*num_cpus = 0;*/
+ /*smp_cpu_info = 0;*/ /* to start with show aggregate */
+ cpu_jif = &cur_jif;
+ cpu_prev_jif = &prev_jif;
+#endif
+
+ /* all args are options; -n NUM */
+ opt_complementary = "-"; /* options can be specified w/o dash */
+ col = getopt32(argv, "d:n:b"IF_FEATURE_TOPMEM("m"), &str_interval, &str_iterations);
+#if ENABLE_FEATURE_TOPMEM
+ if (col & OPT_m) /* -m (busybox specific) */
+ scan_mask = TOPMEM_MASK;
+#endif
+ if (col & OPT_d) {
+ /* work around for "-d 1" -> "-d -1" done by getopt32
+ * (opt_complementary == "-" does this) */
+ if (str_interval[0] == '-')
+ str_interval++;
+ /* Need to limit it to not overflow poll timeout */
+ interval = xatou16(str_interval);
+ }
+ if (col & OPT_n) {
+ if (str_iterations[0] == '-')
+ str_iterations++;
+ iterations = xatou(str_iterations);
+ }
+
+ /* change to /proc */
+ xchdir("/proc");
+
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+ sort_function[0] = pcpu_sort;
+ sort_function[1] = mem_sort;
+ sort_function[2] = time_sort;