inpcb: Use netisr_ncpus for listing inpcbs.
[dragonfly.git] / usr.bin / iconv / iconv.c
blob6df3cb0d0be1bf7f49583bd7e1bec29810496126
1 /* $FreeBSD: head/usr.bin/iconv/iconv.c 252583 2013-07-03 18:27:45Z peter $ */
2 /* $NetBSD: iconv.c,v 1.16 2009/02/20 15:28:21 yamt Exp $ */
4 /*-
5 * Copyright (c)2003 Citrus Project,
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #include <sys/cdefs.h>
32 #include <err.h>
33 #include <errno.h>
34 #include <getopt.h>
35 #include <iconv.h>
36 #include <limits.h>
37 #include <locale.h>
38 #include <stdbool.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
44 static unsigned long long invalids;
46 static void do_conv(FILE *, const char *, const char *, bool, bool);
47 static int do_list(unsigned int, const char * const *, void *);
48 static void usage(void);
50 static struct option long_options[] = {
51 {"from-code", required_argument, NULL, 'f'},
52 {"list", no_argument, NULL, 'l'},
53 {"silent", no_argument, NULL, 's'},
54 {"to-code", required_argument, NULL, 't'},
55 {NULL, no_argument, NULL, 0}
58 static void
59 usage(void)
61 (void)fprintf(stderr,
62 "Usage:\t%1$s [-cs] -f <from_code> -t <to_code> [file ...]\n"
63 "\t%1$s -f <from_code> [-cs] [-t <to_code>] [file ...]\n"
64 "\t%1$s -t <to_code> [-cs] [-f <from_code>] [file ...]\n"
65 "\t%1$s -l\n", getprogname());
66 exit(1);
69 #define INBUFSIZE 1024
70 #define OUTBUFSIZE (INBUFSIZE * 2)
71 static void
72 do_conv(FILE *fp, const char *from, const char *to, bool silent,
73 bool hide_invalid)
75 iconv_t cd;
76 char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *in, *out;
77 size_t inbytes, outbytes, ret;
79 if ((cd = iconv_open(to, from)) == (iconv_t)-1)
80 err(EXIT_FAILURE, "iconv_open(%s, %s)", to, from);
82 if (hide_invalid) {
83 int arg = 1;
85 if (iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, (void *)&arg) == -1)
86 err(1, NULL);
88 while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) {
89 in = inbuf;
90 while (inbytes > 0) {
91 size_t inval;
93 out = outbuf;
94 outbytes = OUTBUFSIZE;
95 ret = __iconv(cd, &in, &inbytes, &out, &outbytes,
96 0, &inval);
97 invalids += inval;
98 if (outbytes < OUTBUFSIZE)
99 (void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes,
100 stdout);
101 if (ret == (size_t)-1 && errno != E2BIG) {
102 if (errno != EINVAL || in == inbuf)
103 err(EXIT_FAILURE, "iconv()");
105 /* incomplete input character */
106 (void)memmove(inbuf, in, inbytes);
107 ret = fread(inbuf + inbytes, 1,
108 INBUFSIZE - inbytes, fp);
109 if (ret == 0) {
110 fflush(stdout);
111 if (feof(fp))
112 errx(EXIT_FAILURE,
113 "unexpected end of file; "
114 "the last character is "
115 "incomplete.");
116 else
117 err(EXIT_FAILURE, "fread()");
119 in = inbuf;
120 inbytes += ret;
124 /* reset the shift state of the output buffer */
125 outbytes = OUTBUFSIZE;
126 out = outbuf;
127 ret = iconv(cd, NULL, NULL, &out, &outbytes);
128 if (ret == (size_t)-1)
129 err(EXIT_FAILURE, "iconv()");
130 if (outbytes < OUTBUFSIZE)
131 (void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes, stdout);
133 if (invalids > 0 && !silent)
134 warnx("warning: invalid characters: %llu", invalids);
136 iconv_close(cd);
139 static int
140 do_list(unsigned int n, const char * const *list, void *data __unused)
142 unsigned int i;
144 for(i = 0; i < n; i++) {
145 printf("%s", list[i]);
146 if (i < n - 1)
147 printf(" ");
149 printf("\n");
151 return (1);
155 main(int argc, char **argv)
157 FILE *fp;
158 char *opt_f, *opt_t;
159 int ch, i;
160 bool opt_c = false, opt_s = false;
162 opt_f = opt_t = strdup("");
164 setlocale(LC_ALL, "");
165 setprogname(argv[0]);
167 while ((ch = getopt_long(argc, argv, "csLlf:t:",
168 long_options, NULL)) != -1) {
169 switch (ch) {
170 case 'c':
171 opt_c = true;
172 break;
173 case 's':
174 opt_s = true;
175 break;
176 case 'l':
177 /* list */
178 if (opt_s || opt_c || strcmp(opt_f, "") != 0 ||
179 strcmp(opt_t, "") != 0) {
180 warnx("-l is not allowed with other flags.");
181 usage();
183 iconvlist(do_list, NULL);
184 return (EXIT_SUCCESS);
185 case 'f':
186 /* from */
187 if (optarg != NULL)
188 opt_f = strdup(optarg);
189 break;
190 case 't':
191 /* to */
192 if (optarg != NULL)
193 opt_t = strdup(optarg);
194 break;
195 default:
196 usage();
199 argc -= optind;
200 argv += optind;
201 if ((strcmp(opt_f, "") == 0) && (strcmp(opt_t, "") == 0))
202 usage();
203 if (argc == 0)
204 do_conv(stdin, opt_f, opt_t, opt_s, opt_c);
205 else {
206 for (i = 0; i < argc; i++) {
207 fp = (strcmp(argv[i], "-") != 0) ?
208 fopen(argv[i], "r") : stdin;
209 if (fp == NULL)
210 err(EXIT_FAILURE, "Cannot open `%s'",
211 argv[i]);
212 do_conv(fp, opt_f, opt_t, opt_s,
213 opt_c);
214 (void)fclose(fp);
217 return (EXIT_SUCCESS);