1 /**************************************************************
3 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
4 * A bombproof version of doprnt (dopr) included.
5 * Sigh. This sort of thing is always nasty do deal with. Note that
6 * the version here does not include floating point...
8 * snprintf() is used instead of sprintf() as it does limit checks
9 * for string length. This covers a nasty loophole.
11 * The other functions are there to prevent NULL pointers from
12 * causing nast effects.
15 * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
16 * This was ugly. It is still ugly. I opted out of floating point
17 * numbers, but the formatter understands just about everything
18 * from the normal C string format, at least as far as I can tell from
19 * the Solaris 2.5 printf(3S) man page.
21 * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
22 * Ok, added some minimal floating point support, which means this
23 * probably requires libm on most operating systems. Don't yet
24 * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
25 * was pretty badly broken, it just wasn't being exercised in ways
26 * which showed it, so that's been fixed. Also, formated the code
27 * to mutt conventions, and removed dead code left over from the
28 * original. Also, there is now a builtin-test, just compile with:
29 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
30 * and run snprintf for results.
32 * Thomas Roessler <roessler@does-not-exist.org> 01/27/98 for mutt 0.89i
33 * The PGP code was using unsigned hexadecimal formats.
34 * Unfortunately, unsigned formats simply didn't work.
36 * Michael Elkins <me@mutt.org> 03/05/98 for mutt 0.90.8
37 * The original code assumed that both snprintf() and vsnprintf() were
38 * missing. Some systems only have snprintf() but not vsnprintf(), so
39 * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
41 * Holger Weiss <holger@zedat.fu-berlin.de> 07/23/06 for mutt 1.5.13
42 * A C99 compliant [v]snprintf() returns the number of characters that
43 * would have been written to a sufficiently sized buffer (excluding
44 * the '\0'). Mutt now relies on this behaviour, but the original
45 * code simply returned the length of the resulting output string, so
48 **************************************************************/
54 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
58 #include <sys/types.h>
60 /* Define this as a fall through, HAVE_STDARG_H is probably already set */
62 #define HAVE_VARARGS_H
64 /* varargs declarations: */
66 #if defined(HAVE_STDARG_H)
68 # define HAVE_STDARGS /* let's hope that works everywhere (mj) */
69 # define VA_LOCAL_DECL va_list ap
70 # define VA_START(f) va_start(ap, f)
71 # define VA_SHIFT(v,t) ; /* no-op for ANSI */
72 # define VA_END va_end(ap)
74 # if defined(HAVE_VARARGS_H)
77 # define VA_LOCAL_DECL va_list ap
78 # define VA_START(f) va_start(ap) /* f is ignored! */
79 # define VA_SHIFT(v,t) v = va_arg(ap,t)
80 # define VA_END va_end(ap)
82 /*XX ** NO VARARGS ** XX*/
86 /*int snprintf (char *str, size_t count, const char *fmt, ...);*/
87 /*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/
89 static int dopr (char *buffer, size_t maxlen, const char *format,
91 static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
92 char *value, int flags, int min, int max);
93 static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
94 long value, int base, int min, int max, int flags);
95 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
96 long double fvalue, int min, int max, int flags);
97 static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c );
100 * dopr(): poor man's version of doprintf
103 /* format read states */
104 #define DP_S_DEFAULT 0
113 /* format flags - Bits */
114 #define DP_F_MINUS (1 << 0)
115 #define DP_F_PLUS (1 << 1)
116 #define DP_F_SPACE (1 << 2)
117 #define DP_F_NUM (1 << 3)
118 #define DP_F_ZERO (1 << 4)
119 #define DP_F_UP (1 << 5)
120 #define DP_F_UNSIGNED (1 << 6)
122 /* Conversion Flags */
125 #define DP_C_LONGLONG 3
126 #define DP_C_LDOUBLE 4
128 #define char_to_int(p) (p - '0')
130 #define MAX(p,q) ((p >= q) ? p : q)
132 static int dopr (char *buffer, size_t maxlen, const char *format, va_list args)
145 state = DP_S_DEFAULT;
146 currlen = flags = cflags = min = 0;
150 while (state != DP_S_DONE)
161 dopr_outch (buffer, &currlen, maxlen, ch);
193 if (isdigit((unsigned char)ch))
195 min = 10*min + char_to_int (ch);
200 min = va_arg (args, int);
217 if (isdigit((unsigned char)ch))
221 max = 10*max + char_to_int (ch);
226 max = va_arg (args, int);
245 cflags = DP_C_LONGLONG;
250 cflags = DP_C_LDOUBLE;
263 if (cflags == DP_C_SHORT)
264 value = va_arg (args, short int);
265 else if (cflags == DP_C_LONG)
266 value = va_arg (args, long int);
267 else if (cflags == DP_C_LONGLONG)
268 value = va_arg (args, long long int);
270 value = va_arg (args, int);
271 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
274 flags |= DP_F_UNSIGNED;
275 if (cflags == DP_C_SHORT)
276 value = va_arg (args, unsigned short int);
277 else if (cflags == DP_C_LONG)
278 value = va_arg (args, unsigned long int);
279 else if (cflags == DP_C_LONGLONG)
280 value = va_arg (args, unsigned long long int);
282 value = va_arg (args, unsigned int);
283 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
286 flags |= DP_F_UNSIGNED;
287 if (cflags == DP_C_SHORT)
288 value = va_arg (args, unsigned short int);
289 else if (cflags == DP_C_LONG)
290 value = va_arg (args, unsigned long int);
291 else if (cflags == DP_C_LONGLONG)
292 value = va_arg (args, unsigned long long int);
294 value = va_arg (args, unsigned int);
295 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
300 flags |= DP_F_UNSIGNED;
301 if (cflags == DP_C_SHORT)
302 value = va_arg (args, unsigned short int);
303 else if (cflags == DP_C_LONG)
304 value = va_arg (args, unsigned long int);
305 else if (cflags == DP_C_LONGLONG)
306 value = va_arg (args, unsigned long long int);
308 value = va_arg (args, unsigned int);
309 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
312 if (cflags == DP_C_LDOUBLE)
313 fvalue = va_arg (args, long double);
315 fvalue = va_arg (args, double);
316 /* um, floating point? */
317 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
322 if (cflags == DP_C_LDOUBLE)
323 fvalue = va_arg (args, long double);
325 fvalue = va_arg (args, double);
330 if (cflags == DP_C_LDOUBLE)
331 fvalue = va_arg (args, long double);
333 fvalue = va_arg (args, double);
336 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
339 strvalue = va_arg (args, char *);
340 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
343 flags |= DP_F_UNSIGNED;
344 strvalue = va_arg (args, void *);
345 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
348 if (cflags == DP_C_SHORT)
351 num = va_arg (args, short int *);
354 else if (cflags == DP_C_LONG)
357 num = va_arg (args, long int *);
360 else if (cflags == DP_C_LONGLONG)
363 num = va_arg (args, long long int *);
369 num = va_arg (args, int *);
374 dopr_outch (buffer, &currlen, maxlen, ch);
377 /* not supported yet, treat as next char */
385 state = DP_S_DEFAULT;
386 flags = cflags = min = 0;
393 break; /* some picky compilers need this */
396 if (currlen < maxlen - 1)
397 buffer[currlen] = '\0';
399 buffer[maxlen - 1] = '\0';
404 static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
405 char *value, int flags, int min, int max)
407 int padlen, strln; /* amount to pad */
415 for (strln = 0; value[strln]; ++strln); /* strlen */
416 padlen = min - strln;
419 if (flags & DP_F_MINUS)
420 padlen = -padlen; /* Left Justify */
422 while ((padlen > 0) && (max == -1 || cnt < max))
424 dopr_outch (buffer, currlen, maxlen, ' ');
428 while (*value && (max == -1 || cnt < max))
430 dopr_outch (buffer, currlen, maxlen, *value++);
433 while ((padlen < 0) && (max == -1 || cnt < max))
435 dopr_outch (buffer, currlen, maxlen, ' ');
441 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
443 static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
444 long value, int base, int min, int max, int flags)
447 unsigned long uvalue;
450 int spadlen = 0; /* amount to space pad */
451 int zpadlen = 0; /* amount to zero pad */
459 if(!(flags & DP_F_UNSIGNED))
466 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
469 if (flags & DP_F_SPACE)
473 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
477 (caps? "0123456789ABCDEF":"0123456789abcdef")
478 [uvalue % (unsigned)base ];
479 uvalue = (uvalue / (unsigned)base );
480 } while(uvalue && (place < 20));
481 if (place == 20) place--;
484 zpadlen = max - place;
485 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
486 if (zpadlen < 0) zpadlen = 0;
487 if (spadlen < 0) spadlen = 0;
488 if (flags & DP_F_ZERO)
490 zpadlen = MAX(zpadlen, spadlen);
493 if (flags & DP_F_MINUS)
494 spadlen = -spadlen; /* Left Justifty */
496 #ifdef DEBUG_SNPRINTF
497 dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
498 zpadlen, spadlen, min, max, place));
504 dopr_outch (buffer, currlen, maxlen, ' ');
510 dopr_outch (buffer, currlen, maxlen, signvalue);
517 dopr_outch (buffer, currlen, maxlen, '0');
524 dopr_outch (buffer, currlen, maxlen, convert[--place]);
526 /* Left Justified spaces */
527 while (spadlen < 0) {
528 dopr_outch (buffer, currlen, maxlen, ' ');
533 static long double abs_val (long double value)
535 long double result = value;
543 static long double pow10 (int exp)
545 long double result = 1;
556 static long round (long double value)
561 value = value - intpart;
568 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
569 long double fvalue, int min, int max, int flags)
577 int padlen = 0; /* amount to pad */
584 * AIX manpage says the default is 0, but Solaris says the default
585 * is 6, and sprintf on AIX defaults to 6
590 ufvalue = abs_val (fvalue);
595 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
598 if (flags & DP_F_SPACE)
602 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
608 * Sorry, we only support 9 digits past the decimal because of our
614 /* We "cheat" by converting the fractional part to integer by
615 * multiplying by a factor of 10
617 fracpart = round ((pow10 (max)) * (ufvalue - intpart));
619 if (fracpart >= pow10 (max))
622 fracpart -= pow10 (max);
625 #ifdef DEBUG_SNPRINTF
626 dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart));
629 /* Convert integer part */
632 (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
633 intpart = (intpart / 10);
634 } while(intpart && (iplace < 20));
635 if (iplace == 20) iplace--;
636 iconvert[iplace] = 0;
638 /* Convert fractional part */
641 (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
642 fracpart = (fracpart / 10);
643 } while(fracpart && (fplace < 20));
644 if (fplace == 20) fplace--;
645 fconvert[fplace] = 0;
647 /* -1 for decimal point, another -1 if we are printing a sign */
648 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
649 zpadlen = max - fplace;
654 if (flags & DP_F_MINUS)
655 padlen = -padlen; /* Left Justifty */
657 if ((flags & DP_F_ZERO) && (padlen > 0))
661 dopr_outch (buffer, currlen, maxlen, signvalue);
667 dopr_outch (buffer, currlen, maxlen, '0');
673 dopr_outch (buffer, currlen, maxlen, ' ');
677 dopr_outch (buffer, currlen, maxlen, signvalue);
680 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
683 * Decimal point. This should probably use locale to find the correct
686 dopr_outch (buffer, currlen, maxlen, '.');
689 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
693 dopr_outch (buffer, currlen, maxlen, '0');
699 dopr_outch (buffer, currlen, maxlen, ' ');
704 static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c)
706 if (*currlen < maxlen)
707 buffer[*currlen] = c;
710 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
712 #ifndef HAVE_VSNPRINTF
713 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
716 return(dopr(str, count, fmt, args));
718 #endif /* !HAVE_VSNPRINTF */
720 #ifndef HAVE_SNPRINTF
723 int snprintf (char *str,size_t count,const char *fmt,...)
725 int snprintf (va_alist) va_dcl
737 VA_SHIFT (str, char *);
738 VA_SHIFT (count, size_t );
739 VA_SHIFT (fmt, char *);
740 len = vsnprintf(str, count, fmt, ap);
747 #define LONG_STRING 1024
751 char buf1[LONG_STRING];
752 char buf2[LONG_STRING];
767 double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
768 0.9996, 1.996, 4.136, 0};
781 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
786 printf ("Testing snprintf format codes against system sprintf...\n");
788 for (x = 0; fp_fmt[x] != NULL ; x++)
789 for (y = 0; fp_nums[y] != 0 ; y++)
791 snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
792 sprintf (buf2, fp_fmt[x], fp_nums[y]);
793 if (strcmp (buf1, buf2))
795 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", /* __SPRINTF_CHECKED__ */
796 fp_fmt[x], buf1, buf2);
802 for (x = 0; int_fmt[x] != NULL ; x++)
803 for (y = 0; int_nums[y] != 0 ; y++)
805 snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
806 sprintf (buf2, int_fmt[x], int_nums[y]);
807 if (strcmp (buf1, buf2))
809 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", /* __SPRINTF_CHECKED__ */
810 int_fmt[x], buf1, buf2);
815 printf ("%d tests failed out of %d.\n", fail, num);
817 #endif /* SNPRINTF_TEST */
819 #endif /* !HAVE_SNPRINTF */