xref: /dragonfly/games/hack/hack.rumors.c (revision 9ddb8543)
1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.rumors.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.rumors.c,v 1.3 1999/11/16 02:57:10 billf Exp $ */
4 /* $DragonFly: src/games/hack/hack.rumors.c,v 1.3 2006/08/21 19:45:32 pavalos Exp $ */
5 
6 #include	"hack.h"		/* for RUMORFILE and BSD (index) */
7 #define	CHARSZ	8			/* number of bits in a char */
8 int n_rumors = 0;
9 int n_used_rumors = -1;
10 char *usedbits;
11 
12 static void	init_rumors(FILE *);
13 static bool	skipline(FILE *);
14 static void	outline(FILE *);
15 static bool	used(int);
16 
17 static void
18 init_rumors(FILE *rumf)
19 {
20 int i;
21 	n_used_rumors = 0;
22 	while(skipline(rumf)) n_rumors++;
23 	rewind(rumf);
24 	i = n_rumors/CHARSZ;
25 	usedbits = (char *) alloc((unsigned)(i+1));
26 	for( ; i>=0; i--) usedbits[i] = 0;
27 }
28 
29 static bool
30 skipline(FILE *rumf)
31 {
32 char line[COLNO];
33 	while(1) {
34 		if(!fgets(line, sizeof(line), rumf)) return(0);
35 		if(index(line, '\n')) return(1);
36 	}
37 }
38 
39 static void
40 outline(FILE *rumf)
41 {
42 char line[COLNO];
43 char *ep;
44 	if(!fgets(line, sizeof(line), rumf)) return;
45 	if((ep = index(line, '\n')) != 0) *ep = 0;
46 	pline("This cookie has a scrap of paper inside! It reads: ");
47 	pline(line);
48 }
49 
50 void
51 outrumor(void)
52 {
53 int rn,i;
54 FILE *rumf;
55 	if(n_rumors <= n_used_rumors ||
56 	  (rumf = fopen(RUMORFILE, "r")) == NULL) return;
57 	if(n_used_rumors < 0) init_rumors(rumf);
58 	if(!n_rumors) goto none;
59 	rn = rn2(n_rumors - n_used_rumors);
60 	i = 0;
61 	while(rn || used(i)) {
62 		skipline(rumf);
63 		if(!used(i)) rn--;
64 		i++;
65 	}
66 	usedbits[i/CHARSZ] |= (1 << (i % CHARSZ));
67 	n_used_rumors++;
68 	outline(rumf);
69 none:
70 	fclose(rumf);
71 }
72 
73 static bool
74 used(int i)
75 {
76 	return(usedbits[i/CHARSZ] & (1 << (i % CHARSZ)));
77 }
78