Thread-secure setlocale(3).
This commit is contained in:
parent
c4e6888fd9
commit
3361620d83
|
@ -1,6 +1,6 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
|
|
||||||
Copyright(C) Jonas 'Sortie' Termansen 2012.
|
Copyright(C) Jonas 'Sortie' Termansen 2012, 2014.
|
||||||
|
|
||||||
This file is part of the Sortix C Library.
|
This file is part of the Sortix C Library.
|
||||||
|
|
||||||
|
@ -25,9 +25,11 @@
|
||||||
#define __SORTIX_STDLIB_REDIRECTS 0
|
#define __SORTIX_STDLIB_REDIRECTS 0
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
static pthread_mutex_t locale_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
static char* current_locales[LC_NUM_CATEGORIES] = { NULL };
|
static char* current_locales[LC_NUM_CATEGORIES] = { NULL };
|
||||||
|
|
||||||
extern "C" const char* sortix_setlocale(int category, const char* locale)
|
extern "C" const char* sortix_setlocale(int category, const char* locale)
|
||||||
|
@ -48,11 +50,13 @@ extern "C" const char* sortix_setlocale(int category, const char* locale)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pthread_mutex_lock(&locale_lock);
|
||||||
for ( int i = from; i <= to; i++ )
|
for ( int i = from; i <= to; i++ )
|
||||||
{
|
{
|
||||||
free(current_locales[i]);
|
free(current_locales[i]);
|
||||||
current_locales[i] = new_strings[i];
|
current_locales[i] = new_strings[i];
|
||||||
}
|
}
|
||||||
|
pthread_mutex_unlock(&locale_lock);
|
||||||
return locale;
|
return locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue