|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* This version of snprintf() and vsnprintf() is based on Sprint from
|
|
Packit |
15a96c |
* SIO by Panagiotis Tsirigotis, as included with xidentd-2.2.1.
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* The modifications were made by The XFree86 Project, Inc and are
|
|
Packit |
15a96c |
* Copyright 1999 by The XFree86 Project, Inc. These modifications
|
|
Packit |
15a96c |
* consist of removing the support for writing to file streams,
|
|
Packit |
15a96c |
* renaming some functions, ansification, and making some adjustments
|
|
Packit |
15a96c |
* to achieve the semantics for snprintf and vsnprintf() as described
|
|
Packit |
15a96c |
* in the relevant man page for FreeBSD 2.2.8.
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* The original version carries the following notice:
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* (c) Copyright 1992, 1993 by Panagiotis Tsirigotis
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* The author (Panagiotis Tsirigotis) grants permission to use, copy,
|
|
Packit |
15a96c |
* and distribute this software and its documentation for any purpose
|
|
Packit |
15a96c |
* and without fee, provided that a) the above copyright notice extant in
|
|
Packit |
15a96c |
* files in this distribution is not removed from files included in any
|
|
Packit |
15a96c |
* redistribution, and b) this file is also included in any redistribution.
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* Modifications to this software may be distributed, either by distributing
|
|
Packit |
15a96c |
* the modified software or by distributing patches to the original software,
|
|
Packit |
15a96c |
* under the following additional terms:
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* 1. The version number will be modified as follows:
|
|
Packit |
15a96c |
* a. The first 3 components of the version number
|
|
Packit |
15a96c |
* (i.e. <number>.<number>.<number>) will remain unchanged.
|
|
Packit |
15a96c |
* b. A new component will be appended to the version number to indicate
|
|
Packit |
15a96c |
* the modification level. The form of this component is up to the
|
|
Packit |
15a96c |
* author of the modifications.
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* 2. The author of the modifications will include his/her name by appending
|
|
Packit |
15a96c |
* it along with the new version number to this file and will be
|
|
Packit |
15a96c |
* responsible for any wrong behavior of the modified software.
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* The author makes no representations about the suitability of this
|
|
Packit |
15a96c |
* software for any purpose. It is provided "as is" without any express
|
|
Packit |
15a96c |
* or implied warranty.
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* Changes and modifications for:
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* xinetd Version 2.1.4-bsdi
|
|
Packit |
15a96c |
* xinetd Version 2.1.4-freebsd
|
|
Packit |
15a96c |
* xinetd Version 2.1.4-linux
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* are
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* (c) Copyright 1995 by Charles Murcko
|
|
Packit |
15a96c |
* All Rights Reserved
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/* $XFree86: xc/lib/misc/snprintf.c,v 3.1 1999/04/28 15:04:51 dawes Exp $ */
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Assumption: systems that don't have snprintf and vsnprintf do have
|
|
Packit |
15a96c |
* ecvt, fcvt and gcvt.
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/* From: Id: sprint.c,v 1.5 1995/09/10 18:35:09 chuck Exp */
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#ifdef HAVE_CONFIG_H
|
|
Packit |
15a96c |
# include <config.h>
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#ifndef HAVE_STDARG_H
|
|
Packit |
15a96c |
#error Need stdarg here!
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#include <ctype.h>
|
|
Packit |
15a96c |
#include <stdlib.h>
|
|
Packit |
15a96c |
#include <stdarg.h>
|
|
Packit |
15a96c |
#include <string.h>
|
|
Packit |
15a96c |
#include <sys/types.h>
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#ifndef SCOPE
|
|
Packit |
15a96c |
#define SCOPE /**/
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#ifndef WIDE_INT
|
|
Packit |
15a96c |
#define WIDE_INT long
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
typedef WIDE_INT wide_int;
|
|
Packit |
15a96c |
typedef unsigned WIDE_INT u_wide_int;
|
|
Packit |
15a96c |
typedef int bool_int;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#ifndef FALSE
|
|
Packit |
15a96c |
#define FALSE 0
|
|
Packit |
15a96c |
#define TRUE 1
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#define NUL '\0'
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#define S_NULL "(null)"
|
|
Packit |
15a96c |
#define S_NULL_LEN 6
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#define FLOAT_DIGITS 6
|
|
Packit |
15a96c |
#define EXPONENT_LENGTH 10
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
typedef enum { NO = 0, YES = 1 } boolean_e ;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* XXX: this is a magic number; do not decrease it
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
#define NUM_BUF_SIZE 512
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* The INS_CHAR macro inserts a character in the buffer.
|
|
Packit |
15a96c |
* It uses the char pointers sp and bep:
|
|
Packit |
15a96c |
* sp points to the next available character in the buffer
|
|
Packit |
15a96c |
* bep points to the end-of-buffer
|
|
Packit |
15a96c |
* While using this macro, note that the nextb pointer is NOT updated.
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* Excess characters are discarded if the string overflows.
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* NOTE: Evaluation of the c argument should not have any side-effects
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
#define INS_CHAR( c, sp, bep, cc ) \
|
|
Packit |
15a96c |
{ \
|
|
Packit |
15a96c |
if ( sp < bep ) \
|
|
Packit |
15a96c |
*sp++ = c ; \
|
|
Packit |
15a96c |
cc++ ; \
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#define NUM( c ) ( c - '0' )
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#define STR_TO_DEC( str, num ) \
|
|
Packit |
15a96c |
num = NUM( *str++ ) ; \
|
|
Packit |
15a96c |
while ( isdigit( *str ) ) { \
|
|
Packit |
15a96c |
num *= 10 ; \
|
|
Packit |
15a96c |
num += NUM( *str++ ) ; \
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* This macro does zero padding so that the precision
|
|
Packit |
15a96c |
* requirement is satisfied. The padding is done by
|
|
Packit |
15a96c |
* adding '0's to the left of the string that is going
|
|
Packit |
15a96c |
* to be printed.
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
#define FIX_PRECISION( adjust, precision, s, s_len ) \
|
|
Packit |
15a96c |
if ( adjust ) \
|
|
Packit |
15a96c |
while ( s_len < precision ) { \
|
|
Packit |
15a96c |
*--s = '0' ; \
|
|
Packit |
15a96c |
s_len++ ; \
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Macro that does padding. The padding is done by printing
|
|
Packit |
15a96c |
* the character ch.
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
#define PAD( width, len, ch ) \
|
|
Packit |
15a96c |
do { \
|
|
Packit |
15a96c |
INS_CHAR( ch, sp, bep, cc ) ; \
|
|
Packit |
15a96c |
width-- ; \
|
|
Packit |
15a96c |
} while ( width > len )
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Prefix the character ch to the string str
|
|
Packit |
15a96c |
* Increase length
|
|
Packit |
15a96c |
* Set the has_prefix flag
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
#define PREFIX( str, length, ch ) \
|
|
Packit |
15a96c |
*--str = ch ; length++ ; has_prefix = YES
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
static char *conv_10(wide_int num, bool_int is_unsigned,
|
|
Packit |
15a96c |
bool_int * is_negative, char *buf_end, int *len);
|
|
Packit |
15a96c |
SCOPE int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* snprintf is based on SIO's Sprint.
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* Sprint is the equivalent of printf for SIO.
|
|
Packit |
15a96c |
* It returns the # of chars written
|
|
Packit |
15a96c |
* Assumptions:
|
|
Packit |
15a96c |
* - all floating point arguments are passed as doubles
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
SCOPE int
|
|
Packit |
15a96c |
snprintf(char *str, size_t size, const char *fmt, ...)
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
int cc;
|
|
Packit |
15a96c |
va_list ap;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
va_start(ap, fmt);
|
|
Packit |
15a96c |
cc = vsnprintf(str, size, fmt, ap);
|
|
Packit |
15a96c |
va_end(ap);
|
|
Packit |
15a96c |
return cc;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Convert a floating point number to a string formats 'f', 'e' or 'E'.
|
|
Packit |
15a96c |
* The result is placed in buf, and len denotes the length of the string
|
|
Packit |
15a96c |
* The sign is returned in the is_negative argument (and is not placed
|
|
Packit |
15a96c |
* in buf). Always add decimal point if add_dp is YES.
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
static char *
|
|
Packit |
15a96c |
conv_fp(char format, double num, boolean_e add_dp, int precision,
|
|
Packit |
15a96c |
bool_int *is_negative, char buf[], int *len)
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
char *s = buf;
|
|
Packit |
15a96c |
char *p;
|
|
Packit |
15a96c |
int decimal_point;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (format == 'f')
|
|
Packit |
15a96c |
p = fcvt(num, precision, &decimal_point, is_negative);
|
|
Packit |
15a96c |
else /* either e or E format */
|
|
Packit |
15a96c |
p = ecvt(num, precision + 1, &decimal_point, is_negative);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Check for Infinity and NaN
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
if (isalpha(*p)) {
|
|
Packit |
15a96c |
*len = strlen(strcpy(buf, p));
|
|
Packit |
15a96c |
*is_negative = FALSE;
|
|
Packit |
15a96c |
return (buf);
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
if (format == 'f')
|
|
Packit |
15a96c |
if (decimal_point <= 0) {
|
|
Packit |
15a96c |
*s++ = '0';
|
|
Packit |
15a96c |
if (precision > 0) {
|
|
Packit |
15a96c |
*s++ = '.';
|
|
Packit |
15a96c |
while (decimal_point++ < 0)
|
|
Packit |
15a96c |
*s++ = '0';
|
|
Packit |
15a96c |
} else if (add_dp)
|
|
Packit |
15a96c |
*s++ = '.';
|
|
Packit |
15a96c |
} else {
|
|
Packit |
15a96c |
while (decimal_point-- > 0)
|
|
Packit |
15a96c |
*s++ = *p++;
|
|
Packit |
15a96c |
if (precision > 0 || add_dp)
|
|
Packit |
15a96c |
*s++ = '.';
|
|
Packit |
15a96c |
} else {
|
|
Packit |
15a96c |
*s++ = *p++;
|
|
Packit |
15a96c |
if (precision > 0 || add_dp)
|
|
Packit |
15a96c |
*s++ = '.';
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* copy the rest of p, the NUL is NOT copied
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
while (*p)
|
|
Packit |
15a96c |
*s++ = *p++;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (format != 'f') {
|
|
Packit |
15a96c |
char temp[EXPONENT_LENGTH]; /* for exponent conversion */
|
|
Packit |
15a96c |
int t_len;
|
|
Packit |
15a96c |
bool_int exponent_is_negative;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
*s++ = format; /* either e or E */
|
|
Packit |
15a96c |
decimal_point--;
|
|
Packit |
15a96c |
if (decimal_point != 0) {
|
|
Packit |
15a96c |
p = conv_10((wide_int) decimal_point, FALSE, &exponent_is_negative,
|
|
Packit |
15a96c |
&temp[EXPONENT_LENGTH], &t_len);
|
|
Packit |
15a96c |
*s++ = exponent_is_negative ? '-' : '+';
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Make sure the exponent has at least 2 digits
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
if (t_len == 1)
|
|
Packit |
15a96c |
*s++ = '0';
|
|
Packit |
15a96c |
while (t_len--)
|
|
Packit |
15a96c |
*s++ = *p++;
|
|
Packit |
15a96c |
} else {
|
|
Packit |
15a96c |
*s++ = '+';
|
|
Packit |
15a96c |
*s++ = '0';
|
|
Packit |
15a96c |
*s++ = '0';
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
*len = s - buf;
|
|
Packit |
15a96c |
return (buf);
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Convert num to a base X number where X is a power of 2. nbits determines X.
|
|
Packit |
15a96c |
* For example, if nbits is 3, we do base 8 conversion
|
|
Packit |
15a96c |
* Return value:
|
|
Packit |
15a96c |
* a pointer to a string containing the number
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* The caller provides a buffer for the string: that is the buf_end argument
|
|
Packit |
15a96c |
* which is a pointer to the END of the buffer + 1 (i.e. if the buffer
|
|
Packit |
15a96c |
* is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
static char *
|
|
Packit |
15a96c |
conv_p2(u_wide_int num, int nbits, char format, char *buf_end, int *len)
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
int mask = (1 << nbits) - 1;
|
|
Packit |
15a96c |
char *p = buf_end;
|
|
Packit |
15a96c |
static char low_digits[] = "0123456789abcdef";
|
|
Packit |
15a96c |
static char upper_digits[] = "0123456789ABCDEF";
|
|
Packit |
15a96c |
char *digits = (format == 'X') ? upper_digits : low_digits;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
do {
|
|
Packit |
15a96c |
*--p = digits[num & mask];
|
|
Packit |
15a96c |
num >>= nbits;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
while (num);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
*len = buf_end - p;
|
|
Packit |
15a96c |
return (p);
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Convert num to its decimal format.
|
|
Packit |
15a96c |
* Return value:
|
|
Packit |
15a96c |
* - a pointer to a string containing the number (no sign)
|
|
Packit |
15a96c |
* - len contains the length of the string
|
|
Packit |
15a96c |
* - is_negative is set to TRUE or FALSE depending on the sign
|
|
Packit |
15a96c |
* of the number (always set to FALSE if is_unsigned is TRUE)
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* The caller provides a buffer for the string: that is the buf_end argument
|
|
Packit |
15a96c |
* which is a pointer to the END of the buffer + 1 (i.e. if the buffer
|
|
Packit |
15a96c |
* is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
static char *
|
|
Packit |
15a96c |
conv_10(wide_int num, bool_int is_unsigned, bool_int *is_negative,
|
|
Packit |
15a96c |
char *buf_end, int *len)
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
char *p = buf_end;
|
|
Packit |
15a96c |
u_wide_int magnitude;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (is_unsigned) {
|
|
Packit |
15a96c |
magnitude = (u_wide_int) num;
|
|
Packit |
15a96c |
*is_negative = FALSE;
|
|
Packit |
15a96c |
} else {
|
|
Packit |
15a96c |
*is_negative = (num < 0);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* On a 2's complement machine, negating the most negative integer
|
|
Packit |
15a96c |
* results in a number that cannot be represented as a signed integer.
|
|
Packit |
15a96c |
* Here is what we do to obtain the number's magnitude:
|
|
Packit |
15a96c |
* a. add 1 to the number
|
|
Packit |
15a96c |
* b. negate it (becomes positive)
|
|
Packit |
15a96c |
* c. convert it to unsigned
|
|
Packit |
15a96c |
* d. add 1
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
if (*is_negative) {
|
|
Packit |
15a96c |
wide_int t = num + 1;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
magnitude = ((u_wide_int) - t) + 1;
|
|
Packit |
15a96c |
} else
|
|
Packit |
15a96c |
magnitude = (u_wide_int) num;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* We use a do-while loop so that we write at least 1 digit
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
do {
|
|
Packit |
15a96c |
register u_wide_int new_magnitude = magnitude / 10;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
*--p = magnitude - new_magnitude * 10 + '0';
|
|
Packit |
15a96c |
magnitude = new_magnitude;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
while (magnitude);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
*len = buf_end - p;
|
|
Packit |
15a96c |
return (p);
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Do format conversion.
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
SCOPE int
|
|
Packit |
15a96c |
vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
char *sp;
|
|
Packit |
15a96c |
char *bep;
|
|
Packit |
15a96c |
int cc = 0;
|
|
Packit |
15a96c |
int i;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
char *s;
|
|
Packit |
15a96c |
char *q;
|
|
Packit |
15a96c |
int s_len;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
int min_width;
|
|
Packit |
15a96c |
int precision;
|
|
Packit |
15a96c |
enum {
|
|
Packit |
15a96c |
LEFT, RIGHT
|
|
Packit |
15a96c |
} adjust;
|
|
Packit |
15a96c |
char pad_char;
|
|
Packit |
15a96c |
char prefix_char;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
double fp_num;
|
|
Packit |
15a96c |
wide_int i_num;
|
|
Packit |
15a96c |
u_wide_int ui_num;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
char num_buf[NUM_BUF_SIZE];
|
|
Packit |
15a96c |
char char_buf[2]; /* for printing %% and %<unknown> */
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Flag variables
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
boolean_e is_long;
|
|
Packit |
15a96c |
boolean_e alternate_form;
|
|
Packit |
15a96c |
boolean_e print_sign;
|
|
Packit |
15a96c |
boolean_e print_blank;
|
|
Packit |
15a96c |
boolean_e adjust_precision;
|
|
Packit |
15a96c |
boolean_e adjust_width;
|
|
Packit |
15a96c |
bool_int is_negative;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (size == 0)
|
|
Packit |
15a96c |
return 0;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
sp = str;
|
|
Packit |
15a96c |
bep = str + size - 1;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
while (*fmt) {
|
|
Packit |
15a96c |
if (*fmt != '%') {
|
|
Packit |
15a96c |
INS_CHAR(*fmt, sp, bep, cc);
|
|
Packit |
15a96c |
} else {
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Default variable settings
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
adjust = RIGHT;
|
|
Packit |
15a96c |
alternate_form = print_sign = print_blank = NO;
|
|
Packit |
15a96c |
pad_char = ' ';
|
|
Packit |
15a96c |
prefix_char = NUL;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
fmt++;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Try to avoid checking for flags, width or precision
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
if (isascii(*fmt) && !islower(*fmt)) {
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Recognize flags: -, #, BLANK, +
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
for (;; fmt++) {
|
|
Packit |
15a96c |
if (*fmt == '-')
|
|
Packit |
15a96c |
adjust = LEFT;
|
|
Packit |
15a96c |
else if (*fmt == '+')
|
|
Packit |
15a96c |
print_sign = YES;
|
|
Packit |
15a96c |
else if (*fmt == '#')
|
|
Packit |
15a96c |
alternate_form = YES;
|
|
Packit |
15a96c |
else if (*fmt == ' ')
|
|
Packit |
15a96c |
print_blank = YES;
|
|
Packit |
15a96c |
else if (*fmt == '0')
|
|
Packit |
15a96c |
pad_char = '0';
|
|
Packit |
15a96c |
else
|
|
Packit |
15a96c |
break;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Check if a width was specified
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
if (isdigit(*fmt)) {
|
|
Packit |
15a96c |
STR_TO_DEC(fmt, min_width);
|
|
Packit |
15a96c |
adjust_width = YES;
|
|
Packit |
15a96c |
} else if (*fmt == '*') {
|
|
Packit |
15a96c |
min_width = va_arg(ap, int);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
fmt++;
|
|
Packit |
15a96c |
adjust_width = YES;
|
|
Packit |
15a96c |
if (min_width < 0) {
|
|
Packit |
15a96c |
adjust = LEFT;
|
|
Packit |
15a96c |
min_width = -min_width;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
} else
|
|
Packit |
15a96c |
adjust_width = NO;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Check if a precision was specified
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* XXX: an unreasonable amount of precision may be specified
|
|
Packit |
15a96c |
* resulting in overflow of num_buf. Currently we
|
|
Packit |
15a96c |
* ignore this possibility.
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
if (*fmt == '.') {
|
|
Packit |
15a96c |
adjust_precision = YES;
|
|
Packit |
15a96c |
fmt++;
|
|
Packit |
15a96c |
if (isdigit(*fmt)) {
|
|
Packit |
15a96c |
STR_TO_DEC(fmt, precision);
|
|
Packit |
15a96c |
} else if (*fmt == '*') {
|
|
Packit |
15a96c |
precision = va_arg(ap, int);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
fmt++;
|
|
Packit |
15a96c |
if (precision < 0)
|
|
Packit |
15a96c |
precision = 0;
|
|
Packit |
15a96c |
} else
|
|
Packit |
15a96c |
precision = 0;
|
|
Packit |
15a96c |
} else
|
|
Packit |
15a96c |
adjust_precision = NO;
|
|
Packit |
15a96c |
} else
|
|
Packit |
15a96c |
adjust_precision = adjust_width = NO;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Modifier check
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
if (*fmt == 'l') {
|
|
Packit |
15a96c |
is_long = YES;
|
|
Packit |
15a96c |
fmt++;
|
|
Packit |
15a96c |
} else
|
|
Packit |
15a96c |
is_long = NO;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Argument extraction and printing.
|
|
Packit |
15a96c |
* First we determine the argument type.
|
|
Packit |
15a96c |
* Then, we convert the argument to a string.
|
|
Packit |
15a96c |
* On exit from the switch, s points to the string that
|
|
Packit |
15a96c |
* must be printed, s_len has the length of the string
|
|
Packit |
15a96c |
* The precision requirements, if any, are reflected in s_len.
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* NOTE: pad_char may be set to '0' because of the 0 flag.
|
|
Packit |
15a96c |
* It is reset to ' ' by non-numeric formats
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
switch (*fmt) {
|
|
Packit |
15a96c |
case 'd':
|
|
Packit |
15a96c |
case 'i':
|
|
Packit |
15a96c |
case 'u':
|
|
Packit |
15a96c |
if (is_long)
|
|
Packit |
15a96c |
i_num = va_arg(ap, wide_int);
|
|
Packit |
15a96c |
else
|
|
Packit |
15a96c |
i_num = (wide_int) va_arg(ap, int);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
s = conv_10(i_num, (*fmt) == 'u', &is_negative,
|
|
Packit |
15a96c |
&num_buf[NUM_BUF_SIZE], &s_len);
|
|
Packit |
15a96c |
FIX_PRECISION(adjust_precision, precision, s, s_len);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (*fmt != 'u') {
|
|
Packit |
15a96c |
if (is_negative)
|
|
Packit |
15a96c |
prefix_char = '-';
|
|
Packit |
15a96c |
else if (print_sign)
|
|
Packit |
15a96c |
prefix_char = '+';
|
|
Packit |
15a96c |
else if (print_blank)
|
|
Packit |
15a96c |
prefix_char = ' ';
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
break;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
case 'o':
|
|
Packit |
15a96c |
if (is_long)
|
|
Packit |
15a96c |
ui_num = va_arg(ap, u_wide_int);
|
|
Packit |
15a96c |
else
|
|
Packit |
15a96c |
ui_num = (u_wide_int) va_arg(ap, unsigned int);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
s = conv_p2(ui_num, 3, *fmt,
|
|
Packit |
15a96c |
&num_buf[NUM_BUF_SIZE], &s_len);
|
|
Packit |
15a96c |
FIX_PRECISION(adjust_precision, precision, s, s_len);
|
|
Packit |
15a96c |
if (alternate_form && *s != '0') {
|
|
Packit |
15a96c |
*--s = '0';
|
|
Packit |
15a96c |
s_len++;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
break;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
case 'x':
|
|
Packit |
15a96c |
case 'X':
|
|
Packit |
15a96c |
if (is_long)
|
|
Packit |
15a96c |
ui_num = (u_wide_int) va_arg(ap, u_wide_int);
|
|
Packit |
15a96c |
else
|
|
Packit |
15a96c |
ui_num = (u_wide_int) va_arg(ap, unsigned int);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
s = conv_p2(ui_num, 4, *fmt,
|
|
Packit |
15a96c |
&num_buf[NUM_BUF_SIZE], &s_len);
|
|
Packit |
15a96c |
FIX_PRECISION(adjust_precision, precision, s, s_len);
|
|
Packit |
15a96c |
if (alternate_form && i_num != 0) {
|
|
Packit |
15a96c |
*--s = *fmt; /* 'x' or 'X' */
|
|
Packit |
15a96c |
*--s = '0';
|
|
Packit |
15a96c |
s_len += 2;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
break;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
case 's':
|
|
Packit |
15a96c |
s = va_arg(ap, char *);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (s != NULL) {
|
|
Packit |
15a96c |
s_len = strlen(s);
|
|
Packit |
15a96c |
if (adjust_precision && precision < s_len)
|
|
Packit |
15a96c |
s_len = precision;
|
|
Packit |
15a96c |
} else {
|
|
Packit |
15a96c |
s = S_NULL;
|
|
Packit |
15a96c |
s_len = S_NULL_LEN;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
pad_char = ' ';
|
|
Packit |
15a96c |
break;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
case 'f':
|
|
Packit |
15a96c |
case 'e':
|
|
Packit |
15a96c |
case 'E':
|
|
Packit |
15a96c |
fp_num = va_arg(ap, double);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
s = conv_fp(*fmt, fp_num, alternate_form,
|
|
Packit |
15a96c |
(adjust_precision == NO) ? FLOAT_DIGITS : precision,
|
|
Packit |
15a96c |
&is_negative, &num_buf[1], &s_len);
|
|
Packit |
15a96c |
if (is_negative)
|
|
Packit |
15a96c |
prefix_char = '-';
|
|
Packit |
15a96c |
else if (print_sign)
|
|
Packit |
15a96c |
prefix_char = '+';
|
|
Packit |
15a96c |
else if (print_blank)
|
|
Packit |
15a96c |
prefix_char = ' ';
|
|
Packit |
15a96c |
break;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
case 'g':
|
|
Packit |
15a96c |
case 'G':
|
|
Packit |
15a96c |
if (adjust_precision == NO)
|
|
Packit |
15a96c |
precision = FLOAT_DIGITS;
|
|
Packit |
15a96c |
else if (precision == 0)
|
|
Packit |
15a96c |
precision = 1;
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* We use &num_buf[ 1 ], so that we have room for the sign
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
s = gcvt(va_arg(ap, double), precision, &num_buf[1]);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (*s == '-')
|
|
Packit |
15a96c |
prefix_char = *s++;
|
|
Packit |
15a96c |
else if (print_sign)
|
|
Packit |
15a96c |
prefix_char = '+';
|
|
Packit |
15a96c |
else if (print_blank)
|
|
Packit |
15a96c |
prefix_char = ' ';
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
s_len = strlen(s);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (alternate_form && (q = strchr(s, '.')) == NULL)
|
|
Packit |
15a96c |
s[s_len++] = '.';
|
|
Packit |
15a96c |
if (*fmt == 'G' && (q = strchr(s, 'e')) != NULL)
|
|
Packit |
15a96c |
*q = 'E';
|
|
Packit |
15a96c |
break;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
case 'c':
|
|
Packit |
15a96c |
char_buf[0] = (char)(va_arg(ap, int));
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
s = &char_buf[0];
|
|
Packit |
15a96c |
s_len = 1;
|
|
Packit |
15a96c |
pad_char = ' ';
|
|
Packit |
15a96c |
break;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
case '%':
|
|
Packit |
15a96c |
char_buf[0] = '%';
|
|
Packit |
15a96c |
s = &char_buf[0];
|
|
Packit |
15a96c |
s_len = 1;
|
|
Packit |
15a96c |
pad_char = ' ';
|
|
Packit |
15a96c |
break;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
case 'n':
|
|
Packit |
15a96c |
*(va_arg(ap, int *)) = cc;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
break;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* If the pointer size is equal to the size of an unsigned
|
|
Packit |
15a96c |
* integer we convert the pointer to a hex number, otherwise
|
|
Packit |
15a96c |
* we print "%p" to indicate that we don't handle "%p".
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
case 'p':
|
|
Packit |
15a96c |
ui_num = (u_wide_int) va_arg(ap, void *);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (sizeof(void *) <= sizeof(u_wide_int))
|
|
Packit |
15a96c |
s = conv_p2(ui_num, 4, 'x',
|
|
Packit |
15a96c |
&num_buf[NUM_BUF_SIZE], &s_len);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
else {
|
|
Packit |
15a96c |
s = "%p";
|
|
Packit |
15a96c |
s_len = 2;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
pad_char = ' ';
|
|
Packit |
15a96c |
break;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
case NUL:
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* The last character of the format string was %.
|
|
Packit |
15a96c |
* We ignore it.
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
continue;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* The default case is for unrecognized %'s.
|
|
Packit |
15a96c |
* We print %<char> to help the user identify what
|
|
Packit |
15a96c |
* option is not understood.
|
|
Packit |
15a96c |
* This is also useful in case the user wants to pass
|
|
Packit |
15a96c |
* the output of __sio_converter to another function
|
|
Packit |
15a96c |
* that understands some other %<char> (like syslog).
|
|
Packit |
15a96c |
* Note that we can't point s inside fmt because the
|
|
Packit |
15a96c |
* unknown <char> could be preceded by width etc.
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
default:
|
|
Packit |
15a96c |
char_buf[0] = '%';
|
|
Packit |
15a96c |
char_buf[1] = *fmt;
|
|
Packit |
15a96c |
s = char_buf;
|
|
Packit |
15a96c |
s_len = 2;
|
|
Packit |
15a96c |
pad_char = ' ';
|
|
Packit |
15a96c |
break;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (prefix_char != NUL) {
|
|
Packit |
15a96c |
*--s = prefix_char;
|
|
Packit |
15a96c |
s_len++;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
if (adjust_width && adjust == RIGHT && min_width > s_len) {
|
|
Packit |
15a96c |
if (pad_char == '0' && prefix_char != NUL) {
|
|
Packit |
15a96c |
INS_CHAR(*s, sp, bep, cc)
|
|
Packit |
15a96c |
s++;
|
|
Packit |
15a96c |
s_len--;
|
|
Packit |
15a96c |
min_width--;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
PAD(min_width, s_len, pad_char);
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* Print the string s.
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
for (i = s_len; i != 0; i--) {
|
|
Packit |
15a96c |
INS_CHAR(*s, sp, bep, cc);
|
|
Packit |
15a96c |
s++;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (adjust_width && adjust == LEFT && min_width > s_len)
|
|
Packit |
15a96c |
PAD(min_width, s_len, pad_char);
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
fmt++;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
if (cc < size)
|
|
Packit |
15a96c |
str[cc] = NUL;
|
|
Packit |
15a96c |
else
|
|
Packit |
15a96c |
str[size - 1] = NUL;
|
|
Packit |
15a96c |
return cc;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|