1 /* $NetBSD: snprintf.c,v 1.1.1.1 2009/12/13 16:57:25 kardel Exp $ */ 2 3 4 #ifndef HAVE_VPRINTF 5 #include "choke-me: no vprintf and no snprintf" 6 #endif 7 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 15 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 23 24 #else 25 # include "must-have-stdarg-or-varargs" 26 #endif 27 28 static int 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 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