In the last episode (Oct 16), Bill Welliver said:
I've cobbled together a System.putenv() and System.getenv(), but I'm having some odd problems. The code itself works most of the time, but randomly getenv() fails to get an envvar I've putenv()ed (ie getenv(2) returns NULL). The functions are below, along with a short test script that demonstrates the problem. Any suggestions are welcome, as my C skills are quite rusty.
in modules/system/system.c:
#include <stdlib.h> static void f_system_putenv(INT32 args) { int rv; char *envvar; get_all_args("putenv", args, "%s", &envvar); pop_n_elems(args); rv=putenv(envvar); push_int(rv); }
You'll want to strdup envvar before calling putenv. http://www.opengroup.org/onlinepubs/007904975/functions/putenv.html :
The putenv() function shall make the value of the environment variable name equal to value by altering an existing variable or creating a new one. In either case, the string pointed to by string shall become part of the environment, so altering the string shall change the environment.
static void f_system_getenv(INT32 args) { char *rv; char *envvar; get_all_args("getenv", args, "%s", &envvar); pop_n_elems(args); rv=getenv(envvar); if(rv==NULL) push_int(0); else push_text(rv); }
You'll probably want to call make_shared_string on rv before returning it, too. http://www.opengroup.org/onlinepubs/007904975/functions/getenv.html :
The string pointed to may be overwritten by a subsequent call to getenv(), setenv(), or unsetenv(), but shall not be overwritten by a call to any other function in this volume of IEEE Std 1003.1-2001. The getenv() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe.