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 **************************************************************/
58 #include <sys/types.h>
60 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
62 /* Define this as a fall through, HAVE_STDARG_H is probably already set */
64 #define HAVE_VARARGS_H
66 /* varargs declarations: */
68 #if defined(HAVE_STDARG_H)
70 # define HAVE_STDARGS /* let's hope that works everywhere (mj) */
71 # define VA_LOCAL_DECL va_list ap
72 # define VA_START(f) va_start(ap, f)
73 # define VA_SHIFT(v,t) ; /* no-op for ANSI */
74 # define VA_END va_end(ap)
76 # if defined(HAVE_VARARGS_H)
79 # define VA_LOCAL_DECL va_list ap
80 # define VA_START(f) va_start(ap) /* f is ignored! */
81 # define VA_SHIFT(v,t) v = va_arg(ap,t)
82 # define VA_END va_end(ap)
84 /*XX ** NO VARARGS ** XX*/
88 #ifdef HAVE_LONG_DOUBLE
89 #define LDOUBLE long double
91 #define LDOUBLE double
95 #define LLONG long long
100 /*int snprintf (char *str, size_t count, const char *fmt, ...);*/
101 /*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/
103 static void dopr (char *buffer
, size_t maxlen
, const char *format
,
105 static void fmtstr (char *buffer
, size_t *currlen
, size_t maxlen
,
106 char *value
, int flags
, int min
, int max
);
107 static void fmtint (char *buffer
, size_t *currlen
, size_t maxlen
,
108 long value
, int base
, int min
, int max
, int flags
);
109 static void fmtfp (char *buffer
, size_t *currlen
, size_t maxlen
,
110 LDOUBLE fvalue
, int min
, int max
, int flags
);
111 static void dopr_outch (char *buffer
, size_t *currlen
, size_t maxlen
, char c
);
114 * dopr(): poor man's version of doprintf
117 /* format read states */
118 #define DP_S_DEFAULT 0
127 /* format flags - Bits */
128 #define DP_F_MINUS (1 << 0)
129 #define DP_F_PLUS (1 << 1)
130 #define DP_F_SPACE (1 << 2)
131 #define DP_F_NUM (1 << 3)
132 #define DP_F_ZERO (1 << 4)
133 #define DP_F_UP (1 << 5)
134 #define DP_F_UNSIGNED (1 << 6)
136 /* Conversion Flags */
139 #define DP_C_LDOUBLE 3
142 #define char_to_int(p) (p - '0')
143 #define MAX(p,q) ((p >= q) ? p : q)
145 static void dopr (char *buffer
, size_t maxlen
, const char *format
, va_list args
)
158 state
= DP_S_DEFAULT
;
159 currlen
= flags
= cflags
= min
= 0;
163 while (state
!= DP_S_DONE
)
165 if ((ch
== '\0') || (currlen
>= maxlen
))
174 dopr_outch (buffer
, &currlen
, maxlen
, ch
);
206 if (isdigit((unsigned char)ch
))
208 min
= 10*min
+ char_to_int (ch
);
213 min
= va_arg (args
, int);
230 if (isdigit((unsigned char)ch
))
234 max
= 10*max
+ char_to_int (ch
);
239 max
= va_arg (args
, int);
256 if (ch
== 'l') { /* It's a long long */
262 cflags
= DP_C_LDOUBLE
;
275 if (cflags
== DP_C_SHORT
)
276 value
= va_arg (args
, short int);
277 else if (cflags
== DP_C_LONG
)
278 value
= va_arg (args
, long int);
279 else if (cflags
== DP_C_LLONG
)
280 value
= va_arg (args
, LLONG
);
282 value
= va_arg (args
, int);
283 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);
289 else if (cflags
== DP_C_LONG
)
290 value
= (long)va_arg (args
, unsigned long int);
291 else if (cflags
== DP_C_LLONG
)
292 value
= (long)va_arg (args
, unsigned LLONG
);
294 value
= (long)va_arg (args
, unsigned int);
295 fmtint (buffer
, &currlen
, maxlen
, value
, 8, min
, max
, flags
);
298 flags
|= DP_F_UNSIGNED
;
299 if (cflags
== DP_C_SHORT
)
300 value
= va_arg (args
, unsigned short int);
301 else if (cflags
== DP_C_LONG
)
302 value
= (long)va_arg (args
, unsigned long int);
303 else if (cflags
== DP_C_LLONG
)
304 value
= (LLONG
)va_arg (args
, unsigned LLONG
);
306 value
= (long)va_arg (args
, unsigned int);
307 fmtint (buffer
, &currlen
, maxlen
, value
, 10, min
, max
, flags
);
312 flags
|= DP_F_UNSIGNED
;
313 if (cflags
== DP_C_SHORT
)
314 value
= va_arg (args
, unsigned short int);
315 else if (cflags
== DP_C_LONG
)
316 value
= (long)va_arg (args
, unsigned long int);
317 else if (cflags
== DP_C_LLONG
)
318 value
= (LLONG
)va_arg (args
, unsigned LLONG
);
320 value
= (long)va_arg (args
, unsigned int);
321 fmtint (buffer
, &currlen
, maxlen
, value
, 16, min
, max
, flags
);
324 if (cflags
== DP_C_LDOUBLE
)
325 fvalue
= va_arg (args
, LDOUBLE
);
327 fvalue
= va_arg (args
, double);
328 /* um, floating point? */
329 fmtfp (buffer
, &currlen
, maxlen
, fvalue
, min
, max
, flags
);
334 if (cflags
== DP_C_LDOUBLE
)
335 fvalue
= va_arg (args
, LDOUBLE
);
337 fvalue
= va_arg (args
, double);
342 if (cflags
== DP_C_LDOUBLE
)
343 fvalue
= va_arg (args
, LDOUBLE
);
345 fvalue
= va_arg (args
, double);
348 dopr_outch (buffer
, &currlen
, maxlen
, va_arg (args
, int));
351 strvalue
= va_arg (args
, char *);
353 max
= maxlen
; /* ie, no max */
354 fmtstr (buffer
, &currlen
, maxlen
, strvalue
, flags
, min
, max
);
357 strvalue
= va_arg (args
, void *);
358 fmtint (buffer
, &currlen
, maxlen
, (long) strvalue
, 16, min
, max
, flags
);
361 if (cflags
== DP_C_SHORT
)
364 num
= va_arg (args
, short int *);
367 else if (cflags
== DP_C_LONG
)
370 num
= va_arg (args
, long int *);
371 *num
= (long int)currlen
;
373 else if (cflags
== DP_C_LLONG
)
376 num
= va_arg (args
, LLONG
*);
377 *num
= (LLONG
)currlen
;
382 num
= va_arg (args
, int *);
387 dopr_outch (buffer
, &currlen
, maxlen
, ch
);
390 /* not supported yet, treat as next char */
398 state
= DP_S_DEFAULT
;
399 flags
= cflags
= min
= 0;
406 break; /* some picky compilers need this */
409 if (currlen
< maxlen
- 1)
410 buffer
[currlen
] = '\0';
412 buffer
[maxlen
- 1] = '\0';
415 static void fmtstr (char *buffer
, size_t *currlen
, size_t maxlen
,
416 char *value
, int flags
, int min
, int max
)
418 int padlen
, strln
; /* amount to pad */
426 for (strln
= 0; value
[strln
]; ++strln
); /* strlen */
427 padlen
= min
- strln
;
430 if (flags
& DP_F_MINUS
)
431 padlen
= -padlen
; /* Left Justify */
433 while ((padlen
> 0) && (cnt
< max
))
435 dopr_outch (buffer
, currlen
, maxlen
, ' ');
439 while (*value
&& (cnt
< max
))
441 dopr_outch (buffer
, currlen
, maxlen
, *value
++);
444 while ((padlen
< 0) && (cnt
< max
))
446 dopr_outch (buffer
, currlen
, maxlen
, ' ');
452 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
454 static void fmtint (char *buffer
, size_t *currlen
, size_t maxlen
,
455 long value
, int base
, int min
, int max
, int flags
)
458 unsigned long uvalue
;
461 int spadlen
= 0; /* amount to space pad */
462 int zpadlen
= 0; /* amount to zero pad */
470 if(!(flags
& DP_F_UNSIGNED
))
477 if (flags
& DP_F_PLUS
) /* Do a sign (+/i) */
480 if (flags
& DP_F_SPACE
)
484 if (flags
& DP_F_UP
) caps
= 1; /* Should characters be upper case? */
488 (caps
? "0123456789ABCDEF":"0123456789abcdef")
489 [uvalue
% (unsigned)base
];
490 uvalue
= (uvalue
/ (unsigned)base
);
491 } while(uvalue
&& (place
< 20));
492 if (place
== 20) place
--;
495 zpadlen
= max
- place
;
496 spadlen
= min
- MAX (max
, place
) - (signvalue
? 1 : 0);
497 if (zpadlen
< 0) zpadlen
= 0;
498 if (spadlen
< 0) spadlen
= 0;
499 if (flags
& DP_F_ZERO
)
501 zpadlen
= MAX(zpadlen
, spadlen
);
504 if (flags
& DP_F_MINUS
)
505 spadlen
= -spadlen
; /* Left Justifty */
507 #ifdef DEBUG_SNPRINTF
508 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
509 zpadlen
, spadlen
, min
, max
, place
);
515 dopr_outch (buffer
, currlen
, maxlen
, ' ');
521 dopr_outch (buffer
, currlen
, maxlen
, signvalue
);
528 dopr_outch (buffer
, currlen
, maxlen
, '0');
535 dopr_outch (buffer
, currlen
, maxlen
, convert
[--place
]);
537 /* Left Justified spaces */
538 while (spadlen
< 0) {
539 dopr_outch (buffer
, currlen
, maxlen
, ' ');
544 static LDOUBLE
abs_val (LDOUBLE value
)
546 LDOUBLE result
= value
;
554 static LDOUBLE
pow10 (int exp
)
567 static long round (LDOUBLE value
)
571 intpart
= (long)value
;
572 value
= value
- intpart
;
579 static void fmtfp (char *buffer
, size_t *currlen
, size_t maxlen
,
580 LDOUBLE fvalue
, int min
, int max
, int flags
)
594 extern char *fcvtl(long double value
, int ndigit
, int *decpt
, int *sign
);
596 extern char *fcvt(double value
, int ndigit
, int *decpt
, int *sign
);
601 int padlen
= 0; /* amount to pad */
608 * AIX manpage says the default is 0, but Solaris says the default
609 * is 6, and sprintf on AIX defaults to 6
614 ufvalue
= abs_val (fvalue
);
619 if (flags
& DP_F_PLUS
) /* Do a sign (+/i) */
622 if (flags
& DP_F_SPACE
)
626 if (flags
& DP_F_UP
) caps
= 1; /* Should characters be upper case? */
630 intpart
= (long)ufvalue
;
633 * Sorry, we only support 9 digits past the decimal because of our
639 /* We "cheat" by converting the fractional part to integer by
640 * multiplying by a factor of 10
642 fracpart
= round ((pow10 (max
)) * (ufvalue
- intpart
));
644 if (fracpart
>= pow10 (max
))
647 fracpart
-= pow10 (max
);
650 #ifdef DEBUG_SNPRINTF
651 printf("fmtfp: %g %d.%d min=%d max=%d\n",
652 (double)fvalue
, intpart
, fracpart
, min
, max
);
655 /* Convert integer part */
658 (caps
? "0123456789ABCDEF":"0123456789abcdef")[intpart
% 10];
659 intpart
= (intpart
/ 10);
660 } while(intpart
&& (iplace
< 20));
661 if (iplace
== 20) iplace
--;
662 iconvert
[iplace
] = 0;
664 /* Convert fractional part */
667 (caps
? "0123456789ABCDEF":"0123456789abcdef")[fracpart
% 10];
668 fracpart
= (fracpart
/ 10);
669 } while(fracpart
&& (fplace
< 20));
670 if (fplace
== 20) fplace
--;
671 fconvert
[fplace
] = 0;
672 #else /* use fcvt() */
676 result
= fcvtl(ufvalue
, max
, &dec_pt
, &sig
);
678 result
= fcvt(ufvalue
, max
, &dec_pt
, &sig
);
681 r_length
= strlen(result
);
684 * Fix broken fcvt implementation returns..
694 if ( r_length
< dec_pt
)
705 fconvert
[fplace
++] = result
[--r_length
];
707 while ((dec_pt
< 0) && (fplace
< max
)) {
708 fconvert
[fplace
++] = '0';
715 for(c
=dec_pt
; c
; iconvert
[iplace
++] = result
[--c
])
717 iconvert
[iplace
] = '\0';
722 for(c
=(r_length
-dec_pt
); c
; fconvert
[fplace
++] = result
[--c
])
727 /* -1 for decimal point, another -1 if we are printing a sign */
728 padlen
= min
- iplace
- max
- 1 - ((signvalue
) ? 1 : 0);
729 zpadlen
= max
- fplace
;
734 if (flags
& DP_F_MINUS
)
735 padlen
= -padlen
; /* Left Justifty */
737 if ((flags
& DP_F_ZERO
) && (padlen
> 0))
741 dopr_outch (buffer
, currlen
, maxlen
, signvalue
);
747 dopr_outch (buffer
, currlen
, maxlen
, '0');
753 dopr_outch (buffer
, currlen
, maxlen
, ' ');
757 dopr_outch (buffer
, currlen
, maxlen
, signvalue
);
760 dopr_outch (buffer
, currlen
, maxlen
, iconvert
[--iplace
]);
763 #ifdef DEBUG_SNPRINTF
764 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace
, zpadlen
);
768 * Decimal point. This should probably use locale to find the correct
772 dopr_outch (buffer
, currlen
, maxlen
, '.');
775 dopr_outch (buffer
, currlen
, maxlen
, fconvert
[--fplace
]);
780 dopr_outch (buffer
, currlen
, maxlen
, '0');
786 dopr_outch (buffer
, currlen
, maxlen
, ' ');
791 static void dopr_outch (char *buffer
, size_t *currlen
, size_t maxlen
, char c
)
793 if (*currlen
< maxlen
)
794 buffer
[(*currlen
)++] = c
;
796 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
798 #ifndef HAVE_VSNPRINTF
799 int vsnprintf (char *str
, size_t count
, const char *fmt
, va_list args
)
802 dopr(str
, count
, fmt
, args
);
805 #endif /* !HAVE_VSNPRINTF */
807 #ifndef HAVE_SNPRINTF
810 int snprintf (char *str
,size_t count
,const char *fmt
,...)
812 int snprintf (va_alist
) va_dcl
823 VA_SHIFT (str
, char *);
824 VA_SHIFT (count
, size_t );
825 VA_SHIFT (fmt
, char *);
826 (void) vsnprintf(str
, count
, fmt
, ap
);
833 /* keep compilers happy about empty files */
834 void dummy_snprintf(void) {}
835 #endif /* !HAVE_SNPRINTF */
839 #define LONG_STRING 1024
843 char buf1
[LONG_STRING
];
844 char buf2
[LONG_STRING
];
861 double fp_nums
[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
862 0.9996, 1.996, 4.136, 6442452944.1234, 0};
875 long int_nums
[] = { -1, 134, 91340, 341, 0203, 0};
880 printf ("Testing snprintf format codes against system sprintf...\n");
882 for (x
= 0; fp_fmt
[x
] != NULL
; x
++)
883 for (y
= 0; fp_nums
[y
] != 0 ; y
++)
885 snprintf (buf1
, sizeof (buf1
), fp_fmt
[x
], fp_nums
[y
]);
886 sprintf (buf2
, fp_fmt
[x
], fp_nums
[y
]);
887 if (strcmp (buf1
, buf2
))
889 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
890 fp_fmt
[x
], buf1
, buf2
);
896 for (x
= 0; int_fmt
[x
] != NULL
; x
++)
897 for (y
= 0; int_nums
[y
] != 0 ; y
++)
899 snprintf (buf1
, sizeof (buf1
), int_fmt
[x
], int_nums
[y
]);
900 sprintf (buf2
, int_fmt
[x
], int_nums
[y
]);
901 if (strcmp (buf1
, buf2
))
903 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
904 int_fmt
[x
], buf1
, buf2
);
909 printf ("%d tests failed out of %d.\n", fail
, num
);
911 #endif /* SNPRINTF_TEST */