1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/honeyd/vasprintf.c Tue Aug 28 18:35:30 2012 +0200 1.3 @@ -0,0 +1,133 @@ 1.4 +/* Like vsprintf but provides a pointer to malloc'd storage, which must 1.5 + be freed by the caller. 1.6 + Copyright (C) 1994, 2003 Free Software Foundation, Inc. 1.7 + 1.8 +This file is part of the libiberty library. 1.9 +Libiberty is free software; you can redistribute it and/or 1.10 +modify it under the terms of the GNU Library General Public 1.11 +License as published by the Free Software Foundation; either 1.12 +version 2 of the License, or (at your option) any later version. 1.13 + 1.14 +Libiberty is distributed in the hope that it will be useful, 1.15 +but WITHOUT ANY WARRANTY; without even the implied warranty of 1.16 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1.17 +Library General Public License for more details. 1.18 + 1.19 +You should have received a copy of the GNU Library General Public 1.20 +License along with libiberty; see the file COPYING.LIB. If 1.21 +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 1.22 +Boston, MA 02111-1307, USA. */ 1.23 + 1.24 +#include <stdarg.h> 1.25 +#include <stdio.h> 1.26 +#include <stdlib.h> 1.27 +#include <string.h> 1.28 +#include "vasprintf.h" 1.29 + 1.30 +/* 1.31 + 1.32 +@deftypefn Extension int vasprintf (char **@var{resptr}, const char *@var{format}, va_list @var{args}) 1.33 + 1.34 +Like @code{vsprintf}, but instead of passing a pointer to a buffer, 1.35 +you pass a pointer to a pointer. This function will compute the size 1.36 +of the buffer needed, allocate memory with @code{malloc}, and store a 1.37 +pointer to the allocated memory in @code{*@var{resptr}}. The value 1.38 +returned is the same as @code{vsprintf} would return. If memory could 1.39 +not be allocated, minus one is returned and @code{NULL} is stored in 1.40 +@code{*@var{resptr}}. 1.41 + 1.42 +@end deftypefn 1.43 + 1.44 +*/ 1.45 + 1.46 +static int int_vasprintf(char **, const char *, va_list); 1.47 + 1.48 +static int 1.49 +int_vasprintf (result, format, args) 1.50 + char **result; 1.51 + const char *format; 1.52 + va_list args; 1.53 +{ 1.54 + const char *p = format; 1.55 + /* Add one to make sure that it is never zero, which might cause malloc 1.56 + to return NULL. */ 1.57 + int total_width = strlen (format) + 1; 1.58 + va_list ap; 1.59 + 1.60 + memcpy ((void *) &ap, (const void *) &args, sizeof (va_list)); 1.61 + 1.62 + while (*p != '\0') 1.63 + { 1.64 + if (*p++ == '%') 1.65 + { 1.66 + while (strchr ("-+ #0", *p)) 1.67 + ++p; 1.68 + if (*p == '*') 1.69 + { 1.70 + ++p; 1.71 + total_width += abs (va_arg (ap, int)); 1.72 + } 1.73 + else 1.74 + total_width += strtoul (p, (char **) &p, 10); 1.75 + if (*p == '.') 1.76 + { 1.77 + ++p; 1.78 + if (*p == '*') 1.79 + { 1.80 + ++p; 1.81 + total_width += abs (va_arg (ap, int)); 1.82 + } 1.83 + else 1.84 + total_width += strtoul (p, (char **) &p, 10); 1.85 + } 1.86 + while (strchr ("hlL", *p)) 1.87 + ++p; 1.88 + /* Should be big enough for any format specifier except %s and floats. */ 1.89 + total_width += 30; 1.90 + switch (*p) 1.91 + { 1.92 + case 'd': 1.93 + case 'i': 1.94 + case 'o': 1.95 + case 'u': 1.96 + case 'x': 1.97 + case 'X': 1.98 + case 'c': 1.99 + (void) va_arg (ap, int); 1.100 + break; 1.101 + case 'f': 1.102 + case 'e': 1.103 + case 'E': 1.104 + case 'g': 1.105 + case 'G': 1.106 + (void) va_arg (ap, double); 1.107 + /* Since an ieee double can have an exponent of 307, we'll 1.108 + make the buffer wide enough to cover the gross case. */ 1.109 + total_width += 307; 1.110 + break; 1.111 + case 's': 1.112 + total_width += strlen (va_arg (ap, char *)); 1.113 + break; 1.114 + case 'p': 1.115 + case 'n': 1.116 + (void) va_arg (ap, char *); 1.117 + break; 1.118 + } 1.119 + p++; 1.120 + } 1.121 + } 1.122 + *result = (char *) malloc (total_width); 1.123 + if (*result != NULL) 1.124 + return vsprintf (*result, format, args); 1.125 + else 1.126 + return -1; 1.127 +} 1.128 + 1.129 +int 1.130 +vasprintf (result, format, args) 1.131 + char **result; 1.132 + const char *format; 1.133 + va_list args; 1.134 +{ 1.135 + return int_vasprintf (result, format, args); 1.136 +}