1 /* $NetBSD: iconv.c,v 1.4 2003/10/20 12:56:18 yamt Exp $ */
2 /* $DragonFly: src/usr.bin/iconv/iconv.c,v 1.2 2008/07/10 18:29:51 swildner Exp $ */
5 * Copyright (c)2003 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
30 #include <sys/endian.h>
44 "\t%s [-cs] -f <from> -t <to> [file ...]\n"
46 getprogname(), getprogname());
51 * qsort() helper function
54 scmp(const void *v1
, const void *v2
)
56 const char * const *s1
= v1
;
57 const char * const *s2
= v2
;
59 return(strcasecmp(*s1
, *s2
));
68 if (__iconv_get_list(&list
, &sz
))
69 err(EXIT_FAILURE
, "__iconv_get_list()");
71 qsort(list
, sz
, sizeof(char *), scmp
);
73 for (i
=0; i
<sz
; i
++) {
74 printf("%s\n", list
[i
]);
77 __iconv_free_list(list
, sz
);
80 #define INBUFSIZE 1024
81 #define OUTBUFSIZE (INBUFSIZE*2)
84 do_conv(FILE *fp
, const char *from
, const char *to
, int silent
,
87 char inbuf
[INBUFSIZE
], outbuf
[OUTBUFSIZE
], *out
;
89 size_t inbytes
, outbytes
, invalids
;
95 flags
|= __ICONV_F_HIDE_INVALID
;
96 cd
= iconv_open(to
, from
);
97 if (cd
== (iconv_t
)-1)
98 err(EXIT_FAILURE
, "iconv_open(%s, %s)", to
, from
);
101 while ((inbytes
= fread(inbuf
, 1, INBUFSIZE
, fp
)) > 0) {
107 outbytes
= OUTBUFSIZE
;
108 ret
= __iconv(cd
, &in
, &inbytes
, &out
, &outbytes
,
111 if (ret
== -1 && errno
!= E2BIG
) {
113 * XXX: iconv(3) is bad interface.
114 * invalid character count is lost here.
115 * instead, we just provide __iconv function.
117 if (errno
!= EINVAL
|| in
== inbuf
)
118 err(EXIT_FAILURE
, "iconv()");
120 /* incomplete input character */
121 memmove(inbuf
, in
, inbytes
);
122 ret
= fread(inbuf
+inbytes
, 1,
123 INBUFSIZE
-inbytes
, fp
);
130 err(EXIT_FAILURE
, "fread()");
135 if (outbytes
< OUTBUFSIZE
)
136 fwrite(outbuf
, 1, OUTBUFSIZE
-outbytes
, stdout
);
139 /* reset the shift state of the output buffer */
140 outbytes
= OUTBUFSIZE
;
142 ret
= iconv(cd
, NULL
, NULL
, &out
, &outbytes
);
144 err(EXIT_FAILURE
, "iconv()");
145 if (outbytes
< OUTBUFSIZE
)
146 fwrite(outbuf
, 1, OUTBUFSIZE
-outbytes
, stdout
);
148 if (invalids
> 0 && !silent
)
149 warnx("warning: invalid characters: %lu",
150 (unsigned long)invalids
);
156 main(int argc
, char **argv
)
159 int opt_l
= 0, opt_s
= 0, opt_c
= 0;
160 char *opt_f
= NULL
, *opt_t
= NULL
;
163 while ((ch
=getopt(argc
, argv
, "cslf:t:")) != -1) {
177 opt_f
= strdup(optarg
);
181 opt_t
= strdup(optarg
);
190 if (argc
>0 || opt_s
|| opt_f
!= NULL
|| opt_t
!= NULL
) {
191 warnx("%s: -l should be specified solely.",
197 if (opt_f
== NULL
|| opt_t
== NULL
)
201 do_conv(stdin
, opt_f
, opt_t
, opt_s
, opt_c
);
203 for (i
=0; i
<argc
; i
++) {
204 fp
= fopen(argv
[i
], "r");
206 errx(EXIT_FAILURE
, "%s: %s:%s",
207 getprogname(), argv
[i
],
209 do_conv(fp
, opt_f
, opt_t
, opt_s
, opt_c
);
215 return(EXIT_SUCCESS
);