1*45555366Smestre /* $OpenBSD: hack.rumors.c,v 1.10 2016/03/16 15:00:35 mestre Exp $ */
2d0b779f3Sniklas
3df930be7Sderaadt /*
4d25013f2Scamield * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5d25013f2Scamield * Amsterdam
6d25013f2Scamield * All rights reserved.
7d25013f2Scamield *
8d25013f2Scamield * Redistribution and use in source and binary forms, with or without
9d25013f2Scamield * modification, are permitted provided that the following conditions are
10d25013f2Scamield * met:
11d25013f2Scamield *
12d25013f2Scamield * - Redistributions of source code must retain the above copyright notice,
13d25013f2Scamield * this list of conditions and the following disclaimer.
14d25013f2Scamield *
15d25013f2Scamield * - Redistributions in binary form must reproduce the above copyright
16d25013f2Scamield * notice, this list of conditions and the following disclaimer in the
17d25013f2Scamield * documentation and/or other materials provided with the distribution.
18d25013f2Scamield *
19d25013f2Scamield * - Neither the name of the Stichting Centrum voor Wiskunde en
20d25013f2Scamield * Informatica, nor the names of its contributors may be used to endorse or
21d25013f2Scamield * promote products derived from this software without specific prior
22d25013f2Scamield * written permission.
23d25013f2Scamield *
24d25013f2Scamield * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25d25013f2Scamield * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26d25013f2Scamield * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27d25013f2Scamield * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28d25013f2Scamield * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29d25013f2Scamield * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30d25013f2Scamield * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31d25013f2Scamield * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32d25013f2Scamield * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33d25013f2Scamield * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34d25013f2Scamield * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35d25013f2Scamield */
36d25013f2Scamield
37d25013f2Scamield /*
38d25013f2Scamield * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39d25013f2Scamield * All rights reserved.
40d25013f2Scamield *
41d25013f2Scamield * Redistribution and use in source and binary forms, with or without
42d25013f2Scamield * modification, are permitted provided that the following conditions
43d25013f2Scamield * are met:
44d25013f2Scamield * 1. Redistributions of source code must retain the above copyright
45d25013f2Scamield * notice, this list of conditions and the following disclaimer.
46d25013f2Scamield * 2. Redistributions in binary form must reproduce the above copyright
47d25013f2Scamield * notice, this list of conditions and the following disclaimer in the
48d25013f2Scamield * documentation and/or other materials provided with the distribution.
49d25013f2Scamield * 3. The name of the author may not be used to endorse or promote products
50d25013f2Scamield * derived from this software without specific prior written permission.
51d25013f2Scamield *
52d25013f2Scamield * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53d25013f2Scamield * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54d25013f2Scamield * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55d25013f2Scamield * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56d25013f2Scamield * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57d25013f2Scamield * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58d25013f2Scamield * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59d25013f2Scamield * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60d25013f2Scamield * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61d25013f2Scamield * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62df930be7Sderaadt */
63df930be7Sderaadt
64*45555366Smestre #include <err.h>
65df930be7Sderaadt #include <stdio.h>
66aed906e4Smestre
67df930be7Sderaadt #include "hack.h" /* for RUMORFILE and BSD (index) */
68aed906e4Smestre
69df930be7Sderaadt #define CHARSZ 8 /* number of bits in a char */
70df930be7Sderaadt int n_rumors = 0;
71df930be7Sderaadt int n_used_rumors = -1;
72df930be7Sderaadt char *usedbits;
73df930be7Sderaadt
744a5fbbc4Spjanzen static void init_rumors(FILE *);
754a5fbbc4Spjanzen static int skipline(FILE *);
764a5fbbc4Spjanzen static void outline(FILE *);
774a5fbbc4Spjanzen static int used(int);
784a5fbbc4Spjanzen
794a5fbbc4Spjanzen static void
init_rumors(FILE * rumf)804a5fbbc4Spjanzen init_rumors(FILE *rumf)
814a5fbbc4Spjanzen {
824a5fbbc4Spjanzen int i;
834a5fbbc4Spjanzen
84df930be7Sderaadt n_used_rumors = 0;
85df930be7Sderaadt while(skipline(rumf)) n_rumors++;
86*45555366Smestre if (fseek(rumf, 0L, SEEK_SET) == -1)
87*45555366Smestre err(1, "fseek");
88df930be7Sderaadt i = n_rumors/CHARSZ;
89df930be7Sderaadt usedbits = (char *) alloc((unsigned)(i+1));
90df930be7Sderaadt for( ; i>=0; i--) usedbits[i] = 0;
91df930be7Sderaadt }
92df930be7Sderaadt
934a5fbbc4Spjanzen static int
skipline(FILE * rumf)944a5fbbc4Spjanzen skipline(FILE *rumf)
954a5fbbc4Spjanzen {
96df930be7Sderaadt char line[COLNO];
974a5fbbc4Spjanzen
98df930be7Sderaadt while(1) {
99df930be7Sderaadt if(!fgets(line, sizeof(line), rumf)) return(0);
100180acc8fSmillert if(strchr(line, '\n')) return(1);
101df930be7Sderaadt }
102df930be7Sderaadt }
103df930be7Sderaadt
1044a5fbbc4Spjanzen static void
outline(FILE * rumf)1054a5fbbc4Spjanzen outline(FILE *rumf)
1064a5fbbc4Spjanzen {
107df930be7Sderaadt char line[COLNO];
1084a5fbbc4Spjanzen
109df930be7Sderaadt if(!fgets(line, sizeof(line), rumf)) return;
110712678dfSgilles line[strcspn(line, "\n")] = '\0';
111df930be7Sderaadt pline("This cookie has a scrap of paper inside! It reads: ");
112911134d2Sguenther pline("%s", line);
113df930be7Sderaadt }
114df930be7Sderaadt
1154a5fbbc4Spjanzen void
outrumor(void)116aed906e4Smestre outrumor(void)
1174a5fbbc4Spjanzen {
1184a5fbbc4Spjanzen int rn,i;
1194a5fbbc4Spjanzen FILE *rumf;
1204a5fbbc4Spjanzen
121df930be7Sderaadt if(n_rumors <= n_used_rumors ||
122df930be7Sderaadt (rumf = fopen(RUMORFILE, "r")) == (FILE *) 0) return;
123df930be7Sderaadt if(n_used_rumors < 0) init_rumors(rumf);
124df930be7Sderaadt if(!n_rumors) goto none;
125df930be7Sderaadt rn = rn2(n_rumors - n_used_rumors);
126df930be7Sderaadt i = 0;
127df930be7Sderaadt while(rn || used(i)) {
128df930be7Sderaadt (void) skipline(rumf);
129df930be7Sderaadt if(!used(i)) rn--;
130df930be7Sderaadt i++;
131df930be7Sderaadt }
132df930be7Sderaadt usedbits[i/CHARSZ] |= (1 << (i % CHARSZ));
133df930be7Sderaadt n_used_rumors++;
134df930be7Sderaadt outline(rumf);
135df930be7Sderaadt none:
136df930be7Sderaadt (void) fclose(rumf);
137df930be7Sderaadt }
138df930be7Sderaadt
1394a5fbbc4Spjanzen static int
used(int i)1404a5fbbc4Spjanzen used(int i)
1414a5fbbc4Spjanzen {
142df930be7Sderaadt return(usedbits[i/CHARSZ] & (1 << (i % CHARSZ)));
143df930be7Sderaadt }
144