2 * Copyright (C) 2004, 2005, 2007, 2011, 2012 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.
23 #include <stdio.h> /* for sprintf */
26 #define LWRES__PRINT_SOURCE /* Used to get the lwres_print_* prototypes. */
28 #include <lwres/stdlib.h>
33 #define LWRES_PRINT_QUADFORMAT LWRES_PLATFORM_QUADFORMAT
36 lwres__print_sprintf(char *str, const char *format, ...) {
40 vsprintf(str, format, ap);
46 * Return length of string that would have been written if not truncated.
50 lwres__print_snprintf(char *str, size_t size, const char *format, ...) {
55 ret = vsnprintf(str, size, format, ap);
62 * Return length of string that would have been written if not truncated.
66 lwres__print_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
76 unsigned long long tmpui;
78 unsigned long precision;
91 #ifdef HAVE_LONG_DOUBLE
97 INSIST(format != NULL);
99 while (*format != '\0') {
100 if (*format != '%') {
114 dot = space = plus = left = zero = alt = h = l = q = 0;
115 width = precision = 0;
117 length = pad = zeropad = 0;
121 if (*format == '#') {
124 } else if (*format == '-') {
128 } else if (*format == ' ') {
132 } else if (*format == '+') {
136 } else if (*format == '0') {
147 if (*format == '*') {
148 width = va_arg(ap, int);
150 } else if (isdigit((unsigned char)*format)) {
152 width = strtoul(format, &e, 10);
159 if (*format == '.') {
162 if (*format == '*') {
163 precision = va_arg(ap, int);
165 } else if (isdigit((unsigned char)*format)) {
167 precision = strtoul(format, &e, 10);
193 if (*format == 'l') {
212 p = va_arg(ap, short *);
217 p = va_arg(ap, long *);
222 p = va_arg(ap, int *);
230 tmpi = va_arg(ap, long long int);
232 tmpi = va_arg(ap, long int);
234 tmpi = va_arg(ap, int);
247 sprintf(buf, "%" LWRES_PRINT_QUADFORMAT "u",
253 unsigned long long int);
255 tmpui = va_arg(ap, long int);
257 tmpui = va_arg(ap, int);
259 alt ? "%#" LWRES_PRINT_QUADFORMAT "o"
260 : "%" LWRES_PRINT_QUADFORMAT "o",
266 unsigned long long int);
268 tmpui = va_arg(ap, unsigned long int);
270 tmpui = va_arg(ap, unsigned int);
271 sprintf(buf, "%" LWRES_PRINT_QUADFORMAT "u",
277 unsigned long long int);
279 tmpui = va_arg(ap, unsigned long int);
281 tmpui = va_arg(ap, unsigned int);
287 sprintf(buf, "%" LWRES_PRINT_QUADFORMAT "x",
293 unsigned long long int);
295 tmpui = va_arg(ap, unsigned long int);
297 tmpui = va_arg(ap, unsigned int);
303 sprintf(buf, "%" LWRES_PRINT_QUADFORMAT "X",
307 if (precision != 0U || width != 0U) {
308 length = strlen(buf);
309 if (length < precision)
310 zeropad = precision - length;
311 else if (length < width && zero)
312 zeropad = width - length;
314 pad = width - length -
315 zeropad - strlen(head);
320 count += strlen(head) + strlen(buf) + pad +
323 while (pad > 0 && size > 1U) {
330 while (*cp != '\0' && size > 1U) {
334 while (zeropad > 0 && size > 1U) {
340 while (*cp != '\0' && size > 1U) {
344 while (pad > 0 && size > 1U) {
355 cp = va_arg(ap, char *);
358 if (precision != 0U) {
360 * cp need not be NULL terminated.
367 while (n != 0U && *tp != '\0')
369 length = precision - n;
374 pad = width - length;
378 count += pad + length;
380 while (pad > 0 && size > 1U) {
386 while (precision > 0U && *cp != '\0' &&
393 while (*cp != '\0' && size > 1U) {
397 while (pad > 0 && size > 1U) {
412 while (width-- > 0U && size > 1U) {
416 if (!left && size > 1U) {
429 v = va_arg(ap, void *);
430 sprintf(buf, "%p", v);
431 length = strlen(buf);
432 if (precision > length)
433 zeropad = precision - length;
435 pad = width - length - zeropad;
439 count += length + pad + zeropad;
441 while (pad > 0 && size > 1U) {
447 if (zeropad > 0 && buf[0] == '0' &&
448 (buf[1] == 'x' || buf[1] == 'X')) {
457 while (zeropad > 0 && size > 1U) {
463 while (*cp != '\0' && size > 1U) {
467 while (pad > 0 && size > 1U) {
474 case 'D': /*deprecated*/
475 INSIST("use %ld instead of %D" == NULL);
477 case 'O': /*deprecated*/
478 INSIST("use %lo instead of %O" == NULL);
480 case 'U': /*deprecated*/
481 INSIST("use %lu instead of %U" == NULL);
485 #ifdef HAVE_LONG_DOUBLE
488 INSIST("long doubles are not supported" == NULL);
499 * IEEE floating point.
500 * MIN 2.2250738585072014E-308
501 * MAX 1.7976931348623157E+308
502 * VAX floating point has a smaller range than IEEE.
504 * precisions > 324 don't make much sense.
505 * if we cap the precision at 512 we will not
508 if (precision > 512U)
510 sprintf(fmt, "%%%s%s.%lu%s%c", alt ? "#" : "",
511 plus ? "+" : space ? " " : "",
512 precision, l ? "L" : "", *format);
519 #ifdef HAVE_LONG_DOUBLE
521 ldbl = va_arg(ap, long double);
522 sprintf(buf, fmt, ldbl);
526 dbl = va_arg(ap, double);
527 sprintf(buf, fmt, dbl);
529 length = strlen(buf);
531 pad = width - length;
535 count += length + pad;
537 while (pad > 0 && size > 1U) {
543 while (*cp != ' ' && size > 1U) {
547 while (pad > 0 && size > 1U) {