summaryrefslogtreecommitdiffstats
path: root/lib/snprintf.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/snprintf.c')
-rw-r--r--lib/snprintf.c1023
1 files changed, 0 insertions, 1023 deletions
diff --git a/lib/snprintf.c b/lib/snprintf.c
deleted file mode 100644
index 633517de..00000000
--- a/lib/snprintf.c
+++ /dev/null
@@ -1,1023 +0,0 @@
1/*
2 * NOTE: If you change this file, please merge it into rsync, samba, etc.
3 */
4
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
10 */
11
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...
18 *
19 * snprintf() is used instead of sprintf() as it does limit checks
20 * for string length. This covers a nasty loophole.
21 *
22 * The other functions are there to prevent NULL pointers from
23 * causing nast effects.
24 *
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.
31 *
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.
42 *
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.
46 *
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.
51 *
52 * Andrew Tridgell (tridge@samba.org) Oct 1998
53 * fixed handling of %.0f
54 * added test for HAVE_LONG_DOUBLE
55 *
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
59 *
60 * date: 2002/12/19 19:56:31; author: herb; state: Exp; lines: +2 -0
61 * actually print args for %g and %e
62 *
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.
67 *
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
71 *
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.
75 *
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.
81 *
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.
86 *
87 * Martin Pool (mbp@samba.org) May 2003
88 * Put in a prototype for dummy_snprintf() to quiet compiler warnings.
89 *
90 * Move #endif to make sure VA_COPY, LDOUBLE, etc are defined even
91 * if the C library has some snprintf functions already.
92 **************************************************************/
93
94#ifndef NO_CONFIG_H
95#include "config.h"
96#else
97#define NULL 0
98#endif
99
100#ifdef TEST_SNPRINTF /* need math library headers for testing */
101
102/* In test mode, we pretend that this system doesn't have any snprintf
103 * functions, regardless of what config.h says. */
104# undef HAVE_SNPRINTF
105# undef HAVE_VSNPRINTF
106# undef HAVE_C99_VSNPRINTF
107# undef HAVE_ASPRINTF
108# undef HAVE_VASPRINTF
109# include <math.h>
110#endif /* TEST_SNPRINTF */
111
112#ifdef HAVE_STRING_H
113#include <string.h>
114#endif
115
116#ifdef HAVE_STRINGS_H
117#include <strings.h>
118#endif
119#ifdef HAVE_CTYPE_H
120#include <ctype.h>
121#endif
122#include <sys/types.h>
123#include <stdarg.h>
124#ifdef HAVE_STDLIB_H
125#include <stdlib.h>
126#endif
127
128#if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF) && defined(HAVE_C99_VSNPRINTF)
129/* only include stdio.h if we are not re-defining snprintf or vsnprintf */
130#include <stdio.h>
131 /* make the compiler happy with an empty file */
132 void dummy_snprintf(void);
133 void dummy_snprintf(void) {}
134#endif /* HAVE_SNPRINTF, etc */
135
136#ifdef HAVE_LONG_DOUBLE
137#define LDOUBLE long double
138#else
139#define LDOUBLE double
140#endif
141
142#ifdef HAVE_LONG_LONG
143#define LLONG long long
144#else
145#define LLONG long
146#endif
147
148#ifndef VA_COPY
149#ifdef HAVE_VA_COPY
150#define VA_COPY(dest, src) va_copy(dest, src)
151#else
152#ifdef HAVE___VA_COPY
153#define VA_COPY(dest, src) __va_copy(dest, src)
154#else
155#define VA_COPY(dest, src) (dest) = (src)
156#endif
157#endif
158
159/*
160 * dopr(): poor man's version of doprintf
161 */
162
163/* format read states */
164#define DP_S_DEFAULT 0
165#define DP_S_FLAGS 1
166#define DP_S_MIN 2
167#define DP_S_DOT 3
168#define DP_S_MAX 4
169#define DP_S_MOD 5
170#define DP_S_CONV 6
171#define DP_S_DONE 7
172
173/* format flags - Bits */
174#define DP_F_MINUS (1 << 0)
175#define DP_F_PLUS (1 << 1)
176#define DP_F_SPACE (1 << 2)
177#define DP_F_NUM (1 << 3)
178#define DP_F_ZERO (1 << 4)
179#define DP_F_UP (1 << 5)
180#define DP_F_UNSIGNED (1 << 6)
181
182/* Conversion Flags */
183#define DP_C_SHORT 1
184#define DP_C_LONG 2
185#define DP_C_LDOUBLE 3
186#define DP_C_LLONG 4
187
188#define char_to_int(p) ((p)- '0')
189#ifndef MAX
190#define MAX(p,q) (((p) >= (q)) ? (p) : (q))
191#endif
192
193/* yes this really must be a ||. Don't muck with this (tridge) */
194#if !defined(HAVE_VSNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
195
196static size_t dopr(char *buffer, size_t maxlen, const char *format,
197 va_list args_in);
198static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
199 char *value, int flags, int min, int max);
200static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
201 long value, int base, int min, int max, int flags);
202static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
203 LDOUBLE fvalue, int min, int max, int flags);
204static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
205
206static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
207{
208 char ch;
209 LLONG value;
210 LDOUBLE fvalue;
211 char *strvalue;
212 int min;
213 int max;
214 int state;
215 int flags;
216 int cflags;
217 size_t currlen;
218 va_list args;
219
220 VA_COPY(args, args_in);
221
222 state = DP_S_DEFAULT;
223 currlen = flags = cflags = min = 0;
224 max = -1;
225 ch = *format++;
226
227 while (state != DP_S_DONE) {
228 if (ch == '\0')
229 state = DP_S_DONE;
230
231 switch(state) {
232 case DP_S_DEFAULT:
233 if (ch == '%')
234 state = DP_S_FLAGS;
235 else
236 dopr_outch (buffer, &currlen, maxlen, ch);
237 ch = *format++;
238 break;
239 case DP_S_FLAGS:
240 switch (ch) {
241 case '-':
242 flags |= DP_F_MINUS;
243 ch = *format++;
244 break;
245 case '+':
246 flags |= DP_F_PLUS;
247 ch = *format++;
248 break;
249 case ' ':
250 flags |= DP_F_SPACE;
251 ch = *format++;
252 break;
253 case '#':
254 flags |= DP_F_NUM;
255 ch = *format++;
256 break;
257 case '0':
258 flags |= DP_F_ZERO;
259 ch = *format++;
260 break;
261 default:
262 state = DP_S_MIN;
263 break;
264 }
265 break;
266 case DP_S_MIN:
267 if (isdigit((unsigned char)ch)) {
268 min = 10*min + char_to_int (ch);
269 ch = *format++;
270 } else if (ch == '*') {
271 min = va_arg (args, int);
272 ch = *format++;
273 state = DP_S_DOT;
274 } else {
275 state = DP_S_DOT;
276 }
277 break;
278 case DP_S_DOT:
279 if (ch == '.') {
280 state = DP_S_MAX;
281 ch = *format++;
282 } else {
283 state = DP_S_MOD;
284 }
285 break;
286 case DP_S_MAX:
287 if (isdigit((unsigned char)ch)) {
288 if (max < 0)
289 max = 0;
290 max = 10*max + char_to_int (ch);
291 ch = *format++;
292 } else if (ch == '*') {
293 max = va_arg (args, int);
294 ch = *format++;
295 state = DP_S_MOD;
296 } else {
297 state = DP_S_MOD;
298 }
299 break;
300 case DP_S_MOD:
301 switch (ch) {
302 case 'h':
303 cflags = DP_C_SHORT;
304 ch = *format++;
305 break;
306 case 'l':
307 cflags = DP_C_LONG;
308 ch = *format++;
309 if (ch == 'l') { /* It's a long long */
310 cflags = DP_C_LLONG;
311 ch = *format++;
312 }
313 break;
314 case 'L':
315 cflags = DP_C_LDOUBLE;
316 ch = *format++;
317 break;
318 default:
319 break;
320 }
321 state = DP_S_CONV;
322 break;
323 case DP_S_CONV:
324 switch (ch) {
325 case 'd':
326 case 'i':
327 if (cflags == DP_C_SHORT)
328 value = va_arg (args, int);
329 else if (cflags == DP_C_LONG)
330 value = va_arg (args, long int);
331 else if (cflags == DP_C_LLONG)
332 value = va_arg (args, LLONG);
333 else
334 value = va_arg (args, int);
335 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
336 break;
337 case 'o':
338 flags |= DP_F_UNSIGNED;
339 if (cflags == DP_C_SHORT)
340 value = va_arg (args, unsigned int);
341 else if (cflags == DP_C_LONG)
342 value = (long)va_arg (args, unsigned long int);
343 else if (cflags == DP_C_LLONG)
344 value = (long)va_arg (args, unsigned LLONG);
345 else
346 value = (long)va_arg (args, unsigned int);
347 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
348 break;
349 case 'u':
350 flags |= DP_F_UNSIGNED;
351 if (cflags == DP_C_SHORT)
352 value = va_arg (args, unsigned int);
353 else if (cflags == DP_C_LONG)
354 value = (long)va_arg (args, unsigned long int);
355 else if (cflags == DP_C_LLONG)
356 value = (LLONG)va_arg (args, unsigned LLONG);
357 else
358 value = (long)va_arg (args, unsigned int);
359 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
360 break;
361 case 'X':
362 flags |= DP_F_UP;
363 case 'x':
364 flags |= DP_F_UNSIGNED;
365 if (cflags == DP_C_SHORT)
366 value = va_arg (args, unsigned int);
367 else if (cflags == DP_C_LONG)
368 value = (long)va_arg (args, unsigned long int);
369 else if (cflags == DP_C_LLONG)
370 value = (LLONG)va_arg (args, unsigned LLONG);
371 else
372 value = (long)va_arg (args, unsigned int);
373 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
374 break;
375 case 'f':
376 if (cflags == DP_C_LDOUBLE)
377 fvalue = va_arg (args, LDOUBLE);
378 else
379 fvalue = va_arg (args, double);
380 /* um, floating point? */
381 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
382 break;
383 case 'E':
384 flags |= DP_F_UP;
385 case 'e':
386 if (cflags == DP_C_LDOUBLE)
387 fvalue = va_arg (args, LDOUBLE);
388 else
389 fvalue = va_arg (args, double);
390 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
391 break;
392 case 'G':
393 flags |= DP_F_UP;
394 case 'g':
395 if (cflags == DP_C_LDOUBLE)
396 fvalue = va_arg (args, LDOUBLE);
397 else
398 fvalue = va_arg (args, double);
399 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
400 break;
401 case 'c':
402 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
403 break;
404 case 's':
405 strvalue = va_arg (args, char *);
406 if (!strvalue) strvalue = "(NULL)";
407 if (max == -1) {
408 max = strlen(strvalue);
409 }
410 if (min > 0 && max >= 0 && min > max) max = min;
411 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
412 break;
413 case 'p':
414 strvalue = va_arg (args, void *);
415 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
416 break;
417 case 'n':
418 if (cflags == DP_C_SHORT) {
419 short int *num;
420 num = va_arg (args, short int *);
421 *num = currlen;
422 } else if (cflags == DP_C_LONG) {
423 long int *num;
424 num = va_arg (args, long int *);
425 *num = (long int)currlen;
426 } else if (cflags == DP_C_LLONG) {
427 LLONG *num;
428 num = va_arg (args, LLONG *);
429 *num = (LLONG)currlen;
430 } else {
431 int *num;
432 num = va_arg (args, int *);
433 *num = currlen;
434 }
435 break;
436 case '%':
437 dopr_outch (buffer, &currlen, maxlen, ch);
438 break;
439 case 'w':
440 /* not supported yet, treat as next char */
441 ch = *format++;
442 break;
443 default:
444 /* Unknown, skip */
445 break;
446 }
447 ch = *format++;
448 state = DP_S_DEFAULT;
449 flags = cflags = min = 0;
450 max = -1;
451 break;
452 case DP_S_DONE:
453 break;
454 default:
455 /* hmm? */
456 break; /* some picky compilers need this */
457 }
458 }
459 if (maxlen != 0) {
460 if (currlen < maxlen - 1)
461 buffer[currlen] = '\0';
462 else if (maxlen > 0)
463 buffer[maxlen - 1] = '\0';
464 }
465
466 return currlen;
467}
468
469static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
470 char *value, int flags, int min, int max)
471{
472 int padlen, strln; /* amount to pad */
473 int cnt = 0;
474
475#ifdef DEBUG_SNPRINTF
476 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
477#endif
478 if (value == 0) {
479 value = "<NULL>";
480 }
481
482 for (strln = 0; value[strln]; ++strln); /* strlen */
483 padlen = min - strln;
484 if (padlen < 0)
485 padlen = 0;
486 if (flags & DP_F_MINUS)
487 padlen = -padlen; /* Left Justify */
488
489 while ((padlen > 0) && (cnt < max)) {
490 dopr_outch (buffer, currlen, maxlen, ' ');
491 --padlen;
492 ++cnt;
493 }
494 while (*value && (cnt < max)) {
495 dopr_outch (buffer, currlen, maxlen, *value++);
496 ++cnt;
497 }
498 while ((padlen < 0) && (cnt < max)) {
499 dopr_outch (buffer, currlen, maxlen, ' ');
500 ++padlen;
501 ++cnt;
502 }
503}
504
505/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
506
507static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
508 long value, int base, int min, int max, int flags)
509{
510 int signvalue = 0;
511 unsigned long uvalue;
512 char convert[20];
513 int place = 0;
514 int spadlen = 0; /* amount to space pad */
515 int zpadlen = 0; /* amount to zero pad */
516 int caps = 0;
517
518 if (max < 0)
519 max = 0;
520
521 uvalue = value;
522
523 if(!(flags & DP_F_UNSIGNED)) {
524 if( value < 0 ) {
525 signvalue = '-';
526 uvalue = -value;
527 } else {
528 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
529 signvalue = '+';
530 else if (flags & DP_F_SPACE)
531 signvalue = ' ';
532 }
533 }
534
535 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
536
537 do {
538 convert[place++] =
539 (caps? "0123456789ABCDEF":"0123456789abcdef")
540 [uvalue % (unsigned)base ];
541 uvalue = (uvalue / (unsigned)base );
542 } while(uvalue && (place < 20));
543 if (place == 20) place--;
544 convert[place] = 0;
545
546 zpadlen = max - place;
547 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
548 if (zpadlen < 0) zpadlen = 0;
549 if (spadlen < 0) spadlen = 0;
550 if (flags & DP_F_ZERO) {
551 zpadlen = MAX(zpadlen, spadlen);
552 spadlen = 0;
553 }
554 if (flags & DP_F_MINUS)
555 spadlen = -spadlen; /* Left Justifty */
556
557#ifdef DEBUG_SNPRINTF
558 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
559 zpadlen, spadlen, min, max, place);
560#endif
561
562 /* Spaces */
563 while (spadlen > 0) {
564 dopr_outch (buffer, currlen, maxlen, ' ');
565 --spadlen;
566 }
567
568 /* Sign */
569 if (signvalue)
570 dopr_outch (buffer, currlen, maxlen, signvalue);
571
572 /* Zeros */
573 if (zpadlen > 0) {
574 while (zpadlen > 0) {
575 dopr_outch (buffer, currlen, maxlen, '0');
576 --zpadlen;
577 }
578 }
579
580 /* Digits */
581 while (place > 0)
582 dopr_outch (buffer, currlen, maxlen, convert[--place]);
583
584 /* Left Justified spaces */
585 while (spadlen < 0) {
586 dopr_outch (buffer, currlen, maxlen, ' ');
587 ++spadlen;
588 }
589}
590
591static LDOUBLE abs_val(LDOUBLE value)
592{
593 LDOUBLE result = value;
594
595 if (value < 0)
596 result = -value;
597
598 return result;
599}
600
601static LDOUBLE POW10(int exp)
602{
603 LDOUBLE result = 1;
604
605 while (exp) {
606 result *= 10;
607 exp--;
608 }
609
610 return result;
611}
612
613static LLONG ROUND(LDOUBLE value)
614{
615 LLONG intpart;
616
617 intpart = (LLONG)value;
618 value = value - intpart;
619 if (value >= 0.5) intpart++;
620
621 return intpart;
622}
623
624/* a replacement for modf that doesn't need the math library. Should
625 be portable, but slow */
626static double my_modf(double x0, double *iptr)
627{
628 int i;
629 long l;
630 double x = x0;
631 double f = 1.0;
632
633 for (i=0;i<100;i++) {
634 l = (long)x;
635 if (l <= (x+1) && l >= (x-1)) break;
636 x *= 0.1;
637 f *= 10.0;
638 }
639
640 if (i == 100) {
641 /* yikes! the number is beyond what we can handle. What do we do? */
642 (*iptr) = 0;
643 return 0;
644 }
645
646 if (i != 0) {
647 double i2;
648 double ret;
649
650 ret = my_modf(x0-l*f, &i2);
651 (*iptr) = l*f + i2;
652 return ret;
653 }
654
655 (*iptr) = l;
656 return x - (*iptr);
657}
658
659
660static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
661 LDOUBLE fvalue, int min, int max, int flags)
662{
663 int signvalue = 0;
664 double ufvalue;
665 char iconvert[311];
666 char fconvert[311];
667 int iplace = 0;
668 int fplace = 0;
669 int padlen = 0; /* amount to pad */
670 int zpadlen = 0;
671 int caps = 0;
672 int idx;
673 double intpart;
674 double fracpart;
675 double temp;
676
677 /*
678 * AIX manpage says the default is 0, but Solaris says the default
679 * is 6, and sprintf on AIX defaults to 6
680 */
681 if (max < 0)
682 max = 6;
683
684 ufvalue = abs_val (fvalue);
685
686 if (fvalue < 0) {
687 signvalue = '-';
688 } else {
689 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
690 signvalue = '+';
691 } else {
692 if (flags & DP_F_SPACE)
693 signvalue = ' ';
694 }
695 }
696
697#if 0
698 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
699#endif
700
701#if 0
702 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
703#endif
704
705 /*
706 * Sorry, we only support 16 digits past the decimal because of our
707 * conversion method
708 */
709 if (max > 16)
710 max = 16;
711
712 /* We "cheat" by converting the fractional part to integer by
713 * multiplying by a factor of 10
714 */
715
716 temp = ufvalue;
717 my_modf(temp, &intpart);
718
719 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
720
721 if (fracpart >= POW10(max)) {
722 intpart++;
723 fracpart -= POW10(max);
724 }
725
726
727 /* Convert integer part */
728 do {
729 temp = intpart*0.1;
730 my_modf(temp, &intpart);
731 idx = (int) ((temp -intpart +0.05)* 10.0);
732 /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
733 /* printf ("%llf, %f, %x\n", temp, intpart, idx); */
734 iconvert[iplace++] =
735 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
736 } while (intpart && (iplace < 311));
737 if (iplace == 311) iplace--;
738 iconvert[iplace] = 0;
739
740 /* Convert fractional part */
741 if (fracpart)
742 {
743 do {
744 temp = fracpart*0.1;
745 my_modf(temp, &fracpart);
746 idx = (int) ((temp -fracpart +0.05)* 10.0);
747 /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
748 /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
749 fconvert[fplace++] =
750 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
751 } while(fracpart && (fplace < 311));
752 if (fplace == 311) fplace--;
753 }
754 fconvert[fplace] = 0;
755
756 /* -1 for decimal point, another -1 if we are printing a sign */
757 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
758 zpadlen = max - fplace;
759 if (zpadlen < 0) zpadlen = 0;
760 if (padlen < 0)
761 padlen = 0;
762 if (flags & DP_F_MINUS)
763 padlen = -padlen; /* Left Justifty */
764
765 if ((flags & DP_F_ZERO) && (padlen > 0)) {
766 if (signvalue) {
767 dopr_outch (buffer, currlen, maxlen, signvalue);
768 --padlen;
769 signvalue = 0;
770 }
771 while (padlen > 0) {
772 dopr_outch (buffer, currlen, maxlen, '0');
773 --padlen;
774 }
775 }
776 while (padlen > 0) {
777 dopr_outch (buffer, currlen, maxlen, ' ');
778 --padlen;
779 }
780 if (signvalue)
781 dopr_outch (buffer, currlen, maxlen, signvalue);
782
783 while (iplace > 0)
784 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
785
786#ifdef DEBUG_SNPRINTF
787 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
788#endif
789
790 /*
791 * Decimal point. This should probably use locale to find the correct
792 * char to print out.
793 */
794 if (max > 0) {
795 dopr_outch (buffer, currlen, maxlen, '.');
796
797 while (zpadlen > 0) {
798 dopr_outch (buffer, currlen, maxlen, '0');
799 --zpadlen;
800 }
801
802 while (fplace > 0)
803 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
804 }
805
806 while (padlen < 0) {
807 dopr_outch (buffer, currlen, maxlen, ' ');
808 ++padlen;
809 }
810}
811
812static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
813{
814 if (*currlen < maxlen) {
815 buffer[(*currlen)] = c;
816 }
817 (*currlen)++;
818}
819
820 int smb_vsnprintf (char *str, size_t count, const char *fmt, va_list args)
821{
822 return dopr(str, count, fmt, args);
823}
824#define vsnprintf smb_vsnprintf
825#endif
826
827/* yes this really must be a ||. Don't muck with this (tridge)
828 *
829 * The logic for these two is that we need our own definition if the
830 * OS *either* has no definition of *sprintf, or if it does have one
831 * that doesn't work properly according to the autoconf test.
832 */
833#if !defined(HAVE_SNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
834int smb_snprintf(char *str,size_t count,const char *fmt,...)
835{
836 size_t ret;
837 va_list ap;
838
839 va_start(ap, fmt);
840 ret = vsnprintf(str, count, fmt, ap);
841 va_end(ap);
842 return ret;
843}
844#define snprintf smb_snprintf
845#endif
846
847#endif
848
849#ifndef HAVE_VASPRINTF
850 int vasprintf(char **ptr, const char *format, va_list ap)
851{
852 int ret;
853 va_list ap2;
854
855 VA_COPY(ap2, ap);
856
857 ret = vsnprintf(NULL, 0, format, ap2);
858 if (ret <= 0) return ret;
859
860 (*ptr) = (char *)malloc(ret+1);
861 if (!*ptr) return -1;
862
863 VA_COPY(ap2, ap);
864
865 ret = vsnprintf(*ptr, ret+1, format, ap2);
866
867 return ret;
868}
869#endif
870
871
872#ifndef HAVE_ASPRINTF
873 int asprintf(char **ptr, const char *format, ...)
874{
875 va_list ap;
876 int ret;
877
878 *ptr = NULL;
879 va_start(ap, format);
880 ret = vasprintf(ptr, format, ap);
881 va_end(ap);
882
883 return ret;
884}
885#endif
886
887#ifdef TEST_SNPRINTF
888
889 int sprintf(char *str,const char *fmt,...);
890
891 int main (void)
892{
893 char buf1[1024];
894 char buf2[1024];
895 char *fp_fmt[] = {
896 "%1.1f",
897 "%-1.5f",
898 "%1.5f",
899 "%123.9f",
900 "%10.5f",
901 "% 10.5f",
902 "%+22.9f",
903 "%+4.9f",
904 "%01.3f",
905 "%4f",
906 "%3.1f",
907 "%3.2f",
908 "%.0f",
909 "%f",
910 "-16.16f",
911 NULL
912 };
913 double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 203.9, 0.96, 0.996,
914 0.9996, 1.996, 4.136, 5.030201, 0.00205,
915 /* END LIST */ 0};
916 char *int_fmt[] = {
917 "%-1.5d",
918 "%1.5d",
919 "%123.9d",
920 "%5.5d",
921 "%10.5d",
922 "% 10.5d",
923 "%+22.33d",
924 "%01.3d",
925 "%4d",
926 "%d",
927 NULL
928 };
929 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
930 char *str_fmt[] = {
931 "10.5s",
932 "5.10s",
933 "10.1s",
934 "0.10s",
935 "10.0s",
936 "1.10s",
937 "%s",
938 "%.1s",
939 "%.10s",
940 "%10s",
941 NULL
942 };
943 char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
944 int x, y;
945 int fail = 0;
946 int num = 0;
947
948 printf ("Testing snprintf format codes against system sprintf...\n");
949
950 for (x = 0; fp_fmt[x] ; x++) {
951 for (y = 0; fp_nums[y] != 0 ; y++) {
952 int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
953 int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
954 sprintf (buf2, fp_fmt[x], fp_nums[y]);
955 if (strcmp (buf1, buf2)) {
956 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
957 fp_fmt[x], buf1, buf2);
958 fail++;
959 }
960 if (l1 != l2) {
961 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
962 fail++;
963 }
964 num++;
965 }
966 }
967
968 for (x = 0; int_fmt[x] ; x++) {
969 for (y = 0; int_nums[y] != 0 ; y++) {
970 int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
971 int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
972 sprintf (buf2, int_fmt[x], int_nums[y]);
973 if (strcmp (buf1, buf2)) {
974 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
975 int_fmt[x], buf1, buf2);
976 fail++;
977 }
978 if (l1 != l2) {
979 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
980 fail++;
981 }
982 num++;
983 }
984 }
985
986 for (x = 0; str_fmt[x] ; x++) {
987 for (y = 0; str_vals[y] != 0 ; y++) {
988 int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
989 int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
990 sprintf (buf2, str_fmt[x], str_vals[y]);
991 if (strcmp (buf1, buf2)) {
992 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
993 str_fmt[x], buf1, buf2);
994 fail++;
995 }
996 if (l1 != l2) {
997 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
998 fail++;
999 }
1000 num++;
1001 }
1002 }
1003
1004 printf ("%d tests failed out of %d.\n", fail, num);
1005
1006 printf("seeing how many digits we support\n");
1007 {
1008 double v0 = 0.12345678901234567890123456789012345678901;
1009 for (x=0; x<100; x++) {
1010 double p = pow(10, x);
1011 double r = v0*p;
1012 snprintf(buf1, sizeof(buf1), "%1.1f", r);
1013 sprintf(buf2, "%1.1f", r);
1014 if (strcmp(buf1, buf2)) {
1015 printf("we seem to support %d digits\n", x-1);
1016 break;
1017 }
1018 }
1019 }
1020
1021 return 0;
1022}
1023#endif /* TEST_SNPRINTF */