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.
12 size_t strnlen(const char * s
, size_t count
)
16 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
21 # define do_div(n, base) ({ \
22 unsigned int __base = (base); \
24 __rem = ((unsigned long long)(n)) % __base; \
25 (n) = ((unsigned long long)(n)) / __base; \
30 static int skip_atoi(const char **s
)
34 for (i
= 0; '0' <= (c
= **s
) && c
<= '9'; ++*s
)
39 #define ZEROPAD 1 /* pad with zero */
40 #define SIGN 2 /* unsigned/signed long */
41 #define PLUS 4 /* show plus */
42 #define SPACE 8 /* space if plus */
43 #define LEFT 16 /* left justified */
44 #define SPECIAL 32 /* 0x */
45 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
47 static char * number(char * str
, unsigned long long num
, int base
, int size
, int precision
, int type
)
50 const char *digits
="0123456789abcdefghijklmnopqrstuvwxyz";
54 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
57 if (base
< 2 || base
> 36)
59 c
= (type
& ZEROPAD
) ? '0' : ' ';
62 if ((signed long long)num
< 0) {
64 num
= - (signed long long)num
;
66 } else if (type
& PLUS
) {
69 } else if (type
& SPACE
) {
83 else while (num
!= 0) {
84 tmp
[i
++] = digits
[do_div(num
, base
)];
89 if (!(type
&(ZEROPAD
+LEFT
)))
105 while (i
< precision
--)
114 int vsprintf(char *buf
, const char *fmt
, va_list args
)
117 unsigned long long num
;
122 int flags
; /* flags to number() */
124 int field_width
; /* width of output field */
125 int precision
; /* min. # of digits for integers; max
126 number of chars for from string */
127 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
128 /* 'z' support added 23/7/1999 S.H. */
129 /* 'z' changed to 'Z' --davidm 1/25/99 */
132 for (str
=buf
; *fmt
; ++fmt
) {
141 ++fmt
; /* this also skips first '%' */
143 case '-': flags
|= LEFT
; goto repeat
;
144 case '+': flags
|= PLUS
; goto repeat
;
145 case ' ': flags
|= SPACE
; goto repeat
;
146 case '#': flags
|= SPECIAL
; goto repeat
;
147 case '0': flags
|= ZEROPAD
; goto repeat
;
150 /* get field width */
152 if ('0' <= *fmt
&& *fmt
<= '9')
153 field_width
= skip_atoi(&fmt
);
154 else if (*fmt
== '*') {
156 /* it's the next argument */
157 field_width
= va_arg(args
, int);
158 if (field_width
< 0) {
159 field_width
= -field_width
;
164 /* get the precision */
168 if ('0' <= *fmt
&& *fmt
<= '9')
169 precision
= skip_atoi(&fmt
);
170 else if (*fmt
== '*') {
172 /* it's the next argument */
173 precision
= va_arg(args
, int);
179 /* get the conversion qualifier */
181 if (*fmt
== 'l' && *(fmt
+ 1) == 'l') {
184 } else if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L'
196 while (--field_width
> 0)
198 *str
++ = (unsigned char) va_arg(args
, int);
199 while (--field_width
> 0)
204 s
= va_arg(args
, char *);
208 len
= strnlen(s
, precision
);
211 while (len
< field_width
--)
213 for (i
= 0; i
< len
; ++i
)
215 while (len
< field_width
--)
220 if (field_width
== -1) {
221 field_width
= 2*sizeof(void *);
225 (unsigned long) va_arg(args
, void *), 16,
226 field_width
, precision
, flags
);
231 if (qualifier
== 'l') {
232 long * ip
= va_arg(args
, long *);
234 } else if (qualifier
== 'Z') {
235 size_t * ip
= va_arg(args
, size_t *);
238 int * ip
= va_arg(args
, int *);
247 /* integer number formats - set up the flags and "break" */
272 if (qualifier
== 'l') {
273 num
= va_arg(args
, unsigned long);
275 num
= (signed long) num
;
276 } else if (qualifier
== 'q') {
277 num
= va_arg(args
, unsigned long long);
279 num
= (signed long long) num
;
280 } else if (qualifier
== 'Z') {
281 num
= va_arg(args
, size_t);
282 } else if (qualifier
== 'h') {
283 num
= (unsigned short) va_arg(args
, int);
285 num
= (signed short) num
;
287 num
= va_arg(args
, unsigned int);
289 num
= (signed int) num
;
291 str
= number(str
, num
, base
, field_width
, precision
, flags
);
297 int sprintf(char * buf
, const char *fmt
, ...)
303 i
=vsprintf(buf
,fmt
,args
);