X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/d7c75ff32a979078381d12f2121e1b1097d42a55..3c3f8c0880fed372a83ed917c866247edb974156:/quiz/quiz.c diff --git a/quiz/quiz.c b/quiz/quiz.c index 00e2e2a4..eb11f916 100644 --- a/quiz/quiz.c +++ b/quiz/quiz.c @@ -1,9 +1,12 @@ +/* $NetBSD: quiz.c,v 1.26 2009/08/27 00:31:12 dholland 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 @@ -13,11 +16,7 @@ * 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 + * 3. 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. * @@ -34,52 +33,58 @@ * SUCH DAMAGE. */ +#include #ifndef lint -char copyright[] = -"@(#) Copyright (c) 1991 The Regents of the University of California.\n\ - All rights reserved.\n"; +__COPYRIGHT("@(#) Copyright (c) 1991, 1993\ + The Regents of the University of California. All rights reserved."); #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.6 1994/04/08 08:33:13 pk Exp $"; +#if 0 +static char sccsid[] = "@(#)quiz.c 8.3 (Berkeley) 5/4/95"; +#else +__RCSID("$NetBSD: quiz.c,v 1.26 2009/08/27 00:31:12 dholland Exp $"); +#endif #endif /* not lint */ #include + +#include #include -#include #include #include #include -#include #include +#include +#include #include "quiz.h" #include "pathnames.h" static QE qlist; static int catone, cattwo, tflag; -static u_int qsize; +static unsigned qsize; -char *appdstr __P((char *, char *)); -void downcase __P((char *)); -void get_cats __P((char *, char *)); -void get_file __P((char *)); -char *next_cat __P((char *)); -void quiz __P((void)); -void score __P((u_int, u_int, u_int)); -void show_index __P((void)); -void usage __P((void)); +static char *appdstr(char *, const char *, size_t); +static void downcase(char *); +static void get_cats(char *, char *); +static void get_file(const char *); +static const char *next_cat(const char *); +static void quiz(void); +static void score(unsigned, unsigned, unsigned); +static void show_index(void); +static void usage(void) __dead; int -main(argc, argv) - int argc; - char *argv[]; +main(int argc, char *argv[]) { - register int ch; - char *indexfile; + int ch; + const char *indexfile; + + /* Revoke setgid privileges */ + setgid(getgid()); indexfile = _PATH_QUIZIDX; - while ((ch = getopt(argc, argv, "i:t")) != EOF) + while ((ch = getopt(argc, argv, "i:t")) != -1) switch(ch) { case 'i': indexfile = optarg; @@ -110,12 +115,11 @@ main(argc, argv) exit(0); } -void -get_file(file) - char *file; +static void +get_file(const char *file) { - register FILE *fp; - register QE *qp; + FILE *fp; + QE *qp; size_t len; char *lp; @@ -130,16 +134,18 @@ get_file(file) qp = &qlist; qsize = 0; while ((lp = fgetln(fp, &len)) != NULL) { - if (qp->q_text && qp->q_text[strlen(qp->q_text) - 1] == '\\') { - lp[len - 1] = '\0'; - qp->q_text = appdstr(qp->q_text, lp); - } else { + if (lp[len - 1] == '\n') + lp[--len] = '\0'; + if (qp->q_text && qp->q_text[strlen(qp->q_text) - 1] == '\\') + qp->q_text = appdstr(qp->q_text, lp, len); + else { if ((qp->q_next = malloc(sizeof(QE))) == NULL) - err(1, NULL); + errx(1, "malloc"); qp = qp->q_next; - lp[len - 1] = '\0'; - if ((qp->q_text = strdup(lp)) == NULL) - err(1, NULL); + if ((qp->q_text = malloc(len + 1)) == NULL) + errx(1, "malloc"); + strncpy(qp->q_text, lp, len); + qp->q_text[len] = '\0'; qp->q_asked = qp->q_answered = FALSE; qp->q_next = NULL; ++qsize; @@ -148,21 +154,28 @@ get_file(file) (void)fclose(fp); } -void -show_index() +static void +show_index(void) { - register QE *qp; - register char *p, *s; + QE *qp; + const char *p, *s; FILE *pf; + const char *pager; - if ((pf = popen(_PATH_PAGER, "w")) == NULL) - err(1, "%s", _PATH_PAGER); + if (!isatty(1)) + pager = "cat"; + else { + if (!(pager = getenv("PAGER")) || (*pager == 0)) + pager = _PATH_PAGER; + } + if ((pf = popen(pager, "w")) == NULL) + err(1, "%s", 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)) errx(1, "%s", rxperr); - if (p = rxp_expand()) + if ((p = rxp_expand()) != NULL) (void)fprintf(pf, "%s ", p); } (void)fprintf(pf, "\n"); @@ -174,13 +187,12 @@ show_index() (void)pclose(pf); } -void -get_cats(cat1, cat2) - char *cat1, *cat2; +static void +get_cats(char *cat1, char *cat2) { - register QE *qp; + QE *qp; int i; - char *s; + const char *s; downcase(cat1); downcase(cat2); @@ -207,15 +219,16 @@ get_cats(cat1, cat2) errx(1, "invalid categories"); } -void -quiz() +static void +quiz(void) { - register QE *qp; - register int i; - u_int guesses, rights, wrongs; - int len, next; - char *s, *t, question[LINE_SZ]; - char *answer; + QE *qp; + int i; + size_t len; + unsigned guesses, rights, wrongs; + int next; + char *answer, *t, question[LINE_SZ]; + const char *s; srandom(time(NULL)); guesses = rights = wrongs = 0; @@ -263,7 +276,8 @@ quiz() qp->q_asked = TRUE; (void)printf("%s?\n", question); for (;; ++guesses) { - if ((answer = fgetln(stdin, &len)) == NULL) { + if ((answer = fgetln(stdin, &len)) == NULL || + answer[len - 1] != '\n') { score(rights, wrongs, guesses); exit(0); } @@ -288,48 +302,55 @@ quiz() score(rights, wrongs, guesses); } -char * -next_cat(s) - register char * s; +static const char * +next_cat(const char *s) { + int esc; + + esc = 0; for (;;) switch (*s++) { case '\0': return (NULL); case '\\': + esc = 1; break; case ':': - return (s); + if (!esc) + return (s); + default: + esc = 0; + break; } /* NOTREACHED */ } -char * -appdstr(s, tp) - char *s; - register char *tp; +static char * +appdstr(char *s, const char *tp, size_t len) { - register char *mp, *sp; - register int ch; + char *mp; + const char *sp; + int ch; char *m; - if ((m = malloc(strlen(s) + strlen(tp) + 1)) == NULL) - err(1, NULL); - for (mp = m, sp = s; *mp++ = *sp++;); + if ((m = malloc(strlen(s) + len + 1)) == NULL) + errx(1, "malloc"); + for (mp = m, sp = s; (*mp++ = *sp++) != '\0'; ) + ; --mp; - if (*(mp - 1) == '\\') --mp; - while ((ch = *mp++ = *tp++) && ch != '\n'); + + while ((ch = *mp++ = *tp++) && ch != '\n') + ; *mp = '\0'; free(s); return (m); } -void -score(r, w, g) - u_int r, w, g; +static void +score(unsigned r, unsigned w, unsigned g) { (void)printf("Rights %d, wrongs %d,", r, w); if (g) @@ -337,19 +358,18 @@ score(r, w, g) (void)printf(" score %d%%\n", (r + w + g) ? r * 100 / (r + w + g) : 0); } -void -downcase(p) - register char *p; +static void +downcase(char *p) { - register int ch; + int ch; - for (; ch = *p; ++p) + for (; (ch = *p) != '\0'; ++p) if (isascii(ch) && isupper(ch)) *p = tolower(ch); } -void -usage() +static void +usage(void) { (void)fprintf(stderr, "quiz [-t] [-i file] category1 category2\n"); exit(1);