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