X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/462fca95471224f85b85be911e72faee16cd1597..db81cb3c9b81aac50cf37614d2da2f4f781d7211:/quiz/quiz.c diff --git a/quiz/quiz.c b/quiz/quiz.c index c00a6ef0..60a89264 100644 --- a/quiz/quiz.c +++ b/quiz/quiz.c @@ -1,9 +1,12 @@ +/* $NetBSD: quiz.c,v 1.10 1997/01/07 12:27:30 tls Exp $ */ + /*- - * Copyright (c) 1991 The Regents of the University of California. - * All rights reserved. + * 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. + * 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 @@ -35,23 +38,30 @@ */ #ifndef lint -char copyright[] = -"@(#) Copyright (c) 1991 The Regents of the University of California.\n\ - All rights reserved.\n"; +static char copyright[] = +"@(#) Copyright (c) 1991, 1993\n\ + The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint -/*static char sccsid[] = "from: @(#)quiz.c 5.1 (Berkeley) 11/10/91";*/ -static char rcsid[] = "$Id: quiz.c,v 1.2 1993/08/01 18:52:59 mycroft Exp $"; +#if 0 +static char sccsid[] = "@(#)quiz.c 8.3 (Berkeley) 5/4/95"; +#else +static char rcsid[] = "$NetBSD: quiz.c,v 1.10 1997/01/07 12:27:30 tls Exp $"; +#endif #endif /* not lint */ #include + +#include #include -#include #include #include #include #include +#include +#include +#include #include "quiz.h" #include "pathnames.h" @@ -59,9 +69,8 @@ static QE qlist; static int catone, cattwo, tflag; static u_int qsize; -char *appdstr __P((char *, char *)); +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 *)); @@ -120,7 +129,7 @@ get_file(file) char *lp; if ((fp = fopen(file, "r")) == NULL) - err("%s: %s", file, strerror(errno)); + err(1, "%s", file); /* * XXX @@ -129,15 +138,16 @@ get_file(file) */ qp = &qlist; qsize = 0; - while ((lp = fgetline(fp, &len)) != NULL) { + while ((lp = fgetln(fp, &len)) != NULL) { + lp[--len] = '\0'; if (qp->q_text && qp->q_text[strlen(qp->q_text) - 1] == '\\') - qp->q_text = appdstr(qp->q_text, lp); + qp->q_text = appdstr(qp->q_text, lp, len); else { if ((qp->q_next = malloc(sizeof(QE))) == NULL) - err("%s", strerror(errno)); + err(1, NULL); qp = qp->q_next; if ((qp->q_text = strdup(lp)) == NULL) - err("%s", strerror(errno)); + err(1, NULL); qp->q_asked = qp->q_answered = FALSE; qp->q_next = NULL; ++qsize; @@ -154,12 +164,12 @@ show_index() FILE *pf; if ((pf = popen(_PATH_PAGER, "w")) == NULL) - err("%s: %s", _PATH_PAGER, strerror(errno)); + err(1, "%s", _PATH_PAGER); (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)) { if (!rxp_compile(s)) - err("%s", rxperr); + errx(1, "%s", rxperr); if (p = rxp_expand()) (void)fprintf(pf, "%s ", p); } @@ -187,7 +197,7 @@ get_cats(cat1, cat2) catone = cattwo = i = 0; while (s) { if (!rxp_compile(s)) - err("%s", rxperr); + errx(1, "%s", rxperr); i++; if (rxp_match(cat1)) catone = i; @@ -197,12 +207,12 @@ get_cats(cat1, cat2) } if (catone && cattwo && catone != cattwo) { if (!rxp_compile(qp->q_text)) - err("%s", rxperr); + errx(1, "%s", rxperr); get_file(rxp_expand()); return; } } - err("invalid categories"); + errx(1, "invalid categories"); } void @@ -210,10 +220,10 @@ quiz() { register QE *qp; register int i; + size_t len; u_int guesses, rights, wrongs; int next; - char *s, *t, question[LINE_SZ]; - char *answer; + char *answer, *s, *t, question[LINE_SZ]; srandom(time(NULL)); guesses = rights = wrongs = 0; @@ -241,7 +251,7 @@ quiz() for (i = 0; i < catone - 1; i++) s = next_cat(s); if (!rxp_compile(s)) - err("%s", rxperr); + errx(1, "%s", rxperr); t = rxp_expand(); if (!t || *t == '\0') { qp->q_answered = TRUE; @@ -252,7 +262,7 @@ quiz() for (i = 0; i < cattwo - 1; i++) s = next_cat(s); if (!rxp_compile(s)) - err("%s", rxperr); + errx(1, "%s", rxperr); t = rxp_expand(); if (!t || *t == '\0') { qp->q_answered = TRUE; @@ -261,10 +271,11 @@ quiz() qp->q_asked = TRUE; (void)printf("%s?\n", question); for (;; ++guesses) { - if ((answer = fgetline(stdin, NULL)) == NULL) { + if ((answer = fgetln(stdin, &len)) == NULL) { score(rights, wrongs, guesses); exit(0); } + answer[len - 1] = '\0'; downcase(answer); if (rxp_match(answer)) { (void)printf("Right!\n"); @@ -302,20 +313,22 @@ next_cat(s) } char * -appdstr(s, tp) +appdstr(s, tp, len) char *s; register char *tp; + size_t len; { register char *mp, *sp; register int ch; char *m; - if ((m = malloc(strlen(sp) + strlen(tp) + 1)) == NULL) - err("%s", strerror(errno)); + if ((m = malloc(strlen(s) + len + 1)) == NULL) + err(1, NULL); for (mp = m, sp = s; *mp++ = *sp++;); - + --mp; if (*(mp - 1) == '\\') --mp; + while ((ch = *mp++ = *tp++) && ch != '\n'); *mp = '\0'; @@ -350,31 +363,3 @@ usage() (void)fprintf(stderr, "quiz [-t] [-i file] category1 category2\n"); exit(1); } - -#if __STDC__ -#include -#else -#include -#endif - -void -#if __STDC__ -err(const char *fmt, ...) -#else -err(fmt, va_alist) - char *fmt; - va_dcl -#endif -{ - va_list ap; -#if __STDC__ - va_start(ap, fmt); -#else - va_start(ap); -#endif - (void)fprintf(stderr, "quiz: "); - (void)vfprintf(stderr, fmt, ap); - va_end(ap); - (void)fprintf(stderr, "\n"); - exit(1); -}