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)
95 /* Conversion Flags */
98 #define DP_C_LDOUBLE 3
101 #define char_to_int(p) ((p)- '0')
103 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
106 static size_t dopr(char *buffer, size_t maxlen, const char *format,
108 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
109 char *value, int flags, int min, int max);
110 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
111 long value, int base, int min, int max, int flags);
112 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
113 LDOUBLE fvalue, int min, int max, int flags);
114 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
116 static size_t dopr(char *buffer, size_t maxlen, const char *format,
131 silc_va_copy(args, args_in);
133 state = DP_S_DEFAULT;
134 currlen = flags = cflags = min = 0;
138 while (state != DP_S_DONE) {
147 dopr_outch (buffer, &currlen, maxlen, ch);
178 if (isdigit((unsigned char)ch)) {
179 min = 10*min + char_to_int (ch);
181 } else if (ch == '*') {
182 min = va_arg (args, int);
198 if (isdigit((unsigned char)ch)) {
201 max = 10*max + char_to_int (ch);
203 } else if (ch == '*') {
204 max = va_arg (args, int);
220 if (ch == 'l') { /* It's a long long */
226 cflags = DP_C_LDOUBLE;
238 if (cflags == DP_C_SHORT)
239 value = va_arg (args, int);
240 else if (cflags == DP_C_LONG)
241 value = va_arg (args, long int);
242 else if (cflags == DP_C_LLONG)
243 value = va_arg (args, LLONG);
245 value = va_arg (args, int);
246 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
249 flags |= DP_F_UNSIGNED;
250 if (cflags == DP_C_SHORT)
251 value = va_arg (args, unsigned int);
252 else if (cflags == DP_C_LONG)
253 value = (long)va_arg (args, unsigned long int);
254 else if (cflags == DP_C_LLONG)
255 value = (long)va_arg (args, unsigned LLONG);
257 value = (long)va_arg (args, unsigned int);
258 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
261 flags |= DP_F_UNSIGNED;
262 if (cflags == DP_C_SHORT)
263 value = va_arg (args, unsigned int);
264 else if (cflags == DP_C_LONG)
265 value = (long)va_arg (args, unsigned long int);
266 else if (cflags == DP_C_LLONG)
267 value = (LLONG)va_arg (args, unsigned LLONG);
269 value = (long)va_arg (args, unsigned int);
270 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
275 flags |= DP_F_UNSIGNED;
276 if (cflags == DP_C_SHORT)
277 value = va_arg (args, unsigned int);
278 else if (cflags == DP_C_LONG)
279 value = (long)va_arg (args, unsigned long int);
280 else if (cflags == DP_C_LLONG)
281 value = (LLONG)va_arg (args, unsigned LLONG);
283 value = (long)va_arg (args, unsigned int);
284 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
287 if (cflags == DP_C_LDOUBLE)
288 fvalue = va_arg (args, LDOUBLE);
290 fvalue = va_arg (args, double);
291 /* um, floating point? */
292 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
297 if (cflags == DP_C_LDOUBLE)
298 fvalue = va_arg (args, LDOUBLE);
300 fvalue = va_arg (args, double);
301 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
306 if (cflags == DP_C_LDOUBLE)
307 fvalue = va_arg (args, LDOUBLE);
309 fvalue = va_arg (args, double);
310 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
313 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
316 strvalue = va_arg (args, char *);
317 if (!strvalue) strvalue = "(NULL)";
319 max = strlen(strvalue);
321 if (min > 0 && max >= 0 && min > max) max = min;
322 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
325 strvalue = va_arg (args, void *);
326 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
329 if (cflags == DP_C_SHORT) {
331 num = va_arg (args, short int *);
333 } else if (cflags == DP_C_LONG) {
335 num = va_arg (args, long int *);
336 *num = (long int)currlen;
337 } else if (cflags == DP_C_LLONG) {
339 num = va_arg (args, LLONG *);
340 *num = (LLONG)currlen;
343 num = va_arg (args, int *);
348 dopr_outch (buffer, &currlen, maxlen, ch);
351 /* not supported yet, treat as next char */
359 state = DP_S_DEFAULT;
360 flags = cflags = min = 0;
367 break; /* some picky compilers need this */
371 if (currlen < maxlen - 1)
372 buffer[currlen] = '\0';
374 buffer[maxlen - 1] = '\0';
380 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
381 char *value, int flags, int min, int max)
383 int padlen, strln; /* amount to pad */
390 for (strln = 0; value[strln]; ++strln); /* strlen */
391 padlen = min - strln;
394 if (flags & DP_F_MINUS)
395 padlen = -padlen; /* Left Justify */
397 while ((padlen > 0) && (cnt < max)) {
398 dopr_outch (buffer, currlen, maxlen, ' ');
402 while (*value && (cnt < max)) {
403 dopr_outch (buffer, currlen, maxlen, *value++);
406 while ((padlen < 0) && (cnt < max)) {
407 dopr_outch (buffer, currlen, maxlen, ' ');
413 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
415 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
416 long value, int base, int min, int max, int flags)
419 unsigned long uvalue;
422 int spadlen = 0; /* amount to space pad */
423 int zpadlen = 0; /* amount to zero pad */
431 if(!(flags & DP_F_UNSIGNED)) {
436 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
438 else if (flags & DP_F_SPACE)
443 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
447 (caps? "0123456789ABCDEF":"0123456789abcdef")
448 [uvalue % (unsigned)base ];
449 uvalue = (uvalue / (unsigned)base );
450 } while(uvalue && (place < 20));
451 if (place == 20) place--;
454 zpadlen = max - place;
455 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
456 if (zpadlen < 0) zpadlen = 0;
457 if (spadlen < 0) spadlen = 0;
458 if (flags & DP_F_ZERO) {
459 zpadlen = MAX(zpadlen, spadlen);
462 if (flags & DP_F_MINUS)
463 spadlen = -spadlen; /* Left Justifty */
466 while (spadlen > 0) {
467 dopr_outch (buffer, currlen, maxlen, ' ');
473 dopr_outch (buffer, currlen, maxlen, signvalue);
477 while (zpadlen > 0) {
478 dopr_outch (buffer, currlen, maxlen, '0');
485 dopr_outch (buffer, currlen, maxlen, convert[--place]);
487 /* Left Justified spaces */
488 while (spadlen < 0) {
489 dopr_outch (buffer, currlen, maxlen, ' ');
494 static LDOUBLE abs_val(LDOUBLE value)
496 LDOUBLE result = value;
504 static LDOUBLE POW10(int exp)
516 static LLONG ROUND(LDOUBLE value)
520 intpart = (LLONG)value;
521 value = value - intpart;
522 if (value >= 0.5) intpart++;
527 /* a replacement for modf that doesn't need the math library. Should
528 be portable, but slow */
529 static double my_modf(double x0, double *iptr)
536 for (i=0;i<100;i++) {
538 if (l <= (x+1) && l >= (x-1)) break;
544 /* yikes! the number is beyond what we can handle.
554 ret = my_modf(x0-l*f, &i2);
564 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
565 LDOUBLE fvalue, int min, int max, int flags)
573 int padlen = 0; /* amount to pad */
582 * AIX manpage says the default is 0, but Solaris says the default
583 * is 6, and sprintf on AIX defaults to 6
588 ufvalue = abs_val (fvalue);
593 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
596 if (flags & DP_F_SPACE)
602 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
606 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
610 * Sorry, we only support 16 digits past the decimal because of our
616 /* We "cheat" by converting the fractional part to integer by
617 * multiplying by a factor of 10
621 my_modf(temp, &intpart);
623 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
625 if (fracpart >= POW10(max)) {
627 fracpart -= POW10(max);
631 /* Convert integer part */
634 my_modf(temp, &intpart);
635 idx = (int) ((temp -intpart +0.05)* 10.0);
636 /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
637 /* printf ("%llf, %f, %x\n", temp, intpart, idx); */
639 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
640 } while (intpart && (iplace < 311));
641 if (iplace == 311) iplace--;
642 iconvert[iplace] = 0;
644 /* Convert fractional part */
649 my_modf(temp, &fracpart);
650 idx = (int) ((temp -fracpart +0.05)* 10.0);
651 /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
652 /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
654 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
655 } while(fracpart && (fplace < 311));
656 if (fplace == 311) fplace--;
658 fconvert[fplace] = 0;
660 /* -1 for decimal point, another -1 if we are printing a sign */
661 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
662 zpadlen = max - fplace;
663 if (zpadlen < 0) zpadlen = 0;
666 if (flags & DP_F_MINUS)
667 padlen = -padlen; /* Left Justifty */
669 if ((flags & DP_F_ZERO) && (padlen > 0)) {
671 dopr_outch (buffer, currlen, maxlen, signvalue);
676 dopr_outch (buffer, currlen, maxlen, '0');
681 dopr_outch (buffer, currlen, maxlen, ' ');
685 dopr_outch (buffer, currlen, maxlen, signvalue);
688 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
691 * Decimal point. This should probably use locale to find the correct
695 dopr_outch (buffer, currlen, maxlen, '.');
697 while (zpadlen > 0) {
698 dopr_outch (buffer, currlen, maxlen, '0');
703 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
707 dopr_outch (buffer, currlen, maxlen, ' ');
712 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
714 if (*currlen < maxlen) {
715 buffer[(*currlen)] = c;
720 int silc_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
724 return dopr(str, count, fmt, args);
727 int silc_snprintf(char *str, size_t count, const char *fmt, ...)
733 ret = silc_vsnprintf(str, count, fmt, ap);
738 int silc_vasprintf(char **ptr, const char *format, va_list ap)
743 silc_va_copy(ap2, ap);
745 ret = silc_vsnprintf(NULL, 0, format, ap2);
746 if (ret <= 0) return ret;
748 (*ptr) = (char *)silc_malloc(ret+1);
749 if (!*ptr) return -1;
751 silc_va_copy(ap2, ap);
753 ret = silc_vsnprintf(*ptr, ret+1, format, ap2);
758 int silc_asprintf(char **ptr, const char *format, ...)
764 va_start(ap, format);
765 ret = silc_vasprintf(ptr, format, ap);