199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
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 *
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 * are met:
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 *
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Standaloneified version of the FreeBSD kernel printf family.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/types.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/stddef.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/stdint.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <limits.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "stand.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/stdarg.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MAXNBUF (sizeof(intmax_t) * CHAR_BIT + 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef void (kvprintf_fn_t)(int, void *);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomeputchar_wrapper(int cc, void *arg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome putchar(cc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomeprintf(const char *fmt, ...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_list ap;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_start(ap, fmt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome retval = kvprintf(fmt, putchar_wrapper, NULL, 10, ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_end(ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomevprintf(const char *fmt, va_list ap)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome kvprintf(fmt, putchar_wrapper, NULL, 10, ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomesprintf(char *buf, const char *cfmt, ...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_list ap;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_start(ap, cfmt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[retval] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_end(ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct print_buf {
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomesnprint_func(int ch, void *arg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct print_buf *pbuf = arg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pbuf->size < 2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Reserve last buffer position for the terminating
199767f8919635c4928607450d9e0abb932109ceToomas Soome * character:
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(pbuf->buf)++ = ch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pbuf->size--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomesnprintf(char *buf, size_t size, const char *cfmt, ...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_list ap;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct print_buf arg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome arg.buf = buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome arg.size = size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_start(ap, cfmt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_end(ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (arg.size >= 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(arg.buf)++ = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomevsprintf(char *buf, const char *cfmt, va_list ap)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[retval] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomevsnprintf(char *buf, size_t size, const char *cfmt, va_list ap)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct print_buf arg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome arg.buf = buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome arg.size = size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[retval] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *
199767f8919635c4928607450d9e0abb932109ceToomas Soomeksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *p, c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = nbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *p = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = hex2ascii(num % base);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *++p = upper ? toupper(c) : c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (num /= base);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (lenp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *lenp = p - nbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (p);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Scaled down version of printf(3).
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Two additional formats:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The format %b is supported to decode error registers.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Its usage is:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * printf("reg=%b\n", regval, "<base><arg>*");
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE");
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * would produce output:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * reg=3<BITTWO,BITONE>
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomekvprintf(char const *fmt, kvprintf_fn_t *func, void *arg, int radix, va_list ap)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define PCHAR(c) {int cc=(c); if (func) (*func)(cc, arg); else *d++ = cc; retval++; }
199767f8919635c4928607450d9e0abb932109ceToomas Soome char nbuf[MAXNBUF];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *p, *percent, *q;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char *up;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int ch, n;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uintmax_t num;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int cflag, hflag, jflag, tflag, zflag;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int dwidth, upper;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char padc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int stop = 0, retval = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!func)
199767f8919635c4928607450d9e0abb932109ceToomas Soome d = (char *) arg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome d = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fmt == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome fmt = "(fmt null)\n";
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (radix < 2 || radix > 36)
199767f8919635c4928607450d9e0abb932109ceToomas Soome radix = 10;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (;;) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome padc = ' ';
199767f8919635c4928607450d9e0abb932109ceToomas Soome width = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((ch = (u_char)*fmt++) != '%' || stop) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ch == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (retval);
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(ch);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome percent = fmt - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sign = 0; dot = 0; dwidth = 0; upper = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soomereswitch: switch (ch = (u_char)*fmt++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '.':
199767f8919635c4928607450d9e0abb932109ceToomas Soome dot = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto reswitch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '#':
199767f8919635c4928607450d9e0abb932109ceToomas Soome sharpflag = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto reswitch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '+':
199767f8919635c4928607450d9e0abb932109ceToomas Soome sign = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto reswitch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '-':
199767f8919635c4928607450d9e0abb932109ceToomas Soome ladjust = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto reswitch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '%':
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(ch);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '*':
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!dot) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome width = va_arg(ap, int);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (width < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ladjust = !ladjust;
199767f8919635c4928607450d9e0abb932109ceToomas Soome width = -width;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome dwidth = va_arg(ap, int);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto reswitch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '0':
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!dot) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome padc = '0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto reswitch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '1': case '2': case '3': case '4':
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '5': case '6': case '7': case '8': case '9':
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0;; ++fmt) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n = n * 10 + ch - '0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome ch = *fmt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ch < '0' || ch > '9')
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dot)
199767f8919635c4928607450d9e0abb932109ceToomas Soome dwidth = n;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome width = n;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto reswitch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'b':
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = (u_int)va_arg(ap, int);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = va_arg(ap, char *);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(*q--);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (num == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (tmp = 0; *p;) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n = *p++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (num & (1 << (n - 1))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(tmp ? ',' : '<');
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (; (n = *p) > ' '; ++p)
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(n);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (; *p > ' '; ++p)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tmp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR('>');
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'c':
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(va_arg(ap, int));
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'D':
199767f8919635c4928607450d9e0abb932109ceToomas Soome up = va_arg(ap, u_char *);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = va_arg(ap, char *);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!width)
199767f8919635c4928607450d9e0abb932109ceToomas Soome width = 16;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while(width--) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(hex2ascii(*up >> 4));
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(hex2ascii(*up & 0x0f));
199767f8919635c4928607450d9e0abb932109ceToomas Soome up++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (width)
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (q=p;*q;q++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(*q);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'd':
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'i':
199767f8919635c4928607450d9e0abb932109ceToomas Soome base = 10;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sign = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto handle_sign;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'h':
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (hflag) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome hflag = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome cflag = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome hflag = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto reswitch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'j':
199767f8919635c4928607450d9e0abb932109ceToomas Soome jflag = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto reswitch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'l':
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (lflag) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome lflag = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome qflag = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome lflag = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto reswitch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'n':
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (jflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(va_arg(ap, intmax_t *)) = retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (qflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(va_arg(ap, quad_t *)) = retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (lflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(va_arg(ap, long *)) = retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (zflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(va_arg(ap, size_t *)) = retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (hflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(va_arg(ap, short *)) = retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (cflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(va_arg(ap, char *)) = retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(va_arg(ap, int *)) = retval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'o':
199767f8919635c4928607450d9e0abb932109ceToomas Soome base = 8;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto handle_nosign;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'p':
199767f8919635c4928607450d9e0abb932109ceToomas Soome base = 16;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sharpflag = (width == 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sign = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = (uintptr_t)va_arg(ap, void *);
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto number;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'q':
199767f8919635c4928607450d9e0abb932109ceToomas Soome qflag = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto reswitch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'r':
199767f8919635c4928607450d9e0abb932109ceToomas Soome base = radix;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sign)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto handle_sign;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto handle_nosign;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 's':
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = va_arg(ap, char *);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (p == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = "(null)";
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!dot)
199767f8919635c4928607450d9e0abb932109ceToomas Soome n = strlen (p);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n < dwidth && p[n]; n++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome width -= n;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!ladjust && width > 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (width--)
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(padc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (n--)
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(*p++);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ladjust && width > 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (width--)
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(padc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 't':
199767f8919635c4928607450d9e0abb932109ceToomas Soome tflag = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto reswitch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'u':
199767f8919635c4928607450d9e0abb932109ceToomas Soome base = 10;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto handle_nosign;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'X':
199767f8919635c4928607450d9e0abb932109ceToomas Soome upper = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'x':
199767f8919635c4928607450d9e0abb932109ceToomas Soome base = 16;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto handle_nosign;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'y':
199767f8919635c4928607450d9e0abb932109ceToomas Soome base = 16;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sign = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto handle_sign;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'z':
199767f8919635c4928607450d9e0abb932109ceToomas Soome zflag = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto reswitch;
199767f8919635c4928607450d9e0abb932109ceToomas Soomehandle_nosign:
199767f8919635c4928607450d9e0abb932109ceToomas Soome sign = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (jflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = va_arg(ap, uintmax_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (qflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = va_arg(ap, u_quad_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (tflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = va_arg(ap, ptrdiff_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (lflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = va_arg(ap, u_long);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (zflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = va_arg(ap, size_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (hflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = (u_short)va_arg(ap, int);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (cflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = (u_char)va_arg(ap, int);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = va_arg(ap, u_int);
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto number;
199767f8919635c4928607450d9e0abb932109ceToomas Soomehandle_sign:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (jflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = va_arg(ap, intmax_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (qflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = va_arg(ap, quad_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (tflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = va_arg(ap, ptrdiff_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (lflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = va_arg(ap, long);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (zflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = va_arg(ap, ssize_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (hflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = (short)va_arg(ap, int);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (cflag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = (char)va_arg(ap, int);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = va_arg(ap, int);
199767f8919635c4928607450d9e0abb932109ceToomas Soomenumber:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sign && (intmax_t)num < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome neg = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome num = -(intmax_t)num;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = ksprintn(nbuf, num, base, &n, upper);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sharpflag && num != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (base == 8)
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (base == 16)
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp += 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (neg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!ladjust && padc == '0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome dwidth = width - tmp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome width -= tmp + imax(dwidth, n);
199767f8919635c4928607450d9e0abb932109ceToomas Soome dwidth -= n;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!ladjust)
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (width-- > 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(' ');
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (neg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR('-');
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sharpflag && num != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (base == 8) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR('0');
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (base == 16) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR('0');
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR('x');
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (dwidth-- > 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR('0');
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*p)
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(*p--);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ladjust)
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (width-- > 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(' ');
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (percent < fmt)
199767f8919635c4928607450d9e0abb932109ceToomas Soome PCHAR(*percent++);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome stop = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#undef PCHAR
199767f8919635c4928607450d9e0abb932109ceToomas Soome}