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.
72 **************************************************************/
78 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
81 #include <sys/types.h>
82 #include <stdio.h> /* for NULL */
83 #include <safe-ctype.h>
85 /* varargs declarations: */
87 #if defined(HAVE_STDARG_H)
89 # define HAVE_STDARGS /* let's hope that works everywhere (mj) */
90 # define VA_LOCAL_DECL va_list ap
91 # define VA_START(f) va_start(ap, f)
92 # define VA_SHIFT(v,t) ; /* no-op for ANSI */
93 # define VA_END va_end(ap)
97 # define VA_LOCAL_DECL va_list ap
98 # define VA_START(f) va_start(ap) /* f is ignored! */
99 # define VA_SHIFT(v,t) v = va_arg(ap,t)
100 # define VA_END va_end(ap)
103 #ifdef HAVE_LONG_DOUBLE
104 #define LDOUBLE long double
106 #define LDOUBLE double
109 #ifdef HAVE_LONG_LONG
110 # define LLONG long long
116 int snprintf (char *str, size_t count, const char *fmt, ...);
117 int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);
123 static int dopr (char *buffer, size_t maxlen, const char *format,
125 static int fmtstr (char *buffer, size_t *currlen, size_t maxlen,
126 char *value, int flags, int min, int max);
127 static int fmtint (char *buffer, size_t *currlen, size_t maxlen,
128 LLONG value, int base, int min, int max, int flags);
129 static int fmtfp (char *buffer, size_t *currlen, size_t maxlen,
130 LDOUBLE fvalue, int min, int max, int flags);
131 static int dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c );
134 * dopr(): poor man's version of doprintf
137 /* format read states */
138 #define DP_S_DEFAULT 0
148 /* format flags - Bits */
149 #define DP_F_MINUS (1 << 0)
150 #define DP_F_PLUS (1 << 1)
151 #define DP_F_SPACE (1 << 2)
152 #define DP_F_NUM (1 << 3)
153 #define DP_F_ZERO (1 << 4)
154 #define DP_F_UP (1 << 5)
155 #define DP_F_UNSIGNED (1 << 6)
157 /* Conversion Flags */
161 #define DP_C_LDOUBLE 4
163 #define char_to_int(p) (p - '0')
164 #define MAX(p,q) ((p >= q) ? p : q)
165 #define MIN(p,q) ((p <= q) ? p : q)
167 static int dopr (char *buffer, size_t maxlen, const char *format, va_list args)
181 state = DP_S_DEFAULT;
182 currlen = flags = cflags = min = 0;
187 while (state != DP_S_DONE)
198 total += dopr_outch (buffer, &currlen, maxlen, ch);
232 min = 10*min + char_to_int (ch);
237 min = va_arg (args, int);
258 max = 10*max + char_to_int (ch);
263 max = va_arg (args, int);
282 cflags = DP_C_LDOUBLE;
288 if (cflags != DP_C_LONG)
310 if (cflags == DP_C_SHORT)
311 value = va_arg (args, short int);
312 else if (cflags == DP_C_LONG)
313 value = va_arg (args, long int);
314 else if (cflags == DP_C_LLONG)
315 value = va_arg (args, LLONG);
317 value = va_arg (args, int);
318 total += fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
321 flags |= DP_F_UNSIGNED;
322 if (cflags == DP_C_SHORT)
323 value = va_arg (args, unsigned short int);
324 else if (cflags == DP_C_LONG)
325 value = va_arg (args, unsigned long int);
326 else if (cflags == DP_C_LLONG)
327 value = va_arg (args, unsigned LLONG);
329 value = va_arg (args, unsigned int);
330 total += fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
333 flags |= DP_F_UNSIGNED;
334 if (cflags == DP_C_SHORT)
335 value = va_arg (args, unsigned short int);
336 else if (cflags == DP_C_LONG)
337 value = va_arg (args, unsigned long int);
338 else if (cflags == DP_C_LLONG)
339 value = va_arg (args, unsigned LLONG);
341 value = va_arg (args, unsigned int);
342 total += fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
347 flags |= DP_F_UNSIGNED;
348 if (cflags == DP_C_SHORT)
349 value = va_arg (args, unsigned short int);
350 else if (cflags == DP_C_LONG)
351 value = va_arg (args, unsigned long int);
352 else if (cflags == DP_C_LLONG)
353 value = va_arg (args, unsigned LLONG);
355 value = va_arg (args, unsigned int);
356 total += fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
359 if (cflags == DP_C_LDOUBLE)
360 fvalue = va_arg (args, LDOUBLE);
362 fvalue = va_arg (args, double);
363 /* um, floating point? */
364 total += fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
369 if (cflags == DP_C_LDOUBLE)
370 fvalue = va_arg (args, LDOUBLE);
372 fvalue = va_arg (args, double);
377 if (cflags == DP_C_LDOUBLE)
378 fvalue = va_arg (args, LDOUBLE);
380 fvalue = va_arg (args, double);
383 total += dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
386 strvalue = va_arg (args, char *);
387 total += fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
390 strvalue = va_arg (args, void *);
391 total += fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min,
395 if (cflags == DP_C_SHORT)
398 num = va_arg (args, short int *);
401 else if (cflags == DP_C_LONG)
404 num = va_arg (args, long int *);
407 else if (cflags == DP_C_LLONG)
410 num = va_arg (args, LLONG *);
416 num = va_arg (args, int *);
421 total += dopr_outch (buffer, &currlen, maxlen, ch);
424 /* not supported yet, treat as next char */
432 state = DP_S_DEFAULT;
433 flags = cflags = min = 0;
440 break; /* some picky compilers need this */
445 if (currlen < maxlen - 1)
446 buffer[currlen] = '\0';
448 buffer[maxlen - 1] = '\0';
453 static int fmtstr (char *buffer, size_t *currlen, size_t maxlen,
454 char *value, int flags, int min, int max)
456 int padlen, strln; /* amount to pad */
465 for (strln = 0; value[strln]; ++strln); /* strlen */
466 if (max >= 0 && max < strln)
468 padlen = min - strln;
471 if (flags & DP_F_MINUS)
472 padlen = -padlen; /* Left Justify */
476 total += dopr_outch (buffer, currlen, maxlen, ' ');
479 while (*value && ((max < 0) || (cnt < max)))
481 total += dopr_outch (buffer, currlen, maxlen, *value++);
486 total += dopr_outch (buffer, currlen, maxlen, ' ');
492 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
494 static int fmtint (char *buffer, size_t *currlen, size_t maxlen,
495 LLONG value, int base, int min, int max, int flags)
498 unsigned LLONG uvalue;
501 int spadlen = 0; /* amount to space pad */
502 int zpadlen = 0; /* amount to zero pad */
511 if(!(flags & DP_F_UNSIGNED))
518 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
521 if (flags & DP_F_SPACE)
526 /* Should characters be upper case? */
527 digits = "0123456789ABCDEF";
529 digits = "0123456789abcdef";
532 convert[place++] = digits[uvalue % (unsigned)base];
533 uvalue = (uvalue / (unsigned)base );
534 } while(uvalue && (place < sizeof (convert)));
535 if (place == sizeof (convert)) place--;
538 zpadlen = max - place;
539 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
540 if (zpadlen < 0) zpadlen = 0;
541 if (spadlen < 0) spadlen = 0;
542 if (flags & DP_F_ZERO)
544 zpadlen = MAX(zpadlen, spadlen);
547 if (flags & DP_F_MINUS)
548 spadlen = -spadlen; /* Left Justifty */
550 #ifdef DEBUG_SNPRINTF
551 dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
552 zpadlen, spadlen, min, max, place));
558 total += dopr_outch (buffer, currlen, maxlen, ' ');
564 total += dopr_outch (buffer, currlen, maxlen, signvalue);
571 total += dopr_outch (buffer, currlen, maxlen, '0');
578 total += dopr_outch (buffer, currlen, maxlen, convert[--place]);
580 /* Left Justified spaces */
581 while (spadlen < 0) {
582 total += dopr_outch (buffer, currlen, maxlen, ' ');
589 static LDOUBLE abs_val (LDOUBLE value)
591 LDOUBLE result = value;
599 static LDOUBLE pow10 (int exp)
612 static long round (LDOUBLE value)
617 value = value - intpart;
624 static int fmtfp (char *buffer, size_t *currlen, size_t maxlen,
625 LDOUBLE fvalue, int min, int max, int flags)
633 int padlen = 0; /* amount to pad */
641 * AIX manpage says the default is 0, but Solaris says the default
642 * is 6, and sprintf on AIX defaults to 6
647 ufvalue = abs_val (fvalue);
652 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
655 if (flags & DP_F_SPACE)
659 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
665 * Sorry, we only support 9 digits past the decimal because of our
671 /* We "cheat" by converting the fractional part to integer by
672 * multiplying by a factor of 10
674 fracpart = round ((pow10 (max)) * (ufvalue - intpart));
676 if (fracpart >= pow10 (max))
679 fracpart -= pow10 (max);
682 #ifdef DEBUG_SNPRINTF
683 dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart));
686 /* Convert integer part */
689 (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
690 intpart = (intpart / 10);
691 } while(intpart && (iplace < 20));
692 if (iplace == 20) iplace--;
693 iconvert[iplace] = 0;
695 /* Convert fractional part */
698 (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
699 fracpart = (fracpart / 10);
700 } while(fracpart && (fplace < 20));
701 if (fplace == 20) fplace--;
702 fconvert[fplace] = 0;
704 /* -1 for decimal point, another -1 if we are printing a sign */
705 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
706 zpadlen = max - fplace;
711 if (flags & DP_F_MINUS)
712 padlen = -padlen; /* Left Justifty */
714 if ((flags & DP_F_ZERO) && (padlen > 0))
718 total += dopr_outch (buffer, currlen, maxlen, signvalue);
724 total += dopr_outch (buffer, currlen, maxlen, '0');
730 total += dopr_outch (buffer, currlen, maxlen, ' ');
734 total += dopr_outch (buffer, currlen, maxlen, signvalue);
737 total += dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
740 * Decimal point. This should probably use locale to find the correct
745 total += dopr_outch (buffer, currlen, maxlen, '.');
748 total += dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
753 total += dopr_outch (buffer, currlen, maxlen, '0');
759 total += dopr_outch (buffer, currlen, maxlen, ' ');
766 static int dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c)
768 if (*currlen + 1 < maxlen)
769 buffer[(*currlen)++] = c;
773 #ifndef HAVE_VSNPRINTF
774 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
778 return dopr(str, count, fmt, args);
780 #endif /* !HAVE_VSNPRINTF */
782 #ifndef HAVE_SNPRINTF
785 int snprintf (char *str,size_t count,const char *fmt,...)
787 int snprintf (va_alist) va_dcl
799 VA_SHIFT (str, char *);
800 VA_SHIFT (count, size_t );
801 VA_SHIFT (fmt, char *);
802 total = vsnprintf(str, count, fmt, ap);
806 #endif /* !HAVE_SNPRINTF */
807 #endif /* !HAVE_SNPRINTF || !HAVE_VSNPRINTF */
814 #define LONG_STRING 1024
818 char buf1[LONG_STRING];
819 char buf2[LONG_STRING];
836 double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
837 0.9996, 1.996, 4.136, 0};
850 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
851 #ifdef HAVE_LONG_LONG
852 char *llong_fmt[] = {
854 "%-1.5lld", "%-1.5llu",
855 "%1.5lld", "%1.5llu",
856 "%123.9lld", "%123.9llu",
857 "%5.5lld", "%5.5llu",
858 "%10.5lld", "%10.5llu",
859 "% 10.5lld", "% 10.5llu",
860 "%+22.33lld", "%+22.33llu",
861 "%01.3lld", "%01.3llu",
865 long long llong_nums[] = {
866 ~(long long)0, /* all-1 bit pattern */
867 (~(unsigned long long)0) >> 1, /* largest signed long long */
869 -150, 134, 91340, 341,
877 printf ("Testing snprintf format codes against system sprintf...\n");
879 for (x = 0; fp_fmt[x] != NULL ; x++)
880 for (y = 0; fp_nums[y] != 0 ; y++)
882 snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
883 sprintf (buf2, fp_fmt[x], fp_nums[y]);
884 if (strcmp (buf1, buf2))
886 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
887 fp_fmt[x], buf1, buf2);
893 for (x = 0; int_fmt[x] != NULL ; x++)
894 for (y = 0; int_nums[y] != 0 ; y++)
896 snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
897 sprintf (buf2, int_fmt[x], int_nums[y]);
898 if (strcmp (buf1, buf2))
900 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
901 int_fmt[x], buf1, buf2);
907 #ifdef HAVE_LONG_LONG
908 for (x = 0; llong_fmt[x] != NULL ; x++)
909 for (y = 0; llong_nums[y] != 0 ; y++)
911 snprintf (buf1, sizeof (buf1), llong_fmt[x], llong_nums[y]);
912 sprintf (buf2, llong_fmt[x], llong_nums[y]);
913 if (strcmp (buf1, buf2))
915 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
916 llong_fmt[x], buf1, buf2);
923 printf ("%d tests failed out of %d.\n", fail, num);
925 #endif /* SNPRINTF_TEST */