Mercurial > hg > toybox
annotate toys/pending/iconv.c @ 1639:856b544f8fce draft
strncpy(optptr, hname, strlen(hname)) is really just strcpy().
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 01 Jan 2015 16:49:55 -0600 |
parents | 02e3df773122 |
children |
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 |
1330 | 7 USE_ICONV(NEWTOY(iconv, "cst:f:", TOYFLAG_USR|TOYFLAG_BIN)) |
1254
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" |
1313
5d64dc59e569
Pending commands should default n
Rob Landley <rob@landley.net>
parents:
1266
diff
changeset
|
11 default n |
1266
a302299dbb98
As long as uClibc's still around and requires you to jump through hoops to get iconv(), probe and build defconfig without it if it's not there.
Rob Landley <rob@landley.net>
parents:
1255
diff
changeset
|
12 depends on TOYBOX_ICONV |
1254
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
13 help |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
14 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
|
15 |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
16 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
|
17 |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
18 -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
|
19 -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
|
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 |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
22 #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
|
23 #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
|
24 #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
|
25 |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
26 GLOBALS( |
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 *from; |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
28 char *to; |
1255 | 29 |
30 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
|
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 |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
33 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
|
34 { |
1255 | 35 char *outstart = toybuf+2048; |
36 size_t inleft = 0; | |
37 int len = 1; | |
38 | |
39 do { | |
40 size_t outleft = 2048; | |
1330 | 41 char *in = toybuf+inleft, *out = outstart; |
42 | |
43 len = read(fd, in, 2048-inleft); | |
1255 | 44 |
1330 | 45 if (len < 0) { |
46 perror_msg("read '%s'"); | |
47 return; | |
48 } | |
1255 | 49 inleft += len; |
50 | |
51 do { | |
52 if (iconv(TT.ic, &in, &inleft, &out, &outleft) == -1 | |
1330 | 53 && (errno == EILSEQ || (in == toybuf+inleft-len && errno == EINVAL))) |
1255 | 54 { |
55 if (outleft) { | |
56 // Skip first byte of illegal sequence to avoid endless loops | |
57 *(out++) = *(in++); | |
58 inleft--; | |
59 } | |
1254
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
60 } |
1255 | 61 xwrite(1, outstart, out-outstart); |
62 // Top off input buffer | |
63 memmove(in, toybuf, inleft); | |
64 } while (len < 1 && inleft); | |
65 } 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
|
66 } |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
67 |
cd4020db5a2c
iconv is actually something I'm missing on my current musl based system.
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
68 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
|
69 { |
1255 | 70 TT.ic = iconv_open(TT.to ? TT.to : "utf8", TT.from ? TT.from : "utf8"); |
71 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
|
72 loopfiles(toys.optargs, do_iconv); |
1255 | 73 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
|
74 } |