1 /* This file is NOT part of Wget, but is used by Wget on the systems
2 where vsnprintf() is not defined. It has been written by Patrick
3 Powell and modified by other people. All the copyright and other
4 notices have been left intact.
6 My changes are documented at the bottom, along with other changes.
7 I hereby place my modifications to this file under the public
11 * Copyright Patrick Powell 1995
12 * This code is based on code written by Patrick Powell (papowell@astart.com)
13 * It may be used for any purpose as long as this notice remains intact
14 * on all source code distributions
17 /**************************************************************
19 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
20 * A bombproof version of doprnt (dopr) included.
21 * Sigh. This sort of thing is always nasty do deal with. Note that
22 * the version here does not include floating point...
24 * snprintf() is used instead of sprintf() as it does limit checks
25 * for string length. This covers a nasty loophole.
27 * The other functions are there to prevent NULL pointers from
28 * causing nast effects.
31 * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
32 * This was ugly. It is still ugly. I opted out of floating point
33 * numbers, but the formatter understands just about everything
34 * from the normal C string format, at least as far as I can tell from
35 * the Solaris 2.5 printf(3S) man page.
37 * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
38 * Ok, added some minimal floating point support, which means this
39 * probably requires libm on most operating systems. Don't yet
40 * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
41 * was pretty badly broken, it just wasn't being exercised in ways
42 * which showed it, so that's been fixed. Also, formated the code
43 * to mutt conventions, and removed dead code left over from the
44 * original. Also, there is now a builtin-test, just compile with:
45 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
46 * and run snprintf for results.
48 * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
49 * The PGP code was using unsigned hexadecimal formats.
50 * Unfortunately, unsigned formats simply didn't work.
52 * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
53 * The original code assumed that both snprintf() and vsnprintf() were
54 * missing. Some systems only have snprintf() but not vsnprintf(), so
55 * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
57 * Andrew Tridgell (tridge@samba.org) Oct 1998
58 * fixed handling of %.0f
59 * added test for HAVE_LONG_DOUBLE
61 * Russ Allbery <rra@stanford.edu> 2000-08-26
62 * fixed return value to comply with C99
63 * fixed handling of snprintf(NULL, ...)
65 * Hrvoje Niksic <hniksic@arsdigita.com> 2000-11-04
66 * include <config.h> instead of "config.h".
67 * moved TEST_SNPRINTF stuff out of HAVE_SNPRINTF ifdef.
68 * include <stdio.h> for NULL.
69 * added support and test cases for long long.
70 * don't declare argument types to (v)snprintf if stdarg is not used.
71 * use int instead of short int as 2nd arg to va_arg.
73 **************************************************************/
79 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
82 #include <sys/types.h>
83 #include <stdio.h> /* for NULL */
84 #include <safe-ctype.h>
86 /* varargs declarations: */
88 #if defined(HAVE_STDARG_H)
90 # define HAVE_STDARGS /* let's hope that works everywhere (mj) */
91 # define VA_LOCAL_DECL va_list ap
92 # define VA_START(f) va_start(ap, f)
93 # define VA_SHIFT(v,t) ; /* no-op for ANSI */
94 # define VA_END va_end(ap)
98 # define VA_LOCAL_DECL va_list ap
99 # define VA_START(f) va_start(ap) /* f is ignored! */
100 # define VA_SHIFT(v,t) v = va_arg(ap,t)
101 # define VA_END va_end(ap)
104 #ifdef HAVE_LONG_DOUBLE
105 #define LDOUBLE long double
107 #define LDOUBLE double
110 #ifdef HAVE_LONG_LONG
111 # define LLONG long long
117 int snprintf (char *str, size_t count, const char *fmt, ...);
118 int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);
124 static int dopr (char *buffer, size_t maxlen, const char *format,
126 static int fmtstr (char *buffer, size_t *currlen, size_t maxlen,
127 char *value, int flags, int min, int max);
128 static int fmtint (char *buffer, size_t *currlen, size_t maxlen,
129 LLONG value, int base, int min, int max, int flags);
130 static int fmtfp (char *buffer, size_t *currlen, size_t maxlen,
131 LDOUBLE fvalue, int min, int max, int flags);
132 static int dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c );
135 * dopr(): poor man's version of doprintf
138 /* format read states */
139 #define DP_S_DEFAULT 0
149 /* format flags - Bits */
150 #define DP_F_MINUS (1 << 0)
151 #define DP_F_PLUS (1 << 1)
152 #define DP_F_SPACE (1 << 2)
153 #define DP_F_NUM (1 << 3)
154 #define DP_F_ZERO (1 << 4)
155 #define DP_F_UP (1 << 5)
156 #define DP_F_UNSIGNED (1 << 6)
158 /* Conversion Flags */
162 #define DP_C_LDOUBLE 4
164 #define char_to_int(p) (p - '0')
165 #define MAX(p,q) ((p >= q) ? p : q)
166 #define MIN(p,q) ((p <= q) ? p : q)
168 static int dopr (char *buffer, size_t maxlen, const char *format, va_list args)
182 state = DP_S_DEFAULT;
183 currlen = flags = cflags = min = 0;
188 while (state != DP_S_DONE)
199 total += dopr_outch (buffer, &currlen, maxlen, ch);
231 if ('0' <= ch && ch <= '9')
233 min = 10*min + char_to_int (ch);
238 min = va_arg (args, int);
255 if ('0' <= ch && ch <= '9')
259 max = 10*max + char_to_int (ch);
264 max = va_arg (args, int);
283 cflags = DP_C_LDOUBLE;
289 if (cflags != DP_C_LONG)
311 if (cflags == DP_C_SHORT)
312 value = (short int)va_arg (args, int);
313 else if (cflags == DP_C_LONG)
314 value = va_arg (args, long int);
315 else if (cflags == DP_C_LLONG)
316 value = va_arg (args, LLONG);
318 value = va_arg (args, int);
319 total += fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
322 flags |= DP_F_UNSIGNED;
323 if (cflags == DP_C_SHORT)
324 value = (unsigned short int)va_arg (args, unsigned int);
325 else if (cflags == DP_C_LONG)
326 value = va_arg (args, unsigned long int);
327 else if (cflags == DP_C_LLONG)
328 value = va_arg (args, unsigned LLONG);
330 value = va_arg (args, unsigned int);
331 total += fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
334 flags |= DP_F_UNSIGNED;
335 if (cflags == DP_C_SHORT)
336 value = (unsigned short int)va_arg (args, unsigned int);
337 else if (cflags == DP_C_LONG)
338 value = va_arg (args, unsigned long int);
339 else if (cflags == DP_C_LLONG)
340 value = va_arg (args, unsigned LLONG);
342 value = va_arg (args, unsigned int);
343 total += fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
348 flags |= DP_F_UNSIGNED;
349 if (cflags == DP_C_SHORT)
350 value = (unsigned short int)va_arg (args, unsigned int);
351 else if (cflags == DP_C_LONG)
352 value = va_arg (args, unsigned long int);
353 else if (cflags == DP_C_LLONG)
354 value = va_arg (args, unsigned LLONG);
356 value = va_arg (args, unsigned int);
357 total += fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
360 if (cflags == DP_C_LDOUBLE)
361 fvalue = va_arg (args, LDOUBLE);
363 fvalue = va_arg (args, double);
364 /* um, floating point? */
365 total += fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
370 if (cflags == DP_C_LDOUBLE)
371 fvalue = va_arg (args, LDOUBLE);
373 fvalue = va_arg (args, double);
378 if (cflags == DP_C_LDOUBLE)
379 fvalue = va_arg (args, LDOUBLE);
381 fvalue = va_arg (args, double);
384 total += dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
387 strvalue = va_arg (args, char *);
388 total += fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
391 strvalue = va_arg (args, void *);
392 total += fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min,
396 if (cflags == DP_C_SHORT)
399 num = va_arg (args, short int *);
402 else if (cflags == DP_C_LONG)
405 num = va_arg (args, long int *);
408 else if (cflags == DP_C_LLONG)
411 num = va_arg (args, LLONG *);
417 num = va_arg (args, int *);
422 total += dopr_outch (buffer, &currlen, maxlen, ch);
425 /* not supported yet, treat as next char */
433 state = DP_S_DEFAULT;
434 flags = cflags = min = 0;
441 break; /* some picky compilers need this */
446 if (currlen < maxlen - 1)
447 buffer[currlen] = '\0';
449 buffer[maxlen - 1] = '\0';
454 static int fmtstr (char *buffer, size_t *currlen, size_t maxlen,
455 char *value, int flags, int min, int max)
457 int padlen, strln; /* amount to pad */
466 for (strln = 0; value[strln]; ++strln); /* strlen */
467 if (max >= 0 && max < strln)
469 padlen = min - strln;
472 if (flags & DP_F_MINUS)
473 padlen = -padlen; /* Left Justify */
477 total += dopr_outch (buffer, currlen, maxlen, ' ');
480 while (*value && ((max < 0) || (cnt < max)))
482 total += dopr_outch (buffer, currlen, maxlen, *value++);
487 total += dopr_outch (buffer, currlen, maxlen, ' ');
493 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
495 static int fmtint (char *buffer, size_t *currlen, size_t maxlen,
496 LLONG value, int base, int min, int max, int flags)
499 unsigned LLONG uvalue;
502 int spadlen = 0; /* amount to space pad */
503 int zpadlen = 0; /* amount to zero pad */
512 if(!(flags & DP_F_UNSIGNED))
519 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
522 if (flags & DP_F_SPACE)
527 /* Should characters be upper case? */
528 digits = "0123456789ABCDEF";
530 digits = "0123456789abcdef";
533 convert[place++] = digits[uvalue % (unsigned)base];
534 uvalue = (uvalue / (unsigned)base );
535 } while(uvalue && (place < sizeof (convert)));
536 if (place == sizeof (convert)) place--;
539 zpadlen = max - place;
540 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
541 if (zpadlen < 0) zpadlen = 0;
542 if (spadlen < 0) spadlen = 0;
543 if (flags & DP_F_ZERO)
545 zpadlen = MAX(zpadlen, spadlen);
548 if (flags & DP_F_MINUS)
549 spadlen = -spadlen; /* Left Justifty */
551 #ifdef DEBUG_SNPRINTF
552 dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
553 zpadlen, spadlen, min, max, place));
559 total += dopr_outch (buffer, currlen, maxlen, ' ');
565 total += dopr_outch (buffer, currlen, maxlen, signvalue);
572 total += dopr_outch (buffer, currlen, maxlen, '0');
579 total += dopr_outch (buffer, currlen, maxlen, convert[--place]);
581 /* Left Justified spaces */
582 while (spadlen < 0) {
583 total += dopr_outch (buffer, currlen, maxlen, ' ');
590 static LDOUBLE abs_val (LDOUBLE value)
592 LDOUBLE result = value;
600 static LDOUBLE pow10 (int exp)
613 static long round (LDOUBLE value)
618 value = value - intpart;
625 static int fmtfp (char *buffer, size_t *currlen, size_t maxlen,
626 LDOUBLE fvalue, int min, int max, int flags)
634 int padlen = 0; /* amount to pad */
642 * AIX manpage says the default is 0, but Solaris says the default
643 * is 6, and sprintf on AIX defaults to 6
648 ufvalue = abs_val (fvalue);
653 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
656 if (flags & DP_F_SPACE)
660 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
666 * Sorry, we only support 9 digits past the decimal because of our
672 /* We "cheat" by converting the fractional part to integer by
673 * multiplying by a factor of 10
675 fracpart = round ((pow10 (max)) * (ufvalue - intpart));
677 if (fracpart >= pow10 (max))
680 fracpart -= pow10 (max);
683 #ifdef DEBUG_SNPRINTF
684 dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart));
687 /* Convert integer part */
690 (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
691 intpart = (intpart / 10);
692 } while(intpart && (iplace < 20));
693 if (iplace == 20) iplace--;
694 iconvert[iplace] = 0;
696 /* Convert fractional part */
699 (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
700 fracpart = (fracpart / 10);
701 } while(fracpart && (fplace < 20));
702 if (fplace == 20) fplace--;
703 fconvert[fplace] = 0;
705 /* -1 for decimal point, another -1 if we are printing a sign */
706 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
707 zpadlen = max - fplace;
712 if (flags & DP_F_MINUS)
713 padlen = -padlen; /* Left Justifty */
715 if ((flags & DP_F_ZERO) && (padlen > 0))
719 total += dopr_outch (buffer, currlen, maxlen, signvalue);
725 total += dopr_outch (buffer, currlen, maxlen, '0');
731 total += dopr_outch (buffer, currlen, maxlen, ' ');
735 total += dopr_outch (buffer, currlen, maxlen, signvalue);
738 total += dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
741 * Decimal point. This should probably use locale to find the correct
746 total += dopr_outch (buffer, currlen, maxlen, '.');
749 total += dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
754 total += dopr_outch (buffer, currlen, maxlen, '0');
760 total += dopr_outch (buffer, currlen, maxlen, ' ');
767 static int dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c)
769 if (*currlen + 1 < maxlen)
770 buffer[(*currlen)++] = c;
774 #ifndef HAVE_VSNPRINTF
775 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
779 return dopr(str, count, fmt, args);
781 #endif /* !HAVE_VSNPRINTF */
783 #ifndef HAVE_SNPRINTF
786 int snprintf (char *str,size_t count,const char *fmt,...)
788 int snprintf (va_alist) va_dcl
800 VA_SHIFT (str, char *);
801 VA_SHIFT (count, size_t );
802 VA_SHIFT (fmt, char *);
803 total = vsnprintf(str, count, fmt, ap);
807 #endif /* !HAVE_SNPRINTF */
808 #endif /* !HAVE_SNPRINTF || !HAVE_VSNPRINTF */
815 #define LONG_STRING 1024
819 char buf1[LONG_STRING];
820 char buf2[LONG_STRING];
837 double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
838 0.9996, 1.996, 4.136, 0};
851 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
852 #ifdef HAVE_LONG_LONG
853 char *llong_fmt[] = {
855 "%-1.5lld", "%-1.5llu",
856 "%1.5lld", "%1.5llu",
857 "%123.9lld", "%123.9llu",
858 "%5.5lld", "%5.5llu",
859 "%10.5lld", "%10.5llu",
860 "% 10.5lld", "% 10.5llu",
861 "%+22.33lld", "%+22.33llu",
862 "%01.3lld", "%01.3llu",
866 long long llong_nums[] = {
867 ~(long long)0, /* all-1 bit pattern */
868 (~(unsigned long long)0) >> 1, /* largest signed long long */
870 -150, 134, 91340, 341,
878 printf ("Testing snprintf format codes against system sprintf...\n");
880 for (x = 0; fp_fmt[x] != NULL ; x++)
881 for (y = 0; fp_nums[y] != 0 ; y++)
883 snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
884 sprintf (buf2, fp_fmt[x], fp_nums[y]);
885 if (strcmp (buf1, buf2))
887 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
888 fp_fmt[x], buf1, buf2);
894 for (x = 0; int_fmt[x] != NULL ; x++)
895 for (y = 0; int_nums[y] != 0 ; y++)
897 snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
898 sprintf (buf2, int_fmt[x], int_nums[y]);
899 if (strcmp (buf1, buf2))
901 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
902 int_fmt[x], buf1, buf2);
908 #ifdef HAVE_LONG_LONG
909 for (x = 0; llong_fmt[x] != NULL ; x++)
910 for (y = 0; llong_nums[y] != 0 ; y++)
912 snprintf (buf1, sizeof (buf1), llong_fmt[x], llong_nums[y]);
913 sprintf (buf2, llong_fmt[x], llong_nums[y]);
914 if (strcmp (buf1, buf2))
916 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
917 llong_fmt[x], buf1, buf2);
924 printf ("%d tests failed out of %d.\n", fail, num);
926 #endif /* SNPRINTF_TEST */