correct interpretation of return value from pclose

This commit is contained in:
Roberto Ierusalimschy 2011-02-21 16:12:54 -03:00
parent 3c710f056b
commit 03b769053a
1 changed files with 34 additions and 28 deletions

View File

@ -1,5 +1,5 @@
/*
** $Id: liolib.c,v 2.96 2011/01/26 16:30:02 roberto Exp roberto $
** $Id: liolib.c,v 2.97 2011/02/10 15:35:50 roberto Exp roberto $
** Standard I/O (and system) library
** See Copyright Notice in lua.h
*/
@ -31,40 +31,42 @@
#if defined(LUA_USE_POPEN) /* { */
#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
#define lua_pclose(L,file) ((void)L, pclose(file))
#if defined(LUA_USE_POSIX) /* { */
#include <sys/wait.h>
#define lua_pclose(L,file,stat,tp) \
{(void)L; \
stat = pclose(file); \
if (stat == -1) { /* keep this value */ } \
else if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); tp = "exit"; } \
else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); tp = "signal"; } \
else if (WIFSTOPPED(stat)) { stat = WSTOPSIG(stat); tp = "stop"; } }
#else /* }{ */
#define lua_pclose(L,file,stat,tp) {(void)L; stat = pclose(file);}
/*
** use appropriate macros to interpret 'pclose' return status
*/
#define inspectstat(stat,what) \
if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
#endif /* } */
#elif defined(LUA_WIN) /* }{ */
#define lua_popen(L,c,m) ((void)L, _popen(c,m))
#define lua_pclose(L,file,stat,tp) {(void)L; stat = _pclose(file);}
#define lua_pclose(L,file) ((void)L, _pclose(file))
#else /* }{ */
#define lua_popen(L,c,m) ((void)((void)c, m), \
luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
#define lua_pclose(L,file,stat,tp) {(void)L; (void)file; stat = -1;}
#define lua_pclose(L,file) ((void)((void)L, file), -1)
#endif /* } */
#endif /* } */
#if !defined(inspectstat)
#define inspectstat(stat,what) /* no op */
#endif
#define IO_INPUT 1
@ -159,16 +161,21 @@ static int io_noclose (lua_State *L) {
*/
static int io_pclose (lua_State *L) {
FILE **p = tofilep(L);
int stat;
const char *tp = NULL; /* type of termination (when available) */
lua_pclose(L, *p, stat, tp);
*p = NULL;
int stat = lua_pclose(L, *p);
const char *what = "exit"; /* type of termination */
*p = NULL; /* mark stream as closed (for GC) */
if (stat == -1) /* error? */
return pushresult(L, 0, NULL);
else {
inspectstat(stat, what); /* interpret result from 'pclose' */
if (*what == 'e' && stat == 0) /* successful termination? */
return pushresult(L, 1, NULL);
else { /* return nil,what,code */
lua_pushnil(L);
lua_pushstring(L, what);
lua_pushinteger(L, stat);
lua_pushstring(L, tp);
return 2; /* return status and type */
return 3;
}
}
}
@ -179,7 +186,7 @@ static int io_pclose (lua_State *L) {
static int io_fclose (lua_State *L) {
FILE **p = tofilep(L);
int ok = (fclose(*p) == 0);
*p = NULL;
*p = NULL; /* mark stream as closed (for GC) */
return pushresult(L, ok, NULL);
}
@ -201,8 +208,7 @@ static int io_close (lua_State *L) {
static int io_gc (lua_State *L) {
FILE *f = *tofilep(L);
/* ignore closed files */
if (f != NULL)
if (f != NULL) /* ignore closed files */
aux_close(L);
return 0;
}