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 **************************************************************/
47 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
51 #include <sys/types.h>
53 #include "lib/debug.h"
55 /* Define this as a fall through, HAVE_STDARG_H is probably already set */
57 #define HAVE_VARARGS_H
59 /* varargs declarations: */
61 #if defined(HAVE_STDARG_H)
63 # define HAVE_STDARGS /* let's hope that works everywhere (mj) */
64 # define VA_LOCAL_DECL va_list ap
65 # define VA_START(f) va_start(ap, f)
66 # define VA_SHIFT(v,t) ; /* no-op for ANSI */
67 # define VA_END va_end(ap)
69 # if defined(HAVE_VARARGS_H)
72 # define VA_LOCAL_DECL va_list ap
73 # define VA_START(f) va_start(ap) /* f is ignored! */
74 # define VA_SHIFT(v,t) v = va_arg(ap,t)
75 # define VA_END va_end(ap)
77 /*XX ** NO VARARGS ** XX*/
81 /*int snprintf (char *str, size_t count, const char *fmt, ...);*/
82 /*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/
84 static void dopr (char *buffer, size_t maxlen, const char *format,
86 static void fmtstr (char *buffer, size_t * currlen, size_t maxlen,
87 char *value, int flags, int min, int max);
88 static void fmtint (char *buffer, size_t * currlen, size_t maxlen,
89 long value, int base, int min, int max, int flags);
90 static void fmtfp (char *buffer, size_t * currlen, size_t maxlen,
91 long double fvalue, int min, int max, int flags);
92 static void dopr_outch (char *buffer, size_t * currlen, size_t maxlen,
96 * dopr(): poor man's version of doprintf
99 /* format read states */
100 #define DP_S_DEFAULT 0
109 /* format flags - Bits */
110 #define DP_F_MINUS (1 << 0)
111 #define DP_F_PLUS (1 << 1)
112 #define DP_F_SPACE (1 << 2)
113 #define DP_F_NUM (1 << 3)
114 #define DP_F_ZERO (1 << 4)
115 #define DP_F_UP (1 << 5)
116 #define DP_F_UNSIGNED (1 << 6)
118 /* Conversion Flags */
121 #define DP_C_LDOUBLE 3
123 #define char_to_int(p) (p - '0')
125 #define MAX(p,q) ((p >= q) ? p : q)
127 static void dopr (char *buffer, size_t maxlen, const char *format,
141 state = DP_S_DEFAULT;
142 currlen = flags = cflags = min = 0;
146 while (state != DP_S_DONE) {
147 if ((ch == '\0') || (currlen >= maxlen))
155 dopr_outch (buffer, &currlen, maxlen, ch);
186 if (isdigit ((unsigned char) ch)) {
187 min = 10 * min + char_to_int (ch);
190 else if (ch == '*') {
191 min = va_arg (args, int);
208 if (isdigit ((unsigned char) ch)) {
211 max = 10 * max + char_to_int (ch);
214 else if (ch == '*') {
215 max = va_arg (args, int);
224 /* Currently, we don't support Long Long, bummer */
235 cflags = DP_C_LDOUBLE;
247 if (cflags == DP_C_SHORT)
248 value = va_arg (args, short int);
250 else if (cflags == DP_C_LONG)
251 value = va_arg (args, long int);
254 value = va_arg (args, int);
256 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
259 flags |= DP_F_UNSIGNED;
260 if (cflags == DP_C_SHORT)
261 value = va_arg (args, unsigned short int);
263 else if (cflags == DP_C_LONG)
264 value = va_arg (args, unsigned long int);
267 value = va_arg (args, unsigned int);
269 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
272 flags |= DP_F_UNSIGNED;
273 if (cflags == DP_C_SHORT)
274 value = va_arg (args, unsigned short int);
276 else if (cflags == DP_C_LONG)
277 value = va_arg (args, unsigned long int);
280 value = va_arg (args, unsigned int);
282 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
287 flags |= DP_F_UNSIGNED;
288 if (cflags == DP_C_SHORT)
289 value = va_arg (args, unsigned short int);
291 else if (cflags == DP_C_LONG)
292 value = va_arg (args, unsigned long int);
295 value = va_arg (args, unsigned int);
297 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
300 if (cflags == DP_C_LDOUBLE)
301 fvalue = va_arg (args, long double);
304 fvalue = va_arg (args, double);
306 /* um, floating point? */
307 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
312 if (cflags == DP_C_LDOUBLE)
313 fvalue = va_arg (args, long double);
316 fvalue = va_arg (args, double);
322 if (cflags == DP_C_LDOUBLE)
323 fvalue = va_arg (args, long double);
326 fvalue = va_arg (args, double);
330 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
334 strvalue = va_arg (args, char *);
337 max = maxlen; /* ie, no max */
338 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
341 strvalue = va_arg (args, void *);
343 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max,
347 if (cflags == DP_C_SHORT) {
349 num = va_arg (args, short int *);
353 else if (cflags == DP_C_LONG) {
355 num = va_arg (args, long int *);
361 num = va_arg (args, int *);
367 dopr_outch (buffer, &currlen, maxlen, ch);
370 /* not supported yet, treat as next char */
378 state = DP_S_DEFAULT;
379 flags = cflags = min = 0;
386 break; /* some picky compilers need this */
389 if (currlen < maxlen - 1)
390 buffer[currlen] = '\0';
392 buffer[maxlen - 1] = '\0';
395 static void fmtstr (char *buffer, size_t * currlen, size_t maxlen,
396 char *value, int flags, int min, int max)
398 int padlen, strln; /* amount to pad */
405 for (strln = 0; value[strln]; ++strln); /* str_len */
406 padlen = min - strln;
409 if (flags & DP_F_MINUS)
410 padlen = -padlen; /* Left Justify */
412 while ((padlen > 0) && (cnt < max)) {
413 dopr_outch (buffer, currlen, maxlen, ' ');
417 while (*value && (cnt < max)) {
418 dopr_outch (buffer, currlen, maxlen, *value++);
421 while ((padlen < 0) && (cnt < max)) {
422 dopr_outch (buffer, currlen, maxlen, ' ');
428 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
430 static void fmtint (char *buffer, size_t * currlen, size_t maxlen,
431 long value, int base, int min, int max, int flags)
434 unsigned long uvalue;
437 int spadlen = 0; /* amount to space pad */
438 int zpadlen = 0; /* amount to zero pad */
446 if (!(flags & DP_F_UNSIGNED)) {
451 else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
453 else if (flags & DP_F_SPACE)
458 caps = 1; /* Should characters be upper case? */
461 convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
462 [uvalue % (unsigned) base];
463 uvalue = (uvalue / (unsigned) base);
464 } while (uvalue && (place < 20));
469 zpadlen = max - place;
470 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
475 if (flags & DP_F_ZERO) {
476 zpadlen = MAX (zpadlen, spadlen);
479 if (flags & DP_F_MINUS)
480 spadlen = -spadlen; /* Left Justifty */
482 #ifdef DEBUG_SNPRINTF
483 debug_print (1, ("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
484 zpadlen, spadlen, min, max, place));
488 while (spadlen > 0) {
489 dopr_outch (buffer, currlen, maxlen, ' ');
495 dopr_outch (buffer, currlen, maxlen, signvalue);
499 while (zpadlen > 0) {
500 dopr_outch (buffer, currlen, maxlen, '0');
507 dopr_outch (buffer, currlen, maxlen, convert[--place]);
509 /* Left Justified spaces */
510 while (spadlen < 0) {
511 dopr_outch (buffer, currlen, maxlen, ' ');
516 static long double abs_val (long double value)
518 long double result = value;
526 static long double pow10 (int exp)
528 long double result = 1;
538 static long round (long double value)
543 value = value - intpart;
550 static void fmtfp (char *buffer, size_t * currlen, size_t maxlen,
551 long double fvalue, int min, int max, int flags)
559 int padlen = 0; /* amount to pad */
566 * AIX manpage says the default is 0, but Solaris says the default
567 * is 6, and sprintf on AIX defaults to 6
572 ufvalue = abs_val (fvalue);
576 else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
578 else if (flags & DP_F_SPACE)
583 caps = 1; /* Should characters be upper case? */
589 * Sorry, we only support 9 digits past the decimal because of our
595 /* We "cheat" by converting the fractional part to integer by
596 * multiplying by a factor of 10
598 fracpart = round ((pow10 (max)) * (ufvalue - intpart));
600 if (fracpart >= pow10 (max)) {
602 fracpart -= pow10 (max);
605 #ifdef DEBUG_SNPRINTF
606 debug_print (1, ("fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart));
609 /* Convert integer part */
612 (caps ? "0123456789ABCDEF" : "0123456789abcdef")[intpart % 10];
613 intpart = (intpart / 10);
614 } while (intpart && (iplace < 20));
617 iconvert[iplace] = 0;
619 /* Convert fractional part */
622 (caps ? "0123456789ABCDEF" : "0123456789abcdef")[fracpart % 10];
623 fracpart = (fracpart / 10);
624 } while (fracpart && (fplace < 20));
627 fconvert[fplace] = 0;
629 /* -1 for decimal point, another -1 if we are printing a sign */
630 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
631 zpadlen = max - fplace;
636 if (flags & DP_F_MINUS)
637 padlen = -padlen; /* Left Justifty */
639 if ((flags & DP_F_ZERO) && (padlen > 0)) {
641 dopr_outch (buffer, currlen, maxlen, signvalue);
646 dopr_outch (buffer, currlen, maxlen, '0');
651 dopr_outch (buffer, currlen, maxlen, ' ');
655 dopr_outch (buffer, currlen, maxlen, signvalue);
658 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
661 * Decimal point. This should probably use locale to find the correct
664 dopr_outch (buffer, currlen, maxlen, '.');
667 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
669 while (zpadlen > 0) {
670 dopr_outch (buffer, currlen, maxlen, '0');
675 dopr_outch (buffer, currlen, maxlen, ' ');
680 static void dopr_outch (char *buffer, size_t * currlen, size_t maxlen, char c)
682 if (*currlen < maxlen)
683 buffer[(*currlen)++] = c;
685 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
687 #ifndef HAVE_VSNPRINTF
688 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
691 dopr (str, count, fmt, args);
692 return (str_len (str));
694 #endif /* !HAVE_VSNPRINTF */
696 #ifndef HAVE_SNPRINTF
699 int snprintf (char *str, size_t count, const char *fmt, ...)
701 int snprintf (va_alist)
713 VA_SHIFT (str, char *);
715 VA_SHIFT (count, size_t);
716 VA_SHIFT (fmt, char *);
718 (void) vsnprintf (str, count, fmt, ap);
720 return (str_len (str));
725 #define LONG_STRING 1024
729 char buf1[LONG_STRING];
730 char buf2[LONG_STRING];
745 double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
746 0.9996, 1.996, 4.136, 0
760 long int_nums[] = { -1, 134, 91340, 341, 0203, 0 };
765 printf ("Testing snprintf format codes against system sprintf...\n");
767 for (x = 0; fp_fmt[x] != NULL; x++)
768 for (y = 0; fp_nums[y] != 0; y++) {
769 snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
770 sprintf (buf2, fp_fmt[x], fp_nums[y]);
771 if (str_cmp (buf1, buf2)) {
772 printf ("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", /* __SPRINTF_CHECKED__ */
773 fp_fmt[x], buf1, buf2);
779 for (x = 0; int_fmt[x] != NULL; x++)
780 for (y = 0; int_nums[y] != 0; y++) {
781 snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
782 sprintf (buf2, int_fmt[x], int_nums[y]);
783 if (str_cmp (buf1, buf2)) {
784 printf ("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", /* __SPRINTF_CHECKED__ */
785 int_fmt[x], buf1, buf2);
790 printf ("%d tests failed out of %d.\n", fail, num);
792 #endif /* SNPRINTF_TEST */
794 #endif /* !HAVE_SNPRINTF */