2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1986, 1988, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 * Copyright (c) 2011 Konstantin Belousov <kib@FreeBSD.org>
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/param.h>
46 #include "rtld_printf.h"
48 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
50 #define PRINT_METHOD_SNPRINTF 1
51 #define PRINT_METHOD_WRITE 2
63 printf_out(struct snprintf_arg *info)
66 if (info->remain == info->buf_total)
68 write(info->fd, info->buf, info->buf_total - info->remain);
69 info->str = info->buf;
70 info->remain = info->buf_total;
74 snprintf_func(int ch, struct snprintf_arg *const info)
77 switch (info->method) {
78 case PRINT_METHOD_SNPRINTF:
79 if (info->remain >= 2) {
84 case PRINT_METHOD_WRITE:
85 if (info->remain > 0) {
94 static char const hex2ascii_lower[] = "0123456789abcdefghijklmnopqrstuvwxyz";
95 static char const hex2ascii_upper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
96 #define hex2ascii(hex) (hex2ascii_lower[hex])
97 #define hex2ascii_upper(hex) (hex2ascii_upper[hex])
103 return (a > b ? a : b);
107 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
114 c = upper ? hex2ascii_upper(num % base) :
115 hex2ascii(num % base);
117 } while (num /= base);
124 kvprintf(char const *fmt, struct snprintf_arg *arg, int radix, va_list ap)
126 #define PCHAR(c) snprintf_func((c), arg)
128 const char *p, *percent, *q;
132 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
133 int cflag, hflag, jflag, tflag, zflag;
136 int stop = 0, retval = 0;
141 fmt = "(fmt null)\n";
143 if (radix < 2 || radix > 36)
149 while ((ch = (u_char)*fmt++) != '%' || stop) {
155 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
156 sign = 0; dot = 0; dwidth = 0; upper = 0;
157 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
158 reswitch: switch (ch = (u_char)*fmt++) {
176 width = va_arg(ap, int);
182 dwidth = va_arg(ap, int);
191 case '1': case '2': case '3': case '4':
192 case '5': case '6': case '7': case '8': case '9':
193 for (n = 0;; ++fmt) {
194 n = n * 10 + ch - '0';
196 if (ch < '0' || ch > '9')
205 num = (u_int)va_arg(ap, int);
206 p = va_arg(ap, char *);
207 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
215 if (num & (1 << (n - 1))) {
216 PCHAR(tmp ? ',' : '<');
217 for (; (n = *p) > ' '; ++p)
221 for (; *p > ' '; ++p)
228 PCHAR(va_arg(ap, int));
231 up = va_arg(ap, u_char *);
232 p = va_arg(ap, char *);
236 PCHAR(hex2ascii(*up >> 4));
237 PCHAR(hex2ascii(*up & 0x0f));
268 *(va_arg(ap, intmax_t *)) = retval;
270 *(va_arg(ap, quad_t *)) = retval;
272 *(va_arg(ap, long *)) = retval;
274 *(va_arg(ap, size_t *)) = retval;
276 *(va_arg(ap, short *)) = retval;
278 *(va_arg(ap, char *)) = retval;
280 *(va_arg(ap, int *)) = retval;
287 sharpflag = (width == 0);
289 num = (uintptr_t)va_arg(ap, void *);
300 p = va_arg(ap, char *);
306 for (n = 0; n < dwidth && p[n]; n++)
311 if (!ladjust && width > 0)
316 if (ladjust && width > 0)
342 num = va_arg(ap, uintmax_t);
344 num = va_arg(ap, u_quad_t);
346 num = va_arg(ap, ptrdiff_t);
348 num = va_arg(ap, u_long);
350 num = va_arg(ap, size_t);
352 num = (u_short)va_arg(ap, int);
354 num = (u_char)va_arg(ap, int);
356 num = va_arg(ap, u_int);
360 num = va_arg(ap, intmax_t);
362 num = va_arg(ap, quad_t);
364 num = va_arg(ap, ptrdiff_t);
366 num = va_arg(ap, long);
368 num = va_arg(ap, ssize_t);
370 num = (short)va_arg(ap, int);
372 num = (char)va_arg(ap, int);
374 num = va_arg(ap, int);
376 if (sign && (intmax_t)num < 0) {
378 num = -(intmax_t)num;
380 p = ksprintn(nbuf, num, base, &n, upper);
382 if (sharpflag && num != 0) {
391 if (!ladjust && padc == '0')
392 dwidth = width - tmp;
393 width -= tmp + imax(dwidth, n);
400 if (sharpflag && num != 0) {
403 } else if (base == 16) {
420 while (percent < fmt)
423 * Since we ignore an formatting argument it is no
424 * longer safe to obey the remaining formatting
425 * arguments as the arguments will no longer match
436 rtld_snprintf(char *buf, size_t bufsize, const char *fmt, ...)
442 retval = rtld_vsnprintf(buf, bufsize, fmt, ap);
448 rtld_vsnprintf(char *buf, size_t bufsize, const char *fmt, va_list ap)
450 struct snprintf_arg info;
453 info.method = PRINT_METHOD_SNPRINTF;
454 info.buf = info.str = buf;
455 info.buf_total = info.remain = bufsize;
457 retval = kvprintf(fmt, &info, 10, ap);
458 if (info.remain >= 1)
464 rtld_vfdprintf(int fd, const char *fmt, va_list ap)
467 struct snprintf_arg info;
470 info.method = PRINT_METHOD_WRITE;
471 info.buf = info.str = buf;
472 info.buf_total = info.remain = sizeof(buf);
474 retval = kvprintf(fmt, &info, 10, ap);
480 rtld_fdprintf(int fd, const char *fmt, ...)
486 retval = rtld_vfdprintf(fd, fmt, ap);
492 rtld_fdputstr(int fd, const char *str)
495 write(fd, str, strlen(str));
499 rtld_fdputchar(int fd, int c)