1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) Paul Mackerras 1997.
5 #include <linux/string.h>
6 #include <linux/stdarg.h>
8 size_t strnlen(const char * s, size_t count)
12 for (sc = s; count-- && *sc != '\0'; ++sc)
17 # define do_div(n, base) ({ \
18 unsigned int __base = (base); \
20 __rem = ((unsigned long long)(n)) % __base; \
21 (n) = ((unsigned long long)(n)) / __base; \
26 static int skip_atoi(const char **s)
30 for (i = 0; '0' <= (c = **s) && c <= '9'; ++*s)
35 #define ZEROPAD 1 /* pad with zero */
36 #define SIGN 2 /* unsigned/signed long */
37 #define PLUS 4 /* show plus */
38 #define SPACE 8 /* space if plus */
39 #define LEFT 16 /* left justified */
40 #define SPECIAL 32 /* 0x */
41 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
43 static char * number(char * str, unsigned long long num, int base, int size, int precision, int type)
45 char c, sign, tmp[66];
46 const char *digits = "0123456789abcdefghijklmnopqrstuvwxyz";
50 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
53 if (base < 2 || base > 36)
55 c = (type & ZEROPAD) ? '0' : ' ';
58 if ((signed long long)num < 0) {
60 num = - (signed long long)num;
62 } else if (type & PLUS) {
65 } else if (type & SPACE) {
79 else while (num != 0) {
80 tmp[i++] = digits[do_div(num, base)];
85 if (!(type&(ZEROPAD+LEFT)))
93 else if (base == 16) {
101 while (i < precision--)
110 int vsprintf(char *buf, const char *fmt, va_list args)
113 unsigned long long num;
118 int flags; /* flags to number() */
120 int field_width; /* width of output field */
121 int precision; /* min. # of digits for integers; max
122 number of chars for from string */
123 int qualifier; /* 'h', 'l', or 'L' for integer fields */
124 /* 'z' support added 23/7/1999 S.H. */
125 /* 'z' changed to 'Z' --davidm 1/25/99 */
128 for (str = buf ; *fmt ; ++fmt) {
137 ++fmt; /* this also skips first '%' */
139 case '-': flags |= LEFT; goto repeat;
140 case '+': flags |= PLUS; goto repeat;
141 case ' ': flags |= SPACE; goto repeat;
142 case '#': flags |= SPECIAL; goto repeat;
143 case '0': flags |= ZEROPAD; goto repeat;
146 /* get field width */
148 if ('0' <= *fmt && *fmt <= '9')
149 field_width = skip_atoi(&fmt);
150 else if (*fmt == '*') {
152 /* it's the next argument */
153 field_width = va_arg(args, int);
154 if (field_width < 0) {
155 field_width = -field_width;
160 /* get the precision */
164 if ('0' <= *fmt && *fmt <= '9')
165 precision = skip_atoi(&fmt);
166 else if (*fmt == '*') {
168 /* it's the next argument */
169 precision = va_arg(args, int);
175 /* get the conversion qualifier */
177 if (*fmt == 'l' && *(fmt + 1) == 'l') {
180 } else if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L'
192 while (--field_width > 0)
194 *str++ = (unsigned char) va_arg(args, int);
195 while (--field_width > 0)
200 s = va_arg(args, char *);
204 len = strnlen(s, precision);
207 while (len < field_width--)
209 for (i = 0; i < len; ++i)
211 while (len < field_width--)
216 if (field_width == -1) {
217 field_width = 2*sizeof(void *);
221 (unsigned long) va_arg(args, void *), 16,
222 field_width, precision, flags);
227 if (qualifier == 'l') {
228 long * ip = va_arg(args, long *);
230 } else if (qualifier == 'Z') {
231 size_t * ip = va_arg(args, size_t *);
234 int * ip = va_arg(args, int *);
243 /* integer number formats - set up the flags and "break" */
268 if (qualifier == 'l') {
269 num = va_arg(args, unsigned long);
271 num = (signed long) num;
272 } else if (qualifier == 'q') {
273 num = va_arg(args, unsigned long long);
275 num = (signed long long) num;
276 } else if (qualifier == 'Z') {
277 num = va_arg(args, size_t);
278 } else if (qualifier == 'h') {
279 num = (unsigned short) va_arg(args, int);
281 num = (signed short) num;
283 num = va_arg(args, unsigned int);
285 num = (signed int) num;
287 str = number(str, num, base, field_width, precision, flags);
293 int sprintf(char * buf, const char *fmt, ...)
299 i = vsprintf(buf, fmt, args);