aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-02-10 13:35:50 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-02-10 13:35:50 -0200
commit7b968bb51404d73cd0b4d75ad092f3cab299fac9 (patch)
tree281fcf874ee2d3a5a00bc56c88a4568783093cf0 /liolib.c
parentbf8b08295aa2f4b66aeb7d8313d464ccd80c0b0c (diff)
downloadlua-7b968bb51404d73cd0b4d75ad092f3cab299fac9.tar.gz
lua-7b968bb51404d73cd0b4d75ad092f3cab299fac9.tar.bz2
lua-7b968bb51404d73cd0b4d75ad092f3cab299fac9.zip
p-close returns "correct" status plus type of termination
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c52
1 files changed, 36 insertions, 16 deletions
diff --git a/liolib.c b/liolib.c
index 1fa05477..0c1151be 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 2.95 2010/11/10 18:05:36 roberto Exp roberto $ 2** $Id: liolib.c,v 2.96 2011/01/26 16:30:02 roberto Exp roberto $
3** Standard I/O (and system) library 3** Standard I/O (and system) library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -26,27 +26,44 @@
26** lua_popen spawns a new process connected to the current one through 26** lua_popen spawns a new process connected to the current one through
27** the file streams. 27** the file streams.
28*/ 28*/
29#if !defined(lua_popen) 29#if !defined(lua_popen) /* { */
30 30
31#if defined(LUA_USE_POPEN) 31#if defined(LUA_USE_POPEN) /* { */
32 32
33#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m)) 33#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
34#define lua_pclose(L,file) ((void)L, pclose(file))
35 34
36#elif defined(LUA_WIN) 35#if defined(LUA_USE_POSIX) /* { */
37 36
38#define lua_popen(L,c,m) ((void)L, _popen(c,m)) 37#include <sys/wait.h>
39#define lua_pclose(L,file) ((void)L, _pclose(file))
40 38
41#else 39#define lua_pclose(L,file,stat,tp) \
40 {(void)L; \
41 stat = pclose(file); \
42 if (stat == -1) { /* keep this value */ } \
43 else if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); tp = "exit"; } \
44 else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); tp = "signal"; } \
45 else if (WIFSTOPPED(stat)) { stat = WSTOPSIG(stat); tp = "stop"; } }
42 46
43#define lua_popen(L,c,m) ((void)((void)c, m), \ 47#else /* }{ */
44 luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
45#define lua_pclose(L,file) ((void)((void)L, file), -1)
46 48
47#endif 49#define lua_pclose(L,file,stat,tp) {(void)L; stat = pclose(file);}
48 50
49#endif 51#endif /* } */
52
53#elif defined(LUA_WIN) /* }{ */
54
55#define lua_popen(L,c,m) ((void)L, _popen(c,m))
56#define lua_pclose(L,file,stat,tp) {(void)L; stat = _pclose(file);}
57
58#else /* }{ */
59
60#define lua_popen(L,c,m) ((void)((void)c, m), \
61 luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
62#define lua_pclose(L,file,stat,tp) {(void)L; (void)file; stat = -1;}
63
64#endif /* } */
65
66#endif /* } */
50 67
51 68
52 69
@@ -142,13 +159,16 @@ static int io_noclose (lua_State *L) {
142*/ 159*/
143static int io_pclose (lua_State *L) { 160static int io_pclose (lua_State *L) {
144 FILE **p = tofilep(L); 161 FILE **p = tofilep(L);
145 int stat = lua_pclose(L, *p); 162 int stat;
163 const char *tp = NULL; /* type of termination (when available) */
164 lua_pclose(L, *p, stat, tp);
146 *p = NULL; 165 *p = NULL;
147 if (stat == -1) /* error? */ 166 if (stat == -1) /* error? */
148 return pushresult(L, 0, NULL); 167 return pushresult(L, 0, NULL);
149 else { 168 else {
150 lua_pushinteger(L, stat); 169 lua_pushinteger(L, stat);
151 return 1; /* return status */ 170 lua_pushstring(L, tp);
171 return 2; /* return status and type */
152 } 172 }
153} 173}
154 174