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