2 * Copyright Patrick Powell 1995
3 * This code is based on code written by Patrick Powell (papowell@astart.com)
4 * It may be used for any purpose as long as this notice remains intact
5 * on all source code distributions
8 /**************************************************************
10 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
11 * A bombproof version of doprnt (dopr) included.
12 * Sigh. This sort of thing is always nasty do deal with. Note that
13 * the version here does not include floating point...
15 * snprintf() is used instead of sprintf() as it does limit checks
16 * for string length. This covers a nasty loophole.
18 * The other functions are there to prevent NULL pointers from
19 * causing nast effects.
22 * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
23 * This was ugly. It is still ugly. I opted out of floating point
24 * numbers, but the formatter understands just about everything
25 * from the normal C string format, at least as far as I can tell from
26 * the Solaris 2.5 printf(3S) man page.
28 * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
29 * Ok, added some minimal floating point support, which means this
30 * probably requires libm on most operating systems. Don't yet
31 * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
32 * was pretty badly broken, it just wasn't being exercised in ways
33 * which showed it, so that's been fixed. Also, formated the code
34 * to mutt conventions, and removed dead code left over from the
35 * original. Also, there is now a builtin-test, just compile with:
36 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
37 * and run snprintf for results.
39 * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
40 * The PGP code was using unsigned hexadecimal formats.
41 * Unfortunately, unsigned formats simply didn't work.
43 * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
44 * The original code assumed that both snprintf() and vsnprintf() were
45 * missing. Some systems only have snprintf() but not vsnprintf(), so
46 * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
48 * Andrew Tridgell (tridge@samba.org) Oct 1998
49 * fixed handling of %.0f
50 * added test for HAVE_LONG_DOUBLE
52 * tridge@samba.org, idra@samba.org, April 2001
53 * got rid of fcvt code (twas buggy and made testing harder)
56 * date: 2002/12/19 19:56:31; author: herb; state: Exp; lines: +2 -0
57 * actually print args for %g and %e
59 * date: 2002/06/03 13:37:52; author: jmcd; state: Exp; lines: +8 -0
60 * Since includes.h isn't included here, VA_COPY has to be defined here. I don't
61 * see any include file that is guaranteed to be here, so I'm defining it
62 * locally. Fixes AIX and Solaris builds.
64 * date: 2002/06/03 03:07:24; author: tridge; state: Exp; lines: +5 -13
65 * put the ifdef for HAVE_VA_COPY in one place rather than in lots of
68 * date: 2002/05/17 14:51:22; author: jmcd; state: Exp; lines: +21 -4
69 * Fix usage of va_list passed as an arg. Use __va_copy before using it
72 * date: 2002/04/16 22:38:04; author: idra; state: Exp; lines: +20 -14
73 * Fix incorrect zpadlen handling in fmtfp.
74 * Thanks to Ollie Oldham <ollie.oldham@metro-optix.com> for spotting it.
75 * few mods to make it easier to compile the tests.
76 * addedd the "Ollie" test to the floating point ones.
78 * Martin Pool (mbp@samba.org) April 2003
79 * Remove NO_CONFIG_H so that the test case can be built within a source
80 * tree with less trouble.
81 * Remove unnecessary SAFE_FREE() definition.
83 * Martin Pool (mbp@samba.org) May 2003
84 * Put in a prototype for dummy_snprintf() to quiet compiler warnings.
86 * Move #endif to make sure VA_COPY, LDOUBLE, etc are defined even
87 * if the C library has some snprintf functions already.
88 **************************************************************/
92 #if defined(BROKEN_SNPRINTF) /* For those with broken snprintf() */
94 # undef HAVE_VSNPRINTF
99 # define VA_COPY(dest, src) va_copy(dest, src)
101 # ifdef HAVE___VA_COPY
102 # define VA_COPY(dest, src) __va_copy(dest, src)
104 # define VA_COPY(dest, src) (dest) = (src)
109 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
115 #ifdef HAVE_LONG_DOUBLE
116 # define LDOUBLE long double
118 # define LDOUBLE double
121 #ifdef HAVE_LONG_LONG
122 # define LLONG long long
128 * dopr(): poor man's version of doprintf
131 /* format read states */
132 #define DP_S_DEFAULT 0
141 /* format flags - Bits */
142 #define DP_F_MINUS (1 << 0)
143 #define DP_F_PLUS (1 << 1)
144 #define DP_F_SPACE (1 << 2)
145 #define DP_F_NUM (1 << 3)
146 #define DP_F_ZERO (1 << 4)
147 #define DP_F_UP (1 << 5)
148 #define DP_F_UNSIGNED (1 << 6)
150 /* Conversion Flags */
153 #define DP_C_LDOUBLE 3
156 #define char_to_int(p) ((p)- '0')
158 # define MAX(p,q) (((p) >= (q)) ? (p) : (q))
161 static size_t dopr(char *buffer, size_t maxlen, const char *format,
163 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
164 char *value, int flags, int min, int max);
165 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
166 LLONG value, int base, int min, int max, int flags);
167 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
168 LDOUBLE fvalue, int min, int max, int flags);
169 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
171 static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
185 VA_COPY(args, args_in);
187 state = DP_S_DEFAULT;
188 currlen = flags = cflags = min = 0;
192 while (state != DP_S_DONE) {
201 dopr_outch (buffer, &currlen, maxlen, ch);
232 if (isdigit((unsigned char)ch)) {
233 min = 10*min + char_to_int (ch);
235 } else if (ch == '*') {
236 min = va_arg (args, int);
252 if (isdigit((unsigned char)ch)) {
255 max = 10*max + char_to_int (ch);
257 } else if (ch == '*') {
258 max = va_arg (args, int);
274 if (ch == 'l') { /* It's a long long */
280 cflags = DP_C_LDOUBLE;
292 if (cflags == DP_C_SHORT)
293 value = va_arg (args, int);
294 else if (cflags == DP_C_LONG)
295 value = va_arg (args, long int);
296 else if (cflags == DP_C_LLONG)
297 value = va_arg (args, LLONG);
299 value = va_arg (args, int);
300 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
303 flags |= DP_F_UNSIGNED;
304 if (cflags == DP_C_SHORT)
305 value = va_arg (args, unsigned int);
306 else if (cflags == DP_C_LONG)
307 value = (long)va_arg (args, unsigned long int);
308 else if (cflags == DP_C_LLONG)
309 value = (long)va_arg (args, unsigned LLONG);
311 value = (long)va_arg (args, unsigned int);
312 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
315 flags |= DP_F_UNSIGNED;
316 if (cflags == DP_C_SHORT)
317 value = va_arg (args, unsigned int);
318 else if (cflags == DP_C_LONG)
319 value = (long)va_arg (args, unsigned long int);
320 else if (cflags == DP_C_LLONG)
321 value = (LLONG)va_arg (args, unsigned LLONG);
323 value = (long)va_arg (args, unsigned int);
324 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
329 flags |= DP_F_UNSIGNED;
330 if (cflags == DP_C_SHORT)
331 value = va_arg (args, unsigned int);
332 else if (cflags == DP_C_LONG)
333 value = (long)va_arg (args, unsigned long int);
334 else if (cflags == DP_C_LLONG)
335 value = (LLONG)va_arg (args, unsigned LLONG);
337 value = (long)va_arg (args, unsigned int);
338 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
341 if (cflags == DP_C_LDOUBLE)
342 fvalue = va_arg (args, LDOUBLE);
344 fvalue = va_arg (args, double);
345 /* um, floating point? */
346 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
351 if (cflags == DP_C_LDOUBLE)
352 fvalue = va_arg (args, LDOUBLE);
354 fvalue = va_arg (args, double);
355 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
360 if (cflags == DP_C_LDOUBLE)
361 fvalue = va_arg (args, LDOUBLE);
363 fvalue = va_arg (args, double);
364 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
367 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
370 strvalue = va_arg (args, char *);
371 if (!strvalue) strvalue = "(NULL)";
373 max = strlen(strvalue);
375 if (min > 0 && max >= 0 && min > max) max = min;
376 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
379 strvalue = va_arg (args, void *);
380 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
383 if (cflags == DP_C_SHORT) {
385 num = va_arg (args, short int *);
387 } else if (cflags == DP_C_LONG) {
389 num = va_arg (args, long int *);
390 *num = (long int)currlen;
391 } else if (cflags == DP_C_LLONG) {
393 num = va_arg (args, LLONG *);
394 *num = (LLONG)currlen;
397 num = va_arg (args, int *);
402 dopr_outch (buffer, &currlen, maxlen, ch);
405 /* not supported yet, treat as next char */
413 state = DP_S_DEFAULT;
414 flags = cflags = min = 0;
421 break; /* some picky compilers need this */
425 if (currlen < maxlen - 1)
426 buffer[currlen] = '\0';
428 buffer[maxlen - 1] = '\0';
434 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
435 char *value, int flags, int min, int max)
437 int padlen, strln; /* amount to pad */
440 #ifdef DEBUG_SNPRINTF
441 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
447 for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */
448 padlen = min - strln;
451 if (flags & DP_F_MINUS)
452 padlen = -padlen; /* Left Justify */
454 while ((padlen > 0) && (cnt < max)) {
455 dopr_outch (buffer, currlen, maxlen, ' ');
459 while (*value && (cnt < max)) {
460 dopr_outch (buffer, currlen, maxlen, *value++);
463 while ((padlen < 0) && (cnt < max)) {
464 dopr_outch (buffer, currlen, maxlen, ' ');
470 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
472 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
473 LLONG value, int base, int min, int max, int flags)
476 unsigned LLONG uvalue;
479 int spadlen = 0; /* amount to space pad */
480 int zpadlen = 0; /* amount to zero pad */
488 if(!(flags & DP_F_UNSIGNED)) {
493 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
495 else if (flags & DP_F_SPACE)
500 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
504 (caps? "0123456789ABCDEF":"0123456789abcdef")
505 [uvalue % (unsigned)base ];
506 uvalue = (uvalue / (unsigned)base );
507 } while(uvalue && (place < 20));
508 if (place == 20) place--;
511 zpadlen = max - place;
512 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
513 if (zpadlen < 0) zpadlen = 0;
514 if (spadlen < 0) spadlen = 0;
515 if (flags & DP_F_ZERO) {
516 zpadlen = MAX(zpadlen, spadlen);
519 if (flags & DP_F_MINUS)
520 spadlen = -spadlen; /* Left Justifty */
522 #ifdef DEBUG_SNPRINTF
523 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
524 zpadlen, spadlen, min, max, place);
528 while (spadlen > 0) {
529 dopr_outch (buffer, currlen, maxlen, ' ');
535 dopr_outch (buffer, currlen, maxlen, signvalue);
539 while (zpadlen > 0) {
540 dopr_outch (buffer, currlen, maxlen, '0');
547 dopr_outch (buffer, currlen, maxlen, convert[--place]);
549 /* Left Justified spaces */
550 while (spadlen < 0) {
551 dopr_outch (buffer, currlen, maxlen, ' ');
556 static LDOUBLE abs_val(LDOUBLE value)
558 LDOUBLE result = value;
566 static LDOUBLE POW10(int exp)
578 static LLONG ROUND(LDOUBLE value)
582 intpart = (LLONG)value;
583 value = value - intpart;
584 if (value >= 0.5) intpart++;
589 /* a replacement for modf that doesn't need the math library. Should
590 be portable, but slow */
591 static double my_modf(double x0, double *iptr)
598 for (i=0;i<100;i++) {
600 if (l <= (x+1) && l >= (x-1)) break;
606 /* yikes! the number is beyond what we can handle. What do we do? */
615 ret = my_modf(x0-l*f, &i2);
625 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
626 LDOUBLE fvalue, int min, int max, int flags)
634 int padlen = 0; /* amount to pad */
643 * AIX manpage says the default is 0, but Solaris says the default
644 * is 6, and sprintf on AIX defaults to 6
649 ufvalue = abs_val (fvalue);
654 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
657 if (flags & DP_F_SPACE)
663 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
667 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
671 * Sorry, we only support 16 digits past the decimal because of our
677 /* We "cheat" by converting the fractional part to integer by
678 * multiplying by a factor of 10
682 my_modf(temp, &intpart);
684 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
686 if (fracpart >= POW10(max)) {
688 fracpart -= POW10(max);
691 /* Convert integer part */
694 my_modf(temp, &intpart);
695 idx = (int) ((temp -intpart +0.05)* 10.0);
696 /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
697 /* printf ("%llf, %f, %x\n", temp, intpart, idx); */
699 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
700 } while (intpart && (iplace < 311));
701 if (iplace == 311) iplace--;
702 iconvert[iplace] = 0;
704 /* Convert fractional part */
709 my_modf(temp, &fracpart);
710 idx = (int) ((temp -fracpart +0.05)* 10.0);
711 /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
712 /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
714 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
715 } while(fracpart && (fplace < 311));
716 if (fplace == 311) fplace--;
718 fconvert[fplace] = 0;
720 /* -1 for decimal point, another -1 if we are printing a sign */
721 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
722 zpadlen = max - fplace;
723 if (zpadlen < 0) zpadlen = 0;
726 if (flags & DP_F_MINUS)
727 padlen = -padlen; /* Left Justifty */
729 if ((flags & DP_F_ZERO) && (padlen > 0)) {
731 dopr_outch (buffer, currlen, maxlen, signvalue);
736 dopr_outch (buffer, currlen, maxlen, '0');
741 dopr_outch (buffer, currlen, maxlen, ' ');
745 dopr_outch (buffer, currlen, maxlen, signvalue);
748 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
750 #ifdef DEBUG_SNPRINTF
751 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
755 * Decimal point. This should probably use locale to find the correct
759 dopr_outch (buffer, currlen, maxlen, '.');
761 while (zpadlen > 0) {
762 dopr_outch (buffer, currlen, maxlen, '0');
767 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
771 dopr_outch (buffer, currlen, maxlen, ' ');
776 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
778 if (*currlen < maxlen) {
779 buffer[(*currlen)] = c;
783 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
785 #if !defined(HAVE_VSNPRINTF)
786 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
788 return dopr(str, count, fmt, args);
792 #if !defined(HAVE_SNPRINTF)
793 int snprintf(char *str, size_t count, SNPRINTF_CONST char *fmt, ...)
799 ret = vsnprintf(str, count, fmt, ap);