1
2#ifndef HAVE_VPRINTF
3#include "choke-me: no vprintf and no snprintf"
4#endif
5
6#if defined(HAVE_STDARG_H)
7#  include <stdarg.h>
8#  ifndef   VA_START
9#    define VA_START(a, f)  va_start(a, f)
10#    define VA_END(a)       va_end(a)
11#  endif /* VA_START */
12#  define SNV_USING_STDARG_H
13
14#elif defined(HAVE_VARARGS_H)
15#  include <varargs.h>
16#  ifndef   VA_START
17#    define VA_START(a, f) va_start(a)
18#    define VA_END(a)    va_end(a)
19#  endif /* VA_START */
20#  undef  SNV_USING_STDARG_H
21
22#else
23#  include "must-have-stdarg-or-varargs"
24#endif
25
26static int
27snprintf(char *str, size_t n, char const *fmt, ...)
28{
29    va_list ap;
30    int rval;
31
32#ifdef VSPRINTF_CHARSTAR
33    char *rp;
34    VA_START(ap, fmt);
35    rp = vsprintf(str, fmt, ap);
36    VA_END(ap);
37    rval = strlen(rp);
38
39#else
40    VA_START(ap, fmt);
41    rval = vsprintf(str, fmt, ap);
42    VA_END(ap);
43#endif
44
45    if (rval > n) {
46        fprintf(stderr, "snprintf buffer overrun %d > %d\n", rval, (int)n);
47        abort();
48    }
49    return rval;
50}
51
52static int
53vsnprintf( char *str, size_t n, char const *fmt, va_list ap )
54{
55#ifdef VSPRINTF_CHARSTAR
56    return (strlen(vsprintf(str, fmt, ap)));
57#else
58    return (vsprintf(str, fmt, ap));
59#endif
60}
61