1
0
mirror of https://github.com/openbsd/src.git synced 2025-01-10 06:47:55 -08:00

fix searching and display of offensive fortunes.

now, if you want to find an offensive fortune about (e.g.) meat,
you can simply run:
$ fortune -o -m meat
This is much easier than the previous requirement to run a command like:
$ fortune -o -m `echo meat | rot13` | rot13
requested by guenther phessler
This commit is contained in:
tedu 2015-02-06 09:55:01 +00:00
parent e5d5fd76cd
commit 3619beb36f

View File

@ -1,4 +1,4 @@
/* $OpenBSD: fortune.c,v 1.32 2014/11/16 04:49:48 guenther Exp $ */
/* $OpenBSD: fortune.c,v 1.33 2015/02/06 09:55:01 tedu Exp $ */
/* $NetBSD: fortune.c,v 1.8 1995/03/23 08:28:40 cgd Exp $ */
/*-
@ -209,6 +209,19 @@ main(int ac, char *av[])
/* NOTREACHED */
}
void
rot13(char *p, size_t len)
{
while (len--) {
char ch = *p;
if (isupper(ch))
*p = 'A' + (ch - 'A' + 13) % 26;
else if (islower(ch))
*p = 'a' + (ch - 'a' + 13) % 26;
p++;
}
}
void
display(FILEDESC *fp)
{
@ -220,11 +233,7 @@ display(FILEDESC *fp)
for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
!STR_ENDSTRING(line, fp->tbl); Fort_len++) {
if (fp->tbl.str_flags & STR_ROTATED)
for (p = line; ch = *p; ++p)
if (isupper(ch))
*p = 'A' + (ch - 'A' + 13) % 26;
else if (islower(ch))
*p = 'a' + (ch - 'a' + 13) % 26;
rot13(line, strlen(line));
fputs(line, stdout);
}
(void) fflush(stdout);
@ -1319,6 +1328,8 @@ matches_in_list(FILEDESC *list)
sp += strlen(sp);
else {
*sp = '\0';
if (fp->tbl.str_flags & STR_ROTATED)
rot13(Fortbuf, sp - Fortbuf);
if (regexec(&regex, Fortbuf, 0, NULL, 0) == 0) {
printf("%c%c", fp->tbl.str_delim,
fp->tbl.str_delim);