Date: Tue, 10 Jun 2014 17:13:29 +0200 From: Tijl Coosemans <tijl@FreeBSD.org> To: Pavel Timofeev <timp87@gmail.com> Cc: freebsd-stable stable <freebsd-stable@freebsd.org> Subject: Re: iconv exit code on 10.0 Message-ID: <20140610171329.0633ca62@kalimero.tijl.coosemans.org> In-Reply-To: <CAAoTqfsgta6Rxo-VGDkJtjHAqthnh4HC8E=GBnyuY0yM8PhfvA@mail.gmail.com> References: <CAAoTqfsgta6Rxo-VGDkJtjHAqthnh4HC8E=GBnyuY0yM8PhfvA@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
--MP_/tZgTu3wWF4q56Xh.b/ja2fz Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On Tue, 20 May 2014 16:04:21 +0400 Pavel Timofeev wrote: > Hi! >=20 > I've encountered with wrong iconv work. >=20 > root@timbsd:~ # uname -a > FreeBSD timbsd 10.0-RELEASE-p3 FreeBSD 10.0-RELEASE-p3 #0: Tue May 13 > 18:31:10 UTC 2014 > root@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC amd64 >=20 > root@timbsd:~ # locale > LANG=3D > LC_CTYPE=3Dru_RU.UTF-8 > LC_COLLATE=3D"C" > LC_TIME=3D"C" > LC_NUMERIC=3D"C" > LC_MONETARY=3D"C" > LC_MESSAGES=3D"C" > LC_ALL=3D >=20 > I have file with russian content. > root@timbsd:~ # cat /tmp/delete_it > =D0=9A=D0=B0=D0=BA=D0=BE=D0=B9-=D1=82=D0=BE =D1=82=D0=B5=D0=BA=D1=81=D1= =82 =D0=BD=D0=B0 =D1=80=D1=83=D1=81=D1=81=D0=BA=D0=BE=D0=BC. Some text on r= ussian >=20 > =D0=95=D1=89=D0=B5 =D0=BD=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE. Some more. >=20 >=20 > And I wanted to try to convert it to ISO. > root@timbsd:~ # /usr/bin/iconv -t ISO-8859-15 /tmp/delete_it > ?????-?? ????? ?? ???????. Some text on russian >=20 > ??? ???????. Some more. >=20 > iconv: warning: invalid characters: 31 >=20 > root@timbsd:~ # echo $? > 0 >=20 >=20 > Why exit code is 0? Base iconv didn't manage to convert strings so > exit code should be more than 0. That's really bad! >=20 >=20 > converters/libiconv from ports works better in this case! > See: > root@timbsd:~ # /usr/local/bin/iconv -t ISO-8859-15 /tmp/delete_it > /usr/local/bin/iconv: /tmp/delete_it:1:0: cannot convert > root@timbsd:~ # echo $? > 1 >=20 > I tried it on FreeBSD 11-CURRENT and base iconv doesn't have such problem= there. > So I hope that suitable fixes will be MFCd to 10-STABLE. Actually, FreeBSD 11 doesn't return an error either. Can you try the attached patch? --MP_/tZgTu3wWF4q56Xh.b/ja2fz Content-Type: text/x-patch Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=iconv.patch Index: usr.bin/iconv/iconv.c =================================================================== --- usr.bin/iconv/iconv.c (revision 267225) +++ usr.bin/iconv/iconv.c (working copy) @@ -41,13 +41,11 @@ #include <string.h> #include <unistd.h> -static unsigned long long invalids; - -static void do_conv(FILE *, const char *, const char *, bool, bool); -static int do_list(unsigned int, const char * const *, void *); -static void usage(void); +static int do_conv(FILE *, const char *, const char *, bool, bool); +static int do_list(unsigned int, const char * const *, void *); +static void usage(void) __dead2; -static struct option long_options[] = { +static const struct option long_options[] = { {"from-code", required_argument, NULL, 'f'}, {"list", no_argument, NULL, 'l'}, {"silent", no_argument, NULL, 's'}, @@ -68,12 +66,13 @@ usage(void) #define INBUFSIZE 1024 #define OUTBUFSIZE (INBUFSIZE * 2) -static void +static int do_conv(FILE *fp, const char *from, const char *to, bool silent, bool hide_invalid) { iconv_t cd; char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *out; + unsigned long long invalids; const char *in; size_t inbytes, outbytes, ret; @@ -84,8 +83,9 @@ do_conv(FILE *fp, const char *from, cons int arg = 1; if (iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, (void *)&arg) == -1) - err(1, NULL); + err(EXIT_FAILURE, NULL); } + invalids = 0; while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) { in = inbuf; while (inbytes > 0) { @@ -135,6 +135,7 @@ do_conv(FILE *fp, const char *from, cons warnx("warning: invalid characters: %llu", invalids); iconv_close(cd); + return (invalids > 0); } static int @@ -157,7 +158,7 @@ main(int argc, char **argv) { FILE *fp; char *opt_f, *opt_t; - int ch, i; + int ch, i, res; bool opt_c = false, opt_s = false; opt_f = opt_t = strdup(""); @@ -202,18 +203,18 @@ main(int argc, char **argv) if ((strcmp(opt_f, "") == 0) && (strcmp(opt_t, "") == 0)) usage(); if (argc == 0) - do_conv(stdin, opt_f, opt_t, opt_s, opt_c); + res = do_conv(stdin, opt_f, opt_t, opt_s, opt_c); else { + res = 0; for (i = 0; i < argc; i++) { fp = (strcmp(argv[i], "-") != 0) ? fopen(argv[i], "r") : stdin; if (fp == NULL) err(EXIT_FAILURE, "Cannot open `%s'", argv[i]); - do_conv(fp, opt_f, opt_t, opt_s, - opt_c); + res |= do_conv(fp, opt_f, opt_t, opt_s, opt_c); (void)fclose(fp); } } - return (EXIT_SUCCESS); + return (res == 0 ? EXIT_SUCCESS : EXIT_FAILURE); } --MP_/tZgTu3wWF4q56Xh.b/ja2fz--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20140610171329.0633ca62>