diff options
| author | ame <[email protected]> | 2023-12-19 11:38:32 -0600 |
|---|---|---|
| committer | ame <[email protected]> | 2023-12-19 11:38:32 -0600 |
| commit | 697c9bdd10e845d27018d83afb7828b6c7d82605 (patch) | |
| tree | 69a999e4d39ddb4dfa3b7be37f5454f8e7d4e5a2 /src | |
| parent | 0112e2609f8aceb791656d19db568d04c586d7be (diff) | |
updated some io stuff
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.c | 1 | ||||
| -rw-r--r-- | src/config.h | 2 | ||||
| -rw-r--r-- | src/io.c | 22 |
3 files changed, 18 insertions, 7 deletions
diff --git a/src/config.c b/src/config.c index 46beca7..2fd4455 100644 --- a/src/config.c +++ b/src/config.c @@ -4,6 +4,7 @@ int _print_type = 0; int _max_depth = 2; int _start_nl_at = 3; +int _file_malloc_chunk = 512; int get_config_map(const char* key){ for(int i = 0; config_map[i].key != NULL; i++){ diff --git a/src/config.h b/src/config.h index 1d45cad..15a4342 100644 --- a/src/config.h +++ b/src/config.h @@ -4,6 +4,7 @@ extern int _print_type; extern int _max_depth; extern int _start_nl_at; +extern int _file_malloc_chunk; struct str_to_int { const char* key; @@ -11,6 +12,7 @@ struct str_to_int { }; static struct str_to_int config_map[] = { + {"file_chunksize", &_file_malloc_chunk}, {"print_type", &_print_type}, {"max_depth", &_max_depth}, {"start_nl_at", &_start_nl_at}, @@ -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); } |
