2 /* $NetBSD: iconv.c,v 1.16 2009/02/20 15:28:21 yamt Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c)2003 Citrus Project,
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
46 static int do_conv(FILE *, const char *, const char *, bool, bool);
47 static int do_list(unsigned int, const char * const *, void *);
48 static void usage(void) __dead2;
50 static const struct option long_options[] = {
51 {"from-code", required_argument, NULL, 'f'},
52 {"list", no_argument, NULL, 'l'},
53 {"silent", no_argument, NULL, 's'},
54 {"to-code", required_argument, NULL, 't'},
55 {NULL, no_argument, NULL, 0}
62 "Usage:\t%1$s [-cs] -f <from_code> -t <to_code> [file ...]\n"
63 "\t%1$s -f <from_code> [-cs] [-t <to_code>] [file ...]\n"
64 "\t%1$s -t <to_code> [-cs] [-f <from_code>] [file ...]\n"
65 "\t%1$s -l\n", getprogname());
69 #define INBUFSIZE 1024
70 #define OUTBUFSIZE (INBUFSIZE * 2)
72 do_conv(FILE *fp, const char *from, const char *to, bool silent,
76 char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *in, *out;
77 unsigned long long invalids;
78 size_t inbytes, outbytes, ret;
80 if ((cd = iconv_open(to, from)) == (iconv_t)-1)
81 err(EXIT_FAILURE, "iconv_open(%s, %s)", to, from);
86 if (iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, (void *)&arg) == -1)
87 err(EXIT_FAILURE, NULL);
90 while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) {
96 outbytes = OUTBUFSIZE;
97 ret = __iconv(cd, &in, &inbytes, &out, &outbytes,
100 if (outbytes < OUTBUFSIZE)
101 (void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes,
103 if (ret == (size_t)-1 && errno != E2BIG) {
104 if (errno != EINVAL || in == inbuf)
105 err(EXIT_FAILURE, "iconv()");
107 /* incomplete input character */
108 (void)memmove(inbuf, in, inbytes);
109 ret = fread(inbuf + inbytes, 1,
110 INBUFSIZE - inbytes, fp);
115 "unexpected end of file; "
116 "the last character is "
119 err(EXIT_FAILURE, "fread()");
126 /* reset the shift state of the output buffer */
127 outbytes = OUTBUFSIZE;
129 ret = iconv(cd, NULL, NULL, &out, &outbytes);
130 if (ret == (size_t)-1)
131 err(EXIT_FAILURE, "iconv()");
132 if (outbytes < OUTBUFSIZE)
133 (void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes, stdout);
135 if (invalids > 0 && !silent)
136 warnx("warning: invalid characters: %llu", invalids);
139 return (invalids > 0);
143 do_list(unsigned int n, const char * const *list, void *data __unused)
147 for(i = 0; i < n; i++) {
148 printf("%s", list[i]);
158 main(int argc, char **argv)
161 const char *opt_f, *opt_t;
163 bool opt_c = false, opt_s = false;
167 setlocale(LC_ALL, "");
168 setprogname(argv[0]);
170 while ((ch = getopt_long(argc, argv, "csLlf:t:",
171 long_options, NULL)) != -1) {
181 if (opt_s || opt_c || strcmp(opt_f, "") != 0 ||
182 strcmp(opt_t, "") != 0) {
183 warnx("-l is not allowed with other flags.");
186 iconvlist(do_list, NULL);
187 return (EXIT_SUCCESS);
204 if ((strcmp(opt_f, "") == 0) && (strcmp(opt_t, "") == 0))
207 res = do_conv(stdin, opt_f, opt_t, opt_s, opt_c);
210 for (i = 0; i < argc; i++) {
211 fp = (strcmp(argv[i], "-") != 0) ?
212 fopen(argv[i], "r") : stdin;
214 err(EXIT_FAILURE, "Cannot open `%s'",
216 res |= do_conv(fp, opt_f, opt_t, opt_s, opt_c);
220 return (res == 0 ? EXIT_SUCCESS : EXIT_FAILURE);