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