aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-03-03 13:34:46 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-03-03 13:34:46 -0300
commitd806710ab5622487159f2a8aecf72003d831542b (patch)
tree0a2c49e7ee732f17002ed086420f51ca7f42b221 /liolib.c
parente049abb69a45e06ca5769a3956a0ba36a959d29e (diff)
downloadlua-d806710ab5622487159f2a8aecf72003d831542b.tar.gz
lua-d806710ab5622487159f2a8aecf72003d831542b.tar.bz2
lua-d806710ab5622487159f2a8aecf72003d831542b.zip
returns for file-related functions and process-related functions
unified in 'auxlib'
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c73
1 files changed, 12 insertions, 61 deletions
diff --git a/liolib.c b/liolib.c
index 85a4e0b2..a688ad93 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 2.97 2011/02/10 15:35:50 roberto Exp roberto $ 2** $Id: liolib.c,v 2.98 2011/02/21 19:12:54 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*/
@@ -33,20 +33,6 @@
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)) 34#define lua_pclose(L,file) ((void)L, pclose(file))
35 35
36
37#if defined(LUA_USE_POSIX) /* { */
38
39#include <sys/wait.h>
40
41/*
42** use appropriate macros to interpret 'pclose' return status
43*/
44#define inspectstat(stat,what) \
45 if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
46 else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
47
48#endif /* } */
49
50#elif defined(LUA_WIN) /* }{ */ 36#elif defined(LUA_WIN) /* }{ */
51 37
52#define lua_popen(L,c,m) ((void)L, _popen(c,m)) 38#define lua_popen(L,c,m) ((void)L, _popen(c,m))
@@ -64,10 +50,6 @@
64 50
65#endif /* } */ 51#endif /* } */
66 52
67#if !defined(inspectstat)
68#define inspectstat(stat,what) /* no op */
69#endif
70
71 53
72#define IO_INPUT 1 54#define IO_INPUT 1
73#define IO_OUTPUT 2 55#define IO_OUTPUT 2
@@ -76,24 +58,6 @@
76static const char *const fnames[] = {"input", "output"}; 58static const char *const fnames[] = {"input", "output"};
77 59
78 60
79static int pushresult (lua_State *L, int i, const char *filename) {
80 int en = errno; /* calls to Lua API may change this value */
81 if (i) {
82 lua_pushboolean(L, 1);
83 return 1;
84 }
85 else {
86 lua_pushnil(L);
87 if (filename)
88 lua_pushfstring(L, "%s: %s", filename, strerror(en));
89 else
90 lua_pushfstring(L, "%s", strerror(en));
91 lua_pushinteger(L, en);
92 return 3;
93 }
94}
95
96
97static void fileerror (lua_State *L, int arg, const char *filename) { 61static void fileerror (lua_State *L, int arg, const char *filename) {
98 lua_pushfstring(L, "%s: %s", filename, strerror(errno)); 62 lua_pushfstring(L, "%s: %s", filename, strerror(errno));
99 luaL_argerror(L, arg, lua_tostring(L, -1)); 63 luaL_argerror(L, arg, lua_tostring(L, -1));
@@ -162,21 +126,8 @@ static int io_noclose (lua_State *L) {
162static int io_pclose (lua_State *L) { 126static int io_pclose (lua_State *L) {
163 FILE **p = tofilep(L); 127 FILE **p = tofilep(L);
164 int stat = lua_pclose(L, *p); 128 int stat = lua_pclose(L, *p);
165 const char *what = "exit"; /* type of termination */
166 *p = NULL; /* mark stream as closed (for GC) */ 129 *p = NULL; /* mark stream as closed (for GC) */
167 if (stat == -1) /* error? */ 130 return luaL_execresult(L, stat);
168 return pushresult(L, 0, NULL);
169 else {
170 inspectstat(stat, what); /* interpret result from 'pclose' */
171 if (*what == 'e' && stat == 0) /* successful termination? */
172 return pushresult(L, 1, NULL);
173 else { /* return nil,what,code */
174 lua_pushnil(L);
175 lua_pushstring(L, what);
176 lua_pushinteger(L, stat);
177 return 3;
178 }
179 }
180} 131}
181 132
182 133
@@ -187,7 +138,7 @@ static int io_fclose (lua_State *L) {
187 FILE **p = tofilep(L); 138 FILE **p = tofilep(L);
188 int ok = (fclose(*p) == 0); 139 int ok = (fclose(*p) == 0);
189 *p = NULL; /* mark stream as closed (for GC) */ 140 *p = NULL; /* mark stream as closed (for GC) */
190 return pushresult(L, ok, NULL); 141 return luaL_fileresult(L, ok, NULL);
191} 142}
192 143
193 144
@@ -238,7 +189,7 @@ static int io_open (lua_State *L) {
238 " (should match " LUA_QL("[rwa]%%+?b?") ")", mode); 189 " (should match " LUA_QL("[rwa]%%+?b?") ")", mode);
239 pf = newfile(L); 190 pf = newfile(L);
240 *pf = fopen(filename, mode); 191 *pf = fopen(filename, mode);
241 return (*pf == NULL) ? pushresult(L, 0, filename) : 1; 192 return (*pf == NULL) ? luaL_fileresult(L, 0, filename) : 1;
242} 193}
243 194
244 195
@@ -251,14 +202,14 @@ static int io_popen (lua_State *L) {
251 const char *mode = luaL_optstring(L, 2, "r"); 202 const char *mode = luaL_optstring(L, 2, "r");
252 FILE **pf = newfile(L); 203 FILE **pf = newfile(L);
253 *pf = lua_popen(L, filename, mode); 204 *pf = lua_popen(L, filename, mode);
254 return (*pf == NULL) ? pushresult(L, 0, filename) : 1; 205 return (*pf == NULL) ? luaL_fileresult(L, 0, filename) : 1;
255} 206}
256 207
257 208
258static int io_tmpfile (lua_State *L) { 209static int io_tmpfile (lua_State *L) {
259 FILE **pf = newfile(L); 210 FILE **pf = newfile(L);
260 *pf = tmpfile(); 211 *pf = tmpfile();
261 return (*pf == NULL) ? pushresult(L, 0, NULL) : 1; 212 return (*pf == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
262} 213}
263 214
264 215
@@ -469,7 +420,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
469 } 420 }
470 } 421 }
471 if (ferror(f)) 422 if (ferror(f))
472 return pushresult(L, 0, NULL); 423 return luaL_fileresult(L, 0, NULL);
473 if (!success) { 424 if (!success) {
474 lua_pop(L, 1); /* remove last result */ 425 lua_pop(L, 1); /* remove last result */
475 lua_pushnil(L); /* push nil instead */ 426 lua_pushnil(L); /* push nil instead */
@@ -533,7 +484,7 @@ static int g_write (lua_State *L, FILE *f, int arg) {
533 } 484 }
534 } 485 }
535 if (status) return 1; /* file handle already on stack top */ 486 if (status) return 1; /* file handle already on stack top */
536 else return pushresult(L, status, NULL); 487 else return luaL_fileresult(L, status, NULL);
537} 488}
538 489
539 490
@@ -557,7 +508,7 @@ static int f_seek (lua_State *L) {
557 long offset = luaL_optlong(L, 3, 0); 508 long offset = luaL_optlong(L, 3, 0);
558 op = fseek(f, offset, mode[op]); 509 op = fseek(f, offset, mode[op]);
559 if (op) 510 if (op)
560 return pushresult(L, 0, NULL); /* error */ 511 return luaL_fileresult(L, 0, NULL); /* error */
561 else { 512 else {
562 lua_pushinteger(L, ftell(f)); 513 lua_pushinteger(L, ftell(f));
563 return 1; 514 return 1;
@@ -572,18 +523,18 @@ static int f_setvbuf (lua_State *L) {
572 int op = luaL_checkoption(L, 2, NULL, modenames); 523 int op = luaL_checkoption(L, 2, NULL, modenames);
573 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); 524 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
574 int res = setvbuf(f, NULL, mode[op], sz); 525 int res = setvbuf(f, NULL, mode[op], sz);
575 return pushresult(L, res == 0, NULL); 526 return luaL_fileresult(L, res == 0, NULL);
576} 527}
577 528
578 529
579 530
580static int io_flush (lua_State *L) { 531static int io_flush (lua_State *L) {
581 return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); 532 return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
582} 533}
583 534
584 535
585static int f_flush (lua_State *L) { 536static int f_flush (lua_State *L) {
586 return pushresult(L, fflush(tofile(L)) == 0, NULL); 537 return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
587} 538}
588 539
589 540