# nl_langinfo(CODESET) seems to always return US-ASCII whatever environment values



## graemeg (Sep 10, 2015)

As the subject line says, it always returns US-ASCII when I expected UTF-8 on my FreeBSD 10.1 system.

Here is a sample program to test this function:


```
/* Compile with "gcc48 -W -Wall -o langinfo langinfo_test.c" */
#include <stdio.h>
#include <langinfo.h>

int main()
{
  printf("%s\n", nl_langinfo(CODESET));
  return 0;
}
```

These are the environment variables setup on my system.

```
[tmp]$ locale
LANG=en_GB.UTF-8
LC_CTYPE="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_TIME=en_DK.UTF-8
LC_NUMERIC="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_ALL=
[tmp]$ export LC_TYPE=en_GB.UTF-8
[tmp]$ ./langinfo
US-ASCII
```

From the FreeBSD man page:



> HISTORY
> The nl_langinfo() function first appeared in FreeBSD 4.6.



FreeBSD 4.6 was released back in 2002. So I'm puzzled as to why it doesn't seem to work on my system.


----------



## kpa (Sep 10, 2015)

I think you need to call setlocale(3) as the first step in your program, otherwise your program will stay in the C locale as mentioned in the manual page.


----------



## graemeg (Sep 10, 2015)

KPA, that seems to do the trick. 

My update test program:


```
/* Compile with "gcc48 -W -Wall -o langinfo langinfo_test.c" */
#include <stdio.h>
#include <langinfo.h>
#include <locale.h>

int main()
{
   setlocale(LC_ALL,"");
  printf("%s\n", nl_langinfo(CODESET));
   return 0;
}
```

I'm actually an Object Pascal [http://www.freepascal.org] developer. In the Object Pascal unit we do call *setlocale(LC_ALL,"");* first, and then *nl_langinfo()*, but it doesn't seem to work there. Anyway, now that I know the C library indeed functions correctly under FreeBSD, I'll turn my attention back to the Object Pascal unit in question.

Many thanks for the quick response.


----------



## kpa (Sep 10, 2015)

The use of empty string second argument for setlocale(3) to use the environment variable values seems to be an extension that may not be available everywhere. The FreeBSD manual page doesn't mention it but it seems to work anyway.

http://pubs.opengroup.org/onlinepubs/009695399/functions/setlocale.html


----------

