3 * Implementation of SUSv4 XCU iconv utility
4 * Copyright © 2011 Rich Felker
5 * Licensed under the terms of the GNU General Public License, v2 or later
17 int main(int argc
, char **argv
)
19 const char *from
=0, *to
=0;
23 char outbuf
[BUFSIZ
*4];
31 while ((b
= getopt(argc
, argv
, "f:t:csl")) != EOF
) switch(b
) {
33 puts("UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF32-LE, UCS-2BE, UCS-2LE, WCHAR_T,\n"
34 "US_ASCII, ISO8859-1, ISO8859-2, ISO8859-3, ISO8859-4, ISO8859-5,\n"
35 "ISO8859-6, ISO8859-7, ...");
37 case 'c': case 's': break;
38 case 'f': from
=optarg
; break;
39 case 't': to
=optarg
; break;
44 setlocale(LC_CTYPE
, "");
45 if (!to
) to
= nl_langinfo(CODESET
);
46 if (!from
) from
= nl_langinfo(CODESET
);
48 cd
= iconv_open(to
, from
);
49 if (cd
== (iconv_t
)-1) {
50 if (iconv_open(to
, "WCHAR_T") == (iconv_t
)-1)
51 fprintf(stderr
, "iconv: destination charset %s: ", to
);
53 fprintf(stderr
, "iconv: source charset %s: ", from
);
57 if (optind
== argc
) argv
[argc
++] = "-";
59 for (; optind
< argc
; optind
++) {
60 if (argv
[optind
][0]=='-' && !argv
[optind
][1]) {
62 argv
[optind
] = "(stdin)";
63 } else if (!(f
= fopen(argv
[optind
], "rb"))) {
64 fprintf(stderr
, "iconv: %s: ", argv
[optind
]);
73 l
= fread(buf
+inb
, 1, sizeof(buf
)-inb
, f
);
76 if (iconv(cd
, &in
, &inb
, &out
, (size_t [1]){sizeof outbuf
})==-1
80 char dummy
[4], *dummyp
=dummy
;
81 iconv_t cd2
= iconv_open(from
, "WCHAR_T");
82 if (cd
== (iconv_t
)-1) {
86 (char *[1]){(char *)&wc
},
88 &dummyp
, (size_t[1]){4});
89 unitsize
= dummyp
-dummy
;
90 if (!unitsize
) unitsize
=1;
96 if (inb
&& !l
&& errno
==EINVAL
) break;
97 if (out
>outbuf
&& !fwrite(outbuf
, out
-outbuf
, 1, stdout
)) {
98 perror("iconv: write error");
101 if (inb
) memmove(buf
, in
, inb
);
104 fprintf(stderr
, "iconv: %s: ", argv
[optind
]);