aboutsummaryrefslogtreecommitdiff
path: root/src/io.c
diff options
context:
space:
mode:
authorame <[email protected]>2023-12-19 11:38:32 -0600
committerame <[email protected]>2023-12-19 11:38:32 -0600
commit697c9bdd10e845d27018d83afb7828b6c7d82605 (patch)
tree69a999e4d39ddb4dfa3b7be37f5454f8e7d4e5a2 /src/io.c
parent0112e2609f8aceb791656d19db568d04c586d7be (diff)
updated some io stuff
Diffstat (limited to 'src/io.c')
-rw-r--r--src/io.c22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/io.c b/src/io.c
index 9b84804..70771b6 100644
--- a/src/io.c
+++ b/src/io.c
@@ -1,3 +1,4 @@
+#include <unistd.h>
#include "io.h"
#include "stdlib.h"
#include "stdio.h"
@@ -10,18 +11,25 @@ int l_readfile(lua_State* L){
char* a = (char*)luaL_checklstring(L, 1, &len);
FILE *fp;
- const uint64_t chunk_iter = 512;
- uint64_t chunk = 512;
+ const uint64_t chunk_iter = _file_malloc_chunk;
+ uint64_t chunk = chunk_iter;
uint64_t count = 0;
- char* out = malloc(chunk);
-
+
+ if(access(a, F_OK)) {
+ p_fatal("file not found");
+ }
+ if(access(a, R_OK)){
+ p_fatal("missing permissions");
+ }
+
fp = fopen(a, "r");
-
+ char* out = malloc(chunk);
+
for(;;){
- char ch = fgetc(fp);
+ char ch = fgetc(fp);
if(ch==EOF) break;
- if(count > chunk){
+ if(count >= chunk){
chunk += chunk_iter;
out = realloc(out, chunk);
}