diff options
-rw-r--r-- | win32/winansi.c | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/win32/winansi.c b/win32/winansi.c index aaaa2fa50..c88c096d2 100644 --- a/win32/winansi.c +++ b/win32/winansi.c | |||
@@ -1534,6 +1534,25 @@ void console_write(const char *str, int len) | |||
1534 | free(buf); | 1534 | free(buf); |
1535 | } | 1535 | } |
1536 | 1536 | ||
1537 | // LC_ALL=C disables console output conversion, so that the source | ||
1538 | // data is interpreted only by the console according to its output CP. | ||
1539 | static int conout_conv_enabled(void) | ||
1540 | { | ||
1541 | static int enabled, tested; /* = 0 */ | ||
1542 | |||
1543 | if (!tested) { | ||
1544 | // keep in sync with [re]init_unicode at libbb/unicode.c | ||
1545 | char *s = getenv("LC_ALL"); | ||
1546 | if (!s) s = getenv("LC_CTYPE"); | ||
1547 | if (!s) s = getenv("LANG"); | ||
1548 | |||
1549 | enabled = !(s && s[0] == 'C' && s[1] == 0); | ||
1550 | tested = 1; | ||
1551 | } | ||
1552 | |||
1553 | return enabled; | ||
1554 | } | ||
1555 | |||
1537 | // TODO: improvements: | 1556 | // TODO: improvements: |
1538 | // | 1557 | // |
1539 | // 1. currently conv_[f]writeCon modify buf inplace, which means the caller | 1558 | // 1. currently conv_[f]writeCon modify buf inplace, which means the caller |
@@ -1554,12 +1573,14 @@ void console_write(const char *str, int len) | |||
1554 | // returns EOF on error, 0 on success | 1573 | // returns EOF on error, 0 on success |
1555 | static int conv_fwriteCon(FILE *stream, char *buf, size_t siz) | 1574 | static int conv_fwriteCon(FILE *stream, char *buf, size_t siz) |
1556 | { | 1575 | { |
1576 | if (conout_conv_enabled()) { | ||
1557 | #if ENABLE_FEATURE_UTF8_OUTPUT | 1577 | #if ENABLE_FEATURE_UTF8_OUTPUT |
1558 | if (GetConsoleOutputCP() != CP_UTF8) | 1578 | if (GetConsoleOutputCP() != CP_UTF8) |
1559 | return writeCon_utf8(fileno(stream), buf, siz) ? EOF : 0; | 1579 | return writeCon_utf8(fileno(stream), buf, siz) ? EOF : 0; |
1560 | #else | 1580 | #else |
1561 | charToConBuffA(buf, siz); | 1581 | charToConBuffA(buf, siz); |
1562 | #endif | 1582 | #endif |
1583 | } | ||
1563 | return fwrite(buf, 1, siz, stream) < siz ? EOF : 0; | 1584 | return fwrite(buf, 1, siz, stream) < siz ? EOF : 0; |
1564 | } | 1585 | } |
1565 | 1586 | ||
@@ -1567,11 +1588,13 @@ static int conv_fwriteCon(FILE *stream, char *buf, size_t siz) | |||
1567 | // returns -1 on error, actually-written bytes on suceess | 1588 | // returns -1 on error, actually-written bytes on suceess |
1568 | static int conv_writeCon(int fd, char *buf, size_t siz) | 1589 | static int conv_writeCon(int fd, char *buf, size_t siz) |
1569 | { | 1590 | { |
1591 | if (conout_conv_enabled()) { | ||
1570 | #if ENABLE_FEATURE_UTF8_OUTPUT | 1592 | #if ENABLE_FEATURE_UTF8_OUTPUT |
1571 | if (GetConsoleOutputCP() != CP_UTF8) | 1593 | if (GetConsoleOutputCP() != CP_UTF8) |
1572 | return writeCon_utf8(fd, buf, siz) ? -1 : siz; | 1594 | return writeCon_utf8(fd, buf, siz) ? -1 : siz; |
1573 | #else | 1595 | #else |
1574 | charToConBuffA(buf, siz); | 1596 | charToConBuffA(buf, siz); |
1575 | #endif | 1597 | #endif |
1598 | } | ||
1576 | return write(fd, buf, siz); | 1599 | return write(fd, buf, siz); |
1577 | } | 1600 | } |