From 412c2cab62dcae5509404b48d83aa10717d3b68e Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 18 Feb 2020 11:19:04 +0000 Subject: 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. --- miscutils/iconv.c | 27 ++++++++++++++------------- 1 file 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) char outbuf[BUFSIZ]; const char *pin; char *pout; - size_t inbytesleft; + size_t inbytesleft = 0; size_t outbytesleft; size_t rest = 0; size_t r; - while ((inbytesleft=fread(inbuf+rest, 1, sizeof(inbuf)-rest, in)) != 0 + while ((!feof(in) && + (inbytesleft=fread(inbuf+rest, 1, sizeof(inbuf)-rest, in)) != 0) || rest != 0) { inbytesleft += rest; pin = inbuf; pout = outbuf; outbytesleft = sizeof(outbuf); r = iconv(cd, &pin, &inbytesleft, &pout, &outbytesleft); - fwrite(outbuf, 1, sizeof(outbuf) - outbytesleft, out); if (r == (size_t)(-1) && errno != E2BIG && (errno != EINVAL || feof(in))) bb_perror_msg_and_die("conversion error"); + fwrite(outbuf, 1, sizeof(outbuf) - outbytesleft, out); memmove(inbuf, pin, inbytesleft); rest = inbytesleft; } @@ -1718,7 +1719,7 @@ int iconv_main(int argc, char **argv) const char *fromcode = "", *tocode = "", *outfile; int i, opt; iconv_t cd; - FILE *in = stdin; + FILE *in; FILE *out = stdout; opt = getopt32(argv, "f:t:lco:", &fromcode, &tocode, &outfile); @@ -1742,16 +1743,16 @@ int iconv_main(int argc, char **argv) if (cd == (iconv_t)(-1)) bb_perror_msg_and_die("iconv_open error"); - if (optind == argc || - (optind == argc-1 && strcmp(argv[optind], "-") == 0)) { + if (optind == argc) + argv[argc++] = (char *)"-"; + + for (i=optind; i