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