2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2005 Poul-Henning Kamp
5 * Copyright (c) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <namespace.h>
40 #include <sys/types.h>
48 #include <namespace.h>
51 #include <un-namespace.h>
55 /* private stuff -----------------------------------------------------*/
67 * Macros for converting digits to letters and vice versa
69 #define to_char(n) ((n) + '0')
71 /* various globals ---------------------------------------------------*/
74 * The size of the buffer we use for integer conversions.
75 * Technically, we would need the most space for base 10
76 * conversions with thousands' grouping characters between
77 * each pair of digits: 60 digits for 128 bit intmax_t.
78 * Use a bit more for better alignment of stuff.
82 /* misc --------------------------------------------------------------*/
85 * Convert an unsigned long to ASCII for printf purposes, returning
86 * a pointer to the first character of the string representation.
87 * Octal numbers can be forced to have a leading zero; hex numbers
88 * use the given digits.
91 __ultoa(u_long val, char *endp, int base, const char *xdigs,
92 int needgrp, char thousep, const char *grp)
99 * Handle the three cases separately, in the hope of getting
100 * better/faster code.
104 if (val < 10) { /* many numbers are 1 digit */
105 *--cp = to_char(val);
110 * On many machines, unsigned arithmetic is harder than
111 * signed arithmetic, so we do at most one unsigned mod and
112 * divide; this is sufficient to reduce the range of
113 * the incoming value to where signed arithmetic works.
115 if (val > LONG_MAX) {
116 *--cp = to_char(val % 10);
122 *--cp = to_char(sval % 10);
125 * If (*grp == CHAR_MAX) then no more grouping
126 * should be performed.
128 if (needgrp && ndig == *grp && *grp != CHAR_MAX
133 * If (*(grp+1) == '\0') then we have to
134 * use *grp character (last grouping rule)
137 if (*(grp+1) != '\0')
146 *--cp = to_char(val & 7);
153 *--cp = xdigs[val & 15];
165 /* Identical to __ultoa, but for intmax_t. */
167 __ujtoa(uintmax_t val, char *endp, int base, const char *xdigs,
168 int needgrp, char thousep, const char *grp)
177 *--cp = to_char(val % 10);
181 if (val > INTMAX_MAX) {
182 *--cp = to_char(val % 10);
188 *--cp = to_char(sval % 10);
191 * If (*grp == CHAR_MAX) then no more grouping
192 * should be performed.
194 if (needgrp && *grp != CHAR_MAX && ndig == *grp
199 * If (*(grp+1) == '\0') then we have to
200 * use *grp character (last grouping rule)
203 if (*(grp+1) != '\0')
212 *--cp = to_char(val & 7);
219 *--cp = xdigs[val & 15];
231 /* 'd' ---------------------------------------------------------------*/
234 __printf_arginfo_int(const struct printf_info *pi, size_t n, int *argt)
239 argt[0] |= PA_FLAG_PTRDIFF;
240 else if (pi->is_size)
241 argt[0] |= PA_FLAG_SIZE;
242 else if (pi->is_long)
243 argt[0] |= PA_FLAG_LONG;
244 else if (pi->is_intmax)
245 argt[0] |= PA_FLAG_INTMAX;
246 else if (pi->is_quad)
247 argt[0] |= PA_FLAG_QUAD;
248 else if (pi->is_long_double)
249 argt[0] |= PA_FLAG_LONG_LONG;
250 else if (pi->is_short)
251 argt[0] |= PA_FLAG_SHORT;
252 else if (pi->is_char)
258 __printf_render_int(struct __printf_io *io, const struct printf_info *pi, const void *const *arg)
260 const union arg *argp;
264 int l, ngrp, rdx, sign, zext;
265 const char *nalt, *digit;
266 char thousands_sep; /* locale specific thousands separator */
267 const char *grouping; /* locale specific numeric grouping rules */
273 digit = __lowercase_hex;
275 pe = buf + sizeof buf - 1;
278 thousands_sep = *(localeconv()->thousands_sep);
279 grouping = localeconv()->grouping;
294 digit = __uppercase_hex;
311 fprintf(stderr, "pi->spec = '%c'\n", pi->spec);
319 if (pi->is_long_double || pi->is_quad || pi->is_intmax ||
320 pi->is_size || pi->is_ptrdiff) {
321 if (sign && argp->intmaxarg < 0) {
322 uu = -argp->intmaxarg;
325 uu = argp->uintmaxarg;
326 } else if (pi->is_long) {
327 if (sign && argp->longarg < 0) {
328 uu = (u_long)-argp->longarg;
332 } else if (pi->is_short) {
333 if (sign && (short)argp->intarg < 0) {
334 uu = -(short)argp->intarg;
337 uu = (unsigned short)argp->uintarg;
338 } else if (pi->is_char) {
339 if (sign && (signed char)argp->intarg < 0) {
340 uu = -(signed char)argp->intarg;
343 uu = (unsigned char)argp->uintarg;
345 if (sign && argp->intarg < 0) {
346 uu = (unsigned)-argp->intarg;
352 p = __ultoa(uu, pe, rdx, digit, ngrp, thousands_sep, grouping);
354 p = __ujtoa(uu, pe, rdx, digit, ngrp, thousands_sep, grouping);
359 * ``The result of converting a zero value with an
360 * explicit precision of zero is no characters.''
363 * ``The C Standard is clear enough as is. The call
364 * printf("%#.0o", 0) should print 0.''
365 * -- Defect Report #151
368 if (pi->prec == 0 && !(pi->alt && rdx == 8))
370 } else if (pi->alt) {
386 * ``... diouXx conversions ... if a precision is
387 * specified, the 0 flag will be ignored.''
390 if (pi->prec > (pe - p))
391 zext = pi->prec - (pe - p);
392 else if (pi->prec != -1)
394 else if (pi->pad == '0' && pi->width > l && !pi->left)
395 zext = pi->width - l;
401 while (zext > 0 && p > buf) {
409 } else if (nalt != NULL) {
413 if (pi->width > (pe - p) && !pi->left) {
414 l = pi->width - (pe - p);
415 while (l > 0 && p > buf) {
420 ret += __printf_pad(io, l, 0);
423 if (!pi->left && pi->width > l)
424 ret += __printf_pad(io, pi->width - l, 0);
426 ret += __printf_puts(io, &ns, 1);
427 else if (nalt != NULL)
428 ret += __printf_puts(io, nalt, 2);
430 ret += __printf_pad(io, zext, 1);
433 ret += __printf_puts(io, p, pe - p);
434 if (pi->width > ret && pi->left)
435 ret += __printf_pad(io, pi->width - ret, 0);
440 /* 'p' ---------------------------------------------------------------*/
443 __printf_arginfo_ptr(const struct printf_info *pi __unused, size_t n, int *argt)
447 argt[0] = PA_POINTER;
452 __printf_render_ptr(struct __printf_io *io, const struct printf_info *pi, const void *const *arg)
454 struct printf_info p2;
459 * ``The argument shall be a pointer to void. The
460 * value of the pointer is converted to a sequence
461 * of printable characters, in an implementation-
465 u = (uintmax_t)(uintptr_t) *((void **)arg[0]);
470 p2.is_long_double = 1;
472 return (__printf_render_int(io, &p2, &p));