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