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
36 #include <namespace.h>
38 #include <sys/types.h>
46 #include <namespace.h>
49 #include <un-namespace.h>
53 /* private stuff -----------------------------------------------------*/
65 * Macros for converting digits to letters and vice versa
67 #define to_char(n) ((n) + '0')
69 /* various globals ---------------------------------------------------*/
72 * The size of the buffer we use for integer conversions.
73 * Technically, we would need the most space for base 10
74 * conversions with thousands' grouping characters between
75 * each pair of digits: 60 digits for 128 bit intmax_t.
76 * Use a bit more for better alignment of stuff.
80 /* misc --------------------------------------------------------------*/
83 * Convert an unsigned long to ASCII for printf purposes, returning
84 * a pointer to the first character of the string representation.
85 * Octal numbers can be forced to have a leading zero; hex numbers
86 * use the given digits.
89 __ultoa(u_long val
, char *endp
, int base
, const char *xdigs
,
90 int needgrp
, char thousep
, const char *grp
)
97 * Handle the three cases separately, in the hope of getting
102 if (val
< 10) { /* many numbers are 1 digit */
103 *--cp
= to_char(val
);
108 * On many machines, unsigned arithmetic is harder than
109 * signed arithmetic, so we do at most one unsigned mod and
110 * divide; this is sufficient to reduce the range of
111 * the incoming value to where signed arithmetic works.
113 if (val
> LONG_MAX
) {
114 *--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);
186 *--cp
= to_char(sval
% 10);
189 * If (*grp == CHAR_MAX) then no more grouping
190 * should be performed.
192 if (needgrp
&& *grp
!= CHAR_MAX
&& ndig
== *grp
197 * If (*(grp+1) == '\0') then we have to
198 * use *grp character (last grouping rule)
201 if (*(grp
+1) != '\0')
210 *--cp
= to_char(val
& 7);
217 *--cp
= xdigs
[val
& 15];
229 /* 'd' ---------------------------------------------------------------*/
232 __printf_arginfo_int(const struct printf_info
*pi
, size_t n
, int *argt
)
237 argt
[0] |= PA_FLAG_PTRDIFF
;
238 else if (pi
->is_size
)
239 argt
[0] |= PA_FLAG_SIZE
;
240 else if (pi
->is_long
)
241 argt
[0] |= PA_FLAG_LONG
;
242 else if (pi
->is_intmax
)
243 argt
[0] |= PA_FLAG_INTMAX
;
244 else if (pi
->is_quad
)
245 argt
[0] |= PA_FLAG_QUAD
;
246 else if (pi
->is_long_double
)
247 argt
[0] |= PA_FLAG_LONG_LONG
;
248 else if (pi
->is_short
)
249 argt
[0] |= PA_FLAG_SHORT
;
250 else if (pi
->is_char
)
256 __printf_render_int(struct __printf_io
*io
, const struct printf_info
*pi
, const void *const *arg
)
258 const union arg
*argp
;
262 int rdx
, sign
, zext
, ngrp
;
263 const char *nalt
, *digit
;
264 char thousands_sep
; /* locale specific thousands separator */
265 const char *grouping
; /* locale specific numeric grouping rules */
271 digit
= __lowercase_hex
;
273 pe
= buf
+ sizeof buf
- 1;
276 thousands_sep
= *(localeconv()->thousands_sep
);
277 grouping
= localeconv()->grouping
;
292 digit
= __uppercase_hex
;
309 fprintf(stderr
, "pi->spec = '%c'\n", pi
->spec
);
317 if (pi
->is_long_double
|| pi
->is_quad
|| pi
->is_intmax
||
318 pi
->is_size
|| pi
->is_ptrdiff
) {
319 if (sign
&& argp
->intmaxarg
< 0) {
320 uu
= -argp
->intmaxarg
;
323 uu
= argp
->uintmaxarg
;
324 } else if (pi
->is_long
) {
325 if (sign
&& argp
->longarg
< 0) {
326 uu
= (u_long
)-argp
->longarg
;
330 } else if (pi
->is_short
) {
331 if (sign
&& (short)argp
->intarg
< 0) {
332 uu
= -(short)argp
->intarg
;
335 uu
= (unsigned short)argp
->uintarg
;
336 } else if (pi
->is_char
) {
337 if (sign
&& (signed char)argp
->intarg
< 0) {
338 uu
= -(signed char)argp
->intarg
;
341 uu
= (unsigned char)argp
->uintarg
;
343 if (sign
&& argp
->intarg
< 0) {
344 uu
= (unsigned)-argp
->intarg
;
350 p
= __ultoa(uu
, pe
, rdx
, digit
, ngrp
, thousands_sep
, grouping
);
352 p
= __ujtoa(uu
, pe
, rdx
, digit
, ngrp
, thousands_sep
, grouping
);
357 * ``The result of converting a zero value with an
358 * explicit precision of zero is no characters.''
361 * ``The C Standard is clear enough as is. The call
362 * printf("%#.0o", 0) should print 0.''
363 * -- Defect Report #151
366 if (pi
->prec
== 0 && !(pi
->alt
&& rdx
== 8))
368 } else if (pi
->alt
) {
384 * ``... diouXx conversions ... if a precision is
385 * specified, the 0 flag will be ignored.''
388 if (pi
->prec
> (pe
- p
))
389 zext
= pi
->prec
- (pe
- p
);
390 else if (pi
->prec
!= -1)
392 else if (pi
->pad
== '0' && pi
->width
> l
&& !pi
->left
)
393 zext
= pi
->width
- l
;
399 while (zext
> 0 && p
> buf
) {
407 } else if (nalt
!= NULL
) {
411 if (pi
->width
> (pe
- p
) && !pi
->left
) {
412 l
= pi
->width
- (pe
- p
);
413 while (l
> 0 && p
> buf
) {
418 ret
+= __printf_pad(io
, l
, 0);
421 if (!pi
->left
&& pi
->width
> l
)
422 ret
+= __printf_pad(io
, pi
->width
- l
, 0);
424 ret
+= __printf_puts(io
, &ns
, 1);
425 else if (nalt
!= NULL
)
426 ret
+= __printf_puts(io
, nalt
, 2);
428 ret
+= __printf_pad(io
, zext
, 1);
431 ret
+= __printf_puts(io
, p
, pe
- p
);
432 if (pi
->width
> ret
&& pi
->left
)
433 ret
+= __printf_pad(io
, pi
->width
- ret
, 0);
438 /* 'p' ---------------------------------------------------------------*/
441 __printf_arginfo_ptr(const struct printf_info
*pi __unused
, size_t n
, int *argt
)
445 argt
[0] = PA_POINTER
;
450 __printf_render_ptr(struct __printf_io
*io
, const struct printf_info
*pi
, const void *const *arg
)
452 struct printf_info p2
;
457 * ``The argument shall be a pointer to void. The
458 * value of the pointer is converted to a sequence
459 * of printable characters, in an implementation-
463 u
= (uintmax_t)(uintptr_t) *((void **)arg
[0]);
468 p2
.is_long_double
= 1;
470 return (__printf_render_int(io
, &p2
, &p
));