smbd: Remove "st" from struct open_symlink_err
[Samba.git] / lib / replace / snprintf.c
blobdd878fca113f71871a99de7fabbe03130ec88f93
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 nasty 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, formatted 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 * added 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 #include "replace.h"
107 #include "system/locale.h"
109 #ifdef TEST_SNPRINTF /* need math library headers for testing */
111 /* In test mode, we pretend that this system doesn't have any snprintf
112 * functions, regardless of what config.h says. */
113 # undef HAVE_SNPRINTF
114 # undef HAVE_VSNPRINTF
115 # undef HAVE_C99_VSNPRINTF
116 # undef HAVE_ASPRINTF
117 # undef HAVE_VASPRINTF
118 # include <math.h>
119 #endif /* TEST_SNPRINTF */
121 #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF) && defined(HAVE_C99_VSNPRINTF)
122 /* only include stdio.h if we are not re-defining snprintf or vsnprintf */
123 #include <stdio.h>
124 /* make the compiler happy with an empty file */
125 void dummy_snprintf(void);
126 void dummy_snprintf(void) {}
127 #endif /* HAVE_SNPRINTF, etc */
129 /* yes this really must be a ||. Don't muck with this (tridge) */
130 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
132 #ifdef HAVE_LONG_DOUBLE
133 #define LDOUBLE long double
134 #else
135 #define LDOUBLE double
136 #endif
138 #ifdef HAVE_LONG_LONG
139 #define LLONG long long
140 #else
141 #define LLONG long
142 #endif
144 #ifndef VA_COPY
145 #ifdef HAVE_VA_COPY
146 #define VA_COPY(dest, src) va_copy(dest, src)
147 #else
148 #ifdef HAVE___VA_COPY
149 #define VA_COPY(dest, src) __va_copy(dest, src)
150 #else
151 #define VA_COPY(dest, src) (dest) = (src)
152 #endif
153 #endif
156 * dopr(): poor man's version of doprintf
159 /* format read states */
160 #define DP_S_DEFAULT 0
161 #define DP_S_FLAGS 1
162 #define DP_S_MIN 2
163 #define DP_S_DOT 3
164 #define DP_S_MAX 4
165 #define DP_S_MOD 5
166 #define DP_S_CONV 6
167 #define DP_S_DONE 7
169 /* format flags - Bits */
170 #define DP_F_MINUS (1 << 0)
171 #define DP_F_PLUS (1 << 1)
172 #define DP_F_SPACE (1 << 2)
173 #define DP_F_NUM (1 << 3)
174 #define DP_F_ZERO (1 << 4)
175 #define DP_F_UP (1 << 5)
176 #define DP_F_UNSIGNED (1 << 6)
178 /* Conversion Flags */
179 #define DP_C_CHAR 1
180 #define DP_C_SHORT 2
181 #define DP_C_LONG 3
182 #define DP_C_LDOUBLE 4
183 #define DP_C_LLONG 5
184 #define DP_C_SIZET 6
186 /* Chunk types */
187 #define CNK_FMT_STR 0
188 #define CNK_INT 1
189 #define CNK_OCTAL 2
190 #define CNK_UINT 3
191 #define CNK_HEX 4
192 #define CNK_FLOAT 5
193 #define CNK_CHAR 6
194 #define CNK_STRING 7
195 #define CNK_PTR 8
196 #define CNK_NUM 9
197 #define CNK_PRCNT 10
199 #define char_to_int(p) ((p)- '0')
200 #ifndef MAX
201 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
202 #endif
204 struct pr_chunk {
205 int type; /* chunk type */
206 int num; /* parameter number */
207 int min;
208 int max;
209 int flags;
210 int cflags;
211 int start;
212 int len;
213 LLONG value;
214 LDOUBLE fvalue;
215 char *strvalue;
216 void *pnum;
217 struct pr_chunk *min_star;
218 struct pr_chunk *max_star;
219 struct pr_chunk *next;
222 struct pr_chunk_x {
223 struct pr_chunk **chunks;
224 int num;
227 static int dopr(char *buffer, size_t maxlen, const char *format,
228 va_list args_in);
229 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
230 char *value, int flags, int min, int max);
231 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
232 LLONG value, int base, int min, int max, int flags);
233 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
234 LDOUBLE fvalue, int min, int max, int flags);
235 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
236 static struct pr_chunk *new_chunk(void);
237 static int add_cnk_list_entry(struct pr_chunk_x **list,
238 int max_num, struct pr_chunk *chunk);
240 static int dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
242 char ch;
243 int state;
244 int pflag;
245 int pnum;
246 int pfirst;
247 size_t currlen;
248 va_list args;
249 const char *base;
250 struct pr_chunk *chunks = NULL;
251 struct pr_chunk *cnk = NULL;
252 struct pr_chunk_x *clist = NULL;
253 int max_pos;
254 int ret = -1;
256 VA_COPY(args, args_in);
258 state = DP_S_DEFAULT;
259 pfirst = 1;
260 pflag = 0;
261 pnum = 0;
263 max_pos = 0;
264 base = format;
265 ch = *format++;
267 /* retrieve the string structure as chunks */
268 while (state != DP_S_DONE) {
269 if (ch == '\0')
270 state = DP_S_DONE;
272 switch(state) {
273 case DP_S_DEFAULT:
275 if (cnk) {
276 cnk->next = new_chunk();
277 cnk = cnk->next;
278 } else {
279 cnk = new_chunk();
281 if (!cnk) goto done;
282 if (!chunks) chunks = cnk;
284 if (ch == '%') {
285 state = DP_S_FLAGS;
286 ch = *format++;
287 } else {
288 cnk->type = CNK_FMT_STR;
289 cnk->start = format - base -1;
290 while ((ch != '\0') && (ch != '%')) ch = *format++;
291 cnk->len = format - base - cnk->start -1;
293 break;
294 case DP_S_FLAGS:
295 switch (ch) {
296 case '-':
297 cnk->flags |= DP_F_MINUS;
298 ch = *format++;
299 break;
300 case '+':
301 cnk->flags |= DP_F_PLUS;
302 ch = *format++;
303 break;
304 case ' ':
305 cnk->flags |= DP_F_SPACE;
306 ch = *format++;
307 break;
308 case '#':
309 cnk->flags |= DP_F_NUM;
310 ch = *format++;
311 break;
312 case '0':
313 cnk->flags |= DP_F_ZERO;
314 ch = *format++;
315 break;
316 case 'I':
317 /* internationalization not supported yet */
318 ch = *format++;
319 break;
320 default:
321 state = DP_S_MIN;
322 break;
324 break;
325 case DP_S_MIN:
326 if (isdigit((unsigned char)ch)) {
327 cnk->min = 10 * cnk->min + char_to_int (ch);
328 ch = *format++;
329 } else if (ch == '$') {
330 if (!pfirst && !pflag) {
331 /* parameters must be all positioned or none */
332 goto done;
334 if (pfirst) {
335 pfirst = 0;
336 pflag = 1;
338 if (cnk->min == 0) /* what ?? */
339 goto done;
340 cnk->num = cnk->min;
341 cnk->min = 0;
342 ch = *format++;
343 } else if (ch == '*') {
344 if (pfirst) pfirst = 0;
345 cnk->min_star = new_chunk();
346 if (!cnk->min_star) /* out of memory :-( */
347 goto done;
348 cnk->min_star->type = CNK_INT;
349 if (pflag) {
350 int num;
351 ch = *format++;
352 if (!isdigit((unsigned char)ch)) {
353 /* parameters must be all positioned or none */
354 goto done;
356 for (num = 0; isdigit((unsigned char)ch); ch = *format++) {
357 num = 10 * num + char_to_int(ch);
359 cnk->min_star->num = num;
360 if (ch != '$') /* what ?? */
361 goto done;
362 } else {
363 cnk->min_star->num = ++pnum;
365 max_pos = add_cnk_list_entry(&clist, max_pos, cnk->min_star);
366 if (max_pos == 0) /* out of memory :-( */
367 goto done;
368 ch = *format++;
369 state = DP_S_DOT;
370 } else {
371 if (pfirst) pfirst = 0;
372 state = DP_S_DOT;
374 break;
375 case DP_S_DOT:
376 if (ch == '.') {
377 state = DP_S_MAX;
378 ch = *format++;
379 } else {
380 state = DP_S_MOD;
382 break;
383 case DP_S_MAX:
384 if (isdigit((unsigned char)ch)) {
385 if (cnk->max < 0)
386 cnk->max = 0;
387 cnk->max = 10 * cnk->max + char_to_int (ch);
388 ch = *format++;
389 } else if (ch == '$') {
390 if (!pfirst && !pflag) {
391 /* parameters must be all positioned or none */
392 goto done;
394 if (cnk->max <= 0) /* what ?? */
395 goto done;
396 cnk->num = cnk->max;
397 cnk->max = -1;
398 ch = *format++;
399 } else if (ch == '*') {
400 cnk->max_star = new_chunk();
401 if (!cnk->max_star) /* out of memory :-( */
402 goto done;
403 cnk->max_star->type = CNK_INT;
404 if (pflag) {
405 int num;
406 ch = *format++;
407 if (!isdigit((unsigned char)ch)) {
408 /* parameters must be all positioned or none */
409 goto done;
411 for (num = 0; isdigit((unsigned char)ch); ch = *format++) {
412 num = 10 * num + char_to_int(ch);
414 cnk->max_star->num = num;
415 if (ch != '$') /* what ?? */
416 goto done;
417 } else {
418 cnk->max_star->num = ++pnum;
420 max_pos = add_cnk_list_entry(&clist, max_pos, cnk->max_star);
421 if (max_pos == 0) /* out of memory :-( */
422 goto done;
424 ch = *format++;
425 state = DP_S_MOD;
426 } else {
427 state = DP_S_MOD;
429 break;
430 case DP_S_MOD:
431 switch (ch) {
432 case 'h':
433 cnk->cflags = DP_C_SHORT;
434 ch = *format++;
435 if (ch == 'h') {
436 cnk->cflags = DP_C_CHAR;
437 ch = *format++;
439 break;
440 case 'l':
441 cnk->cflags = DP_C_LONG;
442 ch = *format++;
443 if (ch == 'l') { /* It's a long long */
444 cnk->cflags = DP_C_LLONG;
445 ch = *format++;
447 break;
448 case 'j':
449 cnk->cflags = DP_C_LLONG;
450 ch = *format++;
451 break;
452 case 'L':
453 cnk->cflags = DP_C_LDOUBLE;
454 ch = *format++;
455 break;
456 case 'z':
457 cnk->cflags = DP_C_SIZET;
458 ch = *format++;
459 break;
460 default:
461 break;
463 state = DP_S_CONV;
464 break;
465 case DP_S_CONV:
466 if (cnk->num == 0) cnk->num = ++pnum;
467 max_pos = add_cnk_list_entry(&clist, max_pos, cnk);
468 if (max_pos == 0) /* out of memory :-( */
469 goto done;
471 switch (ch) {
472 case 'd':
473 case 'i':
474 cnk->type = CNK_INT;
475 break;
476 case 'o':
477 cnk->type = CNK_OCTAL;
478 cnk->flags |= DP_F_UNSIGNED;
479 break;
480 case 'u':
481 cnk->type = CNK_UINT;
482 cnk->flags |= DP_F_UNSIGNED;
483 break;
484 case 'X':
485 cnk->flags |= DP_F_UP;
486 case 'x':
487 cnk->type = CNK_HEX;
488 cnk->flags |= DP_F_UNSIGNED;
489 break;
490 case 'A':
491 /* hex float not supported yet */
492 case 'E':
493 case 'G':
494 case 'F':
495 cnk->flags |= DP_F_UP;
496 case 'a':
497 /* hex float not supported yet */
498 case 'e':
499 case 'f':
500 case 'g':
501 cnk->type = CNK_FLOAT;
502 break;
503 case 'c':
504 cnk->type = CNK_CHAR;
505 break;
506 case 's':
507 cnk->type = CNK_STRING;
508 break;
509 case 'p':
510 cnk->type = CNK_PTR;
511 cnk->flags |= DP_F_UNSIGNED;
512 break;
513 case 'n':
514 cnk->type = CNK_NUM;
515 break;
516 case '%':
517 cnk->type = CNK_PRCNT;
518 break;
519 default:
520 /* Unknown, bail out*/
521 goto done;
523 ch = *format++;
524 state = DP_S_DEFAULT;
525 break;
526 case DP_S_DONE:
527 break;
528 default:
529 /* hmm? */
530 break; /* some picky compilers need this */
534 /* retrieve the format arguments */
535 for (pnum = 0; pnum < max_pos; pnum++) {
536 int i;
538 if (clist[pnum].num == 0) {
539 /* ignoring a parameter should not be permitted
540 * all parameters must be matched at least once
541 * BUT seem some system ignore this rule ...
542 * at least my glibc based system does --SSS
544 #ifdef DEBUG_SNPRINTF
545 printf("parameter at position %d not used\n", pnum+1);
546 #endif
547 /* eat the parameter */
548 va_arg (args, int);
549 continue;
551 for (i = 1; i < clist[pnum].num; i++) {
552 if (clist[pnum].chunks[0]->type != clist[pnum].chunks[i]->type) {
553 /* nooo no no!
554 * all the references to a parameter
555 * must be of the same type
557 goto done;
560 cnk = clist[pnum].chunks[0];
561 switch (cnk->type) {
562 case CNK_INT:
563 if (cnk->cflags == DP_C_SHORT)
564 cnk->value = va_arg (args, int);
565 else if (cnk->cflags == DP_C_LONG)
566 cnk->value = va_arg (args, long int);
567 else if (cnk->cflags == DP_C_LLONG)
568 cnk->value = va_arg (args, LLONG);
569 else if (cnk->cflags == DP_C_SIZET)
570 cnk->value = va_arg (args, ssize_t);
571 else
572 cnk->value = va_arg (args, int);
574 for (i = 1; i < clist[pnum].num; i++) {
575 clist[pnum].chunks[i]->value = cnk->value;
577 break;
579 case CNK_OCTAL:
580 case CNK_UINT:
581 case CNK_HEX:
582 if (cnk->cflags == DP_C_SHORT)
583 cnk->value = va_arg (args, unsigned int);
584 else if (cnk->cflags == DP_C_LONG)
585 cnk->value = (unsigned long int)va_arg (args, unsigned long int);
586 else if (cnk->cflags == DP_C_LLONG)
587 cnk->value = (LLONG)va_arg (args, unsigned LLONG);
588 else if (cnk->cflags == DP_C_SIZET)
589 cnk->value = (size_t)va_arg (args, size_t);
590 else
591 cnk->value = (unsigned int)va_arg (args, unsigned int);
593 for (i = 1; i < clist[pnum].num; i++) {
594 clist[pnum].chunks[i]->value = cnk->value;
596 break;
598 case CNK_FLOAT:
599 if (cnk->cflags == DP_C_LDOUBLE)
600 cnk->fvalue = va_arg (args, LDOUBLE);
601 else
602 cnk->fvalue = va_arg (args, double);
604 for (i = 1; i < clist[pnum].num; i++) {
605 clist[pnum].chunks[i]->fvalue = cnk->fvalue;
607 break;
609 case CNK_CHAR:
610 cnk->value = va_arg (args, int);
612 for (i = 1; i < clist[pnum].num; i++) {
613 clist[pnum].chunks[i]->value = cnk->value;
615 break;
617 case CNK_STRING:
618 cnk->strvalue = va_arg (args, char *);
619 if (!cnk->strvalue) cnk->strvalue = "(NULL)";
621 for (i = 1; i < clist[pnum].num; i++) {
622 clist[pnum].chunks[i]->strvalue = cnk->strvalue;
624 break;
626 case CNK_PTR:
627 cnk->strvalue = va_arg (args, void *);
628 for (i = 1; i < clist[pnum].num; i++) {
629 clist[pnum].chunks[i]->strvalue = cnk->strvalue;
631 break;
633 case CNK_NUM:
634 if (cnk->cflags == DP_C_CHAR)
635 cnk->pnum = va_arg (args, char *);
636 else if (cnk->cflags == DP_C_SHORT)
637 cnk->pnum = va_arg (args, short int *);
638 else if (cnk->cflags == DP_C_LONG)
639 cnk->pnum = va_arg (args, long int *);
640 else if (cnk->cflags == DP_C_LLONG)
641 cnk->pnum = va_arg (args, LLONG *);
642 else if (cnk->cflags == DP_C_SIZET)
643 cnk->pnum = va_arg (args, ssize_t *);
644 else
645 cnk->pnum = va_arg (args, int *);
647 for (i = 1; i < clist[pnum].num; i++) {
648 clist[pnum].chunks[i]->pnum = cnk->pnum;
650 break;
652 case CNK_PRCNT:
653 break;
655 default:
656 /* what ?? */
657 goto done;
660 /* print out the actual string from chunks */
661 currlen = 0;
662 cnk = chunks;
663 while (cnk) {
664 int len, min, max;
666 if (cnk->min_star) min = cnk->min_star->value;
667 else min = cnk->min;
668 if (cnk->max_star) max = cnk->max_star->value;
669 else max = cnk->max;
671 switch (cnk->type) {
673 case CNK_FMT_STR:
674 if (maxlen != 0 && maxlen > currlen) {
675 if (maxlen > (currlen + cnk->len)) len = cnk->len;
676 else len = maxlen - currlen;
678 memcpy(&(buffer[currlen]), &(base[cnk->start]), len);
680 currlen += cnk->len;
682 break;
684 case CNK_INT:
685 case CNK_UINT:
686 fmtint (buffer, &currlen, maxlen, cnk->value, 10, min, max, cnk->flags);
687 break;
689 case CNK_OCTAL:
690 fmtint (buffer, &currlen, maxlen, cnk->value, 8, min, max, cnk->flags);
691 break;
693 case CNK_HEX:
694 fmtint (buffer, &currlen, maxlen, cnk->value, 16, min, max, cnk->flags);
695 break;
697 case CNK_FLOAT:
698 fmtfp (buffer, &currlen, maxlen, cnk->fvalue, min, max, cnk->flags);
699 break;
701 case CNK_CHAR:
702 dopr_outch (buffer, &currlen, maxlen, cnk->value);
703 break;
705 case CNK_STRING:
706 if (max == -1) {
707 max = strlen(cnk->strvalue);
709 fmtstr (buffer, &currlen, maxlen, cnk->strvalue, cnk->flags, min, max);
710 break;
712 case CNK_PTR:
713 fmtint (buffer, &currlen, maxlen, (long)(cnk->strvalue), 16, min, max, cnk->flags);
714 break;
716 case CNK_NUM:
717 if (cnk->cflags == DP_C_CHAR)
718 *((char *)(cnk->pnum)) = (char)currlen;
719 else if (cnk->cflags == DP_C_SHORT)
720 *((short int *)(cnk->pnum)) = (short int)currlen;
721 else if (cnk->cflags == DP_C_LONG)
722 *((long int *)(cnk->pnum)) = (long int)currlen;
723 else if (cnk->cflags == DP_C_LLONG)
724 *((LLONG *)(cnk->pnum)) = (LLONG)currlen;
725 else if (cnk->cflags == DP_C_SIZET)
726 *((ssize_t *)(cnk->pnum)) = (ssize_t)currlen;
727 else
728 *((int *)(cnk->pnum)) = (int)currlen;
729 break;
731 case CNK_PRCNT:
732 dopr_outch (buffer, &currlen, maxlen, '%');
733 break;
735 default:
736 /* what ?? */
737 goto done;
739 cnk = cnk->next;
741 if (maxlen != 0) {
742 if (currlen < maxlen - 1)
743 buffer[currlen] = '\0';
744 else if (maxlen > 0)
745 buffer[maxlen - 1] = '\0';
747 ret = currlen;
749 done:
750 va_end(args);
752 while (chunks) {
753 cnk = chunks->next;
754 if (chunks->min_star) free(chunks->min_star);
755 if (chunks->max_star) free(chunks->max_star);
756 free(chunks);
757 chunks = cnk;
759 if (clist) {
760 for (pnum = 0; pnum < max_pos; pnum++) {
761 if (clist[pnum].chunks) free(clist[pnum].chunks);
763 free(clist);
765 return ret;
768 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
769 char *value, int flags, int min, int max)
771 int padlen, strln; /* amount to pad */
772 int cnt = 0;
774 #ifdef DEBUG_SNPRINTF
775 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
776 #endif
777 if (value == 0) {
778 value = "<NULL>";
781 for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */
782 padlen = min - strln;
783 if (padlen < 0)
784 padlen = 0;
785 if (flags & DP_F_MINUS)
786 padlen = -padlen; /* Left Justify */
788 while (padlen > 0) {
789 dopr_outch (buffer, currlen, maxlen, ' ');
790 --padlen;
792 while (*value && (cnt < max)) {
793 dopr_outch (buffer, currlen, maxlen, *value++);
794 ++cnt;
796 while (padlen < 0) {
797 dopr_outch (buffer, currlen, maxlen, ' ');
798 ++padlen;
802 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
804 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
805 LLONG value, int base, int min, int max, int flags)
807 int signvalue = 0;
808 unsigned LLONG uvalue;
809 char convert[22+1]; /* 64-bit value in octal: 22 digits + \0 */
810 int place = 0;
811 int spadlen = 0; /* amount to space pad */
812 int zpadlen = 0; /* amount to zero pad */
813 int caps = 0;
815 if (max < 0)
816 max = 0;
818 uvalue = value;
820 if(!(flags & DP_F_UNSIGNED)) {
821 if( value < 0 ) {
822 signvalue = '-';
823 uvalue = -value;
824 } else {
825 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
826 signvalue = '+';
827 else if (flags & DP_F_SPACE)
828 signvalue = ' ';
832 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
834 do {
835 convert[place++] =
836 (caps? "0123456789ABCDEF":"0123456789abcdef")
837 [uvalue % (unsigned)base ];
838 uvalue = (uvalue / (unsigned)base );
839 } while(uvalue && (place < sizeof(convert)));
840 if (place == sizeof(convert)) place--;
841 convert[place] = 0;
843 zpadlen = max - place;
844 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
845 if (zpadlen < 0) zpadlen = 0;
846 if (spadlen < 0) spadlen = 0;
847 if (flags & DP_F_ZERO) {
848 zpadlen = MAX(zpadlen, spadlen);
849 spadlen = 0;
851 if (flags & DP_F_MINUS)
852 spadlen = -spadlen; /* Left Justifty */
854 #ifdef DEBUG_SNPRINTF
855 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
856 zpadlen, spadlen, min, max, place);
857 #endif
859 /* Spaces */
860 while (spadlen > 0) {
861 dopr_outch (buffer, currlen, maxlen, ' ');
862 --spadlen;
865 /* Sign */
866 if (signvalue)
867 dopr_outch (buffer, currlen, maxlen, signvalue);
869 /* Zeros */
870 if (zpadlen > 0) {
871 while (zpadlen > 0) {
872 dopr_outch (buffer, currlen, maxlen, '0');
873 --zpadlen;
877 /* Digits */
878 while (place > 0)
879 dopr_outch (buffer, currlen, maxlen, convert[--place]);
881 /* Left Justified spaces */
882 while (spadlen < 0) {
883 dopr_outch (buffer, currlen, maxlen, ' ');
884 ++spadlen;
888 static LDOUBLE abs_val(LDOUBLE value)
890 LDOUBLE result = value;
892 if (value < 0)
893 result = -value;
895 return result;
898 static LDOUBLE POW10(int exp)
900 LDOUBLE result = 1;
902 while (exp) {
903 result *= 10;
904 exp--;
907 return result;
910 static LLONG ROUND(LDOUBLE value)
912 LLONG intpart;
914 intpart = (LLONG)value;
915 value = value - intpart;
916 if (value >= 0.5) intpart++;
918 return intpart;
921 /* a replacement for modf that doesn't need the math library. Should
922 be portable, but slow */
923 static double my_modf(double x0, double *iptr)
925 int i;
926 LLONG l=0;
927 double x = x0;
928 double f = 1.0;
930 for (i=0;i<100;i++) {
931 l = (long)x;
932 if (l <= (x+1) && l >= (x-1)) break;
933 x *= 0.1;
934 f *= 10.0;
937 if (i == 100) {
938 /* yikes! the number is beyond what we can handle. What do we do? */
939 (*iptr) = 0;
940 return 0;
943 if (i != 0) {
944 double i2;
945 double ret;
947 ret = my_modf(x0-l*f, &i2);
948 (*iptr) = l*f + i2;
949 return ret;
952 (*iptr) = l;
953 return x - (*iptr);
957 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
958 LDOUBLE fvalue, int min, int max, int flags)
960 int signvalue = 0;
961 double ufvalue;
962 char iconvert[311];
963 char fconvert[311];
964 int iplace = 0;
965 int fplace = 0;
966 int padlen = 0; /* amount to pad */
967 int zpadlen = 0;
968 int caps = 0;
969 int idx;
970 double intpart;
971 double fracpart;
972 double temp;
975 * AIX manpage says the default is 0, but Solaris says the default
976 * is 6, and sprintf on AIX defaults to 6
978 if (max < 0)
979 max = 6;
981 ufvalue = abs_val (fvalue);
983 if (fvalue < 0) {
984 signvalue = '-';
985 } else {
986 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
987 signvalue = '+';
988 } else {
989 if (flags & DP_F_SPACE)
990 signvalue = ' ';
994 #if 0
995 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
996 #endif
998 #if 0
999 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
1000 #endif
1003 * Sorry, we only support 9 digits past the decimal because of our
1004 * conversion method
1006 if (max > 9)
1007 max = 9;
1009 /* We "cheat" by converting the fractional part to integer by
1010 * multiplying by a factor of 10
1013 temp = ufvalue;
1014 my_modf(temp, &intpart);
1016 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
1018 if (fracpart >= POW10(max)) {
1019 intpart++;
1020 fracpart -= POW10(max);
1024 /* Convert integer part */
1025 do {
1026 temp = intpart*0.1;
1027 my_modf(temp, &intpart);
1028 idx = (int) ((temp -intpart +0.05)* 10.0);
1029 /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
1030 /* printf ("%llf, %f, %x\n", temp, intpart, idx); */
1031 iconvert[iplace++] =
1032 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
1033 } while (intpart && (iplace < 311));
1034 if (iplace == 311) iplace--;
1035 iconvert[iplace] = 0;
1037 /* Convert fractional part */
1038 if (fracpart)
1040 do {
1041 temp = fracpart*0.1;
1042 my_modf(temp, &fracpart);
1043 idx = (int) ((temp -fracpart +0.05)* 10.0);
1044 /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
1045 /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
1046 fconvert[fplace++] =
1047 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
1048 } while(fracpart && (fplace < 311));
1049 if (fplace == 311) fplace--;
1051 fconvert[fplace] = 0;
1053 /* -1 for decimal point, another -1 if we are printing a sign */
1054 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
1055 zpadlen = max - fplace;
1056 if (zpadlen < 0) zpadlen = 0;
1057 if (padlen < 0)
1058 padlen = 0;
1059 if (flags & DP_F_MINUS)
1060 padlen = -padlen; /* Left Justifty */
1062 if ((flags & DP_F_ZERO) && (padlen > 0)) {
1063 if (signvalue) {
1064 dopr_outch (buffer, currlen, maxlen, signvalue);
1065 --padlen;
1066 signvalue = 0;
1068 while (padlen > 0) {
1069 dopr_outch (buffer, currlen, maxlen, '0');
1070 --padlen;
1073 while (padlen > 0) {
1074 dopr_outch (buffer, currlen, maxlen, ' ');
1075 --padlen;
1077 if (signvalue)
1078 dopr_outch (buffer, currlen, maxlen, signvalue);
1080 while (iplace > 0)
1081 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
1083 #ifdef DEBUG_SNPRINTF
1084 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
1085 #endif
1088 * Decimal point. This should probably use locale to find the correct
1089 * char to print out.
1091 if (max > 0) {
1092 dopr_outch (buffer, currlen, maxlen, '.');
1094 while (zpadlen > 0) {
1095 dopr_outch (buffer, currlen, maxlen, '0');
1096 --zpadlen;
1099 while (fplace > 0)
1100 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
1103 while (padlen < 0) {
1104 dopr_outch (buffer, currlen, maxlen, ' ');
1105 ++padlen;
1109 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
1111 if (*currlen < maxlen) {
1112 buffer[(*currlen)] = c;
1114 (*currlen)++;
1117 static struct pr_chunk *new_chunk(void) {
1118 struct pr_chunk *new_c = (struct pr_chunk *)malloc(sizeof(struct pr_chunk));
1120 if (!new_c)
1121 return NULL;
1123 new_c->type = 0;
1124 new_c->num = 0;
1125 new_c->min = 0;
1126 new_c->min_star = NULL;
1127 new_c->max = -1;
1128 new_c->max_star = NULL;
1129 new_c->flags = 0;
1130 new_c->cflags = 0;
1131 new_c->start = 0;
1132 new_c->len = 0;
1133 new_c->value = 0;
1134 new_c->fvalue = 0;
1135 new_c->strvalue = NULL;
1136 new_c->pnum = NULL;
1137 new_c->next = NULL;
1139 return new_c;
1142 static int add_cnk_list_entry(struct pr_chunk_x **list,
1143 int max_num, struct pr_chunk *chunk) {
1144 struct pr_chunk_x *l;
1145 struct pr_chunk **c;
1146 int max;
1147 int cnum;
1148 int i, pos;
1150 if (chunk->num > max_num) {
1151 max = chunk->num;
1153 if (*list == NULL) {
1154 l = (struct pr_chunk_x *)malloc(sizeof(struct pr_chunk_x) * max);
1155 pos = 0;
1156 } else {
1157 l = (struct pr_chunk_x *)realloc(*list, sizeof(struct pr_chunk_x) * max);
1158 pos = max_num;
1160 if (l == NULL) {
1161 for (i = 0; i < max; i++) {
1162 if ((*list)[i].chunks) free((*list)[i].chunks);
1164 return 0;
1166 for (i = pos; i < max; i++) {
1167 l[i].chunks = NULL;
1168 l[i].num = 0;
1170 } else {
1171 l = *list;
1172 max = max_num;
1175 i = chunk->num - 1;
1176 cnum = l[i].num + 1;
1177 if (l[i].chunks == NULL) {
1178 c = (struct pr_chunk **)malloc(sizeof(struct pr_chunk *) * cnum);
1179 } else {
1180 c = (struct pr_chunk **)realloc(l[i].chunks, sizeof(struct pr_chunk *) * cnum);
1182 if (c == NULL) {
1183 for (i = 0; i < max; i++) {
1184 if (l[i].chunks) free(l[i].chunks);
1186 return 0;
1188 c[l[i].num] = chunk;
1189 l[i].chunks = c;
1190 l[i].num = cnum;
1192 *list = l;
1193 return max;
1196 int rep_vsnprintf (char *str, size_t count, const char *fmt, va_list args)
1198 return dopr(str, count, fmt, args);
1200 #endif
1202 /* yes this really must be a ||. Don't muck with this (tridge)
1204 * The logic for these two is that we need our own definition if the
1205 * OS *either* has no definition of *sprintf, or if it does have one
1206 * that doesn't work properly according to the autoconf test.
1208 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
1209 int rep_snprintf(char *str,size_t count,const char *fmt,...)
1211 size_t ret;
1212 va_list ap;
1214 va_start(ap, fmt);
1215 ret = vsnprintf(str, count, fmt, ap);
1216 va_end(ap);
1217 return ret;
1219 #endif
1221 #ifndef HAVE_C99_VSNPRINTF
1222 int rep_printf(const char *fmt, ...)
1224 va_list ap;
1225 int ret;
1226 char *s;
1228 s = NULL;
1229 va_start(ap, fmt);
1230 ret = vasprintf(&s, fmt, ap);
1231 va_end(ap);
1233 if (s) {
1234 fwrite(s, 1, strlen(s), stdout);
1236 free(s);
1238 return ret;
1240 #endif
1242 #ifndef HAVE_C99_VSNPRINTF
1243 int rep_fprintf(FILE *stream, const char *fmt, ...)
1245 va_list ap;
1246 int ret;
1247 char *s;
1249 s = NULL;
1250 va_start(ap, fmt);
1251 ret = vasprintf(&s, fmt, ap);
1252 va_end(ap);
1254 if (s) {
1255 fwrite(s, 1, strlen(s), stream);
1257 free(s);
1259 return ret;
1261 #endif
1263 #endif
1265 #if !defined(HAVE_VASPRINTF) || !defined(HAVE_C99_VSNPRINTF)
1266 int rep_vasprintf(char **ptr, const char *format, va_list ap)
1268 int ret;
1269 va_list ap2;
1271 VA_COPY(ap2, ap);
1272 ret = vsnprintf(NULL, 0, format, ap2);
1273 va_end(ap2);
1274 if (ret < 0) return ret;
1276 (*ptr) = (char *)malloc(ret+1);
1277 if (!*ptr) return -1;
1279 VA_COPY(ap2, ap);
1280 ret = vsnprintf(*ptr, ret+1, format, ap2);
1281 va_end(ap2);
1283 return ret;
1285 #endif
1287 #if !defined(HAVE_ASPRINTF) || !defined(HAVE_C99_VSNPRINTF)
1288 int rep_asprintf(char **ptr, const char *format, ...)
1290 va_list ap;
1291 int ret;
1293 *ptr = NULL;
1294 va_start(ap, format);
1295 ret = vasprintf(ptr, format, ap);
1296 va_end(ap);
1298 return ret;
1300 #endif
1302 #ifdef TEST_SNPRINTF
1304 int sprintf(char *str,const char *fmt,...);
1305 int printf(const char *fmt,...);
1307 int main (void)
1309 char buf1[1024];
1310 char buf2[1024];
1311 char *buf3;
1312 char *fp_fmt[] = {
1313 "%1.1f",
1314 "%-1.5f",
1315 "%1.5f",
1316 "%123.9f",
1317 "%10.5f",
1318 "% 10.5f",
1319 "%+22.9f",
1320 "%+4.9f",
1321 "%01.3f",
1322 "%4f",
1323 "%3.1f",
1324 "%3.2f",
1325 "%.0f",
1326 "%f",
1327 "%-8.8f",
1328 "%-9.9f",
1329 NULL
1331 double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 203.9, 0.96, 0.996,
1332 0.9996, 1.996, 4.136, 5.030201, 0.00205,
1333 /* END LIST */ 0};
1334 char *int_fmt[] = {
1335 "%-1.5d",
1336 "%1.5d",
1337 "%123.9d",
1338 "%5.5d",
1339 "%10.5d",
1340 "% 10.5d",
1341 "%+22.33d",
1342 "%01.3d",
1343 "%4d",
1344 "%d",
1345 NULL
1347 long int_nums[] = { -1, 134, 91340, 341, 0203, 1234567890, 0};
1348 char *str_fmt[] = {
1349 "%10.5s",
1350 "%-10.5s",
1351 "%5.10s",
1352 "%-5.10s",
1353 "%10.1s",
1354 "%0.10s",
1355 "%10.0s",
1356 "%1.10s",
1357 "%s",
1358 "%.1s",
1359 "%.10s",
1360 "%10s",
1361 NULL
1363 char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
1364 #ifdef HAVE_LONG_LONG
1365 char *ll_fmt[] = {
1366 "%llu",
1367 NULL
1369 LLONG ll_nums[] = { 134, 91340, 341, 0203, 1234567890, 128006186140000000LL, 0};
1370 #endif
1371 int x, y;
1372 int fail = 0;
1373 int num = 0;
1374 int l1, l2;
1375 char *ss_fmt[] = {
1376 "%zd",
1377 "%zu",
1378 NULL
1380 size_t ss_nums[] = {134, 91340, 123456789, 0203, 1234567890, 0};
1382 printf ("Testing snprintf format codes against system sprintf...\n");
1384 for (x = 0; fp_fmt[x] ; x++) {
1385 for (y = 0; fp_nums[y] != 0 ; y++) {
1386 buf1[0] = buf2[0] = '\0';
1387 l1 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
1388 l2 = sprintf (buf2, fp_fmt[x], fp_nums[y]);
1389 buf1[1023] = buf2[1023] = '\0';
1390 if (strcmp (buf1, buf2) || (l1 != l2)) {
1391 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1392 fp_fmt[x], l1, buf1, l2, buf2);
1393 fail++;
1395 num++;
1399 for (x = 0; int_fmt[x] ; x++) {
1400 for (y = 0; int_nums[y] != 0 ; y++) {
1401 buf1[0] = buf2[0] = '\0';
1402 l1 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
1403 l2 = sprintf (buf2, int_fmt[x], int_nums[y]);
1404 buf1[1023] = buf2[1023] = '\0';
1405 if (strcmp (buf1, buf2) || (l1 != l2)) {
1406 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1407 int_fmt[x], l1, buf1, l2, buf2);
1408 fail++;
1410 num++;
1414 for (x = 0; str_fmt[x] ; x++) {
1415 for (y = 0; str_vals[y] != 0 ; y++) {
1416 buf1[0] = buf2[0] = '\0';
1417 l1 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
1418 l2 = sprintf (buf2, str_fmt[x], str_vals[y]);
1419 buf1[1023] = buf2[1023] = '\0';
1420 if (strcmp (buf1, buf2) || (l1 != l2)) {
1421 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1422 str_fmt[x], l1, buf1, l2, buf2);
1423 fail++;
1425 num++;
1429 #ifdef HAVE_LONG_LONG
1430 for (x = 0; ll_fmt[x] ; x++) {
1431 for (y = 0; ll_nums[y] != 0 ; y++) {
1432 buf1[0] = buf2[0] = '\0';
1433 l1 = snprintf(buf1, sizeof(buf1), ll_fmt[x], ll_nums[y]);
1434 l2 = sprintf (buf2, ll_fmt[x], ll_nums[y]);
1435 buf1[1023] = buf2[1023] = '\0';
1436 if (strcmp (buf1, buf2) || (l1 != l2)) {
1437 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1438 ll_fmt[x], l1, buf1, l2, buf2);
1439 fail++;
1441 num++;
1444 #endif
1446 #define BUFSZ 2048
1448 buf1[0] = buf2[0] = '\0';
1449 if ((buf3 = malloc(BUFSZ)) == NULL) {
1450 fail++;
1451 } else {
1452 num++;
1453 memset(buf3, 'a', BUFSZ);
1454 snprintf(buf1, sizeof(buf1), "%.*s", 1, buf3);
1455 buf1[1023] = '\0';
1456 if (strcmp(buf1, "a") != 0) {
1457 printf("length limit buf1 '%s' expected 'a'\n", buf1);
1458 fail++;
1462 buf1[0] = buf2[0] = '\0';
1463 l1 = snprintf(buf1, sizeof(buf1), "%4$*1$d %2$s %3$*1$.*1$f", 3, "pos test", 12.3456, 9);
1464 l2 = sprintf(buf2, "%4$*1$d %2$s %3$*1$.*1$f", 3, "pos test", 12.3456, 9);
1465 buf1[1023] = buf2[1023] = '\0';
1466 if (strcmp(buf1, buf2) || (l1 != l2)) {
1467 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1468 "%4$*1$d %2$s %3$*1$.*1$f", l1, buf1, l2, buf2);
1469 fail++;
1472 buf1[0] = buf2[0] = '\0';
1473 l1 = snprintf(buf1, sizeof(buf1), "%4$*4$d %2$s %3$*4$.*4$f", 3, "pos test", 12.3456, 9);
1474 l2 = sprintf(buf2, "%4$*4$d %2$s %3$*4$.*4$f", 3, "pos test", 12.3456, 9);
1475 buf1[1023] = buf2[1023] = '\0';
1476 if (strcmp(buf1, buf2)) {
1477 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1478 "%4$*1$d %2$s %3$*1$.*1$f", l1, buf1, l2, buf2);
1479 fail++;
1482 for (x = 0; ss_fmt[x] ; x++) {
1483 for (y = 0; ss_nums[y] != 0 ; y++) {
1484 buf1[0] = buf2[0] = '\0';
1485 l1 = snprintf(buf1, sizeof(buf1), ss_fmt[x], ss_nums[y]);
1486 l2 = sprintf (buf2, ss_fmt[x], ss_nums[y]);
1487 buf1[1023] = buf2[1023] = '\0';
1488 if (strcmp (buf1, buf2) || (l1 != l2)) {
1489 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1490 ss_fmt[x], l1, buf1, l2, buf2);
1491 fail++;
1493 num++;
1496 #if 0
1497 buf1[0] = buf2[0] = '\0';
1498 l1 = snprintf(buf1, sizeof(buf1), "%lld", (LLONG)1234567890);
1499 l2 = sprintf(buf2, "%lld", (LLONG)1234567890);
1500 buf1[1023] = buf2[1023] = '\0';
1501 if (strcmp(buf1, buf2)) {
1502 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1503 "%lld", l1, buf1, l2, buf2);
1504 fail++;
1507 buf1[0] = buf2[0] = '\0';
1508 l1 = snprintf(buf1, sizeof(buf1), "%Lf", (LDOUBLE)890.1234567890123);
1509 l2 = sprintf(buf2, "%Lf", (LDOUBLE)890.1234567890123);
1510 buf1[1023] = buf2[1023] = '\0';
1511 if (strcmp(buf1, buf2)) {
1512 printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
1513 "%Lf", l1, buf1, l2, buf2);
1514 fail++;
1516 #endif
1517 printf ("%d tests failed out of %d.\n", fail, num);
1519 printf("seeing how many digits we support\n");
1521 double v0 = 0.12345678901234567890123456789012345678901;
1522 for (x=0; x<100; x++) {
1523 double p = pow(10, x);
1524 double r = v0*p;
1525 snprintf(buf1, sizeof(buf1), "%1.1f", r);
1526 sprintf(buf2, "%1.1f", r);
1527 if (strcmp(buf1, buf2)) {
1528 printf("we seem to support %d digits\n", x-1);
1529 break;
1534 return 0;
1536 #endif /* TEST_SNPRINTF */