Bug #142

locale support not POSIX compliant

Added by Garrett D'Amore over 10 years ago. Updated over 10 years ago.

Start date:
Due date:
% Done:


Estimated time:
3.00 h
Gerrit CR:


Joerg has indicated that only LANG is properly honored in the locale support. The following was reported:

The reason why the i18n code does not work correctly is that the order of the 
locale names did change.

Currently only setting LANG has the desired effect, but LANG should have the 
lowes priority after LC_* while LC_ALL should have the highest priority.

The original order in locale.c:

 * Category names for getenv() 
static char *categories[_LC_LAST] = { 

works as expected. 

If you change this order, you of course also need to change any code that 
depends on the order of this array. This is e.g. 

static const char * 
        int category; 
        const char *env; 

        /* 1. check LC_ALL. */ 
        env = getenv(categories[0]); 

        /* 2. check LC_* */ 
        if (env == NULL || !*env) 
                env = getenv(categories[category]); 

        /* 3. check LANG */ 
        if (env == NULL || !*env) 
                env = getenv("LANG"); 

        /* 4. if none is set, fall to "C" */ 
        if (env == NULL || !*env) 
                env = "C"; 

        return (env); 

It turns out that he also identified a problem with an unitialized reference to buf after being alloca'd.

The code was totally busted.

Joerg gave some code that I used as a starting point, but I've also made some changes of my own to make it better, fixing the alloca problem, making it more readable, and verifying the correct functionality for composite locales, which I don't think ever worked properly.

Now everything seems to work, and setlocale() appears to honor the POSIX precedence rules for environment variables.

Diffs coming shortly.


t.c (600 Bytes) t.c This is a simple program I used to test setlocale with. Garrett D'Amore, 2010-09-03 01:24 AM

Also available in: Atom PDF