i think it's idiotic for a program to offer users the choice between stdio and mmap modes. nobody knows which to pick, and it reveals some terrible incompetence somewhere else in the code.
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??? (this is one small step towards fixing locate.c to not *repeatedly* include fastfind.c) Index: fastfind.c =================================================================== RCS file: /cvs/src/usr.bin/locate/locate/fastfind.c,v retrieving revision 1.12 diff -u -p -r1.12 fastfind.c --- fastfind.c 16 Jan 2015 06:40:09 -0000 1.12 +++ fastfind.c 9 Oct 2015 19:59:05 -0000 @@ -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 */ Index: locate.1 =================================================================== RCS file: /cvs/src/usr.bin/locate/locate/locate.1,v retrieving revision 1.28 diff -u -p -r1.28 locate.1 --- locate.1 15 Jul 2010 20:51:38 -0000 1.28 +++ locate.1 9 Oct 2015 20:06:06 -0000 @@ -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 Index: locate.c =================================================================== RCS file: /cvs/src/usr.bin/locate/locate/locate.c,v retrieving revision 1.26 diff -u -p -r1.26 locate.c --- locate.c 16 Jan 2015 06:40:09 -0000 1.26 +++ locate.c 9 Oct 2015 20:06:20 -0000 @@ -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 *, c 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