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/sys/kern/subr_prf.c,v 1.61.2.5 2002/08/31 18:22:08 dwmalone Exp $
40 * $DragonFly: src/sys/kern/subr_prf.c,v 1.21 2008/07/17 23:56:23 dillon Exp $
42 * Modified for use with the AROS AHCI subsystem, 2012/01/03
46 #include <sys/types.h>
50 typedef unsigned char u_char
;
51 typedef unsigned short u_short
;
52 typedef unsigned long u_long
;
54 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
55 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
62 static char *ksprintn (char *nbuf
, uintmax_t num
, int base
, int *lenp
,
64 static void snprintf_func (int ch
, void *arg
);
67 * Scaled down version of sprintf(3).
70 ksprintf(char *buf
, const char *cfmt
, ...)
76 retval
= kvcprintf(cfmt
, NULL
, (void *)buf
, 10, ap
);
83 * Scaled down version of vsprintf(3).
86 kvsprintf(char *buf
, const char *cfmt
, va_list ap
)
90 retval
= kvcprintf(cfmt
, NULL
, (void *)buf
, 10, ap
);
96 * Scaled down version of snprintf(3).
99 ksnprintf(char *str
, size_t size
, const char *format
, ...)
104 va_start(ap
, format
);
105 retval
= kvsnprintf(str
, size
, format
, ap
);
111 * Scaled down version of vsnprintf(3).
114 kvsnprintf(char *str
, size_t size
, const char *format
, va_list ap
)
116 struct snprintf_arg info
;
121 retval
= kvcprintf(format
, snprintf_func
, &info
, 10, ap
);
122 if (info
.remain
>= 1)
128 ksnrprintf(char *str
, size_t size
, int radix
, const char *format
, ...)
133 va_start(ap
, format
);
134 retval
= kvsnrprintf(str
, size
, radix
, format
, ap
);
140 kvsnrprintf(char *str
, size_t size
, int radix
, const char *format
, va_list ap
)
142 struct snprintf_arg info
;
147 retval
= kvcprintf(format
, snprintf_func
, &info
, radix
, ap
);
148 if (info
.remain
>= 1)
154 kvasnrprintf(char **strp
, size_t size
, int radix
,
155 const char *format
, va_list ap
)
157 struct snprintf_arg info
;
160 *strp
= kmalloc(size
, M_TEMP
, M_WAITOK
);
163 retval
= kvcprintf(format
, snprintf_func
, &info
, radix
, ap
);
164 if (info
.remain
>= 1)
170 kvasfree(char **strp
)
173 kfree(*strp
, M_TEMP
);
179 snprintf_func(int ch
, void *arg
)
181 struct snprintf_arg
*const info
= arg
;
183 if (info
->remain
>= 2) {
189 static inline char hex2ascii(int n
)
191 return "0123456789abcdef"[n
& 0xf];
194 static inline int toupper(int c
)
196 if (c
>= 'a' && c
<= 'z')
197 return (c
- 'a') + 'A';
203 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
204 * order; return an optional length and a pointer to the last character
205 * written in the buffer (i.e., the first character of the string).
206 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
209 ksprintn(char *nbuf
, uintmax_t num
, int base
, int *lenp
, int upper
)
216 c
= hex2ascii(num
% base
);
217 *++p
= upper
? toupper(c
) : c
;
218 } while (num
/= base
);
225 * Scaled down version of printf(3).
227 * Two additional formats:
229 * The format %b is supported to decode error registers.
232 * kprintf("reg=%b\n", regval, "<base><arg>*");
234 * where <base> is the output base expressed as a control character, e.g.
235 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
236 * the first of which gives the bit number to be inspected (origin 1), and
237 * the next characters (up to a control character, i.e. a character <= 32),
238 * give the name of the register. Thus:
240 * kvcprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
242 * would produce output:
244 * reg=3<BITTWO,BITONE>
246 * XXX: %D -- Hexdump, takes pointer and separator string:
247 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
248 * ("%*D", len, ptr, " " -> XX XX XX XX ...
251 #define PCHAR(c) {int cc=(c); if(func) (*func)(cc,arg); else *d++=cc; retval++;}
254 kvcprintf(char const *fmt
, void (*func
)(int, void*), void *arg
,
255 int radix
, va_list ap
)
259 const char *p
, *percent
, *q
;
263 int base
, tmp
, width
, ladjust
, sharpflag
, neg
, sign
, dot
;
264 int cflag
, hflag
, jflag
, lflag
, qflag
, tflag
, zflag
;
267 int retval
= 0, stop
= 0;
276 fmt
= "(fmt null)\n";
278 if (radix
< 2 || radix
> 36)
284 while ((ch
= (u_char
)*fmt
++) != '%' || stop
) {
290 dot
= dwidth
= ladjust
= neg
= sharpflag
= sign
= upper
= 0;
291 cflag
= hflag
= jflag
= lflag
= qflag
= tflag
= zflag
= 0;
294 switch (ch
= (u_char
)*fmt
++) {
312 width
= va_arg(ap
, int);
318 dwidth
= va_arg(ap
, int);
326 case '1': case '2': case '3': case '4':
327 case '5': case '6': case '7': case '8': case '9':
328 for (n
= 0;; ++fmt
) {
329 n
= n
* 10 + ch
- '0';
331 if (ch
< '0' || ch
> '9')
340 num
= (u_int
)va_arg(ap
, int);
341 p
= va_arg(ap
, char *);
342 for (q
= ksprintn(nbuf
, num
, *p
++, NULL
, 0); *q
;)
350 if (num
& (1 << (n
- 1))) {
351 PCHAR(tmp
? ',' : '<');
352 for (; (n
= *p
) > ' '; ++p
)
356 for (; *p
> ' '; ++p
)
363 PCHAR(va_arg(ap
, int));
366 up
= va_arg(ap
, u_char
*);
367 p
= va_arg(ap
, char *);
371 PCHAR(hex2ascii(*up
>> 4));
372 PCHAR(hex2ascii(*up
& 0x0f));
403 *(va_arg(ap
, char *)) = retval
;
405 *(va_arg(ap
, short *)) = retval
;
407 *(va_arg(ap
, intmax_t *)) = retval
;
409 *(va_arg(ap
, long *)) = retval
;
411 *(va_arg(ap
, quad_t
*)) = retval
;
413 *(va_arg(ap
, int *)) = retval
;
420 sharpflag
= (width
== 0);
422 num
= (uintptr_t)va_arg(ap
, void *);
433 p
= va_arg(ap
, char *);
439 for (n
= 0; n
< dwidth
&& p
[n
]; n
++)
444 if (!ladjust
&& width
> 0)
449 if (ladjust
&& width
> 0)
471 num
= (u_char
)va_arg(ap
, int);
473 num
= (u_short
)va_arg(ap
, int);
475 num
= va_arg(ap
, uintmax_t);
477 num
= va_arg(ap
, u_long
);
479 num
= va_arg(ap
, u_quad_t
);
481 num
= va_arg(ap
, ptrdiff_t);
483 num
= va_arg(ap
, size_t);
485 num
= va_arg(ap
, u_int
);
489 num
= (char)va_arg(ap
, int);
491 num
= (short)va_arg(ap
, int);
493 num
= va_arg(ap
, intmax_t);
495 num
= va_arg(ap
, long);
497 num
= va_arg(ap
, quad_t
);
499 num
= va_arg(ap
, ptrdiff_t);
501 num
= va_arg(ap
, ssize_t
);
503 num
= va_arg(ap
, int);
505 if (sign
&& (intmax_t)num
< 0) {
507 num
= -(intmax_t)num
;
509 p
= ksprintn(nbuf
, num
, base
, &tmp
, upper
);
510 if (sharpflag
&& num
!= 0) {
519 if (!ladjust
&& padc
!= '0' && width
&&
520 (width
-= tmp
) > 0) {
526 if (sharpflag
&& num
!= 0) {
529 } else if (base
== 16) {
534 if (!ladjust
&& width
&& (width
-= tmp
) > 0)
541 if (ladjust
&& width
&& (width
-= tmp
) > 0)
547 while (percent
< fmt
)
550 * Since we ignore an formatting argument it is no
551 * longer safe to obey the remaining formatting
552 * arguments as the arguments will no longer match