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
)
24 extern unsigned int __div64_32(unsigned long long *dividend
,
25 unsigned int divisor
);
27 /* The unnecessary pointer compare is there
28 * to check for type safety (n must be 64bit)
30 # define do_div(n,base) ({ \
31 unsigned int __base = (base); \
33 (void)(((typeof((n)) *)0) == ((unsigned long long *)0)); \
34 if (((n) >> 32) == 0) { \
35 __rem = (unsigned int)(n) % __base; \
36 (n) = (unsigned int)(n) / __base; \
38 __rem = __div64_32(&(n), __base); \
42 static int skip_atoi(const char **s
)
46 for (i
= 0; '0' <= (c
= **s
) && c
<= '9'; ++*s
)
51 #define ZEROPAD 1 /* pad with zero */
52 #define SIGN 2 /* unsigned/signed long */
53 #define PLUS 4 /* show plus */
54 #define SPACE 8 /* space if plus */
55 #define LEFT 16 /* left justified */
56 #define SPECIAL 32 /* 0x */
57 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
59 static char * number(char * str
, unsigned long long num
, int base
, int size
, int precision
, int type
)
62 const char *digits
="0123456789abcdefghijklmnopqrstuvwxyz";
66 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
69 if (base
< 2 || base
> 36)
71 c
= (type
& ZEROPAD
) ? '0' : ' ';
74 if ((signed long long)num
< 0) {
76 num
= - (signed long long)num
;
78 } else if (type
& PLUS
) {
81 } else if (type
& SPACE
) {
95 else while (num
!= 0) {
96 tmp
[i
++] = digits
[do_div(num
, base
)];
101 if (!(type
&(ZEROPAD
+LEFT
)))
106 if (type
& SPECIAL
) {
117 while (i
< precision
--)
126 int vsprintf(char *buf
, const char *fmt
, va_list args
)
129 unsigned long long num
;
134 int flags
; /* flags to number() */
136 int field_width
; /* width of output field */
137 int precision
; /* min. # of digits for integers; max
138 number of chars for from string */
139 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
140 /* 'z' support added 23/7/1999 S.H. */
141 /* 'z' changed to 'Z' --davidm 1/25/99 */
144 for (str
=buf
; *fmt
; ++fmt
) {
153 ++fmt
; /* this also skips first '%' */
155 case '-': flags
|= LEFT
; goto repeat
;
156 case '+': flags
|= PLUS
; goto repeat
;
157 case ' ': flags
|= SPACE
; goto repeat
;
158 case '#': flags
|= SPECIAL
; goto repeat
;
159 case '0': flags
|= ZEROPAD
; goto repeat
;
162 /* get field width */
164 if ('0' <= *fmt
&& *fmt
<= '9')
165 field_width
= skip_atoi(&fmt
);
166 else if (*fmt
== '*') {
168 /* it's the next argument */
169 field_width
= va_arg(args
, int);
170 if (field_width
< 0) {
171 field_width
= -field_width
;
176 /* get the precision */
180 if ('0' <= *fmt
&& *fmt
<= '9')
181 precision
= skip_atoi(&fmt
);
182 else if (*fmt
== '*') {
184 /* it's the next argument */
185 precision
= va_arg(args
, int);
191 /* get the conversion qualifier */
193 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' || *fmt
=='Z') {
204 while (--field_width
> 0)
206 *str
++ = (unsigned char) va_arg(args
, int);
207 while (--field_width
> 0)
212 s
= va_arg(args
, char *);
216 len
= strnlen(s
, precision
);
219 while (len
< field_width
--)
221 for (i
= 0; i
< len
; ++i
)
223 while (len
< field_width
--)
228 if (field_width
== -1) {
229 field_width
= 2*sizeof(void *);
233 (unsigned long) va_arg(args
, void *), 16,
234 field_width
, precision
, flags
);
239 if (qualifier
== 'l') {
240 long * ip
= va_arg(args
, long *);
242 } else if (qualifier
== 'Z') {
243 size_t * ip
= va_arg(args
, size_t *);
246 int * ip
= va_arg(args
, int *);
255 /* integer number formats - set up the flags and "break" */
280 if (qualifier
== 'l') {
281 num
= va_arg(args
, unsigned long);
283 num
= (signed long) num
;
284 } else if (qualifier
== 'Z') {
285 num
= va_arg(args
, size_t);
286 } else if (qualifier
== 'h') {
287 num
= (unsigned short) va_arg(args
, int);
289 num
= (signed short) num
;
291 num
= va_arg(args
, unsigned int);
293 num
= (signed int) num
;
295 str
= number(str
, num
, base
, field_width
, precision
, flags
);
301 int sprintf(char * buf
, const char *fmt
, ...)
307 i
=vsprintf(buf
,fmt
,args
);
312 static char sprint_buf
[1024];
315 printf(const char *fmt
, ...)
321 n
= vsprintf(sprint_buf
, fmt
, args
);
323 console_ops
.write(sprint_buf
, n
);