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 **************************************************************/
60 #ifdef HAVE_LONG_DOUBLE
61 #define LDOUBLE long double
63 #define LDOUBLE double
67 #define LLONG long long
73 * dopr(): poor man's version of doprintf
76 /* format read states */
77 #define DP_S_DEFAULT 0
86 /* format flags - Bits */
87 #define DP_F_MINUS (1 << 0)
88 #define DP_F_PLUS (1 << 1)
89 #define DP_F_SPACE (1 << 2)
90 #define DP_F_NUM (1 << 3)
91 #define DP_F_ZERO (1 << 4)
92 #define DP_F_UP (1 << 5)
93 #define DP_F_UNSIGNED (1 << 6)
94 #define DP_F_HEXPREFIX (1 << 7)
96 /* Conversion Flags */
99 #define DP_C_LDOUBLE 3
102 #define char_to_int(p) ((p)- '0')
104 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
107 static size_t dopr(char *buffer, size_t maxlen, const char *format,
109 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
110 char *value, int flags, int min, int max);
111 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
112 long value, int base, int min, int max, int flags);
113 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
114 LDOUBLE fvalue, int min, int max, int flags);
115 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
117 static size_t dopr(char *buffer, size_t maxlen, const char *format,
132 silc_va_copy(args, args_in);
134 state = DP_S_DEFAULT;
135 currlen = flags = cflags = min = 0;
139 while (state != DP_S_DONE) {
148 dopr_outch (buffer, &currlen, maxlen, ch);
179 if (isdigit((unsigned char)ch)) {
180 min = 10*min + char_to_int (ch);
182 } else if (ch == '*') {
183 min = va_arg (args, int);
199 if (isdigit((unsigned char)ch)) {
202 max = 10*max + char_to_int (ch);
204 } else if (ch == '*') {
205 max = va_arg (args, int);
221 if (ch == 'l') { /* It's a long long */
227 cflags = DP_C_LDOUBLE;
239 if (cflags == DP_C_SHORT)
240 value = va_arg (args, int);
241 else if (cflags == DP_C_LONG)
242 value = va_arg (args, long int);
243 else if (cflags == DP_C_LLONG)
244 value = va_arg (args, LLONG);
246 value = va_arg (args, int);
247 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
250 flags |= DP_F_UNSIGNED;
251 if (cflags == DP_C_SHORT)
252 value = va_arg (args, unsigned int);
253 else if (cflags == DP_C_LONG)
254 value = (long)va_arg (args, unsigned long int);
255 else if (cflags == DP_C_LLONG)
256 value = (long)va_arg (args, unsigned LLONG);
258 value = (long)va_arg (args, unsigned int);
259 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
262 flags |= DP_F_UNSIGNED;
263 if (cflags == DP_C_SHORT)
264 value = va_arg (args, unsigned int);
265 else if (cflags == DP_C_LONG)
266 value = (long)va_arg (args, unsigned long int);
267 else if (cflags == DP_C_LLONG)
268 value = (LLONG)va_arg (args, unsigned LLONG);
270 value = (long)va_arg (args, unsigned int);
271 fmtint (buffer, &currlen, maxlen, value, 10, 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, 16, min, max, flags);
288 if (cflags == DP_C_LDOUBLE)
289 fvalue = va_arg (args, LDOUBLE);
291 fvalue = va_arg (args, double);
292 /* um, floating point? */
293 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
298 if (cflags == DP_C_LDOUBLE)
299 fvalue = va_arg (args, LDOUBLE);
301 fvalue = va_arg (args, double);
302 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
307 if (cflags == DP_C_LDOUBLE)
308 fvalue = va_arg (args, LDOUBLE);
310 fvalue = va_arg (args, double);
311 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
314 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
317 strvalue = va_arg (args, char *);
318 if (!strvalue) strvalue = "(NULL)";
320 max = strlen(strvalue);
322 if (min > 0 && max >= 0 && min > max) max = min;
323 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
326 flags |= (DP_F_UNSIGNED | DP_F_HEXPREFIX);
327 strvalue = va_arg (args, void *);
328 fmtint (buffer, &currlen, maxlen, (long )strvalue, 16, min, max,
332 if (cflags == DP_C_SHORT) {
334 num = va_arg (args, short int *);
336 } else if (cflags == DP_C_LONG) {
338 num = va_arg (args, long int *);
339 *num = (long int)currlen;
340 } else if (cflags == DP_C_LLONG) {
342 num = va_arg (args, LLONG *);
343 *num = (LLONG)currlen;
346 num = va_arg (args, int *);
351 dopr_outch (buffer, &currlen, maxlen, ch);
354 /* not supported yet, treat as next char */
362 state = DP_S_DEFAULT;
363 flags = cflags = min = 0;
370 break; /* some picky compilers need this */
374 if (currlen < maxlen - 1)
375 buffer[currlen] = '\0';
377 buffer[maxlen - 1] = '\0';
383 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
384 char *value, int flags, int min, int max)
386 int padlen, strln; /* amount to pad */
393 for (strln = 0; value[strln]; ++strln); /* strlen */
394 padlen = min - strln;
397 if (flags & DP_F_MINUS)
398 padlen = -padlen; /* Left Justify */
400 while ((padlen > 0) && (cnt < max)) {
401 dopr_outch (buffer, currlen, maxlen, ' ');
405 while (*value && (cnt < max)) {
406 dopr_outch (buffer, currlen, maxlen, *value++);
409 while ((padlen < 0) && (cnt < max)) {
410 dopr_outch (buffer, currlen, maxlen, ' ');
416 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
418 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
419 long value, int base, int min, int max, int flags)
422 unsigned long uvalue;
425 int spadlen = 0; /* amount to space pad */
426 int zpadlen = 0; /* amount to zero pad */
434 if(!(flags & DP_F_UNSIGNED)) {
439 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
441 else if (flags & DP_F_SPACE)
446 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
450 (caps? "0123456789ABCDEF":"0123456789abcdef")
451 [uvalue % (unsigned)base ];
452 uvalue = (uvalue / (unsigned)base );
453 } while(uvalue && (place < 20));
454 if (place == 20) place--;
457 zpadlen = max - place;
458 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
459 if (zpadlen < 0) zpadlen = 0;
460 if (spadlen < 0) spadlen = 0;
461 if (flags & DP_F_ZERO) {
462 zpadlen = MAX(zpadlen, spadlen);
465 if (flags & DP_F_MINUS)
466 spadlen = -spadlen; /* Left Justifty */
469 while (spadlen > 0) {
470 dopr_outch (buffer, currlen, maxlen, ' ');
475 if (flags & DP_F_HEXPREFIX) {
476 dopr_outch (buffer, currlen, maxlen, '0');
477 dopr_outch (buffer, currlen, maxlen, 'x');
482 dopr_outch (buffer, currlen, maxlen, signvalue);
486 while (zpadlen > 0) {
487 dopr_outch (buffer, currlen, maxlen, '0');
494 dopr_outch (buffer, currlen, maxlen, convert[--place]);
496 /* Left Justified spaces */
497 while (spadlen < 0) {
498 dopr_outch (buffer, currlen, maxlen, ' ');
503 static LDOUBLE abs_val(LDOUBLE value)
505 LDOUBLE result = value;
513 static LDOUBLE POW10(int exp)
525 static LLONG ROUND(LDOUBLE value)
529 intpart = (LLONG)value;
530 value = value - intpart;
531 if (value >= 0.5) intpart++;
536 /* a replacement for modf that doesn't need the math library. Should
537 be portable, but slow */
538 static double my_modf(double x0, double *iptr)
545 for (i=0;i<100;i++) {
547 if (l <= (x+1) && l >= (x-1)) break;
553 /* yikes! the number is beyond what we can handle.
563 ret = my_modf(x0-l*f, &i2);
573 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
574 LDOUBLE fvalue, int min, int max, int flags)
582 int padlen = 0; /* amount to pad */
591 * AIX manpage says the default is 0, but Solaris says the default
592 * is 6, and sprintf on AIX defaults to 6
597 ufvalue = abs_val (fvalue);
602 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
605 if (flags & DP_F_SPACE)
611 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
615 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
619 * Sorry, we only support 16 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
630 my_modf(temp, &intpart);
632 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
634 if (fracpart >= POW10(max)) {
636 fracpart -= POW10(max);
640 /* Convert integer part */
643 my_modf(temp, &intpart);
644 idx = (int) ((temp -intpart +0.05)* 10.0);
645 /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
646 /* printf ("%llf, %f, %x\n", temp, intpart, idx); */
648 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
649 } while (intpart && (iplace < 311));
650 if (iplace == 311) iplace--;
651 iconvert[iplace] = 0;
653 /* Convert fractional part */
658 my_modf(temp, &fracpart);
659 idx = (int) ((temp -fracpart +0.05)* 10.0);
660 /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
661 /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
663 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
664 } while(fracpart && (fplace < 311));
665 if (fplace == 311) fplace--;
667 fconvert[fplace] = 0;
669 /* -1 for decimal point, another -1 if we are printing a sign */
670 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
671 zpadlen = max - fplace;
672 if (zpadlen < 0) zpadlen = 0;
675 if (flags & DP_F_MINUS)
676 padlen = -padlen; /* Left Justifty */
678 if ((flags & DP_F_ZERO) && (padlen > 0)) {
680 dopr_outch (buffer, currlen, maxlen, signvalue);
685 dopr_outch (buffer, currlen, maxlen, '0');
690 dopr_outch (buffer, currlen, maxlen, ' ');
694 dopr_outch (buffer, currlen, maxlen, signvalue);
697 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
700 * Decimal point. This should probably use locale to find the correct
704 dopr_outch (buffer, currlen, maxlen, '.');
706 while (zpadlen > 0) {
707 dopr_outch (buffer, currlen, maxlen, '0');
712 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
716 dopr_outch (buffer, currlen, maxlen, ' ');
721 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
723 if (*currlen < maxlen) {
724 buffer[(*currlen)] = c;
729 int silc_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
733 return dopr(str, count, fmt, args);
736 int silc_snprintf(char *str, size_t count, const char *fmt, ...)
742 ret = silc_vsnprintf(str, count, fmt, ap);
747 int silc_vasprintf(char **ptr, const char *format, va_list ap)
752 silc_va_copy(ap2, ap);
754 ret = silc_vsnprintf(NULL, 0, format, ap2);
755 if (ret <= 0) return ret;
757 (*ptr) = (char *)silc_malloc(ret+1);
758 if (!*ptr) return -1;
760 silc_va_copy(ap2, ap);
762 ret = silc_vsnprintf(*ptr, ret+1, format, ap2);
767 int silc_asprintf(char **ptr, const char *format, ...)
773 va_start(ap, format);
774 ret = silc_vasprintf(ptr, format, ap);