Permalink
Browse files

Set the collate locale to C globally

This is done to make sure that when this goes multithreaded, things don't
break because setlocale() isn't thread safe.
  • Loading branch information...
1 parent b2e4fe2 commit f91bb4e413f3b2d56420452ae38e3ed3b5c59181 @spc476 spc476 committed Jan 15, 2011
Showing with 9 additions and 18 deletions.
  1. +8 −0 src/clients/gnugol.c
  2. +1 −18 src/common/handy.c
View
@@ -24,6 +24,7 @@ along with this program. If not, see http://www.gnu.org/licenses/
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
+#include <locale.h>
#include <assert.h>
#include <getopt.h>
@@ -437,6 +438,13 @@ int main(int argc, char **argv) {
QueryOptions_t master;
QueryOptions_t q;
GnuGolEngine engine;
+
+ /*------------------------------------------------------------------------
+ ; sorted data in this program is collated in the C locale. Set that here
+ ; to make sure the program returns sane data when calling bsearch().
+ ;-----------------------------------------------------------------------*/
+
+ setlocale(LC_COLLATE,"C");
ListInit(&c_engines);
View
@@ -295,26 +295,10 @@ size_t substitute_utf8(char *string,size_t lenstr,char *entity)
assert(string != NULL);
assert(entity != NULL);
assert(*entity == '&');
+
if (lenstr < 5) /* not enough space for worst case scenario */
return 0;
- /*-------------------------------------------------------------------------
- ; The above table is sorted using the C collate sequence (US-ASCII) so we
- ; need to make sure our locale is set so a subsequent search will return
- ; the appropriate value. But since I don't want to make any assumptions
- ; as to the locale elsewhere in the code, we have the current locale, set
- ; it, then restore it upon return.
- ;-------------------------------------------------------------------------*/
-
- char *collate = setlocale(LC_COLLATE,NULL);
- if (collate == NULL)
- collate = "";
-
- char savecollate[strlen(collate) + 1];
- memcpy(savecollate,collate,strlen(collate) + 1);
-
- setlocale(LC_COLLATE,"C");
-
const struct entitymap *e;
size_t len;
size_t size;
@@ -348,7 +332,6 @@ size_t substitute_utf8(char *string,size_t lenstr,char *entity)
memcpy(string,buf,size);
string[size] = '\0';
- setlocale(LC_COLLATE,savecollate);
return size;
}

0 comments on commit f91bb4e

Please sign in to comment.