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 #include <linux/compiler.h>
19 #include <linux/ctype.h>
20 #include <linux/string.h>
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 > 16)
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" */
273 if (qualifier == 'l') {
274 num = va_arg(args, unsigned long);
275 } else if (qualifier == 'h') {
276 num = (unsigned short)va_arg(args, int);
279 } else if (flags & SIGN) {
280 num = va_arg(args, int);
282 num = va_arg(args, unsigned int);
284 str = number(str, num, base, field_width, precision, flags);
290 int sprintf(char *buf, const char *fmt, ...)
296 i = vsprintf(buf, fmt, args);