summaryrefslogtreecommitdiffstats
path: root/sysif.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2015-04-18 10:47:14 -0700
committerKaz Kylheku <kaz@kylheku.com>2015-04-18 10:47:14 -0700
commite21f19001551eddbb8d27acea1327590ce124461 (patch)
tree20d20e1f3ed7deca262fcd0c2bd366af88715c81 /sysif.c
parentc765b773e63b2099a050fef21b5e7f06296af2ec (diff)
downloadtxr-e21f19001551eddbb8d27acea1327590ce124461.tar.gz
txr-e21f19001551eddbb8d27acea1327590ce124461.tar.bz2
txr-e21f19001551eddbb8d27acea1327590ce124461.zip
Adding getenv, setenv and unsetenv.
* lib.c (setenv, unsetenv): Changed static functions to external. Moved them out of the #if !HAVE_TIMEGM block. * lib.h (setenv, unsetenv): Declared. * sysif.c (getenv_wrap, setenv_wrap, unsetenv_wrap): New functions. (sysif_init): Registered getenv, setenv and unsetenv. * txr.1: Documented getenv, setenv and unsetenv. * tl.vim, txr.vim: Regenerated.
Diffstat (limited to 'sysif.c')
-rw-r--r--sysif.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/sysif.c b/sysif.c
index 8f659ced..880e4a47 100644
--- a/sysif.c
+++ b/sysif.c
@@ -572,6 +572,34 @@ static val pipe_wrap(void)
#endif
+static val getenv_wrap(val name)
+{
+ char *nameu8 = utf8_dup_to(c_str(name));
+ char *lookup = getenv(nameu8);
+ val result = lookup ? string_utf8(lookup) : nil;
+ free(nameu8);
+ return result;
+}
+
+static val setenv_wrap(val name, val value, val overwrite)
+{
+ char *nameu8 = utf8_dup_to(c_str(name));
+ char *valu8 = utf8_dup_to(c_str(value));
+ setenv(nameu8, valu8, default_arg(overwrite, t) != nil);
+ free(valu8);
+ free(nameu8);
+ return value;
+}
+
+static val unsetenv_wrap(val name)
+{
+ char *nameu8 = utf8_dup_to(c_str(name));
+ unsetenv(nameu8);
+ free(nameu8);
+ return name;
+}
+
+
void sysif_init(void)
{
reg_fun(intern(lit("errno"), user_package), func_n1o(errno_wrap, 0));
@@ -733,4 +761,7 @@ void sysif_init(void)
#if HAVE_PIPE
reg_fun(intern(lit("pipe"), user_package), func_n0(pipe_wrap));
#endif
+ reg_fun(intern(lit("getenv"), user_package), func_n1(getenv_wrap));
+ reg_fun(intern(lit("setenv"), user_package), func_n3o(setenv_wrap, 2));
+ reg_fun(intern(lit("unsetenv"), user_package), func_n1(unsetenv_wrap));
}