1 /* $NetBSD: iscprint.c,v 1.1.1.4 2014/07/12 11:57:59 spz Exp $ */
3 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
4 * Copyright (C) 1999-2001, 2003 Internet Software Consortium.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/cdefs.h>
20 __RCSID("$NetBSD: iscprint.c,v 1.1.1.4 2014/07/12 11:57:59 spz Exp $");
22 /* Id: iscprint.c,v 1.2 2005/03/17 20:30:41 dhankins Exp */
29 static char copyright
[] =
30 "Id: iscprint.c,v 1.2 2005/03/17 20:30:41 dhankins Exp Copyright (c) 2004 Internet Systems Consortium, Inc. All rights reserved.";
33 #define INSIST(cond) REQUIRE(cond)
34 #define REQUIRE(cond) if (!(cond)) { return 0; }
37 * Return length of string that would have been written if not truncated.
41 isc_print_snprintf(char *str
, size_t size
, const char *format
, ...) {
46 ret
= vsnprintf(str
, size
, format
, ap
);
52 * Return length of string that would have been written if not truncated.
56 isc_print_vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
) {
69 unsigned long precision
;
82 #ifdef HAVE_LONG_DOUBLE
88 INSIST(format
!= NULL
);
90 while (*format
!= '\0') {
105 dot
= neg
= space
= plus
= left
= zero
= alt
= h
= l
= q
= 0;
106 width
= precision
= 0;
108 length
= pad
= zeropad
= 0;
111 if (*format
== '#') {
114 } else if (*format
== '-') {
118 } else if (*format
== ' ') {
122 } else if (*format
== '+') {
126 } else if (*format
== '0') {
137 if (*format
== '*') {
138 width
= va_arg(ap
, int);
140 } else if (isdigit((unsigned char)*format
)) {
142 width
= strtoul(format
, &e
, 10);
149 if (*format
== '.') {
152 if (*format
== '*') {
153 precision
= va_arg(ap
, int);
155 } else if (isdigit((unsigned char)*format
)) {
157 precision
= strtoul(format
, &e
, 10);
183 if (*format
== 'l') {
202 p
= va_arg(ap
, short *);
207 p
= va_arg(ap
, long *);
212 p
= va_arg(ap
, int *);
220 tmpi
= va_arg(ap
, isc_int64_t
);
222 tmpi
= va_arg(ap
, long int);
224 tmpi
= va_arg(ap
, int);
237 sprintf(buf
, "%u", tmpui
);
241 tmpui
= va_arg(ap
, isc_uint64_t
);
243 tmpui
= va_arg(ap
, long int);
245 tmpui
= va_arg(ap
, int);
246 sprintf(buf
, alt
? "%#o"
251 tmpui
= va_arg(ap
, isc_uint64_t
);
253 tmpui
= va_arg(ap
, unsigned long int);
255 tmpui
= va_arg(ap
, unsigned int);
256 sprintf(buf
, "%u", tmpui
);
260 tmpui
= va_arg(ap
, isc_uint64_t
);
262 tmpui
= va_arg(ap
, unsigned long int);
264 tmpui
= va_arg(ap
, unsigned int);
270 sprintf(buf
, "%x", tmpui
);
274 tmpui
= va_arg(ap
, isc_uint64_t
);
276 tmpui
= va_arg(ap
, unsigned long int);
278 tmpui
= va_arg(ap
, unsigned int);
284 sprintf(buf
, "%X", tmpui
);
287 if (precision
!= 0 || width
!= 0) {
288 length
= strlen(buf
);
289 if (length
< precision
)
290 zeropad
= precision
- length
;
291 else if (length
< width
&& zero
)
292 zeropad
= width
- length
;
294 pad
= width
- length
-
295 zeropad
- strlen(head
);
300 count
+= strlen(head
) + strlen(buf
) + pad
+
303 while (pad
> 0 && size
> 1) {
310 while (*cp
!= '\0' && size
> 1) {
314 while (zeropad
> 0 && size
> 1) {
320 while (*cp
!= '\0' && size
> 1) {
324 while (pad
> 0 && size
> 1) {
335 cp
= va_arg(ap
, char *);
338 if (precision
!= 0) {
340 * cp need not be NULL terminated.
347 while (n
!= 0 && *tp
!= '\0')
349 length
= precision
- n
;
354 pad
= width
- length
;
358 count
+= pad
+ length
;
360 while (pad
> 0 && size
> 1) {
366 while (precision
> 0 && *cp
!= '\0' &&
373 while (*cp
!= '\0' && size
> 1) {
377 while (pad
> 0 && size
> 1) {
392 while (width
-- > 0 && size
> 1) {
396 if (!left
&& size
> 1) {
409 v
= va_arg(ap
, void *);
410 sprintf(buf
, "%p", v
);
411 length
= strlen(buf
);
412 if (precision
> length
)
413 zeropad
= precision
- length
;
415 pad
= width
- length
- zeropad
;
419 count
+= length
+ pad
+ zeropad
;
421 while (pad
> 0 && size
> 1) {
427 if (zeropad
> 0 && buf
[0] == '0' &&
428 (buf
[1] == 'x' || buf
[1] == 'X')) {
437 while (zeropad
> 0 && size
> 1) {
443 while (*cp
!= '\0' && size
> 1) {
447 while (pad
> 0 && size
> 1) {
453 case 'D': /*deprecated*/
454 INSIST("use %ld instead of %D" == NULL
);
455 case 'O': /*deprecated*/
456 INSIST("use %lo instead of %O" == NULL
);
457 case 'U': /*deprecated*/
458 INSIST("use %lu instead of %U" == NULL
);
461 #ifdef HAVE_LONG_DOUBLE
464 INSIST("long doubles are not supported" == NULL
);
475 * IEEE floating point.
476 * MIN 2.2250738585072014E-308
477 * MAX 1.7976931348623157E+308
478 * VAX floating point has a smaller range than IEEE.
480 * precisions > 324 don't make much sense.
481 * if we cap the precision at 512 we will not
486 sprintf(fmt
, "%%%s%s.%lu%s%c", alt
? "#" : "",
487 plus
? "+" : space
? " " : "",
488 precision
, l
? "L" : "", *format
);
495 #ifdef HAVE_LONG_DOUBLE
497 ldbl
= va_arg(ap
, long double);
498 sprintf(buf
, fmt
, ldbl
);
502 dbl
= va_arg(ap
, double);
503 sprintf(buf
, fmt
, dbl
);
505 length
= strlen(buf
);
507 pad
= width
- length
;
511 count
+= length
+ pad
;
513 while (pad
> 0 && size
> 1) {
519 while (*cp
!= ' ' && size
> 1) {
523 while (pad
> 0 && size
> 1) {