2 * Copyright (C) 2004-2008, 2010 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001, 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or 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.37 2010/10/18 23:47:08 tbox Exp $ */
25 #include <stdio.h> /* for sprintf() */
26 #include <string.h> /* for strlen() */
28 #define ISC__PRINT_SOURCE /* Used to get the isc_print_* prototypes. */
30 #include <isc/assertions.h>
33 #include <isc/print.h>
34 #include <isc/stdlib.h>
38 isc_print_sprintf(char *str, const char *format, ...) {
42 vsprintf(str, format, ap);
48 * Return length of string that would have been written if not truncated.
52 isc_print_snprintf(char *str, size_t size, const char *format, ...) {
57 ret = vsnprintf(str, size, format, ap);
64 * Return length of string that would have been written if not truncated.
68 isc_print_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
81 unsigned long precision;
94 #ifdef HAVE_LONG_DOUBLE
100 INSIST(format != NULL);
102 while (*format != '\0') {
103 if (*format != '%') {
117 dot = neg = space = plus = left = zero = alt = h = l = q = 0;
118 width = precision = 0;
120 length = pad = zeropad = 0;
123 if (*format == '#') {
126 } else if (*format == '-') {
130 } else if (*format == ' ') {
134 } else if (*format == '+') {
138 } else if (*format == '0') {
149 if (*format == '*') {
150 width = va_arg(ap, int);
152 } else if (isdigit((unsigned char)*format)) {
154 width = strtoul(format, &e, 10);
161 if (*format == '.') {
164 if (*format == '*') {
165 precision = va_arg(ap, int);
167 } else if (isdigit((unsigned char)*format)) {
169 precision = strtoul(format, &e, 10);
195 if (*format == 'l') {
214 p = va_arg(ap, short *);
219 p = va_arg(ap, long *);
224 p = va_arg(ap, int *);
232 tmpi = va_arg(ap, isc_int64_t);
234 tmpi = va_arg(ap, long int);
236 tmpi = va_arg(ap, int);
249 if (tmpui <= 0xffffffffU)
251 (unsigned long)tmpui);
256 lo = tmpui % 1000000000;
258 mid = tmpui % 1000000000;
259 hi = tmpui / 1000000000;
261 sprintf(buf, "%lu", hi);
264 sprintf(buf + strlen(buf), "%lu", mid);
265 sprintf(buf + strlen(buf), "%lu", lo);
270 tmpui = va_arg(ap, isc_uint64_t);
272 tmpui = va_arg(ap, long int);
274 tmpui = va_arg(ap, int);
275 if (tmpui <= 0xffffffffU)
276 sprintf(buf, alt ? "%#lo" : "%lo",
277 (unsigned long)tmpui);
282 lo = tmpui % 010000000000;
283 tmpui /= 010000000000;
284 mid = tmpui % 010000000000;
285 hi = tmpui / 010000000000;
288 alt ? "%#lo" : "%lo",
290 sprintf(buf + strlen(buf),
294 alt ? "%#lo" : "%lo",
296 sprintf(buf + strlen(buf), "%lo", lo);
301 tmpui = va_arg(ap, isc_uint64_t);
303 tmpui = va_arg(ap, unsigned long int);
305 tmpui = va_arg(ap, unsigned int);
306 if (tmpui <= 0xffffffffU)
308 (unsigned long)tmpui);
313 lo = tmpui % 1000000000;
315 mid = tmpui % 1000000000;
316 hi = tmpui / 1000000000;
318 sprintf(buf, "%lu", hi);
321 sprintf(buf + strlen(buf), "%lu", mid);
322 sprintf(buf + strlen(buf), "%lu", lo);
327 tmpui = va_arg(ap, isc_uint64_t);
329 tmpui = va_arg(ap, unsigned long int);
331 tmpui = va_arg(ap, unsigned int);
337 if (tmpui <= 0xffffffffU)
339 (unsigned long)tmpui);
341 unsigned long hi = tmpui>>32;
342 unsigned long lo = tmpui & 0xffffffff;
343 sprintf(buf, "%lx", hi);
344 sprintf(buf + strlen(buf), "%lx", lo);
349 tmpui = va_arg(ap, isc_uint64_t);
351 tmpui = va_arg(ap, unsigned long int);
353 tmpui = va_arg(ap, unsigned int);
359 if (tmpui <= 0xffffffffU)
361 (unsigned long)tmpui);
363 unsigned long hi = tmpui>>32;
364 unsigned long lo = tmpui & 0xffffffff;
365 sprintf(buf, "%lX", hi);
366 sprintf(buf + strlen(buf), "%lX", lo);
370 if (precision != 0 || width != 0) {
371 length = strlen(buf);
372 if (length < precision)
373 zeropad = precision - length;
374 else if (length < width && zero)
375 zeropad = width - length;
377 pad = width - length -
378 zeropad - strlen(head);
383 count += strlen(head) + strlen(buf) + pad +
386 while (pad > 0 && size > 1) {
393 while (*cp != '\0' && size > 1) {
397 while (zeropad > 0 && size > 1) {
403 while (*cp != '\0' && size > 1) {
407 while (pad > 0 && size > 1) {
418 cp = va_arg(ap, char *);
421 if (precision != 0) {
423 * cp need not be NULL terminated.
430 while (n != 0 && *tp != '\0')
432 length = precision - n;
437 pad = width - length;
441 count += pad + length;
443 while (pad > 0 && size > 1) {
449 while (precision > 0 && *cp != '\0' &&
456 while (*cp != '\0' && size > 1) {
460 while (pad > 0 && size > 1) {
471 if (left && size > 1) {
475 while (width-- > 0 && size > 1) {
479 if (!left && size > 1) {
492 v = va_arg(ap, void *);
493 sprintf(buf, "%p", v);
494 length = strlen(buf);
495 if (precision > length)
496 zeropad = precision - length;
498 pad = width - length - zeropad;
502 count += length + pad + zeropad;
504 while (pad > 0 && size > 1) {
510 if (zeropad > 0 && buf[0] == '0' &&
511 (buf[1] == 'x' || buf[1] == 'X')) {
520 while (zeropad > 0 && size > 1) {
526 while (*cp != '\0' && size > 1) {
530 while (pad > 0 && size > 1) {
536 case 'D': /*deprecated*/
537 INSIST("use %ld instead of %D" == NULL);
538 case 'O': /*deprecated*/
539 INSIST("use %lo instead of %O" == NULL);
540 case 'U': /*deprecated*/
541 INSIST("use %lu instead of %U" == NULL);
544 #ifdef HAVE_LONG_DOUBLE
547 INSIST("long doubles are not supported" == NULL);
558 * IEEE floating point.
559 * MIN 2.2250738585072014E-308
560 * MAX 1.7976931348623157E+308
561 * VAX floating point has a smaller range than IEEE.
563 * precisions > 324 don't make much sense.
564 * if we cap the precision at 512 we will not
569 sprintf(fmt, "%%%s%s.%lu%s%c", alt ? "#" : "",
570 plus ? "+" : space ? " " : "",
571 precision, l ? "L" : "", *format);
578 #ifdef HAVE_LONG_DOUBLE
580 ldbl = va_arg(ap, long double);
581 sprintf(buf, fmt, ldbl);
585 dbl = va_arg(ap, double);
586 sprintf(buf, fmt, dbl);
588 length = strlen(buf);
590 pad = width - length;
594 count += length + pad;
596 while (pad > 0 && size > 1) {
602 while (*cp != ' ' && size > 1) {
606 while (pad > 0 && size > 1) {