Home | History | Annotate | Line # | Download | only in iconv
iconv.c revision 1.6
      1 /*	$NetBSD: iconv.c,v 1.6 2004/12/21 11:33:07 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.6 2004/12/21 11:33:07 yamt 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 /*
     54  * qsort() helper function
     55  */
     56 static int
     57 scmp(const void *v1, const void *v2)
     58 {
     59 	const char * const *s1 = v1;
     60 	const char * const *s2 = v2;
     61 
     62 	return strcasecmp(*s1, *s2);
     63 }
     64 
     65 static void
     66 show_codesets(void)
     67 {
     68 	char **list;
     69 	size_t sz, i;
     70 
     71 	if (__iconv_get_list(&list, &sz))
     72 		err(EXIT_FAILURE, "__iconv_get_list()");
     73 
     74 	qsort(list, sz, sizeof(char *), scmp);
     75 
     76 	for (i=0; i<sz; i++) {
     77 		printf("%s\n", list[i]);
     78 	}
     79 
     80 	__iconv_free_list(list, sz);
     81 }
     82 
     83 #define INBUFSIZE 1024
     84 #define OUTBUFSIZE (INBUFSIZE*2)
     85 static void
     86 do_conv(const char *fn, FILE *fp, const char *from, const char *to, int silent,
     87 	int hide_invalid)
     88 {
     89 	char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *out;
     90 	const char *in;
     91 	size_t inbytes, outbytes, ret, invalids;
     92 	iconv_t cd;
     93 	u_int32_t flags = 0;
     94 
     95 	if (hide_invalid)
     96 		flags |= __ICONV_F_HIDE_INVALID;
     97 	cd = iconv_open(to, from);
     98 	if (cd == (iconv_t)-1)
     99 		err(EXIT_FAILURE, "iconv_open(%s, %s)", to, from);
    100 
    101 	invalids = 0;
    102 	while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) {
    103 		in = inbuf;
    104 		while (inbytes>0) {
    105 			size_t inval;
    106 
    107 			out = outbuf;
    108 			outbytes = OUTBUFSIZE;
    109 			ret = __iconv(cd, &in, &inbytes, &out, &outbytes,
    110 				      flags, &inval);
    111 			invalids += inval;
    112 			if (ret==(size_t)-1 && errno != E2BIG) {
    113 				/*
    114 				 * XXX: iconv(3) is bad interface.
    115 				 *   invalid character count is lost here.
    116 				 *   instead, we just provide __iconv function.
    117 				 */
    118 				if (errno != EINVAL || in == inbuf)
    119 					err(EXIT_FAILURE, "iconv()");
    120 
    121 				/* incomplete input character */
    122 				memmove(inbuf, in, inbytes);
    123 				ret = fread(inbuf+inbytes, 1,
    124 					    INBUFSIZE-inbytes, fp);
    125 				if (ret == 0) {
    126 					if (feof(fp))
    127 						errx(EXIT_FAILURE,
    128 						     "iconv(): %s",
    129 						     strerror(EINVAL));
    130 					else
    131 						err(EXIT_FAILURE, "fread()");
    132 				}
    133 				in = inbuf;
    134 				inbytes += ret;
    135 			}
    136 			if (outbytes < OUTBUFSIZE)
    137 				fwrite(outbuf, 1, OUTBUFSIZE-outbytes, stdout);
    138 		}
    139 	}
    140 	/* reset the shift state of the output buffer */
    141 	outbytes = OUTBUFSIZE;
    142 	out = outbuf;
    143 	ret = iconv(cd, NULL, NULL, &out, &outbytes);
    144 	if (ret == -1)
    145 		err(EXIT_FAILURE, "iconv()");
    146 	if (outbytes < OUTBUFSIZE)
    147 		fwrite(outbuf, 1, OUTBUFSIZE-outbytes, stdout);
    148 
    149 	if (invalids > 0 && !silent)
    150 		warnx("warning: invalid characters: %lu",
    151 		      (unsigned long)invalids);
    152 
    153 	iconv_close(cd);
    154 }
    155 
    156 int
    157 main(int argc, char **argv)
    158 {
    159 	int ch, i;
    160 	extern char *optarg;
    161 	extern int optind;
    162 	int opt_l = 0, opt_s = 0, opt_c = 0;
    163 	char *opt_f = NULL, *opt_t = NULL;
    164 	FILE *fp;
    165 
    166 	while ((ch=getopt(argc, argv, "cslf:t:")) != EOF) {
    167 		switch (ch) {
    168 		case 'c':
    169 			opt_c = 1;
    170 			break;
    171 		case 's':
    172 			opt_s = 1;
    173 			break;
    174 		case 'l':
    175 			/* list */
    176 			opt_l = 1;
    177 			break;
    178 		case 'f':
    179 			/* from */
    180 			opt_f = strdup(optarg);
    181 			break;
    182 		case 't':
    183 			/* to */
    184 			opt_t = strdup(optarg);
    185 			break;
    186 		default:
    187 			usage();
    188 		}
    189 	}
    190 	argc-=optind;
    191 	argv+=optind;
    192 	if (opt_l) {
    193 		if (argc>0 || opt_s || opt_f != NULL || opt_t != NULL) {
    194 			warnx("%s: -l should be specified solely.",
    195 			      getprogname());
    196 			usage();
    197 		}
    198 		show_codesets();
    199 	} else {
    200 		if (opt_f == NULL || opt_t == NULL)
    201 			usage();
    202 
    203 		if (argc == 0)
    204 			do_conv("<stdin>", stdin, opt_f, opt_t, opt_s, opt_c);
    205 		else {
    206 			for (i=0; i<argc; i++) {
    207 				fp = fopen(argv[i], "r");
    208 				if (fp == NULL)
    209 					errx(EXIT_FAILURE, "%s: %s:%s",
    210 					     getprogname(), argv[i],
    211 					     strerror(errno));
    212 				do_conv(argv[i], fp, opt_f, opt_t, opt_s,
    213 					opt_c);
    214 				fclose(fp);
    215 			}
    216 		}
    217 	}
    218 
    219 	return EXIT_SUCCESS;
    220 }
    221