diff options
author | Leah Neukirchen <leah@vuxu.org> | 2020-04-03 20:29:35 +0200 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2020-04-03 20:29:35 +0200 |
commit | 385d3d71ffadaf30ae115d79a5626cbce059a974 (patch) | |
tree | 254a677946f13d0c63a423af91c0f50f16f0048d | |
parent | 34a9899d1156f15de106b42250aa78d411c685e6 (diff) | |
download | px-385d3d71ffadaf30ae115d79a5626cbce059a974.tar.gz px-385d3d71ffadaf30ae115d79a5626cbce059a974.tar.xz px-385d3d71ffadaf30ae115d79a5626cbce059a974.zip |
add -f to search full commandline
-rw-r--r-- | px.c | 54 |
1 files changed, 37 insertions, 17 deletions
diff --git a/px.c b/px.c index bf74d51..d1cd3b4 100644 --- a/px.c +++ b/px.c @@ -18,6 +18,8 @@ #include <proc/readproc.h> +int fflag; + static void print_human(intmax_t i) { @@ -64,6 +66,8 @@ main(int argc, char *argv[]) struct sysinfo si; sysinfo(&si); + pid_t me = getpid(); + time_t now = time(0); proc_t **result; @@ -72,29 +76,45 @@ main(int argc, char *argv[]) int matched = 0; + int c; + while ((c = getopt(argc, argv, "f")) != -1) + switch (c) { + case 'f': fflag = 1; break; + default: + fprintf(stderr, + "Usage: %s [-f] [PATTERN...]\n", argv[0]); + exit(1); + } + for (; *result; result++) { proc_t *p = *result; - if (argc > 1) { - for (int i = 1; i < argc; i++) { - for (size_t j = 0; j < strlen(argv[i]); j++) { - if (!isdigit(argv[i][j])) - goto word; - } - if (p->tid == atoi(argv[i])) - goto match; - else - continue; + if (argc <= optind) + goto match; + + for (int i = optind; i < argc; i++) { + for (size_t j = 0; j < strlen(argv[i]); j++) + if (!isdigit(argv[i][j])) + goto word; + if (p->tid == atoi(argv[i])) + goto match; + else + continue; word: - if (strstr(p->cmd, argv[i])) - goto match; + if (fflag && p->cmdline) { + if (p->tid == me) + continue; /* we'd always match ourself */ + for (int j = 0; p->cmdline[j]; j++) + if (strstr(p->cmdline[j], argv[i])) + goto match; } - continue; -match: - matched++; - } else { - matched++; + else if (strstr(p->cmd, argv[i])) + goto match; } + continue; + +match: + matched++; if (matched == 1) printf(" PID USER %%CPU VSZ RSS START ELAPSED CPUTIME COMMAND\n"); |