From 6f3ed4a7b420ac5220e955abfe32bec689923871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 15 Jul 2025 10:15:39 +0800 Subject: [PATCH] Processing (Linux): cache the index of `$LANG` --- src/common/processing_linux.c | 42 ++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/src/common/processing_linux.c b/src/common/processing_linux.c index e4ba9bd37..701ca4833 100644 --- a/src/common/processing_linux.c +++ b/src/common/processing_linux.c @@ -53,6 +53,8 @@ static inline int ffPipe2(int* fds, int flags) #endif } + +// Not thread-safe const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool useStdErr) { int pipes[2]; @@ -67,16 +69,40 @@ const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool use posix_spawn_file_actions_addopen(&file_actions, useStdErr ? STDOUT_FILENO : STDERR_FILENO, "/dev/null", O_WRONLY, 0); pid_t childPid = -1; - char* oldLang = NULL; - int langIndex = -1; - for (int i = 0; environ[i] != NULL; i++) + static char* oldLang = NULL; + static int langIndex = -1; + + if (langIndex >= 0) { - if (ffStrStartsWith(environ[i], "LANG=")) + // Found before + if (oldLang) // oldLang was set only if it needed to be changed { - oldLang = environ[i]; - environ[i] = (char*) "LANG=C"; - langIndex = i; - break; + if (environ[langIndex] != oldLang) + { + // environ is changed outside of this function + langIndex = -1; + } + else + environ[langIndex] = (char*) "LANG=C.UTF-8"; + } + } + if (langIndex < 0) + { + for (int i = 0; environ[i] != NULL; i++) + { + if (ffStrStartsWith(environ[i], "LANG=")) + { + langIndex = i; + const char* langValue = environ[i] + 5; // Skip "LANG=" + if (ffStrEqualsIgnCase(langValue, "C") || + ffStrStartsWithIgnCase(environ[i], "C.") || + ffStrEqualsIgnCase(langValue, "en_US") || + ffStrStartsWithIgnCase(langValue, "en_US.")) + break; // No need to change LANG + oldLang = environ[i]; + environ[i] = (char*) "LANG=C.UTF-8"; // Set LANG to C.UTF-8 for consistent output + break; + } } }