]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - quiz/quiz.c
1 /* $NetBSD: quiz.c,v 1.20 2004/01/27 20:30:30 jsm 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\n\
39 The Regents of the University of California. All rights reserved.\n");
44 static char sccsid
[] = "@(#)quiz.c 8.3 (Berkeley) 5/4/95";
46 __RCSID("$NetBSD: quiz.c,v 1.20 2004/01/27 20:30:30 jsm Exp $");
50 #include <sys/types.h>
62 #include "pathnames.h"
65 static int catone
, cattwo
, tflag
;
68 char *appdstr(char *, const char *, size_t);
69 void downcase(char *);
70 void get_cats(char *, char *);
71 void get_file(const char *);
72 int main(int, char *[]);
73 const char *next_cat(const char *);
75 void score(u_int
, u_int
, u_int
);
76 void show_index(void);
77 void usage(void) __attribute__((__noreturn__
));
85 const char *indexfile
;
87 /* Revoke setgid privileges */
90 indexfile
= _PATH_QUIZIDX
;
91 while ((ch
= getopt(argc
, argv
, "i:t")) != -1)
113 get_cats(argv
[0], argv
[1]);
131 if ((fp
= fopen(file
, "r")) == NULL
)
136 * Should really free up space from any earlier read list
137 * but there are no reverse pointers to do so with.
141 while ((lp
= fgetln(fp
, &len
)) != NULL
) {
142 if (lp
[len
- 1] == '\n')
144 if (qp
->q_text
&& qp
->q_text
[strlen(qp
->q_text
) - 1] == '\\')
145 qp
->q_text
= appdstr(qp
->q_text
, lp
, len
);
147 if ((qp
->q_next
= malloc(sizeof(QE
))) == NULL
)
150 if ((qp
->q_text
= malloc(len
+ 1)) == NULL
)
152 strncpy(qp
->q_text
, lp
, len
);
153 qp
->q_text
[len
] = '\0';
154 qp
->q_asked
= qp
->q_answered
= FALSE
;
173 if (!(pager
= getenv("PAGER")) || (*pager
== 0))
176 if ((pf
= popen(pager
, "w")) == NULL
)
178 (void)fprintf(pf
, "Subjects:\n\n");
179 for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
180 for (s
= next_cat(qp
->q_text
); s
; s
= next_cat(s
)) {
182 errx(1, "%s", rxperr
);
183 if ((p
= rxp_expand()) != NULL
)
184 (void)fprintf(pf
, "%s ", p
);
186 (void)fprintf(pf
, "\n");
188 (void)fprintf(pf
, "\n%s\n%s\n%s\n",
189 "For example, \"quiz victim killer\" prints a victim's name and you reply",
190 "with the killer, and \"quiz killer victim\" works the other way around.",
191 "Type an empty line to get the correct answer.");
205 for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
206 s
= next_cat(qp
->q_text
);
207 catone
= cattwo
= i
= 0;
210 errx(1, "%s", rxperr
);
218 if (catone
&& cattwo
&& catone
!= cattwo
) {
219 if (!rxp_compile(qp
->q_text
))
220 errx(1, "%s", rxperr
);
221 get_file(rxp_expand());
225 errx(1, "invalid categories");
234 u_int guesses
, rights
, wrongs
;
236 char *answer
, *t
, question
[LINE_SZ
];
240 guesses
= rights
= wrongs
= 0;
244 next
= random() % qsize
;
246 for (i
= 0; i
< next
; i
++)
248 while (qp
&& qp
->q_answered
)
254 if (tflag
&& random() % 100 > 20) {
255 /* repeat questions in tutorial mode */
256 while (qp
&& (!qp
->q_asked
|| qp
->q_answered
))
262 for (i
= 0; i
< catone
- 1; i
++)
265 errx(1, "%s", rxperr
);
267 if (!t
|| *t
== '\0') {
268 qp
->q_answered
= TRUE
;
271 (void)strcpy(question
, t
);
273 for (i
= 0; i
< cattwo
- 1; i
++)
276 errx(1, "%s", rxperr
);
278 if (!t
|| *t
== '\0') {
279 qp
->q_answered
= TRUE
;
283 (void)printf("%s?\n", question
);
285 if ((answer
= fgetln(stdin
, &len
)) == NULL
||
286 answer
[len
- 1] != '\n') {
287 score(rights
, wrongs
, guesses
);
290 answer
[len
- 1] = '\0';
292 if (rxp_match(answer
)) {
293 (void)printf("Right!\n");
295 qp
->q_answered
= TRUE
;
298 if (*answer
== '\0') {
299 (void)printf("%s\n", t
);
302 qp
->q_answered
= TRUE
;
305 (void)printf("What?\n");
308 score(rights
, wrongs
, guesses
);
346 if ((m
= malloc(strlen(s
) + len
+ 1)) == NULL
)
348 for (mp
= m
, sp
= s
; (*mp
++ = *sp
++) != '\0'; )
351 if (*(mp
- 1) == '\\')
354 while ((ch
= *mp
++ = *tp
++) && ch
!= '\n')
366 (void)printf("Rights %d, wrongs %d,", r
, w
);
368 (void)printf(" extra guesses %d,", g
);
369 (void)printf(" score %d%%\n", (r
+ w
+ g
) ? r
* 100 / (r
+ w
+ g
) : 0);
378 for (; (ch
= *p
) != '\0'; ++p
)
379 if (isascii(ch
) && isupper(ch
))
386 (void)fprintf(stderr
, "quiz [-t] [-i file] category1 category2\n");