Mercurial > hg > toybox
annotate toys/pending/iconv.c @ 1265:0ecfaa7022e8 draft
usage: is lower case (the help generator looks for that, might as well be consistent).
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 23 Apr 2014 08:37:07 -0500 |
parents | 69b34a10f1e7 |
children | a302299dbb98 |
rev | line source |
---|---|
1254
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
1 /* iconv.c - Convert character encoding |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
2 * |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
3 * Copyright 2014 Felix Janda <felix.janda@posteo.de> |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
4 * |
1255 | 5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/iconv.html |
1254
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
6 |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
7 USE_ICONV(NEWTOY(iconv, "t:f:", TOYFLAG_USR|TOYFLAG_BIN)) |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
8 |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
9 config ICONV |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
10 bool "iconv" |
1255 | 11 default y |
1254
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
12 help |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
13 usage: iconv [-f FROM] [-t TO] [FILE...] |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
14 |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
15 Convert character encoding of files. |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
16 |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
17 -f convert from (default utf8) |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
18 -t convert to (default utf8) |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
19 */ |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
20 |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
21 #define FOR_iconv |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
22 #include "toys.h" |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
23 #include <iconv.h> |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
24 |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
25 GLOBALS( |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
26 char *from; |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
27 char *to; |
1255 | 28 |
29 void *ic; | |
1254
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
30 ) |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
31 |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
32 static void do_iconv(int fd, char *name) |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
33 { |
1255 | 34 char *outstart = toybuf+2048; |
35 size_t inleft = 0; | |
36 int len = 1; | |
37 | |
38 do { | |
39 size_t outleft = 2048; | |
40 char *in = toybuf, *out = outstart; | |
41 | |
42 len = read(fd, toybuf+inleft, 2048-inleft); | |
1254
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
43 |
1255 | 44 if (len < 0) perror_msg("read '%s'"); |
45 inleft += len; | |
46 | |
47 do { | |
48 if (iconv(TT.ic, &in, &inleft, &out, &outleft) == -1 | |
49 && (errno == EILSEQ || (in == toybuf && errno == EINVAL))) | |
50 { | |
51 if (outleft) { | |
52 // Skip first byte of illegal sequence to avoid endless loops | |
53 *(out++) = *(in++); | |
54 inleft--; | |
55 } | |
1254
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
56 } |
1255 | 57 xwrite(1, outstart, out-outstart); |
58 // Top off input buffer | |
59 memmove(in, toybuf, inleft); | |
60 } while (len < 1 && inleft); | |
61 } while (len > 0); | |
1254
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
62 } |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
63 |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
64 void iconv_main(void) |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
65 { |
1255 | 66 TT.ic = iconv_open(TT.to ? TT.to : "utf8", TT.from ? TT.from : "utf8"); |
67 if (TT.ic == (iconv_t)-1) error_exit("bad encoding"); | |
1254
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
68 loopfiles(toys.optargs, do_iconv); |
1255 | 69 if (CFG_TOYBOX_FREE) iconv_close(TT.ic); |
1254
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
70 } |