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.1 2004/08/28 06:25:25 marka Exp $ */
23 #include <stdio.h> /* for sprintf */
25 #define LWRES__PRINT_SOURCE /* Used to get the lwres_print_* prototypes. */
33 lwres__print_sprintf(char *str, const char *format, ...) {
37 vsprintf(str, format, ap);
43 * Return length of string that would have been written if not truncated.
47 lwres__print_snprintf(char *str, size_t size, const char *format, ...) {
52 ret = vsnprintf(str, size, format, ap);
59 * Return length of string that would have been written if not truncated.
63 lwres__print_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
74 unsigned long long tmpui;
76 unsigned long precision;
89 #ifdef HAVE_LONG_DOUBLE
95 INSIST(format != NULL);
97 while (*format != '\0') {
112 dot = neg = space = plus = left = zero = alt = h = l = q = 0;
113 width = precision = 0;
115 length = pad = zeropad = 0;
118 if (*format == '#') {
121 } else if (*format == '-') {
125 } else if (*format == ' ') {
129 } else if (*format == '+') {
133 } else if (*format == '0') {
144 if (*format == '*') {
145 width = va_arg(ap, int);
147 } else if (isdigit((unsigned char)*format)) {
149 width = strtoul(format, &e, 10);
156 if (*format == '.') {
159 if (*format == '*') {
160 precision = va_arg(ap, int);
162 } else if (isdigit((unsigned char)*format)) {
164 precision = strtoul(format, &e, 10);
190 if (*format == 'l') {
209 p = va_arg(ap, short *);
214 p = va_arg(ap, long *);
219 p = va_arg(ap, int *);
227 tmpi = va_arg(ap, long long int);
229 tmpi = va_arg(ap, long int);
231 tmpi = va_arg(ap, int);
250 unsigned long long int);
252 tmpui = va_arg(ap, long int);
254 tmpui = va_arg(ap, int);
256 alt ? "%#llo" : "%llo", tmpui);
261 unsigned long long int);
263 tmpui = va_arg(ap, unsigned long int);
265 tmpui = va_arg(ap, unsigned int);
266 sprintf(buf, "%llu", tmpui);
271 unsigned long long int);
273 tmpui = va_arg(ap, unsigned long int);
275 tmpui = va_arg(ap, unsigned int);
281 sprintf(buf, "%llx", tmpui);
286 unsigned long long int);
288 tmpui = va_arg(ap, unsigned long int);
290 tmpui = va_arg(ap, unsigned int);
296 sprintf(buf, "%llX", tmpui);
299 if (precision != 0 || width != 0) {
300 length = strlen(buf);
301 if (length < precision)
302 zeropad = precision - length;
303 else if (length < width && zero)
304 zeropad = width - length;
306 pad = width - length -
307 zeropad - strlen(head);
312 count += strlen(head) + strlen(buf) + pad +
315 while (pad > 0 && size > 1) {
322 while (*cp != '\0' && size > 1) {
326 while (zeropad > 0 && size > 1) {
332 while (*cp != '\0' && size > 1) {
336 while (pad > 0 && size > 1) {
347 cp = va_arg(ap, char *);
350 if (precision != 0) {
352 * cp need not be NULL terminated.
359 while (n != 0 && *tp != '\0')
361 length = precision - n;
366 pad = width - length;
370 count += pad + length;
372 while (pad > 0 && size > 1) {
378 while (precision > 0 && *cp != '\0' &&
385 while (*cp != '\0' && size > 1) {
389 while (pad > 0 && size > 1) {
404 while (width-- > 0 && size > 1) {
408 if (!left && size > 1) {
421 v = va_arg(ap, void *);
422 sprintf(buf, "%p", v);
423 length = strlen(buf);
424 if (precision > length)
425 zeropad = precision - length;
427 pad = width - length - zeropad;
431 count += length + pad + zeropad;
433 while (pad > 0 && size > 1) {
439 if (zeropad > 0 && buf[0] == '0' &&
440 (buf[1] == 'x' || buf[1] == 'X')) {
449 while (zeropad > 0 && size > 1) {
455 while (*cp != '\0' && size > 1) {
459 while (pad > 0 && size > 1) {
465 case 'D': /*deprecated*/
466 INSIST("use %ld instead of %D" == NULL);
467 case 'O': /*deprecated*/
468 INSIST("use %lo instead of %O" == NULL);
469 case 'U': /*deprecated*/
470 INSIST("use %lu instead of %U" == NULL);
473 #ifdef HAVE_LONG_DOUBLE
476 INSIST("long doubles are not supported" == NULL);
487 * IEEE floating point.
488 * MIN 2.2250738585072014E-308
489 * MAX 1.7976931348623157E+308
490 * VAX floating point has a smaller range than IEEE.
492 * precisions > 324 don't make much sense.
493 * if we cap the precision at 512 we will not
498 sprintf(fmt, "%%%s%s.%lu%s%c", alt ? "#" : "",
499 plus ? "+" : space ? " " : "",
500 precision, l ? "L" : "", *format);
507 #ifdef HAVE_LONG_DOUBLE
509 ldbl = va_arg(ap, long double);
510 sprintf(buf, fmt, ldbl);
514 dbl = va_arg(ap, double);
515 sprintf(buf, fmt, dbl);
517 length = strlen(buf);
519 pad = width - length;
523 count += length + pad;
525 while (pad > 0 && size > 1) {
531 while (*cp != ' ' && size > 1) {
535 while (pad > 0 && size > 1) {