r18643: Fix a 64-bit warning
[Samba/nascimento.git] / source / lib / snprintf.c
blob02202283d4a413bd6fc6f4d76a3e1e6c9d956897
1 /*
2 * NOTE: If you change this file, please merge it into rsync, samba, etc.
3 */
5 /*
6 * Copyright Patrick Powell 1995
7 * This code is based on code written by Patrick Powell (papowell@astart.com)
8 * It may be used for any purpose as long as this notice remains intact
9 * on all source code distributions
12 /**************************************************************
13 * Original:
14 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
15 * A bombproof version of doprnt (dopr) included.
16 * Sigh. This sort of thing is always nasty do deal with. Note that
17 * the version here does not include floating point...
19 * snprintf() is used instead of sprintf() as it does limit checks
20 * for string length. This covers a nasty loophole.
22 * The other functions are there to prevent NULL pointers from
23 * causing nast effects.
25 * More Recently:
26 * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
27 * This was ugly. It is still ugly. I opted out of floating point
28 * numbers, but the formatter understands just about everything
29 * from the normal C string format, at least as far as I can tell from
30 * the Solaris 2.5 printf(3S) man page.
32 * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
33 * Ok, added some minimal floating point support, which means this
34 * probably requires libm on most operating systems. Don't yet
35 * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
36 * was pretty badly broken, it just wasn't being exercised in ways
37 * which showed it, so that's been fixed. Also, formated the code
38 * to mutt conventions, and removed dead code left over from the
39 * original. Also, there is now a builtin-test, just compile with:
40 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
41 * and run snprintf for results.
43 * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
44 * The PGP code was using unsigned hexadecimal formats.
45 * Unfortunately, unsigned formats simply didn't work.
47 * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
48 * The original code assumed that both snprintf() and vsnprintf() were
49 * missing. Some systems only have snprintf() but not vsnprintf(), so
50 * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
52 * Andrew Tridgell (tridge@samba.org) Oct 1998
53 * fixed handling of %.0f
54 * added test for HAVE_LONG_DOUBLE
56 * tridge@samba.org, idra@samba.org, April 2001
57 * got rid of fcvt code (twas buggy and made testing harder)
58 * added C99 semantics
60 * date: 2002/12/19 19:56:31; author: herb; state: Exp; lines: +2 -0
61 * actually print args for %g and %e
63 * date: 2002/06/03 13:37:52; author: jmcd; state: Exp; lines: +8 -0
64 * Since includes.h isn't included here, VA_COPY has to be defined here. I don't
65 * see any include file that is guaranteed to be here, so I'm defining it
66 * locally. Fixes AIX and Solaris builds.
68 * date: 2002/06/03 03:07:24; author: tridge; state: Exp; lines: +5 -13
69 * put the ifdef for HAVE_VA_COPY in one place rather than in lots of
70 * functions
72 * date: 2002/05/17 14:51:22; author: jmcd; state: Exp; lines: +21 -4
73 * Fix usage of va_list passed as an arg. Use __va_copy before using it
74 * when it exists.
76 * date: 2002/04/16 22:38:04; author: idra; state: Exp; lines: +20 -14
77 * Fix incorrect zpadlen handling in fmtfp.
78 * Thanks to Ollie Oldham <ollie.oldham@metro-optix.com> for spotting it.
79 * few mods to make it easier to compile the tests.
80 * addedd the "Ollie" test to the floating point ones.
82 * Martin Pool (mbp@samba.org) April 2003
83 * Remove NO_CONFIG_H so that the test case can be built within a source
84 * tree with less trouble.
85 * Remove unnecessary SAFE_FREE() definition.
87 * Martin Pool (mbp@samba.org) May 2003
88 * Put in a prototype for dummy_snprintf() to quiet compiler warnings.
90 * Move #endif to make sure VA_COPY, LDOUBLE, etc are defined even
91 * if the C library has some snprintf functions already.
93 * Darren Tucker (dtucker@zip.com.au) 2005
94 * Fix bug allowing read overruns of the source string with "%.*s"
95 * Usually harmless unless the read runs outside the process' allocation
96 * (eg if your malloc does guard pages) in which case it will segfault.
97 * From OpenSSH. Also added test for same.
99 * Simo Sorce (idra@samba.org) Jan 2006
101 * Add support for position independent parameters
102 * fix fmtstr now it conforms to sprintf wrt min.max
104 **************************************************************/
106 #ifndef NO_CONFIG_H
107 #include "config.h"
108 #else
109 #define NULL 0
110 #endif
112 #ifdef TEST_SNPRINTF /* need math library headers for testing */
114 /* In test mode, we pretend that this system doesn't have any snprintf
115 * functions, regardless of what config.h says. */
116 # undef HAVE_SNPRINTF
117 # undef HAVE_VSNPRINTF
118 # undef HAVE_C99_VSNPRINTF
119 # undef HAVE_ASPRINTF
120 # undef HAVE_VASPRINTF
121 # include <math.h>
122 #endif /* TEST_SNPRINTF */
124 #ifdef HAVE_STRING_H
125 #include <string.h>
126 #endif
128 #ifdef HAVE_STRINGS_H
129 #include <strings.h>
130 #endif
131 #ifdef HAVE_CTYPE_H
132 #include <ctype.h>
133 #endif
134 #include <sys/types.h>
135 #include <stdarg.h>
136 #ifdef HAVE_STDLIB_H
137 #include <stdlib.h>
138 #endif
140 #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF) && defined(HAVE_C99_VSNPRINTF)
141 /* only include stdio.h if we are not re-defining snprintf or vsnprintf */
142 #include <stdio.h>
143 /* make the compiler happy with an empty file */
144 void dummy_snprintf(void);
145 void dummy_snprintf(void) {}
146 #endif /* HAVE_SNPRINTF, etc */
148 #ifdef HAVE_LONG_DOUBLE
149 #define LDOUBLE long double
150 #else
151 #define LDOUBLE double
152 #endif
154 #ifdef HAVE_LONG_LONG
155 #define LLONG long long
156 #else
157 #define LLONG long
158 #endif
160 #ifndef VA_COPY
161 #ifdef HAVE_VA_COPY
162 #define VA_COPY(dest, src) va_copy(dest, src)
163 #else
164 #ifdef HAVE___VA_COPY
165 #define VA_COPY(dest, src) __va_copy(dest, src)
166 #else
167 #define VA_COPY(dest, src) (dest) = (src)
168 #endif
169 #endif
172 * dopr(): poor man's version of doprintf
175 /* format read states */
176 #define DP_S_DEFAULT 0
177 #define DP_S_FLAGS 1
178 #define DP_S_MIN 2
179 #define DP_S_DOT 3
180 #define DP_S_MAX 4
181 #define DP_S_MOD 5
182 #define DP_S_CONV 6
183 #define DP_S_DONE 7
185 /* format flags - Bits */
186 #define DP_F_MINUS (1 << 0)
187 #define DP_F_PLUS (1 << 1)
188 #define DP_F_SPACE (1 << 2)
189 #define DP_F_NUM (1 << 3)
190 #define DP_F_ZERO (1 << 4)
191 #define DP_F_UP (1 << 5)
192 #define DP_F_UNSIGNED (1 << 6)
194 /* Conversion Flags */
195 #define DP_C_CHAR 1
196 #define DP_C_SHORT 2
197 #define DP_C_LONG 3
198 #define DP_C_LDOUBLE 4
199 #define DP_C_LLONG 5
200 #define DP_C_SIZET 6
202 /* Chunk types */
203 #define CNK_FMT_STR 0
204 #define CNK_INT 1
205 #define CNK_OCTAL 2
206 #define CNK_UINT 3
207 #define CNK_HEX 4
208 #define CNK_FLOAT 5
209 #define CNK_CHAR 6
210 #define CNK_STRING 7
211 #define CNK_PTR 8
212 #define CNK_NUM 9
213 #define CNK_PRCNT 10
215 #define char_to_int(p) ((p)- '0')
216 #ifndef MAX
217 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
218 #endif
220 /* yes this really must be a ||. Don't muck with this (tridge) */
221 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
223 struct pr_chunk {
224 int type; /* chunk type */
225 int num; /* parameter number */
226 int min;
227 int max;
228 int flags;
229 int cflags;
230 int start;
231 int len;
232 LLONG value;
233 LDOUBLE fvalue;
234 char *strvalue;
235 void *pnum;
236 struct pr_chunk *min_star;
237 struct pr_chunk *max_star;
238 struct pr_chunk *next;
241 struct pr_chunk_x {
242 struct pr_chunk **chunks;
243 int num;
246 static size_t dopr(char *buffer, size_t maxlen, const char *format,
247 va_list args_in);
248 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
249 char *value, int flags, int min, int max);
250 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
251 LLONG value, int base, int min, int max, int flags);
252 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
253 LDOUBLE fvalue, int min, int max, int flags);
254 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
255 static struct pr_chunk *new_chunk(void);
256 static int add_cnk_list_entry(struct pr_chunk_x **list,
257 int max_num, struct pr_chunk *chunk);
259 static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
261 char ch;
262 int state;
263 int pflag;
264 int pnum;
265 int pfirst;
266 size_t currlen;
267 va_list args;
268 const char *base;
269 struct pr_chunk *chunks = NULL;
270 struct pr_chunk *cnk = NULL;
271 struct pr_chunk_x *clist = NULL;
272 int max_pos;
273 size_t ret = -1;
275 VA_COPY(args, args_in);
277 state = DP_S_DEFAULT;
278 pfirst = 1;
279 pflag = 0;
280 pnum = 0;
282 max_pos = 0;
283 base = format;
284 ch = *format++;
286 /* retrieve the string structure as chunks */
287 while (state != DP_S_DONE) {
288 if (ch == '\0')
289 state = DP_S_DONE;
291 switch(state) {
292 case DP_S_DEFAULT:
294 if (cnk) {
295 cnk->next = new_chunk();
296 cnk = cnk->next;
297 } else {
298 cnk = new_chunk();
300 if (!cnk) goto done;
301 if (!chunks) chunks = cnk;
303 if (ch == '%') {
304 state = DP_S_FLAGS;
305 ch = *format++;
306 } else {
307 cnk->type = CNK_FMT_STR;
308 cnk->start = format - base -1;
309 while ((ch != '\0') && (ch != '%')) ch = *format++;
310 cnk->len = format - base - cnk->start -1;
312 break;
313 case DP_S_FLAGS:
314 switch (ch) {
315 case '-':
316 cnk->flags |= DP_F_MINUS;
317 ch = *format++;
318 break;
319 case '+':
320 cnk->flags |= DP_F_PLUS;
321 ch = *format++;
322 break;
323 case ' ':
324 cnk->flags |= DP_F_SPACE;
325 ch = *format++;
326 break;
327 case '#':
328 cnk->flags |= DP_F_NUM;
329 ch = *format++;
330 break;
331 case '0':
332 cnk->flags |= DP_F_ZERO;
333 ch = *format++;
334 break;
335 case 'I':
336 /* internationalization not supported yet */
337 ch = *format++;
338 break;
339 default:
340 state = DP_S_MIN;
341 break;
343 break;
344 case DP_S_MIN:
345 if (isdigit((unsigned char)ch)) {
346 cnk->min = 10 * cnk->min + char_to_int (ch);
347 ch = *format++;
348 } else if (ch == '$') {
349 if (!pfirst && !pflag) {
350 /* parameters must be all positioned or none */
351 goto done;
353 if (pfirst) {
354 pfirst = 0;
355 pflag = 1;
357 if (cnk->min == 0) /* what ?? */
358 goto done;
359 cnk->num = cnk->min;
360 cnk->min = 0;
361 ch = *format++;
362 } else if (ch == '*') {
363 if (pfirst) pfirst = 0;
364 cnk->min_star = new_chunk();
365 if (!cnk->min_star) /* out of memory :-( */
366 goto done;
367 cnk->min_star->type = CNK_INT;
368 if (pflag) {
369 int num;
370 ch = *format++;
371 if (!isdigit((unsigned char)ch)) {
372 /* parameters must be all positioned or none */
373 goto done;
375 for (num = 0; isdigit((unsigned char)ch); ch = *format++) {
376 num = 10 * num + char_to_int(ch);
378 cnk->min_star->num = num;
379 if (ch != '$') /* what ?? */
380 goto done;
381 } else {
382 cnk->min_star->num = ++pnum;
384 max_pos = add_cnk_list_entry(&clist, max_pos, cnk->min_star);
385 if (max_pos == 0) /* out of memory :-( */
386 goto done;
387 ch = *format++;
388 state = DP_S_DOT;
389 } else {
390 if (pfirst) pfirst = 0;
391 state = DP_S_DOT;
393 break;
394 case DP_S_DOT:
395 if (ch == '.') {
396 state = DP_S_MAX;
397 ch = *format++;
398 } else {
399 state = DP_S_MOD;
401 break;
402 case DP_S_MAX:
403 if (isdigit((unsigned char)ch)) {
404 if (cnk->max < 0)
405 cnk->max = 0;
406 cnk->max = 10 * cnk->max + char_to_int (ch);
407 ch = *format++;
408 } else if (ch == '$') {
409 if (!pfirst && !pflag) {
410 /* parameters must be all positioned or none */
411 goto done;
413 if (cnk->max <= 0) /* what ?? */
414 goto done;
415 cnk->num = cnk->max;
416 cnk->max = -1;
417 ch = *format++;
418 } else if (ch == '*') {
419 cnk->max_star = new_chunk();
420 if (!cnk->max_star) /* out of memory :-( */
421 goto done;
422 cnk->max_star->type = CNK_INT;
423 if (pflag) {
424 int num;
425 ch = *format++;
426 if (!isdigit((unsigned char)ch)) {
427 /* parameters must be all positioned or none */
428 goto done;
430 for (num = 0; isdigit((unsigned char)ch); ch = *format++) {
431 num = 10 * num + char_to_int(ch);
433 cnk->max_star->num = num;
434 if (ch != '$') /* what ?? */
435 goto done;
436 } else {
437 cnk->max_star->num = ++pnum;
439 max_pos = add_cnk_list_entry(&clist, max_pos, cnk->max_star);
440 if (max_pos == 0) /* out of memory :-( */
441 goto done;
443 ch = *format++;
444 state = DP_S_MOD;
445 } else {
446 state = DP_S_MOD;
448 break;
449 case DP_S_MOD:
450 switch (ch) {
451 case 'h':
452 cnk->cflags = DP_C_SHORT;
453 ch = *format++;
454 if (ch == 'h') {
455 cnk->cflags = DP_C_CHAR;
456 ch = *format++;
458 break;
459 case 'l':
460 cnk->cflags = DP_C_LONG;
461 ch = *format++;
462 if (ch == 'l') { /* It's a long long */
463 cnk->cflags = DP_C_LLONG;
464 ch = *format++;
466 break;
467 case 'L':
468 cnk->cflags = DP_C_LDOUBLE;
469 ch = *format++;
470 break;
471 case 'z':
472 cnk->cflags = DP_C_SIZET;
473 ch = *format++;
474 break;
475 default:
476 break;
478 state = DP_S_CONV;
479 break;
480 case DP_S_CONV:
481 if (cnk->num == 0) cnk->num = ++pnum;
482 max_pos = add_cnk_list_entry(&clist, max_pos, cnk);
483 if (max_pos == 0) /* out of memory :-( */
484 goto done;
486 switch (ch) {
487 case 'd':
488 case 'i':
489 cnk->type = CNK_INT;
490 break;
491 case 'o':
492 cnk->type = CNK_OCTAL;
493 cnk->flags |= DP_F_UNSIGNED;
494 break;
495 case 'u':
496 cnk->type = CNK_UINT;
497 cnk->flags |= DP_F_UNSIGNED;
498 break;
499 case 'X':
500 cnk->flags |= DP_F_UP;
501 case 'x':
502 cnk->type = CNK_HEX;
503 cnk->flags |= DP_F_UNSIGNED;
504 break;
505 case 'A':
506 /* hex float not supported yet */
507 case 'E':
508 case 'G':
509 case 'F':
510 cnk->flags |= DP_F_UP;
511 case 'a':
512 /* hex float not supported yet */
513 case 'e':
514 case 'f':
515 case 'g':
516 cnk->type = CNK_FLOAT;
517 break;
518 case 'c':
519 cnk->type = CNK_CHAR;
520 break;
521 case 's':
522 cnk->type = CNK_STRING;
523 break;
524 case 'p':
525 cnk->type = CNK_PTR;
526 break;
527 case 'n':
528 cnk->type = CNK_NUM;
529 break;
530 case '%':
531 cnk->type = CNK_PRCNT;
532 break;
533 default:
534 /* Unknown, bail out*/
535 goto done;
537 ch = *format++;
538 state = DP_S_DEFAULT;
539 break;
540 case DP_S_DONE:
541 break;
542 default:
543 /* hmm? */
544 break; /* some picky compilers need this */
548 /* retieve the format arguments */
549 for (pnum = 0; pnum < max_pos; pnum++) {
550 int i;
552 if (clist[pnum].num == 0) {
553 /* ignoring a parameter should not be permitted
554 * all parameters must be matched at least once
555 * BUT seem some system ignore this rule ...
556 * at least my glibc based system does --SSS
558 #ifdef DEBUG_SNPRINTF
559 printf("parameter at position %d not used\n", pnum+1);
560 #endif
561 /* eat the parameter */
562 va_arg (args, int);
563 continue;
565 for (i = 1; i < clist[pnum].num; i++) {
566 if (clist[pnum].chunks[0]->type != clist[pnum].chunks[i]->type) {
567 /* nooo noo no!
568 * all the references to a parameter
569 * must be of the same type
571 goto done;
574 cnk = clist[pnum].chunks[0];
575 switch (cnk->type) {
576 case CNK_INT:
577 if (cnk->cflags == DP_C_SHORT)
578 cnk->value = va_arg (args, int);
579 else if (cnk->cflags == DP_C_LONG)
580 cnk->value = va_arg (args, long int);
581 else if (cnk->cflags == DP_C_LLONG)
582 cnk->value = va_arg (args, LLONG);
583 else if (cnk->cflags == DP_C_SIZET)
584 cnk->value = va_arg (args, ssize_t);
585 else
586 cnk->value = va_arg (args, int);
588 for (i = 1; i < clist[pnum].num; i++) {
589 clist[pnum].chunks[i]->value = cnk->value;
591 break;
593 case CNK_OCTAL:
594 case CNK_UINT:
595 case CNK_HEX:
596 if (cnk->cflags == DP_C_SHORT)
597 cnk->value = va_arg (args, unsigned int);
598 else if (cnk->cflags == DP_C_LONG)
599 cnk->value = (unsigned long int)va_arg (args, unsigned long int);
600 else if (cnk->cflags == DP_C_LLONG)
601 cnk->value = (LLONG)va_arg (args, unsigned LLONG);
602 else if (cnk->cflags == DP_C_SIZET)
603 cnk->value = (size_t)va_arg (args, size_t);
604 else
605 cnk->value = (unsigned int)va_arg (args, unsigned int);
607 for (i = 1; i < clist[pnum].num; i++) {
608 clist[pnum].chunks[i]->value = cnk->value;
610 break;
612 case CNK_FLOAT:
613 if (cnk->cflags == DP_C_LDOUBLE)
614 cnk->fvalue = va_arg (args, LDOUBLE);
615 else
616 cnk->fvalue = va_arg (args, double);
618 for (i = 1; i < clist[pnum].num; i++) {
619 clist[pnum].chunks[i]->fvalue = cnk->fvalue;
621 break;
623 case CNK_CHAR:
624 cnk->value = va_arg (args, int);
626 for (i = 1; i < clist[pnum].num; i++) {
627 clist[pnum].chunks[i]->value = cnk->value;
629 break;
631 case CNK_STRING:
632 cnk->strvalue = va_arg (args, char *);
633 if (!cnk->strvalue) cnk->strvalue = "(NULL)";
635 for (i = 1; i < clist[pnum].num; i++) {
636 clist[pnum].chunks[i]->strvalue = cnk->strvalue;
638 break;
640 case CNK_PTR:
641 cnk->strvalue = (char *)va_arg (args, void *);
642 for (i = 1; i < clist[pnum].num; i++) {
643 clist[pnum].chunks[i]->strvalue = cnk->strvalue;
645 break;
647 case CNK_NUM:
648 if (cnk->cflags == DP_C_CHAR)
649 cnk->pnum = va_arg (args, char *);
650 else if (cnk->cflags == DP_C_SHORT)
651 cnk->pnum = va_arg (args, short int *);
652 else if (cnk->cflags == DP_C_LONG)
653 cnk->pnum = va_arg (args, long int *);
654 else if (cnk->cflags == DP_C_LLONG)
655 cnk->pnum = va_arg (args, LLONG *);
656 else if (cnk->cflags == DP_C_SIZET)
657 cnk->pnum = va_arg (args, ssize_t *);
658 else
659 cnk->pnum = va_arg (args, int *);
661 for (i = 1; i < clist[pnum].num; i++) {
662 clist[pnum].chunks[i]->pnum = cnk->pnum;
664 break;
666 case CNK_PRCNT:
667 break;
669 default:
670 /* what ?? */
671 goto done;
674 /* print out the actual string from chunks */
675 currlen = 0;
676 cnk = chunks;
677 while (cnk) {
678 int len, min, max;
680 if (cnk->min_star) min = cnk->min_star->value;
681 else min = cnk->min;
682 if (cnk->max_star) max = cnk->max_star->value;
683 else max = cnk->max;
685 switch (cnk->type) {
687 case CNK_FMT_STR:
688 if (maxlen != 0 && maxlen > currlen) {
689 if (maxlen > (currlen + cnk->len)) len = cnk->len;
690 else len = maxlen - currlen;
692 memcpy(&(buffer[currlen]), &(base[cnk->start]), len);
694 currlen += cnk->len;
696 break;
698 case CNK_INT:
699 case CNK_UINT:
700 fmtint (buffer, &currlen, maxlen, cnk->value, 10, min, max, cnk->flags);
701 break;
703 case CNK_OCTAL:
704 fmtint (buffer, &currlen, maxlen, cnk->value, 8, min, max, cnk->flags);
705 break;
707 case CNK_HEX:
708 fmtint (buffer, &currlen, maxlen, cnk->value, 16, min, max, cnk->flags);
709 break;
711 case CNK_FLOAT:
712 fmtfp (buffer, &currlen, maxlen, cnk->fvalue, min, max, cnk->flags);
713 break;
715 case CNK_CHAR:
716 dopr_outch (buffer, &currlen, maxlen, cnk->value);
717 break;
719 case CNK_STRING:
720 if (max == -1) {
721 max = strlen(cnk->strvalue);
723 fmtstr (buffer, &currlen, maxlen, cnk->strvalue, cnk->flags, min, max);
724 break;
726 case CNK_PTR:
727 fmtint (buffer, &currlen, maxlen, (long)(cnk->strvalue), 16, min, max, cnk->flags);
728 break;
730 case CNK_NUM:
731 if (cnk->cflags == DP_C_CHAR)
732 *((char *)(cnk->pnum)) = (char)currlen;
733 else if (cnk->cflags == DP_C_SHORT)
734 *((short int *)(cnk->pnum)) = (short int)currlen;
735 else if (cnk->cflags == DP_C_LONG)
736 *((long int *)(cnk->pnum)) = (long int)currlen;
737 else if (cnk->cflags == DP_C_LLONG)
738 *((LLONG *)(cnk->pnum)) = (LLONG)currlen;
739 else if (cnk->cflags == DP_C_SIZET)
740 *((ssize_t *)(cnk->pnum)) = (ssize_t)currlen;
741 else
742 *((int *)(cnk->pnum)) = (int)currlen;
743 break;
745 case CNK_PRCNT:
746 dopr_outch (buffer, &currlen, maxlen, '%');
747 break;
749 default:
750 /* what ?? */
751 goto done;
753 cnk = cnk->next;
755 if (maxlen != 0) {
756 if (currlen < maxlen - 1)
757 buffer[currlen] = '\0';
758 else if (maxlen > 0)
759 buffer[maxlen - 1] = '\0';
761 ret = currlen;
763 done:
764 while (chunks) {
765 cnk = chunks->next;
766 free(chunks);
767 chunks = cnk;
769 if (clist) {
770 for (pnum = 0; pnum < max_pos; pnum++) {
771 if (clist[pnum].chunks) free(clist[pnum].chunks);
773 free(clist);
775 return ret;
778 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
779 char *value, int flags, int min, int max)
781 int padlen, strln; /* amount to pad */
782 int cnt = 0;
784 #ifdef DEBUG_SNPRINTF
785 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
786 #endif
787 if (value == 0) {
788 value = "<NULL>";
791 for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */
792 padlen = min - strln;
793 if (padlen < 0)
794 padlen = 0;
795 if (flags & DP_F_MINUS)
796 padlen = -padlen; /* Left Justify */
798 while (padlen > 0) {
799 dopr_outch (buffer, currlen, maxlen, ' ');
800 --padlen;
802 while (*value && (cnt < max)) {
803 dopr_outch (buffer, currlen, maxlen, *value++);
804 ++cnt;
806 while (padlen < 0) {
807 dopr_outch (buffer, currlen, maxlen, ' ');
808 ++padlen;
812 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
814 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
815 LLONG value, int base, int min, int max, int flags)
817 int signvalue = 0;
818 unsigned LLONG uvalue;
819 char convert[20];
820 int place = 0;
821 int spadlen = 0; /* amount to space pad */
822 int zpadlen = 0; /* amount to zero pad */
823 int caps = 0;
825 if (max < 0)
826 max = 0;
828 uvalue = value;
830 if(!(flags & DP_F_UNSIGNED)) {
831 if( value < 0 ) {
832 signvalue = '-';
833 uvalue = -value;
834 } else {
835 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
836 signvalue = '+';
837 else if (flags & DP_F_SPACE)
838 signvalue = ' ';
842 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
844 do {
845 convert[place++] =
846 (caps? "0123456789ABCDEF":"0123456789abcdef")
847 [uvalue % (unsigned)base ];
848 uvalue = (uvalue / (unsigned)base );
849 } while(uvalue && (place < 20));
850 if (place == 20) place--;
851 convert[place] = 0;
853 zpadlen = max - place;
854 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
855 if (zpadlen < 0) zpadlen = 0;
856 if (spadlen < 0) spadlen = 0;
857 if (flags & DP_F_ZERO) {
858 zpadlen = MAX(zpadlen, spadlen);
859 spadlen = 0;
861 if (flags & DP_F_MINUS)
862 spadlen = -spadlen; /* Left Justifty */
864 #ifdef DEBUG_SNPRINTF
865 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
866 zpadlen, spadlen, min, max, place);
867 #endif
869 /* Spaces */
870 while (spadlen > 0) {
871 dopr_outch (buffer, currlen, maxlen, ' ');
872 --spadlen;
875 /* Sign */
876 if (signvalue)
877 dopr_outch (buffer, currlen, maxlen, signvalue);
879 /* Zeros */
880 if (zpadlen > 0) {
881 while (zpadlen > 0) {
882 dopr_outch (buffer, currlen, maxlen, '0');
883 --zpadlen;
887 /* Digits */
888 while (place > 0)
889 dopr_outch (buffer, currlen, maxlen, convert[--place]);
891 /* Left Justified spaces */
892 while (spadlen < 0) {
893 dopr_outch (buffer, currlen, maxlen, ' ');
894 ++spadlen;
898 static LDOUBLE abs_val(LDOUBLE value)
900 LDOUBLE result = value;
902 if (value < 0)
903 result = -value;
905 return result;
908 static LDOUBLE POW10(int exp)
910 LDOUBLE result = 1;
912 while (exp) {
913 result *= 10;
914 exp--;
917 return result;
920 static LLONG ROUND(LDOUBLE value)
922 LLONG intpart;
924 intpart = (LLONG)value;
925 value = value - intpart;
926 if (value >= 0.5) intpart++;
928 return intpart;
931 /* a replacement for modf that doesn't need the math library. Should
932 be portable, but slow */
933 static double my_modf(double x0, double *iptr)
935 int i;
936 LLONG l;
937 double x = x0;
938 double f = 1.0;
940 for (i=0;i<100;i++) {
941 l = (long)x;
942 if (l <= (x+1) && l >= (x-1)) break;
943 x *= 0.1;
944 f *= 10.0;
947 if (i == 100) {
948 /* yikes! the number is beyond what we can handle. What do we do? */
949 (*iptr) = 0;
950 return 0;
953 if (i != 0) {
954 double i2;
955 double ret;
957 ret = my_modf(x0-l*f, &i2);
958 (*iptr) = l*f + i2;
959 return ret;
962 (*iptr) = l;
963 return x - (*iptr);
967 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
968 LDOUBLE fvalue, int min, int max, int flags)
970 int signvalue = 0;
971 double ufvalue;
972 char iconvert[311];
973 char fconvert[311];
974 int iplace = 0;
975 int fplace = 0;
976 int padlen = 0; /* amount to pad */
977 int zpadlen = 0;
978 int caps = 0;
979 int idx;
980 double intpart;
981 double fracpart;
982 double temp;
985 * AIX manpage says the default is 0, but Solaris says the default
986 * is 6, and sprintf on AIX defaults to 6
988 if (max < 0)
989 max = 6;
991 ufvalue = abs_val (fvalue);
993 if (fvalue < 0) {
994 signvalue = '-';
995 } else {
996 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
997 signvalue = '+';
998 } else {
999 if (flags & DP_F_SPACE)
1000 signvalue = ' ';
1004 #if 0
1005 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
1006 #endif
1008 #if 0
1009 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
1010 #endif
1013 * Sorry, we only support 9 digits past the decimal because of our
1014 * conversion method
1016 if (max > 9)
1017 max = 9;
1019 /* We "cheat" by converting the fractional part to integer by
1020 * multiplying by a factor of 10
1023 temp = ufvalue;
1024 my_modf(temp, &intpart);
1026 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
1028 if (fracpart >= POW10(max)) {
1029 intpart++;
1030 fracpart -= POW10(max);
1034 /* Convert integer part */
1035 do {
1036 temp = intpart*0.1;
1037 my_modf(temp, &intpart);
1038 idx = (int) ((temp -intpart +0.05)* 10.0);
1039 /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
1040 /* printf ("%llf, %f, %x\n", temp, intpart, idx); */
1041 iconvert[iplace++] =
1042 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
1043 } while (intpart && (iplace < 311));
1044 if (iplace == 311) iplace--;
1045 iconvert[iplace] = 0;
1047 /* Convert fractional part */
1048 if (fracpart)
1050 do {
1051 temp = fracpart*0.1;
1052 my_modf(temp, &fracpart);
1053 idx = (int) ((temp -fracpart +0.05)* 10.0);
1054 /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
1055 /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
1056 fconvert[fplace++] =
1057 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
1058 } while(fracpart && (fplace < 311));
1059 if (fplace == 311) fplace--;
1061 fconvert[fplace] = 0;
1063 /* -1 for decimal point, another -1 if we are printing a sign */
1064 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
1065 zpadlen = max - fplace;
1066 if (zpadlen < 0) zpadlen = 0;
1067 if (padlen < 0)
1068 padlen = 0;
1069 if (flags & DP_F_MINUS)
1070 padlen = -padlen; /* Left Justifty */
1072 if ((flags & DP_F_ZERO) && (padlen > 0)) {
1073 if (signvalue) {
1074 dopr_outch (buffer, currlen, maxlen, signvalue);
1075 --padlen;
1076 signvalue = 0;
1078 while (padlen > 0) {
1079 dopr_outch (buffer, currlen, maxlen, '0');
1080 --padlen;
1083 while (padlen > 0) {
1084 dopr_outch (buffer, currlen, maxlen, ' ');
1085 --padlen;
1087 if (signvalue)
1088 dopr_outch (buffer, currlen, maxlen, signvalue);
1090 while (iplace > 0)
1091 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
1093 #ifdef DEBUG_SNPRINTF
1094 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
1095 #endif
1098 * Decimal point. This should probably use locale to find the correct
1099 * char to print out.
1101 if (max > 0) {
1102 dopr_outch (buffer, currlen, maxlen, '.');
1104 while (zpadlen > 0) {
1105 dopr_outch (buffer, currlen, maxlen, '0');
1106 --zpadlen;
1109 while (fplace > 0)
1110 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
1113 while (padlen < 0) {
1114 dopr_outch (buffer, currlen, maxlen, ' ');
1115 ++padlen;
1119 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
1121 if (*currlen < maxlen) {
1122 buffer[(*currlen)] = c;
1124 (*currlen)++;
1127 static struct pr_chunk *new_chunk(void) {
1128 struct pr_chunk *new_c = (struct pr_chunk *)malloc(sizeof(struct pr_chunk));
1130 if (!new_c)
1131 return NULL;
1133 new_c->type = 0;
1134 new_c->num = 0;
1135 new_c->min = 0;
1136 new_c->min_star = NULL;
1137 new_c->max = -1;
1138 new_c->max_star = NULL;
1139 new_c->flags = 0;
1140 new_c->cflags = 0;
1141 new_c->start = 0;
1142 new_c->len = 0;
1143 new_c->value = 0;
1144 new_c->fvalue = 0;
1145 new_c->strvalue = NULL;
1146 new_c->pnum = NULL;
1147 new_c->next = NULL;
1149 return new_c;
1152 static int add_cnk_list_entry(struct pr_chunk_x **list,
1153 int max_num, struct pr_chunk *chunk) {
1154 struct pr_chunk_x *l;
1155 struct pr_chunk **c;
1156 int max;
1157 int cnum;
1158 int i, pos;
1160 if (chunk->num > max_num) {
1161 max = chunk->num;
1163 if (*list == NULL) {
1164 l = (struct pr_chunk_x *)malloc(sizeof(struct pr_chunk_x) * max);
1165 pos = 0;
1166 } else {
1167 l = (struct pr_chunk_x *)realloc(*list, sizeof(struct pr_chunk_x) * max);
1168 pos = max_num;
1170 if (l == NULL) {
1171 for (i = 0; i < max; i++) {
1172 if ((*list)[i].chunks) free((*list)[i].chunks);
1174 return 0;
1176 for (i = pos; i < max; i++) {
1177 l[i].chunks = NULL;
1178 l[i].num = 0;
1180 } else {
1181 l = *list;
1182 max = max_num;
1185 i = chunk->num - 1;
1186 cnum = l[i].num + 1;
1187 if (l[i].chunks == NULL) {
1188 c = (struct pr_chunk **)malloc(sizeof(struct pr_chunk *) * cnum);
1189 } else {
1190 c = (struct pr_chunk **)realloc(l[i].chunks, sizeof(struct pr_chunk *) * cnum);
1192 if (c == NULL) {
1193 for (i = 0; i < max; i++) {
1194 if (l[i].chunks) free(l[i].chunks);
1196 return 0;
1198 c[l[i].num] = chunk;
1199 l[i].chunks = c;
1200 l[i].num = cnum;
1202 *list = l;
1203 return max;
1206 int smb_vsnprintf (char *str, size_t count, const char *fmt, va_list args)
1208 return dopr(str, count, fmt, args);
1210 #define vsnprintf smb_vsnprintf
1211 #endif
1213 /* yes this really must be a ||. Don't muck with this (tridge)
1215 * The logic for these two is that we need our own definition if the
1216 * OS *either* has no definition of *sprintf, or if it does have one
1217 * that doesn't work properly according to the autoconf test.
1219 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
1220 int smb_snprintf(char *str,size_t count,const char *fmt,...)
1222 size_t ret;
1223 va_list ap;
1225 va_start(ap, fmt);
1226 ret = vsnprintf(str, count, fmt, ap);
1227 va_end(ap);
1228 return ret;
1230 #define snprintf smb_snprintf
1231 #endif
1233 #endif
1235 #ifndef HAVE_VASPRINTF
1236 int vasprintf(char **ptr, const char *format, va_list ap)
1238 int ret;
1239 va_list ap2;
1241 VA_COPY(ap2, ap);
1243 ret = vsnprintf(NULL, 0, format, ap2);
1244 if (ret <= 0) return ret;
1246 (*ptr) = (char *)malloc(ret+1);
1247 if (!*ptr) return -1;
1249 VA_COPY(ap2, ap);
1251 ret = vsnprintf(*ptr, ret+1, format, ap2);
1253 return ret;
1255 #endif
1258 #ifndef HAVE_ASPRINTF
1259 int asprintf(char **ptr, const char *format, ...)
1261 va_list ap;
1262 int ret;
1264 *ptr = NULL;
1265 va_start(ap, format);
1266 ret = vasprintf(ptr, format, ap);
1267 va_end(ap);
1269 return ret;
1271 #endif
1273 #ifdef TEST_SNPRINTF
1275 int sprintf(char *str,const char *fmt,...);
1276 int printf(const char *fmt,...);
1278 int main (void)
1280 char buf1[1024];
1281 char buf2[1024];
1282 char *buf3;
1283 char *fp_fmt[] = {
1284 "%1.1f",
1285 "%-1.5f",
1286 "%1.5f",
1287 "%123.9f",
1288 "%10.5f",
1289 "% 10.5f",
1290 "%+22.9f",
1291 "%+4.9f",
1292 "%01.3f",
1293 "%4f",
1294 "%3.1f",
1295 "%3.2f",
1296 "%.0f",
1297 "%f",
1298 "%-8.8f",
1299 "%-9.9f",
1300 NULL
1302 double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 203.9, 0.96, 0.996,
1303 0.9996, 1.996, 4.136, 5.030201, 0.00205,
1304 /* END LIST */ 0};
1305 char *int_fmt[] = {
1306 "%-1.5d",
1307 "%1.5d",
1308 "%123.9d",
1309 "%5.5d",
1310 "%10.5d",
1311 "% 10.5d",
1312 "%+22.33d",
1313 "%01.3d",
1314 "%4d",
1315 "%d",
1316 NULL
1318 long int_nums[] = { -1, 134, 91340, 341, 0203, 1234567890, 0};
1319 char *str_fmt[] = {
1320 "%10.5s",
1321 "%-10.5s",
1322 "%5.10s",
1323 "%-5.10s",
1324 "%10.1s",
1325 "%0.10s",
1326 "%10.0s",
1327 "%1.10s",
1328 "%s",
1329 "%.1s",
1330 "%.10s",
1331 "%10s",
1332 NULL
1334 char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
1335 #ifdef HAVE_LONG_LONG
1336 char *ll_fmt[] = {
1337 "%llu",
1338 NULL
1340 LLONG ll_nums[] = { 134, 91340, 341, 0203, 1234567890, 128006186140000000LL, 0};
1341 #endif
1342 int x, y;
1343 int fail = 0;
1344 int num = 0;
1345 int l1, l2;
1346 char *ss_fmt[] = {
1347 "%zd",
1348 "%zu",
1349 NULL
1351 size_t ss_nums[] = {134, 91340, 123456789, 0203, 1234567890, 0};
1353 printf ("Testing snprintf format codes against system sprintf...\n");
1355 for (x = 0; fp_fmt[x] ; x++) {
1356 for (y = 0; fp_nums[y] != 0 ; y++) {
1357 buf1[0] = buf2[0] = '\0';
1358 l1 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
1359 l2 = sprintf (buf2, fp_fmt[x], fp_nums[y]);
1360 buf1[1023] = buf2[1023] = '\0';
1361 if (strcmp (buf1, buf2) || (l1 != l2)) {
1362 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1363 fp_fmt[x], l1, buf1, l2, buf2);
1364 fail++;
1366 num++;
1370 for (x = 0; int_fmt[x] ; x++) {
1371 for (y = 0; int_nums[y] != 0 ; y++) {
1372 buf1[0] = buf2[0] = '\0';
1373 l1 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
1374 l2 = sprintf (buf2, int_fmt[x], int_nums[y]);
1375 buf1[1023] = buf2[1023] = '\0';
1376 if (strcmp (buf1, buf2) || (l1 != l2)) {
1377 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1378 int_fmt[x], l1, buf1, l2, buf2);
1379 fail++;
1381 num++;
1385 for (x = 0; str_fmt[x] ; x++) {
1386 for (y = 0; str_vals[y] != 0 ; y++) {
1387 buf1[0] = buf2[0] = '\0';
1388 l1 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
1389 l2 = sprintf (buf2, str_fmt[x], str_vals[y]);
1390 buf1[1023] = buf2[1023] = '\0';
1391 if (strcmp (buf1, buf2) || (l1 != l2)) {
1392 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1393 str_fmt[x], l1, buf1, l2, buf2);
1394 fail++;
1396 num++;
1400 #ifdef HAVE_LONG_LONG
1401 for (x = 0; ll_fmt[x] ; x++) {
1402 for (y = 0; ll_nums[y] != 0 ; y++) {
1403 buf1[0] = buf2[0] = '\0';
1404 l1 = snprintf(buf1, sizeof(buf1), ll_fmt[x], ll_nums[y]);
1405 l2 = sprintf (buf2, ll_fmt[x], ll_nums[y]);
1406 buf1[1023] = buf2[1023] = '\0';
1407 if (strcmp (buf1, buf2) || (l1 != l2)) {
1408 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1409 ll_fmt[x], l1, buf1, l2, buf2);
1410 fail++;
1412 num++;
1415 #endif
1417 #define BUFSZ 2048
1419 buf1[0] = buf2[0] = '\0';
1420 if ((buf3 = malloc(BUFSZ)) == NULL) {
1421 fail++;
1422 } else {
1423 num++;
1424 memset(buf3, 'a', BUFSZ);
1425 snprintf(buf1, sizeof(buf1), "%.*s", 1, buf3);
1426 buf1[1023] = '\0';
1427 if (strcmp(buf1, "a") != 0) {
1428 printf("length limit buf1 '%s' expected 'a'\n", buf1);
1429 fail++;
1433 buf1[0] = buf2[0] = '\0';
1434 l1 = snprintf(buf1, sizeof(buf1), "%4$*1$d %2$s %3$*1$.*1$f", 3, "pos test", 12.3456, 9);
1435 l2 = sprintf(buf2, "%4$*1$d %2$s %3$*1$.*1$f", 3, "pos test", 12.3456, 9);
1436 buf1[1023] = buf2[1023] = '\0';
1437 if (strcmp(buf1, buf2) || (l1 != l2)) {
1438 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1439 "%4$*1$d %2$s %3$*1$.*1$f", l1, buf1, l2, buf2);
1440 fail++;
1443 buf1[0] = buf2[0] = '\0';
1444 l1 = snprintf(buf1, sizeof(buf1), "%4$*4$d %2$s %3$*4$.*4$f", 3, "pos test", 12.3456, 9);
1445 l2 = sprintf(buf2, "%4$*4$d %2$s %3$*4$.*4$f", 3, "pos test", 12.3456, 9);
1446 buf1[1023] = buf2[1023] = '\0';
1447 if (strcmp(buf1, buf2)) {
1448 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1449 "%4$*1$d %2$s %3$*1$.*1$f", l1, buf1, l2, buf2);
1450 fail++;
1453 for (x = 0; ss_fmt[x] ; x++) {
1454 for (y = 0; ss_nums[y] != 0 ; y++) {
1455 buf1[0] = buf2[0] = '\0';
1456 l1 = snprintf(buf1, sizeof(buf1), ss_fmt[x], ss_nums[y]);
1457 l2 = sprintf (buf2, ss_fmt[x], ss_nums[y]);
1458 buf1[1023] = buf2[1023] = '\0';
1459 if (strcmp (buf1, buf2) || (l1 != l2)) {
1460 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1461 ss_fmt[x], l1, buf1, l2, buf2);
1462 fail++;
1464 num++;
1467 #if 0
1468 buf1[0] = buf2[0] = '\0';
1469 l1 = snprintf(buf1, sizeof(buf1), "%lld", (LLONG)1234567890);
1470 l2 = sprintf(buf2, "%lld", (LLONG)1234567890);
1471 buf1[1023] = buf2[1023] = '\0';
1472 if (strcmp(buf1, buf2)) {
1473 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1474 "%lld", l1, buf1, l2, buf2);
1475 fail++;
1478 buf1[0] = buf2[0] = '\0';
1479 l1 = snprintf(buf1, sizeof(buf1), "%Lf", (LDOUBLE)890.1234567890123);
1480 l2 = sprintf(buf2, "%Lf", (LDOUBLE)890.1234567890123);
1481 buf1[1023] = buf2[1023] = '\0';
1482 if (strcmp(buf1, buf2)) {
1483 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1484 "%Lf", l1, buf1, l2, buf2);
1485 fail++;
1487 #endif
1488 printf ("%d tests failed out of %d.\n", fail, num);
1490 printf("seeing how many digits we support\n");
1492 double v0 = 0.12345678901234567890123456789012345678901;
1493 for (x=0; x<100; x++) {
1494 double p = pow(10, x);
1495 double r = v0*p;
1496 snprintf(buf1, sizeof(buf1), "%1.1f", r);
1497 sprintf(buf2, "%1.1f", r);
1498 if (strcmp(buf1, buf2)) {
1499 printf("we seem to support %d digits\n", x-1);
1500 break;
1505 return 0;
1507 #endif /* TEST_SNPRINTF */