freopen() with "ccs=<charset>" in the mode

Ilya Dryomov idryomov@gmail.com
Sun Sep 16 08:05:00 GMT 2018


Hello,

How is freopen() supposed to work with "ccs=<charset>" in the mode?

  #include <assert.h>
  #include <stdio.h>
  #include <wchar.h>

  int main()
  {
          FILE *fp;

          fp = fopen("/tmp/foo", "w,ccs=UTF-16LE");
          assert(fwide(fp, 0) > 0); // ok
          fp = freopen(NULL, "w,ccs=UTF-16LE", fp);
          assert(fwide(fp, 0) > 0); // failure
  }

The man page explicitly lists freopen() alongside fopen():

  In addition to the above characters, fopen() and freopen() support
  the following syntax in mode:

      ,ccs=string

  The  given  string is taken as the name of a coded character set
  and the stream is marked as wide-oriented.

However freopen() appears to unconditionally put the stream into the
unoriented state.  When one of the wide functions is called sometime
later, the stream picks up the current locale -- the "ccs" parameter
is effectively ignored even though it is parsed.

Thanks,

                Ilya



More information about the Libc-alpha mailing list