2 * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001, 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: print.c,v 1.2.4.7 2005/10/14 01:38:51 marka Exp $ */
23 #include <stdio.h> /* for sprintf */
26 #define LWRES__PRINT_SOURCE /* Used to get the lwres_print_* prototypes. */
28 #include <lwres/stdlib.h>
33 #define LWRES_PRINT_QUADFORMAT LWRES_PLATFORM_QUADFORMAT
36 lwres__print_sprintf(char *str
, const char *format
, ...) {
40 vsprintf(str
, format
, ap
);
46 * Return length of string that would have been written if not truncated.
50 lwres__print_snprintf(char *str
, size_t size
, const char *format
, ...) {
55 ret
= vsnprintf(str
, size
, format
, ap
);
62 * Return length of string that would have been written if not truncated.
66 lwres__print_vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
) {
76 unsigned long long tmpui
;
78 unsigned long precision
;
91 #ifdef HAVE_LONG_DOUBLE
97 INSIST(format
!= NULL
);
99 while (*format
!= '\0') {
100 if (*format
!= '%') {
114 dot
= space
= plus
= left
= zero
= alt
= h
= l
= q
= 0;
115 width
= precision
= 0;
117 length
= pad
= zeropad
= 0;
120 if (*format
== '#') {
123 } else if (*format
== '-') {
127 } else if (*format
== ' ') {
131 } else if (*format
== '+') {
135 } else if (*format
== '0') {
146 if (*format
== '*') {
147 width
= va_arg(ap
, int);
149 } else if (isdigit((unsigned char)*format
)) {
151 width
= strtoul(format
, &e
, 10);
158 if (*format
== '.') {
161 if (*format
== '*') {
162 precision
= va_arg(ap
, int);
164 } else if (isdigit((unsigned char)*format
)) {
166 precision
= strtoul(format
, &e
, 10);
192 if (*format
== 'l') {
211 p
= va_arg(ap
, short *);
216 p
= va_arg(ap
, long *);
221 p
= va_arg(ap
, int *);
229 tmpi
= va_arg(ap
, long long int);
231 tmpi
= va_arg(ap
, long int);
233 tmpi
= va_arg(ap
, int);
246 sprintf(buf
, "%" LWRES_PRINT_QUADFORMAT
"u",
252 unsigned long long int);
254 tmpui
= va_arg(ap
, long int);
256 tmpui
= va_arg(ap
, int);
258 alt
? "%#" LWRES_PRINT_QUADFORMAT
"o"
259 : "%" LWRES_PRINT_QUADFORMAT
"o",
265 unsigned long long int);
267 tmpui
= va_arg(ap
, unsigned long int);
269 tmpui
= va_arg(ap
, unsigned int);
270 sprintf(buf
, "%" LWRES_PRINT_QUADFORMAT
"u",
276 unsigned long long int);
278 tmpui
= va_arg(ap
, unsigned long int);
280 tmpui
= va_arg(ap
, unsigned int);
286 sprintf(buf
, "%" LWRES_PRINT_QUADFORMAT
"x",
292 unsigned long long int);
294 tmpui
= va_arg(ap
, unsigned long int);
296 tmpui
= va_arg(ap
, unsigned int);
302 sprintf(buf
, "%" LWRES_PRINT_QUADFORMAT
"X",
306 if (precision
!= 0U || width
!= 0U) {
307 length
= strlen(buf
);
308 if (length
< precision
)
309 zeropad
= precision
- length
;
310 else if (length
< width
&& zero
)
311 zeropad
= width
- length
;
313 pad
= width
- length
-
314 zeropad
- strlen(head
);
319 count
+= strlen(head
) + strlen(buf
) + pad
+
322 while (pad
> 0 && size
> 1U) {
329 while (*cp
!= '\0' && size
> 1U) {
333 while (zeropad
> 0 && size
> 1U) {
339 while (*cp
!= '\0' && size
> 1U) {
343 while (pad
> 0 && size
> 1U) {
354 cp
= va_arg(ap
, char *);
357 if (precision
!= 0U) {
359 * cp need not be NULL terminated.
366 while (n
!= 0U && *tp
!= '\0')
368 length
= precision
- n
;
373 pad
= width
- length
;
377 count
+= pad
+ length
;
379 while (pad
> 0 && size
> 1U) {
385 while (precision
> 0U && *cp
!= '\0' &&
392 while (*cp
!= '\0' && size
> 1U) {
396 while (pad
> 0 && size
> 1U) {
411 while (width
-- > 0U && size
> 1U) {
415 if (!left
&& size
> 1U) {
428 v
= va_arg(ap
, void *);
429 sprintf(buf
, "%p", v
);
430 length
= strlen(buf
);
431 if (precision
> length
)
432 zeropad
= precision
- length
;
434 pad
= width
- length
- zeropad
;
438 count
+= length
+ pad
+ zeropad
;
440 while (pad
> 0 && size
> 1U) {
446 if (zeropad
> 0 && buf
[0] == '0' &&
447 (buf
[1] == 'x' || buf
[1] == 'X')) {
456 while (zeropad
> 0 && size
> 1U) {
462 while (*cp
!= '\0' && size
> 1U) {
466 while (pad
> 0 && size
> 1U) {
472 case 'D': /*deprecated*/
473 INSIST("use %ld instead of %D" == NULL
);
474 case 'O': /*deprecated*/
475 INSIST("use %lo instead of %O" == NULL
);
476 case 'U': /*deprecated*/
477 INSIST("use %lu instead of %U" == NULL
);
480 #ifdef HAVE_LONG_DOUBLE
483 INSIST("long doubles are not supported" == NULL
);
494 * IEEE floating point.
495 * MIN 2.2250738585072014E-308
496 * MAX 1.7976931348623157E+308
497 * VAX floating point has a smaller range than IEEE.
499 * precisions > 324 don't make much sense.
500 * if we cap the precision at 512 we will not
503 if (precision
> 512U)
505 sprintf(fmt
, "%%%s%s.%lu%s%c", alt
? "#" : "",
506 plus
? "+" : space
? " " : "",
507 precision
, l
? "L" : "", *format
);
514 #ifdef HAVE_LONG_DOUBLE
516 ldbl
= va_arg(ap
, long double);
517 sprintf(buf
, fmt
, ldbl
);
521 dbl
= va_arg(ap
, double);
522 sprintf(buf
, fmt
, dbl
);
524 length
= strlen(buf
);
526 pad
= width
- length
;
530 count
+= length
+ pad
;
532 while (pad
> 0 && size
> 1U) {
538 while (*cp
!= ' ' && size
> 1U) {
542 while (pad
> 0 && size
> 1U) {