From f3d24e08a385a68c4bacb284bd8a8e3da7f0f4b3 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 9 Mar 2018 09:04:49 +0000 Subject: 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. --- win32/winansi.c | 28 +++++++++++++++++++++------- 1 file 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) return (winansi_fputs(s, stdout) == EOF || putchar('\n') == EOF) ? EOF : 0; } -static void check_pipe(FILE *stream) +static void check_pipe_fd(int fd) { - if (GetLastError() == ERROR_NO_DATA && ferror(stream)) { - int fd = fileno(stream); - if (fd != -1 && - GetFileType((HANDLE)_get_osfhandle(fd)) == FILE_TYPE_PIPE) { + if (GetLastError() == ERROR_NO_DATA) { + if (GetFileType((HANDLE)_get_osfhandle(fd)) == FILE_TYPE_PIPE) { errno = EPIPE; } } } +static void check_pipe(FILE *stream) +{ + int fd = fileno(stream); + + if (fd != -1 && ferror(stream)) { + check_pipe_fd(fd); + } +} + size_t winansi_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t lsize, lmemb, ret; @@ -675,8 +682,15 @@ static int ansi_emulate_write(int fd, const void *buf, size_t count) int winansi_write(int fd, const void *buf, size_t count) { - if (!is_console(fd)) - return write(fd, buf, count); + if (!is_console(fd)) { + int ret; + + SetLastError(0); + if ((ret=write(fd, buf, count)) == -1) { + check_pipe_fd(fd); + } + return ret; + } return ansi_emulate_write(fd, buf, count); } -- cgit v1.2.3-55-g6feb