diff options
Diffstat (limited to 'liolib.c')
| -rw-r--r-- | liolib.c | 34 |
1 files changed, 21 insertions, 13 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 1.103 2001/02/02 19:02:40 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.104 2001/02/06 16:01:29 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 | */ |
| @@ -272,20 +272,28 @@ static void read_file (lua_State *L, FILE *f) { | |||
| 272 | 272 | ||
| 273 | 273 | ||
| 274 | static int read_chars (lua_State *L, FILE *f, size_t n) { | 274 | static int read_chars (lua_State *L, FILE *f, size_t n) { |
| 275 | char *buffer; | 275 | if (n == 0) { /* test eof? */ |
| 276 | size_t n1; | 276 | int c = fgetc(f); |
| 277 | char statbuff[LUAL_BUFFERSIZE]; | 277 | ungetc(c, f); |
| 278 | if (n <= LUAL_BUFFERSIZE) | 278 | lua_pushlstring(L, NULL, 0); |
| 279 | buffer = statbuff; | 279 | return (c != EOF); |
| 280 | } | ||
| 280 | else { | 281 | else { |
| 281 | buffer = (char *)l_malloc(n); | 282 | char *buffer; |
| 282 | if (buffer == NULL) | 283 | size_t n1; |
| 283 | lua_error(L, "not enough memory to read a file"); | 284 | char statbuff[LUAL_BUFFERSIZE]; |
| 285 | if (n <= LUAL_BUFFERSIZE) | ||
| 286 | buffer = statbuff; | ||
| 287 | else { | ||
| 288 | buffer = (char *)l_malloc(n); | ||
| 289 | if (buffer == NULL) | ||
| 290 | lua_error(L, "not enough memory to read a file"); | ||
| 291 | } | ||
| 292 | n1 = fread(buffer, sizeof(char), n, f); | ||
| 293 | lua_pushlstring(L, buffer, n1); | ||
| 294 | if (buffer != statbuff) l_free(buffer, n); | ||
| 295 | return (n1 > 0 || n == 0); | ||
| 284 | } | 296 | } |
| 285 | n1 = fread(buffer, sizeof(char), n, f); | ||
| 286 | lua_pushlstring(L, buffer, n1); | ||
| 287 | if (buffer != statbuff) l_free(buffer, n); | ||
| 288 | return (n1 > 0 || n == 0); | ||
| 289 | } | 297 | } |
| 290 | 298 | ||
| 291 | 299 | ||
