1 /* $NetBSD: iconv.c,v 1.15 2009/02/20 15:27:08 yamt Exp $ */
4 * Copyright (c)2003 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: iconv.c,v 1.15 2009/02/20 15:27:08 yamt Exp $");
32 #endif /* LIBC_SCCS and not lint */
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);
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());
62 * qsort() helper function
65 scmp(const void *v1
, const void *v2
)
67 const char * const *s1
= v1
;
68 const char * const *s2
= v2
;
70 return strcasecmp(*s1
, *s2
);
79 if (__iconv_get_list(&list
, &sz
))
80 err(EXIT_FAILURE
, "__iconv_get_list()");
82 qsort(list
, sz
, sizeof(char *), scmp
);
84 for (i
= 0; i
< sz
; i
++)
85 (void)printf("%s\n", list
[i
]);
87 __iconv_free_list(list
, sz
);
90 #define INBUFSIZE 1024
91 #define OUTBUFSIZE (INBUFSIZE * 2)
93 do_conv(const char *fn
, FILE *fp
, const char *from
, const char *to
, int silent
,
96 char inbuf
[INBUFSIZE
], outbuf
[OUTBUFSIZE
], *out
;
98 size_t inbytes
, outbytes
, ret
, invalids
;
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
);
109 while ((inbytes
= fread(inbuf
, 1, INBUFSIZE
, fp
)) > 0) {
111 while (inbytes
> 0) {
115 outbytes
= OUTBUFSIZE
;
116 ret
= __iconv(cd
, &in
, &inbytes
, &out
, &outbytes
,
119 if (outbytes
< OUTBUFSIZE
)
120 (void)fwrite(outbuf
, 1, OUTBUFSIZE
- outbytes
,
122 if (ret
== (size_t)-1 && errno
!= E2BIG
) {
124 * XXX: iconv(3) is bad interface.
125 * invalid character count is lost here.
126 * instead, we just provide __iconv function.
128 if (errno
!= EINVAL
|| in
== inbuf
)
129 err(EXIT_FAILURE
, "iconv()");
131 /* incomplete input character */
132 (void)memmove(inbuf
, in
, inbytes
);
133 ret
= fread(inbuf
+ inbytes
, 1,
134 INBUFSIZE
- inbytes
, fp
);
139 "unexpected end of file; "
140 "the last character is "
143 err(EXIT_FAILURE
, "fread()");
150 /* reset the shift state of the output buffer */
151 outbytes
= OUTBUFSIZE
;
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
);
159 if (invalids
> 0 && !silent
)
160 warnx("warning: invalid characters: %lu",
161 (unsigned long)invalids
);
167 main(int argc
, char **argv
)
170 int opt_l
= 0, opt_s
= 0, opt_c
= 0;
171 char *opt_f
= NULL
, *opt_t
= NULL
;
174 setlocale(LC_ALL
, "");
175 setprogname(argv
[0]);
177 while ((ch
= getopt(argc
, argv
, "cslf:t:")) != EOF
) {
191 opt_f
= estrdup(optarg
);
195 opt_t
= estrdup(optarg
);
204 if (argc
> 0 || opt_s
|| opt_f
!= NULL
|| opt_t
!= NULL
) {
205 warnx("-l is not allowed with other flags.");
213 opt_f
= nl_langinfo(CODESET
);
214 } else if (opt_t
== NULL
)
215 opt_t
= nl_langinfo(CODESET
);
218 do_conv("<stdin>", stdin
, opt_f
, opt_t
, opt_s
, opt_c
);
220 for (i
= 0; i
< argc
; i
++) {
221 fp
= fopen(argv
[i
], "r");
223 err(EXIT_FAILURE
, "Cannot open `%s'",
225 do_conv(argv
[i
], fp
, opt_f
, opt_t
, opt_s
,