1/* $Id: asprintf.c,v 1.1.1.2 2011/08/17 18:40:06 jmmv Exp $ */ 2 3/* 4 * Copyright (c) 2006 Nicholas Marriott <nicm@users.sourceforge.net> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19#include <stdarg.h> 20#include <stdio.h> 21#ifdef HAVE_STDINT_H 22#include <stdint.h> 23#else 24#include <inttypes.h> 25#endif 26#include <string.h> 27 28#include "tmux.h" 29 30int 31asprintf(char **ret, const char *fmt, ...) 32{ 33 va_list ap; 34 int n; 35 36 va_start(ap, fmt); 37 n = vasprintf(ret, fmt, ap); 38 va_end(ap); 39 40 return (n); 41} 42 43int 44vasprintf(char **ret, const char *fmt, va_list ap) 45{ 46 int n; 47 48 if ((n = vsnprintf(NULL, 0, fmt, ap)) < 0) 49 goto error; 50 51 *ret = xmalloc(n + 1); 52 if ((n = vsnprintf(*ret, n + 1, fmt, ap)) < 0) { 53 xfree(*ret); 54 goto error; 55 } 56 57 return (n); 58 59error: 60 *ret = NULL; 61 return (-1); 62} 63