From 7c6209d6c336df74984e62aa88679c06641ae309 Mon Sep 17 00:00:00 2001 From: Elie De Brauwer Date: Sat, 8 Dec 2012 20:10:05 +0100 Subject: Adding -s (single shot) and -o (omit pids) options to pidof --- toys/lsb/pidof.c | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'toys/lsb/pidof.c') diff --git a/toys/lsb/pidof.c b/toys/lsb/pidof.c index 03643015..8aeb33e4 100644 --- a/toys/lsb/pidof.c +++ b/toys/lsb/pidof.c @@ -1,26 +1,54 @@ /* pidof.c - Print the Process IDs of all processes with the given names. * * Copyright 2012 Andreas Heck + * Copyright 2012 Elie De Brauwer * * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html -USE_PIDOF(NEWTOY(pidof, "<1", TOYFLAG_USR|TOYFLAG_BIN)) +USE_PIDOF(NEWTOY(pidof, "so:<1", TOYFLAG_USR|TOYFLAG_BIN)) config PIDOF bool "pidof" default y help - usage: pidof [NAME]... + usage: pidof [-s] [-o omitpid[,omitpid..]] [NAME]... Print the PIDs of all processes with the given names. + -s single shot, only return one pid. + -o omits processes with specified PID */ +#define FOR_pidof #include "toys.h" -static void print_pid(pid_t pid) +GLOBALS( + char *omit; +) + +static int print_pid(pid_t pid) { + + if (toys.optflags & FLAG_o) + { + char * res; + int len; + snprintf(toybuf, sizeof(toybuf), "%d", pid); + len = strlen(toybuf); + res = strstr(TT.omit, toybuf); + if (res && + (res == TT.omit || res[-1] == ',') && + (res[len] == ',' || res[len] == 0)) + // Found in omit string + return 1; + } + xprintf("%s%ld", toys.exitval ? "" : " ", (long)pid); toys.exitval = 0; + + if (toys.optflags & FLAG_s) + return 0; + + return 1; } void pidof_main(void) -- cgit v1.2.3