2 * Copyright (c) 2005 Poul-Henning Kamp
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/lib/libc/stdio/xprintf_int.c,v 1.2 2005/12/22 14:23:54 cognet Exp $
36 #include "namespace.h"
38 #include <sys/types.h>
48 #include "un-namespace.h"
52 /* private stuff -----------------------------------------------------*/
64 * Macros for converting digits to letters and vice versa
66 #define to_char(n) ((n) + '0')
68 /* various globals ---------------------------------------------------*/
71 * The size of the buffer we use for integer conversions.
72 * Technically, we would need the most space for base 10
73 * conversions with thousands' grouping characters between
74 * each pair of digits: 60 digits for 128 bit intmax_t.
75 * Use a bit more for better alignment of stuff.
79 /* misc --------------------------------------------------------------*/
82 * Convert an unsigned long to ASCII for printf purposes, returning
83 * a pointer to the first character of the string representation.
84 * Octal numbers can be forced to have a leading zero; hex numbers
85 * use the given digits.
88 __ultoa(u_long val
, char *endp
, int base
, const char *xdigs
,
89 int needgrp
, char thousep
, const char *grp
)
96 * Handle the three cases separately, in the hope of getting
101 if (val
< 10) { /* many numbers are 1 digit */
102 *--cp
= to_char(val
);
107 * On many machines, unsigned arithmetic is harder than
108 * signed arithmetic, so we do at most one unsigned mod and
109 * divide; this is sufficient to reduce the range of
110 * the incoming value to where signed arithmetic works.
112 if (val
> LONG_MAX
) {
113 *--cp
= to_char(val
% 10);
120 *--cp
= to_char(sval
% 10);
123 * If (*grp == CHAR_MAX) then no more grouping
124 * should be performed.
126 if (needgrp
&& ndig
== *grp
&& *grp
!= CHAR_MAX
131 * If (*(grp+1) == '\0') then we have to
132 * use *grp character (last grouping rule)
135 if (*(grp
+1) != '\0')
144 *--cp
= to_char(val
& 7);
151 *--cp
= xdigs
[val
& 15];
163 /* Identical to __ultoa, but for intmax_t. */
165 __ujtoa(uintmax_t val
, char *endp
, int base
, const char *xdigs
,
166 int needgrp
, char thousep
, const char *grp
)
175 *--cp
= to_char(val
% 10);
179 if (val
> INTMAX_MAX
) {
180 *--cp
= to_char(val
% 10);
187 *--cp
= to_char(sval
% 10);
190 * If (*grp == CHAR_MAX) then no more grouping
191 * should be performed.
193 if (needgrp
&& *grp
!= CHAR_MAX
&& ndig
== *grp
198 * If (*(grp+1) == '\0') then we have to
199 * use *grp character (last grouping rule)
202 if (*(grp
+1) != '\0')
211 *--cp
= to_char(val
& 7);
218 *--cp
= xdigs
[val
& 15];
230 /* 'd' ---------------------------------------------------------------*/
233 __printf_arginfo_int(const struct printf_info
*pi
, size_t n
, int *argt
)
238 argt
[0] |= PA_FLAG_PTRDIFF
;
239 else if (pi
->is_size
)
240 argt
[0] |= PA_FLAG_SIZE
;
241 else if (pi
->is_long
)
242 argt
[0] |= PA_FLAG_LONG
;
243 else if (pi
->is_intmax
)
244 argt
[0] |= PA_FLAG_INTMAX
;
245 else if (pi
->is_quad
)
246 argt
[0] |= PA_FLAG_QUAD
;
247 else if (pi
->is_long_double
)
248 argt
[0] |= PA_FLAG_LONG_LONG
;
249 else if (pi
->is_short
)
250 argt
[0] |= PA_FLAG_SHORT
;
251 else if (pi
->is_char
)
257 __printf_render_int(struct __printf_io
*io
, const struct printf_info
*pi
,
258 const void *const *arg
)
260 const union arg
*argp
;
264 int rdx
, sign
, zext
, ngrp
;
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
;
327 } else if (pi
->is_long
) {
328 if (sign
&& argp
->longarg
< 0) {
329 uu
= (u_long
)-argp
->longarg
;
334 } else if (pi
->is_short
) {
335 if (sign
&& (short)argp
->intarg
< 0) {
336 uu
= -(short)argp
->intarg
;
339 uu
= (unsigned short)argp
->uintarg
;
341 } else if (pi
->is_char
) {
342 if (sign
&& (signed char)argp
->intarg
< 0) {
343 uu
= -(signed char)argp
->intarg
;
346 uu
= (unsigned char)argp
->uintarg
;
349 if (sign
&& argp
->intarg
< 0) {
350 uu
= (unsigned)-argp
->intarg
;
357 p
= __ultoa(uu
, pe
, rdx
, digit
, ngrp
, thousands_sep
, grouping
);
359 p
= __ujtoa(uu
, pe
, rdx
, digit
, ngrp
, thousands_sep
, grouping
);
364 * ``The result of converting a zero value with an
365 * explicit precision of zero is no characters.''
368 * ``The C Standard is clear enough as is. The call
369 * printf("%#.0o", 0) should print 0.''
370 * -- Defect Report #151
372 if (pi
->prec
== 0 && !(pi
->alt
&& rdx
== 8))
374 } else if (pi
->alt
) {
390 * ``... diouXx conversions ... if a precision is
391 * specified, the 0 flag will be ignored.''
394 if (pi
->prec
> (pe
- p
))
395 zext
= pi
->prec
- (pe
- p
);
396 else if (pi
->prec
!= -1)
398 else if (pi
->pad
== '0' && pi
->width
> l
&& !pi
->left
)
399 zext
= pi
->width
- l
;
405 while (zext
> 0 && p
> buf
) {
413 } else if (nalt
!= NULL
) {
417 if (pi
->width
> (pe
- p
) && !pi
->left
) {
418 l
= pi
->width
- (pe
- p
);
419 while (l
> 0 && p
> buf
) {
424 ret
+= __printf_pad(io
, l
, 0);
427 if (!pi
->left
&& pi
->width
> l
)
428 ret
+= __printf_pad(io
, pi
->width
- l
, 0);
430 ret
+= __printf_puts(io
, &ns
, 1);
431 else if (nalt
!= NULL
)
432 ret
+= __printf_puts(io
, nalt
, 2);
434 ret
+= __printf_pad(io
, zext
, 1);
437 ret
+= __printf_puts(io
, p
, pe
- p
);
438 if (pi
->width
> ret
&& pi
->left
)
439 ret
+= __printf_pad(io
, pi
->width
- ret
, 0);
444 /* 'p' ---------------------------------------------------------------*/
447 __printf_arginfo_ptr(const struct printf_info
*pi __unused
, size_t n
, int *argt
)
451 argt
[0] = PA_POINTER
;
456 __printf_render_ptr(struct __printf_io
*io
, const struct printf_info
*pi
,
457 const void *const *arg
)
459 struct printf_info p2
;
464 * ``The argument shall be a pointer to void. The
465 * value of the pointer is converted to a sequence
466 * of printable characters, in an implementation-
470 u
= (uintmax_t)(uintptr_t) *((void **)arg
[0]);
475 p2
.is_long_double
= 1;
477 return (__printf_render_int(io
, &p2
, &p
));