From eugen@grosbein.pp.ru  Sun Sep  7 09:15:06 2003
Return-Path: <eugen@grosbein.pp.ru>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 9C17216A4BF
	for <FreeBSD-gnats-submit@freebsd.org>; Sun,  7 Sep 2003 09:15:06 -0700 (PDT)
Received: from grosbein.pp.ru (D00015.dialonly.kemerovo.su [213.184.66.105])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 7462D43FF2
	for <FreeBSD-gnats-submit@freebsd.org>; Sun,  7 Sep 2003 09:15:02 -0700 (PDT)
	(envelope-from eugen@grosbein.pp.ru)
Received: from grosbein.pp.ru (smmsp@localhost [127.0.0.1])
	by grosbein.pp.ru (8.12.9/8.12.9) with ESMTP id h87G7wZ1001880
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 8 Sep 2003 00:07:58 +0800 (KRAST)
	(envelope-from eugen@grosbein.pp.ru)
Received: (from eugen@localhost)
	by grosbein.pp.ru (8.12.9/8.12.9/Submit) id h87G6l5t001833;
	Mon, 8 Sep 2003 00:06:47 +0800 (KRAST)
	(envelope-from eugen)
Message-Id: <200309071606.h87G6l5t001833@grosbein.pp.ru>
Date: Mon, 8 Sep 2003 00:06:47 +0800 (KRAST)
From: Eugene Grosbein <eugen@grosbein.pp.ru>
Reply-To: Eugene Grosbein <eugen@grosbein.pp.ru>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [PATCH] locate(1) cannot be safely used with xargs(1)
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         56558
>Category:       bin
>Synopsis:       [PATCH] locate(1) cannot be safely used with xargs(1)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    des
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sun Sep 07 09:20:00 PDT 2003
>Closed-Date:    Wed Dec 21 12:18:09 GMT 2005
>Last-Modified:  Wed Dec 21 12:18:09 GMT 2005
>Originator:     Eugene Grosbein
>Release:        FreeBSD 4.9-PRERELEASE i386
>Organization:
Svyaz Service JSC
>Environment:
System: FreeBSD grosbein.pp.ru 4.9-PRERELEASE FreeBSD 4.9-PRERELEASE #19: Fri Sep 5 23:33:57 KRAST 2003 eu@grosbein.pp.ru:/usr/local/obj/usr/local/src/sys/DADV i386

>Description:

	'locate pattern | xargs' will fail when found pathname
	contains signle quote character, for example.

>How-To-Repeat:

	Obvious.

>Fix:

	Implement an option -0 for locate(1) to make it compatible
	with xargs -0. A patch for locate(1) man page and source follows.

--- locate.1.orig	Tue Jul  9 05:31:28 2002
+++ locate.1	Sun Sep  7 23:58:21 2003
@@ -41,7 +41,7 @@
 .Nd find filenames quickly
 .Sh SYNOPSIS
 .Nm
-.Op Fl Scims
+.Op Fl 0Scims
 .Op Fl l Ar limit
 .Op Fl d Ar database
 .Ar pattern ...
@@ -89,6 +89,11 @@
 .Pp
 The following options are available:
 .Bl -tag -width 10n
+.It Fl 0
+Print pathnames separated by an
+.Tn ASCII NUL
+character (character code 0) instead of default NL
+(newline, character code 10).
 .It Fl S
 Print some statistic about the database and exit.
 .It Fl c
--- fastfind.c.orig	Sun Sep  7 23:49:30 2003
+++ fastfind.c	Sun Sep  7 23:44:23 2003
@@ -103,6 +103,7 @@
 }
 #endif /* _LOCATE_STATISTIC_ */
 
+extern	char	separator;
 
 void
 #ifdef FF_MMAP
@@ -315,11 +316,11 @@
 						else if (f_limit) {
 							counter++;
 							if (f_limit >= counter)
-								(void)puts(path);
+								(void)printf("%s%c",path,separator);
 							else 
 								errx(0, "[show only %d lines]", counter - 1);
 						} else
-							(void)puts(path);
+							(void)printf("%s%c",path,separator);
 					}
 					break;
 				}
--- locate.c.orig	Sun Mar  4 15:47:25 2001
+++ locate.c	Sun Sep  7 23:44:30 2003
@@ -120,6 +120,7 @@
 int f_silent;           /* suppress output, show only count of matches */
 int f_limit;            /* limit number of output lines, 0 == infinite */
 u_int counter;          /* counter for matches [-c] */
+char separator='\n';	/* line separator */
 
 
 void    usage __P((void));
@@ -152,8 +153,11 @@
 #endif
 	(void) setlocale(LC_ALL, "");
 
-        while ((ch = getopt(argc, argv, "Scd:il:ms")) != -1)
+        while ((ch = getopt(argc, argv, "0Scd:il:ms")) != -1)
                 switch(ch) {
+                case '0':	/* 'find -print0' style */
+			separator = '\0';
+			break;
                 case 'S':	/* statistic lines */   
                         f_statistic = 1;
                         break;



Eugene Grosbein
>Release-Note:
>Audit-Trail:

From: FUJISHIMA Satsuki <sf@FreeBSD.org>
To: Eugene Grosbein <eugen@grosbein.pp.ru>
Cc: FreeBSD-gnats-submit@FreeBSD.org
Subject: Re: bin/56558: [PATCH] locate(1) cannot be safely used with xargs(1)
Date: Fri, 12 Sep 2003 13:28:21 +0900

 command | tr '\n' '\0' | xargs -0 foo
 would work for any commands, not limited to locate(1).

From: Eugene Grosbein <eugen@kuzbass.ru>
To: FUJISHIMA Satsuki <sf@FreeBSD.org>
Cc: Eugene Grosbein <eugen@grosbein.pp.ru>,
	FreeBSD-gnats-submit@FreeBSD.org
Subject: Re: bin/56558: [PATCH] locate(1) cannot be safely used with xargs(1)
Date: Fri, 12 Sep 2003 13:12:18 +0800

 FUJISHIMA Satsuki wrote:
 > 
 > command | tr '\n' '\0' | xargs -0 foo
 > would work for any commands, not limited to locate(1).
 
 It will break when filename contains '\n' that is allowed.
 
 Eugene Grosbein

From: Eugene Grosbein <eugen@kuzbass.ru>
To: bug-followup@freebsd.org
Cc: stable@freebsd.org
Subject: Re: bin/56558: [PATCH] locate(1) cannot be safely used with xargs(1)
Date: Mon, 9 May 2005 23:44:37 +0800

 Hi!
 
 The problem is still here for 5.4-STABLE.
 
 http://www.freebsd.org/cgi/query-pr.cgi?pr=bin/56558
 
 Eugene Grosbein
State-Changed-From-To: open->patched 
State-Changed-By: des 
State-Changed-When: Wed Dec 7 12:23:23 GMT 2005 
State-Changed-Why:  
Fixed in -CURRENT, awaiting MFC. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=56558 
Responsible-Changed-From-To: freebsd-bugs->des 
Responsible-Changed-By: des 
Responsible-Changed-When: Wed Dec 7 12:23:33 GMT 2005 
Responsible-Changed-Why:  
Will handle MFC. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=56558 
State-Changed-From-To: patched->closed 
State-Changed-By: des 
State-Changed-When: Wed Dec 21 12:18:07 UTC 2005 
State-Changed-Why:  
MFCed. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=56558 
>Unformatted:
