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