xref: /original-bsd/games/hangman/getword.c (revision 6cca134b)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)getword.c	5.2 (Berkeley) 06/18/88";
20 #endif /* not lint */
21 
22 # include	"hangman.h"
23 
24 # if pdp11
25 #	define	RN	(((off_t) rand() << 16) | (off_t) rand())
26 # else
27 #	define	RN	rand()
28 # endif
29 
30 /*
31  * getword:
32  *	Get a valid word out of the dictionary file
33  */
34 getword()
35 {
36 	register FILE		*inf;
37 	register char		*wp, *gp;
38 
39 	inf = Dict;
40 	for (;;) {
41 		fseek(inf, abs(RN % Dict_size), 0);
42 		if (fgets(Word, BUFSIZ, inf) == NULL)
43 			continue;
44 		if (fgets(Word, BUFSIZ, inf) == NULL)
45 			continue;
46 		Word[strlen(Word) - 1] = '\0';
47 		if (strlen(Word) < MINLEN)
48 			continue;
49 		for (wp = Word; *wp; wp++)
50 			if (!islower(*wp))
51 				goto cont;
52 		break;
53 cont:		;
54 	}
55 	gp = Known;
56 	wp = Word;
57 	while (*wp) {
58 		*gp++ = '-';
59 		wp++;
60 	}
61 	*gp = '\0';
62 }
63 
64 /*
65  * abs:
66  *	Return the absolute value of an integer
67  */
68 off_t
69 abs(i)
70 off_t	i;
71 {
72 	if (i < 0)
73 		return -(off_t) i;
74 	else
75 		return (off_t) i;
76 }
77