summaryrefslogtreecommitdiff
path: root/openbsd-compat/vis.c
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat/vis.c')
-rw-r--r--openbsd-compat/vis.c45
1 files changed, 25 insertions, 20 deletions
diff --git a/openbsd-compat/vis.c b/openbsd-compat/vis.c
index fc5741390..e6a2ce98d 100644
--- a/openbsd-compat/vis.c
+++ b/openbsd-compat/vis.c
@@ -10,11 +10,7 @@
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the 11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software 13 * 3. Neither the name of the University nor the names of its contributors
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software 14 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission. 15 * without specific prior written permission.
20 * 16 *
@@ -30,14 +26,15 @@
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE. 27 * SUCH DAMAGE.
32 */ 28 */
33#include "config.h" 29#include "includes.h"
34#if !defined(HAVE_STRNVIS) 30#if !defined(HAVE_STRNVIS)
35 31
36#if defined(LIBC_SCCS) && !defined(lint) 32#if defined(LIBC_SCCS) && !defined(lint)
37static char rcsid[] = "$OpenBSD: vis.c,v 1.8 2002/02/19 19:39:36 millert Exp $"; 33static char rcsid[] = "$OpenBSD: vis.c,v 1.12 2003/06/02 20:18:35 millert Exp $";
38#endif /* LIBC_SCCS and not lint */ 34#endif /* LIBC_SCCS and not lint */
39 35
40#include <ctype.h> 36#include <ctype.h>
37#include <string.h>
41 38
42#include "vis.h" 39#include "vis.h"
43 40
@@ -47,8 +44,9 @@ static char rcsid[] = "$OpenBSD: vis.c,v 1.8 2002/02/19 19:39:36 millert Exp $";
47 ((flag & VIS_SP) == 0 && (c) == ' ') || \ 44 ((flag & VIS_SP) == 0 && (c) == ' ') || \
48 ((flag & VIS_TAB) == 0 && (c) == '\t') || \ 45 ((flag & VIS_TAB) == 0 && (c) == '\t') || \
49 ((flag & VIS_NL) == 0 && (c) == '\n') || \ 46 ((flag & VIS_NL) == 0 && (c) == '\n') || \
50 ((flag & VIS_SAFE) && \ 47 ((flag & VIS_SAFE) && ((c) == '\b' || \
51 ((c) == '\b' || (c) == '\007' || (c) == '\r'))) 48 (c) == '\007' || (c) == '\r' || \
49 isgraph((u_char)(c)))))
52 50
53/* 51/*
54 * vis - visually encode characters 52 * vis - visually encode characters
@@ -169,16 +167,20 @@ strvis(dst, src, flag)
169 167
170int 168int
171strnvis(dst, src, siz, flag) 169strnvis(dst, src, siz, flag)
172 register char *dst; 170 char *dst;
173 register const char *src; 171 const char *src;
174 size_t siz; 172 size_t siz;
175 int flag; 173 int flag;
176{ 174{
177 register char c; 175 char c;
178 char *start, *end; 176 char *start, *end;
177 char tbuf[5];
178 int i;
179 179
180 i = 0;
180 for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) { 181 for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
181 if (isvisible(c)) { 182 if (isvisible(c)) {
183 i = 1;
182 *dst++ = c; 184 *dst++ = c;
183 if (c == '\\' && (flag & VIS_NOSLASH) == 0) { 185 if (c == '\\' && (flag & VIS_NOSLASH) == 0) {
184 /* need space for the extra '\\' */ 186 /* need space for the extra '\\' */
@@ -186,22 +188,25 @@ strnvis(dst, src, siz, flag)
186 *dst++ = '\\'; 188 *dst++ = '\\';
187 else { 189 else {
188 dst--; 190 dst--;
191 i = 2;
189 break; 192 break;
190 } 193 }
191 } 194 }
192 src++; 195 src++;
193 } else { 196 } else {
194 /* vis(3) requires up to 4 chars */ 197 i = vis(tbuf, c, flag, *++src) - tbuf;
195 if (dst + 3 < end) 198 if (dst + i <= end) {
196 dst = vis(dst, c, flag, *++src); 199 memcpy(dst, tbuf, i);
197 else 200 dst += i;
201 } else {
202 src--;
198 break; 203 break;
204 }
199 } 205 }
200 } 206 }
201 *dst = '\0'; 207 if (siz > 0)
202 if (dst >= end) { 208 *dst = '\0';
203 char tbuf[5]; 209 if (dst + i > end) {
204
205 /* adjust return value for truncation */ 210 /* adjust return value for truncation */
206 while ((c = *src)) 211 while ((c = *src))
207 dst += vis(tbuf, c, flag, *++src) - tbuf; 212 dst += vis(tbuf, c, flag, *++src) - tbuf;