10f66f451Sopenharmony_ci/* pidof.c - Print the Process IDs of all processes with the given names.
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2012 Andreas Heck <aheck@gmx.de>
40f66f451Sopenharmony_ci * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
50f66f451Sopenharmony_ci *
60f66f451Sopenharmony_ci * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html
70f66f451Sopenharmony_ci
80f66f451Sopenharmony_ciUSE_PIDOF(NEWTOY(pidof, "so:x", TOYFLAG_BIN))
90f66f451Sopenharmony_ci
100f66f451Sopenharmony_ciconfig PIDOF
110f66f451Sopenharmony_ci  bool "pidof"
120f66f451Sopenharmony_ci  default y
130f66f451Sopenharmony_ci  help
140f66f451Sopenharmony_ci    usage: pidof [-s] [-o omitpid[,omitpid...]] [NAME...]
150f66f451Sopenharmony_ci
160f66f451Sopenharmony_ci    Print the PIDs of all processes with the given names.
170f66f451Sopenharmony_ci
180f66f451Sopenharmony_ci    -o	Omit PID(s)
190f66f451Sopenharmony_ci    -s	Single shot, only return one pid
200f66f451Sopenharmony_ci    -x	Match shell scripts too
210f66f451Sopenharmony_ci*/
220f66f451Sopenharmony_ci
230f66f451Sopenharmony_ci#define FOR_pidof
240f66f451Sopenharmony_ci#include "toys.h"
250f66f451Sopenharmony_ci
260f66f451Sopenharmony_ciGLOBALS(
270f66f451Sopenharmony_ci  char *o;
280f66f451Sopenharmony_ci)
290f66f451Sopenharmony_ci
300f66f451Sopenharmony_cistatic int print_pid(pid_t pid, char *name)
310f66f451Sopenharmony_ci{
320f66f451Sopenharmony_ci  sprintf(toybuf, "%d", (int)pid);
330f66f451Sopenharmony_ci  if (comma_scan(TT.o, toybuf, 0)) return 0;
340f66f451Sopenharmony_ci  xprintf(" %s"+!!toys.exitval, toybuf);
350f66f451Sopenharmony_ci  toys.exitval = 0;
360f66f451Sopenharmony_ci
370f66f451Sopenharmony_ci  return FLAG(s);
380f66f451Sopenharmony_ci}
390f66f451Sopenharmony_ci
400f66f451Sopenharmony_civoid pidof_main(void)
410f66f451Sopenharmony_ci{
420f66f451Sopenharmony_ci  toys.exitval = 1;
430f66f451Sopenharmony_ci  names_to_pid(toys.optargs, print_pid, FLAG(x));
440f66f451Sopenharmony_ci  if (!toys.exitval) xputc('\n');
450f66f451Sopenharmony_ci}
46