aboutsummaryrefslogtreecommitdiff
path: root/hack/hack.rumors.c
blob: 5453732b1aa792378f2c5e7ec9fbd78da95de00e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* hack.rumors.c - version 1.0.3 */
/* $FreeBSD: src/games/hack/hack.rumors.c,v 1.3 1999/11/16 02:57:10 billf Exp $ */

#include "hack.h"			/* for RUMORFILE and BSD (index) */
#define	CHARSZ	8			/* number of bits in a char */
int n_rumors = 0;
int n_used_rumors = -1;
char *usedbits;

static void init_rumors(FILE *);
static bool skipline(FILE *);
static void outline(FILE *);
static bool used(int);

static void
init_rumors(FILE *rumf)
{
	int i;

	n_used_rumors = 0;
	while (skipline(rumf))
		n_rumors++;
	rewind(rumf);
	i = n_rumors / CHARSZ;
	usedbits = alloc((unsigned)(i + 1));
	for (; i >= 0; i--)
		usedbits[i] = 0;
}

static bool
skipline(FILE *rumf)
{
	char line[COLNO];

	for (;;) {
		if (!fgets(line, sizeof(line), rumf))
			return (0);
		if (strchr(line, '\n'))
			return (1);
	}
}

static void
outline(FILE *rumf)
{
	char line[COLNO];
	char *ep;

	if (!fgets(line, sizeof(line), rumf))
		return;
	if ((ep = strchr(line, '\n')) != NULL)
		*ep = 0;
	pline("This cookie has a scrap of paper inside! It reads: ");
	pline("%s", line);
}

void
outrumor(void)
{
	int rn, i;
	FILE *rumf;

	if (n_rumors <= n_used_rumors ||
	    (rumf = fopen(RUMORFILE, "r")) == NULL)
		return;
	if (n_used_rumors < 0)
		init_rumors(rumf);
	if (!n_rumors)
		goto none;
	rn = rn2(n_rumors - n_used_rumors);
	i = 0;
	while (rn || used(i)) {
		skipline(rumf);
		if (!used(i))
			rn--;
		i++;
	}
	usedbits[i / CHARSZ] |= (1 << (i % CHARSZ));
	n_used_rumors++;
	outline(rumf);
none:
	fclose(rumf);
}

static bool
used(int i)
{
	return (usedbits[i / CHARSZ] & (1 << (i % CHARSZ)));
}