1 // SPDX-License-Identifier: GPL-2.0-only
2 /* -*- linux-c -*- ------------------------------------------------------- *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright 2007 rPath, Inc. - All Rights Reserved
7 * ----------------------------------------------------------------------- */
10 * Oh, it's a waste of space, but oh-so-yummy for debugging. This
11 * version of printf() does not include 64-bit support. "Live with
18 static int skip_atoi(const char **s)
23 i = i * 10 + *((*s)++) - '0';
27 #define ZEROPAD 1 /* pad with zero */
28 #define SIGN 2 /* unsigned/signed long */
29 #define PLUS 4 /* show plus */
30 #define SPACE 8 /* space if plus */
31 #define LEFT 16 /* left justified */
32 #define SMALL 32 /* Must be 32 == 0x20 */
33 #define SPECIAL 64 /* 0x */
35 #define __do_div(n, base) ({ \
37 __res = ((unsigned long) n) % (unsigned) base; \
38 n = ((unsigned long) n) / (unsigned) base; \
41 static char *number(char *str, long num, int base, int size, int precision,
44 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
45 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
51 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
52 * produces same digits or (maybe lowercased) letters */
53 locase = (type & SMALL);
56 if (base < 2 || base > 16)
58 c = (type & ZEROPAD) ? '0' : ' ';
65 } else if (type & PLUS) {
68 } else if (type & SPACE) {
84 tmp[i++] = (digits[__do_div(num, base)] | locase);
88 if (!(type & (ZEROPAD + LEFT)))
96 else if (base == 16) {
98 *str++ = ('X' | locase);
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" */
270 if (qualifier == 'l')
271 num = va_arg(args, unsigned long);
272 else if (qualifier == 'h') {
273 num = (unsigned short)va_arg(args, int);
276 } else if (flags & SIGN)
277 num = va_arg(args, int);
279 num = va_arg(args, unsigned int);
280 str = number(str, num, base, field_width, precision, flags);
286 int sprintf(char *buf, const char *fmt, ...)
292 i = vsprintf(buf, fmt, args);
297 int printf(const char *fmt, ...)
299 char printf_buf[1024];
304 printed = vsprintf(printf_buf, fmt, args);