Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libopts / compat / snprintf.c
blob4a10060ba304854ac52a676803d2861a3c69ba79
1 /* $NetBSD$ */
4 #ifndef HAVE_VPRINTF
5 #include "choke-me: no vprintf and no snprintf"
6 #endif
8 #if defined(HAVE_STDARG_H)
9 # include <stdarg.h>
10 # ifndef VA_START
11 # define VA_START(a, f) va_start(a, f)
12 # define VA_END(a) va_end(a)
13 # endif /* VA_START */
14 # define SNV_USING_STDARG_H
16 #elif defined(HAVE_VARARGS_H)
17 # include <varargs.h>
18 # ifndef VA_START
19 # define VA_START(a, f) va_start(a)
20 # define VA_END(a) va_end(a)
21 # endif /* VA_START */
22 # undef SNV_USING_STDARG_H
24 #else
25 # include "must-have-stdarg-or-varargs"
26 #endif
28 static int
29 snprintf(char *str, size_t n, char const *fmt, ...)
31 va_list ap;
32 int rval;
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);
41 #else
42 VA_START(ap, fmt);
43 rval = vsprintf(str, fmt, ap);
44 VA_END(ap);
45 #endif
47 if (rval > n) {
48 fprintf(stderr, "snprintf buffer overrun %d > %d\n", rval, (int)n);
49 abort();
51 return rval;
54 static int
55 vsnprintf( char *str, size_t n, char const *fmt, va_list ap )
57 #ifdef VSPRINTF_CHARSTAR
58 return (strlen(vsprintf(str, fmt, ap)));
59 #else
60 return (vsprintf(str, fmt, ap));
61 #endif