]>
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
++))) {
80 if (ch
>= '0' && ch
<= '9') {
82 while (ch
>= '0' && ch
<= '9') {
83 width
= (width
* 10) + ch
- '0';
103 num
= va_arg(va
, unsigned long);
104 if (sizeof(long) > 4)
107 num
= va_arg(va
, unsigned int);
111 if (islong
&& (long)num
< 0) {
114 } else if (!islong
&& (int)num
< 0) {
122 for (; div
; div
/= 10)
123 div_out(info
, &num
, div
);
128 num
= va_arg(va
, unsigned long);
129 div
= 1UL << (sizeof(long) * 8 - 4);
131 num
= va_arg(va
, unsigned int);
137 for (; div
; div
/= 0x10)
138 div_out(info
, &num
, div
);
142 out(info
, (char)(va_arg(va
, int)));
145 p
= va_arg(va
, char*);
155 while (*info
->bf
++ && width
> 0)
158 info
->putc(info
, lz
? '0' : ' ');
161 info
->putc(info
, ch
);
170 int vprintf(const char *fmt
, va_list va
)
172 struct printf_info info
;
174 info
.putc
= putc_normal
;
175 return _vprintf(&info
, fmt
, va
);
178 int printf(const char *fmt
, ...)
180 struct printf_info info
;
185 info
.putc
= putc_normal
;
187 ret
= _vprintf(&info
, fmt
, va
);
193 static void putc_outstr(struct printf_info
*info
, char ch
)
195 *info
->outstr
++ = ch
;
198 int sprintf(char *buf
, const char *fmt
, ...)
200 struct printf_info info
;
206 info
.putc
= putc_outstr
;
207 ret
= _vprintf(&info
, fmt
, va
);
214 /* Note that size is ignored */
215 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
217 struct printf_info info
;
223 info
.putc
= putc_outstr
;
224 ret
= _vprintf(&info
, fmt
, va
);
231 void __assert_fail(const char *assertion
, const char *file
, unsigned line
,
232 const char *function
)
234 /* This will not return */
235 printf("%s:%u: %s: Assertion `%s' failed.", file
, line
, function
,