summaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-11-18 14:53:19 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-11-18 14:53:19 -0200
commit96ba5d0bc217e9b74f0f7656c2f1bb5f6b968778 (patch)
treef223c7b494518a3adbb0edc6f6dc03a0452c5397 /liolib.c
parentf0185f762840cdf80cc81b0c3e67e306e015f1f3 (diff)
downloadlua-96ba5d0bc217e9b74f0f7656c2f1bb5f6b968778.tar.gz
lua-96ba5d0bc217e9b74f0f7656c2f1bb5f6b968778.tar.bz2
lua-96ba5d0bc217e9b74f0f7656c2f1bb5f6b968778.zip
include file name in error messages
Diffstat (limited to '')
-rw-r--r--liolib.c38
1 files changed, 23 insertions, 15 deletions
diff --git a/liolib.c b/liolib.c
index c10c5c31..0ee9f94f 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 2.22 2002/10/21 20:41:24 roberto Exp roberto $ 2** $Id: liolib.c,v 2.23 2002/11/14 15:41:38 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*/
@@ -37,14 +37,17 @@
37#define IO_OUTPUT "_output" 37#define IO_OUTPUT "_output"
38 38
39 39
40static int pushresult (lua_State *L, int i) { 40static int pushresult (lua_State *L, int i, const char *filename) {
41 if (i) { 41 if (i) {
42 lua_pushboolean(L, 1); 42 lua_pushboolean(L, 1);
43 return 1; 43 return 1;
44 } 44 }
45 else { 45 else {
46 lua_pushnil(L); 46 lua_pushnil(L);
47 lua_pushstring(L, strerror(errno)); 47 if (filename)
48 lua_pushfstring(L, "%s: %s", filename, strerror(errno));
49 else
50 lua_pushfstring(L, "%s", strerror(errno));
48 lua_pushnumber(L, errno); 51 lua_pushnumber(L, errno);
49 return 3; 52 return 3;
50 } 53 }
@@ -118,7 +121,7 @@ static int io_close (lua_State *L) {
118 lua_pushstring(L, IO_OUTPUT); 121 lua_pushstring(L, IO_OUTPUT);
119 lua_rawget(L, lua_upvalueindex(1)); 122 lua_rawget(L, lua_upvalueindex(1));
120 } 123 }
121 return pushresult(L, aux_close(L)); 124 return pushresult(L, aux_close(L), NULL);
122} 125}
123 126
124 127
@@ -135,7 +138,7 @@ static int io_open (lua_State *L) {
135 const char *mode = luaL_optstring(L, 2, "r"); 138 const char *mode = luaL_optstring(L, 2, "r");
136 FILE **pf = newfile(L); 139 FILE **pf = newfile(L);
137 *pf = fopen(filename, mode); 140 *pf = fopen(filename, mode);
138 return (*pf == NULL) ? pushresult(L, 0) : 1; 141 return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
139} 142}
140 143
141 144
@@ -148,7 +151,7 @@ static int io_popen (lua_State *L) {
148 const char *mode = luaL_optstring(L, 2, "r"); 151 const char *mode = luaL_optstring(L, 2, "r");
149 FILE **pf = newfile(L); 152 FILE **pf = newfile(L);
150 *pf = popen(filename, mode); 153 *pf = popen(filename, mode);
151 return (*pf == NULL) ? pushresult(L, 0) : 1; 154 return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
152#endif 155#endif
153} 156}
154 157
@@ -156,7 +159,7 @@ static int io_popen (lua_State *L) {
156static int io_tmpfile (lua_State *L) { 159static int io_tmpfile (lua_State *L) {
157 FILE **pf = newfile(L); 160 FILE **pf = newfile(L);
158 *pf = tmpfile(); 161 *pf = tmpfile();
159 return (*pf == NULL) ? pushresult(L, 0) : 1; 162 return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
160} 163}
161 164
162 165
@@ -183,7 +186,10 @@ static int g_iofile (lua_State *L, const char *name, const char *mode) {
183 if (filename) { 186 if (filename) {
184 FILE **pf = newfile(L); 187 FILE **pf = newfile(L);
185 *pf = fopen(filename, mode); 188 *pf = fopen(filename, mode);
186 luaL_argcheck(L, *pf, 1, strerror(errno)); 189 if (*pf == NULL) {
190 lua_pushfstring(L, "%s: %s", filename, strerror(errno));
191 luaL_argerror(L, 1, lua_tostring(L, -1));
192 }
187 } 193 }
188 else { 194 else {
189 tofile(L, 1); /* check that it's a valid file handle */ 195 tofile(L, 1); /* check that it's a valid file handle */
@@ -396,7 +402,7 @@ static int g_write (lua_State *L, FILE *f, int arg) {
396 status = status && (fwrite(s, sizeof(char), l, f) == l); 402 status = status && (fwrite(s, sizeof(char), l, f) == l);
397 } 403 }
398 } 404 }
399 pushresult(L, status); 405 pushresult(L, status, NULL);
400 return 1; 406 return 1;
401} 407}
402 408
@@ -420,7 +426,7 @@ static int f_seek (lua_State *L) {
420 luaL_argcheck(L, op != -1, 2, "invalid mode"); 426 luaL_argcheck(L, op != -1, 2, "invalid mode");
421 op = fseek(f, offset, mode[op]); 427 op = fseek(f, offset, mode[op]);
422 if (op) 428 if (op)
423 return pushresult(L, 0); /* error */ 429 return pushresult(L, 0, NULL); /* error */
424 else { 430 else {
425 lua_pushnumber(L, ftell(f)); 431 lua_pushnumber(L, ftell(f));
426 return 1; 432 return 1;
@@ -429,12 +435,12 @@ static int f_seek (lua_State *L) {
429 435
430 436
431static int io_flush (lua_State *L) { 437static int io_flush (lua_State *L) {
432 return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0); 438 return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
433} 439}
434 440
435 441
436static int f_flush (lua_State *L) { 442static int f_flush (lua_State *L) {
437 return pushresult(L, fflush(tofile(L, 1)) == 0); 443 return pushresult(L, fflush(tofile(L, 1)) == 0, NULL);
438} 444}
439 445
440 446
@@ -497,13 +503,15 @@ static int io_execute (lua_State *L) {
497 503
498 504
499static int io_remove (lua_State *L) { 505static int io_remove (lua_State *L) {
500 return pushresult(L, remove(luaL_checkstring(L, 1)) == 0); 506 const char *filename = luaL_checkstring(L, 1);
507 return pushresult(L, remove(filename) == 0, filename);
501} 508}
502 509
503 510
504static int io_rename (lua_State *L) { 511static int io_rename (lua_State *L) {
505 return pushresult(L, rename(luaL_checkstring(L, 1), 512 const char *fromname = luaL_checkstring(L, 1);
506 luaL_checkstring(L, 2)) == 0); 513 const char *toname = luaL_checkstring(L, 2);
514 return pushresult(L, rename(fromname, toname) == 0, fromname);
507} 515}
508 516
509 517