]>
The Tcpdump Group git mirrors - libpcap/blob - missing/asprintf.c
5 #include "portability.h"
8 * vasprintf() and asprintf() for platforms with a C99-compliant
9 * snprintf() - so that, if you format into a 1-byte buffer, it
10 * will return how many characters it would have produced had
11 * it been given an infinite-sized buffer.
14 pcapint_vasprintf(char **strp
, const char *format
, va_list args
)
23 * XXX - the C99 standard says, in section 7.19.6.5 "The
26 * The snprintf function is equivalent to fprintf, except that
27 * the output is written into an array (specified by argument s)
28 * rather than to a stream. If n is zero, nothing is written,
29 * and s may be a null pointer. Otherwise, output characters
30 * beyond the n-1st are discarded rather than being written
31 * to the array, and a null character is written at the end
32 * of the characters actually written into the array.
36 * The snprintf function returns the number of characters that
37 * would have been written had n been sufficiently large, not
38 * counting the terminating null character, or a negative value
39 * if an encoding error occurred. Thus, the null-terminated
40 * output has been completely written if and only if the returned
41 * value is nonnegative and less than n.
43 * That doesn't make it entirely clear whether, if a null buffer
44 * pointer and a zero count are passed, it will return the number
45 * of characters that would have been written had a buffer been
48 * And, even if C99 *does*, in fact, say it has to work, it
49 * doesn't work in Solaris 8, for example - it returns -1 for
50 * NULL/0, but returns the correct character count for a 1-byte
53 * So we pass a one-character pointer in order to find out how
54 * many characters this format and those arguments will need
55 * without actually generating any more of those characters
58 * (The fact that it might happen to work with GNU libc or with
59 * various BSD libcs is completely uninteresting, as those tend
60 * to have asprintf() already and thus don't even *need* this
61 * code; this is for use in those UN*Xes that *don't* have
64 len
= vsnprintf(&buf
, sizeof buf
, format
, args
);
70 str
= malloc(str_size
);
75 ret
= vsnprintf(str
, str_size
, format
, args
);
83 * vsnprintf() shouldn't truncate the string, as we have
84 * allocated a buffer large enough to hold the string, so its
85 * return value should be the number of characters written.
91 pcapint_asprintf(char **strp
, const char *format
, ...)
96 va_start(args
, format
);
97 ret
= pcapint_vasprintf(strp
, format
, args
);