toybox/toys/lsb/pidof.c
<<
>>
Prefs
   1/* pidof.c - Print the Process IDs of all processes with the given names.
   2 *
   3 * Copyright 2012 Andreas Heck <aheck@gmx.de>
   4 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
   5 *
   6 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html
   7
   8USE_PIDOF(NEWTOY(pidof, "<1so:", TOYFLAG_BIN))
   9
  10config PIDOF
  11  bool "pidof"
  12  default y
  13  help
  14    usage: pidof [-s] [-o omitpid[,omitpid...]] [NAME]...
  15
  16    Print the PIDs of all processes with the given names.
  17
  18    -s  Single shot, only return one pid
  19    -o  Omit PID(s)
  20*/
  21
  22#define FOR_pidof
  23#include "toys.h"
  24
  25GLOBALS(
  26  char *omit;
  27)
  28
  29static int print_pid(pid_t pid, char *name)
  30{
  31  sprintf(toybuf, "%d", (int)pid);
  32  if (comma_scan(TT.omit, toybuf, 0)) return 0;
  33  xprintf(" %s"+!!toys.exitval, toybuf);
  34  toys.exitval = 0;
  35
  36  return toys.optflags & FLAG_s;
  37}
  38
  39void pidof_main(void)
  40{
  41  toys.exitval = 1;
  42  names_to_pid(toys.optargs, print_pid);
  43  if (!toys.exitval) xputc('\n');
  44}
  45