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

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@.
This commit is contained in:
schwarze 2017-07-12 11:36:22 +00:00
parent 65b5242e85
commit 295a164a8e
2 changed files with 30 additions and 3 deletions

View File

@ -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/*

View File

@ -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;
@ -191,6 +195,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)
{
@ -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;