Skip to content

Commit 5df5bbf

Browse files
committed
Global: better handling locales
1 parent 8964681 commit 5df5bbf

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/common/init.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
#include <stdlib.h>
1010
#include <unistd.h>
11+
#include <locale.h>
1112
#ifdef _WIN32
1213
#include <windows.h>
13-
#include <locale.h>
1414
#include "util/windows/unicode.h"
1515
#else
1616
#include <signal.h>
@@ -51,6 +51,13 @@ void ffInitInstance(void)
5151
#ifdef WIN32
5252
//https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale?source=recommendations&view=msvc-170#utf-8-support
5353
setlocale(LC_ALL, ".UTF8");
54+
#else
55+
// Never use `setlocale(LC_ALL, "")`
56+
setlocale(LC_TIME, "");
57+
setlocale(LC_NUMERIC, "");
58+
#ifdef LC_MESSAGES
59+
setlocale(LC_MESSAGES, "");
60+
#endif
5461
#endif
5562

5663
initState(&instance.state);

src/detection/locale/locale_linux.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ void ffDetectLocale(FFstrbuf* result)
1212
if(result->length > 0)
1313
return;
1414

15-
setlocale(LC_ALL, "");
1615
#ifdef LC_MESSAGES
1716
ffStrbufAppendS(result, setlocale(LC_MESSAGES, NULL));
18-
19-
if(result->length == 0)
20-
ffStrbufAppendS(result, setlocale(LC_ALL, NULL));
17+
if(result->length > 0)
18+
return;
2119
#endif
22-
setlocale(LC_ALL, "C");
20+
21+
ffStrbufAppendS(result, setlocale(LC_TIME, NULL));
2322
}

src/modules/separator/separator.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ static inline uint32_t getWcsWidth(const FFstrbuf* mbstr, wchar_t* wstr, mbstate
3333

3434
void ffPrintSeparator(FFSeparatorOptions* options)
3535
{
36-
setlocale(LC_ALL, "");
37-
36+
setlocale(LC_CTYPE, "");
3837
mbstate_t state = {};
3938
bool fqdn = instance.config.modules.title.fqdn;
4039
const FFPlatform* platform = &instance.state.platform;
@@ -90,8 +89,7 @@ void ffPrintSeparator(FFSeparatorOptions* options)
9089
}
9190
}
9291
putchar('\n');
93-
94-
setlocale(LC_ALL, "C");
92+
setlocale(LC_CTYPE, "C");
9593
}
9694

9795
bool ffParseSeparatorCommandOptions(FFSeparatorOptions* options, const char* key, const char* value)

0 commit comments

Comments
 (0)