Commit 1e8fb0de authored by Ondřej Surý's avatar Ondřej Surý

contrib: replace sprintf_alloc with a simple wrapper around vasprintf

parent c3893d3b
......@@ -482,7 +482,12 @@ AC_HEADER_RESOLV
AC_CHECK_HEADERS_ONCE([cap-ng.h netinet/in_systm.h pthread_np.h signal.h sys/time.h sys/wait.h sys/uio.h])
# Checks for library functions.
AC_CHECK_FUNCS([clock_gettime gettimeofday fgetln getline madvise malloc_trim poll posix_memalign pthread_setaffinity_np regcomp setgroups strlcat strlcpy initgroups accept4])
AC_CHECK_FUNCS([clock_gettime gettimeofday fgetln getline madvise malloc_trim poll \
posix_memalign pthread_setaffinity_np regcomp setgroups strlcat strlcpy \
initgroups accept4])
AC_CHECK_FUNC([vasprintf], [], [
AC_MSG_ERROR([vasprintf support in the libc is required])])
# Check for be64toh function
AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <endian.h>]], [[return be64toh(0);]])],
......
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -34,40 +34,17 @@ uint8_t *memdup(const uint8_t *data, size_t data_size)
char *sprintf_alloc(const char *fmt, ...)
{
int size = 100;
char *p = NULL, *np = NULL;
char *strp = NULL;
va_list ap;
if ((p = malloc(size)) == NULL)
return NULL;
va_start(ap, fmt);
int ret = vasprintf(&strp, fmt, ap);
va_end(ap);
while (1) {
/* Try to print in the allocated space. */
va_start(ap, fmt);
int n = vsnprintf(p, size, fmt, ap);
va_end(ap);
/* If that worked, return the string. */
if (n > -1 && n < size)
return p;
/* Else try again with more space. */
if (n > -1) { /* glibc 2.1 */
size = n+1; /* precisely what is needed */
} else { /* glibc 2.0 */
size *= 2; /* twice the old size */
}
if ((np = realloc (p, size)) == NULL) {
free(p);
return NULL;
} else {
p = np;
}
if (ret < 0) {
return NULL;
}
/* Should never get here. */
return p;
return strp;
}
char *strcdup(const char *s1, const char *s2)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment