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