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 SMALL 32 /* Must be 32 == 0x20 */
37 #define SPECIAL 64 /* 0x */
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 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
49 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
55 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
56 * produces same digits or (maybe lowercased) letters */
57 locase = (type & SMALL);
60 if (base < 2 || base > 36)
62 c = (type & ZEROPAD) ? '0' : ' ';
69 } else if (type & PLUS) {
72 } else if (type & SPACE) {
88 tmp[i++] = (digits[do_div(num, base)] | locase);
92 if (!(type & (ZEROPAD + LEFT)))
100 else if (base == 16) {
102 *str++ = ('X' | locase);
108 while (i < precision--)
117 int vsprintf(char *buf, const char *fmt, va_list args)
125 int flags; /* flags to number() */
127 int field_width; /* width of output field */
128 int precision; /* min. # of digits for integers; max
129 number of chars for from string */
130 int qualifier; /* 'h', 'l', or 'L' for integer fields */
132 for (str = buf; *fmt; ++fmt) {
141 ++fmt; /* this also skips first '%' */
160 /* get field width */
163 field_width = skip_atoi(&fmt);
164 else if (*fmt == '*') {
166 /* it's the next argument */
167 field_width = va_arg(args, int);
168 if (field_width < 0) {
169 field_width = -field_width;
174 /* get the precision */
179 precision = skip_atoi(&fmt);
180 else if (*fmt == '*') {
182 /* it's the next argument */
183 precision = va_arg(args, int);
189 /* get the conversion qualifier */
191 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
202 while (--field_width > 0)
204 *str++ = (unsigned char)va_arg(args, int);
205 while (--field_width > 0)
210 s = va_arg(args, char *);
211 len = strnlen(s, precision);
214 while (len < field_width--)
216 for (i = 0; i < len; ++i)
218 while (len < field_width--)
223 if (field_width == -1) {
224 field_width = 2 * sizeof(void *);
228 (unsigned long)va_arg(args, void *), 16,
229 field_width, precision, flags);
233 if (qualifier == 'l') {
234 long *ip = va_arg(args, long *);
237 int *ip = va_arg(args, int *);
246 /* integer number formats - set up the flags and "break" */
271 if (qualifier == 'l')
272 num = va_arg(args, unsigned long);
273 else if (qualifier == 'h') {
274 num = (unsigned short)va_arg(args, int);
277 } else if (flags & SIGN)
278 num = va_arg(args, int);
280 num = va_arg(args, unsigned int);
281 str = number(str, num, base, field_width, precision, flags);
287 int sprintf(char *buf, const char *fmt, ...)
293 i = vsprintf(buf, fmt, args);
298 int printf(const char *fmt, ...)
300 char printf_buf[1024];
305 printed = vsprintf(printf_buf, fmt, args);