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