5 * \defgroup SnprintfReplacement modified from Sten Gunterberg's BUGTRAQ post of 22 Jul 1997
6 * --nathan bryant <bryant@cs.usm.maine.edu>
11 * \brief Replacements for snprintf() and vsnprintf()
13 * Use it only if you have the "spare" cycles needed to effectively
14 * do every snprintf operation twice! Why is that? Because everything
15 * is first vfprintf()'d to /dev/null to determine the number of bytes.
16 * Perhaps a bit slow for demanding applications on slow machines,
17 * no problem for a fast machine with some spare cycles.
19 * You don't have a /dev/null? Every Linux contains one for free!
21 * Because the format string is never even looked at, all current and
22 * possible future printf-conversions should be handled just fine.
24 * Written July 1997 by Sten Gunterberg (gunterberg@ergon.ch)
28 #include "webserver.h"
31 * \brief is it needed????
32 * \param fmt the formatstring?
33 * \param argp how many params?
35 static int needed(const char *fmt, va_list argp)
37 static FILE *sink = NULL;
40 * ok, there's a small race here that could result in the sink being
41 * opened more than once if we're threaded, but I'd rather ignore it than
42 * spend cycles synchronizing :-) */
45 if ((sink = fopen("/dev/null", "w")) == NULL) {
50 return vfprintf(sink, fmt, argp);
54 * \brief vsnprintf wrapper
55 * \param buf the output charbuffer
56 * \param max maximal size of the buffer
57 * \param fmt the formatstring (see man printf)
58 * \param argp the variable argument list
60 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp)
65 if ((p = malloc(needed(fmt, argp) + 1)) == NULL) {
66 lprintf(1, "vsnprintf: malloc failed, aborting\n");
69 if ((size = vsprintf(p, fmt, argp)) >= max)
79 * \brief snprintf wrapper
80 * \param buf the output charbuffer
81 * \param max maximal size of the buffer
82 * \param fmt the formatstring (see man printf)
83 * \param ... the variable argument list
85 int snprintf(char *buf, size_t max, const char *fmt,...)
91 bytes = vsnprintf(buf, max, fmt, argp);