From 7505c6e024369f2ef381512cbc4a790cc6503428 Mon Sep 17 00:00:00 2001 From: Mark H Weaver Date: Thu, 16 Jun 2011 15:07:30 -0400 Subject: Fix several POSIX functions to use the locale encoding * libguile/strings.c (scm_i_allocate_string_pointers): Encode strings using the current locale. Previously, Latin-1 was used. Indirectly, this affects the encoding of strings in `system*', `execl', `execlp', `execle', `environ', and `dynamic-args-call'. (scm_makfromstrs): In header comment, clarify that the C strings are interpreted according to the current locale encoding. * NEWS: Add NEWS entry. --- libguile/strings.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'libguile/strings.c') diff --git a/libguile/strings.c b/libguile/strings.c index 0d379ff68..24c82fc3e 100644 --- a/libguile/strings.c +++ b/libguile/strings.c @@ -2052,8 +2052,9 @@ SCM_DEFINE (scm_string_normalize_nfkd, "string-normalize-nfkd", 1, 0, 0, } #undef FUNC_NAME -/* converts C scm_array of strings to SCM scm_list of strings. */ -/* If argc < 0, a null terminated scm_array is assumed. */ +/* converts C scm_array of strings to SCM scm_list of strings. + If argc < 0, a null terminated scm_array is assumed. + The current locale encoding is assumed */ SCM scm_makfromstrs (int argc, char **argv) { @@ -2067,37 +2068,43 @@ scm_makfromstrs (int argc, char **argv) } /* Return a newly allocated array of char pointers to each of the strings - in args, with a terminating NULL pointer. */ + in args, with a terminating NULL pointer. The strings are encoded using + the current locale. */ char ** scm_i_allocate_string_pointers (SCM list) #define FUNC_NAME "scm_i_allocate_string_pointers" { char **result; - int len = scm_ilength (list); + int list_len = scm_ilength (list); int i; - if (len < 0) + if (list_len < 0) scm_wrong_type_arg_msg (NULL, 0, list, "proper list"); - result = scm_gc_malloc ((len + 1) * sizeof (char *), + result = scm_gc_malloc ((list_len + 1) * sizeof (char *), "string pointers"); - result[len] = NULL; + result[list_len] = NULL; - /* The list might be have been modified in another thread, so + /* The list might have been modified in another thread, so we check LIST before each access. */ - for (i = 0; i < len && scm_is_pair (list); i++) + for (i = 0; i < list_len && scm_is_pair (list); i++) { - SCM str; - size_t len; - - str = SCM_CAR (list); - len = scm_c_string_length (str); - - result[i] = scm_gc_malloc_pointerless (len + 1, "string pointers"); - memcpy (result[i], scm_i_string_chars (str), len); + SCM str = SCM_CAR (list); + size_t len; /* String length in bytes */ + char *c_str = scm_to_locale_stringn (str, &len); + + /* OPTIMIZE-ME: Right now, scm_to_locale_stringn always uses + scm_malloc to allocate the returned string, which must be + explicitly deallocated. This forces us to copy the string a + second time into a new buffer. Ideally there would be variants + of scm_to_*_stringn that can return garbage-collected buffers. */ + + result[i] = scm_gc_malloc_pointerless (len + 1, "string"); + memcpy (result[i], c_str, len); result[i][len] = '\0'; + free (c_str); list = SCM_CDR (list); } -- cgit v1.2.3