-/* $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.
* 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_
void
-#ifdef FF_MMAP
#ifdef FF_ICASE
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;
#endif /* FF_ICASE*/
/* init bigram table */
-#ifdef FF_MMAP
if (len < (2*NBG)) {
(void)fprintf(stderr, "database too small: %s\n", database);
exit(1);
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++)
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;
}
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
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 */
-.\" $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
.\" 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
.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 ...
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
/*
- * $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
* 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 $
*/
/*
#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
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 */
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*);
{
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;
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;
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);
}
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)
{
(void)close(fd);
}
-#endif /* MMAP */
#ifdef DEBUG
unsigned long
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);
/* 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