1 #include <ldns/config.h>
8 /* Define this as a fall through, HAVE_STDARG_H is probably already set */
10 #define HAVE_VARARGS_H
12 /**************************************************************
14 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
15 * A bombproof version of doprnt (dopr) included.
16 * Sigh. This sort of thing is always nasty do deal with. Note that
17 * the version here does not include floating point...
19 * snprintf() is used instead of sprintf() as it does limit checks
20 * for string length. This covers a nasty loophole.
22 * The other functions are there to prevent NULL pointers from
23 * causing nast effects.
26 * Brandon Long (blong@fiction.net) 9/15/96 for mutt 0.43
27 * This was ugly. It is still ugly. I opted out of floating point
28 * numbers, but the formatter understands just about everything
29 * from the normal C string format, at least as far as I can tell from
30 * the Solaris 2.5 printf(3S) man page.
32 * Brandon Long (blong@fiction.net) 10/22/97 for mutt 0.87.1
33 * Ok, added some minimal floating point support, which means this
34 * probably requires libm on most operating systems. Don't yet
35 * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
36 * was pretty badly broken, it just wasn't being exercised in ways
37 * which showed it, so that's been fixed. Also, formated the code
38 * to mutt conventions, and removed dead code left over from the
39 * original. Also, there is now a builtin-test, just compile with:
40 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
41 * and run snprintf for results.
43 **************************************************************/
46 /* varargs declarations: */
48 #if defined(HAVE_STDARG_H)
50 # define HAVE_STDARGS /* let's hope that works everywhere (mj) */
51 # define VA_LOCAL_DECL va_list ap
52 # define VA_START(f) va_start(ap, f)
53 # define VA_SHIFT(v,t) ; /* no-op for ANSI */
54 # define VA_END va_end(ap)
56 # if defined(HAVE_VARARGS_H)
59 # define VA_LOCAL_DECL va_list ap
60 # define VA_START(f) va_start(ap) /* f is ignored! */
61 # define VA_SHIFT(v,t) v = va_arg(ap,t)
62 # define VA_END va_end(ap)
64 /*XX ** NO VARARGS ** XX*/
68 int snprintf (char *str, size_t count, const char *fmt, ...);
69 int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);
71 static void dopr (char *buffer, size_t maxlen, const char *format,
73 static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
74 char *value, int flags, int min, int max);
75 static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
76 long value, int base, int min, int max, int flags);
77 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
78 long double fvalue, int min, int max, int flags);
79 static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c );
81 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
84 dopr(str, count, fmt, args);
90 int snprintf (char *str,size_t count,const char *fmt,...)
92 int snprintf (va_alist) va_dcl
103 VA_SHIFT (str, char *);
104 VA_SHIFT (count, size_t );
105 VA_SHIFT (fmt, char *);
106 (void) vsnprintf(str, count, fmt, ap);
112 * dopr(): poor man's version of doprintf
115 /* format read states */
116 #define DP_S_DEFAULT 0
125 /* format flags - Bits */
133 /* Conversion Flags */
136 #define DP_C_LDOUBLE 3
138 #define char_to_int(p) (p - '0')
139 #define MAX(p,q) ((p >= q) ? p : q)
141 static void dopr (char *buffer, size_t maxlen, const char *format, va_list args)
154 state = DP_S_DEFAULT;
155 currlen = flags = cflags = min = 0;
159 while (state != DP_S_DONE)
161 if ((ch == '\0') || (currlen >= maxlen))
170 dopr_outch (buffer, &currlen, maxlen, ch);
202 if (isdigit((int) ch))
204 min = 10*min + char_to_int (ch);
209 min = va_arg (args, int);
226 if (isdigit((int) ch))
230 max = 10*max + char_to_int (ch);
235 max = va_arg (args, int);
243 /* Currently, we don't support Long Long, bummer */
255 cflags = DP_C_LDOUBLE;
268 if (cflags == DP_C_SHORT)
269 value = va_arg (args, int);
270 else if (cflags == DP_C_LONG)
271 value = va_arg (args, long int);
273 value = va_arg (args, int);
274 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
278 if (cflags == DP_C_SHORT)
279 value = va_arg (args, unsigned int);
280 else if (cflags == DP_C_LONG)
281 value = va_arg (args, unsigned long int);
283 value = va_arg (args, unsigned int);
284 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
288 if (cflags == DP_C_SHORT)
289 value = va_arg (args, unsigned int);
290 else if (cflags == DP_C_LONG)
291 value = va_arg (args, unsigned long int);
293 value = va_arg (args, unsigned int);
294 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
300 if (cflags == DP_C_SHORT)
301 value = va_arg (args, unsigned int);
302 else if (cflags == DP_C_LONG)
303 value = va_arg (args, unsigned long int);
305 value = va_arg (args, unsigned int);
306 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
309 if (cflags == DP_C_LDOUBLE)
310 fvalue = va_arg (args, long double);
312 fvalue = va_arg (args, double);
313 /* um, floating point? */
314 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
319 if (cflags == DP_C_LDOUBLE)
320 fvalue = va_arg (args, long double);
322 fvalue = va_arg (args, double);
327 if (cflags == DP_C_LDOUBLE)
328 fvalue = va_arg (args, long double);
330 fvalue = va_arg (args, double);
333 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
336 strvalue = va_arg (args, char *);
338 max = maxlen; /* ie, no max */
339 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
342 strvalue = va_arg (args, void *);
343 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
346 if (cflags == DP_C_SHORT)
349 num = va_arg (args, short int *);
352 else if (cflags == DP_C_LONG)
355 num = va_arg (args, long int *);
361 num = va_arg (args, int *);
366 dopr_outch (buffer, &currlen, maxlen, ch);
369 /* not supported yet, treat as next char */
377 state = DP_S_DEFAULT;
378 flags = cflags = min = 0;
385 break; /* some picky compilers need this */
388 if (currlen < maxlen - 1)
389 buffer[currlen] = '\0';
391 buffer[maxlen - 1] = '\0';
394 static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
395 char *value, int flags, int min, int max)
397 int padlen, strln; /* amount to pad */
402 value = (char *) "<NULL>";
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))
414 dopr_outch (buffer, currlen, maxlen, ' ');
418 while (*value && (cnt < max))
420 dopr_outch (buffer, currlen, maxlen, *value++);
423 while ((padlen < 0) && (cnt < max))
425 dopr_outch (buffer, currlen, maxlen, ' ');
431 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
433 static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
434 long value, int base, int min, int max, int flags)
437 unsigned long uvalue;
440 int spadlen = 0; /* amount to space pad */
441 int zpadlen = 0; /* amount to zero pad */
453 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
456 if (flags & DP_F_SPACE)
459 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
463 (caps? "0123456789ABCDEF":"0123456789abcdef")
464 [uvalue % (unsigned)base ];
465 uvalue = (uvalue / (unsigned)base );
466 } while(uvalue && (place < 20));
467 if (place == 20) place--;
470 zpadlen = max - place;
471 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
472 if (zpadlen < 0) zpadlen = 0;
473 if (spadlen < 0) spadlen = 0;
474 if (flags & DP_F_ZERO)
476 zpadlen = MAX(zpadlen, spadlen);
479 if (flags & DP_F_MINUS)
480 spadlen = -spadlen; /* Left Justifty */
482 #ifdef DEBUG_SNPRINTF
483 dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
484 zpadlen, spadlen, min, max, place));
490 dopr_outch (buffer, currlen, maxlen, ' ');
496 dopr_outch (buffer, currlen, maxlen, signvalue);
503 dopr_outch (buffer, currlen, maxlen, '0');
510 dopr_outch (buffer, currlen, maxlen, convert[--place]);
512 /* Left Justified spaces */
513 while (spadlen < 0) {
514 dopr_outch (buffer, currlen, maxlen, ' ');
519 static long double abs_val (long double value)
521 long double result = value;
529 static double pow10 (double exp)
531 long double result = 1;
542 static double round (double value)
547 value = value - intpart;
554 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
555 long double fvalue, int min, int max, int flags)
563 int padlen = 0; /* amount to pad */
570 * AIX manpage says the default is 0, but Solaris says the default
571 * is 6, and sprintf on AIX defaults to 6
576 ufvalue = abs_val (fvalue);
581 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
584 if (flags & DP_F_SPACE)
588 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
594 * Sorry, we only support 9 digits past the decimal because of our
600 /* We "cheat" by converting the fractional part to integer by
601 * multiplying by a factor of 10
603 fracpart = round ((pow10 (max)) * (ufvalue - intpart));
605 if (fracpart >= pow10 (max))
608 fracpart -= pow10 (max);
611 #ifdef DEBUG_SNPRINTF
612 dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart));
615 /* Convert integer part */
618 (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
619 intpart = (intpart / 10);
620 } while(intpart && (iplace < 20));
621 if (iplace == 20) iplace--;
622 iconvert[iplace] = 0;
624 /* Convert fractional part */
627 (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
628 fracpart = (fracpart / 10);
629 } while(fracpart && (fplace < 20));
630 if (fplace == 20) fplace--;
631 fconvert[fplace] = 0;
633 /* -1 for decimal point, another -1 if we are printing a sign */
634 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
635 zpadlen = max - fplace;
640 if (flags & DP_F_MINUS)
641 padlen = -padlen; /* Left Justifty */
643 if ((flags & DP_F_ZERO) && (padlen > 0))
647 dopr_outch (buffer, currlen, maxlen, signvalue);
653 dopr_outch (buffer, currlen, maxlen, '0');
659 dopr_outch (buffer, currlen, maxlen, ' ');
663 dopr_outch (buffer, currlen, maxlen, signvalue);
666 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
669 * Decimal point. This should probably use locale to find the correct
672 dopr_outch (buffer, currlen, maxlen, '.');
676 dopr_outch (buffer, currlen, maxlen, '0');
681 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
685 dopr_outch (buffer, currlen, maxlen, ' ');
690 static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c)
692 if (*currlen < maxlen)
693 buffer[(*currlen)++] = c;
698 #define LONG_STRING 1024
702 char buf1[LONG_STRING];
703 char buf2[LONG_STRING];
718 double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
719 0.9996, 1.996, 4.136, 0};
732 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
737 printf ("Testing snprintf format codes against system sprintf...\n");
739 for (x = 0; fp_fmt[x] != NULL ; x++)
740 for (y = 0; fp_nums[y] != 0 ; y++)
742 snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
743 sprintf (buf2, fp_fmt[x], fp_nums[y]);
744 if (strcmp (buf1, buf2))
746 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
747 fp_fmt[x], buf1, buf2);
753 for (x = 0; int_fmt[x] != NULL ; x++)
754 for (y = 0; int_nums[y] != 0 ; y++)
756 snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
757 sprintf (buf2, int_fmt[x], int_nums[y]);
758 if (strcmp (buf1, buf2))
760 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
761 int_fmt[x], buf1, buf2);
766 printf ("%d tests failed out of %d.\n", fail, num);
768 #endif /* SNPRINTF_TEST */
770 #endif /* !HAVE_SNPRINTF */