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