Comparison

util-src/pposix.c @ 4934:5a6a85719b7b

util.pposix: Add setenv()
author Kim Alvefur <zash@zash.se>
date Tue, 03 Jul 2012 15:43:46 +0200
parent 4415:0091db139229
child 4946:2975c7008ccd
comparison
equal deleted inserted replaced
4933:630cb694b364 4934:5a6a85719b7b
579 lua_pushstring(L, uname_info.machine); 579 lua_pushstring(L, uname_info.machine);
580 lua_setfield(L, -2, "machine"); 580 lua_setfield(L, -2, "machine");
581 return 1; 581 return 1;
582 } 582 }
583 583
584 int lc_setenv(lua_State* L)
585 {
586 const char *var = luaL_checkstring(L, 1);
587 const char *value;
588
589 /* If the second argument is nil or nothing, unset the var */
590 if(lua_isnoneornil(L, 2))
591 {
592 if(unsetenv(var) != 0)
593 {
594 lua_pushnil(L);
595 lua_pushstring(L, strerror(errno));
596 return 2;
597 }
598 lua_pushboolean(L, 1);
599 return 1;
600 }
601
602 value = luaL_checkstring(L, 2);
603
604 if(setenv(var, value, 1) != 0)
605 {
606 lua_pushnil(L);
607 lua_pushstring(L, strerror(errno));
608 return 2;
609 }
610
611 lua_pushboolean(L, 1);
612 return 1;
613 }
614
584 /* Register functions */ 615 /* Register functions */
585 616
586 int luaopen_util_pposix(lua_State *L) 617 int luaopen_util_pposix(lua_State *L)
587 { 618 {
588 luaL_Reg exports[] = { 619 luaL_Reg exports[] = {
610 { "setrlimit", lc_setrlimit }, 641 { "setrlimit", lc_setrlimit },
611 { "getrlimit", lc_getrlimit }, 642 { "getrlimit", lc_getrlimit },
612 643
613 { "uname", lc_uname }, 644 { "uname", lc_uname },
614 645
646 { "setenv", lc_setenv },
647
615 { NULL, NULL } 648 { NULL, NULL }
616 }; 649 };
617 650
618 luaL_register(L, "pposix", exports); 651 luaL_register(L, "pposix", exports);
619 652