diff options
author | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2017-07-12 11:36:23 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2017-07-12 11:36:23 +0000 |
commit | 7e20127c5abc15c0632ffd3b07cb427d2f356707 (patch) | |
tree | 8d60c5b69ffd3c4d3f6aac81857431d15faca1b9 /games/fortune | |
parent | 674a39490c64e2e8d3716dcb7dde4c5241255fc1 (diff) |
Inspect LC_CTYPE and if it isn't UTF-8, weed out bytes that are not
printable ASCII. That makes using UTF-8 in fortune datfiles safe.
Potential usefulness of UTF-8 in fortune datfiles noticed by bentley@.
OK tedu@ millert@.
Diffstat (limited to 'games/fortune')
-rw-r--r-- | games/fortune/fortune/fortune.6 | 15 | ||||
-rw-r--r-- | games/fortune/fortune/fortune.c | 18 |
2 files changed, 30 insertions, 3 deletions
diff --git a/games/fortune/fortune/fortune.6 b/games/fortune/fortune/fortune.6 index 57e65bed512..0e1e0793783 100644 --- a/games/fortune/fortune/fortune.6 +++ b/games/fortune/fortune/fortune.6 @@ -1,4 +1,4 @@ -.\" $OpenBSD: fortune.6,v 1.14 2015/09/25 17:37:23 schwarze Exp $ +.\" $OpenBSD: fortune.6,v 1.15 2017/07/12 11:36:22 schwarze Exp $ .\" .\" Copyright (c) 1985, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -32,7 +32,7 @@ .\" .\" @(#)fortune.6 8.3 (Berkeley) 4/19/94 .\" -.Dd $Mdocdate: September 25 2015 $ +.Dd $Mdocdate: July 12 2017 $ .Dt FORTUNE 6 .Os .Sh NAME @@ -177,6 +177,17 @@ the source code and a manual page for this utility can be found in .Pa /usr/src/games/fortune/strfile/ , if it exists. +.Sh ENVIRONMENT +.Bl -tag -width LC_CTYPE +.It Ev LC_CTYPE +The character encoding +.Xr locale 1 . +If unset or set to +.Qq C , +.Qq POSIX , +or an unsupported value, bytes that are not printable ASCII characters +are replaced with question marks in the output. +.El .Sh FILES .Bl -tag -width "/usr/share/games/fortune/*XX" -compact .It Pa /usr/share/games/fortune/* diff --git a/games/fortune/fortune/fortune.c b/games/fortune/fortune/fortune.c index aea8e691ebb..258626bcc39 100644 --- a/games/fortune/fortune/fortune.c +++ b/games/fortune/fortune/fortune.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fortune.c,v 1.58 2017/06/30 08:39:16 mestre Exp $ */ +/* $OpenBSD: fortune.c,v 1.59 2017/07/12 11:36:22 schwarze Exp $ */ /* $NetBSD: fortune.c,v 1.8 1995/03/23 08:28:40 cgd Exp $ */ /*- @@ -41,6 +41,7 @@ #include <err.h> #include <fcntl.h> #include <limits.h> +#include <locale.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -133,6 +134,7 @@ FILEDESC * void print_file_list(void); void print_list(FILEDESC *, int); void rot13(char *, size_t); +void sanitize(unsigned char *cp); void sum_noprobs(FILEDESC *); void sum_tbl(STRFILE *, STRFILE *); __dead void usage(void); @@ -148,6 +150,8 @@ regex_t regex; int main(int ac, char *av[]) { + setlocale(LC_CTYPE, ""); + if (pledge("stdio rpath", NULL) == -1) { perror("pledge"); return 1; @@ -192,6 +196,16 @@ rot13(char *p, size_t len) } void +sanitize(unsigned char *cp) +{ + if (MB_CUR_MAX > 1) + return; + for (; *cp != '\0'; cp++) + if (!isprint(*cp) && !isspace(*cp)) + *cp = '?'; +} + +void display(FILEDESC *fp) { char line[BUFSIZ]; @@ -202,6 +216,7 @@ display(FILEDESC *fp) !STR_ENDSTRING(line, fp->tbl); Fort_len++) { if (fp->tbl.str_flags & STR_ROTATED) rot13(line, strlen(line)); + sanitize(line); fputs(line, stdout); } (void) fflush(stdout); @@ -1189,6 +1204,7 @@ matches_in_list(FILEDESC *list) in_file = 1; } putchar('\n'); + sanitize(Fortbuf); (void) fwrite(Fortbuf, 1, (sp - Fortbuf), stdout); } sp = Fortbuf; |