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