Skip to content

Instantly share code, notes, and snippets.

@minoki
Created June 10, 2018 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minoki/c3d65d15982f17db0801e8a9b06333bf to your computer and use it in GitHub Desktop.
Save minoki/c3d65d15982f17db0801e8a9b06333bf to your computer and use it in GitHub Desktop.
Experiments on MSVCRT's setlocale() and system()
// dumparg.c
#include <stdio.h>
#include <wchar.h>
int wmain(int argc, const wchar_t *argv[])
{
for (int i = 0; i < argc; ++i) {
const wchar_t *s = argv[i];
printf("argv[%d] = ", i);
while (*s != 0) {
if (*s < 0x80 && isprint(*s)) {
printf("%02x(%c) ", (unsigned int)*s, (unsigned int)*s);
} else {
printf("%02x ", (unsigned int)*s);
}
s++;
}
puts("");
}
}
C:\...> cl /Fe:localetest-classic.exe localetest.c
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.31101 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
localetest.c
Microsoft (R) Incremental Linker Version 12.00.31101.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:localetest-classic.exe
localetest.obj
C:\...> localetest-classic
LC_CTYPE = C
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
LC_CTYPE = C
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
setlocale(LC_CTYPE, "") succeeded
LC_CTYPE = Japanese_Japan.932
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
setlocale(LC_CTYPE, ".UTF-8") failed
LC_CTYPE = Japanese_Japan.932
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = 7e3a
setlocale(LC_CTYPE, "French_France") succeeded
LC_CTYPE = French_France.1252
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
setlocale(LC_CTYPE, ".UTF-8") failed
LC_CTYPE = French_France.1252
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = 7e3a
setlocale(LC_CTYPE, "Greek_Greece") succeeded
LC_CTYPE = Greek_Greece.1253
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
_setmbcp(_MB_CP_ANSI) succeeded
LC_CTYPE = Greek_Greece.1253
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
_setmbcp(_MB_CP_LOCALE) succeeded
LC_CTYPE = Greek_Greece.1253
mbcp = _MB_CP_SBCS
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
_setmbcp(_MB_CP_SBCS) succeeded
LC_CTYPE = Greek_Greece.1253
mbcp = _MB_CP_SBCS
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
_setmbcp(65001) failed
LC_CTYPE = Greek_Greece.1253
mbcp = _MB_CP_SBCS
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = 7e3a
C:\...> cl /Fe:localetest-ucrt-dynamic.exe /MD localetest.c
Microsoft(R) C/C++ Optimizing Compiler Version 19.13.26129 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
localetest.c
Microsoft (R) Incremental Linker Version 14.13.26129.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:localetest-ucrt-dynamic.exe
localetest.obj
C:\...> localetest-ucrt-dynamic
LC_CTYPE = C
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = b2 b3
LC_CTYPE = C
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = b2 b3
setlocale(LC_CTYPE, "") succeeded
LC_CTYPE = Japanese_Japan.932
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
setlocale(LC_CTYPE, ".UTF-8") succeeded
LC_CTYPE = Japanese_Japan.utf8
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = 3042
setlocale(LC_CTYPE, "French_France") succeeded
LC_CTYPE = French_France.1252
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = b2 b3
setlocale(LC_CTYPE, ".UTF-8") succeeded
LC_CTYPE = Japanese_Japan.utf8
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = 3042
setlocale(LC_CTYPE, "Greek_Greece") succeeded
LC_CTYPE = Greek_Greece.1253
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = b2 b3
_setmbcp(_MB_CP_ANSI) succeeded
LC_CTYPE = Greek_Greece.1253
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = b2 b3
_setmbcp(_MB_CP_LOCALE) succeeded
LC_CTYPE = Greek_Greece.1253
mbcp = _MB_CP_SBCS
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = b2 b3
_setmbcp(_MB_CP_SBCS) succeeded
LC_CTYPE = Greek_Greece.1253
mbcp = _MB_CP_SBCS
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = b2 b3
_setmbcp(65001) succeeded
LC_CTYPE = Greek_Greece.1253
mbcp = _MB_CP_SBCS
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = 3b3 81 201a
C:\...> cl /Fe:localetest-ucrt.exe localetest.c
Microsoft(R) C/C++ Optimizing Compiler Version 19.13.26129 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
localetest.c
Microsoft (R) Incremental Linker Version 14.13.26129.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:localetest-ucrt.exe
localetest.obj
C:\...> localetest-ucrt
LC_CTYPE = C
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
LC_CTYPE = C
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
setlocale(LC_CTYPE, "") succeeded
LC_CTYPE = Japanese_Japan.932
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
setlocale(LC_CTYPE, ".UTF-8") failed
LC_CTYPE = Japanese_Japan.932
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = 7e3a
setlocale(LC_CTYPE, "French_France") succeeded
LC_CTYPE = French_France.1252
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
setlocale(LC_CTYPE, ".UTF-8") failed
LC_CTYPE = French_France.1252
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = 7e3a
setlocale(LC_CTYPE, "Greek_Greece") succeeded
LC_CTYPE = Greek_Greece.1253
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
_setmbcp(_MB_CP_ANSI) succeeded
LC_CTYPE = Greek_Greece.1253
mbcp = 932
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
_setmbcp(_MB_CP_LOCALE) succeeded
LC_CTYPE = Greek_Greece.1253
mbcp = _MB_CP_SBCS
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
_setmbcp(_MB_CP_SBCS) succeeded
LC_CTYPE = Greek_Greece.1253
mbcp = _MB_CP_SBCS
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = ff72 ff73
_setmbcp(65001) failed
LC_CTYPE = Greek_Greece.1253
mbcp = _MB_CP_SBCS
argv[0] = 64(d) 75(u) 6d(m) 70(p) 61(a) 72(r) 67(g) 2e(.) 65(e) 78(x) 65(e)
argv[1] = 7e3a
// localetest.c
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <mbctype.h>
void printlocale(void)
{
printf("LC_CTYPE = %s\n", setlocale(LC_CTYPE, NULL));
int mbcp = _getmbcp();
switch (mbcp) {
case _MB_CP_ANSI: puts("mbcp = _MB_CP_ANSI"); break;
case _MB_CP_LOCALE: puts("mbcp = _MB_CP_LOCALE"); break;
case _MB_CP_OEM: puts("mbcp = _MB_CP_OEM"); break;
case _MB_CP_SBCS: puts("mbcp = _MB_CP_SBCS"); break;
default: printf("mbcp = %d\n", mbcp); break;
}
}
int main(void)
{
printlocale();
system("dumparg.exe \xb2\xb3");
setlocale(LC_CTYPE, "C");
printlocale();
system("dumparg.exe \xb2\xb3");
printf("setlocale(LC_CTYPE, \"\") %s\n", setlocale(LC_CTYPE, "") != NULL ? "succeeded" : "failed");
printlocale();
system("dumparg.exe \xb2\xb3");
printf("setlocale(LC_CTYPE, \".UTF-8\") %s\n", setlocale(LC_CTYPE, ".UTF-8") != NULL ? "succeeded" : "failed");
printlocale();
system("dumparg.exe \xe3\x81\x82");
printf("setlocale(LC_CTYPE, \"French_France\") %s\n", setlocale(LC_CTYPE, "French_France") != NULL ? "succeeded" : "failed");
printlocale();
system("dumparg.exe \xb2\xb3");
printf("setlocale(LC_CTYPE, \".UTF-8\") %s\n", setlocale(LC_CTYPE, ".UTF-8") != NULL ? "succeeded" : "failed");
printlocale();
system("dumparg.exe \xe3\x81\x82");
printf("setlocale(LC_CTYPE, \"Greek_Greece\") %s\n", setlocale(LC_CTYPE, "Greek_Greece") != NULL ? "succeeded" : "failed");
printlocale();
system("dumparg.exe \xb2\xb3");
printf("_setmbcp(_MB_CP_ANSI) %s\n", _setmbcp(_MB_CP_ANSI) >= 0 ? "succeeded" : "failed");
printlocale();
system("dumparg.exe \xb2\xb3");
printf("_setmbcp(_MB_CP_LOCALE) %s\n", _setmbcp(_MB_CP_LOCALE) >= 0 ? "succeeded" : "failed");
printlocale();
system("dumparg.exe \xb2\xb3");
printf("_setmbcp(_MB_CP_SBCS) %s\n", _setmbcp(_MB_CP_SBCS) >= 0 ? "succeeded" : "failed");
printlocale();
system("dumparg.exe \xb2\xb3");
printf("_setmbcp(65001) %s\n", _setmbcp(65001) >= 0 ? "succeeded" : "failed");
printlocale();
system("dumparg.exe \xe3\x81\x82");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment