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
38 #include <sys/param.h>
44 #include "rtld_printf.h"
45 #include "rtld_libc.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)
92 static char const hex2ascii_lower
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
93 static char const hex2ascii_upper
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
94 #define hex2ascii(hex) (hex2ascii_lower[hex])
95 #define hex2ascii_upper(hex) (hex2ascii_upper[hex])
101 return (a
> b
? a
: b
);
105 ksprintn(char *nbuf
, uintmax_t num
, int base
, int *lenp
, int upper
)
112 c
= upper
? hex2ascii_upper(num
% base
) :
113 hex2ascii(num
% base
);
115 } while (num
/= base
);
122 kvprintf(char const *fmt
, struct snprintf_arg
*arg
, int radix
, va_list ap
)
124 #define PCHAR(c) snprintf_func((c), arg)
126 const char *p
, *percent
, *q
;
130 int base
, lflag
, qflag
, tmp
, width
, ladjust
, sharpflag
, dot
;
131 int cflag
, hflag
, jflag
, tflag
, zflag
;
134 int stop
= 0, retval
= 0;
139 fmt
= "(fmt null)\n";
141 if (radix
< 2 || radix
> 36)
147 while ((ch
= (u_char
)*fmt
++) != '%' || stop
) {
153 qflag
= 0; lflag
= 0; ladjust
= 0; sharpflag
= 0;
154 sign
= 0; dot
= 0; dwidth
= 0; upper
= 0;
155 cflag
= 0; hflag
= 0; jflag
= 0; tflag
= 0; zflag
= 0;
156 reswitch
: switch (ch
= (u_char
)*fmt
++) {
174 width
= va_arg(ap
, int);
180 dwidth
= va_arg(ap
, int);
189 case '1': case '2': case '3': case '4':
190 case '5': case '6': case '7': case '8': case '9':
191 for (n
= 0;; ++fmt
) {
192 n
= n
* 10 + ch
- '0';
194 if (ch
< '0' || ch
> '9')
203 num
= (u_int
)va_arg(ap
, int);
204 p
= va_arg(ap
, char *);
205 for (q
= ksprintn(nbuf
, num
, *p
++, NULL
, 0); *q
;)
213 if (num
& (1 << (n
- 1))) {
214 PCHAR(tmp
? ',' : '<');
215 for (; (n
= *p
) > ' '; ++p
)
219 for (; *p
> ' '; ++p
)
226 PCHAR(va_arg(ap
, int));
229 up
= va_arg(ap
, u_char
*);
230 p
= va_arg(ap
, char *);
234 PCHAR(hex2ascii(*up
>> 4));
235 PCHAR(hex2ascii(*up
& 0x0f));
265 *(va_arg(ap
, intmax_t *)) = retval
;
267 *(va_arg(ap
, quad_t
*)) = retval
;
269 *(va_arg(ap
, long *)) = retval
;
271 *(va_arg(ap
, size_t *)) = retval
;
273 *(va_arg(ap
, short *)) = retval
;
275 *(va_arg(ap
, char *)) = retval
;
277 *(va_arg(ap
, int *)) = retval
;
284 sharpflag
= (width
== 0);
286 num
= (uintptr_t)va_arg(ap
, void *);
299 p
= va_arg(ap
, char *);
305 for (n
= 0; n
< dwidth
&& p
[n
]; n
++)
310 if (!ladjust
&& width
> 0)
315 if (ladjust
&& width
> 0)
339 num
= va_arg(ap
, uintmax_t);
341 num
= va_arg(ap
, u_quad_t
);
343 num
= va_arg(ap
, ptrdiff_t);
345 num
= va_arg(ap
, u_long
);
347 num
= va_arg(ap
, size_t);
349 num
= (u_short
)va_arg(ap
, int);
351 num
= (u_char
)va_arg(ap
, int);
353 num
= va_arg(ap
, u_int
);
357 num
= va_arg(ap
, intmax_t);
359 num
= va_arg(ap
, quad_t
);
361 num
= va_arg(ap
, ptrdiff_t);
363 num
= va_arg(ap
, long);
365 num
= va_arg(ap
, ssize_t
);
367 num
= (short)va_arg(ap
, int);
369 num
= (signed char)va_arg(ap
, int);
371 num
= va_arg(ap
, int);
372 if ((intmax_t)num
< 0) {
374 num
= -(intmax_t)num
;
377 p
= ksprintn(nbuf
, num
, base
, &n
, upper
);
379 if (sharpflag
&& num
!= 0) {
388 if (!ladjust
&& padc
== '0')
389 dwidth
= width
- tmp
;
390 width
-= tmp
+ imax(dwidth
, n
);
397 if (sharpflag
&& num
!= 0) {
400 } else if (base
== 16) {
417 while (percent
< fmt
)
420 * Since we ignore an formatting argument it is no
421 * longer safe to obey the remaining formatting
422 * arguments as the arguments will no longer match
433 rtld_snprintf(char *buf
, size_t bufsize
, const char *fmt
, ...)
439 retval
= rtld_vsnprintf(buf
, bufsize
, fmt
, ap
);
445 rtld_vsnprintf(char *buf
, size_t bufsize
, const char *fmt
, va_list ap
)
447 struct snprintf_arg info
;
450 info
.method
= PRINT_METHOD_SNPRINTF
;
451 info
.buf
= info
.str
= buf
;
452 info
.buf_total
= info
.remain
= bufsize
;
454 retval
= kvprintf(fmt
, &info
, 10, ap
);
455 if (info
.remain
>= 1)
461 rtld_vfdprintf(int fd
, const char *fmt
, va_list ap
)
464 struct snprintf_arg info
;
467 info
.method
= PRINT_METHOD_WRITE
;
468 info
.buf
= info
.str
= buf
;
469 info
.buf_total
= info
.remain
= sizeof(buf
);
471 retval
= kvprintf(fmt
, &info
, 10, ap
);
477 rtld_fdprintf(int fd
, const char *fmt
, ...)
483 retval
= rtld_vfdprintf(fd
, fmt
, ap
);
489 rtld_fdprintfx(int fd
, const char *fmt
, ...)
495 retval
= rtld_vfdprintf(fd
, fmt
, ap
);
501 rtld_fdputstr(int fd
, const char *str
)
504 write(fd
, str
, strlen(str
));
508 rtld_fdputchar(int fd
, int c
)