4 * modified from Sten Gunterberg's BUGTRAQ post of 22 Jul 1997
5 * --nathan bryant <bryant@cs.usm.maine.edu>
10 * Replacements for snprintf() and vsnprintf()
12 * Use it only if you have the "spare" cycles needed to effectively
13 * do every snprintf operation twice! Why is that? Because everything
14 * is first vfprintf()'d to /dev/null to determine the number of bytes.
15 * Perhaps a bit slow for demanding applications on slow machines,
16 * no problem for a fast machine with some spare cycles.
18 * You don't have a /dev/null? Every Linux contains one for free!
20 * Because the format string is never even looked at, all current and
21 * possible future printf-conversions should be handled just fine.
23 * Written July 1997 by Sten Gunterberg (gunterberg@ergon.ch)
30 #include "webserver.h"
32 static int needed(const char *fmt, va_list argp)
34 static FILE *sink = NULL;
36 /* ok, there's a small race here that could result in the sink being
37 * opened more than once if we're threaded, but I'd rather ignore it than
38 * spend cycles synchronizing :-) */
41 if ((sink = fopen("/dev/null", "w")) == NULL) {
46 return vfprintf(sink, fmt, argp);
49 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp)
54 if ((p = malloc(needed(fmt, argp) + 1)) == NULL) {
55 lprintf(1, "vsnprintf: malloc failed, aborting\n");
58 if ((size = vsprintf(p, fmt, argp)) >= max)
67 int snprintf(char *buf, size_t max, const char *fmt,...)
73 bytes = vsnprintf(buf, max, fmt, argp);