Subject: Re: CVS commit: src/sys
To: None <tech-kern@NetBSD.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-kern
Date: 04/25/2004 03:21:48
> #define safe_snprintf(b, s, ...) \
> 	({int _x = snprintf((b), (s), __VA_ARGS__); \
> 	 if (_x > (s)) _x = (s); \
> 	 _x;})

> sorry...dunno how to do that without "gcc extensions".

int safe_snprintf(char *buf, int len, const char *fmt, ...)
{
 va_list ap;
 int r;

 va_start(ap,fmt);
 r = vsnprintf(buf,len,fmt,ap);
 va_end(ap);
 return((r>len)?len:r);
}

This has the additional advantage that it doesn't break if used inside
another macro that thinks _x is a "safe" variable name. :-)

/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML	       mouse@rodents.montreal.qc.ca
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B