xref: /original-bsd/games/hangman/getword.c (revision b4971bb3)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)getword.c	8.1 (Berkeley) 05/31/93";
10 #endif /* not lint */
11 
12 #include "hangman.h"
13 #include <stdlib.h>
14 
15 /*
16  * getword:
17  *	Get a valid word out of the dictionary file
18  */
19 getword()
20 {
21 	register FILE		*inf;
22 	register char		*wp, *gp;
23 	register long		 pos;
24 
25 	inf = Dict;
26 	for (;;) {
27 		pos = (double)rand() / (RAND_MAX + 1.0) * (double)Dict_size;
28 		fseek(inf, pos, 0);
29 		if (fgets(Word, BUFSIZ, inf) == NULL)
30 			continue;
31 		if (fgets(Word, BUFSIZ, inf) == NULL)
32 			continue;
33 		Word[strlen(Word) - 1] = '\0';
34 		if (strlen(Word) < MINLEN)
35 			continue;
36 		for (wp = Word; *wp; wp++)
37 			if (!islower(*wp))
38 				goto cont;
39 		break;
40 cont:		;
41 	}
42 	gp = Known;
43 	wp = Word;
44 	while (*wp) {
45 		*gp++ = '-';
46 		wp++;
47 	}
48 	*gp = '\0';
49 }
50