]> git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/vfwprintf.c
f623fff3e4e7dcda7fb15685173fe8d3e2041ce6
[apple/libc.git] / stdio / FreeBSD / vfwprintf.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 if (flags & LONGDBL) {
760 fparg.ldbl = GETARG(long double);
761 dtoaresult =
762 __hldtoa(fparg.ldbl, xdigs, prec,
763 &expt, &signflag, &dtoaend);
764 } else {
765 fparg.dbl = GETARG(double);
766 dtoaresult =
767 __hdtoa(fparg.dbl, xdigs, prec,
768 &expt, &signflag, &dtoaend);
769 }
770 if (prec < 0)
771 prec = dtoaend - dtoaresult;
772 if (expt == INT_MAX)
773 ox[1] = '\0';
774 free(convbuf);
775 ndig = dtoaend - dtoaresult;
776 cp = convbuf = __mbsconv(dtoaresult, -1, loc);
777 freedtoa(dtoaresult);
778 goto fp_common;
779 case 'e':
780 case 'E':
781 #ifdef VECTORS
782 if (flags & VECTOR) {
783 flags |= FPT;
784 break;
785 }
786 #endif /* VECTORS */
787 expchar = ch;
788 if (prec < 0) /* account for digit before decpt */
789 prec = DEFPREC + 1;
790 else
791 prec++;
792 goto fp_begin;
793 case 'f':
794 case 'F':
795 #ifdef VECTORS
796 if (flags & VECTOR) {
797 flags |= FPT;
798 break;
799 }
800 #endif /* VECTORS */
801 expchar = '\0';
802 goto fp_begin;
803 case 'g':
804 case 'G':
805 #ifdef VECTORS
806 if (flags & VECTOR) {
807 flags |= FPT;
808 break;
809 }
810 #endif /* VECTORS */
811 expchar = ch - ('g' - 'e');
812 if (prec == 0)
813 prec = 1;
814 fp_begin:
815 if (prec < 0)
816 prec = DEFPREC;
817 free(convbuf);
818 if (flags & LONGDBL) {
819 fparg.ldbl = GETARG(long double);
820 dtoaresult =
821 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
822 &expt, &signflag, &dtoaend);
823 } else {
824 fparg.dbl = GETARG(double);
825 dtoaresult =
826 dtoa(fparg.dbl, expchar ? 2 : 3, prec,
827 &expt, &signflag, &dtoaend);
828 if (expt == 9999)
829 expt = INT_MAX;
830 }
831 ndig = dtoaend - dtoaresult;
832 cp = convbuf = __mbsconv(dtoaresult, -1, loc);
833 freedtoa(dtoaresult);
834 fp_common:
835 if (signflag)
836 sign = '-';
837 if (expt == INT_MAX) { /* inf or nan */
838 if (*cp == 'N') {
839 cp = (ch >= 'a') ? L"nan" : L"NAN";
840 sign = '\0';
841 } else
842 cp = (ch >= 'a') ? L"inf" : L"INF";
843 size = 3;
844 flags &= ~ZEROPAD;
845 break;
846 }
847 flags |= FPT;
848 if (ch == 'g' || ch == 'G') {
849 if (expt > -4 && expt <= prec) {
850 /* Make %[gG] smell like %[fF] */
851 expchar = '\0';
852 if (flags & ALT)
853 prec -= expt;
854 else
855 prec = ndig - expt;
856 if (prec < 0)
857 prec = 0;
858 } else {
859 /*
860 * Make %[gG] smell like %[eE], but
861 * trim trailing zeroes if no # flag.
862 */
863 if (!(flags & ALT))
864 prec = ndig;
865 }
866 }
867 if (expchar) {
868 expsize = exponent(expstr, expt - 1, expchar);
869 size = expsize + prec;
870 if (prec > 1 || flags & ALT)
871 ++size;
872 } else {
873 /* space for digits before decimal point */
874 if (expt > 0)
875 size = expt;
876 else /* "0" */
877 size = 1;
878 /* space for decimal pt and following digits */
879 if (prec || flags & ALT)
880 size += prec + 1;
881 if ((flags & GROUPING) && expt > 0)
882 size += grouping_init(&gs, expt, loc);
883 }
884 break;
885 #endif /* !NO_FLOATING_POINT */
886 case 'n':
887 {
888 /*
889 * Assignment-like behavior is specified if the
890 * value overflows or is otherwise unrepresentable.
891 * C99 says to use `signed char' for %hhn conversions.
892 */
893 void *ptr = GETARG(void *);
894 if (ptr == NULL)
895 continue;
896 else if (flags & LLONGINT)
897 *(long long *)ptr = ret;
898 else if (flags & SIZET)
899 *(ssize_t *)ptr = (ssize_t)ret;
900 else if (flags & PTRDIFFT)
901 *(ptrdiff_t *)ptr = ret;
902 else if (flags & INTMAXT)
903 *(intmax_t *)ptr = ret;
904 else if (flags & LONGINT)
905 *(long *)ptr = ret;
906 else if (flags & SHORTINT)
907 *(short *)ptr = ret;
908 else if (flags & CHARINT)
909 *(signed char *)ptr = ret;
910 else
911 *(int *)ptr = ret;
912 continue; /* no output */
913 }
914 case 'O':
915 flags |= LONGINT;
916 /*FALLTHROUGH*/
917 case 'o':
918 #ifdef VECTORS
919 if (flags & VECTOR)
920 break;
921 #endif /* VECTORS */
922 if (flags & INTMAX_SIZE)
923 ujval = UJARG();
924 else
925 ulval = UARG();
926 base = 8;
927 goto nosign;
928 case 'p':
929 /*-
930 * ``The argument shall be a pointer to void. The
931 * value of the pointer is converted to a sequence
932 * of printable characters, in an implementation-
933 * defined manner.''
934 * -- ANSI X3J11
935 */
936 #ifdef VECTORS
937 if (flags & VECTOR)
938 break;
939 #endif /* VECTORS */
940 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
941 base = 16;
942 xdigs = xdigs_lower;
943 flags = flags | INTMAXT;
944 ox[1] = 'x';
945 goto nosign;
946 case 'S':
947 flags |= LONGINT;
948 /*FALLTHROUGH*/
949 case 's':
950 if (flags & LONGINT) {
951 if ((cp = GETARG(wchar_t *)) == NULL)
952 cp = L"(null)";
953 } else {
954 char *mbp;
955
956 free(convbuf);
957 if ((mbp = GETARG(char *)) == NULL) {
958 convbuf = NULL;
959 cp = L"(null)";
960 } else {
961 convbuf = __mbsconv(mbp, prec, loc);
962 if (convbuf == NULL) {
963 fp->_flags |= __SERR;
964 goto error;
965 }
966 cp = convbuf;
967 }
968 }
969 #if 0 // wcsnlen needs API review first
970 size = (prec >= 0) ? wcsnlen(cp, prec) : wcslen(cp);
971 #else
972 size = wcslen(cp);
973 if(prec >= 0 && prec < size)
974 size = prec;
975 #endif
976 sign = '\0';
977 break;
978 case 'U':
979 flags |= LONGINT;
980 /*FALLTHROUGH*/
981 case 'u':
982 #ifdef VECTORS
983 if (flags & VECTOR)
984 break;
985 #endif /* VECTORS */
986 if (flags & INTMAX_SIZE)
987 ujval = UJARG();
988 else
989 ulval = UARG();
990 base = 10;
991 goto nosign;
992 case 'X':
993 xdigs = xdigs_upper;
994 goto hex;
995 case 'x':
996 xdigs = xdigs_lower;
997 hex:
998 #ifdef VECTORS
999 if (flags & VECTOR)
1000 break;
1001 #endif /* VECTORS */
1002 if (flags & INTMAX_SIZE)
1003 ujval = UJARG();
1004 else
1005 ulval = UARG();
1006 base = 16;
1007 /* leading 0x/X only if non-zero */
1008 if (flags & ALT &&
1009 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
1010 ox[1] = ch;
1011
1012 flags &= ~GROUPING;
1013 /* unsigned conversions */
1014 nosign: sign = '\0';
1015 /*-
1016 * ``... diouXx conversions ... if a precision is
1017 * specified, the 0 flag will be ignored.''
1018 * -- ANSI X3J11
1019 * except for %#.0o and zero value
1020 */
1021 number: if ((dprec = prec) >= 0)
1022 flags &= ~ZEROPAD;
1023
1024 /*-
1025 * ``The result of converting a zero value with an
1026 * explicit precision of zero is no characters.''
1027 * -- ANSI X3J11
1028 *
1029 * ``The C Standard is clear enough as is. The call
1030 * printf("%#.0o", 0) should print 0.''
1031 * -- Defect Report #151
1032 */
1033 cp = buf + BUF;
1034 if (flags & INTMAX_SIZE) {
1035 if (ujval != 0 || prec != 0 ||
1036 (flags & ALT && base == 8))
1037 cp = __ujtoa(ujval, cp, base,
1038 flags & ALT, xdigs);
1039 } else {
1040 if (ulval != 0 || prec != 0 ||
1041 (flags & ALT && base == 8))
1042 cp = __ultoa(ulval, cp, base,
1043 flags & ALT, xdigs);
1044 }
1045 size = buf + BUF - cp;
1046 if (size > BUF) /* should never happen */
1047 LIBC_ABORT("size (%d) > BUF (%d)", size, BUF);
1048 if ((flags & GROUPING) && size != 0)
1049 size += grouping_init(&gs, size, loc);
1050 break;
1051 #ifdef VECTORS
1052 case 'v':
1053 flags |= VECTOR;
1054 goto rflag;
1055 #endif /* VECTORS */
1056 default: /* "%?" prints ?, unless ? is NUL */
1057 if (ch == '\0')
1058 goto done;
1059 /* pretend it was %c with argument ch */
1060 cp = buf;
1061 *cp = ch;
1062 size = 1;
1063 sign = '\0';
1064 break;
1065 }
1066
1067 #ifdef VECTORS
1068 if (flags & VECTOR) {
1069 /*
1070 * Do the minimum amount of work necessary to construct
1071 * a format specifier that can be used to recursively
1072 * call vfprintf() for each element in the vector.
1073 */
1074 int i, j; /* Counter. */
1075 int vcnt; /* Number of elements in vector. */
1076 char *vfmt; /* Pointer to format specifier. */
1077 #define EXTRAHH 2
1078 char vfmt_buf[32 + EXTRAHH]; /* Static buffer for format spec. */
1079 int vwidth = 0; /* Width specified via '*'. */
1080 int vprec = 0; /* Precision specified via '*'. */
1081 char *vstr; /* Used for asprintf(). */
1082 int vlen; /* Length returned by asprintf(). */
1083 enum {
1084 V_CHAR, V_SHORT, V_INT,
1085 V_PCHAR, V_PSHORT, V_PINT,
1086 V_FLOAT,
1087 #ifdef V64TYPE
1088 V_LONGLONG, V_PLONGLONG,
1089 V_DOUBLE,
1090 #endif /* V64TYPE */
1091 } vtype;
1092
1093 vval.vectorarg = GETARG(VECTORTYPE);
1094 /*
1095 * Set vfmt. If vfmt_buf may not be big enough,
1096 * malloc() space, taking care to free it later.
1097 * (EXTRAHH is for possible extra "hh")
1098 */
1099 if (&fmt[-1] - pct + EXTRAHH < sizeof(vfmt_buf))
1100 vfmt = vfmt_buf;
1101 else
1102 vfmt = (char *)malloc(&fmt[-1] - pct + EXTRAHH + 1);
1103
1104 /* Set the separator character, if not specified. */
1105 if (vsep == 'X') {
1106 if (ch == 'c')
1107 vsep = '\0';
1108 else
1109 vsep = ' ';
1110 }
1111
1112 /* Create the format specifier. */
1113 for (i = j = 0; i < &fmt[-1] - pct; i++) {
1114 switch (pct[i]) {
1115 case ',': case ';': case ':': case '_':
1116 case 'v': case 'h': case 'l':
1117 /* Ignore. */
1118 break;
1119 case '*':
1120 if (pct[i - 1] != '.')
1121 vwidth = 1;
1122 else
1123 vprec = 1;
1124 /* FALLTHROUGH */
1125 default:
1126 vfmt[j++] = pct[i];
1127 }
1128 }
1129
1130 /*
1131 * Determine the number of elements in the vector and
1132 * finish up the format specifier.
1133 */
1134 if (flags & SHORTINT) {
1135 switch (ch) {
1136 case 'c':
1137 vtype = V_SHORT;
1138 break;
1139 case 'p':
1140 vtype = V_PSHORT;
1141 break;
1142 default:
1143 vfmt[j++] = 'h';
1144 vtype = V_SHORT;
1145 break;
1146 }
1147 vcnt = 8;
1148 } else if (flags & LONGINT) {
1149 vcnt = 4;
1150 vtype = (ch == 'p') ? V_PINT : V_INT;
1151 #ifdef V64TYPE
1152 } else if (flags & LLONGINT) {
1153 switch (ch) {
1154 case 'a':
1155 case 'A':
1156 case 'e':
1157 case 'E':
1158 case 'f':
1159 case 'g':
1160 case 'G':
1161 vcnt = 2;
1162 vtype = V_DOUBLE;
1163 break;
1164 case 'd':
1165 case 'i':
1166 case 'u':
1167 case 'o':
1168 case 'p':
1169 case 'x':
1170 case 'X':
1171 vfmt[j++] = 'l';
1172 vfmt[j++] = 'l';
1173 vcnt = 2;
1174 vtype = (ch == 'p') ? V_PLONGLONG : V_LONGLONG;
1175 break;
1176 default:
1177 /*
1178 * The default case should never
1179 * happen.
1180 */
1181 case 'c':
1182 vcnt = 16;
1183 vtype = V_CHAR;
1184 }
1185 #endif /* V64TYPE */
1186 } else {
1187 switch (ch) {
1188 case 'a':
1189 case 'A':
1190 case 'e':
1191 case 'E':
1192 case 'f':
1193 case 'g':
1194 case 'G':
1195 vcnt = 4;
1196 vtype = V_FLOAT;
1197 break;
1198 default:
1199 /*
1200 * The default case should never
1201 * happen.
1202 */
1203 case 'd':
1204 case 'i':
1205 case 'u':
1206 case 'o':
1207 case 'x':
1208 case 'X':
1209 vfmt[j++] = 'h';
1210 vfmt[j++] = 'h';
1211 /* drop through */
1212 case 'p':
1213 case 'c':
1214 vcnt = 16;
1215 vtype = (ch == 'p') ? V_PCHAR : V_CHAR;
1216 }
1217 }
1218 vfmt[j++] = ch;
1219 vfmt[j++] = '\0';
1220
1221 /* Get a vector element. */
1222 #ifdef V64TYPE
1223 #define VPRINT(type, ind, args...) do { \
1224 switch (type) { \
1225 case V_CHAR: \
1226 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1227 break; \
1228 case V_PCHAR: \
1229 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1230 break; \
1231 case V_SHORT: \
1232 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1233 break; \
1234 case V_PSHORT: \
1235 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1236 break; \
1237 case V_INT: \
1238 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1239 break; \
1240 case V_PINT: \
1241 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1242 break; \
1243 case V_LONGLONG: \
1244 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \
1245 break; \
1246 case V_PLONGLONG: \
1247 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vulonglongarg[ind]); \
1248 break; \
1249 case V_FLOAT: \
1250 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1251 break; \
1252 case V_DOUBLE: \
1253 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \
1254 break; \
1255 } \
1256 ret += vlen; \
1257 PRINT((const CHAR *) vstr, vlen); \
1258 free(vstr); \
1259 } while (0)
1260 #else /* !V64TYPE */
1261 #define VPRINT(type, ind, args...) do { \
1262 switch (type) { \
1263 case V_CHAR: \
1264 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1265 break; \
1266 case V_PCHAR: \
1267 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1268 break; \
1269 case V_SHORT: \
1270 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1271 break; \
1272 case V_PSHORT: \
1273 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1274 break; \
1275 case V_INT: \
1276 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1277 break; \
1278 case V_PINT: \
1279 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1280 break; \
1281 case V_FLOAT: \
1282 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1283 break; \
1284 } \
1285 ret += vlen; \
1286 PRINT((const CHAR *) vstr, vlen); \
1287 free(vstr); \
1288 } while (0)
1289 #endif /* V64TYPE */
1290
1291 /* Actually print. */
1292 if (vwidth == 0) {
1293 if (vprec == 0) {
1294 /* First element. */
1295 VPRINT(vtype, 0);
1296 for (i = 1; i < vcnt; i++) {
1297 /* Separator. */
1298 if(vsep)
1299 PRINT(&vsep, 1);
1300
1301 /* Element. */
1302 VPRINT(vtype, i);
1303 }
1304 } else {
1305 /* First element. */
1306 VPRINT(vtype, 0, prec);
1307 for (i = 1; i < vcnt; i++) {
1308 /* Separator. */
1309 if(vsep)
1310 PRINT(&vsep, 1);
1311
1312 /* Element. */
1313 VPRINT(vtype, i, prec);
1314 }
1315 }
1316 } else {
1317 if (vprec == 0) {
1318 /* First element. */
1319 VPRINT(vtype, 0, width);
1320 for (i = 1; i < vcnt; i++) {
1321 /* Separator. */
1322 if(vsep)
1323 PRINT(&vsep, 1);
1324
1325 /* Element. */
1326 VPRINT(vtype, i, width);
1327 }
1328 } else {
1329 /* First element. */
1330 VPRINT(vtype, 0, width, prec);
1331 for (i = 1; i < vcnt; i++) {
1332 /* Separator. */
1333 if(vsep)
1334 PRINT(&vsep, 1);
1335
1336 /* Element. */
1337 VPRINT(vtype, i, width, prec);
1338 }
1339 }
1340 }
1341 #undef VPRINT
1342
1343 if (vfmt != vfmt_buf)
1344 free(vfmt);
1345
1346 continue;
1347 }
1348 #endif /* VECTORS */
1349 /*
1350 * All reasonable formats wind up here. At this point, `cp'
1351 * points to a string which (if not flags&LADJUST) should be
1352 * padded out to `width' places. If flags&ZEROPAD, it should
1353 * first be prefixed by any sign or other prefix; otherwise,
1354 * it should be blank padded before the prefix is emitted.
1355 * After any left-hand padding and prefixing, emit zeroes
1356 * required by a decimal [diouxX] precision, then print the
1357 * string proper, then emit zeroes required by any leftover
1358 * floating precision; finally, if LADJUST, pad with blanks.
1359 *
1360 * Compute actual size, so we know how much to pad.
1361 * size excludes decimal prec; realsz includes it.
1362 */
1363 realsz = dprec > size ? dprec : size;
1364 if (sign)
1365 realsz++;
1366 if (ox[1])
1367 realsz += 2;
1368
1369 prsize = width > realsz ? width : realsz;
1370 if ((unsigned)ret + prsize > INT_MAX) {
1371 ret = EOF;
1372 goto error;
1373 }
1374
1375 /* right-adjusting blank padding */
1376 if ((flags & (LADJUST|ZEROPAD)) == 0)
1377 PAD(width - realsz, blanks);
1378
1379 /* prefix */
1380 if (sign)
1381 PRINT(&sign, 1);
1382
1383 if (ox[1]) { /* ox[1] is either x, X, or \0 */
1384 ox[0] = '0';
1385 PRINT(ox, 2);
1386 }
1387
1388 /* right-adjusting zero padding */
1389 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1390 PAD(width - realsz, zeroes);
1391
1392 /* the string or number proper */
1393 #ifndef NO_FLOATING_POINT
1394 if ((flags & FPT) == 0) {
1395 #endif
1396 /* leading zeroes from decimal precision */
1397 PAD(dprec - size, zeroes);
1398 if (gs.grouping) {
1399 if (grouping_print(&gs, &io, cp, buf+BUF, loc) < 0)
1400 goto error;
1401 } else {
1402 PRINT(cp, size);
1403 }
1404 #ifndef NO_FLOATING_POINT
1405 } else { /* glue together f_p fragments */
1406 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
1407 if (expt <= 0) {
1408 PRINT(zeroes, 1);
1409 if (prec || flags & ALT)
1410 PRINT(&decimal_point, 1);
1411 PAD(-expt, zeroes);
1412 /* already handled initial 0's */
1413 prec += expt;
1414 } else {
1415 if (gs.grouping) {
1416 n = grouping_print(&gs, &io,
1417 cp, convbuf + ndig, loc);
1418 if (n < 0)
1419 goto error;
1420 cp += n;
1421 } else {
1422 PRINTANDPAD(cp, convbuf + ndig,
1423 expt, zeroes);
1424 cp += expt;
1425 }
1426 if (prec || flags & ALT)
1427 PRINT(&decimal_point, 1);
1428 }
1429 PRINTANDPAD(cp, convbuf + ndig, prec, zeroes);
1430 } else { /* %[eE] or sufficiently long %[gG] */
1431 if (prec > 1 || flags & ALT) {
1432 buf[0] = *cp++;
1433 buf[1] = decimal_point;
1434 PRINT(buf, 2);
1435 PRINT(cp, ndig-1);
1436 PAD(prec - ndig, zeroes);
1437 } else /* XeYYY */
1438 PRINT(cp, 1);
1439 PRINT(expstr, expsize);
1440 }
1441 }
1442 #endif
1443 /* left-adjusting padding (always blank) */
1444 if (flags & LADJUST)
1445 PAD(width - realsz, blanks);
1446
1447 /* finally, adjust ret */
1448 ret += prsize;
1449
1450 FLUSH(); /* copy out the I/O vectors */
1451 }
1452 done:
1453 FLUSH();
1454 error:
1455 va_end(orgap);
1456 free(convbuf);
1457 if (__sferror(fp))
1458 ret = EOF;
1459 if ((argtable != NULL) && (argtable != statargtable))
1460 free (argtable);
1461 return (ret);
1462 /* NOTREACHED */
1463 }