changeset 29290:ef46d5a66bb2

Use a malloced string for the get_term_charset return value. This is necessary at least on POSIX systems since the buffer returned by nl_langinfo may change its contents with e.g. each setlocale call.
author reimar
date Sun, 31 May 2009 13:00:51 +0000
parents 6825c69f6d84
children 79f3477b277e
files mp_msg.c osdep/getch2-os2.c osdep/getch2-win.c osdep/getch2.c
diffstat 4 files changed, 14 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/mp_msg.c	Sun May 31 12:48:53 2009 +0000
+++ b/mp_msg.c	Sun May 31 13:00:51 2009 +0000
@@ -8,6 +8,13 @@
 #ifdef CONFIG_ICONV
 #include <iconv.h>
 #include <errno.h>
+/**
+ * \brief gets the name of the system's terminal character set
+ * \return a malloced string indicating the system charset
+ *
+ * Be warned that this function on many systems is in no way thread-safe
+ * since it modifies global data
+ */
 char* get_term_charset(void);
 #endif
 
--- a/osdep/getch2-os2.c	Sun May 31 12:48:53 2009 +0000
+++ b/osdep/getch2-os2.c	Sun May 31 13:00:51 2009 +0000
@@ -26,6 +26,7 @@
 #include <os2.h>
 
 #include <stdio.h>
+#include <string.h>
 
 #include "config.h"
 #include "keycodes.h"
@@ -189,7 +190,7 @@
 
 #ifdef HAVE_LANGINFO
     setlocale( LC_CTYPE, "");
-    charset = nl_langinfo( CODESET );
+    charset = strdup( nl_langinfo( CODESET ));
     setlocale( LC_CTYPE, "C");
 #endif
 
--- a/osdep/getch2-win.c	Sun May 31 12:48:53 2009 +0000
+++ b/osdep/getch2-win.c	Sun May 31 13:00:51 2009 +0000
@@ -25,6 +25,7 @@
 
 #include "config.h"
 #include <stdio.h>
+#include <string.h>
 #include <windows.h>
 #include "keycodes.h"
 #include "input/input.h"
@@ -187,7 +188,7 @@
 
 char* get_term_charset(void)
 {
-    static char codepage[10];
+    char codepage[10];
     unsigned i, cpno = GetConsoleOutputCP();
     if (!cpno)
         cpno = GetACP();
@@ -196,9 +197,9 @@
 
     for (i = 0; cp_alias[i].cp; i++)
         if (cpno == cp_alias[i].cp)
-            return cp_alias[i].alias;
+            return strdup(cp_alias[i].alias);
 
     snprintf(codepage, sizeof(codepage), "CP%u", cpno);
-    return codepage;
+    return strdup(codepage);
 }
 #endif
--- a/osdep/getch2.c	Sun May 31 12:48:53 2009 +0000
+++ b/osdep/getch2.c	Sun May 31 13:00:51 2009 +0000
@@ -297,7 +297,7 @@
     char* charset = NULL;
 #ifdef HAVE_LANGINFO
     setlocale(LC_CTYPE, "");
-    charset = nl_langinfo(CODESET);
+    charset = strdup(nl_langinfo(CODESET));
     setlocale(LC_CTYPE, "C");
 #endif
     return charset;