From 76ef3e8e8ad42a32b801615c4858aefb6a03988c Mon Sep 17 00:00:00 2001 From: "Kirill A. Korinsky" Date: Wed, 20 Sep 2023 18:27:37 +0000 Subject: [PATCH] Fix access to environ on Darwin --- src/c/main.d | 77 ------------------------------------------------- src/c/unixsys.d | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 77 deletions(-) diff --git a/src/c/main.d b/src/c/main.d index fbaae6674..84b797453 100644 --- a/src/c/main.d +++ b/src/c/main.d @@ -840,83 +840,6 @@ si_argv(cl_object index) FEerror("Illegal argument index: ~S.", 1, index); } -cl_object -si_getenv(cl_object var) -{ - const char *value; - - /* Strings have to be null terminated base strings */ - var = si_copy_to_simple_base_string(var); - value = getenv((char*)var->base_string.self); - @(return ((value == NULL)? ECL_NIL : ecl_make_simple_base_string(value,-1))); -} - -#if defined(HAVE_SETENV) || defined(HAVE_PUTENV) -cl_object -si_setenv(cl_object var, cl_object value) -{ - const cl_env_ptr the_env = ecl_process_env(); - cl_fixnum ret_val; - - /* Strings have to be null terminated base strings */ - var = si_copy_to_simple_base_string(var); - if (value == ECL_NIL) { -#ifdef HAVE_SETENV - /* Remove the variable when setting to nil, so that - * (si:setenv "foo" nil), then (si:getenv "foo) returns - * the right thing. */ - unsetenv((char*)var->base_string.self); -#else -#if defined(ECL_MS_WINDOWS_HOST) - si_setenv(var, cl_core.null_string); -#else - putenv((char*)var->base_string.self); -#endif -#endif - ret_val = 0; - } else { -#ifdef HAVE_SETENV - value = si_copy_to_simple_base_string(value); - ret_val = setenv((char*)var->base_string.self, - (char*)value->base_string.self, 1); -#else - value = cl_format(4, ECL_NIL, @"~A=~A", var, - value); - value = si_copy_to_simple_base_string(value); - putenv((char*)value->base_string.self); -#endif - } - if (ret_val == -1) - CEerror(ECL_T, "SI:SETENV failed: insufficient space in environment.", - 1, ECL_NIL); - ecl_return1(the_env, value); -} -#endif - -cl_object -si_environ(void) -{ - cl_object output = ECL_NIL; -#ifdef HAVE_ENVIRON - char **p; - extern char **environ; - for (p = environ; *p; p++) { - output = CONS(ecl_make_constant_base_string(*p,-1), output); - } - output = cl_nreverse(output); -#else -# if defined(ECL_MS_WINDOWS_HOST) - LPTCH p; - for (p = GetEnvironmentStrings(); *p; ) { - output = CONS(ecl_make_constant_base_string(p,-1), output); - do { (void)0; } while (*(p++)); - } - output = cl_nreverse(output); -# endif -#endif /* HAVE_ENVIRON */ - @(return output); -} - cl_object si_pointer(cl_object x) { diff --git a/src/c/unixsys.d b/src/c/unixsys.d index 89ad5493b..32eae16c0 100644 --- a/src/c/unixsys.d +++ b/src/c/unixsys.d @@ -57,6 +57,82 @@ si_system(cl_object cmd_string) #endif } +cl_object +si_getenv(cl_object var) +{ + const char *value; + + /* Strings have to be null terminated base strings */ + var = si_copy_to_simple_base_string(var); + value = getenv((char*)var->base_string.self); + @(return ((value == NULL)? ECL_NIL : ecl_make_simple_base_string(value,-1))); +} + +#if defined(HAVE_SETENV) || defined(HAVE_PUTENV) +cl_object +si_setenv(cl_object var, cl_object value) +{ + const cl_env_ptr the_env = ecl_process_env(); + cl_fixnum ret_val; + + /* Strings have to be null terminated base strings */ + var = si_copy_to_simple_base_string(var); + if (value == ECL_NIL) { +#ifdef HAVE_SETENV + /* Remove the variable when setting to nil, so that + * (si:setenv "foo" nil), then (si:getenv "foo) returns + * the right thing. */ + unsetenv((char*)var->base_string.self); +#else +#if defined(ECL_MS_WINDOWS_HOST) + si_setenv(var, cl_core.null_string); +#else + putenv((char*)var->base_string.self); +#endif +#endif + ret_val = 0; + } else { +#ifdef HAVE_SETENV + value = si_copy_to_simple_base_string(value); + ret_val = setenv((char*)var->base_string.self, + (char*)value->base_string.self, 1); +#else + value = cl_format(4, ECL_NIL, @"~A=~A", var, + value); + value = si_copy_to_simple_base_string(value); + putenv((char*)value->base_string.self); +#endif + } + if (ret_val == -1) + CEerror(ECL_T, "SI:SETENV failed: insufficient space in environment.", + 1, ECL_NIL); + ecl_return1(the_env, value); +} +#endif + +cl_object +si_environ(void) +{ + cl_object output = ECL_NIL; +#ifdef HAVE_ENVIRON + char **p; + for (p = environ; *p; p++) { + output = CONS(ecl_make_constant_base_string(*p,-1), output); + } + output = cl_nreverse(output); +#else +# if defined(ECL_MS_WINDOWS_HOST) + LPTCH p; + for (p = GetEnvironmentStrings(); *p; ) { + output = CONS(ecl_make_constant_base_string(p,-1), output); + do { (void)0; } while (*(p++)); + } + output = cl_nreverse(output); +# endif +#endif /* HAVE_ENVIRON */ + @(return output); +} + cl_object si_getpid(void) {