diff options
Diffstat (limited to 'coreutils/printf.c')
| -rw-r--r-- | coreutils/printf.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/coreutils/printf.c b/coreutils/printf.c index 4edcfa9b5..379c00cc6 100644 --- a/coreutils/printf.c +++ b/coreutils/printf.c | |||
| @@ -152,6 +152,71 @@ static double my_xstrtod(const char *arg) | |||
| 152 | return result; | 152 | return result; |
| 153 | } | 153 | } |
| 154 | 154 | ||
| 155 | #if ENABLE_PLATFORM_MINGW32 | ||
| 156 | static int buflen = 0; | ||
| 157 | static int bufmax = 0; | ||
| 158 | static char *buffer = NULL; | ||
| 159 | |||
| 160 | static void my_flush(void) | ||
| 161 | { | ||
| 162 | if (buffer) | ||
| 163 | full_write(STDOUT_FILENO, buffer, buflen); | ||
| 164 | free(buffer); | ||
| 165 | buffer = NULL; | ||
| 166 | buflen = bufmax = 0; | ||
| 167 | } | ||
| 168 | |||
| 169 | static void copy_to_buffer(const char *str, int len) | ||
| 170 | { | ||
| 171 | char *newbuf; | ||
| 172 | |||
| 173 | if (buflen + len >= bufmax) { | ||
| 174 | bufmax += 256 + len; | ||
| 175 | if ((newbuf = realloc(buffer, bufmax)) == NULL) { | ||
| 176 | my_flush(); | ||
| 177 | return; | ||
| 178 | } | ||
| 179 | buffer = newbuf; | ||
| 180 | } | ||
| 181 | memcpy(buffer + buflen, str, len); | ||
| 182 | buflen += len; | ||
| 183 | |||
| 184 | if (buflen > 40 && buffer[buflen-1] == '\n') | ||
| 185 | my_flush(); | ||
| 186 | } | ||
| 187 | |||
| 188 | static int my_putchar(int ch) | ||
| 189 | { | ||
| 190 | char str[1] = { (char)ch }; | ||
| 191 | copy_to_buffer(str, 1); | ||
| 192 | return ch; | ||
| 193 | } | ||
| 194 | |||
| 195 | static int my_printf(const char *format, ...) | ||
| 196 | { | ||
| 197 | va_list list; | ||
| 198 | char *str; | ||
| 199 | int len; | ||
| 200 | |||
| 201 | va_start(list, format); | ||
| 202 | len = vasprintf(&str, format, list); | ||
| 203 | va_end(list); | ||
| 204 | |||
| 205 | if (len >= 0) { | ||
| 206 | copy_to_buffer(str, len); | ||
| 207 | free(str); | ||
| 208 | } | ||
| 209 | return len; | ||
| 210 | } | ||
| 211 | |||
| 212 | #undef bb_putchar | ||
| 213 | #undef putchar | ||
| 214 | #undef printf | ||
| 215 | #define bb_putchar(c) my_putchar(c) | ||
| 216 | #define putchar(c) my_putchar(c) | ||
| 217 | #define printf(...) my_printf(__VA_ARGS__) | ||
| 218 | #endif | ||
| 219 | |||
| 155 | /* Handles %b; return 1 if output is to be short-circuited by \c */ | 220 | /* Handles %b; return 1 if output is to be short-circuited by \c */ |
| 156 | static int print_esc_string(const char *str) | 221 | static int print_esc_string(const char *str) |
| 157 | { | 222 | { |
| @@ -444,6 +509,9 @@ int printf_main(int argc UNUSED_PARAM, char **argv) | |||
| 444 | do { | 509 | do { |
| 445 | argv = argv2; | 510 | argv = argv2; |
| 446 | argv2 = print_formatted(format, argv, &conv_err); | 511 | argv2 = print_formatted(format, argv, &conv_err); |
| 512 | #if ENABLE_PLATFORM_MINGW32 | ||
| 513 | my_flush(); | ||
| 514 | #endif | ||
| 447 | } while (argv2 > argv && *argv2); | 515 | } while (argv2 > argv && *argv2); |
| 448 | 516 | ||
| 449 | /* coreutils compat (bash doesn't do this): | 517 | /* coreutils compat (bash doesn't do this): |
