-/* $NetBSD: quiz.c,v 1.9 1995/04/22 10:16:58 cgd 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.
*
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
-static char 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.2 (Berkeley) 1/3/94";
+static char sccsid[] = "@(#)quiz.c 8.3 (Berkeley) 5/4/95";
#else
-static char rcsid[] = "$NetBSD: quiz.c,v 1.9 1995/04/22 10:16:58 cgd Exp $";
+__RCSID("$NetBSD: quiz.c,v 1.28 2019/02/03 03:19:25 mrg Exp $");
#endif
#endif /* not lint */
#include <sys/types.h>
+
+#include <ctype.h>
#include <errno.h>
-#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <ctype.h>
#include <err.h>
+#include <time.h>
+#include <unistd.h>
#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 *, size_t));
-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;
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;
qp = &qlist;
qsize = 0;
while ((lp = fgetln(fp, &len)) != NULL) {
- lp[--len] = '\0';
+ 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;
- 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;
(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");
(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);
errx(1, "invalid categories");
}
-void
-quiz()
+static void
+quiz(void)
{
- register QE *qp;
- register int i;
+ QE *qp;
+ int i;
size_t len;
- u_int guesses, rights, wrongs;
- int next;
- char *answer, *s, *t, question[LINE_SZ];
+ unsigned guesses, rights, wrongs;
+ unsigned next, j;
+ char *answer, *t, question[LINE_SZ];
+ const char *s;
srandom(time(NULL));
guesses = rights = wrongs = 0;
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;
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);
}
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);
+ /* FALLTHROUGH */
+ default:
+ esc = 0;
+ break;
}
/* NOTREACHED */
}
-char *
-appdstr(s, tp, len)
- char *s;
- register char *tp;
- size_t len;
+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) + len + 1)) == NULL)
- err(1, NULL);
- for (mp = m, sp = s; *mp++ = *sp++;);
+ 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)
(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);