1 /* snprintf - formatted output to strings, with bounds checking and allocation */
4 build a test version with
5 gcc -g -DDRIVER -I../.. -I../../include -o test-snprintf snprintf.c fmtu*long.o
9 Unix snprintf implementation.
10 derived from inetutils/libinetutils/snprintf.c Version 1.1
12 Copyright (C) 2001,2006 Free Software Foundation, Inc.
14 This file is part of GNU Bash, the Bourne Again SHell.
16 Bash is free software: you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation, either version 3 of the License, or
19 (at your option) any later version.
21 Bash is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with Bash. If not, see <http://www.gnu.org/licenses/>.
32 * added changes from Miles Bader
33 * corrected a bug with %f
34 * added support for %#g
35 * added more comments :-)
37 * supporting must ANSI syntaxic_sugars
41 THANKS(for the patches and ideas):
50 * Currently doesn't handle (and bash/readline doesn't use):
51 * * *M$ width, precision specifications
52 * * %N$ numbered argument conversions
53 * * inf, nan floating values imperfect (if isinf(), isnan() not in libc)
54 * * support for `F' is imperfect with ldfallback(), since underlying
55 * printf may not handle it -- should ideally have another autoconf test
58 #define FLOATING_POINT
64 /* GCC 4.2 on Snow Leopard doesn't like the snprintf prototype */
65 #if defined(DEBUG) && !defined (MACOSX)
69 # define HAVE_SNPRINTF 0
70 # define HAVE_ASPRINTF 0
73 #if defined(DRIVER) && !defined(HAVE_CONFIG_H)
74 #define HAVE_LONG_LONG
75 #define HAVE_LONG_DOUBLE
77 #define HAVE_PRINTF_A_FORMAT
79 #define HAVE_ISINF_IN_LIBC
80 #define HAVE_ISNAN_IN_LIBC
82 #define HAVE_STRINGIZE
89 #if !HAVE_SNPRINTF || !HAVE_ASPRINTF
91 #include <bashtypes.h>
93 #if defined(PREFER_STDARG)
102 #include <bashansi.h>
106 #include <chartypes.h>
112 #ifdef FLOATING_POINT
113 # include <float.h> /* for manifest constants */
114 # include <stdio.h> /* for sprintf */
124 #include <shmbutil.h>
129 # define FL_PREFIX 0x01 /* add 0x, 0X, or 0 prefix as appropriate */
130 # define FL_ADDBASE 0x02 /* add base# prefix to converted value */
131 # define FL_HEXUPPER 0x04 /* use uppercase when converting to hex */
132 # define FL_UNSIGNED 0x08 /* don't add any sign */
133 extern char *fmtulong __P((unsigned long int, int, char *, size_t, int));
134 extern char *fmtullong __P((unsigned long long int, int, char *, size_t, int));
138 # define FREE(x) if (x) free (x)
141 /* Bound on length of the string representing an integer value of type T.
142 Subtract one for the sign bit if T is signed;
143 302 / 1000 is log10 (2) rounded up;
144 add one for integer division truncation;
145 add one more for a minus sign if t is signed. */
146 #define INT_STRLEN_BOUND(t) \
147 ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
148 + 1 + TYPE_SIGNED (t))
150 /* conversion flags */
151 #define PF_ALTFORM 0x00001 /* # */
152 #define PF_HEXPREFIX 0x00002 /* 0[Xx] */
153 #define PF_LADJUST 0x00004 /* - */
154 #define PF_ZEROPAD 0x00008 /* 0 */
155 #define PF_PLUS 0x00010 /* + */
156 #define PF_SPACE 0x00020 /* ' ' */
157 #define PF_THOUSANDS 0x00040 /* ' */
159 #define PF_DOT 0x00080 /* `.precision' */
160 #define PF_STAR_P 0x00100 /* `*' after precision */
161 #define PF_STAR_W 0x00200 /* `*' before or without precision */
163 /* length modifiers */
164 #define PF_SIGNEDCHAR 0x00400 /* hh */
165 #define PF_SHORTINT 0x00800 /* h */
166 #define PF_LONGINT 0x01000 /* l */
167 #define PF_LONGLONG 0x02000 /* ll */
168 #define PF_LONGDBL 0x04000 /* L */
169 #define PF_INTMAX_T 0x08000 /* j */
170 #define PF_SIZE_T 0x10000 /* z */
171 #define PF_PTRDIFF_T 0x20000 /* t */
173 #define PF_ALLOCBUF 0x40000 /* for asprintf, vasprintf */
175 #define PFM_SN 0x01 /* snprintf, vsnprintf */
176 #define PFM_AS 0x02 /* asprintf, vasprintf */
178 #define ASBUFSIZE 128
180 #define x_digs "0123456789abcdef"
181 #define X_digs "0123456789ABCDEF"
183 static char intbuf[INT_STRLEN_BOUND(unsigned long) + 1];
187 static char *grouping;
190 * For the FLOATING POINT FORMAT :
191 * the challenge was finding a way to
192 * manipulate the Real numbers without having
193 * to resort to mathematical function(it
194 * would require to link with -lm) and not
195 * going down to the bit pattern(not portable)
197 * so a number, a real is:
199 real = integral + fraction
201 integral = ... + a(2)*10^2 + a(1)*10^1 + a(0)*10^0
202 fraction = b(1)*10^-1 + b(2)*10^-2 + ...
208 from then it was simple math
212 * size of the buffer for the integral part
213 * and the fraction part
215 #define MAX_INT 99 + 1 /* 1 for the null */
216 #define MAX_FRACT 307 + 1
219 * These functions use static buffers to store the results,
220 * and so are not reentrant
222 #define itoa(n) fmtulong(n, 10, intbuf, sizeof(intbuf), 0);
223 #define dtoa(n, p, f) numtoa(n, 10, p, f)
225 #define SWAP_INT(a,b) {int t; t = (a); (a) = (b); (b) = t;}
227 #define GETARG(type) (va_arg(args, type))
229 /* Macros that do proper sign extension and handle length modifiers. Used
230 for the integer conversion specifiers. */
231 #define GETSIGNED(p) \
232 (((p)->flags & PF_LONGINT) \
234 : (((p)->flags & PF_SHORTINT) ? (long)(short)GETARG (int) \
235 : (long)GETARG (int)))
237 #define GETUNSIGNED(p) \
238 (((p)->flags & PF_LONGINT) \
239 ? GETARG (unsigned long) \
240 : (((p)->flags & PF_SHORTINT) ? (unsigned long)(unsigned short)GETARG (int) \
241 : (unsigned long)GETARG (unsigned int)))
244 #ifdef HAVE_LONG_DOUBLE
245 #define GETLDOUBLE(p) GETARG (long double)
247 #define GETDOUBLE(p) GETARG (double)
249 #define SET_SIZE_FLAGS(p, type) \
250 if (sizeof (type) > sizeof (int)) \
251 (p)->flags |= PF_LONGINT; \
252 if (sizeof (type) > sizeof (long)) \
253 (p)->flags |= PF_LONGLONG;
255 /* this struct holds everything we need */
259 char *base; /* needed for [v]asprintf */
267 int width, precision;
271 /* the floating point stuff */
272 #ifdef FLOATING_POINT
273 static double pow_10 __P((int));
274 static int log_10 __P((double));
275 static double integral __P((double, double *));
276 static char *numtoa __P((double, int, int, char **));
279 static void init_data __P((struct DATA *, char *, size_t, const char *, int));
280 static void init_conv_flag __P((struct DATA *));
283 #ifdef FLOATING_POINT
284 static void floating __P((struct DATA *, double));
285 static void exponent __P((struct DATA *, double));
287 static void number __P((struct DATA *, unsigned long, int));
288 #ifdef HAVE_LONG_LONG
289 static void lnumber __P((struct DATA *, unsigned long long, int));
291 static void pointer __P((struct DATA *, unsigned long));
292 static void strings __P((struct DATA *, char *));
294 #ifdef FLOATING_POINT
295 # define FALLBACK_FMTSIZE 32
296 # define FALLBACK_BASE 4096
297 # define LFALLBACK_BASE 5120
298 # ifdef HAVE_LONG_DOUBLE
299 static void ldfallback __P((struct DATA *, const char *, const char *, long double));
301 static void dfallback __P((struct DATA *, const char *, const char *, double));
304 static char *groupnum __P((char *));
306 #ifndef HAVE_ISINF_IN_LIBC
307 static int isinf __P((double));
309 #ifndef HAVE_ISNAN_IN_LIBC
310 static int isnan __P((double));
314 static void memory_error_and_abort ();
315 static void *xmalloc __P((size_t));
316 static void *xrealloc __P((void *, size_t));
317 static void xfree __P((void *));
319 # include <xmalloc.h>
322 /* those are defines specific to snprintf to hopefully
323 * make the code clearer :-)
331 /* round off to the precision */
332 #define ROUND(d, p) \
334 d - pow_10(-(p)->precision) * 0.5 : \
335 d + pow_10(-(p)->precision) * 0.5
337 /* set default precision */
338 #define DEF_PREC(p) \
339 if ((p)->precision == NOT_FOUND) \
342 /* put a char. increment the number of chars written even if we've exceeded
343 the vsnprintf/snprintf buffer size (for the return value) */
344 #define PUT_CHAR(c, p) \
347 if (((p)->flags & PF_ALLOCBUF) && ((p)->counter >= (p)->length - 1)) \
349 (p)->length += ASBUFSIZE; \
350 (p)->base = (char *)xrealloc((p)->base, (p)->length); \
351 (p)->holder = (p)->base + (p)->counter; /* in case reallocated */ \
353 if ((p)->counter < (p)->length) \
354 *(p)->holder++ = (c); \
359 /* Output a string. P->WIDTH has already been adjusted for padding. */
360 #define PUT_STRING(string, len, p) \
364 while ((len)-- > 0) \
366 PUT_CHAR (*(string), (p)); \
373 #define PUT_PLUS(d, p, zero) \
374 if ((d) > zero && (p)->justify == RIGHT) \
377 #define PUT_SPACE(d, p, zero) \
378 if (((p)->flags & PF_SPACE) && (d) > zero) \
382 #define PAD_RIGHT(p) \
383 if ((p)->width > 0 && (p)->justify != LEFT) \
384 for (; (p)->width > 0; (p)->width--) \
385 PUT_CHAR((p)->pad, p)
388 #define PAD_LEFT(p) \
389 if ((p)->width > 0 && (p)->justify == LEFT) \
390 for (; (p)->width > 0; (p)->width--) \
391 PUT_CHAR((p)->pad, p)
393 /* pad with zeros from decimal precision */
394 #define PAD_ZERO(p) \
395 if ((p)->precision > 0) \
396 for (; (p)->precision > 0; (p)->precision--) \
399 /* if width and prec. in the args */
400 #define STAR_ARGS(p) \
402 if ((p)->flags & PF_STAR_W) \
404 (p)->width = GETARG (int); \
405 if ((p)->width < 0) \
407 (p)->flags |= PF_LADJUST; \
408 (p)->justify = LEFT; \
409 (p)->width = -(p)->width; \
412 if ((p)->flags & PF_STAR_P) \
414 (p)->precision = GETARG (int); \
415 if ((p)->precision < 0) \
417 (p)->flags &= ~PF_STAR_P; \
418 (p)->precision = NOT_FOUND; \
423 #if defined (HAVE_LOCALE_H) && defined (HAVE_LOCALECONV)
424 # define GETLOCALEDATA(d, t, g) \
429 (d) = '.'; (t) = -1; (g) = 0; /* defaults */ \
433 if (lv->decimal_point && lv->decimal_point[0]) \
434 (d) = lv->decimal_point[0]; \
435 if (lv->thousands_sep && lv->thousands_sep[0]) \
436 (t) = lv->thousands_sep[0]; \
437 (g) = lv->grouping ? lv->grouping : ""; \
438 if (*(g) == '\0' || *(g) == CHAR_MAX || (t) == -1) (g) = 0; \
444 # define GETLOCALEDATA(d, t, g) \
445 ( (d) = '.', (t) = ',', g = "\003" )
448 #ifdef FLOATING_POINT
450 * Find the nth power of 10
458 /* handle common cases with fast switch statement. */
461 case -3: return .001;
467 case 3: return 1000.;
473 for (n += 4; n < 0; n++)
479 for (n -= 4; n > 0; n--)
487 * Find the integral part of the log in base 10
488 * Note: this not a real log10()
489 I just need and approximation(integerpart) of x in:
494 * NOTE: do not call this with r == 0 -- an infinite loop results.
527 * This function return the fraction part of a double
528 * and set in ip the integral part.
529 * In many ways it resemble the modf() found on most Un*x
538 double real_integral = 0.;
540 /* take care of the obvious */
541 /* equal to zero ? */
548 /* negative number ? */
559 /* the real work :-) */
560 for (j = log_10(real); j >= 0; j--)
563 s = (real - real_integral)/p;
567 real_integral += i*p;
570 return (real - real_integral);
573 #define PRECISION 1.e-6
575 * return an ascii representation of the integral part of the number
576 * and set fract to be an ascii representation of the fraction part
577 * the container for the fraction and the integral part or staticly
578 * declare with fix size
581 numtoa(number, base, precision, fract)
587 double ip, fp; /* integer and fraction part */
589 int digits = MAX_INT - 1;
590 static char integral_part[MAX_INT];
591 static char fraction_part[MAX_FRACT];
595 /* taking care of the obvious case: 0.0 */
598 integral_part[0] = '0';
599 integral_part[1] = '\0';
600 /* The fractional part has to take the precision into account */
601 for (ch = 0; ch < precision-1; ch++)
602 fraction_part[ch] = '0';
603 fraction_part[ch] = '0';
604 fraction_part[ch+1] = '\0';
606 *fract = fraction_part;
607 return integral_part;
610 /* for negative numbers */
611 if ((sign = number) < 0.)
614 digits--; /* sign consume one digit */
617 fraction = integral(number, &ip);
620 /* do the integral part */
623 integral_part[0] = '0';
628 for ( i = 0; i < digits && number != 0.; ++i)
631 fp = integral(number, &ip);
632 ch = (int)((fp + PRECISION)*base); /* force to round */
633 integral_part[i] = (ch <= 9) ? ch + '0' : ch + 'a' - 10;
634 if (! ISXDIGIT((unsigned char)integral_part[i]))
635 break; /* bail out overflow !! */
640 /* Oh No !! out of bound, ho well fill it up ! */
642 for (i = 0; i < digits; ++i)
643 integral_part[i] = '9';
647 integral_part[i++] = '-';
649 integral_part[i] = '\0';
651 /* reverse every thing */
652 for ( i--, j = 0; j < i; j++, i--)
653 SWAP_INT(integral_part[i], integral_part[j]);
655 /* the fractional part */
656 for (i=0, fp=fraction; precision > 0 && i < MAX_FRACT ; i++, precision--)
658 fraction_part[i] = (int)((fp + PRECISION)*10. + '0');
659 if (! DIGIT(fraction_part[i])) /* underflow ? */
661 fp = (fp*10.0) - (double)(long)((fp + PRECISION)*10.);
663 fraction_part[i] = '\0';
665 if (fract != (char **)0)
666 *fract = fraction_part;
668 return integral_part;
672 /* for %d and friends, it puts in holder
673 * the representation with the right padding
685 /* An explicit precision turns off the zero-padding flag. */
686 if ((p->flags & PF_ZEROPAD) && p->precision >= 0 && (p->flags & PF_DOT))
687 p->flags &= ~PF_ZEROPAD;
689 sd = d; /* signed for ' ' padding in base 10 */
691 flags = (*p->pf == 'x' || *p->pf == 'X' || *p->pf == 'o' || *p->pf == 'u' || *p->pf == 'U') ? FL_UNSIGNED : 0;
693 flags |= FL_HEXUPPER;
695 tmp = fmtulong (d, base, intbuf, sizeof(intbuf), flags);
697 if ((p->flags & PF_THOUSANDS))
699 GETLOCALEDATA(decpoint, thoussep, grouping);
700 if (grouping && (t = groupnum (tmp)))
704 p->width -= strlen(tmp);
707 if ((p->flags & PF_DOT) && p->precision > 0)
709 p->precision -= strlen(tmp);
720 if (p->flags & PF_ALTFORM)
724 if (p->flags & PF_ALTFORM)
742 #ifdef HAVE_LONG_LONG
744 * identical to number() but works for `long long'
749 unsigned long long d;
756 /* An explicit precision turns off the zero-padding flag. */
757 if ((p->flags & PF_ZEROPAD) && p->precision >= 0 && (p->flags & PF_DOT))
758 p->flags &= ~PF_ZEROPAD;
760 sd = d; /* signed for ' ' padding in base 10 */
761 flags = (*p->pf == 'x' || *p->pf == 'X' || *p->pf == 'o' || *p->pf == 'u' || *p->pf == 'U') ? FL_UNSIGNED : 0;
763 flags |= FL_HEXUPPER;
765 tmp = fmtullong (d, base, intbuf, sizeof(intbuf), flags);
767 if ((p->flags & PF_THOUSANDS))
769 GETLOCALEDATA(decpoint, thoussep, grouping);
770 if (grouping && (t = groupnum (tmp)))
774 p->width -= strlen(tmp);
777 if ((p->flags & PF_DOT) && p->precision > 0)
779 p->precision -= strlen(tmp);
790 if (p->flags & PF_ALTFORM)
794 if (p->flags & PF_ALTFORM)
820 tmp = fmtulong(d, 16, intbuf, sizeof(intbuf), 0);
821 p->width -= strlen(tmp);
824 /* prefix '0x' for pointers */
846 if (p->precision != NOT_FOUND) /* the smallest number */
847 len = (len < p->precision ? len : p->precision);
850 PUT_STRING (tmp, len, p);
854 /* %ls wide-character strings */
865 memset (&mbs, '\0', sizeof (mbstate_t));
866 ws = (const wchar_t *)tmp;
869 if (p->precision != NOT_FOUND)
871 os = (char *)xmalloc (p->precision + 1);
872 len = wcsrtombs (os, &ws, p->precision, &mbs);
876 len = wcsrtombs (NULL, &ws, 0, &mbs);
877 if (len != (size_t)-1)
879 memset (&mbs, '\0', sizeof (mbstate_t));
880 os = (char *)xmalloc (len + 1);
881 (void)wcsrtombs (os, &ws, len + 1, &mbs);
884 if (len == (size_t)-1)
886 /* invalid multibyte sequence; bail now. */
892 PUT_STRING (os, len, p);
905 lbuf = (char *)malloc (MB_CUR_MAX+1);
908 memset (&mbs, '\0', sizeof (mbstate_t));
909 len = wcrtomb (lbuf, wc, &mbs);
910 if (len == (size_t)-1)
911 /* conversion failed; bail now. */
915 PUT_STRING (l, len, p);
918 #endif /* HANDLE_MULTIBYTE */
920 #ifdef FLOATING_POINT
922 #ifndef HAVE_ISINF_IN_LIBC
923 /* Half-assed versions, since we don't want to link with libm. */
931 else if (d > DBL_MAX)
939 #ifndef HAVE_ISNAN_IN_LIBC
948 /* Check for [+-]infinity and NaN. If MODE == 1, we check for Infinity, else
949 (mode == 2) we check for NaN. This does the necessary printing. Returns
950 1 if Inf or Nan, 0 if not. */
952 chkinfnan(p, d, mode)
955 int mode; /* == 1 for inf, == 2 for nan */
961 i = (mode == 1) ? isinf(d) : isnan(d);
964 big = (mode == 1) ? "INF" : "NAN";
965 small = (mode == 1) ? "inf" : "nan";
967 tmp = (*p->pf == 'F' || *p->pf == 'G' || *p->pf == 'E') ? big : small;
981 /* %f %F %g %G floating point representation */
987 char *tmp, *tmp2, *t;
990 if (d != 0 && (chkinfnan(p, d, 1) || chkinfnan(p, d, 2)))
991 return; /* already printed nan or inf */
993 GETLOCALEDATA(decpoint, thoussep, grouping);
996 tmp = dtoa(d, p->precision, &tmp2);
998 if ((p->flags & PF_THOUSANDS) && grouping && (t = groupnum (tmp)))
1001 if ((*p->pf == 'g' || *p->pf == 'G') && (p->flags & PF_ALTFORM) == 0)
1003 /* smash the trailing zeros unless altform */
1004 for (i = strlen(tmp2) - 1; i >= 0 && tmp2[i] == '0'; i--)
1006 if (tmp2[0] == '\0')
1010 /* calculate the padding. 1 for the dot */
1011 p->width = p->width -
1012 ((d > 0. && p->justify == RIGHT) ? 1:0) -
1013 ((p->flags & PF_SPACE) ? 1:0) -
1014 strlen(tmp) - p->precision -
1015 ((p->precision != 0 || (p->flags & PF_ALTFORM)) ? 1 : 0); /* radix char */
1018 PUT_SPACE(d, p, 0.);
1022 PUT_CHAR(*tmp, p); /* the integral */
1027 if (p->precision != 0 || (p->flags & PF_ALTFORM))
1028 PUT_CHAR(decpoint, p); /* put the '.' */
1030 for (; *tmp2; tmp2++)
1031 PUT_CHAR(*tmp2, p); /* the fraction */
1036 /* %e %E %g %G exponent representation */
1045 if (d != 0 && (chkinfnan(p, d, 1) || chkinfnan(p, d, 2)))
1046 return; /* already printed nan or inf */
1048 GETLOCALEDATA(decpoint, thoussep, grouping);
1055 d = d / pow_10(j); /* get the Mantissa */
1058 tmp = dtoa(d, p->precision, &tmp2);
1060 /* 1 for unit, 1 for the '.', 1 for 'e|E',
1061 * 1 for '+|-', 2 for 'exp' */
1062 /* calculate how much padding need */
1063 p->width = p->width -
1064 ((d > 0. && p->justify == RIGHT) ? 1:0) -
1065 ((p->flags & PF_SPACE) ? 1:0) - p->precision - 6;
1069 PUT_SPACE(d, p, 0.);
1077 if (p->precision != 0 || (p->flags & PF_ALTFORM))
1078 PUT_CHAR(decpoint, p); /* the '.' */
1080 if ((*p->pf == 'g' || *p->pf == 'G') && (p->flags & PF_ALTFORM) == 0)
1081 /* smash the trailing zeros unless altform */
1082 for (i = strlen(tmp2) - 1; i >= 0 && tmp2[i] == '0'; i--)
1085 for (; *tmp2; tmp2++)
1086 PUT_CHAR(*tmp2, p); /* the fraction */
1088 /* the exponent put the 'e|E' */
1089 if (*p->pf == 'g' || *p->pf == 'e')
1094 /* the sign of the exp */
1104 /* pad out to at least two spaces. pad with `0' if the exponent is a
1120 /* Return a new string with the digits in S grouped according to the locale's
1121 grouping info and thousands separator. If no grouping should be performed,
1122 this returns NULL; the caller needs to check for it. */
1127 char *se, *ret, *re, *g;
1130 if (grouping == 0 || *grouping <= 0 || *grouping == CHAR_MAX)
1131 return ((char *)NULL);
1133 /* find min grouping to size returned string */
1134 for (len = *grouping, g = grouping; *g; g++)
1135 if (*g > 0 && *g < len)
1139 len = slen / len + 1;
1140 ret = (char *)xmalloc (slen + len + 1);
1141 re = ret + slen + len;
1152 /* handle `-' inserted by numtoa() and the fmtu* family here. */
1153 if (se > s && se[-1] == '-')
1156 /* begin new group. */
1157 if (--len == 0 && se > s)
1160 len = *++g; /* was g++, but that uses first char twice (glibc bug, too) */
1162 len = *--g; /* use previous grouping */
1163 else if (*g == CHAR_MAX)
1175 memmove (ret, re, strlen (re) + 1);
1183 /* initialize the conversion specifiers */
1188 p->flags &= PF_ALLOCBUF; /* preserve PF_ALLOCBUF flag */
1189 p->precision = p->width = NOT_FOUND;
1190 p->justify = NOT_FOUND;
1195 init_data (p, string, length, format, mode)
1202 p->length = length - 1; /* leave room for '\0' */
1203 p->holder = p->base = string;
1206 p->flags = (mode == PFM_AS) ? PF_ALLOCBUF : 0;
1210 #if defined (__STDC__)
1211 vsnprintf_internal(struct DATA *data, char *string, size_t length, const char *format, va_list args)
1213 vsnprintf_internal(data, string, length, format, args)
1221 double d; /* temporary holder */
1222 #ifdef HAVE_LONG_DOUBLE
1223 long double ld; /* for later */
1226 #ifdef HAVE_LONG_LONG
1227 unsigned long long ull;
1231 #if HANDLE_MULTIBYTE
1235 const char *convstart;
1238 /* Sanity check, the string length must be >= 0. C99 actually says that
1239 LENGTH can be zero here, in the case of snprintf/vsnprintf (it's never
1240 0 in the case of asprintf/vasprintf), and the return value is the number
1241 of characters that would have been written. */
1248 /* Reset these for each call because the locale might have changed. */
1249 decpoint = thoussep = 0;
1253 for (; c = *(data->pf); data->pf++)
1261 convstart = data->pf;
1262 init_conv_flag (data); /* initialise format flags */
1265 for (state = 1; state && *data->pf; )
1268 /* fmtend = data->pf */
1269 #if defined (FLOATING_POINT) && defined (HAVE_LONG_DOUBLE)
1270 if (data->flags & PF_LONGDBL)
1277 # ifdef HAVE_PRINTF_A_FORMAT
1281 ld = GETLDOUBLE (data);
1282 ldfallback (data, convstart, data->pf, ld);
1286 #endif /* FLOATING_POINT && HAVE_LONG_DOUBLE */
1290 /* Parse format flags */
1291 case '\0': /* a NULL here ? ? bail out */
1292 *data->holder = '\0';
1293 return data->counter;
1296 data->flags |= PF_ALTFORM;
1299 data->flags |= PF_ZEROPAD;
1303 if (data->flags & PF_DOT)
1304 data->flags |= PF_STAR_P;
1306 data->flags |= PF_STAR_W;
1309 if ((data->flags & PF_DOT) == 0)
1311 data->flags |= PF_LADJUST;
1312 data->justify = LEFT;
1318 if ((data->flags & PF_PLUS) == 0)
1319 data->flags |= PF_SPACE;
1322 if ((data->flags & PF_DOT) == 0)
1324 data->flags |= PF_PLUS;
1325 data->justify = RIGHT;
1329 data->flags |= PF_THOUSANDS;
1332 case '1': case '2': case '3':
1333 case '4': case '5': case '6':
1334 case '7': case '8': case '9':
1338 n = n * 10 + TODIGIT(c);
1342 data->pf--; /* went too far */
1345 if (data->flags & PF_DOT)
1346 data->precision = negprec ? NOT_FOUND : n;
1351 /* optional precision */
1353 data->flags |= PF_DOT;
1354 data->precision = 0;
1357 /* length modifiers */
1359 data->flags |= (data->flags & PF_SHORTINT) ? PF_SIGNEDCHAR : PF_SHORTINT;
1362 data->flags |= (data->flags & PF_LONGINT) ? PF_LONGLONG : PF_LONGINT;
1365 data->flags |= PF_LONGDBL;
1368 data->flags |= PF_LONGLONG;
1371 data->flags |= PF_INTMAX_T;
1372 SET_SIZE_FLAGS(data, intmax_t);
1375 data->flags |= PF_SIZE_T;
1376 SET_SIZE_FLAGS(data, size_t);
1379 data->flags |= PF_PTRDIFF_T;
1380 SET_SIZE_FLAGS(data, ptrdiff_t);
1383 /* Conversion specifiers */
1384 #ifdef FLOATING_POINT
1385 case 'f': /* float, double */
1388 d = GETDOUBLE(data);
1397 d = GETDOUBLE(data);
1398 i = (d != 0.) ? log_10(d) : -1;
1400 * for '%g|%G' ANSI: use f if exponent
1401 * is in the range or [-4,p] exclusively
1404 if (-4 < i && i < data->precision)
1406 /* reset precision */
1407 data->precision -= i + 1;
1412 /* reduce precision by 1 because of leading digit before
1413 decimal point in e format. */
1420 case 'E': /* Exponent double */
1422 d = GETDOUBLE(data);
1426 # ifdef HAVE_PRINTF_A_FORMAT
1430 d = GETDOUBLE(data);
1431 dfallback(data, convstart, data->pf, d);
1434 # endif /* HAVE_PRINTF_A_FORMAT */
1435 #endif /* FLOATING_POINT */
1437 data->flags |= PF_LONGINT;
1441 #ifdef HAVE_LONG_LONG
1442 if (data->flags & PF_LONGLONG)
1444 ull = GETARG (unsigned long long);
1445 lnumber(data, ull, 10);
1450 ul = GETUNSIGNED(data);
1451 number(data, ul, 10);
1456 data->flags |= PF_LONGINT;
1458 case 'd': /* decimal */
1461 #ifdef HAVE_LONG_LONG
1462 if (data->flags & PF_LONGLONG)
1464 ull = GETARG (long long);
1465 lnumber(data, ull, 10);
1470 ul = GETSIGNED(data);
1471 number(data, ul, 10);
1475 case 'o': /* octal */
1477 #ifdef HAVE_LONG_LONG
1478 if (data->flags & PF_LONGLONG)
1480 ull = GETARG (unsigned long long);
1481 lnumber(data, ull, 8);
1486 ul = GETUNSIGNED(data);
1487 number(data, ul, 8);
1492 case 'X': /* hexadecimal */
1494 #ifdef HAVE_LONG_LONG
1495 if (data->flags & PF_LONGLONG)
1497 ull = GETARG (unsigned long long);
1498 lnumber(data, ull, 16);
1503 ul = GETUNSIGNED(data);
1504 number(data, ul, 16);
1510 ul = (unsigned long)GETARG (void *);
1514 #if HANDLE_MULTIBYTE
1516 data->flags |= PF_LONGINT;
1519 case 'c': /* character */
1521 #if HANDLE_MULTIBYTE
1522 if (data->flags & PF_LONGINT)
1524 wc = GETARG (wint_t);
1535 #if HANDLE_MULTIBYTE
1537 data->flags |= PF_LONGINT;
1540 case 's': /* string */
1542 #if HANDLE_MULTIBYTE
1543 if (data->flags & PF_LONGINT)
1545 ws = GETARG (wchar_t *);
1546 wstrings (data, ws);
1551 s = GETARG (char *);
1557 #ifdef HAVE_LONG_LONG
1558 if (data->flags & PF_LONGLONG)
1559 *(GETARG (long long *)) = data->counter;
1562 if (data->flags & PF_LONGINT)
1563 *(GETARG (long *)) = data->counter;
1564 else if (data->flags & PF_SHORTINT)
1565 *(GETARG (short *)) = data->counter;
1567 *(GETARG (int *)) = data->counter;
1570 case '%': /* nothing just % */
1571 PUT_CHAR('%', data);
1575 /* is this an error ? maybe bail out */
1579 } /* end of `%' for loop */
1580 } /* end of format string for loop */
1582 if (data->length >= 0)
1583 *data->holder = '\0'; /* the end ye ! */
1585 return data->counter;
1588 #if defined (FLOATING_POINT) && defined (HAVE_LONG_DOUBLE)
1590 * Printing floating point numbers accurately is an art. I'm not good
1591 * at it. Fall back to sprintf for long double formats.
1594 ldfallback (data, fs, fe, ld)
1596 const char *fs, *fe;
1600 char fmtbuf[FALLBACK_FMTSIZE], *obuf;
1603 fl = LFALLBACK_BASE + (data->precision < 6 ? 6 : data->precision) + 2;
1604 obuf = (char *)xmalloc (fl);
1606 strncpy (fmtbuf, fs, fl);
1609 if ((data->flags & PF_STAR_W) && (data->flags & PF_STAR_P))
1610 sprintf (obuf, fmtbuf, data->width, data->precision, ld);
1611 else if (data->flags & PF_STAR_W)
1612 sprintf (obuf, fmtbuf, data->width, ld);
1613 else if (data->flags & PF_STAR_P)
1614 sprintf (obuf, fmtbuf, data->precision, ld);
1616 sprintf (obuf, fmtbuf, ld);
1618 for (x = obuf; *x; x++)
1619 PUT_CHAR (*x, data);
1622 #endif /* FLOATING_POINT && HAVE_LONG_DOUBLE */
1624 #ifdef FLOATING_POINT
1625 /* Used for %a, %A if the libc printf supports them. */
1627 dfallback (data, fs, fe, d)
1629 const char *fs, *fe;
1633 char fmtbuf[FALLBACK_FMTSIZE], obuf[FALLBACK_BASE];
1637 strncpy (fmtbuf, fs, fl);
1640 if ((data->flags & PF_STAR_W) && (data->flags & PF_STAR_P))
1641 sprintf (obuf, fmtbuf, data->width, data->precision, d);
1642 else if (data->flags & PF_STAR_W)
1643 sprintf (obuf, fmtbuf, data->width, d);
1644 else if (data->flags & PF_STAR_P)
1645 sprintf (obuf, fmtbuf, data->precision, d);
1647 sprintf (obuf, fmtbuf, d);
1649 for (x = obuf; *x; x++)
1650 PUT_CHAR (*x, data);
1652 #endif /* FLOATING_POINT */
1657 #if defined (__STDC__)
1658 vsnprintf(char *string, size_t length, const char *format, va_list args)
1660 vsnprintf(string, length, format, args)
1669 if (string == 0 && length != 0)
1671 init_data (&data, string, length, format, PFM_SN);
1672 return (vsnprintf_internal(&data, string, length, format, args));
1676 #if defined(PREFER_STDARG)
1677 snprintf(char *string, size_t length, const char * format, ...)
1679 snprintf(string, length, format, va_alist)
1690 SH_VA_START(args, format);
1692 if (string == 0 && length != 0)
1694 init_data (&data, string, length, format, PFM_SN);
1695 rval = vsnprintf_internal (&data, string, length, format, args);
1702 #endif /* HAVE_SNPRINTF */
1707 #if defined (__STDC__)
1708 vasprintf(char **stringp, const char *format, va_list args)
1710 vasprintf(stringp, format, args)
1720 string = (char *)xmalloc(ASBUFSIZE);
1721 init_data (&data, string, ASBUFSIZE, format, PFM_AS);
1722 r = vsnprintf_internal(&data, string, ASBUFSIZE, format, args);
1723 *stringp = data.base; /* not string in case reallocated */
1728 #if defined(PREFER_STDARG)
1729 asprintf(char **stringp, const char * format, ...)
1731 asprintf(stringp, format, va_alist)
1740 SH_VA_START(args, format);
1742 rval = vasprintf (stringp, format, args);
1749 #endif /* !HAVE_ASPRINTF */
1751 #endif /* !HAVE_SNPRINTF || !HAVE_ASPRINTF */
1756 memory_error_and_abort ()
1758 write (2, "out of virtual memory\n", 22);
1768 ret = malloc(bytes);
1770 memory_error_and_abort ();
1775 xrealloc (pointer, bytes)
1781 ret = pointer ? realloc(pointer, bytes) : malloc(bytes);
1783 memory_error_and_abort ();
1795 /* set of small tests for snprintf() */
1802 #ifdef HAVE_LOCALE_H
1803 setlocale(LC_ALL, "");
1807 si = snprintf((char *)NULL, 0, "abcde\n");
1808 printf("snprintf returns %d with NULL first argument and size of 0\n", si);
1809 si = snprintf(holder, 0, "abcde\n");
1810 printf("snprintf returns %d with non-NULL first argument and size of 0\n", si);
1811 si = snprintf((char *)NULL, 16, "abcde\n");
1812 printf("snprintf returns %d with NULL first argument and non-zero size\n", si);
1815 printf("Suite of test for snprintf:\n");
1816 printf("a_format\n");
1817 printf("printf() format\n");
1818 printf("snprintf() format\n\n");
1820 /* Checking the field widths */
1822 printf("/%%ld %%ld/, 336, 336\n");
1823 snprintf(holder, sizeof holder, "/%ld %ld/\n", 336, 336);
1824 asprintf(&h, "/%ld %ld/\n", 336, 336);
1825 printf("/%ld %ld/\n", 336, 336);
1826 printf("%s", holder);
1829 printf("/%%d/, 336\n");
1830 snprintf(holder, sizeof holder, "/%d/\n", 336);
1831 asprintf(&h, "/%d/\n", 336);
1832 printf("/%d/\n", 336);
1833 printf("%s", holder);
1836 printf("/%%2d/, 336\n");
1837 snprintf(holder, sizeof holder, "/%2d/\n", 336);
1838 asprintf(&h, "/%2d/\n", 336);
1839 printf("/%2d/\n", 336);
1840 printf("%s", holder);
1843 printf("/%%10d/, 336\n");
1844 snprintf(holder, sizeof holder, "/%10d/\n", 336);
1845 asprintf(&h, "/%10d/\n", 336);
1846 printf("/%10d/\n", 336);
1847 printf("%s", holder);
1850 printf("/%%-10d/, 336\n");
1851 snprintf(holder, sizeof holder, "/%-10d/\n", 336);
1852 asprintf(&h, "/%-10d/\n", 336);
1853 printf("/%-10d/\n", 336);
1854 printf("%s", holder);
1858 /* floating points */
1860 printf("/%%f/, 1234.56\n");
1861 snprintf(holder, sizeof holder, "/%f/\n", 1234.56);
1862 asprintf(&h, "/%f/\n", 1234.56);
1863 printf("/%f/\n", 1234.56);
1864 printf("%s", holder);
1867 printf("/%%e/, 1234.56\n");
1868 snprintf(holder, sizeof holder, "/%e/\n", 1234.56);
1869 asprintf(&h, "/%e/\n", 1234.56);
1870 printf("/%e/\n", 1234.56);
1871 printf("%s", holder);
1874 printf("/%%4.2f/, 1234.56\n");
1875 snprintf(holder, sizeof holder, "/%4.2f/\n", 1234.56);
1876 asprintf(&h, "/%4.2f/\n", 1234.56);
1877 printf("/%4.2f/\n", 1234.56);
1878 printf("%s", holder);
1881 printf("/%%3.1f/, 1234.56\n");
1882 snprintf(holder, sizeof holder, "/%3.1f/\n", 1234.56);
1883 asprintf(&h, "/%3.1f/\n", 1234.56);
1884 printf("/%3.1f/\n", 1234.56);
1885 printf("%s", holder);
1888 printf("/%%10.3f/, 1234.56\n");
1889 snprintf(holder, sizeof holder, "/%10.3f/\n", 1234.56);
1890 asprintf(&h, "/%10.3f/\n", 1234.56);
1891 printf("/%10.3f/\n", 1234.56);
1892 printf("%s", holder);
1895 printf("/%%10.3e/, 1234.56\n");
1896 snprintf(holder, sizeof holder, "/%10.3e/\n", 1234.56);
1897 asprintf(&h, "/%10.3e/\n", 1234.56);
1898 printf("/%10.3e/\n", 1234.56);
1899 printf("%s", holder);
1902 printf("/%%+4.2f/, 1234.56\n");
1903 snprintf(holder, sizeof holder, "/%+4.2f/\n", 1234.56);
1904 asprintf(&h, "/%+4.2f/\n", 1234.56);
1905 printf("/%+4.2f/\n", 1234.56);
1906 printf("%s", holder);
1909 printf("/%%010.2f/, 1234.56\n");
1910 snprintf(holder, sizeof holder, "/%010.2f/\n", 1234.56);
1911 asprintf(&h, "/%010.2f/\n", 1234.56);
1912 printf("/%010.2f/\n", 1234.56);
1913 printf("%s", holder);
1916 #define BLURB "Outstanding acting !"
1917 /* strings precisions */
1919 printf("/%%2s/, \"%s\"\n", BLURB);
1920 snprintf(holder, sizeof holder, "/%2s/\n", BLURB);
1921 asprintf(&h, "/%2s/\n", BLURB);
1922 printf("/%2s/\n", BLURB);
1923 printf("%s", holder);
1926 printf("/%%22s/ %s\n", BLURB);
1927 snprintf(holder, sizeof holder, "/%22s/\n", BLURB);
1928 asprintf(&h, "/%22s/\n", BLURB);
1929 printf("/%22s/\n", BLURB);
1930 printf("%s", holder);
1933 printf("/%%22.5s/ %s\n", BLURB);
1934 snprintf(holder, sizeof holder, "/%22.5s/\n", BLURB);
1935 asprintf(&h, "/%22.5s/\n", BLURB);
1936 printf("/%22.5s/\n", BLURB);
1937 printf("%s", holder);
1940 printf("/%%-22.5s/ %s\n", BLURB);
1941 snprintf(holder, sizeof holder, "/%-22.5s/\n", BLURB);
1942 asprintf(&h, "/%-22.5s/\n", BLURB);
1943 printf("/%-22.5s/\n", BLURB);
1944 printf("%s", holder);
1947 /* see some flags */
1949 printf("%%x %%X %%#x, 31, 31, 31\n");
1950 snprintf(holder, sizeof holder, "%x %X %#x\n", 31, 31, 31);
1951 asprintf(&h, "%x %X %#x\n", 31, 31, 31);
1952 printf("%x %X %#x\n", 31, 31, 31);
1953 printf("%s", holder);
1956 printf("**%%d**%% d**%% d**, 42, 42, -42\n");
1957 snprintf(holder, sizeof holder, "**%d**% d**% d**\n", 42, 42, -42);
1958 asprintf(&h, "**%d**% d**% d**\n", 42, 42, -42);
1959 printf("**%d**% d**% d**\n", 42, 42, -42);
1960 printf("%s", holder);
1965 printf("/%%g/, 31.4\n");
1966 snprintf(holder, sizeof holder, "/%g/\n", 31.4);
1967 asprintf(&h, "/%g/\n", 31.4);
1968 printf("/%g/\n", 31.4);
1969 printf("%s", holder);
1972 printf("/%%.6g/, 31.4\n");
1973 snprintf(holder, sizeof holder, "/%.6g/\n", 31.4);
1974 asprintf(&h, "/%.6g/\n", 31.4);
1975 printf("/%.6g/\n", 31.4);
1976 printf("%s", holder);
1979 printf("/%%.1G/, 31.4\n");
1980 snprintf(holder, sizeof holder, "/%.1G/\n", 31.4);
1981 asprintf(&h, "/%.1G/\n", 31.4);
1982 printf("/%.1G/\n", 31.4);
1983 printf("%s", holder);
1986 printf("/%%.1G/, 3100000000.4\n");
1987 snprintf(holder, sizeof holder, "/%.1G/\n", 3100000000.4);
1988 asprintf(&h, "/%.1G/\n", 3100000000.4);
1989 printf("/%.1G/\n", 3100000000.4);
1990 printf("%s", holder);
1994 printf("abc%n", &i); printf("%d\n", i);
1995 snprintf(holder, sizeof holder, "abc%n", &i);
1996 printf("%s", holder); printf("%d\n\n", i);
1997 asprintf(&h, "abc%n", &i);
1998 printf("%s", h); printf("%d\n\n", i);
2000 printf("%%*.*s --> 10.10\n");
2001 snprintf(holder, sizeof holder, "%*.*s\n", 10, 10, BLURB);
2002 asprintf(&h, "%*.*s\n", 10, 10, BLURB);
2003 printf("%*.*s\n", 10, 10, BLURB);
2004 printf("%s", holder);
2007 printf("%%%%%%%%\n");
2008 snprintf(holder, sizeof holder, "%%%%\n");
2009 asprintf(&h, "%%%%\n");
2011 printf("%s", holder);
2014 #define BIG "Hello this is a too big string for the buffer"
2015 /* printf("A buffer to small of 10, trying to put this:\n");*/
2016 printf("<%%>, %s\n", BIG);
2017 i = snprintf(holder, 10, "%s\n", BIG);
2018 i = asprintf(&h, "%s", BIG);
2019 printf("<%s>\n", BIG);
2020 printf("<%s>\n", holder);
2021 printf("<%s>\n\n", h);
2023 printf ("<%%p> vsnprintf\n");
2024 i = snprintf(holder, 100, "%p", vsnprintf);
2025 i = asprintf(&h, "%p", vsnprintf);
2026 printf("<%p>\n", vsnprintf);
2027 printf("<%s>\n", holder);
2028 printf("<%s>\n\n", h);
2030 printf ("<%%lu> LONG_MAX+1\n");
2031 i = snprintf(holder, 100, "%lu", (unsigned long)(LONG_MAX)+1);
2032 i = asprintf(&h, "%lu", (unsigned long)(LONG_MAX)+1);
2033 printf("<%lu>\n", (unsigned long)(LONG_MAX)+1);
2034 printf("<%s>\n", holder);
2035 printf("<%s>\n\n", h);
2037 #ifdef HAVE_LONG_LONG
2038 printf ("<%%llu> LLONG_MAX+1\n");
2039 i = snprintf(holder, 100, "%llu", (unsigned long long)(LLONG_MAX)+1);
2040 i = asprintf(&h, "%llu", (unsigned long long)(LLONG_MAX)+1);
2041 printf("<%llu>\n", (unsigned long long)(LLONG_MAX)+1);
2042 printf("<%s>\n", holder);
2043 printf("<%s>\n\n", h);
2046 #ifdef HAVE_LONG_DOUBLE
2047 printf ("<%%6.2LE> 42.42\n");
2048 i = snprintf(holder, 100, "%6.2LE", (long double)42.42);
2049 i = asprintf(&h, "%6.2LE", (long double)42.42);
2050 printf ("<%6.2LE>\n", (long double)42.42);
2051 printf ("<%s>\n", holder);
2052 printf ("<%s>\n\n", h);
2055 #ifdef HAVE_PRINTF_A_FORMAT
2056 printf ("<%%6.2A> 42.42\n");
2057 i = snprintf(holder, 100, "%6.2A", 42.42);
2058 i = asprintf(&h, "%6.2A", 42.42);
2059 printf ("<%6.2A>\n", 42.42);
2060 printf ("<%s>\n", holder);
2061 printf ("<%s>\n\n", h);
2063 printf ("<%%6.2LA> 42.42\n");
2064 i = snprintf(holder, 100, "%6.2LA", (long double)42.42);
2065 i = asprintf(&h, "%6.2LA", (long double)42.42);
2066 printf ("<%6.2LA>\n", (long double)42.42);
2067 printf ("<%s>\n", holder);
2068 printf ("<%s>\n\n", h);
2071 printf ("<%%.10240f> DBL_MAX\n");
2072 si = snprintf(holder, 100, "%.10240f", DBL_MAX);
2073 ai = asprintf(&h, "%.10240f", DBL_MAX);
2074 printf ("<%.10240f>\n", DBL_MAX);
2075 printf ("<%d> <%s>\n", si, holder);
2076 printf ("<%d> <%s>\n\n", ai, h);
2078 printf ("<%%.10240Lf> LDBL_MAX\n");
2079 si = snprintf(holder, 100, "%.10240Lf", (long double)LDBL_MAX);
2080 ai = asprintf(&h, "%.10240Lf", (long double)LDBL_MAX);
2081 printf ("<%.10240Lf>\n", (long double)LDBL_MAX);
2082 printf ("<%d> <%s>\n", si, holder);
2083 printf ("<%d> <%s>\n\n", ai, h);
2086 printf("/%%g/, 421.2345\n");
2087 snprintf(holder, sizeof holder, "/%g/\n", 421.2345);
2088 asprintf(&h, "/%g/\n", 421.2345);
2089 printf("/%g/\n", 421.2345);
2090 printf("%s", holder);
2093 printf("/%%g/, 4214.2345\n");
2094 snprintf(holder, sizeof holder, "/%g/\n", 4214.2345);
2095 asprintf(&h, "/%g/\n", 4214.2345);
2096 printf("/%g/\n", 4214.2345);
2097 printf("%s", holder);
2100 printf("/%%.5g/, 4214.2345\n");
2101 snprintf(holder, sizeof holder, "/%.5g/\n", 4214.2345);
2102 asprintf(&h, "/%.5g/\n", 4214.2345);
2103 printf("/%.5g/\n", 4214.2345);
2104 printf("%s", holder);
2107 printf("/%%.4g/, 4214.2345\n");
2108 snprintf(holder, sizeof holder, "/%.4g/\n", 4214.2345);
2109 asprintf(&h, "/%.4g/\n", 4214.2345);
2110 printf("/%.4g/\n", 4214.2345);
2111 printf("%s", holder);
2114 printf("/%%'ld %%'ld/, 12345, 1234567\n");
2115 snprintf(holder, sizeof holder, "/%'ld %'ld/\n", 12345, 1234567);
2116 asprintf(&h, "/%'ld %'ld/\n", 12345, 1234567);
2117 printf("/%'ld %'ld/\n", 12345, 1234567);
2118 printf("%s", holder);
2121 printf("/%%'ld %%'ld/, 336, 3336\n");
2122 snprintf(holder, sizeof holder, "/%'ld %'ld/\n", 336, 3336);
2123 asprintf(&h, "/%'ld %'ld/\n", 336, 3336);
2124 printf("/%'ld %'ld/\n", 336, 3336);
2125 printf("%s", holder);
2128 printf("/%%'ld %%'ld/, -42786, -142786\n");
2129 snprintf(holder, sizeof holder, "/%'ld %'ld/\n", -42786, -142786);
2130 asprintf(&h, "/%'ld %'ld/\n", -42786, -142786);
2131 printf("/%'ld %'ld/\n", -42786, -142786);
2132 printf("%s", holder);
2135 printf("/%%'f %%'f/, 421.2345, 421234.56789\n");
2136 snprintf(holder, sizeof holder, "/%'f %'f/\n", 421.2345, 421234.56789);
2137 asprintf(&h, "/%'f %'f/\n", 421.2345, 421234.56789);
2138 printf("/%'f %'f/\n", 421.2345, 421234.56789);
2139 printf("%s", holder);
2142 printf("/%%'f %%'f/, -421.2345, -421234.56789\n");
2143 snprintf(holder, sizeof holder, "/%'f %'f/\n", -421.2345, -421234.56789);
2144 asprintf(&h, "/%'f %'f/\n", -421.2345, -421234.56789);
2145 printf("/%'f %'f/\n", -421.2345, -421234.56789);
2146 printf("%s", holder);
2149 printf("/%%'g %%'g/, 421.2345, 421234.56789\n");
2150 snprintf(holder, sizeof holder, "/%'g %'g/\n", 421.2345, 421234.56789);
2151 asprintf(&h, "/%'g %'g/\n", 421.2345, 421234.56789);
2152 printf("/%'g %'g/\n", 421.2345, 421234.56789);
2153 printf("%s", holder);
2156 printf("/%%'g %%'g/, -421.2345, -421234.56789\n");
2157 snprintf(holder, sizeof holder, "/%'g %'g/\n", -421.2345, -421234.56789);
2158 asprintf(&h, "/%'g %'g/\n", -421.2345, -421234.56789);
2159 printf("/%'g %'g/\n", -421.2345, -421234.56789);
2160 printf("%s", holder);
2164 printf("/%%'g/, 4213455.8392\n");
2165 snprintf(holder, sizeof holder, "/%'g/\n", 4213455.8392);
2166 asprintf(&h, "/%'g/\n", 4213455.8392);
2167 printf("/%'g/\n", 4213455.8392);
2168 printf("%s", holder);