From 21843f022aac2c168d73c4d7ca3692739296f699 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 10 Aug 1999 10:05:16 -0300 Subject: writeto, readfrom, and closefile must return an error code when closing a file. --- liolib.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'liolib.c') diff --git a/liolib.c b/liolib.c index 2adc399b..27f0afbe 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 1.41 1999/06/23 13:48:39 roberto Exp roberto $ +** $Id: liolib.c,v 1.42 1999/07/22 19:35:50 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -46,11 +46,11 @@ #ifdef POPEN FILE *popen(); int pclose(); -#define CLOSEFILE(f) {if (pclose(f) == -1) fclose(f);} +#define CLOSEFILE(f) ((pclose(f) == -1) ? fclose(f) : 0) #else /* no support for popen */ #define popen(x,y) NULL /* that is, popen always fails */ -#define CLOSEFILE(f) {fclose(f);} +#define CLOSEFILE(f) (fclose(f)) #endif @@ -120,18 +120,20 @@ static FILE *getfileparam (char *name, int *arg) { } -static void closefile (FILE *f) { - if (f != stdin && f != stdout) { +static int closefile (FILE *f) { + if (f == stdin || f == stdout) + return 1; + else { int tag = gettag(); - CLOSEFILE(f); lua_pushusertag(f, tag); lua_settag(CLOSEDTAG(tag)); + return (CLOSEFILE(f) == 0); } } static void io_close (void) { - closefile(getnonullfile(FIRSTARG)); + pushresult(closefile(getnonullfile(FIRSTARG))); } @@ -171,8 +173,10 @@ static void io_readfrom (void) { FILE *current; lua_Object f = lua_getparam(FIRSTARG); if (f == LUA_NOOBJECT) { - closefile(getfilebyname(FINPUT)); - current = stdin; + if (closefile(getfilebyname(FINPUT))) + current = stdin; + else + current = NULL; /* to signal error */ } else if (lua_tag(f) == gettag()) /* deprecated option */ current = lua_getuserdata(f); @@ -188,8 +192,10 @@ static void io_writeto (void) { FILE *current; lua_Object f = lua_getparam(FIRSTARG); if (f == LUA_NOOBJECT) { - closefile(getfilebyname(FOUTPUT)); - current = stdout; + if (closefile(getfilebyname(FOUTPUT))) + current = stdout; + else + current = NULL; /* to signal error */ } else if (lua_tag(f) == gettag()) /* deprecated option */ current = lua_getuserdata(f); -- cgit v1.2.3-55-g6feb