iconv.c revision 1.1 1 /* $NetBSD: iconv.c,v 1.1 2003/06/27 05:21:55 tshiozak 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.1 2003/06/27 05:21:55 tshiozak Exp $");
32 #endif /* LIBC_SCCS and not lint */
33
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <iconv.h>
39 #include <unistd.h>
40 #include <err.h>
41
42 static void
43 usage(void)
44 {
45 fprintf(stderr,
46 "usage:\n"
47 "\t%s [-cs] -f <from> -t <to> [file ...]\n"
48 "\t%s -l\n",
49 getprogname(), getprogname());
50 exit(1);
51 }
52
53 static void
54 show_codesets(void)
55 {
56 char **list;
57 size_t sz, i;
58
59 if (__iconv_get_list(&list, &sz))
60 err(EXIT_FAILURE, "__iconv_get_list()");
61
62 for (i=0; i<sz; i++) {
63 printf("%s\n", list[i]);
64 }
65
66 __iconv_free_list(list, sz);
67 }
68
69 #define INBUFSIZE 1024
70 #define OUTBUFSIZE (INBUFSIZE*2)
71 static void
72 do_conv(const char *fn, FILE *fp, const char *from, const char *to, int silent,
73 int hide_invalid)
74 {
75 char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *out;
76 const char *in;
77 size_t inbytes, outbytes, ret, invalids;
78 iconv_t cd;
79 u_int32_t flags = 0;
80
81 if (hide_invalid)
82 flags |= __ICONV_F_HIDE_INVALID;
83 cd = iconv_open(to, from);
84 if (cd == (iconv_t)-1)
85 err(EXIT_FAILURE, "iconv_open()");
86
87 invalids = 0;
88 while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) {
89 in = inbuf;
90 while (inbytes>0) {
91 out = outbuf;
92 outbytes = OUTBUFSIZE;
93 ret = __iconv(cd, &in, &inbytes, &out, &outbytes,
94 flags, &invalids);
95 if (ret==(size_t)-1 && errno != E2BIG) {
96 /*
97 * XXX: iconv(3) is bad interface.
98 * invalid character count is lost here.
99 * instead, we just provide __iconv function.
100 */
101 if (errno != EINVAL || in == inbuf)
102 err(EXIT_FAILURE, "iconv()");
103
104 /* incomplete input character */
105 memmove(inbuf, in, inbytes);
106 ret = fread(inbuf+inbytes, 1,
107 INBUFSIZE-inbytes, fp);
108 if (ret == 0) {
109 if (feof(fp))
110 errx(EXIT_FAILURE,
111 "iconv(): %s",
112 strerror(EINVAL));
113 else
114 err(EXIT_FAILURE, "fread()");
115 }
116 in = inbuf;
117 inbytes += ret;
118 }
119 if (outbytes < OUTBUFSIZE)
120 fwrite(outbuf, 1, OUTBUFSIZE-outbytes, stdout);
121 }
122 }
123 /* reset the shift state of the output buffer */
124 outbytes = OUTBUFSIZE;
125 out = outbuf;
126 ret = iconv(cd, NULL, NULL, &out, &outbytes);
127 if (ret == -1)
128 err(EXIT_FAILURE, "iconv()");
129 if (outbytes < OUTBUFSIZE)
130 fwrite(outbuf, 1, OUTBUFSIZE-outbytes, stdout);
131
132 if (invalids > 0 && !silent)
133 warnx("warning: invalid characters: %d", invalids);
134
135 iconv_close(cd);
136 }
137
138 int
139 main(int argc, char **argv)
140 {
141 int ch, i;
142 extern char *optarg;
143 extern int optind;
144 int opt_l = 0, opt_s = 0, opt_c = 0;
145 char *opt_f = NULL, *opt_t = NULL;
146 FILE *fp;
147
148 while ((ch=getopt(argc, argv, "cslf:t:")) != EOF) {
149 switch (ch) {
150 case 'c':
151 opt_c = 1;
152 break;
153 case 's':
154 opt_s = 1;
155 break;
156 case 'l':
157 /* list */
158 opt_l = 1;
159 break;
160 case 'f':
161 /* from */
162 opt_f = strdup(optarg);
163 break;
164 case 't':
165 /* to */
166 opt_t = strdup(optarg);
167 break;
168 default:
169 usage();
170 }
171 }
172 argc-=optind;
173 argv+=optind;
174 if (opt_l) {
175 if (argc>0 || opt_s || opt_f != NULL || opt_t != NULL) {
176 warnx("%s: -l should be specified solely.",
177 getprogname());
178 usage();
179 }
180 show_codesets();
181 } else {
182 if (opt_f == NULL || opt_t == NULL)
183 usage();
184
185 if (argc == 0)
186 do_conv("<stdin>", stdin, opt_f, opt_t, opt_s, opt_c);
187 else {
188 for (i=0; i<argc; i++) {
189 fp = fopen(argv[i], "r");
190 if (fp == NULL)
191 errx(EXIT_FAILURE, "%s: %s:%s",
192 getprogname(), argv[i],
193 strerror(errno));
194 do_conv(argv[i], fp, opt_f, opt_t, opt_s,
195 opt_c);
196 fclose(fp);
197 }
198 }
199 }
200
201 return EXIT_SUCCESS;
202 }
203