aboutsummaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-06-02 10:50:46 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-06-02 10:50:46 -0300
commit9d8426bab445935bb0c08f2ecefb2e208dc38747 (patch)
tree481dc68dec58b93e8216f0b68dcb47953213e974 /lauxlib.c
parent557f0009c42ddc8466af83460a2be0eb2d932249 (diff)
downloadlua-9d8426bab445935bb0c08f2ecefb2e208dc38747.tar.gz
lua-9d8426bab445935bb0c08f2ecefb2e208dc38747.tar.bz2
lua-9d8426bab445935bb0c08f2ecefb2e208dc38747.zip
better error messages for `loadfile'
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/lauxlib.c b/lauxlib.c
index ce16c42f..c7e5f146 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.112 2004/05/10 17:50:51 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.113 2004/05/31 19:27:14 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -481,9 +481,10 @@ static const char *getF (lua_State *L, void *ud, size_t *size) {
481} 481}
482 482
483 483
484static int errfile (lua_State *L, int fnameindex) { 484static int errfile (lua_State *L, const char *what, int fnameindex) {
485 const char *serr = strerror(errno);
485 const char *filename = lua_tostring(L, fnameindex) + 1; 486 const char *filename = lua_tostring(L, fnameindex) + 1;
486 lua_pushfstring(L, "cannot read %s: %s", filename, strerror(errno)); 487 lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
487 lua_remove(L, fnameindex); 488 lua_remove(L, fnameindex);
488 return LUA_ERRFILE; 489 return LUA_ERRFILE;
489} 490}
@@ -502,7 +503,7 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
502 else { 503 else {
503 lua_pushfstring(L, "@%s", filename); 504 lua_pushfstring(L, "@%s", filename);
504 lf.f = fopen(filename, "r"); 505 lf.f = fopen(filename, "r");
505 if (lf.f == NULL) return errfile(L, fnameindex); /* unable to open file */ 506 if (lf.f == NULL) return errfile(L, "open", fnameindex);
506 } 507 }
507 c = getc(lf.f); 508 c = getc(lf.f);
508 if (c == '#') { /* Unix exec. file? */ 509 if (c == '#') { /* Unix exec. file? */
@@ -513,7 +514,7 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
513 if (c == LUA_SIGNATURE[0] && lf.f != stdin) { /* binary file? */ 514 if (c == LUA_SIGNATURE[0] && lf.f != stdin) { /* binary file? */
514 fclose(lf.f); 515 fclose(lf.f);
515 lf.f = fopen(filename, "rb"); /* reopen in binary mode */ 516 lf.f = fopen(filename, "rb"); /* reopen in binary mode */
516 if (lf.f == NULL) return errfile(L, fnameindex); /* unable to reopen file */ 517 if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
517 /* skip eventual `#!...' */ 518 /* skip eventual `#!...' */
518 while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; 519 while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
519 lf.extraline = 0; 520 lf.extraline = 0;
@@ -524,7 +525,7 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
524 if (lf.f != stdin) fclose(lf.f); /* close file (even in case of errors) */ 525 if (lf.f != stdin) fclose(lf.f); /* close file (even in case of errors) */
525 if (readstatus) { 526 if (readstatus) {
526 lua_settop(L, fnameindex); /* ignore results from `lua_load' */ 527 lua_settop(L, fnameindex); /* ignore results from `lua_load' */
527 return errfile(L, fnameindex); 528 return errfile(L, "read", fnameindex);
528 } 529 }
529 lua_remove(L, fnameindex); 530 lua_remove(L, fnameindex);
530 return status; 531 return status;