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>
54 /* Define this as a fall through, HAVE_STDARG_H is probably already set */
56 #define HAVE_VARARGS_H
58 /* varargs declarations: */
60 #if defined(HAVE_STDARG_H)
62 # define HAVE_STDARGS /* let's hope that works everywhere (mj) */
63 # define VA_LOCAL_DECL va_list ap
64 # define VA_START(f) va_start(ap, f)
65 # define VA_SHIFT(v,t) ; /* no-op for ANSI */
66 # define VA_END va_end(ap)
68 # if defined(HAVE_VARARGS_H)
71 # define VA_LOCAL_DECL va_list ap
72 # define VA_START(f) va_start(ap) /* f is ignored! */
73 # define VA_SHIFT(v,t) v = va_arg(ap,t)
74 # define VA_END va_end(ap)
76 /*XX ** NO VARARGS ** XX*/
80 /*int snprintf (char *str, size_t count, const char *fmt, ...);*/
81 /*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/
83 static void dopr (char *buffer, size_t maxlen, const char *format,
85 static void fmtstr (char *buffer, size_t * currlen, size_t maxlen,
86 char *value, int flags, int min, int max);
87 static void fmtint (char *buffer, size_t * currlen, size_t maxlen,
88 long value, int base, int min, int max, int flags);
89 static void fmtfp (char *buffer, size_t * currlen, size_t maxlen,
90 long double fvalue, int min, int max, int flags);
91 static void dopr_outch (char *buffer, size_t * currlen, size_t maxlen,
95 * dopr(): poor man's version of doprintf
98 /* format read states */
99 #define DP_S_DEFAULT 0
108 /* format flags - Bits */
109 #define DP_F_MINUS (1 << 0)
110 #define DP_F_PLUS (1 << 1)
111 #define DP_F_SPACE (1 << 2)
112 #define DP_F_NUM (1 << 3)
113 #define DP_F_ZERO (1 << 4)
114 #define DP_F_UP (1 << 5)
115 #define DP_F_UNSIGNED (1 << 6)
117 /* Conversion Flags */
120 #define DP_C_LDOUBLE 3
122 #define char_to_int(p) (p - '0')
124 #define MAX(p,q) ((p >= q) ? p : q)
126 static void dopr (char *buffer, size_t maxlen, const char *format,
140 state = DP_S_DEFAULT;
141 currlen = flags = cflags = min = 0;
145 while (state != DP_S_DONE) {
146 if ((ch == '\0') || (currlen >= maxlen))
154 dopr_outch (buffer, &currlen, maxlen, ch);
185 if (isdigit ((unsigned char) ch)) {
186 min = 10 * min + char_to_int (ch);
189 else if (ch == '*') {
190 min = va_arg (args, int);
207 if (isdigit ((unsigned char) ch)) {
210 max = 10 * max + char_to_int (ch);
213 else if (ch == '*') {
214 max = va_arg (args, int);
223 /* Currently, we don't support Long Long, bummer */
234 cflags = DP_C_LDOUBLE;
246 if (cflags == DP_C_SHORT)
247 value = va_arg (args, short int);
249 else if (cflags == DP_C_LONG)
250 value = va_arg (args, long int);
253 value = va_arg (args, int);
255 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
258 flags |= DP_F_UNSIGNED;
259 if (cflags == DP_C_SHORT)
260 value = va_arg (args, unsigned short int);
262 else if (cflags == DP_C_LONG)
263 value = va_arg (args, unsigned long int);
266 value = va_arg (args, unsigned int);
268 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
271 flags |= DP_F_UNSIGNED;
272 if (cflags == DP_C_SHORT)
273 value = va_arg (args, unsigned short int);
275 else if (cflags == DP_C_LONG)
276 value = va_arg (args, unsigned long int);
279 value = va_arg (args, unsigned int);
281 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
286 flags |= DP_F_UNSIGNED;
287 if (cflags == DP_C_SHORT)
288 value = va_arg (args, unsigned short int);
290 else if (cflags == DP_C_LONG)
291 value = va_arg (args, unsigned long int);
294 value = va_arg (args, unsigned int);
296 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
299 if (cflags == DP_C_LDOUBLE)
300 fvalue = va_arg (args, long double);
303 fvalue = va_arg (args, double);
305 /* um, floating point? */
306 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
311 if (cflags == DP_C_LDOUBLE)
312 fvalue = va_arg (args, long double);
315 fvalue = va_arg (args, double);
321 if (cflags == DP_C_LDOUBLE)
322 fvalue = va_arg (args, long double);
325 fvalue = va_arg (args, double);
329 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
333 strvalue = va_arg (args, char *);
336 max = maxlen; /* ie, no max */
337 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
340 strvalue = va_arg (args, void *);
342 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max,
346 if (cflags == DP_C_SHORT) {
348 num = va_arg (args, short int *);
352 else if (cflags == DP_C_LONG) {
354 num = va_arg (args, long int *);
360 num = va_arg (args, int *);
366 dopr_outch (buffer, &currlen, maxlen, ch);
369 /* not supported yet, treat as next char */
377 state = DP_S_DEFAULT;
378 flags = cflags = min = 0;
385 break; /* some picky compilers need this */
388 if (currlen < maxlen - 1)
389 buffer[currlen] = '\0';
391 buffer[maxlen - 1] = '\0';
394 static void fmtstr (char *buffer, size_t * currlen, size_t maxlen,
395 char *value, int flags, int min, int max)
397 int padlen, strln; /* amount to pad */
404 for (strln = 0; value[strln]; ++strln); /* mutt_strlen */
405 padlen = min - strln;
408 if (flags & DP_F_MINUS)
409 padlen = -padlen; /* Left Justify */
411 while ((padlen > 0) && (cnt < max)) {
412 dopr_outch (buffer, currlen, maxlen, ' ');
416 while (*value && (cnt < max)) {
417 dopr_outch (buffer, currlen, maxlen, *value++);
420 while ((padlen < 0) && (cnt < max)) {
421 dopr_outch (buffer, currlen, maxlen, ' ');
427 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
429 static void fmtint (char *buffer, size_t * currlen, size_t maxlen,
430 long value, int base, int min, int max, int flags)
433 unsigned long uvalue;
436 int spadlen = 0; /* amount to space pad */
437 int zpadlen = 0; /* amount to zero pad */
445 if (!(flags & DP_F_UNSIGNED)) {
450 else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
452 else if (flags & DP_F_SPACE)
457 caps = 1; /* Should characters be upper case? */
460 convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
461 [uvalue % (unsigned) base];
462 uvalue = (uvalue / (unsigned) base);
463 } while (uvalue && (place < 20));
468 zpadlen = max - place;
469 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
474 if (flags & DP_F_ZERO) {
475 zpadlen = MAX (zpadlen, spadlen);
478 if (flags & DP_F_MINUS)
479 spadlen = -spadlen; /* Left Justifty */
481 #ifdef DEBUG_SNPRINTF
482 dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
483 zpadlen, spadlen, min, max, place));
487 while (spadlen > 0) {
488 dopr_outch (buffer, currlen, maxlen, ' ');
494 dopr_outch (buffer, currlen, maxlen, signvalue);
498 while (zpadlen > 0) {
499 dopr_outch (buffer, currlen, maxlen, '0');
506 dopr_outch (buffer, currlen, maxlen, convert[--place]);
508 /* Left Justified spaces */
509 while (spadlen < 0) {
510 dopr_outch (buffer, currlen, maxlen, ' ');
515 static long double abs_val (long double value)
517 long double result = value;
525 static long double pow10 (int exp)
527 long double result = 1;
537 static long round (long double value)
542 value = value - intpart;
549 static void fmtfp (char *buffer, size_t * currlen, size_t maxlen,
550 long double fvalue, int min, int max, int flags)
558 int padlen = 0; /* amount to pad */
565 * AIX manpage says the default is 0, but Solaris says the default
566 * is 6, and sprintf on AIX defaults to 6
571 ufvalue = abs_val (fvalue);
575 else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
577 else if (flags & DP_F_SPACE)
582 caps = 1; /* Should characters be upper case? */
588 * Sorry, we only support 9 digits past the decimal because of our
594 /* We "cheat" by converting the fractional part to integer by
595 * multiplying by a factor of 10
597 fracpart = round ((pow10 (max)) * (ufvalue - intpart));
599 if (fracpart >= pow10 (max)) {
601 fracpart -= pow10 (max);
604 #ifdef DEBUG_SNPRINTF
605 dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart));
608 /* Convert integer part */
611 (caps ? "0123456789ABCDEF" : "0123456789abcdef")[intpart % 10];
612 intpart = (intpart / 10);
613 } while (intpart && (iplace < 20));
616 iconvert[iplace] = 0;
618 /* Convert fractional part */
621 (caps ? "0123456789ABCDEF" : "0123456789abcdef")[fracpart % 10];
622 fracpart = (fracpart / 10);
623 } while (fracpart && (fplace < 20));
626 fconvert[fplace] = 0;
628 /* -1 for decimal point, another -1 if we are printing a sign */
629 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
630 zpadlen = max - fplace;
635 if (flags & DP_F_MINUS)
636 padlen = -padlen; /* Left Justifty */
638 if ((flags & DP_F_ZERO) && (padlen > 0)) {
640 dopr_outch (buffer, currlen, maxlen, signvalue);
645 dopr_outch (buffer, currlen, maxlen, '0');
650 dopr_outch (buffer, currlen, maxlen, ' ');
654 dopr_outch (buffer, currlen, maxlen, signvalue);
657 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
660 * Decimal point. This should probably use locale to find the correct
663 dopr_outch (buffer, currlen, maxlen, '.');
666 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
668 while (zpadlen > 0) {
669 dopr_outch (buffer, currlen, maxlen, '0');
674 dopr_outch (buffer, currlen, maxlen, ' ');
679 static void dopr_outch (char *buffer, size_t * currlen, size_t maxlen, char c)
681 if (*currlen < maxlen)
682 buffer[(*currlen)++] = c;
684 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
686 #ifndef HAVE_VSNPRINTF
687 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
690 dopr (str, count, fmt, args);
691 return (mutt_strlen (str));
693 #endif /* !HAVE_VSNPRINTF */
695 #ifndef HAVE_SNPRINTF
698 int snprintf (char *str, size_t count, const char *fmt, ...)
700 int snprintf (va_alist)
712 VA_SHIFT (str, char *);
714 VA_SHIFT (count, size_t);
715 VA_SHIFT (fmt, char *);
717 (void) vsnprintf (str, count, fmt, ap);
719 return (mutt_strlen (str));
724 #define LONG_STRING 1024
728 char buf1[LONG_STRING];
729 char buf2[LONG_STRING];
744 double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
745 0.9996, 1.996, 4.136, 0
759 long int_nums[] = { -1, 134, 91340, 341, 0203, 0 };
764 printf ("Testing snprintf format codes against system sprintf...\n");
766 for (x = 0; fp_fmt[x] != NULL; x++)
767 for (y = 0; fp_nums[y] != 0; y++) {
768 snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
769 sprintf (buf2, fp_fmt[x], fp_nums[y]);
770 if (mutt_strcmp (buf1, buf2)) {
771 printf ("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", /* __SPRINTF_CHECKED__ */
772 fp_fmt[x], buf1, buf2);
778 for (x = 0; int_fmt[x] != NULL; x++)
779 for (y = 0; int_nums[y] != 0; y++) {
780 snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
781 sprintf (buf2, int_fmt[x], int_nums[y]);
782 if (mutt_strcmp (buf1, buf2)) {
783 printf ("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", /* __SPRINTF_CHECKED__ */
784 int_fmt[x], buf1, buf2);
789 printf ("%d tests failed out of %d.\n", fail, num);
791 #endif /* SNPRINTF_TEST */
793 #endif /* !HAVE_SNPRINTF */