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 /* Define this as a fall through, HAVE_STDARG_H is probably already set */
55 #define HAVE_VARARGS_H
57 /* varargs declarations: */
59 #if defined(HAVE_STDARG_H)
61 # define HAVE_STDARGS /* let's hope that works everywhere (mj) */
62 # define VA_LOCAL_DECL va_list ap
63 # define VA_START(f) va_start(ap, f)
64 # define VA_SHIFT(v,t) ; /* no-op for ANSI */
65 # define VA_END va_end(ap)
67 # if defined(HAVE_VARARGS_H)
70 # define VA_LOCAL_DECL va_list ap
71 # define VA_START(f) va_start(ap) /* f is ignored! */
72 # define VA_SHIFT(v,t) v = va_arg(ap,t)
73 # define VA_END va_end(ap)
75 /*XX ** NO VARARGS ** XX*/
79 /*int snprintf (char *str, size_t count, const char *fmt, ...);*/
80 /*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/
82 static void dopr (char *buffer, size_t maxlen, const char *format,
84 static void fmtstr (char *buffer, size_t * currlen, size_t maxlen,
85 char *value, int flags, int min, int max);
86 static void fmtint (char *buffer, size_t * currlen, size_t maxlen,
87 long value, int base, int min, int max, int flags);
88 static void fmtfp (char *buffer, size_t * currlen, size_t maxlen,
89 long double fvalue, int min, int max, int flags);
90 static void dopr_outch (char *buffer, size_t * currlen, size_t maxlen,
94 * dopr(): poor man's version of doprintf
97 /* format read states */
98 #define DP_S_DEFAULT 0
107 /* format flags - Bits */
108 #define DP_F_MINUS (1 << 0)
109 #define DP_F_PLUS (1 << 1)
110 #define DP_F_SPACE (1 << 2)
111 #define DP_F_NUM (1 << 3)
112 #define DP_F_ZERO (1 << 4)
113 #define DP_F_UP (1 << 5)
114 #define DP_F_UNSIGNED (1 << 6)
116 /* Conversion Flags */
119 #define DP_C_LDOUBLE 3
121 #define char_to_int(p) (p - '0')
123 #define MAX(p,q) ((p >= q) ? p : q)
125 static void dopr (char *buffer, size_t maxlen, const char *format,
139 state = DP_S_DEFAULT;
140 currlen = flags = cflags = min = 0;
144 while (state != DP_S_DONE) {
145 if ((ch == '\0') || (currlen >= maxlen))
153 dopr_outch (buffer, &currlen, maxlen, ch);
184 if (isdigit ((unsigned char) ch)) {
185 min = 10 * min + char_to_int (ch);
188 else if (ch == '*') {
189 min = va_arg (args, int);
206 if (isdigit ((unsigned char) ch)) {
209 max = 10 * max + char_to_int (ch);
212 else if (ch == '*') {
213 max = va_arg (args, int);
222 /* Currently, we don't support Long Long, bummer */
233 cflags = DP_C_LDOUBLE;
245 if (cflags == DP_C_SHORT)
246 value = va_arg (args, short int);
248 else if (cflags == DP_C_LONG)
249 value = va_arg (args, long int);
252 value = va_arg (args, int);
254 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
257 flags |= DP_F_UNSIGNED;
258 if (cflags == DP_C_SHORT)
259 value = va_arg (args, unsigned short int);
261 else if (cflags == DP_C_LONG)
262 value = va_arg (args, unsigned long int);
265 value = va_arg (args, unsigned int);
267 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
270 flags |= DP_F_UNSIGNED;
271 if (cflags == DP_C_SHORT)
272 value = va_arg (args, unsigned short int);
274 else if (cflags == DP_C_LONG)
275 value = va_arg (args, unsigned long int);
278 value = va_arg (args, unsigned int);
280 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
285 flags |= DP_F_UNSIGNED;
286 if (cflags == DP_C_SHORT)
287 value = va_arg (args, unsigned short int);
289 else if (cflags == DP_C_LONG)
290 value = va_arg (args, unsigned long int);
293 value = va_arg (args, unsigned int);
295 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
298 if (cflags == DP_C_LDOUBLE)
299 fvalue = va_arg (args, long double);
302 fvalue = va_arg (args, double);
304 /* um, floating point? */
305 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
310 if (cflags == DP_C_LDOUBLE)
311 fvalue = va_arg (args, long double);
314 fvalue = va_arg (args, double);
320 if (cflags == DP_C_LDOUBLE)
321 fvalue = va_arg (args, long double);
324 fvalue = va_arg (args, double);
328 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
332 strvalue = va_arg (args, char *);
335 max = maxlen; /* ie, no max */
336 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
339 strvalue = va_arg (args, void *);
341 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max,
345 if (cflags == DP_C_SHORT) {
347 num = va_arg (args, short int *);
351 else if (cflags == DP_C_LONG) {
353 num = va_arg (args, long int *);
359 num = va_arg (args, int *);
365 dopr_outch (buffer, &currlen, maxlen, ch);
368 /* not supported yet, treat as next char */
376 state = DP_S_DEFAULT;
377 flags = cflags = min = 0;
384 break; /* some picky compilers need this */
387 if (currlen < maxlen - 1)
388 buffer[currlen] = '\0';
390 buffer[maxlen - 1] = '\0';
393 static void fmtstr (char *buffer, size_t * currlen, size_t maxlen,
394 char *value, int flags, int min, int max)
396 int padlen, strln; /* amount to pad */
403 for (strln = 0; value[strln]; ++strln); /* strlen */
404 padlen = min - strln;
407 if (flags & DP_F_MINUS)
408 padlen = -padlen; /* Left Justify */
410 while ((padlen > 0) && (cnt < max)) {
411 dopr_outch (buffer, currlen, maxlen, ' ');
415 while (*value && (cnt < max)) {
416 dopr_outch (buffer, currlen, maxlen, *value++);
419 while ((padlen < 0) && (cnt < max)) {
420 dopr_outch (buffer, currlen, maxlen, ' ');
426 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
428 static void fmtint (char *buffer, size_t * currlen, size_t maxlen,
429 long value, int base, int min, int max, int flags)
432 unsigned long uvalue;
435 int spadlen = 0; /* amount to space pad */
436 int zpadlen = 0; /* amount to zero pad */
444 if (!(flags & DP_F_UNSIGNED)) {
449 else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
451 else if (flags & DP_F_SPACE)
456 caps = 1; /* Should characters be upper case? */
459 convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
460 [uvalue % (unsigned) base];
461 uvalue = (uvalue / (unsigned) base);
462 } while (uvalue && (place < 20));
467 zpadlen = max - place;
468 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
473 if (flags & DP_F_ZERO) {
474 zpadlen = MAX (zpadlen, spadlen);
477 if (flags & DP_F_MINUS)
478 spadlen = -spadlen; /* Left Justifty */
480 #ifdef DEBUG_SNPRINTF
481 dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
482 zpadlen, spadlen, min, max, place));
486 while (spadlen > 0) {
487 dopr_outch (buffer, currlen, maxlen, ' ');
493 dopr_outch (buffer, currlen, maxlen, signvalue);
497 while (zpadlen > 0) {
498 dopr_outch (buffer, currlen, maxlen, '0');
505 dopr_outch (buffer, currlen, maxlen, convert[--place]);
507 /* Left Justified spaces */
508 while (spadlen < 0) {
509 dopr_outch (buffer, currlen, maxlen, ' ');
514 static long double abs_val (long double value)
516 long double result = value;
524 static long double pow10 (int exp)
526 long double result = 1;
536 static long round (long double value)
541 value = value - intpart;
548 static void fmtfp (char *buffer, size_t * currlen, size_t maxlen,
549 long double fvalue, int min, int max, int flags)
557 int padlen = 0; /* amount to pad */
564 * AIX manpage says the default is 0, but Solaris says the default
565 * is 6, and sprintf on AIX defaults to 6
570 ufvalue = abs_val (fvalue);
574 else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
576 else if (flags & DP_F_SPACE)
581 caps = 1; /* Should characters be upper case? */
587 * Sorry, we only support 9 digits past the decimal because of our
593 /* We "cheat" by converting the fractional part to integer by
594 * multiplying by a factor of 10
596 fracpart = round ((pow10 (max)) * (ufvalue - intpart));
598 if (fracpart >= pow10 (max)) {
600 fracpart -= pow10 (max);
603 #ifdef DEBUG_SNPRINTF
604 dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart));
607 /* Convert integer part */
610 (caps ? "0123456789ABCDEF" : "0123456789abcdef")[intpart % 10];
611 intpart = (intpart / 10);
612 } while (intpart && (iplace < 20));
615 iconvert[iplace] = 0;
617 /* Convert fractional part */
620 (caps ? "0123456789ABCDEF" : "0123456789abcdef")[fracpart % 10];
621 fracpart = (fracpart / 10);
622 } while (fracpart && (fplace < 20));
625 fconvert[fplace] = 0;
627 /* -1 for decimal point, another -1 if we are printing a sign */
628 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
629 zpadlen = max - fplace;
634 if (flags & DP_F_MINUS)
635 padlen = -padlen; /* Left Justifty */
637 if ((flags & DP_F_ZERO) && (padlen > 0)) {
639 dopr_outch (buffer, currlen, maxlen, signvalue);
644 dopr_outch (buffer, currlen, maxlen, '0');
649 dopr_outch (buffer, currlen, maxlen, ' ');
653 dopr_outch (buffer, currlen, maxlen, signvalue);
656 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
659 * Decimal point. This should probably use locale to find the correct
662 dopr_outch (buffer, currlen, maxlen, '.');
665 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
667 while (zpadlen > 0) {
668 dopr_outch (buffer, currlen, maxlen, '0');
673 dopr_outch (buffer, currlen, maxlen, ' ');
678 static void dopr_outch (char *buffer, size_t * currlen, size_t maxlen, char c)
680 if (*currlen < maxlen)
681 buffer[(*currlen)++] = c;
683 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
685 #ifndef HAVE_VSNPRINTF
686 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
689 dopr (str, count, fmt, args);
690 return (strlen (str));
692 #endif /* !HAVE_VSNPRINTF */
694 #ifndef HAVE_SNPRINTF
697 int snprintf (char *str, size_t count, const char *fmt, ...)
699 int snprintf (va_alist)
711 VA_SHIFT (str, char *);
713 VA_SHIFT (count, size_t);
714 VA_SHIFT (fmt, char *);
716 (void) vsnprintf (str, count, fmt, ap);
718 return (strlen (str));
723 #define LONG_STRING 1024
727 char buf1[LONG_STRING];
728 char buf2[LONG_STRING];
743 double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
744 0.9996, 1.996, 4.136, 0
758 long int_nums[] = { -1, 134, 91340, 341, 0203, 0 };
763 printf ("Testing snprintf format codes against system sprintf...\n");
765 for (x = 0; fp_fmt[x] != NULL; x++)
766 for (y = 0; fp_nums[y] != 0; y++) {
767 snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
768 sprintf (buf2, fp_fmt[x], fp_nums[y]);
769 if (strcmp (buf1, buf2)) {
770 printf ("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", /* __SPRINTF_CHECKED__ */
771 fp_fmt[x], buf1, buf2);
777 for (x = 0; int_fmt[x] != NULL; x++)
778 for (y = 0; int_nums[y] != 0; y++) {
779 snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
780 sprintf (buf2, int_fmt[x], int_nums[y]);
781 if (strcmp (buf1, buf2)) {
782 printf ("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", /* __SPRINTF_CHECKED__ */
783 int_fmt[x], buf1, buf2);
788 printf ("%d tests failed out of %d.\n", fail, num);
790 #endif /* SNPRINTF_TEST */
792 #endif /* !HAVE_SNPRINTF */