diff options
author | Ron Yorston <rmy@pobox.com> | 2023-01-29 10:49:10 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2023-01-29 11:28:19 +0000 |
commit | 33ae88c40cf56b53c06e627e0535c9c740767aa4 (patch) | |
tree | 03bb1222b1576b9290ed40e6549a3547ff83be6a | |
parent | 6295322661ac6c2ed90af69cba0003c6e37f9700 (diff) | |
download | busybox-w32-33ae88c40cf56b53c06e627e0535c9c740767aa4.tar.gz busybox-w32-33ae88c40cf56b53c06e627e0535c9c740767aa4.tar.bz2 busybox-w32-33ae88c40cf56b53c06e627e0535c9c740767aa4.zip |
diff: improve --binary implementation
Commit 82f0d19b1 (diff: implement --binary flag) was both over-
complicated and incorrect. If stdin was seekable it was left in
binary mode even if the --binary flag wasn't supplied.
Always open files in binary mode. Only switch to text mode at the
last moment, if necessary.
Saves 48 bytes.
-rw-r--r-- | editors/diff.c | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/editors/diff.c b/editors/diff.c index d91ecea51..b6716455f 100644 --- a/editors/diff.c +++ b/editors/diff.c | |||
@@ -718,6 +718,7 @@ static int diffreg(char *file[2]) | |||
718 | #if ENABLE_PLATFORM_MINGW32 | 718 | #if ENABLE_PLATFORM_MINGW32 |
719 | char *tmpfile[2] = { NULL, NULL }; | 719 | char *tmpfile[2] = { NULL, NULL }; |
720 | char *tmpdir; | 720 | char *tmpdir; |
721 | const char *mode; | ||
721 | #endif | 722 | #endif |
722 | 723 | ||
723 | fp[0] = stdin; | 724 | fp[0] = stdin; |
@@ -726,12 +727,7 @@ static int diffreg(char *file[2]) | |||
726 | int fd = STDIN_FILENO; | 727 | int fd = STDIN_FILENO; |
727 | if (!LONE_DASH(file[i])) { | 728 | if (!LONE_DASH(file[i])) { |
728 | if (!(option_mask32 & FLAG(N))) { | 729 | if (!(option_mask32 & FLAG(N))) { |
729 | #if ENABLE_PLATFORM_MINGW32 | ||
730 | int flag = (option_mask32 & FLAG(binary)) ? 0 : _O_TEXT; | ||
731 | fd = open_or_warn(file[i], O_RDONLY | flag); | ||
732 | #else | ||
733 | fd = open_or_warn(file[i], O_RDONLY); | 730 | fd = open_or_warn(file[i], O_RDONLY); |
734 | #endif | ||
735 | if (fd == -1) | 731 | if (fd == -1) |
736 | goto out; | 732 | goto out; |
737 | } else { | 733 | } else { |
@@ -762,18 +758,16 @@ static int diffreg(char *file[2]) | |||
762 | xfunc_die(); | 758 | xfunc_die(); |
763 | if (fd != STDIN_FILENO) | 759 | if (fd != STDIN_FILENO) |
764 | close(fd); | 760 | close(fd); |
765 | #if ENABLE_PLATFORM_MINGW32 | ||
766 | if (!(option_mask32 & FLAG(binary))) { | ||
767 | /* Close and reopen tmpfile to get text mode */ | ||
768 | close(fd_tmp); | ||
769 | fd_tmp = xopen(tmpfile[i], O_RDONLY | _O_TEXT); | ||
770 | } | ||
771 | #endif | ||
772 | fd = fd_tmp; | 761 | fd = fd_tmp; |
773 | xlseek(fd, 0, SEEK_SET); | 762 | xlseek(fd, 0, SEEK_SET); |
774 | } | 763 | } |
775 | #if ENABLE_PLATFORM_MINGW32 | 764 | #if ENABLE_PLATFORM_MINGW32 |
776 | fp[i] = fdopen(fd, (option_mask32 & FLAG(binary)) ? "r" : "rt"); | 765 | mode = "r"; |
766 | if (!(option_mask32 & FLAG(binary))) { | ||
767 | _setmode(fd, _O_TEXT); | ||
768 | mode = "rt"; | ||
769 | } | ||
770 | fp[i] = fdopen(fd, mode); | ||
777 | #else | 771 | #else |
778 | fp[i] = fdopen(fd, "r"); | 772 | fp[i] = fdopen(fd, "r"); |
779 | #endif | 773 | #endif |