blob: 51896ddd62e777924271e82428c12f1c3989b9a9 [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/*
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +00003 Copyright (C) 2002,2005 Vladimir Oleynik <dzo@simtreas.ru>
Robert Grieblefd49832002-07-19 20:27:11 +00004*/
5
6#include <stdlib.h>
7#include <stdio.h>
8#include <stdarg.h>
Manuel Novoa III 4d0884a2002-09-12 14:52:26 +00009#include "libbb.h"
Robert Grieblefd49832002-07-19 20:27:11 +000010
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +000011char *bb_xasprintf(const char *format, ...)
Robert Grieblefd49832002-07-19 20:27:11 +000012{
Manuel Novoa III cad53642003-03-19 09:13:01 +000013 va_list p;
14 int r;
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +000015 char *string_ptr;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000016
Rob Landley1f305dc2006-03-09 22:21:20 +000017#ifdef HAVE_GNU_EXTENSIONS
Manuel Novoa III cad53642003-03-19 09:13:01 +000018 va_start(p, format);
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +000019 r = vasprintf(&string_ptr, format, p);
Manuel Novoa III cad53642003-03-19 09:13:01 +000020 va_end(p);
Rob Landley1f305dc2006-03-09 22:21:20 +000021#else
22 va_start(p, format);
23 r = vsnprintf(NULL, 0, format, p);
24 va_end(p);
25 string_ptr = xmalloc(r+1);
26 va_start(p, format);
27 r = vsnprintf(string_ptr, r+1, format, p);
28 va_end(p);
29#endif
Robert Grieblefd49832002-07-19 20:27:11 +000030
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 if (r < 0) {
32 bb_perror_msg_and_die("bb_xasprintf");
33 }
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +000034 return string_ptr;
Robert Grieblefd49832002-07-19 20:27:11 +000035}