On Tue, Mar 19, 2019 at 03:36:41PM +0100, Matteo Croce wrote:
> Hi all,
> 
> I have an idea: implement an option to specify the default separator as
> in propcs-ng:
> 
> https://gitlab.com/procps-ng/procps/commit/73492b182dc60c1605d1b0d62de651fad97807af
> 
>     $ pidof bash
>     17701 14019 5276 2967
> 
>     $ pidof -S, bash      
>     17701,14019,5276,2967
> 
>     $ pidof -S'                      
>     ' bash
>     17701
>     14019
>     5276
>     2967
> 
> This should be enough to avoid extra calls to grep/sed/awk and
> impossible to exploit.

I would second this solution, provided that the allowed separator is
exactly one character. Either '-S' or '-F' would recall similar flags
in other tools, and would do the trick.

A possible patch is attached

HND

KatolaZ

diff --git a/man/pidof.8 b/man/pidof.8
index a4295b92..1a993255 100644
--- a/man/pidof.8
+++ b/man/pidof.8
@@ -28,8 +28,8 @@ pidof -- find the process ID of a running program.
 .IR omitpid[,omitpid...] ]
 .RB [ \-o
 .IR omitpid[,omitpid...]... ]
-.RB [ \-f
-.IR format ]
+.RB [ \-F
+.IR sep ]
 .B program
 .RB [ program... ]
 .SH DESCRIPTION
@@ -68,9 +68,9 @@ shells running the named scripts.
 Tells \fIpidof\fP to omit processes with that process id. The special
 pid \fB%PPID\fP can be used to name the parent process of the \fIpidof\fP
 program, in other words the calling shell or shell script.
-.IP "-f \fIformat\fP"
-Tells \fIpidof\fP to format the process ids in the given \fIprintf\fP style.
-For example \fB" -p%d"\fP is useful for \fIstrace\fP.
+.IP "-F \fIsep\fP"
+Tells \fIpidof\fP to use \fIsep\fP as a separator if more than one PID
+is shown. The default separator is a space.
 .SH "EXIT STATUS"
 .TP
 .B 0
diff --git a/src/killall5.c b/src/killall5.c
index 25b333ea..71f88ed5 100644
--- a/src/killall5.c
+++ b/src/killall5.c
@@ -947,7 +947,7 @@ void pidof_usage(void)
    printf("pidof usage: [options] <program-name>\n\n");
    printf(" -c           Return PIDs with the same root directory\n");
    printf(" -h           Display this help text\n");
-   printf(" -f <format>  Display PIDs in a given printf-style format\n");
+   printf(" -F <sep>     Use the provided character as separator\n");
    printf(" -n           Avoid using stat system function on network shares\n");
    printf(" -o <pid>     Omit results with a given PID\n");
    printf(" -q           Quiet mode. Do not display output\n");
@@ -997,7 +997,7 @@ int main_pidof(int argc, char **argv)
 	int		chroot_check = 0;
 	struct stat	st;
 	char		tmp[512];
-        char            *format = NULL;
+        char            sep = ' ';
 
 	omit = (OMIT*)0;
 	nlist = (NFS*)0;
@@ -1006,7 +1006,7 @@ int main_pidof(int argc, char **argv)
 	if ((token = getenv("PIDOF_NETFS")) && (strcmp(token,"no") != 0))
 		flags |= PIDOF_NETFS;
 
-	while ((opt = getopt(argc,argv,"qhco:f:sxn")) != EOF) switch (opt) {
+	while ((opt = getopt(argc,argv,"qhco:F:sxn")) != EOF) switch (opt) {
 		case '?':
 			nsyslog(LOG_ERR,"invalid options on command line!\n");
 			closelog();
@@ -1017,8 +1017,8 @@ int main_pidof(int argc, char **argv)
                 case 'h':
                         pidof_usage();
                         exit(0);
-                case 'f':
-                        format = optarg;
+                case 'F':
+                        sep = optarg[0];
                         break;
 		case 'o':
 			here = optarg;
@@ -1116,14 +1116,9 @@ int main_pidof(int argc, char **argv)
 				}
 
 				if ( ~flags & PIDOF_QUIET ) {
-                                    if (format)
-                                       printf(format, p->pid);
-                                    else
-                                    {
 					if (! first)
-						printf(" ");
+						printf("%c", sep);
 					printf("%d", p->pid);
-                                    }
 				}
 				first = 0;
 			}

Attachment: signature.asc
Description: PGP signature

Reply via email to