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

Mike Frysinger vapier at gentoo.org
Sun Jun 28 17:51:55 UTC 2009


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


More information about the uClibc mailing list