diff options
| author | Ron Yorston <rmy@pobox.com> | 2020-02-18 11:19:04 +0000 |
|---|---|---|
| committer | Ron Yorston <rmy@pobox.com> | 2020-02-18 11:19:04 +0000 |
| commit | 412c2cab62dcae5509404b48d83aa10717d3b68e (patch) | |
| tree | aa2592a12fb8b39d772b4d8e8c1a531ab8d4c5ca /miscutils | |
| parent | d7a88599bd88035b1eeae276140f0ce9e405c271 (diff) | |
| download | busybox-w32-412c2cab62dcae5509404b48d83aa10717d3b68e.tar.gz busybox-w32-412c2cab62dcae5509404b48d83aa10717d3b68e.tar.bz2 busybox-w32-412c2cab62dcae5509404b48d83aa10717d3b68e.zip | |
iconv: minor fixes
Add an explicit test for EOF. Otherwise when processing input from
interactive stdin it's necessary to enter ^Z twice to signal EOF.
Allow '-' to be mixed with actual file names on the command line.
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/iconv.c | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/miscutils/iconv.c b/miscutils/iconv.c index d6ed439b4..ec3c0c92d 100644 --- a/miscutils/iconv.c +++ b/miscutils/iconv.c | |||
| @@ -1677,22 +1677,23 @@ static void process_file(iconv_t cd, FILE *in, FILE *out) | |||
| 1677 | char outbuf[BUFSIZ]; | 1677 | char outbuf[BUFSIZ]; |
| 1678 | const char *pin; | 1678 | const char *pin; |
| 1679 | char *pout; | 1679 | char *pout; |
| 1680 | size_t inbytesleft; | 1680 | size_t inbytesleft = 0; |
| 1681 | size_t outbytesleft; | 1681 | size_t outbytesleft; |
| 1682 | size_t rest = 0; | 1682 | size_t rest = 0; |
| 1683 | size_t r; | 1683 | size_t r; |
| 1684 | 1684 | ||
| 1685 | while ((inbytesleft=fread(inbuf+rest, 1, sizeof(inbuf)-rest, in)) != 0 | 1685 | while ((!feof(in) && |
| 1686 | (inbytesleft=fread(inbuf+rest, 1, sizeof(inbuf)-rest, in)) != 0) | ||
| 1686 | || rest != 0) { | 1687 | || rest != 0) { |
| 1687 | inbytesleft += rest; | 1688 | inbytesleft += rest; |
| 1688 | pin = inbuf; | 1689 | pin = inbuf; |
| 1689 | pout = outbuf; | 1690 | pout = outbuf; |
| 1690 | outbytesleft = sizeof(outbuf); | 1691 | outbytesleft = sizeof(outbuf); |
| 1691 | r = iconv(cd, &pin, &inbytesleft, &pout, &outbytesleft); | 1692 | r = iconv(cd, &pin, &inbytesleft, &pout, &outbytesleft); |
| 1692 | fwrite(outbuf, 1, sizeof(outbuf) - outbytesleft, out); | ||
| 1693 | if (r == (size_t)(-1) && errno != E2BIG && | 1693 | if (r == (size_t)(-1) && errno != E2BIG && |
| 1694 | (errno != EINVAL || feof(in))) | 1694 | (errno != EINVAL || feof(in))) |
| 1695 | bb_perror_msg_and_die("conversion error"); | 1695 | bb_perror_msg_and_die("conversion error"); |
| 1696 | fwrite(outbuf, 1, sizeof(outbuf) - outbytesleft, out); | ||
| 1696 | memmove(inbuf, pin, inbytesleft); | 1697 | memmove(inbuf, pin, inbytesleft); |
| 1697 | rest = inbytesleft; | 1698 | rest = inbytesleft; |
| 1698 | } | 1699 | } |
| @@ -1718,7 +1719,7 @@ int iconv_main(int argc, char **argv) | |||
| 1718 | const char *fromcode = "", *tocode = "", *outfile; | 1719 | const char *fromcode = "", *tocode = "", *outfile; |
| 1719 | int i, opt; | 1720 | int i, opt; |
| 1720 | iconv_t cd; | 1721 | iconv_t cd; |
| 1721 | FILE *in = stdin; | 1722 | FILE *in; |
| 1722 | FILE *out = stdout; | 1723 | FILE *out = stdout; |
| 1723 | 1724 | ||
| 1724 | opt = getopt32(argv, "f:t:lco:", &fromcode, &tocode, &outfile); | 1725 | opt = getopt32(argv, "f:t:lco:", &fromcode, &tocode, &outfile); |
| @@ -1742,16 +1743,16 @@ int iconv_main(int argc, char **argv) | |||
| 1742 | if (cd == (iconv_t)(-1)) | 1743 | if (cd == (iconv_t)(-1)) |
| 1743 | bb_perror_msg_and_die("iconv_open error"); | 1744 | bb_perror_msg_and_die("iconv_open error"); |
| 1744 | 1745 | ||
| 1745 | if (optind == argc || | 1746 | if (optind == argc) |
| 1746 | (optind == argc-1 && strcmp(argv[optind], "-") == 0)) { | 1747 | argv[argc++] = (char *)"-"; |
| 1748 | |||
| 1749 | for (i=optind; i<argc; ++i) { | ||
| 1750 | if (argv[i][0] == '-' && argv[i][1] == '\0') | ||
| 1751 | in = stdin; | ||
| 1752 | else | ||
| 1753 | in = xfopen(argv[optind], "rb"); | ||
| 1747 | process_file(cd, in, out); | 1754 | process_file(cd, in, out); |
| 1748 | } | 1755 | fclose(in); |
| 1749 | else { | ||
| 1750 | for (i=optind; i<argc; ++i) { | ||
| 1751 | in = xfopen(argv[i], "rb"); | ||
| 1752 | process_file(cd, in, out); | ||
| 1753 | fclose(in); | ||
| 1754 | } | ||
| 1755 | } | 1756 | } |
| 1756 | 1757 | ||
| 1757 | if (ENABLE_FEATURE_CLEAN_UP) | 1758 | if (ENABLE_FEATURE_CLEAN_UP) |
