diff options
author | Ted Unangst <tedu@cvs.openbsd.org> | 2015-10-23 07:57:04 +0000 |
---|---|---|
committer | Ted Unangst <tedu@cvs.openbsd.org> | 2015-10-23 07:57:04 +0000 |
commit | 2dc9fefc12fc552cc32e40fce859b607c884a4da (patch) | |
tree | 67885ef9120fbba02a7a0b6f7c872ee319c3ab34 /usr.bin/locate | |
parent | 2adeb44b748618b9afb57ee05ee8329597e0fdf2 (diff) |
mmap is the default. it's effectively the only tested codepath. so burn down
the stdio code path.
pros: everything is simpler. how can anyone read code this heavily spliced
with ifdef?
cons: you can't pipe a database into locate. who does this???
ok deraadt
Diffstat (limited to 'usr.bin/locate')
-rw-r--r-- | usr.bin/locate/locate/fastfind.c | 45 | ||||
-rw-r--r-- | usr.bin/locate/locate/locate.1 | 37 | ||||
-rw-r--r-- | usr.bin/locate/locate/locate.c | 94 |
3 files changed, 16 insertions, 160 deletions
diff --git a/usr.bin/locate/locate/fastfind.c b/usr.bin/locate/locate/fastfind.c index 53253029fa8..427f4a7a236 100644 --- a/usr.bin/locate/locate/fastfind.c +++ b/usr.bin/locate/locate/fastfind.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fastfind.c,v 1.12 2015/01/16 06:40:09 deraadt Exp $ */ +/* $OpenBSD: fastfind.c,v 1.13 2015/10/23 07:57:03 tedu 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.12 2015/01/16 06:40:09 deraadt Exp $ + * $Id: fastfind.c,v 1.13 2015/10/23 07:57:03 tedu Exp $ */ #ifndef _LOCATE_STATISTIC_ @@ -103,7 +103,6 @@ statistic (fp, path_fcodes) void -#ifdef FF_MMAP #ifdef FF_ICASE @@ -118,22 +117,6 @@ fastfind_mmap char *database; /* for error message */ -#else /* MMAP */ - - -#ifdef FF_ICASE -fastfind_icase -#else -fastfind -#endif /* FF_ICASE */ - -(fp, pathpart, database) - FILE *fp; /* open database */ - char *pathpart; /* search string */ - char *database; /* for error message */ - - -#endif /* MMAP */ { u_char *p, *s, *patend, *q, *foundchar; @@ -150,7 +133,6 @@ fastfind #endif /* FF_ICASE*/ /* init bigram table */ -#ifdef FF_MMAP if (len < (2*NBG)) { (void)fprintf(stderr, "database too small: %s\n", database); exit(1); @@ -160,12 +142,6 @@ fastfind p[c] = check_bigram_char(*paddr++); s[c] = check_bigram_char(*paddr++); } -#else - for (c = 0, p = bigram1, s = bigram2; c < NBG; c++) { - p[c] = check_bigram_char(getc(fp)); - s[c] = check_bigram_char(getc(fp)); - } -#endif /* FF_MMAP */ /* find optimal (last) char for searching */ for (p = pathpart; *p != '\0'; p++) @@ -192,22 +168,13 @@ fastfind found = count = 0; foundchar = 0; -#ifdef FF_MMAP c = (u_char)*paddr++; len--; for (; len > 0; ) { -#else - c = getc(fp); - for (; c != EOF; ) { -#endif /* FF_MMAP */ /* go forward or backward */ if (c == SWITCH) { /* big step, an integer */ -#ifdef FF_MMAP count += getwm(paddr) - OFFSET; len -= INTSIZE; paddr += INTSIZE; -#else - count += getwf(fp) - OFFSET; -#endif /* FF_MMAP */ } else { /* slow step, =< 14 chars */ count += c - OFFSET; } @@ -218,12 +185,8 @@ fastfind foundchar = p - 1; for (;;) { -#ifdef FF_MMAP c = (u_char)*paddr++; len--; -#else - c = getc(fp); -#endif /* FF_MMAP */ /* * == UMLAUT: 8 bit char followed * <= SWITCH: offset @@ -235,12 +198,8 @@ fastfind if (c < PARITY) { if (c <= UMLAUT) { if (c == UMLAUT) { -#ifdef FF_MMAP c = (u_char)*paddr++; len--; -#else - c = getc(fp); -#endif /* FF_MMAP */ } else break; /* SWITCH */ diff --git a/usr.bin/locate/locate/locate.1 b/usr.bin/locate/locate/locate.1 index 056f0afb9b9..60317f8f53d 100644 --- a/usr.bin/locate/locate/locate.1 +++ b/usr.bin/locate/locate/locate.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: locate.1,v 1.28 2010/07/15 20:51:38 schwarze Exp $ +.\" $OpenBSD: locate.1,v 1.29 2015/10/23 07:57:03 tedu 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.28 2010/07/15 20:51:38 schwarze Exp $ +.\" $Id: locate.1,v 1.29 2015/10/23 07:57:03 tedu Exp $ .\" -.Dd $Mdocdate: July 15 2010 $ +.Dd $Mdocdate: October 23 2015 $ .Dt LOCATE 1 .Os .Sh NAME @@ -39,7 +39,7 @@ .Nd find filenames quickly .Sh SYNOPSIS .Nm locate -.Op Fl bcimSs +.Op Fl bciS .Op Fl d Ar database .Op Fl l Ar limit .Ar pattern ... @@ -140,41 +140,12 @@ is the same as or .Pp .Dl $ locate -d db1:db2 -d db3 pattern -.Pp -If -.Ql \- -is given as the -.Ar database -name, standard input will be read instead. -For example, you can compress your database -and use: -.Pp -.Dl $ zcat database.gz | locate -d - pattern -.Pp -This might be useful on machines with a fast CPU, little RAM and slow I/O. -.Sy Note: -You can only use -.Em one -pattern for stdin. .It Fl i Ignore case distinctions in both the pattern and the database. .It Fl l Ar limit Limit output to a specific number of files and exit. -.It Fl m -Use -.Xr mmap 2 -instead of the -.Xr stdio 3 -library. -This is the default behavior. -It performs better in most cases. .It Fl S Print some statistics about the database and exit. -.It Fl s -Use the -.Xr stdio 3 -library instead of -.Xr mmap 2 . .El .Sh ENVIRONMENT .Bl -tag -width LOCATE_PATH -compact diff --git a/usr.bin/locate/locate/locate.c b/usr.bin/locate/locate/locate.c index 7acad40a365..fb686a4d382 100644 --- a/usr.bin/locate/locate/locate.c +++ b/usr.bin/locate/locate/locate.c @@ -1,5 +1,5 @@ /* - * $OpenBSD: locate.c,v 1.26 2015/01/16 06:40:09 deraadt Exp $ + * $OpenBSD: locate.c,v 1.27 2015/10/23 07:57:03 tedu 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.26 2015/01/16 06:40:09 deraadt Exp $ + * $Id: locate.c,v 1.27 2015/10/23 07:57:03 tedu Exp $ */ /* @@ -74,12 +74,10 @@ #include <unistd.h> #include <limits.h> -#ifdef MMAP # include <sys/types.h> # include <sys/stat.h> # include <sys/mman.h> # include <fcntl.h> -#endif #ifdef sun @@ -98,7 +96,6 @@ char *path_fcodes; /* locate database */ int f_mmap; /* use mmap */ int f_icase; /* ignore case */ -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 */ @@ -113,7 +110,7 @@ void fastfind_icase(FILE *, char *, char *); void fastfind_mmap(char *, caddr_t, int, char *); void fastfind_mmap_icase(char *, caddr_t, int, char *); void search_mmap(char *, char **); -void search_fopen(char *, char **); +void search_stastic(char *, char **); unsigned long cputime(void); extern char **colon(char **, char*, char*); @@ -133,12 +130,9 @@ main(int argc, char *argv[]) { int ch; char **dbv = NULL; -#ifdef MMAP - f_mmap = 1; /* mmap is default */ -#endif (void) setlocale(LC_ALL, ""); - while ((ch = getopt(argc, argv, "bScd:il:ms")) != -1) + while ((ch = getopt(argc, argv, "bScd:il:")) != -1) switch (ch) { case 'b': f_basename = 1; @@ -155,16 +149,6 @@ main(int argc, char *argv[]) case 'i': /* ignore case */ f_icase = 1; break; - case 'm': /* mmap */ -#ifdef MMAP - f_mmap = 1; -#else - (void)fprintf(stderr, "mmap(2) not implemented\n"); -#endif - break; - case 's': /* stdio lib */ - f_mmap = 0; - break; case 'c': /* suppress output, show only count of matches */ f_silent = 1; break; @@ -197,16 +181,8 @@ main(int argc, char *argv[]) while ((path_fcodes = *dbv) != NULL) { dbv++; - if (!strcmp(path_fcodes, "-")) - f_stdin = 1; - else - f_stdin = 0; - -#ifndef MMAP - f_mmap = 0; /* be paranoid */ -#endif - if (!f_mmap || f_stdin || f_statistic) - search_fopen(path_fcodes, argv); + if (f_statistic) + search_stastic(path_fcodes, argv); else search_mmap(path_fcodes, argv); } @@ -218,55 +194,21 @@ main(int argc, char *argv[]) void -search_fopen(char *db, char **s) +search_stastic(char *db, char **s) { FILE *fp; #ifdef DEBUG long t0; #endif - /* can only read stdin once */ - if (f_stdin) { - fp = stdin; - if (*(s+1) != NULL) { - (void)fprintf(stderr, - "read database from stdin, use only"); - (void)fprintf(stderr, " `%s' as pattern\n", *s); - *(s+1) = NULL; - } - } - else if ((fp = fopen(path_fcodes, "r")) == NULL) + if ((fp = fopen(path_fcodes, "r")) == NULL) err(1, "`%s'", path_fcodes); /* count only chars or lines */ - if (f_statistic) { - statistic(fp, path_fcodes); - (void)fclose(fp); - return; - } - - /* foreach search string ... */ - while (*s != NULL) { -#ifdef DEBUG - t0 = cputime(); -#endif - if (!f_stdin && - fseek(fp, (long)0, SEEK_SET) == -1) - err(1, "fseek to begin of ``%s''", path_fcodes); - - if (f_icase) - fastfind_icase(fp, *s, path_fcodes); - else - fastfind(fp, *s, path_fcodes); -#ifdef DEBUG - (void)fprintf(stderr, "fastfind %ld ms\n", cputime () - t0); -#endif - s++; - } + statistic(fp, path_fcodes); (void)fclose(fp); } -#ifdef MMAP void search_mmap(char *db, char **s) { @@ -308,7 +250,6 @@ search_mmap(char *db, char **s) (void)close(fd); } -#endif /* MMAP */ #ifdef DEBUG unsigned long @@ -324,7 +265,7 @@ cputime(void) void usage(void) { - (void)fprintf(stderr, "usage: locate [-bcimSs] [-d database] "); + (void)fprintf(stderr, "usage: locate [-bciS] [-d database] "); (void)fprintf(stderr, "[-l limit] pattern ...\n"); (void)fprintf(stderr, "default database: `%s' or $LOCATE_PATH\n", _PATH_FCODES); @@ -342,21 +283,6 @@ sane_count(int count) /* load fastfind functions */ -/* statistic */ -/* fastfind_mmap, fastfind_mmap_icase */ -#ifdef MMAP -#undef FF_MMAP -#undef FF_ICASE - -#define FF_MMAP -#include "fastfind.c" -#define FF_ICASE -#include "fastfind.c" -#endif /* MMAP */ - -/* fopen */ -/* fastfind, fastfind_icase */ -#undef FF_MMAP #undef FF_ICASE #include "fastfind.c" #define FF_ICASE |