[PATCH] gen_wc8bit: use a configurable UTF-8 locale

Andy Gibbs andyg1001 at hotmail.co.uk
Fri Apr 30 14:52:33 UTC 2010


Hi,

Sorry to open up a very old topic again, but I find that the patch detailed in the post below is incomplete.  It doesn't work in the scenario where the list returned by "locale -a" already has ".UTF8" already appended to it.

Please can I suggest a patch on top of this patch that solves this problem? (The diff is actually taken against the current "extra/locale/gen_wc8bit.c" in the git repository).

--- a/extra/locale/gen_wc8bit.c
+++ b/extra/locale/gen_wc8bit.c
@@ -112,9 +112,10 @@ int main(int argc, char **argv)
     goto locale_failure;

    len = strlen(buf);
-   if (buf[len - 1] == '\n')
+   if (len > 0 && buf[len - 1] == '\n')
     buf[--len] = '\0';
-   strcat(buf, ".UTF8");
+   if (len < 5 || (strcmp(&buf[len-5], ".utf8") != 0 && strcmp(&buf[len-5], ".UTF8") != 0))
+    strcat(buf, ".UTF8");
    if (setlocale(LC_CTYPE, buf))
     goto locale_success;
   }


This patch fixes two issues: first there is a potential segfault when trimming \n from the buffer; then it only appends ".UTF8" if the string doesn't already have it.  I compare both upper and lower case separately in absence of a dedicated stricmp/strcmpi.  In my case "locale -a" returns "en_GB.utf8".

Cheers
Andy


----- Original Message ----- 
From: "Mike Frysinger" <vapier at gentoo.org>
Newsgroups: gmane.comp.lib.uclibc.general
To: "Daniel Cordero" <theappleman at gmail.com>
Cc: <uclibc at uclibc.org>
Sent: Sunday, June 28, 2009 7:51 PM
Subject: Re: [PATCH] gen_wc8bit: use a configurable UTF-8 locale


> On Sunday 28 June 2009 13:42:00 Mike Frysinger wrote:
>> On Monday 01 June 2009 13:40:16 Daniel Cordero wrote:
>> > In some situations, the en_US.UTF-8 locale is not available (e.g. the
>> > user does not have it installed).
>> > Make a new option in the configuration for the user to specify an
>> > alternative.
>>
>> seems like something the user shouldnt need to worry about.  how about if
>> the default of en_US.UTF8 doesnt work, we popen("locale -a") and try each
>> one listed there with ".UTF8" appended to it.
>
> try the following patch please
>
> diff --git a/extra/locale/gen_wc8bit.c b/extra/locale/gen_wc8bit.c
> index 418a1ac..126cd1a 100644
> --- a/extra/locale/gen_wc8bit.c
> +++ b/extra/locale/gen_wc8bit.c
> @@ -98,8 +98,31 @@ int main(int argc, char **argv)
>  int total_size = 0;
>
>  if (!setlocale(LC_CTYPE, "en_US.UTF-8")) {
> - printf("setlocale(LC_CTYPE,\"en_US.UTF-8\") failed!\n");
> + /* Silly foreigners disabling en_US locales */
> + FILE *fp = popen("locale -a", "r");
> + if (!fp)
> + goto locale_failure;
> +
> + while (!feof(fp)) {
> + char buf[256];
> + size_t len;
> +
> + if (fgets(buf, sizeof(buf) - 10, fp) == NULL)
> + goto locale_failure;
> +
> + len = strlen(buf);
> + if (buf[len - 1] == '\n')
> + buf[--len] = '\0';
> + strcat(buf, ".UTF8");
> + if (setlocale(LC_CTYPE, buf))
> + goto locale_success;
> + }
> +
> + locale_failure:
> + printf("could not find a UTF8 locale ... please enable en_US.UTF-8\n");
>  return EXIT_FAILURE;
> + locale_success:
> + pclose(fp);
>  }
>
>  if (!(out = fopen("c8tables.h","w"))) {
>
> -mike 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gen_wc8bit.diff
Type: application/octet-stream
Size: 474 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/uclibc/attachments/20100430/8a8b4d80/attachment.obj>


More information about the uClibc mailing list