1 |
/* Like sprintf but provides a pointer to malloc'd storage, which must |
/* Like sprintf but provides a pointer to malloc'd storage, which must |
2 |
be freed by the caller. |
be freed by the caller. |
3 |
Copyright (C) 1997 Free Software Foundation, Inc. |
Copyright (C) 1997, 2003 Free Software Foundation, Inc. |
4 |
Contributed by Cygnus Solutions. |
Contributed by Cygnus Solutions. |
5 |
|
|
6 |
This file is part of the libiberty library. |
This file is part of the libiberty library. |
22 |
#include "ansidecl.h" |
#include "ansidecl.h" |
23 |
#include "libiberty.h" |
#include "libiberty.h" |
24 |
|
|
25 |
#if defined (ANSI_PROTOTYPES) || defined (ALMOST_STDC) |
#ifdef ANSI_PROTOTYPES |
|
#define USE_STDARG |
|
|
#endif |
|
|
|
|
|
#ifdef USE_STDARG |
|
26 |
#include <stdarg.h> |
#include <stdarg.h> |
27 |
#else |
#else |
28 |
#include <varargs.h> |
#include <varargs.h> |
29 |
#endif |
#endif |
30 |
|
|
31 |
/* VARARGS */ |
/* |
32 |
#ifdef USE_STDARG |
|
33 |
int |
@deftypefn Extension int asprintf (char **@var{resptr}, const char *@var{format}, ...) |
34 |
asprintf (char **buf, const char *fmt, ...) |
|
35 |
#else |
Like @code{sprintf}, but instead of passing a pointer to a buffer, you |
36 |
|
pass a pointer to a pointer. This function will compute the size of |
37 |
|
the buffer needed, allocate memory with @code{malloc}, and store a |
38 |
|
pointer to the allocated memory in @code{*@var{resptr}}. The value |
39 |
|
returned is the same as @code{sprintf} would return. If memory could |
40 |
|
not be allocated, minus one is returned and @code{NULL} is stored in |
41 |
|
@code{*@var{resptr}}. |
42 |
|
|
43 |
|
@end deftypefn |
44 |
|
|
45 |
|
*/ |
46 |
|
|
47 |
int |
int |
48 |
asprintf (buf, fmt, va_alist) |
asprintf VPARAMS ((char **buf, const char *fmt, ...)) |
|
char **buf; |
|
|
const char *fmt; |
|
|
va_dcl |
|
|
#endif |
|
49 |
{ |
{ |
50 |
int status; |
int status; |
51 |
va_list ap; |
VA_OPEN (ap, fmt); |
52 |
#ifdef USE_STDARG |
VA_FIXEDARG (ap, char **, buf); |
53 |
va_start (ap, fmt); |
VA_FIXEDARG (ap, const char *, fmt); |
|
#else |
|
|
va_start (ap); |
|
|
#endif |
|
54 |
status = vasprintf (buf, fmt, ap); |
status = vasprintf (buf, fmt, ap); |
55 |
va_end (ap); |
VA_CLOSE (ap); |
56 |
return status; |
return status; |
57 |
} |
} |