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 * tridge@samba.org, idra@samba.org, April 2001
53 * got rid of fcvt code (twas buggy and made testing harder)
56 **************************************************************/
58 #ifndef NO_CONFIG_H /* for some tests */
72 #include <sys/types.h>
79 #ifdef HAVE_LONG_DOUBLE
80 #define LDOUBLE long double
82 #define LDOUBLE double
86 #define LLONG long long
91 static size_t dopr(char *buffer, size_t maxlen, const char *format,
93 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
94 char *value, int flags, int min, int max);
95 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
96 long value, int base, int min, int max, int flags);
97 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
98 LDOUBLE fvalue, int min, int max, int flags);
99 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
102 * dopr(): poor man's version of doprintf
105 /* format read states */
106 #define DP_S_DEFAULT 0
115 /* format flags - Bits */
116 #define DP_F_MINUS (1 << 0)
117 #define DP_F_PLUS (1 << 1)
118 #define DP_F_SPACE (1 << 2)
119 #define DP_F_NUM (1 << 3)
120 #define DP_F_ZERO (1 << 4)
121 #define DP_F_UP (1 << 5)
122 #define DP_F_UNSIGNED (1 << 6)
124 /* Conversion Flags */
127 #define DP_C_LDOUBLE 3
130 #define char_to_int(p) ((p)- '0')
132 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
135 static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
148 state = DP_S_DEFAULT;
149 currlen = flags = cflags = min = 0;
153 while (state != DP_S_DONE) {
162 dopr_outch (buffer, &currlen, maxlen, ch);
193 if (isdigit((unsigned char)ch)) {
194 min = 10*min + char_to_int (ch);
196 } else if (ch == '*') {
197 min = va_arg (args, int);
213 if (isdigit((unsigned char)ch)) {
216 max = 10*max + char_to_int (ch);
218 } else if (ch == '*') {
219 max = va_arg (args, int);
235 if (ch == 'l') { /* It's a long long */
241 cflags = DP_C_LDOUBLE;
253 if (cflags == DP_C_SHORT)
254 value = va_arg (args, int);
255 else if (cflags == DP_C_LONG)
256 value = va_arg (args, long int);
257 else if (cflags == DP_C_LLONG)
258 value = va_arg (args, LLONG);
260 value = va_arg (args, int);
261 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
264 flags |= DP_F_UNSIGNED;
265 if (cflags == DP_C_SHORT)
266 value = va_arg (args, unsigned int);
267 else if (cflags == DP_C_LONG)
268 value = (long)va_arg (args, unsigned long int);
269 else if (cflags == DP_C_LLONG)
270 value = (long)va_arg (args, unsigned LLONG);
272 value = (long)va_arg (args, unsigned int);
273 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
276 flags |= DP_F_UNSIGNED;
277 if (cflags == DP_C_SHORT)
278 value = va_arg (args, unsigned int);
279 else if (cflags == DP_C_LONG)
280 value = (long)va_arg (args, unsigned long int);
281 else if (cflags == DP_C_LLONG)
282 value = (LLONG)va_arg (args, unsigned LLONG);
284 value = (long)va_arg (args, unsigned int);
285 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
290 flags |= DP_F_UNSIGNED;
291 if (cflags == DP_C_SHORT)
292 value = va_arg (args, unsigned int);
293 else if (cflags == DP_C_LONG)
294 value = (long)va_arg (args, unsigned long int);
295 else if (cflags == DP_C_LLONG)
296 value = (LLONG)va_arg (args, unsigned LLONG);
298 value = (long)va_arg (args, unsigned int);
299 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
302 if (cflags == DP_C_LDOUBLE)
303 fvalue = va_arg (args, LDOUBLE);
305 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, LDOUBLE);
315 fvalue = va_arg (args, double);
320 if (cflags == DP_C_LDOUBLE)
321 fvalue = va_arg (args, LDOUBLE);
323 fvalue = va_arg (args, double);
326 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
329 strvalue = va_arg (args, char *);
331 max = strlen(strvalue);
333 if (min > 0 && max >= 0 && min > max) max = min;
334 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
337 strvalue = va_arg (args, void *);
338 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
341 if (cflags == DP_C_SHORT) {
343 num = va_arg (args, short int *);
345 } else if (cflags == DP_C_LONG) {
347 num = va_arg (args, long int *);
348 *num = (long int)currlen;
349 } else if (cflags == DP_C_LLONG) {
351 num = va_arg (args, LLONG *);
352 *num = (LLONG)currlen;
355 num = va_arg (args, int *);
360 dopr_outch (buffer, &currlen, maxlen, ch);
363 /* not supported yet, treat as next char */
371 state = DP_S_DEFAULT;
372 flags = cflags = min = 0;
379 break; /* some picky compilers need this */
383 if (currlen < maxlen - 1)
384 buffer[currlen] = '\0';
386 buffer[maxlen - 1] = '\0';
392 static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
393 char *value, int flags, int min, int max)
395 int padlen, strln; /* amount to pad */
398 #ifdef DEBUG_SNPRINTF
399 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
405 for (strln = 0; value[strln]; ++strln); /* strlen */
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 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
453 else if (flags & DP_F_SPACE)
458 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
462 (caps? "0123456789ABCDEF":"0123456789abcdef")
463 [uvalue % (unsigned)base ];
464 uvalue = (uvalue / (unsigned)base );
465 } while(uvalue && (place < 20));
466 if (place == 20) place--;
469 zpadlen = max - place;
470 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
471 if (zpadlen < 0) zpadlen = 0;
472 if (spadlen < 0) spadlen = 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 printf("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 LDOUBLE abs_val (LDOUBLE value)
516 LDOUBLE result = value;
524 static LDOUBLE pow10 (int exp)
536 static LLONG round (LDOUBLE value)
540 intpart = (LLONG)value;
541 value = value - intpart;
542 if (value >= 0.5) intpart++;
547 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
548 LDOUBLE fvalue, int min, int max, int flags)
556 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);
576 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
579 if (flags & DP_F_SPACE)
585 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
589 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
593 * Sorry, we only support 16 digits past the decimal because of our
599 /* We "cheat" by converting the fractional part to integer by
600 * multiplying by a factor of 10
604 modf(temp, &intpart);
606 fracpart = round ((pow10(max)) * (ufvalue - intpart));
608 if (fracpart >= pow10(max)) {
610 fracpart -= pow10(max);
614 /* Convert integer part */
617 modf (intpart*0.1, &intpart);
619 index = (int) ((temp -intpart +0.05)* 10.0);
620 /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
621 /* printf ("%llf, %f, %x\n", temp, intpart, index); */
623 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
624 } while (intpart && (iplace < 311));
625 if (iplace == 311) iplace--;
626 iconvert[iplace] = 0;
628 /* Convert fractional part */
633 modf (fracpart*0.1, &fracpart);
635 index = (int) ((temp -fracpart +0.05)* 10.0);
636 /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
637 /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
639 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
640 } while(fracpart && (fplace < 311));
641 if (fplace == 311) fplace--;
643 fconvert[fplace] = 0;
645 /* -1 for decimal point, another -1 if we are printing a sign */
646 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
647 zpadlen = max - fplace;
648 if (zpadlen < 0) zpadlen = 0;
651 if (flags & DP_F_MINUS)
652 padlen = -padlen; /* Left Justifty */
654 if ((flags & DP_F_ZERO) && (padlen > 0)) {
656 dopr_outch (buffer, currlen, maxlen, signvalue);
661 dopr_outch (buffer, currlen, maxlen, '0');
666 dopr_outch (buffer, currlen, maxlen, ' ');
670 dopr_outch (buffer, currlen, maxlen, signvalue);
673 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
675 #ifdef DEBUG_SNPRINTF
676 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
680 * Decimal point. This should probably use locale to find the correct
684 dopr_outch (buffer, currlen, maxlen, '.');
687 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
690 while (zpadlen > 0) {
691 dopr_outch (buffer, currlen, maxlen, '0');
696 dopr_outch (buffer, currlen, maxlen, ' ');
701 static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c)
703 if (*currlen < maxlen) {
704 buffer[(*currlen)] = c;
709 #ifndef HAVE_VSNPRINTF
710 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
712 return dopr(str, count, fmt, args);
716 #ifndef HAVE_SNPRINTF
717 int snprintf (char *str,size_t count,const char *fmt,...)
723 ret = vsnprintf(str, count, fmt, ap);
729 int vasprintf(char **buf, const char *fmt, va_list args)
733 ret = vsnprintf(NULL, 0, format, ap);
734 if (ret == -1) return NULL;
736 str = (char *)malloc(ret+1);
737 if (!str) return NULL;
738 ret = vsnprintf(str, ret+1, format, ap);
743 #ifndef HAVE_ASPRINTF
744 char *asprintf(const char *format, ...)
750 va_start(ap, format);
751 ret = vsnprintf(NULL, 0, format, ap);
754 if (ret == -1) return NULL;
756 va_start(ap, format);
757 str = (char *)malloc(ret+1);
758 if (!str) return NULL;
759 ret = vsnprintf(str, ret+1, format, ap);
767 #define LONG_STRING 1024
769 int sprintf(char *str,const char *fmt,...);
773 char buf1[LONG_STRING];
774 char buf2[LONG_STRING];
793 double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
794 0.9996, 1.996, 4.136, 0};
808 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
822 char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
827 printf ("Testing snprintf format codes against system sprintf...\n");
829 for (x = 0; fp_fmt[x] ; x++) {
830 for (y = 0; fp_nums[y] != 0 ; y++) {
831 int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
832 int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
833 sprintf (buf2, fp_fmt[x], fp_nums[y]);
834 if (strcmp (buf1, buf2)) {
835 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
836 fp_fmt[x], buf1, buf2);
840 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
847 for (x = 0; int_fmt[x] ; x++) {
848 for (y = 0; int_nums[y] != 0 ; y++) {
849 int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
850 int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
851 sprintf (buf2, int_fmt[x], int_nums[y]);
852 if (strcmp (buf1, buf2)) {
853 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
854 int_fmt[x], buf1, buf2);
858 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
865 for (x = 0; str_fmt[x] ; x++) {
866 for (y = 0; str_vals[y] != 0 ; y++) {
867 int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
868 int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
869 sprintf (buf2, str_fmt[x], str_vals[y]);
870 if (strcmp (buf1, buf2)) {
871 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
872 str_fmt[x], buf1, buf2);
876 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
883 printf ("%d tests failed out of %d.\n", fail, num);
885 printf("seeing how many digits we support\n");
887 double v0 = 0.12345678901234567890123456789012345678901;
888 for (x=0; x<100; x++) {
889 snprintf(buf1, sizeof(buf1), "%1.1f", v0*pow(10, x));
890 sprintf(buf2, "%1.1f", v0*pow(10, x));
891 if (strcmp(buf1, buf2)) {
892 printf("we seem to support %d digits\n", x-1);
900 #endif /* SNPRINTF_TEST */