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 * 3. 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>
44 #include "rtld_printf.h"
46 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
48 #define PRINT_METHOD_SNPRINTF 1
49 #define PRINT_METHOD_WRITE 2
61 printf_out(struct snprintf_arg
*info
)
64 if (info
->remain
== info
->buf_total
)
66 write(info
->fd
, info
->buf
, info
->buf_total
- info
->remain
);
67 info
->str
= info
->buf
;
68 info
->remain
= info
->buf_total
;
72 snprintf_func(int ch
, struct snprintf_arg
*const info
)
75 switch (info
->method
) {
76 case PRINT_METHOD_SNPRINTF
:
77 if (info
->remain
>= 2) {
82 case PRINT_METHOD_WRITE
:
83 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
, neg
, sign
, 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; neg
= 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);
188 case '1': case '2': case '3': case '4':
189 case '5': case '6': case '7': case '8': case '9':
190 for (n
= 0;; ++fmt
) {
191 n
= n
* 10 + ch
- '0';
193 if (ch
< '0' || ch
> '9')
202 num
= (u_int
)va_arg(ap
, int);
203 p
= va_arg(ap
, char *);
204 for (q
= ksprintn(nbuf
, num
, *p
++, NULL
, 0); *q
;)
212 if (num
& (1 << (n
- 1))) {
213 PCHAR(tmp
? ',' : '<');
214 for (; (n
= *p
) > ' '; ++p
)
218 for (; *p
> ' '; ++p
)
225 PCHAR(va_arg(ap
, int));
228 up
= va_arg(ap
, u_char
*);
229 p
= va_arg(ap
, char *);
233 PCHAR(hex2ascii(*up
>> 4));
234 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 *);
297 p
= va_arg(ap
, char *);
303 for (n
= 0; n
< dwidth
&& p
[n
]; n
++)
308 if (!ladjust
&& width
> 0)
313 if (ladjust
&& width
> 0)
338 num
= va_arg(ap
, uintmax_t);
340 num
= va_arg(ap
, u_quad_t
);
342 num
= va_arg(ap
, ptrdiff_t);
344 num
= va_arg(ap
, u_long
);
346 num
= va_arg(ap
, size_t);
348 num
= (u_short
)va_arg(ap
, int);
350 num
= (u_char
)va_arg(ap
, int);
352 num
= va_arg(ap
, u_int
);
356 num
= va_arg(ap
, intmax_t);
358 num
= va_arg(ap
, quad_t
);
360 num
= va_arg(ap
, ptrdiff_t);
362 num
= va_arg(ap
, long);
364 num
= va_arg(ap
, ssize_t
);
366 num
= (short)va_arg(ap
, int);
368 num
= (char)va_arg(ap
, int);
370 num
= va_arg(ap
, int);
372 if (sign
&& (intmax_t)num
< 0) {
374 num
= -(intmax_t)num
;
376 p
= ksprintn(nbuf
, num
, base
, &n
, upper
);
378 if (sharpflag
&& num
!= 0) {
387 if (!ladjust
&& padc
== '0')
388 dwidth
= width
- tmp
;
389 width
-= tmp
+ imax(dwidth
, n
);
396 if (sharpflag
&& num
!= 0) {
399 } else if (base
== 16) {
416 while (percent
< fmt
)
419 * Since we ignore an formatting argument it is no
420 * longer safe to obey the remaining formatting
421 * arguments as the arguments will no longer match
432 rtld_vsnprintf(char *buf
, size_t bufsize
, const char *fmt
, va_list ap
)
434 struct snprintf_arg info
;
437 info
.method
= PRINT_METHOD_SNPRINTF
;
438 info
.buf
= info
.str
= buf
;
439 info
.buf_total
= info
.remain
= bufsize
;
441 retval
= kvprintf(fmt
, &info
, 10, ap
);
442 if (info
.remain
>= 1)
448 rtld_vfdprintf(int fd
, const char *fmt
, va_list ap
)
451 struct snprintf_arg info
;
454 info
.method
= PRINT_METHOD_WRITE
;
455 info
.buf
= info
.str
= buf
;
456 info
.buf_total
= info
.remain
= sizeof(buf
);
458 retval
= kvprintf(fmt
, &info
, 10, ap
);
464 rtld_fdprintf(int fd
, const char *fmt
, ...)
470 retval
= rtld_vfdprintf(fd
, fmt
, ap
);
476 rtld_fdputstr(int fd
, const char *str
)
479 write(fd
, str
, strlen(str
));
483 rtld_fdputchar(int fd
, int c
)