public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Carlos O'Donell <carlos@redhat.com>
To: Godmar Back <godmar@gmail.com>
Cc: libc-help <libc-help@sourceware.org>
Subject: Re: why does `LANG` not influence the locale in GNU/Linux
Date: Thu, 27 Jan 2022 00:28:55 -0500	[thread overview]
Message-ID: <033b9a58-46c7-ceff-212f-203bce879172@redhat.com> (raw)
In-Reply-To: <CAB4+JYLdJbJfp4CdfxQV4BrxTcHN_0mhtE+Ae-+hOD+Qt44Z-w@mail.gmail.com>

On 1/26/22 19:35, Godmar Back via Libc-help wrote:
> If I am writing code that uses fgetwc to read from standard input, I
> need to call setlocale(LC_CTYPE, "en_US.utf8"); to ensure that fgetwc
> will treat stdin as a UTF-8 encoded stream.  This property appears to
> hold (on Ubuntu 20, with default GNU libc), independent of the setting
> of LANG.  LANG is set to en_US.utf8.
> 
> By contrast, Python 3 changes its behavior with regard to the encoding
> it assumes sys.stdin to be in based on the LANG variable - if set to
> en_US.utf8, it'll decode as UTF-8, if set to C or unset, it'll treat
> the input as 8-bit ASCII encoding (I believe).
> 
> My question is why GNU libc (chose?) to not use LANG and what
> standards, if any, apply here.
> Is my characterization of the behavior correct?

The ISO C standard and the POSIX standard have specified the behaviour.

ISO C says that at program startup the equivalent of:
setlocale (LC_ALL, "C"); /* (See 7.11.1.1.4) */
is executed. Therefore C programs start in the C locale, not the locale
as specified by LANG.

POSIX defines several environment variables, and they have precedence
rules for deciding which one should be used by the application [1].
However, this just defines the categories, but it doesn't *apply* them to the
running program. Internationalized programs that wish to initialize locale
specific operation must call setlocale [2]:

setlocale (LC_CALL, "");

Which will set up the global locale according to the environment variables
in the precedence as setup by POSIX.

Does that answer your question?

-- 
Cheers,
Carlos.

[1] https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_02
[2] https://pubs.opengroup.org/onlinepubs/9699919799/functions/setlocale.html


  reply	other threads:[~2022-01-27  5:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-27  0:35 Godmar Back
2022-01-27  5:28 ` Carlos O'Donell [this message]
2022-01-27  5:40   ` Godmar Back
2022-01-27  5:53     ` Carlos O'Donell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=033b9a58-46c7-ceff-212f-203bce879172@redhat.com \
    --to=carlos@redhat.com \
    --cc=godmar@gmail.com \
    --cc=libc-help@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).