diff options
author | Pierre-Yves Ritschard <pyr@cvs.openbsd.org> | 2008-07-26 09:48:01 +0000 |
---|---|---|
committer | Pierre-Yves Ritschard <pyr@cvs.openbsd.org> | 2008-07-26 09:48:01 +0000 |
commit | e7a7a934620a960e5a1f61baf25f66e73d977b17 (patch) | |
tree | bbff41822494851638a3406ffab17436329250cd /usr.bin/locate | |
parent | 50fd48d48fb140b36f30e589b2121c2cf05b0c89 (diff) |
Add -b switch for matching on the last part of the path only.
ok landry@, otto liked the idea and the original diff, ``do it'' deraadt@
Diffstat (limited to 'usr.bin/locate')
-rw-r--r-- | usr.bin/locate/locate/fastfind.c | 15 | ||||
-rw-r--r-- | usr.bin/locate/locate/locate.1 | 9 | ||||
-rw-r--r-- | usr.bin/locate/locate/locate.c | 13 |
3 files changed, 27 insertions, 10 deletions
diff --git a/usr.bin/locate/locate/fastfind.c b/usr.bin/locate/locate/fastfind.c index dd6007e1192..599438f29c5 100644 --- a/usr.bin/locate/locate/fastfind.c +++ b/usr.bin/locate/locate/fastfind.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fastfind.c,v 1.8 2003/09/29 16:03:16 deraadt Exp $ */ +/* $OpenBSD: fastfind.c,v 1.9 2008/07/26 09:48:00 pyr Exp $ */ /* * Copyright (c) 1995 Wolfram Schneider <wosch@FreeBSD.org>. Berlin. @@ -32,7 +32,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: fastfind.c,v 1.8 2003/09/29 16:03:16 deraadt Exp $ + * $Id: fastfind.c,v 1.9 2008/07/26 09:48:00 pyr Exp $ */ #ifndef _LOCATE_STATISTIC_ @@ -295,8 +295,17 @@ fastfind ) break; if (*p == '\0') { /* fast match success */ + char *shortpath; + found = 1; - if (!globflag || !fnmatch(pathpart, path, 0)) { + shortpath = path; + if (f_basename) + shortpath = basename(path); + + if ((!f_basename && (!globflag || + !fnmatch(pathpart, shortpath, 0))) + || (strstr(shortpath, pathpart) != + NULL)) { if (f_silent) counter++; else if (f_limit) { diff --git a/usr.bin/locate/locate/locate.1 b/usr.bin/locate/locate/locate.1 index e5711d6e7f0..a947f966723 100644 --- a/usr.bin/locate/locate/locate.1 +++ b/usr.bin/locate/locate/locate.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: locate.1,v 1.25 2007/06/25 13:12:51 jmc Exp $ +.\" $OpenBSD: locate.1,v 1.26 2008/07/26 09:48:00 pyr Exp $ .\" .\" Copyright (c) 1995 Wolfram Schneider <wosch@FreeBSD.org>. Berlin. .\" Copyright (c) 1990, 1993 @@ -29,9 +29,9 @@ .\" SUCH DAMAGE. .\" .\" @(#)locate.1 8.1 (Berkeley) 6/6/93 -.\" $Id: locate.1,v 1.25 2007/06/25 13:12:51 jmc Exp $ +.\" $Id: locate.1,v 1.26 2008/07/26 09:48:00 pyr Exp $ .\" -.Dd $Mdocdate: June 25 2007 $ +.Dd $Mdocdate: July 26 2008 $ .Dt LOCATE 1 .Os .Sh NAME @@ -90,6 +90,9 @@ Characters less than 32 or greater than 127 are stored as 2 bytes. .Pp The options are as follows: .Bl -tag -width Ds +.It Fl b +For each entry in the database, perform the search on the last +component of path. .It Fl c Suppress normal output; instead print a count of matching file names. .It Fl d Ar database diff --git a/usr.bin/locate/locate/locate.c b/usr.bin/locate/locate/locate.c index d3eba64b39c..fb72f5015bc 100644 --- a/usr.bin/locate/locate/locate.c +++ b/usr.bin/locate/locate/locate.c @@ -1,5 +1,5 @@ /* - * $OpenBSD: locate.c,v 1.19 2006/08/05 23:05:13 ray Exp $ + * $OpenBSD: locate.c,v 1.20 2008/07/26 09:48:00 pyr Exp $ * * Copyright (c) 1995 Wolfram Schneider <wosch@FreeBSD.org>. Berlin. * Copyright (c) 1989, 1993 @@ -32,7 +32,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: locate.c,v 1.19 2006/08/05 23:05:13 ray Exp $ + * $Id: locate.c,v 1.20 2008/07/26 09:48:00 pyr Exp $ */ #ifndef lint @@ -46,7 +46,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)locate.c 8.1 (Berkeley) 6/6/93"; #else -static char rcsid[] = "$OpenBSD: locate.c,v 1.19 2006/08/05 23:05:13 ray Exp $"; +static char rcsid[] = "$OpenBSD: locate.c,v 1.20 2008/07/26 09:48:00 pyr Exp $"; #endif #endif /* not lint */ @@ -82,6 +82,7 @@ static char rcsid[] = "$OpenBSD: locate.c,v 1.19 2006/08/05 23:05:13 ray Exp $"; #include <ctype.h> #include <err.h> #include <fnmatch.h> +#include <libgen.h> #include <locale.h> #include <stdio.h> #include <stdlib.h> @@ -116,6 +117,7 @@ int f_stdin; /* read database from stdin */ int f_statistic; /* print statistic */ int f_silent; /* suppress output, show only count of matches */ int f_limit; /* limit number of output lines, 0 == infinite */ +int f_basename; /* match only on the basename */ u_int counter; /* counter for matches [-c] */ @@ -151,8 +153,11 @@ main(int argc, char *argv[]) #endif (void) setlocale(LC_ALL, ""); - while ((ch = getopt(argc, argv, "Scd:il:ms")) != -1) + while ((ch = getopt(argc, argv, "bScd:il:ms")) != -1) switch (ch) { + case 'b': + f_basename = 1; + break; case 'S': /* statistic lines */ f_statistic = 1; break; |