From d95245bbc4af79f67d7ab65fcd4fa4d6855dc849 Mon Sep 17 00:00:00 2001 From: dholland Date: Sat, 13 Oct 2012 20:12:18 +0000 Subject: Factor out some common code; pass -Wstrict-overflow. --- boggle/boggle/bog.c | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) (limited to 'boggle') diff --git a/boggle/boggle/bog.c b/boggle/boggle/bog.c index b56f568a..6e3c24de 100644 --- a/boggle/boggle/bog.c +++ b/boggle/boggle/bog.c @@ -1,4 +1,4 @@ -/* $NetBSD: bog.c,v 1.27 2011/08/26 06:18:17 dholland Exp $ */ +/* $NetBSD: bog.c,v 1.28 2012/10/13 20:12:18 dholland Exp $ */ /*- * Copyright (c) 1993 @@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1993\ #if 0 static char sccsid[] = "@(#)bog.c 8.2 (Berkeley) 5/4/95"; #else -__RCSID("$NetBSD: bog.c,v 1.27 2011/08/26 06:18:17 dholland Exp $"); +__RCSID("$NetBSD: bog.c,v 1.28 2012/10/13 20:12:18 dholland Exp $"); #endif #endif /* not lint */ @@ -64,6 +64,7 @@ static int validword(const char *); static void checkdict(void); static void newgame(const char *); static int compar(const void *, const void *); +static void clearwordpath(int full); static void usage(void) __dead; struct dictindex dictindex[26]; @@ -286,19 +287,13 @@ main(int argc, char *argv[]) static char * batchword(FILE *fp) { - int *p, *q; char *w; - q = &wordpath[MAXWORDLEN + 1]; - p = wordpath; - while (p < q) - *p++ = -1; + clearwordpath(1); while ((w = nextword(fp)) != NULL) { if (wordlen < minlength) continue; - p = wordpath; - while (p < q && *p != -1) - *p++ = -1; + clearwordpath(0); usedbits = 0; if (checkword(w, -1, wordpath) != -1) return (w); @@ -314,7 +309,7 @@ batchword(FILE *fp) static void playgame(void) { - int i, *p, *q; + int i; time_t t; char buf[MAXWORDLEN + 1]; @@ -326,10 +321,7 @@ playgame(void) time(&start_t); - q = &wordpath[MAXWORDLEN + 1]; - p = wordpath; - while (p < q) - *p++ = -1; + clearwordpath(1); showboard(board); startwords(); if (setjmp(env)) { @@ -362,9 +354,7 @@ playgame(void) continue; } - p = wordpath; - while (p < q && *p != -1) - *p++ = -1; + clearwordpath(0); usedbits = 0; if (checkword(buf, -1, wordpath) < 0) @@ -554,13 +544,12 @@ checkdict(void) char *p, *w; const char **pw; int i; - int prevch, previndex, *pi, *qi, st; + int prevch, previndex, st; mwordsp = mwords; nmwords = 0; pw = pword; prevch ='a'; - qi = &wordpath[MAXWORDLEN + 1]; (void) dictseek(dictfp, 0L, SEEK_SET); while ((w = nextword(dictfp)) != NULL) { @@ -598,9 +587,7 @@ checkdict(void) } } - pi = wordpath; - while (pi < qi && *pi != -1) - *pi++ = -1; + clearwordpath(0); usedbits = 0; if (checkword(w, -1, wordpath) == -1) continue; @@ -697,6 +684,20 @@ newgame(const char *b) } +/* + * Clear wordpath[]. + */ +static void +clearwordpath(int full) +{ + size_t pos; + const size_t max = MAXWORDLEN + 1; + + for (pos = 0; pos < max && (full || wordpath[pos] != -1); pos++) { + wordpath[pos] = -1; + } +} + static int compar(const void *p, const void *q) { -- cgit v1.2.3-56-ge451