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> */
713 IS_QUAD, IS_LONG, IS_SHORT, IS_INT
715 enum var_type_enum var_type = IS_INT;
720 boolean_e alternate_form;
721 boolean_e print_sign;
722 boolean_e print_blank;
723 boolean_e adjust_precision;
724 boolean_e adjust_width;
732 INS_CHAR(*fmt, sp, bep, cc);
736 * Default variable settings
738 boolean_e print_something = YES;
740 alternate_form = print_sign = print_blank = NO;
747 * Try to avoid checking for flags, width or precision
749 if (!apr_islower(*fmt)) {
751 * Recognize flags: -, #, BLANK, +
756 else if (*fmt == '+')
758 else if (*fmt == '#')
759 alternate_form = YES;
760 else if (*fmt == ' ')
762 else if (*fmt == '0')
769 * Check if a width was specified
771 if (apr_isdigit(*fmt)) {
772 STR_TO_DEC(fmt, min_width);
775 else if (*fmt == '*') {
776 int v = va_arg(ap, int);
781 min_width = (apr_size_t)(-v);
784 min_width = (apr_size_t)v;
790 * Check if a precision was specified
793 adjust_precision = YES;
795 if (apr_isdigit(*fmt)) {
796 STR_TO_DEC(fmt, precision);
798 else if (*fmt == '*') {
799 int v = va_arg(ap, int);
801 precision = (v < 0) ? 0 : (apr_size_t)v;
807 adjust_precision = NO;
810 adjust_precision = adjust_width = NO;
813 * Modifier check. In same cases, APR_OFF_T_FMT can be
814 * "lld" and APR_INT64_T_FMT can be "ld" (that is, off_t is
815 * "larger" than int64). Check that case 1st.
816 * Note that if APR_OFF_T_FMT is "d",
817 * the first if condition is never true. If APR_INT64_T_FMT
818 * is "d' then the second if condition is never true.
820 if ((sizeof(APR_OFF_T_FMT) > sizeof(APR_INT64_T_FMT)) &&
821 ((sizeof(APR_OFF_T_FMT) == 4 &&
822 fmt[0] == APR_OFF_T_FMT[0] &&
823 fmt[1] == APR_OFF_T_FMT[1]) ||
824 (sizeof(APR_OFF_T_FMT) == 3 &&
825 fmt[0] == APR_OFF_T_FMT[0]) ||
826 (sizeof(APR_OFF_T_FMT) > 4 &&
827 strncmp(fmt, APR_OFF_T_FMT,
828 sizeof(APR_OFF_T_FMT) - 2) == 0))) {
829 /* Need to account for trailing 'd' and null in sizeof() */
831 fmt += (sizeof(APR_OFF_T_FMT) - 2);
833 else if ((sizeof(APR_INT64_T_FMT) == 4 &&
834 fmt[0] == APR_INT64_T_FMT[0] &&
835 fmt[1] == APR_INT64_T_FMT[1]) ||
836 (sizeof(APR_INT64_T_FMT) == 3 &&
837 fmt[0] == APR_INT64_T_FMT[0]) ||
838 (sizeof(APR_INT64_T_FMT) > 4 &&
839 strncmp(fmt, APR_INT64_T_FMT,
840 sizeof(APR_INT64_T_FMT) - 2) == 0)) {
841 /* Need to account for trailing 'd' and null in sizeof() */
843 fmt += (sizeof(APR_INT64_T_FMT) - 2);
845 else if (*fmt == 'q') {
849 else if (*fmt == 'l') {
853 else if (*fmt == 'h') {
862 * Argument extraction and printing.
863 * First we determine the argument type.
864 * Then, we convert the argument to a string.
865 * On exit from the switch, s points to the string that
866 * must be printed, s_len has the length of the string
867 * The precision requirements, if any, are reflected in s_len.
869 * NOTE: pad_char may be set to '0' because of the 0 flag.
870 * It is reset to ' ' by non-numeric formats
874 if (var_type == IS_QUAD) {
875 i_quad = va_arg(ap, apr_uint64_t);
876 s = conv_10_quad(i_quad, 1, &is_negative,
877 &num_buf[NUM_BUF_SIZE], &s_len);
880 if (var_type == IS_LONG)
881 i_num = (apr_int32_t) va_arg(ap, apr_uint32_t);
882 else if (var_type == IS_SHORT)
883 i_num = (apr_int32_t) (unsigned short) va_arg(ap, unsigned int);
885 i_num = (apr_int32_t) va_arg(ap, unsigned int);
886 s = conv_10(i_num, 1, &is_negative,
887 &num_buf[NUM_BUF_SIZE], &s_len);
889 FIX_PRECISION(adjust_precision, precision, s, s_len);
894 if (var_type == IS_QUAD) {
895 i_quad = va_arg(ap, apr_int64_t);
896 s = conv_10_quad(i_quad, 0, &is_negative,
897 &num_buf[NUM_BUF_SIZE], &s_len);
900 if (var_type == IS_LONG)
901 i_num = va_arg(ap, apr_int32_t);
902 else if (var_type == IS_SHORT)
903 i_num = (short) va_arg(ap, int);
905 i_num = va_arg(ap, int);
906 s = conv_10(i_num, 0, &is_negative,
907 &num_buf[NUM_BUF_SIZE], &s_len);
909 FIX_PRECISION(adjust_precision, precision, s, s_len);
915 else if (print_blank)
921 if (var_type == IS_QUAD) {
922 ui_quad = va_arg(ap, apr_uint64_t);
923 s = conv_p2_quad(ui_quad, 3, *fmt,
924 &num_buf[NUM_BUF_SIZE], &s_len);
927 if (var_type == IS_LONG)
928 ui_num = va_arg(ap, apr_uint32_t);
929 else if (var_type == IS_SHORT)
930 ui_num = (unsigned short) va_arg(ap, unsigned int);
932 ui_num = va_arg(ap, unsigned int);
933 s = conv_p2(ui_num, 3, *fmt,
934 &num_buf[NUM_BUF_SIZE], &s_len);
936 FIX_PRECISION(adjust_precision, precision, s, s_len);
937 if (alternate_form && *s != '0') {
946 if (var_type == IS_QUAD) {
947 ui_quad = va_arg(ap, apr_uint64_t);
948 s = conv_p2_quad(ui_quad, 4, *fmt,
949 &num_buf[NUM_BUF_SIZE], &s_len);
952 if (var_type == IS_LONG)
953 ui_num = va_arg(ap, apr_uint32_t);
954 else if (var_type == IS_SHORT)
955 ui_num = (unsigned short) va_arg(ap, unsigned int);
957 ui_num = va_arg(ap, unsigned int);
958 s = conv_p2(ui_num, 4, *fmt,
959 &num_buf[NUM_BUF_SIZE], &s_len);
961 FIX_PRECISION(adjust_precision, precision, s, s_len);
962 if (alternate_form && ui_num != 0) {
963 *--s = *fmt; /* 'x' or 'X' */
971 s = va_arg(ap, char *);
973 if (!adjust_precision) {
977 /* From the C library standard in section 7.9.6.1:
978 * ...if the precision is specified, no more then
979 * that many characters are written. If the
980 * precision is not specified or is greater
981 * than the size of the array, the array shall
982 * contain a null character.
984 * My reading is is precision is specified and
985 * is less then or equal to the size of the
986 * array, no null character is required. So
987 * we can't do a strlen.
989 * This figures out the length of the string
990 * up to the precision. Once it's long enough
991 * for the specified precision, we don't care
994 * NOTE: you must do the length comparison
995 * before the check for the null character.
996 * Otherwise, you'll check one beyond the
997 * last valid character.
1001 for (walk = s, s_len = 0;
1002 (s_len < precision) && (*walk != '\0');
1017 fp_num = va_arg(ap, double);
1019 * We use &num_buf[ 1 ], so that we have room for the sign
1023 if (isnan(fp_num)) {
1029 if (!s && isinf(fp_num)) {
1035 s = conv_fp(*fmt, fp_num, alternate_form,
1036 (int)((adjust_precision == NO) ? FLOAT_DIGITS : precision),
1037 &is_negative, &num_buf[1], &s_len);
1040 else if (print_sign)
1042 else if (print_blank)
1050 if (adjust_precision == NO)
1051 precision = FLOAT_DIGITS;
1052 else if (precision == 0)
1055 * * We use &num_buf[ 1 ], so that we have room for the sign
1057 s = apr_gcvt(va_arg(ap, double), (int) precision, &num_buf[1],
1061 else if (print_sign)
1063 else if (print_blank)
1068 if (alternate_form && (q = strchr(s, '.')) == NULL) {
1070 s[s_len] = '\0'; /* delimit for following strchr() */
1072 if (*fmt == 'G' && (q = strchr(s, 'e')) != NULL)
1078 char_buf[0] = (char) (va_arg(ap, int));
1094 if (var_type == IS_QUAD)
1095 *(va_arg(ap, apr_int64_t *)) = cc;
1096 else if (var_type == IS_LONG)
1097 *(va_arg(ap, long *)) = cc;
1098 else if (var_type == IS_SHORT)
1099 *(va_arg(ap, short *)) = cc;
1101 *(va_arg(ap, int *)) = cc;
1102 print_something = NO;
1106 * This is where we extend the printf format, with a second
1112 * If the pointer size is equal to or smaller than the size
1113 * of the largest unsigned int, we convert the pointer to a
1114 * hex number, otherwise we print "%p" to indicate that we
1115 * don't handle "%p".
1118 #if APR_SIZEOF_VOIDP == 8
1119 if (sizeof(void *) <= sizeof(apr_uint64_t)) {
1120 ui_quad = (apr_uint64_t) va_arg(ap, void *);
1121 s = conv_p2_quad(ui_quad, 4, 'x',
1122 &num_buf[NUM_BUF_SIZE], &s_len);
1125 if (sizeof(void *) <= sizeof(apr_uint32_t)) {
1126 ui_num = (apr_uint32_t) va_arg(ap, void *);
1127 s = conv_p2(ui_num, 4, 'x',
1128 &num_buf[NUM_BUF_SIZE], &s_len);
1139 /* print an apr_sockaddr_t as a.b.c.d:port */
1144 sa = va_arg(ap, apr_sockaddr_t *);
1146 s = conv_apr_sockaddr(sa, &num_buf[NUM_BUF_SIZE], &s_len);
1147 if (adjust_precision && precision < s_len)
1158 /* print a struct in_addr as a.b.c.d */
1163 ia = va_arg(ap, struct in_addr *);
1165 s = conv_in_addr(ia, &num_buf[NUM_BUF_SIZE], &s_len);
1166 if (adjust_precision && precision < s_len)
1177 /* print the error for an apr_status_t */
1182 mrv = va_arg(ap, apr_status_t *);
1184 s = apr_strerror(*mrv, num_buf, NUM_BUF_SIZE-1);
1198 apr_os_thread_t *tid;
1200 tid = va_arg(ap, apr_os_thread_t *);
1202 s = conv_os_thread_t(tid, &num_buf[NUM_BUF_SIZE], &s_len);
1203 if (adjust_precision && precision < s_len)
1223 apr_os_thread_t *tid;
1225 tid = va_arg(ap, apr_os_thread_t *);
1227 s = conv_os_thread_t_hex(tid, &num_buf[NUM_BUF_SIZE], &s_len);
1228 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;