Module Name: src Committed By: christos Date: Fri Oct 13 00:11:56 UTC 2017
Modified Files: src/usr.bin/fmt: buffer.h fmt.c Log Message: Wse wide functions to avoid file corruption. Q+D because it does not use wcwidth(). To generate a diff of this commit: cvs rdiff -u -r1.4 -r1.5 src/usr.bin/fmt/buffer.h cvs rdiff -u -r1.32 -r1.33 src/usr.bin/fmt/fmt.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/fmt/buffer.h diff -u src/usr.bin/fmt/buffer.h:1.4 src/usr.bin/fmt/buffer.h:1.5 --- src/usr.bin/fmt/buffer.h:1.4 Mon Apr 28 16:24:12 2008 +++ src/usr.bin/fmt/buffer.h Thu Oct 12 20:11:56 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: buffer.h,v 1.4 2008/04/28 20:24:12 martin Exp $ */ +/* $NetBSD: buffer.h,v 1.5 2017/10/13 00:11:56 christos Exp $ */ /*- * Copyright (c) 2005 The NetBSD Foundation, Inc. @@ -32,19 +32,20 @@ #include <stddef.h> #include <stdio.h> #include <stdlib.h> +#include <wchar.h> #include <err.h> #define BUF_SIZE BUFSIZ struct buffer { - char *ptr; - char *bptr; - char *eptr; + wchar_t *ptr; + wchar_t *bptr; + wchar_t *eptr; }; static void buf_init(struct buffer *buf) { - buf->ptr = buf->bptr = malloc(BUF_SIZE); + buf->ptr = buf->bptr = calloc(BUF_SIZE, sizeof(*buf->ptr)); if (buf->ptr == NULL) err(1, "Cannot allocate buffer"); buf->eptr = buf->ptr + BUF_SIZE; @@ -62,7 +63,7 @@ buf_grow(struct buffer *buf, size_t mins ptrdiff_t diff; size_t len = (buf->eptr - buf->bptr) + (minsize > BUF_SIZE ? minsize : BUF_SIZE); - char *nptr = realloc(buf->bptr, len); + wchar_t *nptr = realloc(buf->bptr, len * sizeof(*buf->ptr)); if (nptr == NULL) err(1, "Cannot grow buffer"); @@ -79,7 +80,7 @@ buf_grow(struct buffer *buf, size_t mins } static inline void -buf_putc(struct buffer *buf, char c) +buf_putc(struct buffer *buf, wchar_t c) { if (buf->ptr >= buf->eptr) buf_grow(buf, 1); @@ -92,7 +93,7 @@ buf_reset(struct buffer *buf) buf->ptr = buf->bptr; } -static inline char +static inline wchar_t buf_unputc(struct buffer *buf) { return buf->ptr > buf->bptr ? *--buf->ptr : '\0'; Index: src/usr.bin/fmt/fmt.c diff -u src/usr.bin/fmt/fmt.c:1.32 src/usr.bin/fmt/fmt.c:1.33 --- src/usr.bin/fmt/fmt.c:1.32 Sat Jun 30 17:31:15 2012 +++ src/usr.bin/fmt/fmt.c Thu Oct 12 20:11:56 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: fmt.c,v 1.32 2012/06/30 21:31:15 christos Exp $ */ +/* $NetBSD: fmt.c,v 1.33 2017/10/13 00:11:56 christos Exp $ */ /* * Copyright (c) 1980, 1993 @@ -39,10 +39,10 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 19 #if 0 static char sccsid[] = "@(#)fmt.c 8.1 (Berkeley) 7/20/93"; #endif -__RCSID("$NetBSD: fmt.c,v 1.32 2012/06/30 21:31:15 christos Exp $"); +__RCSID("$NetBSD: fmt.c,v 1.33 2017/10/13 00:11:56 christos Exp $"); #endif /* not lint */ -#include <ctype.h> +#include <wctype.h> #include <locale.h> #include <stdio.h> #include <stdlib.h> @@ -51,6 +51,7 @@ __RCSID("$NetBSD: fmt.c,v 1.32 2012/06/3 #include <err.h> #include <limits.h> #include <string.h> +#include <locale.h> #include "buffer.h" /* @@ -74,21 +75,21 @@ static int mark; /* Last place we saw a static int center; static struct buffer outbuf; -static const char *headnames[] = {"To", "Subject", "Cc", 0}; +static const wchar_t *headnames[] = { L"To", L"Subject", L"Cc", NULL }; static void usage(void) __dead; static int getnum(const char *, const char *, size_t *, int); static void fmt(FILE *); -static int ispref(const char *, const char *); +static int ispref(const wchar_t *, const wchar_t *); static void leadin(void); static void oflush(void); -static void pack(const char *, size_t); +static void pack(const wchar_t *, size_t); static void prefix(const struct buffer *, int); -static void split(const char *, int); +static void split(const wchar_t *, int); static void tabulate(struct buffer *); -int ishead(const char *); +int ishead(const wchar_t *); /* * Drive the whole formatter by managing input files. Also, @@ -210,19 +211,20 @@ static void fmt(FILE *fi) { struct buffer lbuf, cbuf; - char *cp, *cp2; - int c, add_space; + wchar_t *cp, *cp2; + wint_t c; + int add_space; size_t len, col, i; if (center) { for (;;) { - cp = fgetln(fi, &len); + cp = fgetwln(fi, &len); if (!cp) return; /* skip over leading space */ while (len > 0) { - if (!isspace((unsigned char)*cp)) + if (!iswspace(*cp)) break; cp++; len--; @@ -230,55 +232,55 @@ fmt(FILE *fi) /* clear trailing space */ while (len > 0) { - if (!isspace((unsigned char)cp[len-1])) + if (!iswspace((unsigned char)cp[len-1])) break; len--; } if (len == 0) { /* blank line */ - (void)putchar('\n'); + (void)putwchar(L'\n'); continue; } if (goal_length > len) { for (i = 0; i < (goal_length - len) / 2; i++) { - (void)putchar(' '); + (void)putwchar(L' '); } } for (i = 0; i < len; i++) { - (void)putchar(cp[i]); + (void)putwchar(cp[i]); } - (void)putchar('\n'); + (void)putwchar(L'\n'); } } buf_init(&lbuf); buf_init(&cbuf); - c = getc(fi); + c = getwc(fi); - while (c != EOF) { + while (c != WEOF) { /* * Collect a line, doing ^H processing. * Leave tabs for now. */ buf_reset(&lbuf); - while (c != '\n' && c != EOF) { + while (c != '\n' && c != WEOF) { if (c == '\b') { (void)buf_unputc(&lbuf); - c = getc(fi); + c = getwc(fi); continue; } - if(!(isprint(c) || c == '\t' || c >= 160)) { - c = getc(fi); + if(!(iswprint(c) || c == '\t' || c >= 160)) { + c = getwc(fi); continue; } buf_putc(&lbuf, c); - c = getc(fi); + c = getwc(fi); } buf_putc(&lbuf, '\0'); (void)buf_unputc(&lbuf); - add_space = c != EOF; + add_space = c != WEOF; /* * Expand tabs on the way. @@ -307,8 +309,8 @@ fmt(FILE *fi) buf_putc(&cbuf, '\0'); (void)buf_unputc(&cbuf); prefix(&cbuf, add_space); - if (c != EOF) - c = getc(fi); + if (c != WEOF) + c = getwc(fi); } buf_end(&cbuf); buf_end(&lbuf); @@ -324,14 +326,14 @@ fmt(FILE *fi) static void prefix(const struct buffer *buf, int add_space) { - const char *cp; - const char **hp; + const wchar_t *cp; + const wchar_t **hp; size_t np; int h; if (buf->ptr == buf->bptr) { oflush(); - (void)putchar('\n'); + (void)putwchar(L'\n'); return; } for (cp = buf->bptr; *cp == ' '; cp++) @@ -376,9 +378,9 @@ prefix(const struct buffer *buf, int add * line packer. */ static void -split(const char line[], int add_space) +split(const wchar_t line[], int add_space) { - const char *cp; + const wchar_t *cp; struct buffer word; size_t wlen; @@ -393,7 +395,7 @@ split(const char line[], int add_space) * space. */ while (*cp && *cp != ' ') { - if (*cp == '\\' && isspace((unsigned char)cp[1])) + if (*cp == '\\' && iswspace(cp[1])) buf_putc(&word, *cp++); buf_putc(&word, *cp++); wlen++; @@ -443,9 +445,9 @@ split(const char line[], int add_space) */ static void -pack(const char *word, size_t wlen) +pack(const wchar_t *word, size_t wlen) { - const char *cp; + const wchar_t *cp; size_t s, t; if (outbuf.bptr == outbuf.ptr) @@ -496,7 +498,7 @@ oflush(void) static void tabulate(struct buffer *buf) { - char *cp; + wchar_t *cp; size_t b, t; /* @@ -516,15 +518,15 @@ tabulate(struct buffer *buf) b = b % 8; if (t > 0) do - (void)putchar('\t'); + (void)putwchar(L'\t'); while (--t); if (b > 0) do - (void)putchar(' '); + (void)putwchar(L' '); while (--b); while (*cp) - (void)putchar(*cp++); - (void)putchar('\n'); + (void)putwchar(*cp++); + (void)putwchar(L'\n'); } /* @@ -546,7 +548,7 @@ leadin(void) * Is s1 a prefix of s2?? */ static int -ispref(const char *s1, const char *s2) +ispref(const wchar_t *s1, const wchar_t *s2) { while (*s1++ == *s2)