2 build a test version with
3 gcc -g -DDRIVER -I../.. -I../../include -o test-snprintf snprintf.c fmtu*long.o
7 Unix snprintf implementation.
8 derived from inetutils/libinetutils/snprintf.c Version 1.1
10 Copyright (C) 2001 Free Software Foundation, Inc.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General License for more details.
22 You should have received a copy of the GNU General License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * added changes from Miles Bader
30 * corrected a bug with %f
31 * added support for %#g
32 * added more comments :-)
34 * supporting must ANSI syntaxic_sugars
38 THANKS(for the patches and ideas):
47 * Currently doesn't handle (and bash/readline doesn't use):
48 * * *M$ width, precision specifications
49 * * %N$ numbered argument conversions
50 * * inf, nan floating values imperfect (if isinf(), isnan() not in libc)
51 * * support for `F' is imperfect with ldfallback(), since underlying
52 * printf may not handle it -- should ideally have another autoconf test
55 #define FLOATING_POINT
61 #if defined(DRIVER) && !defined(HAVE_CONFIG_H)
62 #define HAVE_LONG_LONG
63 #define HAVE_LONG_DOUBLE
65 #define HAVE_PRINTF_A_FORMAT
67 #define HAVE_ISINF_IN_LIBC
68 #define HAVE_ISNAN_IN_LIBC
70 #define HAVE_STRINGIZE
77 #if !defined (HAVE_SNPRINTF) || !defined (HAVE_ASPRINTF)
79 #include <bashtypes.h>
81 #if defined(PREFER_STDARG)
94 #include <chartypes.h>
100 #ifdef FLOATING_POINT
101 # include <float.h> /* for manifest constants */
102 # include <stdio.h> /* for sprintf */
112 #include <shmbutil.h>
117 # define FL_PREFIX 0x01 /* add 0x, 0X, or 0 prefix as appropriate */
118 # define FL_ADDBASE 0x02 /* add base# prefix to converted value */
119 # define FL_HEXUPPER 0x04 /* use uppercase when converting to hex */
120 # define FL_UNSIGNED 0x08 /* don't add any sign */
121 extern char *fmtulong __P((unsigned long int, int, char *, size_t, int));
122 extern char *fmtullong __P((unsigned long long int, int, char *, size_t, int));
126 # define FREE(x) if (x) free (x)
129 /* Bound on length of the string representing an integer value of type T.
130 Subtract one for the sign bit if T is signed;
131 302 / 1000 is log10 (2) rounded up;
132 add one for integer division truncation;
133 add one more for a minus sign if t is signed. */
134 #define INT_STRLEN_BOUND(t) \
135 ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
136 + 1 + TYPE_SIGNED (t))
138 /* conversion flags */
139 #define PF_ALTFORM 0x00001 /* # */
140 #define PF_HEXPREFIX 0x00002 /* 0[Xx] */
141 #define PF_LADJUST 0x00004 /* - */
142 #define PF_ZEROPAD 0x00008 /* 0 */
143 #define PF_PLUS 0x00010 /* + */
144 #define PF_SPACE 0x00020 /* ' ' */
145 #define PF_THOUSANDS 0x00040 /* ' */
147 #define PF_DOT 0x00080 /* `.precision' */
148 #define PF_STAR_P 0x00100 /* `*' after precision */
149 #define PF_STAR_W 0x00200 /* `*' before or without precision */
151 /* length modifiers */
152 #define PF_SIGNEDCHAR 0x00400 /* hh */
153 #define PF_SHORTINT 0x00800 /* h */
154 #define PF_LONGINT 0x01000 /* l */
155 #define PF_LONGLONG 0x02000 /* ll */
156 #define PF_LONGDBL 0x04000 /* L */
157 #define PF_INTMAX_T 0x08000 /* j */
158 #define PF_SIZE_T 0x10000 /* z */
159 #define PF_PTRDIFF_T 0x20000 /* t */
161 #define PF_ALLOCBUF 0x40000 /* for asprintf, vasprintf */
163 #define PFM_SN 0x01 /* snprintf, vsnprintf */
164 #define PFM_AS 0x02 /* asprintf, vasprintf */
166 #define ASBUFSIZE 128
168 #define x_digs "0123456789abcdef"
169 #define X_digs "0123456789ABCDEF"
171 static char intbuf[INT_STRLEN_BOUND(unsigned long) + 1];
175 static char *grouping;
178 * For the FLOATING POINT FORMAT :
179 * the challenge was finding a way to
180 * manipulate the Real numbers without having
181 * to resort to mathematical function(it
182 * would require to link with -lm) and not
183 * going down to the bit pattern(not portable)
185 * so a number, a real is:
187 real = integral + fraction
189 integral = ... + a(2)*10^2 + a(1)*10^1 + a(0)*10^0
190 fraction = b(1)*10^-1 + b(2)*10^-2 + ...
196 from then it was simple math
200 * size of the buffer for the integral part
201 * and the fraction part
203 #define MAX_INT 99 + 1 /* 1 for the null */
204 #define MAX_FRACT 307 + 1
207 * These functions use static buffers to store the results,
208 * and so are not reentrant
210 #define itoa(n) fmtulong(n, 10, intbuf, sizeof(intbuf), 0);
211 #define dtoa(n, p, f) numtoa(n, 10, p, f)
213 #define SWAP_INT(a,b) {int t; t = (a); (a) = (b); (b) = t;}
215 #define GETARG(type) (va_arg(args, type))
217 /* Macros that do proper sign extension and handle length modifiers. Used
218 for the integer conversion specifiers. */
219 #define GETSIGNED(p) \
220 (((p)->flags & PF_LONGINT) \
222 : (((p)->flags & PF_SHORTINT) ? (long)(short)GETARG (int) \
223 : (long)GETARG (int)))
225 #define GETUNSIGNED(p) \
226 (((p)->flags & PF_LONGINT) \
227 ? GETARG (unsigned long) \
228 : (((p)->flags & PF_SHORTINT) ? (unsigned long)(unsigned short)GETARG (int) \
229 : (unsigned long)GETARG (unsigned int)))
232 #ifdef HAVE_LONG_DOUBLE
233 #define GETLDOUBLE(p) GETARG (long double)
235 #define GETDOUBLE(p) GETARG (double)
237 #define SET_SIZE_FLAGS(p, type) \
238 if (sizeof (type) > sizeof (int)) \
239 (p)->flags |= PF_LONGINT; \
240 if (sizeof (type) > sizeof (long)) \
241 (p)->flags |= PF_LONGLONG;
243 /* this struct holds everything we need */
247 char *base; /* needed for [v]asprintf */
255 int width, precision;
259 /* the floating point stuff */
260 #ifdef FLOATING_POINT
261 static double pow_10 __P((int));
262 static int log_10 __P((double));
263 static double integral __P((double, double *));
264 static char *numtoa __P((double, int, int, char **));
267 static void init_data __P((struct DATA *, char *, size_t, const char *, int));
268 static void init_conv_flag __P((struct DATA *));
271 #ifdef FLOATING_POINT
272 static void floating __P((struct DATA *, double));
273 static void exponent __P((struct DATA *, double));
275 static void number __P((struct DATA *, unsigned long, int));
276 #ifdef HAVE_LONG_LONG
277 static void lnumber __P((struct DATA *, unsigned long long, int));
279 static void pointer __P((struct DATA *, unsigned long));
280 static void strings __P((struct DATA *, char *));
282 #ifdef FLOATING_POINT
283 # define FALLBACK_FMTSIZE 32
284 # define FALLBACK_BASE 4096
285 # define LFALLBACK_BASE 5120
286 # ifdef HAVE_LONG_DOUBLE
287 static void ldfallback __P((struct DATA *, const char *, const char *, long double));
289 static void dfallback __P((struct DATA *, const char *, const char *, double));
292 static char *groupnum __P((char *));
295 static void memory_error_and_abort ();
296 static void *xmalloc __P((size_t));
297 static void *xrealloc __P((void *, size_t));
298 static void xfree __P((void *));
300 # include <xmalloc.h>
303 /* those are defines specific to snprintf to hopefully
304 * make the code clearer :-)
312 /* round off to the precision */
313 #define ROUND(d, p) \
315 d - pow_10(-(p)->precision) * 0.5 : \
316 d + pow_10(-(p)->precision) * 0.5
318 /* set default precision */
319 #define DEF_PREC(p) \
320 if ((p)->precision == NOT_FOUND) \
323 /* put a char. increment the number of chars written even if we've exceeded
324 the vsnprintf/snprintf buffer size (for the return value) */
325 #define PUT_CHAR(c, p) \
328 if (((p)->flags & PF_ALLOCBUF) && ((p)->counter >= (p)->length - 1)) \
330 (p)->length += ASBUFSIZE; \
331 (p)->base = (char *)xrealloc((p)->base, (p)->length); \
332 (p)->holder = (p)->base + (p)->counter; /* in case reallocated */ \
334 if ((p)->counter < (p)->length) \
335 *(p)->holder++ = (c); \
340 /* Output a string. P->WIDTH has already been adjusted for padding. */
341 #define PUT_STRING(string, len, p) \
345 while ((len)-- > 0) \
347 PUT_CHAR (*(string), (p)); \
354 #define PUT_PLUS(d, p, zero) \
355 if ((d) > zero && (p)->justify == RIGHT) \
358 #define PUT_SPACE(d, p, zero) \
359 if (((p)->flags & PF_SPACE) && (d) > zero) \
363 #define PAD_RIGHT(p) \
364 if ((p)->width > 0 && (p)->justify != LEFT) \
365 for (; (p)->width > 0; (p)->width--) \
366 PUT_CHAR((p)->pad, p)
369 #define PAD_LEFT(p) \
370 if ((p)->width > 0 && (p)->justify == LEFT) \
371 for (; (p)->width > 0; (p)->width--) \
372 PUT_CHAR((p)->pad, p)
374 /* pad with zeros from decimal precision */
375 #define PAD_ZERO(p) \
376 if ((p)->precision > 0) \
377 for (; (p)->precision > 0; (p)->precision--) \
380 /* if width and prec. in the args */
381 #define STAR_ARGS(p) \
383 if ((p)->flags & PF_STAR_W) \
385 (p)->width = GETARG (int); \
386 if ((p)->width < 0) \
388 (p)->flags |= PF_LADJUST; \
389 (p)->justify = LEFT; \
390 (p)->width = -(p)->width; \
393 if ((p)->flags & PF_STAR_P) \
395 (p)->precision = GETARG (int); \
396 if ((p)->precision < 0) \
398 (p)->flags &= ~PF_STAR_P; \
399 (p)->precision = NOT_FOUND; \
404 #if defined (HAVE_LOCALE_H)
405 # define GETLOCALEDATA(d, t, g) \
410 (d) = '.'; (t) = -1; (g) = 0; /* defaults */ \
414 if (lv->decimal_point && lv->decimal_point[0]) \
415 (d) = lv->decimal_point[0]; \
416 if (lv->thousands_sep && lv->thousands_sep[0]) \
417 (t) = lv->thousands_sep[0]; \
418 (g) = lv->grouping ? lv->grouping : ""; \
419 if (*(g) == '\0' || *(g) == CHAR_MAX || (t) == -1) (g) = 0; \
425 # define GETLOCALEDATA(d, t, g) \
426 ( (d) = '.', (t) = ',', g = "\003" )
429 #ifdef FLOATING_POINT
431 * Find the nth power of 10
439 /* handle common cases with fast switch statement. */
442 case -3: return .001;
448 case 3: return 1000.;
454 for (n += 4; n < 0; n++)
460 for (n -= 4; n > 0; n--)
468 * Find the integral part of the log in base 10
469 * Note: this not a real log10()
470 I just need and approximation(integerpart) of x in:
475 * NOTE: do not call this with r == 0 -- an infinite loop results.
508 * This function return the fraction part of a double
509 * and set in ip the integral part.
510 * In many ways it resemble the modf() found on most Un*x
519 double real_integral = 0.;
521 /* take care of the obvious */
522 /* equal to zero ? */
529 /* negative number ? */
540 /* the real work :-) */
541 for (j = log_10(real); j >= 0; j--)
544 s = (real - real_integral)/p;
548 real_integral += i*p;
551 return (real - real_integral);
554 #define PRECISION 1.e-6
556 * return an ascii representation of the integral part of the number
557 * and set fract to be an ascii representation of the fraction part
558 * the container for the fraction and the integral part or staticly
559 * declare with fix size
562 numtoa(number, base, precision, fract)
568 double ip, fp; /* integer and fraction part */
570 int digits = MAX_INT - 1;
571 static char integral_part[MAX_INT];
572 static char fraction_part[MAX_FRACT];
576 /* taking care of the obvious case: 0.0 */
579 integral_part[0] = '0';
580 integral_part[1] = '\0';
581 /* The fractional part has to take the precision into account */
582 for (ch = 0; ch < precision-1; ch++)
583 fraction_part[ch] = '0';
584 fraction_part[ch] = '0';
585 fraction_part[ch+1] = '\0';
587 *fract = fraction_part;
588 return integral_part;
591 /* for negative numbers */
592 if ((sign = number) < 0.)
595 digits--; /* sign consume one digit */
598 fraction = integral(number, &ip);
601 /* do the integral part */
604 integral_part[0] = '0';
609 for ( i = 0; i < digits && number != 0.; ++i)
612 fp = integral(number, &ip);
613 ch = (int)((fp + PRECISION)*base); /* force to round */
614 integral_part[i] = (ch <= 9) ? ch + '0' : ch + 'a' - 10;
615 if (! ISXDIGIT((unsigned char)integral_part[i]))
616 break; /* bail out overflow !! */
621 /* Oh No !! out of bound, ho well fill it up ! */
623 for (i = 0; i < digits; ++i)
624 integral_part[i] = '9';
628 integral_part[i++] = '-';
630 integral_part[i] = '\0';
632 /* reverse every thing */
633 for ( i--, j = 0; j < i; j++, i--)
634 SWAP_INT(integral_part[i], integral_part[j]);
636 /* the fractional part */
637 for (i=0, fp=fraction; precision > 0 && i < MAX_FRACT ; i++, precision--)
639 fraction_part[i] = (int)((fp + PRECISION)*10. + '0');
640 if (! DIGIT(fraction_part[i])) /* underflow ? */
642 fp = (fp*10.0) - (double)(long)((fp + PRECISION)*10.);
644 fraction_part[i] = '\0';
646 if (fract != (char **)0)
647 *fract = fraction_part;
649 return integral_part;
653 /* for %d and friends, it puts in holder
654 * the representation with the right padding
666 /* An explicit precision turns off the zero-padding flag. */
667 if ((p->flags & PF_ZEROPAD) && p->precision >= 0 && (p->flags & PF_DOT))
668 p->flags &= ~PF_ZEROPAD;
670 sd = d; /* signed for ' ' padding in base 10 */
672 flags = (*p->pf == 'x' || *p->pf == 'X' || *p->pf == 'o' || *p->pf == 'u' || *p->pf == 'U') ? FL_UNSIGNED : 0;
674 flags |= FL_HEXUPPER;
676 tmp = fmtulong (d, base, intbuf, sizeof(intbuf), flags);
678 if ((p->flags & PF_THOUSANDS))
680 GETLOCALEDATA(decpoint, thoussep, grouping);
681 if (grouping && (t = groupnum (tmp)))
685 p->width -= strlen(tmp);
688 if ((p->flags & PF_DOT) && p->precision > 0)
690 p->precision -= strlen(tmp);
701 if (p->flags & PF_ALTFORM)
705 if (p->flags & PF_ALTFORM)
723 #ifdef HAVE_LONG_LONG
725 * identical to number() but works for `long long'
730 unsigned long long d;
737 /* An explicit precision turns off the zero-padding flag. */
738 if ((p->flags & PF_ZEROPAD) && p->precision >= 0 && (p->flags & PF_DOT))
739 p->flags &= ~PF_ZEROPAD;
741 sd = d; /* signed for ' ' padding in base 10 */
742 flags = (*p->pf == 'x' || *p->pf == 'X' || *p->pf == 'o' || *p->pf == 'u' || *p->pf == 'U') ? FL_UNSIGNED : 0;
744 flags |= FL_HEXUPPER;
746 tmp = fmtullong (d, base, intbuf, sizeof(intbuf), flags);
748 if ((p->flags & PF_THOUSANDS))
750 GETLOCALEDATA(decpoint, thoussep, grouping);
751 if (grouping && (t = groupnum (tmp)))
755 p->width -= strlen(tmp);
758 if ((p->flags & PF_DOT) && p->precision > 0)
760 p->precision -= strlen(tmp);
771 if (p->flags & PF_ALTFORM)
775 if (p->flags & PF_ALTFORM)
801 tmp = fmtulong(d, 16, intbuf, sizeof(intbuf), 0);
802 p->width -= strlen(tmp);
805 /* prefix '0x' for pointers */
827 if (p->precision != NOT_FOUND) /* the smallest number */
828 len = (len < p->precision ? len : p->precision);
831 PUT_STRING (tmp, len, p);
835 /* %ls wide-character strings */
846 memset (&mbs, '\0', sizeof (mbstate_t));
847 ws = (const wchar_t *)tmp;
850 if (p->precision != NOT_FOUND)
852 os = (char *)xmalloc (p->precision + 1);
853 len = wcsrtombs (os, &ws, p->precision, &mbs);
857 len = wcsrtombs (NULL, &ws, 0, &mbs);
858 if (len != (size_t)-1)
860 memset (&mbs, '\0', sizeof (mbstate_t));
861 os = (char *)xmalloc (len + 1);
862 (void)wcsrtombs (os, &ws, len + 1, &mbs);
865 if (len == (size_t)-1)
867 /* invalid multibyte sequence; bail now. */
873 PUT_STRING (os, len, p);
886 lbuf = (char *)malloc (MB_CUR_MAX+1);
889 memset (&mbs, '\0', sizeof (mbstate_t));
890 len = wcrtomb (lbuf, wc, &mbs);
891 if (len == (size_t)-1)
892 /* conversion failed; bail now. */
896 PUT_STRING (l, len, p);
899 #endif /* HANDLE_MULTIBYTE */
901 #ifdef FLOATING_POINT
903 #ifndef HAVE_ISINF_IN_LIBC
904 /* Half-assed versions, since we don't want to link with libm. */
912 else if (d > DBL_MAX)
920 #ifndef HAVE_ISNAN_IN_LIBC
929 /* Check for [+-]infinity and NaN. If MODE == 1, we check for Infinity, else
930 (mode == 2) we check for NaN. This does the necessary printing. Returns
931 1 if Inf or Nan, 0 if not. */
933 chkinfnan(p, d, mode)
936 int mode; /* == 1 for inf, == 2 for nan */
942 i = (mode == 1) ? isinf(d) : isnan(d);
945 big = (mode == 1) ? "INF" : "NAN";
946 small = (mode == 1) ? "inf" : "nan";
948 tmp = (*p->pf == 'F' || *p->pf == 'G' || *p->pf == 'E') ? big : small;
962 /* %f %F %g %G floating point representation */
968 char *tmp, *tmp2, *t;
971 if (d != 0 && (chkinfnan(p, d, 1) || chkinfnan(p, d, 2)))
972 return; /* already printed nan or inf */
974 GETLOCALEDATA(decpoint, thoussep, grouping);
977 tmp = dtoa(d, p->precision, &tmp2);
979 if ((p->flags & PF_THOUSANDS) && grouping && (t = groupnum (tmp)))
982 if ((*p->pf == 'g' || *p->pf == 'G') && (p->flags & PF_ALTFORM) == 0)
984 /* smash the trailing zeros unless altform */
985 for (i = strlen(tmp2) - 1; i >= 0 && tmp2[i] == '0'; i--)
991 /* calculate the padding. 1 for the dot */
992 p->width = p->width -
993 ((d > 0. && p->justify == RIGHT) ? 1:0) -
994 ((p->flags & PF_SPACE) ? 1:0) -
995 strlen(tmp) - p->precision -
996 ((p->precision != 0 || (p->flags & PF_ALTFORM)) ? 1 : 0); /* radix char */
1003 PUT_CHAR(*tmp, p); /* the integral */
1008 if (p->precision != 0 || (p->flags & PF_ALTFORM))
1009 PUT_CHAR(decpoint, p); /* put the '.' */
1011 for (; *tmp2; tmp2++)
1012 PUT_CHAR(*tmp2, p); /* the fraction */
1017 /* %e %E %g %G exponent representation */
1026 if (d != 0 && (chkinfnan(p, d, 1) || chkinfnan(p, d, 2)))
1027 return; /* already printed nan or inf */
1029 GETLOCALEDATA(decpoint, thoussep, grouping);
1036 d = d / pow_10(j); /* get the Mantissa */
1039 tmp = dtoa(d, p->precision, &tmp2);
1041 /* 1 for unit, 1 for the '.', 1 for 'e|E',
1042 * 1 for '+|-', 2 for 'exp' */
1043 /* calculate how much padding need */
1044 p->width = p->width -
1045 ((d > 0. && p->justify == RIGHT) ? 1:0) -
1046 ((p->flags & PF_SPACE) ? 1:0) - p->precision - 6;
1050 PUT_SPACE(d, p, 0.);
1058 if (p->precision != 0 || (p->flags & PF_ALTFORM))
1059 PUT_CHAR(decpoint, p); /* the '.' */
1061 if ((*p->pf == 'g' || *p->pf == 'G') && (p->flags & PF_ALTFORM) == 0)
1062 /* smash the trailing zeros unless altform */
1063 for (i = strlen(tmp2) - 1; i >= 0 && tmp2[i] == '0'; i--)
1066 for (; *tmp2; tmp2++)
1067 PUT_CHAR(*tmp2, p); /* the fraction */
1069 /* the exponent put the 'e|E' */
1070 if (*p->pf == 'g' || *p->pf == 'e')
1075 /* the sign of the exp */
1085 /* pad out to at least two spaces. pad with `0' if the exponent is a
1101 /* Return a new string with the digits in S grouped according to the locale's
1102 grouping info and thousands separator. If no grouping should be performed,
1103 this returns NULL; the caller needs to check for it. */
1108 char *se, *ret, *re, *g;
1111 if (grouping == 0 || *grouping <= 0 || *grouping == CHAR_MAX)
1112 return ((char *)NULL);
1114 /* find min grouping to size returned string */
1115 for (len = *grouping, g = grouping; *g; g++)
1116 if (*g > 0 && *g < len)
1120 len = slen / len + 1;
1121 ret = (char *)xmalloc (slen + len + 1);
1122 re = ret + slen + len;
1133 /* handle `-' inserted by numtoa() and the fmtu* family here. */
1134 if (se > s && se[-1] == '-')
1137 /* begin new group. */
1138 if (--len == 0 && se > s)
1141 len = *++g; /* was g++, but that uses first char twice (glibc bug, too) */
1143 len = *--g; /* use previous grouping */
1144 else if (*g == CHAR_MAX)
1156 memmove (ret, re, strlen (re) + 1);
1164 /* initialize the conversion specifiers */
1169 p->flags &= PF_ALLOCBUF; /* preserve PF_ALLOCBUF flag */
1170 p->precision = p->width = NOT_FOUND;
1171 p->justify = NOT_FOUND;
1176 init_data (p, string, length, format, mode)
1183 p->length = length - 1; /* leave room for '\0' */
1184 p->holder = p->base = string;
1187 p->flags = (mode == PFM_AS) ? PF_ALLOCBUF : 0;
1191 #if defined (__STDC__)
1192 vsnprintf_internal(struct DATA *data, char *string, size_t length, const char *format, va_list args)
1194 vsnprintf_internal(data, string, length, format, args)
1202 double d; /* temporary holder */
1203 #ifdef HAVE_LONG_DOUBLE
1204 long double ld; /* for later */
1207 #ifdef HAVE_LONG_LONG
1208 unsigned long long ull;
1212 #if HANDLE_MULTIBYTE
1216 const char *convstart;
1219 /* Sanity check, the string length must be >= 0. C99 actually says that
1220 LENGTH can be zero here, in the case of snprintf/vsnprintf (it's never
1221 0 in the case of asprintf/vasprintf), and the return value is the number
1222 of characters that would have been written. */
1229 /* Reset these for each call because the locale might have changed. */
1230 decpoint = thoussep = 0;
1234 for (; c = *(data->pf); data->pf++)
1242 convstart = data->pf;
1243 init_conv_flag (data); /* initialise format flags */
1246 for (state = 1; state && *data->pf; )
1249 /* fmtend = data->pf */
1250 #if defined (FLOATING_POINT) && defined (HAVE_LONG_DOUBLE)
1251 if (data->flags & PF_LONGDBL)
1258 # ifdef HAVE_PRINTF_A_FORMAT
1262 ld = GETLDOUBLE (data);
1263 ldfallback (data, convstart, data->pf, ld);
1267 #endif /* FLOATING_POINT && HAVE_LONG_DOUBLE */
1271 /* Parse format flags */
1272 case '\0': /* a NULL here ? ? bail out */
1273 *data->holder = '\0';
1274 return data->counter;
1277 data->flags |= PF_ALTFORM;
1280 data->flags |= PF_ZEROPAD;
1284 if (data->flags & PF_DOT)
1285 data->flags |= PF_STAR_P;
1287 data->flags |= PF_STAR_W;
1290 if ((data->flags & PF_DOT) == 0)
1292 data->flags |= PF_LADJUST;
1293 data->justify = LEFT;
1299 if ((data->flags & PF_PLUS) == 0)
1300 data->flags |= PF_SPACE;
1303 if ((data->flags & PF_DOT) == 0)
1305 data->flags |= PF_PLUS;
1306 data->justify = RIGHT;
1310 data->flags |= PF_THOUSANDS;
1313 case '1': case '2': case '3':
1314 case '4': case '5': case '6':
1315 case '7': case '8': case '9':
1319 n = n * 10 + TODIGIT(c);
1323 data->pf--; /* went too far */
1326 if (data->flags & PF_DOT)
1327 data->precision = negprec ? NOT_FOUND : n;
1332 /* optional precision */
1334 data->flags |= PF_DOT;
1335 data->precision = 0;
1338 /* length modifiers */
1340 data->flags |= (data->flags & PF_SHORTINT) ? PF_SIGNEDCHAR : PF_SHORTINT;
1343 data->flags |= (data->flags & PF_LONGINT) ? PF_LONGLONG : PF_LONGINT;
1346 data->flags |= PF_LONGDBL;
1349 data->flags |= PF_LONGLONG;
1352 data->flags |= PF_INTMAX_T;
1353 SET_SIZE_FLAGS(data, intmax_t);
1356 data->flags |= PF_SIZE_T;
1357 SET_SIZE_FLAGS(data, size_t);
1360 data->flags |= PF_PTRDIFF_T;
1361 SET_SIZE_FLAGS(data, ptrdiff_t);
1364 /* Conversion specifiers */
1365 #ifdef FLOATING_POINT
1366 case 'f': /* float, double */
1369 d = GETDOUBLE(data);
1378 d = GETDOUBLE(data);
1379 i = (d != 0.) ? log_10(d) : -1;
1381 * for '%g|%G' ANSI: use f if exponent
1382 * is in the range or [-4,p] exclusively
1385 if (-4 < i && i < data->precision)
1387 /* reset precision */
1388 data->precision -= i + 1;
1393 /* reduce precision by 1 because of leading digit before
1394 decimal point in e format. */
1401 case 'E': /* Exponent double */
1403 d = GETDOUBLE(data);
1407 # ifdef HAVE_PRINTF_A_FORMAT
1411 d = GETDOUBLE(data);
1412 dfallback(data, convstart, data->pf, d);
1415 # endif /* HAVE_PRINTF_A_FORMAT */
1416 #endif /* FLOATING_POINT */
1418 data->flags |= PF_LONGINT;
1422 #ifdef HAVE_LONG_LONG
1423 if (data->flags & PF_LONGLONG)
1425 ull = GETARG (unsigned long long);
1426 lnumber(data, ull, 10);
1431 ul = GETUNSIGNED(data);
1432 number(data, ul, 10);
1437 data->flags |= PF_LONGINT;
1439 case 'd': /* decimal */
1442 #ifdef HAVE_LONG_LONG
1443 if (data->flags & PF_LONGLONG)
1445 ull = GETARG (long long);
1446 lnumber(data, ull, 10);
1451 ul = GETSIGNED(data);
1452 number(data, ul, 10);
1456 case 'o': /* octal */
1458 #ifdef HAVE_LONG_LONG
1459 if (data->flags & PF_LONGLONG)
1461 ull = GETARG (unsigned long long);
1462 lnumber(data, ull, 8);
1467 ul = GETUNSIGNED(data);
1468 number(data, ul, 8);
1473 case 'X': /* hexadecimal */
1475 #ifdef HAVE_LONG_LONG
1476 if (data->flags & PF_LONGLONG)
1478 ull = GETARG (unsigned long long);
1479 lnumber(data, ull, 16);
1484 ul = GETUNSIGNED(data);
1485 number(data, ul, 16);
1491 ul = (unsigned long)GETARG (void *);
1495 #if HANDLE_MULTIBYTE
1497 data->flags |= PF_LONGINT;
1500 case 'c': /* character */
1502 #if HANDLE_MULTIBYTE
1503 if (data->flags & PF_LONGINT)
1505 wc = GETARG (wint_t);
1516 #if HANDLE_MULTIBYTE
1518 data->flags |= PF_LONGINT;
1521 case 's': /* string */
1523 #if HANDLE_MULTIBYTE
1524 if (data->flags & PF_LONGINT)
1526 ws = GETARG (wchar_t *);
1527 wstrings (data, ws);
1532 s = GETARG (char *);
1538 #ifdef HAVE_LONG_LONG
1539 if (data->flags & PF_LONGLONG)
1540 *(GETARG (long long *)) = data->counter;
1543 if (data->flags & PF_LONGINT)
1544 *(GETARG (long *)) = data->counter;
1545 else if (data->flags & PF_SHORTINT)
1546 *(GETARG (short *)) = data->counter;
1548 *(GETARG (int *)) = data->counter;
1551 case '%': /* nothing just % */
1552 PUT_CHAR('%', data);
1556 /* is this an error ? maybe bail out */
1560 } /* end of `%' for loop */
1561 } /* end of format string for loop */
1563 if (data->length >= 0)
1564 *data->holder = '\0'; /* the end ye ! */
1566 return data->counter;
1569 #if defined (FLOATING_POINT) && defined (HAVE_LONG_DOUBLE)
1571 * Printing floating point numbers accurately is an art. I'm not good
1572 * at it. Fall back to sprintf for long double formats.
1575 ldfallback (data, fs, fe, ld)
1577 const char *fs, *fe;
1581 char fmtbuf[FALLBACK_FMTSIZE], *obuf;
1584 fl = LFALLBACK_BASE + (data->precision < 6 ? 6 : data->precision) + 2;
1585 obuf = (char *)xmalloc (fl);
1587 strncpy (fmtbuf, fs, fl);
1590 if ((data->flags & PF_STAR_W) && (data->flags & PF_STAR_P))
1591 sprintf (obuf, fmtbuf, data->width, data->precision, ld);
1592 else if (data->flags & PF_STAR_W)
1593 sprintf (obuf, fmtbuf, data->width, ld);
1594 else if (data->flags & PF_STAR_P)
1595 sprintf (obuf, fmtbuf, data->precision, ld);
1597 sprintf (obuf, fmtbuf, ld);
1599 for (x = obuf; *x; x++)
1600 PUT_CHAR (*x, data);
1603 #endif /* FLOATING_POINT && HAVE_LONG_DOUBLE */
1605 #ifdef FLOATING_POINT
1606 /* Used for %a, %A if the libc printf supports them. */
1608 dfallback (data, fs, fe, d)
1610 const char *fs, *fe;
1614 char fmtbuf[FALLBACK_FMTSIZE], obuf[FALLBACK_BASE];
1618 strncpy (fmtbuf, fs, fl);
1621 if ((data->flags & PF_STAR_W) && (data->flags & PF_STAR_P))
1622 sprintf (obuf, fmtbuf, data->width, data->precision, d);
1623 else if (data->flags & PF_STAR_W)
1624 sprintf (obuf, fmtbuf, data->width, d);
1625 else if (data->flags & PF_STAR_P)
1626 sprintf (obuf, fmtbuf, data->precision, d);
1628 sprintf (obuf, fmtbuf, d);
1630 for (x = obuf; *x; x++)
1631 PUT_CHAR (*x, data);
1633 #endif /* FLOATING_POINT */
1635 #ifndef HAVE_SNPRINTF
1638 #if defined (__STDC__)
1639 vsnprintf(char *string, size_t length, const char *format, va_list args)
1641 vsnprintf(string, length, format, args)
1650 if (string == 0 && length != 0)
1652 init_data (&data, string, length, format, PFM_SN);
1653 return (vsnprintf_internal(&data, string, length, format, args));
1657 #if defined(PREFER_STDARG)
1658 snprintf(char *string, size_t length, const char * format, ...)
1660 snprintf(string, length, format, va_alist)
1671 SH_VA_START(args, format);
1673 if (string == 0 && length != 0)
1675 init_data (&data, string, length, format, PFM_SN);
1676 rval = vsnprintf_internal (&data, string, length, format, args);
1683 #endif /* HAVE_SNPRINTF */
1685 #ifndef HAVE_ASPRINTF
1688 #if defined (__STDC__)
1689 vasprintf(char **stringp, const char *format, va_list args)
1691 vasprintf(stringp, format, args)
1701 string = (char *)xmalloc(ASBUFSIZE);
1702 init_data (&data, string, ASBUFSIZE, format, PFM_AS);
1703 r = vsnprintf_internal(&data, string, ASBUFSIZE, format, args);
1704 *stringp = data.base; /* not string in case reallocated */
1709 #if defined(PREFER_STDARG)
1710 asprintf(char **stringp, const char * format, ...)
1712 asprintf(stringp, format, va_alist)
1721 SH_VA_START(args, format);
1723 rval = vasprintf (stringp, format, args);
1737 memory_error_and_abort ()
1739 write (2, "out of virtual memory\n", 22);
1749 ret = malloc(bytes);
1751 memory_error_and_abort ();
1756 xrealloc (pointer, bytes)
1762 ret = pointer ? realloc(pointer, bytes) : malloc(bytes);
1764 memory_error_and_abort ();
1776 /* set of small tests for snprintf() */
1783 #ifdef HAVE_LOCALE_H
1784 setlocale(LC_ALL, "");
1788 si = snprintf((char *)NULL, 0, "abcde\n");
1789 printf("snprintf returns %d with NULL first argument and size of 0\n", si);
1790 si = snprintf(holder, 0, "abcde\n");
1791 printf("snprintf returns %d with non-NULL first argument and size of 0\n", si);
1792 si = snprintf((char *)NULL, 16, "abcde\n");
1793 printf("snprintf returns %d with NULL first argument and non-zero size\n", si);
1796 printf("Suite of test for snprintf:\n");
1797 printf("a_format\n");
1798 printf("printf() format\n");
1799 printf("snprintf() format\n\n");
1801 /* Checking the field widths */
1803 printf("/%%ld %%ld/, 336, 336\n");
1804 snprintf(holder, sizeof holder, "/%ld %ld/\n", 336, 336);
1805 asprintf(&h, "/%ld %ld/\n", 336, 336);
1806 printf("/%ld %ld/\n", 336, 336);
1807 printf("%s", holder);
1810 printf("/%%d/, 336\n");
1811 snprintf(holder, sizeof holder, "/%d/\n", 336);
1812 asprintf(&h, "/%d/\n", 336);
1813 printf("/%d/\n", 336);
1814 printf("%s", holder);
1817 printf("/%%2d/, 336\n");
1818 snprintf(holder, sizeof holder, "/%2d/\n", 336);
1819 asprintf(&h, "/%2d/\n", 336);
1820 printf("/%2d/\n", 336);
1821 printf("%s", holder);
1824 printf("/%%10d/, 336\n");
1825 snprintf(holder, sizeof holder, "/%10d/\n", 336);
1826 asprintf(&h, "/%10d/\n", 336);
1827 printf("/%10d/\n", 336);
1828 printf("%s", holder);
1831 printf("/%%-10d/, 336\n");
1832 snprintf(holder, sizeof holder, "/%-10d/\n", 336);
1833 asprintf(&h, "/%-10d/\n", 336);
1834 printf("/%-10d/\n", 336);
1835 printf("%s", holder);
1839 /* floating points */
1841 printf("/%%f/, 1234.56\n");
1842 snprintf(holder, sizeof holder, "/%f/\n", 1234.56);
1843 asprintf(&h, "/%f/\n", 1234.56);
1844 printf("/%f/\n", 1234.56);
1845 printf("%s", holder);
1848 printf("/%%e/, 1234.56\n");
1849 snprintf(holder, sizeof holder, "/%e/\n", 1234.56);
1850 asprintf(&h, "/%e/\n", 1234.56);
1851 printf("/%e/\n", 1234.56);
1852 printf("%s", holder);
1855 printf("/%%4.2f/, 1234.56\n");
1856 snprintf(holder, sizeof holder, "/%4.2f/\n", 1234.56);
1857 asprintf(&h, "/%4.2f/\n", 1234.56);
1858 printf("/%4.2f/\n", 1234.56);
1859 printf("%s", holder);
1862 printf("/%%3.1f/, 1234.56\n");
1863 snprintf(holder, sizeof holder, "/%3.1f/\n", 1234.56);
1864 asprintf(&h, "/%3.1f/\n", 1234.56);
1865 printf("/%3.1f/\n", 1234.56);
1866 printf("%s", holder);
1869 printf("/%%10.3f/, 1234.56\n");
1870 snprintf(holder, sizeof holder, "/%10.3f/\n", 1234.56);
1871 asprintf(&h, "/%10.3f/\n", 1234.56);
1872 printf("/%10.3f/\n", 1234.56);
1873 printf("%s", holder);
1876 printf("/%%10.3e/, 1234.56\n");
1877 snprintf(holder, sizeof holder, "/%10.3e/\n", 1234.56);
1878 asprintf(&h, "/%10.3e/\n", 1234.56);
1879 printf("/%10.3e/\n", 1234.56);
1880 printf("%s", holder);
1883 printf("/%%+4.2f/, 1234.56\n");
1884 snprintf(holder, sizeof holder, "/%+4.2f/\n", 1234.56);
1885 asprintf(&h, "/%+4.2f/\n", 1234.56);
1886 printf("/%+4.2f/\n", 1234.56);
1887 printf("%s", holder);
1890 printf("/%%010.2f/, 1234.56\n");
1891 snprintf(holder, sizeof holder, "/%010.2f/\n", 1234.56);
1892 asprintf(&h, "/%010.2f/\n", 1234.56);
1893 printf("/%010.2f/\n", 1234.56);
1894 printf("%s", holder);
1897 #define BLURB "Outstanding acting !"
1898 /* strings precisions */
1900 printf("/%%2s/, \"%s\"\n", BLURB);
1901 snprintf(holder, sizeof holder, "/%2s/\n", BLURB);
1902 asprintf(&h, "/%2s/\n", BLURB);
1903 printf("/%2s/\n", BLURB);
1904 printf("%s", holder);
1907 printf("/%%22s/ %s\n", BLURB);
1908 snprintf(holder, sizeof holder, "/%22s/\n", BLURB);
1909 asprintf(&h, "/%22s/\n", BLURB);
1910 printf("/%22s/\n", BLURB);
1911 printf("%s", holder);
1914 printf("/%%22.5s/ %s\n", BLURB);
1915 snprintf(holder, sizeof holder, "/%22.5s/\n", BLURB);
1916 asprintf(&h, "/%22.5s/\n", BLURB);
1917 printf("/%22.5s/\n", BLURB);
1918 printf("%s", holder);
1921 printf("/%%-22.5s/ %s\n", BLURB);
1922 snprintf(holder, sizeof holder, "/%-22.5s/\n", BLURB);
1923 asprintf(&h, "/%-22.5s/\n", BLURB);
1924 printf("/%-22.5s/\n", BLURB);
1925 printf("%s", holder);
1928 /* see some flags */
1930 printf("%%x %%X %%#x, 31, 31, 31\n");
1931 snprintf(holder, sizeof holder, "%x %X %#x\n", 31, 31, 31);
1932 asprintf(&h, "%x %X %#x\n", 31, 31, 31);
1933 printf("%x %X %#x\n", 31, 31, 31);
1934 printf("%s", holder);
1937 printf("**%%d**%% d**%% d**, 42, 42, -42\n");
1938 snprintf(holder, sizeof holder, "**%d**% d**% d**\n", 42, 42, -42);
1939 asprintf(&h, "**%d**% d**% d**\n", 42, 42, -42);
1940 printf("**%d**% d**% d**\n", 42, 42, -42);
1941 printf("%s", holder);
1946 printf("/%%g/, 31.4\n");
1947 snprintf(holder, sizeof holder, "/%g/\n", 31.4);
1948 asprintf(&h, "/%g/\n", 31.4);
1949 printf("/%g/\n", 31.4);
1950 printf("%s", holder);
1953 printf("/%%.6g/, 31.4\n");
1954 snprintf(holder, sizeof holder, "/%.6g/\n", 31.4);
1955 asprintf(&h, "/%.6g/\n", 31.4);
1956 printf("/%.6g/\n", 31.4);
1957 printf("%s", holder);
1960 printf("/%%.1G/, 31.4\n");
1961 snprintf(holder, sizeof holder, "/%.1G/\n", 31.4);
1962 asprintf(&h, "/%.1G/\n", 31.4);
1963 printf("/%.1G/\n", 31.4);
1964 printf("%s", holder);
1967 printf("/%%.1G/, 3100000000.4\n");
1968 snprintf(holder, sizeof holder, "/%.1G/\n", 3100000000.4);
1969 asprintf(&h, "/%.1G/\n", 3100000000.4);
1970 printf("/%.1G/\n", 3100000000.4);
1971 printf("%s", holder);
1975 printf("abc%n", &i); printf("%d\n", i);
1976 snprintf(holder, sizeof holder, "abc%n", &i);
1977 printf("%s", holder); printf("%d\n\n", i);
1978 asprintf(&h, "abc%n", &i);
1979 printf("%s", h); printf("%d\n\n", i);
1981 printf("%%*.*s --> 10.10\n");
1982 snprintf(holder, sizeof holder, "%*.*s\n", 10, 10, BLURB);
1983 asprintf(&h, "%*.*s\n", 10, 10, BLURB);
1984 printf("%*.*s\n", 10, 10, BLURB);
1985 printf("%s", holder);
1988 printf("%%%%%%%%\n");
1989 snprintf(holder, sizeof holder, "%%%%\n");
1990 asprintf(&h, "%%%%\n");
1992 printf("%s", holder);
1995 #define BIG "Hello this is a too big string for the buffer"
1996 /* printf("A buffer to small of 10, trying to put this:\n");*/
1997 printf("<%%>, %s\n", BIG);
1998 i = snprintf(holder, 10, "%s\n", BIG);
1999 i = asprintf(&h, "%s", BIG);
2000 printf("<%s>\n", BIG);
2001 printf("<%s>\n", holder);
2002 printf("<%s>\n\n", h);
2004 printf ("<%%p> vsnprintf\n");
2005 i = snprintf(holder, 100, "%p", vsnprintf);
2006 i = asprintf(&h, "%p", vsnprintf);
2007 printf("<%p>\n", vsnprintf);
2008 printf("<%s>\n", holder);
2009 printf("<%s>\n\n", h);
2011 printf ("<%%lu> LONG_MAX+1\n");
2012 i = snprintf(holder, 100, "%lu", (unsigned long)(LONG_MAX)+1);
2013 i = asprintf(&h, "%lu", (unsigned long)(LONG_MAX)+1);
2014 printf("<%lu>\n", (unsigned long)(LONG_MAX)+1);
2015 printf("<%s>\n", holder);
2016 printf("<%s>\n\n", h);
2018 #ifdef HAVE_LONG_LONG
2019 printf ("<%%llu> LLONG_MAX+1\n");
2020 i = snprintf(holder, 100, "%llu", (unsigned long long)(LLONG_MAX)+1);
2021 i = asprintf(&h, "%llu", (unsigned long long)(LLONG_MAX)+1);
2022 printf("<%llu>\n", (unsigned long long)(LLONG_MAX)+1);
2023 printf("<%s>\n", holder);
2024 printf("<%s>\n\n", h);
2027 #ifdef HAVE_LONG_DOUBLE
2028 printf ("<%%6.2LE> 42.42\n");
2029 i = snprintf(holder, 100, "%6.2LE", (long double)42.42);
2030 i = asprintf(&h, "%6.2LE", (long double)42.42);
2031 printf ("<%6.2LE>\n", (long double)42.42);
2032 printf ("<%s>\n", holder);
2033 printf ("<%s>\n\n", h);
2036 #ifdef HAVE_PRINTF_A_FORMAT
2037 printf ("<%%6.2A> 42.42\n");
2038 i = snprintf(holder, 100, "%6.2A", 42.42);
2039 i = asprintf(&h, "%6.2A", 42.42);
2040 printf ("<%6.2A>\n", 42.42);
2041 printf ("<%s>\n", holder);
2042 printf ("<%s>\n\n", h);
2044 printf ("<%%6.2LA> 42.42\n");
2045 i = snprintf(holder, 100, "%6.2LA", (long double)42.42);
2046 i = asprintf(&h, "%6.2LA", (long double)42.42);
2047 printf ("<%6.2LA>\n", (long double)42.42);
2048 printf ("<%s>\n", holder);
2049 printf ("<%s>\n\n", h);
2052 printf ("<%%.10240f> DBL_MAX\n");
2053 si = snprintf(holder, 100, "%.10240f", DBL_MAX);
2054 ai = asprintf(&h, "%.10240f", DBL_MAX);
2055 printf ("<%.10240f>\n", DBL_MAX);
2056 printf ("<%d> <%s>\n", si, holder);
2057 printf ("<%d> <%s>\n\n", ai, h);
2059 printf ("<%%.10240Lf> LDBL_MAX\n");
2060 si = snprintf(holder, 100, "%.10240Lf", (long double)LDBL_MAX);
2061 ai = asprintf(&h, "%.10240Lf", (long double)LDBL_MAX);
2062 printf ("<%.10240Lf>\n", (long double)LDBL_MAX);
2063 printf ("<%d> <%s>\n", si, holder);
2064 printf ("<%d> <%s>\n\n", ai, h);
2067 printf("/%%g/, 421.2345\n");
2068 snprintf(holder, sizeof holder, "/%g/\n", 421.2345);
2069 asprintf(&h, "/%g/\n", 421.2345);
2070 printf("/%g/\n", 421.2345);
2071 printf("%s", holder);
2074 printf("/%%g/, 4214.2345\n");
2075 snprintf(holder, sizeof holder, "/%g/\n", 4214.2345);
2076 asprintf(&h, "/%g/\n", 4214.2345);
2077 printf("/%g/\n", 4214.2345);
2078 printf("%s", holder);
2081 printf("/%%.5g/, 4214.2345\n");
2082 snprintf(holder, sizeof holder, "/%.5g/\n", 4214.2345);
2083 asprintf(&h, "/%.5g/\n", 4214.2345);
2084 printf("/%.5g/\n", 4214.2345);
2085 printf("%s", holder);
2088 printf("/%%.4g/, 4214.2345\n");
2089 snprintf(holder, sizeof holder, "/%.4g/\n", 4214.2345);
2090 asprintf(&h, "/%.4g/\n", 4214.2345);
2091 printf("/%.4g/\n", 4214.2345);
2092 printf("%s", holder);
2095 printf("/%%'ld %%'ld/, 12345, 1234567\n");
2096 snprintf(holder, sizeof holder, "/%'ld %'ld/\n", 12345, 1234567);
2097 asprintf(&h, "/%'ld %'ld/\n", 12345, 1234567);
2098 printf("/%'ld %'ld/\n", 12345, 1234567);
2099 printf("%s", holder);
2102 printf("/%%'ld %%'ld/, 336, 3336\n");
2103 snprintf(holder, sizeof holder, "/%'ld %'ld/\n", 336, 3336);
2104 asprintf(&h, "/%'ld %'ld/\n", 336, 3336);
2105 printf("/%'ld %'ld/\n", 336, 3336);
2106 printf("%s", holder);
2109 printf("/%%'ld %%'ld/, -42786, -142786\n");
2110 snprintf(holder, sizeof holder, "/%'ld %'ld/\n", -42786, -142786);
2111 asprintf(&h, "/%'ld %'ld/\n", -42786, -142786);
2112 printf("/%'ld %'ld/\n", -42786, -142786);
2113 printf("%s", holder);
2116 printf("/%%'f %%'f/, 421.2345, 421234.56789\n");
2117 snprintf(holder, sizeof holder, "/%'f %'f/\n", 421.2345, 421234.56789);
2118 asprintf(&h, "/%'f %'f/\n", 421.2345, 421234.56789);
2119 printf("/%'f %'f/\n", 421.2345, 421234.56789);
2120 printf("%s", holder);
2123 printf("/%%'f %%'f/, -421.2345, -421234.56789\n");
2124 snprintf(holder, sizeof holder, "/%'f %'f/\n", -421.2345, -421234.56789);
2125 asprintf(&h, "/%'f %'f/\n", -421.2345, -421234.56789);
2126 printf("/%'f %'f/\n", -421.2345, -421234.56789);
2127 printf("%s", holder);
2130 printf("/%%'g %%'g/, 421.2345, 421234.56789\n");
2131 snprintf(holder, sizeof holder, "/%'g %'g/\n", 421.2345, 421234.56789);
2132 asprintf(&h, "/%'g %'g/\n", 421.2345, 421234.56789);
2133 printf("/%'g %'g/\n", 421.2345, 421234.56789);
2134 printf("%s", holder);
2137 printf("/%%'g %%'g/, -421.2345, -421234.56789\n");
2138 snprintf(holder, sizeof holder, "/%'g %'g/\n", -421.2345, -421234.56789);
2139 asprintf(&h, "/%'g %'g/\n", -421.2345, -421234.56789);
2140 printf("/%'g %'g/\n", -421.2345, -421234.56789);
2141 printf("%s", holder);
2145 printf("/%%'g/, 4213455.8392\n");
2146 snprintf(holder, sizeof holder, "/%'g/\n", 4213455.8392);
2147 asprintf(&h, "/%'g/\n", 4213455.8392);
2148 printf("/%'g/\n", 4213455.8392);
2149 printf("%s", holder);