print.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
/*
* Copyright (C) 1999-2001, 2003-2005, 2007, 2011, 2012, 2014-2016 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <config.h>
#include <ctype.h>
#include <stdio.h> /* for sprintf */
#include <string.h>
#define LWRES__PRINT_SOURCE /* Used to get the lwres_print_* prototypes. */
#include "assert_p.h"
#include "print_p.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 z;
int alt;
int zero;
int left;
int plus;
int space;
long long tmpi;
unsigned long long tmpui;
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 > 1U) {
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 > 1U) {
size--;
}
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 'z':
z = 1;
format++;
goto doint;
case 'n':
case 'i':
case 'd':
case 'o':
case 'u':
case 'x':
case 'X':
if (precision != 0U)
zero = 0;
switch (*format) {
case 'n':
if (h) {
short int *p;
} else if (l) {
long int *p;
} else if (z) {
size_t *p;
} else {
int *p;
}
break;
case 'i':
case 'd':
if (q)
else if (l)
else if (z)
else
if (tmpi < 0) {
head = "-";
} else {
if (plus)
head = "+";
else if (space)
head = " ";
else
head = "";
}
tmpui);
goto printint;
case 'o':
if (q)
unsigned long long int);
else if (l)
else if (z)
else
tmpui);
goto printint;
case 'u':
if (q)
unsigned long long int);
else if (l)
else if (z)
else
tmpui);
goto printint;
case 'x':
if (q)
unsigned long long int);
else if (l)
else if (z)
else
if (alt) {
head = "0x";
if (precision > 2U)
precision -= 2;
}
tmpui);
goto printint;
case 'X':
if (q)
unsigned long long int);
else if (l)
else if (z)
else
if (alt) {
head = "0X";
if (precision > 2U)
precision -= 2;
}
tmpui);
goto printint;
if (width != 0U) {
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 != 0U) {
/*
* cp need not be NULL terminated.
*/
const char *tp;
unsigned long n;
n = precision;
while (n != 0U && *tp != '\0')
n--, tp++;
} else {
}
if (width != 0U) {
if (pad < 0)
pad = 0;
}
if (!left)
*str++ = ' ';
size--;
pad--;
}
if (precision != 0U)
size > 1U) {
size--;
precision--;
}
else
size--;
}
*str++ = ' ';
size--;
pad--;
}
break;
case 'c':
if (width > 0U) {
width--;
if (left) {
*str++ = c;
size--;
}
*str++ = ' ';
size--;
}
*str++ = c;
size--;
}
} else {
count++;
if (size > 1U) {
*str++ = c;
size--;
}
}
break;
case 'p':
if (width > 0U) {
if (pad < 0)
pad = 0;
}
if (!left)
*str++ = ' ';
size--;
pad--;
}
if (size > 1U) {
size--;
}
if (size > 1U) {
size--;
}
*str++ = '0';
size--;
zeropad--;
}
}
size--;
}
*str++ = ' ';
size--;
pad--;
}
break;
case 'D': /*deprecated*/
break;
case 'O': /*deprecated*/
break;
case 'U': /*deprecated*/
break;
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 > 512U)
precision = 512;
switch (*format) {
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
#ifdef HAVE_LONG_DOUBLE
if (l) {
} else
#endif
{
}
if (width > 0U) {
if (pad < 0)
pad = 0;
}
if (!left)
*str++ = ' ';
size--;
pad--;
}
size--;
}
*str++ = ' ';
size--;
pad--;
}
break;
default:
continue;
}
break;
default:
continue;
}
format++;
}
if (size > 0U)
*str = '\0';
return (count);
}