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