1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
#include "apr_private.h"
21
#include "apr_strings.h"
22
#include "apr_network_io.h"
23
#include "apr_portable.h"
28
#if APR_HAVE_NETINET_IN_H
29
#include <netinet/in.h>
31
#if APR_HAVE_SYS_SOCKET_H
32
#include <sys/socket.h>
34
#if APR_HAVE_ARPA_INET_H
35
#include <arpa/inet.h>
57
typedef WIDE_INT wide_int;
58
typedef unsigned WIDE_INT u_wide_int;
59
typedef apr_int64_t widest_int;
61
/* Although Tandem supports "long long" there is no unsigned variant. */
62
typedef unsigned long u_widest_int;
64
typedef apr_uint64_t u_widest_int;
68
#define S_NULL "(null)"
71
#define FLOAT_DIGITS 6
72
#define EXPONENT_LENGTH 10
75
* NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions
77
* NOTICE: this is a magic number; do not decrease it
79
#define NUM_BUF_SIZE 512
82
* cvt.c - IEEE floating point formatting routines for FreeBSD
83
* from GNU libc-4.6.27. Modified to be thread safe.
87
* apr_ecvt converts to decimal
88
* the number of digits is specified by ndigit
89
* decpt is set to the position of the decimal point
90
* sign is set to 0 for positive, 1 for negative
95
/* buf must have at least NDIG bytes */
96
static char *apr_cvt(double arg, int ndigits, int *decpt, int *sign,
101
register char *p, *p1;
103
if (ndigits >= NDIG - 1)
112
arg = modf(arg, &fi);
119
while (p1 > &buf[0] && fi != 0) {
120
fj = modf(fi / 10, &fi);
121
*--p1 = (int) ((fj + .03) * 10) + '0';
124
while (p1 < &buf[NDIG])
128
while ((fj = arg * 10) < 1) {
142
while (p <= p1 && p < &buf[NDIG]) {
144
arg = modf(arg, &fj);
145
*p++ = (int) fj + '0';
147
if (p1 >= &buf[NDIG]) {
148
buf[NDIG - 1] = '\0';
171
static char *apr_ecvt(double arg, int ndigits, int *decpt, int *sign, char *buf)
173
return (apr_cvt(arg, ndigits, decpt, sign, 1, buf));
176
static char *apr_fcvt(double arg, int ndigits, int *decpt, int *sign, char *buf)
178
return (apr_cvt(arg, ndigits, decpt, sign, 0, buf));
182
* apr_gcvt - Floating output conversion to
183
* minimal length string
186
static char *apr_gcvt(double number, int ndigit, char *buf, boolean_e altform)
189
register char *p1, *p2;
193
p1 = apr_ecvt(number, ndigit, &decpt, &sign, buf1);
197
for (i = ndigit - 1; i > 0 && p1[i] == '0'; i--)
199
if ((decpt >= 0 && decpt - ndigit > 4)
200
|| (decpt < 0 && decpt < -3)) { /* use E-style */
204
for (i = 1; i < ndigit; i++)
214
*p2++ = decpt / 100 + '0';
216
*p2++ = (decpt % 100) / 10 + '0';
217
*p2++ = decpt % 10 + '0';
228
for (i = 1; i <= ndigit; i++) {
233
if (ndigit < decpt) {
234
while (ndigit++ < decpt)
239
if (p2[-1] == '.' && !altform)
246
* The INS_CHAR macro inserts a character in the buffer and writes
247
* the buffer back to disk if necessary
248
* It uses the char pointers sp and bep:
249
* sp points to the next available character in the buffer
250
* bep points to the end-of-buffer+1
251
* While using this macro, note that the nextb pointer is NOT updated.
253
* NOTE: Evaluation of the c argument should not have any side-effects
255
#define INS_CHAR(c, sp, bep, cc) \
259
vbuff->curpos = sp; \
260
if (flush_func(vbuff)) \
262
sp = vbuff->curpos; \
263
bep = vbuff->endpos; \
270
#define NUM(c) (c - '0')
272
#define STR_TO_DEC(str, num) \
274
while (apr_isdigit(*str)) \
277
num += NUM(*str++); \
281
* This macro does zero padding so that the precision
282
* requirement is satisfied. The padding is done by
283
* adding '0's to the left of the string that is going
284
* to be printed. We don't allow precision to be large
285
* enough that we continue past the start of s.
287
* NOTE: this makes use of the magic info that s is
288
* always based on num_buf with a size of NUM_BUF_SIZE.
290
#define FIX_PRECISION(adjust, precision, s, s_len) \
292
apr_size_t p = (precision + 1 < NUM_BUF_SIZE) \
293
? precision : NUM_BUF_SIZE - 1; \
302
* Macro that does padding. The padding is done by printing
305
#define PAD(width, len, ch) \
308
INS_CHAR(ch, sp, bep, cc); \
314
* Prefix the character ch to the string str
316
* Set the has_prefix flag
318
#define PREFIX(str, length, ch) \
325
* Convert num to its decimal format.
327
* - a pointer to a string containing the number (no sign)
328
* - len contains the length of the string
329
* - is_negative is set to TRUE or FALSE depending on the sign
330
* of the number (always set to FALSE if is_unsigned is TRUE)
332
* The caller provides a buffer for the string: that is the buf_end argument
333
* which is a pointer to the END of the buffer + 1 (i.e. if the buffer
334
* is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
336
* Note: we have 2 versions. One is used when we need to use quads
337
* (conv_10_quad), the other when we don't (conv_10). We're assuming the
340
static char *conv_10(register wide_int num, register bool_int is_unsigned,
341
register bool_int *is_negative, char *buf_end,
342
register apr_size_t *len)
344
register char *p = buf_end;
345
register u_wide_int magnitude;
348
magnitude = (u_wide_int) num;
349
*is_negative = FALSE;
352
*is_negative = (num < 0);
355
* On a 2's complement machine, negating the most negative integer
356
* results in a number that cannot be represented as a signed integer.
357
* Here is what we do to obtain the number's magnitude:
358
* a. add 1 to the number
359
* b. negate it (becomes positive)
360
* c. convert it to unsigned
364
wide_int t = num + 1;
366
magnitude = ((u_wide_int) -t) + 1;
369
magnitude = (u_wide_int) num;
373
* We use a do-while loop so that we write at least 1 digit
376
register u_wide_int new_magnitude = magnitude / 10;
378
*--p = (char) (magnitude - new_magnitude * 10 + '0');
379
magnitude = new_magnitude;
387
static char *conv_10_quad(widest_int num, register bool_int is_unsigned,
388
register bool_int *is_negative, char *buf_end,
389
register apr_size_t *len)
391
register char *p = buf_end;
392
u_widest_int magnitude;
395
* We see if we can use the faster non-quad version by checking the
396
* number against the largest long value it can be. If <=, we
397
* punt to the quicker version.
399
if ((num <= ULONG_MAX && is_unsigned)
400
|| (num <= LONG_MAX && num >= LONG_MIN && !is_unsigned))
401
return(conv_10( (wide_int)num, is_unsigned, is_negative,
405
magnitude = (u_widest_int) num;
406
*is_negative = FALSE;
409
*is_negative = (num < 0);
412
* On a 2's complement machine, negating the most negative integer
413
* results in a number that cannot be represented as a signed integer.
414
* Here is what we do to obtain the number's magnitude:
415
* a. add 1 to the number
416
* b. negate it (becomes positive)
417
* c. convert it to unsigned
421
widest_int t = num + 1;
423
magnitude = ((u_widest_int) -t) + 1;
426
magnitude = (u_widest_int) num;
430
* We use a do-while loop so that we write at least 1 digit
433
u_widest_int new_magnitude = magnitude / 10;
435
*--p = (char) (magnitude - new_magnitude * 10 + '0');
436
magnitude = new_magnitude;
446
static char *conv_in_addr(struct in_addr *ia, char *buf_end, apr_size_t *len)
448
unsigned addr = ntohl(ia->s_addr);
450
bool_int is_negative;
453
p = conv_10((addr & 0x000000FF) , TRUE, &is_negative, p, &sub_len);
455
p = conv_10((addr & 0x0000FF00) >> 8, TRUE, &is_negative, p, &sub_len);
457
p = conv_10((addr & 0x00FF0000) >> 16, TRUE, &is_negative, p, &sub_len);
459
p = conv_10((addr & 0xFF000000) >> 24, TRUE, &is_negative, p, &sub_len);
467
static char *conv_apr_sockaddr(apr_sockaddr_t *sa, char *buf_end, apr_size_t *len)
470
bool_int is_negative;
474
p = conv_10(sa->port, TRUE, &is_negative, p, &sub_len);
476
apr_sockaddr_ip_get(&ipaddr_str, sa);
477
sub_len = strlen(ipaddr_str);
479
if (sa->family == APR_INET6 &&
480
!IN6_IS_ADDR_V4MAPPED(&sa->sa.sin6.sin6_addr)) {
484
memcpy(p + 1, ipaddr_str, sub_len);
490
memcpy(p, ipaddr_str, sub_len);
500
static char *conv_os_thread_t(apr_os_thread_t *tid, char *buf_end, apr_size_t *len)
504
apr_uint64_t alignme;
509
switch(sizeof(u.tid)) {
510
case sizeof(apr_int32_t):
511
return conv_10(*(apr_uint32_t *)&u.tid, TRUE, &is_negative, buf_end, len);
512
case sizeof(apr_int64_t):
513
return conv_10_quad(*(apr_uint64_t *)&u.tid, TRUE, &is_negative, buf_end, len);
515
/* not implemented; stick 0 in the buffer */
516
return conv_10(0, TRUE, &is_negative, buf_end, len);
524
* Convert a floating point number to a string formats 'f', 'e' or 'E'.
525
* The result is placed in buf, and len denotes the length of the string
526
* The sign is returned in the is_negative argument (and is not placed
529
static char *conv_fp(register char format, register double num,
530
boolean_e add_dp, int precision, bool_int *is_negative,
531
char *buf, apr_size_t *len)
533
register char *s = buf;
539
p = apr_fcvt(num, precision, &decimal_point, is_negative, buf1);
540
else /* either e or E format */
541
p = apr_ecvt(num, precision + 1, &decimal_point, is_negative, buf1);
544
* Check for Infinity and NaN
546
if (apr_isalpha(*p)) {
548
memcpy(buf, p, *len + 1);
549
*is_negative = FALSE;
554
if (decimal_point <= 0) {
558
while (decimal_point++ < 0)
565
while (decimal_point-- > 0)
567
if (precision > 0 || add_dp)
573
if (precision > 0 || add_dp)
578
* copy the rest of p, the NUL is NOT copied
584
char temp[EXPONENT_LENGTH]; /* for exponent conversion */
586
bool_int exponent_is_negative;
588
*s++ = format; /* either e or E */
590
if (decimal_point != 0) {
591
p = conv_10((wide_int) decimal_point, FALSE, &exponent_is_negative,
592
&temp[EXPONENT_LENGTH], &t_len);
593
*s++ = exponent_is_negative ? '-' : '+';
596
* Make sure the exponent has at least 2 digits
616
* Convert num to a base X number where X is a power of 2. nbits determines X.
617
* For example, if nbits is 3, we do base 8 conversion
619
* a pointer to a string containing the number
621
* The caller provides a buffer for the string: that is the buf_end argument
622
* which is a pointer to the END of the buffer + 1 (i.e. if the buffer
623
* is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
625
* As with conv_10, we have a faster version which is used when
626
* the number isn't quad size.
628
static char *conv_p2(register u_wide_int num, register int nbits,
629
char format, char *buf_end, register apr_size_t *len)
631
register int mask = (1 << nbits) - 1;
632
register char *p = buf_end;
633
static const char low_digits[] = "0123456789abcdef";
634
static const char upper_digits[] = "0123456789ABCDEF";
635
register const char *digits = (format == 'X') ? upper_digits : low_digits;
638
*--p = digits[num & mask];
647
static char *conv_p2_quad(u_widest_int num, register int nbits,
648
char format, char *buf_end, register apr_size_t *len)
650
register int mask = (1 << nbits) - 1;
651
register char *p = buf_end;
652
static const char low_digits[] = "0123456789abcdef";
653
static const char upper_digits[] = "0123456789ABCDEF";
654
register const char *digits = (format == 'X') ? upper_digits : low_digits;
656
if (num <= ULONG_MAX)
657
return(conv_p2((u_wide_int)num, nbits, format, buf_end, len));
660
*--p = digits[num & mask];
670
static char *conv_os_thread_t_hex(apr_os_thread_t *tid, char *buf_end, apr_size_t *len)
674
apr_uint64_t alignme;
679
switch(sizeof(u.tid)) {
680
case sizeof(apr_int32_t):
681
return conv_p2(*(apr_uint32_t *)&u.tid, 4, 'x', buf_end, len);
682
case sizeof(apr_int64_t):
683
return conv_p2_quad(*(apr_uint64_t *)&u.tid, 4, 'x', buf_end, len);
685
/* not implemented; stick 0 in the buffer */
686
return conv_10(0, TRUE, &is_negative, buf_end, len);
692
* Do format conversion placing the output in buffer
694
APR_DECLARE(int) apr_vformatter(int (*flush_func)(apr_vformatter_buff_t *),
695
apr_vformatter_buff_t *vbuff, const char *fmt, va_list ap)
700
register apr_size_t i;
702
register char *s = NULL;
706
register apr_size_t min_width = 0;
707
apr_size_t precision = 0;
715
widest_int i_quad = (widest_int) 0;
716
u_widest_int ui_quad;
717
wide_int i_num = (wide_int) 0;
720
char num_buf[NUM_BUF_SIZE];
721
char char_buf[2]; /* for printing %% and %<unknown> */
724
IS_QUAD, IS_LONG, IS_SHORT, IS_INT
726
enum var_type_enum var_type = IS_INT;
731
boolean_e alternate_form;
732
boolean_e print_sign;
733
boolean_e print_blank;
734
boolean_e adjust_precision;
735
boolean_e adjust_width;
736
bool_int is_negative;
743
INS_CHAR(*fmt, sp, bep, cc);
747
* Default variable settings
749
boolean_e print_something = YES;
751
alternate_form = print_sign = print_blank = NO;
758
* Try to avoid checking for flags, width or precision
760
if (!apr_islower(*fmt)) {
762
* Recognize flags: -, #, BLANK, +
767
else if (*fmt == '+')
769
else if (*fmt == '#')
770
alternate_form = YES;
771
else if (*fmt == ' ')
773
else if (*fmt == '0')
780
* Check if a width was specified
782
if (apr_isdigit(*fmt)) {
783
STR_TO_DEC(fmt, min_width);
786
else if (*fmt == '*') {
787
int v = va_arg(ap, int);
792
min_width = (apr_size_t)(-v);
795
min_width = (apr_size_t)v;
801
* Check if a precision was specified
804
adjust_precision = YES;
806
if (apr_isdigit(*fmt)) {
807
STR_TO_DEC(fmt, precision);
809
else if (*fmt == '*') {
810
int v = va_arg(ap, int);
812
precision = (v < 0) ? 0 : (apr_size_t)v;
818
adjust_precision = NO;
821
adjust_precision = adjust_width = NO;
824
* Modifier check. Note that if APR_INT64_T_FMT is "d",
825
* the first if condition is never true.
827
if ((sizeof(APR_INT64_T_FMT) == 4 &&
828
fmt[0] == APR_INT64_T_FMT[0] &&
829
fmt[1] == APR_INT64_T_FMT[1]) ||
830
(sizeof(APR_INT64_T_FMT) == 3 &&
831
fmt[0] == APR_INT64_T_FMT[0]) ||
832
(sizeof(APR_INT64_T_FMT) > 4 &&
833
strncmp(fmt, APR_INT64_T_FMT,
834
sizeof(APR_INT64_T_FMT) - 2) == 0)) {
835
/* Need to account for trailing 'd' and null in sizeof() */
837
fmt += (sizeof(APR_INT64_T_FMT) - 2);
839
else if (*fmt == 'q') {
843
else if (*fmt == 'l') {
847
else if (*fmt == 'h') {
856
* Argument extraction and printing.
857
* First we determine the argument type.
858
* Then, we convert the argument to a string.
859
* On exit from the switch, s points to the string that
860
* must be printed, s_len has the length of the string
861
* The precision requirements, if any, are reflected in s_len.
863
* NOTE: pad_char may be set to '0' because of the 0 flag.
864
* It is reset to ' ' by non-numeric formats
868
if (var_type == IS_QUAD) {
869
i_quad = va_arg(ap, u_widest_int);
870
s = conv_10_quad(i_quad, 1, &is_negative,
871
&num_buf[NUM_BUF_SIZE], &s_len);
874
if (var_type == IS_LONG)
875
i_num = (wide_int) va_arg(ap, u_wide_int);
876
else if (var_type == IS_SHORT)
877
i_num = (wide_int) (unsigned short) va_arg(ap, unsigned int);
879
i_num = (wide_int) va_arg(ap, unsigned int);
880
s = conv_10(i_num, 1, &is_negative,
881
&num_buf[NUM_BUF_SIZE], &s_len);
883
FIX_PRECISION(adjust_precision, precision, s, s_len);
888
if (var_type == IS_QUAD) {
889
i_quad = va_arg(ap, widest_int);
890
s = conv_10_quad(i_quad, 0, &is_negative,
891
&num_buf[NUM_BUF_SIZE], &s_len);
894
if (var_type == IS_LONG)
895
i_num = (wide_int) va_arg(ap, wide_int);
896
else if (var_type == IS_SHORT)
897
i_num = (wide_int) (short) va_arg(ap, int);
899
i_num = (wide_int) va_arg(ap, int);
900
s = conv_10(i_num, 0, &is_negative,
901
&num_buf[NUM_BUF_SIZE], &s_len);
903
FIX_PRECISION(adjust_precision, precision, s, s_len);
909
else if (print_blank)
915
if (var_type == IS_QUAD) {
916
ui_quad = va_arg(ap, u_widest_int);
917
s = conv_p2_quad(ui_quad, 3, *fmt,
918
&num_buf[NUM_BUF_SIZE], &s_len);
921
if (var_type == IS_LONG)
922
ui_num = (u_wide_int) va_arg(ap, u_wide_int);
923
else if (var_type == IS_SHORT)
924
ui_num = (u_wide_int) (unsigned short) va_arg(ap, unsigned int);
926
ui_num = (u_wide_int) va_arg(ap, unsigned int);
927
s = conv_p2(ui_num, 3, *fmt,
928
&num_buf[NUM_BUF_SIZE], &s_len);
930
FIX_PRECISION(adjust_precision, precision, s, s_len);
931
if (alternate_form && *s != '0') {
940
if (var_type == IS_QUAD) {
941
ui_quad = va_arg(ap, u_widest_int);
942
s = conv_p2_quad(ui_quad, 4, *fmt,
943
&num_buf[NUM_BUF_SIZE], &s_len);
946
if (var_type == IS_LONG)
947
ui_num = (u_wide_int) va_arg(ap, u_wide_int);
948
else if (var_type == IS_SHORT)
949
ui_num = (u_wide_int) (unsigned short) va_arg(ap, unsigned int);
951
ui_num = (u_wide_int) va_arg(ap, unsigned int);
952
s = conv_p2(ui_num, 4, *fmt,
953
&num_buf[NUM_BUF_SIZE], &s_len);
955
FIX_PRECISION(adjust_precision, precision, s, s_len);
956
if (alternate_form && i_num != 0) {
957
*--s = *fmt; /* 'x' or 'X' */
965
s = va_arg(ap, char *);
967
if (!adjust_precision) {
971
/* From the C library standard in section 7.9.6.1:
972
* ...if the precision is specified, no more then
973
* that many characters are written. If the
974
* precision is not specified or is greater
975
* than the size of the array, the array shall
976
* contain a null character.
978
* My reading is is precision is specified and
979
* is less then or equal to the size of the
980
* array, no null character is required. So
981
* we can't do a strlen.
983
* This figures out the length of the string
984
* up to the precision. Once it's long enough
985
* for the specified precision, we don't care
988
* NOTE: you must do the length comparison
989
* before the check for the null character.
990
* Otherwise, you'll check one beyond the
991
* last valid character.
995
for (walk = s, s_len = 0;
996
(s_len < precision) && (*walk != '\0');
1011
fp_num = va_arg(ap, double);
1013
* We use &num_buf[ 1 ], so that we have room for the sign
1017
if (isnan(fp_num)) {
1023
if (!s && isinf(fp_num)) {
1029
s = conv_fp(*fmt, fp_num, alternate_form,
1030
(adjust_precision == NO) ? FLOAT_DIGITS : precision,
1031
&is_negative, &num_buf[1], &s_len);
1034
else if (print_sign)
1036
else if (print_blank)
1044
if (adjust_precision == NO)
1045
precision = FLOAT_DIGITS;
1046
else if (precision == 0)
1049
* * We use &num_buf[ 1 ], so that we have room for the sign
1051
s = apr_gcvt(va_arg(ap, double), precision, &num_buf[1],
1055
else if (print_sign)
1057
else if (print_blank)
1062
if (alternate_form && (q = strchr(s, '.')) == NULL) {
1064
s[s_len] = '\0'; /* delimit for following strchr() */
1066
if (*fmt == 'G' && (q = strchr(s, 'e')) != NULL)
1072
char_buf[0] = (char) (va_arg(ap, int));
1088
if (var_type == IS_QUAD)
1089
*(va_arg(ap, widest_int *)) = cc;
1090
else if (var_type == IS_LONG)
1091
*(va_arg(ap, long *)) = cc;
1092
else if (var_type == IS_SHORT)
1093
*(va_arg(ap, short *)) = cc;
1095
*(va_arg(ap, int *)) = cc;
1096
print_something = NO;
1100
* This is where we extend the printf format, with a second
1106
* If the pointer size is equal to or smaller than the size
1107
* of the largest unsigned int, we convert the pointer to a
1108
* hex number, otherwise we print "%p" to indicate that we
1109
* don't handle "%p".
1112
#ifdef APR_VOID_P_IS_QUAD
1113
if (sizeof(void *) <= sizeof(u_widest_int)) {
1114
ui_quad = (u_widest_int) va_arg(ap, void *);
1115
s = conv_p2_quad(ui_quad, 4, 'x',
1116
&num_buf[NUM_BUF_SIZE], &s_len);
1119
if (sizeof(void *) <= sizeof(u_wide_int)) {
1120
ui_num = (u_wide_int) va_arg(ap, void *);
1121
s = conv_p2(ui_num, 4, 'x',
1122
&num_buf[NUM_BUF_SIZE], &s_len);
1133
/* print an apr_sockaddr_t as a.b.c.d:port */
1138
sa = va_arg(ap, apr_sockaddr_t *);
1140
s = conv_apr_sockaddr(sa, &num_buf[NUM_BUF_SIZE], &s_len);
1141
if (adjust_precision && precision < s_len)
1152
/* print a struct in_addr as a.b.c.d */
1157
ia = va_arg(ap, struct in_addr *);
1159
s = conv_in_addr(ia, &num_buf[NUM_BUF_SIZE], &s_len);
1160
if (adjust_precision && precision < s_len)
1174
apr_os_thread_t *tid;
1176
tid = va_arg(ap, apr_os_thread_t *);
1178
s = conv_os_thread_t(tid, &num_buf[NUM_BUF_SIZE], &s_len);
1179
if (adjust_precision && precision < s_len)
1199
apr_os_thread_t *tid;
1201
tid = va_arg(ap, apr_os_thread_t *);
1203
s = conv_os_thread_t_hex(tid, &num_buf[NUM_BUF_SIZE], &s_len);
1204
if (adjust_precision && precision < s_len)
1222
/* if %p ends the string, oh well ignore it */
1229
(void)va_arg(ap, void *); /* skip the bogus argument on the stack */
1236
* The last character of the format string was %.
1243
* The default case is for unrecognized %'s.
1244
* We print %<char> to help the user identify what
1245
* option is not understood.
1246
* This is also useful in case the user wants to pass
1247
* the output of format_converter to another function
1248
* that understands some other %<char> (like syslog).
1249
* Note that we can't point s inside fmt because the
1250
* unknown <char> could be preceded by width etc.
1261
if (prefix_char != NUL && s != S_NULL && s != char_buf) {
1266
if (adjust_width && adjust == RIGHT && min_width > s_len) {
1267
if (pad_char == '0' && prefix_char != NUL) {
1268
INS_CHAR(*s, sp, bep, cc);
1273
PAD(min_width, s_len, pad_char);
1277
* Print the string s.
1279
if (print_something == YES) {
1280
for (i = s_len; i != 0; i--) {
1281
INS_CHAR(*s, sp, bep, cc);
1286
if (adjust_width && adjust == LEFT && min_width > s_len)
1287
PAD(min_width, s_len, pad_char);
1297
static int snprintf_flush(apr_vformatter_buff_t *vbuff)
1299
/* if the buffer fills we have to abort immediately, there is no way
1300
* to "flush" an apr_snprintf... there's nowhere to flush it to.
1306
APR_DECLARE_NONSTD(int) apr_snprintf(char *buf, apr_size_t len,
1307
const char *format, ...)
1311
apr_vformatter_buff_t vbuff;
1314
/* NOTE: This is a special case; we just want to return the number
1315
* of chars that would be written (minus \0) if the buffer
1316
* size was infinite. We leverage the fact that INS_CHAR
1317
* just does actual inserts iff the buffer pointer is non-NULL.
1318
* In this case, we don't care what buf is; it can be NULL, since
1319
* we don't touch it at all.
1321
vbuff.curpos = NULL;
1322
vbuff.endpos = NULL;
1324
/* save one byte for nul terminator */
1326
vbuff.endpos = buf + len - 1;
1328
va_start(ap, format);
1329
cc = apr_vformatter(snprintf_flush, &vbuff, format, ap);
1332
*vbuff.curpos = '\0';
1334
return (cc == -1) ? (int)len : cc;
1338
APR_DECLARE(int) apr_vsnprintf(char *buf, apr_size_t len, const char *format,
1342
apr_vformatter_buff_t vbuff;
1345
/* See above note */
1346
vbuff.curpos = NULL;
1347
vbuff.endpos = NULL;
1349
/* save one byte for nul terminator */
1351
vbuff.endpos = buf + len - 1;
1353
cc = apr_vformatter(snprintf_flush, &vbuff, format, ap);
1355
*vbuff.curpos = '\0';
1357
return (cc == -1) ? (int)len : cc;