Home | History | Annotate | Line # | Download | only in iconv
iconv.c revision 1.5.4.1
      1 /*	$NetBSD: iconv.c,v 1.5.4.1 2005/05/13 20:35:28 riz 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.5.4.1 2005/05/13 20:35:28 riz 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 						errx(EXIT_FAILURE,
    139 						     "unexpected end of file; "
    140 						     "the last character is "
    141 						     "incomplete.");
    142 					else
    143 						err(EXIT_FAILURE, "fread()");
    144 				}
    145 				in = inbuf;
    146 				inbytes += ret;
    147 			}
    148 			if (outbytes < OUTBUFSIZE)
    149 				(void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes,
    150 				    stdout);
    151 		}
    152 	}
    153 	/* reset the shift state of the output buffer */
    154 	outbytes = OUTBUFSIZE;
    155 	out = outbuf;
    156 	ret = iconv(cd, NULL, NULL, &out, &outbytes);
    157 	if (ret == -1)
    158 		err(EXIT_FAILURE, "iconv()");
    159 	if (outbytes < OUTBUFSIZE)
    160 		(void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes, stdout);
    161 
    162 	if (invalids > 0 && !silent)
    163 		warnx("warning: invalid characters: %lu",
    164 		    (unsigned long)invalids);
    165 
    166 	iconv_close(cd);
    167 }
    168 
    169 int
    170 main(int argc, char **argv)
    171 {
    172 	int ch, i;
    173 	int opt_l = 0, opt_s = 0, opt_c = 0;
    174 	char *opt_f = NULL, *opt_t = NULL;
    175 	FILE *fp;
    176 
    177 	setprogname(argv[0]);
    178 
    179 	while ((ch = getopt(argc, argv, "cslf:t:")) != EOF) {
    180 		switch (ch) {
    181 		case 'c':
    182 			opt_c = 1;
    183 			break;
    184 		case 's':
    185 			opt_s = 1;
    186 			break;
    187 		case 'l':
    188 			/* list */
    189 			opt_l = 1;
    190 			break;
    191 		case 'f':
    192 			/* from */
    193 			opt_f = estrdup(optarg);
    194 			break;
    195 		case 't':
    196 			/* to */
    197 			opt_t = estrdup(optarg);
    198 			break;
    199 		default:
    200 			usage();
    201 		}
    202 	}
    203 	argc -= optind;
    204 	argv += optind;
    205 	if (opt_l) {
    206 		if (argc > 0 || opt_s || opt_f != NULL || opt_t != NULL) {
    207 			warnx("-l is not allowed with other flags.");
    208 			usage();
    209 		}
    210 		show_codesets();
    211 	} else {
    212 		if (opt_f == NULL || opt_t == NULL)
    213 			usage();
    214 
    215 		if (argc == 0)
    216 			do_conv("<stdin>", stdin, opt_f, opt_t, opt_s, opt_c);
    217 		else {
    218 			for (i = 0; i < argc; i++) {
    219 				fp = fopen(argv[i], "r");
    220 				if (fp == NULL)
    221 					err(EXIT_FAILURE, "Cannot open `%s'",
    222 					    argv[i]);
    223 				do_conv(argv[i], fp, opt_f, opt_t, opt_s,
    224 				    opt_c);
    225 				(void)fclose(fp);
    226 			}
    227 		}
    228 	}
    229 	return EXIT_SUCCESS;
    230 }
    231