kernel - TMPFS - Bug fixing pass - directory hierarchy
[dragonfly.git] / games / hack / hack.rumors.c
blob7e308fa3cf7512975ae4d587c43ac7ab076d658b
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 $ */
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;
12 static void init_rumors(FILE *);
13 static bool skipline(FILE *);
14 static void outline(FILE *);
15 static bool used(int);
17 static void
18 init_rumors(FILE *rumf)
20 int i;
22 n_used_rumors = 0;
23 while (skipline(rumf))
24 n_rumors++;
25 rewind(rumf);
26 i = n_rumors / CHARSZ;
27 usedbits = alloc((unsigned)(i + 1));
28 for (; i >= 0; i--)
29 usedbits[i] = 0;
32 static bool
33 skipline(FILE *rumf)
35 char line[COLNO];
37 for (;;) {
38 if (!fgets(line, sizeof(line), rumf))
39 return (0);
40 if (strchr(line, '\n'))
41 return (1);
45 static void
46 outline(FILE *rumf)
48 char line[COLNO];
49 char *ep;
51 if (!fgets(line, sizeof(line), rumf))
52 return;
53 if ((ep = strchr(line, '\n')) != 0)
54 *ep = 0;
55 pline("This cookie has a scrap of paper inside! It reads: ");
56 pline(line);
59 void
60 outrumor(void)
62 int rn, i;
63 FILE *rumf;
65 if (n_rumors <= n_used_rumors ||
66 (rumf = fopen(RUMORFILE, "r")) == NULL)
67 return;
68 if (n_used_rumors < 0)
69 init_rumors(rumf);
70 if (!n_rumors)
71 goto none;
72 rn = rn2(n_rumors - n_used_rumors);
73 i = 0;
74 while (rn || used(i)) {
75 skipline(rumf);
76 if (!used(i))
77 rn--;
78 i++;
80 usedbits[i / CHARSZ] |= (1 << (i % CHARSZ));
81 n_used_rumors++;
82 outline(rumf);
83 none:
84 fclose(rumf);
87 static bool
88 used(int i)
90 return (usedbits[i / CHARSZ] & (1 << (i % CHARSZ)));