From 2715243f86a4afe8e7e1f512899e96e5f9a8bf2a Mon Sep 17 00:00:00 2001 From: ame Date: Wed, 21 Feb 2024 10:41:42 -0600 Subject: fixed reading and some file stuff --- src/io.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) (limited to 'src/io.c') diff --git a/src/io.c b/src/io.c index 674c081..cc33a71 100644 --- a/src/io.c +++ b/src/io.c @@ -23,22 +23,15 @@ int l_readfile(lua_State* L){ p_fatal("missing permissions"); } - fp = fopen(a, "r"); - char* out = malloc(chunk); - - for(;;){ - char ch = fgetc(fp); - if(ch==EOF) break; - - if(count >= chunk){ - chunk += chunk_iter; - out = realloc(out, chunk); - } - out[count] = ch; - count++; - } - out[count] = '\0'; - lua_pushstring(L, out); + fp = fopen(a, "rb"); + fseek(fp, 0L, SEEK_END); + size_t sz = ftell(fp); + fseek(fp, 0L, SEEK_SET); + char* out = calloc(sz + 1, sizeof * out); + + fread(out, sizeof * out, sz, fp); + + lua_pushlstring(L, out, sz); fclose(fp); free(out); -- cgit v1.2.3