diff options
| author | ame <[email protected]> | 2023-12-04 01:04:57 -0600 |
|---|---|---|
| committer | ame <[email protected]> | 2023-12-04 01:04:57 -0600 |
| commit | 7832bc32b4933696e7a34b180bbba46b0c98b858 (patch) | |
| tree | 054f19eb9afeb242539cdf17bbed77898220c0cc /src/io.c | |
| parent | b224d2f0b9b344a08c6c508d61f15bdf205464f8 (diff) | |
crypto stuff and (some) file io
Diffstat (limited to 'src/io.c')
| -rw-r--r-- | src/io.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/io.c b/src/io.c new file mode 100644 index 0000000..f2a5c82 --- /dev/null +++ b/src/io.c @@ -0,0 +1,35 @@ +#include "io.h" +#include "stdlib.h" +#include "stdio.h" +#include "string.h" +#include "stdint.h" +#include "unistd.h" + +int l_readfile(lua_State* L){ + size_t len; + char* a = (char*)luaL_checklstring(L, 1, &len); + + FILE *fp; + const uint64_t chunk_iter = 512; + uint64_t chunk = 512; + uint64_t count = 0; + char* out = malloc(chunk); + + fp = fopen(a, "r"); + + for(;;){ + char ch = fgetc(fp); + if(ch==EOF) break; + + if(count > chunk){ + chunk += chunk_iter; + out = realloc(out, chunk); + } + out[count] = ch; + count++; + } + lua_pushstring(L, out); + + free(out); + return 1; +}; |
