blob: 2bef0b59d617ea721a186e0ec530e323f3aa8d54 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Robert Grieblefd49832002-07-19 20:27:11 +00002/*
"Robert P. J. Day"5d8843e2006-07-10 11:41:19 +00003 * Copyright (C) 2002,2005 Vladimir Oleynik <dzo@simtreas.ru>
4 *
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6 */
Robert Grieblefd49832002-07-19 20:27:11 +00007
8#include <stdlib.h>
9#include <stdio.h>
10#include <stdarg.h>
Manuel Novoa III 4d0884a2002-09-12 14:52:26 +000011#include "libbb.h"
Robert Grieblefd49832002-07-19 20:27:11 +000012
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +000013char *bb_xasprintf(const char *format, ...)
Robert Grieblefd49832002-07-19 20:27:11 +000014{
Manuel Novoa III cad53642003-03-19 09:13:01 +000015 va_list p;
16 int r;
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +000017 char *string_ptr;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000018
Rob Landley1f305dc2006-03-09 22:21:20 +000019#ifdef HAVE_GNU_EXTENSIONS
Manuel Novoa III cad53642003-03-19 09:13:01 +000020 va_start(p, format);
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +000021 r = vasprintf(&string_ptr, format, p);
Manuel Novoa III cad53642003-03-19 09:13:01 +000022 va_end(p);
Rob Landley1f305dc2006-03-09 22:21:20 +000023#else
24 va_start(p, format);
25 r = vsnprintf(NULL, 0, format, p);
26 va_end(p);
27 string_ptr = xmalloc(r+1);
28 va_start(p, format);
29 r = vsnprintf(string_ptr, r+1, format, p);
30 va_end(p);
31#endif
Robert Grieblefd49832002-07-19 20:27:11 +000032
Manuel Novoa III cad53642003-03-19 09:13:01 +000033 if (r < 0) {
34 bb_perror_msg_and_die("bb_xasprintf");
35 }
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +000036 return string_ptr;
Robert Grieblefd49832002-07-19 20:27:11 +000037}