#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1995-1996 Wolfram Schneider, Berlin.\n\
@(#) Copyright (c) 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif
#ifndef lint
#if 0
static char sccsid[] = "@(#)locate.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] =
"$FreeBSD: src/usr.bin/locate/locate/locate.c,v 1.17 2006/06/11 17:40:25 maxim Exp $";
#endif
#include <sys/param.h>
#include <ctype.h>
#include <err.h>
#include <fnmatch.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef MMAP
# include <sys/types.h>
# include <sys/stat.h>
# include <sys/mman.h>
# include <fcntl.h>
#endif
#include "locate.h"
#include "pathnames.h"
#ifdef DEBUG
# include <sys/time.h>
# include <sys/types.h>
# include <sys/resource.h>
#endif
int f_mmap;
int f_icase;
int f_stdin;
int f_statistic;
int f_silent;
int f_limit;
u_int counter;
char separator='\n';
#ifdef __APPLE__
u_char myctype[UCHAR_MAX + 1];
#endif
void usage(void);
void statistic(FILE *, char *);
void fastfind(FILE *, char *, char *);
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 **);
unsigned long cputime(void);
extern char **colon(char **, char*, char*);
extern void print_matches(u_int);
extern int getwm(caddr_t);
extern int getwf(FILE *);
extern u_char *tolower_word(u_char *);
extern int check_bigram_char(int);
extern char *patprep(char *);
int
main(argc, argv)
int argc;
char **argv;
{
register int ch;
char **dbv = NULL;
char *path_fcodes;
#ifdef MMAP
f_mmap = 1;
#endif
(void) setlocale(LC_ALL, "");
while ((ch = getopt(argc, argv, "0Scd:il:ms")) != -1)
switch(ch) {
case '0':
separator = '\0';
break;
case 'S':
f_statistic = 1;
break;
case 'l':
f_limit = atoi(optarg);
break;
case 'd':
dbv = colon(dbv, optarg, _PATH_FCODES);
break;
case 'i':
f_icase = 1;
break;
case 'm':
#ifdef MMAP
f_mmap = 1;
#else
warnx("mmap(2) not implemented");
#endif
break;
case 's':
f_mmap = 0;
break;
case 'c':
f_silent = 1;
break;
default:
usage();
}
argv += optind;
argc -= optind;
if (argc < 1 && !(f_statistic))
usage();
if (dbv == NULL || *dbv == NULL) {
if ((path_fcodes = getenv("LOCATE_PATH")) == NULL ||
*path_fcodes == '\0')
dbv = colon(dbv, _PATH_FCODES, _PATH_FCODES);
else
dbv = colon(dbv, path_fcodes, _PATH_FCODES);
}
if (f_icase && UCHAR_MAX < 4096)
for (ch = 0; ch < UCHAR_MAX + 1; ch++)
myctype[ch] = tolower(ch);
while((path_fcodes = *dbv) != NULL) {
dbv++;
if (!strcmp(path_fcodes, "-"))
f_stdin = 1;
else
f_stdin = 0;
#ifndef MMAP
f_mmap = 0;
#endif
if (!f_mmap || f_stdin || f_statistic)
search_fopen(path_fcodes, argv);
else
search_mmap(path_fcodes, argv);
}
if (f_silent)
print_matches(counter);
exit(0);
}
void
search_fopen(db, s)
char *db;
char **s;
{
FILE *fp;
#ifdef DEBUG
long t0;
#endif
if (f_stdin) {
fp = stdin;
if (*(s+1) != NULL) {
warnx("read database from stdin, use only `%s' as pattern", *s);
*(s+1) = NULL;
}
}
else if ((fp = fopen(db, "r")) == NULL)
err(1, "`%s'", db);
if (f_statistic) {
statistic(fp, db);
(void)fclose(fp);
return;
}
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''\n", db);
if (f_icase)
fastfind_icase(fp, *s, db);
else
fastfind(fp, *s, db);
#ifdef DEBUG
warnx("fastfind %ld ms", cputime () - t0);
#endif
s++;
}
(void)fclose(fp);
}
#ifdef MMAP
void
search_mmap(db, s)
char *db;
char **s;
{
struct stat sb;
int fd;
caddr_t p;
off_t len;
#ifdef DEBUG
long t0;
#endif
if ((fd = open(db, O_RDONLY)) == -1 ||
fstat(fd, &sb) == -1)
err(1, "`%s'", db);
len = sb.st_size;
if ((p = mmap((caddr_t)0, (size_t)len,
PROT_READ, MAP_SHARED,
fd, (off_t)0)) == MAP_FAILED)
err(1, "mmap ``%s''", db);
while (*s != NULL) {
#ifdef DEBUG
t0 = cputime();
#endif
if (f_icase)
fastfind_mmap_icase(*s, p, (int)len, db);
else
fastfind_mmap(*s, p, (int)len, db);
#ifdef DEBUG
warnx("fastfind %ld ms", cputime () - t0);
#endif
s++;
}
if (munmap(p, (size_t)len) == -1)
warn("munmap %s\n", db);
(void)close(fd);
}
#endif
#ifdef DEBUG
unsigned long
cputime ()
{
struct rusage rus;
getrusage(0, &rus);
return(rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000);
}
#endif
void
usage ()
{
(void)fprintf(stderr,
"usage: locate [-0Scims] [-l limit] [-d database] pattern ...\n\n");
(void)fprintf(stderr,
"default database: `%s' or $LOCATE_PATH\n", _PATH_FCODES);
exit(1);
}
#ifdef MMAP
#undef FF_MMAP
#undef FF_ICASE
#define FF_MMAP
#include "fastfind.c"
#define FF_ICASE
#include "fastfind.c"
#endif
#undef FF_MMAP
#undef FF_ICASE
#include "fastfind.c"
#define FF_ICASE
#include "fastfind.c"