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
)
588 int padlen
= 0; /* amount to pad */
595 * AIX manpage says the default is 0, but Solaris says the default
596 * is 6, and sprintf on AIX defaults to 6
601 ufvalue
= abs_val (fvalue
);
606 if (flags
& DP_F_PLUS
) /* Do a sign (+/i) */
609 if (flags
& DP_F_SPACE
)
613 if (flags
& DP_F_UP
) caps
= 1; /* Should characters be upper case? */
616 intpart
= (long)ufvalue
;
619 * Sorry, we only support 9 digits past the decimal because of our
625 /* We "cheat" by converting the fractional part to integer by
626 * multiplying by a factor of 10
628 fracpart
= round ((pow10 (max
)) * (ufvalue
- intpart
));
630 if (fracpart
>= pow10 (max
))
633 fracpart
-= pow10 (max
);
636 #ifdef DEBUG_SNPRINTF
637 printf("fmtfp: %g %d.%d min=%d max=%d\n",
638 (double)fvalue
, intpart
, fracpart
, min
, max
);
641 /* Convert integer part */
644 (caps
? "0123456789ABCDEF":"0123456789abcdef")[intpart
% 10];
645 intpart
= (intpart
/ 10);
646 } while(intpart
&& (iplace
< 20));
647 if (iplace
== 20) iplace
--;
648 iconvert
[iplace
] = 0;
650 /* Convert fractional part */
653 (caps
? "0123456789ABCDEF":"0123456789abcdef")[fracpart
% 10];
654 fracpart
= (fracpart
/ 10);
655 } while(fracpart
&& (fplace
< 20));
656 if (fplace
== 20) fplace
--;
657 fconvert
[fplace
] = 0;
659 /* -1 for decimal point, another -1 if we are printing a sign */
660 padlen
= min
- iplace
- max
- 1 - ((signvalue
) ? 1 : 0);
661 zpadlen
= max
- fplace
;
666 if (flags
& DP_F_MINUS
)
667 padlen
= -padlen
; /* Left Justifty */
669 if ((flags
& DP_F_ZERO
) && (padlen
> 0))
673 dopr_outch (buffer
, currlen
, maxlen
, signvalue
);
679 dopr_outch (buffer
, currlen
, maxlen
, '0');
685 dopr_outch (buffer
, currlen
, maxlen
, ' ');
689 dopr_outch (buffer
, currlen
, maxlen
, signvalue
);
692 dopr_outch (buffer
, currlen
, maxlen
, iconvert
[--iplace
]);
695 #ifdef DEBUG_SNPRINTF
696 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace
, zpadlen
);
700 * Decimal point. This should probably use locale to find the correct
704 dopr_outch (buffer
, currlen
, maxlen
, '.');
707 dopr_outch (buffer
, currlen
, maxlen
, fconvert
[--fplace
]);
712 dopr_outch (buffer
, currlen
, maxlen
, '0');
718 dopr_outch (buffer
, currlen
, maxlen
, ' ');
723 static void dopr_outch (char *buffer
, size_t *currlen
, size_t maxlen
, char c
)
725 if (*currlen
< maxlen
)
726 buffer
[(*currlen
)++] = c
;
728 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
730 #ifndef HAVE_VSNPRINTF
731 int vsnprintf (char *str
, size_t count
, const char *fmt
, va_list args
)
734 dopr(str
, count
, fmt
, args
);
737 #endif /* !HAVE_VSNPRINTF */
739 #ifndef HAVE_SNPRINTF
742 int snprintf (char *str
,size_t count
,const char *fmt
,...)
744 int snprintf (va_alist
) va_dcl
755 VA_SHIFT (str
, char *);
756 VA_SHIFT (count
, size_t );
757 VA_SHIFT (fmt
, char *);
758 (void) vsnprintf(str
, count
, fmt
, ap
);
765 /* keep compilers happy about empty files */
766 void dummy_snprintf(void) {}
767 #endif /* !HAVE_SNPRINTF */
771 #define LONG_STRING 1024
775 char buf1
[LONG_STRING
];
776 char buf2
[LONG_STRING
];
793 double fp_nums
[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
794 0.9996, 1.996, 4.136, 0};
807 long int_nums
[] = { -1, 134, 91340, 341, 0203, 0};
812 printf ("Testing snprintf format codes against system sprintf...\n");
814 for (x
= 0; fp_fmt
[x
] != NULL
; x
++)
815 for (y
= 0; fp_nums
[y
] != 0 ; y
++)
817 snprintf (buf1
, sizeof (buf1
), fp_fmt
[x
], fp_nums
[y
]);
818 sprintf (buf2
, fp_fmt
[x
], fp_nums
[y
]);
819 if (strcmp (buf1
, buf2
))
821 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
822 fp_fmt
[x
], buf1
, buf2
);
828 for (x
= 0; int_fmt
[x
] != NULL
; x
++)
829 for (y
= 0; int_nums
[y
] != 0 ; y
++)
831 snprintf (buf1
, sizeof (buf1
), int_fmt
[x
], int_nums
[y
]);
832 sprintf (buf2
, int_fmt
[x
], int_nums
[y
]);
833 if (strcmp (buf1
, buf2
))
835 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
836 int_fmt
[x
], buf1
, buf2
);
841 printf ("%d tests failed out of %d.\n", fail
, num
);
843 #endif /* SNPRINTF_TEST */