summaryrefslogtreecommitdiffstats
path: root/fortune
diff options
context:
space:
mode:
authorchristos <christos@NetBSD.org>1998-02-04 10:16:19 +0000
committerchristos <christos@NetBSD.org>1998-02-04 10:16:19 +0000
commit219d2019826bcd95f509086ba250928d0fe1d841 (patch)
tree24cbbbb312ae90337adf0d76c3422f799f28658b /fortune
parentde1a759df95db2b97b40f84e44f0ef695625d02d (diff)
downloadbsdgames-darwin-219d2019826bcd95f509086ba250928d0fe1d841.tar.gz
bsdgames-darwin-219d2019826bcd95f509086ba250928d0fe1d841.tar.zst
bsdgames-darwin-219d2019826bcd95f509086ba250928d0fe1d841.zip
Add regexp support; remove -lcompat, use err*, warn*
Diffstat (limited to 'fortune')
-rw-r--r--fortune/fortune/Makefile6
-rw-r--r--fortune/fortune/fortune.c148
2 files changed, 72 insertions, 82 deletions
diff --git a/fortune/fortune/Makefile b/fortune/fortune/Makefile
index 89540f17..d3cb29c6 100644
--- a/fortune/fortune/Makefile
+++ b/fortune/fortune/Makefile
@@ -1,11 +1,9 @@
-# $NetBSD: Makefile,v 1.10 1997/10/22 04:56:18 lukem Exp $
+# $NetBSD: Makefile,v 1.11 1998/02/04 10:16:19 christos Exp $
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= fortune
MAN= fortune.6
-CPPFLAGS+= -I${.CURDIR}/../strfile
-LDADD+= -lcompat
-DPADD+= ${LIBCOMPAT}
+CPPFLAGS+= -I${.CURDIR}/../strfile -DHAVE_REGCOMP
.include "../../Makefile.inc"
.include <bsd.prog.mk>
diff --git a/fortune/fortune/fortune.c b/fortune/fortune/fortune.c
index e1d7dda0..8be947bf 100644
--- a/fortune/fortune/fortune.c
+++ b/fortune/fortune/fortune.c
@@ -1,4 +1,4 @@
-/* $NetBSD: fortune.c,v 1.11 1997/10/19 17:58:53 mycroft Exp $ */
+/* $NetBSD: fortune.c,v 1.12 1998/02/04 10:16:20 christos Exp $ */
/*-
* Copyright (c) 1986, 1993
@@ -46,7 +46,7 @@ __COPYRIGHT("@(#) Copyright (c) 1986, 1993\n\
#if 0
static char sccsid[] = "@(#)fortune.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: fortune.c,v 1.11 1997/10/19 17:58:53 mycroft Exp $");
+__RCSID("$NetBSD: fortune.c,v 1.12 1998/02/04 10:16:20 christos Exp $");
#endif
#endif /* not lint */
@@ -61,6 +61,7 @@ __RCSID("$NetBSD: fortune.c,v 1.11 1997/10/19 17:58:53 mycroft Exp $");
# include <ctype.h>
# include <stdlib.h>
# include <string.h>
+# include <err.h>
# include "strfile.h"
# include "pathnames.h"
@@ -168,20 +169,36 @@ int maxlen_in_list __P((FILEDESC *));
#endif
#ifndef NO_REGEX
-#ifdef REGCMP
-# define RE_COMP(p) (Re_pat = regcmp(p, NULL))
-# define BAD_COMP(f) ((f) == NULL)
-# define RE_EXEC(p) regex(Re_pat, (p))
+# if HAVE_REGCMP
+# define RE_INIT()
+# define RE_COMP(p) (Re_pat = regcmp(p, NULL))
+# define BAD_COMP(f) ((f) == NULL)
+# define RE_EXEC(p) regex(Re_pat, (p))
+# define RE_FREE()
char *Re_pat;
char *regcmp(), *regex();
-#else
-# define RE_COMP(p) (p = re_comp(p))
-# define BAD_COMP(f) ((f) != NULL)
-# define RE_EXEC(p) re_exec(p)
-
-#endif
+# elif HAVE_RE_COMP
+# define RE_INIT()
+# define RE_COMP(p) (p = re_comp(p))
+# define BAD_COMP(f) ((f) != NULL)
+# define RE_EXEC(p) re_exec(p)
+# define RE_FREE()
+# elif HAVE_REGCOMP
+# include <regex.h>
+regex_t *Re_pat = NULL;
+# define RE_INIT() if (Re_pat == NULL && \
+ (Re_pat = calloc(sizeof(*Re_pat), 1)) == NULL)\
+ err(1, "%s", "")
+# define RE_COMP(p) (regcomp(Re_pat, p, REG_EXTENDED))
+# define BAD_COMP(f) ((f) != 0)
+# define RE_EXEC(p) (!regexec(Re_pat, p, 0, NULL, 0))
+# define RE_FREE() if (Re_pat != NULL) \
+ regfree(Re_pat), Re_pat = NULL
+# else
+ #error "Need to define HAVE_REGCMP, HAVE_RE_COMP, or HAVE_REGCOMP"
+# endif
#endif
int
@@ -210,10 +227,8 @@ main(ac, av)
display(Fortfile);
#ifdef OK_TO_WRITE_DISK
- if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
- perror(Fortfile->posfile);
- exit(1);
- }
+ if ((fd = creat(Fortfile->posfile, 0666)) < 0)
+ err(1, "Can't create `%s'", Fortfile->posfile);
#ifdef LOCK_EX
/*
* if we can, we exclusive lock, but since it isn't very
@@ -234,8 +249,7 @@ main(ac, av)
(void) fortlen();
sleep((unsigned int) max(Fort_len / CPERS, MINW));
}
- exit(0);
- /* NOTREACHED */
+ return(0);
}
void
@@ -339,9 +353,7 @@ getargs(argc, argv)
# ifdef NO_REGEX
case 'i': /* case-insensitive match */
case 'm': /* dump out the fortunes */
- (void) fprintf(stderr,
- "fortune: can't match fortunes on this system (Sorry)\n");
- exit(0);
+ errx(1, "Can't match fortunes on this system (Sorry)");
# else /* NO_REGEX */
case 'm': /* dump out the fortunes */
Match++;
@@ -373,12 +385,14 @@ getargs(argc, argv)
if (pat != NULL) {
if (ignore_case)
pat = conv_pat(pat);
+ RE_INIT();
if (BAD_COMP(RE_COMP(pat))) {
-#ifndef REGCMP
- fprintf(stderr, "%s\n", pat);
-#else /* REGCMP */
- fprintf(stderr, "bad pattern: %s\n", pat);
-#endif /* REGCMP */
+#ifdef HAVE_REGCMP
+ warnx("bad pattern: %s\n", pat);
+#else /* !HAVE_REGCMP */
+ warnx("%s\n", pat);
+#endif /* !HAVE_REGCMP */
+ RE_FREE();
}
}
# endif /* NO_REGEX */
@@ -412,11 +426,11 @@ form_file_list(files, file_cnt)
for (sp = files[i]; isdigit(*sp); sp++)
percent = percent * 10 + *sp - '0';
if (percent > 100) {
- fprintf(stderr, "percentages must be <= 100\n");
+ warnx("Percentages must be <= 100");
return FALSE;
}
if (*sp == '.') {
- fprintf(stderr, "percentages must be integers\n");
+ warnx("Percentages must be integers");
return FALSE;
}
/*
@@ -430,7 +444,7 @@ form_file_list(files, file_cnt)
}
else if (*++sp == '\0') {
if (++i >= file_cnt) {
- fprintf(stderr, "percentages must precede files\n");
+ warnx("Percentages must precede files");
return FALSE;
}
sp = files[i];
@@ -491,7 +505,7 @@ add_file(percent, file, dir, head, tail, parent)
DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
over:
- if ((fd = open(path, 0)) < 0) {
+ if ((fd = open(path, O_RDONLY)) < 0) {
/*
* This is a sneak. If the user said -a, and if the
* file we're given isn't a file, we check to see if
@@ -514,7 +528,7 @@ over:
return add_file(percent, file, FORTDIR, head, tail,
parent);
if (parent == NULL)
- perror(path);
+ warn("Cannot open `%s'", path);
if (was_malloc)
free(path);
return FALSE;
@@ -534,9 +548,7 @@ over:
!is_fortfile(path, &fp->datfile, &fp->posfile, (parent != NULL))))
{
if (parent == NULL)
- fprintf(stderr,
- "fortune:%s not a fortune file or directory\n",
- path);
+ warnx("`%s' not a fortune file or directory", path);
free((char *) fp);
if (was_malloc)
free(path);
@@ -644,7 +656,7 @@ all_forts(fp, offensive)
return;
if (!is_fortfile(offensive, &datfile, &posfile, FALSE))
return;
- if ((fd = open(offensive, 0)) < 0)
+ if ((fd = open(offensive, O_RDONLY)) < 0)
return;
DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
scene = new_fp();
@@ -692,7 +704,7 @@ add_dir(fp)
(void) close(fp->fd);
fp->fd = -1;
if ((dir = opendir(fp->path)) == NULL) {
- perror(fp->path);
+ warn("Cannot open `%s'", fp->path);
return FALSE;
}
tailp = NULL;
@@ -708,8 +720,7 @@ add_dir(fp)
free(name);
}
if (fp->num_children == 0) {
- (void) fprintf(stderr,
- "fortune: %s: No fortune files in directory.\n", fp->path);
+ warnx("`%s': No fortune files in directory.\n", fp->path);
return FALSE;
}
return TRUE;
@@ -832,10 +843,8 @@ do_malloc(size)
{
void *new;
- if ((new = malloc(size)) == NULL) {
- (void) fprintf(stderr, "fortune: out of memory.\n");
- exit(1);
- }
+ if ((new = malloc(size)) == NULL)
+ err(1, "%s", "");
return new;
}
@@ -880,22 +889,13 @@ init_prob()
percent += fp->percent;
DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
percent, num_noprob));
- if (percent > 100) {
- (void) fprintf(stderr,
- "fortune: probabilities sum to %d%%!\n", percent);
- exit(1);
- }
- else if (percent < 100 && num_noprob == 0) {
- (void) fprintf(stderr,
- "fortune: no place to put residual probability (%d%%)\n",
+ if (percent > 100)
+ errx(1, "Probabilities sum to %d%%!", percent);
+ else if (percent < 100 && num_noprob == 0)
+ errx(1, "No place to put residual probability (%d%%)",
percent);
- exit(1);
- }
- else if (percent == 100 && num_noprob != 0) {
- (void) fprintf(stderr,
- "fortune: no probability left to put in residual files\n");
- exit(1);
- }
+ else if (percent == 100 && num_noprob != 0)
+ errx(1, "No probability left to put in residual files");
percent = 100 - percent;
if (Equal_probs)
if (num_noprob != 0) {
@@ -1060,10 +1060,8 @@ void
open_fp(fp)
FILEDESC *fp;
{
- if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) {
- perror(fp->path);
- exit(1);
- }
+ if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL)
+ err(1, "Cannot open `%s'", fp->path);
}
/*
@@ -1074,10 +1072,8 @@ void
open_dat(fp)
FILEDESC *fp;
{
- if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, 0)) < 0) {
- perror(fp->datfile);
- exit(1);
- }
+ if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, O_RDONLY)) < 0)
+ err(1, "Cannot open `%s'", fp->datfile);
}
/*
@@ -1096,7 +1092,7 @@ get_pos(fp)
assert(fp->read_tbl);
if (fp->pos == POS_UNKNOWN) {
#ifdef OK_TO_WRITE_DISK
- if ((fd = open(fp->posfile, 0)) < 0 ||
+ if ((fd = open(fp->posfile, O_RDONLY)) < 0 ||
read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
fp->pos = random() % fp->tbl.str_numstr;
else if (fp->pos >= fp->tbl.str_numstr)
@@ -1126,14 +1122,10 @@ get_tbl(fp)
if (fp->read_tbl)
return;
if (fp->child == NULL) {
- if ((fd = open(fp->datfile, 0)) < 0) {
- perror(fp->datfile);
- exit(1);
- }
+ if ((fd = open(fp->datfile, O_RDONLY)) < 0)
+ err(1, "Cannot open `%s'", fp->datfile);
if (read(fd, (char *) &fp->tbl, sizeof fp->tbl) != sizeof fp->tbl) {
- (void)fprintf(stderr,
- "fortune: %s corrupted\n", fp->path);
- exit(1);
+ errx(1, "Database `%s' corrupted", fp->path);
}
/* fp->tbl.str_version = ntohl(fp->tbl.str_version); */
fp->tbl.str_numstr = ntohl(fp->tbl.str_numstr);
@@ -1236,10 +1228,8 @@ conv_pat(orig)
cnt += 4;
else
cnt++;
- if ((new = malloc(cnt)) == NULL) {
- fprintf(stderr, "pattern too long for ignoring case\n");
- exit(1);
- }
+ if ((new = malloc(cnt)) == NULL)
+ err(1, "%s", "");
for (sp = new; *orig != '\0'; orig++) {
if (islower(*orig)) {
@@ -1345,13 +1335,15 @@ matches_in_list(list)
sp = Fortbuf;
}
}
+ RE_FREE();
}
# endif /* NO_REGEX */
void
usage()
{
- (void) fprintf(stderr, "fortune [-a");
+ extern char *__progname;
+ (void) fprintf(stderr, "%s [-a", __progname);
#ifdef DEBUG
(void) fprintf(stderr, "D");
#endif /* DEBUG */