iconv.c revision 1.15 1 /* $NetBSD: iconv.c,v 1.15 2009/02/20 15:27:08 yamt Exp $ */
2
3 /*-
4 * Copyright (c)2003 Citrus Project,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: iconv.c,v 1.15 2009/02/20 15:27:08 yamt Exp $");
32 #endif /* LIBC_SCCS and not lint */
33
34 #include <err.h>
35 #include <errno.h>
36 #include <iconv.h>
37 #include <langinfo.h>
38 #include <locale.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <util.h>
44
45 static void usage(void) __unused;
46 static int scmp(const void *, const void *);
47 static void show_codesets(void);
48 static void do_conv(const char *, FILE *, const char *, const char *, int, int);
49
50 static void
51 usage(void)
52 {
53 (void)fprintf(stderr,
54 "Usage:\t%1$s [-cs] -f <from_code> -t <to_code> [file ...]\n"
55 "\t%1$s -f <from_code> [-cs] [-t <to_code>] [file ...]\n"
56 "\t%1$s -t <to_code> [-cs] [-f <from_code>] [file ...]\n"
57 "\t%1$s -l\n", getprogname());
58 exit(1);
59 }
60
61 /*
62 * qsort() helper function
63 */
64 static int
65 scmp(const void *v1, const void *v2)
66 {
67 const char * const *s1 = v1;
68 const char * const *s2 = v2;
69
70 return strcasecmp(*s1, *s2);
71 }
72
73 static void
74 show_codesets(void)
75 {
76 char **list;
77 size_t sz, i;
78
79 if (__iconv_get_list(&list, &sz))
80 err(EXIT_FAILURE, "__iconv_get_list()");
81
82 qsort(list, sz, sizeof(char *), scmp);
83
84 for (i = 0; i < sz; i++)
85 (void)printf("%s\n", list[i]);
86
87 __iconv_free_list(list, sz);
88 }
89
90 #define INBUFSIZE 1024
91 #define OUTBUFSIZE (INBUFSIZE * 2)
92 static void
93 do_conv(const char *fn, FILE *fp, const char *from, const char *to, int silent,
94 int hide_invalid)
95 {
96 char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *out;
97 const char *in;
98 size_t inbytes, outbytes, ret, invalids;
99 iconv_t cd;
100 uint32_t flags = 0;
101
102 if (hide_invalid)
103 flags |= __ICONV_F_HIDE_INVALID;
104 cd = iconv_open(to, from);
105 if (cd == (iconv_t)-1)
106 err(EXIT_FAILURE, "iconv_open(%s, %s)", to, from);
107
108 invalids = 0;
109 while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) {
110 in = inbuf;
111 while (inbytes > 0) {
112 size_t inval;
113
114 out = outbuf;
115 outbytes = OUTBUFSIZE;
116 ret = __iconv(cd, &in, &inbytes, &out, &outbytes,
117 flags, &inval);
118 invalids += inval;
119 if (ret == (size_t)-1 && errno != E2BIG) {
120 /*
121 * XXX: iconv(3) is bad interface.
122 * invalid character count is lost here.
123 * instead, we just provide __iconv function.
124 */
125 if (errno != EINVAL || in == inbuf)
126 err(EXIT_FAILURE, "iconv()");
127
128 /* incomplete input character */
129 (void)memmove(inbuf, in, inbytes);
130 ret = fread(inbuf + inbytes, 1,
131 INBUFSIZE - inbytes, fp);
132 if (ret == 0) {
133 if (feof(fp))
134 errx(EXIT_FAILURE,
135 "unexpected end of file; "
136 "the last character is "
137 "incomplete.");
138 else
139 err(EXIT_FAILURE, "fread()");
140 }
141 in = inbuf;
142 inbytes += ret;
143 }
144 if (outbytes < OUTBUFSIZE)
145 (void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes,
146 stdout);
147 }
148 }
149 /* reset the shift state of the output buffer */
150 outbytes = OUTBUFSIZE;
151 out = outbuf;
152 ret = iconv(cd, NULL, NULL, &out, &outbytes);
153 if (ret == (size_t)-1)
154 err(EXIT_FAILURE, "iconv()");
155 if (outbytes < OUTBUFSIZE)
156 (void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes, stdout);
157
158 if (invalids > 0 && !silent)
159 warnx("warning: invalid characters: %lu",
160 (unsigned long)invalids);
161
162 iconv_close(cd);
163 }
164
165 int
166 main(int argc, char **argv)
167 {
168 int ch, i;
169 int opt_l = 0, opt_s = 0, opt_c = 0;
170 char *opt_f = NULL, *opt_t = NULL;
171 FILE *fp;
172
173 setlocale(LC_ALL, "");
174 setprogname(argv[0]);
175
176 while ((ch = getopt(argc, argv, "cslf:t:")) != EOF) {
177 switch (ch) {
178 case 'c':
179 opt_c = 1;
180 break;
181 case 's':
182 opt_s = 1;
183 break;
184 case 'l':
185 /* list */
186 opt_l = 1;
187 break;
188 case 'f':
189 /* from */
190 opt_f = estrdup(optarg);
191 break;
192 case 't':
193 /* to */
194 opt_t = estrdup(optarg);
195 break;
196 default:
197 usage();
198 }
199 }
200 argc -= optind;
201 argv += optind;
202 if (opt_l) {
203 if (argc > 0 || opt_s || opt_f != NULL || opt_t != NULL) {
204 warnx("-l is not allowed with other flags.");
205 usage();
206 }
207 show_codesets();
208 } else {
209 if (opt_f == NULL) {
210 if (opt_t == NULL)
211 usage();
212 opt_f = nl_langinfo(CODESET);
213 } else if (opt_t == NULL)
214 opt_t = nl_langinfo(CODESET);
215
216 if (argc == 0)
217 do_conv("<stdin>", stdin, opt_f, opt_t, opt_s, opt_c);
218 else {
219 for (i = 0; i < argc; i++) {
220 fp = fopen(argv[i], "r");
221 if (fp == NULL)
222 err(EXIT_FAILURE, "Cannot open `%s'",
223 argv[i]);
224 do_conv(argv[i], fp, opt_f, opt_t, opt_s,
225 opt_c);
226 (void)fclose(fp);
227 }
228 }
229 }
230 return EXIT_SUCCESS;
231 }
232