summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko2006-09-29 17:16:39 +0000
committerDenis Vlasenko2006-09-29 17:16:39 +0000
commit266bc17690cd8e98bf30514a19f95891b6ccd1d3 (patch)
tree75afe0e05044880298a5ad38a04ad3213f0d2fd3
parentb1726785f947c35c11d2148196b8fa8b30445798 (diff)
downloadbusybox-266bc17690cd8e98bf30514a19f95891b6ccd1d3.zip
busybox-266bc17690cd8e98bf30514a19f95891b6ccd1d3.tar.gz
top: add support for -b, -n <iterations>
-rw-r--r--include/usage.h10
-rw-r--r--libbb/getopt_ulflags.c8
-rw-r--r--procps/ps.c4
-rw-r--r--procps/top.c47
4 files changed, 34 insertions, 35 deletions
diff --git a/include/usage.h b/include/usage.h
index 3c97446..bb814f1 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -2938,13 +2938,11 @@ USE_FEATURE_START_STOP_DAEMON_FANCY( \
"\t-v\tDisplays verbose resource usage information"
#define top_trivial_usage \
- "[-d <seconds>]"
+ "[-b] [-n count] [-d seconds]"
#define top_full_usage \
- "top provides an view of processor activity in real time.\n" \
- "This utility reads the status for all processes in /proc each <seconds>\n" \
- "and shows the status for however many processes will fit on the screen.\n" \
- "This utility will not show processes that are started after program startup,\n" \
- "but it will show the EXIT status for and PIDs that exit while it is running."
+ "top provides a view of process activity in real time.\n" \
+ "It reads the status of all processes from /proc each <seconds>\n" \
+ "and shows the status for however many processes will fit on the screen."
#define touch_trivial_usage \
"[-c] FILE [FILE ...]"
diff --git a/libbb/getopt_ulflags.c b/libbb/getopt_ulflags.c
index 988f3f9..9d27c1f 100644
--- a/libbb/getopt_ulflags.c
+++ b/libbb/getopt_ulflags.c
@@ -319,9 +319,7 @@ bb_getopt_ulflags(int argc, char **argv, const char *applet_opts, ...)
const struct option *l_o;
#endif
unsigned long trigger;
-#ifdef CONFIG_PS
char **pargv = NULL;
-#endif
int min_arg = 0;
int max_arg = -1;
@@ -461,9 +459,7 @@ bb_getopt_ulflags(int argc, char **argv, const char *applet_opts, ...)
#else
while ((c = getopt(argc, argv, applet_opts)) >= 0) {
#endif /* ENABLE_GETOPT_LONG */
-#if ENABLE_PS
loop_arg_is_opt:
-#endif
for (on_off = complementally; on_off->opt != c; on_off++) {
/* c==0 if long opt have non NULL flag */
if (on_off->opt == 0 && c != 0)
@@ -485,13 +481,10 @@ loop_arg_is_opt:
} else if (on_off->optarg) {
*(char **)(on_off->optarg) = optarg;
}
-#if ENABLE_PS
if (pargv != NULL)
break;
-#endif
}
-#if ENABLE_PS
if (spec_flgs & ALL_ARGV_IS_OPTS) {
/* process argv is option, for example "ps" applet */
if (pargv == NULL)
@@ -506,7 +499,6 @@ loop_arg_is_opt:
}
}
}
-#endif
#if (ENABLE_AR || ENABLE_TAR) && ENABLE_FEATURE_CLEAN_UP
if (spec_flgs & FREE_FIRST_ARGV_IS_OPT)
diff --git a/procps/ps.c b/procps/ps.c
index d88fe4a..95502b2 100644
--- a/procps/ps.c
+++ b/procps/ps.c
@@ -16,14 +16,14 @@ int ps_main(int argc, char **argv)
#if ENABLE_SELINUX
int use_selinux = 0;
- security_context_t sid=NULL;
+ security_context_t sid = NULL;
#endif
#if ENABLE_FEATURE_PS_WIDE
int terminal_width;
int w_count = 0;
- bb_opt_complementally="-:ww";
+ bb_opt_complementally = "-:ww";
#else
# define terminal_width 79
#endif
diff --git a/procps/top.c b/procps/top.c
index 7043d3e..be8ac19 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -30,12 +30,12 @@
#include "busybox.h"
-//#define CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE /* + 2k */
-
typedef int (*cmp_t)(procps_status_t *P, procps_status_t *Q);
static procps_status_t *top; /* Hehe */
static int ntop;
+static unsigned option_mask;
+#define OPT_BATCH_MODE (option_mask & 0x4)
#ifdef CONFIG_FEATURE_USE_TERMIOS
static int pid_sort(procps_status_t *P, procps_status_t *Q)
@@ -251,9 +251,11 @@ static unsigned long display_generic(int scr_width)
snprintf(scrbuf, scr_width,
"Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached",
used, mfree, shared, buffers, cached);
- printf("\e[H\e[J%s\n", scrbuf);
+
+ printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
+
snprintf(scrbuf, scr_width,
- "Load average: %s (Status: S=sleeping R=running, W=waiting)", buf);
+ "Load average: %s", buf);
printf("%s\n", scrbuf);
return total;
@@ -276,12 +278,12 @@ static void display_status(int count, int scr_width)
unsigned pcpu_shift, pcpu_scale;
/* what info of the processes is shown */
- printf("\e[7m%.*s\e[0m", scr_width,
+ printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
" PID USER STATUS RSS PPID %CPU %MEM COMMAND");
#define MIN_WIDTH \
sizeof( " PID USER STATUS RSS PPID %CPU %MEM C")
#else
- printf("\e[7m%.*s\e[0m", scr_width,
+ printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
" PID USER STATUS RSS PPID %MEM COMMAND");
#define MIN_WIDTH \
sizeof( " PID USER STATUS RSS PPID %MEM C")
@@ -320,9 +322,9 @@ static void display_status(int count, int scr_width)
}
/* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
#endif
-
- while (count--) {
- div_t pmem = div( (s->rss*pmem_scale) >> pmem_shift, 10);
+ if (OPT_BATCH_MODE) count--;
+ while (count-- > 0) {
+ div_t pmem = div((s->rss*pmem_scale) >> pmem_shift, 10);
int col = scr_width+1;
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(div_t pcpu;)
@@ -343,7 +345,7 @@ static void display_status(int count, int scr_width)
s++;
}
/* printf(" %d", hist_iterations); */
- putchar('\r');
+ putchar(OPT_BATCH_MODE ? '\n' : '\r');
fflush(stdout);
}
@@ -382,8 +384,10 @@ static void sig_catcher(int sig ATTRIBUTE_UNUSED)
int top_main(int argc, char **argv)
{
- int opt, interval, lines, col;
- char *sinterval;
+ int count, lines, col;
+ int interval = 5; /* default update rate is 5 seconds */
+ int iterations = -1; /* 2^32 iterations by default :) */
+ char *sinterval, *siterations;
#ifdef CONFIG_FEATURE_USE_TERMIOS
struct termios new_settings;
struct timeval tv;
@@ -393,9 +397,12 @@ int top_main(int argc, char **argv)
/* do normal option parsing */
interval = 5;
- opt = bb_getopt_ulflags(argc, argv, "d:", &sinterval);
- if (opt & 1)
- interval = atoi(sinterval);
+ bb_opt_complementally = "-";
+ option_mask = bb_getopt_ulflags(argc, argv, "d:n:b",
+ &sinterval, &siterations);
+ if (option_mask & 0x1) interval = atoi(sinterval); // -d
+ if (option_mask & 0x2) iterations = atoi(siterations); // -n
+ //if (option_mask & 0x4) // -b
/* change to /proc */
xchdir("/proc");
@@ -456,12 +463,12 @@ int top_main(int argc, char **argv)
#else
qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function);
#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
- opt = lines;
- if (opt > ntop) {
- opt = ntop;
+ count = lines;
+ if (count > ntop) {
+ count = ntop;
}
/* show status for each of the processes */
- display_status(opt, col);
+ display_status(count, col);
#ifdef CONFIG_FEATURE_USE_TERMIOS
tv.tv_sec = interval;
tv.tv_usec = 0;
@@ -503,6 +510,8 @@ int top_main(int argc, char **argv)
#endif
}
}
+ if (!--iterations)
+ break;
#else
sleep(interval);
#endif /* CONFIG_FEATURE_USE_TERMIOS */