Mercurial > hg > toybox
annotate toys/lsb/pidof.c @ 1142:1e4e707fc0bc draft
Fix pidof -o bug aborting output, reported by Ashwini Sharma.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 16 Dec 2013 17:41:25 -0600 |
parents | 7ada6da9540a |
children | 57f2a26fa92c |
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 | 2 * |
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 | 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 | 7 |
745 | 8 USE_PIDOF(NEWTOY(pidof, "<1so:", TOYFLAG_USR|TOYFLAG_BIN)) |
474 | 9 |
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 | 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 | 20 */ |
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 | 23 #include "toys.h" |
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 | 31 char * res; |
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 | 35 len = strlen(toybuf); |
36 | |
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 | 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 | 46 } |
47 | |
48 void pidof_main(void) | |
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 | 53 } |