I see that setlocale() can also query the current locale by using a NULL arg. So it might be worth querying it first and only setting it if was the default "C" locale - since if there is no LANG environment variable set then setlocale(LC_ALL,"") would set it to "C" when there might have been a better default to start with.
The Linux variant I am using is Fedora 17, Cygwin appears to behave the same, but others might be different.
a test program:
Code: Select all
#include <stdio.h>
#include <locale.h>
#include <langinfo.h>
int main() {
printf("original locale is %s\n", setlocale(LC_ALL, NULL));
printf("code set is %s\n", nl_langinfo(CODESET));
setlocale(LC_ALL, "");
printf("locale is now %s\n", setlocale(LC_ALL, NULL));
printf("code set is %s\n", nl_langinfo(CODESET));
return 0;
}
produces:
Code: Select all
original locale is C
code set is ANSI_X3.4-1968
locale is now en_GB.UTF-8
code set is UTF-8
or:
Code: Select all
original locale is C
code set is ANSI_X3.4-1968
locale is now C
code set is ANSI_X3.4-1968
if LANG is unset