25static char copyright[] =
26"$Id: iscprint.c,v 1.2 2005/03/17 20:30:41 dhankins Exp $ Copyright (c) 2004 Internet Systems Consortium, Inc. All rights reserved.";
29#define INSIST(cond) REQUIRE(cond)
30#define REQUIRE(cond) if (!(cond)) { return 0; }
37isc_print_snprintf(
char *str,
size_t size,
const char *format, ...) {
42 ret = vsnprintf(str, size, format, ap);
52isc_print_vsnprintf(
char *str,
size_t size,
const char *format, va_list ap) {
65 unsigned long precision;
78#ifdef HAVE_LONG_DOUBLE
84 INSIST(format != NULL);
86 while (*format !=
'\0') {
101 dot = neg =
space = plus = left = zero = alt = h = l = q = 0;
102 width = precision = 0;
104 length = pad = zeropad = 0;
107 if (*format ==
'#') {
110 }
else if (*format ==
'-') {
114 }
else if (*format ==
' ') {
118 }
else if (*format ==
'+') {
122 }
else if (*format ==
'0') {
133 if (*format ==
'*') {
134 width = va_arg(ap,
int);
136 }
else if (isdigit((
unsigned char)*format)) {
138 width = strtoul(format, &e, 10);
145 if (*format ==
'.') {
148 if (*format ==
'*') {
149 precision = va_arg(ap,
int);
151 }
else if (isdigit((
unsigned char)*format)) {
153 precision = strtoul(format, &e, 10);
179 if (*format ==
'l') {
198 p = va_arg(ap,
short *);
203 p = va_arg(ap,
long *);
208 p = va_arg(ap,
int *);
216 tmpi = va_arg(ap, isc_int64_t);
218 tmpi = va_arg(ap,
long int);
220 tmpi = va_arg(ap,
int);
233 sprintf(buf,
"%u", tmpui);
237 tmpui = va_arg(ap, isc_uint64_t);
239 tmpui = va_arg(ap,
long int);
241 tmpui = va_arg(ap,
int);
242 sprintf(buf, alt ?
"%#o"
247 tmpui = va_arg(ap, isc_uint64_t);
249 tmpui = va_arg(ap,
unsigned long int);
251 tmpui = va_arg(ap,
unsigned int);
252 sprintf(buf,
"%u", tmpui);
256 tmpui = va_arg(ap, isc_uint64_t);
258 tmpui = va_arg(ap,
unsigned long int);
260 tmpui = va_arg(ap,
unsigned int);
266 sprintf(buf,
"%x", tmpui);
270 tmpui = va_arg(ap, isc_uint64_t);
272 tmpui = va_arg(ap,
unsigned long int);
274 tmpui = va_arg(ap,
unsigned int);
280 sprintf(buf,
"%X", tmpui);
283 if (precision != 0 || width != 0) {
284 length = strlen(buf);
285 if (length < precision)
286 zeropad = precision - length;
287 else if (length < width && zero)
288 zeropad = width - length;
290 pad = width - length -
291 zeropad - strlen(head);
296 count += strlen(head) + strlen(buf) + pad +
299 while (pad > 0 && size > 1) {
306 while (*cp !=
'\0' && size > 1) {
310 while (zeropad > 0 && size > 1) {
316 while (*cp !=
'\0' && size > 1) {
320 while (pad > 0 && size > 1) {
331 cp = va_arg(ap,
char *);
334 if (precision != 0) {
343 while (n != 0 && *tp !=
'\0')
345 length = precision - n;
350 pad = width - length;
354 count += pad + length;
356 while (pad > 0 && size > 1) {
362 while (precision > 0 && *cp !=
'\0' &&
369 while (*cp !=
'\0' && size > 1) {
373 while (pad > 0 && size > 1) {
388 while (width-- > 0 && size > 1) {
392 if (!left && size > 1) {
405 v = va_arg(ap,
void *);
406 sprintf(buf,
"%p", v);
407 length = strlen(buf);
408 if (precision > length)
409 zeropad = precision - length;
411 pad = width - length - zeropad;
415 count += length + pad + zeropad;
417 while (pad > 0 && size > 1) {
423 if (zeropad > 0 && buf[0] ==
'0' &&
424 (buf[1] ==
'x' || buf[1] ==
'X')) {
433 while (zeropad > 0 && size > 1) {
439 while (*cp !=
'\0' && size > 1) {
443 while (pad > 0 && size > 1) {
450 INSIST(
"use %ld instead of %D" == NULL);
452 INSIST(
"use %lo instead of %O" == NULL);
454 INSIST(
"use %lu instead of %U" == NULL);
457#ifdef HAVE_LONG_DOUBLE
460 INSIST(
"long doubles are not supported" == NULL);
482 sprintf(fmt,
"%%%s%s.%lu%s%c", alt ?
"#" :
"",
483 plus ?
"+" :
space ?
" " :
"",
484 precision, l ?
"L" :
"", *format);
491#ifdef HAVE_LONG_DOUBLE
493 ldbl = va_arg(ap,
long double);
494 sprintf(buf, fmt, ldbl);
498 dbl = va_arg(ap,
double);
499 sprintf(buf, fmt, dbl);
501 length = strlen(buf);
503 pad = width - length;
507 count += length + pad;
509 while (pad > 0 && size > 1) {
515 while (*cp !=
' ' && size > 1) {
519 while (pad > 0 && size > 1) {