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. 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
35 * $FreeBSD: src/lib/libstand/printf.c,v 1.14 2010/07/12 15:32:45 jkim Exp $
39 * Standaloneified version of the FreeBSD kernel printf family.
42 #include <sys/types.h>
43 #include <sys/stdint.h>
50 * Note that stdarg.h and the ANSI style va_start macro is used for both
51 * ANSI and traditional C compilers.
60 #define MAXNBUF (sizeof(intmax_t) * CHAR_BIT + 1)
62 static char *ksprintn (char *buf, uintmax_t num, int base, int *len, int upper);
63 static int kvprintf(const char *, void (*)(int, void *), void *, va_list);
64 static void putchar_wrapper(int, void *);
65 static void snprintf_func(int, void *);
68 putchar_wrapper(int ch, void *arg __unused)
74 printf(const char *fmt, ...)
80 retval = kvprintf(fmt, putchar_wrapper, NULL, ap);
86 vprintf(const char *fmt, va_list ap)
89 kvprintf(fmt, putchar_wrapper, NULL, ap);
93 sprintf(char *buf, const char *cfmt, ...)
99 retval = kvprintf(cfmt, NULL, buf, ap);
106 vsprintf(char *buf, const char *cfmt, va_list ap)
110 retval = kvprintf(cfmt, NULL, buf, ap);
115 snprintf(char *buf, size_t size, const char *cfmt, ...)
121 retval = vsnprintf(buf, size, cfmt, ap);
127 vsnprintf(char *buf, size_t size, const char *cfmt, va_list ap)
129 struct snprintf_arg info;
134 retval = kvprintf(cfmt, snprintf_func, &info, ap);
135 if (info.remain >= 1)
141 snprintf_func(int ch, void *arg)
143 struct snprintf_arg * const info = arg;
145 if (info->remain >= 2) {
152 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
153 * order; return an optional length and a pointer to the last character
154 * written in the buffer (i.e., the first character of the string).
155 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
158 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
165 c = hex2ascii(num % base);
166 *++p = upper ? toupper(c) : c;
167 } while (num /= base);
174 * Scaled down version of printf(3).
177 kvprintf(char const *fmt, void (*func)(int, void *), void *arg, va_list ap)
179 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc, arg); else *d++ = cc; retval++; }
182 const char *p, *percent;
185 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
186 int cflag, hflag, jflag, tflag, zflag;
189 int stop = 0, retval = 0;
198 fmt = "(fmt null)\n";
203 while ((ch = (u_char)*fmt++) != '%' || stop) {
209 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
210 sign = 0; dot = 0; dwidth = 0; upper = 0;
211 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
213 reswitch: switch (ch = (u_char)*fmt++) {
231 width = va_arg(ap, int);
237 dwidth = va_arg(ap, int);
245 case '1': case '2': case '3': case '4':
246 case '5': case '6': case '7': case '8': case '9':
247 for (n = 0;; ++fmt) {
248 n = n * 10 + ch - '0';
250 if (ch < '0' || ch > '9')
259 PCHAR(va_arg(ap, int));
285 *(va_arg(ap, intmax_t *)) = retval;
287 *(va_arg(ap, quad_t *)) = retval;
289 *(va_arg(ap, long *)) = retval;
291 *(va_arg(ap, size_t *)) = retval;
293 *(va_arg(ap, short *)) = retval;
295 *(va_arg(ap, char *)) = retval;
297 *(va_arg(ap, int *)) = retval;
304 sharpflag = (width == 0);
306 num = (uintptr_t)va_arg(ap, void *);
312 p = va_arg(ap, char *);
318 for (n = 0; n < dwidth && p[n]; n++)
323 if (!ladjust && width > 0)
328 if (ladjust && width > 0)
349 num = va_arg(ap, uintmax_t);
351 num = va_arg(ap, u_quad_t);
353 num = va_arg(ap, ptrdiff_t);
355 num = va_arg(ap, u_long);
357 num = va_arg(ap, size_t);
359 num = (u_short)va_arg(ap, int);
361 num = (u_char)va_arg(ap, int);
363 num = va_arg(ap, u_int);
367 num = va_arg(ap, intmax_t);
369 num = va_arg(ap, quad_t);
371 num = va_arg(ap, ptrdiff_t);
373 num = va_arg(ap, long);
375 num = va_arg(ap, ssize_t);
377 num = (short)va_arg(ap, int);
379 num = (char)va_arg(ap, int);
381 num = va_arg(ap, int);
383 if (sign && (intmax_t)num < 0) {
385 num = -(intmax_t)num;
387 p = ksprintn(nbuf, num, base, &n, upper);
389 if (sharpflag && num != 0) {
398 if (!ladjust && padc == '0')
399 dwidth = width - tmp;
400 width -= tmp + imax(dwidth, n);
407 if (sharpflag && num != 0) {
410 } else if (base == 16) {
427 while (percent < fmt)
430 * Since we ignore an formatting argument it is no
431 * longer safe to obey the remaining formatting
432 * arguments as the arguments will no longer match