199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1986, 1988, 1991, 1993
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The Regents of the University of California. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (c) UNIX System Laboratories, Inc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All or some portions of this file are derived from material licensed
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to the University of California by American Telephone and Telegraph
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Co. or Unix System Laboratories, Inc. and are reproduced herein with
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the permission of UNIX System Laboratories, Inc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 4. Neither the name of the University nor the names of its contributors
199767f8919635c4928607450d9e0abb932109ceToomas Soome * may be used to endorse or promote products derived from this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Standaloneified version of the FreeBSD kernel printf family.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note that stdarg.h and the ANSI style va_start macro is used for both
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANSI and traditional C compilers.
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MAXNBUF (sizeof(intmax_t) * CHAR_BIT + 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *ksprintn (char *buf, uintmax_t num, int base, int *len, int upper);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int kvprintf(char const *fmt, kvprintf_fn_t *func, void *arg, int radix, va_list ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome retval = kvprintf(fmt, putchar_wrapper, NULL, 10, ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Reserve last buffer position for the terminating
199767f8919635c4928607450d9e0abb932109ceToomas Soome * character:
199767f8919635c4928607450d9e0abb932109ceToomas Soomesnprintf(char *buf, size_t size, const char *cfmt, ...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soomevsprintf(char *buf, const char *cfmt, va_list ap)
199767f8919635c4928607450d9e0abb932109ceToomas Soome retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soomevsnprintf(char *buf, size_t size, const char *cfmt, va_list ap)
199767f8919635c4928607450d9e0abb932109ceToomas Soome retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
199767f8919635c4928607450d9e0abb932109ceToomas Soome * order; return an optional length and a pointer to the last character
199767f8919635c4928607450d9e0abb932109ceToomas Soome * written in the buffer (i.e., the first character of the string).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *
199767f8919635c4928607450d9e0abb932109ceToomas Soomeksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Scaled down version of printf(3).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Two additional formats:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The format %b is supported to decode error registers.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Its usage is:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * printf("reg=%b\n", regval, "<base><arg>*");
199767f8919635c4928607450d9e0abb932109ceToomas Soome * where <base> is the output base expressed as a control character, e.g.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the first of which gives the bit number to be inspected (origin 1), and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the next characters (up to a control character, i.e. a character <= 32),
199767f8919635c4928607450d9e0abb932109ceToomas Soome * give the name of the register. Thus:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE");
199767f8919635c4928607450d9e0abb932109ceToomas Soome * would produce output:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * reg=3<BITTWO,BITONE>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * XXX: %D -- Hexdump, takes pointer and separator string:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ("%*D", len, ptr, " " -> XX XX XX XX ...
199767f8919635c4928607450d9e0abb932109ceToomas Soomekvprintf(char const *fmt, kvprintf_fn_t *func, void *arg, int radix, va_list ap)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define PCHAR(c) {int cc=(c); if (func) (*func)(cc, arg); else *d++ = cc; retval++; }
199767f8919635c4928607450d9e0abb932109ceToomas Soome int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
199767f8919635c4928607450d9e0abb932109ceToomas Soome d = (char *) arg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '5': case '6': case '7': case '8': case '9':
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0;; ++fmt) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (tmp = 0; *p;) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (; (n = *p) > ' '; ++p)
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (; *p > ' '; ++p)
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (q=p;*q;q++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = "(null)";
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n < dwidth && p[n]; n++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (width-- > 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (dwidth-- > 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (width-- > 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Since we ignore a formatting argument it is no
199767f8919635c4928607450d9e0abb932109ceToomas Soome * longer safe to obey the remaining formatting
199767f8919635c4928607450d9e0abb932109ceToomas Soome * arguments as the arguments will no longer match
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the format specs.