1*0Sstevel@tonic-gate /**************************************************************
2*0Sstevel@tonic-gate  * Original:
3*0Sstevel@tonic-gate  * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
4*0Sstevel@tonic-gate  * A bombproof version of doprnt (dopr) included.
5*0Sstevel@tonic-gate  * Sigh.  This sort of thing is always nasty do deal with.  Note that
6*0Sstevel@tonic-gate  * the version here does not include floating point...
7*0Sstevel@tonic-gate  *
8*0Sstevel@tonic-gate  * snprintf() is used instead of sprintf() as it does limit checks
9*0Sstevel@tonic-gate  * for string length.  This covers a nasty loophole.
10*0Sstevel@tonic-gate  *
11*0Sstevel@tonic-gate  * The other functions are there to prevent NULL pointers from
12*0Sstevel@tonic-gate  * causing nast effects.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * More Recently:
15*0Sstevel@tonic-gate  *  Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
16*0Sstevel@tonic-gate  *  This was ugly.  It is still ugly.  I opted out of floating point
17*0Sstevel@tonic-gate  *  numbers, but the formatter understands just about everything
18*0Sstevel@tonic-gate  *  from the normal C string format, at least as far as I can tell from
19*0Sstevel@tonic-gate  *  the Solaris 2.5 printf(3S) man page.
20*0Sstevel@tonic-gate  *
21*0Sstevel@tonic-gate  *  Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
22*0Sstevel@tonic-gate  *    Ok, added some minimal floating point support, which means this
23*0Sstevel@tonic-gate  *    probably requires libm on most operating systems.  Don't yet
24*0Sstevel@tonic-gate  *    support the exponent (e,E) and sigfig (g,G).  Also, fmtint()
25*0Sstevel@tonic-gate  *    was pretty badly broken, it just wasn't being exercised in ways
26*0Sstevel@tonic-gate  *    which showed it, so that's been fixed.  Also, formated the code
27*0Sstevel@tonic-gate  *    to mutt conventions, and removed dead code left over from the
28*0Sstevel@tonic-gate  *    original.  Also, there is now a builtin-test, just compile with:
29*0Sstevel@tonic-gate  *           gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
30*0Sstevel@tonic-gate  *    and run snprintf for results.
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  *  Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
33*0Sstevel@tonic-gate  *    The PGP code was using unsigned hexadecimal formats.
34*0Sstevel@tonic-gate  *    Unfortunately, unsigned formats simply didn't work.
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  *  Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
37*0Sstevel@tonic-gate  *    The original code assumed that both snprintf() and vsnprintf() were
38*0Sstevel@tonic-gate  *    missing.  Some systems only have snprintf() but not vsnprintf(), so
39*0Sstevel@tonic-gate  *    the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  *  Ben Lindstrom <mouring@eviladmin.org> 09/27/00 for OpenSSH
42*0Sstevel@tonic-gate  *    Welcome to the world of %lld and %qd support.  With other
43*0Sstevel@tonic-gate  *    long long support.  This is needed for sftp-server to work
44*0Sstevel@tonic-gate  *    right.
45*0Sstevel@tonic-gate  *
46*0Sstevel@tonic-gate  *  Ben Lindstrom <mouring@eviladmin.org> 02/12/01 for OpenSSH
47*0Sstevel@tonic-gate  *    Removed all hint of VARARGS stuff and banished it to the void,
48*0Sstevel@tonic-gate  *    and did a bit of KNF style work to make things a bit more
49*0Sstevel@tonic-gate  *    acceptable.  Consider stealing from mutt or enlightenment.
50*0Sstevel@tonic-gate  **************************************************************/
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #include "includes.h"
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate RCSID("$Id: bsd-snprintf.c,v 1.5 2001/02/25 23:20:41 mouring Exp $");
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #if defined(BROKEN_SNPRINTF)		/* For those with broken snprintf() */
59*0Sstevel@tonic-gate # undef HAVE_SNPRINTF
60*0Sstevel@tonic-gate # undef HAVE_VSNPRINTF
61*0Sstevel@tonic-gate #endif
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate static void
66*0Sstevel@tonic-gate dopr(char *buffer, size_t maxlen, const char *format, va_list args);
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate static void
69*0Sstevel@tonic-gate fmtstr(char *buffer, size_t *currlen, size_t maxlen, char *value, int flags,
70*0Sstevel@tonic-gate        int min, int max);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate static void
73*0Sstevel@tonic-gate fmtint(char *buffer, size_t *currlen, size_t maxlen, long value, int base,
74*0Sstevel@tonic-gate        int min, int max, int flags);
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate static void
77*0Sstevel@tonic-gate fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue,
78*0Sstevel@tonic-gate       int min, int max, int flags);
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate static void
81*0Sstevel@tonic-gate dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate /*
84*0Sstevel@tonic-gate  * dopr(): poor man's version of doprintf
85*0Sstevel@tonic-gate  */
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate /* format read states */
88*0Sstevel@tonic-gate #define DP_S_DEFAULT 0
89*0Sstevel@tonic-gate #define DP_S_FLAGS   1
90*0Sstevel@tonic-gate #define DP_S_MIN     2
91*0Sstevel@tonic-gate #define DP_S_DOT     3
92*0Sstevel@tonic-gate #define DP_S_MAX     4
93*0Sstevel@tonic-gate #define DP_S_MOD     5
94*0Sstevel@tonic-gate #define DP_S_CONV    6
95*0Sstevel@tonic-gate #define DP_S_DONE    7
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate /* format flags - Bits */
98*0Sstevel@tonic-gate #define DP_F_MINUS 	(1 << 0)
99*0Sstevel@tonic-gate #define DP_F_PLUS  	(1 << 1)
100*0Sstevel@tonic-gate #define DP_F_SPACE 	(1 << 2)
101*0Sstevel@tonic-gate #define DP_F_NUM   	(1 << 3)
102*0Sstevel@tonic-gate #define DP_F_ZERO  	(1 << 4)
103*0Sstevel@tonic-gate #define DP_F_UP    	(1 << 5)
104*0Sstevel@tonic-gate #define DP_F_UNSIGNED 	(1 << 6)
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate /* Conversion Flags */
107*0Sstevel@tonic-gate #define DP_C_SHORT     1
108*0Sstevel@tonic-gate #define DP_C_LONG      2
109*0Sstevel@tonic-gate #define DP_C_LDOUBLE   3
110*0Sstevel@tonic-gate #define DP_C_LONG_LONG 4
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate #define char_to_int(p) (p - '0')
113*0Sstevel@tonic-gate #define abs_val(p) (p < 0 ? -p : p)
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate static void
117*0Sstevel@tonic-gate dopr(char *buffer, size_t maxlen, const char *format, va_list args)
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate 	char *strvalue;
120*0Sstevel@tonic-gate 	char ch;
121*0Sstevel@tonic-gate 	long value;
122*0Sstevel@tonic-gate 	long double fvalue;
123*0Sstevel@tonic-gate 	int min = 0;
124*0Sstevel@tonic-gate 	int max = -1;
125*0Sstevel@tonic-gate 	int state = DP_S_DEFAULT;
126*0Sstevel@tonic-gate 	int flags = 0;
127*0Sstevel@tonic-gate 	int cflags = 0;
128*0Sstevel@tonic-gate 	size_t currlen = 0;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	ch = *format++;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	while (state != DP_S_DONE) {
133*0Sstevel@tonic-gate 		if ((ch == '\0') || (currlen >= maxlen))
134*0Sstevel@tonic-gate 			state = DP_S_DONE;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 		switch(state) {
137*0Sstevel@tonic-gate 			case DP_S_DEFAULT:
138*0Sstevel@tonic-gate 				if (ch == '%')
139*0Sstevel@tonic-gate 					state = DP_S_FLAGS;
140*0Sstevel@tonic-gate 				else
141*0Sstevel@tonic-gate 					dopr_outch(buffer, &currlen, maxlen, ch);
142*0Sstevel@tonic-gate 				ch = *format++;
143*0Sstevel@tonic-gate 				break;
144*0Sstevel@tonic-gate 			case DP_S_FLAGS:
145*0Sstevel@tonic-gate 				switch (ch) {
146*0Sstevel@tonic-gate 					case '-':
147*0Sstevel@tonic-gate 						flags |= DP_F_MINUS;
148*0Sstevel@tonic-gate 						ch = *format++;
149*0Sstevel@tonic-gate 						break;
150*0Sstevel@tonic-gate 					case '+':
151*0Sstevel@tonic-gate 						flags |= DP_F_PLUS;
152*0Sstevel@tonic-gate 						ch = *format++;
153*0Sstevel@tonic-gate 						break;
154*0Sstevel@tonic-gate 					case ' ':
155*0Sstevel@tonic-gate 						flags |= DP_F_SPACE;
156*0Sstevel@tonic-gate 						ch = *format++;
157*0Sstevel@tonic-gate 						break;
158*0Sstevel@tonic-gate 					case '#':
159*0Sstevel@tonic-gate 						flags |= DP_F_NUM;
160*0Sstevel@tonic-gate 						ch = *format++;
161*0Sstevel@tonic-gate 						break;
162*0Sstevel@tonic-gate 					case '0':
163*0Sstevel@tonic-gate 						flags |= DP_F_ZERO;
164*0Sstevel@tonic-gate 						ch = *format++;
165*0Sstevel@tonic-gate 						break;
166*0Sstevel@tonic-gate 					default:
167*0Sstevel@tonic-gate 						state = DP_S_MIN;
168*0Sstevel@tonic-gate 						break;
169*0Sstevel@tonic-gate 				}
170*0Sstevel@tonic-gate 				break;
171*0Sstevel@tonic-gate 			case DP_S_MIN:
172*0Sstevel@tonic-gate 				if (isdigit((unsigned char)ch)) {
173*0Sstevel@tonic-gate 					min = 10*min + char_to_int (ch);
174*0Sstevel@tonic-gate 					ch = *format++;
175*0Sstevel@tonic-gate 				} else if (ch == '*') {
176*0Sstevel@tonic-gate 					min = va_arg (args, int);
177*0Sstevel@tonic-gate 					ch = *format++;
178*0Sstevel@tonic-gate 					state = DP_S_DOT;
179*0Sstevel@tonic-gate 				} else
180*0Sstevel@tonic-gate 					state = DP_S_DOT;
181*0Sstevel@tonic-gate 				break;
182*0Sstevel@tonic-gate 			case DP_S_DOT:
183*0Sstevel@tonic-gate 				if (ch == '.') {
184*0Sstevel@tonic-gate 					state = DP_S_MAX;
185*0Sstevel@tonic-gate 					ch = *format++;
186*0Sstevel@tonic-gate 				} else
187*0Sstevel@tonic-gate 					state = DP_S_MOD;
188*0Sstevel@tonic-gate 				break;
189*0Sstevel@tonic-gate 			case DP_S_MAX:
190*0Sstevel@tonic-gate 				if (isdigit((unsigned char)ch)) {
191*0Sstevel@tonic-gate 					if (max < 0)
192*0Sstevel@tonic-gate 						max = 0;
193*0Sstevel@tonic-gate 					max = 10*max + char_to_int(ch);
194*0Sstevel@tonic-gate 					ch = *format++;
195*0Sstevel@tonic-gate 				} else if (ch == '*') {
196*0Sstevel@tonic-gate 					max = va_arg (args, int);
197*0Sstevel@tonic-gate 					ch = *format++;
198*0Sstevel@tonic-gate 					state = DP_S_MOD;
199*0Sstevel@tonic-gate 				} else
200*0Sstevel@tonic-gate 					state = DP_S_MOD;
201*0Sstevel@tonic-gate 				break;
202*0Sstevel@tonic-gate 			case DP_S_MOD:
203*0Sstevel@tonic-gate 				switch (ch) {
204*0Sstevel@tonic-gate 					case 'h':
205*0Sstevel@tonic-gate 						cflags = DP_C_SHORT;
206*0Sstevel@tonic-gate 						ch = *format++;
207*0Sstevel@tonic-gate 						break;
208*0Sstevel@tonic-gate 					case 'l':
209*0Sstevel@tonic-gate 						cflags = DP_C_LONG;
210*0Sstevel@tonic-gate 						ch = *format++;
211*0Sstevel@tonic-gate 						if (ch == 'l') {
212*0Sstevel@tonic-gate 							cflags = DP_C_LONG_LONG;
213*0Sstevel@tonic-gate 							ch = *format++;
214*0Sstevel@tonic-gate 						}
215*0Sstevel@tonic-gate 						break;
216*0Sstevel@tonic-gate 					case 'q':
217*0Sstevel@tonic-gate 						cflags = DP_C_LONG_LONG;
218*0Sstevel@tonic-gate 						ch = *format++;
219*0Sstevel@tonic-gate 						break;
220*0Sstevel@tonic-gate 					case 'L':
221*0Sstevel@tonic-gate 						cflags = DP_C_LDOUBLE;
222*0Sstevel@tonic-gate 						ch = *format++;
223*0Sstevel@tonic-gate 						break;
224*0Sstevel@tonic-gate 					default:
225*0Sstevel@tonic-gate 						break;
226*0Sstevel@tonic-gate 				}
227*0Sstevel@tonic-gate 				state = DP_S_CONV;
228*0Sstevel@tonic-gate 				break;
229*0Sstevel@tonic-gate 			case DP_S_CONV:
230*0Sstevel@tonic-gate 				switch (ch) {
231*0Sstevel@tonic-gate 					case 'd':
232*0Sstevel@tonic-gate 					case 'i':
233*0Sstevel@tonic-gate 						if (cflags == DP_C_SHORT)
234*0Sstevel@tonic-gate 							value = va_arg(args, int);
235*0Sstevel@tonic-gate 						else if (cflags == DP_C_LONG)
236*0Sstevel@tonic-gate 							value = va_arg(args, long int);
237*0Sstevel@tonic-gate 						else if (cflags == DP_C_LONG_LONG)
238*0Sstevel@tonic-gate 							value = va_arg (args, long long);
239*0Sstevel@tonic-gate 						else
240*0Sstevel@tonic-gate 							value = va_arg (args, int);
241*0Sstevel@tonic-gate 						fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags);
242*0Sstevel@tonic-gate 						break;
243*0Sstevel@tonic-gate 					case 'o':
244*0Sstevel@tonic-gate 						flags |= DP_F_UNSIGNED;
245*0Sstevel@tonic-gate 						if (cflags == DP_C_SHORT)
246*0Sstevel@tonic-gate 							value = va_arg(args, unsigned int);
247*0Sstevel@tonic-gate 						else if (cflags == DP_C_LONG)
248*0Sstevel@tonic-gate 							value = va_arg(args, unsigned long int);
249*0Sstevel@tonic-gate 						else if (cflags == DP_C_LONG_LONG)
250*0Sstevel@tonic-gate 							value = va_arg(args, unsigned long long);
251*0Sstevel@tonic-gate 						else
252*0Sstevel@tonic-gate 							value = va_arg(args, unsigned int);
253*0Sstevel@tonic-gate 						fmtint(buffer, &currlen, maxlen, value, 8, min, max, flags);
254*0Sstevel@tonic-gate 						break;
255*0Sstevel@tonic-gate 					case 'u':
256*0Sstevel@tonic-gate 						flags |= DP_F_UNSIGNED;
257*0Sstevel@tonic-gate 						if (cflags == DP_C_SHORT)
258*0Sstevel@tonic-gate 							value = va_arg(args, unsigned int);
259*0Sstevel@tonic-gate 						else if (cflags == DP_C_LONG)
260*0Sstevel@tonic-gate 							value = va_arg(args, unsigned long int);
261*0Sstevel@tonic-gate 						else if (cflags == DP_C_LONG_LONG)
262*0Sstevel@tonic-gate 							value = va_arg(args, unsigned long long);
263*0Sstevel@tonic-gate 						else
264*0Sstevel@tonic-gate 							value = va_arg(args, unsigned int);
265*0Sstevel@tonic-gate 						fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
266*0Sstevel@tonic-gate 						break;
267*0Sstevel@tonic-gate 					case 'X':
268*0Sstevel@tonic-gate 						flags |= DP_F_UP;
269*0Sstevel@tonic-gate 					case 'x':
270*0Sstevel@tonic-gate 						flags |= DP_F_UNSIGNED;
271*0Sstevel@tonic-gate 						if (cflags == DP_C_SHORT)
272*0Sstevel@tonic-gate 							value = va_arg(args, unsigned int);
273*0Sstevel@tonic-gate 						else if (cflags == DP_C_LONG)
274*0Sstevel@tonic-gate 							value = va_arg(args, unsigned long int);
275*0Sstevel@tonic-gate 						else if (cflags == DP_C_LONG_LONG)
276*0Sstevel@tonic-gate 							value = va_arg(args, unsigned long long);
277*0Sstevel@tonic-gate 						else
278*0Sstevel@tonic-gate 							value = va_arg(args, unsigned int);
279*0Sstevel@tonic-gate 						fmtint(buffer, &currlen, maxlen, value, 16, min, max, flags);
280*0Sstevel@tonic-gate 						break;
281*0Sstevel@tonic-gate 					case 'f':
282*0Sstevel@tonic-gate 						if (cflags == DP_C_LDOUBLE)
283*0Sstevel@tonic-gate 							fvalue = va_arg(args, long double);
284*0Sstevel@tonic-gate 						else
285*0Sstevel@tonic-gate 							fvalue = va_arg(args, double);
286*0Sstevel@tonic-gate 						/* um, floating point? */
287*0Sstevel@tonic-gate 						fmtfp(buffer, &currlen, maxlen, fvalue, min, max, flags);
288*0Sstevel@tonic-gate 						break;
289*0Sstevel@tonic-gate 					case 'E':
290*0Sstevel@tonic-gate 						flags |= DP_F_UP;
291*0Sstevel@tonic-gate 					case 'e':
292*0Sstevel@tonic-gate 						if (cflags == DP_C_LDOUBLE)
293*0Sstevel@tonic-gate 							fvalue = va_arg(args, long double);
294*0Sstevel@tonic-gate 						else
295*0Sstevel@tonic-gate 							fvalue = va_arg(args, double);
296*0Sstevel@tonic-gate 						break;
297*0Sstevel@tonic-gate 					case 'G':
298*0Sstevel@tonic-gate 						flags |= DP_F_UP;
299*0Sstevel@tonic-gate 					case 'g':
300*0Sstevel@tonic-gate 						if (cflags == DP_C_LDOUBLE)
301*0Sstevel@tonic-gate 							fvalue = va_arg(args, long double);
302*0Sstevel@tonic-gate 						else
303*0Sstevel@tonic-gate 							fvalue = va_arg(args, double);
304*0Sstevel@tonic-gate 						break;
305*0Sstevel@tonic-gate 					case 'c':
306*0Sstevel@tonic-gate 						dopr_outch(buffer, &currlen, maxlen, va_arg(args, int));
307*0Sstevel@tonic-gate 						break;
308*0Sstevel@tonic-gate 					case 's':
309*0Sstevel@tonic-gate 						strvalue = va_arg(args, char *);
310*0Sstevel@tonic-gate 						if (max < 0)
311*0Sstevel@tonic-gate 							max = maxlen; /* ie, no max */
312*0Sstevel@tonic-gate 						fmtstr(buffer, &currlen, maxlen, strvalue, flags, min, max);
313*0Sstevel@tonic-gate 						break;
314*0Sstevel@tonic-gate 					case 'p':
315*0Sstevel@tonic-gate 						strvalue = va_arg(args, void *);
316*0Sstevel@tonic-gate 						fmtint(buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
317*0Sstevel@tonic-gate 						break;
318*0Sstevel@tonic-gate 					case 'n':
319*0Sstevel@tonic-gate 						if (cflags == DP_C_SHORT) {
320*0Sstevel@tonic-gate 							short int *num;
321*0Sstevel@tonic-gate 							num = va_arg(args, short int *);
322*0Sstevel@tonic-gate 							*num = currlen;
323*0Sstevel@tonic-gate 						} else if (cflags == DP_C_LONG) {
324*0Sstevel@tonic-gate 							long int *num;
325*0Sstevel@tonic-gate 							num = va_arg(args, long int *);
326*0Sstevel@tonic-gate 							*num = currlen;
327*0Sstevel@tonic-gate 						} else if (cflags == DP_C_LONG_LONG) {
328*0Sstevel@tonic-gate 							long long *num;
329*0Sstevel@tonic-gate 							num = va_arg(args, long long *);
330*0Sstevel@tonic-gate 							*num = currlen;
331*0Sstevel@tonic-gate 						} else {
332*0Sstevel@tonic-gate 							int *num;
333*0Sstevel@tonic-gate 							num = va_arg(args, int *);
334*0Sstevel@tonic-gate 							*num = currlen;
335*0Sstevel@tonic-gate 						}
336*0Sstevel@tonic-gate 						break;
337*0Sstevel@tonic-gate 					case '%':
338*0Sstevel@tonic-gate 						dopr_outch(buffer, &currlen, maxlen, ch);
339*0Sstevel@tonic-gate 						break;
340*0Sstevel@tonic-gate 					case 'w': /* not supported yet, treat as next char */
341*0Sstevel@tonic-gate 						ch = *format++;
342*0Sstevel@tonic-gate 						break;
343*0Sstevel@tonic-gate 					default: /* Unknown, skip */
344*0Sstevel@tonic-gate 					break;
345*0Sstevel@tonic-gate 				}
346*0Sstevel@tonic-gate 				ch = *format++;
347*0Sstevel@tonic-gate 				state = DP_S_DEFAULT;
348*0Sstevel@tonic-gate 				flags = cflags = min = 0;
349*0Sstevel@tonic-gate 				max = -1;
350*0Sstevel@tonic-gate 				break;
351*0Sstevel@tonic-gate 			case DP_S_DONE:
352*0Sstevel@tonic-gate 				break;
353*0Sstevel@tonic-gate 			default: /* hmm? */
354*0Sstevel@tonic-gate 				break; /* some picky compilers need this */
355*0Sstevel@tonic-gate 		}
356*0Sstevel@tonic-gate 	}
357*0Sstevel@tonic-gate 	if (currlen < maxlen - 1)
358*0Sstevel@tonic-gate 		buffer[currlen] = '\0';
359*0Sstevel@tonic-gate 	else
360*0Sstevel@tonic-gate 		buffer[maxlen - 1] = '\0';
361*0Sstevel@tonic-gate }
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate static void
364*0Sstevel@tonic-gate fmtstr(char *buffer, size_t *currlen, size_t maxlen,
365*0Sstevel@tonic-gate        char *value, int flags, int min, int max)
366*0Sstevel@tonic-gate {
367*0Sstevel@tonic-gate 	int padlen, strln;     /* amount to pad */
368*0Sstevel@tonic-gate 	int cnt = 0;
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 	if (value == 0)
371*0Sstevel@tonic-gate 		value = "<NULL>";
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	for (strln = 0; value[strln]; ++strln); /* strlen */
374*0Sstevel@tonic-gate 	padlen = min - strln;
375*0Sstevel@tonic-gate 	if (padlen < 0)
376*0Sstevel@tonic-gate 		padlen = 0;
377*0Sstevel@tonic-gate 	if (flags & DP_F_MINUS)
378*0Sstevel@tonic-gate 		padlen = -padlen; /* Left Justify */
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	while ((padlen > 0) && (cnt < max)) {
381*0Sstevel@tonic-gate 		dopr_outch(buffer, currlen, maxlen, ' ');
382*0Sstevel@tonic-gate 		--padlen;
383*0Sstevel@tonic-gate 		++cnt;
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate 	while (*value && (cnt < max)) {
386*0Sstevel@tonic-gate 		dopr_outch(buffer, currlen, maxlen, *value++);
387*0Sstevel@tonic-gate 		++cnt;
388*0Sstevel@tonic-gate 	}
389*0Sstevel@tonic-gate 	while ((padlen < 0) && (cnt < max)) {
390*0Sstevel@tonic-gate 		dopr_outch(buffer, currlen, maxlen, ' ');
391*0Sstevel@tonic-gate 		++padlen;
392*0Sstevel@tonic-gate 		++cnt;
393*0Sstevel@tonic-gate 	}
394*0Sstevel@tonic-gate }
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate static void
399*0Sstevel@tonic-gate fmtint(char *buffer, size_t *currlen, size_t maxlen,
400*0Sstevel@tonic-gate        long value, int base, int min, int max, int flags)
401*0Sstevel@tonic-gate {
402*0Sstevel@tonic-gate 	unsigned long uvalue;
403*0Sstevel@tonic-gate 	char convert[20];
404*0Sstevel@tonic-gate 	int signvalue = 0;
405*0Sstevel@tonic-gate 	int place = 0;
406*0Sstevel@tonic-gate 	int spadlen = 0; /* amount to space pad */
407*0Sstevel@tonic-gate 	int zpadlen = 0; /* amount to zero pad */
408*0Sstevel@tonic-gate 	int caps = 0;
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	if (max < 0)
411*0Sstevel@tonic-gate 		max = 0;
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	uvalue = value;
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 	if (!(flags & DP_F_UNSIGNED)) {
416*0Sstevel@tonic-gate 		if (value < 0) {
417*0Sstevel@tonic-gate 			signvalue = '-';
418*0Sstevel@tonic-gate 			uvalue = -value;
419*0Sstevel@tonic-gate 		} else if (flags & DP_F_PLUS)  /* Do a sign (+/i) */
420*0Sstevel@tonic-gate 			signvalue = '+';
421*0Sstevel@tonic-gate 		else if (flags & DP_F_SPACE)
422*0Sstevel@tonic-gate 			signvalue = ' ';
423*0Sstevel@tonic-gate 	}
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 	if (flags & DP_F_UP)
426*0Sstevel@tonic-gate 		caps = 1; /* Should characters be upper case? */
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate 	do {
429*0Sstevel@tonic-gate 		convert[place++] =
430*0Sstevel@tonic-gate 			(caps? "0123456789ABCDEF":"0123456789abcdef")
431*0Sstevel@tonic-gate 			[uvalue % (unsigned)base];
432*0Sstevel@tonic-gate 		uvalue = (uvalue / (unsigned)base );
433*0Sstevel@tonic-gate 	} while (uvalue && (place < 20));
434*0Sstevel@tonic-gate 	if (place == 20)
435*0Sstevel@tonic-gate 		place--;
436*0Sstevel@tonic-gate 	convert[place] = 0;
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 	zpadlen = max - place;
439*0Sstevel@tonic-gate 	spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
440*0Sstevel@tonic-gate 	if (zpadlen < 0)
441*0Sstevel@tonic-gate 		zpadlen = 0;
442*0Sstevel@tonic-gate 	if (spadlen < 0)
443*0Sstevel@tonic-gate 		spadlen = 0;
444*0Sstevel@tonic-gate 	if (flags & DP_F_ZERO) {
445*0Sstevel@tonic-gate 		zpadlen = MAX(zpadlen, spadlen);
446*0Sstevel@tonic-gate 		spadlen = 0;
447*0Sstevel@tonic-gate 	}
448*0Sstevel@tonic-gate 	if (flags & DP_F_MINUS)
449*0Sstevel@tonic-gate 		spadlen = -spadlen; /* Left Justifty */
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	/* Spaces */
453*0Sstevel@tonic-gate 	while (spadlen > 0) {
454*0Sstevel@tonic-gate 		dopr_outch(buffer, currlen, maxlen, ' ');
455*0Sstevel@tonic-gate 		--spadlen;
456*0Sstevel@tonic-gate 	}
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 	/* Sign */
459*0Sstevel@tonic-gate 	if (signvalue)
460*0Sstevel@tonic-gate 		dopr_outch(buffer, currlen, maxlen, signvalue);
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 	/* Zeros */
463*0Sstevel@tonic-gate 	if (zpadlen > 0) {
464*0Sstevel@tonic-gate 		while (zpadlen > 0) {
465*0Sstevel@tonic-gate 			dopr_outch(buffer, currlen, maxlen, '0');
466*0Sstevel@tonic-gate 			--zpadlen;
467*0Sstevel@tonic-gate 		}
468*0Sstevel@tonic-gate 	}
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 	/* Digits */
471*0Sstevel@tonic-gate 	while (place > 0)
472*0Sstevel@tonic-gate 		dopr_outch(buffer, currlen, maxlen, convert[--place]);
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate 	/* Left Justified spaces */
475*0Sstevel@tonic-gate 	while (spadlen < 0) {
476*0Sstevel@tonic-gate 		dopr_outch (buffer, currlen, maxlen, ' ');
477*0Sstevel@tonic-gate 		++spadlen;
478*0Sstevel@tonic-gate 	}
479*0Sstevel@tonic-gate }
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate static long double
482*0Sstevel@tonic-gate pow10(int exp)
483*0Sstevel@tonic-gate {
484*0Sstevel@tonic-gate 	long double result = 1;
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 	while (exp) {
487*0Sstevel@tonic-gate 		result *= 10;
488*0Sstevel@tonic-gate 		exp--;
489*0Sstevel@tonic-gate 	}
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	return result;
492*0Sstevel@tonic-gate }
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate static long
495*0Sstevel@tonic-gate round(long double value)
496*0Sstevel@tonic-gate {
497*0Sstevel@tonic-gate 	long intpart = value;
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 	value -= intpart;
500*0Sstevel@tonic-gate 	if (value >= 0.5)
501*0Sstevel@tonic-gate 		intpart++;
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 	return intpart;
504*0Sstevel@tonic-gate }
505*0Sstevel@tonic-gate 
506*0Sstevel@tonic-gate static void
507*0Sstevel@tonic-gate fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue,
508*0Sstevel@tonic-gate       int min, int max, int flags)
509*0Sstevel@tonic-gate {
510*0Sstevel@tonic-gate 	char iconvert[20];
511*0Sstevel@tonic-gate 	char fconvert[20];
512*0Sstevel@tonic-gate 	int signvalue = 0;
513*0Sstevel@tonic-gate 	int iplace = 0;
514*0Sstevel@tonic-gate 	int fplace = 0;
515*0Sstevel@tonic-gate 	int padlen = 0; /* amount to pad */
516*0Sstevel@tonic-gate 	int zpadlen = 0;
517*0Sstevel@tonic-gate 	int caps = 0;
518*0Sstevel@tonic-gate 	long intpart;
519*0Sstevel@tonic-gate 	long fracpart;
520*0Sstevel@tonic-gate 	long double ufvalue;
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 	/*
523*0Sstevel@tonic-gate 	 * AIX manpage says the default is 0, but Solaris says the default
524*0Sstevel@tonic-gate 	 * is 6, and sprintf on AIX defaults to 6
525*0Sstevel@tonic-gate 	 */
526*0Sstevel@tonic-gate 	if (max < 0)
527*0Sstevel@tonic-gate 		max = 6;
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 	ufvalue = abs_val(fvalue);
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	if (fvalue < 0)
532*0Sstevel@tonic-gate 		signvalue = '-';
533*0Sstevel@tonic-gate 	else if (flags & DP_F_PLUS)  /* Do a sign (+/i) */
534*0Sstevel@tonic-gate 		signvalue = '+';
535*0Sstevel@tonic-gate 	else if (flags & DP_F_SPACE)
536*0Sstevel@tonic-gate 		signvalue = ' ';
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 	intpart = ufvalue;
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate 	/*
541*0Sstevel@tonic-gate 	 * Sorry, we only support 9 digits past the decimal because of our
542*0Sstevel@tonic-gate 	 * conversion method
543*0Sstevel@tonic-gate 	 */
544*0Sstevel@tonic-gate 	if (max > 9)
545*0Sstevel@tonic-gate 		max = 9;
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 	/* We "cheat" by converting the fractional part to integer by
548*0Sstevel@tonic-gate 	 * multiplying by a factor of 10
549*0Sstevel@tonic-gate 	 */
550*0Sstevel@tonic-gate 	fracpart = round((pow10 (max)) * (ufvalue - intpart));
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate 	if (fracpart >= pow10 (max)) {
553*0Sstevel@tonic-gate 		intpart++;
554*0Sstevel@tonic-gate 		fracpart -= pow10 (max);
555*0Sstevel@tonic-gate 	}
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 	/* Convert integer part */
558*0Sstevel@tonic-gate 	do {
559*0Sstevel@tonic-gate 		iconvert[iplace++] =
560*0Sstevel@tonic-gate 		  (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
561*0Sstevel@tonic-gate 		intpart = (intpart / 10);
562*0Sstevel@tonic-gate 	} while(intpart && (iplace < 20));
563*0Sstevel@tonic-gate 	if (iplace == 20)
564*0Sstevel@tonic-gate 		iplace--;
565*0Sstevel@tonic-gate 	iconvert[iplace] = 0;
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 	/* Convert fractional part */
568*0Sstevel@tonic-gate 	do {
569*0Sstevel@tonic-gate 		fconvert[fplace++] =
570*0Sstevel@tonic-gate 		  (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
571*0Sstevel@tonic-gate 		fracpart = (fracpart / 10);
572*0Sstevel@tonic-gate 	} while(fracpart && (fplace < 20));
573*0Sstevel@tonic-gate 	if (fplace == 20)
574*0Sstevel@tonic-gate 		fplace--;
575*0Sstevel@tonic-gate 	fconvert[fplace] = 0;
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 	/* -1 for decimal point, another -1 if we are printing a sign */
578*0Sstevel@tonic-gate 	padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
579*0Sstevel@tonic-gate 	zpadlen = max - fplace;
580*0Sstevel@tonic-gate 	if (zpadlen < 0)
581*0Sstevel@tonic-gate 		zpadlen = 0;
582*0Sstevel@tonic-gate 	if (padlen < 0)
583*0Sstevel@tonic-gate 		padlen = 0;
584*0Sstevel@tonic-gate 	if (flags & DP_F_MINUS)
585*0Sstevel@tonic-gate 		padlen = -padlen; /* Left Justifty */
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate 	if ((flags & DP_F_ZERO) && (padlen > 0)) {
588*0Sstevel@tonic-gate 		if (signvalue) {
589*0Sstevel@tonic-gate 			dopr_outch(buffer, currlen, maxlen, signvalue);
590*0Sstevel@tonic-gate 			--padlen;
591*0Sstevel@tonic-gate 			signvalue = 0;
592*0Sstevel@tonic-gate 		}
593*0Sstevel@tonic-gate 		while (padlen > 0) {
594*0Sstevel@tonic-gate 			dopr_outch(buffer, currlen, maxlen, '0');
595*0Sstevel@tonic-gate 			--padlen;
596*0Sstevel@tonic-gate 		}
597*0Sstevel@tonic-gate 	}
598*0Sstevel@tonic-gate 	while (padlen > 0) {
599*0Sstevel@tonic-gate 		dopr_outch(buffer, currlen, maxlen, ' ');
600*0Sstevel@tonic-gate 		--padlen;
601*0Sstevel@tonic-gate 	}
602*0Sstevel@tonic-gate 	if (signvalue)
603*0Sstevel@tonic-gate 		dopr_outch(buffer, currlen, maxlen, signvalue);
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 	while (iplace > 0)
606*0Sstevel@tonic-gate 		dopr_outch(buffer, currlen, maxlen, iconvert[--iplace]);
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate 	/*
609*0Sstevel@tonic-gate 	 * Decimal point.  This should probably use locale to find the correct
610*0Sstevel@tonic-gate 	 * char to print out.
611*0Sstevel@tonic-gate 	 */
612*0Sstevel@tonic-gate 	dopr_outch(buffer, currlen, maxlen, '.');
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate 	while (fplace > 0)
615*0Sstevel@tonic-gate 		dopr_outch(buffer, currlen, maxlen, fconvert[--fplace]);
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 	while (zpadlen > 0) {
618*0Sstevel@tonic-gate 		dopr_outch(buffer, currlen, maxlen, '0');
619*0Sstevel@tonic-gate 		--zpadlen;
620*0Sstevel@tonic-gate 	}
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate 	while (padlen < 0) {
623*0Sstevel@tonic-gate 		dopr_outch(buffer, currlen, maxlen, ' ');
624*0Sstevel@tonic-gate 		++padlen;
625*0Sstevel@tonic-gate 	}
626*0Sstevel@tonic-gate }
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate static void
629*0Sstevel@tonic-gate dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
630*0Sstevel@tonic-gate {
631*0Sstevel@tonic-gate 	if (*currlen < maxlen)
632*0Sstevel@tonic-gate 		buffer[(*currlen)++] = c;
633*0Sstevel@tonic-gate }
634*0Sstevel@tonic-gate #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate #ifndef HAVE_VSNPRINTF
637*0Sstevel@tonic-gate int
638*0Sstevel@tonic-gate vsnprintf(char *str, size_t count, const char *fmt, va_list args)
639*0Sstevel@tonic-gate {
640*0Sstevel@tonic-gate 	str[0] = 0;
641*0Sstevel@tonic-gate 	dopr(str, count, fmt, args);
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 	return(strlen(str));
644*0Sstevel@tonic-gate }
645*0Sstevel@tonic-gate #endif /* !HAVE_VSNPRINTF */
646*0Sstevel@tonic-gate 
647*0Sstevel@tonic-gate #ifndef HAVE_SNPRINTF
648*0Sstevel@tonic-gate int
649*0Sstevel@tonic-gate snprintf(char *str,size_t count,const char *fmt,...)
650*0Sstevel@tonic-gate {
651*0Sstevel@tonic-gate 	va_list ap;
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate 	va_start(ap, fmt);
654*0Sstevel@tonic-gate 	(void) vsnprintf(str, count, fmt, ap);
655*0Sstevel@tonic-gate 	va_end(ap);
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 	return(strlen(str));
658*0Sstevel@tonic-gate }
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate #ifdef TEST_SNPRINTF
661*0Sstevel@tonic-gate int
662*0Sstevel@tonic-gate main(void)
663*0Sstevel@tonic-gate {
664*0Sstevel@tonic-gate #define LONG_STRING 1024
665*0Sstevel@tonic-gate 	char buf1[LONG_STRING];
666*0Sstevel@tonic-gate 	char buf2[LONG_STRING];
667*0Sstevel@tonic-gate 	char *fp_fmt[] = {
668*0Sstevel@tonic-gate 		"%-1.5f",
669*0Sstevel@tonic-gate 		"%1.5f",
670*0Sstevel@tonic-gate 		"%123.9f",
671*0Sstevel@tonic-gate 		"%10.5f",
672*0Sstevel@tonic-gate 		"% 10.5f",
673*0Sstevel@tonic-gate 		"%+22.9f",
674*0Sstevel@tonic-gate 		"%+4.9f",
675*0Sstevel@tonic-gate 		"%01.3f",
676*0Sstevel@tonic-gate 		"%4f",
677*0Sstevel@tonic-gate 		"%3.1f",
678*0Sstevel@tonic-gate 		"%3.2f",
679*0Sstevel@tonic-gate 		NULL
680*0Sstevel@tonic-gate 	};
681*0Sstevel@tonic-gate 	double fp_nums[] = {
682*0Sstevel@tonic-gate 		-1.5,
683*0Sstevel@tonic-gate 		134.21,
684*0Sstevel@tonic-gate 		91340.2,
685*0Sstevel@tonic-gate 		341.1234,
686*0Sstevel@tonic-gate 		0203.9,
687*0Sstevel@tonic-gate 		0.96,
688*0Sstevel@tonic-gate 		0.996,
689*0Sstevel@tonic-gate 		0.9996,
690*0Sstevel@tonic-gate 		1.996,
691*0Sstevel@tonic-gate 		4.136,
692*0Sstevel@tonic-gate 		0
693*0Sstevel@tonic-gate 	};
694*0Sstevel@tonic-gate 	char *int_fmt[] = {
695*0Sstevel@tonic-gate 		"%-1.5d",
696*0Sstevel@tonic-gate 		"%1.5d",
697*0Sstevel@tonic-gate 		"%123.9d",
698*0Sstevel@tonic-gate 		"%5.5d",
699*0Sstevel@tonic-gate 		"%10.5d",
700*0Sstevel@tonic-gate 		"% 10.5d",
701*0Sstevel@tonic-gate 		"%+22.33d",
702*0Sstevel@tonic-gate 		"%01.3d",
703*0Sstevel@tonic-gate 		"%4d",
704*0Sstevel@tonic-gate 		"%lld",
705*0Sstevel@tonic-gate 		"%qd",
706*0Sstevel@tonic-gate 		NULL
707*0Sstevel@tonic-gate 	};
708*0Sstevel@tonic-gate 	long long int_nums[] = { -1, 134, 91340, 341, 0203, 0, 9999999 };
709*0Sstevel@tonic-gate 	int x, y;
710*0Sstevel@tonic-gate 	int fail = 0;
711*0Sstevel@tonic-gate 	int num = 0;
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 	printf("Testing snprintf format codes against system sprintf...\n");
714*0Sstevel@tonic-gate 
715*0Sstevel@tonic-gate 	for (x = 0; fp_fmt[x] != NULL ; x++) {
716*0Sstevel@tonic-gate 		for (y = 0; fp_nums[y] != 0 ; y++) {
717*0Sstevel@tonic-gate 			snprintf(buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
718*0Sstevel@tonic-gate 			sprintf (buf2, fp_fmt[x], fp_nums[y]);
719*0Sstevel@tonic-gate 			if (strcmp (buf1, buf2)) {
720*0Sstevel@tonic-gate 				printf("snprintf doesn't match Format: %s\n\t"
721*0Sstevel@tonic-gate                                        "snprintf = %s\n\tsprintf  = %s\n",
722*0Sstevel@tonic-gate 					fp_fmt[x], buf1, buf2);
723*0Sstevel@tonic-gate 				fail++;
724*0Sstevel@tonic-gate 			}
725*0Sstevel@tonic-gate 			num++;
726*0Sstevel@tonic-gate 		}
727*0Sstevel@tonic-gate 	}
728*0Sstevel@tonic-gate 	for (x = 0; int_fmt[x] != NULL ; x++) {
729*0Sstevel@tonic-gate 		for (y = 0; int_nums[y] != 0 ; y++) {
730*0Sstevel@tonic-gate 			snprintf(buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
731*0Sstevel@tonic-gate 			sprintf(buf2, int_fmt[x], int_nums[y]);
732*0Sstevel@tonic-gate 			if (strcmp (buf1, buf2)) {
733*0Sstevel@tonic-gate 				printf("snprintf doesn't match Format: %s\n\t"
734*0Sstevel@tonic-gate 				       "snprintf = %s\n\tsprintf  = %s\n",
735*0Sstevel@tonic-gate 					int_fmt[x], buf1, buf2);
736*0Sstevel@tonic-gate 				fail++;
737*0Sstevel@tonic-gate 			}
738*0Sstevel@tonic-gate 			num++;
739*0Sstevel@tonic-gate 		}
740*0Sstevel@tonic-gate 	}
741*0Sstevel@tonic-gate 	printf("%d tests failed out of %d.\n", fail, num);
742*0Sstevel@tonic-gate 	return(0);
743*0Sstevel@tonic-gate }
744*0Sstevel@tonic-gate #endif /* SNPRINTF_TEST */
745*0Sstevel@tonic-gate 
746*0Sstevel@tonic-gate #endif /* !HAVE_SNPRINTF */
747