1 /* $NetBSD: iconv.c,v 1.16 2009/02/20 15:28:21 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.16 2009/02/20 15:28:21 yamt 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) __unused; 46 static int scmp(const void *, const void *); 47 static void show_codesets(void); 48 static void do_conv(const char *, FILE *, const char *, const char *, int, int); 49 50 static void 51 usage(void) 52 { 53 (void)fprintf(stderr, 54 "Usage:\t%1$s [-cs] -f <from_code> -t <to_code> [file ...]\n" 55 "\t%1$s -f <from_code> [-cs] [-t <to_code>] [file ...]\n" 56 "\t%1$s -t <to_code> [-cs] [-f <from_code>] [file ...]\n" 57 "\t%1$s -l\n", getprogname()); 58 exit(1); 59 } 60 61 /* 62 * qsort() helper function 63 */ 64 static int 65 scmp(const void *v1, const void *v2) 66 { 67 const char * const *s1 = v1; 68 const char * const *s2 = v2; 69 70 return strcasecmp(*s1, *s2); 71 } 72 73 static void 74 show_codesets(void) 75 { 76 char **list; 77 size_t sz, i; 78 79 if (__iconv_get_list(&list, &sz)) 80 err(EXIT_FAILURE, "__iconv_get_list()"); 81 82 qsort(list, sz, sizeof(char *), scmp); 83 84 for (i = 0; i < sz; i++) 85 (void)printf("%s\n", list[i]); 86 87 __iconv_free_list(list, sz); 88 } 89 90 #define INBUFSIZE 1024 91 #define OUTBUFSIZE (INBUFSIZE * 2) 92 static void 93 do_conv(const char *fn, FILE *fp, const char *from, const char *to, int silent, 94 int hide_invalid) 95 { 96 char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *out; 97 const char *in; 98 size_t inbytes, outbytes, ret, invalids; 99 iconv_t cd; 100 uint32_t flags = 0; 101 102 if (hide_invalid) 103 flags |= __ICONV_F_HIDE_INVALID; 104 cd = iconv_open(to, from); 105 if (cd == (iconv_t)-1) 106 err(EXIT_FAILURE, "iconv_open(%s, %s)", to, from); 107 108 invalids = 0; 109 while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) { 110 in = inbuf; 111 while (inbytes > 0) { 112 size_t inval; 113 114 out = outbuf; 115 outbytes = OUTBUFSIZE; 116 ret = __iconv(cd, &in, &inbytes, &out, &outbytes, 117 flags, &inval); 118 invalids += inval; 119 if (outbytes < OUTBUFSIZE) 120 (void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes, 121 stdout); 122 if (ret == (size_t)-1 && errno != E2BIG) { 123 /* 124 * XXX: iconv(3) is bad interface. 125 * invalid character count is lost here. 126 * instead, we just provide __iconv function. 127 */ 128 if (errno != EINVAL || in == inbuf) 129 err(EXIT_FAILURE, "iconv()"); 130 131 /* incomplete input character */ 132 (void)memmove(inbuf, in, inbytes); 133 ret = fread(inbuf + inbytes, 1, 134 INBUFSIZE - inbytes, fp); 135 if (ret == 0) { 136 fflush(stdout); 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 } 149 } 150 /* reset the shift state of the output buffer */ 151 outbytes = OUTBUFSIZE; 152 out = outbuf; 153 ret = iconv(cd, NULL, NULL, &out, &outbytes); 154 if (ret == (size_t)-1) 155 err(EXIT_FAILURE, "iconv()"); 156 if (outbytes < OUTBUFSIZE) 157 (void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes, stdout); 158 159 if (invalids > 0 && !silent) 160 warnx("warning: invalid characters: %lu", 161 (unsigned long)invalids); 162 163 iconv_close(cd); 164 } 165 166 int 167 main(int argc, char **argv) 168 { 169 int ch, i; 170 int opt_l = 0, opt_s = 0, opt_c = 0; 171 char *opt_f = NULL, *opt_t = NULL; 172 FILE *fp; 173 174 setlocale(LC_ALL, ""); 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) { 211 if (opt_t == NULL) 212 usage(); 213 opt_f = nl_langinfo(CODESET); 214 } else if (opt_t == NULL) 215 opt_t = nl_langinfo(CODESET); 216 217 if (argc == 0) 218 do_conv("<stdin>", stdin, opt_f, opt_t, opt_s, opt_c); 219 else { 220 for (i = 0; i < argc; i++) { 221 fp = fopen(argv[i], "r"); 222 if (fp == NULL) 223 err(EXIT_FAILURE, "Cannot open `%s'", 224 argv[i]); 225 do_conv(argv[i], fp, opt_f, opt_t, opt_s, 226 opt_c); 227 (void)fclose(fp); 228 } 229 } 230 } 231 return EXIT_SUCCESS; 232 } 233