diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-08-13 16:52:13 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-08-13 16:52:13 -0300 |
commit | 2d8b099274699286eed4ebe8a45900de1b28a398 (patch) | |
tree | fb6918365bf64a0415d5fe8c287d141e6e511a48 /liolib.c | |
parent | ff4f8fe59a2d0b44606f4564d13bb4d3b23fc460 (diff) | |
download | lua-2d8b099274699286eed4ebe8a45900de1b28a398.tar.gz lua-2d8b099274699286eed4ebe8a45900de1b28a398.tar.bz2 lua-2d8b099274699286eed4ebe8a45900de1b28a398.zip |
better checking for read errors (with `ferrorĀ“)
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 13 |
1 files changed, 10 insertions, 3 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 2.55 2004/07/09 16:01:38 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.56 2004/08/09 14:35:59 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 | */ |
@@ -96,7 +96,7 @@ static int aux_close (lua_State *L) { | |||
96 | if (f == stdin || f == stdout || f == stderr) | 96 | if (f == stdin || f == stdout || f == stderr) |
97 | return 0; /* file cannot be closed */ | 97 | return 0; /* file cannot be closed */ |
98 | else { | 98 | else { |
99 | int ok = (pclose(f) != -1) || (fclose(f) == 0); | 99 | int ok = (fclose(f) == 0); |
100 | if (ok) | 100 | if (ok) |
101 | *(FILE **)lua_touserdata(L, 1) = NULL; /* mark file as closed */ | 101 | *(FILE **)lua_touserdata(L, 1) = NULL; /* mark file as closed */ |
102 | return ok; | 102 | return ok; |
@@ -293,6 +293,7 @@ static int g_read (lua_State *L, FILE *f, int first) { | |||
293 | int nargs = lua_gettop(L) - 1; | 293 | int nargs = lua_gettop(L) - 1; |
294 | int success; | 294 | int success; |
295 | int n; | 295 | int n; |
296 | clearerr(f); | ||
296 | if (nargs == 0) { /* no arguments? */ | 297 | if (nargs == 0) { /* no arguments? */ |
297 | success = read_line(L, f); | 298 | success = read_line(L, f); |
298 | n = first+1; /* to return 1 result */ | 299 | n = first+1; /* to return 1 result */ |
@@ -327,6 +328,8 @@ static int g_read (lua_State *L, FILE *f, int first) { | |||
327 | } | 328 | } |
328 | } | 329 | } |
329 | } | 330 | } |
331 | if (ferror(f)) | ||
332 | return pushresult(L, 0, NULL); | ||
330 | if (!success) { | 333 | if (!success) { |
331 | lua_pop(L, 1); /* remove last result */ | 334 | lua_pop(L, 1); /* remove last result */ |
332 | lua_pushnil(L); /* push nil instead */ | 335 | lua_pushnil(L); /* push nil instead */ |
@@ -347,9 +350,13 @@ static int f_read (lua_State *L) { | |||
347 | 350 | ||
348 | static int io_readline (lua_State *L) { | 351 | static int io_readline (lua_State *L) { |
349 | FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(2)); | 352 | FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(2)); |
353 | int sucess; | ||
350 | if (f == NULL) /* file is already closed? */ | 354 | if (f == NULL) /* file is already closed? */ |
351 | luaL_error(L, "file is already closed"); | 355 | luaL_error(L, "file is already closed"); |
352 | if (read_line(L, f)) return 1; | 356 | sucess = read_line(L, f); |
357 | if (ferror(f)) | ||
358 | luaL_error(L, "%s", strerror(errno)); | ||
359 | if (sucess) return 1; | ||
353 | else { /* EOF */ | 360 | else { /* EOF */ |
354 | if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ | 361 | if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ |
355 | lua_settop(L, 0); | 362 | lua_settop(L, 0); |