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);
108 while (p1 > &buf[0] && fi != 0) {
109 fj = modf(fi / 10, &fi);
110 *--p1 = (int) ((fj + .03) * 10) + '0';
113 while (p1 < &buf[NDIG])
117 while ((fj = arg * 10) < 1) {
131 while (p <= p1 && p < &buf[NDIG]) {
133 arg = modf(arg, &fj);
134 *p++ = (int) fj + '0';
136 if (p1 >= &buf[NDIG]) {
137 buf[NDIG - 1] = '\0';
160 static char *apr_ecvt(double arg, int ndigits, int *decpt, int *sign, char *buf)
162 return (apr_cvt(arg, ndigits, decpt, sign, 1, buf));
165 static char *apr_fcvt(double arg, int ndigits, int *decpt, int *sign, char *buf)
167 return (apr_cvt(arg, ndigits, decpt, sign, 0, buf));
171 * apr_gcvt - Floating output conversion to
172 * minimal length string
175 static char *apr_gcvt(double number, int ndigit, char *buf, boolean_e altform)
178 register char *p1, *p2;
182 p1 = apr_ecvt(number, ndigit, &decpt, &sign, buf1);
186 for (i = ndigit - 1; i > 0 && p1[i] == '0'; i--)
188 if ((decpt >= 0 && decpt - ndigit > 4)
189 || (decpt < 0 && decpt < -3)) { /* use E-style */
193 for (i = 1; i < ndigit; i++)
203 *p2++ = decpt / 100 + '0';
205 *p2++ = (decpt % 100) / 10 + '0';
206 *p2++ = decpt % 10 + '0';
217 for (i = 1; i <= ndigit; i++) {
222 if (ndigit < decpt) {
223 while (ndigit++ < decpt)
228 if (p2[-1] == '.' && !altform)
235 * The INS_CHAR macro inserts a character in the buffer and writes
236 * the buffer back to disk if necessary
237 * It uses the char pointers sp and bep:
238 * sp points to the next available character in the buffer
239 * bep points to the end-of-buffer+1
240 * While using this macro, note that the nextb pointer is NOT updated.
242 * NOTE: Evaluation of the c argument should not have any side-effects
244 #define INS_CHAR(c, sp, bep, cc) \
248 vbuff->curpos = sp; \
249 if (flush_func(vbuff)) \
251 sp = vbuff->curpos; \
252 bep = vbuff->endpos; \
259 #define NUM(c) (c - '0')
261 #define STR_TO_DEC(str, num) \
263 while (apr_isdigit(*str)) \
266 num += NUM(*str++); \
270 * This macro does zero padding so that the precision
271 * requirement is satisfied. The padding is done by
272 * adding '0's to the left of the string that is going
273 * to be printed. We don't allow precision to be large
274 * enough that we continue past the start of s.
276 * NOTE: this makes use of the magic info that s is
277 * always based on num_buf with a size of NUM_BUF_SIZE.
279 #define FIX_PRECISION(adjust, precision, s, s_len) \
281 apr_size_t p = (precision + 1 < NUM_BUF_SIZE) \
282 ? precision : NUM_BUF_SIZE - 1; \
291 * Macro that does padding. The padding is done by printing
294 #define PAD(width, len, ch) \
297 INS_CHAR(ch, sp, bep, cc); \
303 * Prefix the character ch to the string str
305 * Set the has_prefix flag
307 #define PREFIX(str, length, ch) \
314 * Convert num to its decimal format.
316 * - a pointer to a string containing the number (no sign)
317 * - len contains the length of the string
318 * - is_negative is set to TRUE or FALSE depending on the sign
319 * of the number (always set to FALSE if is_unsigned is TRUE)
321 * The caller provides a buffer for the string: that is the buf_end argument
322 * which is a pointer to the END of the buffer + 1 (i.e. if the buffer
323 * is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
325 * Note: we have 2 versions. One is used when we need to use quads
326 * (conv_10_quad), the other when we don't (conv_10). We're assuming the
329 static char *conv_10(register apr_int32_t num, register int is_unsigned,
330 register int *is_negative, char *buf_end,
331 register apr_size_t *len)
333 register char *p = buf_end;
334 register apr_uint32_t magnitude = num;
337 *is_negative = FALSE;
340 *is_negative = (num < 0);
343 * On a 2's complement machine, negating the most negative integer
344 * results in a number that cannot be represented as a signed integer.
345 * Here is what we do to obtain the number's magnitude:
346 * a. add 1 to the number
347 * b. negate it (becomes positive)
348 * c. convert it to unsigned
352 apr_int32_t t = num + 1;
353 magnitude = ((apr_uint32_t) -t) + 1;
358 * We use a do-while loop so that we write at least 1 digit
361 register apr_uint32_t new_magnitude = magnitude / 10;
363 *--p = (char) (magnitude - new_magnitude * 10 + '0');
364 magnitude = new_magnitude;
372 static char *conv_10_quad(apr_int64_t num, register int is_unsigned,
373 register int *is_negative, char *buf_end,
374 register apr_size_t *len)
376 register char *p = buf_end;
377 apr_uint64_t magnitude = num;
380 * We see if we can use the faster non-quad version by checking the
381 * number against the largest long value it can be. If <=, we
382 * punt to the quicker version.
384 if ((magnitude <= APR_UINT32_MAX && is_unsigned)
385 || (num <= APR_INT32_MAX && num >= APR_INT32_MIN && !is_unsigned))
386 return(conv_10((apr_int32_t)num, is_unsigned, is_negative, buf_end, len));
389 *is_negative = FALSE;
392 *is_negative = (num < 0);
395 * On a 2's complement machine, negating the most negative integer
396 * results in a number that cannot be represented as a signed integer.
397 * Here is what we do to obtain the number's magnitude:
398 * a. add 1 to the number
399 * b. negate it (becomes positive)
400 * c. convert it to unsigned
404 apr_int64_t t = num + 1;
405 magnitude = ((apr_uint64_t) -t) + 1;
410 * We use a do-while loop so that we write at least 1 digit
413 apr_uint64_t new_magnitude = magnitude / 10;
415 *--p = (char) (magnitude - new_magnitude * 10 + '0');
416 magnitude = new_magnitude;
424 static char *conv_in_addr(struct in_addr *ia, char *buf_end, apr_size_t *len)
426 unsigned addr = ntohl(ia->s_addr);
431 p = conv_10((addr & 0x000000FF) , TRUE, &is_negative, p, &sub_len);
433 p = conv_10((addr & 0x0000FF00) >> 8, TRUE, &is_negative, p, &sub_len);
435 p = conv_10((addr & 0x00FF0000) >> 16, TRUE, &is_negative, p, &sub_len);
437 p = conv_10((addr & 0xFF000000) >> 24, TRUE, &is_negative, p, &sub_len);
444 /* Must be passed a buffer of size NUM_BUF_SIZE where buf_end points
445 * to 1 byte past the end of the buffer. */
446 static char *conv_apr_sockaddr(apr_sockaddr_t *sa, char *buf_end, apr_size_t *len)
453 p = conv_10(sa->port, TRUE, &is_negative, p, &sub_len);
455 ipaddr_str = buf_end - NUM_BUF_SIZE;
456 if (apr_sockaddr_ip_getbuf(ipaddr_str, sa->addr_str_len, sa)) {
457 /* Should only fail if the buffer is too small, which it
458 * should not be; but fail safe anyway: */
463 sub_len = strlen(ipaddr_str);
465 if (sa->family == APR_INET6 &&
466 !IN6_IS_ADDR_V4MAPPED(&sa->sa.sin6.sin6_addr)) {
470 memcpy(p + 1, ipaddr_str, sub_len);
476 memcpy(p, ipaddr_str, sub_len);
486 static char *conv_os_thread_t(apr_os_thread_t *tid, char *buf_end, apr_size_t *len)
496 switch(sizeof(u.tid)) {
497 case sizeof(apr_int32_t):
498 return conv_10(u.u32, TRUE, &is_negative, buf_end, len);
499 case sizeof(apr_int64_t):
500 return conv_10_quad(u.u64, TRUE, &is_negative, buf_end, len);
502 /* not implemented; stick 0 in the buffer */
503 return conv_10(0, TRUE, &is_negative, buf_end, len);
511 * Convert a floating point number to a string formats 'f', 'e' or 'E'.
512 * The result is placed in buf, and len denotes the length of the string
513 * The sign is returned in the is_negative argument (and is not placed
516 static char *conv_fp(register char format, register double num,
517 boolean_e add_dp, int precision, int *is_negative,
518 char *buf, apr_size_t *len)
520 register char *s = buf;
526 p = apr_fcvt(num, precision, &decimal_point, is_negative, buf1);
527 else /* either e or E format */
528 p = apr_ecvt(num, precision + 1, &decimal_point, is_negative, buf1);
531 * Check for Infinity and NaN
533 if (apr_isalpha(*p)) {
535 memcpy(buf, p, *len + 1);
536 *is_negative = FALSE;
541 if (decimal_point <= 0) {
545 while (decimal_point++ < 0)
552 while (decimal_point-- > 0)
554 if (precision > 0 || add_dp)
560 if (precision > 0 || add_dp)
565 * copy the rest of p, the NUL is NOT copied
571 char temp[EXPONENT_LENGTH]; /* for exponent conversion */
573 int exponent_is_negative;
575 *s++ = format; /* either e or E */
577 if (decimal_point != 0) {
578 p = conv_10((apr_int32_t) decimal_point, FALSE, &exponent_is_negative,
579 &temp[EXPONENT_LENGTH], &t_len);
580 *s++ = exponent_is_negative ? '-' : '+';
583 * Make sure the exponent has at least 2 digits
603 * Convert num to a base X number where X is a power of 2. nbits determines X.
604 * For example, if nbits is 3, we do base 8 conversion
606 * a pointer to a string containing the number
608 * The caller provides a buffer for the string: that is the buf_end argument
609 * which is a pointer to the END of the buffer + 1 (i.e. if the buffer
610 * is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
612 * As with conv_10, we have a faster version which is used when
613 * the number isn't quad size.
615 static char *conv_p2(register apr_uint32_t num, register int nbits,
616 char format, char *buf_end, register apr_size_t *len)
618 register int mask = (1 << nbits) - 1;
619 register char *p = buf_end;
620 static const char low_digits[] = "0123456789abcdef";
621 static const char upper_digits[] = "0123456789ABCDEF";
622 register const char *digits = (format == 'X') ? upper_digits : low_digits;
625 *--p = digits[num & mask];
634 static char *conv_p2_quad(apr_uint64_t num, register int nbits,
635 char format, char *buf_end, register apr_size_t *len)
637 register int mask = (1 << nbits) - 1;
638 register char *p = buf_end;
639 static const char low_digits[] = "0123456789abcdef";
640 static const char upper_digits[] = "0123456789ABCDEF";
641 register const char *digits = (format == 'X') ? upper_digits : low_digits;
643 if (num <= APR_UINT32_MAX)
644 return(conv_p2((apr_uint32_t)num, nbits, format, buf_end, len));
647 *--p = digits[num & mask];
657 static char *conv_os_thread_t_hex(apr_os_thread_t *tid, char *buf_end, apr_size_t *len)
667 switch(sizeof(u.tid)) {
668 case sizeof(apr_int32_t):
669 return conv_p2(u.u32, 4, 'x', buf_end, len);
670 case sizeof(apr_int64_t):
671 return conv_p2_quad(u.u64, 4, 'x', buf_end, len);
673 /* not implemented; stick 0 in the buffer */
674 return conv_10(0, TRUE, &is_negative, buf_end, len);
680 * Do format conversion placing the output in buffer
682 APR_DECLARE(int) apr_vformatter(int (*flush_func)(apr_vformatter_buff_t *),
683 apr_vformatter_buff_t *vbuff, const char *fmt, va_list ap)
688 register apr_size_t i;
690 register char *s = NULL;
692 apr_size_t s_len = 0;
694 register apr_size_t min_width = 0;
695 apr_size_t precision = 0;
703 apr_int64_t i_quad = 0;
704 apr_uint64_t ui_quad;
705 apr_int32_t i_num = 0;
706 apr_uint32_t ui_num = 0;
708 char num_buf[NUM_BUF_SIZE];
709 char char_buf[2]; /* for printing %% and %<unknown> */
712 IS_QUAD, IS_LONG, IS_SHORT, IS_INT
714 enum var_type_enum var_type = IS_INT;
719 boolean_e alternate_form;
720 boolean_e print_sign;
721 boolean_e print_blank;
722 boolean_e adjust_precision;
723 boolean_e adjust_width;
731 INS_CHAR(*fmt, sp, bep, cc);
735 * Default variable settings
737 boolean_e print_something = YES;
739 alternate_form = print_sign = print_blank = NO;
746 * Try to avoid checking for flags, width or precision
748 if (!apr_islower(*fmt)) {
750 * Recognize flags: -, #, BLANK, +
755 else if (*fmt == '+')
757 else if (*fmt == '#')
758 alternate_form = YES;
759 else if (*fmt == ' ')
761 else if (*fmt == '0')
768 * Check if a width was specified
770 if (apr_isdigit(*fmt)) {
771 STR_TO_DEC(fmt, min_width);
774 else if (*fmt == '*') {
775 int v = va_arg(ap, int);
780 min_width = (apr_size_t)(-v);
783 min_width = (apr_size_t)v;
789 * Check if a precision was specified
792 adjust_precision = YES;
794 if (apr_isdigit(*fmt)) {
795 STR_TO_DEC(fmt, precision);
797 else if (*fmt == '*') {
798 int v = va_arg(ap, int);
800 precision = (v < 0) ? 0 : (apr_size_t)v;
806 adjust_precision = NO;
809 adjust_precision = adjust_width = NO;
812 * Modifier check. In same cases, APR_OFF_T_FMT can be
813 * "lld" and APR_INT64_T_FMT can be "ld" (that is, off_t is
814 * "larger" than int64). Check that case 1st.
815 * Note that if APR_OFF_T_FMT is "d",
816 * the first if condition is never true. If APR_INT64_T_FMT
817 * is "d' then the second if condition is never true.
819 if ((sizeof(APR_OFF_T_FMT) > sizeof(APR_INT64_T_FMT)) &&
820 ((sizeof(APR_OFF_T_FMT) == 4 &&
821 fmt[0] == APR_OFF_T_FMT[0] &&
822 fmt[1] == APR_OFF_T_FMT[1]) ||
823 (sizeof(APR_OFF_T_FMT) == 3 &&
824 fmt[0] == APR_OFF_T_FMT[0]) ||
825 (sizeof(APR_OFF_T_FMT) > 4 &&
826 strncmp(fmt, APR_OFF_T_FMT,
827 sizeof(APR_OFF_T_FMT) - 2) == 0))) {
828 /* Need to account for trailing 'd' and null in sizeof() */
830 fmt += (sizeof(APR_OFF_T_FMT) - 2);
832 else if ((sizeof(APR_INT64_T_FMT) == 4 &&
833 fmt[0] == APR_INT64_T_FMT[0] &&
834 fmt[1] == APR_INT64_T_FMT[1]) ||
835 (sizeof(APR_INT64_T_FMT) == 3 &&
836 fmt[0] == APR_INT64_T_FMT[0]) ||
837 (sizeof(APR_INT64_T_FMT) > 4 &&
838 strncmp(fmt, APR_INT64_T_FMT,
839 sizeof(APR_INT64_T_FMT) - 2) == 0)) {
840 /* Need to account for trailing 'd' and null in sizeof() */
842 fmt += (sizeof(APR_INT64_T_FMT) - 2);
844 else if (*fmt == 'q') {
848 else if (*fmt == 'l') {
852 else if (*fmt == 'h') {
861 * Argument extraction and printing.
862 * First we determine the argument type.
863 * Then, we convert the argument to a string.
864 * On exit from the switch, s points to the string that
865 * must be printed, s_len has the length of the string
866 * The precision requirements, if any, are reflected in s_len.
868 * NOTE: pad_char may be set to '0' because of the 0 flag.
869 * It is reset to ' ' by non-numeric formats
873 if (var_type == IS_QUAD) {
874 i_quad = va_arg(ap, apr_uint64_t);
875 s = conv_10_quad(i_quad, 1, &is_negative,
876 &num_buf[NUM_BUF_SIZE], &s_len);
879 if (var_type == IS_LONG)
880 i_num = (apr_int32_t) va_arg(ap, apr_uint32_t);
881 else if (var_type == IS_SHORT)
882 i_num = (apr_int32_t) (unsigned short) va_arg(ap, unsigned int);
884 i_num = (apr_int32_t) va_arg(ap, unsigned int);
885 s = conv_10(i_num, 1, &is_negative,
886 &num_buf[NUM_BUF_SIZE], &s_len);
888 FIX_PRECISION(adjust_precision, precision, s, s_len);
893 if (var_type == IS_QUAD) {
894 i_quad = va_arg(ap, apr_int64_t);
895 s = conv_10_quad(i_quad, 0, &is_negative,
896 &num_buf[NUM_BUF_SIZE], &s_len);
899 if (var_type == IS_LONG)
900 i_num = va_arg(ap, apr_int32_t);
901 else if (var_type == IS_SHORT)
902 i_num = (short) va_arg(ap, int);
904 i_num = va_arg(ap, int);
905 s = conv_10(i_num, 0, &is_negative,
906 &num_buf[NUM_BUF_SIZE], &s_len);
908 FIX_PRECISION(adjust_precision, precision, s, s_len);
914 else if (print_blank)
920 if (var_type == IS_QUAD) {
921 ui_quad = va_arg(ap, apr_uint64_t);
922 s = conv_p2_quad(ui_quad, 3, *fmt,
923 &num_buf[NUM_BUF_SIZE], &s_len);
926 if (var_type == IS_LONG)
927 ui_num = va_arg(ap, apr_uint32_t);
928 else if (var_type == IS_SHORT)
929 ui_num = (unsigned short) va_arg(ap, unsigned int);
931 ui_num = va_arg(ap, unsigned int);
932 s = conv_p2(ui_num, 3, *fmt,
933 &num_buf[NUM_BUF_SIZE], &s_len);
935 FIX_PRECISION(adjust_precision, precision, s, s_len);
936 if (alternate_form && *s != '0') {
945 if (var_type == IS_QUAD) {
946 ui_quad = va_arg(ap, apr_uint64_t);
947 s = conv_p2_quad(ui_quad, 4, *fmt,
948 &num_buf[NUM_BUF_SIZE], &s_len);
951 if (var_type == IS_LONG)
952 ui_num = va_arg(ap, apr_uint32_t);
953 else if (var_type == IS_SHORT)
954 ui_num = (unsigned short) va_arg(ap, unsigned int);
956 ui_num = va_arg(ap, unsigned int);
957 s = conv_p2(ui_num, 4, *fmt,
958 &num_buf[NUM_BUF_SIZE], &s_len);
960 FIX_PRECISION(adjust_precision, precision, s, s_len);
961 if (alternate_form && ui_num != 0) {
962 *--s = *fmt; /* 'x' or 'X' */
970 s = va_arg(ap, char *);
972 if (!adjust_precision) {
976 /* From the C library standard in section 7.9.6.1:
977 * ...if the precision is specified, no more then
978 * that many characters are written. If the
979 * precision is not specified or is greater
980 * than the size of the array, the array shall
981 * contain a null character.
983 * My reading is is precision is specified and
984 * is less then or equal to the size of the
985 * array, no null character is required. So
986 * we can't do a strlen.
988 * This figures out the length of the string
989 * up to the precision. Once it's long enough
990 * for the specified precision, we don't care
993 * NOTE: you must do the length comparison
994 * before the check for the null character.
995 * Otherwise, you'll check one beyond the
996 * last valid character.
1000 for (walk = s, s_len = 0;
1001 (s_len < precision) && (*walk != '\0');
1016 fp_num = va_arg(ap, double);
1018 * We use &num_buf[ 1 ], so that we have room for the sign
1022 if (isnan(fp_num)) {
1028 if (!s && isinf(fp_num)) {
1034 s = conv_fp(*fmt, fp_num, alternate_form,
1035 (int)((adjust_precision == NO) ? FLOAT_DIGITS : precision),
1036 &is_negative, &num_buf[1], &s_len);
1039 else if (print_sign)
1041 else if (print_blank)
1049 if (adjust_precision == NO)
1050 precision = FLOAT_DIGITS;
1051 else if (precision == 0)
1054 * * We use &num_buf[ 1 ], so that we have room for the sign
1056 s = apr_gcvt(va_arg(ap, double), (int) precision, &num_buf[1],
1060 else if (print_sign)
1062 else if (print_blank)
1067 if (alternate_form && (q = strchr(s, '.')) == NULL) {
1069 s[s_len] = '\0'; /* delimit for following strchr() */
1071 if (*fmt == 'G' && (q = strchr(s, 'e')) != NULL)
1077 char_buf[0] = (char) (va_arg(ap, int));
1093 if (var_type == IS_QUAD)
1094 *(va_arg(ap, apr_int64_t *)) = cc;
1095 else if (var_type == IS_LONG)
1096 *(va_arg(ap, long *)) = cc;
1097 else if (var_type == IS_SHORT)
1098 *(va_arg(ap, short *)) = cc;
1100 *(va_arg(ap, int *)) = cc;
1101 print_something = NO;
1105 * This is where we extend the printf format, with a second
1111 * If the pointer size is equal to or smaller than the size
1112 * of the largest unsigned int, we convert the pointer to a
1113 * hex number, otherwise we print "%p" to indicate that we
1114 * don't handle "%p".
1117 #if APR_SIZEOF_VOIDP == 8
1118 if (sizeof(void *) <= sizeof(apr_uint64_t)) {
1119 ui_quad = (apr_uint64_t) va_arg(ap, void *);
1120 s = conv_p2_quad(ui_quad, 4, 'x',
1121 &num_buf[NUM_BUF_SIZE], &s_len);
1124 if (sizeof(void *) <= sizeof(apr_uint32_t)) {
1125 ui_num = (apr_uint32_t) va_arg(ap, void *);
1126 s = conv_p2(ui_num, 4, 'x',
1127 &num_buf[NUM_BUF_SIZE], &s_len);
1138 /* print an apr_sockaddr_t as a.b.c.d:port */
1143 sa = va_arg(ap, apr_sockaddr_t *);
1145 s = conv_apr_sockaddr(sa, &num_buf[NUM_BUF_SIZE], &s_len);
1146 if (adjust_precision && precision < s_len)
1157 /* print a struct in_addr as a.b.c.d */
1162 ia = va_arg(ap, struct in_addr *);
1164 s = conv_in_addr(ia, &num_buf[NUM_BUF_SIZE], &s_len);
1165 if (adjust_precision && precision < s_len)
1176 /* print the error for an apr_status_t */
1181 mrv = va_arg(ap, apr_status_t *);
1183 s = apr_strerror(*mrv, num_buf, NUM_BUF_SIZE-1);
1197 apr_os_thread_t *tid;
1199 tid = va_arg(ap, apr_os_thread_t *);
1201 s = conv_os_thread_t(tid, &num_buf[NUM_BUF_SIZE], &s_len);
1202 if (adjust_precision && precision < s_len)
1222 apr_os_thread_t *tid;
1224 tid = va_arg(ap, apr_os_thread_t *);
1226 s = conv_os_thread_t_hex(tid, &num_buf[NUM_BUF_SIZE], &s_len);
1227 if (adjust_precision && precision < s_len)
1252 apr_uint32_t *arg = va_arg(ap, apr_uint32_t *);
1253 size = (arg) ? *arg : 0;
1255 else if (*fmt == 'F') {
1256 apr_off_t *arg = va_arg(ap, apr_off_t *);
1257 size = (arg) ? *arg : 0;
1260 apr_size_t *arg = va_arg(ap, apr_size_t *);
1261 size = (arg) ? *arg : 0;
1264 s = apr_strfsize(size, buf);
1271 /* if %p ends the string, oh well ignore it */
1278 (void)va_arg(ap, void *); /* skip the bogus argument on the stack */
1285 * The last character of the format string was %.
1292 * The default case is for unrecognized %'s.
1293 * We print %<char> to help the user identify what
1294 * option is not understood.
1295 * This is also useful in case the user wants to pass
1296 * the output of format_converter to another function
1297 * that understands some other %<char> (like syslog).
1298 * Note that we can't point s inside fmt because the
1299 * unknown <char> could be preceded by width etc.
1310 if (prefix_char != NUL && s != S_NULL && s != char_buf) {
1315 if (adjust_width && adjust == RIGHT && min_width > s_len) {
1316 if (pad_char == '0' && prefix_char != NUL) {
1317 INS_CHAR(*s, sp, bep, cc);
1322 PAD(min_width, s_len, pad_char);
1326 * Print the string s.
1328 if (print_something == YES) {
1329 for (i = s_len; i != 0; i--) {
1330 INS_CHAR(*s, sp, bep, cc);
1335 if (adjust_width && adjust == LEFT && min_width > s_len)
1336 PAD(min_width, s_len, pad_char);
1346 static int snprintf_flush(apr_vformatter_buff_t *vbuff)
1348 /* if the buffer fills we have to abort immediately, there is no way
1349 * to "flush" an apr_snprintf... there's nowhere to flush it to.
1355 APR_DECLARE_NONSTD(int) apr_snprintf(char *buf, apr_size_t len,
1356 const char *format, ...)
1360 apr_vformatter_buff_t vbuff;
1363 /* NOTE: This is a special case; we just want to return the number
1364 * of chars that would be written (minus \0) if the buffer
1365 * size was infinite. We leverage the fact that INS_CHAR
1366 * just does actual inserts iff the buffer pointer is non-NULL.
1367 * In this case, we don't care what buf is; it can be NULL, since
1368 * we don't touch it at all.
1370 vbuff.curpos = NULL;
1371 vbuff.endpos = NULL;
1373 /* save one byte for nul terminator */
1375 vbuff.endpos = buf + len - 1;
1377 va_start(ap, format);
1378 cc = apr_vformatter(snprintf_flush, &vbuff, format, ap);
1381 *vbuff.curpos = '\0';
1383 return (cc == -1) ? (int)len - 1 : cc;
1387 APR_DECLARE(int) apr_vsnprintf(char *buf, apr_size_t len, const char *format,
1391 apr_vformatter_buff_t vbuff;
1394 /* See above note */
1395 vbuff.curpos = NULL;
1396 vbuff.endpos = NULL;
1398 /* save one byte for nul terminator */
1400 vbuff.endpos = buf + len - 1;
1402 cc = apr_vformatter(snprintf_flush, &vbuff, format, ap);
1404 *vbuff.curpos = '\0';
1406 return (cc == -1) ? (int)len - 1 : cc;