portability concerns

master
mark 20 years ago
parent 2843236d3f
commit 448e046364

@ -1,5 +1,5 @@
CFLAGS = $(WARNINGS) $(DEFINES) $(INCLUDES) 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__)' DEBUG= -D'debug(...)=fprintf(stderr,__VA_ARGS__)'
# -D_POSIX_SOURCE -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200112L \ # -D_POSIX_SOURCE -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200112L \
-U__STRICT_ANSI__ -D_GNU_SOURCE -U__STRICT_ANSI__ -D_GNU_SOURCE
@ -12,12 +12,10 @@ LIBS = -L$(LUA)/src -llua5.1
ex-OBJS = ex.o spawn.o ex-OBJS = ex.o spawn.o
default: ex.dll default: ex.so
ex.so: $(ex-OBJS); $(CC) -shared -o $@ $(ex-OBJS)
EXTRA = posix_spawn.o 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 ex.o: ex.c spawn.h
spawn.o: spawn.c spawn.h spawn.o: spawn.c spawn.h

@ -3,6 +3,7 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <unistd.h> /* environ */
#include "lua.h" #include "lua.h"
#include "lualib.h" #include "lualib.h"
@ -15,6 +16,8 @@
#include "spawn.h" #include "spawn.h"
MISSING_ENVIRON_DECL;
#define absindex(L,i) ((i)>0?(i):lua_gettop(L)+(i)+1) #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 */ lua_getfield(L, 2, "env"); /* cmd opts ... envtab */
switch (lua_type(L, -1)) { switch (lua_type(L, -1)) {
default: return luaL_error(L, "bad env option (table expected, got %s)",
luaL_typename(L, -1));
case LUA_TNIL: case LUA_TNIL:
break;
case LUA_TTABLE: case LUA_TTABLE:
spawn_param_env(params); /* cmd opts ... */ spawn_param_env(params); /* cmd opts ... */
break; 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, "stdin", params); /* cmd opts ... */
get_redirect(L, 2, "stdout", params); /* cmd opts ... */ get_redirect(L, 2, "stdout", params); /* cmd opts ... */

@ -8,6 +8,12 @@
#include "posix_spawn.h" #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) int posix_spawn_file_actions_init(posix_spawn_file_actions_t *act)
{ {
act->dups[0] = act->dups[1] = act->dups[2] = -1; 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) 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) { if (d < 0 || OPEN_MAX < d || n < 0 || OPEN_MAX < n) {
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
/* we only support duplication to 0,1,2 */
if (2 < n) { if (2 < n) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;

@ -10,6 +10,7 @@
#endif #endif
#include "spawn.h" #include "spawn.h"
MISSING_ENVIRON_DECL;
struct spawn_params { struct spawn_params {
lua_State *L; lua_State *L;
@ -66,15 +67,11 @@ void spawn_param_args(struct spawn_params *p)
p->argv = argv; p->argv = argv;
} }
/* ... envtab/nil */ /* ... envtab */
void spawn_param_env(struct spawn_params *p) void spawn_param_env(struct spawn_params *p)
{ {
size_t i = 0; size_t i = 0;
luaL_Buffer estr; luaL_Buffer estr;
if (lua_isnil(p->L, -1)) {
p->envp = (const char **)environ;
return;
}
luaL_buffinit(p->L, &estr); luaL_buffinit(p->L, &estr);
lua_newtable(p->L); /* ... envtab arr */ lua_newtable(p->L); /* ... envtab arr */
lua_pushnil(p->L); /* ... envtab arr nil */ lua_pushnil(p->L); /* ... envtab arr nil */

Loading…
Cancel
Save