mirror of
https://github.com/lua/lua
synced 2024-12-24 03:16:50 +03:00
<read(0)> tests for EOF
This commit is contained in:
parent
6548bf7462
commit
e70e6a3b7a
36
liolib.c
36
liolib.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: liolib.c,v 1.103 2001/02/02 19:02:40 roberto Exp roberto $
|
||||
** $Id: liolib.c,v 1.104 2001/02/06 16:01:29 roberto Exp roberto $
|
||||
** Standard I/O (and system) library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -272,20 +272,28 @@ static void read_file (lua_State *L, FILE *f) {
|
||||
|
||||
|
||||
static int read_chars (lua_State *L, FILE *f, size_t n) {
|
||||
char *buffer;
|
||||
size_t n1;
|
||||
char statbuff[LUAL_BUFFERSIZE];
|
||||
if (n <= LUAL_BUFFERSIZE)
|
||||
buffer = statbuff;
|
||||
else {
|
||||
buffer = (char *)l_malloc(n);
|
||||
if (buffer == NULL)
|
||||
lua_error(L, "not enough memory to read a file");
|
||||
if (n == 0) { /* test eof? */
|
||||
int c = fgetc(f);
|
||||
ungetc(c, f);
|
||||
lua_pushlstring(L, NULL, 0);
|
||||
return (c != EOF);
|
||||
}
|
||||
else {
|
||||
char *buffer;
|
||||
size_t n1;
|
||||
char statbuff[LUAL_BUFFERSIZE];
|
||||
if (n <= LUAL_BUFFERSIZE)
|
||||
buffer = statbuff;
|
||||
else {
|
||||
buffer = (char *)l_malloc(n);
|
||||
if (buffer == NULL)
|
||||
lua_error(L, "not enough memory to read a file");
|
||||
}
|
||||
n1 = fread(buffer, sizeof(char), n, f);
|
||||
lua_pushlstring(L, buffer, n1);
|
||||
if (buffer != statbuff) l_free(buffer, n);
|
||||
return (n1 > 0 || n == 0);
|
||||
}
|
||||
n1 = fread(buffer, sizeof(char), n, f);
|
||||
lua_pushlstring(L, buffer, n1);
|
||||
if (buffer != statbuff) l_free(buffer, n);
|
||||
return (n1 > 0 || n == 0);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user