2 * Copyright (C) Paul Mackerras 1997.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
15 size_t strnlen(const char * s
, size_t count
)
19 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
26 # define do_div(n, base) ({ \
27 unsigned int __base = (base); \
29 __rem = ((unsigned long long)(n)) % __base; \
30 (n) = ((unsigned long long)(n)) / __base; \
36 extern unsigned int __div64_32(unsigned long long *dividend
,
37 unsigned int divisor
);
39 /* The unnecessary pointer compare is there
40 * to check for type safety (n must be 64bit)
42 # define do_div(n,base) ({ \
43 unsigned int __base = (base); \
45 (void)(((typeof((n)) *)0) == ((unsigned long long *)0)); \
46 if (((n) >> 32) == 0) { \
47 __rem = (unsigned int)(n) % __base; \
48 (n) = (unsigned int)(n) / __base; \
50 __rem = __div64_32(&(n), __base); \
54 #endif /* __powerpc64__ */
56 static int skip_atoi(const char **s
)
60 for (i
= 0; '0' <= (c
= **s
) && c
<= '9'; ++*s
)
65 #define ZEROPAD 1 /* pad with zero */
66 #define SIGN 2 /* unsigned/signed long */
67 #define PLUS 4 /* show plus */
68 #define SPACE 8 /* space if plus */
69 #define LEFT 16 /* left justified */
70 #define SPECIAL 32 /* 0x */
71 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
73 static char * number(char * str
, unsigned long long num
, int base
, int size
, int precision
, int type
)
76 const char *digits
="0123456789abcdefghijklmnopqrstuvwxyz";
80 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
83 if (base
< 2 || base
> 36)
85 c
= (type
& ZEROPAD
) ? '0' : ' ';
88 if ((signed long long)num
< 0) {
90 num
= - (signed long long)num
;
92 } else if (type
& PLUS
) {
95 } else if (type
& SPACE
) {
100 if (type
& SPECIAL
) {
109 else while (num
!= 0) {
110 tmp
[i
++] = digits
[do_div(num
, base
)];
115 if (!(type
&(ZEROPAD
+LEFT
)))
120 if (type
& SPECIAL
) {
131 while (i
< precision
--)
140 int vsprintf(char *buf
, const char *fmt
, va_list args
)
143 unsigned long long num
;
148 int flags
; /* flags to number() */
150 int field_width
; /* width of output field */
151 int precision
; /* min. # of digits for integers; max
152 number of chars for from string */
153 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
154 /* 'z' support added 23/7/1999 S.H. */
155 /* 'z' changed to 'Z' --davidm 1/25/99 */
158 for (str
=buf
; *fmt
; ++fmt
) {
167 ++fmt
; /* this also skips first '%' */
169 case '-': flags
|= LEFT
; goto repeat
;
170 case '+': flags
|= PLUS
; goto repeat
;
171 case ' ': flags
|= SPACE
; goto repeat
;
172 case '#': flags
|= SPECIAL
; goto repeat
;
173 case '0': flags
|= ZEROPAD
; goto repeat
;
176 /* get field width */
178 if ('0' <= *fmt
&& *fmt
<= '9')
179 field_width
= skip_atoi(&fmt
);
180 else if (*fmt
== '*') {
182 /* it's the next argument */
183 field_width
= va_arg(args
, int);
184 if (field_width
< 0) {
185 field_width
= -field_width
;
190 /* get the precision */
194 if ('0' <= *fmt
&& *fmt
<= '9')
195 precision
= skip_atoi(&fmt
);
196 else if (*fmt
== '*') {
198 /* it's the next argument */
199 precision
= va_arg(args
, int);
205 /* get the conversion qualifier */
207 if (*fmt
== 'l' && *(fmt
+ 1) == 'l') {
210 } else if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L'
222 while (--field_width
> 0)
224 *str
++ = (unsigned char) va_arg(args
, int);
225 while (--field_width
> 0)
230 s
= va_arg(args
, char *);
234 len
= strnlen(s
, precision
);
237 while (len
< field_width
--)
239 for (i
= 0; i
< len
; ++i
)
241 while (len
< field_width
--)
246 if (field_width
== -1) {
247 field_width
= 2*sizeof(void *);
251 (unsigned long) va_arg(args
, void *), 16,
252 field_width
, precision
, flags
);
257 if (qualifier
== 'l') {
258 long * ip
= va_arg(args
, long *);
260 } else if (qualifier
== 'Z') {
261 size_t * ip
= va_arg(args
, size_t *);
264 int * ip
= va_arg(args
, int *);
273 /* integer number formats - set up the flags and "break" */
298 if (qualifier
== 'l') {
299 num
= va_arg(args
, unsigned long);
301 num
= (signed long) num
;
302 } else if (qualifier
== 'q') {
303 num
= va_arg(args
, unsigned long long);
305 num
= (signed long long) num
;
306 } else if (qualifier
== 'Z') {
307 num
= va_arg(args
, size_t);
308 } else if (qualifier
== 'h') {
309 num
= (unsigned short) va_arg(args
, int);
311 num
= (signed short) num
;
313 num
= va_arg(args
, unsigned int);
315 num
= (signed int) num
;
317 str
= number(str
, num
, base
, field_width
, precision
, flags
);
323 int sprintf(char * buf
, const char *fmt
, ...)
329 i
=vsprintf(buf
,fmt
,args
);
334 static char sprint_buf
[1024];
337 printf(const char *fmt
, ...)
343 n
= vsprintf(sprint_buf
, fmt
, args
);
345 if (console_ops
.write
)
346 console_ops
.write(sprint_buf
, n
);