diff options
| author | ame <[email protected]> | 2024-02-21 10:41:42 -0600 |
|---|---|---|
| committer | ame <[email protected]> | 2024-02-21 10:41:42 -0600 |
| commit | d6c5e8386e101a91ae6091763d00be8ebed01fb6 (patch) | |
| tree | 758da16000ef2d8450c261c1c95c9b347de59a54 /src/io.c | |
| parent | 1dc5be8e8ae8a157879de60e0f877a04faed3190 (diff) | |
fixed reading and some file stuff
Diffstat (limited to 'src/io.c')
| -rw-r--r-- | src/io.c | 25 |
1 files changed, 9 insertions, 16 deletions
@@ -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); |
