NetBSD/games/hack/hack.rumors.c

94 lines
1.7 KiB
C
Raw Normal View History

1997-10-19 20:56:41 +04:00
/* $NetBSD: hack.rumors.c,v 1.4 1997/10/19 16:58:55 christos Exp $ */
/*
* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
*/
1997-10-19 20:56:41 +04:00
#include <sys/cdefs.h>
#ifndef lint
1997-10-19 20:56:41 +04:00
__RCSID("$NetBSD: hack.rumors.c,v 1.4 1997/10/19 16:58:55 christos Exp $");
#endif /* not lint */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
#include "hack.h" /* for RUMORFILE and BSD (strchr) */
#include "extern.h"
#define CHARSZ 8 /* number of bits in a char */
int n_rumors = 0;
int n_used_rumors = -1;
char *usedbits;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
void
init_rumors(rumf)
FILE *rumf;
{
int i;
1993-03-21 12:45:37 +03:00
n_used_rumors = 0;
1997-10-19 20:56:41 +04:00
while (skipline(rumf))
n_rumors++;
1993-03-21 12:45:37 +03:00
rewind(rumf);
1997-10-19 20:56:41 +04:00
i = n_rumors / CHARSZ;
usedbits = (char *) alloc((unsigned) (i + 1));
for (; i >= 0; i--)
usedbits[i] = 0;
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
skipline(rumf)
FILE *rumf;
{
char line[COLNO];
while (1) {
if (!fgets(line, sizeof(line), rumf))
return (0);
if (strchr(line, '\n'))
return (1);
1993-03-21 12:45:37 +03:00
}
}
1997-10-19 20:56:41 +04:00
void
outline(rumf)
FILE *rumf;
{
char line[COLNO];
char *ep;
if (!fgets(line, sizeof(line), rumf))
return;
if ((ep = strchr(line, '\n')) != 0)
*ep = 0;
1993-03-21 12:45:37 +03:00
pline("This cookie has a scrap of paper inside! It reads: ");
pline(line);
}
1997-10-19 20:56:41 +04:00
void
outrumor()
{
int rn, i;
FILE *rumf;
if (n_rumors <= n_used_rumors ||
(rumf = fopen(RUMORFILE, "r")) == (FILE *) 0)
return;
if (n_used_rumors < 0)
init_rumors(rumf);
if (!n_rumors)
goto none;
1993-03-21 12:45:37 +03:00
rn = rn2(n_rumors - n_used_rumors);
i = 0;
1997-10-19 20:56:41 +04:00
while (rn || used(i)) {
1993-03-21 12:45:37 +03:00
(void) skipline(rumf);
1997-10-19 20:56:41 +04:00
if (!used(i))
rn--;
1993-03-21 12:45:37 +03:00
i++;
}
1997-10-19 20:56:41 +04:00
usedbits[i / CHARSZ] |= (1 << (i % CHARSZ));
1993-03-21 12:45:37 +03:00
n_used_rumors++;
outline(rumf);
none:
(void) fclose(rumf);
}
1997-10-19 20:56:41 +04:00
int
used(i)
int i;
{
return (usedbits[i / CHARSZ] & (1 << (i % CHARSZ)));
1993-03-21 12:45:37 +03:00
}