* Garbage collect useless hand-rolled lookup tables.
* Merge one-line helper functions into callers.
* Garbage collect obfuscating macros.
* Fix one format string error (%d used for u_int).
* Garbage collect setlocale(3) and <locale.h>.
* Garbage collect several unused constants.
* Minus 45 LOC, no functional change.
As noticed by tedu@, the lookup table "table" was used uninitialized.
But since it was only used as a pre-matching optimization and the real
string comparison is done further down the line, that bug probably did
not cause wrong results but merely ruined -i performance.
Code review triggered by a naive question from Jan Stary.
OK deraadt@ tedu@
-/* $OpenBSD: fastfind.c,v 1.14 2017/12/08 17:26:42 millert Exp $ */
+/* $OpenBSD: fastfind.c,v 1.15 2019/01/14 09:06:04 schwarze 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.14 2017/12/08 17:26:42 millert Exp $
+ * $Id: fastfind.c,v 1.15 2019/01/14 09:06:04 schwarze Exp $
*/
#ifndef _LOCATE_STATISTIC_
u_char bigram1[NBG], bigram2[NBG], path[PATH_MAX];
#ifdef FF_ICASE
- /* use a lookup table for case insensitive search */
- u_char table[UCHAR_MAX + 1];
-
- tolower_word(pathpart);
+ for (p = pathpart; *p != '\0'; p++)
+ *p = tolower(*p);
#endif /* FF_ICASE*/
/* init bigram table */
p = pathpart;
patend = patprep(p);
cc = *patend;
-
#ifdef FF_ICASE
- /* set patend char to true */
- table[TOLOWER(*patend)] = 1;
- table[toupper(*patend)] = 1;
+ cc = tolower(cc);
#endif /* FF_ICASE */
/* go forward or backward */
if (c == SWITCH) { /* big step, an integer */
- if (len < INTSIZE)
+ if (len < sizeof(int))
break;
count += getwm(paddr) - OFFSET;
- len -= INTSIZE; paddr += INTSIZE;
+ len -= sizeof(int);
+ paddr += sizeof(int);
} else { /* slow step, =< 14 chars */
count += c - OFFSET;
}
break; /* SWITCH */
}
#ifdef FF_ICASE
- if (table[c])
+ if (tolower(c) == cc)
#else
if (c == cc)
#endif /* FF_ICASE */
*p++ = c;
} else {
/* bigrams are parity-marked */
- TO7BIT(c);
-
-#ifndef FF_ICASE
+ c &= ASCII_MAX;
+#ifdef FF_ICASE
+ if (tolower(bigram1[c]) == cc ||
+ tolower(bigram2[c]) == cc)
+#else
if (bigram1[c] == cc ||
bigram2[c] == cc)
-#else
-
- if (table[bigram1[c]] ||
- table[bigram2[c]])
#endif /* FF_ICASE */
- foundchar = p + 1;
+ foundchar = p + 1;
*p++ = bigram1[c];
*p++ = bigram2[c];
for (s = foundchar; s >= cutoff; s--) {
if (*s == cc
#ifdef FF_ICASE
- || TOLOWER(*s) == cc
+ || tolower(*s) == cc
#endif /* FF_ICASE */
) { /* fast first char check */
for (p = patend - 1, q = s - 1; *p != '\0';
p--, q--)
if (*q != *p
#ifdef FF_ICASE
- && TOLOWER(*q) != *p
+ && tolower(*q) != *p
#endif /* FF_ICASE */
)
break;
if (f_limit >= counter)
(void)puts(path);
else {
- (void)fprintf(stderr, "[show only %d lines]\n", counter - 1);
+ fprintf(stderr, "[show only %u lines]\n", counter - 1);
exit(0);
}
} else
/*
- * $OpenBSD: locate.c,v 1.31 2015/11/19 21:46:05 mmcc Exp $
+ * $OpenBSD: locate.c,v 1.32 2019/01/14 09:06:04 schwarze 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.31 2015/11/19 21:46:05 mmcc Exp $
+ * $Id: locate.c,v 1.32 2019/01/14 09:06:04 schwarze Exp $
*/
/*
#include <fnmatch.h>
#include <libgen.h>
#include <limits.h>
-#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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 ch;
char **dbv = NULL;
- (void) setlocale(LC_ALL, "");
if (pledge("stdio rpath", NULL) == -1)
err(1, "pledge");
dbv = colon(dbv, path_fcodes, _PATH_FCODES);
}
- if (f_icase && UCHAR_MAX < 4096) /* init tolower lookup table */
- for (ch = 0; ch < UCHAR_MAX + 1; ch++)
- myctype[ch] = tolower(ch);
-
/* foreach database ... */
while ((path_fcodes = *dbv) != NULL) {
dbv++;
}
if (f_silent)
- print_matches(counter);
+ printf("%u\n", counter);
exit(0);
}
-/* $OpenBSD: locate.h,v 1.10 2003/09/29 16:03:16 deraadt Exp $ */
+/* $OpenBSD: locate.h,v 1.11 2019/01/14 09:06:04 schwarze Exp $ */
/*
* Copyright (c) 1989, 1993
#define NBG 128 /* number of bigrams considered */
#define OFFSET 14 /* abs value of max likely diff */
#define PARITY 0200 /* parity bit */
-#define SWITCH 30 /* switch code */
-#define UMLAUT 31 /* an 8 bit char followed */
-
-/* 0-28 likeliest differential counts + offset to make nonnegative */
-#define LDC_MIN 0
-#define LDC_MAX 28
-
-#undef CHAR_MAX
-#define CHAR_MAX 127
-/* 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))
+#define SWITCH 30 /* switch code */
+#define UMLAUT 31 /* a byte < 32 or > 127 follows */
+#define ASCII_MIN 32 /* printable ASCII characters */
+#define ASCII_MAX 127
#define LOCATE_REG "*?[]\\" /* fnmatch(3) meta characters */
-
-/* $OpenBSD: util.c,v 1.14 2016/09/01 09:48:20 tedu Exp $
+/* $OpenBSD: util.c,v 1.15 2019/01/14 09:06:04 schwarze 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: util.c,v 1.14 2016/09/01 09:48:20 tedu Exp $
+ * $Id: util.c,v 1.15 2019/01/14 09:06:04 schwarze Exp $
*/
char **colon(char **, char*, char*);
char *patprep(char *);
-void print_matches(u_int);
-u_char *tolower_word(u_char *);
int getwm(caddr_t);
int getwf(FILE *);
int check_bigram_char(int);
return (dbv);
}
-void
-print_matches(counter)
- u_int counter;
-{
- (void)printf("%d\n", counter);
-}
-
/*
* extract last glob-free subpattern in name for fast pre-match; prepend
return(--subp);
}
-/* tolower word */
-u_char *
-tolower_word(word)
- u_char *word;
-{
- u_char *p;
-
- for (p = word; *p != '\0'; p++)
- *p = TOLOWER(*p);
-
- return(word);
-}
-
/*
* Read integer from mmap pointer.
caddr_t p;
{
union {
- char buf[INTSIZE];
+ char buf[sizeof(int)];
int i;
} u;
int i;
- for (i = 0; i < INTSIZE; i++)
+ for (i = 0; i < sizeof(int); i++)
u.buf[i] = *p++;
i = u.i;