aboutsummaryrefslogtreecommitdiff
path: root/src/io.c
diff options
context:
space:
mode:
authorame <[email protected]>2024-02-21 10:41:42 -0600
committerame <[email protected]>2024-02-21 10:41:42 -0600
commitd6c5e8386e101a91ae6091763d00be8ebed01fb6 (patch)
tree758da16000ef2d8450c261c1c95c9b347de59a54 /src/io.c
parent1dc5be8e8ae8a157879de60e0f877a04faed3190 (diff)
fixed reading and some file stuff
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);