OSDN Git Service

Promote iconv.
[android-x86/external-toybox.git] / toys / posix / iconv.c
1 /* iconv.c - Convert character encoding
2  *
3  * Copyright 2014 Felix Janda <felix.janda@posteo.de>
4  *
5  * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/iconv.html
6  *
7  * Deviations from posix: no idea how to implement -l
8
9 USE_ICONV(NEWTOY(iconv, "cst:f:", TOYFLAG_USR|TOYFLAG_BIN))
10
11 config ICONV
12   bool "iconv"
13   default y
14   depends on TOYBOX_ICONV
15   help
16     usage: iconv [-f FROM] [-t TO] [FILE...]
17
18     Convert character encoding of files.
19
20     -c  Omit invalid chars
21     -f  convert from (default utf8)
22     -t  convert to   (default utf8)
23 */
24
25 #define FOR_iconv
26 #include "toys.h"
27 #include <iconv.h>
28
29 GLOBALS(
30   char *from;
31   char *to;
32
33   void *ic;
34 )
35
36 static void do_iconv(int fd, char *name)
37 {
38   char *outstart = toybuf+2048;
39   size_t outlen, inlen = 0;
40   int readlen = 1;
41
42   for (;;) {
43     char *in = toybuf, *out = outstart;
44
45     if (readlen && 0>(readlen = read(fd, in+inlen, 2048-inlen))) {
46       perror_msg("read '%s'", name);
47       return;
48     }
49     inlen += readlen;
50     if (!inlen) break;
51
52     outlen = 2048;
53     iconv(TT.ic, &in, &inlen, &out, &outlen);
54     if (in == toybuf) {
55       // Skip first byte of illegal sequence to avoid endless loops
56       if (toys.optflags & FLAG_c) in++;
57       else *(out++) = *(in++);
58       inlen--;
59     }
60     if (out != outstart) xwrite(1, outstart, out-outstart);
61     memmove(toybuf, in, inlen);
62   }
63 }
64
65 void iconv_main(void)
66 {
67   if (!TT.to) TT.to = "utf8";
68   if (!TT.from) TT.from = "utf8";
69
70   if ((iconv_t)-1 == (TT.ic = iconv_open(TT.to, TT.from)))
71     perror_exit("%s/%s", TT.to, TT.from);
72   loopfiles(toys.optargs, do_iconv);
73   if (CFG_TOYBOX_FREE) iconv_close(TT.ic);
74 }