1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#define FOR_iconv
26#include "toys.h"
27#include <iconv.h>
28
29GLOBALS(
30 char *from;
31 char *to;
32
33 void *ic;
34)
35
36static 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
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
65void 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}
75