summaryrefslogtreecommitdiffstats
path: root/fortune/fortune/fortune.c
diff options
context:
space:
mode:
authordholland <dholland@NetBSD.org>2009-08-27 03:09:17 +0000
committerdholland <dholland@NetBSD.org>2009-08-27 03:09:17 +0000
commitdebd68cbca00721bd771a9a352f0b5047a2a9b48 (patch)
treec4a4ea1365a2a0c46b3a51d424e68a70db9782da /fortune/fortune/fortune.c
parent1ffad968680536cc642b291b9829f5e477691b2e (diff)
downloadbsdgames-darwin-debd68cbca00721bd771a9a352f0b5047a2a9b48.tar.gz
bsdgames-darwin-debd68cbca00721bd771a9a352f0b5047a2a9b48.tar.zst
bsdgames-darwin-debd68cbca00721bd771a9a352f0b5047a2a9b48.zip
Instead of trying to cons up a regexp that ignores case, use REG_ICASE.
This makes fortune -im work; it's been broken since whenever.
Diffstat (limited to 'fortune/fortune/fortune.c')
-rw-r--r--fortune/fortune/fortune.c64
1 files changed, 13 insertions, 51 deletions
diff --git a/fortune/fortune/fortune.c b/fortune/fortune/fortune.c
index c6a1c799..42603f81 100644
--- a/fortune/fortune/fortune.c
+++ b/fortune/fortune/fortune.c
@@ -1,4 +1,4 @@
-/* $NetBSD: fortune.c,v 1.61 2009/08/27 03:04:58 dholland Exp $ */
+/* $NetBSD: fortune.c,v 1.62 2009/08/27 03:09:17 dholland Exp $ */
/*-
* Copyright (c) 1986, 1993
@@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1986, 1993\
#if 0
static char sccsid[] = "@(#)fortune.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: fortune.c,v 1.61 2009/08/27 03:04:58 dholland Exp $");
+__RCSID("$NetBSD: fortune.c,v 1.62 2009/08/27 03:09:17 dholland Exp $");
#endif
#endif /* not lint */
@@ -176,9 +176,8 @@ static void zero_tbl(STRFILE *);
int main(int, char *[]);
#ifndef NO_REGEX
-static void re_setup(struct re *rx, const char *pattern);
+static void re_setup(struct re *rx, const char *pattern, bool ignore_case);
static void re_cleanup(struct re *rx);
-static char *conv_pat(char *);
static int find_matches(void);
static void matches_in_list(FILEDESC *);
static size_t maxlen_in_list(FILEDESC *);
@@ -379,11 +378,9 @@ getargs(int argc, char **argv)
#ifndef NO_REGEX
if (pat != NULL) {
- if (ignore_case)
- pat = conv_pat(pat);
- re_setup(&Re_pat, pat);
+ re_setup(&Re_pat, pat, ignore_case);
rot13(pat, 0);
- re_setup(&Re_pat13, pat);
+ re_setup(&Re_pat13, pat, ignore_case);
}
#endif /* NO_REGEX */
}
@@ -1204,13 +1201,18 @@ print_list(FILEDESC *list, int lev)
* Initialize regular expression pattern.
*/
static void
-re_setup(struct re *rx, const char *pattern)
+re_setup(struct re *rx, const char *pattern, bool ignore_case)
{
- int code;
+ int code, flags;
char errbuf[1024];
assert(!rx->valid);
- code = regcomp(&rx->regex, pattern, REG_EXTENDED);
+
+ flags = REG_EXTENDED | REG_NOSUB;
+ if (ignore_case) {
+ flags |= REG_ICASE;
+ }
+ code = regcomp(&rx->regex, pattern, flags);
if (code != 0) {
regerror(code, &rx->regex, errbuf, sizeof(errbuf));
@@ -1241,46 +1243,6 @@ re_match(struct re *rx, const char *string)
}
/*
- * conv_pat:
- * Convert the pattern to an ignore-case equivalent.
- */
-static char *
-conv_pat(char *orig)
-{
- char *sp;
- unsigned int cnt;
- char *new;
-
- cnt = 1; /* allow for '\0' */
- for (sp = orig; *sp != '\0'; sp++)
- if (isalpha((unsigned char)*sp))
- cnt += 4;
- else
- cnt++;
- if ((new = malloc(cnt)) == NULL)
- err(1, NULL);
-
- for (sp = new; *orig != '\0'; orig++) {
- if (islower((unsigned char)*orig)) {
- *sp++ = '[';
- *sp++ = *orig;
- *sp++ = toupper((unsigned char)*orig);
- *sp++ = ']';
- }
- else if (isupper((unsigned char)*orig)) {
- *sp++ = '[';
- *sp++ = *orig;
- *sp++ = tolower((unsigned char)*orig);
- *sp++ = ']';
- }
- else
- *sp++ = *orig;
- }
- *sp = '\0';
- return new;
-}
-
-/*
* find_matches:
* Find all the fortunes which match the pattern we've been given.
*/