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.
9 * Copyright (c) 2011 Konstantin Belousov <kib@FreeBSD.org>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/param.h>
45 #include "rtld_printf.h"
47 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
49 #define PRINT_METHOD_SNPRINTF 1
50 #define PRINT_METHOD_WRITE 2
62 printf_out(struct snprintf_arg *info)
65 if (info->remain == info->buf_total)
67 write(info->fd, info->buf, info->buf_total - info->remain);
68 info->str = info->buf;
69 info->remain = info->buf_total;
73 snprintf_func(int ch, struct snprintf_arg *const info)
76 switch (info->method) {
77 case PRINT_METHOD_SNPRINTF:
78 if (info->remain >= 2) {
83 case PRINT_METHOD_WRITE:
84 if (info->remain > 0) {
93 static char const hex2ascii_data[] = "0123456789abcdefghijklmnopqrstuvwxyz";
94 #define hex2ascii(hex) (hex2ascii_data[hex])
100 return (a > b ? a : b);
104 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
111 c = hex2ascii(num % base);
112 *++p = upper ? toupper(c) : c;
113 } while (num /= base);
120 kvprintf(char const *fmt, struct snprintf_arg *arg, int radix, va_list ap)
122 #define PCHAR(c) snprintf_func((c), arg)
124 const char *p, *percent, *q;
128 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
129 int cflag, hflag, jflag, tflag, zflag;
132 int stop = 0, retval = 0;
137 fmt = "(fmt null)\n";
139 if (radix < 2 || radix > 36)
145 while ((ch = (u_char)*fmt++) != '%' || stop) {
151 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
152 sign = 0; dot = 0; dwidth = 0; upper = 0;
153 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
154 reswitch: switch (ch = (u_char)*fmt++) {
172 width = va_arg(ap, int);
178 dwidth = va_arg(ap, int);
186 case '1': case '2': case '3': case '4':
187 case '5': case '6': case '7': case '8': case '9':
188 for (n = 0;; ++fmt) {
189 n = n * 10 + ch - '0';
191 if (ch < '0' || ch > '9')
200 num = (u_int)va_arg(ap, int);
201 p = va_arg(ap, char *);
202 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
210 if (num & (1 << (n - 1))) {
211 PCHAR(tmp ? ',' : '<');
212 for (; (n = *p) > ' '; ++p)
216 for (; *p > ' '; ++p)
223 PCHAR(va_arg(ap, int));
226 up = va_arg(ap, u_char *);
227 p = va_arg(ap, char *);
231 PCHAR(hex2ascii(*up >> 4));
232 PCHAR(hex2ascii(*up & 0x0f));
263 *(va_arg(ap, intmax_t *)) = retval;
265 *(va_arg(ap, quad_t *)) = retval;
267 *(va_arg(ap, long *)) = retval;
269 *(va_arg(ap, size_t *)) = retval;
271 *(va_arg(ap, short *)) = retval;
273 *(va_arg(ap, char *)) = retval;
275 *(va_arg(ap, int *)) = retval;
282 sharpflag = (width == 0);
284 num = (uintptr_t)va_arg(ap, void *);
295 p = va_arg(ap, char *);
301 for (n = 0; n < dwidth && p[n]; n++)
306 if (!ladjust && width > 0)
311 if (ladjust && width > 0)
336 num = va_arg(ap, uintmax_t);
338 num = va_arg(ap, u_quad_t);
340 num = va_arg(ap, ptrdiff_t);
342 num = va_arg(ap, u_long);
344 num = va_arg(ap, size_t);
346 num = (u_short)va_arg(ap, int);
348 num = (u_char)va_arg(ap, int);
350 num = va_arg(ap, u_int);
354 num = va_arg(ap, intmax_t);
356 num = va_arg(ap, quad_t);
358 num = va_arg(ap, ptrdiff_t);
360 num = va_arg(ap, long);
362 num = va_arg(ap, ssize_t);
364 num = (short)va_arg(ap, int);
366 num = (char)va_arg(ap, int);
368 num = va_arg(ap, int);
370 if (sign && (intmax_t)num < 0) {
372 num = -(intmax_t)num;
374 p = ksprintn(nbuf, num, base, &n, upper);
376 if (sharpflag && num != 0) {
385 if (!ladjust && padc == '0')
386 dwidth = width - tmp;
387 width -= tmp + imax(dwidth, n);
394 if (sharpflag && num != 0) {
397 } else if (base == 16) {
414 while (percent < fmt)
417 * Since we ignore an formatting argument it is no
418 * longer safe to obey the remaining formatting
419 * arguments as the arguments will no longer match
430 rtld_vsnprintf(char *buf, size_t bufsize, const char *fmt, va_list ap)
432 struct snprintf_arg info;
435 info.method = PRINT_METHOD_SNPRINTF;
436 info.buf = info.str = buf;
437 info.buf_total = info.remain = bufsize;
439 retval = kvprintf(fmt, &info, 10, ap);
440 if (info.remain >= 1)
446 rtld_vfdprintf(int fd, const char *fmt, va_list ap)
449 struct snprintf_arg info;
452 info.method = PRINT_METHOD_WRITE;
453 info.buf = info.str = buf;
454 info.buf_total = info.remain = sizeof(buf);
456 retval = kvprintf(fmt, &info, 10, ap);
462 rtld_fdprintf(int fd, const char *fmt, ...)
468 retval = rtld_vfdprintf(fd, fmt, ap);
474 rtld_fdputstr(int fd, const char *str)
477 write(fd, str, strlen(str));
481 rtld_fdputchar(int fd, int c)