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