changeset 1330:02e3df773122 draft

iconv: some fixes - fix problem with sequences at buffer boundaries - add (ignored) -c and -s options - don't try to continue with a file when read() fails
author Felix Janda <felix.janda@posteo.de>
date Sat, 31 May 2014 11:18:30 +0200
parents 5662f0664060
children 11fe363078b8
files toys/pending/iconv.c
diffstat 1 files changed, 9 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/toys/pending/iconv.c	Sat May 31 18:00:04 2014 -0500
+++ b/toys/pending/iconv.c	Sat May 31 11:18:30 2014 +0200
@@ -4,7 +4,7 @@
  *
  * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/iconv.html
 
-USE_ICONV(NEWTOY(iconv, "t:f:", TOYFLAG_USR|TOYFLAG_BIN))
+USE_ICONV(NEWTOY(iconv, "cst:f:", TOYFLAG_USR|TOYFLAG_BIN))
 
 config ICONV
   bool "iconv"
@@ -38,16 +38,19 @@
 
   do {
     size_t outleft = 2048;
-    char *in = toybuf, *out = outstart;
+    char *in = toybuf+inleft, *out = outstart;
+
+    len = read(fd, in, 2048-inleft);
 
-    len = read(fd, toybuf+inleft, 2048-inleft);
-
-    if (len < 0) perror_msg("read '%s'");
+    if (len < 0) {
+      perror_msg("read '%s'");
+      return;
+    }
     inleft += len;
 
     do {
       if (iconv(TT.ic, &in, &inleft, &out, &outleft) == -1
-          && (errno == EILSEQ || (in == toybuf && errno == EINVAL)))
+          && (errno == EILSEQ || (in == toybuf+inleft-len && errno == EINVAL)))
       {
         if (outleft) {
           // Skip first byte of illegal sequence to avoid endless loops