hack.rumors.c revision 1.4 1 1.4 christos /* $NetBSD: hack.rumors.c,v 1.4 1997/10/19 16:58:55 christos Exp $ */
2 1.4 christos
3 1.2 mycroft /*
4 1.2 mycroft * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
5 1.2 mycroft */
6 1.2 mycroft
7 1.4 christos #include <sys/cdefs.h>
8 1.2 mycroft #ifndef lint
9 1.4 christos __RCSID("$NetBSD: hack.rumors.c,v 1.4 1997/10/19 16:58:55 christos Exp $");
10 1.4 christos #endif /* not lint */
11 1.1 cgd
12 1.4 christos #include "hack.h" /* for RUMORFILE and BSD (strchr) */
13 1.4 christos #include "extern.h"
14 1.4 christos #define CHARSZ 8 /* number of bits in a char */
15 1.4 christos int n_rumors = 0;
16 1.4 christos int n_used_rumors = -1;
17 1.4 christos char *usedbits;
18 1.4 christos
19 1.4 christos void
20 1.4 christos init_rumors(rumf)
21 1.4 christos FILE *rumf;
22 1.4 christos {
23 1.4 christos int i;
24 1.1 cgd n_used_rumors = 0;
25 1.4 christos while (skipline(rumf))
26 1.4 christos n_rumors++;
27 1.1 cgd rewind(rumf);
28 1.4 christos i = n_rumors / CHARSZ;
29 1.4 christos usedbits = (char *) alloc((unsigned) (i + 1));
30 1.4 christos for (; i >= 0; i--)
31 1.4 christos usedbits[i] = 0;
32 1.1 cgd }
33 1.1 cgd
34 1.4 christos int
35 1.4 christos skipline(rumf)
36 1.4 christos FILE *rumf;
37 1.4 christos {
38 1.4 christos char line[COLNO];
39 1.4 christos while (1) {
40 1.4 christos if (!fgets(line, sizeof(line), rumf))
41 1.4 christos return (0);
42 1.4 christos if (strchr(line, '\n'))
43 1.4 christos return (1);
44 1.1 cgd }
45 1.1 cgd }
46 1.1 cgd
47 1.4 christos void
48 1.4 christos outline(rumf)
49 1.4 christos FILE *rumf;
50 1.4 christos {
51 1.4 christos char line[COLNO];
52 1.4 christos char *ep;
53 1.4 christos if (!fgets(line, sizeof(line), rumf))
54 1.4 christos return;
55 1.4 christos if ((ep = strchr(line, '\n')) != 0)
56 1.4 christos *ep = 0;
57 1.1 cgd pline("This cookie has a scrap of paper inside! It reads: ");
58 1.1 cgd pline(line);
59 1.1 cgd }
60 1.1 cgd
61 1.4 christos void
62 1.4 christos outrumor()
63 1.4 christos {
64 1.4 christos int rn, i;
65 1.4 christos FILE *rumf;
66 1.4 christos if (n_rumors <= n_used_rumors ||
67 1.4 christos (rumf = fopen(RUMORFILE, "r")) == (FILE *) 0)
68 1.4 christos return;
69 1.4 christos if (n_used_rumors < 0)
70 1.4 christos init_rumors(rumf);
71 1.4 christos if (!n_rumors)
72 1.4 christos goto none;
73 1.1 cgd rn = rn2(n_rumors - n_used_rumors);
74 1.1 cgd i = 0;
75 1.4 christos while (rn || used(i)) {
76 1.1 cgd (void) skipline(rumf);
77 1.4 christos if (!used(i))
78 1.4 christos rn--;
79 1.1 cgd i++;
80 1.1 cgd }
81 1.4 christos usedbits[i / CHARSZ] |= (1 << (i % CHARSZ));
82 1.1 cgd n_used_rumors++;
83 1.1 cgd outline(rumf);
84 1.1 cgd none:
85 1.1 cgd (void) fclose(rumf);
86 1.1 cgd }
87 1.1 cgd
88 1.4 christos int
89 1.4 christos used(i)
90 1.4 christos int i;
91 1.4 christos {
92 1.4 christos return (usedbits[i / CHARSZ] & (1 << (i % CHARSZ)));
93 1.1 cgd }
94