1.1 --- a/templates/native/locale.c Sun Dec 11 18:07:32 2016 +0100
1.2 +++ b/templates/native/locale.c Sun Dec 11 18:33:09 2016 +0100
1.3 @@ -17,6 +17,7 @@
1.4 */
1.5
1.6 #include <locale.h> /* setlocale */
1.7 +#include <string.h> /* strlen */
1.8 #include "native/common.h"
1.9 #include "types.h"
1.10 #include "exceptions.h"
1.11 @@ -31,33 +32,44 @@
1.12 __attr __fn_native_locale_getlocale(__attr __args[])
1.13 {
1.14 __attr * const category = &__args[1];
1.15 - /* category interpreted as int */
1.16 + /* category.__data__ interpreted as int */
1.17 int cat = __load_via_object(category->value, __pos___data__).intvalue;
1.18 - char *result;
1.19 + char *result, *out;
1.20 + size_t length;
1.21
1.22 result = setlocale(cat, NULL);
1.23
1.24 if (result == NULL)
1.25 return __builtins___none_None;
1.26 - else
1.27 - return __new_str(result);
1.28 +
1.29 + length = strlen(result);
1.30 + out = __ALLOCATE(length + 1, sizeof(char));
1.31 + strncpy(out, result, length);
1.32 +
1.33 + return __new_str(result);
1.34 }
1.35
1.36 __attr __fn_native_locale_setlocale(__attr __args[])
1.37 {
1.38 __attr * const category = &__args[1];
1.39 __attr * const value = &__args[2];
1.40 - /* category interpreted as int */
1.41 + /* category.__data__ interpreted as int */
1.42 int cat = __load_via_object(category->value, __pos___data__).intvalue;
1.43 - /* value interpreted as string */
1.44 - char *s = __load_via_object(value->value, __pos___data__).strvalue, *result;
1.45 + /* value.__data__ interpreted as string */
1.46 + char *s = __load_via_object(value->value, __pos___data__).strvalue;
1.47 + char *result, *out;
1.48 + size_t length;
1.49
1.50 result = setlocale(cat, s);
1.51
1.52 if (result == NULL)
1.53 return __builtins___none_None;
1.54 - else
1.55 - return __new_str(result);
1.56 +
1.57 + length = strlen(result);
1.58 + out = __ALLOCATE(length + 1, sizeof(char));
1.59 + strncpy(out, result, length);
1.60 +
1.61 + return __new_str(result);
1.62 }
1.63
1.64 /* Module initialisation. */