Changeset

4934:5a6a85719b7b

util.pposix: Add setenv()
author Kim Alvefur <zash@zash.se>
date Tue, 03 Jul 2012 15:43:46 +0200
parents 4933:630cb694b364
children 4935:bc62abceef07
files util-src/pposix.c
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/util-src/pposix.c	Mon Jun 18 16:57:46 2012 +0100
+++ b/util-src/pposix.c	Tue Jul 03 15:43:46 2012 +0200
@@ -581,6 +581,37 @@
 	return 1;
 }
 
+int lc_setenv(lua_State* L)
+{
+	const char *var = luaL_checkstring(L, 1);
+	const char *value;
+
+	/* If the second argument is nil or nothing, unset the var */
+	if(lua_isnoneornil(L, 2))
+	{
+		if(unsetenv(var) != 0)
+		{
+			lua_pushnil(L);
+			lua_pushstring(L, strerror(errno));
+			return 2;
+		}
+		lua_pushboolean(L, 1);
+		return 1;
+	}
+
+	value = luaL_checkstring(L, 2);
+
+	if(setenv(var, value, 1) != 0)
+	{
+		lua_pushnil(L);
+		lua_pushstring(L, strerror(errno));
+		return 2;
+	}
+
+	lua_pushboolean(L, 1);
+	return 1;
+}
+
 /* Register functions */
 
 int luaopen_util_pposix(lua_State *L)
@@ -612,6 +643,8 @@
 
 		{ "uname", lc_uname },
 
+		{ "setenv", lc_setenv },
+
 		{ NULL, NULL }
 	};