/* $NetBSD: vsnprintf_ss.c,v 1.2.2.1 2007/05/07 19:49:09 pavel Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <LibConfig.h>
#include <sys/EfiCdefs.h>
#if 0
static char sccsid[] = "@(#)vsnprintf.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: vsnprintf_ss.c,v 1.2.2.1 2007/05/07 19:49:09 pavel Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <inttypes.h>
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include "reentrant.h"
#include "extern.h"
#include "local.h"
#ifdef __weak_alias
#endif
/*
* vsnprintf_ss: scaled down version of printf(3).
*
* this version based on vfprintf() from libc which was derived from
* software contributed to Berkeley by Chris Torek.
*
*/
/*
* macros for converting digits to letters and vice versa
*/
/*
* flags used during conversion.
*/
/*
* To extend shorts properly, we need both signed and unsigned
* argument extraction methods.
*/
#define SARG() \
#define UARG() \
#define PUTCHAR(C) do { \
*sbuf++ = (C); \
} while (/*CONSTCOND*/0)
int
{
int n; /* handy integer (short term usage) */
size = 0; /* XXX: shutup gcc */
ret = 0;
/*
* Scan the format for conversions (`%' character).
*/
for (;;) {
ret++;
}
if (*fmt == 0)
goto done;
fmt++; /* skip over '%' */
flags = 0;
dprec = 0;
width = 0;
prec = -1;
sign = '\0';
case ' ':
/*
* ``If the space and + flags both appear, the space
* flag will be ignored.''
* -- ANSI X3J11
*/
if (!sign)
sign = ' ';
goto rflag;
case '#':
goto rflag;
case '*':
/*
* ``A negative field width argument is taken as a
* - flag followed by a positive field width.''
* -- ANSI X3J11
* They don't exclude field widths read from args.
*/
goto rflag;
/* FALLTHROUGH */
case '-':
goto rflag;
case '+':
sign = '+';
goto rflag;
case '.':
prec = n < 0 ? -1 : n;
goto rflag;
}
n = 0;
}
prec = n < 0 ? -1 : n;
goto reswitch;
case '0':
/*
* ``Note that 0 is taken as a flag, not as the
* beginning of a field width.''
* -- ANSI X3J11
*/
goto rflag;
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
n = 0;
do {
width = n;
goto reswitch;
case 'h':
goto rflag;
case 'j':
goto rflag;
case 'l':
if (*fmt == 'l') {
fmt++;
} else {
}
goto rflag;
case 'q':
goto rflag;
case 't':
goto rflag;
case 'z':
goto rflag;
case 'c':
size = 1;
sign = '\0';
break;
case 'D':
/*FALLTHROUGH*/
case 'd':
case 'i':
sign = '-';
}
goto number;
case 'n':
else
continue; /* no output */
case 'O':
/*FALLTHROUGH*/
case 'o':
goto nosign;
case 'p':
/*
* ``The argument shall be a pointer to void. The
* value of the pointer is converted to a sequence
* of printable characters, in an implementation-
* defined manner.''
* -- ANSI X3J11
*/
/* NOSTRICT */
xdigs = xdigs_lower;
ch = 'x';
goto nosign;
case 's':
/*XXXUNCONST*/
if (prec >= 0) {
/*
* can't use strlen; can only look for the
* NUL in the first `prec' characters, and
* strlen() will go further.
*/
if (p != NULL) {
} else
} else
sign = '\0';
break;
case 'U':
/*FALLTHROUGH*/
case 'u':
goto nosign;
case 'X':
xdigs = xdigs_upper;
goto hex;
case 'x':
xdigs = xdigs_lower;
/* leading 0x/X only if non-zero */
/* unsigned conversions */
/*
* ``... diouXx conversions ... if a precision is
* specified, the 0 flag will be ignored.''
* -- ANSI X3J11
*/
/*
* ``The result of converting a zero value with an
* explicit precision of zero is no characters.''
* -- ANSI X3J11
*/
/*
* Unsigned mod is hard, and unsigned mod
* by a constant is easier than that by
* a variable; hence this switch.
*/
switch (base) {
case OCT:
do {
_uquad >>= 3;
} while (_uquad);
/* handle octal leading 0 */
*--cp = '0';
break;
case DEC:
/* many numbers are 1 digit */
while (_uquad >= 10) {
_uquad /= 10;
}
break;
case HEX:
do {
_uquad >>= 4;
} while (_uquad);
break;
default:
/*XXXUNCONST*/
goto skipsize;
}
}
break;
default: /* "%?" prints ?, unless ? is NUL */
if (ch == '\0')
goto done;
/* pretend it was %c with argument ch */
size = 1;
sign = '\0';
break;
}
/*
* All reasonable formats wind up here. At this point, `cp'
* points to a string which (if not flags&LADJUST) should be
* padded out to `width' places. If flags&ZEROPAD, it should
* first be prefixed by any sign or other prefix; otherwise,
* it should be blank padded before the prefix is emitted.
* After any left-hand padding and prefixing, emit zeroes
* required by a decimal [diouxX] precision, then print the
* string proper, then emit zeroes required by any leftover
* floating precision; finally, if LADJUST, pad with blanks.
*
* Compute actual size, so we know how much to pad.
* size excludes decimal prec; realsz includes it.
*/
if (sign)
realsz++;
realsz+= 2;
/* adjust ret */
/* right-adjusting blank padding */
while (n-- > 0)
PUTCHAR(' ');
}
/* prefix */
if (sign) {
PUTCHAR('0');
}
/* right-adjusting zero padding */
while (n-- > 0)
PUTCHAR('0');
}
/* leading zeroes from decimal precision */
while (n-- > 0)
PUTCHAR('0');
/* the string or number proper */
while (size--)
/* left-adjusting padding (always blank) */
while (n-- > 0)
PUTCHAR(' ');
}
}
done:
else
*sbuf = '\0';
return (ret);
/* NOTREACHED */
}