"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Robert Griebl | efd4983 | 2002-07-19 20:27:11 +0000 | [diff] [blame] | 2 | /* |
"Vladimir N. Oleynik" | 39a841c | 2005-09-29 16:18:57 +0000 | [diff] [blame] | 3 | Copyright (C) 2002,2005 Vladimir Oleynik <dzo@simtreas.ru> |
Robert Griebl | efd4983 | 2002-07-19 20:27:11 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <stdlib.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdarg.h> |
Manuel Novoa III | 4d0884a | 2002-09-12 14:52:26 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Robert Griebl | efd4983 | 2002-07-19 20:27:11 +0000 | [diff] [blame] | 10 | |
"Vladimir N. Oleynik" | 39a841c | 2005-09-29 16:18:57 +0000 | [diff] [blame] | 11 | char *bb_xasprintf(const char *format, ...) |
Robert Griebl | efd4983 | 2002-07-19 20:27:11 +0000 | [diff] [blame] | 12 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 13 | va_list p; |
| 14 | int r; |
"Vladimir N. Oleynik" | 39a841c | 2005-09-29 16:18:57 +0000 | [diff] [blame] | 15 | char *string_ptr; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 16 | |
Rob Landley | 1f305dc | 2006-03-09 22:21:20 +0000 | [diff] [blame] | 17 | #ifdef HAVE_GNU_EXTENSIONS |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 18 | va_start(p, format); |
"Vladimir N. Oleynik" | 39a841c | 2005-09-29 16:18:57 +0000 | [diff] [blame] | 19 | r = vasprintf(&string_ptr, format, p); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 20 | va_end(p); |
Rob Landley | 1f305dc | 2006-03-09 22:21:20 +0000 | [diff] [blame] | 21 | #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 Griebl | efd4983 | 2002-07-19 20:27:11 +0000 | [diff] [blame] | 30 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 31 | if (r < 0) { |
| 32 | bb_perror_msg_and_die("bb_xasprintf"); |
| 33 | } |
"Vladimir N. Oleynik" | 39a841c | 2005-09-29 16:18:57 +0000 | [diff] [blame] | 34 | return string_ptr; |
Robert Griebl | efd4983 | 2002-07-19 20:27:11 +0000 | [diff] [blame] | 35 | } |