]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - quiz/quiz.c
2 * Copyright (c) 1991 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
6 * Jim R. Oldroyd at The Instruction Set.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
40 All rights reserved.\n";
44 static char sccsid
[] = "@(#)quiz.c 5.1 (Berkeley) 11/10/91";
47 #include <sys/types.h>
55 #include "pathnames.h"
58 static int catone
, cattwo
, tflag
;
61 char *appdstr
__P((char *, char *));
62 void downcase
__P((char *));
63 void err
__P((const char *, ...));
64 void get_cats
__P((char *, char *));
65 void get_file
__P((char *));
66 char *next_cat
__P((char *));
67 void quiz
__P((void));
68 void score
__P((u_int
, u_int
, u_int
));
69 void show_index
__P((void));
70 void usage
__P((void));
80 indexfile
= _PATH_QUIZIDX
;
81 while ((ch
= getopt(argc
, argv
, "i:t")) != EOF
)
103 get_cats(argv
[0], argv
[1]);
121 if ((fp
= fopen(file
, "r")) == NULL
)
122 err("%s: %s", file
, strerror(errno
));
126 * Should really free up space from any earlier read list
127 * but there are no reverse pointers to do so with.
131 while ((lp
= fgetline(fp
, &len
)) != NULL
) {
132 if (qp
->q_text
&& qp
->q_text
[strlen(qp
->q_text
) - 1] == '\\')
133 qp
->q_text
= appdstr(qp
->q_text
, lp
);
135 if ((qp
->q_next
= malloc(sizeof(QE
))) == NULL
)
136 err("%s", strerror(errno
));
138 if ((qp
->q_text
= strdup(lp
)) == NULL
)
139 err("%s", strerror(errno
));
140 qp
->q_asked
= qp
->q_answered
= FALSE
;
152 register char *p
, *s
;
155 if ((pf
= popen(_PATH_PAGER
, "w")) == NULL
)
156 err("%s: %s", _PATH_PAGER
, strerror(errno
));
157 (void)fprintf(pf
, "Subjects:\n\n");
158 for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
159 for (s
= next_cat(qp
->q_text
); s
; s
= next_cat(s
)) {
162 if (p
= rxp_expand())
163 (void)fprintf(pf
, "%s ", p
);
165 (void)fprintf(pf
, "\n");
167 (void)fprintf(pf
, "\n%s\n%s\n%s\n",
168 "For example, \"quiz victim killer\" prints a victim's name and you reply",
169 "with the killer, and \"quiz killer victim\" works the other way around.",
170 "Type an empty line to get the correct answer.");
184 for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
185 s
= next_cat(qp
->q_text
);
186 catone
= cattwo
= i
= 0;
197 if (catone
&& cattwo
&& catone
!= cattwo
) {
198 if (!rxp_compile(qp
->q_text
))
200 get_file(rxp_expand());
204 err("invalid categories");
212 u_int guesses
, rights
, wrongs
;
214 char *s
, *t
, question
[LINE_SZ
];
218 guesses
= rights
= wrongs
= 0;
222 next
= random() % qsize
;
224 for (i
= 0; i
< next
; i
++)
226 while (qp
&& qp
->q_answered
)
232 if (tflag
&& random() % 100 > 20) {
233 /* repeat questions in tutorial mode */
234 while (qp
&& (!qp
->q_asked
|| qp
->q_answered
))
240 for (i
= 0; i
< catone
- 1; i
++)
245 if (!t
|| *t
== '\0') {
246 qp
->q_answered
= TRUE
;
249 (void)strcpy(question
, t
);
251 for (i
= 0; i
< cattwo
- 1; i
++)
256 if (!t
|| *t
== '\0') {
257 qp
->q_answered
= TRUE
;
261 (void)printf("%s?\n", question
);
263 if ((answer
= fgetline(stdin
, NULL
)) == NULL
) {
264 score(rights
, wrongs
, guesses
);
268 if (rxp_match(answer
)) {
269 (void)printf("Right!\n");
271 qp
->q_answered
= TRUE
;
274 if (*answer
== '\0') {
275 (void)printf("%s\n", t
);
278 qp
->q_answered
= TRUE
;
281 (void)printf("What?\n");
284 score(rights
, wrongs
, guesses
);
308 register char *mp
, *sp
;
312 if ((m
= malloc(strlen(sp
) + strlen(tp
) + 1)) == NULL
)
313 err("%s", strerror(errno
));
314 for (mp
= m
, sp
= s
; *mp
++ = *sp
++;);
316 if (*(mp
- 1) == '\\')
318 while ((ch
= *mp
++ = *tp
++) && ch
!= '\n');
329 (void)printf("Rights %d, wrongs %d,", r
, w
);
331 (void)printf(" extra guesses %d,", g
);
332 (void)printf(" score %d%%\n", (r
+ w
+ g
) ? r
* 100 / (r
+ w
+ g
) : 0);
342 if (isascii(ch
) && isupper(ch
))
349 (void)fprintf(stderr
, "quiz [-t] [-i file] category1 category2\n");
361 err(const char *fmt
, ...)
374 (void)fprintf(stderr
, "quiz: ");
375 (void)vfprintf(stderr
, fmt
, ap
);
377 (void)fprintf(stderr
, "\n");