]> git.saurik.com Git - apple/libc.git/blob - stdio/vfwprintf-fbsd.c
d085dcc3a543d1e51081ec5597662e01ecc790dc
[apple/libc.git] / stdio / vfwprintf-fbsd.c
1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #if 0
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
36 #endif /* LIBC_SCCS and not lint */
37 #endif
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.42 2009/11/25 04:27:55 wollman Exp $");
40
41 #include "xlocale_private.h"
42
43 /*
44 * Actual wprintf innards.
45 *
46 * Avoid making gratuitous changes to this source file; it should be kept
47 * as close as possible to vfprintf.c for ease of maintenance.
48 */
49
50 #include "namespace.h"
51 #include <sys/types.h>
52
53 #include <ctype.h>
54 #include <limits.h>
55 #include <locale.h>
56 #include <stdarg.h>
57 #include <stddef.h>
58 #include <stdint.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <wchar.h>
63 #include <wctype.h>
64 #include <errno.h>
65 #include "un-namespace.h"
66
67 #include "libc_private.h"
68 #include "local.h"
69 #include "fvwrite.h"
70 #include "printflocal.h"
71
72 static int __sprint(FILE *, locale_t, struct __suio *);
73 static int __sbprintf(FILE *, locale_t, const wchar_t *, va_list);
74 static wint_t __xfputwc(wchar_t, FILE *, locale_t);
75 static wchar_t *__mbsconv(char *, int, locale_t);
76 __private_extern__ const char *__fix_nogrouping(const char *);
77
78 #define CHAR wchar_t
79 #include "printfcommon.h"
80
81 struct grouping_state {
82 wchar_t thousands_sep; /* locale-specific thousands separator */
83 const char *grouping; /* locale-specific numeric grouping rules */
84 int lead; /* sig figs before decimal or group sep */
85 int nseps; /* number of group separators with ' */
86 int nrepeats; /* number of repeats of the last group */
87 };
88
89 static const mbstate_t initial_mbs;
90
91 static inline wchar_t
92 get_decpt(locale_t loc)
93 {
94 mbstate_t mbs;
95 wchar_t decpt;
96 int nconv;
97
98 mbs = initial_mbs;
99 nconv = mbrtowc_l(&decpt, localeconv_l(loc)->decimal_point, MB_CUR_MAX_L(loc), &mbs, loc);
100 if (nconv == (size_t)-1 || nconv == (size_t)-2)
101 decpt = '.'; /* failsafe */
102 return (decpt);
103 }
104
105 static inline wchar_t
106 get_thousep(locale_t loc)
107 {
108 mbstate_t mbs;
109 wchar_t thousep;
110 int nconv;
111
112 mbs = initial_mbs;
113 nconv = mbrtowc_l(&thousep, localeconv_l(loc)->thousands_sep,
114 MB_CUR_MAX_L(loc), &mbs, loc);
115 if (nconv == (size_t)-1 || nconv == (size_t)-2)
116 thousep = '\0'; /* failsafe */
117 return (thousep);
118 }
119
120 /*
121 * Initialize the thousands' grouping state in preparation to print a
122 * number with ndigits digits. This routine returns the total number
123 * of wide characters that will be printed.
124 */
125 static int
126 grouping_init(struct grouping_state *gs, int ndigits, locale_t loc)
127 {
128
129 gs->grouping = __fix_nogrouping(localeconv_l(loc)->grouping);
130 gs->thousands_sep = get_thousep(loc);
131
132 gs->nseps = gs->nrepeats = 0;
133 gs->lead = ndigits;
134 while (*gs->grouping != CHAR_MAX) {
135 if (gs->lead <= *gs->grouping)
136 break;
137 gs->lead -= *gs->grouping;
138 if (*(gs->grouping+1)) {
139 gs->nseps++;
140 gs->grouping++;
141 } else
142 gs->nrepeats++;
143 }
144 return (gs->nseps + gs->nrepeats);
145 }
146
147 /*
148 * Print a number with thousands' separators.
149 */
150 static int
151 grouping_print(struct grouping_state *gs, struct io_state *iop,
152 const CHAR *cp, const CHAR *ep, locale_t loc)
153 {
154 const CHAR *cp0 = cp;
155
156 if (io_printandpad(iop, cp, ep, gs->lead, zeroes, loc))
157 return (-1);
158 cp += gs->lead;
159 while (gs->nseps > 0 || gs->nrepeats > 0) {
160 if (gs->nrepeats > 0)
161 gs->nrepeats--;
162 else {
163 gs->grouping--;
164 gs->nseps--;
165 }
166 if (io_print(iop, &gs->thousands_sep, 1, loc))
167 return (-1);
168 if (io_printandpad(iop, cp, ep, *gs->grouping, zeroes, loc))
169 return (-1);
170 cp += *gs->grouping;
171 }
172 if (cp > ep)
173 cp = ep;
174 return (cp - cp0);
175 }
176
177
178 /*
179 * Flush out all the vectors defined by the given uio,
180 * then reset it so that it can be reused.
181 *
182 * XXX The fact that we do this a character at a time and convert to a
183 * multibyte character sequence even if the destination is a wide
184 * string eclipses the benefits of buffering.
185 */
186 static int
187 __sprint(FILE *fp, locale_t loc, struct __suio *uio)
188 {
189 struct __siov *iov;
190 wchar_t *p;
191 int i, len;
192
193 iov = uio->uio_iov;
194 for (; uio->uio_resid != 0; uio->uio_resid -= len, iov++) {
195 p = (wchar_t *)iov->iov_base;
196 len = iov->iov_len;
197 for (i = 0; i < len; i++) {
198 if (__xfputwc(p[i], fp, loc) == WEOF)
199 return (-1);
200 }
201 }
202 uio->uio_iovcnt = 0;
203 return (0);
204 }
205
206 /*
207 * Helper function for `fprintf to unbuffered unix file': creates a
208 * temporary buffer. We only work on write-only files; this avoids
209 * worries about ungetc buffers and so forth.
210 */
211 static int
212 __sbprintf(FILE *fp, locale_t loc, const wchar_t *fmt, va_list ap)
213 {
214 int ret;
215 FILE fake;
216 unsigned char buf[BUFSIZ];
217 struct __sFILEX ext;
218 fake._extra = &ext;
219 INITEXTRA(&fake);
220
221 /* XXX This is probably not needed. */
222 if (prepwrite(fp) != 0)
223 return (EOF);
224
225 /* copy the important variables */
226 fake._flags = fp->_flags & ~__SNBF;
227 fake._file = fp->_file;
228 fake._cookie = fp->_cookie;
229 fake._write = fp->_write;
230 fake._orientation = fp->_orientation;
231 fake._mbstate = fp->_mbstate;
232
233 /* set up the buffer */
234 fake._bf._base = fake._p = buf;
235 fake._bf._size = fake._w = sizeof(buf);
236 fake._lbfsize = 0; /* not actually used, but Just In Case */
237
238 /* do the work, then copy any error status */
239 ret = __vfwprintf(&fake, loc, fmt, ap);
240 if (ret >= 0 && __fflush(&fake))
241 ret = WEOF;
242 if (fake._flags & __SERR)
243 fp->_flags |= __SERR;
244 return (ret);
245 }
246
247 /*
248 * Like __fputwc, but handles fake string (__SSTR) files properly.
249 * File must already be locked.
250 */
251 static wint_t
252 __xfputwc(wchar_t wc, FILE *fp, locale_t loc)
253 {
254 mbstate_t mbs;
255 char buf[MB_LEN_MAX];
256 struct __suio uio;
257 struct __siov iov;
258 size_t len;
259
260 if ((fp->_flags & __SSTR) == 0)
261 return (__fputwc(wc, fp, loc));
262
263 mbs = initial_mbs;
264 if ((len = wcrtomb_l(buf, wc, &mbs, loc)) == (size_t)-1) {
265 fp->_flags |= __SERR;
266 return (WEOF);
267 }
268 uio.uio_iov = &iov;
269 uio.uio_resid = len;
270 uio.uio_iovcnt = 1;
271 iov.iov_base = buf;
272 iov.iov_len = len;
273 return (__sfvwrite(fp, &uio) != EOF ? (wint_t)wc : WEOF);
274 }
275
276 /*
277 * Convert a multibyte character string argument for the %s format to a wide
278 * string representation. ``prec'' specifies the maximum number of bytes
279 * to output. If ``prec'' is greater than or equal to zero, we can't assume
280 * that the multibyte char. string ends in a null character.
281 */
282 static wchar_t *
283 __mbsconv(char *mbsarg, int prec, locale_t loc)
284 {
285 mbstate_t mbs;
286 wchar_t *convbuf, *wcp;
287 const char *p;
288 size_t insize, nchars, nconv = 0;
289 int mb_cur_max = MB_CUR_MAX_L(loc);
290
291 if (mbsarg == NULL)
292 return (NULL);
293
294 /*
295 * Supplied argument is a multibyte string; convert it to wide
296 * characters first.
297 */
298 if (prec >= 0) {
299 /*
300 * String is not guaranteed to be NUL-terminated. Find the
301 * number of characters to print.
302 */
303 p = mbsarg;
304 insize = nchars = nconv = 0;
305 mbs = initial_mbs;
306 while (nchars != (size_t)prec) {
307 nconv = mbrlen_l(p, mb_cur_max, &mbs, loc);
308 if (nconv == 0 || nconv == (size_t)-1 ||
309 nconv == (size_t)-2)
310 break;
311 p += nconv;
312 nchars++;
313 insize += nconv;
314 }
315 if (nconv == (size_t)-1 || nconv == (size_t)-2)
316 return (NULL);
317 } else {
318 insize = strlen(mbsarg);
319 nconv = 0;
320 }
321
322 /*
323 * Allocate buffer for the result and perform the conversion,
324 * converting at most `size' bytes of the input multibyte string to
325 * wide characters for printing.
326 */
327 convbuf = malloc((insize + 1) * sizeof(*convbuf));
328 if (convbuf == NULL)
329 return (NULL);
330 wcp = convbuf;
331 p = mbsarg;
332 mbs = initial_mbs;
333 while (insize != 0) {
334 nconv = mbrtowc_l(wcp, p, insize, &mbs, loc);
335 if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2)
336 break;
337 wcp++;
338 p += nconv;
339 insize -= nconv;
340 }
341 if (nconv == (size_t)-1 || nconv == (size_t)-2) {
342 free(convbuf);
343 return (NULL);
344 }
345 *wcp = L'\0';
346
347 return (convbuf);
348 }
349
350 /*
351 * MT-safe version
352 */
353 int
354 vfwprintf_l(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt0, va_list ap)
355 {
356 int ret;
357
358 NORMALIZE_LOCALE(loc);
359 FLOCKFILE(fp);
360 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
361 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
362 fp->_file >= 0)
363 ret = __sbprintf(fp, loc, fmt0, ap);
364 else
365 ret = __vfwprintf(fp, loc, fmt0, ap);
366 FUNLOCKFILE(fp);
367 return (ret);
368 }
369
370 int
371 vfwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt0, va_list ap)
372 {
373 return vfwprintf_l(fp, __current_locale(), fmt0, ap);
374 }
375
376 /*
377 * The size of the buffer we use as scratch space for integer
378 * conversions, among other things. We need enough space to
379 * write a uintmax_t in octal (plus one byte).
380 */
381 #if UINTMAX_MAX <= UINT64_MAX
382 #define BUF 32
383 #else
384 #error "BUF must be large enough to format a uintmax_t"
385 #endif
386
387 /*
388 * Non-MT-safe version
389 */
390 __private_extern__ int
391 __vfwprintf(FILE *fp, locale_t loc, const wchar_t *fmt0, va_list ap)
392 {
393 wchar_t *fmt; /* format string */
394 wchar_t ch; /* character from fmt */
395 int n, n2; /* handy integer (short term usage) */
396 wchar_t *cp; /* handy char pointer (short term usage) */
397 int flags; /* flags as above */
398 int ret; /* return value accumulator */
399 int width; /* width from format (%8d), or 0 */
400 int prec; /* precision from format; <0 for N/A */
401 wchar_t sign; /* sign prefix (' ', '+', '-', or \0) */
402 struct grouping_state gs; /* thousands' grouping info */
403 #ifndef NO_FLOATING_POINT
404 /*
405 * We can decompose the printed representation of floating
406 * point numbers into several parts, some of which may be empty:
407 *
408 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
409 * A B ---C--- D E F
410 *
411 * A: 'sign' holds this value if present; '\0' otherwise
412 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
413 * C: cp points to the string MMMNNN. Leading and trailing
414 * zeros are not in the string and must be added.
415 * D: expchar holds this character; '\0' if no exponent, e.g. %f
416 * F: at least two digits for decimal, at least one digit for hex
417 */
418 wchar_t decimal_point; /* locale specific decimal point */
419 int signflag; /* true if float is negative */
420 union { /* floating point arguments %[aAeEfFgG] */
421 double dbl;
422 long double ldbl;
423 } fparg;
424 int expt; /* integer value of exponent */
425 char expchar; /* exponent character: [eEpP\0] */
426 char *dtoaend; /* pointer to end of converted digits */
427 int expsize; /* character count for expstr */
428 int ndig; /* actual number of digits returned by dtoa */
429 wchar_t expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */
430 char *dtoaresult; /* buffer allocated by dtoa */
431 #endif
432 #ifdef VECTORS
433 union arg vval; /* Vector argument. */
434 wchar_t *pct; /* Pointer to '%' at beginning of specifier. */
435 wchar_t vsep; /* Vector separator character. */
436 #endif
437 u_long ulval; /* integer arguments %[diouxX] */
438 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
439 int base; /* base for [diouxX] conversion */
440 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
441 int realsz; /* field size expanded by dprec, sign, etc */
442 int size; /* size of converted field or string */
443 int prsize; /* max size of printed field */
444 const char *xdigs; /* digits for [xX] conversion */
445 struct io_state io; /* I/O buffering state */
446 wchar_t buf[BUF]; /* buffer with space for digits of uintmax_t */
447 wchar_t ox[2]; /* space for 0x hex-prefix */
448 union arg *argtable; /* args, built due to positional arg */
449 union arg statargtable [STATIC_ARG_TBL_SIZE];
450 int nextarg; /* 1-based argument index */
451 va_list orgap; /* original argument pointer */
452 wchar_t *convbuf; /* multibyte to wide conversion result */
453
454 static const char xdigs_lower[16] = "0123456789abcdef";
455 static const char xdigs_upper[16] = "0123456789ABCDEF";
456
457 /* BEWARE, these `goto error' on error. */
458 #define PRINT(ptr, len) do { \
459 if (io_print(&io, (ptr), (len), loc)) \
460 goto error; \
461 } while (0)
462 #define PAD(howmany, with) { \
463 if (io_pad(&io, (howmany), (with), loc)) \
464 goto error; \
465 }
466 #define PRINTANDPAD(p, ep, len, with) { \
467 if (io_printandpad(&io, (p), (ep), (len), (with), loc)) \
468 goto error; \
469 }
470 #define FLUSH() { \
471 if (io_flush(&io, loc)) \
472 goto error; \
473 }
474
475 /*
476 * Get the argument indexed by nextarg. If the argument table is
477 * built, use it to get the argument. If its not, get the next
478 * argument (and arguments must be gotten sequentially).
479 */
480 #define GETARG(type) \
481 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
482 (nextarg++, va_arg(ap, type)))
483
484 /*
485 * To extend shorts properly, we need both signed and unsigned
486 * argument extraction methods.
487 */
488 #define SARG() \
489 (flags&LONGINT ? GETARG(long) : \
490 flags&SHORTINT ? (long)(short)GETARG(int) : \
491 flags&CHARINT ? (long)(signed char)GETARG(int) : \
492 (long)GETARG(int))
493 #define UARG() \
494 (flags&LONGINT ? GETARG(u_long) : \
495 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
496 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
497 (u_long)GETARG(u_int))
498 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
499 #define SJARG() \
500 (flags&INTMAXT ? GETARG(intmax_t) : \
501 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
502 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
503 (intmax_t)GETARG(long long))
504 #define UJARG() \
505 (flags&INTMAXT ? GETARG(uintmax_t) : \
506 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
507 flags&PTRDIFFT ? (uintmax_t)(unsigned long)GETARG(ptrdiff_t) : \
508 (uintmax_t)GETARG(unsigned long long))
509
510 /*
511 * Get * arguments, including the form *nn$. Preserve the nextarg
512 * that the argument can be gotten once the type is determined.
513 */
514 #define GETASTER(val) \
515 n2 = 0; \
516 cp = fmt; \
517 while (is_digit(*cp)) { \
518 n2 = 10 * n2 + to_digit(*cp); \
519 cp++; \
520 } \
521 if (*cp == '$') { \
522 int hold = nextarg; \
523 if (argtable == NULL) { \
524 argtable = statargtable; \
525 if (__find_warguments (fmt0, orgap, &argtable)) { \
526 ret = EOF; \
527 goto error; \
528 } \
529 } \
530 nextarg = n2; \
531 val = GETARG (int); \
532 nextarg = hold; \
533 fmt = ++cp; \
534 } else { \
535 val = GETARG (int); \
536 }
537
538
539 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
540 if (prepwrite(fp) != 0) {
541 errno = EBADF;
542 return (EOF);
543 }
544 ORIENT(fp, 1);
545
546 convbuf = NULL;
547 fmt = (wchar_t *)fmt0;
548 argtable = NULL;
549 nextarg = 1;
550 va_copy(orgap, ap);
551 io_init(&io, fp);
552 ret = 0;
553 #ifndef NO_FLOATING_POINT
554 decimal_point = get_decpt(loc);
555 #endif
556
557 /*
558 * Scan the format for conversions (`%' character).
559 */
560 for (;;) {
561 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
562 /* void */;
563 if ((n = fmt - cp) != 0) {
564 if ((unsigned)ret + n > INT_MAX) {
565 ret = EOF;
566 goto error;
567 }
568 PRINT(cp, n);
569 ret += n;
570 }
571 if (ch == '\0')
572 goto done;
573 #ifdef VECTORS
574 pct = fmt;
575 #endif /* VECTORS */
576 fmt++; /* skip over '%' */
577
578 flags = 0;
579 dprec = 0;
580 width = 0;
581 prec = -1;
582 gs.grouping = NULL;
583 sign = '\0';
584 ox[1] = '\0';
585 #ifdef VECTORS
586 vsep = 'X'; /* Illegal value, changed to defaults later. */
587 #endif /* VECTORS */
588
589 rflag: ch = *fmt++;
590 reswitch: switch (ch) {
591 case ' ':
592 /*-
593 * ``If the space and + flags both appear, the space
594 * flag will be ignored.''
595 * -- ANSI X3J11
596 */
597 if (!sign)
598 sign = ' ';
599 goto rflag;
600 case '#':
601 flags |= ALT;
602 goto rflag;
603 #ifdef VECTORS
604 case ',': case ';': case ':': case '_':
605 vsep = ch;
606 goto rflag;
607 #endif /* VECTORS */
608 case '*':
609 /*-
610 * ``A negative field width argument is taken as a
611 * - flag followed by a positive field width.''
612 * -- ANSI X3J11
613 * They don't exclude field widths read from args.
614 */
615 GETASTER (width);
616 if (width >= 0)
617 goto rflag;
618 width = -width;
619 /* FALLTHROUGH */
620 case '-':
621 flags |= LADJUST;
622 goto rflag;
623 case '+':
624 sign = '+';
625 goto rflag;
626 case '\'':
627 flags |= GROUPING;
628 goto rflag;
629 case '.':
630 if ((ch = *fmt++) == '*') {
631 GETASTER (prec);
632 goto rflag;
633 }
634 prec = 0;
635 while (is_digit(ch)) {
636 prec = 10 * prec + to_digit(ch);
637 ch = *fmt++;
638 }
639 goto reswitch;
640 case '0':
641 /*-
642 * ``Note that 0 is taken as a flag, not as the
643 * beginning of a field width.''
644 * -- ANSI X3J11
645 */
646 flags |= ZEROPAD;
647 goto rflag;
648 case '1': case '2': case '3': case '4':
649 case '5': case '6': case '7': case '8': case '9':
650 n = 0;
651 do {
652 n = 10 * n + to_digit(ch);
653 ch = *fmt++;
654 } while (is_digit(ch));
655 if (ch == '$') {
656 nextarg = n;
657 if (argtable == NULL) {
658 argtable = statargtable;
659 if (__find_warguments (fmt0, orgap,
660 &argtable)) {
661 ret = EOF;
662 goto error;
663 }
664 }
665 goto rflag;
666 }
667 width = n;
668 goto reswitch;
669 #ifndef NO_FLOATING_POINT
670 case 'L':
671 flags |= LONGDBL;
672 goto rflag;
673 #endif
674 case 'h':
675 if (flags & SHORTINT) {
676 flags &= ~SHORTINT;
677 flags |= CHARINT;
678 } else
679 flags |= SHORTINT;
680 goto rflag;
681 case 'j':
682 flags |= INTMAXT;
683 goto rflag;
684 case 'l':
685 if (flags & LONGINT) {
686 flags &= ~LONGINT;
687 flags |= LLONGINT;
688 } else
689 flags |= LONGINT;
690 goto rflag;
691 case 'q':
692 flags |= LLONGINT; /* not necessarily */
693 goto rflag;
694 case 't':
695 flags |= PTRDIFFT;
696 goto rflag;
697 case 'z':
698 flags |= SIZET;
699 goto rflag;
700 case 'C':
701 flags |= LONGINT;
702 /*FALLTHROUGH*/
703 case 'c':
704 #ifdef VECTORS
705 if (flags & VECTOR)
706 break;
707 #endif /* VECTORS */
708 if (flags & LONGINT)
709 *(cp = buf) = (wchar_t)GETARG(wint_t);
710 else
711 *(cp = buf) = (wchar_t)btowc_l(GETARG(int), loc);
712 size = 1;
713 sign = '\0';
714 break;
715 case 'D':
716 flags |= LONGINT;
717 /*FALLTHROUGH*/
718 case 'd':
719 case 'i':
720 #ifdef VECTORS
721 if (flags & VECTOR)
722 break;
723 #endif /* VECTORS */
724 if (flags & INTMAX_SIZE) {
725 ujval = SJARG();
726 if ((intmax_t)ujval < 0) {
727 ujval = -ujval;
728 sign = '-';
729 }
730 } else {
731 ulval = SARG();
732 if ((long)ulval < 0) {
733 ulval = -ulval;
734 sign = '-';
735 }
736 }
737 base = 10;
738 goto number;
739 #ifndef NO_FLOATING_POINT
740 case 'a':
741 case 'A':
742 #ifdef VECTORS
743 if (flags & VECTOR) {
744 flags |= FPT;
745 break;
746 }
747 #endif /* VECTORS */
748 if (ch == 'a') {
749 ox[1] = 'x';
750 xdigs = xdigs_lower;
751 expchar = 'p';
752 } else {
753 ox[1] = 'X';
754 xdigs = xdigs_upper;
755 expchar = 'P';
756 }
757 if (prec >= 0)
758 prec++;
759 #ifdef LDBL_COMPAT
760 fparg.dbl = GETARG(double);
761 dtoaresult =
762 __hdtoa(fparg.dbl, xdigs, prec,
763 &expt, &signflag, &dtoaend);
764 #else /* !LDBL_COMPAT */
765 if (flags & LONGDBL) {
766 fparg.ldbl = GETARG(long double);
767 dtoaresult =
768 __hldtoa(fparg.ldbl, xdigs, prec,
769 &expt, &signflag, &dtoaend);
770 } else {
771 fparg.dbl = GETARG(double);
772 dtoaresult =
773 __hdtoa(fparg.dbl, xdigs, prec,
774 &expt, &signflag, &dtoaend);
775 }
776 #endif /* LDBL_COMPAT */
777 if (prec < 0)
778 prec = dtoaend - dtoaresult;
779 if (expt == INT_MAX)
780 ox[1] = '\0';
781 if (convbuf != NULL)
782 free(convbuf);
783 ndig = dtoaend - dtoaresult;
784 cp = convbuf = __mbsconv(dtoaresult, -1, loc);
785 freedtoa(dtoaresult);
786 goto fp_common;
787 case 'e':
788 case 'E':
789 #ifdef VECTORS
790 if (flags & VECTOR) {
791 flags |= FPT;
792 break;
793 }
794 #endif /* VECTORS */
795 expchar = ch;
796 if (prec < 0) /* account for digit before decpt */
797 prec = DEFPREC + 1;
798 else
799 prec++;
800 goto fp_begin;
801 case 'f':
802 case 'F':
803 #ifdef VECTORS
804 if (flags & VECTOR) {
805 flags |= FPT;
806 break;
807 }
808 #endif /* VECTORS */
809 expchar = '\0';
810 goto fp_begin;
811 case 'g':
812 case 'G':
813 #ifdef VECTORS
814 if (flags & VECTOR) {
815 flags |= FPT;
816 break;
817 }
818 #endif /* VECTORS */
819 expchar = ch - ('g' - 'e');
820 if (prec == 0)
821 prec = 1;
822 fp_begin:
823 if (prec < 0)
824 prec = DEFPREC;
825 if (convbuf != NULL)
826 free(convbuf);
827 #ifdef LDBL_COMPAT
828 fparg.dbl = GETARG(double);
829 dtoaresult =
830 dtoa(fparg.dbl, expchar ? 2 : 3, prec,
831 &expt, &signflag, &dtoaend);
832 if (expt == 9999)
833 expt = INT_MAX;
834 #else /* !LDBL_COMPAT */
835 if (flags & LONGDBL) {
836 fparg.ldbl = GETARG(long double);
837 dtoaresult =
838 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
839 &expt, &signflag, &dtoaend);
840 } else {
841 fparg.dbl = GETARG(double);
842 dtoaresult =
843 dtoa(fparg.dbl, expchar ? 2 : 3, prec,
844 &expt, &signflag, &dtoaend);
845 if (expt == 9999)
846 expt = INT_MAX;
847 }
848 #endif /* LDBL_COMPAT */
849 ndig = dtoaend - dtoaresult;
850 cp = convbuf = __mbsconv(dtoaresult, -1, loc);
851 freedtoa(dtoaresult);
852 fp_common:
853 if (signflag)
854 sign = '-';
855 if (expt == INT_MAX) { /* inf or nan */
856 if (*cp == 'N') {
857 cp = (ch >= 'a') ? L"nan" : L"NAN";
858 sign = '\0';
859 } else
860 cp = (ch >= 'a') ? L"inf" : L"INF";
861 size = 3;
862 flags &= ~ZEROPAD;
863 break;
864 }
865 flags |= FPT;
866 if (ch == 'g' || ch == 'G') {
867 if (expt > -4 && expt <= prec) {
868 /* Make %[gG] smell like %[fF] */
869 expchar = '\0';
870 if (flags & ALT)
871 prec -= expt;
872 else
873 prec = ndig - expt;
874 if (prec < 0)
875 prec = 0;
876 } else {
877 /*
878 * Make %[gG] smell like %[eE], but
879 * trim trailing zeroes if no # flag.
880 */
881 if (!(flags & ALT))
882 prec = ndig;
883 }
884 }
885 if (expchar) {
886 expsize = exponent(expstr, expt - 1, expchar);
887 size = expsize + prec;
888 if (prec > 1 || flags & ALT)
889 ++size;
890 } else {
891 /* space for digits before decimal point */
892 if (expt > 0)
893 size = expt;
894 else /* "0" */
895 size = 1;
896 /* space for decimal pt and following digits */
897 if (prec || flags & ALT)
898 size += prec + 1;
899 if ((flags & GROUPING) && expt > 0)
900 size += grouping_init(&gs, expt, loc);
901 }
902 break;
903 #endif /* !NO_FLOATING_POINT */
904 case 'n':
905 {
906 /*
907 * Assignment-like behavior is specified if the
908 * value overflows or is otherwise unrepresentable.
909 * C99 says to use `signed char' for %hhn conversions.
910 */
911 void *ptr = GETARG(void *);
912 if (ptr == NULL)
913 continue;
914 else if (flags & LLONGINT)
915 *(long long *)ptr = ret;
916 else if (flags & SIZET)
917 *(ssize_t *)ptr = (ssize_t)ret;
918 else if (flags & PTRDIFFT)
919 *(ptrdiff_t *)ptr = ret;
920 else if (flags & INTMAXT)
921 *(intmax_t *)ptr = ret;
922 else if (flags & LONGINT)
923 *(long *)ptr = ret;
924 else if (flags & SHORTINT)
925 *(short *)ptr = ret;
926 else if (flags & CHARINT)
927 *(signed char *)ptr = ret;
928 else
929 *(int *)ptr = ret;
930 continue; /* no output */
931 }
932 case 'O':
933 flags |= LONGINT;
934 /*FALLTHROUGH*/
935 case 'o':
936 #ifdef VECTORS
937 if (flags & VECTOR)
938 break;
939 #endif /* VECTORS */
940 if (flags & INTMAX_SIZE)
941 ujval = UJARG();
942 else
943 ulval = UARG();
944 base = 8;
945 goto nosign;
946 case 'p':
947 /*-
948 * ``The argument shall be a pointer to void. The
949 * value of the pointer is converted to a sequence
950 * of printable characters, in an implementation-
951 * defined manner.''
952 * -- ANSI X3J11
953 */
954 #ifdef VECTORS
955 if (flags & VECTOR)
956 break;
957 #endif /* VECTORS */
958 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
959 base = 16;
960 xdigs = xdigs_lower;
961 flags = flags | INTMAXT;
962 ox[1] = 'x';
963 goto nosign;
964 case 'S':
965 flags |= LONGINT;
966 /*FALLTHROUGH*/
967 case 's':
968 if (flags & LONGINT) {
969 if ((cp = GETARG(wchar_t *)) == NULL)
970 cp = L"(null)";
971 } else {
972 char *mbp;
973
974 if (convbuf != NULL)
975 free(convbuf);
976 if ((mbp = GETARG(char *)) == NULL)
977 cp = L"(null)";
978 else {
979 convbuf = __mbsconv(mbp, prec, loc);
980 if (convbuf == NULL) {
981 fp->_flags |= __SERR;
982 goto error;
983 }
984 cp = convbuf;
985 }
986 }
987 #if 0 // wcsnlen needs API review first
988 size = (prec >= 0) ? wcsnlen(cp, prec) : wcslen(cp);
989 #else
990 size = wcslen(cp);
991 if(prec >= 0 && prec < size)
992 size = prec;
993 #endif
994 sign = '\0';
995 break;
996 case 'U':
997 flags |= LONGINT;
998 /*FALLTHROUGH*/
999 case 'u':
1000 #ifdef VECTORS
1001 if (flags & VECTOR)
1002 break;
1003 #endif /* VECTORS */
1004 if (flags & INTMAX_SIZE)
1005 ujval = UJARG();
1006 else
1007 ulval = UARG();
1008 base = 10;
1009 goto nosign;
1010 case 'X':
1011 xdigs = xdigs_upper;
1012 goto hex;
1013 case 'x':
1014 xdigs = xdigs_lower;
1015 hex:
1016 #ifdef VECTORS
1017 if (flags & VECTOR)
1018 break;
1019 #endif /* VECTORS */
1020 if (flags & INTMAX_SIZE)
1021 ujval = UJARG();
1022 else
1023 ulval = UARG();
1024 base = 16;
1025 /* leading 0x/X only if non-zero */
1026 if (flags & ALT &&
1027 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
1028 ox[1] = ch;
1029
1030 flags &= ~GROUPING;
1031 /* unsigned conversions */
1032 nosign: sign = '\0';
1033 /*-
1034 * ``... diouXx conversions ... if a precision is
1035 * specified, the 0 flag will be ignored.''
1036 * -- ANSI X3J11
1037 * except for %#.0o and zero value
1038 */
1039 number: if ((dprec = prec) >= 0)
1040 flags &= ~ZEROPAD;
1041
1042 /*-
1043 * ``The result of converting a zero value with an
1044 * explicit precision of zero is no characters.''
1045 * -- ANSI X3J11
1046 *
1047 * ``The C Standard is clear enough as is. The call
1048 * printf("%#.0o", 0) should print 0.''
1049 * -- Defect Report #151
1050 */
1051 cp = buf + BUF;
1052 if (flags & INTMAX_SIZE) {
1053 if (ujval != 0 || prec != 0 ||
1054 (flags & ALT && base == 8))
1055 cp = __ujtoa(ujval, cp, base,
1056 flags & ALT, xdigs);
1057 } else {
1058 if (ulval != 0 || prec != 0 ||
1059 (flags & ALT && base == 8))
1060 cp = __ultoa(ulval, cp, base,
1061 flags & ALT, xdigs);
1062 }
1063 size = buf + BUF - cp;
1064 if (size > BUF) /* should never happen */
1065 LIBC_ABORT("size (%d) > BUF (%d)", size, BUF);
1066 if ((flags & GROUPING) && size != 0)
1067 size += grouping_init(&gs, size, loc);
1068 break;
1069 #ifdef VECTORS
1070 case 'v':
1071 flags |= VECTOR;
1072 goto rflag;
1073 #endif /* VECTORS */
1074 default: /* "%?" prints ?, unless ? is NUL */
1075 if (ch == '\0')
1076 goto done;
1077 /* pretend it was %c with argument ch */
1078 cp = buf;
1079 *cp = ch;
1080 size = 1;
1081 sign = '\0';
1082 break;
1083 }
1084
1085 #ifdef VECTORS
1086 if (flags & VECTOR) {
1087 /*
1088 * Do the minimum amount of work necessary to construct
1089 * a format specifier that can be used to recursively
1090 * call vfprintf() for each element in the vector.
1091 */
1092 int i, j; /* Counter. */
1093 int vcnt; /* Number of elements in vector. */
1094 char *vfmt; /* Pointer to format specifier. */
1095 #define EXTRAHH 2
1096 char vfmt_buf[32 + EXTRAHH]; /* Static buffer for format spec. */
1097 int vwidth = 0; /* Width specified via '*'. */
1098 int vprec = 0; /* Precision specified via '*'. */
1099 char *vstr; /* Used for asprintf(). */
1100 int vlen; /* Length returned by asprintf(). */
1101 enum {
1102 V_CHAR, V_SHORT, V_INT,
1103 V_PCHAR, V_PSHORT, V_PINT,
1104 V_FLOAT,
1105 #ifdef V64TYPE
1106 V_LONGLONG, V_PLONGLONG,
1107 V_DOUBLE,
1108 #endif /* V64TYPE */
1109 } vtype;
1110
1111 vval.vectorarg = GETARG(VECTORTYPE);
1112 /*
1113 * Set vfmt. If vfmt_buf may not be big enough,
1114 * malloc() space, taking care to free it later.
1115 * (EXTRAHH is for possible extra "hh")
1116 */
1117 if (&fmt[-1] - pct + EXTRAHH < sizeof(vfmt_buf))
1118 vfmt = vfmt_buf;
1119 else
1120 vfmt = (char *)malloc(&fmt[-1] - pct + EXTRAHH + 1);
1121
1122 /* Set the separator character, if not specified. */
1123 if (vsep == 'X') {
1124 if (ch == 'c')
1125 vsep = '\0';
1126 else
1127 vsep = ' ';
1128 }
1129
1130 /* Create the format specifier. */
1131 for (i = j = 0; i < &fmt[-1] - pct; i++) {
1132 switch (pct[i]) {
1133 case ',': case ';': case ':': case '_':
1134 case 'v': case 'h': case 'l':
1135 /* Ignore. */
1136 break;
1137 case '*':
1138 if (pct[i - 1] != '.')
1139 vwidth = 1;
1140 else
1141 vprec = 1;
1142 /* FALLTHROUGH */
1143 default:
1144 vfmt[j++] = pct[i];
1145 }
1146 }
1147
1148 /*
1149 * Determine the number of elements in the vector and
1150 * finish up the format specifier.
1151 */
1152 if (flags & SHORTINT) {
1153 switch (ch) {
1154 case 'c':
1155 vtype = V_SHORT;
1156 break;
1157 case 'p':
1158 vtype = V_PSHORT;
1159 break;
1160 default:
1161 vfmt[j++] = 'h';
1162 vtype = V_SHORT;
1163 break;
1164 }
1165 vcnt = 8;
1166 } else if (flags & LONGINT) {
1167 vcnt = 4;
1168 vtype = (ch == 'p') ? V_PINT : V_INT;
1169 #ifdef V64TYPE
1170 } else if (flags & LLONGINT) {
1171 switch (ch) {
1172 case 'a':
1173 case 'A':
1174 case 'e':
1175 case 'E':
1176 case 'f':
1177 case 'g':
1178 case 'G':
1179 vcnt = 2;
1180 vtype = V_DOUBLE;
1181 break;
1182 case 'd':
1183 case 'i':
1184 case 'u':
1185 case 'o':
1186 case 'p':
1187 case 'x':
1188 case 'X':
1189 vfmt[j++] = 'l';
1190 vfmt[j++] = 'l';
1191 vcnt = 2;
1192 vtype = (ch == 'p') ? V_PLONGLONG : V_LONGLONG;
1193 break;
1194 default:
1195 /*
1196 * The default case should never
1197 * happen.
1198 */
1199 case 'c':
1200 vcnt = 16;
1201 vtype = V_CHAR;
1202 }
1203 #endif /* V64TYPE */
1204 } else {
1205 switch (ch) {
1206 case 'a':
1207 case 'A':
1208 case 'e':
1209 case 'E':
1210 case 'f':
1211 case 'g':
1212 case 'G':
1213 vcnt = 4;
1214 vtype = V_FLOAT;
1215 break;
1216 default:
1217 /*
1218 * The default case should never
1219 * happen.
1220 */
1221 case 'd':
1222 case 'i':
1223 case 'u':
1224 case 'o':
1225 case 'x':
1226 case 'X':
1227 vfmt[j++] = 'h';
1228 vfmt[j++] = 'h';
1229 /* drop through */
1230 case 'p':
1231 case 'c':
1232 vcnt = 16;
1233 vtype = (ch == 'p') ? V_PCHAR : V_CHAR;
1234 }
1235 }
1236 vfmt[j++] = ch;
1237 vfmt[j++] = '\0';
1238
1239 /* Get a vector element. */
1240 #ifdef V64TYPE
1241 #define VPRINT(type, ind, args...) do { \
1242 switch (type) { \
1243 case V_CHAR: \
1244 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1245 break; \
1246 case V_PCHAR: \
1247 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1248 break; \
1249 case V_SHORT: \
1250 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1251 break; \
1252 case V_PSHORT: \
1253 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1254 break; \
1255 case V_INT: \
1256 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1257 break; \
1258 case V_PINT: \
1259 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1260 break; \
1261 case V_LONGLONG: \
1262 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \
1263 break; \
1264 case V_PLONGLONG: \
1265 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vulonglongarg[ind]); \
1266 break; \
1267 case V_FLOAT: \
1268 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1269 break; \
1270 case V_DOUBLE: \
1271 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \
1272 break; \
1273 } \
1274 ret += vlen; \
1275 PRINT(vstr, vlen); \
1276 free(vstr); \
1277 } while (0)
1278 #else /* !V64TYPE */
1279 #define VPRINT(type, ind, args...) do { \
1280 switch (type) { \
1281 case V_CHAR: \
1282 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1283 break; \
1284 case V_PCHAR: \
1285 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1286 break; \
1287 case V_SHORT: \
1288 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1289 break; \
1290 case V_PSHORT: \
1291 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1292 break; \
1293 case V_INT: \
1294 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1295 break; \
1296 case V_PINT: \
1297 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1298 break; \
1299 case V_FLOAT: \
1300 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1301 break; \
1302 } \
1303 ret += vlen; \
1304 PRINT(vstr, vlen); \
1305 free(vstr); \
1306 } while (0)
1307 #endif /* V64TYPE */
1308
1309 /* Actually print. */
1310 if (vwidth == 0) {
1311 if (vprec == 0) {
1312 /* First element. */
1313 VPRINT(vtype, 0);
1314 for (i = 1; i < vcnt; i++) {
1315 /* Separator. */
1316 if(vsep)
1317 PRINT(&vsep, 1);
1318
1319 /* Element. */
1320 VPRINT(vtype, i);
1321 }
1322 } else {
1323 /* First element. */
1324 VPRINT(vtype, 0, prec);
1325 for (i = 1; i < vcnt; i++) {
1326 /* Separator. */
1327 if(vsep)
1328 PRINT(&vsep, 1);
1329
1330 /* Element. */
1331 VPRINT(vtype, i, prec);
1332 }
1333 }
1334 } else {
1335 if (vprec == 0) {
1336 /* First element. */
1337 VPRINT(vtype, 0, width);
1338 for (i = 1; i < vcnt; i++) {
1339 /* Separator. */
1340 if(vsep)
1341 PRINT(&vsep, 1);
1342
1343 /* Element. */
1344 VPRINT(vtype, i, width);
1345 }
1346 } else {
1347 /* First element. */
1348 VPRINT(vtype, 0, width, prec);
1349 for (i = 1; i < vcnt; i++) {
1350 /* Separator. */
1351 if(vsep)
1352 PRINT(&vsep, 1);
1353
1354 /* Element. */
1355 VPRINT(vtype, i, width, prec);
1356 }
1357 }
1358 }
1359 #undef VPRINT
1360
1361 if (vfmt != vfmt_buf)
1362 free(vfmt);
1363
1364 continue;
1365 }
1366 #endif /* VECTORS */
1367 /*
1368 * All reasonable formats wind up here. At this point, `cp'
1369 * points to a string which (if not flags&LADJUST) should be
1370 * padded out to `width' places. If flags&ZEROPAD, it should
1371 * first be prefixed by any sign or other prefix; otherwise,
1372 * it should be blank padded before the prefix is emitted.
1373 * After any left-hand padding and prefixing, emit zeroes
1374 * required by a decimal [diouxX] precision, then print the
1375 * string proper, then emit zeroes required by any leftover
1376 * floating precision; finally, if LADJUST, pad with blanks.
1377 *
1378 * Compute actual size, so we know how much to pad.
1379 * size excludes decimal prec; realsz includes it.
1380 */
1381 realsz = dprec > size ? dprec : size;
1382 if (sign)
1383 realsz++;
1384 if (ox[1])
1385 realsz += 2;
1386
1387 prsize = width > realsz ? width : realsz;
1388 if ((unsigned)ret + prsize > INT_MAX) {
1389 ret = EOF;
1390 goto error;
1391 }
1392
1393 /* right-adjusting blank padding */
1394 if ((flags & (LADJUST|ZEROPAD)) == 0)
1395 PAD(width - realsz, blanks);
1396
1397 /* prefix */
1398 if (sign)
1399 PRINT(&sign, 1);
1400
1401 if (ox[1]) { /* ox[1] is either x, X, or \0 */
1402 ox[0] = '0';
1403 PRINT(ox, 2);
1404 }
1405
1406 /* right-adjusting zero padding */
1407 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1408 PAD(width - realsz, zeroes);
1409
1410 /* the string or number proper */
1411 #ifndef NO_FLOATING_POINT
1412 if ((flags & FPT) == 0) {
1413 #endif
1414 /* leading zeroes from decimal precision */
1415 PAD(dprec - size, zeroes);
1416 if (gs.grouping) {
1417 if (grouping_print(&gs, &io, cp, buf+BUF, loc) < 0)
1418 goto error;
1419 } else {
1420 PRINT(cp, size);
1421 }
1422 #ifndef NO_FLOATING_POINT
1423 } else { /* glue together f_p fragments */
1424 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
1425 if (expt <= 0) {
1426 PRINT(zeroes, 1);
1427 if (prec || flags & ALT)
1428 PRINT(&decimal_point, 1);
1429 PAD(-expt, zeroes);
1430 /* already handled initial 0's */
1431 prec += expt;
1432 } else {
1433 if (gs.grouping) {
1434 n = grouping_print(&gs, &io,
1435 cp, convbuf + ndig, loc);
1436 if (n < 0)
1437 goto error;
1438 cp += n;
1439 } else {
1440 PRINTANDPAD(cp, convbuf + ndig,
1441 expt, zeroes);
1442 cp += expt;
1443 }
1444 if (prec || flags & ALT)
1445 PRINT(&decimal_point, 1);
1446 }
1447 PRINTANDPAD(cp, convbuf + ndig, prec, zeroes);
1448 } else { /* %[eE] or sufficiently long %[gG] */
1449 if (prec > 1 || flags & ALT) {
1450 buf[0] = *cp++;
1451 buf[1] = decimal_point;
1452 PRINT(buf, 2);
1453 PRINT(cp, ndig-1);
1454 PAD(prec - ndig, zeroes);
1455 } else /* XeYYY */
1456 PRINT(cp, 1);
1457 PRINT(expstr, expsize);
1458 }
1459 }
1460 #endif
1461 /* left-adjusting padding (always blank) */
1462 if (flags & LADJUST)
1463 PAD(width - realsz, blanks);
1464
1465 /* finally, adjust ret */
1466 ret += prsize;
1467
1468 FLUSH(); /* copy out the I/O vectors */
1469 }
1470 done:
1471 FLUSH();
1472 error:
1473 va_end(orgap);
1474 if (convbuf != NULL)
1475 free(convbuf);
1476 if (__sferror(fp))
1477 ret = EOF;
1478 if ((argtable != NULL) && (argtable != statargtable))
1479 free (argtable);
1480 return (ret);
1481 /* NOTREACHED */
1482 }