1294109Sbapt#include "config.h" 2294109Sbapt 3294109Sbapt#if HAVE_VASPRINTF 4294109Sbapt 5294109Sbaptint dummy; 6294109Sbapt 7294109Sbapt#else 8294109Sbapt 9294109Sbapt/* $Id: compat_vasprintf.c,v 1.3 2015/10/06 18:32:19 schwarze Exp $ */ 10294109Sbapt/* 11294109Sbapt * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org> 12294109Sbapt * 13294109Sbapt * Permission to use, copy, modify, and distribute this software for any 14294109Sbapt * purpose with or without fee is hereby granted, provided that the above 15294109Sbapt * copyright notice and this permission notice appear in all copies. 16294109Sbapt * 17294109Sbapt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 18294109Sbapt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 19294109Sbapt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 20294109Sbapt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 21294109Sbapt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 22294109Sbapt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 23294109Sbapt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 24294109Sbapt * 25294109Sbapt * This fallback implementation is not efficient: 26294109Sbapt * It does the formatting twice. 27294109Sbapt * Short of fiddling with the unknown internals of the system's 28294109Sbapt * printf(3) or completely reimplementing printf(3), i can't think 29294109Sbapt * of another portable solution. 30294109Sbapt */ 31294109Sbapt 32294109Sbapt#include <stdarg.h> 33294109Sbapt#include <stdio.h> 34294109Sbapt#include <stdlib.h> 35294109Sbapt 36294109Sbaptint 37294109Sbaptvasprintf(char **ret, const char *format, va_list ap) 38294109Sbapt{ 39294109Sbapt char buf[2]; 40294109Sbapt va_list ap2; 41294109Sbapt int sz; 42294109Sbapt 43294109Sbapt va_copy(ap2, ap); 44294109Sbapt sz = vsnprintf(buf, sizeof(buf), format, ap2); 45294109Sbapt va_end(ap2); 46294109Sbapt 47294109Sbapt if (sz != -1 && (*ret = malloc(sz + 1)) != NULL) { 48294109Sbapt if (vsnprintf(*ret, sz + 1, format, ap) == sz) 49294109Sbapt return sz; 50294109Sbapt free(*ret); 51294109Sbapt } 52294109Sbapt *ret = NULL; 53294109Sbapt return -1; 54294109Sbapt} 55294109Sbapt 56294109Sbapt#endif 57