aboutsummaryrefslogtreecommitdiff
path: root/src/io.c
diff options
context:
space:
mode:
authorame <[email protected]>2023-12-04 01:04:57 -0600
committerame <[email protected]>2023-12-04 01:04:57 -0600
commit0cb307c50930d9aba2b1a6af46c1da75943778d3 (patch)
tree054f19eb9afeb242539cdf17bbed77898220c0cc /src/io.c
parent7eb07ccdca178abffe5ffccf686b4f818db3601d (diff)
crypto stuff and (some) file io
Diffstat (limited to 'src/io.c')
-rw-r--r--src/io.c35
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;
+};