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 * arch/i386/boot/printf.c
14 * Oh, it's a waste of space, but oh-so-yummy for debugging. This
15 * version of printf() does not include 64-bit support. "Live with
22 static int skip_atoi(const char **s)
27 i = i * 10 + *((*s)++) - '0';
31 #define ZEROPAD 1 /* pad with zero */
32 #define SIGN 2 /* unsigned/signed long */
33 #define PLUS 4 /* show plus */
34 #define SPACE 8 /* space if plus */
35 #define LEFT 16 /* left justified */
36 #define SPECIAL 32 /* 0x */
37 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
39 #define do_div(n,base) ({ \
41 __res = ((unsigned long) n) % (unsigned) base; \
42 n = ((unsigned long) n) / (unsigned) base; \
45 static char *number(char *str, long num, int base, int size, int precision,
48 char c, sign, tmp[66];
49 const char *digits = "0123456789abcdefghijklmnopqrstuvwxyz";
53 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
56 if (base < 2 || base > 36)
58 c = (type & ZEROPAD) ? '0' : ' ';
65 } else if (type & PLUS) {
68 } else if (type & SPACE) {
84 tmp[i++] = digits[do_div(num, base)];
88 if (!(type & (ZEROPAD + LEFT)))
96 else if (base == 16) {
104 while (i < precision--)
113 int vsprintf(char *buf, const char *fmt, va_list args)
121 int flags; /* flags to number() */
123 int field_width; /* width of output field */
124 int precision; /* min. # of digits for integers; max
125 number of chars for from string */
126 int qualifier; /* 'h', 'l', or 'L' for integer fields */
128 for (str = buf; *fmt; ++fmt) {
137 ++fmt; /* this also skips first '%' */
156 /* get field width */
159 field_width = skip_atoi(&fmt);
160 else if (*fmt == '*') {
162 /* it's the next argument */
163 field_width = va_arg(args, int);
164 if (field_width < 0) {
165 field_width = -field_width;
170 /* get the precision */
175 precision = skip_atoi(&fmt);
176 else if (*fmt == '*') {
178 /* it's the next argument */
179 precision = va_arg(args, int);
185 /* get the conversion qualifier */
187 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
198 while (--field_width > 0)
200 *str++ = (unsigned char)va_arg(args, int);
201 while (--field_width > 0)
206 s = va_arg(args, char *);
207 len = strnlen(s, precision);
210 while (len < field_width--)
212 for (i = 0; i < len; ++i)
214 while (len < field_width--)
219 if (field_width == -1) {
220 field_width = 2 * sizeof(void *);
224 (unsigned long)va_arg(args, void *), 16,
225 field_width, precision, flags);
229 if (qualifier == 'l') {
230 long *ip = va_arg(args, long *);
233 int *ip = va_arg(args, int *);
242 /* integer number formats - set up the flags and "break" */
267 if (qualifier == 'l')
268 num = va_arg(args, unsigned long);
269 else if (qualifier == 'h') {
270 num = (unsigned short)va_arg(args, int);
273 } else if (flags & SIGN)
274 num = va_arg(args, int);
276 num = va_arg(args, unsigned int);
277 str = number(str, num, base, field_width, precision, flags);
283 int sprintf(char *buf, const char *fmt, ...)
289 i = vsprintf(buf, fmt, args);
294 int printf(const char *fmt, ...)
296 char printf_buf[1024];
301 printed = vsprintf(printf_buf, fmt, args);