annotate toys/lsb/pidof.c @ 1776:7bf68329eb3b draft default tip

Repository switched to git at https://github.com/landley/toybox
author Rob Landley <rob@landley.net>
date Thu, 09 Apr 2015 02:28:32 -0500
parents 57f2a26fa92c
children
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
1775
57f2a26fa92c To ensure that toybox can be installed alongside busybox without
Paul Barker <paul@paulbarker.me.uk>
parents: 1142
diff changeset
8 USE_PIDOF(NEWTOY(pidof, "<1so:", 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
1142
1e4e707fc0bc Fix pidof -o bug aborting output, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1068
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.
1142
1e4e707fc0bc Fix pidof -o bug aborting output, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1068
diff changeset
17
744
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
18 -s single shot, only return one pid.
1142
1e4e707fc0bc Fix pidof -o bug aborting output, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1068
diff changeset
19 -o omit PID(s)
474
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 */
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
21
744
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
22 #define FOR_pidof
474
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 #include "toys.h"
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
744
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
25 GLOBALS(
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
26 char *omit;
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
27 )
43e6ec52aa29 Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 694
diff changeset
28
1142
1e4e707fc0bc Fix pidof -o bug aborting output, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1068
diff changeset
29 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
30 {
745
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
31 char * res;
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
32 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
33
1142
1e4e707fc0bc Fix pidof -o bug aborting output, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1068
diff changeset
34 sprintf(toybuf, "%d", (int)pid);
745
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
35 len = strlen(toybuf);
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
36
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
37 // Check omit string
1142
1e4e707fc0bc Fix pidof -o bug aborting output, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1068
diff changeset
38 if (TT.omit && (res = strstr(TT.omit, toybuf)))
1e4e707fc0bc Fix pidof -o bug aborting output, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1068
diff changeset
39 if ((res == TT.omit || res[-1] == ',') &&
1e4e707fc0bc Fix pidof -o bug aborting output, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1068
diff changeset
40 (res[len] == ',' || !res[len])) return 0;
1e4e707fc0bc Fix pidof -o bug aborting output, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1068
diff changeset
41
745
0faab963ea92 Meddle.
Rob Landley <rob@landley.net>
parents: 744
diff changeset
42 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
43 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
44
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
45 return toys.optflags & FLAG_s;
474
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 }
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 void pidof_main(void)
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 {
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
50 toys.exitval = 1;
1068
7ada6da9540a Ah, that's why commit 1057 was skipped last pull: it was unfinished. Oops. (Fix it.)
Rob Landley <rob@landley.net>
parents: 1057
diff changeset
51 names_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
52 if (!toys.exitval) xputc('\n');
474
d76583999029 Add pidof by Andreas Heck.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 }