aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toys/posix/ps.c115
1 files changed, 55 insertions, 60 deletions
diff --git a/toys/posix/ps.c b/toys/posix/ps.c
index 76873861..f5e080c7 100644
--- a/toys/posix/ps.c
+++ b/toys/posix/ps.c
@@ -489,10 +489,10 @@ static int ps_match_process(long long *slot)
if (!i) return 0;
// Filter implicit categories for other display types
- if ((toys.optflags&(FLAG_a|FLAG_d)) && slot[SLOT_sid]==*slot) return 0;
- if ((toys.optflags&FLAG_a) && !slot[SLOT_ttynr]) return 0;
- if (!(toys.optflags&(FLAG_a|FLAG_d|FLAG_A|FLAG_e))
- && TT.tty!=slot[SLOT_ttynr]) return 0;
+ if ((FLAG(a)||FLAG(d)) && slot[SLOT_sid]==*slot) return 0;
+ if (FLAG(a) && !slot[SLOT_ttynr]) return 0;
+ if (!(FLAG(a)||FLAG(d)||FLAG(A)||FLAG(e)) && TT.tty!=slot[SLOT_ttynr])
+ return 0;
return 1;
}
@@ -649,7 +649,7 @@ static void show_ps(void *p)
abslen = abs(field->len);
sign = field->len<0 ? -1 : 1;
olen = (TT.tty) ? utf8len(out) : strlen(out);
- if ((field->which<=PS_BIT || (toys.optflags&FLAG_w)) && olen>abslen) {
+ if ((field->which<=PS_BIT || FLAG(w)) && olen>abslen) {
// overflow but remember by how much
extra += olen-abslen;
abslen = olen;
@@ -1271,9 +1271,8 @@ void ps_main(void)
// If we can't query terminal size pad to 80 but do -w
TT.width = 80;
- if (!isatty(1) || !terminal_size(&TT.width, 0))
- toys.optflags |= FLAG_w;
- if (toys.optflags&FLAG_w) TT.width = 99999;
+ if (!isatty(1) || !terminal_size(&TT.width, 0)) toys.optflags |= FLAG_w;
+ if (FLAG(w)) TT.width = 99999;
// parse command line options other than -o
comma_args(TT.ps.P, &TT.PP, "bad -P", parse_rest);
@@ -1289,24 +1288,23 @@ void ps_main(void)
// It's undocumented, but traditionally extra arguments are extra -p args
for (arg = toys.optargs; *arg; arg++)
- if (parse_rest(&TT.pp, *arg, strlen(*arg))) error_exit_raw(*arg);
+ if (parse_rest(&TT.pp, *arg, strlen(*arg))) error_exit("bad %s", *arg);
// Figure out which fields to display
not_o = "%sTTY,TIME,CMD";
- if (toys.optflags&FLAG_f)
+ if (FLAG(f))
sprintf(not_o = toybuf+128,
- "USER:12=UID,%%sPPID,%s,STIME,TTY,TIME,ARGS=CMD",
- (toys.optflags&FLAG_T) ? "TCNT" : "C");
- else if (toys.optflags&FLAG_l)
+ "USER:12=UID,%%sPPID,%s,STIME,TTY,TIME,ARGS=CMD", FLAG(T) ? "TCNT" :"C");
+ else if (FLAG(l))
not_o = "F,S,UID,%sPPID,C,PRI,NI,BIT,SZ,WCHAN,TTY,TIME,CMD";
else if (CFG_TOYBOX_ON_ANDROID)
sprintf(not_o = toybuf+128,
"USER,%%sPPID,VSIZE,RSS,WCHAN:10,ADDR:10,S,%s",
- (toys.optflags&FLAG_T) ? "CMD" : "NAME");
- sprintf(toybuf, not_o, (toys.optflags & FLAG_T) ? "PID,TID," : "PID,");
+ FLAG(T) ? "CMD" : "NAME");
+ sprintf(toybuf, not_o, FLAG(T) ? "PID,TID," : "PID,");
// Init TT.fields. This only uses toybuf if TT.ps.o is NULL
- if (toys.optflags&FLAG_Z) default_ko("LABEL", &TT.fields, 0, 0);
+ if (FLAG(Z)) default_ko("LABEL", &TT.fields, 0, 0);
default_ko(toybuf, &TT.fields, "bad -o", TT.ps.o);
if (TT.ps.O) {
@@ -1317,11 +1315,11 @@ void ps_main(void)
dlist_terminate(TT.fields);
// -f and -n change the meaning of some fields
- if (toys.optflags&(FLAG_f|FLAG_n)) {
+ if (FLAG(f)||FLAG(n)) {
struct ofields *field;
for (field = TT.fields; field; field = field->next) {
- if ((toys.optflags&FLAG_n) && field->which>=PS_UID
+ if (FLAG(n) && field->which>=PS_UID
&& field->which<=PS_RGROUP && (typos[field->which].slot&64))
field->which--;
}
@@ -1330,17 +1328,17 @@ void ps_main(void)
// Calculate seen fields bit array, and if we aren't deferring printing
// print headers now (for low memory/nommu systems).
TT.bits = get_headers(TT.fields, toybuf, sizeof(toybuf));
- if (!(toys.optflags&FLAG_M)) printf("%.*s\n", TT.width, toybuf);
- if (!(toys.optflags&(FLAG_k|FLAG_M))) TT.show_process = show_ps;
+ if (!FLAG(M)) printf("%.*s\n", TT.width, toybuf);
+ if (!(FLAG(k)||FLAG(M))) TT.show_process = show_ps;
TT.match_process = ps_match_process;
dt = dirtree_flagread("/proc", DIRTREE_SHUTUP|DIRTREE_PROC,
- ((toys.optflags&FLAG_T) || (TT.bits&(_PS_TID|_PS_TCNT)))
+ (FLAG(T) || (TT.bits&(_PS_TID|_PS_TCNT)))
? get_threads : get_ps);
- if ((dt != DIRTREE_ABORTVAL) && toys.optflags&(FLAG_k|FLAG_M)) {
+ if ((dt != DIRTREE_ABORTVAL) && (FLAG(k)||FLAG(M))) {
struct procpid **tbsort = collate(TT.kcount, dt);
- if (toys.optflags&FLAG_M) {
+ if (FLAG(M)) {
for (i = 0; i<TT.kcount; i++) {
struct ofields *field;
@@ -1356,8 +1354,7 @@ void ps_main(void)
printf("%.*s\n", TT.width, toybuf);
}
- if (toys.optflags&FLAG_k)
- qsort(tbsort, TT.kcount, sizeof(struct procpid *), (void *)ksort);
+ if (FLAG(k)) qsort(tbsort, TT.kcount, sizeof(void *), (void *)ksort);
for (i = 0; i<TT.kcount; i++) {
show_ps(tbsort[i]);
free(tbsort[i]);
@@ -1419,11 +1416,10 @@ static int header_line(int line, int rev)
{
if (!line) return 0;
- if (toys.optflags&FLAG_b) rev = 0;
+ if (FLAG(b)) rev = 0;
- printf("%s%*.*s%s%s\n", rev ? "\033[7m" : "",
- (toys.optflags&FLAG_b) ? 0 : -TT.width, TT.width, toybuf,
- rev ? "\033[0m" : "", (toys.optflags&FLAG_b) ? "" : "\r");
+ printf("%s%*.*s%s%s\n", rev ? "\033[7m" : "", -TT.width*!!FLAG(b), TT.width,
+ toybuf, rev ? "\033[0m" : "", FLAG(b) ? "" : "\r");
return line-1;
}
@@ -1445,7 +1441,7 @@ static void top_common(
char stdout_buf[BUFSIZ];
// Avoid flicker in interactive mode.
- if (!(toys.optflags&FLAG_b)) setbuf(stdout, stdout_buf);
+ if (!FLAG(b)) setbuf(stdout, stdout_buf);
toys.signal = SIGWINCH;
TT.bits = get_headers(TT.fields, toybuf, sizeof(toybuf));
@@ -1460,8 +1456,7 @@ static void top_common(
plnew = plist+(tock&1);
plnew->whence = millitime();
dt = dirtree_flagread("/proc", DIRTREE_SHUTUP|DIRTREE_PROC,
- ((toys.optflags&FLAG_H) || (TT.bits&(_PS_TID|_PS_TCNT)))
- ? get_threads : get_ps);
+ (FLAG(H) || (TT.bits&(_PS_TID|_PS_TCNT))) ? get_threads : get_ps);
if (dt == DIRTREE_ABORTVAL) error_exit("no /proc");
plnew->tb = collate(plnew->count = TT.kcount, dt);
TT.kcount = 0;
@@ -1519,7 +1514,7 @@ static void top_common(
if (recalc) {
qsort(mix.tb, mix.count, sizeof(struct procpid *), (void *)ksort);
- if (!(toys.optflags&FLAG_b)) {
+ if (!FLAG(b)) {
printf("\033[H\033[J");
if (toys.signal) {
toys.signal = 0;
@@ -1529,7 +1524,7 @@ static void top_common(
if (TT.top.m) TT.height = TT.top.m+5;
lines = TT.height;
}
- if (recalc && !(toys.optflags&FLAG_q)) {
+ if (recalc && !FLAG(q)) {
// Display "top" header.
if (*toys.which->name == 't') {
struct ofields field;
@@ -1617,15 +1612,15 @@ static void top_common(
*pos = 0;
lines = header_line(lines, 1);
}
- if (!recalc && !(toys.optflags&FLAG_b))
+ if (!recalc && !FLAG(b))
printf("\033[%dH\033[J", 1+TT.height-lines);
recalc = 1;
for (i = 0; i<lines && i+topoff<mix.count; i++) {
// Running processes are shown in bold.
- int bold = !(toys.optflags&FLAG_b) && mix.tb[i+topoff]->state == 'R';
+ int bold = !FLAG(b) && mix.tb[i+topoff]->state == 'R';
- if (!(toys.optflags&FLAG_b) && i) putchar('\n');
+ if (!FLAG(b) && i) putchar('\n');
if (bold) printf("\033[1m");
show_ps(mix.tb[i+topoff]);
if (bold) printf("\033[m");
@@ -1641,7 +1636,7 @@ static void top_common(
if (timeout<=now || timeout>now+TT.top.d) timeout = now+TT.top.d;
// In batch mode, we ignore the keyboard.
- if (toys.optflags&FLAG_b) {
+ if (FLAG(b)) {
msleep(timeout-now);
// Make an obvious gap between datasets.
xputs("\n\n");
@@ -1687,7 +1682,7 @@ static void top_common(
free(plold->tb);
} while (!done);
- if (!(toys.optflags&FLAG_b)) tty_reset();
+ if (!FLAG(b)) tty_reset();
}
static void top_setup(char *defo, char *defk)
@@ -1696,7 +1691,7 @@ static void top_setup(char *defo, char *defk)
TT.tty = tty_fd() != -1;
// Are we doing "batch" output or interactive?
- if (toys.optflags&FLAG_b) TT.width = TT.height = 99999;
+ if (FLAG(b)) TT.width = TT.height = 99999;
else {
// Grab starting time, make terminal raw, switch off cursor,
// set signal handler to put terminal/cursor back to normal at exit.
@@ -1722,7 +1717,7 @@ void top_main(void)
{
sprintf(toybuf, "PID,USER,%s%%CPU,%%MEM,TIME+,%s",
TT.top.O ? "" : "PR,NI,VIRT,RES,SHR,S,",
- toys.optflags&FLAG_H ? "CMD:15=THREAD,NAME=PROCESS" : "ARGS");
+ FLAG(H) ? "CMD:15=THREAD,NAME=PROCESS" : "ARGS");
if (!TT.top.s) TT.top.s = TT.top.O ? 3 : 9;
top_setup(toybuf, "-%CPU,-ETIME,-PID");
if (TT.top.O) {
@@ -1739,19 +1734,21 @@ void top_main(void)
#define FOR_iotop
#include "generated/flags.h"
+// Compare old and new proces lists to measure changes
static int iotop_filter(long long *oslot, long long *nslot, int milis)
{
- if (!(toys.optflags&FLAG_a)) merge_deltas(oslot, nslot, milis);
+ // Current I/O, or accumulated since process start?
+ if (!FLAG(a)) merge_deltas(oslot, nslot, milis);
else oslot[SLOT_upticks] = ((millitime()-TT.time)*TT.ticks)/1000;
- return !(toys.optflags&FLAG_o)||oslot[SLOT_iobytes+!(toys.optflags&FLAG_A)];
+ return !FLAG(O)||oslot[SLOT_iobytes+!FLAG(A)];
}
void iotop_main(void)
{
- char *s1 = 0, *s2 = 0, *d = "D"+!!(toys.optflags&FLAG_A);
+ char *s1 = 0, *s2 = 0, *d = "D"+!!FLAG(A);
- if (toys.optflags&FLAG_K) TT.forcek++;
+ if (FLAG(K)) TT.forcek++;
top_setup(s1 = xmprintf("PID,PR,USER,%sREAD,%sWRITE,SWAP,%sIO,COMM",d,d,d),
s2 = xmprintf("-%sIO,-ETIME,-PID",d));
@@ -1783,10 +1780,10 @@ static void do_pgk(struct procpid *tb)
perror_msg("%s->%lld", s, *tb->slot);
}
}
- if (!(toys.optflags&FLAG_c) && (!TT.pgrep.signal || TT.tty)) {
+ if (!FLAG(c) && (!TT.pgrep.signal || TT.tty)) {
printf("%lld", *tb->slot);
- if (toys.optflags&FLAG_l)
- printf(" %s", tb->str+tb->offset[4]*!!(toys.optflags&FLAG_f));
+ if (FLAG(l))
+ printf(" %s", tb->str+tb->offset[4]*!!FLAG(f));
printf("%s", TT.pgrep.d ? TT.pgrep.d : "\n");
}
@@ -1797,7 +1794,7 @@ static void match_pgrep(void *p)
struct procpid *tb = p;
regmatch_t match;
struct regex_list *reg;
- char *name = tb->str+tb->offset[4]*!!(toys.optflags&FLAG_f);;
+ char *name = tb->str+tb->offset[4]*!!FLAG(f);
// Never match ourselves.
if (TT.pgrep.self == *tb->slot) return;
@@ -1805,11 +1802,11 @@ static void match_pgrep(void *p)
if (TT.pgrep.regexes) {
for (reg = TT.pgrep.regexes; reg; reg = reg->next) {
if (regexec(&reg->reg, name, 1, &match, 0)) continue;
- if (toys.optflags&FLAG_x)
+ if (FLAG(x))
if (match.rm_so || match.rm_eo!=strlen(name)) continue;
break;
}
- if ((toys.optflags&FLAG_v) ? !!reg : !reg) return;
+ if (!FLAG(v) == !reg) return;
}
// pgrep should return success if there's a match.
@@ -1817,10 +1814,10 @@ static void match_pgrep(void *p)
// Repurpose a field for -c count.
TT.sortpos++;
- if (toys.optflags&(FLAG_n|FLAG_o)) {
+ if (FLAG(n)||FLAG(o)) {
long long ll = tb->slot[SLOT_starttime];
- if (toys.optflags&FLAG_o) ll *= -1;
+ if (FLAG(o)) ll *= -1;
if (TT.time && TT.time>ll) return;
TT.time = ll;
free(TT.pgrep.snapshot);
@@ -1830,9 +1827,7 @@ static void match_pgrep(void *p)
static int pgrep_match_process(long long *slot)
{
- int match = shared_match_process(slot);
-
- return (toys.optflags&FLAG_v) ? !match : match;
+ return !FLAG(v) == !!shared_match_process(slot);
}
void pgrep_main(void)
@@ -1858,7 +1853,7 @@ void pgrep_main(void)
!(toys.optflags&(FLAG_G|FLAG_g|FLAG_P|FLAG_s|FLAG_t|FLAG_U|FLAG_u)))
if (!toys.optc) help_exit("No PATTERN");
- if (toys.optflags&FLAG_f) TT.bits |= _PS_CMDLINE;
+ if (FLAG(f)) TT.bits |= _PS_CMDLINE;
for (arg = toys.optargs; *arg; arg++) {
reg = xmalloc(sizeof(struct regex_list));
xregcomp(&reg->reg, *arg, REG_EXTENDED);
@@ -1872,7 +1867,7 @@ void pgrep_main(void)
toys.exitval = 1;
dirtree_flagread("/proc", DIRTREE_SHUTUP|DIRTREE_PROC, get_ps);
- if (toys.optflags&FLAG_c) printf("%d\n", TT.sortpos);
+ if (FLAG(c)) printf("%d\n", TT.sortpos);
if (TT.pgrep.snapshot) {
do_pgk(TT.pgrep.snapshot);
if (CFG_TOYBOX_FREE) free(TT.pgrep.snapshot);
@@ -1888,8 +1883,8 @@ void pkill_main(void)
{
char **args = toys.optargs;
- if (!(toys.optflags&FLAG_l) && *args && **args=='-') TT.pgrep.L = *(args++)+1;
+ if (!FLAG(l) && *args && **args=='-') TT.pgrep.L = *(args++)+1;
if (!TT.pgrep.L) TT.pgrep.signal = SIGTERM;
- if (toys.optflags & FLAG_V) TT.tty = 1;
+ if (FLAG(V)) TT.tty = 1;
pgrep_main();
}