SystemCall run(block) can now exit the run if it returns false
[io/quag.git] / libs / basekit / source / PortableSnprintf.c
blob98a1fa174c9cfdf4769789a8c4ba56c939b19a88
1 /*
2 * Copyright Patrick Powell 1995
3 * This code is based on code written by Patrick Powell (papowell@astart.com)
4 * It may be used for any purpose as long as this notice remains intact
5 * on all source code distributions
6 */
8 /**************************************************************
9 * Original:
10 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
11 * A bombproof version of doprnt (dopr) included.
12 * Sigh. This sort of thing is always nasty do deal with. Note that
13 * the version here does not include floating point...
15 * snprintf() is used instead of sprintf() as it does limit checks
16 * for string length. This covers a nasty loophole.
18 * The other functions are there to prevent NULL pointers from
19 * causing nast effects.
21 * More Recently:
22 * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
23 * This was ugly. It is still ugly. I opted out of floating point
24 * numbers, but the formatter understands just about everything
25 * from the normal C string format, at least as far as I can tell from
26 * the Solaris 2.5 printf(3S) man page.
28 * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
29 * Ok, added some minimal floating point support, which means this
30 * probably requires libm on most operating systems. Don't yet
31 * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
32 * was pretty badly broken, it just wasn't being exercised in ways
33 * which showed it, so that's been fixed. Also, formated the code
34 * to mutt conventions, and removed dead code left over from the
35 * original. Also, there is now a builtin-test, just compile with:
36 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
37 * and run snprintf for results.
39 * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
40 * The PGP code was using unsigned hexadecimal formats.
41 * Unfortunately, unsigned formats simply didn't work.
43 * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
44 * The original code assumed that both snprintf() and vsnprintf() were
45 * missing. Some systems only have snprintf() but not vsnprintf(), so
46 * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
48 * Andrew Tridgell (tridge@samba.org) Oct 1998
49 * fixed handling of %.0f
50 * added test for HAVE_LONG_DOUBLE
52 * tridge@samba.org, idra@samba.org, April 2001
53 * got rid of fcvt code (twas buggy and made testing harder)
54 * added C99 semantics
56 **************************************************************/
58 #include <string.h>
59 #include <ctype.h>
60 #include <sys/types.h>
61 #include <stdarg.h>
62 #include <stdlib.h>
64 #define NO_CONFIG_H 1
65 #define HAVE_STRING_H 1
66 #define HAVE_CTYPE_H 1
67 #define HAVE_STDLIB_H 1
69 #define HAVE_SNPRINTF 1
70 #define HAVE_VSNPRINTF 1
71 #define HAVE_C99_VSNPRINTF 1
73 #define HAVE_LONG_DOUBLE 1
74 #define HAVE_LONG_LONG 1
75 #define HAVE_SNPRINTF 1
76 #define HAVE_C99_VSNPRINTF 1
77 #define HAVE_ASPRINTF 1
78 #define HAVE_VASPRINTF 1
80 #ifndef NO_CONFIG_H
81 #include "config.h"
82 #endif
84 #ifdef HAVE_STRING_H
85 #include <string.h>
86 #endif
88 #ifdef HAVE_STRINGS_H
89 #include <strings.h>
90 #endif
91 #ifdef HAVE_CTYPE_H
92 #include <ctype.h>
93 #endif
94 #include <sys/types.h>
95 #include <stdarg.h>
96 #ifdef HAVE_STDLIB_H
97 #include <stdlib.h>
98 #endif
100 #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF) && defined(HAVE_C99_VSNPRINTF)
101 /* only include stdio.h if we are not re-defining snprintf or vsnprintf */
102 #include <stdio.h>
103 /* make the compiler happy with an empty file */
104 void dummy_snprintf(void) {}
105 #else
107 #ifdef HAVE_LONG_DOUBLE
108 #define LDOUBLE long double
109 #else
110 #define LDOUBLE double
111 #endif
113 #ifdef HAVE_LONG_LONG
114 #define LLONG long long
115 #else
116 #define LLONG long
117 #endif
119 static size_t dopr(char *buffer, size_t maxlen, const char *format,
120 va_list args);
121 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
122 char *value, int flags, int min, int max);
123 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
124 long value, int base, int min, int max, int flags);
125 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
126 LDOUBLE fvalue, int min, int max, int flags);
127 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
130 * dopr(): poor man's version of doprintf
133 /* format read states */
134 #define DP_S_DEFAULT 0
135 #define DP_S_FLAGS 1
136 #define DP_S_MIN 2
137 #define DP_S_DOT 3
138 #define DP_S_MAX 4
139 #define DP_S_MOD 5
140 #define DP_S_CONV 6
141 #define DP_S_DONE 7
143 /* format flags - Bits */
144 #define DP_F_MINUS (1 << 0)
145 #define DP_F_PLUS (1 << 1)
146 #define DP_F_SPACE (1 << 2)
147 #define DP_F_NUM (1 << 3)
148 #define DP_F_ZERO (1 << 4)
149 #define DP_F_UP (1 << 5)
150 #define DP_F_UNSIGNED (1 << 6)
152 /* Conversion Flags */
153 #define DP_C_SHORT 1
154 #define DP_C_LONG 2
155 #define DP_C_LDOUBLE 3
156 #define DP_C_LLONG 4
158 #define char_to_int(p) ((p)- '0')
159 #ifndef MAX
160 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
161 #endif
163 static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
165 char ch;
166 LLONG value;
167 LDOUBLE fvalue;
168 char *strvalue;
169 int min;
170 int max;
171 int state;
172 int flags;
173 int cflags;
174 size_t currlen;
176 state = DP_S_DEFAULT;
177 currlen = flags = cflags = min = 0;
178 max = -1;
179 ch = *format++;
181 while (state != DP_S_DONE) {
182 if (ch == '\0')
183 state = DP_S_DONE;
185 switch(state) {
186 case DP_S_DEFAULT:
187 if (ch == '%')
188 state = DP_S_FLAGS;
189 else
190 dopr_outch (buffer, &currlen, maxlen, ch);
191 ch = *format++;
192 break;
193 case DP_S_FLAGS:
194 switch (ch) {
195 case '-':
196 flags |= DP_F_MINUS;
197 ch = *format++;
198 break;
199 case '+':
200 flags |= DP_F_PLUS;
201 ch = *format++;
202 break;
203 case ' ':
204 flags |= DP_F_SPACE;
205 ch = *format++;
206 break;
207 case '#':
208 flags |= DP_F_NUM;
209 ch = *format++;
210 break;
211 case '0':
212 flags |= DP_F_ZERO;
213 ch = *format++;
214 break;
215 default:
216 state = DP_S_MIN;
217 break;
219 break;
220 case DP_S_MIN:
221 if (isdigit((unsigned char)ch)) {
222 min = 10*min + char_to_int (ch);
223 ch = *format++;
224 } else if (ch == '*') {
225 min = va_arg (args, int);
226 ch = *format++;
227 state = DP_S_DOT;
228 } else {
229 state = DP_S_DOT;
231 break;
232 case DP_S_DOT:
233 if (ch == '.') {
234 state = DP_S_MAX;
235 ch = *format++;
236 } else {
237 state = DP_S_MOD;
239 break;
240 case DP_S_MAX:
241 if (isdigit((unsigned char)ch)) {
242 if (max < 0)
243 max = 0;
244 max = 10*max + char_to_int (ch);
245 ch = *format++;
246 } else if (ch == '*') {
247 max = va_arg (args, int);
248 ch = *format++;
249 state = DP_S_MOD;
250 } else {
251 state = DP_S_MOD;
253 break;
254 case DP_S_MOD:
255 switch (ch) {
256 case 'h':
257 cflags = DP_C_SHORT;
258 ch = *format++;
259 break;
260 case 'l':
261 cflags = DP_C_LONG;
262 ch = *format++;
263 if (ch == 'l') { /* It's a long long */
264 cflags = DP_C_LLONG;
265 ch = *format++;
267 break;
268 case 'L':
269 cflags = DP_C_LDOUBLE;
270 ch = *format++;
271 break;
272 default:
273 break;
275 state = DP_S_CONV;
276 break;
277 case DP_S_CONV:
278 switch (ch) {
279 case 'd':
280 case 'i':
281 if (cflags == DP_C_SHORT)
282 value = va_arg (args, int);
283 else if (cflags == DP_C_LONG)
284 value = va_arg (args, long int);
285 else if (cflags == DP_C_LLONG)
286 value = va_arg (args, LLONG);
287 else
288 value = va_arg (args, int);
289 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
290 break;
291 case 'o':
292 flags |= DP_F_UNSIGNED;
293 if (cflags == DP_C_SHORT)
294 value = va_arg (args, unsigned int);
295 else if (cflags == DP_C_LONG)
296 value = (long)va_arg (args, unsigned long int);
297 else if (cflags == DP_C_LLONG)
298 value = (long)va_arg (args, unsigned LLONG);
299 else
300 value = (long)va_arg (args, unsigned int);
301 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
302 break;
303 case 'u':
304 flags |= DP_F_UNSIGNED;
305 if (cflags == DP_C_SHORT)
306 value = va_arg (args, unsigned int);
307 else if (cflags == DP_C_LONG)
308 value = (long)va_arg (args, unsigned long int);
309 else if (cflags == DP_C_LLONG)
310 value = (LLONG)va_arg (args, unsigned LLONG);
311 else
312 value = (long)va_arg (args, unsigned int);
313 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
314 break;
315 case 'X':
316 flags |= DP_F_UP;
317 case 'x':
318 flags |= DP_F_UNSIGNED;
319 if (cflags == DP_C_SHORT)
320 value = va_arg (args, unsigned int);
321 else if (cflags == DP_C_LONG)
322 value = (long)va_arg (args, unsigned long int);
323 else if (cflags == DP_C_LLONG)
324 value = (LLONG)va_arg (args, unsigned LLONG);
325 else
326 value = (long)va_arg (args, unsigned int);
327 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
328 break;
329 case 'f':
330 if (cflags == DP_C_LDOUBLE)
331 fvalue = va_arg (args, LDOUBLE);
332 else
333 fvalue = va_arg (args, double);
334 /* um, floating point? */
335 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
336 break;
337 case 'E':
338 flags |= DP_F_UP;
339 case 'e':
340 if (cflags == DP_C_LDOUBLE)
341 fvalue = va_arg (args, LDOUBLE);
342 else
343 fvalue = va_arg (args, double);
344 break;
345 case 'G':
346 flags |= DP_F_UP;
347 case 'g':
348 if (cflags == DP_C_LDOUBLE)
349 fvalue = va_arg (args, LDOUBLE);
350 else
351 fvalue = va_arg (args, double);
352 break;
353 case 'c':
354 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
355 break;
356 case 's':
357 strvalue = va_arg (args, char *);
358 if (max == -1) {
359 max = strlen(strvalue);
361 if (min > 0 && max >= 0 && min > max) max = min;
362 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
363 break;
364 case 'p':
365 strvalue = va_arg (args, void *);
366 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
367 break;
368 case 'n':
369 if (cflags == DP_C_SHORT) {
370 short int *num;
371 num = va_arg (args, short int *);
372 *num = currlen;
373 } else if (cflags == DP_C_LONG) {
374 long int *num;
375 num = va_arg (args, long int *);
376 *num = (long int)currlen;
377 } else if (cflags == DP_C_LLONG) {
378 LLONG *num;
379 num = va_arg (args, LLONG *);
380 *num = (LLONG)currlen;
381 } else {
382 int *num;
383 num = va_arg (args, int *);
384 *num = currlen;
386 break;
387 case '%':
388 dopr_outch (buffer, &currlen, maxlen, ch);
389 break;
390 case 'w':
391 /* not supported yet, treat as next char */
392 ch = *format++;
393 break;
394 default:
395 /* Unknown, skip */
396 break;
398 ch = *format++;
399 state = DP_S_DEFAULT;
400 flags = cflags = min = 0;
401 max = -1;
402 break;
403 case DP_S_DONE:
404 break;
405 default:
406 /* hmm? */
407 break; /* some picky compilers need this */
410 if (maxlen != 0) {
411 if (currlen < maxlen - 1)
412 buffer[currlen] = '\0';
413 else if (maxlen > 0)
414 buffer[maxlen - 1] = '\0';
417 return currlen;
420 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
421 char *value, int flags, int min, int max)
423 int padlen, strln; /* amount to pad */
424 int cnt = 0;
426 #ifdef DEBUG_SNPRINTF
427 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
428 #endif
429 if (value == 0) {
430 value = "<NULL>";
433 for (strln = 0; value[strln]; ++strln); /* strlen */
434 padlen = min - strln;
435 if (padlen < 0)
436 padlen = 0;
437 if (flags & DP_F_MINUS)
438 padlen = -padlen; /* Left Justify */
440 while ((padlen > 0) && (cnt < max)) {
441 dopr_outch (buffer, currlen, maxlen, ' ');
442 --padlen;
443 ++cnt;
445 while (*value && (cnt < max)) {
446 dopr_outch (buffer, currlen, maxlen, *value++);
447 ++cnt;
449 while ((padlen < 0) && (cnt < max)) {
450 dopr_outch (buffer, currlen, maxlen, ' ');
451 ++padlen;
452 ++cnt;
456 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
458 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
459 long value, int base, int min, int max, int flags)
461 int signvalue = 0;
462 unsigned long uvalue;
463 char convert[20];
464 int place = 0;
465 int spadlen = 0; /* amount to space pad */
466 int zpadlen = 0; /* amount to zero pad */
467 int caps = 0;
469 if (max < 0)
470 max = 0;
472 uvalue = value;
474 if(!(flags & DP_F_UNSIGNED)) {
475 if( value < 0 ) {
476 signvalue = '-';
477 uvalue = -value;
478 } else {
479 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
480 signvalue = '+';
481 else if (flags & DP_F_SPACE)
482 signvalue = ' ';
486 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
488 do {
489 convert[place++] =
490 (caps? "0123456789ABCDEF":"0123456789abcdef")
491 [uvalue % (unsigned)base ];
492 uvalue = (uvalue / (unsigned)base );
493 } while(uvalue && (place < 20));
494 if (place == 20) place--;
495 convert[place] = 0;
497 zpadlen = max - place;
498 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
499 if (zpadlen < 0) zpadlen = 0;
500 if (spadlen < 0) spadlen = 0;
501 if (flags & DP_F_ZERO) {
502 zpadlen = MAX(zpadlen, spadlen);
503 spadlen = 0;
505 if (flags & DP_F_MINUS)
506 spadlen = -spadlen; /* Left Justifty */
508 #ifdef DEBUG_SNPRINTF
509 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
510 zpadlen, spadlen, min, max, place);
511 #endif
513 /* Spaces */
514 while (spadlen > 0) {
515 dopr_outch (buffer, currlen, maxlen, ' ');
516 --spadlen;
519 /* Sign */
520 if (signvalue)
521 dopr_outch (buffer, currlen, maxlen, signvalue);
523 /* Zeros */
524 if (zpadlen > 0) {
525 while (zpadlen > 0) {
526 dopr_outch (buffer, currlen, maxlen, '0');
527 --zpadlen;
531 /* Digits */
532 while (place > 0)
533 dopr_outch (buffer, currlen, maxlen, convert[--place]);
535 /* Left Justified spaces */
536 while (spadlen < 0) {
537 dopr_outch (buffer, currlen, maxlen, ' ');
538 ++spadlen;
542 static LDOUBLE abs_val(LDOUBLE value)
544 LDOUBLE result = value;
546 if (value < 0)
547 result = -value;
549 return result;
552 static LDOUBLE POW10(int exp)
554 LDOUBLE result = 1;
556 while (exp) {
557 result *= 10;
558 exp--;
561 return result;
564 static LLONG ROUND(LDOUBLE value)
566 LLONG intpart;
568 intpart = (LLONG)value;
569 value = value - intpart;
570 if (value >= 0.5) intpart++;
572 return intpart;
575 /* a replacement for modf that doesn't need the math library. Should
576 be portable, but slow */
577 static double my_modf(double x0, double *iptr)
579 int i;
580 long l;
581 double x = x0;
582 double f = 1.0;
584 for (i=0;i<100;i ++) {
585 l = (long)x;
586 if (l <= (x+1) && l >= (x-1)) break;
587 x *= 0.1;
588 f *= 10.0;
591 if (i == 100) {
592 /* yikes! the number is beyond what we can handle. What do we do? */
593 (*iptr) = 0;
594 return 0;
597 if (i != 0) {
598 double i2;
599 double ret;
601 ret = my_modf(x0-l*f, &i2);
602 (*iptr) = l*f + i2;
603 return ret;
606 (*iptr) = l;
607 return x - (*iptr);
611 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
612 LDOUBLE fvalue, int min, int max, int flags)
614 int signvalue = 0;
615 double ufvalue;
616 char iconvert[311];
617 char fconvert[311];
618 int iplace = 0;
619 int fplace = 0;
620 int padlen = 0; /* amount to pad */
621 int zpadlen = 0;
622 int caps = 0;
623 int index;
624 double intpart;
625 double fracpart;
626 double temp;
629 * AIX manpage says the default is 0, but Solaris says the default
630 * is 6, and sprintf on AIX defaults to 6
632 if (max < 0)
633 max = 6;
635 ufvalue = abs_val (fvalue);
637 if (fvalue < 0) {
638 signvalue = '-';
639 } else {
640 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
641 signvalue = '+';
642 } else {
643 if (flags & DP_F_SPACE)
644 signvalue = ' ';
648 #if 0
649 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
650 #endif
652 #if 0
653 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
654 #endif
657 * Sorry, we only support 16 digits past the decimal because of our
658 * conversion method
660 if (max > 16)
661 max = 16;
663 /* We "cheat" by converting the fractional part to integer by
664 * multiplying by a factor of 10
667 temp = ufvalue;
668 my_modf(temp, &intpart);
670 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
672 if (fracpart >= POW10(max)) {
673 intpart++;
674 fracpart -= POW10(max);
678 /* Convert integer part */
679 do {
680 temp = intpart;
681 my_modf(intpart*0.1, &intpart);
682 temp = temp*0.1;
683 index = (int) ((temp -intpart +0.05)* 10.0);
684 /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
685 /* printf ("%llf, %f, %x\n", temp, intpart, index); */
686 iconvert[iplace++] =
687 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
688 } while (intpart && (iplace < 311));
689 if (iplace == 311) iplace--;
690 iconvert[iplace] = 0;
692 /* Convert fractional part */
693 if (fracpart)
695 do {
696 temp = fracpart;
697 my_modf(fracpart*0.1, &fracpart);
698 temp = temp*0.1;
699 index = (int) ((temp -fracpart +0.05)* 10.0);
700 /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
701 /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
702 fconvert[fplace++] =
703 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
704 } while(fracpart && (fplace < 311));
705 if (fplace == 311) fplace--;
707 fconvert[fplace] = 0;
709 /* -1 for decimal point, another -1 if we are printing a sign */
710 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
711 zpadlen = max - fplace;
712 if (zpadlen < 0) zpadlen = 0;
713 if (padlen < 0)
714 padlen = 0;
715 if (flags & DP_F_MINUS)
716 padlen = -padlen; /* Left Justifty */
718 if ((flags & DP_F_ZERO) && (padlen > 0)) {
719 if (signvalue) {
720 dopr_outch (buffer, currlen, maxlen, signvalue);
721 --padlen;
722 signvalue = 0;
724 while (padlen > 0) {
725 dopr_outch (buffer, currlen, maxlen, '0');
726 --padlen;
729 while (padlen > 0) {
730 dopr_outch (buffer, currlen, maxlen, ' ');
731 --padlen;
733 if (signvalue)
734 dopr_outch (buffer, currlen, maxlen, signvalue);
736 while (iplace > 0)
737 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
739 #ifdef DEBUG_SNPRINTF
740 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
741 #endif
744 * Decimal point. This should probably use locale to find the correct
745 * char to print out.
747 if (max > 0) {
748 dopr_outch (buffer, currlen, maxlen, '.');
750 while (fplace > 0)
751 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
754 while (zpadlen > 0) {
755 dopr_outch (buffer, currlen, maxlen, '0');
756 --zpadlen;
759 while (padlen < 0) {
760 dopr_outch (buffer, currlen, maxlen, ' ');
761 ++padlen;
765 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
767 if (*currlen < maxlen) {
768 buffer[(*currlen)] = c;
770 (*currlen)++;
773 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
774 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
776 return dopr(str, count, fmt, args);
778 #endif
780 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
781 int snprintf(char *str,size_t count,const char *fmt,...)
783 size_t ret;
784 va_list ap;
786 va_start(ap, fmt);
787 ret = vsnprintf(str, count, fmt, ap);
788 va_end(ap);
789 return ret;
791 #endif
793 #endif
795 #ifndef HAVE_VASPRINTF
796 int vasprintf(char **ptr, const char *format, va_list ap)
798 int ret;
800 ret = vsnprintf(NULL, 0, format, ap);
801 if (ret <= 0) return ret;
803 (*ptr) = (char *)io_calloc(1, ret+1);
804 if (!*ptr) return -1;
805 ret = vsnprintf(*ptr, ret+1, format, ap);
807 return ret;
809 #endif
812 #ifndef HAVE_ASPRINTF
813 int asprintf(char **ptr, const char *format, ...)
815 va_list ap;
816 int ret;
818 va_start(ap, format);
819 ret = vasprintf(ptr, format, ap);
820 va_end(ap);
822 return ret;
824 #endif
826 #ifdef TEST_SNPRINTF
828 int sprintf(char *str,const char *fmt,...);
830 int main (void)
832 char buf1[1024];
833 char buf2[1024];
834 char *fp_fmt[] = {
835 "%1.1f",
836 "%-1.5f",
837 "%1.5f",
838 "%123.9f",
839 "%10.5f",
840 "% 10.5f",
841 "%+22.9f",
842 "%+4.9f",
843 "%01.3f",
844 "%4f",
845 "%3.1f",
846 "%3.2f",
847 "%.0f",
848 "%f",
849 "-16.16f",
850 NULL
852 double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
853 0.9996, 1.996, 4.136, 0};
854 char *int_fmt[] = {
855 "%-1.5d",
856 "%1.5d",
857 "%123.9d",
858 "%5.5d",
859 "%10.5d",
860 "% 10.5d",
861 "%+22.33d",
862 "%01.3d",
863 "%4d",
864 "%d",
865 NULL
867 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
868 char *str_fmt[] = {
869 "10.5s",
870 "5.10s",
871 "10.1s",
872 "0.10s",
873 "10.0s",
874 "1.10s",
875 "%s",
876 "%.1s",
877 "%.10s",
878 "%10s",
879 NULL
881 char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
882 int x, y;
883 int fail = 0;
884 int num = 0;
886 printf ("Testing snprintf format codes against system sprintf...\n");
888 for (x = 0; fp_fmt[x] ; x++) {
889 for (y = 0; fp_nums[y] != 0 ; y++) {
890 int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
891 int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
892 sprintf (buf2, fp_fmt[x], fp_nums[y]);
893 if (strcmp (buf1, buf2)) {
894 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
895 fp_fmt[x], buf1, buf2);
896 fail++;
898 if (l1 != l2) {
899 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
900 fail++;
902 num++;
906 for (x = 0; int_fmt[x] ; x++) {
907 for (y = 0; int_nums[y] != 0 ; y++) {
908 int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
909 int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
910 sprintf (buf2, int_fmt[x], int_nums[y]);
911 if (strcmp (buf1, buf2)) {
912 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
913 int_fmt[x], buf1, buf2);
914 fail++;
916 if (l1 != l2) {
917 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
918 fail++;
920 num++;
924 for (x = 0; str_fmt[x] ; x++) {
925 for (y = 0; str_vals[y] != 0 ; y++) {
926 int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
927 int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
928 sprintf (buf2, str_fmt[x], str_vals[y]);
929 if (strcmp (buf1, buf2)) {
930 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
931 str_fmt[x], buf1, buf2);
932 fail++;
934 if (l1 != l2) {
935 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
936 fail++;
938 num++;
942 printf ("%d tests failed out of %d.\n", fail, num);
944 printf("seeing how many digits we support\n");
946 double v0 = 0.12345678901234567890123456789012345678901;
947 for (x=0; x<100; x++) {
948 snprintf(buf1, sizeof(buf1), "%1.1f", v0*pow(10, x));
949 sprintf(buf2, "%1.1f", v0*pow(10, x));
950 if (strcmp(buf1, buf2)) {
951 printf("we seem to support %d digits\n", x-1);
952 break;
957 return 0;
959 #endif /* SNPRINTF_TEST */