diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-08-10 10:05:16 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-08-10 10:05:16 -0300 |
| commit | 21843f022aac2c168d73c4d7ca3692739296f699 (patch) | |
| tree | 9a13a5af1d9fe3ff18c8f63d04c3773bef1938d4 /liolib.c | |
| parent | 900257e8143b29a1d623eda3ec6d9f1b2a7267ab (diff) | |
| download | lua-21843f022aac2c168d73c4d7ca3692739296f699.tar.gz lua-21843f022aac2c168d73c4d7ca3692739296f699.tar.bz2 lua-21843f022aac2c168d73c4d7ca3692739296f699.zip | |
writeto, readfrom, and closefile must return an error code when
closing a file.
Diffstat (limited to '')
| -rw-r--r-- | liolib.c | 28 |
1 files changed, 17 insertions, 11 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 1.41 1999/06/23 13:48:39 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.42 1999/07/22 19:35:50 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 | */ |
| @@ -46,11 +46,11 @@ | |||
| 46 | #ifdef POPEN | 46 | #ifdef POPEN |
| 47 | FILE *popen(); | 47 | FILE *popen(); |
| 48 | int pclose(); | 48 | int pclose(); |
| 49 | #define CLOSEFILE(f) {if (pclose(f) == -1) fclose(f);} | 49 | #define CLOSEFILE(f) ((pclose(f) == -1) ? fclose(f) : 0) |
| 50 | #else | 50 | #else |
| 51 | /* no support for popen */ | 51 | /* no support for popen */ |
| 52 | #define popen(x,y) NULL /* that is, popen always fails */ | 52 | #define popen(x,y) NULL /* that is, popen always fails */ |
| 53 | #define CLOSEFILE(f) {fclose(f);} | 53 | #define CLOSEFILE(f) (fclose(f)) |
| 54 | #endif | 54 | #endif |
| 55 | 55 | ||
| 56 | 56 | ||
| @@ -120,18 +120,20 @@ static FILE *getfileparam (char *name, int *arg) { | |||
| 120 | } | 120 | } |
| 121 | 121 | ||
| 122 | 122 | ||
| 123 | static void closefile (FILE *f) { | 123 | static int closefile (FILE *f) { |
| 124 | if (f != stdin && f != stdout) { | 124 | if (f == stdin || f == stdout) |
| 125 | return 1; | ||
| 126 | else { | ||
| 125 | int tag = gettag(); | 127 | int tag = gettag(); |
| 126 | CLOSEFILE(f); | ||
| 127 | lua_pushusertag(f, tag); | 128 | lua_pushusertag(f, tag); |
| 128 | lua_settag(CLOSEDTAG(tag)); | 129 | lua_settag(CLOSEDTAG(tag)); |
| 130 | return (CLOSEFILE(f) == 0); | ||
| 129 | } | 131 | } |
| 130 | } | 132 | } |
| 131 | 133 | ||
| 132 | 134 | ||
| 133 | static void io_close (void) { | 135 | static void io_close (void) { |
| 134 | closefile(getnonullfile(FIRSTARG)); | 136 | pushresult(closefile(getnonullfile(FIRSTARG))); |
| 135 | } | 137 | } |
| 136 | 138 | ||
| 137 | 139 | ||
| @@ -171,8 +173,10 @@ static void io_readfrom (void) { | |||
| 171 | FILE *current; | 173 | FILE *current; |
| 172 | lua_Object f = lua_getparam(FIRSTARG); | 174 | lua_Object f = lua_getparam(FIRSTARG); |
| 173 | if (f == LUA_NOOBJECT) { | 175 | if (f == LUA_NOOBJECT) { |
| 174 | closefile(getfilebyname(FINPUT)); | 176 | if (closefile(getfilebyname(FINPUT))) |
| 175 | current = stdin; | 177 | current = stdin; |
| 178 | else | ||
| 179 | current = NULL; /* to signal error */ | ||
| 176 | } | 180 | } |
| 177 | else if (lua_tag(f) == gettag()) /* deprecated option */ | 181 | else if (lua_tag(f) == gettag()) /* deprecated option */ |
| 178 | current = lua_getuserdata(f); | 182 | current = lua_getuserdata(f); |
| @@ -188,8 +192,10 @@ static void io_writeto (void) { | |||
| 188 | FILE *current; | 192 | FILE *current; |
| 189 | lua_Object f = lua_getparam(FIRSTARG); | 193 | lua_Object f = lua_getparam(FIRSTARG); |
| 190 | if (f == LUA_NOOBJECT) { | 194 | if (f == LUA_NOOBJECT) { |
| 191 | closefile(getfilebyname(FOUTPUT)); | 195 | if (closefile(getfilebyname(FOUTPUT))) |
| 192 | current = stdout; | 196 | current = stdout; |
| 197 | else | ||
| 198 | current = NULL; /* to signal error */ | ||
| 193 | } | 199 | } |
| 194 | else if (lua_tag(f) == gettag()) /* deprecated option */ | 200 | else if (lua_tag(f) == gettag()) /* deprecated option */ |
| 195 | current = lua_getuserdata(f); | 201 | current = lua_getuserdata(f); |
