1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) Paul Mackerras 1997.
11 size_t strnlen(const char * s
, size_t count
)
15 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
20 char *strrchr(const char *s
, int c
)
22 const char *last
= NULL
;
32 # define do_div(n, base) ({ \
33 unsigned int __base = (base); \
35 __rem = ((unsigned long long)(n)) % __base; \
36 (n) = ((unsigned long long)(n)) / __base; \
42 extern unsigned int __div64_32(unsigned long long *dividend
,
43 unsigned int divisor
);
45 /* The unnecessary pointer compare is there
46 * to check for type safety (n must be 64bit)
48 # define do_div(n,base) ({ \
49 unsigned int __base = (base); \
51 (void)(((typeof((n)) *)0) == ((unsigned long long *)0)); \
52 if (((n) >> 32) == 0) { \
53 __rem = (unsigned int)(n) % __base; \
54 (n) = (unsigned int)(n) / __base; \
56 __rem = __div64_32(&(n), __base); \
60 #endif /* __powerpc64__ */
62 static int skip_atoi(const char **s
)
66 for (i
= 0; '0' <= (c
= **s
) && c
<= '9'; ++*s
)
71 #define ZEROPAD 1 /* pad with zero */
72 #define SIGN 2 /* unsigned/signed long */
73 #define PLUS 4 /* show plus */
74 #define SPACE 8 /* space if plus */
75 #define LEFT 16 /* left justified */
76 #define SPECIAL 32 /* 0x */
77 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
79 static char * number(char * str
, unsigned long long num
, int base
, int size
, int precision
, int type
)
82 const char *digits
="0123456789abcdefghijklmnopqrstuvwxyz";
86 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
89 if (base
< 2 || base
> 36)
91 c
= (type
& ZEROPAD
) ? '0' : ' ';
94 if ((signed long long)num
< 0) {
96 num
= - (signed long long)num
;
98 } else if (type
& PLUS
) {
101 } else if (type
& SPACE
) {
106 if (type
& SPECIAL
) {
115 else while (num
!= 0) {
116 tmp
[i
++] = digits
[do_div(num
, base
)];
121 if (!(type
&(ZEROPAD
+LEFT
)))
126 if (type
& SPECIAL
) {
137 while (i
< precision
--)
146 int vsprintf(char *buf
, const char *fmt
, va_list args
)
149 unsigned long long num
;
154 int flags
; /* flags to number() */
156 int field_width
; /* width of output field */
157 int precision
; /* min. # of digits for integers; max
158 number of chars for from string */
159 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
160 /* 'z' support added 23/7/1999 S.H. */
161 /* 'z' changed to 'Z' --davidm 1/25/99 */
164 for (str
=buf
; *fmt
; ++fmt
) {
173 ++fmt
; /* this also skips first '%' */
175 case '-': flags
|= LEFT
; goto repeat
;
176 case '+': flags
|= PLUS
; goto repeat
;
177 case ' ': flags
|= SPACE
; goto repeat
;
178 case '#': flags
|= SPECIAL
; goto repeat
;
179 case '0': flags
|= ZEROPAD
; goto repeat
;
182 /* get field width */
184 if ('0' <= *fmt
&& *fmt
<= '9')
185 field_width
= skip_atoi(&fmt
);
186 else if (*fmt
== '*') {
188 /* it's the next argument */
189 field_width
= va_arg(args
, int);
190 if (field_width
< 0) {
191 field_width
= -field_width
;
196 /* get the precision */
200 if ('0' <= *fmt
&& *fmt
<= '9')
201 precision
= skip_atoi(&fmt
);
202 else if (*fmt
== '*') {
204 /* it's the next argument */
205 precision
= va_arg(args
, int);
211 /* get the conversion qualifier */
213 if (*fmt
== 'l' && *(fmt
+ 1) == 'l') {
216 } else if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L'
228 while (--field_width
> 0)
230 *str
++ = (unsigned char) va_arg(args
, int);
231 while (--field_width
> 0)
236 s
= va_arg(args
, char *);
240 len
= strnlen(s
, precision
);
243 while (len
< field_width
--)
245 for (i
= 0; i
< len
; ++i
)
247 while (len
< field_width
--)
252 if (field_width
== -1) {
253 field_width
= 2*sizeof(void *);
257 (unsigned long) va_arg(args
, void *), 16,
258 field_width
, precision
, flags
);
263 if (qualifier
== 'l') {
264 long * ip
= va_arg(args
, long *);
266 } else if (qualifier
== 'Z') {
267 size_t * ip
= va_arg(args
, size_t *);
270 int * ip
= va_arg(args
, int *);
279 /* integer number formats - set up the flags and "break" */
304 if (qualifier
== 'l') {
305 num
= va_arg(args
, unsigned long);
307 num
= (signed long) num
;
308 } else if (qualifier
== 'q') {
309 num
= va_arg(args
, unsigned long long);
311 num
= (signed long long) num
;
312 } else if (qualifier
== 'Z') {
313 num
= va_arg(args
, size_t);
314 } else if (qualifier
== 'h') {
315 num
= (unsigned short) va_arg(args
, int);
317 num
= (signed short) num
;
319 num
= va_arg(args
, unsigned int);
321 num
= (signed int) num
;
323 str
= number(str
, num
, base
, field_width
, precision
, flags
);
329 int sprintf(char * buf
, const char *fmt
, ...)
335 i
=vsprintf(buf
,fmt
,args
);
340 static char sprint_buf
[1024];
343 printf(const char *fmt
, ...)
349 n
= vsprintf(sprint_buf
, fmt
, args
);
351 if (console_ops
.write
)
352 console_ops
.write(sprint_buf
, n
);