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.
18 /* $Id: print.c,v 1.22.2.4 2004/03/09 06:11:50 marka Exp $ */
23 #include <stdio.h> /* for sprintf */
26 #define ISC__PRINT_SOURCE /* Used to get the isc_print_* prototypes. */
28 #include <isc/assertions.h>
31 #include <isc/print.h>
35 * Return length of string that would have been written if not truncated.
39 isc_print_snprintf(char *str, size_t size, const char *format, ...) {
44 ret = vsnprintf(str, size, format, ap);
51 * Return length of string that would have been written if not truncated.
55 isc_print_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
68 unsigned long precision;
81 #ifdef HAVE_LONG_DOUBLE
87 INSIST(format != NULL);
89 while (*format != '\0') {
104 dot = neg = space = plus = left = zero = alt = h = l = q = 0;
105 width = precision = 0;
107 length = pad = zeropad = 0;
110 if (*format == '#') {
113 } else if (*format == '-') {
117 } else if (*format == ' ') {
121 } else if (*format == '+') {
125 } else if (*format == '0') {
136 if (*format == '*') {
137 width = va_arg(ap, int);
139 } else if (isdigit((unsigned char)*format)) {
141 width = strtoul(format, &e, 10);
148 if (*format == '.') {
151 if (*format == '*') {
152 precision = va_arg(ap, int);
154 } else if (isdigit((unsigned char)*format)) {
156 precision = strtoul(format, &e, 10);
182 if (*format == 'l') {
201 p = va_arg(ap, short *);
206 p = va_arg(ap, long *);
211 p = va_arg(ap, int *);
219 tmpi = va_arg(ap, isc_int64_t);
221 tmpi = va_arg(ap, long int);
223 tmpi = va_arg(ap, int);
236 sprintf(buf, "%" ISC_PRINT_QUADFORMAT "u",
241 tmpui = va_arg(ap, isc_uint64_t);
243 tmpui = va_arg(ap, long int);
245 tmpui = va_arg(ap, int);
247 alt ? "%#" ISC_PRINT_QUADFORMAT "o"
248 : "%" ISC_PRINT_QUADFORMAT "o",
253 tmpui = va_arg(ap, isc_uint64_t);
255 tmpui = va_arg(ap, unsigned long int);
257 tmpui = va_arg(ap, unsigned int);
258 sprintf(buf, "%" ISC_PRINT_QUADFORMAT "u",
263 tmpui = va_arg(ap, isc_uint64_t);
265 tmpui = va_arg(ap, unsigned long int);
267 tmpui = va_arg(ap, unsigned int);
273 sprintf(buf, "%" ISC_PRINT_QUADFORMAT "x",
278 tmpui = va_arg(ap, isc_uint64_t);
280 tmpui = va_arg(ap, unsigned long int);
282 tmpui = va_arg(ap, unsigned int);
288 sprintf(buf, "%" ISC_PRINT_QUADFORMAT "X",
292 if (precision != 0 || width != 0) {
293 length = strlen(buf);
294 if (length < precision)
295 zeropad = precision - length;
296 else if (length < width && zero)
297 zeropad = width - length;
299 pad = width - length -
300 zeropad - strlen(head);
305 count += strlen(head) + strlen(buf) + pad +
308 while (pad > 0 && size > 1) {
315 while (*cp != '\0' && size > 1) {
319 while (zeropad > 0 && size > 1) {
325 while (*cp != '\0' && size > 1) {
329 while (pad > 0 && size > 1) {
340 cp = va_arg(ap, char *);
343 if (precision != 0) {
345 * cp need not be NULL terminated.
352 while (n != 0 && *tp != '\0')
354 length = precision - n;
359 pad = width - length;
363 count += pad + length;
365 while (pad > 0 && size > 1) {
371 while (precision > 0 && *cp != '\0' &&
378 while (*cp != '\0' && size > 1) {
382 while (pad > 0 && size > 1) {
397 while (width-- > 0 && size > 1) {
401 if (!left && size > 1) {
414 v = va_arg(ap, void *);
415 sprintf(buf, "%p", v);
416 length = strlen(buf);
417 if (precision > length)
418 zeropad = precision - length;
420 pad = width - length - zeropad;
424 count += length + pad + zeropad;
426 while (pad > 0 && size > 1) {
432 if (zeropad > 0 && buf[0] == '0' &&
433 (buf[1] == 'x' || buf[1] == 'X')) {
442 while (zeropad > 0 && size > 1) {
448 while (*cp != '\0' && size > 1) {
452 while (pad > 0 && size > 1) {
458 case 'D': /*deprecated*/
459 INSIST("use %ld instead of %D" == NULL);
460 case 'O': /*deprecated*/
461 INSIST("use %lo instead of %O" == NULL);
462 case 'U': /*deprecated*/
463 INSIST("use %lu instead of %U" == NULL);
466 #ifdef HAVE_LONG_DOUBLE
469 INSIST("long doubles are not supported" == NULL);
480 * IEEE floating point.
481 * MIN 2.2250738585072014E-308
482 * MAX 1.7976931348623157E+308
483 * VAX floating point has a smaller range than IEEE.
485 * precisions > 324 don't make much sense.
486 * if we cap the precision at 512 we will not
491 sprintf(fmt, "%%%s%s.%lu%s%c", alt ? "#" : "",
492 plus ? "+" : space ? " " : "",
493 precision, l ? "L" : "", *format);
500 #ifdef HAVE_LONG_DOUBLE
502 ldbl = va_arg(ap, long double);
503 sprintf(buf, fmt, ldbl);
507 dbl = va_arg(ap, double);
508 sprintf(buf, fmt, dbl);
510 length = strlen(buf);
512 pad = width - length;
516 count += length + pad;
518 while (pad > 0 && size > 1) {
524 while (*cp != ' ' && size > 1) {
528 while (pad > 0 && size > 1) {