Generally use NULL instead of explicitly casting 0 to some pointer type (part2).
[dragonfly.git] / games / hack / hack.rumors.c
CommitLineData
984263bc
MD
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 $ */
c7106d58 4/* $DragonFly: src/games/hack/hack.rumors.c,v 1.3 2006/08/21 19:45:32 pavalos Exp $ */
984263bc 5
984263bc
MD
6#include "hack.h" /* for RUMORFILE and BSD (index) */
7#define CHARSZ 8 /* number of bits in a char */
984263bc
MD
8int n_rumors = 0;
9int n_used_rumors = -1;
10char *usedbits;
11
c7106d58
PA
12static void init_rumors(FILE *);
13static bool skipline(FILE *);
14static void outline(FILE *);
15static bool used(int);
16
17static void
18init_rumors(FILE *rumf)
19{
984263bc
MD
20int 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
c7106d58
PA
29static bool
30skipline(FILE *rumf)
31{
984263bc
MD
32char line[COLNO];
33 while(1) {
34 if(!fgets(line, sizeof(line), rumf)) return(0);
35 if(index(line, '\n')) return(1);
36 }
37}
38
c7106d58
PA
39static void
40outline(FILE *rumf)
41{
984263bc
MD
42char line[COLNO];
43char *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
c7106d58
PA
50void
51outrumor(void)
52{
984263bc
MD
53int rn,i;
54FILE *rumf;
55 if(n_rumors <= n_used_rumors ||
902ec341 56 (rumf = fopen(RUMORFILE, "r")) == NULL) return;
984263bc
MD
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)) {
c7106d58 62 skipline(rumf);
984263bc
MD
63 if(!used(i)) rn--;
64 i++;
65 }
66 usedbits[i/CHARSZ] |= (1 << (i % CHARSZ));
67 n_used_rumors++;
68 outline(rumf);
69none:
c7106d58 70 fclose(rumf);
984263bc
MD
71}
72
c7106d58
PA
73static bool
74used(int i)
75{
984263bc
MD
76 return(usedbits[i/CHARSZ] & (1 << (i % CHARSZ)));
77}