This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

clearer illustration of locale-related initialization bug


Hopefully this example will be a better illustration of a previously
reported problem involving wide output and locale initialization order.


/* Illustration of a locale-related initialization bug (second try).
   Annotated output for glibc -- cvs Sep 10  14:37 CDT

In the first run, we try (in order) "fa_IR.UTF-8" and "C".

  $ ./a.out fa_IR.UTF-8 C 2>&1 | hexdump -C
  00000000  20 db b0 0a 20 db b0 0a  20 30 0a 20 30 0a        | ... ... 0. 0.|

In the second run, we reverse the order of the locales and
try (in order) "C" and "fa_IR.UTF-8".

  $ ./a.out C fa_IR.UTF-8 2>&1 | hexdump -C
  00000000  20 30 0a 20 30 0a 20 db  b0 0a 20 3f 0a           | 0. 0. ... ?.|

Note that the number of bytes output is different depending on the order
of locales specified.  If the "C" locale is used first, then when in
the "fa_IR.UTF-8" locale, the bytes output to stdout by "wprintf(L"%s", buf);"
for buf with bytes { 0x20, 0xdb,  0xb0, 0x0a } are { 0x20, 0x3f, 0x0a }
instead of the expected { 0x20, 0xdb,  0xb0, 0x0a }.


#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>

int main(int argc, char **argv)
    int n, nw;
#define NBUF 128
    char buf[NBUF];

    if (argc < 2) {
        fprintf(stderr, "usage: %s [locale name]...\n", argv[0]);
        return EXIT_FAILURE;

    while (*++argv) {
        if (!setlocale(LC_ALL, *argv)) {
            fprintf(stderr, "setlocale for %s failed!\n", *argv);
            return EXIT_FAILURE;

        n = snprintf(buf, NBUF, " %I'ld\n", 0L);
        if (n <= 0) {
            fprintf(stderr, "snprintf failed!\n");
            return EXIT_FAILURE;

        if (fprintf(stderr, "%s", buf) != n) {
            fprintf(stderr, "fprintf failed!\n");
            return EXIT_FAILURE;

        nw = wprintf(L"%s", buf);
        if (nw <= 0) {
            fprintf(stderr, "wprintf failed!\n");
            return EXIT_FAILURE;

        if (fflush(stdout)) {
            fprintf(stderr, "fflush failed!\n");
            return EXIT_FAILURE;

    return EXIT_SUCCESS;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]