diff options
| -rw-r--r-- | liolib.c | 30 |
1 files changed, 14 insertions, 16 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 2.100 2011/06/21 13:43:48 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.101 2011/06/27 19:42:31 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 | */ |
| @@ -60,11 +60,6 @@ typedef struct LStream { | |||
| 60 | } LStream; | 60 | } LStream; |
| 61 | 61 | ||
| 62 | 62 | ||
| 63 | static void fileerror (lua_State *L, int arg, const char *filename) { | ||
| 64 | lua_pushfstring(L, "%s: %s", filename, strerror(errno)); | ||
| 65 | luaL_argerror(L, arg, lua_tostring(L, -1)); | ||
| 66 | } | ||
| 67 | |||
| 68 | 63 | ||
| 69 | #define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE)) | 64 | #define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE)) |
| 70 | 65 | ||
| @@ -159,6 +154,16 @@ static LStream *newfile (lua_State *L) { | |||
| 159 | } | 154 | } |
| 160 | 155 | ||
| 161 | 156 | ||
| 157 | static void opencheck (lua_State *L, const char *fname, const char *mode) { | ||
| 158 | LStream *p = newfile(L); | ||
| 159 | p->f = fopen(fname, mode); | ||
| 160 | if (p->f == NULL) { | ||
| 161 | lua_pushfstring(L, "%s: %s", fname, strerror(errno)); | ||
| 162 | luaL_argerror(L, 1, lua_tostring(L, -1)); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 162 | static int io_open (lua_State *L) { | 167 | static int io_open (lua_State *L) { |
| 163 | const char *filename = luaL_checkstring(L, 1); | 168 | const char *filename = luaL_checkstring(L, 1); |
| 164 | const char *mode = luaL_optstring(L, 2, "r"); | 169 | const char *mode = luaL_optstring(L, 2, "r"); |
| @@ -215,12 +220,8 @@ static FILE *getiofile (lua_State *L, const char *findex) { | |||
| 215 | static int g_iofile (lua_State *L, const char *f, const char *mode) { | 220 | static int g_iofile (lua_State *L, const char *f, const char *mode) { |
| 216 | if (!lua_isnoneornil(L, 1)) { | 221 | if (!lua_isnoneornil(L, 1)) { |
| 217 | const char *filename = lua_tostring(L, 1); | 222 | const char *filename = lua_tostring(L, 1); |
| 218 | if (filename) { | 223 | if (filename) |
| 219 | LStream *p = newfile(L); | 224 | opencheck(L, filename, mode); |
| 220 | p->f = fopen(filename, mode); | ||
| 221 | if (p->f == NULL) | ||
| 222 | fileerror(L, 1, filename); | ||
| 223 | } | ||
| 224 | else { | 225 | else { |
| 225 | tofile(L); /* check that it's a valid file handle */ | 226 | tofile(L); /* check that it's a valid file handle */ |
| 226 | lua_pushvalue(L, 1); | 227 | lua_pushvalue(L, 1); |
| @@ -277,10 +278,7 @@ static int io_lines (lua_State *L) { | |||
| 277 | } | 278 | } |
| 278 | else { /* open a new file */ | 279 | else { /* open a new file */ |
| 279 | const char *filename = luaL_checkstring(L, 1); | 280 | const char *filename = luaL_checkstring(L, 1); |
| 280 | LStream *p = newfile(L); | 281 | opencheck(L, filename, "r"); |
| 281 | p->f = fopen(filename, "r"); | ||
| 282 | if (p->f == NULL) | ||
| 283 | fileerror(L, 1, filename); | ||
| 284 | lua_replace(L, 1); /* put file at index 1 */ | 282 | lua_replace(L, 1); /* put file at index 1 */ |
| 285 | toclose = 1; /* close it after iteration */ | 283 | toclose = 1; /* close it after iteration */ |
| 286 | } | 284 | } |
