pidof.c 986 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. USE_PIDOF(NEWTOY(pidof, "so:x", TOYFLAG_BIN))
  8. config PIDOF
  9. bool "pidof"
  10. default y
  11. help
  12. usage: pidof [-s] [-o omitpid[,omitpid...]] [NAME...]
  13. Print the PIDs of all processes with the given names.
  14. -o Omit PID(s)
  15. -s Single shot, only return one pid
  16. -x Match shell scripts too
  17. */
  18. #define FOR_pidof
  19. #include "toys.h"
  20. GLOBALS(
  21. char *o;
  22. )
  23. static int print_pid(pid_t pid, char *name)
  24. {
  25. sprintf(toybuf, "%d", (int)pid);
  26. if (comma_scan(TT.o, toybuf, 0)) return 0;
  27. xprintf(" %s"+!!toys.exitval, toybuf);
  28. toys.exitval = 0;
  29. return FLAG(s);
  30. }
  31. void pidof_main(void)
  32. {
  33. toys.exitval = 1;
  34. names_to_pid(toys.optargs, print_pid, FLAG(x));
  35. if (!toys.exitval) xputc('\n');
  36. }