Solaris does not have vasnprintf()

This commit is contained in:
Lev Walkin 2017-10-05 23:07:22 -07:00
parent d28b45fd93
commit a06b1b4ddf
2 changed files with 36 additions and 0 deletions

View File

@ -244,6 +244,7 @@ AC_CHECK_FUNCS(mergesort)
AC_CHECK_FUNCS(mkstemps)
AC_CHECK_FUNCS(timegm)
AC_CHECK_DECLS(alloca strcasecmp)
AC_CHECK_DECLS(vasprintf)
AC_TRY_LINK_FUNC([symlink],[AC_DEFINE([HAVE_SYMLINK], 1, [Define to 1 if you have the symlink function.])])
dnl Use pandoc to generate manual pages.

View File

@ -5,9 +5,14 @@
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "config.h"
#include "asn1_buffer.h"
#ifndef HAVE_DECL_VASPRINTF
int vasprintf(char **ret, const char *fmt, va_list args);
#endif
/*
* Create and destroy the buffer.
*/
@ -134,3 +139,33 @@ int abuf_vprintf(abuf *ab, const char *fmt, va_list ap) {
return ret;
}
#ifndef HAVE_DECL_VASPRINTF
/* Solaris doesn't have vasprintf(3). */
int
vasprintf(char **ret, const char *fmt, va_list args) {
int actual_length = -1;
va_list copy;
va_copy(copy, args);
int suggested = vsnprintf(NULL, 0, fmt, args);
if(suggested >= 0) {
*ret = malloc(suggested + 1);
if(*ret) {
int actual_length = vsnprintf(*ret, suggested + 1, fmt, copy);
if(actual_length >= 0) {
assert(actual_length == suggested);
assert((*ret)[actual_length] == '\0');
} else {
free(*ret);
*ret = 0;
}
}
} else {
*ret = NULL;
assert(suggested >= 0); /* Can't function like this */
}
va_end(args);
return actual_length;
}
#endif