after

Echo a string after process dies
git clone _git@git.zakaria.org/after.git
Log | Files | Refs | README | LICENSE

commit f518a1d404813c9bb423682c14a0d859f73737a2
parent aabe7fd0afc52fdca961fbd8dcef4db7cec06003
Author: e-zk <58356365+e-zk@users.noreply.github.com>
Date:   Wed,  9 Dec 2020 14:51:47 +1000

simplify verbose printing; reorder opts

Diffstat:
Mafter.c | 35+++++++++++++++++++++++------------
1 file changed, 23 insertions(+), 12 deletions(-)

diff --git a/after.c b/after.c @@ -10,6 +10,9 @@ #include <unistd.h> #include <limits.h> +char *progname; +int verbose; + void usage(char *progname) { @@ -17,6 +20,14 @@ usage(char *progname) progname); } +void +debug_print(const char *message) +{ + if (verbose == 1) + fprintf(stderr, "%s: %s\n", progname, message); +} + + int pid_is_in(int pid, struct kinfo_proc **kinfo, int entries) { @@ -63,26 +74,28 @@ int main(int argc, char *argv[]) { int i, ch, entries; - int verbose = 0, pid = 0; - char *progname = argv[0]; + int pid = 0; char *pname = NULL, *cmd = NULL; char errbuf[_POSIX2_LINE_MAX]; kvm_t *kd; struct kinfo_proc **kinfo; + verbose = 0; + progname = argv[0]; + // argument parsing... - while((ch = getopt(argc, argv, "hn:p:ve:")) != -1) + while((ch = getopt(argc, argv, "e:hn:p:v")) != -1) switch(ch) { + case 'e': + cmd = optarg; + break; case 'n': pname = optarg; break; case 'p': sscanf(optarg, "%d", &pid); break; - case 'e': - cmd = optarg; - break; case 'v': verbose = 1; break; @@ -119,22 +132,20 @@ main(int argc, char *argv[]) if (pname != NULL) { while(pname_is_in(pname, kinfo, entries) == 0) { kinfo = get_proc_list(kd, &entries); - if (verbose == 1) - fprintf(stderr, "%s: waiting...\n", progname); + debug_print("waiting..."); sleep(1); } } else { while(pid_is_in(pid, kinfo, entries) == 0) { kinfo = get_proc_list(kd, &entries); - if (verbose == 1) - fprintf(stderr, "%s: waiting...\n", progname); + debug_print("waiting..."); sleep(1); } } - if (verbose == 1) - fprintf(stderr, "%s: process not in process list (died?).\n", progname); + + debug_print("process not in process list (died?)."); printf("%s\n", cmd); return 0;