2 * Copyright (C) 2004 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.
25 static char copyright
[] =
26 "$Id$ Copyright (c) 2004 Internet Systems Consortium, Inc. All rights reserved.";
29 #define INSIST(cond) REQUIRE(cond)
30 #define REQUIRE(cond) if (!(cond)) { return 0; }
33 * Return length of string that would have been written if not truncated.
37 isc_print_snprintf(char *str
, size_t size
, const char *format
, ...) {
42 ret
= vsnprintf(str
, size
, format
, ap
);
48 * Return length of string that would have been written if not truncated.
52 isc_print_vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
) {
65 unsigned long precision
;
78 #ifdef HAVE_LONG_DOUBLE
84 INSIST(format
!= NULL
);
86 while (*format
!= '\0') {
101 dot
= neg
= space
= plus
= left
= zero
= alt
= h
= l
= q
= 0;
102 width
= precision
= 0;
104 length
= pad
= zeropad
= 0;
107 if (*format
== '#') {
110 } else if (*format
== '-') {
114 } else if (*format
== ' ') {
118 } else if (*format
== '+') {
122 } else if (*format
== '0') {
133 if (*format
== '*') {
134 width
= va_arg(ap
, int);
136 } else if (isdigit((unsigned char)*format
)) {
138 width
= strtoul(format
, &e
, 10);
145 if (*format
== '.') {
148 if (*format
== '*') {
149 precision
= va_arg(ap
, int);
151 } else if (isdigit((unsigned char)*format
)) {
153 precision
= strtoul(format
, &e
, 10);
179 if (*format
== 'l') {
198 p
= va_arg(ap
, short *);
203 p
= va_arg(ap
, long *);
208 p
= va_arg(ap
, int *);
216 tmpi
= va_arg(ap
, isc_int64_t
);
218 tmpi
= va_arg(ap
, long int);
220 tmpi
= va_arg(ap
, int);
233 sprintf(buf
, "%u", tmpui
);
237 tmpui
= va_arg(ap
, isc_uint64_t
);
239 tmpui
= va_arg(ap
, long int);
241 tmpui
= va_arg(ap
, int);
242 sprintf(buf
, alt
? "%#o"
247 tmpui
= va_arg(ap
, isc_uint64_t
);
249 tmpui
= va_arg(ap
, unsigned long int);
251 tmpui
= va_arg(ap
, unsigned int);
252 sprintf(buf
, "%u", tmpui
);
256 tmpui
= va_arg(ap
, isc_uint64_t
);
258 tmpui
= va_arg(ap
, unsigned long int);
260 tmpui
= va_arg(ap
, unsigned int);
266 sprintf(buf
, "%x", tmpui
);
270 tmpui
= va_arg(ap
, isc_uint64_t
);
272 tmpui
= va_arg(ap
, unsigned long int);
274 tmpui
= va_arg(ap
, unsigned int);
280 sprintf(buf
, "%X", tmpui
);
283 if (precision
!= 0 || width
!= 0) {
284 length
= strlen(buf
);
285 if (length
< precision
)
286 zeropad
= precision
- length
;
287 else if (length
< width
&& zero
)
288 zeropad
= width
- length
;
290 pad
= width
- length
-
291 zeropad
- strlen(head
);
296 count
+= strlen(head
) + strlen(buf
) + pad
+
299 while (pad
> 0 && size
> 1) {
306 while (*cp
!= '\0' && size
> 1) {
310 while (zeropad
> 0 && size
> 1) {
316 while (*cp
!= '\0' && size
> 1) {
320 while (pad
> 0 && size
> 1) {
331 cp
= va_arg(ap
, char *);
334 if (precision
!= 0) {
336 * cp need not be NULL terminated.
343 while (n
!= 0 && *tp
!= '\0')
345 length
= precision
- n
;
350 pad
= width
- length
;
354 count
+= pad
+ length
;
356 while (pad
> 0 && size
> 1) {
362 while (precision
> 0 && *cp
!= '\0' &&
369 while (*cp
!= '\0' && size
> 1) {
373 while (pad
> 0 && size
> 1) {
388 while (width
-- > 0 && size
> 1) {
392 if (!left
&& size
> 1) {
405 v
= va_arg(ap
, void *);
406 sprintf(buf
, "%p", v
);
407 length
= strlen(buf
);
408 if (precision
> length
)
409 zeropad
= precision
- length
;
411 pad
= width
- length
- zeropad
;
415 count
+= length
+ pad
+ zeropad
;
417 while (pad
> 0 && size
> 1) {
423 if (zeropad
> 0 && buf
[0] == '0' &&
424 (buf
[1] == 'x' || buf
[1] == 'X')) {
433 while (zeropad
> 0 && size
> 1) {
439 while (*cp
!= '\0' && size
> 1) {
443 while (pad
> 0 && size
> 1) {
449 case 'D': /*deprecated*/
450 INSIST("use %ld instead of %D" == NULL
);
451 case 'O': /*deprecated*/
452 INSIST("use %lo instead of %O" == NULL
);
453 case 'U': /*deprecated*/
454 INSIST("use %lu instead of %U" == NULL
);
457 #ifdef HAVE_LONG_DOUBLE
460 INSIST("long doubles are not supported" == NULL
);
471 * IEEE floating point.
472 * MIN 2.2250738585072014E-308
473 * MAX 1.7976931348623157E+308
474 * VAX floating point has a smaller range than IEEE.
476 * precisions > 324 don't make much sense.
477 * if we cap the precision at 512 we will not
482 sprintf(fmt
, "%%%s%s.%lu%s%c", alt
? "#" : "",
483 plus
? "+" : space
? " " : "",
484 precision
, l
? "L" : "", *format
);
491 #ifdef HAVE_LONG_DOUBLE
493 ldbl
= va_arg(ap
, long double);
494 sprintf(buf
, fmt
, ldbl
);
498 dbl
= va_arg(ap
, double);
499 sprintf(buf
, fmt
, dbl
);
501 length
= strlen(buf
);
503 pad
= width
- length
;
507 count
+= length
+ pad
;
509 while (pad
> 0 && size
> 1) {
515 while (*cp
!= ' ' && size
> 1) {
519 while (pad
> 0 && size
> 1) {