diff options
author | Ron Yorston <rmy@pobox.com> | 2018-03-09 09:04:49 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2018-03-09 09:18:20 +0000 |
commit | f3d24e08a385a68c4bacb284bd8a8e3da7f0f4b3 (patch) | |
tree | 1f6d731e76500446609c17eacf0bf959789dcd28 /win32 | |
parent | b07439a68123cc4a0df20754daec448bb9b42606 (diff) | |
download | busybox-w32-f3d24e08a385a68c4bacb284bd8a8e3da7f0f4b3.tar.gz busybox-w32-f3d24e08a385a68c4bacb284bd8a8e3da7f0f4b3.tar.bz2 busybox-w32-f3d24e08a385a68c4bacb284bd8a8e3da7f0f4b3.zip |
winansi: check for broken pipe in winansi_write
write(2) is commonly used in applets like cat and tr so we should
check for broken pipes there too.
See issue #99.
Diffstat (limited to 'win32')
-rw-r--r-- | win32/winansi.c | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/win32/winansi.c b/win32/winansi.c index 256ef4fa9..e36f7565b 100644 --- a/win32/winansi.c +++ b/win32/winansi.c | |||
@@ -475,17 +475,24 @@ int winansi_puts(const char *s) | |||
475 | return (winansi_fputs(s, stdout) == EOF || putchar('\n') == EOF) ? EOF : 0; | 475 | return (winansi_fputs(s, stdout) == EOF || putchar('\n') == EOF) ? EOF : 0; |
476 | } | 476 | } |
477 | 477 | ||
478 | static void check_pipe(FILE *stream) | 478 | static void check_pipe_fd(int fd) |
479 | { | 479 | { |
480 | if (GetLastError() == ERROR_NO_DATA && ferror(stream)) { | 480 | if (GetLastError() == ERROR_NO_DATA) { |
481 | int fd = fileno(stream); | 481 | if (GetFileType((HANDLE)_get_osfhandle(fd)) == FILE_TYPE_PIPE) { |
482 | if (fd != -1 && | ||
483 | GetFileType((HANDLE)_get_osfhandle(fd)) == FILE_TYPE_PIPE) { | ||
484 | errno = EPIPE; | 482 | errno = EPIPE; |
485 | } | 483 | } |
486 | } | 484 | } |
487 | } | 485 | } |
488 | 486 | ||
487 | static void check_pipe(FILE *stream) | ||
488 | { | ||
489 | int fd = fileno(stream); | ||
490 | |||
491 | if (fd != -1 && ferror(stream)) { | ||
492 | check_pipe_fd(fd); | ||
493 | } | ||
494 | } | ||
495 | |||
489 | size_t winansi_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) | 496 | size_t winansi_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) |
490 | { | 497 | { |
491 | size_t lsize, lmemb, ret; | 498 | size_t lsize, lmemb, ret; |
@@ -675,8 +682,15 @@ static int ansi_emulate_write(int fd, const void *buf, size_t count) | |||
675 | 682 | ||
676 | int winansi_write(int fd, const void *buf, size_t count) | 683 | int winansi_write(int fd, const void *buf, size_t count) |
677 | { | 684 | { |
678 | if (!is_console(fd)) | 685 | if (!is_console(fd)) { |
679 | return write(fd, buf, count); | 686 | int ret; |
687 | |||
688 | SetLastError(0); | ||
689 | if ((ret=write(fd, buf, count)) == -1) { | ||
690 | check_pipe_fd(fd); | ||
691 | } | ||
692 | return ret; | ||
693 | } | ||
680 | 694 | ||
681 | return ansi_emulate_write(fd, buf, count); | 695 | return ansi_emulate_write(fd, buf, count); |
682 | } | 696 | } |