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"
47 #include "rtld_libc.h"
49 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
51 #define PRINT_METHOD_SNPRINTF 1
52 #define PRINT_METHOD_WRITE 2
64 printf_out(struct snprintf_arg *info)
67 if (info->remain == info->buf_total)
69 write(info->fd, info->buf, info->buf_total - info->remain);
70 info->str = info->buf;
71 info->remain = info->buf_total;
75 snprintf_func(int ch, struct snprintf_arg *const info)
78 switch (info->method) {
79 case PRINT_METHOD_SNPRINTF:
80 if (info->remain >= 2) {
85 case PRINT_METHOD_WRITE:
86 if (info->remain > 0) {
95 static char const hex2ascii_lower[] = "0123456789abcdefghijklmnopqrstuvwxyz";
96 static char const hex2ascii_upper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
97 #define hex2ascii(hex) (hex2ascii_lower[hex])
98 #define hex2ascii_upper(hex) (hex2ascii_upper[hex])
104 return (a > b ? a : b);
108 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
115 c = upper ? hex2ascii_upper(num % base) :
116 hex2ascii(num % base);
118 } while (num /= base);
125 kvprintf(char const *fmt, struct snprintf_arg *arg, int radix, va_list ap)
127 #define PCHAR(c) snprintf_func((c), arg)
129 const char *p, *percent, *q;
133 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
134 int cflag, hflag, jflag, tflag, zflag;
137 int stop = 0, retval = 0;
142 fmt = "(fmt null)\n";
144 if (radix < 2 || radix > 36)
150 while ((ch = (u_char)*fmt++) != '%' || stop) {
156 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
157 sign = 0; dot = 0; dwidth = 0; upper = 0;
158 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
159 reswitch: switch (ch = (u_char)*fmt++) {
177 width = va_arg(ap, int);
183 dwidth = va_arg(ap, int);
192 case '1': case '2': case '3': case '4':
193 case '5': case '6': case '7': case '8': case '9':
194 for (n = 0;; ++fmt) {
195 n = n * 10 + ch - '0';
197 if (ch < '0' || ch > '9')
206 num = (u_int)va_arg(ap, int);
207 p = va_arg(ap, char *);
208 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
216 if (num & (1 << (n - 1))) {
217 PCHAR(tmp ? ',' : '<');
218 for (; (n = *p) > ' '; ++p)
222 for (; *p > ' '; ++p)
229 PCHAR(va_arg(ap, int));
232 up = va_arg(ap, u_char *);
233 p = va_arg(ap, char *);
237 PCHAR(hex2ascii(*up >> 4));
238 PCHAR(hex2ascii(*up & 0x0f));
269 *(va_arg(ap, intmax_t *)) = retval;
271 *(va_arg(ap, quad_t *)) = retval;
273 *(va_arg(ap, long *)) = retval;
275 *(va_arg(ap, size_t *)) = retval;
277 *(va_arg(ap, short *)) = retval;
279 *(va_arg(ap, char *)) = retval;
281 *(va_arg(ap, int *)) = retval;
288 sharpflag = (width == 0);
290 num = (uintptr_t)va_arg(ap, void *);
301 p = va_arg(ap, char *);
307 for (n = 0; n < dwidth && p[n]; n++)
312 if (!ladjust && width > 0)
317 if (ladjust && width > 0)
343 num = va_arg(ap, uintmax_t);
345 num = va_arg(ap, u_quad_t);
347 num = va_arg(ap, ptrdiff_t);
349 num = va_arg(ap, u_long);
351 num = va_arg(ap, size_t);
353 num = (u_short)va_arg(ap, int);
355 num = (u_char)va_arg(ap, int);
357 num = va_arg(ap, u_int);
361 num = va_arg(ap, intmax_t);
363 num = va_arg(ap, quad_t);
365 num = va_arg(ap, ptrdiff_t);
367 num = va_arg(ap, long);
369 num = va_arg(ap, ssize_t);
371 num = (short)va_arg(ap, int);
373 num = (char)va_arg(ap, int);
375 num = va_arg(ap, int);
377 if (sign && (intmax_t)num < 0) {
379 num = -(intmax_t)num;
381 p = ksprintn(nbuf, num, base, &n, upper);
383 if (sharpflag && num != 0) {
392 if (!ladjust && padc == '0')
393 dwidth = width - tmp;
394 width -= tmp + imax(dwidth, n);
401 if (sharpflag && num != 0) {
404 } else if (base == 16) {
421 while (percent < fmt)
424 * Since we ignore an formatting argument it is no
425 * longer safe to obey the remaining formatting
426 * arguments as the arguments will no longer match
437 rtld_snprintf(char *buf, size_t bufsize, const char *fmt, ...)
443 retval = rtld_vsnprintf(buf, bufsize, fmt, ap);
449 rtld_vsnprintf(char *buf, size_t bufsize, const char *fmt, va_list ap)
451 struct snprintf_arg info;
454 info.method = PRINT_METHOD_SNPRINTF;
455 info.buf = info.str = buf;
456 info.buf_total = info.remain = bufsize;
458 retval = kvprintf(fmt, &info, 10, ap);
459 if (info.remain >= 1)
465 rtld_vfdprintf(int fd, const char *fmt, va_list ap)
468 struct snprintf_arg info;
471 info.method = PRINT_METHOD_WRITE;
472 info.buf = info.str = buf;
473 info.buf_total = info.remain = sizeof(buf);
475 retval = kvprintf(fmt, &info, 10, ap);
481 rtld_fdprintf(int fd, const char *fmt, ...)
487 retval = rtld_vfdprintf(fd, fmt, ap);
493 rtld_fdputstr(int fd, const char *str)
496 write(fd, str, strlen(str));
500 rtld_fdputchar(int fd, int c)