+/* $NetBSD: quiz.c,v 1.23 2008/07/20 01:03:22 lukem 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
* 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
-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.5 1994/01/04 05:23:56 cgd Exp $";
+#if 0
+static char sccsid[] = "@(#)quiz.c 8.3 (Berkeley) 5/4/95";
+#else
+__RCSID("$NetBSD: quiz.c,v 1.23 2008/07/20 01:03:22 lukem 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 int catone, cattwo, tflag;
static u_int qsize;
-char *appdstr __P((char *, char *));
-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 quiz __P((void));
-void score __P((u_int, u_int, u_int));
-void show_index __P((void));
-void usage __P((void));
+char *appdstr(char *, const char *, size_t);
+void downcase(char *);
+void get_cats(char *, char *);
+void get_file(const char *);
+int main(int, char *[]);
+const char *next_cat(const char *);
+void quiz(void);
+void score(u_int, u_int, u_int);
+void show_index(void);
+void usage(void) __dead;
int
main(argc, argv)
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;
void
get_file(file)
- char *file;
+ const char *file;
{
- register FILE *fp;
- register QE *qp;
+ FILE *fp;
+ QE *qp;
size_t len;
char *lp;
if ((fp = fopen(file, "r")) == NULL)
- err("%s: %s", file, strerror(errno));
+ err(1, "%s", file);
/*
* XXX
qp = &qlist;
qsize = 0;
while ((lp = fgetln(fp, &len)) != NULL) {
+ 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);
+ qp->q_text = appdstr(qp->q_text, lp, len);
else {
if ((qp->q_next = malloc(sizeof(QE))) == NULL)
- err("%s", strerror(errno));
+ errx(1, "malloc");
qp = qp->q_next;
- lp[len - 1] = '\0';
- if ((qp->q_text = strdup(lp)) == NULL)
- err("%s", strerror(errno));
+ 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
show_index()
{
- 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("%s: %s", _PATH_PAGER, strerror(errno));
+ 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))
- err("%s", rxperr);
- if (p = rxp_expand())
+ errx(1, "%s", rxperr);
+ if ((p = rxp_expand()) != NULL)
(void)fprintf(pf, "%s ", p);
}
(void)fprintf(pf, "\n");
get_cats(cat1, cat2)
char *cat1, *cat2;
{
- register QE *qp;
+ QE *qp;
int i;
- char *s;
+ const char *s;
downcase(cat1);
downcase(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;
}
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
quiz()
{
- register QE *qp;
- register int i;
+ QE *qp;
+ int i;
+ size_t len;
u_int guesses, rights, wrongs;
- int len, next;
- char *s, *t, question[LINE_SZ];
- char *answer;
+ int next;
+ char *answer, *t, question[LINE_SZ];
+ const char *s;
srandom(time(NULL));
guesses = rights = wrongs = 0;
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;
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;
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 *
+const char *
next_cat(s)
- register char * s;
+ 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)
+appdstr(s, tp, len)
char *s;
- register char *tp;
+ 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("%s", strerror(errno));
- 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);
void
downcase(p)
- register char *p;
+ 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)fprintf(stderr, "quiz [-t] [-i file] category1 category2\n");
exit(1);
}
-
-#if __STDC__
-#include <stdarg.h>
-#else
-#include <varargs.h>
-#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);
-}