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 == 'l' && *(fmt + 1) == 'l') {
196 } else if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L'
208 while (--field_width > 0)
210 *str++ = (unsigned char) va_arg(args, int);
211 while (--field_width > 0)
216 s = va_arg(args, char *);
220 len = strnlen(s, precision);
223 while (len < field_width--)
225 for (i = 0; i < len; ++i)
227 while (len < field_width--)
232 if (field_width == -1) {
233 field_width = 2*sizeof(void *);
237 (unsigned long) va_arg(args, void *), 16,
238 field_width, precision, flags);
243 if (qualifier == 'l') {
244 long * ip = va_arg(args, long *);
246 } else if (qualifier == 'Z') {
247 size_t * ip = va_arg(args, size_t *);
250 int * ip = va_arg(args, int *);
259 /* integer number formats - set up the flags and "break" */
284 if (qualifier == 'l') {
285 num = va_arg(args, unsigned long);
287 num = (signed long) num;
288 } else if (qualifier == 'q') {
289 num = va_arg(args, unsigned long long);
291 num = (signed long long) num;
292 } else if (qualifier == 'Z') {
293 num = va_arg(args, size_t);
294 } else if (qualifier == 'h') {
295 num = (unsigned short) va_arg(args, int);
297 num = (signed short) num;
299 num = va_arg(args, unsigned int);
301 num = (signed int) num;
303 str = number(str, num, base, field_width, precision, flags);
309 int sprintf(char * buf, const char *fmt, ...)
315 i=vsprintf(buf,fmt,args);
320 static char sprint_buf[1024];
323 printf(const char *fmt, ...)
329 n = vsprintf(sprint_buf, fmt, args);
331 if (console_ops.write)
332 console_ops.write(sprint_buf, n);