2010-08-08 10:06:34 +02:00
|
|
|
#include <platform.h>
|
2014-03-15 19:29:11 +01:00
|
|
|
#include <stdio.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2012-05-29 19:45:46 +02:00
|
|
|
#include <stdarg.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
#include "bsdstring.h"
|
|
|
|
|
2015-05-15 06:43:47 +02:00
|
|
|
int wrptr(char **ptr, size_t * size, size_t bytes)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (bytes == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-15 20:56:43 +02:00
|
|
|
if (bytes <= *size) {
|
2015-01-30 20:37:14 +01:00
|
|
|
*ptr += bytes;
|
|
|
|
*size -= bytes;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
*ptr += *size;
|
|
|
|
*size = 0;
|
2015-05-24 14:01:37 +02:00
|
|
|
return ERANGE;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2012-05-29 19:45:46 +02:00
|
|
|
#ifndef HAVE_STRLCPY
|
|
|
|
#define HAVE_STRLCPY
|
|
|
|
size_t strlcpy(char *dst, const char *src, size_t siz)
|
2011-03-07 08:02:35 +01:00
|
|
|
{ /* copied from OpenBSD source code */
|
2015-01-30 20:37:14 +01:00
|
|
|
register char *d = dst;
|
|
|
|
register const char *s = src;
|
|
|
|
register size_t n = siz;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-06-19 13:17:01 +02:00
|
|
|
assert(src && dst);
|
2015-01-30 20:37:14 +01:00
|
|
|
/* Copy as many bytes as will fit */
|
|
|
|
if (n != 0 && --n != 0) {
|
|
|
|
do {
|
|
|
|
if ((*d++ = *s++) == 0)
|
|
|
|
break;
|
|
|
|
} while (--n != 0);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
/* Not enough room in dst, add NUL and traverse rest of src */
|
|
|
|
if (n == 0) {
|
|
|
|
if (siz != 0)
|
|
|
|
*d = '\0'; /* NUL-terminate dst */
|
|
|
|
while (*s++);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
return (s - src - 1); /* count does not include NUL */
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2012-05-29 19:45:46 +02:00
|
|
|
#endif
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2012-05-29 19:45:46 +02:00
|
|
|
#ifndef HAVE_STRLCAT
|
|
|
|
#define HAVE_STRLCAT
|
|
|
|
size_t strlcat(char *dst, const char *src, size_t siz)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
register char *d = dst;
|
|
|
|
register const char *s = src;
|
|
|
|
register size_t n = siz;
|
|
|
|
size_t dlen;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
/* Find the end of dst and adjust bytes left but don't go past end */
|
|
|
|
while (*d != '\0' && n-- != 0)
|
|
|
|
d++;
|
|
|
|
dlen = d - dst;
|
|
|
|
n = siz - dlen;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
if (n == 0)
|
|
|
|
return (dlen + strlen(s));
|
|
|
|
while (*s != '\0') {
|
|
|
|
if (n != 1) {
|
|
|
|
*d++ = *s;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
s++;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
*d = '\0';
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
return (dlen + (s - src)); /* count does not include NUL */
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
#endif
|
2012-05-29 19:45:46 +02:00
|
|
|
|
|
|
|
#ifndef HAVE_SLPRINTF
|
|
|
|
#define HAVE_SLPRINTF
|
|
|
|
size_t slprintf(char * dst, size_t size, const char * format, ...)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
va_list args;
|
|
|
|
int result;
|
2012-05-29 19:45:46 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
va_start(args, format);
|
|
|
|
result = vsnprintf(dst, size, format, args);
|
|
|
|
if (result < 0 || result >= (int)size) {
|
|
|
|
dst[size - 1] = '\0';
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
va_start(args, format);
|
|
|
|
va_end(args);
|
2012-05-29 19:45:46 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
return (size_t)result;
|
2012-05-29 19:45:46 +02:00
|
|
|
}
|
|
|
|
#endif
|