diff --git a/posix/Makefile b/posix/Makefile index f9aa506..19c984e 100755 --- a/posix/Makefile +++ b/posix/Makefile @@ -1,5 +1,5 @@ CFLAGS = $(WARNINGS) $(DEFINES) $(INCLUDES) -DEFINES = $(DEBUG) -D_XOPEN_SOURCE=500 -DMISSING_POSIX_SPAWN +DEFINES = $(DEBUG) -D_XOPEN_SOURCE=600 -DMISSING_POSIX_SPAWN -DMISSING_ENVIRON_DECL="extern char **environ" DEBUG= -D'debug(...)=fprintf(stderr,__VA_ARGS__)' # -D_POSIX_SOURCE -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200112L \ -U__STRICT_ANSI__ -D_GNU_SOURCE @@ -12,12 +12,10 @@ LIBS = -L$(LUA)/src -llua5.1 ex-OBJS = ex.o spawn.o -default: ex.dll - -ex.so: $(ex-OBJS); $(CC) -shared -o $@ $(ex-OBJS) +default: ex.so EXTRA = posix_spawn.o -ex.dll: $(ex-OBJS) $(EXTRA); $(CC) -shared -o $@ $(ex-OBJS) $(EXTRA) $(LIBS) +ex.so: $(ex-OBJS) $(EXTRA); $(CC) -shared -o $@ $(ex-OBJS) $(EXTRA) $(LIBS) ex.o: ex.c spawn.h spawn.o: spawn.c spawn.h diff --git a/posix/ex.c b/posix/ex.c index dc9a6d9..9e8e6eb 100755 --- a/posix/ex.c +++ b/posix/ex.c @@ -3,6 +3,7 @@ #include #include #include +#include /* environ */ #include "lua.h" #include "lualib.h" @@ -15,6 +16,8 @@ #include "spawn.h" +MISSING_ENVIRON_DECL; + #define absindex(L,i) ((i)>0?(i):lua_gettop(L)+(i)+1) @@ -384,12 +387,13 @@ static int ex_spawn(lua_State *L) } lua_getfield(L, 2, "env"); /* cmd opts ... envtab */ switch (lua_type(L, -1)) { - default: return luaL_error(L, "bad env option (table expected, got %s)", - luaL_typename(L, -1)); case LUA_TNIL: + break; case LUA_TTABLE: spawn_param_env(params); /* cmd opts ... */ break; + default: + return luaL_error(L, "bad env option (table expected, got %s)", luaL_typename(L, -1)); } get_redirect(L, 2, "stdin", params); /* cmd opts ... */ get_redirect(L, 2, "stdout", params); /* cmd opts ... */ diff --git a/posix/posix_spawn.c b/posix/posix_spawn.c index cc69844..7514df4 100755 --- a/posix/posix_spawn.c +++ b/posix/posix_spawn.c @@ -8,6 +8,12 @@ #include "posix_spawn.h" +#ifndef OPEN_MAX +#define OPEN_MAX sysconf(_SC_OPEN_MAX) +#endif + +MISSING_ENVIRON_DECL; + int posix_spawn_file_actions_init(posix_spawn_file_actions_t *act) { act->dups[0] = act->dups[1] = act->dups[2] = -1; @@ -16,10 +22,12 @@ int posix_spawn_file_actions_init(posix_spawn_file_actions_t *act) int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *act, int d, int n) { + /* good faith effort to determine validity of descriptors */ if (d < 0 || OPEN_MAX < d || n < 0 || OPEN_MAX < n) { errno = EBADF; return -1; } + /* we only support duplication to 0,1,2 */ if (2 < n) { errno = EINVAL; return -1; diff --git a/posix/spawn.c b/posix/spawn.c index 64b885f..fd30bdf 100755 --- a/posix/spawn.c +++ b/posix/spawn.c @@ -10,6 +10,7 @@ #endif #include "spawn.h" +MISSING_ENVIRON_DECL; struct spawn_params { lua_State *L; @@ -66,15 +67,11 @@ void spawn_param_args(struct spawn_params *p) p->argv = argv; } -/* ... envtab/nil */ +/* ... envtab */ void spawn_param_env(struct spawn_params *p) { size_t i = 0; luaL_Buffer estr; - if (lua_isnil(p->L, -1)) { - p->envp = (const char **)environ; - return; - } luaL_buffinit(p->L, &estr); lua_newtable(p->L); /* ... envtab arr */ lua_pushnil(p->L); /* ... envtab arr nil */