* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* Jim R. Oldroyd at The Instruction Set and Keith Gabryelski at
* Commodore Business Machines.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char copyright
[] =
"@(#) Copyright (c) 1991, 1993\n\
The Regents of the University of California. All rights reserved.\n";
static char sccsid
[] = "@(#)quiz.c 8.2 (Berkeley) 1/3/94";
static int catone
, cattwo
, tflag
;
char *appdstr
__P((char *, char *, size_t));
void downcase
__P((char *));
void err
__P((const char *, ...));
void get_cats
__P((char *, char *));
void get_file
__P((char *));
char *next_cat
__P((char *));
void score
__P((u_int
, u_int
, u_int
));
void show_index
__P((void));
indexfile
= _PATH_QUIZIDX
;
while ((ch
= getopt(argc
, argv
, "i:t")) != EOF
)
get_cats(argv
[0], argv
[1]);
if ((fp
= fopen(file
, "r")) == NULL
)
err("%s: %s", file
, strerror(errno
));
* Should really free up space from any earlier read list
* but there are no reverse pointers to do so with.
while ((lp
= fgetln(fp
, &len
)) != NULL
) {
if (qp
->q_text
&& qp
->q_text
[strlen(qp
->q_text
) - 1] == '\\')
qp
->q_text
= appdstr(qp
->q_text
, lp
, len
);
if ((qp
->q_next
= malloc(sizeof(QE
))) == NULL
)
err("%s", strerror(errno
));
if ((qp
->q_text
= strdup(lp
)) == NULL
)
err("%s", strerror(errno
));
qp
->q_asked
= qp
->q_answered
= FALSE
;
if ((pf
= popen(_PATH_PAGER
, "w")) == NULL
)
err("%s: %s", _PATH_PAGER
, strerror(errno
));
(void)fprintf(pf
, "Subjects:\n\n");
for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
for (s
= next_cat(qp
->q_text
); s
; s
= next_cat(s
)) {
(void)fprintf(pf
, "%s ", p
);
(void)fprintf(pf
, "\n%s\n%s\n%s\n",
"For example, \"quiz victim killer\" prints a victim's name and you reply",
"with the killer, and \"quiz killer victim\" works the other way around.",
"Type an empty line to get the correct answer.");
for (qp
= qlist
.q_next
; qp
; qp
= qp
->q_next
) {
s
= next_cat(qp
->q_text
);
if (catone
&& cattwo
&& catone
!= cattwo
) {
if (!rxp_compile(qp
->q_text
))
err("invalid categories");
u_int guesses
, rights
, wrongs
;
char *answer
, *s
, *t
, question
[LINE_SZ
];
guesses
= rights
= wrongs
= 0;
for (i
= 0; i
< next
; i
++)
while (qp
&& qp
->q_answered
)
if (tflag
&& random() % 100 > 20) {
/* repeat questions in tutorial mode */
while (qp
&& (!qp
->q_asked
|| qp
->q_answered
))
for (i
= 0; i
< catone
- 1; i
++)
(void)strcpy(question
, t
);
for (i
= 0; i
< cattwo
- 1; i
++)
(void)printf("%s?\n", question
);
if ((answer
= fgetln(stdin
, &len
)) == NULL
) {
score(rights
, wrongs
, guesses
);
(void)printf("Right!\n");
score(rights
, wrongs
, guesses
);
if ((m
= malloc(strlen(s
) + len
+ 1)) == NULL
)
err("%s", strerror(errno
));
for (mp
= m
, sp
= s
; *mp
++ = *sp
++;);
while ((ch
= *mp
++ = *tp
++) && ch
!= '\n');
(void)printf("Rights %d, wrongs %d,", r
, w
);
(void)printf(" extra guesses %d,", g
);
(void)printf(" score %d%%\n", (r
+ w
+ g
) ? r
* 100 / (r
+ w
+ g
) : 0);
if (isascii(ch
) && isupper(ch
))
(void)fprintf(stderr
, "quiz [-t] [-i file] category1 category2\n");
err(const char *fmt
, ...)
(void)fprintf(stderr
, "quiz: ");
(void)vfprintf(stderr
, fmt
, ap
);
(void)fprintf(stderr
, "\n");