2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * This file defines common routines used by both printf and wprintf.
37 * You must define CHAR to either char or wchar_t prior to including this.
41 #ifndef NO_FLOATING_POINT
44 #define freedtoa __freedtoa
53 static int exponent(CHAR *, int, CHAR);
55 #endif /* !NO_FLOATING_POINT */
57 static CHAR *__ujtoa(uintmax_t, CHAR *, int, int, const char *, int, char,
59 static CHAR *__ultoa(u_long, CHAR *, int, int, const char *, int, char,
65 struct __suio uio; /* output information: summary */
66 struct __siov iov[NIOV];/* ... and individual io vectors */
67 struct __siov *iovp; /* pointer to next free slot in iov */
71 io_init(struct io_state *iop, FILE *fp)
74 iop->uio.uio_iov = iop->iovp = iop->iov;
75 iop->uio.uio_resid = 0;
76 iop->uio.uio_iovcnt = 0;
81 * WARNING: The buffer passed to io_print() is not copied immediately; it must
82 * remain valid until io_flush() is called.
85 io_print(struct io_state *iop, const CHAR * __restrict ptr, int len)
88 iop->iovp->iov_base = (char *)ptr;
89 iop->iovp->iov_len = len;
90 iop->uio.uio_resid += len;
92 if (++iop->uio.uio_iovcnt >= NIOV) {
94 return (__sprint(iop->fp, &iop->uio));
100 * Choose PADSIZE to trade efficiency vs. size. If larger printf
101 * fields occur frequently, increase PADSIZE and make the initialisers
104 #define PADSIZE 16 /* pad chunk size */
105 static const CHAR blanks[PADSIZE] =
106 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
107 static const CHAR zeroes[PADSIZE] =
108 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
111 * Pad with blanks or zeroes. 'with' should point to either the blanks array
112 * or the zeroes array.
115 io_pad(struct io_state *iop, int howmany, const CHAR * __restrict with)
118 while (howmany > PADSIZE) {
119 if (io_print(iop, with, PADSIZE))
123 if (howmany > 0 && io_print(iop, with, howmany))
129 * Print exactly len characters of the string spanning p to ep, truncating
130 * or padding with 'with' as necessary.
133 io_printandpad(struct io_state *iop, const CHAR *p, const CHAR *ep,
134 int len, const CHAR * __restrict with)
141 if (p_len > 0 && io_print(iop, p, p_len))
143 return (io_pad(iop, len - (p_len > 0 ? p_len : 0), with));
147 io_flush(struct io_state *iop)
150 iop->iovp = iop->iov;
151 return (__sprint(iop->fp, &iop->uio));
155 * Convert an unsigned long to ASCII for printf purposes, returning
156 * a pointer to the first character of the string representation.
157 * Octal numbers can be forced to have a leading zero; hex numbers
158 * use the given digits.
161 __ultoa(u_long val, CHAR *endp, int base, int octzero, const char *xdigs,
162 int needgrp, char thousep, const char *grp)
169 * Handle the three cases separately, in the hope of getting
170 * better/faster code.
174 if (val < 10) { /* many numbers are 1 digit */
175 *--cp = to_char(val);
180 * On many machines, unsigned arithmetic is harder than
181 * signed arithmetic, so we do at most one unsigned mod and
182 * divide; this is sufficient to reduce the range of
183 * the incoming value to where signed arithmetic works.
185 if (val > LONG_MAX) {
186 *--cp = to_char(val % 10);
192 *--cp = to_char(sval % 10);
195 * If (*grp == CHAR_MAX) then no more grouping
196 * should be performed.
198 if (needgrp && ndig == *grp && *grp != CHAR_MAX
203 * If (*(grp+1) == '\0') then we have to
204 * use *grp character (last grouping rule)
207 if (*(grp+1) != '\0')
216 *--cp = to_char(val & 7);
219 if (octzero && *cp != '0')
225 *--cp = xdigs[val & 15];
236 /* Identical to __ultoa, but for intmax_t. */
238 __ujtoa(uintmax_t val, CHAR *endp, int base, int octzero, const char *xdigs,
239 int needgrp, char thousep, const char *grp)
245 /* quick test for small values; __ultoa is typically much faster */
246 /* (perhaps instead we should run until small, then call __ultoa?) */
247 if (val <= ULONG_MAX)
248 return (__ultoa((u_long)val, endp, base, octzero, xdigs,
249 needgrp, thousep, grp));
253 *--cp = to_char(val % 10);
257 if (val > INTMAX_MAX) {
258 *--cp = to_char(val % 10);
264 *--cp = to_char(sval % 10);
267 * If (*grp == CHAR_MAX) then no more grouping
268 * should be performed.
270 if (needgrp && *grp != CHAR_MAX && ndig == *grp
275 * If (*(grp+1) == '\0') then we have to
276 * use *grp character (last grouping rule)
279 if (*(grp+1) != '\0')
288 *--cp = to_char(val & 7);
291 if (octzero && *cp != '0')
297 *--cp = xdigs[val & 15];
308 #ifndef NO_FLOATING_POINT
311 exponent(CHAR *p0, int exp, CHAR fmtch)
314 CHAR expbuf[MAXEXPDIG];
324 t = expbuf + MAXEXPDIG;
327 *--t = to_char(exp % 10);
328 } while ((exp /= 10) > 9);
330 for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
334 * Exponents for decimal floating point conversions
335 * (%[eEgG]) must be at least two characters long,
336 * whereas exponents for hexadecimal conversions can
337 * be only one character long.
339 if (fmtch == 'e' || fmtch == 'E')
346 #endif /* !NO_FLOATING_POINT */