1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. 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"
24 #include "apr_errno.h"
29 #if APR_HAVE_NETINET_IN_H
30 #include <netinet/in.h>
32 #if APR_HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
35 #if APR_HAVE_ARPA_INET_H
36 #include <arpa/inet.h>
57 static const char null_string[] = "(null)";
58 #define S_NULL ((char *)null_string)
61 #define FLOAT_DIGITS 6
62 #define EXPONENT_LENGTH 10
65 * NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions
67 * NOTICE: this is a magic number; do not decrease it
69 #define NUM_BUF_SIZE 512
72 * cvt - IEEE floating point formatting routines.
73 * Derived from UNIX V7, Copyright(C) Caldera International Inc.
77 * apr_ecvt converts to decimal
78 * the number of digits is specified by ndigit
79 * decpt is set to the position of the decimal point
80 * sign is set to 0 for positive, 1 for negative
85 /* buf must have at least NDIG bytes */
86 static char *apr_cvt(double arg, int ndigits, int *decpt, int *sign,
91 register char *p, *p1;
93 if (ndigits >= NDIG - 1)
102 arg = modf(arg, &fi);
109 while (p1 > &buf[0] && fi != 0) {
110 fj = modf(fi / 10, &fi);
111 *--p1 = (int) ((fj + .03) * 10) + '0';
114 while (p1 < &buf[NDIG])
118 while ((fj = arg * 10) < 1) {
132 while (p <= p1 && p < &buf[NDIG]) {
134 arg = modf(arg, &fj);
135 *p++ = (int) fj + '0';
137 if (p1 >= &buf[NDIG]) {
138 buf[NDIG - 1] = '\0';
161 static char *apr_ecvt(double arg, int ndigits, int *decpt, int *sign, char *buf)
163 return (apr_cvt(arg, ndigits, decpt, sign, 1, buf));
166 static char *apr_fcvt(double arg, int ndigits, int *decpt, int *sign, char *buf)
168 return (apr_cvt(arg, ndigits, decpt, sign, 0, buf));
172 * apr_gcvt - Floating output conversion to
173 * minimal length string
176 static char *apr_gcvt(double number, int ndigit, char *buf, boolean_e altform)
179 register char *p1, *p2;
183 p1 = apr_ecvt(number, ndigit, &decpt, &sign, buf1);
187 for (i = ndigit - 1; i > 0 && p1[i] == '0'; i--)
189 if ((decpt >= 0 && decpt - ndigit > 4)
190 || (decpt < 0 && decpt < -3)) { /* use E-style */
194 for (i = 1; i < ndigit; i++)
204 *p2++ = decpt / 100 + '0';
206 *p2++ = (decpt % 100) / 10 + '0';
207 *p2++ = decpt % 10 + '0';
218 for (i = 1; i <= ndigit; i++) {
223 if (ndigit < decpt) {
224 while (ndigit++ < decpt)
229 if (p2[-1] == '.' && !altform)
236 * The INS_CHAR macro inserts a character in the buffer and writes
237 * the buffer back to disk if necessary
238 * It uses the char pointers sp and bep:
239 * sp points to the next available character in the buffer
240 * bep points to the end-of-buffer+1
241 * While using this macro, note that the nextb pointer is NOT updated.
243 * NOTE: Evaluation of the c argument should not have any side-effects
245 #define INS_CHAR(c, sp, bep, cc) \
249 vbuff->curpos = sp; \
250 if (flush_func(vbuff)) \
252 sp = vbuff->curpos; \
253 bep = vbuff->endpos; \
260 #define NUM(c) (c - '0')
262 #define STR_TO_DEC(str, num) \
264 while (apr_isdigit(*str)) \
267 num += NUM(*str++); \
271 * This macro does zero padding so that the precision
272 * requirement is satisfied. The padding is done by
273 * adding '0's to the left of the string that is going
274 * to be printed. We don't allow precision to be large
275 * enough that we continue past the start of s.
277 * NOTE: this makes use of the magic info that s is
278 * always based on num_buf with a size of NUM_BUF_SIZE.
280 #define FIX_PRECISION(adjust, precision, s, s_len) \
282 apr_size_t p = (precision + 1 < NUM_BUF_SIZE) \
283 ? precision : NUM_BUF_SIZE - 1; \
292 * Macro that does padding. The padding is done by printing
295 #define PAD(width, len, ch) \
298 INS_CHAR(ch, sp, bep, cc); \
304 * Prefix the character ch to the string str
306 * Set the has_prefix flag
308 #define PREFIX(str, length, ch) \
315 * Convert num to its decimal format.
317 * - a pointer to a string containing the number (no sign)
318 * - len contains the length of the string
319 * - is_negative is set to TRUE or FALSE depending on the sign
320 * of the number (always set to FALSE if is_unsigned is TRUE)
322 * The caller provides a buffer for the string: that is the buf_end argument
323 * which is a pointer to the END of the buffer + 1 (i.e. if the buffer
324 * is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
326 * Note: we have 2 versions. One is used when we need to use quads
327 * (conv_10_quad), the other when we don't (conv_10). We're assuming the
330 static char *conv_10(register apr_int32_t num, register int is_unsigned,
331 register int *is_negative, char *buf_end,
332 register apr_size_t *len)
334 register char *p = buf_end;
335 register apr_uint32_t magnitude = num;
338 *is_negative = FALSE;
341 *is_negative = (num < 0);
344 * On a 2's complement machine, negating the most negative integer
345 * results in a number that cannot be represented as a signed integer.
346 * Here is what we do to obtain the number's magnitude:
347 * a. add 1 to the number
348 * b. negate it (becomes positive)
349 * c. convert it to unsigned
353 apr_int32_t t = num + 1;
354 magnitude = ((apr_uint32_t) -t) + 1;
359 * We use a do-while loop so that we write at least 1 digit
362 register apr_uint32_t new_magnitude = magnitude / 10;
364 *--p = (char) (magnitude - new_magnitude * 10 + '0');
365 magnitude = new_magnitude;
373 static char *conv_10_quad(apr_int64_t num, register int is_unsigned,
374 register int *is_negative, char *buf_end,
375 register apr_size_t *len)
377 register char *p = buf_end;
378 apr_uint64_t magnitude = num;
381 * We see if we can use the faster non-quad version by checking the
382 * number against the largest long value it can be. If <=, we
383 * punt to the quicker version.
385 if ((magnitude <= APR_UINT32_MAX && is_unsigned)
386 || (num <= APR_INT32_MAX && num >= APR_INT32_MIN && !is_unsigned))
387 return(conv_10((apr_int32_t)num, is_unsigned, is_negative, buf_end, len));
390 *is_negative = FALSE;
393 *is_negative = (num < 0);
396 * On a 2's complement machine, negating the most negative integer
397 * results in a number that cannot be represented as a signed integer.
398 * Here is what we do to obtain the number's magnitude:
399 * a. add 1 to the number
400 * b. negate it (becomes positive)
401 * c. convert it to unsigned
405 apr_int64_t t = num + 1;
406 magnitude = ((apr_uint64_t) -t) + 1;
411 * We use a do-while loop so that we write at least 1 digit
414 apr_uint64_t new_magnitude = magnitude / 10;
416 *--p = (char) (magnitude - new_magnitude * 10 + '0');
417 magnitude = new_magnitude;
425 static char *conv_in_addr(struct in_addr *ia, char *buf_end, apr_size_t *len)
427 unsigned addr = ntohl(ia->s_addr);
432 p = conv_10((addr & 0x000000FF) , TRUE, &is_negative, p, &sub_len);
434 p = conv_10((addr & 0x0000FF00) >> 8, TRUE, &is_negative, p, &sub_len);
436 p = conv_10((addr & 0x00FF0000) >> 16, TRUE, &is_negative, p, &sub_len);
438 p = conv_10((addr & 0xFF000000) >> 24, TRUE, &is_negative, p, &sub_len);
445 /* Must be passed a buffer of size NUM_BUF_SIZE where buf_end points
446 * to 1 byte past the end of the buffer. */
447 static char *conv_apr_sockaddr(apr_sockaddr_t *sa, char *buf_end, apr_size_t *len)
454 p = conv_10(sa->port, TRUE, &is_negative, p, &sub_len);
456 ipaddr_str = buf_end - NUM_BUF_SIZE;
457 if (apr_sockaddr_ip_getbuf(ipaddr_str, sa->addr_str_len, sa)) {
458 /* Should only fail if the buffer is too small, which it
459 * should not be; but fail safe anyway: */
464 sub_len = strlen(ipaddr_str);
466 if (sa->family == APR_INET6 &&
467 !IN6_IS_ADDR_V4MAPPED(&sa->sa.sin6.sin6_addr)) {
471 memcpy(p + 1, ipaddr_str, sub_len);
477 memcpy(p, ipaddr_str, sub_len);
487 static char *conv_os_thread_t(apr_os_thread_t *tid, char *buf_end, apr_size_t *len)
497 switch(sizeof(u.tid)) {
498 case sizeof(apr_int32_t):
499 return conv_10(u.u32, TRUE, &is_negative, buf_end, len);
500 case sizeof(apr_int64_t):
501 return conv_10_quad(u.u64, TRUE, &is_negative, buf_end, len);
503 /* not implemented; stick 0 in the buffer */
504 return conv_10(0, TRUE, &is_negative, buf_end, len);
512 * Convert a floating point number to a string formats 'f', 'e' or 'E'.
513 * The result is placed in buf, and len denotes the length of the string
514 * The sign is returned in the is_negative argument (and is not placed
517 static char *conv_fp(register char format, register double num,
518 boolean_e add_dp, int precision, int *is_negative,
519 char *buf, apr_size_t *len)
521 register char *s = buf;
527 p = apr_fcvt(num, precision, &decimal_point, is_negative, buf1);
528 else /* either e or E format */
529 p = apr_ecvt(num, precision + 1, &decimal_point, is_negative, buf1);
532 * Check for Infinity and NaN
534 if (apr_isalpha(*p)) {
536 memcpy(buf, p, *len + 1);
537 *is_negative = FALSE;
542 if (decimal_point <= 0) {
546 while (decimal_point++ < 0)
553 while (decimal_point-- > 0)
555 if (precision > 0 || add_dp)
561 if (precision > 0 || add_dp)
566 * copy the rest of p, the NUL is NOT copied
572 char temp[EXPONENT_LENGTH]; /* for exponent conversion */
574 int exponent_is_negative;
576 *s++ = format; /* either e or E */
578 if (decimal_point != 0) {
579 p = conv_10((apr_int32_t) decimal_point, FALSE, &exponent_is_negative,
580 &temp[EXPONENT_LENGTH], &t_len);
581 *s++ = exponent_is_negative ? '-' : '+';
584 * Make sure the exponent has at least 2 digits
604 * Convert num to a base X number where X is a power of 2. nbits determines X.
605 * For example, if nbits is 3, we do base 8 conversion
607 * a pointer to a string containing the number
609 * The caller provides a buffer for the string: that is the buf_end argument
610 * which is a pointer to the END of the buffer + 1 (i.e. if the buffer
611 * is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
613 * As with conv_10, we have a faster version which is used when
614 * the number isn't quad size.
616 static char *conv_p2(register apr_uint32_t num, register int nbits,
617 char format, char *buf_end, register apr_size_t *len)
619 register int mask = (1 << nbits) - 1;
620 register char *p = buf_end;
621 static const char low_digits[] = "0123456789abcdef";
622 static const char upper_digits[] = "0123456789ABCDEF";
623 register const char *digits = (format == 'X') ? upper_digits : low_digits;
626 *--p = digits[num & mask];
635 static char *conv_p2_quad(apr_uint64_t num, register int nbits,
636 char format, char *buf_end, register apr_size_t *len)
638 register int mask = (1 << nbits) - 1;
639 register char *p = buf_end;
640 static const char low_digits[] = "0123456789abcdef";
641 static const char upper_digits[] = "0123456789ABCDEF";
642 register const char *digits = (format == 'X') ? upper_digits : low_digits;
644 if (num <= APR_UINT32_MAX)
645 return(conv_p2((apr_uint32_t)num, nbits, format, buf_end, len));
648 *--p = digits[num & mask];
658 static char *conv_os_thread_t_hex(apr_os_thread_t *tid, char *buf_end, apr_size_t *len)
668 switch(sizeof(u.tid)) {
669 case sizeof(apr_int32_t):
670 return conv_p2(u.u32, 4, 'x', buf_end, len);
671 case sizeof(apr_int64_t):
672 return conv_p2_quad(u.u64, 4, 'x', buf_end, len);
674 /* not implemented; stick 0 in the buffer */
675 return conv_10(0, TRUE, &is_negative, buf_end, len);
681 * Do format conversion placing the output in buffer
683 APR_DECLARE(int) apr_vformatter(int (*flush_func)(apr_vformatter_buff_t *),
684 apr_vformatter_buff_t *vbuff, const char *fmt, va_list ap)
689 register apr_size_t i;
691 register char *s = NULL;
693 apr_size_t s_len = 0;
695 register apr_size_t min_width = 0;
696 apr_size_t precision = 0;
704 apr_int64_t i_quad = 0;
705 apr_uint64_t ui_quad;
706 apr_int32_t i_num = 0;
707 apr_uint32_t ui_num = 0;
709 char num_buf[NUM_BUF_SIZE];
710 char char_buf[2]; /* for printing %% and %<unknown> */
711 char buf[5]; /* for printing %B, %F, and %S */
714 IS_QUAD, IS_LONG, IS_SHORT, IS_INT
716 enum var_type_enum var_type = IS_INT;
721 boolean_e alternate_form;
722 boolean_e print_sign;
723 boolean_e print_blank;
724 boolean_e adjust_precision;
725 boolean_e adjust_width;
733 INS_CHAR(*fmt, sp, bep, cc);
737 * Default variable settings
739 boolean_e print_something = YES;
741 alternate_form = print_sign = print_blank = NO;
748 * Try to avoid checking for flags, width or precision
750 if (!apr_islower(*fmt)) {
752 * Recognize flags: -, #, BLANK, +
757 else if (*fmt == '+')
759 else if (*fmt == '#')
760 alternate_form = YES;
761 else if (*fmt == ' ')
763 else if (*fmt == '0')
770 * Check if a width was specified
772 if (apr_isdigit(*fmt)) {
773 STR_TO_DEC(fmt, min_width);
776 else if (*fmt == '*') {
777 int v = va_arg(ap, int);
782 min_width = (apr_size_t)(-v);
785 min_width = (apr_size_t)v;
791 * Check if a precision was specified
794 adjust_precision = YES;
796 if (apr_isdigit(*fmt)) {
797 STR_TO_DEC(fmt, precision);
799 else if (*fmt == '*') {
800 int v = va_arg(ap, int);
802 precision = (v < 0) ? 0 : (apr_size_t)v;
808 adjust_precision = NO;
811 adjust_precision = adjust_width = NO;
814 * Modifier check. In same cases, APR_OFF_T_FMT can be
815 * "lld" and APR_INT64_T_FMT can be "ld" (that is, off_t is
816 * "larger" than int64). Check that case 1st.
817 * Note that if APR_OFF_T_FMT is "d",
818 * the first if condition is never true. If APR_INT64_T_FMT
819 * is "d' then the second if condition is never true.
821 if ((sizeof(APR_OFF_T_FMT) > sizeof(APR_INT64_T_FMT)) &&
822 ((sizeof(APR_OFF_T_FMT) == 4 &&
823 fmt[0] == APR_OFF_T_FMT[0] &&
824 fmt[1] == APR_OFF_T_FMT[1]) ||
825 (sizeof(APR_OFF_T_FMT) == 3 &&
826 fmt[0] == APR_OFF_T_FMT[0]) ||
827 (sizeof(APR_OFF_T_FMT) > 4 &&
828 strncmp(fmt, APR_OFF_T_FMT,
829 sizeof(APR_OFF_T_FMT) - 2) == 0))) {
830 /* Need to account for trailing 'd' and null in sizeof() */
832 fmt += (sizeof(APR_OFF_T_FMT) - 2);
834 else if ((sizeof(APR_INT64_T_FMT) == 4 &&
835 fmt[0] == APR_INT64_T_FMT[0] &&
836 fmt[1] == APR_INT64_T_FMT[1]) ||
837 (sizeof(APR_INT64_T_FMT) == 3 &&
838 fmt[0] == APR_INT64_T_FMT[0]) ||
839 (sizeof(APR_INT64_T_FMT) > 4 &&
840 strncmp(fmt, APR_INT64_T_FMT,
841 sizeof(APR_INT64_T_FMT) - 2) == 0)) {
842 /* Need to account for trailing 'd' and null in sizeof() */
844 fmt += (sizeof(APR_INT64_T_FMT) - 2);
846 else if (*fmt == 'q') {
850 else if (*fmt == 'l') {
854 else if (*fmt == 'h') {
863 * Argument extraction and printing.
864 * First we determine the argument type.
865 * Then, we convert the argument to a string.
866 * On exit from the switch, s points to the string that
867 * must be printed, s_len has the length of the string
868 * The precision requirements, if any, are reflected in s_len.
870 * NOTE: pad_char may be set to '0' because of the 0 flag.
871 * It is reset to ' ' by non-numeric formats
875 if (var_type == IS_QUAD) {
876 i_quad = va_arg(ap, apr_uint64_t);
877 s = conv_10_quad(i_quad, 1, &is_negative,
878 &num_buf[NUM_BUF_SIZE], &s_len);
881 if (var_type == IS_LONG)
882 i_num = (apr_int32_t) va_arg(ap, apr_uint32_t);
883 else if (var_type == IS_SHORT)
884 i_num = (apr_int32_t) (unsigned short) va_arg(ap, unsigned int);
886 i_num = (apr_int32_t) va_arg(ap, unsigned int);
887 s = conv_10(i_num, 1, &is_negative,
888 &num_buf[NUM_BUF_SIZE], &s_len);
890 FIX_PRECISION(adjust_precision, precision, s, s_len);
895 if (var_type == IS_QUAD) {
896 i_quad = va_arg(ap, apr_int64_t);
897 s = conv_10_quad(i_quad, 0, &is_negative,
898 &num_buf[NUM_BUF_SIZE], &s_len);
901 if (var_type == IS_LONG)
902 i_num = va_arg(ap, apr_int32_t);
903 else if (var_type == IS_SHORT)
904 i_num = (short) va_arg(ap, int);
906 i_num = va_arg(ap, int);
907 s = conv_10(i_num, 0, &is_negative,
908 &num_buf[NUM_BUF_SIZE], &s_len);
910 FIX_PRECISION(adjust_precision, precision, s, s_len);
916 else if (print_blank)
922 if (var_type == IS_QUAD) {
923 ui_quad = va_arg(ap, apr_uint64_t);
924 s = conv_p2_quad(ui_quad, 3, *fmt,
925 &num_buf[NUM_BUF_SIZE], &s_len);
928 if (var_type == IS_LONG)
929 ui_num = va_arg(ap, apr_uint32_t);
930 else if (var_type == IS_SHORT)
931 ui_num = (unsigned short) va_arg(ap, unsigned int);
933 ui_num = va_arg(ap, unsigned int);
934 s = conv_p2(ui_num, 3, *fmt,
935 &num_buf[NUM_BUF_SIZE], &s_len);
937 FIX_PRECISION(adjust_precision, precision, s, s_len);
938 if (alternate_form && *s != '0') {
947 if (var_type == IS_QUAD) {
948 ui_quad = va_arg(ap, apr_uint64_t);
949 s = conv_p2_quad(ui_quad, 4, *fmt,
950 &num_buf[NUM_BUF_SIZE], &s_len);
953 if (var_type == IS_LONG)
954 ui_num = va_arg(ap, apr_uint32_t);
955 else if (var_type == IS_SHORT)
956 ui_num = (unsigned short) va_arg(ap, unsigned int);
958 ui_num = va_arg(ap, unsigned int);
959 s = conv_p2(ui_num, 4, *fmt,
960 &num_buf[NUM_BUF_SIZE], &s_len);
962 FIX_PRECISION(adjust_precision, precision, s, s_len);
963 if (alternate_form && ui_num != 0) {
964 *--s = *fmt; /* 'x' or 'X' */
972 s = va_arg(ap, char *);
974 if (!adjust_precision) {
978 /* From the C library standard in section 7.9.6.1:
979 * ...if the precision is specified, no more then
980 * that many characters are written. If the
981 * precision is not specified or is greater
982 * than the size of the array, the array shall
983 * contain a null character.
985 * My reading is is precision is specified and
986 * is less then or equal to the size of the
987 * array, no null character is required. So
988 * we can't do a strlen.
990 * This figures out the length of the string
991 * up to the precision. Once it's long enough
992 * for the specified precision, we don't care
995 * NOTE: you must do the length comparison
996 * before the check for the null character.
997 * Otherwise, you'll check one beyond the
998 * last valid character.
1002 for (walk = s, s_len = 0;
1003 (s_len < precision) && (*walk != '\0');
1018 fp_num = va_arg(ap, double);
1020 * We use &num_buf[ 1 ], so that we have room for the sign
1024 if (isnan(fp_num)) {
1030 if (!s && isinf(fp_num)) {
1036 s = conv_fp(*fmt, fp_num, alternate_form,
1037 (int)((adjust_precision == NO) ? FLOAT_DIGITS : precision),
1038 &is_negative, &num_buf[1], &s_len);
1041 else if (print_sign)
1043 else if (print_blank)
1051 if (adjust_precision == NO)
1052 precision = FLOAT_DIGITS;
1053 else if (precision == 0)
1056 * * We use &num_buf[ 1 ], so that we have room for the sign
1058 s = apr_gcvt(va_arg(ap, double), (int) precision, &num_buf[1],
1062 else if (print_sign)
1064 else if (print_blank)
1069 if (alternate_form && (q = strchr(s, '.')) == NULL) {
1071 s[s_len] = '\0'; /* delimit for following strchr() */
1073 if (*fmt == 'G' && (q = strchr(s, 'e')) != NULL)
1079 char_buf[0] = (char) (va_arg(ap, int));
1095 if (var_type == IS_QUAD)
1096 *(va_arg(ap, apr_int64_t *)) = cc;
1097 else if (var_type == IS_LONG)
1098 *(va_arg(ap, long *)) = cc;
1099 else if (var_type == IS_SHORT)
1100 *(va_arg(ap, short *)) = cc;
1102 *(va_arg(ap, int *)) = cc;
1103 print_something = NO;
1107 * This is where we extend the printf format, with a second
1113 * If the pointer size is equal to or smaller than the size
1114 * of the largest unsigned int, we convert the pointer to a
1115 * hex number, otherwise we print "%p" to indicate that we
1116 * don't handle "%p".
1119 #if APR_SIZEOF_VOIDP == 8
1120 if (sizeof(void *) <= sizeof(apr_uint64_t)) {
1121 ui_quad = (apr_uint64_t) va_arg(ap, void *);
1122 s = conv_p2_quad(ui_quad, 4, 'x',
1123 &num_buf[NUM_BUF_SIZE], &s_len);
1126 if (sizeof(void *) <= sizeof(apr_uint32_t)) {
1127 ui_num = (apr_uint32_t) va_arg(ap, void *);
1128 s = conv_p2(ui_num, 4, 'x',
1129 &num_buf[NUM_BUF_SIZE], &s_len);
1140 /* print an apr_sockaddr_t as a.b.c.d:port */
1145 sa = va_arg(ap, apr_sockaddr_t *);
1147 s = conv_apr_sockaddr(sa, &num_buf[NUM_BUF_SIZE], &s_len);
1148 if (adjust_precision && precision < s_len)
1159 /* print a struct in_addr as a.b.c.d */
1164 ia = va_arg(ap, struct in_addr *);
1166 s = conv_in_addr(ia, &num_buf[NUM_BUF_SIZE], &s_len);
1167 if (adjust_precision && precision < s_len)
1178 /* print the error for an apr_status_t */
1183 mrv = va_arg(ap, apr_status_t *);
1185 s = apr_strerror(*mrv, num_buf, NUM_BUF_SIZE-1);
1199 apr_os_thread_t *tid;
1201 tid = va_arg(ap, apr_os_thread_t *);
1203 s = conv_os_thread_t(tid, &num_buf[NUM_BUF_SIZE], &s_len);
1204 if (adjust_precision && precision < s_len)
1224 apr_os_thread_t *tid;
1226 tid = va_arg(ap, apr_os_thread_t *);
1228 s = conv_os_thread_t_hex(tid, &num_buf[NUM_BUF_SIZE], &s_len);
1229 if (adjust_precision && precision < s_len)
1253 apr_uint32_t *arg = va_arg(ap, apr_uint32_t *);
1254 size = (arg) ? *arg : 0;
1256 else if (*fmt == 'F') {
1257 apr_off_t *arg = va_arg(ap, apr_off_t *);
1258 size = (arg) ? *arg : 0;
1261 apr_size_t *arg = va_arg(ap, apr_size_t *);
1262 size = (arg) ? *arg : 0;
1265 s = apr_strfsize(size, buf);
1272 /* if %p ends the string, oh well ignore it */
1279 (void)va_arg(ap, void *); /* skip the bogus argument on the stack */
1286 * The last character of the format string was %.
1293 * The default case is for unrecognized %'s.
1294 * We print %<char> to help the user identify what
1295 * option is not understood.
1296 * This is also useful in case the user wants to pass
1297 * the output of format_converter to another function
1298 * that understands some other %<char> (like syslog).
1299 * Note that we can't point s inside fmt because the
1300 * unknown <char> could be preceded by width etc.
1311 if (prefix_char != NUL && s != S_NULL && s != char_buf) {
1316 if (adjust_width && adjust == RIGHT && min_width > s_len) {
1317 if (pad_char == '0' && prefix_char != NUL) {
1318 INS_CHAR(*s, sp, bep, cc);
1323 PAD(min_width, s_len, pad_char);
1327 * Print the string s.
1329 if (print_something == YES) {
1330 for (i = s_len; i != 0; i--) {
1331 INS_CHAR(*s, sp, bep, cc);
1336 if (adjust_width && adjust == LEFT && min_width > s_len)
1337 PAD(min_width, s_len, pad_char);
1347 static int snprintf_flush(apr_vformatter_buff_t *vbuff)
1349 /* if the buffer fills we have to abort immediately, there is no way
1350 * to "flush" an apr_snprintf... there's nowhere to flush it to.
1356 APR_DECLARE_NONSTD(int) apr_snprintf(char *buf, apr_size_t len,
1357 const char *format, ...)
1361 apr_vformatter_buff_t vbuff;
1364 /* NOTE: This is a special case; we just want to return the number
1365 * of chars that would be written (minus \0) if the buffer
1366 * size was infinite. We leverage the fact that INS_CHAR
1367 * just does actual inserts iff the buffer pointer is non-NULL.
1368 * In this case, we don't care what buf is; it can be NULL, since
1369 * we don't touch it at all.
1371 vbuff.curpos = NULL;
1372 vbuff.endpos = NULL;
1374 /* save one byte for nul terminator */
1376 vbuff.endpos = buf + len - 1;
1378 va_start(ap, format);
1379 cc = apr_vformatter(snprintf_flush, &vbuff, format, ap);
1382 *vbuff.curpos = '\0';
1384 return (cc == -1) ? (int)len - 1 : cc;
1388 APR_DECLARE(int) apr_vsnprintf(char *buf, apr_size_t len, const char *format,
1392 apr_vformatter_buff_t vbuff;
1395 /* See above note */
1396 vbuff.curpos = NULL;
1397 vbuff.endpos = NULL;
1399 /* save one byte for nul terminator */
1401 vbuff.endpos = buf + len - 1;
1403 cc = apr_vformatter(snprintf_flush, &vbuff, format, ap);
1405 *vbuff.curpos = '\0';
1407 return (cc == -1) ? (int)len - 1 : cc;