1 |
/* Like vsprintf but provides a pointer to malloc'd storage, which must |
/* Like vsprintf but provides a pointer to malloc'd storage, which must |
2 |
be freed by the caller. |
be freed by the caller. |
3 |
Copyright (C) 1994 Free Software Foundation, Inc. |
Copyright (C) 1994, 2003 Free Software Foundation, Inc. |
4 |
|
|
5 |
This file is part of the libiberty library. |
This file is part of the libiberty library. |
6 |
Libiberty is free software; you can redistribute it and/or |
Libiberty is free software; you can redistribute it and/or |
27 |
#else |
#else |
28 |
#include <varargs.h> |
#include <varargs.h> |
29 |
#endif |
#endif |
30 |
|
#if !defined (va_copy) && defined (__va_copy) |
31 |
|
# define va_copy(d,s) __va_copy((d),(s)) |
32 |
|
#endif |
33 |
#include <stdio.h> |
#include <stdio.h> |
34 |
#ifdef HAVE_STRING_H |
#ifdef HAVE_STRING_H |
35 |
#include <string.h> |
#include <string.h> |
55 |
of the buffer needed, allocate memory with @code{malloc}, and store a |
of the buffer needed, allocate memory with @code{malloc}, and store a |
56 |
pointer to the allocated memory in @code{*@var{resptr}}. The value |
pointer to the allocated memory in @code{*@var{resptr}}. The value |
57 |
returned is the same as @code{vsprintf} would return. If memory could |
returned is the same as @code{vsprintf} would return. If memory could |
58 |
not be allocated, zero is returned and @code{NULL} is stored in |
not be allocated, minus one is returned and @code{NULL} is stored in |
59 |
@code{*@var{resptr}}. |
@code{*@var{resptr}}. |
60 |
|
|
61 |
@end deftypefn |
@end deftypefn |
62 |
|
|
63 |
*/ |
*/ |
64 |
|
|
65 |
static int int_vasprintf PARAMS ((char **, const char *, va_list *)); |
static int int_vasprintf PARAMS ((char **, const char *, va_list)); |
66 |
|
|
67 |
static int |
static int |
68 |
int_vasprintf (result, format, args) |
int_vasprintf (result, format, args) |
69 |
char **result; |
char **result; |
70 |
const char *format; |
const char *format; |
71 |
va_list *args; |
va_list args; |
72 |
{ |
{ |
73 |
const char *p = format; |
const char *p = format; |
74 |
/* Add one to make sure that it is never zero, which might cause malloc |
/* Add one to make sure that it is never zero, which might cause malloc |
76 |
int total_width = strlen (format) + 1; |
int total_width = strlen (format) + 1; |
77 |
va_list ap; |
va_list ap; |
78 |
|
|
79 |
memcpy ((PTR) &ap, (PTR) args, sizeof (va_list)); |
#ifdef va_copy |
80 |
|
va_copy (ap, args); |
81 |
|
#else |
82 |
|
memcpy ((PTR) &ap, (PTR) &args, sizeof (va_list)); |
83 |
|
#endif |
84 |
|
|
85 |
while (*p != '\0') |
while (*p != '\0') |
86 |
{ |
{ |
142 |
p++; |
p++; |
143 |
} |
} |
144 |
} |
} |
145 |
|
#ifdef va_copy |
146 |
|
va_end (ap); |
147 |
|
#endif |
148 |
#ifdef TEST |
#ifdef TEST |
149 |
global_total_width = total_width; |
global_total_width = total_width; |
150 |
#endif |
#endif |
151 |
*result = malloc (total_width); |
*result = (char *) malloc (total_width); |
152 |
if (*result != NULL) |
if (*result != NULL) |
153 |
return vsprintf (*result, format, *args); |
return vsprintf (*result, format, args); |
154 |
else |
else |
155 |
return 0; |
return -1; |
156 |
} |
} |
157 |
|
|
158 |
int |
int |
165 |
va_list args; |
va_list args; |
166 |
#endif |
#endif |
167 |
{ |
{ |
168 |
return int_vasprintf (result, format, &args); |
return int_vasprintf (result, format, args); |
169 |
} |
} |
170 |
|
|
171 |
#ifdef TEST |
#ifdef TEST |