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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
39 typedef unsigned char u_char
;
40 typedef unsigned int u_int
;
41 typedef unsigned long long u_quad_t
;
42 typedef long long quad_t
;
43 typedef unsigned long u_long
;
44 typedef unsigned short u_short
;
47 #define NBBY 8 /* number of bits in a byte */
49 static char const hex2ascii_data
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
50 #define hex2ascii(hex) (hex2ascii_data[hex])
51 #define toupper(c) ((c) - 0x20 * (((c) >= 'a') && ((c) <= 'z')))
53 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
54 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
57 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
58 * order; return an optional length and a pointer to the last character
59 * written in the buffer (i.e., the first character of the string).
60 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
63 ksprintn(char *nbuf
, uintmax_t num
, int base
, int *lenp
, int upper
) {
67 char c
= hex2ascii(num
% base
);
68 *++p
= upper
? toupper(c
) : c
;
69 } while (num
/= base
);
76 * Scaled down version of printf(3).
78 * Two additional formats:
80 * The format %b is supported to decode error registers.
83 * printf("reg=%b\n", regval, "*");
85 * where is the output base expressed as a control character, e.g.
86 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
87 * the first of which gives the bit number to be inspected (origin 1), and
88 * the next characters (up to a control character, i.e. a character <= 32),
89 * give the name of the register. Thus:
91 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
93 * would produce output:
97 * XXX: %D -- Hexdump, takes pointer and separator string:
98 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
99 * ("%*D", len, ptr, " " -> XX XX XX XX ...
102 kvsprintf(char const *fmt
, void *arg
, int radix
, va_list ap
) {
103 #define PCHAR(c) {int cc=(c); *d++ = cc; retval++; }
106 const char *p
, *percent
, *q
;
110 int base
, lflag
, qflag
, tmp
, width
, ladjust
, sharpflag
, neg
, sign
, dot
;
111 int cflag
, hflag
, jflag
, tflag
, zflag
;
114 int stop
= 0, retval
= 0;
120 fmt
= "(fmt null)\n";
122 if (radix
< 2 || radix
> 36)
128 while ((ch
= (u_char
) * fmt
++) != '%' || stop
) {
149 switch (ch
= (u_char
) * fmt
++) {
167 width
= va_arg(ap
, int);
173 dwidth
= va_arg(ap
, int);
190 for (n
= 0;; ++fmt
) {
191 n
= n
* 10 + ch
- '0';
193 if (ch
< '0' || ch
> '9')
202 num
= (u_int
)va_arg(ap
, int);
203 p
= va_arg(ap
, char *);
204 for (q
= ksprintn(nbuf
, num
, *p
++, NULL
, 0); *q
;)
212 if (num
& (1 << (n
- 1))) {
213 PCHAR(tmp
? ',' : '<');
214 for (; (n
= *p
) > ' '; ++p
)
218 for (; *p
> ' '; ++p
)
225 PCHAR(va_arg(ap
, int));
228 up
= va_arg(ap
, u_char
*);
229 p
= va_arg(ap
, char *);
233 PCHAR(hex2ascii(*up
>> 4));
234 PCHAR(hex2ascii(*up
& 0x0f));
265 *(va_arg(ap
, intmax_t *)) = retval
;
267 *(va_arg(ap
, quad_t
*)) = retval
;
269 *(va_arg(ap
, long *)) = retval
;
271 *(va_arg(ap
, size_t *)) = retval
;
273 *(va_arg(ap
, short *)) = retval
;
275 *(va_arg(ap
, char *)) = retval
;
277 *(va_arg(ap
, int *)) = retval
;
284 sharpflag
= (width
== 0);
286 num
= (uintptr_t)va_arg(ap
, void *);
297 p
= va_arg(ap
, char *);
303 for (n
= 0; n
< dwidth
&& p
[n
]; n
++)
308 if (!ladjust
&& width
> 0)
313 if (ladjust
&& width
> 0)
338 num
= va_arg(ap
, uintmax_t);
340 num
= va_arg(ap
, u_quad_t
);
342 num
= va_arg(ap
, ptrdiff_t);
344 num
= va_arg(ap
, u_long
);
346 num
= va_arg(ap
, size_t);
348 num
= (u_short
)va_arg(ap
, int);
350 num
= (u_char
)va_arg(ap
, int);
352 num
= va_arg(ap
, u_int
);
356 num
= va_arg(ap
, intmax_t);
358 num
= va_arg(ap
, quad_t
);
360 num
= va_arg(ap
, ptrdiff_t);
362 num
= va_arg(ap
, long);
364 num
= va_arg(ap
, ssize_t
);
366 num
= (short)va_arg(ap
, int);
368 num
= (char)va_arg(ap
, int);
370 num
= va_arg(ap
, int);
372 if (sign
&& (intmax_t)num
< 0) {
374 num
= -(intmax_t)num
;
376 p
= ksprintn(nbuf
, num
, base
, &tmp
, upper
);
377 if (sharpflag
&& num
!= 0) {
386 if (!ladjust
&& padc
!= '0' && width
387 && (width
-= tmp
) > 0)
392 if (sharpflag
&& num
!= 0) {
395 } else if (base
== 16) {
400 if (!ladjust
&& width
&& (width
-= tmp
) > 0)
407 if (ladjust
&& width
&& (width
-= tmp
) > 0)
413 while (percent
< fmt
)
416 * Since we ignore an formatting argument it is no
417 * longer safe to obey the remaining formatting
418 * arguments as the arguments will no longer match
430 int vsprintf(char *dest
, const char *fmt
, va_list ap
) {
431 return kvsprintf(fmt
, dest
, 10, ap
);
435 sprintf(char *dest
, const char *fmt
, ...) {
436 /* http://www.pagetable.com/?p=298 */
440 retval
= kvsprintf(fmt
, dest
, 10, ap
);