comparison toys/lsb/pidof.c @ 745:0faab963ea92

Meddle. The <1 has to come first in the option string, normalize whitespace, sprintf of %d maxes out at -2 billion ala 12 bytes with null terminator so we don't need a length check in a 4k buffer, use the "%*s" feature of printf to prepend whitespace for us, take advantage of c99 defining ! to return 0 or 1.
author Rob Landley <rob@landley.net>
date Sun, 09 Dec 2012 13:57:04 -0600
parents 43e6ec52aa29
children f169d9708518
comparison
equal deleted inserted replaced
744:43e6ec52aa29 745:0faab963ea92
3 * Copyright 2012 Andreas Heck <aheck@gmx.de> 3 * Copyright 2012 Andreas Heck <aheck@gmx.de>
4 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> 4 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
5 * 5 *
6 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html 6 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html
7 7
8 USE_PIDOF(NEWTOY(pidof, "so:<1", TOYFLAG_USR|TOYFLAG_BIN)) 8 USE_PIDOF(NEWTOY(pidof, "<1so:", TOYFLAG_USR|TOYFLAG_BIN))
9 9
10 config PIDOF 10 config PIDOF
11 bool "pidof" 11 bool "pidof"
12 default y 12 default y
13 help 13 help
25 char *omit; 25 char *omit;
26 ) 26 )
27 27
28 static int print_pid(pid_t pid) 28 static int print_pid(pid_t pid)
29 { 29 {
30 char * res;
31 int len;
30 32
33 sprintf(toybuf, "%d", pid);
34 len = strlen(toybuf);
35
36 // Check omit string
31 if (toys.optflags & FLAG_o) 37 if (toys.optflags & FLAG_o)
32 { 38 {
33 char * res; 39 res = strstr(TT.omit, toybuf);
34 int len; 40 if (res && (res == TT.omit || res[-1] == ',') &&
35 snprintf(toybuf, sizeof(toybuf), "%d", pid); 41 (res[len] == ',' || res[len] == 0)) return 1;
36 len = strlen(toybuf);
37 res = strstr(TT.omit, toybuf);
38 if (res &&
39 (res == TT.omit || res[-1] == ',') &&
40 (res[len] == ',' || res[len] == 0))
41 // Found in omit string
42 return 1;
43 } 42 }
44 43 xprintf("%*s", len+(!toys.exitval), toybuf);
45 xprintf("%s%ld", toys.exitval ? "" : " ", (long)pid);
46 toys.exitval = 0; 44 toys.exitval = 0;
47 45
48 if (toys.optflags & FLAG_s) 46 return !(toys.optflags & FLAG_s);
49 return 0;
50
51 return 1;
52 } 47 }
53 48
54 void pidof_main(void) 49 void pidof_main(void)
55 { 50 {
56 toys.exitval = 1; 51 toys.exitval = 1;