diff options
author | michaels <michaels@cvs.openbsd.org> | 1996-08-16 22:00:14 +0000 |
---|---|---|
committer | michaels <michaels@cvs.openbsd.org> | 1996-08-16 22:00:14 +0000 |
commit | ebafd495eb3cbfae9ca5cfa70e8df0e4e28944f2 (patch) | |
tree | 46b3f22b1cd128e2c0d785ddf03b160e173daec5 /usr.bin | |
parent | 60c325372de33908aff95d74393cc4a025d99c38 (diff) |
Better locate from wosch@freebsd (Wolfram Schneider), sligthly modified.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/locate/Makefile | 6 | ||||
-rw-r--r-- | usr.bin/locate/bigram/Makefile | 8 | ||||
-rw-r--r-- | usr.bin/locate/bigram/locate.bigram.c | 78 | ||||
-rw-r--r-- | usr.bin/locate/code/Makefile | 9 | ||||
-rw-r--r-- | usr.bin/locate/code/locate.code.c | 81 | ||||
-rw-r--r-- | usr.bin/locate/locate/Makefile | 17 | ||||
-rw-r--r-- | usr.bin/locate/locate/locate.1 | 25 | ||||
-rw-r--r-- | usr.bin/locate/locate/locate.c | 26 | ||||
-rw-r--r-- | usr.bin/locate/locate/locate.h | 29 | ||||
-rw-r--r-- | usr.bin/locate/locate/pathnames.h | 3 | ||||
-rw-r--r-- | usr.bin/locate/locate/updatedb.csh | 14 |
11 files changed, 199 insertions, 97 deletions
diff --git a/usr.bin/locate/Makefile b/usr.bin/locate/Makefile index 3ac42aadb5d..0de6327c99f 100644 --- a/usr.bin/locate/Makefile +++ b/usr.bin/locate/Makefile @@ -1,5 +1,7 @@ -# $OpenBSD: Makefile,v 1.2 1996/06/26 05:35:47 deraadt Exp $ -# $NetBSD: Makefile,v 1.3 1994/12/22 06:17:28 jtc Exp $ +# $OpenBSD: Makefile,v 1.3 1996/08/16 22:00:09 michaels Exp $ +# +# @(#)Makefile 8.1 (Berkeley) 6/6/93 +# $Id: Makefile,v 1.3 1996/08/16 22:00:09 michaels Exp $ SUBDIR= bigram code locate diff --git a/usr.bin/locate/bigram/Makefile b/usr.bin/locate/bigram/Makefile index 20ebbec614d..1d5c2980986 100644 --- a/usr.bin/locate/bigram/Makefile +++ b/usr.bin/locate/bigram/Makefile @@ -1,8 +1,10 @@ -# $OpenBSD: Makefile,v 1.2 1996/06/26 05:35:48 deraadt Exp $ -# $NetBSD: Makefile,v 1.3 1994/12/22 06:17:39 jtc Exp $ +# $OpenBSD: Makefile,v 1.3 1996/08/16 22:00:10 michaels Exp $ +# +# @(#)Makefile 8.1 (Berkeley) 6/6/93 PROG= locate.bigram NOMAN= noman -BINDIR= /usr/libexec +BINDIR= ${LIBEXECDIR} +CFLAGS+= -I${.CURDIR}/../locate .include <bsd.prog.mk> diff --git a/usr.bin/locate/bigram/locate.bigram.c b/usr.bin/locate/bigram/locate.bigram.c index d8c0f0e0204..d0c13d86e26 100644 --- a/usr.bin/locate/bigram/locate.bigram.c +++ b/usr.bin/locate/bigram/locate.bigram.c @@ -1,6 +1,4 @@ -/* $OpenBSD: locate.bigram.c,v 1.2 1996/06/26 05:35:49 deraadt Exp $ */ -/* $NetBSD: locate.bigram.c,v 1.5 1995/09/01 23:48:13 thorpej Exp $ */ - +/* $OpenBSD: locate.bigram.c,v 1.3 1996/08/16 22:00:10 michaels Exp $ */ /* * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. @@ -45,47 +43,77 @@ static char copyright[] = #ifndef lint #if 0 -static char sccsid[] = "@(#)locate.bigram.c 8.2 (Berkeley) 4/28/95"; +static char sccsid[] = "@(#)locate.bigram.c 8.1 (Berkeley) 6/6/93"; +#else +static char rcsid[] = "$OpenBSD: locate.bigram.c,v 1.3 1996/08/16 22:00:10 michaels Exp $"; #endif -static char rcsid[] = "$OpenBSD: locate.bigram.c,v 1.2 1996/06/26 05:35:49 deraadt Exp $"; #endif /* not lint */ /* * bigram < text > bigrams - * + * * List bigrams for 'updatedb' script. * Use 'code' to encode a file using this output. */ #include <stdio.h> #include <sys/param.h> /* for MAXPATHLEN */ +#include <string.h> /* memchr */ +#include "locate.h" -char buf1[MAXPATHLEN] = " "; -char buf2[MAXPATHLEN]; +u_char buf1[MAXPATHLEN] = " "; +u_char buf2[MAXPATHLEN]; +unsigned int bigram[UCHAR_MAX][UCHAR_MAX]; -main ( ) +int main(void) { - register char *cp; - register char *oldpath = buf1, *path = buf2; + register u_char *cp; + register u_char *oldpath = buf1, *path = buf2; + register int i, j; - while ( fgets ( path, sizeof(buf2), stdin ) != NULL ) { + while (fgets(path, sizeof(buf2), stdin) != NULL) { + /* skip empty lines */ + if (*path == '\n') + continue; + /* Squelch characters that would botch the decoding. */ + for (cp = path; *cp != NULL; cp++) { + /* chop newline */ + if (*cp == '\n') + *cp = NULL; + /* range */ + else if (*cp < ASCII_MIN || *cp > ASCII_MAX) + *cp = '?'; + } /* skip longest common prefix */ - for ( cp = path; *cp == *oldpath; cp++, oldpath++ ) - if ( *oldpath == '\0' ) - break; + for (cp = path; *cp == *oldpath && *cp != NULL; cp++, oldpath++) + ; /* * output post-residue bigrams only */ - while ( *cp != '\0' && *(cp + 1) != '\0' ) { - putchar ( *cp++ ); - putchar ( *cp++ ); - putchar ( '\n' ); + + /* check later for boundary */ + while ( *cp != NULL && *(cp+1) != NULL ) { + bigram[*cp][*(cp+1)]++; + cp += 2; } - if ( path == buf1 ) /* swap pointers */ - path = buf2, oldpath = buf1; - else - path = buf1, oldpath = buf2; - } - return (0); + + if ( path == buf1 ) { /* swap pointers */ + path = buf2; + oldpath = buf1; + } + else { + path = buf1; + oldpath = buf2; + } + } + + /* output, boundary check */ + for (i = ASCII_MIN; i <= ASCII_MAX; i++) + for (j = ASCII_MIN; j <= ASCII_MAX; j++) + if (bigram[i][j] != 0) + fprintf(stdout, "%4d %c%c\n", + bigram[i][j], i, j); + + return 0; } diff --git a/usr.bin/locate/code/Makefile b/usr.bin/locate/code/Makefile index eec7f4be81c..97b193456b7 100644 --- a/usr.bin/locate/code/Makefile +++ b/usr.bin/locate/code/Makefile @@ -1,9 +1,10 @@ -# $OpenBSD: Makefile,v 1.2 1996/06/26 05:35:50 deraadt Exp $ -# $NetBSD: Makefile,v 1.3 1994/12/22 06:17:41 jtc Exp $ +# $OpenBSD: Makefile,v 1.3 1996/08/16 22:00:11 michaels Exp $ +# +# @(#)Makefile 8.1 (Berkeley) 6/6/93 PROG= locate.code -CFLAGS+=-I${.CURDIR}/../locate +CFLAGS+= -I${.CURDIR}/../locate NOMAN= noman -BINDIR= /usr/libexec +BINDIR= ${LIBEXECDIR} .include <bsd.prog.mk> diff --git a/usr.bin/locate/code/locate.code.c b/usr.bin/locate/code/locate.code.c index 8bdcce9a7e2..af435e68a22 100644 --- a/usr.bin/locate/code/locate.code.c +++ b/usr.bin/locate/code/locate.code.c @@ -1,5 +1,4 @@ -/* $OpenBSD: locate.code.c,v 1.2 1996/06/26 05:35:50 deraadt Exp $ */ -/* $NetBSD: locate.code.c,v 1.5 1995/08/31 22:36:33 jtc Exp $ */ +/* $OpenBSD: locate.code.c,v 1.3 1996/08/16 22:00:11 michaels Exp $ */ /* * Copyright (c) 1989, 1993 @@ -45,9 +44,10 @@ static char copyright[] = #ifndef lint #if 0 -static char sccsid[] = "@(#)locate.code.c 8.4 (Berkeley) 5/4/95"; +static char sccsid[] = "@(#)locate.code.c 8.1 (Berkeley) 6/6/93"; +#else +static char rcsid[] = "$OpenBSD: locate.code.c,v 1.3 1996/08/16 22:00:11 michaels Exp $"; #endif -static char rcsid[] = "$OpenBSD: locate.code.c,v 1.2 1996/06/26 05:35:50 deraadt Exp $"; #endif /* not lint */ /* @@ -86,37 +86,47 @@ static char rcsid[] = "$OpenBSD: locate.code.c,v 1.2 1996/06/26 05:35:50 deraadt */ #include <sys/param.h> - #include <err.h> #include <errno.h> -#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> - +#include <stdio.h> #include "locate.h" #define BGBUFSIZE (NBG * 2) /* size of bigram buffer */ -char buf1[MAXPATHLEN] = " "; -char buf2[MAXPATHLEN]; +u_char buf1[MAXPATHLEN] = " "; +u_char buf2[MAXPATHLEN]; char bigrams[BGBUFSIZE + 1] = { 0 }; +#define LOOKUP 1 +#ifdef LOOKUP +#define BGINDEX(x) (big[(u_int)*x][(u_int)*(x+1)]) +typedef u_char bg_t; +bg_t big[UCHAR_MAX][UCHAR_MAX]; + +#else +#define BGINDEX(x) bgindex(x) +typedef int bg_t; +#endif + int bgindex __P((char *)); void usage __P((void)); +extern int optind; +extern int optopt; int main(argc, argv) int argc; char *argv[]; { - register char *cp, *oldpath, *path; + register u_char *cp, *oldpath, *path; int ch, code, count, diffcount, oldcount; FILE *fp; + register int i, j; while ((ch = getopt(argc, argv, "")) != EOF) switch(ch) { - case '?': default: usage(); } @@ -135,26 +145,38 @@ main(argc, argv) err(1, "stdout"); (void)fclose(fp); +#ifdef LOOKUP + /* init lookup table */ + for (i = 0; i < UCHAR_MAX; i++) + for (j = 0; j < UCHAR_MAX; j++) + big[i][j] = (bg_t)-1; + + for (cp = bigrams, i = 0; *cp != NULL; i += 2, cp += 2) + big[(int)*cp][(int)*(cp + 1)] = (bg_t)i; +#endif + oldpath = buf1; path = buf2; oldcount = 0; while (fgets(path, sizeof(buf2), stdin) != NULL) { - /* Truncate newline. */ - cp = path + strlen(path) - 1; - if (cp > path && *cp == '\n') - *cp = '\0'; + + /* skip empty lines */ + if (*path == '\n') + continue; /* Squelch characters that would botch the decoding. */ - for (cp = path; *cp != '\0'; cp++) { - *cp &= PARITY-1; - if (*cp <= SWITCH) + for (cp = path; *cp != NULL; cp++) { + /* chop newline */ + if (*cp == '\n') + *cp = NULL; + /* range */ + else if (*cp < ASCII_MIN || *cp > ASCII_MAX) *cp = '?'; } /* Skip longest common prefix. */ - for (cp = path; *cp == *oldpath; cp++, oldpath++) - if (*oldpath == '\0') - break; + for (cp = path; *cp == *oldpath && *cp; cp++, oldpath++); + count = cp - path; diffcount = count - oldcount + OFFSET; oldcount = count; @@ -166,13 +188,13 @@ main(argc, argv) if (putchar(diffcount) == EOF) err(1, "stdout"); - while (*cp != '\0') { - if (*(cp + 1) == '\0') { + while (*cp != NULL) { + if (*(cp + 1) == NULL) { if (putchar(*cp) == EOF) err(1, "stdout"); break; } - if ((code = bgindex(cp)) < 0) { + if ((code = BGINDEX(cp)) == (bg_t)-1) { if (putchar(*cp++) == EOF || putchar(*cp++) == EOF) err(1, "stdout"); @@ -194,9 +216,11 @@ main(argc, argv) /* Non-zero status if there were errors */ if (fflush(stdout) != 0 || ferror(stdout)) exit(1); - exit(0); + + return 0; } +#ifndef LOOKUP int bgindex(bg) /* Return location of bg in bigrams or -1. */ char *bg; @@ -205,11 +229,12 @@ bgindex(bg) /* Return location of bg in bigrams or -1. */ bg0 = bg[0]; bg1 = bg[1]; - for (p = bigrams; *p != '\0'; p++) + for (p = bigrams; *p != NULL; p++) if (*p++ == bg0 && *p == bg1) break; - return (*p == '\0' ? -1 : --p - bigrams); + return (*p == NULL ? -1 : (--p - bigrams)); } +#endif /* !LOOKUP */ void usage() diff --git a/usr.bin/locate/locate/Makefile b/usr.bin/locate/locate/Makefile index efac911058d..1c810bf413f 100644 --- a/usr.bin/locate/locate/Makefile +++ b/usr.bin/locate/locate/Makefile @@ -1,11 +1,22 @@ -# $OpenBSD: Makefile,v 1.2 1996/06/26 05:35:51 deraadt Exp $ -# $NetBSD: Makefile,v 1.3 1994/12/22 06:17:44 jtc Exp $ +# $OpenBSD: Makefile,v 1.3 1996/08/16 22:00:12 michaels Exp $ +# +# @(#)Makefile 8.1 (Berkeley) 6/6/93 +# $Id: Makefile,v 1.3 1996/08/16 22:00:12 michaels Exp $ PROG= locate +MAN= locate.1 locate.updatedb.8 +SCRIPTS= updatedb mklocatedb concatdb +MLINKS+= locate.updatedb.8 updatedb.8 beforeinstall: +.for script in ${SCRIPTS} install -c -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \ - ${.CURDIR}/updatedb.csh ${DESTDIR}/usr/libexec/locate.updatedb + ${.CURDIR}/${script}.sh ${DESTDIR}${LIBEXECDIR}/locate.${script} +.endfor + +# only /usr/src/etc/Makefile install files in /etc +# ${INSTALL} -c -o root -g wheel -m 644 \ +# ${.CURDIR}/locate.rc ${DESTDIR}/etc .include "../../Makefile.inc" .include <bsd.prog.mk> diff --git a/usr.bin/locate/locate/locate.1 b/usr.bin/locate/locate/locate.1 index 4c6001e14b3..f2ddad83722 100644 --- a/usr.bin/locate/locate/locate.1 +++ b/usr.bin/locate/locate/locate.1 @@ -1,6 +1,3 @@ -.\" $OpenBSD: locate.1,v 1.2 1996/06/26 05:35:52 deraadt Exp $ -.\" $NetBSD: locate.1,v 1.4 1995/07/25 18:20:25 jtc Exp $ -.\" .\" Copyright (c) 1990, 1993 .\" The Regents of the University of California. All rights reserved. .\" @@ -41,8 +38,8 @@ .Nm locate .Nd find files .Sh SYNOPSIS -.Nm locate -.Ar pattern +.Ar locate +pattern .Sh DESCRIPTION .Nm Locate searches a database for all pathnames which match the specified @@ -63,12 +60,16 @@ including slashes (``/''). As a special case, a pattern containing no globbing characters (``foo'') is matched as though it were ``*foo*''. .Sh FILES -.Bl -tag -width /var/db/locate.database -compact +.Bl -tag -width /usr/libexec/locate.updatedb -compact .It Pa /var/db/locate.database +The actual database +.It Pa /usr/libexec/locate.updatedb +Script to update the locate database .El .Sh SEE ALSO .Xr find 1 , -.Xr fnmatch 3 +.Xr fnmatch 3 , +.Xr locate.updatedb 8 .Rs .%A Woods, James A. .%D 1983 @@ -77,6 +78,16 @@ is matched as though it were ``*foo*''. .%V 8:1 .%P pp. 8-10 .Re +.Sh BUGS +.Nm Locate +may fail to list some files that are present, or may +to list files that have been removed from the system. This is because +locate only reports files that are present in the database, which is +typically only regenerated once a week by the +.Nm /etc/weekly +script. Use +.Xr find 1 +to locate files that are of a more transitory nature. .Sh HISTORY The .Nm locate diff --git a/usr.bin/locate/locate/locate.c b/usr.bin/locate/locate/locate.c index b51abf5738c..3dbc520aca4 100644 --- a/usr.bin/locate/locate/locate.c +++ b/usr.bin/locate/locate/locate.c @@ -1,5 +1,4 @@ -/* $OpenBSD: locate.c,v 1.2 1996/06/26 05:35:52 deraadt Exp $ */ -/* $NetBSD: locate.c,v 1.6 1994/12/22 06:17:47 jtc Exp $ */ +/* $OpenBSD: locate.c,v 1.3 1996/08/16 22:00:12 michaels Exp $ */ /* * Copyright (c) 1989, 1993 @@ -46,8 +45,9 @@ static char copyright[] = #ifndef lint #if 0 static char sccsid[] = "@(#)locate.c 8.1 (Berkeley) 6/6/93"; +#else +static char rcsid[] = "$OpenBSD: locate.c,v 1.3 1996/08/16 22:00:12 michaels Exp $"; #endif -static char rcsid[] = "$OpenBSD: locate.c,v 1.2 1996/06/26 05:35:52 deraadt Exp $"; #endif /* not lint */ /* @@ -59,19 +59,19 @@ static char rcsid[] = "$OpenBSD: locate.c,v 1.2 1996/06/26 05:35:52 deraadt Exp * bigram coding by a further 20-25%. * * The codes are: - * + * * 0-28 likeliest differential counts + offset to make nonnegative * 30 switch code for out-of-range count to follow in next word * 128-255 bigram codes (128 most common, as determined by 'updatedb') * 32-127 single character (printable) ascii residue (ie, literal) - * + * * A novel two-tiered string search technique is employed: - * + * * First, a metacharacter-free subpattern and partial pathname is matched * BACKWARDS to avoid full expansion of the pathname list. The time savings * is 40-50% over forward matching, which cannot efficiently handle * overlapped search patterns and compressed path residue. - * + * * Then, the actual shell glob-style regular expression (if in this form) is * matched against the candidate pathnames using the slower routines provided * in the standard 'find'. @@ -98,14 +98,15 @@ main(argc, argv) (void)fprintf(stderr, "usage: locate pattern\n"); exit(1); } - if (!(fp = fopen(_PATH_FCODES, "r"))) { + if ((fp = fopen(_PATH_FCODES, "r")) == NULL) { (void)fprintf(stderr, "locate: no database file %s.\n", _PATH_FCODES); exit(1); } - while (*++argv) + while (*(++argv) != NULL) fastfind(*argv); - exit(0); + + return 0; } fastfind(pathpart) @@ -125,7 +126,7 @@ fastfind(pathpart) patend = patprep(p); found = 0; - for (c = getc(fp), count = 0; c != EOF;) { + for (c = getc(fp), count = 0; c != EOF; ) { count += ((c == SWITCH) ? getw(fp) : c) - OFFSET; /* overlay old path */ for (p = path + count; (c = getc(fp)) > SWITCH;) @@ -145,8 +146,7 @@ fastfind(pathpart) break; if (*p == NULL) { /* fast match success */ found = 1; - if (!globflag || - !fnmatch(pathpart, path, 0)) + if (!globflag || !fnmatch(pathpart, path, 0)) (void)printf("%s\n", path); break; } diff --git a/usr.bin/locate/locate/locate.h b/usr.bin/locate/locate/locate.h index 05febeadccc..04437e13a1b 100644 --- a/usr.bin/locate/locate/locate.h +++ b/usr.bin/locate/locate/locate.h @@ -1,5 +1,4 @@ -/* $OpenBSD: locate.h,v 1.2 1996/06/26 05:35:53 deraadt Exp $ */ -/* $NetBSD: locate.h,v 1.3 1994/12/22 06:17:48 jtc Exp $ */ +/* $OpenBSD: locate.h,v 1.3 1996/08/16 22:00:13 michaels Exp $ */ /* * Copyright (c) 1989, 1993 @@ -42,3 +41,29 @@ #define OFFSET 14 /* abs value of max likely diff */ #define PARITY 0200 /* parity bit */ #define SWITCH 30 /* switch code */ + +/* 0-28 likeliest differential counts + offset to make nonnegative */ +#define LDC_MIN 0 +#define LDC_MAX 28 + +/* 128-255 bigram codes (128 most common, as determined by 'updatedb') */ +#define BIGRAM_MIN (UCHAR_MAX - CHAR_MAX) +#define BIGRAM_MAX UCHAR_MAX + +/* 32-127 single character (printable) ascii residue (ie, literal) */ +#define ASCII_MIN 32 +#define ASCII_MAX CHAR_MAX + +/* #define TO7BIT(x) (x = ( ((u_char)x) & CHAR_MAX )) */ +#define TO7BIT(x) (x = x & CHAR_MAX ) + + +#if UCHAR_MAX >= 4096 + define TOLOWER(ch) tolower(ch) +#else + +u_char myctype[UCHAR_MAX + 1]; +#define TOLOWER(ch) (myctype[ch]) +#endif + +#define INTSIZE (sizeof(int)) diff --git a/usr.bin/locate/locate/pathnames.h b/usr.bin/locate/locate/pathnames.h index ce83deea2f4..5ede34ba9b4 100644 --- a/usr.bin/locate/locate/pathnames.h +++ b/usr.bin/locate/locate/pathnames.h @@ -1,5 +1,4 @@ -/* $OpenBSD: pathnames.h,v 1.2 1996/06/26 05:35:54 deraadt Exp $ */ -/* $NetBSD: pathnames.h,v 1.3 1994/12/22 06:17:49 jtc Exp $ */ +/* $OpenBSD: pathnames.h,v 1.3 1996/08/16 22:00:13 michaels Exp $ */ /* * Copyright (c) 1989, 1993 diff --git a/usr.bin/locate/locate/updatedb.csh b/usr.bin/locate/locate/updatedb.csh index a8a71d22310..74f65fa11d0 100644 --- a/usr.bin/locate/locate/updatedb.csh +++ b/usr.bin/locate/locate/updatedb.csh @@ -1,7 +1,6 @@ #!/bin/csh -f # -# $OpenBSD: updatedb.csh,v 1.2 1996/06/26 05:35:54 deraadt Exp $ -# $NetBSD: updatedb.csh,v 1.7 1995/08/31 22:36:35 jtc Exp $ +# $OpenBSD: updatedb.csh,v 1.3 1996/08/16 22:00:13 michaels Exp $ # # Copyright (c) 1989, 1993 # The Regents of the University of California. All rights reserved. @@ -37,7 +36,7 @@ # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # -# @(#)updatedb.csh 8.4 (Berkeley) 10/27/94 +# @(#)updatedb.csh 8.3 (Berkeley) 3/19/94 # set SRCHPATHS = "/" # directories to be put in the database @@ -60,14 +59,13 @@ set errs = $TMPDIR/locate.errs.$$ # search locally or everything # find ${SRCHPATHS} -print | \ -find ${SRCHPATHS} \( ! -fstype local -o -fstype fdesc -o -fstype kernfs \) -a \ - -prune -o -print | \ +find ${SRCHPATHS} \! -fstype ufs -prune -or -print | \ tr '/' '\001' | \ - (sort -T "$TMPDIR" -f; echo $status > $errs) | tr '\001' '/' > $filelist + (sort -T $TMPDIR -f; echo $status > $errs) | tr '\001' '/' > $filelist $LIBDIR/locate.bigram < $filelist | \ - (sort -T "$TMPDIR"; echo $status >> $errs) | \ - uniq -c | sort -T "$TMPDIR" -nr | \ + (sort -T /$TMPDIR; echo $status >> $errs) | \ + uniq -c | sort -T /$TMPDIR -nr | \ awk '{ if (NR <= 128) print $2 }' | tr -d '\012' > $bigrams # code the file list |