diff options
author | Damien Miller <djm@mindrot.org> | 2003-05-18 22:24:09 +1000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2003-05-18 22:24:09 +1000 |
commit | e323df6c4851b04386e747a009474f469fe97137 (patch) | |
tree | 2c4209855f9ed24545b1add919db8319b34ec992 /openbsd-compat/vis.c | |
parent | 0b8e9006d87075968dc402afa0f6ca359184b83b (diff) |
- (djm) Sync openbsd-compat/ with OpenBSD CVS head
Diffstat (limited to 'openbsd-compat/vis.c')
-rw-r--r-- | openbsd-compat/vis.c | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/openbsd-compat/vis.c b/openbsd-compat/vis.c index fc574139..0d15886f 100644 --- a/openbsd-compat/vis.c +++ b/openbsd-compat/vis.c @@ -34,7 +34,7 @@ #if !defined(HAVE_STRNVIS) #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: vis.c,v 1.8 2002/02/19 19:39:36 millert Exp $"; +static char rcsid[] = "$OpenBSD: vis.c,v 1.11 2003/05/14 05:16:43 pjanzen Exp $"; #endif /* LIBC_SCCS and not lint */ #include <ctype.h> @@ -47,8 +47,9 @@ static char rcsid[] = "$OpenBSD: vis.c,v 1.8 2002/02/19 19:39:36 millert Exp $"; ((flag & VIS_SP) == 0 && (c) == ' ') || \ ((flag & VIS_TAB) == 0 && (c) == '\t') || \ ((flag & VIS_NL) == 0 && (c) == '\n') || \ - ((flag & VIS_SAFE) && \ - ((c) == '\b' || (c) == '\007' || (c) == '\r'))) + ((flag & VIS_SAFE) && ((c) == '\b' || \ + (c) == '\007' || (c) == '\r' || \ + isgraph((u_char)(c))))) /* * vis - visually encode characters @@ -169,16 +170,20 @@ strvis(dst, src, flag) int strnvis(dst, src, siz, flag) - register char *dst; - register const char *src; + char *dst; + const char *src; size_t siz; int flag; { - register char c; + char c; char *start, *end; + char tbuf[5]; + int i; + i = 0; for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) { if (isvisible(c)) { + i = 1; *dst++ = c; if (c == '\\' && (flag & VIS_NOSLASH) == 0) { /* need space for the extra '\\' */ @@ -186,22 +191,25 @@ strnvis(dst, src, siz, flag) *dst++ = '\\'; else { dst--; + i = 2; break; } } src++; } else { - /* vis(3) requires up to 4 chars */ - if (dst + 3 < end) - dst = vis(dst, c, flag, *++src); - else + i = vis(tbuf, c, flag, *++src) - tbuf; + if (dst + i <= end) { + memcpy(dst, tbuf, i); + dst += i; + } else { + src--; break; + } } } - *dst = '\0'; - if (dst >= end) { - char tbuf[5]; - + if (siz > 0) + *dst = '\0'; + if (dst + i > end) { /* adjust return value for truncation */ while ((c = *src)) dst += vis(tbuf, c, flag, *++src) - tbuf; |