aboutsummaryrefslogtreecommitdiff
path: root/win32
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2018-03-06 12:59:13 +0000
committerRon Yorston <rmy@pobox.com>2018-03-06 12:59:13 +0000
commit6cc225008d9b34daf1b547630e204fdcafaa76ab (patch)
tree3c8b148751393a98e2d2c1dcb23dbc5b70a4b56a /win32
parent7478b47cb678bcfbca16e41443a496b3bd8496f9 (diff)
downloadbusybox-w32-6cc225008d9b34daf1b547630e204fdcafaa76ab.tar.gz
busybox-w32-6cc225008d9b34daf1b547630e204fdcafaa76ab.tar.bz2
busybox-w32-6cc225008d9b34daf1b547630e204fdcafaa76ab.zip
winansi: improve error return values in ANSI emulation
Also tighten up the code slightly.
Diffstat (limited to 'win32')
-rw-r--r--win32/winansi.c43
1 files changed, 23 insertions, 20 deletions
diff --git a/win32/winansi.c b/win32/winansi.c
index da1f406e9..e3ffa67bd 100644
--- a/win32/winansi.c
+++ b/win32/winansi.c
@@ -394,14 +394,14 @@ static const char *set_attr(const char *str)
394static int ansi_emulate(const char *s, FILE *stream) 394static int ansi_emulate(const char *s, FILE *stream)
395{ 395{
396 int rv = 0; 396 int rv = 0;
397 const char *t; 397 const unsigned char *t;
398 char *pos, *str; 398 char *pos, *str;
399 size_t out_len, cur_len; 399 size_t cur_len;
400 static size_t max_len = 0; 400 static size_t max_len = 0;
401 static char *mem = NULL; 401 static char *mem = NULL;
402 402
403 /* if no special treatment is required output the string as-is */ 403 /* if no special treatment is required output the string as-is */
404 for ( t=s; *t; ++t ) { 404 for ( t=(unsigned char *)s; *t; ++t ) {
405 if ( *t == '\033' || *t > 0x7f ) { 405 if ( *t == '\033' || *t > 0x7f ) {
406 break; 406 break;
407 } 407 }
@@ -411,9 +411,13 @@ static int ansi_emulate(const char *s, FILE *stream)
411 return fputs(s, stream) == EOF ? EOF : strlen(s); 411 return fputs(s, stream) == EOF ? EOF : strlen(s);
412 } 412 }
413 413
414 /* make a writable copy of the string and retain it for reuse */ 414 /*
415 * Make a writable copy of the string and retain array for reuse.
416 * The test above guarantees that the string length won't be zero
417 * so the array will always be allocated.
418 */
415 cur_len = strlen(s); 419 cur_len = strlen(s);
416 if ( cur_len == 0 || cur_len > max_len ) { 420 if ( cur_len > max_len ) {
417 free(mem); 421 free(mem);
418 mem = strdup(s); 422 mem = strdup(s);
419 max_len = cur_len; 423 max_len = cur_len;
@@ -429,17 +433,18 @@ static int ansi_emulate(const char *s, FILE *stream)
429 size_t len = pos - str; 433 size_t len = pos - str;
430 434
431 if (len) { 435 if (len) {
432 CharToOemBuff(str, str, len); 436 *pos = '\0';
433 out_len = fwrite(str, 1, len, stream); 437 CharToOem(str, str);
434 rv += out_len; 438 if (fputs(str, stream) == EOF)
435 if (out_len < len) 439 return EOF;
436 return rv; 440 rv += len;
437 } 441 }
438 442
439 str = pos + 2; 443 str = pos + 2;
440 rv += 2; 444 rv += 2;
441 445
442 fflush(stream); 446 if (fflush(stream) == EOF)
447 return EOF;
443 448
444 pos = (char *)set_attr(str); 449 pos = (char *)set_attr(str);
445 rv += pos - str; 450 rv += pos - str;
@@ -447,8 +452,7 @@ static int ansi_emulate(const char *s, FILE *stream)
447 } else { 452 } else {
448 rv += strlen(str); 453 rv += strlen(str);
449 CharToOem(str, str); 454 CharToOem(str, str);
450 fputs(str, stream); 455 return fputs(str, stream) == EOF ? EOF : rv;
451 return rv;
452 } 456 }
453 } 457 }
454 return rv; 458 return rv;
@@ -598,7 +602,7 @@ static int ansi_emulate_write(int fd, const void *buf, size_t count)
598{ 602{
599 int rv = 0, i; 603 int rv = 0, i;
600 int special = FALSE, has_null = FALSE; 604 int special = FALSE, has_null = FALSE;
601 const char *s = (const char *)buf; 605 const unsigned char *s = (const unsigned char *)buf;
602 char *pos, *str; 606 char *pos, *str;
603 size_t len, out_len; 607 size_t len, out_len;
604 static size_t max_len = 0; 608 static size_t max_len = 0;
@@ -621,7 +625,7 @@ static int ansi_emulate_write(int fd, const void *buf, size_t count)
621 return write(fd, buf, count); 625 return write(fd, buf, count);
622 } 626 }
623 627
624 /* make a writable copy of the data and retain it for reuse */ 628 /* make a writable copy of the data and retain array for reuse */
625 if ( count > max_len ) { 629 if ( count > max_len ) {
626 free(mem); 630 free(mem);
627 mem = malloc(count+1); 631 mem = malloc(count+1);
@@ -640,9 +644,9 @@ static int ansi_emulate_write(int fd, const void *buf, size_t count)
640 if (len) { 644 if (len) {
641 CharToOemBuff(str, str, len); 645 CharToOemBuff(str, str, len);
642 out_len = write(fd, str, len); 646 out_len = write(fd, str, len);
647 if (out_len == -1)
648 return -1;
643 rv += out_len; 649 rv += out_len;
644 if (out_len < len)
645 return rv;
646 } 650 }
647 651
648 str = pos + 2; 652 str = pos + 2;
@@ -653,10 +657,9 @@ static int ansi_emulate_write(int fd, const void *buf, size_t count)
653 str = pos; 657 str = pos;
654 } else { 658 } else {
655 len = strlen(str); 659 len = strlen(str);
656 rv += len;
657 CharToOem(str, str); 660 CharToOem(str, str);
658 write(fd, str, len); 661 out_len = write(fd, str, len);
659 return rv; 662 return (out_len == -1) ? -1 : rv+out_len;
660 } 663 }
661 } 664 }
662 return rv; 665 return rv;