2 * Copyright (c) 1986, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
39 * $FreeBSD: src/lib/libstand/printf.c,v 1.4 1999/12/27 08:45:14 peter Exp $
40 * $DragonFly: src/lib/libstand/printf.c,v 1.6 2005/12/11 02:27:26 swildner Exp $
44 * Standaloneified version of the FreeBSD kernel printf family.
47 #include <sys/types.h>
52 * Note that stdarg.h and the ANSI style va_start macro is used for both
53 * ANSI and traditional C compilers.
62 static char *ksprintn(u_long
, int, int *);
63 static int kvprintf(const char *, void (*)(int, void *), void *, int,
65 static void putchar_wrapper(int, void *);
66 static void snprintf_func(int, void *);
69 putchar_wrapper(int ch
, void *arg __unused
)
75 printf(const char *fmt
, ...)
81 retval
= kvprintf(fmt
, putchar_wrapper
, NULL
, 10, ap
);
87 vprintf(const char *fmt
, va_list ap
)
90 kvprintf(fmt
, putchar_wrapper
, NULL
, 10, ap
);
94 sprintf(char *buf
, const char *cfmt
, ...)
100 retval
= kvprintf(cfmt
, NULL
, (void *)buf
, 10, ap
);
107 vsprintf(char *buf
, const char *cfmt
, va_list ap
)
111 retval
= kvprintf(cfmt
, NULL
, (void *)buf
, 10, ap
);
116 snprintf(char *buf
, size_t size
, const char *cfmt
, ...)
122 retval
= vsnprintf(buf
, size
, cfmt
, ap
);
128 vsnprintf(char *buf
, size_t size
, const char *cfmt
, va_list ap
)
130 struct snprintf_arg info
;
135 retval
= kvprintf(cfmt
, snprintf_func
, &info
, 10, ap
);
136 if (info
.remain
>= 1)
142 snprintf_func(int ch
, void *arg
)
144 struct snprintf_arg
* const info
= arg
;
146 if (info
->remain
>= 2) {
153 * Put a number (base <= 16) in a buffer in reverse order; return an
154 * optional length and a pointer to the NULL terminated (preceded?)
158 ksprintn(u_long ul
, int base
, int *lenp
)
159 { /* A long in base 8, plus NULL. */
160 static char buf
[sizeof(long) * NBBY
/ 3 + 2];
165 *++p
= hex2ascii(ul
% base
);
166 } while (ul
/= base
);
173 * Scaled down version of printf(3).
175 * Two additional formats:
177 * The format %b is supported to decode error registers.
180 * printf("reg=%b\n", regval, "<base><arg>*");
182 * where <base> is the output base expressed as a control character, e.g.
183 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
184 * the first of which gives the bit number to be inspected (origin 1), and
185 * the next characters (up to a control character, i.e. a character <= 32),
186 * give the name of the register. Thus:
188 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
190 * would produce output:
192 * reg=3<BITTWO,BITONE>
194 * XXX: %D -- Hexdump, takes pointer and separator string:
195 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
196 * ("%*D", len, ptr, " " -> XX XX XX XX ...
199 kvprintf(char const *fmt
, void (*func
)(int, void *), void *arg
, int radix
,
202 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc, arg); else *d++ = cc; retval++; }
207 int base
, lflag
, tmp
, width
, ladjust
, sharpflag
, neg
, sign
, dot
;
218 fmt
= "(fmt null)\n";
220 if (radix
< 2 || radix
> 36)
226 while ((ch
= (u_char
)*fmt
++) != '%') {
231 lflag
= 0; ladjust
= 0; sharpflag
= 0; neg
= 0;
232 sign
= 0; dot
= 0; dwidth
= 0;
233 reswitch
: switch (ch
= (u_char
)*fmt
++) {
251 width
= va_arg(ap
, int);
257 dwidth
= va_arg(ap
, int);
265 case '1': case '2': case '3': case '4':
266 case '5': case '6': case '7': case '8': case '9':
267 for (n
= 0;; ++fmt
) {
268 n
= n
* 10 + ch
- '0';
270 if (ch
< '0' || ch
> '9')
279 ul
= va_arg(ap
, int);
280 p
= va_arg(ap
, char *);
281 for (q
= ksprintn(ul
, *p
++, NULL
); *q
;)
289 if (ul
& (1 << (n
- 1))) {
290 PCHAR(tmp
? ',' : '<');
291 for (; (n
= *p
) > ' '; ++p
)
295 for (; *p
> ' '; ++p
)
302 PCHAR(va_arg(ap
, int));
305 up
= va_arg(ap
, u_char
*);
306 p
= va_arg(ap
, char *);
310 PCHAR(hex2ascii(*up
>> 4));
311 PCHAR(hex2ascii(*up
& 0x0f));
319 ul
= lflag
? va_arg(ap
, long) : va_arg(ap
, int);
327 ul
= lflag
? va_arg(ap
, u_long
) : va_arg(ap
, u_int
);
331 ul
= lflag
? va_arg(ap
, u_long
) : va_arg(ap
, u_int
);
335 ul
= (u_long
)va_arg(ap
, void *);
340 p
= va_arg(ap
, char *);
346 for (n
= 0; n
< dwidth
&& p
[n
]; n
++)
351 if (!ladjust
&& width
> 0)
356 if (ladjust
&& width
> 0)
361 ul
= lflag
? va_arg(ap
, u_long
) : va_arg(ap
, u_int
);
365 ul
= lflag
? va_arg(ap
, u_long
) : va_arg(ap
, u_int
);
367 number
: if (sign
&& (long)ul
< 0L) {
371 p
= ksprintn(ul
, base
, &tmp
);
372 if (sharpflag
&& ul
!= 0) {
381 if (!ladjust
&& width
&& (width
-= tmp
) > 0)
386 if (sharpflag
&& ul
!= 0) {
389 } else if (base
== 16) {
398 if (ladjust
&& width
&& (width
-= tmp
) > 0)