aboutsummaryrefslogtreecommitdiff
path: root/src/io.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/io.c')
-rw-r--r--src/io.c25
1 files changed, 9 insertions, 16 deletions
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);