-/* $NetBSD: quiz.c,v 1.16 1999/09/17 17:07:11 jsm Exp $ */
+/* $NetBSD: quiz.c,v 1.28 2019/02/03 03:19:25 mrg Exp $ */
/*-
* Copyright (c) 1991, 1993
* 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.
*
#include <sys/cdefs.h>
#ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
- The Regents of the University of California. All rights reserved.\n");
+__COPYRIGHT("@(#) Copyright (c) 1991, 1993\
+ The Regents of the University of California. All rights reserved.");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)quiz.c 8.3 (Berkeley) 5/4/95";
#else
-__RCSID("$NetBSD: quiz.c,v 1.16 1999/09/17 17:07:11 jsm Exp $");
+__RCSID("$NetBSD: quiz.c,v 1.28 2019/02/03 03:19:25 mrg Exp $");
#endif
#endif /* not lint */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <ctype.h>
#include <err.h>
#include <time.h>
#include <unistd.h>
static QE qlist;
static int catone, cattwo, tflag;
-static u_int qsize;
+static unsigned qsize;
-char *appdstr __P((char *, const char *, size_t));
-void downcase __P((char *));
-void get_cats __P((char *, char *));
-void get_file __P((const char *));
-int main __P((int, char *[]));
-const char *next_cat __P((const char *));
-void quiz __P((void));
-void score __P((u_int, u_int, u_int));
-void show_index __P((void));
-void usage __P((void)) __attribute__((__noreturn__));
+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[])
{
int ch;
const char *indexfile;
/* Revoke setgid privileges */
- setregid(getgid(), getgid());
+ setgid(getgid());
indexfile = _PATH_QUIZIDX;
while ((ch = getopt(argc, argv, "i:t")) != -1)
exit(0);
}
-void
-get_file(file)
- const char *file;
+static void
+get_file(const char *file)
{
FILE *fp;
QE *qp;
(void)fclose(fp);
}
-void
-show_index()
+static void
+show_index(void)
{
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)) {
(void)pclose(pf);
}
-void
-get_cats(cat1, cat2)
- char *cat1, *cat2;
+static void
+get_cats(char *cat1, char *cat2)
{
QE *qp;
int i;
errx(1, "invalid categories");
}
-void
-quiz()
+static void
+quiz(void)
{
QE *qp;
int i;
size_t len;
- u_int guesses, rights, wrongs;
- int next;
+ unsigned guesses, rights, wrongs;
+ unsigned next, j;
char *answer, *t, question[LINE_SZ];
const char *s;
break;
next = random() % qsize;
qp = qlist.q_next;
- for (i = 0; i < next; i++)
+ for (j = 0; j < next; j++)
qp = qp->q_next;
while (qp && qp->q_answered)
qp = qp->q_next;
score(rights, wrongs, guesses);
}
-const char *
-next_cat(s)
- const char * s;
+static const char *
+next_cat(const char *s)
{
int esc;
case ':':
if (!esc)
return (s);
+ /* FALLTHROUGH */
default:
esc = 0;
break;
/* NOTREACHED */
}
-char *
-appdstr(s, tp, len)
- char *s;
- const char *tp;
- size_t len;
+static char *
+appdstr(char *s, const char *tp, size_t len)
{
char *mp;
const char *sp;
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)
(void)printf(" score %d%%\n", (r + w + g) ? r * 100 / (r + w + g) : 0);
}
-void
-downcase(p)
- char *p;
+static void
+downcase(char *p)
{
int ch;
*p = tolower(ch);
}
-void
-usage()
+static void
+usage(void)
{
(void)fprintf(stderr, "quiz [-t] [-i file] category1 category2\n");
exit(1);