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.2.4.3 2004/09/16 07:01:13 marka Exp $ */
23 #include <stdio.h> /* for sprintf */
26 #define LWRES__PRINT_SOURCE /* Used to get the lwres_print_* prototypes. */
34 lwres__print_sprintf(char *str, const char *format, ...) {
38 vsprintf(str, format, ap);
44 * Return length of string that would have been written if not truncated.
48 lwres__print_snprintf(char *str, size_t size, const char *format, ...) {
53 ret = vsnprintf(str, size, format, ap);
60 * Return length of string that would have been written if not truncated.
64 lwres__print_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
75 unsigned long long tmpui;
77 unsigned long precision;
90 #ifdef HAVE_LONG_DOUBLE
96 INSIST(format != NULL);
98 while (*format != '\0') {
113 dot = neg = space = plus = left = zero = alt = h = l = q = 0;
114 width = precision = 0;
116 length = pad = zeropad = 0;
119 if (*format == '#') {
122 } else if (*format == '-') {
126 } else if (*format == ' ') {
130 } else if (*format == '+') {
134 } else if (*format == '0') {
145 if (*format == '*') {
146 width = va_arg(ap, int);
148 } else if (isdigit((unsigned char)*format)) {
150 width = strtoul(format, &e, 10);
157 if (*format == '.') {
160 if (*format == '*') {
161 precision = va_arg(ap, int);
163 } else if (isdigit((unsigned char)*format)) {
165 precision = strtoul(format, &e, 10);
191 if (*format == 'l') {
210 p = va_arg(ap, short *);
215 p = va_arg(ap, long *);
220 p = va_arg(ap, int *);
228 tmpi = va_arg(ap, long long int);
230 tmpi = va_arg(ap, long int);
232 tmpi = va_arg(ap, int);
251 unsigned long long int);
253 tmpui = va_arg(ap, long int);
255 tmpui = va_arg(ap, int);
257 alt ? "%#llo" : "%llo", tmpui);
262 unsigned long long int);
264 tmpui = va_arg(ap, unsigned long int);
266 tmpui = va_arg(ap, unsigned int);
267 sprintf(buf, "%llu", tmpui);
272 unsigned long long int);
274 tmpui = va_arg(ap, unsigned long int);
276 tmpui = va_arg(ap, unsigned int);
282 sprintf(buf, "%llx", tmpui);
287 unsigned long long int);
289 tmpui = va_arg(ap, unsigned long int);
291 tmpui = va_arg(ap, unsigned int);
297 sprintf(buf, "%llX", tmpui);
300 if (precision != 0U || width != 0U) {
301 length = strlen(buf);
302 if (length < precision)
303 zeropad = precision - length;
304 else if (length < width && zero)
305 zeropad = width - length;
307 pad = width - length -
308 zeropad - strlen(head);
313 count += strlen(head) + strlen(buf) + pad +
316 while (pad > 0 && size > 1U) {
323 while (*cp != '\0' && size > 1U) {
327 while (zeropad > 0 && size > 1U) {
333 while (*cp != '\0' && size > 1U) {
337 while (pad > 0 && size > 1U) {
348 cp = va_arg(ap, char *);
351 if (precision != 0U) {
353 * cp need not be NULL terminated.
360 while (n != 0U && *tp != '\0')
362 length = precision - n;
367 pad = width - length;
371 count += pad + length;
373 while (pad > 0 && size > 1U) {
379 while (precision > 0U && *cp != '\0' &&
386 while (*cp != '\0' && size > 1U) {
390 while (pad > 0 && size > 1U) {
405 while (width-- > 0U && size > 1U) {
409 if (!left && size > 1U) {
422 v = va_arg(ap, void *);
423 sprintf(buf, "%p", v);
424 length = strlen(buf);
425 if (precision > length)
426 zeropad = precision - length;
428 pad = width - length - zeropad;
432 count += length + pad + zeropad;
434 while (pad > 0 && size > 1U) {
440 if (zeropad > 0 && buf[0] == '0' &&
441 (buf[1] == 'x' || buf[1] == 'X')) {
450 while (zeropad > 0 && size > 1U) {
456 while (*cp != '\0' && size > 1U) {
460 while (pad > 0 && size > 1U) {
466 case 'D': /*deprecated*/
467 INSIST("use %ld instead of %D" == NULL);
468 case 'O': /*deprecated*/
469 INSIST("use %lo instead of %O" == NULL);
470 case 'U': /*deprecated*/
471 INSIST("use %lu instead of %U" == NULL);
474 #ifdef HAVE_LONG_DOUBLE
477 INSIST("long doubles are not supported" == NULL);
488 * IEEE floating point.
489 * MIN 2.2250738585072014E-308
490 * MAX 1.7976931348623157E+308
491 * VAX floating point has a smaller range than IEEE.
493 * precisions > 324 don't make much sense.
494 * if we cap the precision at 512 we will not
497 if (precision > 512U)
499 sprintf(fmt, "%%%s%s.%lu%s%c", alt ? "#" : "",
500 plus ? "+" : space ? " " : "",
501 precision, l ? "L" : "", *format);
508 #ifdef HAVE_LONG_DOUBLE
510 ldbl = va_arg(ap, long double);
511 sprintf(buf, fmt, ldbl);
515 dbl = va_arg(ap, double);
516 sprintf(buf, fmt, dbl);
518 length = strlen(buf);
520 pad = width - length;
524 count += length + pad;
526 while (pad > 0 && size > 1U) {
532 while (*cp != ' ' && size > 1U) {
536 while (pad > 0 && size > 1U) {