print.c revision f95231835fdcd8d5f58486c9f9993602f27fe157
/*
* Copyright (C) 1999-2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: print.c,v 1.25 2003/06/24 06:23:58 marka Exp $ */
#include <config.h>
#include <ctype.h>
#include <stdio.h> /* for sprintf */
#define ISC__PRINT_SOURCE /* Used to get the isc_print_* prototypes. */
#include <isc/assertions.h>
int
}
/*
* Return length of string that would have been written if not truncated.
*/
int
int ret;
return (ret);
}
/*
* Return length of string that would have been written if not truncated.
*/
int
int h;
int l;
int q;
int alt;
int zero;
int left;
int plus;
int space;
int neg;
unsigned long width;
unsigned long precision;
unsigned int length;
char buf[1024];
char c;
void *v;
const char *cp;
const char *head;
int count = 0;
int pad;
int zeropad;
int dot;
double dbl;
#ifdef HAVE_LONG_DOUBLE
long double ldbl;
#endif
char fmt[32];
while (*format != '\0') {
if (*format != '%') {
if (size > 1) {
size--;
}
count++;
format++;
continue;
}
format++;
/*
* Reset flags.
*/
head = "";
do {
if (*format == '#') {
alt = 1;
format++;
} else if (*format == '-') {
left = 1;
zero = 0;
format++;
} else if (*format == ' ') {
if (!plus)
space = 1;
format++;
} else if (*format == '+') {
plus = 1;
space = 0;
format++;
} else if (*format == '0') {
if (!left)
zero = 1;
format++;
} else
break;
} while (1);
/*
* Width.
*/
if (*format == '*') {
format++;
char *e;
format = e;
}
/*
* Precision.
*/
if (*format == '.') {
format++;
dot = 1;
if (*format == '*') {
format++;
char *e;
format = e;
}
}
switch (*format) {
case '\0':
continue;
case '%':
if (size > 1) {
size--;
}
format++;
count++;
break;
case 'q':
q = 1;
format++;
goto doint;
case 'h':
h = 1;
format++;
goto doint;
case 'l':
l = 1;
format++;
if (*format == 'l') {
q = 1;
format++;
}
goto doint;
case 'n':
case 'i':
case 'd':
case 'o':
case 'u':
case 'x':
case 'X':
if (precision != 0)
zero = 0;
switch (*format) {
case 'n':
if (h) {
short int *p;
} else if (l) {
long int *p;
} else {
int *p;
}
break;
case 'i':
case 'd':
if (q)
else if (l)
else
if (tmpi < 0) {
head = "-";
} else {
if (plus)
head = "+";
else if (space)
head = " ";
else
head = "";
}
tmpui);
goto printint;
case 'o':
if (q)
else if (l)
else
tmpui);
goto printint;
case 'u':
if (q)
else if (l)
else
tmpui);
goto printint;
case 'x':
if (q)
else if (l)
else
if (alt) {
head = "0x";
if (precision > 2)
precision -= 2;
}
tmpui);
goto printint;
case 'X':
if (q)
else if (l)
else
if (alt) {
head = "0X";
if (precision > 2)
precision -= 2;
}
tmpui);
goto printint;
if (width != 0) {
if (pad < 0)
pad = 0;
}
}
if (!left) {
*str++ = ' ';
size--;
pad--;
}
}
size--;
}
*str++ = '0';
size--;
zeropad--;
}
size--;
}
*str++ = ' ';
size--;
pad--;
}
break;
default:
break;
}
break;
case 's':
if (precision != 0) {
/*
* cp need not be NULL terminated.
*/
const char *tp;
unsigned long n;
n = precision;
while (n != 0 && *tp != '\0')
n--, tp++;
} else {
}
if (width != 0) {
if (pad < 0)
pad = 0;
}
if (!left)
*str++ = ' ';
size--;
pad--;
}
if (precision != 0)
size > 1) {
size--;
precision--;
}
else
size--;
}
*str++ = ' ';
size--;
pad--;
}
break;
case 'c':
if (width > 0) {
width--;
if (left) {
*str++ = c;
size--;
}
*str++ = ' ';
size--;
}
*str++ = c;
size--;
}
} else {
count++;
if (size > 1) {
*str++ = c;
size--;
}
}
break;
case 'p':
if (width > 0) {
if (pad < 0)
pad = 0;
}
if (!left)
*str++ = ' ';
size--;
pad--;
}
if (size > 1) {
size--;
}
if (size > 1) {
size--;
}
*str++ = '0';
size--;
zeropad--;
}
}
size--;
}
*str++ = ' ';
size--;
pad--;
}
break;
case 'D': /*deprecated*/
case 'O': /*deprecated*/
case 'U': /*deprecated*/
case 'L':
#ifdef HAVE_LONG_DOUBLE
l = 1;
#else
#endif
/*FALLTHROUGH*/
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
if (!dot)
precision = 6;
/*
* IEEE floating point.
* MIN 2.2250738585072014E-308
* MAX 1.7976931348623157E+308
* VAX floating point has a smaller range than IEEE.
*
* precisions > 324 don't make much sense.
* if we cap the precision at 512 we will not
* overflow buf.
*/
if (precision > 512)
precision = 512;
switch (*format) {
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
#ifdef HAVE_LONG_DOUBLE
if (l) {
} else
#endif
{
}
if (width > 0) {
if (pad < 0)
pad = 0;
}
if (!left)
*str++ = ' ';
size--;
pad--;
}
size--;
}
*str++ = ' ';
size--;
pad--;
}
break;
default:
continue;
}
break;
default:
continue;
}
format++;
}
if (size > 0)
*str = '\0';
return (count);
}