|
|
eef1e4 |
diff --git a/bin/iconv.c b/bin/iconv.c
|
|
|
eef1e4 |
deleted file mode 100644
|
|
|
eef1e4 |
index f5d5ce2..0000000
|
|
|
eef1e4 |
--- a/bin/iconv.c
|
|
|
eef1e4 |
+++ /dev/null
|
|
|
eef1e4 |
@@ -1,110 +0,0 @@
|
|
|
eef1e4 |
-/*
|
|
|
eef1e4 |
- * iconv.c
|
|
|
eef1e4 |
- * Implementation of SUSv4 XCU iconv utility
|
|
|
eef1e4 |
- * Copyright © 2011 Rich Felker
|
|
|
eef1e4 |
- * Licensed under the terms of the GNU General Public License, v2 or later
|
|
|
eef1e4 |
- */
|
|
|
eef1e4 |
-
|
|
|
eef1e4 |
-#include <stdlib.h>
|
|
|
eef1e4 |
-#include <stdio.h>
|
|
|
eef1e4 |
-#include <iconv.h>
|
|
|
eef1e4 |
-#include <locale.h>
|
|
|
eef1e4 |
-#include <langinfo.h>
|
|
|
eef1e4 |
-#include <unistd.h>
|
|
|
eef1e4 |
-#include <errno.h>
|
|
|
eef1e4 |
-#include <string.h>
|
|
|
eef1e4 |
-
|
|
|
eef1e4 |
-int main(int argc, char **argv)
|
|
|
eef1e4 |
-{
|
|
|
eef1e4 |
- const char *from=0, *to=0;
|
|
|
eef1e4 |
- int b;
|
|
|
eef1e4 |
- iconv_t cd;
|
|
|
eef1e4 |
- char buf[BUFSIZ];
|
|
|
eef1e4 |
- char outbuf[BUFSIZ*4];
|
|
|
eef1e4 |
- char *in, *out;
|
|
|
eef1e4 |
- size_t inb;
|
|
|
eef1e4 |
- size_t l;
|
|
|
eef1e4 |
- size_t unitsize=0;
|
|
|
eef1e4 |
- int err=0;
|
|
|
eef1e4 |
- FILE *f;
|
|
|
eef1e4 |
-
|
|
|
eef1e4 |
- while ((b = getopt(argc, argv, "f:t:csl")) != EOF) switch(b) {
|
|
|
eef1e4 |
- case 'l':
|
|
|
eef1e4 |
- puts("UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF32-LE, UCS-2BE, UCS-2LE, WCHAR_T,\n"
|
|
|
eef1e4 |
- "US_ASCII, ISO8859-1, ISO8859-2, ISO8859-3, ISO8859-4, ISO8859-5,\n"
|
|
|
eef1e4 |
- "ISO8859-6, ISO8859-7, ...");
|
|
|
eef1e4 |
- exit(0);
|
|
|
eef1e4 |
- case 'c': case 's': break;
|
|
|
eef1e4 |
- case 'f': from=optarg; break;
|
|
|
eef1e4 |
- case 't': to=optarg; break;
|
|
|
eef1e4 |
- default: exit(1);
|
|
|
eef1e4 |
- }
|
|
|
eef1e4 |
-
|
|
|
eef1e4 |
- if (!from || !to) {
|
|
|
eef1e4 |
- setlocale(LC_CTYPE, "");
|
|
|
eef1e4 |
- if (!to) to = nl_langinfo(CODESET);
|
|
|
eef1e4 |
- if (!from) from = nl_langinfo(CODESET);
|
|
|
eef1e4 |
- }
|
|
|
eef1e4 |
- cd = iconv_open(to, from);
|
|
|
eef1e4 |
- if (cd == (iconv_t)-1) {
|
|
|
eef1e4 |
- if (iconv_open(to, "WCHAR_T") == (iconv_t)-1)
|
|
|
eef1e4 |
- fprintf(stderr, "iconv: destination charset %s: ", to);
|
|
|
eef1e4 |
- else
|
|
|
eef1e4 |
- fprintf(stderr, "iconv: source charset %s: ", from);
|
|
|
eef1e4 |
- perror("");
|
|
|
eef1e4 |
- exit(1);
|
|
|
eef1e4 |
- }
|
|
|
eef1e4 |
- if (optind == argc) argv[argc++] = "-";
|
|
|
eef1e4 |
-
|
|
|
eef1e4 |
- for (; optind < argc; optind++) {
|
|
|
eef1e4 |
- if (argv[optind][0]=='-' && !argv[optind][1]) {
|
|
|
eef1e4 |
- f = stdin;
|
|
|
eef1e4 |
- argv[optind] = "(stdin)";
|
|
|
eef1e4 |
- } else if (!(f = fopen(argv[optind], "rb"))) {
|
|
|
eef1e4 |
- fprintf(stderr, "iconv: %s: ", argv[optind]);
|
|
|
eef1e4 |
- perror("");
|
|
|
eef1e4 |
- err = 1;
|
|
|
eef1e4 |
- continue;
|
|
|
eef1e4 |
- }
|
|
|
eef1e4 |
- inb = 0;
|
|
|
eef1e4 |
- for (;;) {
|
|
|
eef1e4 |
- in = buf;
|
|
|
eef1e4 |
- out = outbuf;
|
|
|
eef1e4 |
- l = fread(buf+inb, 1, sizeof(buf)-inb, f);
|
|
|
eef1e4 |
- inb += l;
|
|
|
eef1e4 |
- if (!inb) break;
|
|
|
eef1e4 |
- if (iconv(cd, &in, &inb, &out, (size_t [1]){sizeof outbuf})==-1
|
|
|
eef1e4 |
- && errno == EILSEQ) {
|
|
|
eef1e4 |
- if (!unitsize) {
|
|
|
eef1e4 |
- wchar_t wc='0';
|
|
|
eef1e4 |
- char dummy[4], *dummyp=dummy;
|
|
|
eef1e4 |
- iconv_t cd2 = iconv_open(from, "WCHAR_T");
|
|
|
eef1e4 |
- if (cd == (iconv_t)-1) {
|
|
|
eef1e4 |
- unitsize = 1;
|
|
|
eef1e4 |
- } else {
|
|
|
eef1e4 |
- iconv(cd2,
|
|
|
eef1e4 |
- (char *[1]){(char *)&wc},
|
|
|
eef1e4 |
- (size_t[1]){1},
|
|
|
eef1e4 |
- &dummyp, (size_t[1]){4});
|
|
|
eef1e4 |
- unitsize = dummyp-dummy;
|
|
|
eef1e4 |
- if (!unitsize) unitsize=1;
|
|
|
eef1e4 |
- }
|
|
|
eef1e4 |
- }
|
|
|
eef1e4 |
- inb-=unitsize;
|
|
|
eef1e4 |
- in+=unitsize;
|
|
|
eef1e4 |
- }
|
|
|
eef1e4 |
- if (inb && !l && errno==EINVAL) break;
|
|
|
eef1e4 |
- if (out>outbuf && !fwrite(outbuf, out-outbuf, 1, stdout)) {
|
|
|
eef1e4 |
- perror("iconv: write error");
|
|
|
eef1e4 |
- exit(1);
|
|
|
eef1e4 |
- }
|
|
|
eef1e4 |
- if (inb) memmove(buf, in, inb);
|
|
|
eef1e4 |
- }
|
|
|
eef1e4 |
- if (ferror(f)) {
|
|
|
eef1e4 |
- fprintf(stderr, "iconv: %s: ", argv[optind]);
|
|
|
eef1e4 |
- perror("");
|
|
|
eef1e4 |
- err = 1;
|
|
|
eef1e4 |
- }
|
|
|
eef1e4 |
- }
|
|
|
eef1e4 |
- return err;
|
|
|
eef1e4 |
-}
|