2 * Copyright (C) 2004-2006 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.3.2.4 2006/04/17 18:27:20 explorer 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 sprintf(buf, "%" ISC_PRINT_QUADFORMAT "u",
254 tmpui = va_arg(ap, isc_uint64_t);
256 tmpui = va_arg(ap, long int);
258 tmpui = va_arg(ap, int);
260 alt ? "%#" ISC_PRINT_QUADFORMAT "o"
261 : "%" ISC_PRINT_QUADFORMAT "o",
266 tmpui = va_arg(ap, isc_uint64_t);
268 tmpui = va_arg(ap, unsigned long int);
270 tmpui = va_arg(ap, unsigned int);
271 sprintf(buf, "%" ISC_PRINT_QUADFORMAT "u",
276 tmpui = va_arg(ap, isc_uint64_t);
278 tmpui = va_arg(ap, unsigned long int);
280 tmpui = va_arg(ap, unsigned int);
286 sprintf(buf, "%" ISC_PRINT_QUADFORMAT "x",
291 tmpui = va_arg(ap, isc_uint64_t);
293 tmpui = va_arg(ap, unsigned long int);
295 tmpui = va_arg(ap, unsigned int);
301 sprintf(buf, "%" ISC_PRINT_QUADFORMAT "X",
305 if (precision != 0 || width != 0) {
306 length = strlen(buf);
307 if (length < precision)
308 zeropad = precision - length;
309 else if (length < width && zero)
310 zeropad = width - length;
312 pad = width - length -
313 zeropad - strlen(head);
318 count += strlen(head) + strlen(buf) + pad +
321 while (pad > 0 && size > 1) {
328 while (*cp != '\0' && size > 1) {
332 while (zeropad > 0 && size > 1) {
338 while (*cp != '\0' && size > 1) {
342 while (pad > 0 && size > 1) {
353 cp = va_arg(ap, char *);
356 if (precision != 0) {
358 * cp need not be NULL terminated.
365 while (n != 0 && *tp != '\0')
367 length = precision - n;
372 pad = width - length;
376 count += pad + length;
378 while (pad > 0 && size > 1) {
384 while (precision > 0 && *cp != '\0' &&
391 while (*cp != '\0' && size > 1) {
395 while (pad > 0 && size > 1) {
410 while (width-- > 0 && size > 1) {
414 if (!left && size > 1) {
427 v = va_arg(ap, void *);
428 sprintf(buf, "%p", v);
429 length = strlen(buf);
430 if (precision > length)
431 zeropad = precision - length;
433 pad = width - length - zeropad;
437 count += length + pad + zeropad;
439 while (pad > 0 && size > 1) {
445 if (zeropad > 0 && buf[0] == '0' &&
446 (buf[1] == 'x' || buf[1] == 'X')) {
455 while (zeropad > 0 && size > 1) {
461 while (*cp != '\0' && size > 1) {
465 while (pad > 0 && size > 1) {
471 case 'D': /*deprecated*/
472 INSIST("use %ld instead of %D" == NULL);
473 case 'O': /*deprecated*/
474 INSIST("use %lo instead of %O" == NULL);
475 case 'U': /*deprecated*/
476 INSIST("use %lu instead of %U" == NULL);
479 #ifdef HAVE_LONG_DOUBLE
482 INSIST("long doubles are not supported" == NULL);
493 * IEEE floating point.
494 * MIN 2.2250738585072014E-308
495 * MAX 1.7976931348623157E+308
496 * VAX floating point has a smaller range than IEEE.
498 * precisions > 324 don't make much sense.
499 * if we cap the precision at 512 we will not
504 sprintf(fmt, "%%%s%s.%lu%s%c", alt ? "#" : "",
505 plus ? "+" : space ? " " : "",
506 precision, l ? "L" : "", *format);
513 #ifdef HAVE_LONG_DOUBLE
515 ldbl = va_arg(ap, long double);
516 sprintf(buf, fmt, ldbl);
520 dbl = va_arg(ap, double);
521 sprintf(buf, fmt, dbl);
523 length = strlen(buf);
525 pad = width - length;
529 count += length + pad;
531 while (pad > 0 && size > 1) {
537 while (*cp != ' ' && size > 1) {
541 while (pad > 0 && size > 1) {