2
* This version of snprintf() and vsnprintf() is based on Sprint from
3
* SIO by Panagiotis Tsirigotis, as included with xidentd-2.2.1.
5
* The modifications were made by The XFree86 Project, Inc and are
6
* Copyright 1999 by The XFree86 Project, Inc. These modifications
7
* consist of removing the support for writing to file streams,
8
* renaming some functions, ansification, and making some adjustments
9
* to achieve the semantics for snprintf and vsnprintf() as described
10
* in the relevant man page for FreeBSD 2.2.8.
13
* The original version carries the following notice:
15
* (c) Copyright 1992, 1993 by Panagiotis Tsirigotis
17
* The author (Panagiotis Tsirigotis) grants permission to use, copy,
18
* and distribute this software and its documentation for any purpose
19
* and without fee, provided that a) the above copyright notice extant in
20
* files in this distribution is not removed from files included in any
21
* redistribution, and b) this file is also included in any redistribution.
23
* Modifications to this software may be distributed, either by distributing
24
* the modified software or by distributing patches to the original software,
25
* under the following additional terms:
27
* 1. The version number will be modified as follows:
28
* a. The first 3 components of the version number
29
* (i.e. <number>.<number>.<number>) will remain unchanged.
30
* b. A new component will be appended to the version number to indicate
31
* the modification level. The form of this component is up to the
32
* author of the modifications.
34
* 2. The author of the modifications will include his/her name by appending
35
* it along with the new version number to this file and will be
36
* responsible for any wrong behavior of the modified software.
38
* The author makes no representations about the suitability of this
39
* software for any purpose. It is provided "as is" without any express
40
* or implied warranty.
42
* Changes and modifications for:
44
* xinetd Version 2.1.4-bsdi
45
* xinetd Version 2.1.4-freebsd
46
* xinetd Version 2.1.4-linux
50
* (c) Copyright 1995 by Charles Murcko
54
/* $XFree86: xc/lib/misc/snprintf.c,v 3.1 1999/04/28 15:04:51 dawes Exp $ */
58
* Assumption: systems that don't have snprintf and vsnprintf do have
59
* ecvt, fcvt and gcvt.
62
/* From: Id: sprint.c,v 1.5 1995/09/10 18:35:09 chuck Exp */
68
#include <sys/types.h>
78
typedef WIDE_INT wide_int;
79
typedef unsigned WIDE_INT u_wide_int;
89
#define S_NULL "(null)"
92
#define FLOAT_DIGITS 6
93
#define EXPONENT_LENGTH 10
95
typedef enum { NO = 0, YES = 1 } boolean_e ;
98
* NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions
100
* XXX: this is a magic number; do not decrease it
102
#define NUM_BUF_SIZE 512
105
* The INS_CHAR macro inserts a character in the buffer.
106
* It uses the char pointers sp and bep:
107
* sp points to the next available character in the buffer
108
* bep points to the end-of-buffer
109
* While using this macro, note that the nextb pointer is NOT updated.
111
* Excess characters are discarded if the string overflows.
113
* NOTE: Evaluation of the c argument should not have any side-effects
115
#define INS_CHAR( c, sp, bep, cc ) \
122
#define NUM( c ) ( c - '0' )
124
#define STR_TO_DEC( str, num ) \
125
num = NUM( *str++ ) ; \
126
while ( isdigit( *str ) ) { \
128
num += NUM( *str++ ) ; \
132
* This macro does zero padding so that the precision
133
* requirement is satisfied. The padding is done by
134
* adding '0's to the left of the string that is going
137
#define FIX_PRECISION( adjust, precision, s, s_len ) \
139
while ( s_len < precision ) { \
145
* Macro that does padding. The padding is done by printing
148
#define PAD( width, len, ch ) \
150
INS_CHAR( ch, sp, bep, cc ) ; \
152
} while ( width > len )
155
* Prefix the character ch to the string str
157
* Set the has_prefix flag
159
#define PREFIX( str, length, ch ) \
160
*--str = ch ; length++ ; has_prefix = YES
162
static char *conv_10(wide_int num, bool_int is_unsigned,
163
bool_int * is_negative, char *buf_end, int *len);
164
SCOPE int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
167
* snprintf is based on SIO's Sprint.
169
* Sprint is the equivalent of printf for SIO.
170
* It returns the # of chars written
172
* - all floating point arguments are passed as doubles
175
snprintf(char *str, size_t size, const char *fmt, ...)
181
cc = vsnprintf(str, size, fmt, ap);
187
* Convert a floating point number to a string formats 'f', 'e' or 'E'.
188
* The result is placed in buf, and len denotes the length of the string
189
* The sign is returned in the is_negative argument (and is not placed
190
* in buf). Always add decimal point if add_dp is YES.
193
conv_fp(char format, double num, boolean_e add_dp, int precision,
194
bool_int *is_negative, char buf[], int *len)
201
p = fcvt(num, precision, &decimal_point, is_negative);
202
else /* either e or E format */
203
p = ecvt(num, precision + 1, &decimal_point, is_negative);
206
* Check for Infinity and NaN
209
*len = strlen(strcpy(buf, p));
210
*is_negative = FALSE;
214
if (decimal_point <= 0) {
218
while (decimal_point++ < 0)
223
while (decimal_point-- > 0)
225
if (precision > 0 || add_dp)
229
if (precision > 0 || add_dp)
234
* copy the rest of p, the NUL is NOT copied
240
char temp[EXPONENT_LENGTH]; /* for exponent conversion */
242
bool_int exponent_is_negative;
244
*s++ = format; /* either e or E */
246
if (decimal_point != 0) {
247
p = conv_10((wide_int) decimal_point, FALSE, &exponent_is_negative,
248
&temp[EXPONENT_LENGTH], &t_len);
249
*s++ = exponent_is_negative ? '-' : '+';
252
* Make sure the exponent has at least 2 digits
269
* Convert num to a base X number where X is a power of 2. nbits determines X.
270
* For example, if nbits is 3, we do base 8 conversion
272
* a pointer to a string containing the number
274
* The caller provides a buffer for the string: that is the buf_end argument
275
* which is a pointer to the END of the buffer + 1 (i.e. if the buffer
276
* is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
279
conv_p2(u_wide_int num, int nbits, char format, char *buf_end, int *len)
281
int mask = (1 << nbits) - 1;
283
static char low_digits[] = "0123456789abcdef";
284
static char upper_digits[] = "0123456789ABCDEF";
285
char *digits = (format == 'X') ? upper_digits : low_digits;
288
*--p = digits[num & mask];
298
* Convert num to its decimal format.
300
* - a pointer to a string containing the number (no sign)
301
* - len contains the length of the string
302
* - is_negative is set to TRUE or FALSE depending on the sign
303
* of the number (always set to FALSE if is_unsigned is TRUE)
305
* The caller provides a buffer for the string: that is the buf_end argument
306
* which is a pointer to the END of the buffer + 1 (i.e. if the buffer
307
* is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
310
conv_10(wide_int num, bool_int is_unsigned, bool_int *is_negative,
311
char *buf_end, int *len)
314
u_wide_int magnitude;
317
magnitude = (u_wide_int) num;
318
*is_negative = FALSE;
320
*is_negative = (num < 0);
323
* On a 2's complement machine, negating the most negative integer
324
* results in a number that cannot be represented as a signed integer.
325
* Here is what we do to obtain the number's magnitude:
326
* a. add 1 to the number
327
* b. negate it (becomes positive)
328
* c. convert it to unsigned
332
wide_int t = num + 1;
334
magnitude = ((u_wide_int) - t) + 1;
336
magnitude = (u_wide_int) num;
340
* We use a do-while loop so that we write at least 1 digit
343
register u_wide_int new_magnitude = magnitude / 10;
345
*--p = magnitude - new_magnitude * 10 + '0';
346
magnitude = new_magnitude;
355
* Do format conversion.
358
vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
381
char num_buf[NUM_BUF_SIZE];
382
char char_buf[2]; /* for printing %% and %<unknown> */
388
boolean_e alternate_form;
389
boolean_e print_sign;
390
boolean_e print_blank;
391
boolean_e adjust_precision;
392
boolean_e adjust_width;
393
bool_int is_negative;
399
bep = str + size - 1;
403
INS_CHAR(*fmt, sp, bep, cc);
406
* Default variable settings
409
alternate_form = print_sign = print_blank = NO;
416
* Try to avoid checking for flags, width or precision
418
if (isascii(*fmt) && !islower(*fmt)) {
420
* Recognize flags: -, #, BLANK, +
425
else if (*fmt == '+')
427
else if (*fmt == '#')
428
alternate_form = YES;
429
else if (*fmt == ' ')
431
else if (*fmt == '0')
438
* Check if a width was specified
441
STR_TO_DEC(fmt, min_width);
443
} else if (*fmt == '*') {
444
min_width = va_arg(ap, int);
450
min_width = -min_width;
456
* Check if a precision was specified
458
* XXX: an unreasonable amount of precision may be specified
459
* resulting in overflow of num_buf. Currently we
460
* ignore this possibility.
463
adjust_precision = YES;
466
STR_TO_DEC(fmt, precision);
467
} else if (*fmt == '*') {
468
precision = va_arg(ap, int);
476
adjust_precision = NO;
478
adjust_precision = adjust_width = NO;
490
* Argument extraction and printing.
491
* First we determine the argument type.
492
* Then, we convert the argument to a string.
493
* On exit from the switch, s points to the string that
494
* must be printed, s_len has the length of the string
495
* The precision requirements, if any, are reflected in s_len.
497
* NOTE: pad_char may be set to '0' because of the 0 flag.
498
* It is reset to ' ' by non-numeric formats
505
i_num = va_arg(ap, wide_int);
507
i_num = (wide_int) va_arg(ap, int);
509
s = conv_10(i_num, (*fmt) == 'u', &is_negative,
510
&num_buf[NUM_BUF_SIZE], &s_len);
511
FIX_PRECISION(adjust_precision, precision, s, s_len);
518
else if (print_blank)
525
ui_num = va_arg(ap, u_wide_int);
527
ui_num = (u_wide_int) va_arg(ap, unsigned int);
529
s = conv_p2(ui_num, 3, *fmt,
530
&num_buf[NUM_BUF_SIZE], &s_len);
531
FIX_PRECISION(adjust_precision, precision, s, s_len);
532
if (alternate_form && *s != '0') {
541
ui_num = (u_wide_int) va_arg(ap, u_wide_int);
543
ui_num = (u_wide_int) va_arg(ap, unsigned int);
545
s = conv_p2(ui_num, 4, *fmt,
546
&num_buf[NUM_BUF_SIZE], &s_len);
547
FIX_PRECISION(adjust_precision, precision, s, s_len);
548
if (alternate_form && i_num != 0) {
549
*--s = *fmt; /* 'x' or 'X' */
556
s = va_arg(ap, char *);
560
if (adjust_precision && precision < s_len)
572
fp_num = va_arg(ap, double);
574
s = conv_fp(*fmt, fp_num, alternate_form,
575
(adjust_precision == NO) ? FLOAT_DIGITS : precision,
576
&is_negative, &num_buf[1], &s_len);
581
else if (print_blank)
587
if (adjust_precision == NO)
588
precision = FLOAT_DIGITS;
589
else if (precision == 0)
592
* We use &num_buf[ 1 ], so that we have room for the sign
594
s = gcvt(va_arg(ap, double), precision, &num_buf[1]);
600
else if (print_blank)
605
if (alternate_form && (q = strchr(s, '.')) == NULL)
607
if (*fmt == 'G' && (q = strchr(s, 'e')) != NULL)
612
char_buf[0] = (char)(va_arg(ap, int));
627
*(va_arg(ap, int *)) = cc;
632
* If the pointer size is equal to the size of an unsigned
633
* integer we convert the pointer to a hex number, otherwise
634
* we print "%p" to indicate that we don't handle "%p".
637
ui_num = (u_wide_int) va_arg(ap, void *);
639
if (sizeof(void *) <= sizeof(u_wide_int))
640
s = conv_p2(ui_num, 4, 'x',
641
&num_buf[NUM_BUF_SIZE], &s_len);
652
* The last character of the format string was %.
658
* The default case is for unrecognized %'s.
659
* We print %<char> to help the user identify what
660
* option is not understood.
661
* This is also useful in case the user wants to pass
662
* the output of __sio_converter to another function
663
* that understands some other %<char> (like syslog).
664
* Note that we can't point s inside fmt because the
665
* unknown <char> could be preceded by width etc.
676
if (prefix_char != NUL) {
680
if (adjust_width && adjust == RIGHT && min_width > s_len) {
681
if (pad_char == '0' && prefix_char != NUL) {
682
INS_CHAR(*s, sp, bep, cc)
687
PAD(min_width, s_len, pad_char);
690
* Print the string s.
692
for (i = s_len; i != 0; i--) {
693
INS_CHAR(*s, sp, bep, cc);
697
if (adjust_width && adjust == LEFT && min_width > s_len)
698
PAD(min_width, s_len, pad_char);