File: get-utf8.c

package info (click to toggle)
kbtin 2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,856 kB
  • sloc: ansic: 18,627; perl: 179; sh: 88; makefile: 17
file content (41 lines) | stat: -rw-r--r-- 1,000 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <langinfo.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>

static void sl(const char* loc)
{
    if (!setlocale(LC_CTYPE, loc))
        return;
    if (strcmp(nl_langinfo(CODESET), "UTF-8"))
        return;
    printf("%s\n", loc);
    exit(0);
}

int main(void)
{
    /* try preferred values first */
    sl("C.UTF-8");
    sl("en_US.UTF-8");

    DIR* dir = opendir("/usr/share/locale");
    /* In that dir, FreeBSD and Solaris have qualified locale names.  OpenBSD
       has bare "UTF-8" which does work.  Linux has unqualified dirs whose
       names form ancient locales (except for some, like "vi"), but on the
       other hand, Linux is likely to have "C.UTF-8".
    */
    if (!dir)
    {
        fprintf(stderr, "Can't read /usr/share/locale: %s\n", strerror(errno));
        return 1;
    }
    struct dirent *d;
    while ((d = readdir(dir)))
        sl(d->d_name);
    return 1;
}