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.3.2.3 2004/03/06 08:14:33 marka Exp $ */
23 #include <stdio.h> /* for sprintf */
25 #define ISC__PRINT_SOURCE /* Used to get the isc_print_* prototypes. */
27 #include <isc/assertions.h>
30 #include <isc/print.h>
31 #include <isc/stdlib.h>
35 isc_print_sprintf(char *str, const char *format, ...) {
39 vsprintf(str, format, ap);
45 * Return length of string that would have been written if not truncated.
49 isc_print_snprintf(char *str, size_t size, const char *format, ...) {
54 ret = vsnprintf(str, size, format, ap);
61 * Return length of string that would have been written if not truncated.
65 isc_print_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
78 unsigned long precision;
91 #ifdef HAVE_LONG_DOUBLE
97 INSIST(format != NULL);
99 while (*format != '\0') {
100 if (*format != '%') {
114 dot = neg = space = plus = left = zero = alt = h = l = q = 0;
115 width = precision = 0;
117 length = pad = zeropad = 0;
120 if (*format == '#') {
123 } else if (*format == '-') {
127 } else if (*format == ' ') {
131 } else if (*format == '+') {
135 } else if (*format == '0') {
146 if (*format == '*') {
147 width = va_arg(ap, int);
149 } else if (isdigit((unsigned char)*format)) {
151 width = strtoul(format, &e, 10);
158 if (*format == '.') {
161 if (*format == '*') {
162 precision = va_arg(ap, int);
164 } else if (isdigit((unsigned char)*format)) {
166 precision = strtoul(format, &e, 10);
192 if (*format == 'l') {
211 p = va_arg(ap, short *);
216 p = va_arg(ap, long *);
221 p = va_arg(ap, int *);
229 tmpi = va_arg(ap, isc_int64_t);
231 tmpi = va_arg(ap, long int);
233 tmpi = va_arg(ap, int);
246 sprintf(buf, "%" ISC_PRINT_QUADFORMAT "u",
251 tmpui = va_arg(ap, isc_uint64_t);
253 tmpui = va_arg(ap, long int);
255 tmpui = va_arg(ap, int);
257 alt ? "%#" ISC_PRINT_QUADFORMAT "o"
258 : "%" ISC_PRINT_QUADFORMAT "o",
263 tmpui = va_arg(ap, isc_uint64_t);
265 tmpui = va_arg(ap, unsigned long int);
267 tmpui = va_arg(ap, unsigned int);
268 sprintf(buf, "%" ISC_PRINT_QUADFORMAT "u",
273 tmpui = va_arg(ap, isc_uint64_t);
275 tmpui = va_arg(ap, unsigned long int);
277 tmpui = va_arg(ap, unsigned int);
283 sprintf(buf, "%" ISC_PRINT_QUADFORMAT "x",
288 tmpui = va_arg(ap, isc_uint64_t);
290 tmpui = va_arg(ap, unsigned long int);
292 tmpui = va_arg(ap, unsigned int);
298 sprintf(buf, "%" ISC_PRINT_QUADFORMAT "X",
302 if (precision != 0 || width != 0) {
303 length = strlen(buf);
304 if (length < precision)
305 zeropad = precision - length;
306 else if (length < width && zero)
307 zeropad = width - length;
309 pad = width - length -
310 zeropad - strlen(head);
315 count += strlen(head) + strlen(buf) + pad +
318 while (pad > 0 && size > 1) {
325 while (*cp != '\0' && size > 1) {
329 while (zeropad > 0 && size > 1) {
335 while (*cp != '\0' && size > 1) {
339 while (pad > 0 && size > 1) {
350 cp = va_arg(ap, char *);
353 if (precision != 0) {
355 * cp need not be NULL terminated.
362 while (n != 0 && *tp != '\0')
364 length = precision - n;
369 pad = width - length;
373 count += pad + length;
375 while (pad > 0 && size > 1) {
381 while (precision > 0 && *cp != '\0' &&
388 while (*cp != '\0' && size > 1) {
392 while (pad > 0 && size > 1) {
407 while (width-- > 0 && size > 1) {
411 if (!left && size > 1) {
424 v = va_arg(ap, void *);
425 sprintf(buf, "%p", v);
426 length = strlen(buf);
427 if (precision > length)
428 zeropad = precision - length;
430 pad = width - length - zeropad;
434 count += length + pad + zeropad;
436 while (pad > 0 && size > 1) {
442 if (zeropad > 0 && buf[0] == '0' &&
443 (buf[1] == 'x' || buf[1] == 'X')) {
452 while (zeropad > 0 && size > 1) {
458 while (*cp != '\0' && size > 1) {
462 while (pad > 0 && size > 1) {
468 case 'D': /*deprecated*/
469 INSIST("use %ld instead of %D" == NULL);
470 case 'O': /*deprecated*/
471 INSIST("use %lo instead of %O" == NULL);
472 case 'U': /*deprecated*/
473 INSIST("use %lu instead of %U" == NULL);
476 #ifdef HAVE_LONG_DOUBLE
479 INSIST("long doubles are not supported" == NULL);
490 * IEEE floating point.
491 * MIN 2.2250738585072014E-308
492 * MAX 1.7976931348623157E+308
493 * VAX floating point has a smaller range than IEEE.
495 * precisions > 324 don't make much sense.
496 * if we cap the precision at 512 we will not
501 sprintf(fmt, "%%%s%s.%lu%s%c", alt ? "#" : "",
502 plus ? "+" : space ? " " : "",
503 precision, l ? "L" : "", *format);
510 #ifdef HAVE_LONG_DOUBLE
512 ldbl = va_arg(ap, long double);
513 sprintf(buf, fmt, ldbl);
517 dbl = va_arg(ap, double);
518 sprintf(buf, fmt, dbl);
520 length = strlen(buf);
522 pad = width - length;
526 count += length + pad;
528 while (pad > 0 && size > 1) {
534 while (*cp != ' ' && size > 1) {
538 while (pad > 0 && size > 1) {