1 /* -*- linux-c -*- ------------------------------------------------------- *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright 2007 rPath, Inc. - All Rights Reserved
6 * This file is part of the Linux kernel, and is made available under
7 * the terms of the GNU General Public License version 2.
9 * ----------------------------------------------------------------------- */
12 * Oh, it's a waste of space, but oh-so-yummy for debugging. This
13 * version of printf() does not include 64-bit support. "Live with
20 static int skip_atoi(const char **s
)
25 i
= i
* 10 + *((*s
)++) - '0';
29 #define ZEROPAD 1 /* pad with zero */
30 #define SIGN 2 /* unsigned/signed long */
31 #define PLUS 4 /* show plus */
32 #define SPACE 8 /* space if plus */
33 #define LEFT 16 /* left justified */
34 #define SMALL 32 /* Must be 32 == 0x20 */
35 #define SPECIAL 64 /* 0x */
37 #define __do_div(n, base) ({ \
39 __res = ((unsigned long) n) % (unsigned) base; \
40 n = ((unsigned long) n) / (unsigned) base; \
43 static char *number(char *str
, long num
, int base
, int size
, int precision
,
46 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
47 static const char digits
[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
53 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
54 * produces same digits or (maybe lowercased) letters */
55 locase
= (type
& SMALL
);
58 if (base
< 2 || base
> 36)
60 c
= (type
& ZEROPAD
) ? '0' : ' ';
67 } else if (type
& PLUS
) {
70 } else if (type
& SPACE
) {
86 tmp
[i
++] = (digits
[__do_div(num
, base
)] | locase
);
90 if (!(type
& (ZEROPAD
+ LEFT
)))
98 else if (base
== 16) {
100 *str
++ = ('X' | locase
);
106 while (i
< precision
--)
115 int vsprintf(char *buf
, const char *fmt
, va_list args
)
123 int flags
; /* flags to number() */
125 int field_width
; /* width of output field */
126 int precision
; /* min. # of digits for integers; max
127 number of chars for from string */
128 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
130 for (str
= buf
; *fmt
; ++fmt
) {
139 ++fmt
; /* this also skips first '%' */
158 /* get field width */
161 field_width
= skip_atoi(&fmt
);
162 else if (*fmt
== '*') {
164 /* it's the next argument */
165 field_width
= va_arg(args
, int);
166 if (field_width
< 0) {
167 field_width
= -field_width
;
172 /* get the precision */
177 precision
= skip_atoi(&fmt
);
178 else if (*fmt
== '*') {
180 /* it's the next argument */
181 precision
= va_arg(args
, int);
187 /* get the conversion qualifier */
189 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L') {
200 while (--field_width
> 0)
202 *str
++ = (unsigned char)va_arg(args
, int);
203 while (--field_width
> 0)
208 s
= va_arg(args
, char *);
209 len
= strnlen(s
, precision
);
212 while (len
< field_width
--)
214 for (i
= 0; i
< len
; ++i
)
216 while (len
< field_width
--)
221 if (field_width
== -1) {
222 field_width
= 2 * sizeof(void *);
226 (unsigned long)va_arg(args
, void *), 16,
227 field_width
, precision
, flags
);
231 if (qualifier
== 'l') {
232 long *ip
= va_arg(args
, long *);
235 int *ip
= va_arg(args
, int *);
244 /* integer number formats - set up the flags and "break" */
269 if (qualifier
== 'l')
270 num
= va_arg(args
, unsigned long);
271 else if (qualifier
== 'h') {
272 num
= (unsigned short)va_arg(args
, int);
275 } else if (flags
& SIGN
)
276 num
= va_arg(args
, int);
278 num
= va_arg(args
, unsigned int);
279 str
= number(str
, num
, base
, field_width
, precision
, flags
);
285 int sprintf(char *buf
, const char *fmt
, ...)
291 i
= vsprintf(buf
, fmt
, args
);
296 int printf(const char *fmt
, ...)
298 char printf_buf
[1024];
303 printed
= vsprintf(printf_buf
, fmt
, args
);