diff options
| -rw-r--r-- | lauxlib.c | 16 | ||||
| -rw-r--r-- | liolib.c | 25 | ||||
| -rw-r--r-- | loslib.c | 2 |
3 files changed, 33 insertions, 10 deletions
| @@ -249,11 +249,13 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { | |||
| 249 | return 1; | 249 | return 1; |
| 250 | } | 250 | } |
| 251 | else { | 251 | else { |
| 252 | const char *msg; | ||
| 252 | luaL_pushfail(L); | 253 | luaL_pushfail(L); |
| 254 | msg = (en != 0) ? strerror(en) : "(no extra info)"; | ||
| 253 | if (fname) | 255 | if (fname) |
| 254 | lua_pushfstring(L, "%s: %s", fname, strerror(en)); | 256 | lua_pushfstring(L, "%s: %s", fname, msg); |
| 255 | else | 257 | else |
| 256 | lua_pushstring(L, strerror(en)); | 258 | lua_pushstring(L, msg); |
| 257 | lua_pushinteger(L, en); | 259 | lua_pushinteger(L, en); |
| 258 | return 3; | 260 | return 3; |
| 259 | } | 261 | } |
| @@ -750,9 +752,12 @@ static const char *getF (lua_State *L, void *ud, size_t *size) { | |||
| 750 | 752 | ||
| 751 | 753 | ||
| 752 | static int errfile (lua_State *L, const char *what, int fnameindex) { | 754 | static int errfile (lua_State *L, const char *what, int fnameindex) { |
| 753 | const char *serr = strerror(errno); | 755 | int err = errno; |
| 754 | const char *filename = lua_tostring(L, fnameindex) + 1; | 756 | const char *filename = lua_tostring(L, fnameindex) + 1; |
| 755 | lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); | 757 | if (err != 0) |
| 758 | lua_pushfstring(L, "cannot %s %s: %s", what, filename, strerror(err)); | ||
| 759 | else | ||
| 760 | lua_pushfstring(L, "cannot %s %s", what, filename); | ||
| 756 | lua_remove(L, fnameindex); | 761 | lua_remove(L, fnameindex); |
| 757 | return LUA_ERRFILE; | 762 | return LUA_ERRFILE; |
| 758 | } | 763 | } |
| @@ -805,6 +810,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, | |||
| 805 | } | 810 | } |
| 806 | else { | 811 | else { |
| 807 | lua_pushfstring(L, "@%s", filename); | 812 | lua_pushfstring(L, "@%s", filename); |
| 813 | errno = 0; | ||
| 808 | lf.f = fopen(filename, "r"); | 814 | lf.f = fopen(filename, "r"); |
| 809 | if (lf.f == NULL) return errfile(L, "open", fnameindex); | 815 | if (lf.f == NULL) return errfile(L, "open", fnameindex); |
| 810 | } | 816 | } |
| @@ -814,6 +820,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, | |||
| 814 | if (c == LUA_SIGNATURE[0]) { /* binary file? */ | 820 | if (c == LUA_SIGNATURE[0]) { /* binary file? */ |
| 815 | lf.n = 0; /* remove possible newline */ | 821 | lf.n = 0; /* remove possible newline */ |
| 816 | if (filename) { /* "real" file? */ | 822 | if (filename) { /* "real" file? */ |
| 823 | errno = 0; | ||
| 817 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ | 824 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ |
| 818 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); | 825 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); |
| 819 | skipcomment(lf.f, &c); /* re-read initial portion */ | 826 | skipcomment(lf.f, &c); /* re-read initial portion */ |
| @@ -823,6 +830,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, | |||
| 823 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ | 830 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ |
| 824 | status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); | 831 | status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); |
| 825 | readstatus = ferror(lf.f); | 832 | readstatus = ferror(lf.f); |
| 833 | errno = 0; /* no useful error number until here */ | ||
| 826 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ | 834 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ |
| 827 | if (readstatus) { | 835 | if (readstatus) { |
| 828 | lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ | 836 | lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ |
| @@ -245,8 +245,8 @@ static int f_gc (lua_State *L) { | |||
| 245 | */ | 245 | */ |
| 246 | static int io_fclose (lua_State *L) { | 246 | static int io_fclose (lua_State *L) { |
| 247 | LStream *p = tolstream(L); | 247 | LStream *p = tolstream(L); |
| 248 | int res = fclose(p->f); | 248 | errno = 0; |
| 249 | return luaL_fileresult(L, (res == 0), NULL); | 249 | return luaL_fileresult(L, (fclose(p->f) == 0), NULL); |
| 250 | } | 250 | } |
| 251 | 251 | ||
| 252 | 252 | ||
| @@ -272,6 +272,7 @@ static int io_open (lua_State *L) { | |||
| 272 | LStream *p = newfile(L); | 272 | LStream *p = newfile(L); |
| 273 | const char *md = mode; /* to traverse/check mode */ | 273 | const char *md = mode; /* to traverse/check mode */ |
| 274 | luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); | 274 | luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); |
| 275 | errno = 0; | ||
| 275 | p->f = fopen(filename, mode); | 276 | p->f = fopen(filename, mode); |
| 276 | return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; | 277 | return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; |
| 277 | } | 278 | } |
| @@ -292,6 +293,7 @@ static int io_popen (lua_State *L) { | |||
| 292 | const char *mode = luaL_optstring(L, 2, "r"); | 293 | const char *mode = luaL_optstring(L, 2, "r"); |
| 293 | LStream *p = newprefile(L); | 294 | LStream *p = newprefile(L); |
| 294 | luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode"); | 295 | luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode"); |
| 296 | errno = 0; | ||
| 295 | p->f = l_popen(L, filename, mode); | 297 | p->f = l_popen(L, filename, mode); |
| 296 | p->closef = &io_pclose; | 298 | p->closef = &io_pclose; |
| 297 | return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; | 299 | return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; |
| @@ -300,6 +302,7 @@ static int io_popen (lua_State *L) { | |||
| 300 | 302 | ||
| 301 | static int io_tmpfile (lua_State *L) { | 303 | static int io_tmpfile (lua_State *L) { |
| 302 | LStream *p = newfile(L); | 304 | LStream *p = newfile(L); |
| 305 | errno = 0; | ||
| 303 | p->f = tmpfile(); | 306 | p->f = tmpfile(); |
| 304 | return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; | 307 | return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; |
| 305 | } | 308 | } |
| @@ -567,6 +570,7 @@ static int g_read (lua_State *L, FILE *f, int first) { | |||
| 567 | int nargs = lua_gettop(L) - 1; | 570 | int nargs = lua_gettop(L) - 1; |
| 568 | int n, success; | 571 | int n, success; |
| 569 | clearerr(f); | 572 | clearerr(f); |
| 573 | errno = 0; | ||
| 570 | if (nargs == 0) { /* no arguments? */ | 574 | if (nargs == 0) { /* no arguments? */ |
| 571 | success = read_line(L, f, 1); | 575 | success = read_line(L, f, 1); |
| 572 | n = first + 1; /* to return 1 result */ | 576 | n = first + 1; /* to return 1 result */ |
| @@ -660,6 +664,7 @@ static int io_readline (lua_State *L) { | |||
| 660 | static int g_write (lua_State *L, FILE *f, int arg) { | 664 | static int g_write (lua_State *L, FILE *f, int arg) { |
| 661 | int nargs = lua_gettop(L) - arg; | 665 | int nargs = lua_gettop(L) - arg; |
| 662 | int status = 1; | 666 | int status = 1; |
| 667 | errno = 0; | ||
| 663 | for (; nargs--; arg++) { | 668 | for (; nargs--; arg++) { |
| 664 | if (lua_type(L, arg) == LUA_TNUMBER) { | 669 | if (lua_type(L, arg) == LUA_TNUMBER) { |
| 665 | /* optimization: could be done exactly as for strings */ | 670 | /* optimization: could be done exactly as for strings */ |
| @@ -678,7 +683,8 @@ static int g_write (lua_State *L, FILE *f, int arg) { | |||
| 678 | } | 683 | } |
| 679 | if (l_likely(status)) | 684 | if (l_likely(status)) |
| 680 | return 1; /* file handle already on stack top */ | 685 | return 1; /* file handle already on stack top */ |
| 681 | else return luaL_fileresult(L, status, NULL); | 686 | else |
| 687 | return luaL_fileresult(L, status, NULL); | ||
| 682 | } | 688 | } |
| 683 | 689 | ||
| 684 | 690 | ||
| @@ -703,6 +709,7 @@ static int f_seek (lua_State *L) { | |||
| 703 | l_seeknum offset = (l_seeknum)p3; | 709 | l_seeknum offset = (l_seeknum)p3; |
| 704 | luaL_argcheck(L, (lua_Integer)offset == p3, 3, | 710 | luaL_argcheck(L, (lua_Integer)offset == p3, 3, |
| 705 | "not an integer in proper range"); | 711 | "not an integer in proper range"); |
| 712 | errno = 0; | ||
| 706 | op = l_fseek(f, offset, mode[op]); | 713 | op = l_fseek(f, offset, mode[op]); |
| 707 | if (l_unlikely(op)) | 714 | if (l_unlikely(op)) |
| 708 | return luaL_fileresult(L, 0, NULL); /* error */ | 715 | return luaL_fileresult(L, 0, NULL); /* error */ |
| @@ -719,19 +726,25 @@ static int f_setvbuf (lua_State *L) { | |||
| 719 | FILE *f = tofile(L); | 726 | FILE *f = tofile(L); |
| 720 | int op = luaL_checkoption(L, 2, NULL, modenames); | 727 | int op = luaL_checkoption(L, 2, NULL, modenames); |
| 721 | lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); | 728 | lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); |
| 722 | int res = setvbuf(f, NULL, mode[op], (size_t)sz); | 729 | int res; |
| 730 | errno = 0; | ||
| 731 | res = setvbuf(f, NULL, mode[op], (size_t)sz); | ||
| 723 | return luaL_fileresult(L, res == 0, NULL); | 732 | return luaL_fileresult(L, res == 0, NULL); |
| 724 | } | 733 | } |
| 725 | 734 | ||
| 726 | 735 | ||
| 727 | 736 | ||
| 728 | static int io_flush (lua_State *L) { | 737 | static int io_flush (lua_State *L) { |
| 729 | return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); | 738 | FILE *f = getiofile(L, IO_OUTPUT); |
| 739 | errno = 0; | ||
| 740 | return luaL_fileresult(L, fflush(f) == 0, NULL); | ||
| 730 | } | 741 | } |
| 731 | 742 | ||
| 732 | 743 | ||
| 733 | static int f_flush (lua_State *L) { | 744 | static int f_flush (lua_State *L) { |
| 734 | return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL); | 745 | FILE *f = tofile(L); |
| 746 | errno = 0; | ||
| 747 | return luaL_fileresult(L, fflush(f) == 0, NULL); | ||
| 735 | } | 748 | } |
| 736 | 749 | ||
| 737 | 750 | ||
| @@ -155,6 +155,7 @@ static int os_execute (lua_State *L) { | |||
| 155 | 155 | ||
| 156 | static int os_remove (lua_State *L) { | 156 | static int os_remove (lua_State *L) { |
| 157 | const char *filename = luaL_checkstring(L, 1); | 157 | const char *filename = luaL_checkstring(L, 1); |
| 158 | errno = 0; | ||
| 158 | return luaL_fileresult(L, remove(filename) == 0, filename); | 159 | return luaL_fileresult(L, remove(filename) == 0, filename); |
| 159 | } | 160 | } |
| 160 | 161 | ||
| @@ -162,6 +163,7 @@ static int os_remove (lua_State *L) { | |||
| 162 | static int os_rename (lua_State *L) { | 163 | static int os_rename (lua_State *L) { |
| 163 | const char *fromname = luaL_checkstring(L, 1); | 164 | const char *fromname = luaL_checkstring(L, 1); |
| 164 | const char *toname = luaL_checkstring(L, 2); | 165 | const char *toname = luaL_checkstring(L, 2); |
| 166 | errno = 0; | ||
| 165 | return luaL_fileresult(L, rename(fromname, toname) == 0, NULL); | 167 | return luaL_fileresult(L, rename(fromname, toname) == 0, NULL); |
| 166 | } | 168 | } |
| 167 | 169 | ||
