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 #include "silcruntime.h"
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,
131 SilcSnprintfRender render;
133 silc_va_copy(args, args_in);
135 state = DP_S_DEFAULT;
136 currlen = flags = cflags = min = 0;
140 while (state != DP_S_DONE) {
149 dopr_outch (buffer, &currlen, maxlen, ch);
180 if (isdigit((unsigned char)ch)) {
181 min = 10*min + char_to_int (ch);
183 } else if (ch == '*') {
184 min = va_arg (args, int);
200 if (isdigit((unsigned char)ch)) {
203 max = 10*max + char_to_int (ch);
205 } else if (ch == '*') {
206 max = va_arg (args, int);
222 if (ch == 'l') { /* It's a long long */
228 cflags = DP_C_LDOUBLE;
240 if (cflags == DP_C_SHORT)
241 value = va_arg (args, int);
242 else if (cflags == DP_C_LONG)
243 value = va_arg (args, long int);
244 else if (cflags == DP_C_LLONG)
245 value = va_arg (args, LLONG);
247 value = va_arg (args, int);
248 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
251 flags |= DP_F_UNSIGNED;
252 if (cflags == DP_C_SHORT)
253 value = va_arg (args, unsigned int);
254 else if (cflags == DP_C_LONG)
255 value = (long)va_arg (args, unsigned long int);
256 else if (cflags == DP_C_LLONG)
257 value = (long)va_arg (args, unsigned LLONG);
259 value = (long)va_arg (args, unsigned int);
260 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
263 flags |= DP_F_UNSIGNED;
264 if (cflags == DP_C_SHORT)
265 value = va_arg (args, unsigned int);
266 else if (cflags == DP_C_LONG)
267 value = (long)va_arg (args, unsigned long int);
268 else if (cflags == DP_C_LLONG)
269 value = (LLONG)va_arg (args, unsigned LLONG);
271 value = (long)va_arg (args, unsigned int);
272 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
277 flags |= DP_F_UNSIGNED;
278 if (cflags == DP_C_SHORT)
279 value = va_arg (args, unsigned int);
280 else if (cflags == DP_C_LONG)
281 value = (long)va_arg (args, unsigned long int);
282 else if (cflags == DP_C_LLONG)
283 value = (LLONG)va_arg (args, unsigned LLONG);
285 value = (long)va_arg (args, unsigned int);
286 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
289 if (cflags == DP_C_LDOUBLE)
290 fvalue = va_arg (args, LDOUBLE);
292 fvalue = va_arg (args, double);
293 /* um, floating point? */
294 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
299 if (cflags == DP_C_LDOUBLE)
300 fvalue = va_arg (args, LDOUBLE);
302 fvalue = va_arg (args, double);
303 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
308 if (cflags == DP_C_LDOUBLE)
309 fvalue = va_arg (args, LDOUBLE);
311 fvalue = va_arg (args, double);
312 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
315 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
318 strvalue = va_arg (args, char *);
319 if (!strvalue) strvalue = "(NULL)";
321 max = strlen(strvalue);
323 if (min > 0 && max >= 0 && min > max) max = min;
324 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
327 flags |= (DP_F_UNSIGNED | DP_F_HEXPREFIX);
328 strvalue = va_arg (args, void *);
329 fmtint (buffer, &currlen, maxlen, (long )strvalue, 16, min, max,
333 if (cflags == DP_C_SHORT) {
335 num = va_arg (args, short int *);
337 } else if (cflags == DP_C_LONG) {
339 num = va_arg (args, long int *);
340 *num = (long int)currlen;
341 } else if (cflags == DP_C_LLONG) {
343 num = va_arg (args, LLONG *);
344 *num = (LLONG)currlen;
347 num = va_arg (args, int *);
352 dopr_outch (buffer, &currlen, maxlen, ch);
355 /* not supported yet, treat as next char */
359 /* Renderer function */
360 render = va_arg (args, SilcSnprintfRender);
362 void *ptr = va_arg (args, void *);
364 strvalue = render (ptr);
367 max = strlen(strvalue);
368 if (min > 0 && max >= 0 && min > max) max = min;
369 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
380 state = DP_S_DEFAULT;
381 flags = cflags = min = 0;
388 break; /* some picky compilers need this */
392 if (currlen < maxlen - 1)
393 buffer[currlen] = '\0';
395 buffer[maxlen - 1] = '\0';
401 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
402 char *value, int flags, int min, int max)
404 int padlen, strln; /* amount to pad */
411 for (strln = 0; value[strln]; ++strln); /* strlen */
412 padlen = min - strln;
415 if (flags & DP_F_MINUS)
416 padlen = -padlen; /* Left Justify */
418 while ((padlen > 0) && (cnt < max)) {
419 dopr_outch (buffer, currlen, maxlen, ' ');
423 while (*value && (cnt < max)) {
424 dopr_outch (buffer, currlen, maxlen, *value++);
427 while ((padlen < 0) && (cnt < max)) {
428 dopr_outch (buffer, currlen, maxlen, ' ');
434 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
436 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
437 long value, int base, int min, int max, int flags)
440 unsigned long uvalue;
443 int spadlen = 0; /* amount to space pad */
444 int zpadlen = 0; /* amount to zero pad */
452 if(!(flags & DP_F_UNSIGNED)) {
457 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
459 else if (flags & DP_F_SPACE)
464 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
468 (caps? "0123456789ABCDEF":"0123456789abcdef")
469 [uvalue % (unsigned)base ];
470 uvalue = (uvalue / (unsigned)base );
471 } while(uvalue && (place < 20));
472 if (place == 20) place--;
475 zpadlen = max - place;
476 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
477 if (zpadlen < 0) zpadlen = 0;
478 if (spadlen < 0) spadlen = 0;
479 if (flags & DP_F_ZERO) {
480 zpadlen = MAX(zpadlen, spadlen);
483 if (flags & DP_F_MINUS)
484 spadlen = -spadlen; /* Left Justifty */
487 while (spadlen > 0) {
488 dopr_outch (buffer, currlen, maxlen, ' ');
493 if (flags & DP_F_HEXPREFIX) {
494 dopr_outch (buffer, currlen, maxlen, '0');
495 dopr_outch (buffer, currlen, maxlen, 'x');
500 dopr_outch (buffer, currlen, maxlen, signvalue);
504 while (zpadlen > 0) {
505 dopr_outch (buffer, currlen, maxlen, '0');
512 dopr_outch (buffer, currlen, maxlen, convert[--place]);
514 /* Left Justified spaces */
515 while (spadlen < 0) {
516 dopr_outch (buffer, currlen, maxlen, ' ');
521 static LDOUBLE abs_val(LDOUBLE value)
523 LDOUBLE result = value;
531 static LDOUBLE POW10(int exp)
543 static LLONG ROUND(LDOUBLE value)
547 intpart = (LLONG)value;
548 value = value - intpart;
549 if (value >= 0.5) intpart++;
554 /* a replacement for modf that doesn't need the math library. Should
555 be portable, but slow */
556 static double my_modf(double x0, double *iptr)
563 for (i=0;i<100;i++) {
565 if (l <= (x+1) && l >= (x-1)) break;
571 /* yikes! the number is beyond what we can handle.
581 ret = my_modf(x0-l*f, &i2);
591 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
592 LDOUBLE fvalue, int min, int max, int flags)
600 int padlen = 0; /* amount to pad */
609 * AIX manpage says the default is 0, but Solaris says the default
610 * is 6, and sprintf on AIX defaults to 6
615 ufvalue = abs_val (fvalue);
620 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
623 if (flags & DP_F_SPACE)
629 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
633 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
637 * Sorry, we only support 16 digits past the decimal because of our
643 /* We "cheat" by converting the fractional part to integer by
644 * multiplying by a factor of 10
648 my_modf(temp, &intpart);
650 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
652 if (fracpart >= POW10(max)) {
654 fracpart -= POW10(max);
658 /* Convert integer part */
661 my_modf(temp, &intpart);
662 idx = (int) ((temp -intpart +0.05)* 10.0);
663 /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
664 /* printf ("%llf, %f, %x\n", temp, intpart, idx); */
666 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
667 } while (intpart && (iplace < 311));
668 if (iplace == 311) iplace--;
669 iconvert[iplace] = 0;
671 /* Convert fractional part */
676 my_modf(temp, &fracpart);
677 idx = (int) ((temp -fracpart +0.05)* 10.0);
678 /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
679 /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
681 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
682 } while(fracpart && (fplace < 311));
683 if (fplace == 311) fplace--;
685 fconvert[fplace] = 0;
687 /* -1 for decimal point, another -1 if we are printing a sign */
688 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
689 zpadlen = max - fplace;
690 if (zpadlen < 0) zpadlen = 0;
693 if (flags & DP_F_MINUS)
694 padlen = -padlen; /* Left Justifty */
696 if ((flags & DP_F_ZERO) && (padlen > 0)) {
698 dopr_outch (buffer, currlen, maxlen, signvalue);
703 dopr_outch (buffer, currlen, maxlen, '0');
708 dopr_outch (buffer, currlen, maxlen, ' ');
712 dopr_outch (buffer, currlen, maxlen, signvalue);
715 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
718 * Decimal point. This should probably use locale to find the correct
722 dopr_outch (buffer, currlen, maxlen, '.');
724 while (zpadlen > 0) {
725 dopr_outch (buffer, currlen, maxlen, '0');
730 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
734 dopr_outch (buffer, currlen, maxlen, ' ');
739 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
741 if (*currlen < maxlen) {
742 buffer[(*currlen)] = c;
747 int silc_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
751 return dopr(str, count, fmt, args);
754 int silc_snprintf(char *str, size_t count, const char *fmt, ...)
760 ret = silc_vsnprintf(str, count, fmt, ap);
765 int silc_vasprintf(char **ptr, const char *format, va_list ap)
770 silc_va_copy(ap2, ap);
772 ret = silc_vsnprintf(NULL, 0, format, ap2);
773 if (ret <= 0) return ret;
775 (*ptr) = (char *)silc_malloc(ret+1);
776 if (!*ptr) return -1;
778 silc_va_copy(ap2, ap);
780 ret = silc_vsnprintf(*ptr, ret+1, format, ap2);
785 int silc_asprintf(char **ptr, const char *format, ...)
791 va_start(ap, format);
792 ret = silc_vasprintf(ptr, format, ap);