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