]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - lib/tiny-printf.c
2 * Tiny printf version for SPL
5 * http://www.sparetimelabs.com/printfrevisited/printfrevisited.php
7 * Copyright (C) 2004,2008 Kustaa Nyholm
9 * SPDX-License-Identifier: LGPL-2.1+
17 char *bf
; /* Digit buffer */
18 char zs
; /* non-zero if a digit has been written */
19 char *outstr
; /* Next output position for sprintf() */
21 /* Output a character */
22 void (*putc
)(struct printf_info
*info
, char ch
);
25 void putc_normal(struct printf_info
*info
, char ch
)
30 static void out(struct printf_info
*info
, char c
)
35 static void out_dgt(struct printf_info
*info
, char dgt
)
37 out(info
, dgt
+ (dgt
< 10 ? '0' : 'a' - 10));
41 static void div_out(struct printf_info
*info
, unsigned long *num
,
44 unsigned char dgt
= 0;
51 if (info
->zs
|| dgt
> 0)
55 int _vprintf(struct printf_info
*info
, const char *fmt
, va_list va
)
63 while ((ch
= *(fmt
++))) {
77 if (ch
>= '0' && ch
<= '9') {
79 while (ch
>= '0' && ch
<= '9') {
80 width
= (width
* 10) + ch
- '0';
100 num
= va_arg(va
, unsigned long);
101 if (sizeof(long) > 4)
104 num
= va_arg(va
, unsigned int);
108 if (islong
&& (long)num
< 0) {
111 } else if (!islong
&& (int)num
< 0) {
119 for (; div
; div
/= 10)
120 div_out(info
, &num
, div
);
125 num
= va_arg(va
, unsigned long);
126 div
= 1UL << (sizeof(long) * 8 - 4);
128 num
= va_arg(va
, unsigned int);
134 for (; div
; div
/= 0x10)
135 div_out(info
, &num
, div
);
139 out(info
, (char)(va_arg(va
, int)));
142 p
= va_arg(va
, char*);
152 while (*info
->bf
++ && width
> 0)
155 info
->putc(info
, lz
? '0' : ' ');
158 info
->putc(info
, ch
);
167 int vprintf(const char *fmt
, va_list va
)
169 struct printf_info info
;
171 info
.putc
= putc_normal
;
172 return _vprintf(&info
, fmt
, va
);
175 int printf(const char *fmt
, ...)
177 struct printf_info info
;
182 info
.putc
= putc_normal
;
184 ret
= _vprintf(&info
, fmt
, va
);
190 static void putc_outstr(struct printf_info
*info
, char ch
)
192 *info
->outstr
++ = ch
;
195 int sprintf(char *buf
, const char *fmt
, ...)
197 struct printf_info info
;
203 info
.putc
= putc_outstr
;
204 ret
= _vprintf(&info
, fmt
, va
);
211 /* Note that size is ignored */
212 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
214 struct printf_info info
;
220 info
.putc
= putc_outstr
;
221 ret
= _vprintf(&info
, fmt
, va
);
228 void __assert_fail(const char *assertion
, const char *file
, unsigned line
,
229 const char *function
)
231 /* This will not return */
232 printf("%s:%u: %s: Assertion `%s' failed.", file
, line
, function
,