]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - quiz/quiz.c
1 /* $NetBSD: quiz.c,v 1.25 2009/08/27 00:24:11 dholland Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Jim R. Oldroyd at The Instruction Set and Keith Gabryelski at
9 * Commodore Business Machines.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
39 The Regents of the University of California. All rights reserved.");
44 static char sccsid
[] = "@(#)quiz.c 8.3 (Berkeley) 5/4/95";
46 __RCSID("$NetBSD: quiz.c,v 1.25 2009/08/27 00:24:11 dholland Exp $");
50 #include <sys/types.h>
61 #include "pathnames.h"
64 static int catone
, cattwo
, tflag
;
65 static unsigned qsize
;
67 int main(int, char *[]);
68 static char *appdstr(char *, const char *, size_t);
69 static void downcase(char *);
70 static void get_cats(char *, char *);
71 static void get_file(const char *);
72 static const char *next_cat(const char *);
73 static void quiz(void);
74 static void score(unsigned, unsigned, unsigned);
75 static void show_index(void);
76 static void usage(void) __dead
;
84 const char *indexfile
;
86 /* Revoke setgid privileges */
89 indexfile
= _PATH_QUIZIDX
;
90 while ((ch
= getopt(argc
, argv
, "i:t")) != -1)
112 get_cats(argv
[0], argv
[1]);
130 if ((fp
= fopen(file
, "r")) == NULL
)
135 * Should really free up space from any earlier read list
136 * but there are no reverse pointers to do so with.
140 while ((lp
= fgetln(fp
, &len
)) != NULL
) {
141 if (lp
[len
- 1] == '\n')
143 if (qp
->q_text
&& qp
->q_text
[strlen(qp
->q_text
) - 1] == '\\')
144 qp
->q_text
= appdstr(qp
->q_text
, lp
, len
);
146 if ((qp
->q_next
= malloc(sizeof(QE
))) == NULL
)
149 if ((qp
->q_text
= malloc(len
+ 1)) == NULL
)
151 strncpy(qp
->q_text
, lp
, len
);
152 qp
->q_text
[len
] = '\0';
153 qp
->q_asked
= qp
->q_answered
= FALSE
;
172 if (!(pager
= getenv("PAGER")) || (*pager
== 0))
175 if ((pf
= popen(pager
, "w")) == NULL
)
177 (void)fprintf(pf
, "Subjects:\n\n");
178 for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
179 for (s
= next_cat(qp
->q_text
); s
; s
= next_cat(s
)) {
181 errx(1, "%s", rxperr
);
182 if ((p
= rxp_expand()) != NULL
)
183 (void)fprintf(pf
, "%s ", p
);
185 (void)fprintf(pf
, "\n");
187 (void)fprintf(pf
, "\n%s\n%s\n%s\n",
188 "For example, \"quiz victim killer\" prints a victim's name and you reply",
189 "with the killer, and \"quiz killer victim\" works the other way around.",
190 "Type an empty line to get the correct answer.");
204 for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
205 s
= next_cat(qp
->q_text
);
206 catone
= cattwo
= i
= 0;
209 errx(1, "%s", rxperr
);
217 if (catone
&& cattwo
&& catone
!= cattwo
) {
218 if (!rxp_compile(qp
->q_text
))
219 errx(1, "%s", rxperr
);
220 get_file(rxp_expand());
224 errx(1, "invalid categories");
233 unsigned guesses
, rights
, wrongs
;
235 char *answer
, *t
, question
[LINE_SZ
];
239 guesses
= rights
= wrongs
= 0;
243 next
= random() % qsize
;
245 for (i
= 0; i
< next
; i
++)
247 while (qp
&& qp
->q_answered
)
253 if (tflag
&& random() % 100 > 20) {
254 /* repeat questions in tutorial mode */
255 while (qp
&& (!qp
->q_asked
|| qp
->q_answered
))
261 for (i
= 0; i
< catone
- 1; i
++)
264 errx(1, "%s", rxperr
);
266 if (!t
|| *t
== '\0') {
267 qp
->q_answered
= TRUE
;
270 (void)strcpy(question
, t
);
272 for (i
= 0; i
< cattwo
- 1; i
++)
275 errx(1, "%s", rxperr
);
277 if (!t
|| *t
== '\0') {
278 qp
->q_answered
= TRUE
;
282 (void)printf("%s?\n", question
);
284 if ((answer
= fgetln(stdin
, &len
)) == NULL
||
285 answer
[len
- 1] != '\n') {
286 score(rights
, wrongs
, guesses
);
289 answer
[len
- 1] = '\0';
291 if (rxp_match(answer
)) {
292 (void)printf("Right!\n");
294 qp
->q_answered
= TRUE
;
297 if (*answer
== '\0') {
298 (void)printf("%s\n", t
);
301 qp
->q_answered
= TRUE
;
304 (void)printf("What?\n");
307 score(rights
, wrongs
, guesses
);
345 if ((m
= malloc(strlen(s
) + len
+ 1)) == NULL
)
347 for (mp
= m
, sp
= s
; (*mp
++ = *sp
++) != '\0'; )
350 if (*(mp
- 1) == '\\')
353 while ((ch
= *mp
++ = *tp
++) && ch
!= '\n')
365 (void)printf("Rights %d, wrongs %d,", r
, w
);
367 (void)printf(" extra guesses %d,", g
);
368 (void)printf(" score %d%%\n", (r
+ w
+ g
) ? r
* 100 / (r
+ w
+ g
) : 0);
377 for (; (ch
= *p
) != '\0'; ++p
)
378 if (isascii(ch
) && isupper(ch
))
385 (void)fprintf(stderr
, "quiz [-t] [-i file] category1 category2\n");