annotate toys/lsb/pidof.c @ 1057:242c5de2bb22 draft

Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
author Rob Landley <rob@landley.net>
date Tue, 03 Sep 2013 18:43:32 -0500
parents f169d9708518
children 7ada6da9540a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
1 /* pidof.c - Print the Process IDs of all processes with the given names.
474
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2012 Andreas Heck <aheck@gmx.de>
744
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
4 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
474
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 *
656
6df4ccc0acbe Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
6 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html
474
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
7
745
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
8 USE_PIDOF(NEWTOY(pidof, "<1so:", TOYFLAG_USR|TOYFLAG_BIN))
474
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
9
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 config PIDOF
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
11 bool "pidof"
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
12 default y
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
13 help
744
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
14 usage: pidof [-s] [-o omitpid[,omitpid..]] [NAME]...
474
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
15
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
16 Print the PIDs of all processes with the given names.
744
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
17 -s single shot, only return one pid.
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
18 -o omits processes with specified PID
474
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 */
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
20
744
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
21 #define FOR_pidof
474
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 #include "toys.h"
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
23
744
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
24 GLOBALS(
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
25 char *omit;
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
26 )
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
27
762
f169d9708518 Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 745
diff changeset
28 static int print_pid(pid_t pid, char * name)
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
29 {
745
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
30 char * res;
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
31 int len;
744
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
32
745
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
33 sprintf(toybuf, "%d", pid);
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
34 len = strlen(toybuf);
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
35
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
36 // Check omit string
744
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
37 if (toys.optflags & FLAG_o)
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
38 {
745
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
39 res = strstr(TT.omit, toybuf);
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
40 if (res && (res == TT.omit || res[-1] == ',') &&
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
41 (res[len] == ',' || res[len] == 0)) return 1;
744
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
42 }
745
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
43 xprintf("%*s", len+(!toys.exitval), toybuf);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
44 toys.exitval = 0;
744
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
45
1057
242c5de2bb22 Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Rob Landley <rob@landley.net>
parents: 762
diff changeset
46 return toys.optflags & FLAG_s;
474
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 }
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
48
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 void pidof_main(void)
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
51 toys.exitval = 1;
1057
242c5de2bb22 Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Rob Landley <rob@landley.net>
parents: 762
diff changeset
52 name_to_pid(toys.optargs, print_pid);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
53 if (!toys.exitval) xputc('\n');
474
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 }