| 822 | */ |
| 823 | |
| 824 | int my_vfprintf(FILE *stream, const char* format, va_list args) |
| 825 | { |
| 826 | char cvtbuf[1024]; |
| 827 | int alloc= 0; |
| 828 | char *p= cvtbuf; |
| 829 | size_t cur_len= sizeof(cvtbuf), actual; |
| 830 | int ret; |
| 831 | |
| 832 | /* |
| 833 | We do not know how much buffer we need. |
| 834 | So start with a reasonably-sized stack-allocated buffer, and increase |
| 835 | it exponentially until it is big enough. |
| 836 | */ |
| 837 | for (;;) |
| 838 | { |
| 839 | size_t new_len; |
| 840 | actual= my_vsnprintf(p, cur_len, format, args); |
| 841 | if (actual < cur_len - 1) |
| 842 | break; |
| 843 | /* |
| 844 | Not enough space (or just enough with nothing to spare - but we cannot |
| 845 | distinguish this case from the return value). Allocate a bigger buffer |
| 846 | and try again. |
| 847 | */ |
| 848 | if (alloc) |
| 849 | my_free(p); |
| 850 | else |
| 851 | alloc= 1; |
| 852 | new_len= cur_len*2; |
| 853 | if (new_len < cur_len) |
| 854 | return 0; /* Overflow */ |
| 855 | cur_len= new_len; |
| 856 | p= my_malloc(PSI_INSTRUMENT_ME, cur_len, MYF(MY_FAE)); |
| 857 | if (!p) |
| 858 | return 0; |
| 859 | } |
| 860 | ret= (int) actual; |
| 861 | if (fputs(p, stream) < 0) |
| 862 | ret= -1; |
| 863 | if (alloc) |
| 864 | my_free(p); |
| 865 | return ret; |
| 866 | } |
| 867 | |
| 868 | int my_fprintf(FILE *stream, const char* format, ...) |
| 869 | { |
no test coverage detected