2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Copyright (c) 2011 The FreeBSD Foundation
11 * All rights reserved.
12 * Portions of this software were developed by David Chisnall
13 * under sponsorship from the FreeBSD Foundation.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * This file defines common routines used by both printf and wprintf.
44 * You must define CHAR to either char or wchar_t prior to including this.
48 #ifndef NO_FLOATING_POINT
51 #define freedtoa __freedtoa
60 static int exponent(CHAR *, int, CHAR);
62 #endif /* !NO_FLOATING_POINT */
64 static CHAR *__ujtoa(uintmax_t, CHAR *, int, int, const char *);
65 static CHAR *__ultoa(u_long, CHAR *, int, int, const char *);
70 struct __suio uio; /* output information: summary */
71 struct __siov iov[NIOV];/* ... and individual io vectors */
75 io_init(struct io_state *iop, FILE *fp)
78 iop->uio.uio_iov = iop->iov;
79 iop->uio.uio_resid = 0;
80 iop->uio.uio_iovcnt = 0;
85 * WARNING: The buffer passed to io_print() is not copied immediately; it must
86 * remain valid until io_flush() is called.
89 io_print(struct io_state *iop, const CHAR * __restrict ptr, int len, locale_t locale)
92 iop->iov[iop->uio.uio_iovcnt].iov_base = (char *)ptr;
93 iop->iov[iop->uio.uio_iovcnt].iov_len = len;
94 iop->uio.uio_resid += len;
95 if (++iop->uio.uio_iovcnt >= NIOV)
96 return (__sprint(iop->fp, &iop->uio, locale));
102 * Choose PADSIZE to trade efficiency vs. size. If larger printf
103 * fields occur frequently, increase PADSIZE and make the initialisers
106 #define PADSIZE 16 /* pad chunk size */
107 static const CHAR blanks[PADSIZE] =
108 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
109 static const CHAR zeroes[PADSIZE] =
110 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
113 * Pad with blanks or zeroes. 'with' should point to either the blanks array
114 * or the zeroes array.
117 io_pad(struct io_state *iop, int howmany, const CHAR * __restrict with,
122 while (howmany > 0) {
123 n = (howmany >= PADSIZE) ? PADSIZE : howmany;
124 if (io_print(iop, with, n, locale))
132 * Print exactly len characters of the string spanning p to ep, truncating
133 * or padding with 'with' as necessary.
136 io_printandpad(struct io_state *iop, const CHAR *p, const CHAR *ep,
137 int len, const CHAR * __restrict with, locale_t locale)
145 if (io_print(iop, p, p_len, locale))
150 return (io_pad(iop, len - p_len, with, locale));
154 io_flush(struct io_state *iop, locale_t locale)
157 return (__sprint(iop->fp, &iop->uio, locale));
161 * Convert an unsigned long to ASCII for printf purposes, returning
162 * a pointer to the first character of the string representation.
163 * Octal numbers can be forced to have a leading zero; hex numbers
164 * use the given digits.
167 __ultoa(u_long val, CHAR *endp, int base, int octzero, const char *xdigs)
173 * Handle the three cases separately, in the hope of getting
174 * better/faster code.
178 if (val < 10) { /* many numbers are 1 digit */
179 *--cp = to_char(val);
183 * On many machines, unsigned arithmetic is harder than
184 * signed arithmetic, so we do at most one unsigned mod and
185 * divide; this is sufficient to reduce the range of
186 * the incoming value to where signed arithmetic works.
188 if (val > LONG_MAX) {
189 *--cp = to_char(val % 10);
194 *--cp = to_char(sval % 10);
201 *--cp = to_char(val & 7);
204 if (octzero && *cp != '0')
210 *--cp = xdigs[val & 15];
221 /* Identical to __ultoa, but for intmax_t. */
223 __ujtoa(uintmax_t val, CHAR *endp, int base, int octzero, const char *xdigs)
228 /* quick test for small values; __ultoa is typically much faster */
229 /* (perhaps instead we should run until small, then call __ultoa?) */
230 if (val <= ULONG_MAX)
231 return (__ultoa((u_long)val, endp, base, octzero, xdigs));
235 *--cp = to_char(val % 10);
238 if (val > INTMAX_MAX) {
239 *--cp = to_char(val % 10);
244 *--cp = to_char(sval % 10);
251 *--cp = to_char(val & 7);
254 if (octzero && *cp != '0')
260 *--cp = xdigs[val & 15];
271 #ifndef NO_FLOATING_POINT
274 exponent(CHAR *p0, int exp, CHAR fmtch)
277 CHAR expbuf[MAXEXPDIG];
287 t = expbuf + MAXEXPDIG;
290 *--t = to_char(exp % 10);
291 } while ((exp /= 10) > 9);
293 for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
297 * Exponents for decimal floating point conversions
298 * (%[eEgG]) must be at least two characters long,
299 * whereas exponents for hexadecimal conversions can
300 * be only one character long.
302 if (fmtch == 'e' || fmtch == 'E')
309 #endif /* !NO_FLOATING_POINT */