xref: /netbsd-src/usr.bin/iconv/iconv.c (revision 77a1ad5f0039ea2bc6af76846debc20728bae498)
1 /*	$NetBSD: iconv.c,v 1.20 2019/10/24 18:18:00 kamil 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.20 2019/10/24 18:18:00 kamil 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) __dead;
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
usage(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
scmp(const void * v1,const void * v2)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
show_codesets(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 /*ARGSUSED*/
93 static void
do_conv(const char * fn,FILE * fp,const char * from,const char * to,int silent,int hide_invalid)94 do_conv(const char *fn, FILE *fp, const char *from, const char *to, int silent,
95     int hide_invalid)
96 {
97 	char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *out;
98 	const char *in;
99 	size_t inbytes, outbytes, ret, invalids;
100 	iconv_t cd;
101 	uint32_t flags = 0;
102 	int serrno;
103 
104 	if (hide_invalid)
105 		flags |= __ICONV_F_HIDE_INVALID;
106 	cd = iconv_open(to, from);
107 	if (cd == (iconv_t)-1)
108 		err(EXIT_FAILURE, "iconv_open(%s, %s)", to, from);
109 
110 	invalids = 0;
111 	while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) {
112 		in = inbuf;
113 		while (inbytes > 0) {
114 			size_t inval;
115 
116 			out = outbuf;
117 			outbytes = OUTBUFSIZE;
118 			ret = __iconv(cd, __UNCONST(&in), &inbytes, &out,
119 			    &outbytes, flags, &inval);
120 			serrno = errno;
121 			invalids += inval;
122 			if (outbytes < OUTBUFSIZE)
123 				(void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes,
124 				    stdout);
125 			errno = serrno;
126 			if (ret == (size_t)-1 && errno != E2BIG) {
127 				/*
128 				 * XXX: iconv(3) is bad interface.
129 				 *   invalid character count is lost here.
130 				 *   instead, we just provide __iconv function.
131 				 */
132 				if (errno != EINVAL || in == inbuf)
133 					err(EXIT_FAILURE, "iconv()");
134 
135 				/* incomplete input character */
136 				(void)memmove(inbuf, in, inbytes);
137 				ret = fread(inbuf + inbytes, 1,
138 				    INBUFSIZE - inbytes, fp);
139 				if (ret == 0) {
140 					fflush(stdout);
141 					if (feof(fp))
142 						errx(EXIT_FAILURE,
143 						     "unexpected end of file; "
144 						     "the last character is "
145 						     "incomplete.");
146 					else
147 						err(EXIT_FAILURE, "fread()");
148 				}
149 				in = inbuf;
150 				inbytes += ret;
151 			}
152 		}
153 	}
154 	/* reset the shift state of the output buffer */
155 	outbytes = OUTBUFSIZE;
156 	out = outbuf;
157 	ret = iconv(cd, NULL, NULL, &out, &outbytes);
158 	if (ret == (size_t)-1)
159 		err(EXIT_FAILURE, "iconv()");
160 	if (outbytes < OUTBUFSIZE)
161 		(void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes, stdout);
162 
163 	if (invalids > 0 && !silent)
164 		warnx("warning: invalid characters: %lu",
165 		    (unsigned long)invalids);
166 
167 	iconv_close(cd);
168 }
169 
170 int
main(int argc,char ** argv)171 main(int argc, char **argv)
172 {
173 	int ch, i;
174 	int opt_l = 0, opt_s = 0, opt_c = 0;
175 	char *opt_f = NULL, *opt_t = NULL;
176 	FILE *fp;
177 
178 	setlocale(LC_ALL, "");
179 	setprogname(argv[0]);
180 
181 	while ((ch = getopt(argc, argv, "cslf:t:")) != EOF) {
182 		switch (ch) {
183 		case 'c':
184 			opt_c = 1;
185 			break;
186 		case 's':
187 			opt_s = 1;
188 			break;
189 		case 'l':
190 			/* list */
191 			opt_l = 1;
192 			break;
193 		case 'f':
194 			/* from */
195 			opt_f = estrdup(optarg);
196 			break;
197 		case 't':
198 			/* to */
199 			opt_t = estrdup(optarg);
200 			break;
201 		default:
202 			usage();
203 		}
204 	}
205 	argc -= optind;
206 	argv += optind;
207 	if (opt_l) {
208 		if (argc > 0 || opt_s || opt_f != NULL || opt_t != NULL) {
209 			warnx("-l is not allowed with other flags.");
210 			usage();
211 		}
212 		show_codesets();
213 	} else {
214 		if (opt_f == NULL) {
215 			if (opt_t == NULL)
216 				usage();
217 			opt_f = nl_langinfo(CODESET);
218 		} else if (opt_t == NULL)
219 			opt_t = nl_langinfo(CODESET);
220 
221 		if (argc == 0)
222 			do_conv("<stdin>", stdin, opt_f, opt_t, opt_s, opt_c);
223 		else {
224 			for (i = 0; i < argc; i++) {
225 				fp = fopen(argv[i], "r");
226 				if (fp == NULL)
227 					err(EXIT_FAILURE, "Cannot open `%s'",
228 					    argv[i]);
229 				do_conv(argv[i], fp, opt_f, opt_t, opt_s,
230 				    opt_c);
231 				(void)fclose(fp);
232 			}
233 		}
234 	}
235 	return EXIT_SUCCESS;
236 }
237