aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c21
1 files changed, 2 insertions, 19 deletions
diff --git a/liolib.c b/liolib.c
index 5881b029..b2a2fec8 100644
--- a/liolib.c
+++ b/liolib.c
@@ -246,22 +246,9 @@ static LStream *newfile (lua_State *L) {
246} 246}
247 247
248 248
249/*
250** Equivalent to 'fopen', but if it fails due to a lack of resources
251** (see 'luaL_resourcetryagain'), do an "emergency" garbage collection
252** to try to close some files and then tries to open the file again.
253*/
254static FILE *trytoopen (lua_State *L, const char *path, const char *mode) {
255 FILE *f = fopen(path, mode);
256 if (f == NULL && luaL_resourcetryagain(L)) /* resource failure? */
257 f = fopen(path, mode); /* try to open again */
258 return f;
259}
260
261
262static void opencheck (lua_State *L, const char *fname, const char *mode) { 249static void opencheck (lua_State *L, const char *fname, const char *mode) {
263 LStream *p = newfile(L); 250 LStream *p = newfile(L);
264 p->f = trytoopen(L, fname, mode); 251 p->f = fopen(fname, mode);
265 if (p->f == NULL) 252 if (p->f == NULL)
266 luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); 253 luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
267} 254}
@@ -273,7 +260,7 @@ static int io_open (lua_State *L) {
273 LStream *p = newfile(L); 260 LStream *p = newfile(L);
274 const char *md = mode; /* to traverse/check mode */ 261 const char *md = mode; /* to traverse/check mode */
275 luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); 262 luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
276 p->f = trytoopen(L, filename, mode); 263 p->f = fopen(filename, mode);
277 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; 264 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
278} 265}
279 266
@@ -292,8 +279,6 @@ static int io_popen (lua_State *L) {
292 const char *mode = luaL_optstring(L, 2, "r"); 279 const char *mode = luaL_optstring(L, 2, "r");
293 LStream *p = newprefile(L); 280 LStream *p = newprefile(L);
294 p->f = l_popen(L, filename, mode); 281 p->f = l_popen(L, filename, mode);
295 if (p->f == NULL && luaL_resourcetryagain(L)) /* resource failure? */
296 p->f = l_popen(L, filename, mode); /* try to open again */
297 p->closef = &io_pclose; 282 p->closef = &io_pclose;
298 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; 283 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
299} 284}
@@ -302,8 +287,6 @@ static int io_popen (lua_State *L) {
302static int io_tmpfile (lua_State *L) { 287static int io_tmpfile (lua_State *L) {
303 LStream *p = newfile(L); 288 LStream *p = newfile(L);
304 p->f = tmpfile(); 289 p->f = tmpfile();
305 if (p->f == NULL && luaL_resourcetryagain(L)) /* resource failure? */
306 p->f = tmpfile(); /* try to open again */
307 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; 290 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
308} 291}
309 292