Add strnvis and strnunvis functions

Taken from OpenBSD.
This commit is contained in:
Guillem Jover
2009-10-24 00:15:57 +02:00
parent 5c078ce2f5
commit 04250f6a7c
4 changed files with 105 additions and 2 deletions

View File

@@ -257,6 +257,47 @@ strunvis(char *dst, const char *src)
return (dst - start);
}
ssize_t
strnunvis(char *dst, const char *src, size_t sz)
{
char c, p;
char *start = dst, *end = dst + sz - 1;
int state = 0;
if (sz > 0)
*end = '\0';
while ((c = *src++)) {
again:
switch (unvis(&p, c, &state, 0)) {
case UNVIS_VALID:
if (dst < end)
*dst = p;
dst++;
break;
case UNVIS_VALIDPUSH:
if (dst < end)
*dst = p;
dst++;
goto again;
case 0:
case UNVIS_NOCHAR:
break;
default:
if (dst <= end)
*dst = '\0';
return (-1);
}
}
if (unvis(&p, c, &state, UNVIS_END) == UNVIS_VALID) {
if (dst < end)
*dst = p;
dst++;
}
if (dst <= end)
*dst = '\0';
return (dst - start);
}
int
strunvisx(char *dst, const char *src, int flag)
{