/*
* Copyright (C) 1999-2001, 2003-2008, 2010, 2014-2017 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/.
*/
/*! \file */
#include <config.h>
#include <ctype.h>
#include <stdio.h> /* for sprintf() */
#include <string.h> /* for strlen() */
#include <assert.h> /* for assert() */
#include <isc/assertions.h>
/*
* We use the system's sprintf so we undef it here.
*/
static int
static void
int i = c & 0xff;
}
#if 0
static int
}
#endif
int
int n;
return (n);
}
int
int n;
return (n);
}
static void
*(a->str)++ = c;
}
int
int n;
return (n);
}
/*!
* 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.
*/
static void
if (p->size > 0U) {
*(p->str)++ = c;
p->size--;
}
}
int
int n;
return (n);
}
static int
{
int h;
int l;
int z;
int q;
int alt;
int zero;
int left;
int plus;
int space;
int neg;
unsigned long width;
unsigned long precision;
unsigned int length;
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
while (*format != '\0') {
if (*format != '%') {
count++;
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 '%':
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;
#ifdef WIN32
case 'I':
/* Windows has I64 as a modifier for a quad. */
q = 1;
format += 3;
goto doint;
}
continue;
#endif
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;
*p = count;
} else if (l) {
long int *p;
*p = count;
} else if (z) {
size_t *p;
*p = count;
} else {
int *p;
*p = count;
}
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 = "";
}
if (tmpui <= 0xffffffffU)
(unsigned long)tmpui);
else {
unsigned long mid;
unsigned long lo;
unsigned long hi;
tmpui /= 1000000000;
if (hi != 0U) {
"%09lu", mid);
} else
lo);
}
goto printint;
case 'o':
if (q)
else if (l)
else if (z)
else
if (tmpui <= 0xffffffffU)
(unsigned long)tmpui);
else {
unsigned long mid;
unsigned long lo;
unsigned long hi;
tmpui /= 010000000000;
if (hi != 0U) {
hi);
"%09lo", mid);
} else
mid);
}
goto printint;
case 'u':
if (q)
else if (l)
else if (z)
else
if (tmpui <= 0xffffffffU)
(unsigned long)tmpui);
else {
unsigned long mid;
unsigned long lo;
unsigned long hi;
tmpui /= 1000000000;
if (hi != 0U) {
"%09lu", mid);
} else
lo);
}
goto printint;
case 'x':
if (q)
else if (l)
else if (z)
else
if (alt) {
head = "0x";
if (precision > 2U)
precision -= 2;
}
if (tmpui <= 0xffffffffU)
(unsigned long)tmpui);
else {
}
goto printint;
case 'X':
if (q)
else if (l)
else if (z)
else
if (alt) {
head = "0X";
if (precision > 2U)
precision -= 2;
}
if (tmpui <= 0xffffffffU)
(unsigned long)tmpui);
else {
}
goto printint;
if (precision_set || width != 0U) {
if (width != 0U) {
if (pad < 0)
pad = 0;
}
}
if (!left) {
while (pad > 0) {
pad--;
}
}
while (*cp != '\0')
while (zeropad > 0) {
zeropad--;
}
while (*cp != '\0')
while (pad > 0) {
pad--;
}
break;
default:
break;
}
break;
case 's':
if (precision_set) {
/*
* cp need not be NULL terminated.
*/
const char *tp;
unsigned long n;
if (precision != 0U)
n = precision;
while (n != 0U && *tp != '\0')
n--, tp++;
} else {
}
if (width != 0U) {
if (pad < 0)
pad = 0;
}
if (!left)
while (pad > 0) {
pad--;
}
if (precision_set)
precision--;
}
else
while (*cp != '\0')
while (pad > 0) {
pad--;
}
break;
case 'c':
if (width > 0U) {
width--;
if (left)
while (width-- > 0U)
if (!left)
} else {
count++;
}
break;
case 'p':
if (width > 0U) {
if (pad < 0)
pad = 0;
}
if (!left)
while (pad > 0) {
pad--;
}
while (zeropad > 0) {
zeropad--;
}
}
while (*cp != '\0')
while (pad > 0) {
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 > 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)
while (pad > 0) {
pad--;
}
while (*cp != ' ')
while (pad > 0) {
pad--;
}
break;
default:
continue;
}
break;
default:
continue;
}
format++;
}
return (count);
}