diff --git a/posix/ex.c b/posix/ex.c index f82b2cd..d0da174 100755 --- a/posix/ex.c +++ b/posix/ex.c @@ -15,7 +15,7 @@ /* Generally useful function -- what luaL_checkudata() should do */ -void *checkuserdata(lua_State *L, int idx, const char *tname) +extern void *checkuserdata(lua_State *L, int idx, const char *tname) { void *ud; luaL_argcheck(L, ud = luaL_checkudata(L, idx, tname), idx, tname); @@ -24,7 +24,7 @@ void *checkuserdata(lua_State *L, int idx, const char *tname) /* -- nil error */ -int push_error(lua_State *L) +extern int push_error(lua_State *L) { lua_pushnil(L); lua_pushstring(L, strerror(errno)); @@ -163,10 +163,22 @@ static int ex_unlock(lua_State *L) } -/* -- LUA_FILEHANDLE file file */ -static int make_pipe(lua_State *L, FILE *i, FILE *o) +static int make_pipe(FILE **i, FILE **o) { - FILE **pf; + int fd[2]; + if (-1 == pipe(fd)) + return 0; + *i = fdopen(fd[0], "r"); + *o = fdopen(fd[1], "w"); + return 1; +} + +/* -- in out/nil error */ +static int ex_pipe(lua_State *L) +{ + FILE *i, *o, **pf; + if (!make_pipe(&i, &o)) + return push_error(L); luaL_getmetatable(L, LUA_FILEHANDLE); *(pf = lua_newuserdata(L, sizeof *pf)) = i; lua_pushvalue(L, -2); @@ -177,15 +189,6 @@ static int make_pipe(lua_State *L, FILE *i, FILE *o) return 2; } -/* -- in out/nil error */ -static int ex_pipe(lua_State *L) -{ - int fd[2]; - if (-1 == pipe(fd)) - return push_error(L); - return make_pipe(L, fdopen(fd[0], "r"), fdopen(fd[1], "w")); -} - /* filename [args-opts] -- true/nil,error */ /* args-opts -- true/nil,error */ diff --git a/w32api/ex.c b/w32api/ex.c index aa6cdc7..df6baa8 100755 --- a/w32api/ex.c +++ b/w32api/ex.c @@ -31,7 +31,7 @@ extern HANDLE get_handle(FILE *f) /* -- nil error */ -static int push_error(lua_State *L) +extern int push_error(lua_State *L) { DWORD error = GetLastError(); char buffer[1024]; @@ -56,12 +56,8 @@ static int ex_getenv(lua_State *L) char val[1024]; size_t len; len = GetEnvironmentVariable(nam, val, sizeof val); - if (sizeof val < len) + if (sizeof val < len || (len == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND)) return push_error(L); - else if (len == 0) { - lua_pushnil(L); - return 1; - } lua_pushlstring(L, val, len); return 1; } @@ -194,9 +190,22 @@ static int ex_unlock(lua_State *L) /* -- LUA_FILEHANDLE file file */ -static int make_pipe(lua_State *L, FILE *i, FILE *o) +static int make_pipe(FILE **i, FILE **o) +{ + HANDLE ph[2]; + if (0 == CreatePipe(ph+0, ph+1, 0, 0)) + return 0; + *i = _fdopen(_open_osfhandle((long)ph[0], _O_RDONLY), "r"); + *o = _fdopen(_open_osfhandle((long)ph[1], _O_WRONLY), "w"); + return 1; +} + +/* -- in out/nil error */ +static int ex_pipe(lua_State *L) { - FILE **pf; + FILE *i, *o, **pf; + if (!make_pipe(&i, &o)) + return push_error(L); luaL_getmetatable(L, LUA_FILEHANDLE); *(pf = lua_newuserdata(L, sizeof *pf)) = i; lua_pushvalue(L, -2); @@ -207,17 +216,6 @@ static int make_pipe(lua_State *L, FILE *i, FILE *o) return 2; } -/* -- in out/nil error */ -static int ex_pipe(lua_State *L) -{ - HANDLE ph[2]; - if (0 == CreatePipe(ph+0, ph+1, 0, 0)) - return push_error(L); - return make_pipe(L, - _fdopen(_open_osfhandle((long)ph[0], _O_RDONLY), "r"), - _fdopen(_open_osfhandle((long)ph[1], _O_WRONLY), "w")); -} - /* filename [args-opts] -- true/nil,error */ /* args-opts -- true/nil,error */ diff --git a/w32api/spawn.c b/w32api/spawn.c index 926a4ce..9d3c96d 100755 --- a/w32api/spawn.c +++ b/w32api/spawn.c @@ -10,6 +10,7 @@ extern void *checkuserdata(lua_State *L, int index, const char *name); extern HANDLE get_handle(FILE *f); +extern int push_error(lua_State *L); static int needs_quoting(const char *s) { @@ -144,8 +145,9 @@ int process_wait(lua_State *L) struct process *p = checkuserdata(L, 1, PROCESS_HANDLE); if (p->status == -1) { DWORD exitcode; - WaitForSingleObject(p->hProcess, INFINITE); - GetExitCodeProcess(p->hProcess, &exitcode); + if (WAIT_FAILED == WaitForSingleObject(p->hProcess, INFINITE) + || !GetExitCodeProcess(p->hProcess, &exitcode)) + return push_error(L); p->status = exitcode; } lua_pushnumber(L, p->status);