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.
Bill
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); }
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); }
and later:
ADD_FUNCTION("putenv", f_system_putenv, tFunc(tStr, tInt), 0); ADD_FUNCTION("getenv", f_system_getenv, tFunc(tStr, tOr(tStr, tInt)), 0);
test script:
int fail=0; int main() { string var="DISPLAY"; string val="BASE"; int a='A'; for(int i=0; i<2050; i++) { val+=String.int2char(a); werror("iteration " + i + "\n"); doit(var,val); } werror("failures: " + fail + "\n"); return 1; } void doit(string var, string val) { if(System.putenv(var+"="+ val)) werror("failed putenv\n"); string r=System.getenv(var); if(r!=val) { fail++; }
The test fails consistently and in the same locations for a given environment variable. The manpage doesn't say what might cause failures like this.
Any thoughts?
Bill